ebpf_mdflush.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "ebpf.h"
  3. #include "ebpf_mdflush.h"
  4. struct config mdflush_config = { .first_section = NULL,
  5. .last_section = NULL,
  6. .mutex = NETDATA_MUTEX_INITIALIZER,
  7. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  8. .rwlock = AVL_LOCK_INITIALIZER } };
  9. #define MDFLUSH_MAP_COUNT 0
  10. static ebpf_local_maps_t mdflush_maps[] = {
  11. {
  12. .name = "tbl_mdflush",
  13. .internal_input = 1024,
  14. .user_input = 0,
  15. .type = NETDATA_EBPF_MAP_STATIC,
  16. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED,
  17. #ifdef LIBBPF_MAJOR_VERSION
  18. .map_type = BPF_MAP_TYPE_PERCPU_HASH
  19. #endif
  20. },
  21. /* end */
  22. {
  23. .name = NULL,
  24. .internal_input = 0,
  25. .user_input = 0,
  26. .type = NETDATA_EBPF_MAP_CONTROLLER,
  27. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED
  28. }
  29. };
  30. netdata_ebpf_targets_t mdflush_targets[] = { {.name = "md_flush_request", .mode = EBPF_LOAD_TRAMPOLINE},
  31. {.name = NULL, .mode = EBPF_LOAD_TRAMPOLINE}};
  32. // store for "published" data from the reader thread, which the collector
  33. // thread will write to netdata agent.
  34. static avl_tree_lock mdflush_pub;
  35. // tmp store for mdflush values we get from a per-CPU eBPF map.
  36. static mdflush_ebpf_val_t *mdflush_ebpf_vals = NULL;
  37. #ifdef LIBBPF_MAJOR_VERSION
  38. /**
  39. * Disable probes
  40. *
  41. * Disable probes to use trampolines.
  42. *
  43. * @param obj the loaded object structure.
  44. */
  45. static inline void ebpf_disable_probes(struct mdflush_bpf *obj)
  46. {
  47. bpf_program__set_autoload(obj->progs.netdata_md_flush_request_kprobe, false);
  48. }
  49. /**
  50. * Disable trampolines
  51. *
  52. * Disable trampoliness to use probes.
  53. *
  54. * @param obj the loaded object structure.
  55. */
  56. static inline void ebpf_disable_trampoline(struct mdflush_bpf *obj)
  57. {
  58. bpf_program__set_autoload(obj->progs.netdata_md_flush_request_fentry, false);
  59. }
  60. /**
  61. * Set Trampoline
  62. *
  63. * Define target to attach trampoline
  64. *
  65. * @param obj the loaded object structure.
  66. */
  67. static void ebpf_set_trampoline_target(struct mdflush_bpf *obj)
  68. {
  69. bpf_program__set_attach_target(obj->progs.netdata_md_flush_request_fentry, 0,
  70. mdflush_targets[NETDATA_MD_FLUSH_REQUEST].name);
  71. }
  72. /**
  73. * Load probe
  74. *
  75. * Load probe to monitor internal function.
  76. *
  77. * @param obj the loaded object structure.
  78. */
  79. static inline int ebpf_load_probes(struct mdflush_bpf *obj)
  80. {
  81. obj->links.netdata_md_flush_request_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_md_flush_request_kprobe,
  82. false,
  83. mdflush_targets[NETDATA_MD_FLUSH_REQUEST].name);
  84. return libbpf_get_error(obj->links.netdata_md_flush_request_kprobe);
  85. }
  86. /**
  87. * Load and Attach
  88. *
  89. * Load and attach bpf codes according user selection.
  90. *
  91. * @param obj the loaded object structure.
  92. * @param em the structure with configuration
  93. */
  94. static inline int ebpf_mdflush_load_and_attach(struct mdflush_bpf *obj, ebpf_module_t *em)
  95. {
  96. int mode = em->targets[NETDATA_MD_FLUSH_REQUEST].mode;
  97. if (mode == EBPF_LOAD_TRAMPOLINE) { // trampoline
  98. ebpf_disable_probes(obj);
  99. ebpf_set_trampoline_target(obj);
  100. } else // kprobe
  101. ebpf_disable_trampoline(obj);
  102. int ret = mdflush_bpf__load(obj);
  103. if (ret) {
  104. fprintf(stderr, "failed to load BPF object: %d\n", ret);
  105. return -1;
  106. }
  107. if (mode == EBPF_LOAD_TRAMPOLINE)
  108. ret = mdflush_bpf__attach(obj);
  109. else
  110. ret = ebpf_load_probes(obj);
  111. return ret;
  112. }
  113. #endif
  114. /**
  115. * MDflush exit
  116. *
  117. * Cancel thread and exit.
  118. *
  119. * @param ptr thread data.
  120. */
  121. static void mdflush_exit(void *ptr)
  122. {
  123. ebpf_module_t *em = (ebpf_module_t *)ptr;
  124. if (em->objects)
  125. ebpf_unload_legacy_code(em->objects, em->probe_links);
  126. pthread_mutex_lock(&ebpf_exit_cleanup);
  127. em->enabled = NETDATA_THREAD_EBPF_STOPPED;
  128. pthread_mutex_unlock(&ebpf_exit_cleanup);
  129. }
  130. /**
  131. * Compare mdflush values.
  132. *
  133. * @param a `netdata_mdflush_t *`.
  134. * @param b `netdata_mdflush_t *`.
  135. *
  136. * @return 0 if a==b, 1 if a>b, -1 if a<b.
  137. */
  138. static int mdflush_val_cmp(void *a, void *b)
  139. {
  140. netdata_mdflush_t *ptr1 = a;
  141. netdata_mdflush_t *ptr2 = b;
  142. if (ptr1->unit > ptr2->unit) {
  143. return 1;
  144. }
  145. else if (ptr1->unit < ptr2->unit) {
  146. return -1;
  147. }
  148. else {
  149. return 0;
  150. }
  151. }
  152. /**
  153. * Read count map
  154. *
  155. * Read the hash table and store data to allocated vectors.
  156. *
  157. * @param maps_per_core do I need to read all cores?
  158. */
  159. static void mdflush_read_count_map(int maps_per_core)
  160. {
  161. int mapfd = mdflush_maps[MDFLUSH_MAP_COUNT].map_fd;
  162. mdflush_ebpf_key_t curr_key = (uint32_t)-1;
  163. mdflush_ebpf_key_t key = (uint32_t)-1;
  164. netdata_mdflush_t search_v;
  165. netdata_mdflush_t *v = NULL;
  166. while (bpf_map_get_next_key(mapfd, &curr_key, &key) == 0) {
  167. curr_key = key;
  168. // get val for this key.
  169. int test = bpf_map_lookup_elem(mapfd, &key, mdflush_ebpf_vals);
  170. if (unlikely(test < 0)) {
  171. continue;
  172. }
  173. // is this record saved yet?
  174. //
  175. // if not, make a new one, mark it as unsaved for now, and continue; we
  176. // will insert it at the end after all of its values are correctly set,
  177. // so that we can safely publish it to the collector within a single,
  178. // short locked operation.
  179. //
  180. // otherwise simply continue; we will only update the flush count,
  181. // which can be republished safely without a lock.
  182. //
  183. // NOTE: lock isn't strictly necessary for this initial search, as only
  184. // this thread does writing, but the AVL is using a read-write lock so
  185. // there is no congestion.
  186. bool v_is_new = false;
  187. search_v.unit = key;
  188. v = (netdata_mdflush_t *)avl_search_lock(
  189. &mdflush_pub,
  190. (avl_t *)&search_v
  191. );
  192. if (unlikely(v == NULL)) {
  193. // flush count can only be added reliably at a later time.
  194. // when they're added, only then will we AVL insert.
  195. v = callocz(1, sizeof(netdata_mdflush_t));
  196. v->unit = key;
  197. sprintf(v->disk_name, "md%u", key);
  198. v->dim_exists = false;
  199. v_is_new = true;
  200. }
  201. // we must add up count value for this record across all CPUs.
  202. uint64_t total_cnt = 0;
  203. int i;
  204. int end = (!maps_per_core) ? 1 : ebpf_nprocs;
  205. for (i = 0; i < end; i++) {
  206. total_cnt += mdflush_ebpf_vals[i];
  207. }
  208. // can now safely publish count for existing records.
  209. v->cnt = total_cnt;
  210. // can now safely publish new record.
  211. if (v_is_new) {
  212. avl_t *check = avl_insert_lock(&mdflush_pub, (avl_t *)v);
  213. if (check != (avl_t *)v) {
  214. netdata_log_error("Internal error, cannot insert the AVL tree.");
  215. }
  216. }
  217. }
  218. }
  219. static void mdflush_create_charts(int update_every)
  220. {
  221. ebpf_create_chart(
  222. "mdstat",
  223. "mdstat_flush",
  224. "MD flushes",
  225. "flushes",
  226. "flush (eBPF)",
  227. "md.flush",
  228. NETDATA_EBPF_CHART_TYPE_STACKED,
  229. NETDATA_CHART_PRIO_MDSTAT_FLUSH,
  230. NULL, NULL, 0, update_every,
  231. NETDATA_EBPF_MODULE_NAME_MDFLUSH
  232. );
  233. fflush(stdout);
  234. }
  235. // callback for avl tree traversal on `mdflush_pub`.
  236. static int mdflush_write_dims(void *entry, void *data)
  237. {
  238. UNUSED(data);
  239. netdata_mdflush_t *v = entry;
  240. // records get dynamically added in, so add the dim if we haven't yet.
  241. if (!v->dim_exists) {
  242. ebpf_write_global_dimension(
  243. v->disk_name, v->disk_name,
  244. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX]
  245. );
  246. v->dim_exists = true;
  247. }
  248. write_chart_dimension(v->disk_name, v->cnt);
  249. return 1;
  250. }
  251. /**
  252. * Main loop for this collector.
  253. */
  254. static void mdflush_collector(ebpf_module_t *em)
  255. {
  256. mdflush_ebpf_vals = callocz(ebpf_nprocs, sizeof(mdflush_ebpf_val_t));
  257. int update_every = em->update_every;
  258. avl_init_lock(&mdflush_pub, mdflush_val_cmp);
  259. // create chart and static dims.
  260. pthread_mutex_lock(&lock);
  261. mdflush_create_charts(update_every);
  262. ebpf_update_stats(&plugin_statistics, em);
  263. ebpf_update_kernel_memory_with_vector(&plugin_statistics, em->maps);
  264. pthread_mutex_unlock(&lock);
  265. // loop and read from published data until ebpf plugin is closed.
  266. heartbeat_t hb;
  267. heartbeat_init(&hb);
  268. int counter = update_every - 1;
  269. int maps_per_core = em->maps_per_core;
  270. while (!ebpf_exit_plugin) {
  271. (void)heartbeat_next(&hb, USEC_PER_SEC);
  272. if (ebpf_exit_plugin || ++counter != update_every)
  273. continue;
  274. counter = 0;
  275. mdflush_read_count_map(maps_per_core);
  276. pthread_mutex_lock(&lock);
  277. // write dims now for all hitherto discovered devices.
  278. write_begin_chart("mdstat", "mdstat_flush");
  279. avl_traverse_lock(&mdflush_pub, mdflush_write_dims, NULL);
  280. write_end_chart();
  281. pthread_mutex_unlock(&lock);
  282. }
  283. }
  284. /*
  285. * Load BPF
  286. *
  287. * Load BPF files.
  288. *
  289. * @param em the structure with configuration
  290. *
  291. * @return It returns 0 on success and -1 otherwise.
  292. */
  293. static int ebpf_mdflush_load_bpf(ebpf_module_t *em)
  294. {
  295. int ret = 0;
  296. if (em->load & EBPF_LOAD_LEGACY) {
  297. em->probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &em->objects);
  298. if (!em->probe_links) {
  299. ret = -1;
  300. }
  301. }
  302. #ifdef LIBBPF_MAJOR_VERSION
  303. else {
  304. mdflush_bpf_obj = mdflush_bpf__open();
  305. if (!mdflush_bpf_obj)
  306. ret = -1;
  307. else {
  308. ret = ebpf_mdflush_load_and_attach(mdflush_bpf_obj, em);
  309. if (ret && em->targets[NETDATA_MD_FLUSH_REQUEST].mode == EBPF_LOAD_TRAMPOLINE) {
  310. mdflush_bpf__destroy(mdflush_bpf_obj);
  311. mdflush_bpf_obj = mdflush_bpf__open();
  312. if (!mdflush_bpf_obj)
  313. ret = -1;
  314. else {
  315. em->targets[NETDATA_MD_FLUSH_REQUEST].mode = EBPF_LOAD_PROBE;
  316. ret = ebpf_mdflush_load_and_attach(mdflush_bpf_obj, em);
  317. }
  318. }
  319. }
  320. }
  321. #endif
  322. return ret;
  323. }
  324. /**
  325. * mdflush thread.
  326. *
  327. * @param ptr a `ebpf_module_t *`.
  328. * @return always NULL.
  329. */
  330. void *ebpf_mdflush_thread(void *ptr)
  331. {
  332. netdata_thread_cleanup_push(mdflush_exit, ptr);
  333. ebpf_module_t *em = (ebpf_module_t *)ptr;
  334. em->maps = mdflush_maps;
  335. char *md_flush_request = ebpf_find_symbol("md_flush_request");
  336. if (!md_flush_request) {
  337. netdata_log_error("Cannot monitor MD devices, because md is not loaded.");
  338. goto endmdflush;
  339. }
  340. #ifdef LIBBPF_MAJOR_VERSION
  341. ebpf_define_map_type(em->maps, em->maps_per_core, running_on_kernel);
  342. ebpf_adjust_thread_load(em, default_btf);
  343. #endif
  344. if (ebpf_mdflush_load_bpf(em)) {
  345. netdata_log_error("Cannot load eBPF software.");
  346. goto endmdflush;
  347. }
  348. mdflush_collector(em);
  349. endmdflush:
  350. freez(md_flush_request);
  351. ebpf_update_disabled_plugin_stats(em);
  352. netdata_thread_cleanup_pop(1);
  353. return NULL;
  354. }