ebpf_disk.c 26 KB

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