ebpf_process.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include <sys/resource.h>
  3. #include "ebpf.h"
  4. #include "ebpf_process.h"
  5. /*****************************************************************
  6. *
  7. * GLOBAL VARIABLES
  8. *
  9. *****************************************************************/
  10. static char *process_dimension_names[NETDATA_KEY_PUBLISH_PROCESS_END] = { "open", "close", "process",
  11. "task", "process", "thread" };
  12. static char *process_id_names[NETDATA_KEY_PUBLISH_PROCESS_END] = { "do_sys_open", "__close_fd", "do_exit",
  13. "release_task", "_do_fork", "sys_clone" };
  14. static char *status[] = { "process", "zombie" };
  15. static ebpf_local_maps_t process_maps[] = {{.name = "tbl_pid_stats", .internal_input = ND_EBPF_DEFAULT_PID_SIZE,
  16. .user_input = 0},
  17. {.name = NULL, .internal_input = 0, .user_input = 0}};
  18. static netdata_idx_t *process_hash_values = NULL;
  19. static netdata_syscall_stat_t process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_END];
  20. static netdata_publish_syscall_t process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_END];
  21. static ebpf_data_t process_data;
  22. ebpf_process_stat_t **global_process_stats = NULL;
  23. ebpf_process_publish_apps_t **current_apps_data = NULL;
  24. int process_enabled = 0;
  25. static int *map_fd = NULL;
  26. static struct bpf_object *objects = NULL;
  27. static struct bpf_link **probe_links = NULL;
  28. struct config process_config = { .first_section = NULL,
  29. .last_section = NULL,
  30. .mutex = NETDATA_MUTEX_INITIALIZER,
  31. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  32. .rwlock = AVL_LOCK_INITIALIZER } };
  33. /*****************************************************************
  34. *
  35. * PROCESS DATA AND SEND TO NETDATA
  36. *
  37. *****************************************************************/
  38. /**
  39. * Update publish structure before to send data to Netdata.
  40. *
  41. * @param publish the first output structure with independent dimensions
  42. * @param pvc the second output structure with correlated dimensions
  43. * @param input the structure with the input data.
  44. */
  45. static void ebpf_update_global_publish(netdata_publish_syscall_t *publish, netdata_publish_vfs_common_t *pvc,
  46. netdata_syscall_stat_t *input)
  47. {
  48. netdata_publish_syscall_t *move = publish;
  49. int selector = NETDATA_KEY_PUBLISH_PROCESS_OPEN;
  50. while (move) {
  51. // Until NETDATA_KEY_PUBLISH_PROCESS_EXIT we are creating accumulators, so it is possible
  52. // to use incremental charts, but after this we will do some math with the values, so we are storing
  53. // absolute values
  54. if (selector < NETDATA_KEY_PUBLISH_PROCESS_EXIT) {
  55. move->ncall = input->call;
  56. move->nbyte = input->bytes;
  57. move->nerr = input->ecall;
  58. } else {
  59. move->ncall = (input->call > move->pcall) ? input->call - move->pcall : move->pcall - input->call;
  60. move->nbyte = (input->bytes > move->pbyte) ? input->bytes - move->pbyte : move->pbyte - input->bytes;
  61. move->nerr = (input->ecall > move->nerr) ? input->ecall - move->perr : move->perr - input->ecall;
  62. move->pcall = input->call;
  63. move->pbyte = input->bytes;
  64. move->perr = input->ecall;
  65. }
  66. input = input->next;
  67. move = move->next;
  68. selector++;
  69. }
  70. pvc->running = (long)publish[NETDATA_KEY_PUBLISH_PROCESS_FORK].ncall -
  71. (long)publish[NETDATA_KEY_PUBLISH_PROCESS_CLONE].ncall;
  72. publish[NETDATA_KEY_PUBLISH_PROCESS_RELEASE_TASK].ncall = -publish[NETDATA_KEY_PUBLISH_PROCESS_RELEASE_TASK].ncall;
  73. pvc->zombie = (long)publish[NETDATA_KEY_PUBLISH_PROCESS_EXIT].ncall +
  74. (long)publish[NETDATA_KEY_PUBLISH_PROCESS_RELEASE_TASK].ncall;
  75. }
  76. /**
  77. * Call the necessary functions to create a chart.
  78. *
  79. * @param family the chart family
  80. * @param move the pointer with the values that will be published
  81. */
  82. static void write_status_chart(char *family, netdata_publish_vfs_common_t *pvc)
  83. {
  84. write_begin_chart(family, NETDATA_PROCESS_STATUS_NAME);
  85. write_chart_dimension(status[0], (long long)pvc->running);
  86. write_chart_dimension(status[1], (long long)pvc->zombie);
  87. write_end_chart();
  88. }
  89. /**
  90. * Send data to Netdata calling auxiliar functions.
  91. *
  92. * @param em the structure with thread information
  93. */
  94. static void ebpf_process_send_data(ebpf_module_t *em)
  95. {
  96. netdata_publish_vfs_common_t pvc;
  97. ebpf_update_global_publish(process_publish_aggregated, &pvc, process_aggregated_data);
  98. write_count_chart(NETDATA_FILE_OPEN_CLOSE_COUNT, NETDATA_EBPF_FAMILY, process_publish_aggregated, 2);
  99. write_count_chart(NETDATA_EXIT_SYSCALL, NETDATA_EBPF_FAMILY,
  100. &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_EXIT], 2);
  101. write_count_chart(NETDATA_PROCESS_SYSCALL, NETDATA_EBPF_FAMILY,
  102. &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_FORK], 2);
  103. write_status_chart(NETDATA_EBPF_FAMILY, &pvc);
  104. if (em->mode < MODE_ENTRY) {
  105. write_err_chart(NETDATA_FILE_OPEN_ERR_COUNT, NETDATA_EBPF_FAMILY,
  106. process_publish_aggregated, 2);
  107. write_err_chart(NETDATA_PROCESS_ERROR_NAME, NETDATA_EBPF_FAMILY,
  108. &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_FORK], 2);
  109. }
  110. }
  111. /**
  112. * Sum values for pid
  113. *
  114. * @param root the structure with all available PIDs
  115. *
  116. * @param offset the address that we are reading
  117. *
  118. * @return it returns the sum of all PIDs
  119. */
  120. long long ebpf_process_sum_values_for_pids(struct pid_on_target *root, size_t offset)
  121. {
  122. long long ret = 0;
  123. while (root) {
  124. int32_t pid = root->pid;
  125. ebpf_process_publish_apps_t *w = current_apps_data[pid];
  126. if (w) {
  127. ret += get_value_from_structure((char *)w, offset);
  128. }
  129. root = root->next;
  130. }
  131. return ret;
  132. }
  133. /**
  134. * Remove process pid
  135. *
  136. * Remove from PID task table when task_release was called.
  137. */
  138. void ebpf_process_remove_pids()
  139. {
  140. struct pid_stat *pids = root_of_pids;
  141. int pid_fd = map_fd[0];
  142. while (pids) {
  143. uint32_t pid = pids->pid;
  144. ebpf_process_stat_t *w = global_process_stats[pid];
  145. if (w) {
  146. if (w->removeme) {
  147. freez(w);
  148. global_process_stats[pid] = NULL;
  149. bpf_map_delete_elem(pid_fd, &pid);
  150. }
  151. }
  152. pids = pids->next;
  153. }
  154. }
  155. /**
  156. * Send data to Netdata calling auxiliar functions.
  157. *
  158. * @param em the structure with thread information
  159. * @param root the target list.
  160. */
  161. void ebpf_process_send_apps_data(ebpf_module_t *em, struct target *root)
  162. {
  163. struct target *w;
  164. collected_number value;
  165. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_FILE_OPEN);
  166. for (w = root; w; w = w->next) {
  167. if (unlikely(w->exposed && w->processes)) {
  168. value = ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, call_sys_open));
  169. write_chart_dimension(w->name, value);
  170. }
  171. }
  172. write_end_chart();
  173. if (em->mode < MODE_ENTRY) {
  174. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_FILE_OPEN_ERROR);
  175. for (w = root; w; w = w->next) {
  176. if (unlikely(w->exposed && w->processes)) {
  177. value = ebpf_process_sum_values_for_pids(w->root_pid,
  178. offsetof(ebpf_process_publish_apps_t, ecall_sys_open));
  179. write_chart_dimension(w->name, value);
  180. }
  181. }
  182. write_end_chart();
  183. }
  184. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_FILE_CLOSED);
  185. for (w = root; w; w = w->next) {
  186. if (unlikely(w->exposed && w->processes)) {
  187. value = ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, call_close_fd));
  188. write_chart_dimension(w->name, value);
  189. }
  190. }
  191. write_end_chart();
  192. if (em->mode < MODE_ENTRY) {
  193. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_FILE_CLOSE_ERROR);
  194. for (w = root; w; w = w->next) {
  195. if (unlikely(w->exposed && w->processes)) {
  196. value = ebpf_process_sum_values_for_pids(w->root_pid,
  197. offsetof(ebpf_process_publish_apps_t, ecall_close_fd));
  198. write_chart_dimension(w->name, value);
  199. }
  200. }
  201. write_end_chart();
  202. }
  203. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_TASK_PROCESS);
  204. for (w = root; w; w = w->next) {
  205. if (unlikely(w->exposed && w->processes)) {
  206. value = ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, call_do_fork));
  207. write_chart_dimension(w->name, value);
  208. }
  209. }
  210. write_end_chart();
  211. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_TASK_THREAD);
  212. for (w = root; w; w = w->next) {
  213. if (unlikely(w->exposed && w->processes)) {
  214. value = ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, call_sys_clone));
  215. write_chart_dimension(w->name, value);
  216. }
  217. }
  218. write_end_chart();
  219. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_TASK_CLOSE);
  220. for (w = root; w; w = w->next) {
  221. if (unlikely(w->exposed && w->processes)) {
  222. value = ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t,
  223. call_release_task));
  224. write_chart_dimension(w->name, value);
  225. }
  226. }
  227. write_end_chart();
  228. ebpf_process_remove_pids();
  229. }
  230. /*****************************************************************
  231. *
  232. * READ INFORMATION FROM KERNEL RING
  233. *
  234. *****************************************************************/
  235. /**
  236. * Read the hash table and store data to allocated vectors.
  237. */
  238. static void read_hash_global_tables()
  239. {
  240. uint64_t idx;
  241. netdata_idx_t res[NETDATA_KEY_END_VECTOR];
  242. netdata_idx_t *val = process_hash_values;
  243. for (idx = 0; idx < NETDATA_KEY_END_VECTOR; idx++) {
  244. if (!bpf_map_lookup_elem(map_fd[1], &idx, val)) {
  245. uint64_t total = 0;
  246. int i;
  247. int end = ebpf_nprocs;
  248. for (i = 0; i < end; i++)
  249. total += val[i];
  250. res[idx] = total;
  251. } else {
  252. res[idx] = 0;
  253. }
  254. }
  255. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_OPEN].call = res[NETDATA_KEY_CALLS_DO_SYS_OPEN];
  256. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_CLOSE].call = res[NETDATA_KEY_CALLS_CLOSE_FD];
  257. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_EXIT].call = res[NETDATA_KEY_CALLS_DO_EXIT];
  258. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_RELEASE_TASK].call = res[NETDATA_KEY_CALLS_RELEASE_TASK];
  259. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_FORK].call = res[NETDATA_KEY_CALLS_DO_FORK];
  260. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_CLONE].call = res[NETDATA_KEY_CALLS_SYS_CLONE];
  261. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_OPEN].ecall = res[NETDATA_KEY_ERROR_DO_SYS_OPEN];
  262. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_CLOSE].ecall = res[NETDATA_KEY_ERROR_CLOSE_FD];
  263. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_FORK].ecall = res[NETDATA_KEY_ERROR_DO_FORK];
  264. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_CLONE].ecall = res[NETDATA_KEY_ERROR_SYS_CLONE];
  265. }
  266. /**
  267. * Read the hash table and store data to allocated vectors.
  268. */
  269. static void ebpf_process_update_apps_data()
  270. {
  271. struct pid_stat *pids = root_of_pids;
  272. while (pids) {
  273. uint32_t current_pid = pids->pid;
  274. ebpf_process_stat_t *ps = global_process_stats[current_pid];
  275. if (!ps) {
  276. pids = pids->next;
  277. continue;
  278. }
  279. ebpf_process_publish_apps_t *cad = current_apps_data[current_pid];
  280. if (!cad) {
  281. cad = callocz(1, sizeof(ebpf_process_publish_apps_t));
  282. current_apps_data[current_pid] = cad;
  283. }
  284. //Read data
  285. cad->call_sys_open = ps->open_call;
  286. cad->call_close_fd = ps->close_call;
  287. cad->call_do_exit = ps->exit_call;
  288. cad->call_release_task = ps->release_call;
  289. cad->call_do_fork = ps->fork_call;
  290. cad->call_sys_clone = ps->clone_call;
  291. cad->ecall_sys_open = ps->open_err;
  292. cad->ecall_close_fd = ps->close_err;
  293. cad->ecall_do_fork = ps->fork_err;
  294. cad->ecall_sys_clone = ps->clone_err;
  295. pids = pids->next;
  296. }
  297. }
  298. /*****************************************************************
  299. *
  300. * FUNCTIONS TO CREATE CHARTS
  301. *
  302. *****************************************************************/
  303. /**
  304. * Create process status chart
  305. *
  306. * @param family the chart family
  307. * @param name the chart name
  308. * @param axis the axis label
  309. * @param web the group name used to attach the chart on dashboard
  310. * @param order the order number of the specified chart
  311. */
  312. static void ebpf_process_status_chart(char *family, char *name, char *axis,
  313. char *web, char *algorithm, int order)
  314. {
  315. printf("CHART %s.%s '' 'Process not closed' '%s' '%s' '' line %d %d ''\n",
  316. family,
  317. name,
  318. axis,
  319. web,
  320. order,
  321. update_every);
  322. printf("DIMENSION %s '' %s 1 1\n", status[0], algorithm);
  323. printf("DIMENSION %s '' %s 1 1\n", status[1], algorithm);
  324. }
  325. /**
  326. * Create global charts
  327. *
  328. * Call ebpf_create_chart to create the charts for the collector.
  329. *
  330. * @param em a pointer to the structure with the default values.
  331. */
  332. static void ebpf_create_global_charts(ebpf_module_t *em)
  333. {
  334. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  335. NETDATA_FILE_OPEN_CLOSE_COUNT,
  336. "Open and close calls",
  337. EBPF_COMMON_DIMENSION_CALL,
  338. NETDATA_FILE_GROUP,
  339. NULL,
  340. NETDATA_EBPF_CHART_TYPE_LINE,
  341. 21000,
  342. ebpf_create_global_dimension,
  343. process_publish_aggregated,
  344. 2);
  345. if (em->mode < MODE_ENTRY) {
  346. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  347. NETDATA_FILE_OPEN_ERR_COUNT,
  348. "Open fails",
  349. EBPF_COMMON_DIMENSION_CALL,
  350. NETDATA_FILE_GROUP,
  351. NULL,
  352. NETDATA_EBPF_CHART_TYPE_LINE,
  353. 21001,
  354. ebpf_create_global_dimension,
  355. process_publish_aggregated,
  356. 2);
  357. }
  358. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  359. NETDATA_PROCESS_SYSCALL,
  360. "Start process",
  361. EBPF_COMMON_DIMENSION_CALL,
  362. NETDATA_PROCESS_GROUP,
  363. NULL,
  364. NETDATA_EBPF_CHART_TYPE_LINE,
  365. 21002,
  366. ebpf_create_global_dimension,
  367. &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_FORK],
  368. 2);
  369. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  370. NETDATA_EXIT_SYSCALL,
  371. "Exit process",
  372. EBPF_COMMON_DIMENSION_CALL,
  373. NETDATA_PROCESS_GROUP,
  374. NULL,
  375. NETDATA_EBPF_CHART_TYPE_LINE,
  376. 21003,
  377. ebpf_create_global_dimension,
  378. &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_EXIT],
  379. 2);
  380. ebpf_process_status_chart(NETDATA_EBPF_FAMILY,
  381. NETDATA_PROCESS_STATUS_NAME,
  382. EBPF_COMMON_DIMENSION_DIFFERENCE,
  383. NETDATA_PROCESS_GROUP,
  384. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  385. 21004);
  386. if (em->mode < MODE_ENTRY) {
  387. ebpf_create_chart(NETDATA_EBPF_FAMILY,
  388. NETDATA_PROCESS_ERROR_NAME,
  389. "Fails to create process",
  390. EBPF_COMMON_DIMENSION_CALL,
  391. NETDATA_PROCESS_GROUP,
  392. NULL,
  393. NETDATA_EBPF_CHART_TYPE_LINE,
  394. 21005,
  395. ebpf_create_global_dimension,
  396. &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_FORK],
  397. 2);
  398. }
  399. }
  400. /**
  401. * Create process apps charts
  402. *
  403. * Call ebpf_create_chart to create the charts on apps submenu.
  404. *
  405. * @param em a pointer to the structure with the default values.
  406. * @param ptr a pointer for the targets.
  407. */
  408. void ebpf_process_create_apps_charts(struct ebpf_module *em, void *ptr)
  409. {
  410. struct target *root = ptr;
  411. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_FILE_OPEN,
  412. "Number of open files",
  413. EBPF_COMMON_DIMENSION_CALL,
  414. NETDATA_APPS_FILE_GROUP,
  415. NETDATA_EBPF_CHART_TYPE_STACKED,
  416. 20061,
  417. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  418. root);
  419. if (em->mode < MODE_ENTRY) {
  420. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_FILE_OPEN_ERROR,
  421. "Fails to open files",
  422. EBPF_COMMON_DIMENSION_CALL,
  423. NETDATA_APPS_FILE_GROUP,
  424. NETDATA_EBPF_CHART_TYPE_STACKED,
  425. 20062,
  426. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  427. root);
  428. }
  429. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_FILE_CLOSED,
  430. "Files closed",
  431. EBPF_COMMON_DIMENSION_CALL,
  432. NETDATA_APPS_FILE_GROUP,
  433. NETDATA_EBPF_CHART_TYPE_STACKED,
  434. 20063,
  435. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  436. root);
  437. if (em->mode < MODE_ENTRY) {
  438. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_FILE_CLOSE_ERROR,
  439. "Fails to close files",
  440. EBPF_COMMON_DIMENSION_CALL,
  441. NETDATA_APPS_FILE_GROUP,
  442. NETDATA_EBPF_CHART_TYPE_STACKED,
  443. 20064,
  444. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  445. root);
  446. }
  447. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_TASK_PROCESS,
  448. "Process started",
  449. EBPF_COMMON_DIMENSION_CALL,
  450. NETDATA_APPS_PROCESS_GROUP,
  451. NETDATA_EBPF_CHART_TYPE_STACKED,
  452. 20065,
  453. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  454. root);
  455. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_TASK_THREAD,
  456. "Threads started",
  457. EBPF_COMMON_DIMENSION_CALL,
  458. NETDATA_APPS_PROCESS_GROUP,
  459. NETDATA_EBPF_CHART_TYPE_STACKED,
  460. 20066,
  461. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  462. root);
  463. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_TASK_CLOSE,
  464. "Tasks closed",
  465. EBPF_COMMON_DIMENSION_CALL,
  466. NETDATA_APPS_PROCESS_GROUP,
  467. NETDATA_EBPF_CHART_TYPE_STACKED,
  468. 20067,
  469. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  470. root);
  471. }
  472. /**
  473. * Create apps charts
  474. *
  475. * Call ebpf_create_chart to create the charts on apps submenu.
  476. *
  477. * @param root a pointer for the targets.
  478. */
  479. static void ebpf_create_apps_charts(struct target *root)
  480. {
  481. struct target *w;
  482. int newly_added = 0;
  483. for (w = root; w; w = w->next) {
  484. if (w->target)
  485. continue;
  486. if (unlikely(w->processes && (debug_enabled || w->debug_enabled))) {
  487. struct pid_on_target *pid_on_target;
  488. fprintf(
  489. stderr, "ebpf.plugin: target '%s' has aggregated %u process%s:", w->name, w->processes,
  490. (w->processes == 1) ? "" : "es");
  491. for (pid_on_target = w->root_pid; pid_on_target; pid_on_target = pid_on_target->next) {
  492. fprintf(stderr, " %d", pid_on_target->pid);
  493. }
  494. fputc('\n', stderr);
  495. }
  496. if (!w->exposed && w->processes) {
  497. newly_added++;
  498. w->exposed = 1;
  499. if (debug_enabled || w->debug_enabled)
  500. debug_log_int("%s just added - regenerating charts.", w->name);
  501. }
  502. }
  503. if (!newly_added)
  504. return;
  505. int counter;
  506. for (counter = 0; ebpf_modules[counter].thread_name; counter++) {
  507. ebpf_module_t *current = &ebpf_modules[counter];
  508. if (current->enabled && current->apps_charts && current->apps_routine)
  509. current->apps_routine(current, root);
  510. }
  511. }
  512. /*****************************************************************
  513. *
  514. * FUNCTIONS WITH THE MAIN LOOP
  515. *
  516. *****************************************************************/
  517. /**
  518. * Main loop for this collector.
  519. *
  520. * @param step the number of microseconds used with heart beat
  521. * @param em the structure with thread information
  522. */
  523. static void process_collector(usec_t step, ebpf_module_t *em)
  524. {
  525. heartbeat_t hb;
  526. heartbeat_init(&hb);
  527. int publish_global = em->global_charts;
  528. int apps_enabled = em->apps_charts;
  529. int pid_fd = map_fd[0];
  530. while (!close_ebpf_plugin) {
  531. usec_t dt = heartbeat_next(&hb, step);
  532. (void)dt;
  533. read_hash_global_tables();
  534. pthread_mutex_lock(&collect_data_mutex);
  535. cleanup_exited_pids();
  536. collect_data_for_all_processes(pid_fd);
  537. ebpf_create_apps_charts(apps_groups_root_target);
  538. pthread_cond_broadcast(&collect_data_cond_var);
  539. pthread_mutex_unlock(&collect_data_mutex);
  540. int publish_apps = 0;
  541. if (apps_enabled && all_pids_count > 0) {
  542. publish_apps = 1;
  543. ebpf_process_update_apps_data();
  544. }
  545. pthread_mutex_lock(&lock);
  546. if (publish_global) {
  547. ebpf_process_send_data(em);
  548. }
  549. if (publish_apps) {
  550. ebpf_process_send_apps_data(em, apps_groups_root_target);
  551. }
  552. pthread_mutex_unlock(&lock);
  553. fflush(stdout);
  554. }
  555. }
  556. /*****************************************************************
  557. *
  558. * FUNCTIONS TO CLOSE THE THREAD
  559. *
  560. *****************************************************************/
  561. void clean_global_memory() {
  562. int pid_fd = map_fd[0];
  563. struct pid_stat *pids = root_of_pids;
  564. while (pids) {
  565. uint32_t pid = pids->pid;
  566. freez(global_process_stats[pid]);
  567. bpf_map_delete_elem(pid_fd, &pid);
  568. freez(current_apps_data[pid]);
  569. pids = pids->next;
  570. }
  571. }
  572. /**
  573. * Clean up the main thread.
  574. *
  575. * @param ptr thread data.
  576. */
  577. static void ebpf_process_cleanup(void *ptr)
  578. {
  579. UNUSED(ptr);
  580. heartbeat_t hb;
  581. heartbeat_init(&hb);
  582. uint32_t tick = 50*USEC_PER_MS;
  583. while (!finalized_threads) {
  584. usec_t dt = heartbeat_next(&hb, tick);
  585. UNUSED(dt);
  586. }
  587. ebpf_cleanup_publish_syscall(process_publish_aggregated);
  588. freez(process_hash_values);
  589. clean_global_memory();
  590. freez(global_process_stats);
  591. freez(current_apps_data);
  592. freez(process_data.map_fd);
  593. if (probe_links) {
  594. struct bpf_program *prog;
  595. size_t i = 0 ;
  596. bpf_object__for_each_program(prog, objects) {
  597. bpf_link__destroy(probe_links[i]);
  598. i++;
  599. }
  600. bpf_object__close(objects);
  601. }
  602. }
  603. /*****************************************************************
  604. *
  605. * FUNCTIONS TO START THREAD
  606. *
  607. *****************************************************************/
  608. /**
  609. * Allocate vectors used with this thread.
  610. * We are not testing the return, because callocz does this and shutdown the software
  611. * case it was not possible to allocate.
  612. *
  613. * @param length is the length for the vectors used inside the collector.
  614. */
  615. static void ebpf_process_allocate_global_vectors(size_t length)
  616. {
  617. memset(process_aggregated_data, 0, length * sizeof(netdata_syscall_stat_t));
  618. memset(process_publish_aggregated, 0, length * sizeof(netdata_publish_syscall_t));
  619. process_hash_values = callocz(ebpf_nprocs, sizeof(netdata_idx_t));
  620. global_process_stats = callocz((size_t)pid_max, sizeof(ebpf_process_stat_t *));
  621. current_apps_data = callocz((size_t)pid_max, sizeof(ebpf_process_publish_apps_t *));
  622. }
  623. static void change_syscalls()
  624. {
  625. static char *lfork = { "do_fork" };
  626. process_id_names[NETDATA_KEY_PUBLISH_PROCESS_FORK] = lfork;
  627. }
  628. /**
  629. * Set local variables
  630. *
  631. */
  632. static void set_local_pointers()
  633. {
  634. map_fd = process_data.map_fd;
  635. if (process_data.isrh >= NETDATA_MINIMUM_RH_VERSION && process_data.isrh < NETDATA_RH_8)
  636. change_syscalls();
  637. }
  638. /*****************************************************************
  639. *
  640. * EBPF PROCESS THREAD
  641. *
  642. *****************************************************************/
  643. /**
  644. *
  645. */
  646. static void wait_for_all_threads_die()
  647. {
  648. ebpf_modules[EBPF_MODULE_PROCESS_IDX].enabled = 0;
  649. heartbeat_t hb;
  650. heartbeat_init(&hb);
  651. int max = 10;
  652. int i;
  653. for (i = 0; i < max; i++) {
  654. heartbeat_next(&hb, 200000);
  655. size_t j, counter = 0, compare = 0;
  656. for (j = 0; ebpf_modules[j].thread_name; j++) {
  657. if (!ebpf_modules[j].enabled)
  658. counter++;
  659. compare++;
  660. }
  661. if (counter == compare)
  662. break;
  663. }
  664. }
  665. /**
  666. * Process thread
  667. *
  668. * Thread used to generate process charts.
  669. *
  670. * @param ptr a pointer to `struct ebpf_module`
  671. *
  672. * @return It always return NULL
  673. */
  674. void *ebpf_process_thread(void *ptr)
  675. {
  676. netdata_thread_cleanup_push(ebpf_process_cleanup, ptr);
  677. ebpf_module_t *em = (ebpf_module_t *)ptr;
  678. em->maps = process_maps;
  679. process_enabled = em->enabled;
  680. fill_ebpf_data(&process_data);
  681. pthread_mutex_lock(&lock);
  682. ebpf_process_allocate_global_vectors(NETDATA_KEY_PUBLISH_PROCESS_END);
  683. if (ebpf_update_kernel(&process_data)) {
  684. pthread_mutex_unlock(&lock);
  685. goto endprocess;
  686. }
  687. ebpf_update_pid_table(&process_maps[0], em);
  688. set_local_pointers();
  689. probe_links = ebpf_load_program(ebpf_plugin_dir, em, kernel_string, &objects, process_data.map_fd);
  690. if (!probe_links) {
  691. pthread_mutex_unlock(&lock);
  692. goto endprocess;
  693. }
  694. int algorithms[NETDATA_KEY_PUBLISH_PROCESS_END] = {
  695. NETDATA_EBPF_INCREMENTAL_IDX, NETDATA_EBPF_INCREMENTAL_IDX, NETDATA_EBPF_ABSOLUTE_IDX,
  696. NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_ABSOLUTE_IDX
  697. };
  698. ebpf_global_labels(
  699. process_aggregated_data, process_publish_aggregated, process_dimension_names, process_id_names,
  700. algorithms, NETDATA_KEY_PUBLISH_PROCESS_END);
  701. if (process_enabled) {
  702. ebpf_create_global_charts(em);
  703. }
  704. pthread_mutex_unlock(&lock);
  705. process_collector((usec_t)(em->update_time * USEC_PER_SEC), em);
  706. endprocess:
  707. wait_for_all_threads_die();
  708. netdata_thread_cleanup_pop(1);
  709. return NULL;
  710. }