ebpf_process.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include <sys/time.h>
  3. #include <sys/resource.h>
  4. #include "ebpf_process.h"
  5. // callback required by eval()
  6. int health_variable_lookup(const char *variable, uint32_t hash, struct rrdcalc *rc, calculated_number *result) {
  7. (void)variable;
  8. (void)hash;
  9. (void)rc;
  10. (void)result;
  11. return 0;
  12. };
  13. void send_statistics( const char *action, const char *action_result, const char *action_data) {
  14. (void) action;
  15. (void) action_result;
  16. (void) action_data;
  17. return;
  18. }
  19. // callbacks required by popen()
  20. void signals_block(void) {};
  21. void signals_unblock(void) {};
  22. void signals_reset(void) {};
  23. // required by get_system_cpus()
  24. char *netdata_configured_host_prefix = "";
  25. // callback required by fatal()
  26. void netdata_cleanup_and_exit(int ret) {
  27. exit(ret);
  28. }
  29. // ----------------------------------------------------------------------
  30. //Netdata eBPF library
  31. void *libnetdata = NULL;
  32. int (*load_bpf_file)(char *, int) = NULL;
  33. int (*set_bpf_perf_event)(int, int);
  34. int (*perf_event_unmap)(struct perf_event_mmap_page *, size_t);
  35. int (*perf_event_mmap_header)(int, struct perf_event_mmap_page **, int);
  36. void (*netdata_perf_loop_multi)(int *, struct perf_event_mmap_page **, int, int *, int (*nsb)(void *, int), int);
  37. int *map_fd = NULL;
  38. //Perf event variables
  39. static int pmu_fd[NETDATA_MAX_PROCESSOR];
  40. static struct perf_event_mmap_page *headers[NETDATA_MAX_PROCESSOR];
  41. int page_cnt = 8;
  42. //Libbpf (It is necessary to have at least kernel 4.10)
  43. int (*bpf_map_lookup_elem)(int, const void *, void *);
  44. static char *plugin_dir = PLUGINS_DIR;
  45. static char *user_config_dir = CONFIG_DIR;
  46. static char *stock_config_dir = LIBCONFIG_DIR;
  47. static char *netdata_configured_log_dir = LOG_DIR;
  48. FILE *developer_log = NULL;
  49. //Global vectors
  50. netdata_syscall_stat_t *aggregated_data = NULL;
  51. netdata_publish_syscall_t *publish_aggregated = NULL;
  52. static int update_every = 1;
  53. static int thread_finished = 0;
  54. static int close_plugin = 0;
  55. static int mode = 2;
  56. static int debug_log = 0;
  57. static int use_stdout = 0;
  58. struct config collector_config;
  59. static int mykernel = 0;
  60. static int nprocs;
  61. uint32_t *hash_values;
  62. pthread_mutex_t lock;
  63. static char *dimension_names[NETDATA_MAX_MONITOR_VECTOR] = { "open", "close", "delete", "read", "write", "process", "task", "process", "thread" };
  64. static char *id_names[NETDATA_MAX_MONITOR_VECTOR] = { "do_sys_open", "__close_fd", "vfs_unlink", "vfs_read", "vfs_write", "do_exit", "release_task", "_do_fork", "sys_clone" };
  65. static char *status[] = { "process", "zombie" };
  66. int event_pid = 0;
  67. netdata_ebpf_events_t collector_events[] = {
  68. { .type = 'r', .name = "vfs_write" },
  69. { .type = 'r', .name = "vfs_writev" },
  70. { .type = 'r', .name = "vfs_read" },
  71. { .type = 'r', .name = "vfs_readv" },
  72. { .type = 'r', .name = "do_sys_open" },
  73. { .type = 'r', .name = "vfs_unlink" },
  74. { .type = 'p', .name = "do_exit" },
  75. { .type = 'p', .name = "release_task" },
  76. { .type = 'r', .name = "_do_fork" },
  77. { .type = 'r', .name = "__close_fd" },
  78. { .type = 'r', .name = "__x64_sys_clone" },
  79. { .type = 0, .name = NULL }
  80. };
  81. void open_developer_log() {
  82. char filename[FILENAME_MAX+1];
  83. int tot = sprintf(filename, "%s/%s", netdata_configured_log_dir, NETDATA_DEVELOPER_LOG_FILE);
  84. if(tot > 0)
  85. developer_log = fopen(filename, "a");
  86. }
  87. static int unmap_memory() {
  88. int i;
  89. int size = (int)sysconf(_SC_PAGESIZE)*(page_cnt + 1);
  90. for ( i = 0 ; i < nprocs ; i++ ) {
  91. if (perf_event_unmap(headers[i], size) < 0) {
  92. fprintf(stderr,"[EBPF PROCESS] CANNOT unmap headers.\n");
  93. return -1;
  94. }
  95. close(pmu_fd[i]);
  96. }
  97. return 0;
  98. }
  99. static void int_exit(int sig)
  100. {
  101. close_plugin = 1;
  102. //When both threads were not finished case I try to go in front this address, the collector will crash
  103. if (!thread_finished) {
  104. return;
  105. }
  106. if (aggregated_data) {
  107. free(aggregated_data);
  108. aggregated_data = NULL;
  109. }
  110. if (publish_aggregated) {
  111. free(publish_aggregated);
  112. publish_aggregated = NULL;
  113. }
  114. if(mode == 1 && debug_log) {
  115. unmap_memory();
  116. }
  117. if (libnetdata) {
  118. dlclose(libnetdata);
  119. libnetdata = NULL;
  120. }
  121. if (developer_log) {
  122. fclose(developer_log);
  123. developer_log = NULL;
  124. }
  125. if (hash_values) {
  126. freez(hash_values);
  127. }
  128. if (event_pid) {
  129. int ret = fork();
  130. if (ret < 0) //error
  131. error("[EBPF PROCESS] Cannot fork(), so I won't be able to clean %skprobe_events", NETDATA_DEBUGFS);
  132. else if (!ret) { //child
  133. int i;
  134. for ( i=getdtablesize(); i>=0; --i)
  135. close(i);
  136. int fd = open("/dev/null",O_RDWR, 0);
  137. if (fd != -1) {
  138. dup2 (fd, STDIN_FILENO);
  139. dup2 (fd, STDOUT_FILENO);
  140. dup2 (fd, STDERR_FILENO);
  141. }
  142. if (fd > 2)
  143. close (fd);
  144. int sid = setsid();
  145. if(sid >= 0) {
  146. sleep(1);
  147. if(debug_log) {
  148. open_developer_log();
  149. }
  150. debug(D_EXIT, "Wait for father %d die", event_pid);
  151. clean_kprobe_events(developer_log, event_pid, collector_events);
  152. } else {
  153. error("Cannot become session id leader, so I won't try to clean kprobe_events.\n");
  154. }
  155. } else { //parent
  156. exit(0);
  157. }
  158. if (developer_log) {
  159. fclose(developer_log);
  160. developer_log = NULL;
  161. }
  162. }
  163. exit(sig);
  164. }
  165. static inline void netdata_write_chart_cmd(char *type
  166. , char *id
  167. , char *axis
  168. , char *web
  169. , int order)
  170. {
  171. printf("CHART %s.%s '' '' '%s' '%s' '' line %d 1 ''\n"
  172. , type
  173. , id
  174. , axis
  175. , web
  176. , order);
  177. }
  178. static void netdata_write_global_dimension(char *d, char *n)
  179. {
  180. printf("DIMENSION %s %s absolute 1 1\n", d, n);
  181. }
  182. static void netdata_create_global_dimension(void *ptr, int end)
  183. {
  184. netdata_publish_syscall_t *move = ptr;
  185. int i = 0;
  186. while (move && i < end) {
  187. netdata_write_global_dimension(move->name, move->dimension);
  188. move = move->next;
  189. i++;
  190. }
  191. }
  192. static inline void netdata_create_chart(char *family
  193. , char *name
  194. , char *axis
  195. , char *web
  196. , int order
  197. , void (*ncd)(void *, int)
  198. , void *move
  199. , int end)
  200. {
  201. netdata_write_chart_cmd(family, name, axis, web, order);
  202. ncd(move, end);
  203. }
  204. static void netdata_create_io_chart(char *family, char *name, char *axis, char *web, int order) {
  205. printf("CHART %s.%s '' '' '%s' '%s' '' line %d 1 ''\n"
  206. , family
  207. , name
  208. , axis
  209. , web
  210. , order);
  211. printf("DIMENSION %s %s absolute 1 1\n", id_names[3], NETDATA_VFS_DIM_OUT_FILE_BYTES);
  212. printf("DIMENSION %s %s absolute 1 1\n", id_names[4], NETDATA_VFS_DIM_IN_FILE_BYTES);
  213. }
  214. static void netdata_process_status_chart(char *family, char *name, char *axis, char *web, int order) {
  215. printf("CHART %s.%s '' '' '%s' '%s' '' line %d 1 ''\n"
  216. , family
  217. , name
  218. , axis
  219. , web
  220. , order);
  221. printf("DIMENSION %s '' absolute 1 1\n", status[0]);
  222. printf("DIMENSION %s '' absolute 1 1\n", status[1]);
  223. }
  224. static void netdata_global_charts_create() {
  225. netdata_create_chart(NETDATA_EBPF_FAMILY
  226. , NETDATA_FILE_OPEN_CLOSE_COUNT
  227. , "Calls"
  228. , NETDATA_FILE_GROUP
  229. , 970
  230. , netdata_create_global_dimension
  231. , publish_aggregated
  232. , 2);
  233. if(mode < 2) {
  234. netdata_create_chart(NETDATA_EBPF_FAMILY
  235. , NETDATA_FILE_OPEN_ERR_COUNT
  236. , "Calls"
  237. , NETDATA_FILE_GROUP
  238. , 971
  239. , netdata_create_global_dimension
  240. , publish_aggregated
  241. , 2);
  242. }
  243. netdata_create_chart(NETDATA_EBPF_FAMILY
  244. , NETDATA_VFS_FILE_CLEAN_COUNT
  245. , "Calls"
  246. , NETDATA_VFS_GROUP
  247. , 972
  248. , netdata_create_global_dimension
  249. , &publish_aggregated[NETDATA_DEL_START]
  250. , 1);
  251. netdata_create_chart(NETDATA_EBPF_FAMILY
  252. , NETDATA_VFS_FILE_IO_COUNT
  253. , "Calls"
  254. , NETDATA_VFS_GROUP
  255. , 973
  256. , netdata_create_global_dimension
  257. , &publish_aggregated[NETDATA_IN_START_BYTE]
  258. , 2);
  259. if(mode < 2) {
  260. netdata_create_io_chart(NETDATA_EBPF_FAMILY
  261. , NETDATA_VFS_IO_FILE_BYTES
  262. , "bytes/s"
  263. , NETDATA_VFS_GROUP
  264. , 974);
  265. netdata_create_chart(NETDATA_EBPF_FAMILY
  266. , NETDATA_VFS_FILE_ERR_COUNT
  267. , "Calls"
  268. , NETDATA_VFS_GROUP
  269. , 975
  270. , netdata_create_global_dimension
  271. , &publish_aggregated[2]
  272. , NETDATA_VFS_ERRORS);
  273. }
  274. netdata_create_chart(NETDATA_EBPF_FAMILY
  275. , NETDATA_PROCESS_SYSCALL
  276. , "Calls"
  277. , NETDATA_PROCESS_GROUP
  278. , 976
  279. , netdata_create_global_dimension
  280. , &publish_aggregated[NETDATA_PROCESS_START]
  281. , 2);
  282. netdata_create_chart(NETDATA_EBPF_FAMILY
  283. , NETDATA_EXIT_SYSCALL
  284. , "Calls"
  285. , NETDATA_PROCESS_GROUP
  286. , 977
  287. , netdata_create_global_dimension
  288. , &publish_aggregated[NETDATA_EXIT_START]
  289. , 2);
  290. netdata_process_status_chart(NETDATA_EBPF_FAMILY
  291. , NETDATA_PROCESS_STATUS_NAME
  292. , "Total"
  293. , NETDATA_PROCESS_GROUP
  294. , 978);
  295. if(mode < 2) {
  296. netdata_create_chart(NETDATA_EBPF_FAMILY
  297. , NETDATA_PROCESS_ERROR_NAME
  298. , "Calls"
  299. , NETDATA_PROCESS_GROUP
  300. , 979
  301. , netdata_create_global_dimension
  302. , &publish_aggregated[NETDATA_PROCESS_START]
  303. , 2);
  304. }
  305. }
  306. static void netdata_create_charts() {
  307. netdata_global_charts_create();
  308. }
  309. static void netdata_update_publish(netdata_publish_syscall_t *publish
  310. , netdata_publish_vfs_common_t *pvc
  311. , netdata_syscall_stat_t *input) {
  312. netdata_publish_syscall_t *move = publish;
  313. while(move) {
  314. if(input->call != move->pcall) {
  315. //This condition happens to avoid initial values with dimensions higher than normal values.
  316. if(move->pcall) {
  317. move->ncall = (input->call > move->pcall)?input->call - move->pcall: move->pcall - input->call;
  318. move->nbyte = (input->bytes > move->pbyte)?input->bytes - move->pbyte: move->pbyte - input->bytes;
  319. move->nerr = (input->ecall > move->nerr)?input->ecall - move->perr: move->perr - input->ecall;
  320. } else {
  321. move->ncall = 0;
  322. move->nbyte = 0;
  323. move->nerr = 0;
  324. }
  325. move->pcall = input->call;
  326. move->pbyte = input->bytes;
  327. move->perr = input->ecall;
  328. } else {
  329. move->ncall = 0;
  330. move->nbyte = 0;
  331. move->nerr = 0;
  332. }
  333. input = input->next;
  334. move = move->next;
  335. }
  336. pvc->write = -((long)publish[2].nbyte);
  337. pvc->read = (long)publish[3].nbyte;
  338. pvc->running = (long)publish[7].ncall - (long)publish[8].ncall;
  339. publish[6].ncall = -publish[6].ncall; // release
  340. pvc->zombie = (long)publish[5].ncall + (long)publish[6].ncall;
  341. }
  342. static inline void write_begin_chart(char *family, char *name)
  343. {
  344. int ret = printf( "BEGIN %s.%s\n"
  345. , family
  346. , name);
  347. (void)ret;
  348. }
  349. static inline void write_chart_dimension(char *dim, long long value)
  350. {
  351. int ret = printf("SET %s = %lld\n", dim, value);
  352. (void)ret;
  353. }
  354. static void write_global_count_chart(char *name, char *family, netdata_publish_syscall_t *move, int end) {
  355. write_begin_chart(family, name);
  356. int i = 0;
  357. while (move && i < end) {
  358. write_chart_dimension(move->name, move->ncall);
  359. move = move->next;
  360. i++;
  361. }
  362. printf("END\n");
  363. }
  364. static void write_global_err_chart(char *name, char *family, netdata_publish_syscall_t *move, int end) {
  365. write_begin_chart(family, name);
  366. int i = 0;
  367. while (move && i < end) {
  368. write_chart_dimension(move->name, move->nerr);
  369. move = move->next;
  370. i++;
  371. }
  372. printf("END\n");
  373. }
  374. static void write_io_chart(char *family, netdata_publish_vfs_common_t *pvc) {
  375. write_begin_chart(family, NETDATA_VFS_IO_FILE_BYTES);
  376. write_chart_dimension(id_names[3], (long long) pvc->write);
  377. write_chart_dimension(id_names[4], (long long) pvc->read);
  378. printf("END\n");
  379. }
  380. static void write_status_chart(char *family, netdata_publish_vfs_common_t *pvc) {
  381. write_begin_chart(family, NETDATA_PROCESS_STATUS_NAME);
  382. write_chart_dimension(status[0], (long long) pvc->running);
  383. write_chart_dimension(status[1], (long long) pvc->zombie);
  384. printf("END\n");
  385. }
  386. static void netdata_publish_data() {
  387. netdata_publish_vfs_common_t pvc;
  388. netdata_update_publish(publish_aggregated, &pvc, aggregated_data);
  389. write_global_count_chart(NETDATA_FILE_OPEN_CLOSE_COUNT, NETDATA_EBPF_FAMILY, publish_aggregated, 2);
  390. write_global_count_chart(NETDATA_VFS_FILE_CLEAN_COUNT, NETDATA_EBPF_FAMILY, &publish_aggregated[NETDATA_DEL_START], 1);
  391. write_global_count_chart(NETDATA_VFS_FILE_IO_COUNT, NETDATA_EBPF_FAMILY, &publish_aggregated[NETDATA_IN_START_BYTE], 2);
  392. write_global_count_chart(NETDATA_EXIT_SYSCALL, NETDATA_EBPF_FAMILY, &publish_aggregated[NETDATA_EXIT_START], 2);
  393. write_global_count_chart(NETDATA_PROCESS_SYSCALL, NETDATA_EBPF_FAMILY, &publish_aggregated[NETDATA_PROCESS_START], 2);
  394. write_status_chart(NETDATA_EBPF_FAMILY, &pvc);
  395. if(mode < 2) {
  396. write_global_err_chart(NETDATA_FILE_OPEN_ERR_COUNT, NETDATA_EBPF_FAMILY, publish_aggregated, 2);
  397. write_global_err_chart(NETDATA_VFS_FILE_ERR_COUNT, NETDATA_EBPF_FAMILY, &publish_aggregated[2], NETDATA_VFS_ERRORS);
  398. write_global_err_chart(NETDATA_PROCESS_ERROR_NAME, NETDATA_EBPF_FAMILY, &publish_aggregated[NETDATA_PROCESS_START], 2);
  399. write_io_chart(NETDATA_EBPF_FAMILY, &pvc);
  400. }
  401. }
  402. void *process_publisher(void *ptr)
  403. {
  404. (void)ptr;
  405. netdata_create_charts();
  406. usec_t step = update_every * USEC_PER_SEC;
  407. heartbeat_t hb;
  408. heartbeat_init(&hb);
  409. while(!close_plugin) {
  410. usec_t dt = heartbeat_next(&hb, step);
  411. (void)dt;
  412. pthread_mutex_lock(&lock);
  413. netdata_publish_data();
  414. pthread_mutex_unlock(&lock);
  415. fflush(stdout);
  416. }
  417. return NULL;
  418. }
  419. static void move_from_kernel2user_global() {
  420. uint32_t idx;
  421. uint32_t res[NETDATA_GLOBAL_VECTOR];
  422. uint32_t *val = hash_values;
  423. for (idx = 0; idx < NETDATA_GLOBAL_VECTOR; idx++) {
  424. if(!bpf_map_lookup_elem(map_fd[1], &idx, val)) {
  425. uint32_t total = 0;
  426. int i;
  427. int end = (mykernel < 265984)?1:nprocs;
  428. for (i = 0; i < end; i++)
  429. total += val[i];
  430. res[idx] = total;
  431. } else {
  432. res[idx] = 0;
  433. }
  434. }
  435. aggregated_data[0].call = res[0]; //open
  436. aggregated_data[1].call = res[14]; //close
  437. aggregated_data[2].call = res[8]; //unlink
  438. aggregated_data[3].call = res[5] + res[21]; //read + readv
  439. aggregated_data[4].call = res[2] + res[18]; //write + writev
  440. aggregated_data[5].call = res[10]; //exit
  441. aggregated_data[6].call = res[11]; //release
  442. aggregated_data[7].call = res[12]; //fork
  443. aggregated_data[8].call = res[16]; //thread
  444. aggregated_data[0].ecall = res[1]; //open
  445. aggregated_data[1].ecall = res[15]; //close
  446. aggregated_data[2].ecall = res[9]; //unlink
  447. aggregated_data[3].ecall = res[6] + res[22]; //read + readv
  448. aggregated_data[4].ecall = res[3] + res[19]; //write + writev
  449. aggregated_data[7].ecall = res[13]; //fork
  450. aggregated_data[8].ecall = res[17]; //thread
  451. aggregated_data[2].bytes = (uint64_t)res[4] + (uint64_t)res[20]; //write + writev
  452. aggregated_data[3].bytes = (uint64_t)res[7] + (uint64_t)res[23];//read + readv
  453. }
  454. static void move_from_kernel2user()
  455. {
  456. move_from_kernel2user_global();
  457. }
  458. void *process_collector(void *ptr)
  459. {
  460. (void)ptr;
  461. usec_t step = 778879ULL;
  462. heartbeat_t hb;
  463. heartbeat_init(&hb);
  464. while(!close_plugin) {
  465. usec_t dt = heartbeat_next(&hb, step);
  466. (void)dt;
  467. pthread_mutex_lock(&lock);
  468. move_from_kernel2user();
  469. pthread_mutex_unlock(&lock);
  470. }
  471. return NULL;
  472. }
  473. static int netdata_store_bpf(void *data, int size) {
  474. (void)size;
  475. if (close_plugin)
  476. return 0;
  477. if(!debug_log)
  478. return -2; //LIBBPF_PERF_EVENT_CONT;
  479. netdata_error_report_t *e = data;
  480. fprintf(developer_log
  481. ,"%llu %s %u: %s, %d\n"
  482. , now_realtime_usec() ,e->comm, e->pid, dimension_names[e->type], e->err);
  483. fflush(developer_log);
  484. return -2; //LIBBPF_PERF_EVENT_CONT;
  485. }
  486. void *process_log(void *ptr)
  487. {
  488. (void) ptr;
  489. if (mode == 1 && debug_log) {
  490. netdata_perf_loop_multi(pmu_fd, headers, nprocs, &close_plugin, netdata_store_bpf, page_cnt);
  491. }
  492. return NULL;
  493. }
  494. void set_global_labels() {
  495. int i;
  496. netdata_syscall_stat_t *is = aggregated_data;
  497. netdata_syscall_stat_t *prev = NULL;
  498. netdata_publish_syscall_t *pio = publish_aggregated;
  499. netdata_publish_syscall_t *publish_prev = NULL;
  500. for (i = 0; i < NETDATA_MAX_MONITOR_VECTOR; i++) {
  501. if(prev) {
  502. prev->next = &is[i];
  503. }
  504. prev = &is[i];
  505. pio[i].dimension = dimension_names[i];
  506. pio[i].name = id_names[i];
  507. if(publish_prev) {
  508. publish_prev->next = &pio[i];
  509. }
  510. publish_prev = &pio[i];
  511. }
  512. }
  513. int allocate_global_vectors() {
  514. aggregated_data = callocz(NETDATA_MAX_MONITOR_VECTOR, sizeof(netdata_syscall_stat_t));
  515. if(!aggregated_data) {
  516. return -1;
  517. }
  518. publish_aggregated = callocz(NETDATA_MAX_MONITOR_VECTOR, sizeof(netdata_publish_syscall_t));
  519. if(!publish_aggregated) {
  520. return -1;
  521. }
  522. hash_values = callocz(nprocs, sizeof(uint32_t));
  523. if(!hash_values) {
  524. return -1;
  525. }
  526. return 0;
  527. }
  528. static void build_complete_path(char *out, size_t length,char *path, char *filename) {
  529. if(path){
  530. snprintf(out, length, "%s/%s", path, filename);
  531. } else {
  532. snprintf(out, length, "%s", filename);
  533. }
  534. }
  535. static int map_memory() {
  536. int i;
  537. for (i = 0; i < nprocs; i++) {
  538. pmu_fd[i] = set_bpf_perf_event(i, 2);
  539. if (perf_event_mmap_header(pmu_fd[i], &headers[i], page_cnt) < 0) {
  540. return -1;
  541. }
  542. }
  543. return 0;
  544. }
  545. static int ebpf_load_libraries()
  546. {
  547. char *err = NULL;
  548. char lpath[4096];
  549. build_complete_path(lpath, 4096, plugin_dir, "libnetdata_ebpf.so");
  550. libnetdata = dlopen(lpath, RTLD_LAZY);
  551. if (!libnetdata) {
  552. error("[EBPF_PROCESS] Cannot load %s.", lpath);
  553. return -1;
  554. } else {
  555. load_bpf_file = dlsym(libnetdata, "load_bpf_file");
  556. if ((err = dlerror()) != NULL) {
  557. error("[EBPF_PROCESS] Cannot find load_bpf_file: %s", err);
  558. return -1;
  559. }
  560. map_fd = dlsym(libnetdata, "map_fd");
  561. if ((err = dlerror()) != NULL) {
  562. error("[EBPF_PROCESS] Cannot find map_fd: %s", err);
  563. return -1;
  564. }
  565. bpf_map_lookup_elem = dlsym(libnetdata, "bpf_map_lookup_elem");
  566. if ((err = dlerror()) != NULL) {
  567. error("[EBPF_PROCESS] Cannot find bpf_map_lookup_elem: %s", err);
  568. return -1;
  569. }
  570. if(mode == 1) {
  571. set_bpf_perf_event = dlsym(libnetdata, "set_bpf_perf_event");
  572. if ((err = dlerror()) != NULL) {
  573. error("[EBPF_PROCESS] Cannot find set_bpf_perf_event: %s", err);
  574. return -1;
  575. }
  576. perf_event_unmap = dlsym(libnetdata, "perf_event_unmap");
  577. if ((err = dlerror()) != NULL) {
  578. error("[EBPF_PROCESS] Cannot find perf_event_unmap: %s", err);
  579. return -1;
  580. }
  581. perf_event_mmap_header = dlsym(libnetdata, "perf_event_mmap_header");
  582. if ((err = dlerror()) != NULL) {
  583. error("[EBPF_PROCESS] Cannot find perf_event_mmap_header: %s", err);
  584. return -1;
  585. }
  586. netdata_perf_loop_multi = dlsym(libnetdata, "netdata_perf_loop_multi");
  587. if ((err = dlerror()) != NULL) {
  588. error("[EBPF_PROCESS] Cannot find netdata_perf_loop_multi: %s", err);
  589. return -1;
  590. }
  591. }
  592. }
  593. return 0;
  594. }
  595. char *select_file() {
  596. if(!mode)
  597. return "rnetdata_ebpf_process.o";
  598. if(mode == 1)
  599. return "dnetdata_ebpf_process.o";
  600. return "pnetdata_ebpf_process.o";
  601. }
  602. int process_load_ebpf()
  603. {
  604. char lpath[4096];
  605. char *name = select_file();
  606. build_complete_path(lpath, 4096, plugin_dir, name);
  607. event_pid = getpid();
  608. if (load_bpf_file(lpath, event_pid) ) {
  609. error("[EBPF_PROCESS] Cannot load program: %s", lpath);
  610. return -1;
  611. } else {
  612. info("[EBPF PROCESS]: The eBPF program %s was loaded with success.", name);
  613. }
  614. return 0;
  615. }
  616. void set_global_variables() {
  617. //Get environment variables
  618. plugin_dir = getenv("NETDATA_PLUGINS_DIR");
  619. if(!plugin_dir)
  620. plugin_dir = PLUGINS_DIR;
  621. user_config_dir = getenv("NETDATA_USER_CONFIG_DIR");
  622. if(!user_config_dir)
  623. user_config_dir = CONFIG_DIR;
  624. stock_config_dir = getenv("NETDATA_STOCK_CONFIG_DIR");
  625. if(!stock_config_dir)
  626. stock_config_dir = LIBCONFIG_DIR;
  627. netdata_configured_log_dir = getenv("NETDATA_LOG_DIR");
  628. if(!netdata_configured_log_dir)
  629. netdata_configured_log_dir = LOG_DIR;
  630. page_cnt *= (int)sysconf(_SC_NPROCESSORS_ONLN);
  631. nprocs = (int)sysconf(_SC_NPROCESSORS_ONLN);
  632. if (nprocs > NETDATA_MAX_PROCESSOR) {
  633. nprocs = NETDATA_MAX_PROCESSOR;
  634. }
  635. }
  636. static void change_collector_event() {
  637. int i;
  638. for (i = 0; collector_events[i].name ; i++ ) {
  639. collector_events[i].type = 'p';
  640. }
  641. if (mykernel < 328448)
  642. collector_events[i].name = NULL;
  643. }
  644. static inline void what_to_load(char *ptr) {
  645. if (!strcasecmp(ptr, "return"))
  646. mode = 0;
  647. /*
  648. else if (!strcasecmp(ptr, "dev"))
  649. mode = 1;
  650. */
  651. else
  652. change_collector_event();
  653. }
  654. static inline void enable_debug(char *ptr) {
  655. if (!strcasecmp(ptr, "yes"))
  656. debug_log = 1;
  657. }
  658. static inline void set_log_file(char *ptr) {
  659. if (!strcasecmp(ptr, "yes"))
  660. use_stdout = 1;
  661. }
  662. static void set_global_values() {
  663. struct section *sec = collector_config.first_section;
  664. while(sec) {
  665. if(!strcasecmp(sec->name, "global")) {
  666. struct config_option *values = sec->values;
  667. while(values) {
  668. if(!strcasecmp(values->name, "load"))
  669. what_to_load(values->value);
  670. else if(!strcasecmp(values->name, "debug log"))
  671. enable_debug(values->value);
  672. else if(!strcasecmp(values->name, "use stdout"))
  673. set_log_file(values->value);
  674. values = values->next;
  675. }
  676. }
  677. sec = sec->next;
  678. }
  679. }
  680. static int load_collector_file(char *path) {
  681. char lpath[4096];
  682. build_complete_path(lpath, 4096, path, "ebpf_process.conf" );
  683. if (!appconfig_load(&collector_config, lpath, 0, NULL))
  684. return 1;
  685. set_global_values();
  686. return 0;
  687. }
  688. int main(int argc, char **argv)
  689. {
  690. (void)argc;
  691. (void)argv;
  692. mykernel = get_kernel_version();
  693. if(!has_condition_to_run(mykernel))
  694. return 1;
  695. //set name
  696. program_name = "ebpf_process.plugin";
  697. //disable syslog
  698. error_log_syslog = 0;
  699. // set errors flood protection to 100 logs per hour
  700. error_log_errors_per_period = 100;
  701. error_log_throttle_period = 3600;
  702. if (argc > 1) {
  703. update_every = (int)strtol(argv[1], NULL, 10);
  704. }
  705. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  706. if (setrlimit(RLIMIT_MEMLOCK, &r)) {
  707. error("[EBPF PROCESS] setrlimit(RLIMIT_MEMLOCK)");
  708. return 2;
  709. }
  710. set_global_variables();
  711. if (load_collector_file(user_config_dir)) {
  712. info("[EBPF PROCESS] does not have a configuration file. It is starting with default options.");
  713. }
  714. if(ebpf_load_libraries()) {
  715. error("[EBPF_PROCESS] Cannot load library.");
  716. thread_finished++;
  717. int_exit(3);
  718. }
  719. signal(SIGINT, int_exit);
  720. signal(SIGTERM, int_exit);
  721. if (process_load_ebpf()) {
  722. thread_finished++;
  723. int_exit(4);
  724. }
  725. if(allocate_global_vectors()) {
  726. thread_finished++;
  727. error("[EBPF_PROCESS] Cannot allocate necessary vectors.");
  728. int_exit(5);
  729. }
  730. if(mode == 1 && debug_log) {
  731. if(map_memory()) {
  732. thread_finished++;
  733. error("[EBPF_PROCESS] Cannot map memory used with perf events.");
  734. int_exit(6);
  735. }
  736. }
  737. set_global_labels();
  738. if(debug_log) {
  739. open_developer_log();
  740. }
  741. if (pthread_mutex_init(&lock, NULL)) {
  742. thread_finished++;
  743. int_exit(7);
  744. }
  745. pthread_attr_t attr;
  746. pthread_attr_init(&attr);
  747. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  748. pthread_t thread[NETDATA_EBPF_PROCESS_THREADS];
  749. int i;
  750. int end = NETDATA_EBPF_PROCESS_THREADS;
  751. void * (*function_pointer[])(void *) = {process_publisher, process_collector, process_log };
  752. for ( i = 0; i < end ; i++ ) {
  753. if ( ( pthread_create(&thread[i], &attr, function_pointer[i], NULL) ) ) {
  754. error("[EBPF_PROCESS] Cannot create threads.");
  755. thread_finished++;
  756. int_exit(8);
  757. }
  758. }
  759. for ( i = 0; i < end ; i++ ) {
  760. if ( (pthread_join(thread[i], NULL) ) ) {
  761. error("[EBPF_PROCESS] Cannot join threads.");
  762. thread_finished++;
  763. int_exit(9);
  764. }
  765. }
  766. thread_finished++;
  767. int_exit(0);
  768. return 0;
  769. }