ebpf_disk.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include <sys/resource.h>
  3. #include <stdlib.h>
  4. #include "ebpf.h"
  5. #include "ebpf_disk.h"
  6. struct config disk_config = { .first_section = NULL,
  7. .last_section = NULL,
  8. .mutex = NETDATA_MUTEX_INITIALIZER,
  9. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  10. .rwlock = AVL_LOCK_INITIALIZER } };
  11. static ebpf_local_maps_t disk_maps[] = {{.name = "tbl_disk_iocall", .internal_input = NETDATA_DISK_HISTOGRAM_LENGTH,
  12. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  13. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  14. {.name = NULL, .internal_input = 0, .user_input = 0,
  15. .type = NETDATA_EBPF_MAP_CONTROLLER,
  16. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED}};
  17. static avl_tree_lock disk_tree;
  18. netdata_ebpf_disks_t *disk_list = NULL;
  19. char *tracepoint_block_type = { "block"} ;
  20. char *tracepoint_block_issue = { "block_rq_issue" };
  21. char *tracepoint_block_rq_complete = { "block_rq_complete" };
  22. static struct bpf_link **probe_links = NULL;
  23. static struct bpf_object *objects = NULL;
  24. static int was_block_issue_enabled = 0;
  25. static int was_block_rq_complete_enabled = 0;
  26. static char **dimensions = NULL;
  27. static netdata_syscall_stat_t disk_aggregated_data[NETDATA_EBPF_HIST_MAX_BINS];
  28. static netdata_publish_syscall_t disk_publish_aggregated[NETDATA_EBPF_HIST_MAX_BINS];
  29. static int read_thread_closed = 1;
  30. static netdata_idx_t *disk_hash_values = NULL;
  31. static struct netdata_static_thread disk_threads = {"DISK KERNEL",
  32. NULL, NULL, 1, NULL,
  33. NULL, NULL };
  34. ebpf_publish_disk_t *plot_disks = NULL;
  35. pthread_mutex_t plot_mutex;
  36. /*****************************************************************
  37. *
  38. * FUNCTIONS TO MANIPULATE HARD DISKS
  39. *
  40. *****************************************************************/
  41. /**
  42. * Parse start
  43. *
  44. * Parse start address of disk
  45. *
  46. * @param w structure where data is stored
  47. * @param filename variable used to store value
  48. *
  49. * @return It returns 0 on success and -1 otherwise
  50. */
  51. static inline int ebpf_disk_parse_start(netdata_ebpf_disks_t *w, char *filename)
  52. {
  53. char content[FILENAME_MAX + 1];
  54. int fd = open(filename, O_RDONLY, 0);
  55. if (fd < 0) {
  56. return -1;
  57. }
  58. ssize_t file_length = read(fd, content, 4095);
  59. if (file_length > 0) {
  60. if (file_length > FILENAME_MAX)
  61. file_length = FILENAME_MAX;
  62. content[file_length] = '\0';
  63. w->start = strtoul(content, NULL, 10);
  64. }
  65. close(fd);
  66. return 0;
  67. }
  68. /**
  69. * Parse uevent
  70. *
  71. * Parse uevent file
  72. *
  73. * @param w structure where data is stored
  74. * @param filename variable used to store value
  75. *
  76. * @return It returns 0 on success and -1 otherwise
  77. */
  78. static inline int ebpf_parse_uevent(netdata_ebpf_disks_t *w, char *filename)
  79. {
  80. char content[FILENAME_MAX + 1];
  81. int fd = open(filename, O_RDONLY, 0);
  82. if (fd < 0) {
  83. return -1;
  84. }
  85. ssize_t file_length = read(fd, content, FILENAME_MAX);
  86. if (file_length > 0) {
  87. if (file_length > FILENAME_MAX)
  88. file_length = FILENAME_MAX;
  89. content[file_length] = '\0';
  90. char *s = strstr(content, "PARTNAME=EFI");
  91. if (s) {
  92. w->main->boot_partition = w;
  93. w->flags |= NETDATA_DISK_HAS_EFI;
  94. w->boot_chart = strdupz("disk_bootsector");
  95. }
  96. }
  97. close(fd);
  98. return 0;
  99. }
  100. /**
  101. * Parse Size
  102. *
  103. * @param w structure where data is stored
  104. * @param filename variable used to store value
  105. *
  106. * @return It returns 0 on success and -1 otherwise
  107. */
  108. static inline int ebpf_parse_size(netdata_ebpf_disks_t *w, char *filename)
  109. {
  110. char content[FILENAME_MAX + 1];
  111. int fd = open(filename, O_RDONLY, 0);
  112. if (fd < 0) {
  113. return -1;
  114. }
  115. ssize_t file_length = read(fd, content, FILENAME_MAX);
  116. if (file_length > 0) {
  117. if (file_length > FILENAME_MAX)
  118. file_length = FILENAME_MAX;
  119. content[file_length] = '\0';
  120. w->end = w->start + strtoul(content, NULL, 10) -1;
  121. }
  122. close(fd);
  123. return 0;
  124. }
  125. /**
  126. * Read Disk information
  127. *
  128. * Read disk information from /sys/block
  129. *
  130. * @param w structure where data is stored
  131. * @param name disk name
  132. */
  133. static void ebpf_read_disk_info(netdata_ebpf_disks_t *w, char *name)
  134. {
  135. static netdata_ebpf_disks_t *main_disk = NULL;
  136. static uint32_t key = 0;
  137. char *path = { "/sys/block" };
  138. char disk[NETDATA_DISK_NAME_LEN + 1];
  139. char filename[FILENAME_MAX + 1];
  140. snprintfz(disk, NETDATA_DISK_NAME_LEN, "%s", name);
  141. size_t length = strlen(disk);
  142. if (!length) {
  143. return;
  144. }
  145. length--;
  146. size_t curr = length;
  147. while (isdigit((int)disk[length])) {
  148. disk[length--] = '\0';
  149. }
  150. // We are looking for partition information, if it is a device we will ignore it.
  151. if (curr == length) {
  152. main_disk = w;
  153. key = MKDEV(w->major, w->minor);
  154. w->bootsector_key = key;
  155. return;
  156. }
  157. w->bootsector_key = key;
  158. w->main = main_disk;
  159. snprintfz(filename, FILENAME_MAX, "%s/%s/%s/uevent", path, disk, name);
  160. if (ebpf_parse_uevent(w, filename))
  161. return;
  162. snprintfz(filename, FILENAME_MAX, "%s/%s/%s/start", path, disk, name);
  163. if (ebpf_disk_parse_start(w, filename))
  164. return;
  165. snprintfz(filename, FILENAME_MAX, "%s/%s/%s/size", path, disk, name);
  166. ebpf_parse_size(w, filename);
  167. }
  168. /**
  169. * New encode dev
  170. *
  171. * New encode algorithm extracted from https://elixir.bootlin.com/linux/v5.10.8/source/include/linux/kdev_t.h#L39
  172. *
  173. * @param major driver major number
  174. * @param minor driver minor number
  175. *
  176. * @return
  177. */
  178. static inline uint32_t netdata_new_encode_dev(uint32_t major, uint32_t minor) {
  179. return (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
  180. }
  181. /**
  182. * Compare disks
  183. *
  184. * Compare major and minor values to add disks to tree.
  185. *
  186. * @param a pointer to netdata_ebpf_disks
  187. * @param b pointer to netdata_ebpf_disks
  188. *
  189. * @return It returns 0 case the values are equal, 1 case a is bigger than b and -1 case a is smaller than b.
  190. */
  191. static int ebpf_compare_disks(void *a, void *b)
  192. {
  193. netdata_ebpf_disks_t *ptr1 = a;
  194. netdata_ebpf_disks_t *ptr2 = b;
  195. if (ptr1->dev > ptr2->dev)
  196. return 1;
  197. if (ptr1->dev < ptr2->dev)
  198. return -1;
  199. return 0;
  200. }
  201. /**
  202. * Update listen table
  203. *
  204. * Update link list when it is necessary.
  205. *
  206. * @param name disk name
  207. * @param major major disk identifier
  208. * @param minor minor disk identifier
  209. * @param current_time current timestamp
  210. */
  211. static void update_disk_table(char *name, int major, int minor, time_t current_time)
  212. {
  213. netdata_ebpf_disks_t find;
  214. netdata_ebpf_disks_t *w;
  215. size_t length;
  216. uint32_t dev = netdata_new_encode_dev(major, minor);
  217. find.dev = dev;
  218. netdata_ebpf_disks_t *ret = (netdata_ebpf_disks_t *) avl_search_lock(&disk_tree, (avl_t *)&find);
  219. if (ret) { // Disk is already present
  220. ret->flags |= NETDATA_DISK_IS_HERE;
  221. ret->last_update = current_time;
  222. return;
  223. }
  224. netdata_ebpf_disks_t *update_next = disk_list;
  225. if (likely(disk_list)) {
  226. netdata_ebpf_disks_t *move = disk_list;
  227. while (move) {
  228. if (dev == move->dev)
  229. return;
  230. update_next = move;
  231. move = move->next;
  232. }
  233. w = callocz(1, sizeof(netdata_ebpf_disks_t));
  234. length = strlen(name);
  235. if (length >= NETDATA_DISK_NAME_LEN)
  236. length = NETDATA_DISK_NAME_LEN;
  237. memcpy(w->family, name, length);
  238. w->family[length] = '\0';
  239. w->major = major;
  240. w->minor = minor;
  241. w->dev = netdata_new_encode_dev(major, minor);
  242. update_next->next = w;
  243. } else {
  244. disk_list = callocz(1, sizeof(netdata_ebpf_disks_t));
  245. length = strlen(name);
  246. if (length >= NETDATA_DISK_NAME_LEN)
  247. length = NETDATA_DISK_NAME_LEN;
  248. memcpy(disk_list->family, name, length);
  249. disk_list->family[length] = '\0';
  250. disk_list->major = major;
  251. disk_list->minor = minor;
  252. disk_list->dev = netdata_new_encode_dev(major, minor);
  253. w = disk_list;
  254. }
  255. ebpf_read_disk_info(w, name);
  256. netdata_ebpf_disks_t *check;
  257. check = (netdata_ebpf_disks_t *) avl_insert_lock(&disk_tree, (avl_t *)w);
  258. if (check != w)
  259. error("Internal error, cannot insert the AVL tree.");
  260. #ifdef NETDATA_INTERNAL_CHECKS
  261. info("The Latency is monitoring the hard disk %s (Major = %d, Minor = %d, Device = %u)", name, major, minor,w->dev);
  262. #endif
  263. w->flags |= NETDATA_DISK_IS_HERE;
  264. }
  265. /**
  266. * Read Local Disks
  267. *
  268. * Parse /proc/partitions to get block disks used to measure latency.
  269. *
  270. * @return It returns 0 on success and -1 otherwise
  271. */
  272. static int read_local_disks()
  273. {
  274. char filename[FILENAME_MAX + 1];
  275. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, NETDATA_EBPF_PROC_PARTITIONS);
  276. procfile *ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
  277. if (!ff)
  278. return -1;
  279. ff = procfile_readall(ff);
  280. if (!ff)
  281. return -1;
  282. size_t lines = procfile_lines(ff), l;
  283. time_t current_time = now_realtime_sec();
  284. for(l = 2; l < lines ;l++) {
  285. size_t words = procfile_linewords(ff, l);
  286. // This is header or end of file
  287. if (unlikely(words < 4))
  288. continue;
  289. int major = (int)strtol(procfile_lineword(ff, l, 0), NULL, 10);
  290. // The main goal of this thread is to measure block devices, so any block device with major number
  291. // smaller than 7 according /proc/devices is not "important".
  292. if (major > 7) {
  293. int minor = (int)strtol(procfile_lineword(ff, l, 1), NULL, 10);
  294. update_disk_table(procfile_lineword(ff, l, 3), major, minor, current_time);
  295. }
  296. }
  297. procfile_close(ff);
  298. return 0;
  299. }
  300. /**
  301. * Update disks
  302. *
  303. * @param em main thread structure
  304. */
  305. void ebpf_update_disks(ebpf_module_t *em)
  306. {
  307. static time_t update_every = 0;
  308. time_t curr = now_realtime_sec();
  309. if (curr < update_every)
  310. return;
  311. update_every = curr + 5 * em->update_every;
  312. (void)read_local_disks();
  313. }
  314. /*****************************************************************
  315. *
  316. * FUNCTIONS TO CLOSE THE THREAD
  317. *
  318. *****************************************************************/
  319. /**
  320. * Disk disable tracepoints
  321. *
  322. * Disable tracepoints when the plugin was responsible to enable it.
  323. */
  324. static void ebpf_disk_disable_tracepoints()
  325. {
  326. char *default_message = { "Cannot disable the tracepoint" };
  327. if (!was_block_issue_enabled) {
  328. if (ebpf_disable_tracing_values(tracepoint_block_type, tracepoint_block_issue))
  329. error("%s %s/%s.", default_message, tracepoint_block_type, tracepoint_block_issue);
  330. }
  331. if (!was_block_rq_complete_enabled) {
  332. if (ebpf_disable_tracing_values(tracepoint_block_type, tracepoint_block_rq_complete))
  333. error("%s %s/%s.", default_message, tracepoint_block_type, tracepoint_block_rq_complete);
  334. }
  335. }
  336. /**
  337. * Cleanup plot disks
  338. *
  339. * Clean disk list
  340. */
  341. static void ebpf_cleanup_plot_disks()
  342. {
  343. ebpf_publish_disk_t *move = plot_disks, *next;
  344. while (move) {
  345. next = move->next;
  346. freez(move);
  347. move = next;
  348. }
  349. }
  350. /**
  351. * Cleanup Disk List
  352. */
  353. static void ebpf_cleanup_disk_list()
  354. {
  355. netdata_ebpf_disks_t *move = disk_list;
  356. while (move) {
  357. netdata_ebpf_disks_t *next = move->next;
  358. freez(move->histogram.name);
  359. freez(move->boot_chart);
  360. freez(move);
  361. move = next;
  362. }
  363. }
  364. /**
  365. * Clean up the main thread.
  366. *
  367. * @param ptr thread data.
  368. */
  369. static void ebpf_disk_cleanup(void *ptr)
  370. {
  371. ebpf_disk_disable_tracepoints();
  372. ebpf_module_t *em = (ebpf_module_t *)ptr;
  373. if (!em->enabled)
  374. return;
  375. heartbeat_t hb;
  376. heartbeat_init(&hb);
  377. uint32_t tick = 2 * USEC_PER_MS;
  378. while (!read_thread_closed) {
  379. usec_t dt = heartbeat_next(&hb, tick);
  380. UNUSED(dt);
  381. }
  382. if (dimensions)
  383. ebpf_histogram_dimension_cleanup(dimensions, NETDATA_EBPF_HIST_MAX_BINS);
  384. freez(disk_hash_values);
  385. freez(disk_threads.thread);
  386. pthread_mutex_destroy(&plot_mutex);
  387. ebpf_cleanup_plot_disks();
  388. ebpf_cleanup_disk_list();
  389. if (probe_links) {
  390. struct bpf_program *prog;
  391. size_t i = 0 ;
  392. bpf_object__for_each_program(prog, objects) {
  393. bpf_link__destroy(probe_links[i]);
  394. i++;
  395. }
  396. bpf_object__close(objects);
  397. }
  398. }
  399. /*****************************************************************
  400. *
  401. * MAIN LOOP
  402. *
  403. *****************************************************************/
  404. /**
  405. * Fill Plot list
  406. *
  407. * @param ptr a pointer for current disk
  408. */
  409. static void ebpf_fill_plot_disks(netdata_ebpf_disks_t *ptr)
  410. {
  411. pthread_mutex_lock(&plot_mutex);
  412. ebpf_publish_disk_t *w;
  413. if (likely(plot_disks)) {
  414. ebpf_publish_disk_t *move = plot_disks, *store = plot_disks;
  415. while (move) {
  416. if (move->plot == ptr) {
  417. pthread_mutex_unlock(&plot_mutex);
  418. return;
  419. }
  420. store = move;
  421. move = move->next;
  422. }
  423. w = callocz(1, sizeof(ebpf_publish_disk_t));
  424. w->plot = ptr;
  425. store->next = w;
  426. } else {
  427. plot_disks = callocz(1, sizeof(ebpf_publish_disk_t));
  428. plot_disks->plot = ptr;
  429. }
  430. pthread_mutex_unlock(&plot_mutex);
  431. ptr->flags |= NETDATA_DISK_ADDED_TO_PLOT_LIST;
  432. }
  433. /**
  434. * Read hard disk table
  435. *
  436. * @param table file descriptor for table
  437. *
  438. * Read the table with number of calls for all functions
  439. */
  440. static void read_hard_disk_tables(int table)
  441. {
  442. netdata_idx_t *values = disk_hash_values;
  443. block_key_t key = {};
  444. block_key_t next_key = {};
  445. netdata_ebpf_disks_t *ret = NULL;
  446. while (bpf_map_get_next_key(table, &key, &next_key) == 0) {
  447. int test = bpf_map_lookup_elem(table, &key, values);
  448. if (test < 0) {
  449. key = next_key;
  450. continue;
  451. }
  452. netdata_ebpf_disks_t find;
  453. find.dev = key.dev;
  454. if (likely(ret)) {
  455. if (find.dev != ret->dev)
  456. ret = (netdata_ebpf_disks_t *)avl_search_lock(&disk_tree, (avl_t *)&find);
  457. } else
  458. ret = (netdata_ebpf_disks_t *)avl_search_lock(&disk_tree, (avl_t *)&find);
  459. // Disk was inserted after we parse /proc/partitions
  460. if (!ret) {
  461. if (read_local_disks()) {
  462. key = next_key;
  463. continue;
  464. }
  465. ret = (netdata_ebpf_disks_t *)avl_search_lock(&disk_tree, (avl_t *)&find);
  466. if (!ret) {
  467. // We should never reach this point, but we are adding it to keep a safe code
  468. key = next_key;
  469. continue;
  470. }
  471. }
  472. uint64_t total = 0;
  473. int i;
  474. int end = (running_on_kernel < NETDATA_KERNEL_V4_15) ? 1 : ebpf_nprocs;
  475. for (i = 0; i < end; i++) {
  476. total += values[i];
  477. }
  478. ret->histogram.histogram[key.bin] = total;
  479. if (!(ret->flags & NETDATA_DISK_ADDED_TO_PLOT_LIST))
  480. ebpf_fill_plot_disks(ret);
  481. key = next_key;
  482. }
  483. }
  484. /**
  485. * Disk read hash
  486. *
  487. * This is the thread callback.
  488. * This thread is necessary, because we cannot freeze the whole plugin to read the data on very busy socket.
  489. *
  490. * @param ptr It is a NULL value for this thread.
  491. *
  492. * @return It always returns NULL.
  493. */
  494. void *ebpf_disk_read_hash(void *ptr)
  495. {
  496. heartbeat_t hb;
  497. heartbeat_init(&hb);
  498. ebpf_module_t *em = (ebpf_module_t *)ptr;
  499. usec_t step = NETDATA_LATENCY_DISK_SLEEP_MS * em->update_every;
  500. while (!close_ebpf_plugin) {
  501. usec_t dt = heartbeat_next(&hb, step);
  502. (void)dt;
  503. read_hard_disk_tables(disk_maps[NETDATA_DISK_READ].map_fd);
  504. }
  505. return NULL;
  506. }
  507. /**
  508. * Obsolete Hard Disk charts
  509. *
  510. * Make Hard disk charts and fill chart name
  511. *
  512. * @param w the structure with necessary information to create the chart
  513. * @param update_every value to overwrite the update frequency set by the server.
  514. */
  515. static void ebpf_obsolete_hd_charts(netdata_ebpf_disks_t *w, int update_every)
  516. {
  517. ebpf_write_chart_obsolete(w->histogram.name, w->family, w->histogram.title, EBPF_COMMON_DIMENSION_CALL,
  518. w->family, NETDATA_EBPF_CHART_TYPE_STACKED, "disk.latency_io",
  519. w->histogram.order, update_every);
  520. w->flags = 0;
  521. }
  522. /**
  523. * Create Hard Disk charts
  524. *
  525. * Make Hard disk charts and fill chart name
  526. *
  527. * @param w the structure with necessary information to create the chart
  528. * @param update_every value to overwrite the update frequency set by the server.
  529. */
  530. static void ebpf_create_hd_charts(netdata_ebpf_disks_t *w, int update_every)
  531. {
  532. int order = NETDATA_CHART_PRIO_DISK_LATENCY;
  533. char *family = w->family;
  534. w->histogram.name = strdupz("disk_latency_io");
  535. w->histogram.title = NULL;
  536. w->histogram.order = order;
  537. ebpf_create_chart(w->histogram.name, family, "Disk latency", EBPF_COMMON_DIMENSION_CALL,
  538. family, "disk.latency_io", NETDATA_EBPF_CHART_TYPE_STACKED, order,
  539. ebpf_create_global_dimension, disk_publish_aggregated, NETDATA_EBPF_HIST_MAX_BINS,
  540. update_every, NETDATA_EBPF_MODULE_NAME_DISK);
  541. order++;
  542. w->flags |= NETDATA_DISK_CHART_CREATED;
  543. }
  544. /**
  545. * Remove pointer from plot
  546. *
  547. * Remove pointer from plot list when the disk is not present.
  548. */
  549. static void ebpf_remove_pointer_from_plot_disk(ebpf_module_t *em)
  550. {
  551. time_t current_time = now_realtime_sec();
  552. time_t limit = 10 * em->update_every;
  553. pthread_mutex_lock(&plot_mutex);
  554. ebpf_publish_disk_t *move = plot_disks, *prev = plot_disks;
  555. int update_every = em->update_every;
  556. while (move) {
  557. netdata_ebpf_disks_t *ned = move->plot;
  558. uint32_t flags = ned->flags;
  559. if (!(flags & NETDATA_DISK_IS_HERE) && ((current_time - ned->last_update) > limit)) {
  560. ebpf_obsolete_hd_charts(ned, update_every);
  561. avl_t *ret = (avl_t *)avl_remove_lock(&disk_tree, (avl_t *)ned);
  562. UNUSED(ret);
  563. if (move == plot_disks) {
  564. freez(move);
  565. plot_disks = NULL;
  566. break;
  567. } else {
  568. prev->next = move->next;
  569. ebpf_publish_disk_t *clean = move;
  570. move = move->next;
  571. freez(clean);
  572. continue;
  573. }
  574. }
  575. prev = move;
  576. move = move->next;
  577. }
  578. pthread_mutex_unlock(&plot_mutex);
  579. }
  580. /**
  581. * Send Hard disk data
  582. *
  583. * Send hard disk information to Netdata.
  584. *
  585. * @param update_every value to overwrite the update frequency set by the server.
  586. */
  587. static void ebpf_latency_send_hd_data(int update_every)
  588. {
  589. pthread_mutex_lock(&plot_mutex);
  590. if (!plot_disks) {
  591. pthread_mutex_unlock(&plot_mutex);
  592. return;
  593. }
  594. ebpf_publish_disk_t *move = plot_disks;
  595. while (move) {
  596. netdata_ebpf_disks_t *ned = move->plot;
  597. uint32_t flags = ned->flags;
  598. if (!(flags & NETDATA_DISK_CHART_CREATED)) {
  599. ebpf_create_hd_charts(ned, update_every);
  600. }
  601. if ((flags & NETDATA_DISK_CHART_CREATED)) {
  602. write_histogram_chart(ned->histogram.name, ned->family,
  603. ned->histogram.histogram, dimensions, NETDATA_EBPF_HIST_MAX_BINS);
  604. }
  605. ned->flags &= ~NETDATA_DISK_IS_HERE;
  606. move = move->next;
  607. }
  608. pthread_mutex_unlock(&plot_mutex);
  609. }
  610. /**
  611. * Main loop for this collector.
  612. */
  613. static void disk_collector(ebpf_module_t *em)
  614. {
  615. disk_hash_values = callocz(ebpf_nprocs, sizeof(netdata_idx_t));
  616. disk_threads.thread = mallocz(sizeof(netdata_thread_t));
  617. disk_threads.start_routine = ebpf_disk_read_hash;
  618. netdata_thread_create(disk_threads.thread, disk_threads.name, NETDATA_THREAD_OPTION_JOINABLE,
  619. ebpf_disk_read_hash, em);
  620. int update_every = em->update_every;
  621. int counter = update_every - 1;
  622. read_thread_closed = 0;
  623. while (!close_ebpf_plugin) {
  624. pthread_mutex_lock(&collect_data_mutex);
  625. pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
  626. if (++counter == update_every) {
  627. counter = 0;
  628. pthread_mutex_lock(&lock);
  629. ebpf_remove_pointer_from_plot_disk(em);
  630. ebpf_latency_send_hd_data(update_every);
  631. pthread_mutex_unlock(&lock);
  632. }
  633. pthread_mutex_unlock(&collect_data_mutex);
  634. ebpf_update_disks(em);
  635. }
  636. read_thread_closed = 1;
  637. }
  638. /*****************************************************************
  639. *
  640. * EBPF DISK THREAD
  641. *
  642. *****************************************************************/
  643. /**
  644. * Enable tracepoints
  645. *
  646. * Enable necessary tracepoints for thread.
  647. *
  648. * @return It returns 0 on success and -1 otherwise
  649. */
  650. static int ebpf_disk_enable_tracepoints()
  651. {
  652. int test = ebpf_is_tracepoint_enabled(tracepoint_block_type, tracepoint_block_issue);
  653. if (test == -1)
  654. return -1;
  655. else if (!test) {
  656. if (ebpf_enable_tracing_values(tracepoint_block_type, tracepoint_block_issue))
  657. return -1;
  658. }
  659. was_block_issue_enabled = test;
  660. test = ebpf_is_tracepoint_enabled(tracepoint_block_type, tracepoint_block_rq_complete);
  661. if (test == -1)
  662. return -1;
  663. else if (!test) {
  664. if (ebpf_enable_tracing_values(tracepoint_block_type, tracepoint_block_rq_complete))
  665. return -1;
  666. }
  667. was_block_rq_complete_enabled = test;
  668. return 0;
  669. }
  670. /**
  671. * Disk thread
  672. *
  673. * Thread used to generate disk charts.
  674. *
  675. * @param ptr a pointer to `struct ebpf_module`
  676. *
  677. * @return It always return NULL
  678. */
  679. void *ebpf_disk_thread(void *ptr)
  680. {
  681. netdata_thread_cleanup_push(ebpf_disk_cleanup, ptr);
  682. ebpf_module_t *em = (ebpf_module_t *)ptr;
  683. em->maps = disk_maps;
  684. if (!em->enabled)
  685. goto enddisk;
  686. if (ebpf_disk_enable_tracepoints()) {
  687. em->enabled = CONFIG_BOOLEAN_NO;
  688. goto enddisk;
  689. }
  690. avl_init_lock(&disk_tree, ebpf_compare_disks);
  691. if (read_local_disks()) {
  692. em->enabled = CONFIG_BOOLEAN_NO;
  693. goto enddisk;
  694. }
  695. if (pthread_mutex_init(&plot_mutex, NULL)) {
  696. em->enabled = 0;
  697. error("Cannot initialize local mutex");
  698. goto enddisk;
  699. }
  700. probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &objects);
  701. if (!probe_links) {
  702. em->enabled = 0;
  703. goto enddisk;
  704. }
  705. int algorithms[NETDATA_EBPF_HIST_MAX_BINS];
  706. ebpf_fill_algorithms(algorithms, NETDATA_EBPF_HIST_MAX_BINS, NETDATA_EBPF_INCREMENTAL_IDX);
  707. dimensions = ebpf_fill_histogram_dimension(NETDATA_EBPF_HIST_MAX_BINS);
  708. ebpf_global_labels(disk_aggregated_data, disk_publish_aggregated, dimensions, dimensions, algorithms,
  709. NETDATA_EBPF_HIST_MAX_BINS);
  710. pthread_mutex_lock(&lock);
  711. ebpf_update_stats(&plugin_statistics, em);
  712. pthread_mutex_unlock(&lock);
  713. disk_collector(em);
  714. enddisk:
  715. if (!em->enabled)
  716. ebpf_update_disabled_plugin_stats(em);
  717. netdata_thread_cleanup_pop(1);
  718. return NULL;
  719. }