ebpf_disk.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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. if (objects)
  397. bpf_object__close(objects);
  398. }
  399. }
  400. /*****************************************************************
  401. *
  402. * MAIN LOOP
  403. *
  404. *****************************************************************/
  405. /**
  406. * Fill Plot list
  407. *
  408. * @param ptr a pointer for current disk
  409. */
  410. static void ebpf_fill_plot_disks(netdata_ebpf_disks_t *ptr)
  411. {
  412. pthread_mutex_lock(&plot_mutex);
  413. ebpf_publish_disk_t *w;
  414. if (likely(plot_disks)) {
  415. ebpf_publish_disk_t *move = plot_disks, *store = plot_disks;
  416. while (move) {
  417. if (move->plot == ptr) {
  418. pthread_mutex_unlock(&plot_mutex);
  419. return;
  420. }
  421. store = move;
  422. move = move->next;
  423. }
  424. w = callocz(1, sizeof(ebpf_publish_disk_t));
  425. w->plot = ptr;
  426. store->next = w;
  427. } else {
  428. plot_disks = callocz(1, sizeof(ebpf_publish_disk_t));
  429. plot_disks->plot = ptr;
  430. }
  431. pthread_mutex_unlock(&plot_mutex);
  432. ptr->flags |= NETDATA_DISK_ADDED_TO_PLOT_LIST;
  433. }
  434. /**
  435. * Read hard disk table
  436. *
  437. * @param table file descriptor for table
  438. *
  439. * Read the table with number of calls for all functions
  440. */
  441. static void read_hard_disk_tables(int table)
  442. {
  443. netdata_idx_t *values = disk_hash_values;
  444. block_key_t key = {};
  445. block_key_t next_key = {};
  446. netdata_ebpf_disks_t *ret = NULL;
  447. while (bpf_map_get_next_key(table, &key, &next_key) == 0) {
  448. int test = bpf_map_lookup_elem(table, &key, values);
  449. if (test < 0) {
  450. key = next_key;
  451. continue;
  452. }
  453. netdata_ebpf_disks_t find;
  454. find.dev = key.dev;
  455. if (likely(ret)) {
  456. if (find.dev != ret->dev)
  457. ret = (netdata_ebpf_disks_t *)avl_search_lock(&disk_tree, (avl_t *)&find);
  458. } else
  459. ret = (netdata_ebpf_disks_t *)avl_search_lock(&disk_tree, (avl_t *)&find);
  460. // Disk was inserted after we parse /proc/partitions
  461. if (!ret) {
  462. if (read_local_disks()) {
  463. key = next_key;
  464. continue;
  465. }
  466. ret = (netdata_ebpf_disks_t *)avl_search_lock(&disk_tree, (avl_t *)&find);
  467. if (!ret) {
  468. // We should never reach this point, but we are adding it to keep a safe code
  469. key = next_key;
  470. continue;
  471. }
  472. }
  473. uint64_t total = 0;
  474. int i;
  475. int end = (running_on_kernel < NETDATA_KERNEL_V4_15) ? 1 : ebpf_nprocs;
  476. for (i = 0; i < end; i++) {
  477. total += values[i];
  478. }
  479. ret->histogram.histogram[key.bin] = total;
  480. if (!(ret->flags & NETDATA_DISK_ADDED_TO_PLOT_LIST))
  481. ebpf_fill_plot_disks(ret);
  482. key = next_key;
  483. }
  484. }
  485. /**
  486. * Disk read hash
  487. *
  488. * This is the thread callback.
  489. * This thread is necessary, because we cannot freeze the whole plugin to read the data on very busy socket.
  490. *
  491. * @param ptr It is a NULL value for this thread.
  492. *
  493. * @return It always returns NULL.
  494. */
  495. void *ebpf_disk_read_hash(void *ptr)
  496. {
  497. heartbeat_t hb;
  498. heartbeat_init(&hb);
  499. ebpf_module_t *em = (ebpf_module_t *)ptr;
  500. usec_t step = NETDATA_LATENCY_DISK_SLEEP_MS * em->update_every;
  501. while (!close_ebpf_plugin) {
  502. usec_t dt = heartbeat_next(&hb, step);
  503. (void)dt;
  504. read_hard_disk_tables(disk_maps[NETDATA_DISK_READ].map_fd);
  505. }
  506. return NULL;
  507. }
  508. /**
  509. * Obsolete Hard Disk charts
  510. *
  511. * Make Hard disk charts and fill chart name
  512. *
  513. * @param w the structure with necessary information to create the chart
  514. * @param update_every value to overwrite the update frequency set by the server.
  515. */
  516. static void ebpf_obsolete_hd_charts(netdata_ebpf_disks_t *w, int update_every)
  517. {
  518. ebpf_write_chart_obsolete(w->histogram.name, w->family, w->histogram.title, EBPF_COMMON_DIMENSION_CALL,
  519. w->family, NETDATA_EBPF_CHART_TYPE_STACKED, "disk.latency_io",
  520. w->histogram.order, update_every);
  521. w->flags = 0;
  522. }
  523. /**
  524. * Create Hard Disk charts
  525. *
  526. * Make Hard disk charts and fill chart name
  527. *
  528. * @param w the structure with necessary information to create the chart
  529. * @param update_every value to overwrite the update frequency set by the server.
  530. */
  531. static void ebpf_create_hd_charts(netdata_ebpf_disks_t *w, int update_every)
  532. {
  533. int order = NETDATA_CHART_PRIO_DISK_LATENCY;
  534. char *family = w->family;
  535. w->histogram.name = strdupz("disk_latency_io");
  536. w->histogram.title = NULL;
  537. w->histogram.order = order;
  538. ebpf_create_chart(w->histogram.name, family, "Disk latency", EBPF_COMMON_DIMENSION_CALL,
  539. family, "disk.latency_io", NETDATA_EBPF_CHART_TYPE_STACKED, order,
  540. ebpf_create_global_dimension, disk_publish_aggregated, NETDATA_EBPF_HIST_MAX_BINS,
  541. update_every, NETDATA_EBPF_MODULE_NAME_DISK);
  542. order++;
  543. w->flags |= NETDATA_DISK_CHART_CREATED;
  544. }
  545. /**
  546. * Remove pointer from plot
  547. *
  548. * Remove pointer from plot list when the disk is not present.
  549. */
  550. static void ebpf_remove_pointer_from_plot_disk(ebpf_module_t *em)
  551. {
  552. time_t current_time = now_realtime_sec();
  553. time_t limit = 10 * em->update_every;
  554. pthread_mutex_lock(&plot_mutex);
  555. ebpf_publish_disk_t *move = plot_disks, *prev = plot_disks;
  556. int update_every = em->update_every;
  557. while (move) {
  558. netdata_ebpf_disks_t *ned = move->plot;
  559. uint32_t flags = ned->flags;
  560. if (!(flags & NETDATA_DISK_IS_HERE) && ((current_time - ned->last_update) > limit)) {
  561. ebpf_obsolete_hd_charts(ned, update_every);
  562. avl_t *ret = (avl_t *)avl_remove_lock(&disk_tree, (avl_t *)ned);
  563. UNUSED(ret);
  564. if (move == plot_disks) {
  565. freez(move);
  566. plot_disks = NULL;
  567. break;
  568. } else {
  569. prev->next = move->next;
  570. ebpf_publish_disk_t *clean = move;
  571. move = move->next;
  572. freez(clean);
  573. continue;
  574. }
  575. }
  576. prev = move;
  577. move = move->next;
  578. }
  579. pthread_mutex_unlock(&plot_mutex);
  580. }
  581. /**
  582. * Send Hard disk data
  583. *
  584. * Send hard disk information to Netdata.
  585. *
  586. * @param update_every value to overwrite the update frequency set by the server.
  587. */
  588. static void ebpf_latency_send_hd_data(int update_every)
  589. {
  590. pthread_mutex_lock(&plot_mutex);
  591. if (!plot_disks) {
  592. pthread_mutex_unlock(&plot_mutex);
  593. return;
  594. }
  595. ebpf_publish_disk_t *move = plot_disks;
  596. while (move) {
  597. netdata_ebpf_disks_t *ned = move->plot;
  598. uint32_t flags = ned->flags;
  599. if (!(flags & NETDATA_DISK_CHART_CREATED)) {
  600. ebpf_create_hd_charts(ned, update_every);
  601. }
  602. if ((flags & NETDATA_DISK_CHART_CREATED)) {
  603. write_histogram_chart(ned->histogram.name, ned->family,
  604. ned->histogram.histogram, dimensions, NETDATA_EBPF_HIST_MAX_BINS);
  605. }
  606. ned->flags &= ~NETDATA_DISK_IS_HERE;
  607. move = move->next;
  608. }
  609. pthread_mutex_unlock(&plot_mutex);
  610. }
  611. /**
  612. * Main loop for this collector.
  613. */
  614. static void disk_collector(ebpf_module_t *em)
  615. {
  616. disk_hash_values = callocz(ebpf_nprocs, sizeof(netdata_idx_t));
  617. disk_threads.thread = mallocz(sizeof(netdata_thread_t));
  618. disk_threads.start_routine = ebpf_disk_read_hash;
  619. netdata_thread_create(disk_threads.thread, disk_threads.name, NETDATA_THREAD_OPTION_JOINABLE,
  620. ebpf_disk_read_hash, em);
  621. int update_every = em->update_every;
  622. int counter = update_every - 1;
  623. read_thread_closed = 0;
  624. while (!close_ebpf_plugin) {
  625. pthread_mutex_lock(&collect_data_mutex);
  626. pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
  627. if (++counter == update_every) {
  628. counter = 0;
  629. pthread_mutex_lock(&lock);
  630. ebpf_remove_pointer_from_plot_disk(em);
  631. ebpf_latency_send_hd_data(update_every);
  632. pthread_mutex_unlock(&lock);
  633. }
  634. pthread_mutex_unlock(&collect_data_mutex);
  635. ebpf_update_disks(em);
  636. }
  637. read_thread_closed = 1;
  638. }
  639. /*****************************************************************
  640. *
  641. * EBPF DISK THREAD
  642. *
  643. *****************************************************************/
  644. /**
  645. * Enable tracepoints
  646. *
  647. * Enable necessary tracepoints for thread.
  648. *
  649. * @return It returns 0 on success and -1 otherwise
  650. */
  651. static int ebpf_disk_enable_tracepoints()
  652. {
  653. int test = ebpf_is_tracepoint_enabled(tracepoint_block_type, tracepoint_block_issue);
  654. if (test == -1)
  655. return -1;
  656. else if (!test) {
  657. if (ebpf_enable_tracing_values(tracepoint_block_type, tracepoint_block_issue))
  658. return -1;
  659. }
  660. was_block_issue_enabled = test;
  661. test = ebpf_is_tracepoint_enabled(tracepoint_block_type, tracepoint_block_rq_complete);
  662. if (test == -1)
  663. return -1;
  664. else if (!test) {
  665. if (ebpf_enable_tracing_values(tracepoint_block_type, tracepoint_block_rq_complete))
  666. return -1;
  667. }
  668. was_block_rq_complete_enabled = test;
  669. return 0;
  670. }
  671. /**
  672. * Disk thread
  673. *
  674. * Thread used to generate disk charts.
  675. *
  676. * @param ptr a pointer to `struct ebpf_module`
  677. *
  678. * @return It always return NULL
  679. */
  680. void *ebpf_disk_thread(void *ptr)
  681. {
  682. netdata_thread_cleanup_push(ebpf_disk_cleanup, ptr);
  683. ebpf_module_t *em = (ebpf_module_t *)ptr;
  684. em->maps = disk_maps;
  685. if (!em->enabled)
  686. goto enddisk;
  687. if (ebpf_disk_enable_tracepoints()) {
  688. em->enabled = CONFIG_BOOLEAN_NO;
  689. goto enddisk;
  690. }
  691. avl_init_lock(&disk_tree, ebpf_compare_disks);
  692. if (read_local_disks()) {
  693. em->enabled = CONFIG_BOOLEAN_NO;
  694. goto enddisk;
  695. }
  696. if (pthread_mutex_init(&plot_mutex, NULL)) {
  697. em->enabled = 0;
  698. error("Cannot initialize local mutex");
  699. goto enddisk;
  700. }
  701. probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &objects);
  702. if (!probe_links) {
  703. em->enabled = 0;
  704. goto enddisk;
  705. }
  706. int algorithms[NETDATA_EBPF_HIST_MAX_BINS];
  707. ebpf_fill_algorithms(algorithms, NETDATA_EBPF_HIST_MAX_BINS, NETDATA_EBPF_INCREMENTAL_IDX);
  708. dimensions = ebpf_fill_histogram_dimension(NETDATA_EBPF_HIST_MAX_BINS);
  709. ebpf_global_labels(disk_aggregated_data, disk_publish_aggregated, dimensions, dimensions, algorithms,
  710. NETDATA_EBPF_HIST_MAX_BINS);
  711. pthread_mutex_lock(&lock);
  712. ebpf_update_stats(&plugin_statistics, em);
  713. pthread_mutex_unlock(&lock);
  714. disk_collector(em);
  715. enddisk:
  716. if (!em->enabled)
  717. ebpf_update_disabled_plugin_stats(em);
  718. netdata_thread_cleanup_pop(1);
  719. return NULL;
  720. }