ebpf_process.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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] = { "process", "task", "process", "thread" };
  11. static char *process_id_names[NETDATA_KEY_PUBLISH_PROCESS_END] = { "do_exit", "release_task", "_do_fork", "sys_clone" };
  12. static char *status[] = { "process", "zombie" };
  13. static ebpf_local_maps_t process_maps[] = {{.name = "tbl_pid_stats", .internal_input = ND_EBPF_DEFAULT_PID_SIZE,
  14. .user_input = 0,
  15. .type = NETDATA_EBPF_MAP_RESIZABLE | NETDATA_EBPF_MAP_PID,
  16. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  17. {.name = "tbl_total_stats", .internal_input = NETDATA_KEY_END_VECTOR,
  18. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  19. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  20. {.name = "process_ctrl", .internal_input = NETDATA_CONTROLLER_END,
  21. .user_input = 0,
  22. .type = NETDATA_EBPF_MAP_CONTROLLER,
  23. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  24. {.name = NULL, .internal_input = 0, .user_input = 0,
  25. .type = NETDATA_EBPF_MAP_CONTROLLER,
  26. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED}};
  27. char *tracepoint_sched_type = { "sched" } ;
  28. char *tracepoint_sched_process_exit = { "sched_process_exit" };
  29. char *tracepoint_sched_process_exec = { "sched_process_exec" };
  30. char *tracepoint_sched_process_fork = { "sched_process_fork" };
  31. static int was_sched_process_exit_enabled = 0;
  32. static int was_sched_process_exec_enabled = 0;
  33. static int was_sched_process_fork_enabled = 0;
  34. static netdata_idx_t *process_hash_values = NULL;
  35. static netdata_syscall_stat_t process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_END];
  36. static netdata_publish_syscall_t process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_END];
  37. ebpf_process_stat_t **global_process_stats = NULL;
  38. ebpf_process_publish_apps_t **current_apps_data = NULL;
  39. int process_enabled = 0;
  40. static struct bpf_object *objects = NULL;
  41. static struct bpf_link **probe_links = NULL;
  42. struct config process_config = { .first_section = NULL,
  43. .last_section = NULL,
  44. .mutex = NETDATA_MUTEX_INITIALIZER,
  45. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  46. .rwlock = AVL_LOCK_INITIALIZER } };
  47. static struct netdata_static_thread cgroup_thread = {"EBPF CGROUP", NULL, NULL,
  48. 1, NULL, NULL, NULL};
  49. /*****************************************************************
  50. *
  51. * PROCESS DATA AND SEND TO NETDATA
  52. *
  53. *****************************************************************/
  54. /**
  55. * Update publish structure before to send data to Netdata.
  56. *
  57. * @param publish the first output structure with independent dimensions
  58. * @param pvc the second output structure with correlated dimensions
  59. * @param input the structure with the input data.
  60. */
  61. static void ebpf_update_global_publish(netdata_publish_syscall_t *publish, netdata_publish_vfs_common_t *pvc,
  62. netdata_syscall_stat_t *input)
  63. {
  64. netdata_publish_syscall_t *move = publish;
  65. int selector = NETDATA_KEY_PUBLISH_PROCESS_EXIT;
  66. while (move) {
  67. move->ncall = (input->call > move->pcall) ? input->call - move->pcall : move->pcall - input->call;
  68. move->nbyte = (input->bytes > move->pbyte) ? input->bytes - move->pbyte : move->pbyte - input->bytes;
  69. move->nerr = (input->ecall > move->nerr) ? input->ecall - move->perr : move->perr - input->ecall;
  70. move->pcall = input->call;
  71. move->pbyte = input->bytes;
  72. move->perr = input->ecall;
  73. input = input->next;
  74. move = move->next;
  75. selector++;
  76. }
  77. pvc->running = (long)publish[NETDATA_KEY_PUBLISH_PROCESS_FORK].ncall -
  78. (long)publish[NETDATA_KEY_PUBLISH_PROCESS_CLONE].ncall;
  79. publish[NETDATA_KEY_PUBLISH_PROCESS_RELEASE_TASK].ncall = -publish[NETDATA_KEY_PUBLISH_PROCESS_RELEASE_TASK].ncall;
  80. pvc->zombie = (long)publish[NETDATA_KEY_PUBLISH_PROCESS_EXIT].ncall +
  81. (long)publish[NETDATA_KEY_PUBLISH_PROCESS_RELEASE_TASK].ncall;
  82. }
  83. /**
  84. * Call the necessary functions to create a chart.
  85. *
  86. * @param family the chart family
  87. * @param move the pointer with the values that will be published
  88. */
  89. static void write_status_chart(char *family, netdata_publish_vfs_common_t *pvc)
  90. {
  91. write_begin_chart(family, NETDATA_PROCESS_STATUS_NAME);
  92. write_chart_dimension(status[0], (long long)pvc->running);
  93. write_chart_dimension(status[1], (long long)pvc->zombie);
  94. write_end_chart();
  95. }
  96. /**
  97. * Send data to Netdata calling auxiliar functions.
  98. *
  99. * @param em the structure with thread information
  100. */
  101. static void ebpf_process_send_data(ebpf_module_t *em)
  102. {
  103. netdata_publish_vfs_common_t pvc;
  104. ebpf_update_global_publish(process_publish_aggregated, &pvc, process_aggregated_data);
  105. write_count_chart(NETDATA_EXIT_SYSCALL, NETDATA_EBPF_SYSTEM_GROUP,
  106. &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_EXIT], 2);
  107. write_count_chart(NETDATA_PROCESS_SYSCALL, NETDATA_EBPF_SYSTEM_GROUP,
  108. &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_FORK], 2);
  109. write_status_chart(NETDATA_EBPF_SYSTEM_GROUP, &pvc);
  110. if (em->mode < MODE_ENTRY) {
  111. write_err_chart(NETDATA_PROCESS_ERROR_NAME, NETDATA_EBPF_SYSTEM_GROUP,
  112. &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_FORK], 2);
  113. }
  114. }
  115. /**
  116. * Sum values for pid
  117. *
  118. * @param root the structure with all available PIDs
  119. *
  120. * @param offset the address that we are reading
  121. *
  122. * @return it returns the sum of all PIDs
  123. */
  124. long long ebpf_process_sum_values_for_pids(struct pid_on_target *root, size_t offset)
  125. {
  126. long long ret = 0;
  127. while (root) {
  128. int32_t pid = root->pid;
  129. ebpf_process_publish_apps_t *w = current_apps_data[pid];
  130. if (w) {
  131. ret += get_value_from_structure((char *)w, offset);
  132. }
  133. root = root->next;
  134. }
  135. return ret;
  136. }
  137. /**
  138. * Remove process pid
  139. *
  140. * Remove from PID task table when task_release was called.
  141. */
  142. void ebpf_process_remove_pids()
  143. {
  144. struct pid_stat *pids = root_of_pids;
  145. int pid_fd = process_maps[NETDATA_PROCESS_PID_TABLE].map_fd;
  146. while (pids) {
  147. uint32_t pid = pids->pid;
  148. ebpf_process_stat_t *w = global_process_stats[pid];
  149. if (w) {
  150. if (w->removeme) {
  151. freez(w);
  152. global_process_stats[pid] = NULL;
  153. bpf_map_delete_elem(pid_fd, &pid);
  154. }
  155. }
  156. pids = pids->next;
  157. }
  158. }
  159. /**
  160. * Send data to Netdata calling auxiliar functions.
  161. *
  162. * @param root the target list.
  163. */
  164. void ebpf_process_send_apps_data(struct target *root, ebpf_module_t *em)
  165. {
  166. struct target *w;
  167. collected_number value;
  168. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_TASK_PROCESS);
  169. for (w = root; w; w = w->next) {
  170. if (unlikely(w->exposed && w->processes)) {
  171. value = ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, create_process));
  172. write_chart_dimension(w->name, value);
  173. }
  174. }
  175. write_end_chart();
  176. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_TASK_THREAD);
  177. for (w = root; w; w = w->next) {
  178. if (unlikely(w->exposed && w->processes)) {
  179. value = ebpf_process_sum_values_for_pids(w->root_pid, offsetof(ebpf_process_publish_apps_t, create_thread));
  180. write_chart_dimension(w->name, value);
  181. }
  182. }
  183. write_end_chart();
  184. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_TASK_EXIT);
  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,
  188. call_do_exit));
  189. write_chart_dimension(w->name, value);
  190. }
  191. }
  192. write_end_chart();
  193. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_TASK_CLOSE);
  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, offsetof(ebpf_process_publish_apps_t,
  197. call_release_task));
  198. write_chart_dimension(w->name, value);
  199. }
  200. }
  201. write_end_chart();
  202. if (em->mode < MODE_ENTRY) {
  203. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_SYSCALL_APPS_TASK_ERROR);
  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,
  207. task_err));
  208. write_chart_dimension(w->name, value);
  209. }
  210. }
  211. write_end_chart();
  212. }
  213. ebpf_process_remove_pids();
  214. }
  215. /*****************************************************************
  216. *
  217. * READ INFORMATION FROM KERNEL RING
  218. *
  219. *****************************************************************/
  220. /**
  221. * Read the hash table and store data to allocated vectors.
  222. */
  223. static void read_hash_global_tables()
  224. {
  225. uint64_t idx;
  226. netdata_idx_t res[NETDATA_KEY_END_VECTOR];
  227. netdata_idx_t *val = process_hash_values;
  228. int fd = process_maps[NETDATA_PROCESS_GLOBAL_TABLE].map_fd;
  229. for (idx = 0; idx < NETDATA_KEY_END_VECTOR; idx++) {
  230. if (!bpf_map_lookup_elem(fd, &idx, val)) {
  231. uint64_t total = 0;
  232. int i;
  233. int end = ebpf_nprocs;
  234. for (i = 0; i < end; i++)
  235. total += val[i];
  236. res[idx] = total;
  237. } else {
  238. res[idx] = 0;
  239. }
  240. }
  241. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_EXIT].call = res[NETDATA_KEY_CALLS_DO_EXIT];
  242. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_RELEASE_TASK].call = res[NETDATA_KEY_CALLS_RELEASE_TASK];
  243. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_FORK].call = res[NETDATA_KEY_CALLS_DO_FORK];
  244. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_CLONE].call = res[NETDATA_KEY_CALLS_SYS_CLONE];
  245. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_FORK].ecall = res[NETDATA_KEY_ERROR_DO_FORK];
  246. process_aggregated_data[NETDATA_KEY_PUBLISH_PROCESS_CLONE].ecall = res[NETDATA_KEY_ERROR_SYS_CLONE];
  247. }
  248. /**
  249. * Read the hash table and store data to allocated vectors.
  250. */
  251. static void ebpf_process_update_apps_data()
  252. {
  253. struct pid_stat *pids = root_of_pids;
  254. while (pids) {
  255. uint32_t current_pid = pids->pid;
  256. ebpf_process_stat_t *ps = global_process_stats[current_pid];
  257. if (!ps) {
  258. pids = pids->next;
  259. continue;
  260. }
  261. ebpf_process_publish_apps_t *cad = current_apps_data[current_pid];
  262. if (!cad) {
  263. cad = callocz(1, sizeof(ebpf_process_publish_apps_t));
  264. current_apps_data[current_pid] = cad;
  265. }
  266. //Read data
  267. cad->call_do_exit = ps->exit_call;
  268. cad->call_release_task = ps->release_call;
  269. cad->create_process = ps->create_process;
  270. cad->create_thread = ps->create_thread;
  271. cad->task_err = ps->task_err;
  272. pids = pids->next;
  273. }
  274. }
  275. /**
  276. * Update cgroup
  277. *
  278. * Update cgroup data based in
  279. */
  280. static void ebpf_update_process_cgroup()
  281. {
  282. ebpf_cgroup_target_t *ect ;
  283. int pid_fd = process_maps[NETDATA_PROCESS_PID_TABLE].map_fd;
  284. pthread_mutex_lock(&mutex_cgroup_shm);
  285. for (ect = ebpf_cgroup_pids; ect; ect = ect->next) {
  286. struct pid_on_target2 *pids;
  287. for (pids = ect->pids; pids; pids = pids->next) {
  288. int pid = pids->pid;
  289. ebpf_process_stat_t *out = &pids->ps;
  290. if (global_process_stats[pid]) {
  291. ebpf_process_stat_t *in = global_process_stats[pid];
  292. memcpy(out, in, sizeof(ebpf_process_stat_t));
  293. } else {
  294. if (bpf_map_lookup_elem(pid_fd, &pid, out)) {
  295. memset(out, 0, sizeof(ebpf_process_stat_t));
  296. }
  297. }
  298. }
  299. }
  300. pthread_mutex_unlock(&mutex_cgroup_shm);
  301. }
  302. /*****************************************************************
  303. *
  304. * FUNCTIONS TO CREATE CHARTS
  305. *
  306. *****************************************************************/
  307. /**
  308. * Create process status chart
  309. *
  310. * @param family the chart family
  311. * @param name the chart name
  312. * @param axis the axis label
  313. * @param web the group name used to attach the chart on dashboard
  314. * @param order the order number of the specified chart
  315. */
  316. static void ebpf_process_status_chart(char *family, char *name, char *axis,
  317. char *web, char *algorithm, int order)
  318. {
  319. printf("CHART %s.%s '' 'Process not closed' '%s' '%s' '' line %d %d '' 'ebpf.plugin' 'process'\n",
  320. family,
  321. name,
  322. axis,
  323. web,
  324. order,
  325. update_every);
  326. printf("DIMENSION %s '' %s 1 1\n", status[0], algorithm);
  327. printf("DIMENSION %s '' %s 1 1\n", status[1], algorithm);
  328. }
  329. /**
  330. * Create global charts
  331. *
  332. * Call ebpf_create_chart to create the charts for the collector.
  333. *
  334. * @param em a pointer to the structure with the default values.
  335. */
  336. static void ebpf_create_global_charts(ebpf_module_t *em)
  337. {
  338. ebpf_create_chart(NETDATA_EBPF_SYSTEM_GROUP,
  339. NETDATA_PROCESS_SYSCALL,
  340. "Start process",
  341. EBPF_COMMON_DIMENSION_CALL,
  342. NETDATA_PROCESS_GROUP,
  343. NULL,
  344. NETDATA_EBPF_CHART_TYPE_LINE,
  345. 21002,
  346. ebpf_create_global_dimension,
  347. &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_FORK],
  348. 2, NETDATA_EBPF_MODULE_NAME_PROCESS);
  349. ebpf_create_chart(NETDATA_EBPF_SYSTEM_GROUP,
  350. NETDATA_EXIT_SYSCALL,
  351. "Exit process",
  352. EBPF_COMMON_DIMENSION_CALL,
  353. NETDATA_PROCESS_GROUP,
  354. NULL,
  355. NETDATA_EBPF_CHART_TYPE_LINE,
  356. 21003,
  357. ebpf_create_global_dimension,
  358. &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_EXIT],
  359. 2, NETDATA_EBPF_MODULE_NAME_PROCESS);
  360. ebpf_process_status_chart(NETDATA_EBPF_SYSTEM_GROUP,
  361. NETDATA_PROCESS_STATUS_NAME,
  362. EBPF_COMMON_DIMENSION_DIFFERENCE,
  363. NETDATA_PROCESS_GROUP,
  364. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  365. 21004);
  366. if (em->mode < MODE_ENTRY) {
  367. ebpf_create_chart(NETDATA_EBPF_SYSTEM_GROUP,
  368. NETDATA_PROCESS_ERROR_NAME,
  369. "Fails to create process",
  370. EBPF_COMMON_DIMENSION_CALL,
  371. NETDATA_PROCESS_GROUP,
  372. NULL,
  373. NETDATA_EBPF_CHART_TYPE_LINE,
  374. 21005,
  375. ebpf_create_global_dimension,
  376. &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_FORK],
  377. 2, NETDATA_EBPF_MODULE_NAME_PROCESS);
  378. }
  379. }
  380. /**
  381. * Create process apps charts
  382. *
  383. * Call ebpf_create_chart to create the charts on apps submenu.
  384. *
  385. * @param em a pointer to the structure with the default values.
  386. * @param ptr a pointer for the targets.
  387. */
  388. void ebpf_process_create_apps_charts(struct ebpf_module *em, void *ptr)
  389. {
  390. struct target *root = ptr;
  391. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_TASK_PROCESS,
  392. "Process started",
  393. EBPF_COMMON_DIMENSION_CALL,
  394. NETDATA_PROCESS_GROUP,
  395. NETDATA_EBPF_CHART_TYPE_STACKED,
  396. 20065,
  397. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  398. root, NETDATA_EBPF_MODULE_NAME_PROCESS);
  399. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_TASK_THREAD,
  400. "Threads started",
  401. EBPF_COMMON_DIMENSION_CALL,
  402. NETDATA_PROCESS_GROUP,
  403. NETDATA_EBPF_CHART_TYPE_STACKED,
  404. 20066,
  405. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  406. root, NETDATA_EBPF_MODULE_NAME_PROCESS);
  407. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_TASK_EXIT,
  408. "Tasks starts exit process.",
  409. EBPF_COMMON_DIMENSION_CALL,
  410. NETDATA_PROCESS_GROUP,
  411. NETDATA_EBPF_CHART_TYPE_STACKED,
  412. 20067,
  413. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  414. root, NETDATA_EBPF_MODULE_NAME_PROCESS);
  415. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_TASK_CLOSE,
  416. "Tasks closed",
  417. EBPF_COMMON_DIMENSION_CALL,
  418. NETDATA_PROCESS_GROUP,
  419. NETDATA_EBPF_CHART_TYPE_STACKED,
  420. 20068,
  421. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  422. root, NETDATA_EBPF_MODULE_NAME_PROCESS);
  423. if (em->mode < MODE_ENTRY) {
  424. ebpf_create_charts_on_apps(NETDATA_SYSCALL_APPS_TASK_ERROR,
  425. "Errors to create process or threads.",
  426. EBPF_COMMON_DIMENSION_CALL,
  427. NETDATA_PROCESS_GROUP,
  428. NETDATA_EBPF_CHART_TYPE_STACKED,
  429. 20069,
  430. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  431. root,
  432. NETDATA_EBPF_MODULE_NAME_PROCESS);
  433. }
  434. }
  435. /**
  436. * Create apps charts
  437. *
  438. * Call ebpf_create_chart to create the charts on apps submenu.
  439. *
  440. * @param root a pointer for the targets.
  441. */
  442. static void ebpf_create_apps_charts(struct target *root)
  443. {
  444. struct target *w;
  445. int newly_added = 0;
  446. for (w = root; w; w = w->next) {
  447. if (w->target)
  448. continue;
  449. if (unlikely(w->processes && (debug_enabled || w->debug_enabled))) {
  450. struct pid_on_target *pid_on_target;
  451. fprintf(
  452. stderr, "ebpf.plugin: target '%s' has aggregated %u process%s:", w->name, w->processes,
  453. (w->processes == 1) ? "" : "es");
  454. for (pid_on_target = w->root_pid; pid_on_target; pid_on_target = pid_on_target->next) {
  455. fprintf(stderr, " %d", pid_on_target->pid);
  456. }
  457. fputc('\n', stderr);
  458. }
  459. if (!w->exposed && w->processes) {
  460. newly_added++;
  461. w->exposed = 1;
  462. if (debug_enabled || w->debug_enabled)
  463. debug_log_int("%s just added - regenerating charts.", w->name);
  464. }
  465. }
  466. if (!newly_added)
  467. return;
  468. int counter;
  469. for (counter = 0; ebpf_modules[counter].thread_name; counter++) {
  470. ebpf_module_t *current = &ebpf_modules[counter];
  471. if (current->enabled && current->apps_charts && current->apps_routine)
  472. current->apps_routine(current, root);
  473. }
  474. }
  475. /*****************************************************************
  476. *
  477. * FUNCTIONS WITH THE MAIN LOOP
  478. *
  479. *****************************************************************/
  480. /**
  481. * Cgroup update shm
  482. *
  483. * This is the thread callback.
  484. * This thread is necessary, because we cannot freeze the whole plugin to read the data from shared memory.
  485. *
  486. * @param ptr It is a NULL value for this thread.
  487. *
  488. * @return It always returns NULL.
  489. */
  490. void *ebpf_cgroup_update_shm(void *ptr)
  491. {
  492. UNUSED(ptr);
  493. heartbeat_t hb;
  494. heartbeat_init(&hb);
  495. usec_t step = 30 * USEC_PER_SEC;
  496. while (!close_ebpf_plugin) {
  497. usec_t dt = heartbeat_next(&hb, step);
  498. (void)dt;
  499. if (!shm_ebpf_cgroup.header)
  500. ebpf_map_cgroup_shared_memory();
  501. ebpf_parse_cgroup_shm_data();
  502. }
  503. return NULL;
  504. }
  505. /**
  506. * Sum PIDs
  507. *
  508. * Sum values for all targets.
  509. *
  510. * @param ps structure used to store data
  511. * @param pids input data
  512. */
  513. static void ebpf_process_sum_cgroup_pids(ebpf_process_stat_t *ps, struct pid_on_target2 *pids)
  514. {
  515. ebpf_process_stat_t accumulator;
  516. memset(&accumulator, 0, sizeof(accumulator));
  517. while (pids) {
  518. ebpf_process_stat_t *ps = &pids->ps;
  519. accumulator.exit_call += ps->exit_call;
  520. accumulator.release_call += ps->release_call;
  521. accumulator.create_process += ps->create_process;
  522. accumulator.create_thread += ps->create_thread;
  523. accumulator.task_err += ps->task_err;
  524. pids = pids->next;
  525. }
  526. ps->exit_call = (accumulator.exit_call >= ps->exit_call) ? accumulator.exit_call : ps->exit_call;
  527. ps->release_call = (accumulator.release_call >= ps->release_call) ? accumulator.release_call : ps->release_call;
  528. ps->create_process = (accumulator.create_process >= ps->create_process) ? accumulator.create_process : ps->create_process;
  529. ps->create_thread = (accumulator.create_thread >= ps->create_thread) ? accumulator.create_thread : ps->create_thread;
  530. ps->task_err = (accumulator.task_err >= ps->task_err) ? accumulator.task_err : ps->task_err;
  531. }
  532. /*
  533. * Send Specific Process data
  534. *
  535. * Send data for specific cgroup/apps.
  536. *
  537. * @param type chart type
  538. * @param values structure with values that will be sent to netdata
  539. * @param em the structure with thread information
  540. */
  541. static void ebpf_send_specific_process_data(char *type, ebpf_process_stat_t *values, ebpf_module_t *em)
  542. {
  543. write_begin_chart(type, NETDATA_SYSCALL_APPS_TASK_PROCESS);
  544. write_chart_dimension(process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_FORK].name,
  545. (long long) values->create_process);
  546. write_end_chart();
  547. write_begin_chart(type, NETDATA_SYSCALL_APPS_TASK_THREAD);
  548. write_chart_dimension(process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_CLONE].name,
  549. (long long) values->create_thread);
  550. write_end_chart();
  551. write_begin_chart(type, NETDATA_SYSCALL_APPS_TASK_EXIT);
  552. write_chart_dimension(process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_EXIT].name,
  553. (long long) values->release_call);
  554. write_end_chart();
  555. write_begin_chart(type, NETDATA_SYSCALL_APPS_TASK_CLOSE);
  556. write_chart_dimension(process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_RELEASE_TASK].name,
  557. (long long) values->release_call);
  558. write_end_chart();
  559. if (em->mode < MODE_ENTRY) {
  560. write_begin_chart(type, NETDATA_SYSCALL_APPS_TASK_ERROR);
  561. write_chart_dimension(process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_EXIT].name,
  562. (long long) values->task_err);
  563. write_end_chart();
  564. }
  565. }
  566. /**
  567. * Create specific process charts
  568. *
  569. * Create charts for cgroup/application
  570. *
  571. * @param type the chart type.
  572. * @param em the structure with thread information
  573. */
  574. static void ebpf_create_specific_process_charts(char *type, ebpf_module_t *em)
  575. {
  576. ebpf_create_chart(type, NETDATA_SYSCALL_APPS_TASK_PROCESS, "Process started",
  577. EBPF_COMMON_DIMENSION_CALL, NETDATA_PROCESS_CGROUP_GROUP,
  578. NETDATA_CGROUP_PROCESS_CREATE_CONTEXT, NETDATA_EBPF_CHART_TYPE_LINE,
  579. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5000,
  580. ebpf_create_global_dimension, &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_FORK],
  581. 1, NETDATA_EBPF_MODULE_NAME_PROCESS);
  582. ebpf_create_chart(type, NETDATA_SYSCALL_APPS_TASK_THREAD, "Threads started",
  583. EBPF_COMMON_DIMENSION_CALL, NETDATA_PROCESS_CGROUP_GROUP,
  584. NETDATA_CGROUP_THREAD_CREATE_CONTEXT, NETDATA_EBPF_CHART_TYPE_LINE,
  585. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5001,
  586. ebpf_create_global_dimension,
  587. &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_CLONE],
  588. 1, NETDATA_EBPF_MODULE_NAME_PROCESS);
  589. ebpf_create_chart(type, NETDATA_SYSCALL_APPS_TASK_EXIT, "Tasks starts exit process.",
  590. EBPF_COMMON_DIMENSION_CALL, NETDATA_PROCESS_CGROUP_GROUP,
  591. NETDATA_CGROUP_PROCESS_EXIT_CONTEXT,
  592. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5002,
  593. ebpf_create_global_dimension,
  594. &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_EXIT],
  595. 1, NETDATA_EBPF_MODULE_NAME_PROCESS);
  596. ebpf_create_chart(type, NETDATA_SYSCALL_APPS_TASK_CLOSE, "Tasks closed",
  597. EBPF_COMMON_DIMENSION_CALL, NETDATA_PROCESS_CGROUP_GROUP,
  598. NETDATA_CGROUP_PROCESS_CLOSE_CONTEXT,
  599. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5003,
  600. ebpf_create_global_dimension,
  601. &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_RELEASE_TASK],
  602. 1, NETDATA_EBPF_MODULE_NAME_PROCESS);
  603. if (em->mode < MODE_ENTRY) {
  604. ebpf_create_chart(type, NETDATA_SYSCALL_APPS_TASK_ERROR, "Errors to create process or threads.",
  605. EBPF_COMMON_DIMENSION_CALL, NETDATA_PROCESS_CGROUP_GROUP,
  606. NETDATA_CGROUP_PROCESS_ERROR_CONTEXT,
  607. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5004,
  608. ebpf_create_global_dimension,
  609. &process_publish_aggregated[NETDATA_KEY_PUBLISH_PROCESS_EXIT],
  610. 1, NETDATA_EBPF_MODULE_NAME_PROCESS);
  611. }
  612. }
  613. /**
  614. * Obsolete specific process charts
  615. *
  616. * Obsolete charts for cgroup/application
  617. *
  618. * @param type the chart type.
  619. * @param em the structure with thread information
  620. */
  621. static void ebpf_obsolete_specific_process_charts(char *type, ebpf_module_t *em)
  622. {
  623. ebpf_write_chart_obsolete(type, NETDATA_SYSCALL_APPS_TASK_PROCESS, "Process started",
  624. EBPF_COMMON_DIMENSION_CALL, NETDATA_PROCESS_GROUP, NETDATA_EBPF_CHART_TYPE_LINE,
  625. NETDATA_CGROUP_PROCESS_CREATE_CONTEXT, NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5000);
  626. ebpf_write_chart_obsolete(type, NETDATA_SYSCALL_APPS_TASK_THREAD, "Threads started",
  627. EBPF_COMMON_DIMENSION_CALL, NETDATA_PROCESS_GROUP, NETDATA_EBPF_CHART_TYPE_LINE,
  628. NETDATA_CGROUP_THREAD_CREATE_CONTEXT, NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5001);
  629. ebpf_write_chart_obsolete(type, NETDATA_SYSCALL_APPS_TASK_EXIT,"Tasks starts exit process.",
  630. EBPF_COMMON_DIMENSION_CALL, NETDATA_PROCESS_GROUP, NETDATA_EBPF_CHART_TYPE_LINE,
  631. NETDATA_CGROUP_PROCESS_EXIT_CONTEXT, NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5003);
  632. ebpf_write_chart_obsolete(type, NETDATA_SYSCALL_APPS_TASK_CLOSE,"Tasks closed",
  633. EBPF_COMMON_DIMENSION_CALL, NETDATA_PROCESS_GROUP, NETDATA_EBPF_CHART_TYPE_LINE,
  634. NETDATA_CGROUP_PROCESS_CLOSE_CONTEXT, NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5003);
  635. if (em->mode < MODE_ENTRY) {
  636. ebpf_write_chart_obsolete(type, NETDATA_SYSCALL_APPS_TASK_ERROR,"Errors to create process or threads.",
  637. EBPF_COMMON_DIMENSION_CALL, NETDATA_PROCESS_GROUP, NETDATA_EBPF_CHART_TYPE_LINE,
  638. NETDATA_CGROUP_PROCESS_ERROR_CONTEXT, NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5004);
  639. }
  640. }
  641. /**
  642. * Create Systemd process Charts
  643. *
  644. * Create charts when systemd is enabled
  645. *
  646. * @param em the structure with thread information
  647. **/
  648. static void ebpf_create_systemd_process_charts(ebpf_module_t *em)
  649. {
  650. ebpf_create_charts_on_systemd(NETDATA_SYSCALL_APPS_TASK_PROCESS, "Process started",
  651. EBPF_COMMON_DIMENSION_CALL, NETDATA_APPS_PROCESS_GROUP,
  652. NETDATA_EBPF_CHART_TYPE_STACKED, 20065,
  653. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX], NETDATA_SYSTEMD_PROCESS_CREATE_CONTEXT,
  654. NETDATA_EBPF_MODULE_NAME_PROCESS);
  655. ebpf_create_charts_on_systemd(NETDATA_SYSCALL_APPS_TASK_THREAD, "Threads started",
  656. EBPF_COMMON_DIMENSION_CALL, NETDATA_APPS_PROCESS_GROUP,
  657. NETDATA_EBPF_CHART_TYPE_STACKED, 20066,
  658. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX], NETDATA_SYSTEMD_THREAD_CREATE_CONTEXT,
  659. NETDATA_EBPF_MODULE_NAME_PROCESS);
  660. ebpf_create_charts_on_systemd(NETDATA_SYSCALL_APPS_TASK_CLOSE, "Tasks starts exit process.",
  661. EBPF_COMMON_DIMENSION_CALL, NETDATA_APPS_PROCESS_GROUP,
  662. NETDATA_EBPF_CHART_TYPE_STACKED, 20067,
  663. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX], NETDATA_SYSTEMD_PROCESS_EXIT_CONTEXT,
  664. NETDATA_EBPF_MODULE_NAME_PROCESS);
  665. ebpf_create_charts_on_systemd(NETDATA_SYSCALL_APPS_TASK_EXIT, "Tasks closed",
  666. EBPF_COMMON_DIMENSION_CALL, NETDATA_APPS_PROCESS_GROUP,
  667. NETDATA_EBPF_CHART_TYPE_STACKED, 20068,
  668. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX], NETDATA_SYSTEMD_PROCESS_CLOSE_CONTEXT,
  669. NETDATA_EBPF_MODULE_NAME_PROCESS);
  670. if (em->mode < MODE_ENTRY) {
  671. ebpf_create_charts_on_systemd(NETDATA_SYSCALL_APPS_TASK_ERROR, "Errors to create process or threads.",
  672. EBPF_COMMON_DIMENSION_CALL, NETDATA_APPS_PROCESS_GROUP,
  673. NETDATA_EBPF_CHART_TYPE_STACKED, 20069,
  674. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX], NETDATA_SYSTEMD_PROCESS_ERROR_CONTEXT,
  675. NETDATA_EBPF_MODULE_NAME_PROCESS);
  676. }
  677. }
  678. /**
  679. * Send Systemd charts
  680. *
  681. * Send collected data to Netdata.
  682. *
  683. * @param em the structure with thread information
  684. *
  685. * @return It returns the status for chart creation, if it is necessary to remove a specific dimension, zero is returned
  686. * otherwise function returns 1 to avoid chart recreation
  687. */
  688. static int ebpf_send_systemd_process_charts(ebpf_module_t *em)
  689. {
  690. int ret = 1;
  691. ebpf_cgroup_target_t *ect;
  692. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_SYSCALL_APPS_TASK_PROCESS);
  693. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  694. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  695. write_chart_dimension(ect->name, ect->publish_systemd_ps.create_process);
  696. } else
  697. ret = 0;
  698. }
  699. write_end_chart();
  700. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_SYSCALL_APPS_TASK_THREAD);
  701. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  702. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  703. write_chart_dimension(ect->name, ect->publish_systemd_ps.create_thread);
  704. }
  705. }
  706. write_end_chart();
  707. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_SYSCALL_APPS_TASK_EXIT);
  708. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  709. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  710. write_chart_dimension(ect->name, ect->publish_systemd_ps.exit_call);
  711. }
  712. }
  713. write_end_chart();
  714. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_SYSCALL_APPS_TASK_CLOSE);
  715. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  716. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  717. write_chart_dimension(ect->name, ect->publish_systemd_ps.release_call);
  718. }
  719. }
  720. write_end_chart();
  721. if (em->mode < MODE_ENTRY) {
  722. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_SYSCALL_APPS_TASK_ERROR);
  723. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  724. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  725. write_chart_dimension(ect->name, ect->publish_systemd_ps.task_err);
  726. }
  727. }
  728. write_end_chart();
  729. }
  730. return ret;
  731. }
  732. /**
  733. * Send data to Netdata calling auxiliar functions.
  734. *
  735. * @param em the structure with thread information
  736. */
  737. static void ebpf_process_send_cgroup_data(ebpf_module_t *em)
  738. {
  739. if (!ebpf_cgroup_pids)
  740. return;
  741. pthread_mutex_lock(&mutex_cgroup_shm);
  742. ebpf_cgroup_target_t *ect;
  743. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  744. ebpf_process_sum_cgroup_pids(&ect->publish_systemd_ps, ect->pids);
  745. }
  746. int has_systemd = shm_ebpf_cgroup.header->systemd_enabled;
  747. if (has_systemd) {
  748. static int systemd_chart = 0;
  749. if (!systemd_chart) {
  750. ebpf_create_systemd_process_charts(em);
  751. systemd_chart = 1;
  752. }
  753. systemd_chart = ebpf_send_systemd_process_charts(em);
  754. }
  755. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  756. if (ect->systemd)
  757. continue;
  758. if (!(ect->flags & NETDATA_EBPF_CGROUP_HAS_PROCESS_CHART) && ect->updated) {
  759. ebpf_create_specific_process_charts(ect->name, em);
  760. ect->flags |= NETDATA_EBPF_CGROUP_HAS_PROCESS_CHART;
  761. }
  762. if (ect->flags & NETDATA_EBPF_CGROUP_HAS_PROCESS_CHART) {
  763. if (ect->updated) {
  764. ebpf_send_specific_process_data(ect->name, &ect->publish_systemd_ps, em);
  765. } else {
  766. ebpf_obsolete_specific_process_charts(ect->name, em);
  767. ect->flags &= ~NETDATA_EBPF_CGROUP_HAS_PROCESS_CHART;
  768. }
  769. }
  770. }
  771. pthread_mutex_unlock(&mutex_cgroup_shm);
  772. }
  773. /**
  774. * Update Cgroup algorithm
  775. *
  776. * Change algorithm from absolute to incremental
  777. */
  778. void ebpf_process_update_cgroup_algorithm()
  779. {
  780. int i;
  781. for (i = 0; i < NETDATA_KEY_PUBLISH_PROCESS_END; i++) {
  782. netdata_publish_syscall_t *ptr = &process_publish_aggregated[i];
  783. freez(ptr->algorithm);
  784. ptr->algorithm = strdupz(ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX]);
  785. }
  786. }
  787. /**
  788. * Main loop for this collector.
  789. *
  790. * @param step the number of microseconds used with heart beat
  791. * @param em the structure with thread information
  792. */
  793. static void process_collector(usec_t step, ebpf_module_t *em)
  794. {
  795. cgroup_thread.thread = mallocz(sizeof(netdata_thread_t));
  796. cgroup_thread.start_routine = ebpf_cgroup_update_shm;
  797. netdata_thread_create(cgroup_thread.thread, cgroup_thread.name, NETDATA_THREAD_OPTION_JOINABLE,
  798. ebpf_cgroup_update_shm, em);
  799. heartbeat_t hb;
  800. heartbeat_init(&hb);
  801. int publish_global = em->global_charts;
  802. int apps_enabled = em->apps_charts;
  803. int cgroups = em->cgroup_charts;
  804. if (cgroups)
  805. ebpf_process_update_cgroup_algorithm();
  806. int pid_fd = process_maps[NETDATA_PROCESS_PID_TABLE].map_fd;
  807. while (!close_ebpf_plugin) {
  808. usec_t dt = heartbeat_next(&hb, step);
  809. (void)dt;
  810. read_hash_global_tables();
  811. pthread_mutex_lock(&collect_data_mutex);
  812. cleanup_exited_pids();
  813. collect_data_for_all_processes(pid_fd);
  814. ebpf_create_apps_charts(apps_groups_root_target);
  815. pthread_cond_broadcast(&collect_data_cond_var);
  816. pthread_mutex_unlock(&collect_data_mutex);
  817. int publish_apps = 0;
  818. if (all_pids_count > 0) {
  819. if (apps_enabled) {
  820. publish_apps = 1;
  821. ebpf_process_update_apps_data();
  822. }
  823. if (cgroups) {
  824. ebpf_update_process_cgroup();
  825. }
  826. }
  827. pthread_mutex_lock(&lock);
  828. if (publish_global) {
  829. ebpf_process_send_data(em);
  830. }
  831. if (publish_apps) {
  832. ebpf_process_send_apps_data(apps_groups_root_target, em);
  833. }
  834. if (cgroups) {
  835. ebpf_process_send_cgroup_data(em);
  836. }
  837. pthread_mutex_unlock(&lock);
  838. fflush(stdout);
  839. }
  840. }
  841. /*****************************************************************
  842. *
  843. * FUNCTIONS TO CLOSE THE THREAD
  844. *
  845. *****************************************************************/
  846. void clean_global_memory() {
  847. int pid_fd = process_maps[NETDATA_PROCESS_PID_TABLE].map_fd;
  848. struct pid_stat *pids = root_of_pids;
  849. while (pids) {
  850. uint32_t pid = pids->pid;
  851. freez(global_process_stats[pid]);
  852. bpf_map_delete_elem(pid_fd, &pid);
  853. freez(current_apps_data[pid]);
  854. pids = pids->next;
  855. }
  856. }
  857. /**
  858. * Process disable tracepoints
  859. *
  860. * Disable tracepoints when the plugin was responsible to enable it.
  861. */
  862. static void ebpf_process_disable_tracepoints()
  863. {
  864. char *default_message = { "Cannot disable the tracepoint" };
  865. if (!was_sched_process_exit_enabled) {
  866. if (ebpf_disable_tracing_values(tracepoint_sched_type, tracepoint_sched_process_exit))
  867. error("%s %s/%s.", default_message, tracepoint_sched_type, tracepoint_sched_process_exit);
  868. }
  869. if (!was_sched_process_exec_enabled) {
  870. if (ebpf_disable_tracing_values(tracepoint_sched_type, tracepoint_sched_process_exec))
  871. error("%s %s/%s.", default_message, tracepoint_sched_type, tracepoint_sched_process_exec);
  872. }
  873. if (!was_sched_process_fork_enabled) {
  874. if (ebpf_disable_tracing_values(tracepoint_sched_type, tracepoint_sched_process_fork))
  875. error("%s %s/%s.", default_message, tracepoint_sched_type, tracepoint_sched_process_fork);
  876. }
  877. }
  878. /**
  879. * Clean up the main thread.
  880. *
  881. * @param ptr thread data.
  882. */
  883. static void ebpf_process_cleanup(void *ptr)
  884. {
  885. UNUSED(ptr);
  886. heartbeat_t hb;
  887. heartbeat_init(&hb);
  888. uint32_t tick = 50*USEC_PER_MS;
  889. while (!finalized_threads) {
  890. usec_t dt = heartbeat_next(&hb, tick);
  891. UNUSED(dt);
  892. }
  893. ebpf_cleanup_publish_syscall(process_publish_aggregated);
  894. freez(process_hash_values);
  895. clean_global_memory();
  896. freez(global_process_stats);
  897. freez(current_apps_data);
  898. ebpf_process_disable_tracepoints();
  899. if (probe_links) {
  900. struct bpf_program *prog;
  901. size_t i = 0 ;
  902. bpf_object__for_each_program(prog, objects) {
  903. bpf_link__destroy(probe_links[i]);
  904. i++;
  905. }
  906. bpf_object__close(objects);
  907. }
  908. freez(cgroup_thread.thread);
  909. }
  910. /*****************************************************************
  911. *
  912. * FUNCTIONS TO START THREAD
  913. *
  914. *****************************************************************/
  915. /**
  916. * Allocate vectors used with this thread.
  917. * We are not testing the return, because callocz does this and shutdown the software
  918. * case it was not possible to allocate.
  919. *
  920. * @param length is the length for the vectors used inside the collector.
  921. */
  922. static void ebpf_process_allocate_global_vectors(size_t length)
  923. {
  924. memset(process_aggregated_data, 0, length * sizeof(netdata_syscall_stat_t));
  925. memset(process_publish_aggregated, 0, length * sizeof(netdata_publish_syscall_t));
  926. process_hash_values = callocz(ebpf_nprocs, sizeof(netdata_idx_t));
  927. global_process_stats = callocz((size_t)pid_max, sizeof(ebpf_process_stat_t *));
  928. current_apps_data = callocz((size_t)pid_max, sizeof(ebpf_process_publish_apps_t *));
  929. }
  930. static void change_syscalls()
  931. {
  932. static char *lfork = { "do_fork" };
  933. process_id_names[NETDATA_KEY_PUBLISH_PROCESS_FORK] = lfork;
  934. }
  935. /**
  936. * Set local variables
  937. *
  938. */
  939. static void set_local_pointers()
  940. {
  941. if (isrh >= NETDATA_MINIMUM_RH_VERSION && isrh < NETDATA_RH_8)
  942. change_syscalls();
  943. }
  944. /*****************************************************************
  945. *
  946. * EBPF PROCESS THREAD
  947. *
  948. *****************************************************************/
  949. /**
  950. *
  951. */
  952. static void wait_for_all_threads_die()
  953. {
  954. ebpf_modules[EBPF_MODULE_PROCESS_IDX].enabled = 0;
  955. heartbeat_t hb;
  956. heartbeat_init(&hb);
  957. int max = 10;
  958. int i;
  959. for (i = 0; i < max; i++) {
  960. heartbeat_next(&hb, 200000);
  961. size_t j, counter = 0, compare = 0;
  962. for (j = 0; ebpf_modules[j].thread_name; j++) {
  963. if (!ebpf_modules[j].enabled)
  964. counter++;
  965. compare++;
  966. }
  967. if (counter == compare)
  968. break;
  969. }
  970. }
  971. /**
  972. * Enable tracepoints
  973. *
  974. * Enable necessary tracepoints for thread.
  975. *
  976. * @return It returns 0 on success and -1 otherwise
  977. */
  978. static int ebpf_process_enable_tracepoints()
  979. {
  980. int test = ebpf_is_tracepoint_enabled(tracepoint_sched_type, tracepoint_sched_process_exit);
  981. if (test == -1)
  982. return -1;
  983. else if (!test) {
  984. if (ebpf_enable_tracing_values(tracepoint_sched_type, tracepoint_sched_process_exit))
  985. return -1;
  986. }
  987. was_sched_process_exit_enabled = test;
  988. test = ebpf_is_tracepoint_enabled(tracepoint_sched_type, tracepoint_sched_process_exec);
  989. if (test == -1)
  990. return -1;
  991. else if (!test) {
  992. if (ebpf_enable_tracing_values(tracepoint_sched_type, tracepoint_sched_process_exec))
  993. return -1;
  994. }
  995. was_sched_process_exec_enabled = test;
  996. test = ebpf_is_tracepoint_enabled(tracepoint_sched_type, tracepoint_sched_process_fork);
  997. if (test == -1)
  998. return -1;
  999. else if (!test) {
  1000. if (ebpf_enable_tracing_values(tracepoint_sched_type, tracepoint_sched_process_fork))
  1001. return -1;
  1002. }
  1003. was_sched_process_fork_enabled = test;
  1004. return 0;
  1005. }
  1006. /**
  1007. * Process thread
  1008. *
  1009. * Thread used to generate process charts.
  1010. *
  1011. * @param ptr a pointer to `struct ebpf_module`
  1012. *
  1013. * @return It always return NULL
  1014. */
  1015. void *ebpf_process_thread(void *ptr)
  1016. {
  1017. netdata_thread_cleanup_push(ebpf_process_cleanup, ptr);
  1018. ebpf_module_t *em = (ebpf_module_t *)ptr;
  1019. em->maps = process_maps;
  1020. if (ebpf_process_enable_tracepoints()) {
  1021. em->enabled = em->global_charts = em->apps_charts = em->cgroup_charts = CONFIG_BOOLEAN_NO;
  1022. }
  1023. process_enabled = em->enabled;
  1024. pthread_mutex_lock(&lock);
  1025. ebpf_process_allocate_global_vectors(NETDATA_KEY_PUBLISH_PROCESS_END);
  1026. ebpf_update_pid_table(&process_maps[0], em);
  1027. set_local_pointers();
  1028. probe_links = ebpf_load_program(ebpf_plugin_dir, em, kernel_string, &objects);
  1029. if (!probe_links) {
  1030. pthread_mutex_unlock(&lock);
  1031. goto endprocess;
  1032. }
  1033. int algorithms[NETDATA_KEY_PUBLISH_PROCESS_END] = {
  1034. NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_ABSOLUTE_IDX
  1035. };
  1036. ebpf_global_labels(
  1037. process_aggregated_data, process_publish_aggregated, process_dimension_names, process_id_names,
  1038. algorithms, NETDATA_KEY_PUBLISH_PROCESS_END);
  1039. if (process_enabled) {
  1040. ebpf_create_global_charts(em);
  1041. }
  1042. pthread_mutex_unlock(&lock);
  1043. process_collector((usec_t)(em->update_time * USEC_PER_SEC), em);
  1044. endprocess:
  1045. wait_for_all_threads_die();
  1046. netdata_thread_cleanup_pop(1);
  1047. return NULL;
  1048. }