ebpf_apps.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "ebpf.h"
  3. #include "ebpf_socket.h"
  4. #include "ebpf_apps.h"
  5. // ----------------------------------------------------------------------------
  6. // ARAL vectors used to speed up processing
  7. ARAL *ebpf_aral_apps_pid_stat = NULL;
  8. ARAL *ebpf_aral_process_stat = NULL;
  9. ARAL *ebpf_aral_socket_pid = NULL;
  10. ARAL *ebpf_aral_cachestat_pid = NULL;
  11. ARAL *ebpf_aral_dcstat_pid = NULL;
  12. ARAL *ebpf_aral_vfs_pid = NULL;
  13. ARAL *ebpf_aral_fd_pid = NULL;
  14. ARAL *ebpf_aral_shm_pid = NULL;
  15. // ----------------------------------------------------------------------------
  16. // Global vectors used with apps
  17. ebpf_socket_publish_apps_t **socket_bandwidth_curr = NULL;
  18. netdata_publish_cachestat_t **cachestat_pid = NULL;
  19. netdata_publish_dcstat_t **dcstat_pid = NULL;
  20. netdata_publish_swap_t **swap_pid = NULL;
  21. netdata_publish_vfs_t **vfs_pid = NULL;
  22. netdata_fd_stat_t **fd_pid = NULL;
  23. netdata_publish_shm_t **shm_pid = NULL;
  24. ebpf_process_stat_t **global_process_stats = NULL;
  25. /**
  26. * eBPF ARAL Init
  27. *
  28. * Initiallize array allocator that will be used when integration with apps and ebpf is created.
  29. */
  30. void ebpf_aral_init(void)
  31. {
  32. size_t max_elements = NETDATA_EBPF_ALLOC_MAX_PID;
  33. if (max_elements < NETDATA_EBPF_ALLOC_MIN_ELEMENTS) {
  34. netdata_log_error("Number of elements given is too small, adjusting it for %d", NETDATA_EBPF_ALLOC_MIN_ELEMENTS);
  35. max_elements = NETDATA_EBPF_ALLOC_MIN_ELEMENTS;
  36. }
  37. ebpf_aral_apps_pid_stat = ebpf_allocate_pid_aral("ebpf_pid_stat", sizeof(struct ebpf_pid_stat));
  38. ebpf_aral_process_stat = ebpf_allocate_pid_aral(NETDATA_EBPF_PROC_ARAL_NAME, sizeof(ebpf_process_stat_t));
  39. #ifdef NETDATA_DEV_MODE
  40. netdata_log_info("Plugin is using ARAL with values %d", NETDATA_EBPF_ALLOC_MAX_PID);
  41. #endif
  42. }
  43. /**
  44. * eBPF pid stat get
  45. *
  46. * Get a ebpf_pid_stat entry to be used with a specific PID.
  47. *
  48. * @return it returns the address on success.
  49. */
  50. struct ebpf_pid_stat *ebpf_pid_stat_get(void)
  51. {
  52. struct ebpf_pid_stat *target = aral_mallocz(ebpf_aral_apps_pid_stat);
  53. memset(target, 0, sizeof(struct ebpf_pid_stat));
  54. return target;
  55. }
  56. /**
  57. * eBPF target release
  58. *
  59. * @param stat Release a target after usage.
  60. */
  61. void ebpf_pid_stat_release(struct ebpf_pid_stat *stat)
  62. {
  63. aral_freez(ebpf_aral_apps_pid_stat, stat);
  64. }
  65. /*****************************************************************
  66. *
  67. * PROCESS ARAL FUNCTIONS
  68. *
  69. *****************************************************************/
  70. /**
  71. * eBPF process stat get
  72. *
  73. * Get a ebpf_pid_stat entry to be used with a specific PID.
  74. *
  75. * @return it returns the address on success.
  76. */
  77. ebpf_process_stat_t *ebpf_process_stat_get(void)
  78. {
  79. ebpf_process_stat_t *target = aral_mallocz(ebpf_aral_process_stat);
  80. memset(target, 0, sizeof(ebpf_process_stat_t));
  81. return target;
  82. }
  83. /**
  84. * eBPF process release
  85. *
  86. * @param stat Release a target after usage.
  87. */
  88. void ebpf_process_stat_release(ebpf_process_stat_t *stat)
  89. {
  90. aral_freez(ebpf_aral_process_stat, stat);
  91. }
  92. /*****************************************************************
  93. *
  94. * SOCKET ARAL FUNCTIONS
  95. *
  96. *****************************************************************/
  97. /**
  98. * eBPF socket Aral init
  99. *
  100. * Initiallize array allocator that will be used when integration with apps is enabled.
  101. */
  102. void ebpf_socket_aral_init()
  103. {
  104. ebpf_aral_socket_pid = ebpf_allocate_pid_aral(NETDATA_EBPF_SOCKET_ARAL_NAME, sizeof(ebpf_socket_publish_apps_t));
  105. }
  106. /**
  107. * eBPF socket get
  108. *
  109. * Get a ebpf_socket_publish_apps_t entry to be used with a specific PID.
  110. *
  111. * @return it returns the address on success.
  112. */
  113. ebpf_socket_publish_apps_t *ebpf_socket_stat_get(void)
  114. {
  115. ebpf_socket_publish_apps_t *target = aral_mallocz(ebpf_aral_socket_pid);
  116. memset(target, 0, sizeof(ebpf_socket_publish_apps_t));
  117. return target;
  118. }
  119. /*****************************************************************
  120. *
  121. * CACHESTAT ARAL FUNCTIONS
  122. *
  123. *****************************************************************/
  124. /**
  125. * eBPF Cachestat Aral init
  126. *
  127. * Initiallize array allocator that will be used when integration with apps is enabled.
  128. */
  129. void ebpf_cachestat_aral_init()
  130. {
  131. ebpf_aral_cachestat_pid = ebpf_allocate_pid_aral(NETDATA_EBPF_CACHESTAT_ARAL_NAME, sizeof(netdata_publish_cachestat_t));
  132. }
  133. /**
  134. * eBPF publish cachestat get
  135. *
  136. * Get a netdata_publish_cachestat_t entry to be used with a specific PID.
  137. *
  138. * @return it returns the address on success.
  139. */
  140. netdata_publish_cachestat_t *ebpf_publish_cachestat_get(void)
  141. {
  142. netdata_publish_cachestat_t *target = aral_mallocz(ebpf_aral_cachestat_pid);
  143. memset(target, 0, sizeof(netdata_publish_cachestat_t));
  144. return target;
  145. }
  146. /**
  147. * eBPF cachestat release
  148. *
  149. * @param stat Release a target after usage.
  150. */
  151. void ebpf_cachestat_release(netdata_publish_cachestat_t *stat)
  152. {
  153. aral_freez(ebpf_aral_cachestat_pid, stat);
  154. }
  155. /*****************************************************************
  156. *
  157. * DCSTAT ARAL FUNCTIONS
  158. *
  159. *****************************************************************/
  160. /**
  161. * eBPF directory cache Aral init
  162. *
  163. * Initiallize array allocator that will be used when integration with apps is enabled.
  164. */
  165. void ebpf_dcstat_aral_init()
  166. {
  167. ebpf_aral_dcstat_pid = ebpf_allocate_pid_aral(NETDATA_EBPF_DCSTAT_ARAL_NAME, sizeof(netdata_publish_dcstat_t));
  168. }
  169. /**
  170. * eBPF publish dcstat get
  171. *
  172. * Get a netdata_publish_dcstat_t entry to be used with a specific PID.
  173. *
  174. * @return it returns the address on success.
  175. */
  176. netdata_publish_dcstat_t *ebpf_publish_dcstat_get(void)
  177. {
  178. netdata_publish_dcstat_t *target = aral_mallocz(ebpf_aral_dcstat_pid);
  179. memset(target, 0, sizeof(netdata_publish_dcstat_t));
  180. return target;
  181. }
  182. /**
  183. * eBPF dcstat release
  184. *
  185. * @param stat Release a target after usage.
  186. */
  187. void ebpf_dcstat_release(netdata_publish_dcstat_t *stat)
  188. {
  189. aral_freez(ebpf_aral_dcstat_pid, stat);
  190. }
  191. /*****************************************************************
  192. *
  193. * VFS ARAL FUNCTIONS
  194. *
  195. *****************************************************************/
  196. /**
  197. * eBPF VFS Aral init
  198. *
  199. * Initiallize array allocator that will be used when integration with apps is enabled.
  200. */
  201. void ebpf_vfs_aral_init()
  202. {
  203. ebpf_aral_vfs_pid = ebpf_allocate_pid_aral(NETDATA_EBPF_VFS_ARAL_NAME, sizeof(netdata_publish_vfs_t));
  204. }
  205. /**
  206. * eBPF publish VFS get
  207. *
  208. * Get a netdata_publish_vfs_t entry to be used with a specific PID.
  209. *
  210. * @return it returns the address on success.
  211. */
  212. netdata_publish_vfs_t *ebpf_vfs_get(void)
  213. {
  214. netdata_publish_vfs_t *target = aral_mallocz(ebpf_aral_vfs_pid);
  215. memset(target, 0, sizeof(netdata_publish_vfs_t));
  216. return target;
  217. }
  218. /**
  219. * eBPF VFS release
  220. *
  221. * @param stat Release a target after usage.
  222. */
  223. void ebpf_vfs_release(netdata_publish_vfs_t *stat)
  224. {
  225. aral_freez(ebpf_aral_vfs_pid, stat);
  226. }
  227. /*****************************************************************
  228. *
  229. * FD ARAL FUNCTIONS
  230. *
  231. *****************************************************************/
  232. /**
  233. * eBPF file descriptor Aral init
  234. *
  235. * Initiallize array allocator that will be used when integration with apps is enabled.
  236. */
  237. void ebpf_fd_aral_init()
  238. {
  239. ebpf_aral_fd_pid = ebpf_allocate_pid_aral(NETDATA_EBPF_FD_ARAL_NAME, sizeof(netdata_fd_stat_t));
  240. }
  241. /**
  242. * eBPF publish file descriptor get
  243. *
  244. * Get a netdata_fd_stat_t entry to be used with a specific PID.
  245. *
  246. * @return it returns the address on success.
  247. */
  248. netdata_fd_stat_t *ebpf_fd_stat_get(void)
  249. {
  250. netdata_fd_stat_t *target = aral_mallocz(ebpf_aral_fd_pid);
  251. memset(target, 0, sizeof(netdata_fd_stat_t));
  252. return target;
  253. }
  254. /**
  255. * eBPF file descriptor release
  256. *
  257. * @param stat Release a target after usage.
  258. */
  259. void ebpf_fd_release(netdata_fd_stat_t *stat)
  260. {
  261. aral_freez(ebpf_aral_fd_pid, stat);
  262. }
  263. /*****************************************************************
  264. *
  265. * SHM ARAL FUNCTIONS
  266. *
  267. *****************************************************************/
  268. /**
  269. * eBPF shared memory Aral init
  270. *
  271. * Initiallize array allocator that will be used when integration with apps is enabled.
  272. */
  273. void ebpf_shm_aral_init()
  274. {
  275. ebpf_aral_shm_pid = ebpf_allocate_pid_aral(NETDATA_EBPF_SHM_ARAL_NAME, sizeof(netdata_publish_shm_t));
  276. }
  277. /**
  278. * eBPF shared memory get
  279. *
  280. * Get a netdata_publish_shm_t entry to be used with a specific PID.
  281. *
  282. * @return it returns the address on success.
  283. */
  284. netdata_publish_shm_t *ebpf_shm_stat_get(void)
  285. {
  286. netdata_publish_shm_t *target = aral_mallocz(ebpf_aral_shm_pid);
  287. memset(target, 0, sizeof(netdata_publish_shm_t));
  288. return target;
  289. }
  290. /**
  291. * eBPF shared memory release
  292. *
  293. * @param stat Release a target after usage.
  294. */
  295. void ebpf_shm_release(netdata_publish_shm_t *stat)
  296. {
  297. aral_freez(ebpf_aral_shm_pid, stat);
  298. }
  299. // ----------------------------------------------------------------------------
  300. // internal flags
  301. // handled in code (automatically set)
  302. static int proc_pid_cmdline_is_needed = 0; // 1 when we need to read /proc/cmdline
  303. /*****************************************************************
  304. *
  305. * FUNCTIONS USED TO READ HASH TABLES
  306. *
  307. *****************************************************************/
  308. /**
  309. * Read statistic hash table.
  310. *
  311. * @param ep the output structure.
  312. * @param fd the file descriptor mapped from kernel ring.
  313. * @param pid the index used to select the data.
  314. * @param bpf_map_lookup_elem a pointer for the function used to read data.
  315. *
  316. * @return It returns 0 when the data was copied and -1 otherwise
  317. */
  318. int ebpf_read_hash_table(void *ep, int fd, uint32_t pid)
  319. {
  320. if (!ep)
  321. return -1;
  322. if (!bpf_map_lookup_elem(fd, &pid, ep))
  323. return 0;
  324. return -1;
  325. }
  326. /*****************************************************************
  327. *
  328. * FUNCTIONS CALLED FROM COLLECTORS
  329. *
  330. *****************************************************************/
  331. /**
  332. * Am I running as Root
  333. *
  334. * Verify the user that is running the collector.
  335. *
  336. * @return It returns 1 for root and 0 otherwise.
  337. */
  338. int am_i_running_as_root()
  339. {
  340. uid_t uid = getuid(), euid = geteuid();
  341. if (uid == 0 || euid == 0) {
  342. return 1;
  343. }
  344. return 0;
  345. }
  346. /**
  347. * Reset the target values
  348. *
  349. * @param root the pointer to the chain that will be reset.
  350. *
  351. * @return it returns the number of structures that was reset.
  352. */
  353. size_t zero_all_targets(struct ebpf_target *root)
  354. {
  355. struct ebpf_target *w;
  356. size_t count = 0;
  357. for (w = root; w; w = w->next) {
  358. count++;
  359. if (unlikely(w->root_pid)) {
  360. struct ebpf_pid_on_target *pid_on_target = w->root_pid;
  361. while (pid_on_target) {
  362. struct ebpf_pid_on_target *pid_on_target_to_free = pid_on_target;
  363. pid_on_target = pid_on_target->next;
  364. freez(pid_on_target_to_free);
  365. }
  366. w->root_pid = NULL;
  367. }
  368. }
  369. return count;
  370. }
  371. /**
  372. * Clean the allocated structures
  373. *
  374. * @param agrt the pointer to be cleaned.
  375. */
  376. void clean_apps_groups_target(struct ebpf_target *agrt)
  377. {
  378. struct ebpf_target *current_target;
  379. while (agrt) {
  380. current_target = agrt;
  381. agrt = current_target->target;
  382. freez(current_target);
  383. }
  384. }
  385. /**
  386. * Find or create a new target
  387. * there are targets that are just aggregated to other target (the second argument)
  388. *
  389. * @param id
  390. * @param target
  391. * @param name
  392. *
  393. * @return It returns the target on success and NULL otherwise
  394. */
  395. struct ebpf_target *get_apps_groups_target(struct ebpf_target **agrt, const char *id, struct ebpf_target *target, const char *name)
  396. {
  397. int tdebug = 0, thidden = target ? target->hidden : 0, ends_with = 0;
  398. const char *nid = id;
  399. // extract the options
  400. while (nid[0] == '-' || nid[0] == '+' || nid[0] == '*') {
  401. if (nid[0] == '-')
  402. thidden = 1;
  403. if (nid[0] == '+')
  404. tdebug = 1;
  405. if (nid[0] == '*')
  406. ends_with = 1;
  407. nid++;
  408. }
  409. uint32_t hash = simple_hash(id);
  410. // find if it already exists
  411. struct ebpf_target *w, *last = *agrt;
  412. for (w = *agrt; w; w = w->next) {
  413. if (w->idhash == hash && strncmp(nid, w->id, EBPF_MAX_NAME) == 0)
  414. return w;
  415. last = w;
  416. }
  417. // find an existing target
  418. if (unlikely(!target)) {
  419. while (*name == '-') {
  420. if (*name == '-')
  421. thidden = 1;
  422. name++;
  423. }
  424. for (target = *agrt; target != NULL; target = target->next) {
  425. if (!target->target && strcmp(name, target->name) == 0)
  426. break;
  427. }
  428. }
  429. if (target && target->target)
  430. fatal(
  431. "Internal Error: request to link process '%s' to target '%s' which is linked to target '%s'", id,
  432. target->id, target->target->id);
  433. w = callocz(1, sizeof(struct ebpf_target));
  434. strncpyz(w->id, nid, EBPF_MAX_NAME);
  435. w->idhash = simple_hash(w->id);
  436. if (unlikely(!target))
  437. // copy the name
  438. strncpyz(w->name, name, EBPF_MAX_NAME);
  439. else
  440. // copy the id
  441. strncpyz(w->name, nid, EBPF_MAX_NAME);
  442. strncpyz(w->clean_name, w->name, EBPF_MAX_NAME);
  443. netdata_fix_chart_name(w->clean_name);
  444. for (char *d = w->clean_name; *d; d++) {
  445. if (*d == '.')
  446. *d = '_';
  447. }
  448. strncpyz(w->compare, nid, EBPF_MAX_COMPARE_NAME);
  449. size_t len = strlen(w->compare);
  450. if (w->compare[len - 1] == '*') {
  451. w->compare[len - 1] = '\0';
  452. w->starts_with = 1;
  453. }
  454. w->ends_with = ends_with;
  455. if (w->starts_with && w->ends_with)
  456. proc_pid_cmdline_is_needed = 1;
  457. w->comparehash = simple_hash(w->compare);
  458. w->comparelen = strlen(w->compare);
  459. w->hidden = thidden;
  460. #ifdef NETDATA_INTERNAL_CHECKS
  461. w->debug_enabled = tdebug;
  462. #else
  463. if (tdebug)
  464. fprintf(stderr, "apps.plugin has been compiled without debugging\n");
  465. #endif
  466. w->target = target;
  467. // append it, to maintain the order in apps_groups.conf
  468. if (last)
  469. last->next = w;
  470. else
  471. *agrt = w;
  472. return w;
  473. }
  474. /**
  475. * Read the apps_groups.conf file
  476. *
  477. * @param agrt a pointer to apps_group_root_target
  478. * @param path the directory to search apps_%s.conf
  479. * @param file the word to complement the file name.
  480. *
  481. * @return It returns 0 on success and -1 otherwise
  482. */
  483. int ebpf_read_apps_groups_conf(struct ebpf_target **agdt, struct ebpf_target **agrt, const char *path, const char *file)
  484. {
  485. char filename[FILENAME_MAX + 1];
  486. snprintfz(filename, FILENAME_MAX, "%s/apps_%s.conf", path, file);
  487. // ----------------------------------------
  488. procfile *ff = procfile_open_no_log(filename, " :\t", PROCFILE_FLAG_DEFAULT);
  489. if (!ff)
  490. return -1;
  491. procfile_set_quotes(ff, "'\"");
  492. ff = procfile_readall(ff);
  493. if (!ff)
  494. return -1;
  495. size_t line, lines = procfile_lines(ff);
  496. for (line = 0; line < lines; line++) {
  497. size_t word, words = procfile_linewords(ff, line);
  498. if (!words)
  499. continue;
  500. char *name = procfile_lineword(ff, line, 0);
  501. if (!name || !*name)
  502. continue;
  503. // find a possibly existing target
  504. struct ebpf_target *w = NULL;
  505. // loop through all words, skipping the first one (the name)
  506. for (word = 0; word < words; word++) {
  507. char *s = procfile_lineword(ff, line, word);
  508. if (!s || !*s)
  509. continue;
  510. if (*s == '#')
  511. break;
  512. // is this the first word? skip it
  513. if (s == name)
  514. continue;
  515. // add this target
  516. struct ebpf_target *n = get_apps_groups_target(agrt, s, w, name);
  517. if (!n) {
  518. netdata_log_error("Cannot create target '%s' (line %zu, word %zu)", s, line, word);
  519. continue;
  520. }
  521. // just some optimization
  522. // to avoid searching for a target for each process
  523. if (!w)
  524. w = n->target ? n->target : n;
  525. }
  526. }
  527. procfile_close(ff);
  528. *agdt = get_apps_groups_target(agrt, "p+!o@w#e$i^r&7*5(-i)l-o_", NULL, "other"); // match nothing
  529. if (!*agdt)
  530. fatal("Cannot create default target");
  531. struct ebpf_target *ptr = *agdt;
  532. if (ptr->target)
  533. *agdt = ptr->target;
  534. return 0;
  535. }
  536. // the minimum PID of the system
  537. // this is also the pid of the init process
  538. #define INIT_PID 1
  539. // ----------------------------------------------------------------------------
  540. // string lengths
  541. #define MAX_CMDLINE 16384
  542. struct ebpf_pid_stat **ebpf_all_pids = NULL; // to avoid allocations, we pre-allocate the
  543. // the entire pid space.
  544. struct ebpf_pid_stat *ebpf_root_of_pids = NULL; // global list of all processes running
  545. size_t ebpf_all_pids_count = 0; // the number of processes running
  546. struct ebpf_target
  547. *apps_groups_default_target = NULL, // the default target
  548. *apps_groups_root_target = NULL, // apps_groups.conf defined
  549. *users_root_target = NULL, // users
  550. *groups_root_target = NULL; // user groups
  551. size_t apps_groups_targets_count = 0; // # of apps_groups.conf targets
  552. // ----------------------------------------------------------------------------
  553. // internal counters
  554. static size_t
  555. // global_iterations_counter = 1,
  556. calls_counter = 0,
  557. // file_counter = 0,
  558. // filenames_allocated_counter = 0,
  559. // inodes_changed_counter = 0,
  560. // links_changed_counter = 0,
  561. targets_assignment_counter = 0;
  562. // ----------------------------------------------------------------------------
  563. // debugging
  564. // log each problem once per process
  565. // log flood protection flags (log_thrown)
  566. #define PID_LOG_IO 0x00000001
  567. #define PID_LOG_STATUS 0x00000002
  568. #define PID_LOG_CMDLINE 0x00000004
  569. #define PID_LOG_FDS 0x00000008
  570. #define PID_LOG_STAT 0x00000010
  571. int debug_enabled = 0;
  572. #ifdef NETDATA_INTERNAL_CHECKS
  573. #define debug_log(fmt, args...) \
  574. do { \
  575. if (unlikely(debug_enabled)) \
  576. debug_log_int(fmt, ##args); \
  577. } while (0)
  578. #else
  579. static inline void debug_log_dummy(void)
  580. {
  581. }
  582. #define debug_log(fmt, args...) debug_log_dummy()
  583. #endif
  584. /**
  585. * Managed log
  586. *
  587. * Store log information if it is necessary.
  588. *
  589. * @param p the pid stat structure
  590. * @param log the log id
  591. * @param status the return from a function.
  592. *
  593. * @return It returns the status value.
  594. */
  595. static inline int managed_log(struct ebpf_pid_stat *p, uint32_t log, int status)
  596. {
  597. if (unlikely(!status)) {
  598. // netdata_log_error("command failed log %u, errno %d", log, errno);
  599. if (unlikely(debug_enabled || errno != ENOENT)) {
  600. if (unlikely(debug_enabled || !(p->log_thrown & log))) {
  601. p->log_thrown |= log;
  602. switch (log) {
  603. case PID_LOG_IO:
  604. netdata_log_error(
  605. "Cannot process %s/proc/%d/io (command '%s')", netdata_configured_host_prefix, p->pid,
  606. p->comm);
  607. break;
  608. case PID_LOG_STATUS:
  609. netdata_log_error(
  610. "Cannot process %s/proc/%d/status (command '%s')", netdata_configured_host_prefix, p->pid,
  611. p->comm);
  612. break;
  613. case PID_LOG_CMDLINE:
  614. netdata_log_error(
  615. "Cannot process %s/proc/%d/cmdline (command '%s')", netdata_configured_host_prefix, p->pid,
  616. p->comm);
  617. break;
  618. case PID_LOG_FDS:
  619. netdata_log_error(
  620. "Cannot process entries in %s/proc/%d/fd (command '%s')", netdata_configured_host_prefix,
  621. p->pid, p->comm);
  622. break;
  623. case PID_LOG_STAT:
  624. break;
  625. default:
  626. netdata_log_error("unhandled error for pid %d, command '%s'", p->pid, p->comm);
  627. break;
  628. }
  629. }
  630. }
  631. errno = 0;
  632. } else if (unlikely(p->log_thrown & log)) {
  633. // netdata_log_error("unsetting log %u on pid %d", log, p->pid);
  634. p->log_thrown &= ~log;
  635. }
  636. return status;
  637. }
  638. /**
  639. * Get PID entry
  640. *
  641. * Get or allocate the PID entry for the specified pid.
  642. *
  643. * @param pid the pid to search the data.
  644. *
  645. * @return It returns the pid entry structure
  646. */
  647. static inline struct ebpf_pid_stat *get_pid_entry(pid_t pid)
  648. {
  649. if (unlikely(ebpf_all_pids[pid]))
  650. return ebpf_all_pids[pid];
  651. struct ebpf_pid_stat *p = ebpf_pid_stat_get();
  652. if (likely(ebpf_root_of_pids))
  653. ebpf_root_of_pids->prev = p;
  654. p->next = ebpf_root_of_pids;
  655. ebpf_root_of_pids = p;
  656. p->pid = pid;
  657. ebpf_all_pids[pid] = p;
  658. ebpf_all_pids_count++;
  659. return p;
  660. }
  661. /**
  662. * Assign the PID to a target.
  663. *
  664. * @param p the pid_stat structure to assign for a target.
  665. */
  666. static inline void assign_target_to_pid(struct ebpf_pid_stat *p)
  667. {
  668. targets_assignment_counter++;
  669. uint32_t hash = simple_hash(p->comm);
  670. size_t pclen = strlen(p->comm);
  671. struct ebpf_target *w;
  672. for (w = apps_groups_root_target; w; w = w->next) {
  673. // if(debug_enabled || (p->target && p->target->debug_enabled)) debug_log_int("\t\tcomparing '%s' with '%s'", w->compare, p->comm);
  674. // find it - 4 cases:
  675. // 1. the target is not a pattern
  676. // 2. the target has the prefix
  677. // 3. the target has the suffix
  678. // 4. the target is something inside cmdline
  679. if (unlikely(
  680. ((!w->starts_with && !w->ends_with && w->comparehash == hash && !strcmp(w->compare, p->comm)) ||
  681. (w->starts_with && !w->ends_with && !strncmp(w->compare, p->comm, w->comparelen)) ||
  682. (!w->starts_with && w->ends_with && pclen >= w->comparelen && !strcmp(w->compare, &p->comm[pclen - w->comparelen])) ||
  683. (proc_pid_cmdline_is_needed && w->starts_with && w->ends_with && p->cmdline && strstr(p->cmdline, w->compare))))) {
  684. if (w->target)
  685. p->target = w->target;
  686. else
  687. p->target = w;
  688. if (debug_enabled || (p->target && p->target->debug_enabled))
  689. debug_log_int("%s linked to target %s", p->comm, p->target->name);
  690. break;
  691. }
  692. }
  693. }
  694. // ----------------------------------------------------------------------------
  695. // update pids from proc
  696. /**
  697. * Read cmd line from /proc/PID/cmdline
  698. *
  699. * @param p the ebpf_pid_stat_structure.
  700. *
  701. * @return It returns 1 on success and 0 otherwise.
  702. */
  703. static inline int read_proc_pid_cmdline(struct ebpf_pid_stat *p)
  704. {
  705. static char cmdline[MAX_CMDLINE + 1];
  706. int ret = 0;
  707. if (unlikely(!p->cmdline_filename)) {
  708. char filename[FILENAME_MAX + 1];
  709. snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", netdata_configured_host_prefix, p->pid);
  710. p->cmdline_filename = strdupz(filename);
  711. }
  712. int fd = open(p->cmdline_filename, procfile_open_flags, 0666);
  713. if (unlikely(fd == -1))
  714. goto cleanup;
  715. ssize_t i, bytes = read(fd, cmdline, MAX_CMDLINE);
  716. close(fd);
  717. if (unlikely(bytes < 0))
  718. goto cleanup;
  719. cmdline[bytes] = '\0';
  720. for (i = 0; i < bytes; i++) {
  721. if (unlikely(!cmdline[i]))
  722. cmdline[i] = ' ';
  723. }
  724. debug_log("Read file '%s' contents: %s", p->cmdline_filename, p->cmdline);
  725. ret = 1;
  726. cleanup:
  727. // copy the command to the command line
  728. if (p->cmdline)
  729. freez(p->cmdline);
  730. p->cmdline = strdupz(p->comm);
  731. rw_spinlock_write_lock(&ebpf_judy_pid.index.rw_spinlock);
  732. netdata_ebpf_judy_pid_stats_t *pid_ptr = ebpf_get_pid_from_judy_unsafe(&ebpf_judy_pid.index.JudyLArray, p->pid);
  733. if (pid_ptr)
  734. pid_ptr->cmdline = p->cmdline;
  735. rw_spinlock_write_unlock(&ebpf_judy_pid.index.rw_spinlock);
  736. return ret;
  737. }
  738. /**
  739. * Read information from /proc/PID/stat and /proc/PID/cmdline
  740. * Assign target to pid
  741. *
  742. * @param p the pid stat structure to store the data.
  743. * @param ptr an useless argument.
  744. */
  745. static inline int read_proc_pid_stat(struct ebpf_pid_stat *p, void *ptr)
  746. {
  747. UNUSED(ptr);
  748. static procfile *ff = NULL;
  749. if (unlikely(!p->stat_filename)) {
  750. char filename[FILENAME_MAX + 1];
  751. snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", netdata_configured_host_prefix, p->pid);
  752. p->stat_filename = strdupz(filename);
  753. }
  754. int set_quotes = (!ff) ? 1 : 0;
  755. struct stat statbuf;
  756. if (stat(p->stat_filename, &statbuf))
  757. return 0;
  758. ff = procfile_reopen(ff, p->stat_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
  759. if (unlikely(!ff))
  760. return 0;
  761. if (unlikely(set_quotes))
  762. procfile_set_open_close(ff, "(", ")");
  763. ff = procfile_readall(ff);
  764. if (unlikely(!ff))
  765. return 0;
  766. p->last_stat_collected_usec = p->stat_collected_usec;
  767. p->stat_collected_usec = now_monotonic_usec();
  768. calls_counter++;
  769. char *comm = procfile_lineword(ff, 0, 1);
  770. p->ppid = (int32_t)str2pid_t(procfile_lineword(ff, 0, 3));
  771. if (strcmp(p->comm, comm) != 0) {
  772. if (unlikely(debug_enabled)) {
  773. if (p->comm[0])
  774. debug_log("\tpid %d (%s) changed name to '%s'", p->pid, p->comm, comm);
  775. else
  776. debug_log("\tJust added %d (%s)", p->pid, comm);
  777. }
  778. strncpyz(p->comm, comm, EBPF_MAX_COMPARE_NAME);
  779. // /proc/<pid>/cmdline
  780. if (likely(proc_pid_cmdline_is_needed))
  781. managed_log(p, PID_LOG_CMDLINE, read_proc_pid_cmdline(p));
  782. assign_target_to_pid(p);
  783. }
  784. if (unlikely(debug_enabled || (p->target && p->target->debug_enabled)))
  785. debug_log_int(
  786. "READ PROC/PID/STAT: %s/proc/%d/stat, process: '%s' on target '%s' (dt=%llu)",
  787. netdata_configured_host_prefix, p->pid, p->comm, (p->target) ? p->target->name : "UNSET",
  788. p->stat_collected_usec - p->last_stat_collected_usec);
  789. return 1;
  790. }
  791. /**
  792. * Collect data for PID
  793. *
  794. * @param pid the current pid that we are working
  795. * @param ptr a NULL value
  796. *
  797. * @return It returns 1 on success and 0 otherwise
  798. */
  799. static inline int collect_data_for_pid(pid_t pid, void *ptr)
  800. {
  801. if (unlikely(pid < 0 || pid > pid_max)) {
  802. netdata_log_error("Invalid pid %d read (expected %d to %d). Ignoring process.", pid, 0, pid_max);
  803. return 0;
  804. }
  805. struct ebpf_pid_stat *p = get_pid_entry(pid);
  806. if (unlikely(!p || p->read))
  807. return 0;
  808. p->read = 1;
  809. if (unlikely(!managed_log(p, PID_LOG_STAT, read_proc_pid_stat(p, ptr))))
  810. // there is no reason to proceed if we cannot get its status
  811. return 0;
  812. // check its parent pid
  813. if (unlikely(p->ppid < 0 || p->ppid > pid_max)) {
  814. netdata_log_error("Pid %d (command '%s') states invalid parent pid %d. Using 0.", pid, p->comm, p->ppid);
  815. p->ppid = 0;
  816. }
  817. // mark it as updated
  818. p->updated = 1;
  819. p->keep = 0;
  820. p->keeploops = 0;
  821. return 1;
  822. }
  823. /**
  824. * Fill link list of parents with children PIDs
  825. */
  826. static inline void link_all_processes_to_their_parents(void)
  827. {
  828. struct ebpf_pid_stat *p, *pp;
  829. // link all children to their parents
  830. // and update children count on parents
  831. for (p = ebpf_root_of_pids; p; p = p->next) {
  832. // for each process found
  833. p->sortlist = 0;
  834. p->parent = NULL;
  835. if (unlikely(!p->ppid)) {
  836. p->parent = NULL;
  837. continue;
  838. }
  839. pp = ebpf_all_pids[p->ppid];
  840. if (likely(pp)) {
  841. p->parent = pp;
  842. pp->children_count++;
  843. if (unlikely(debug_enabled || (p->target && p->target->debug_enabled)))
  844. debug_log_int(
  845. "child %d (%s, %s) on target '%s' has parent %d (%s, %s).", p->pid, p->comm,
  846. p->updated ? "running" : "exited", (p->target) ? p->target->name : "UNSET", pp->pid, pp->comm,
  847. pp->updated ? "running" : "exited");
  848. } else {
  849. p->parent = NULL;
  850. debug_log("pid %d %s states parent %d, but the later does not exist.", p->pid, p->comm, p->ppid);
  851. }
  852. }
  853. }
  854. /**
  855. * Aggregate PIDs to targets.
  856. */
  857. static void apply_apps_groups_targets_inheritance(void)
  858. {
  859. struct ebpf_pid_stat *p = NULL;
  860. // children that do not have a target
  861. // inherit their target from their parent
  862. int found = 1, loops = 0;
  863. while (found) {
  864. if (unlikely(debug_enabled))
  865. loops++;
  866. found = 0;
  867. for (p = ebpf_root_of_pids; p; p = p->next) {
  868. // if this process does not have a target
  869. // and it has a parent
  870. // and its parent has a target
  871. // then, set the parent's target to this process
  872. if (unlikely(!p->target && p->parent && p->parent->target)) {
  873. p->target = p->parent->target;
  874. found++;
  875. if (debug_enabled || (p->target && p->target->debug_enabled))
  876. debug_log_int(
  877. "TARGET INHERITANCE: %s is inherited by %d (%s) from its parent %d (%s).", p->target->name,
  878. p->pid, p->comm, p->parent->pid, p->parent->comm);
  879. }
  880. }
  881. }
  882. // find all the procs with 0 childs and merge them to their parents
  883. // repeat, until nothing more can be done.
  884. int sortlist = 1;
  885. found = 1;
  886. while (found) {
  887. if (unlikely(debug_enabled))
  888. loops++;
  889. found = 0;
  890. for (p = ebpf_root_of_pids; p; p = p->next) {
  891. if (unlikely(!p->sortlist && !p->children_count))
  892. p->sortlist = sortlist++;
  893. if (unlikely(
  894. !p->children_count // if this process does not have any children
  895. && !p->merged // and is not already merged
  896. && p->parent // and has a parent
  897. && p->parent->children_count // and its parent has children
  898. // and the target of this process and its parent is the same,
  899. // or the parent does not have a target
  900. && (p->target == p->parent->target || !p->parent->target) &&
  901. p->ppid != INIT_PID // and its parent is not init
  902. )) {
  903. // mark it as merged
  904. p->parent->children_count--;
  905. p->merged = 1;
  906. // the parent inherits the child's target, if it does not have a target itself
  907. if (unlikely(p->target && !p->parent->target)) {
  908. p->parent->target = p->target;
  909. if (debug_enabled || (p->target && p->target->debug_enabled))
  910. debug_log_int(
  911. "TARGET INHERITANCE: %s is inherited by %d (%s) from its child %d (%s).", p->target->name,
  912. p->parent->pid, p->parent->comm, p->pid, p->comm);
  913. }
  914. found++;
  915. }
  916. }
  917. debug_log("TARGET INHERITANCE: merged %d processes", found);
  918. }
  919. // init goes always to default target
  920. if (ebpf_all_pids[INIT_PID])
  921. ebpf_all_pids[INIT_PID]->target = apps_groups_default_target;
  922. // pid 0 goes always to default target
  923. if (ebpf_all_pids[0])
  924. ebpf_all_pids[0]->target = apps_groups_default_target;
  925. // give a default target on all top level processes
  926. if (unlikely(debug_enabled))
  927. loops++;
  928. for (p = ebpf_root_of_pids; p; p = p->next) {
  929. // if the process is not merged itself
  930. // then is is a top level process
  931. if (unlikely(!p->merged && !p->target))
  932. p->target = apps_groups_default_target;
  933. // make sure all processes have a sortlist
  934. if (unlikely(!p->sortlist))
  935. p->sortlist = sortlist++;
  936. }
  937. if (ebpf_all_pids[1])
  938. ebpf_all_pids[1]->sortlist = sortlist++;
  939. // give a target to all merged child processes
  940. found = 1;
  941. while (found) {
  942. if (unlikely(debug_enabled))
  943. loops++;
  944. found = 0;
  945. for (p = ebpf_root_of_pids; p; p = p->next) {
  946. if (unlikely(!p->target && p->merged && p->parent && p->parent->target)) {
  947. p->target = p->parent->target;
  948. found++;
  949. if (debug_enabled || (p->target && p->target->debug_enabled))
  950. debug_log_int(
  951. "TARGET INHERITANCE: %s is inherited by %d (%s) from its parent %d (%s) at phase 2.",
  952. p->target->name, p->pid, p->comm, p->parent->pid, p->parent->comm);
  953. }
  954. }
  955. }
  956. debug_log("apply_apps_groups_targets_inheritance() made %d loops on the process tree", loops);
  957. }
  958. /**
  959. * Update target timestamp.
  960. *
  961. * @param root the targets that will be updated.
  962. */
  963. static inline void post_aggregate_targets(struct ebpf_target *root)
  964. {
  965. struct ebpf_target *w;
  966. for (w = root; w; w = w->next) {
  967. if (w->collected_starttime) {
  968. if (!w->starttime || w->collected_starttime < w->starttime) {
  969. w->starttime = w->collected_starttime;
  970. }
  971. } else {
  972. w->starttime = 0;
  973. }
  974. }
  975. }
  976. /**
  977. * Remove PID from the link list.
  978. *
  979. * @param pid the PID that will be removed.
  980. */
  981. static inline void del_pid_entry(pid_t pid)
  982. {
  983. struct ebpf_pid_stat *p = ebpf_all_pids[pid];
  984. if (unlikely(!p)) {
  985. netdata_log_error("attempted to free pid %d that is not allocated.", pid);
  986. return;
  987. }
  988. debug_log("process %d %s exited, deleting it.", pid, p->comm);
  989. if (ebpf_root_of_pids == p)
  990. ebpf_root_of_pids = p->next;
  991. if (p->next)
  992. p->next->prev = p->prev;
  993. if (p->prev)
  994. p->prev->next = p->next;
  995. freez(p->stat_filename);
  996. freez(p->status_filename);
  997. freez(p->io_filename);
  998. freez(p->cmdline_filename);
  999. rw_spinlock_write_lock(&ebpf_judy_pid.index.rw_spinlock);
  1000. netdata_ebpf_judy_pid_stats_t *pid_ptr = ebpf_get_pid_from_judy_unsafe(&ebpf_judy_pid.index.JudyLArray, p->pid);
  1001. if (pid_ptr) {
  1002. if (pid_ptr->socket_stats.JudyLArray) {
  1003. Word_t local_socket = 0;
  1004. Pvoid_t *socket_value;
  1005. bool first_socket = true;
  1006. while ((socket_value = JudyLFirstThenNext(pid_ptr->socket_stats.JudyLArray, &local_socket, &first_socket))) {
  1007. netdata_socket_plus_t *socket_clean = *socket_value;
  1008. aral_freez(aral_socket_table, socket_clean);
  1009. }
  1010. JudyLFreeArray(&pid_ptr->socket_stats.JudyLArray, PJE0);
  1011. }
  1012. aral_freez(ebpf_judy_pid.pid_table, pid_ptr);
  1013. JudyLDel(&ebpf_judy_pid.index.JudyLArray, p->pid, PJE0);
  1014. }
  1015. rw_spinlock_write_unlock(&ebpf_judy_pid.index.rw_spinlock);
  1016. freez(p->cmdline);
  1017. ebpf_pid_stat_release(p);
  1018. ebpf_all_pids[pid] = NULL;
  1019. ebpf_all_pids_count--;
  1020. }
  1021. /**
  1022. * Get command string associated with a PID.
  1023. * This can only safely be used when holding the `collect_data_mutex` lock.
  1024. *
  1025. * @param pid the pid to search the data.
  1026. * @param n the maximum amount of bytes to copy into dest.
  1027. * if this is greater than the size of the command, it is clipped.
  1028. * @param dest the target memory buffer to write the command into.
  1029. * @return -1 if the PID hasn't been scraped yet, 0 otherwise.
  1030. */
  1031. int get_pid_comm(pid_t pid, size_t n, char *dest)
  1032. {
  1033. struct ebpf_pid_stat *stat;
  1034. stat = ebpf_all_pids[pid];
  1035. if (unlikely(stat == NULL)) {
  1036. return -1;
  1037. }
  1038. if (unlikely(n > sizeof(stat->comm))) {
  1039. n = sizeof(stat->comm);
  1040. }
  1041. strncpyz(dest, stat->comm, n);
  1042. return 0;
  1043. }
  1044. /**
  1045. * Cleanup variable from other threads
  1046. *
  1047. * @param pid current pid.
  1048. */
  1049. void cleanup_variables_from_other_threads(uint32_t pid)
  1050. {
  1051. // Clean cachestat structure
  1052. if (cachestat_pid) {
  1053. ebpf_cachestat_release(cachestat_pid[pid]);
  1054. cachestat_pid[pid] = NULL;
  1055. }
  1056. // Clean directory cache structure
  1057. if (dcstat_pid) {
  1058. ebpf_dcstat_release(dcstat_pid[pid]);
  1059. dcstat_pid[pid] = NULL;
  1060. }
  1061. // Clean swap structure
  1062. if (swap_pid) {
  1063. freez(swap_pid[pid]);
  1064. swap_pid[pid] = NULL;
  1065. }
  1066. // Clean vfs structure
  1067. if (vfs_pid) {
  1068. ebpf_vfs_release(vfs_pid[pid]);
  1069. vfs_pid[pid] = NULL;
  1070. }
  1071. // Clean fd structure
  1072. if (fd_pid) {
  1073. ebpf_fd_release(fd_pid[pid]);
  1074. fd_pid[pid] = NULL;
  1075. }
  1076. // Clean shm structure
  1077. if (shm_pid) {
  1078. ebpf_shm_release(shm_pid[pid]);
  1079. shm_pid[pid] = NULL;
  1080. }
  1081. }
  1082. /**
  1083. * Remove PIDs when they are not running more.
  1084. */
  1085. void cleanup_exited_pids()
  1086. {
  1087. struct ebpf_pid_stat *p = NULL;
  1088. for (p = ebpf_root_of_pids; p;) {
  1089. if (!p->updated && (!p->keep || p->keeploops > 0)) {
  1090. if (unlikely(debug_enabled && (p->keep || p->keeploops)))
  1091. debug_log(" > CLEANUP cannot keep exited process %d (%s) anymore - removing it.", p->pid, p->comm);
  1092. pid_t r = p->pid;
  1093. p = p->next;
  1094. // Clean process structure
  1095. if (global_process_stats) {
  1096. ebpf_process_stat_release(global_process_stats[r]);
  1097. global_process_stats[r] = NULL;
  1098. }
  1099. cleanup_variables_from_other_threads(r);
  1100. del_pid_entry(r);
  1101. } else {
  1102. if (unlikely(p->keep))
  1103. p->keeploops++;
  1104. p->keep = 0;
  1105. p = p->next;
  1106. }
  1107. }
  1108. }
  1109. /**
  1110. * Read proc filesystem for the first time.
  1111. *
  1112. * @return It returns 0 on success and -1 otherwise.
  1113. */
  1114. static inline void read_proc_filesystem()
  1115. {
  1116. char dirname[FILENAME_MAX + 1];
  1117. snprintfz(dirname, FILENAME_MAX, "%s/proc", netdata_configured_host_prefix);
  1118. DIR *dir = opendir(dirname);
  1119. if (!dir)
  1120. return;
  1121. struct dirent *de = NULL;
  1122. while ((de = readdir(dir))) {
  1123. char *endptr = de->d_name;
  1124. if (unlikely(de->d_type != DT_DIR || de->d_name[0] < '0' || de->d_name[0] > '9'))
  1125. continue;
  1126. pid_t pid = (pid_t)strtoul(de->d_name, &endptr, 10);
  1127. // make sure we read a valid number
  1128. if (unlikely(endptr == de->d_name || *endptr != '\0'))
  1129. continue;
  1130. collect_data_for_pid(pid, NULL);
  1131. }
  1132. closedir(dir);
  1133. }
  1134. /**
  1135. * Aggregated PID on target
  1136. *
  1137. * @param w the target output
  1138. * @param p the pid with information to update
  1139. * @param o never used
  1140. */
  1141. static inline void aggregate_pid_on_target(struct ebpf_target *w, struct ebpf_pid_stat *p, struct ebpf_target *o)
  1142. {
  1143. UNUSED(o);
  1144. if (unlikely(!p->updated)) {
  1145. // the process is not running
  1146. return;
  1147. }
  1148. if (unlikely(!w)) {
  1149. netdata_log_error("pid %d %s was left without a target!", p->pid, p->comm);
  1150. return;
  1151. }
  1152. w->processes++;
  1153. struct ebpf_pid_on_target *pid_on_target = mallocz(sizeof(struct ebpf_pid_on_target));
  1154. pid_on_target->pid = p->pid;
  1155. pid_on_target->next = w->root_pid;
  1156. w->root_pid = pid_on_target;
  1157. }
  1158. /**
  1159. * Process Accumulator
  1160. *
  1161. * Sum all values read from kernel and store in the first address.
  1162. *
  1163. * @param out the vector with read values.
  1164. * @param maps_per_core do I need to read all cores?
  1165. */
  1166. void ebpf_process_apps_accumulator(ebpf_process_stat_t *out, int maps_per_core)
  1167. {
  1168. int i, end = (maps_per_core) ? ebpf_nprocs : 1;
  1169. ebpf_process_stat_t *total = &out[0];
  1170. for (i = 1; i < end; i++) {
  1171. ebpf_process_stat_t *w = &out[i];
  1172. total->exit_call += w->exit_call;
  1173. total->task_err += w->task_err;
  1174. total->create_thread += w->create_thread;
  1175. total->create_process += w->create_process;
  1176. total->release_call += w->release_call;
  1177. }
  1178. }
  1179. /**
  1180. * Collect data for all process
  1181. *
  1182. * Read data from hash table and store it in appropriate vectors.
  1183. * It also creates the link between targets and PIDs.
  1184. *
  1185. * @param tbl_pid_stats_fd The mapped file descriptor for the hash table.
  1186. * @param maps_per_core do I have hash maps per core?
  1187. */
  1188. void collect_data_for_all_processes(int tbl_pid_stats_fd, int maps_per_core)
  1189. {
  1190. if (unlikely(!ebpf_all_pids))
  1191. return;
  1192. struct ebpf_pid_stat *pids = ebpf_root_of_pids; // global list of all processes running
  1193. while (pids) {
  1194. if (pids->updated_twice) {
  1195. pids->read = 0; // mark it as not read, so that collect_data_for_pid() will read it
  1196. pids->updated = 0;
  1197. pids->merged = 0;
  1198. pids->children_count = 0;
  1199. pids->parent = NULL;
  1200. } else {
  1201. if (pids->updated)
  1202. pids->updated_twice = 1;
  1203. }
  1204. pids = pids->next;
  1205. }
  1206. read_proc_filesystem();
  1207. uint32_t key;
  1208. pids = ebpf_root_of_pids; // global list of all processes running
  1209. // while (bpf_map_get_next_key(tbl_pid_stats_fd, &key, &next_key) == 0) {
  1210. if (tbl_pid_stats_fd != -1) {
  1211. size_t length = sizeof(ebpf_process_stat_t);
  1212. if (maps_per_core)
  1213. length *= ebpf_nprocs;
  1214. while (pids) {
  1215. key = pids->pid;
  1216. ebpf_process_stat_t *w = global_process_stats[key];
  1217. if (!w) {
  1218. w = ebpf_process_stat_get();
  1219. global_process_stats[key] = w;
  1220. }
  1221. if (bpf_map_lookup_elem(tbl_pid_stats_fd, &key, process_stat_vector)) {
  1222. // Clean Process structures
  1223. ebpf_process_stat_release(w);
  1224. global_process_stats[key] = NULL;
  1225. cleanup_variables_from_other_threads(key);
  1226. pids = pids->next;
  1227. continue;
  1228. }
  1229. ebpf_process_apps_accumulator(process_stat_vector, maps_per_core);
  1230. memcpy(w, process_stat_vector, sizeof(ebpf_process_stat_t));
  1231. memset(process_stat_vector, 0, length);
  1232. pids = pids->next;
  1233. }
  1234. }
  1235. link_all_processes_to_their_parents();
  1236. apply_apps_groups_targets_inheritance();
  1237. apps_groups_targets_count = zero_all_targets(apps_groups_root_target);
  1238. // this has to be done, before the cleanup
  1239. // // concentrate everything on the targets
  1240. for (pids = ebpf_root_of_pids; pids; pids = pids->next)
  1241. aggregate_pid_on_target(pids->target, pids, NULL);
  1242. post_aggregate_targets(apps_groups_root_target);
  1243. }