ebpf_filesystem.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "ebpf_filesystem.h"
  3. struct config fs_config = { .first_section = NULL,
  4. .last_section = NULL,
  5. .mutex = NETDATA_MUTEX_INITIALIZER,
  6. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  7. .rwlock = AVL_LOCK_INITIALIZER } };
  8. static ebpf_local_maps_t fs_maps[] = {{.name = "tbl_ext4", .internal_input = NETDATA_KEY_CALLS_SYNC,
  9. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  10. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  11. {.name = "tbl_xfs", .internal_input = NETDATA_KEY_CALLS_SYNC,
  12. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  13. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  14. {.name = "tbl_nfs", .internal_input = NETDATA_KEY_CALLS_SYNC,
  15. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  16. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  17. {.name = "tbl_zfs", .internal_input = NETDATA_KEY_CALLS_SYNC,
  18. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  19. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  20. {.name = "tbl_btrfs", .internal_input = NETDATA_KEY_CALLS_SYNC,
  21. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  22. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  23. {.name = "tbl_ext_addr", .internal_input = 1,
  24. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  25. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  26. {.name = NULL, .internal_input = 0, .user_input = 0,
  27. .type = NETDATA_EBPF_MAP_CONTROLLER,
  28. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED}};
  29. struct netdata_static_thread filesystem_threads = {
  30. .name = "EBPF FS READ",
  31. .config_section = NULL,
  32. .config_name = NULL,
  33. .env_name = NULL,
  34. .enabled = 1,
  35. .thread = NULL,
  36. .init_routine = NULL,
  37. .start_routine = NULL
  38. };
  39. static netdata_syscall_stat_t filesystem_aggregated_data[NETDATA_EBPF_HIST_MAX_BINS];
  40. static netdata_publish_syscall_t filesystem_publish_aggregated[NETDATA_EBPF_HIST_MAX_BINS];
  41. char **dimensions = NULL;
  42. static netdata_idx_t *filesystem_hash_values = NULL;
  43. /*****************************************************************
  44. *
  45. * COMMON FUNCTIONS
  46. *
  47. *****************************************************************/
  48. /**
  49. * Create Filesystem chart
  50. *
  51. * Create latency charts
  52. *
  53. * @param update_every value to overwrite the update frequency set by the server.
  54. */
  55. static void ebpf_obsolete_fs_charts(int update_every)
  56. {
  57. int i;
  58. uint32_t test = NETDATA_FILESYSTEM_FLAG_CHART_CREATED | NETDATA_FILESYSTEM_REMOVE_CHARTS;
  59. for (i = 0; localfs[i].filesystem; i++) {
  60. ebpf_filesystem_partitions_t *efp = &localfs[i];
  61. uint32_t flags = efp->flags;
  62. if ((flags & test) == test) {
  63. flags &= ~NETDATA_FILESYSTEM_FLAG_CHART_CREATED;
  64. ebpf_write_chart_obsolete(NETDATA_FILESYSTEM_FAMILY, efp->hread.name,
  65. efp->hread.title,
  66. EBPF_COMMON_DIMENSION_CALL, efp->family_name,
  67. NULL, NETDATA_EBPF_CHART_TYPE_STACKED, efp->hread.order, update_every);
  68. ebpf_write_chart_obsolete(NETDATA_FILESYSTEM_FAMILY, efp->hwrite.name,
  69. efp->hwrite.title,
  70. EBPF_COMMON_DIMENSION_CALL, efp->family_name,
  71. NULL, NETDATA_EBPF_CHART_TYPE_STACKED, efp->hwrite.order, update_every);
  72. ebpf_write_chart_obsolete(NETDATA_FILESYSTEM_FAMILY, efp->hopen.name, efp->hopen.title,
  73. EBPF_COMMON_DIMENSION_CALL, efp->family_name,
  74. NULL, NETDATA_EBPF_CHART_TYPE_STACKED, efp->hopen.order, update_every);
  75. ebpf_write_chart_obsolete(NETDATA_FILESYSTEM_FAMILY, efp->hadditional.name, efp->hadditional.title,
  76. EBPF_COMMON_DIMENSION_CALL, efp->family_name,
  77. NULL, NETDATA_EBPF_CHART_TYPE_STACKED, efp->hadditional.order,
  78. update_every);
  79. }
  80. efp->flags = flags;
  81. }
  82. }
  83. /**
  84. * Create Filesystem chart
  85. *
  86. * Create latency charts
  87. *
  88. * @param update_every value to overwrite the update frequency set by the server.
  89. */
  90. static void ebpf_create_fs_charts(int update_every)
  91. {
  92. static int order = NETDATA_CHART_PRIO_EBPF_FILESYSTEM_CHARTS;
  93. char chart_name[64], title[256], family[64];
  94. int i;
  95. uint32_t test = NETDATA_FILESYSTEM_FLAG_CHART_CREATED|NETDATA_FILESYSTEM_REMOVE_CHARTS;
  96. for (i = 0; localfs[i].filesystem; i++) {
  97. ebpf_filesystem_partitions_t *efp = &localfs[i];
  98. uint32_t flags = efp->flags;
  99. if (flags & NETDATA_FILESYSTEM_FLAG_HAS_PARTITION && !(flags & test)) {
  100. snprintfz(title, 255, "%s latency for each read request.", efp->filesystem);
  101. snprintfz(family, 63, "%s_latency", efp->family);
  102. snprintfz(chart_name, 63, "%s_read_latency", efp->filesystem);
  103. efp->hread.name = strdupz(chart_name);
  104. efp->hread.title = strdupz(title);
  105. efp->hread.order = order;
  106. efp->family_name = strdupz(family);
  107. ebpf_create_chart(NETDATA_FILESYSTEM_FAMILY, efp->hread.name,
  108. title,
  109. EBPF_COMMON_DIMENSION_CALL, family,
  110. NULL, NETDATA_EBPF_CHART_TYPE_STACKED, order, ebpf_create_global_dimension,
  111. filesystem_publish_aggregated, NETDATA_EBPF_HIST_MAX_BINS,
  112. update_every, NETDATA_EBPF_MODULE_NAME_FILESYSTEM);
  113. order++;
  114. snprintfz(title, 255, "%s latency for each write request.", efp->filesystem);
  115. snprintfz(chart_name, 63, "%s_write_latency", efp->filesystem);
  116. efp->hwrite.name = strdupz(chart_name);
  117. efp->hwrite.title = strdupz(title);
  118. efp->hwrite.order = order;
  119. ebpf_create_chart(NETDATA_FILESYSTEM_FAMILY, efp->hwrite.name,
  120. title,
  121. EBPF_COMMON_DIMENSION_CALL, family,
  122. NULL, NETDATA_EBPF_CHART_TYPE_STACKED, order, ebpf_create_global_dimension,
  123. filesystem_publish_aggregated, NETDATA_EBPF_HIST_MAX_BINS,
  124. update_every, NETDATA_EBPF_MODULE_NAME_FILESYSTEM);
  125. order++;
  126. snprintfz(title, 255, "%s latency for each open request.", efp->filesystem);
  127. snprintfz(chart_name, 63, "%s_open_latency", efp->filesystem);
  128. efp->hopen.name = strdupz(chart_name);
  129. efp->hopen.title = strdupz(title);
  130. efp->hopen.order = order;
  131. ebpf_create_chart(NETDATA_FILESYSTEM_FAMILY, efp->hopen.name,
  132. title,
  133. EBPF_COMMON_DIMENSION_CALL, family,
  134. NULL, NETDATA_EBPF_CHART_TYPE_STACKED, order, ebpf_create_global_dimension,
  135. filesystem_publish_aggregated, NETDATA_EBPF_HIST_MAX_BINS,
  136. update_every, NETDATA_EBPF_MODULE_NAME_FILESYSTEM);
  137. order++;
  138. char *type = (efp->flags & NETDATA_FILESYSTEM_ATTR_CHARTS) ? "attribute" : "sync";
  139. snprintfz(title, 255, "%s latency for each %s request.", efp->filesystem, type);
  140. snprintfz(chart_name, 63, "%s_%s_latency", efp->filesystem, type);
  141. efp->hadditional.name = strdupz(chart_name);
  142. efp->hadditional.title = strdupz(title);
  143. efp->hadditional.order = order;
  144. ebpf_create_chart(NETDATA_FILESYSTEM_FAMILY, efp->hadditional.name, title,
  145. EBPF_COMMON_DIMENSION_CALL, family,
  146. NULL, NETDATA_EBPF_CHART_TYPE_STACKED, order, ebpf_create_global_dimension,
  147. filesystem_publish_aggregated, NETDATA_EBPF_HIST_MAX_BINS,
  148. update_every, NETDATA_EBPF_MODULE_NAME_FILESYSTEM);
  149. order++;
  150. efp->flags |= NETDATA_FILESYSTEM_FLAG_CHART_CREATED;
  151. }
  152. }
  153. }
  154. /**
  155. * Initialize eBPF data
  156. *
  157. * @param em main thread structure.
  158. *
  159. * @return it returns 0 on success and -1 otherwise.
  160. */
  161. int ebpf_filesystem_initialize_ebpf_data(ebpf_module_t *em)
  162. {
  163. int i;
  164. const char *saved_name = em->thread_name;
  165. uint64_t kernels = em->kernels;
  166. for (i = 0; localfs[i].filesystem; i++) {
  167. ebpf_filesystem_partitions_t *efp = &localfs[i];
  168. if (!efp->probe_links && efp->flags & NETDATA_FILESYSTEM_LOAD_EBPF_PROGRAM) {
  169. em->thread_name = efp->filesystem;
  170. em->kernels = efp->kernels;
  171. efp->probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &efp->objects);
  172. if (!efp->probe_links) {
  173. em->thread_name = saved_name;
  174. em->kernels = kernels;
  175. return -1;
  176. }
  177. efp->flags |= NETDATA_FILESYSTEM_FLAG_HAS_PARTITION;
  178. // Nedeed for filesystems like btrfs
  179. if ((efp->flags & NETDATA_FILESYSTEM_FILL_ADDRESS_TABLE) && (efp->addresses.function)) {
  180. ebpf_load_addresses(&efp->addresses, fs_maps[i + 1].map_fd);
  181. }
  182. }
  183. efp->flags &= ~NETDATA_FILESYSTEM_LOAD_EBPF_PROGRAM;
  184. }
  185. em->thread_name = saved_name;
  186. em->kernels = kernels;
  187. if (!dimensions) {
  188. dimensions = ebpf_fill_histogram_dimension(NETDATA_EBPF_HIST_MAX_BINS);
  189. memset(filesystem_aggregated_data, 0 , NETDATA_EBPF_HIST_MAX_BINS * sizeof(netdata_syscall_stat_t));
  190. memset(filesystem_publish_aggregated, 0 , NETDATA_EBPF_HIST_MAX_BINS * sizeof(netdata_publish_syscall_t));
  191. filesystem_hash_values = callocz(ebpf_nprocs, sizeof(netdata_idx_t));
  192. }
  193. return 0;
  194. }
  195. /**
  196. * Read Local partitions
  197. *
  198. * @return the total of partitions that will be monitored
  199. */
  200. static int ebpf_read_local_partitions()
  201. {
  202. char filename[FILENAME_MAX + 1];
  203. snprintfz(filename, FILENAME_MAX, "%s/proc/self/mountinfo", netdata_configured_host_prefix);
  204. procfile *ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  205. if(unlikely(!ff)) {
  206. snprintfz(filename, FILENAME_MAX, "%s/proc/1/mountinfo", netdata_configured_host_prefix);
  207. ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT);
  208. if(unlikely(!ff)) return 0;
  209. }
  210. ff = procfile_readall(ff);
  211. if(unlikely(!ff))
  212. return 0;
  213. int count = 0;
  214. unsigned long l, i, lines = procfile_lines(ff);
  215. for (i = 0; localfs[i].filesystem; i++) {
  216. localfs[i].flags |= NETDATA_FILESYSTEM_REMOVE_CHARTS;
  217. }
  218. for(l = 0; l < lines ; l++) {
  219. // In "normal" situation the expected value is at column 7
  220. // When `shared` options is added to mount information, the filesystem is at column 8
  221. // Finally when we have systemd starting netdata, it will be at column 9
  222. unsigned long index = procfile_linewords(ff, l) - 3;
  223. char *fs = procfile_lineword(ff, l, index);
  224. for (i = 0; localfs[i].filesystem; i++) {
  225. ebpf_filesystem_partitions_t *w = &localfs[i];
  226. if (w->enabled && (!strcmp(fs, w->filesystem) ||
  227. (w->optional_filesystem && !strcmp(fs, w->optional_filesystem)))) {
  228. localfs[i].flags |= NETDATA_FILESYSTEM_LOAD_EBPF_PROGRAM;
  229. localfs[i].flags &= ~NETDATA_FILESYSTEM_REMOVE_CHARTS;
  230. count++;
  231. break;
  232. }
  233. }
  234. }
  235. procfile_close(ff);
  236. return count;
  237. }
  238. /**
  239. * Update partition
  240. *
  241. * Update the partition structures before to plot
  242. *
  243. * @param em main thread structure
  244. *
  245. * @return 0 on success and -1 otherwise.
  246. */
  247. static int ebpf_update_partitions(ebpf_module_t *em)
  248. {
  249. static time_t update_every = 0;
  250. time_t curr = now_realtime_sec();
  251. if (curr < update_every)
  252. return 0;
  253. update_every = curr + 5 * em->update_every;
  254. if (!ebpf_read_local_partitions()) {
  255. em->optional = -1;
  256. return -1;
  257. }
  258. if (ebpf_filesystem_initialize_ebpf_data(em)) {
  259. return -1;
  260. }
  261. return 0;
  262. }
  263. /*****************************************************************
  264. *
  265. * CLEANUP FUNCTIONS
  266. *
  267. *****************************************************************/
  268. /*
  269. * Cleanup eBPF data
  270. */
  271. void ebpf_filesystem_cleanup_ebpf_data()
  272. {
  273. int i;
  274. for (i = 0; localfs[i].filesystem; i++) {
  275. ebpf_filesystem_partitions_t *efp = &localfs[i];
  276. if (efp->probe_links) {
  277. freez(efp->family_name);
  278. freez(efp->hread.name);
  279. freez(efp->hread.title);
  280. freez(efp->hwrite.name);
  281. freez(efp->hwrite.title);
  282. freez(efp->hopen.name);
  283. freez(efp->hopen.title);
  284. freez(efp->hadditional.name);
  285. freez(efp->hadditional.title);
  286. }
  287. }
  288. }
  289. /**
  290. * Filesystem Free
  291. *
  292. * Cleanup variables after child threads to stop
  293. *
  294. * @param ptr thread data.
  295. */
  296. static void ebpf_filesystem_free(ebpf_module_t *em)
  297. {
  298. pthread_mutex_lock(&ebpf_exit_cleanup);
  299. if (em->thread->enabled == NETDATA_THREAD_EBPF_RUNNING) {
  300. em->thread->enabled = NETDATA_THREAD_EBPF_STOPPING;
  301. pthread_mutex_unlock(&ebpf_exit_cleanup);
  302. return;
  303. }
  304. pthread_mutex_unlock(&ebpf_exit_cleanup);
  305. freez(filesystem_threads.thread);
  306. ebpf_cleanup_publish_syscall(filesystem_publish_aggregated);
  307. ebpf_filesystem_cleanup_ebpf_data();
  308. if (dimensions)
  309. ebpf_histogram_dimension_cleanup(dimensions, NETDATA_EBPF_HIST_MAX_BINS);
  310. freez(filesystem_hash_values);
  311. pthread_mutex_lock(&ebpf_exit_cleanup);
  312. em->thread->enabled = NETDATA_THREAD_EBPF_STOPPED;
  313. pthread_mutex_unlock(&ebpf_exit_cleanup);
  314. }
  315. /**
  316. * Filesystem exit
  317. *
  318. * Cancel child thread.
  319. *
  320. * @param ptr thread data.
  321. */
  322. static void ebpf_filesystem_exit(void *ptr)
  323. {
  324. ebpf_module_t *em = (ebpf_module_t *)ptr;
  325. netdata_thread_cancel(*filesystem_threads.thread);
  326. ebpf_filesystem_free(em);
  327. }
  328. /**
  329. * File system cleanup
  330. *
  331. * Clean up allocated thread.
  332. *
  333. * @param ptr thread data.
  334. */
  335. static void ebpf_filesystem_cleanup(void *ptr)
  336. {
  337. ebpf_module_t *em = (ebpf_module_t *)ptr;
  338. ebpf_filesystem_free(em);
  339. }
  340. /*****************************************************************
  341. *
  342. * MAIN THREAD
  343. *
  344. *****************************************************************/
  345. /**
  346. * Select hist
  347. *
  348. * Select a histogram to store data.
  349. *
  350. * @param efp pointer for the structure with pointers.
  351. * @param id histogram selector
  352. *
  353. * @return It returns a pointer for the histogram
  354. */
  355. static inline netdata_ebpf_histogram_t *select_hist(ebpf_filesystem_partitions_t *efp, uint32_t *idx, uint32_t id)
  356. {
  357. if (id < NETDATA_KEY_CALLS_READ) {
  358. *idx = id;
  359. return &efp->hread;
  360. } else if (id < NETDATA_KEY_CALLS_WRITE) {
  361. *idx = id - NETDATA_KEY_CALLS_READ;
  362. return &efp->hwrite;
  363. } else if (id < NETDATA_KEY_CALLS_OPEN) {
  364. *idx = id - NETDATA_KEY_CALLS_WRITE;
  365. return &efp->hopen;
  366. } else if (id < NETDATA_KEY_CALLS_SYNC ){
  367. *idx = id - NETDATA_KEY_CALLS_OPEN;
  368. return &efp->hadditional;
  369. }
  370. return NULL;
  371. }
  372. /**
  373. * Read hard disk table
  374. *
  375. * @param table index for the hash table
  376. *
  377. * Read the table with number of calls for all functions
  378. */
  379. static void read_filesystem_table(ebpf_filesystem_partitions_t *efp, int fd)
  380. {
  381. netdata_idx_t *values = filesystem_hash_values;
  382. uint32_t key;
  383. uint32_t idx;
  384. for (key = 0; key < NETDATA_KEY_CALLS_SYNC; key++) {
  385. netdata_ebpf_histogram_t *w = select_hist(efp, &idx, key);
  386. if (!w) {
  387. continue;
  388. }
  389. int test = bpf_map_lookup_elem(fd, &key, values);
  390. if (test < 0) {
  391. continue;
  392. }
  393. uint64_t total = 0;
  394. int i;
  395. int end = ebpf_nprocs;
  396. for (i = 0; i < end; i++) {
  397. total += values[i];
  398. }
  399. if (idx >= NETDATA_EBPF_HIST_MAX_BINS)
  400. idx = NETDATA_EBPF_HIST_MAX_BINS - 1;
  401. w->histogram[idx] = total;
  402. }
  403. }
  404. /**
  405. * Read hard disk table
  406. *
  407. * @param table index for the hash table
  408. *
  409. * Read the table with number of calls for all functions
  410. */
  411. static void read_filesystem_tables()
  412. {
  413. int i;
  414. for (i = 0; localfs[i].filesystem; i++) {
  415. ebpf_filesystem_partitions_t *efp = &localfs[i];
  416. if (efp->flags & NETDATA_FILESYSTEM_FLAG_HAS_PARTITION) {
  417. read_filesystem_table(efp, fs_maps[i].map_fd);
  418. }
  419. }
  420. }
  421. /**
  422. * Socket read hash
  423. *
  424. * This is the thread callback.
  425. * This thread is necessary, because we cannot freeze the whole plugin to read the data on very busy socket.
  426. *
  427. * @param ptr It is a NULL value for this thread.
  428. *
  429. * @return It always returns NULL.
  430. */
  431. void *ebpf_filesystem_read_hash(void *ptr)
  432. {
  433. netdata_thread_cleanup_push(ebpf_filesystem_cleanup, ptr);
  434. ebpf_module_t *em = (ebpf_module_t *)ptr;
  435. heartbeat_t hb;
  436. heartbeat_init(&hb);
  437. usec_t step = NETDATA_FILESYSTEM_READ_SLEEP_MS * em->update_every;
  438. int update_every = em->update_every;
  439. while (!ebpf_exit_plugin) {
  440. (void)heartbeat_next(&hb, step);
  441. (void) ebpf_update_partitions(em);
  442. ebpf_obsolete_fs_charts(update_every);
  443. // No more partitions, it is not necessary to read tables
  444. if (em->optional)
  445. continue;
  446. read_filesystem_tables();
  447. }
  448. netdata_thread_cleanup_pop(1);
  449. return NULL;
  450. }
  451. /**
  452. * Send Hard disk data
  453. *
  454. * Send hard disk information to Netdata.
  455. */
  456. static void ebpf_histogram_send_data()
  457. {
  458. uint32_t i;
  459. uint32_t test = NETDATA_FILESYSTEM_FLAG_HAS_PARTITION | NETDATA_FILESYSTEM_REMOVE_CHARTS;
  460. for (i = 0; localfs[i].filesystem; i++) {
  461. ebpf_filesystem_partitions_t *efp = &localfs[i];
  462. if ((efp->flags & test) == NETDATA_FILESYSTEM_FLAG_HAS_PARTITION) {
  463. write_histogram_chart(NETDATA_FILESYSTEM_FAMILY, efp->hread.name,
  464. efp->hread.histogram, dimensions, NETDATA_EBPF_HIST_MAX_BINS);
  465. write_histogram_chart(NETDATA_FILESYSTEM_FAMILY, efp->hwrite.name,
  466. efp->hwrite.histogram, dimensions, NETDATA_EBPF_HIST_MAX_BINS);
  467. write_histogram_chart(NETDATA_FILESYSTEM_FAMILY, efp->hopen.name,
  468. efp->hopen.histogram, dimensions, NETDATA_EBPF_HIST_MAX_BINS);
  469. write_histogram_chart(NETDATA_FILESYSTEM_FAMILY, efp->hadditional.name,
  470. efp->hadditional.histogram, dimensions, NETDATA_EBPF_HIST_MAX_BINS);
  471. }
  472. }
  473. }
  474. /**
  475. * Main loop for this collector.
  476. *
  477. * @param em main structure for this thread
  478. */
  479. static void filesystem_collector(ebpf_module_t *em)
  480. {
  481. filesystem_threads.thread = mallocz(sizeof(netdata_thread_t));
  482. filesystem_threads.start_routine = ebpf_filesystem_read_hash;
  483. netdata_thread_create(filesystem_threads.thread, filesystem_threads.name,
  484. NETDATA_THREAD_OPTION_DEFAULT, ebpf_filesystem_read_hash, em);
  485. int update_every = em->update_every;
  486. heartbeat_t hb;
  487. heartbeat_init(&hb);
  488. usec_t step = update_every * USEC_PER_SEC;
  489. while (!ebpf_exit_plugin) {
  490. (void)heartbeat_next(&hb, step);
  491. if (ebpf_exit_plugin)
  492. break;
  493. pthread_mutex_lock(&lock);
  494. ebpf_create_fs_charts(update_every);
  495. ebpf_histogram_send_data();
  496. pthread_mutex_unlock(&lock);
  497. }
  498. }
  499. /*****************************************************************
  500. *
  501. * ENTRY THREAD
  502. *
  503. *****************************************************************/
  504. /**
  505. * Update Filesystem
  506. *
  507. * Update file system structure using values read from configuration file.
  508. */
  509. static void ebpf_update_filesystem()
  510. {
  511. char dist[NETDATA_FS_MAX_DIST_NAME + 1];
  512. int i;
  513. for (i = 0; localfs[i].filesystem; i++) {
  514. snprintfz(dist, NETDATA_FS_MAX_DIST_NAME, "%sdist", localfs[i].filesystem);
  515. localfs[i].enabled = appconfig_get_boolean(&fs_config, NETDATA_FILESYSTEM_CONFIG_NAME, dist,
  516. CONFIG_BOOLEAN_YES);
  517. }
  518. }
  519. /**
  520. * Filesystem thread
  521. *
  522. * Thread used to generate socket charts.
  523. *
  524. * @param ptr a pointer to `struct ebpf_module`
  525. *
  526. * @return It always return NULL
  527. */
  528. void *ebpf_filesystem_thread(void *ptr)
  529. {
  530. netdata_thread_cleanup_push(ebpf_filesystem_exit, ptr);
  531. ebpf_module_t *em = (ebpf_module_t *)ptr;
  532. em->maps = fs_maps;
  533. ebpf_update_filesystem();
  534. // Initialize optional as zero, to identify when there are not partitions to monitor
  535. em->optional = 0;
  536. if (ebpf_update_partitions(em)) {
  537. if (em->optional)
  538. info("Netdata cannot monitor the filesystems used on this host.");
  539. em->thread->enabled = NETDATA_THREAD_EBPF_STOPPED;
  540. goto endfilesystem;
  541. }
  542. int algorithms[NETDATA_EBPF_HIST_MAX_BINS];
  543. ebpf_fill_algorithms(algorithms, NETDATA_EBPF_HIST_MAX_BINS, NETDATA_EBPF_INCREMENTAL_IDX);
  544. ebpf_global_labels(filesystem_aggregated_data, filesystem_publish_aggregated, dimensions, dimensions,
  545. algorithms, NETDATA_EBPF_HIST_MAX_BINS);
  546. pthread_mutex_lock(&lock);
  547. ebpf_create_fs_charts(em->update_every);
  548. ebpf_update_stats(&plugin_statistics, em);
  549. pthread_mutex_unlock(&lock);
  550. filesystem_collector(em);
  551. endfilesystem:
  552. ebpf_update_disabled_plugin_stats(em);
  553. netdata_thread_cleanup_pop(1);
  554. return NULL;
  555. }