ebpf_apps.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  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. JudyLDel(&ebpf_judy_pid.index.JudyLArray, p->pid, PJE0);
  1013. }
  1014. rw_spinlock_write_unlock(&ebpf_judy_pid.index.rw_spinlock);
  1015. freez(p->cmdline);
  1016. ebpf_pid_stat_release(p);
  1017. ebpf_all_pids[pid] = NULL;
  1018. ebpf_all_pids_count--;
  1019. }
  1020. /**
  1021. * Get command string associated with a PID.
  1022. * This can only safely be used when holding the `collect_data_mutex` lock.
  1023. *
  1024. * @param pid the pid to search the data.
  1025. * @param n the maximum amount of bytes to copy into dest.
  1026. * if this is greater than the size of the command, it is clipped.
  1027. * @param dest the target memory buffer to write the command into.
  1028. * @return -1 if the PID hasn't been scraped yet, 0 otherwise.
  1029. */
  1030. int get_pid_comm(pid_t pid, size_t n, char *dest)
  1031. {
  1032. struct ebpf_pid_stat *stat;
  1033. stat = ebpf_all_pids[pid];
  1034. if (unlikely(stat == NULL)) {
  1035. return -1;
  1036. }
  1037. if (unlikely(n > sizeof(stat->comm))) {
  1038. n = sizeof(stat->comm);
  1039. }
  1040. strncpyz(dest, stat->comm, n);
  1041. return 0;
  1042. }
  1043. /**
  1044. * Cleanup variable from other threads
  1045. *
  1046. * @param pid current pid.
  1047. */
  1048. void cleanup_variables_from_other_threads(uint32_t pid)
  1049. {
  1050. // Clean cachestat structure
  1051. if (cachestat_pid) {
  1052. ebpf_cachestat_release(cachestat_pid[pid]);
  1053. cachestat_pid[pid] = NULL;
  1054. }
  1055. // Clean directory cache structure
  1056. if (dcstat_pid) {
  1057. ebpf_dcstat_release(dcstat_pid[pid]);
  1058. dcstat_pid[pid] = NULL;
  1059. }
  1060. // Clean swap structure
  1061. if (swap_pid) {
  1062. freez(swap_pid[pid]);
  1063. swap_pid[pid] = NULL;
  1064. }
  1065. // Clean vfs structure
  1066. if (vfs_pid) {
  1067. ebpf_vfs_release(vfs_pid[pid]);
  1068. vfs_pid[pid] = NULL;
  1069. }
  1070. // Clean fd structure
  1071. if (fd_pid) {
  1072. ebpf_fd_release(fd_pid[pid]);
  1073. fd_pid[pid] = NULL;
  1074. }
  1075. // Clean shm structure
  1076. if (shm_pid) {
  1077. ebpf_shm_release(shm_pid[pid]);
  1078. shm_pid[pid] = NULL;
  1079. }
  1080. }
  1081. /**
  1082. * Remove PIDs when they are not running more.
  1083. */
  1084. void cleanup_exited_pids()
  1085. {
  1086. struct ebpf_pid_stat *p = NULL;
  1087. for (p = ebpf_root_of_pids; p;) {
  1088. if (!p->updated && (!p->keep || p->keeploops > 0)) {
  1089. if (unlikely(debug_enabled && (p->keep || p->keeploops)))
  1090. debug_log(" > CLEANUP cannot keep exited process %d (%s) anymore - removing it.", p->pid, p->comm);
  1091. pid_t r = p->pid;
  1092. p = p->next;
  1093. // Clean process structure
  1094. if (global_process_stats) {
  1095. ebpf_process_stat_release(global_process_stats[r]);
  1096. global_process_stats[r] = NULL;
  1097. }
  1098. cleanup_variables_from_other_threads(r);
  1099. del_pid_entry(r);
  1100. } else {
  1101. if (unlikely(p->keep))
  1102. p->keeploops++;
  1103. p->keep = 0;
  1104. p = p->next;
  1105. }
  1106. }
  1107. }
  1108. /**
  1109. * Read proc filesystem for the first time.
  1110. *
  1111. * @return It returns 0 on success and -1 otherwise.
  1112. */
  1113. static inline void read_proc_filesystem()
  1114. {
  1115. char dirname[FILENAME_MAX + 1];
  1116. snprintfz(dirname, FILENAME_MAX, "%s/proc", netdata_configured_host_prefix);
  1117. DIR *dir = opendir(dirname);
  1118. if (!dir)
  1119. return;
  1120. struct dirent *de = NULL;
  1121. while ((de = readdir(dir))) {
  1122. char *endptr = de->d_name;
  1123. if (unlikely(de->d_type != DT_DIR || de->d_name[0] < '0' || de->d_name[0] > '9'))
  1124. continue;
  1125. pid_t pid = (pid_t)strtoul(de->d_name, &endptr, 10);
  1126. // make sure we read a valid number
  1127. if (unlikely(endptr == de->d_name || *endptr != '\0'))
  1128. continue;
  1129. collect_data_for_pid(pid, NULL);
  1130. }
  1131. closedir(dir);
  1132. }
  1133. /**
  1134. * Aggregated PID on target
  1135. *
  1136. * @param w the target output
  1137. * @param p the pid with information to update
  1138. * @param o never used
  1139. */
  1140. static inline void aggregate_pid_on_target(struct ebpf_target *w, struct ebpf_pid_stat *p, struct ebpf_target *o)
  1141. {
  1142. UNUSED(o);
  1143. if (unlikely(!p->updated)) {
  1144. // the process is not running
  1145. return;
  1146. }
  1147. if (unlikely(!w)) {
  1148. netdata_log_error("pid %d %s was left without a target!", p->pid, p->comm);
  1149. return;
  1150. }
  1151. w->processes++;
  1152. struct ebpf_pid_on_target *pid_on_target = mallocz(sizeof(struct ebpf_pid_on_target));
  1153. pid_on_target->pid = p->pid;
  1154. pid_on_target->next = w->root_pid;
  1155. w->root_pid = pid_on_target;
  1156. }
  1157. /**
  1158. * Process Accumulator
  1159. *
  1160. * Sum all values read from kernel and store in the first address.
  1161. *
  1162. * @param out the vector with read values.
  1163. * @param maps_per_core do I need to read all cores?
  1164. */
  1165. void ebpf_process_apps_accumulator(ebpf_process_stat_t *out, int maps_per_core)
  1166. {
  1167. int i, end = (maps_per_core) ? ebpf_nprocs : 1;
  1168. ebpf_process_stat_t *total = &out[0];
  1169. for (i = 1; i < end; i++) {
  1170. ebpf_process_stat_t *w = &out[i];
  1171. total->exit_call += w->exit_call;
  1172. total->task_err += w->task_err;
  1173. total->create_thread += w->create_thread;
  1174. total->create_process += w->create_process;
  1175. total->release_call += w->release_call;
  1176. }
  1177. }
  1178. /**
  1179. * Collect data for all process
  1180. *
  1181. * Read data from hash table and store it in appropriate vectors.
  1182. * It also creates the link between targets and PIDs.
  1183. *
  1184. * @param tbl_pid_stats_fd The mapped file descriptor for the hash table.
  1185. * @param maps_per_core do I have hash maps per core?
  1186. */
  1187. void collect_data_for_all_processes(int tbl_pid_stats_fd, int maps_per_core)
  1188. {
  1189. if (unlikely(!ebpf_all_pids))
  1190. return;
  1191. struct ebpf_pid_stat *pids = ebpf_root_of_pids; // global list of all processes running
  1192. while (pids) {
  1193. if (pids->updated_twice) {
  1194. pids->read = 0; // mark it as not read, so that collect_data_for_pid() will read it
  1195. pids->updated = 0;
  1196. pids->merged = 0;
  1197. pids->children_count = 0;
  1198. pids->parent = NULL;
  1199. } else {
  1200. if (pids->updated)
  1201. pids->updated_twice = 1;
  1202. }
  1203. pids = pids->next;
  1204. }
  1205. read_proc_filesystem();
  1206. uint32_t key;
  1207. pids = ebpf_root_of_pids; // global list of all processes running
  1208. // while (bpf_map_get_next_key(tbl_pid_stats_fd, &key, &next_key) == 0) {
  1209. if (tbl_pid_stats_fd != -1) {
  1210. size_t length = sizeof(ebpf_process_stat_t);
  1211. if (maps_per_core)
  1212. length *= ebpf_nprocs;
  1213. while (pids) {
  1214. key = pids->pid;
  1215. ebpf_process_stat_t *w = global_process_stats[key];
  1216. if (!w) {
  1217. w = ebpf_process_stat_get();
  1218. global_process_stats[key] = w;
  1219. }
  1220. if (bpf_map_lookup_elem(tbl_pid_stats_fd, &key, process_stat_vector)) {
  1221. // Clean Process structures
  1222. ebpf_process_stat_release(w);
  1223. global_process_stats[key] = NULL;
  1224. cleanup_variables_from_other_threads(key);
  1225. pids = pids->next;
  1226. continue;
  1227. }
  1228. ebpf_process_apps_accumulator(process_stat_vector, maps_per_core);
  1229. memcpy(w, process_stat_vector, sizeof(ebpf_process_stat_t));
  1230. memset(process_stat_vector, 0, length);
  1231. pids = pids->next;
  1232. }
  1233. }
  1234. link_all_processes_to_their_parents();
  1235. apply_apps_groups_targets_inheritance();
  1236. apps_groups_targets_count = zero_all_targets(apps_groups_root_target);
  1237. // this has to be done, before the cleanup
  1238. // // concentrate everything on the targets
  1239. for (pids = ebpf_root_of_pids; pids; pids = pids->next)
  1240. aggregate_pid_on_target(pids->target, pids, NULL);
  1241. post_aggregate_targets(apps_groups_root_target);
  1242. }