ebpf_disk.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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. "",
  422. "Disk latency",
  423. EBPF_COMMON_DIMENSION_CALL,
  424. ned->family,
  425. NETDATA_EBPF_CHART_TYPE_STACKED,
  426. NULL,
  427. ned->histogram.order,
  428. em->update_every);
  429. }
  430. move = move->next;
  431. }
  432. }
  433. /**
  434. * Disk exit.
  435. *
  436. * Cancel child and exit.
  437. *
  438. * @param ptr thread data.
  439. */
  440. static void ebpf_disk_exit(void *ptr)
  441. {
  442. ebpf_module_t *em = (ebpf_module_t *)ptr;
  443. if (em->enabled == NETDATA_THREAD_EBPF_FUNCTION_RUNNING) {
  444. pthread_mutex_lock(&lock);
  445. ebpf_obsolete_disk_global(em);
  446. pthread_mutex_unlock(&lock);
  447. fflush(stdout);
  448. }
  449. ebpf_disk_disable_tracepoints();
  450. ebpf_update_kernel_memory_with_vector(&plugin_statistics, disk_maps, EBPF_ACTION_STAT_REMOVE);
  451. if (em->objects) {
  452. ebpf_unload_legacy_code(em->objects, em->probe_links);
  453. em->objects = NULL;
  454. em->probe_links = NULL;
  455. }
  456. if (dimensions)
  457. ebpf_histogram_dimension_cleanup(dimensions, NETDATA_EBPF_HIST_MAX_BINS);
  458. freez(disk_hash_values);
  459. disk_hash_values = NULL;
  460. pthread_mutex_destroy(&plot_mutex);
  461. ebpf_cleanup_plot_disks();
  462. ebpf_cleanup_disk_list();
  463. pthread_mutex_lock(&ebpf_exit_cleanup);
  464. em->enabled = NETDATA_THREAD_EBPF_STOPPED;
  465. ebpf_update_stats(&plugin_statistics, em);
  466. pthread_mutex_unlock(&ebpf_exit_cleanup);
  467. }
  468. /*****************************************************************
  469. *
  470. * MAIN LOOP
  471. *
  472. *****************************************************************/
  473. /**
  474. * Fill Plot list
  475. *
  476. * @param ptr a pointer for current disk
  477. */
  478. static void ebpf_fill_plot_disks(netdata_ebpf_disks_t *ptr)
  479. {
  480. pthread_mutex_lock(&plot_mutex);
  481. ebpf_publish_disk_t *w;
  482. if (likely(plot_disks)) {
  483. ebpf_publish_disk_t *move = plot_disks, *store = plot_disks;
  484. while (move) {
  485. if (move->plot == ptr) {
  486. pthread_mutex_unlock(&plot_mutex);
  487. return;
  488. }
  489. store = move;
  490. move = move->next;
  491. }
  492. w = callocz(1, sizeof(ebpf_publish_disk_t));
  493. w->plot = ptr;
  494. store->next = w;
  495. } else {
  496. plot_disks = callocz(1, sizeof(ebpf_publish_disk_t));
  497. plot_disks->plot = ptr;
  498. }
  499. pthread_mutex_unlock(&plot_mutex);
  500. ptr->flags |= NETDATA_DISK_ADDED_TO_PLOT_LIST;
  501. }
  502. /**
  503. * Read hard disk table
  504. *
  505. * Read the table with number of calls for all functions
  506. *
  507. * @param table file descriptor for table
  508. * @param maps_per_core do I need to read all cores?
  509. */
  510. static void read_hard_disk_tables(int table, int maps_per_core)
  511. {
  512. netdata_idx_t *values = disk_hash_values;
  513. block_key_t key = {};
  514. block_key_t next_key = {};
  515. netdata_ebpf_disks_t *ret = NULL;
  516. while (bpf_map_get_next_key(table, &key, &next_key) == 0) {
  517. int test = bpf_map_lookup_elem(table, &key, values);
  518. if (test < 0) {
  519. key = next_key;
  520. continue;
  521. }
  522. netdata_ebpf_disks_t find;
  523. find.dev = key.dev;
  524. if (likely(ret)) {
  525. if (find.dev != ret->dev)
  526. ret = (netdata_ebpf_disks_t *)avl_search_lock(&disk_tree, (avl_t *)&find);
  527. } else
  528. ret = (netdata_ebpf_disks_t *)avl_search_lock(&disk_tree, (avl_t *)&find);
  529. // Disk was inserted after we parse /proc/partitions
  530. if (!ret) {
  531. if (read_local_disks()) {
  532. key = next_key;
  533. continue;
  534. }
  535. ret = (netdata_ebpf_disks_t *)avl_search_lock(&disk_tree, (avl_t *)&find);
  536. if (!ret) {
  537. // We should never reach this point, but we are adding it to keep a safe code
  538. key = next_key;
  539. continue;
  540. }
  541. }
  542. uint64_t total = 0;
  543. int i;
  544. int end = (maps_per_core) ? 1 : ebpf_nprocs;
  545. for (i = 0; i < end; i++) {
  546. total += values[i];
  547. }
  548. ret->histogram.histogram[key.bin] = total;
  549. if (!(ret->flags & NETDATA_DISK_ADDED_TO_PLOT_LIST))
  550. ebpf_fill_plot_disks(ret);
  551. key = next_key;
  552. }
  553. }
  554. /**
  555. * Obsolete Hard Disk charts
  556. *
  557. * Make Hard disk charts and fill chart name
  558. *
  559. * @param w the structure with necessary information to create the chart
  560. * @param update_every value to overwrite the update frequency set by the server.
  561. */
  562. static void ebpf_obsolete_hd_charts(netdata_ebpf_disks_t *w, int update_every)
  563. {
  564. ebpf_write_chart_obsolete(w->histogram.name, w->family, "", w->histogram.title, EBPF_COMMON_DIMENSION_CALL,
  565. w->family, NETDATA_EBPF_CHART_TYPE_STACKED, "disk.latency_io",
  566. w->histogram.order, update_every);
  567. w->flags = 0;
  568. }
  569. /**
  570. * Create Hard Disk charts
  571. *
  572. * Make Hard disk charts and fill chart name
  573. *
  574. * @param w the structure with necessary information to create the chart
  575. * @param update_every value to overwrite the update frequency set by the server.
  576. */
  577. static void ebpf_create_hd_charts(netdata_ebpf_disks_t *w, int update_every)
  578. {
  579. int order = NETDATA_CHART_PRIO_DISK_LATENCY;
  580. char *family = w->family;
  581. w->histogram.name = strdupz("disk_latency_io");
  582. w->histogram.title = NULL;
  583. w->histogram.order = order;
  584. ebpf_create_chart(w->histogram.name, family, "Disk latency", EBPF_COMMON_DIMENSION_CALL,
  585. family, "disk.latency_io", NETDATA_EBPF_CHART_TYPE_STACKED, order,
  586. ebpf_create_global_dimension, disk_publish_aggregated, NETDATA_EBPF_HIST_MAX_BINS,
  587. update_every, NETDATA_EBPF_MODULE_NAME_DISK);
  588. order++;
  589. w->flags |= NETDATA_DISK_CHART_CREATED;
  590. fflush(stdout);
  591. }
  592. /**
  593. * Remove pointer from plot
  594. *
  595. * Remove pointer from plot list when the disk is not present.
  596. */
  597. static void ebpf_remove_pointer_from_plot_disk(ebpf_module_t *em)
  598. {
  599. time_t current_time = now_realtime_sec();
  600. time_t limit = 10 * em->update_every;
  601. pthread_mutex_lock(&plot_mutex);
  602. ebpf_publish_disk_t *move = plot_disks, *prev = plot_disks;
  603. int update_every = em->update_every;
  604. while (move) {
  605. netdata_ebpf_disks_t *ned = move->plot;
  606. uint32_t flags = ned->flags;
  607. if (!(flags & NETDATA_DISK_IS_HERE) && ((current_time - ned->last_update) > limit)) {
  608. ebpf_obsolete_hd_charts(ned, update_every);
  609. avl_t *ret = (avl_t *)avl_remove_lock(&disk_tree, (avl_t *)ned);
  610. UNUSED(ret);
  611. if (move == plot_disks) {
  612. freez(move);
  613. plot_disks = NULL;
  614. break;
  615. } else {
  616. prev->next = move->next;
  617. ebpf_publish_disk_t *clean = move;
  618. move = move->next;
  619. freez(clean);
  620. continue;
  621. }
  622. }
  623. prev = move;
  624. move = move->next;
  625. }
  626. pthread_mutex_unlock(&plot_mutex);
  627. }
  628. /**
  629. * Send Hard disk data
  630. *
  631. * Send hard disk information to Netdata.
  632. *
  633. * @param update_every value to overwrite the update frequency set by the server.
  634. */
  635. static void ebpf_latency_send_hd_data(int update_every)
  636. {
  637. pthread_mutex_lock(&plot_mutex);
  638. if (!plot_disks) {
  639. pthread_mutex_unlock(&plot_mutex);
  640. return;
  641. }
  642. ebpf_publish_disk_t *move = plot_disks;
  643. while (move) {
  644. netdata_ebpf_disks_t *ned = move->plot;
  645. uint32_t flags = ned->flags;
  646. if (!(flags & NETDATA_DISK_CHART_CREATED)) {
  647. ebpf_create_hd_charts(ned, update_every);
  648. }
  649. if ((flags & NETDATA_DISK_CHART_CREATED)) {
  650. write_histogram_chart(ned->histogram.name, ned->family,
  651. ned->histogram.histogram, dimensions, NETDATA_EBPF_HIST_MAX_BINS);
  652. }
  653. ned->flags &= ~NETDATA_DISK_IS_HERE;
  654. move = move->next;
  655. }
  656. pthread_mutex_unlock(&plot_mutex);
  657. }
  658. /**
  659. * Main loop for this collector.
  660. */
  661. static void disk_collector(ebpf_module_t *em)
  662. {
  663. disk_hash_values = callocz(ebpf_nprocs, sizeof(netdata_idx_t));
  664. int update_every = em->update_every;
  665. heartbeat_t hb;
  666. heartbeat_init(&hb);
  667. int counter = update_every - 1;
  668. int maps_per_core = em->maps_per_core;
  669. uint32_t running_time = 0;
  670. uint32_t lifetime = em->lifetime;
  671. while (!ebpf_plugin_exit && running_time < lifetime) {
  672. (void)heartbeat_next(&hb, USEC_PER_SEC);
  673. if (ebpf_plugin_exit || ++counter != update_every)
  674. continue;
  675. counter = 0;
  676. read_hard_disk_tables(disk_maps[NETDATA_DISK_IO].map_fd, maps_per_core);
  677. pthread_mutex_lock(&lock);
  678. ebpf_remove_pointer_from_plot_disk(em);
  679. ebpf_latency_send_hd_data(update_every);
  680. pthread_mutex_unlock(&lock);
  681. ebpf_update_disks(em);
  682. pthread_mutex_lock(&ebpf_exit_cleanup);
  683. if (running_time && !em->running_time)
  684. running_time = update_every;
  685. else
  686. running_time += update_every;
  687. em->running_time = running_time;
  688. pthread_mutex_unlock(&ebpf_exit_cleanup);
  689. }
  690. }
  691. /*****************************************************************
  692. *
  693. * EBPF DISK THREAD
  694. *
  695. *****************************************************************/
  696. /**
  697. * Enable tracepoints
  698. *
  699. * Enable necessary tracepoints for thread.
  700. *
  701. * @return It returns 0 on success and -1 otherwise
  702. */
  703. static int ebpf_disk_enable_tracepoints()
  704. {
  705. int test = ebpf_is_tracepoint_enabled(tracepoint_block_type, tracepoint_block_issue);
  706. if (test == -1)
  707. return -1;
  708. else if (!test) {
  709. if (ebpf_enable_tracing_values(tracepoint_block_type, tracepoint_block_issue))
  710. return -1;
  711. }
  712. was_block_issue_enabled = test;
  713. test = ebpf_is_tracepoint_enabled(tracepoint_block_type, tracepoint_block_rq_complete);
  714. if (test == -1)
  715. return -1;
  716. else if (!test) {
  717. if (ebpf_enable_tracing_values(tracepoint_block_type, tracepoint_block_rq_complete))
  718. return -1;
  719. }
  720. was_block_rq_complete_enabled = test;
  721. return 0;
  722. }
  723. /*
  724. * Load BPF
  725. *
  726. * Load BPF files.
  727. *
  728. * @param em the structure with configuration
  729. *
  730. * @return It returns 0 on success and -1 otherwise.
  731. */
  732. static int ebpf_disk_load_bpf(ebpf_module_t *em)
  733. {
  734. int ret = 0;
  735. if (em->load & EBPF_LOAD_LEGACY) {
  736. em->probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &em->objects);
  737. if (!em->probe_links) {
  738. ret = -1;
  739. }
  740. }
  741. #ifdef LIBBPF_MAJOR_VERSION
  742. else {
  743. disk_bpf_obj = disk_bpf__open();
  744. if (!disk_bpf_obj)
  745. ret = -1;
  746. else {
  747. ret = ebpf_disk_load_and_attach(disk_bpf_obj);
  748. if (!ret)
  749. ebpf_disk_set_hash_table(disk_bpf_obj);
  750. }
  751. }
  752. #endif
  753. if (ret)
  754. netdata_log_error("%s %s", EBPF_DEFAULT_ERROR_MSG, em->info.thread_name);
  755. return ret;
  756. }
  757. /**
  758. * Disk thread
  759. *
  760. * Thread used to generate disk charts.
  761. *
  762. * @param ptr a pointer to `struct ebpf_module`
  763. *
  764. * @return It always return NULL
  765. */
  766. void *ebpf_disk_thread(void *ptr)
  767. {
  768. netdata_thread_cleanup_push(ebpf_disk_exit, ptr);
  769. ebpf_module_t *em = (ebpf_module_t *)ptr;
  770. em->maps = disk_maps;
  771. if (ebpf_disk_enable_tracepoints()) {
  772. goto enddisk;
  773. }
  774. avl_init_lock(&disk_tree, ebpf_compare_disks);
  775. if (read_local_disks()) {
  776. goto enddisk;
  777. }
  778. if (pthread_mutex_init(&plot_mutex, NULL)) {
  779. netdata_log_error("Cannot initialize local mutex");
  780. goto enddisk;
  781. }
  782. #ifdef LIBBPF_MAJOR_VERSION
  783. ebpf_define_map_type(disk_maps, em->maps_per_core, running_on_kernel);
  784. ebpf_adjust_thread_load(em, default_btf);
  785. #endif
  786. if (ebpf_disk_load_bpf(em)) {
  787. goto enddisk;
  788. }
  789. int algorithms[NETDATA_EBPF_HIST_MAX_BINS];
  790. ebpf_fill_algorithms(algorithms, NETDATA_EBPF_HIST_MAX_BINS, NETDATA_EBPF_INCREMENTAL_IDX);
  791. dimensions = ebpf_fill_histogram_dimension(NETDATA_EBPF_HIST_MAX_BINS);
  792. ebpf_global_labels(disk_aggregated_data, disk_publish_aggregated, dimensions, dimensions, algorithms,
  793. NETDATA_EBPF_HIST_MAX_BINS);
  794. pthread_mutex_lock(&lock);
  795. ebpf_update_stats(&plugin_statistics, em);
  796. ebpf_update_kernel_memory_with_vector(&plugin_statistics, disk_maps, EBPF_ACTION_STAT_ADD);
  797. pthread_mutex_unlock(&lock);
  798. disk_collector(em);
  799. enddisk:
  800. ebpf_update_disabled_plugin_stats(em);
  801. netdata_thread_cleanup_pop(1);
  802. return NULL;
  803. }