ebpf_mdflush.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. * Obsolete global
  116. *
  117. * Obsolete global charts created by thread.
  118. *
  119. * @param em a pointer to `struct ebpf_module`
  120. */
  121. static void ebpf_obsolete_mdflush_global(ebpf_module_t *em)
  122. {
  123. ebpf_write_chart_obsolete("mdstat",
  124. "mdstat_flush",
  125. "MD flushes",
  126. "flushes",
  127. "flush (eBPF)",
  128. NETDATA_EBPF_CHART_TYPE_STACKED,
  129. NULL,
  130. NETDATA_CHART_PRIO_MDSTAT_FLUSH,
  131. em->update_every);
  132. }
  133. /**
  134. * MDflush exit
  135. *
  136. * Cancel thread and exit.
  137. *
  138. * @param ptr thread data.
  139. */
  140. static void mdflush_exit(void *ptr)
  141. {
  142. ebpf_module_t *em = (ebpf_module_t *)ptr;
  143. if (em->enabled == NETDATA_THREAD_EBPF_FUNCTION_RUNNING) {
  144. pthread_mutex_lock(&lock);
  145. ebpf_obsolete_mdflush_global(em);
  146. pthread_mutex_unlock(&lock);
  147. fflush(stdout);
  148. }
  149. ebpf_update_kernel_memory_with_vector(&plugin_statistics, em->maps, EBPF_ACTION_STAT_REMOVE);
  150. if (em->objects) {
  151. ebpf_unload_legacy_code(em->objects, em->probe_links);
  152. em->objects = NULL;
  153. em->probe_links = NULL;
  154. }
  155. pthread_mutex_lock(&ebpf_exit_cleanup);
  156. em->enabled = NETDATA_THREAD_EBPF_STOPPED;
  157. ebpf_update_stats(&plugin_statistics, em);
  158. pthread_mutex_unlock(&ebpf_exit_cleanup);
  159. }
  160. /**
  161. * Compare mdflush values.
  162. *
  163. * @param a `netdata_mdflush_t *`.
  164. * @param b `netdata_mdflush_t *`.
  165. *
  166. * @return 0 if a==b, 1 if a>b, -1 if a<b.
  167. */
  168. static int mdflush_val_cmp(void *a, void *b)
  169. {
  170. netdata_mdflush_t *ptr1 = a;
  171. netdata_mdflush_t *ptr2 = b;
  172. if (ptr1->unit > ptr2->unit) {
  173. return 1;
  174. }
  175. else if (ptr1->unit < ptr2->unit) {
  176. return -1;
  177. }
  178. else {
  179. return 0;
  180. }
  181. }
  182. /**
  183. * Read count map
  184. *
  185. * Read the hash table and store data to allocated vectors.
  186. *
  187. * @param maps_per_core do I need to read all cores?
  188. */
  189. static void mdflush_read_count_map(int maps_per_core)
  190. {
  191. int mapfd = mdflush_maps[MDFLUSH_MAP_COUNT].map_fd;
  192. mdflush_ebpf_key_t curr_key = (uint32_t)-1;
  193. mdflush_ebpf_key_t key = (uint32_t)-1;
  194. netdata_mdflush_t search_v;
  195. netdata_mdflush_t *v = NULL;
  196. while (bpf_map_get_next_key(mapfd, &curr_key, &key) == 0) {
  197. curr_key = key;
  198. // get val for this key.
  199. int test = bpf_map_lookup_elem(mapfd, &key, mdflush_ebpf_vals);
  200. if (unlikely(test < 0)) {
  201. continue;
  202. }
  203. // is this record saved yet?
  204. //
  205. // if not, make a new one, mark it as unsaved for now, and continue; we
  206. // will insert it at the end after all of its values are correctly set,
  207. // so that we can safely publish it to the collector within a single,
  208. // short locked operation.
  209. //
  210. // otherwise simply continue; we will only update the flush count,
  211. // which can be republished safely without a lock.
  212. //
  213. // NOTE: lock isn't strictly necessary for this initial search, as only
  214. // this thread does writing, but the AVL is using a read-write lock so
  215. // there is no congestion.
  216. bool v_is_new = false;
  217. search_v.unit = key;
  218. v = (netdata_mdflush_t *)avl_search_lock(
  219. &mdflush_pub,
  220. (avl_t *)&search_v
  221. );
  222. if (unlikely(v == NULL)) {
  223. // flush count can only be added reliably at a later time.
  224. // when they're added, only then will we AVL insert.
  225. v = callocz(1, sizeof(netdata_mdflush_t));
  226. v->unit = key;
  227. sprintf(v->disk_name, "md%u", key);
  228. v->dim_exists = false;
  229. v_is_new = true;
  230. }
  231. // we must add up count value for this record across all CPUs.
  232. uint64_t total_cnt = 0;
  233. int i;
  234. int end = (!maps_per_core) ? 1 : ebpf_nprocs;
  235. for (i = 0; i < end; i++) {
  236. total_cnt += mdflush_ebpf_vals[i];
  237. }
  238. // can now safely publish count for existing records.
  239. v->cnt = total_cnt;
  240. // can now safely publish new record.
  241. if (v_is_new) {
  242. avl_t *check = avl_insert_lock(&mdflush_pub, (avl_t *)v);
  243. if (check != (avl_t *)v) {
  244. netdata_log_error("Internal error, cannot insert the AVL tree.");
  245. }
  246. }
  247. }
  248. }
  249. static void mdflush_create_charts(int update_every)
  250. {
  251. ebpf_create_chart(
  252. "mdstat",
  253. "mdstat_flush",
  254. "MD flushes",
  255. "flushes",
  256. "flush (eBPF)",
  257. "md.flush",
  258. NETDATA_EBPF_CHART_TYPE_STACKED,
  259. NETDATA_CHART_PRIO_MDSTAT_FLUSH,
  260. NULL, NULL, 0, update_every,
  261. NETDATA_EBPF_MODULE_NAME_MDFLUSH
  262. );
  263. fflush(stdout);
  264. }
  265. // callback for avl tree traversal on `mdflush_pub`.
  266. static int mdflush_write_dims(void *entry, void *data)
  267. {
  268. UNUSED(data);
  269. netdata_mdflush_t *v = entry;
  270. // records get dynamically added in, so add the dim if we haven't yet.
  271. if (!v->dim_exists) {
  272. ebpf_write_global_dimension(
  273. v->disk_name, v->disk_name,
  274. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX]
  275. );
  276. v->dim_exists = true;
  277. }
  278. write_chart_dimension(v->disk_name, v->cnt);
  279. return 1;
  280. }
  281. /**
  282. * Main loop for this collector.
  283. */
  284. static void mdflush_collector(ebpf_module_t *em)
  285. {
  286. mdflush_ebpf_vals = callocz(ebpf_nprocs, sizeof(mdflush_ebpf_val_t));
  287. int update_every = em->update_every;
  288. avl_init_lock(&mdflush_pub, mdflush_val_cmp);
  289. // create chart and static dims.
  290. pthread_mutex_lock(&lock);
  291. mdflush_create_charts(update_every);
  292. ebpf_update_stats(&plugin_statistics, em);
  293. ebpf_update_kernel_memory_with_vector(&plugin_statistics, em->maps, EBPF_ACTION_STAT_ADD);
  294. pthread_mutex_unlock(&lock);
  295. // loop and read from published data until ebpf plugin is closed.
  296. heartbeat_t hb;
  297. heartbeat_init(&hb);
  298. int counter = update_every - 1;
  299. int maps_per_core = em->maps_per_core;
  300. uint32_t running_time = 0;
  301. uint32_t lifetime = em->lifetime;
  302. while (!ebpf_exit_plugin && running_time < lifetime) {
  303. (void)heartbeat_next(&hb, USEC_PER_SEC);
  304. if (ebpf_exit_plugin || ++counter != update_every)
  305. continue;
  306. counter = 0;
  307. mdflush_read_count_map(maps_per_core);
  308. pthread_mutex_lock(&lock);
  309. // write dims now for all hitherto discovered devices.
  310. write_begin_chart("mdstat", "mdstat_flush");
  311. avl_traverse_lock(&mdflush_pub, mdflush_write_dims, NULL);
  312. write_end_chart();
  313. pthread_mutex_unlock(&lock);
  314. pthread_mutex_lock(&ebpf_exit_cleanup);
  315. if (running_time && !em->running_time)
  316. running_time = update_every;
  317. else
  318. running_time += update_every;
  319. em->running_time = running_time;
  320. pthread_mutex_unlock(&ebpf_exit_cleanup);
  321. }
  322. }
  323. /*
  324. * Load BPF
  325. *
  326. * Load BPF files.
  327. *
  328. * @param em the structure with configuration
  329. *
  330. * @return It returns 0 on success and -1 otherwise.
  331. */
  332. static int ebpf_mdflush_load_bpf(ebpf_module_t *em)
  333. {
  334. int ret = 0;
  335. if (em->load & EBPF_LOAD_LEGACY) {
  336. em->probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &em->objects);
  337. if (!em->probe_links) {
  338. ret = -1;
  339. }
  340. }
  341. #ifdef LIBBPF_MAJOR_VERSION
  342. else {
  343. mdflush_bpf_obj = mdflush_bpf__open();
  344. if (!mdflush_bpf_obj)
  345. ret = -1;
  346. else {
  347. ret = ebpf_mdflush_load_and_attach(mdflush_bpf_obj, em);
  348. if (ret && em->targets[NETDATA_MD_FLUSH_REQUEST].mode == EBPF_LOAD_TRAMPOLINE) {
  349. mdflush_bpf__destroy(mdflush_bpf_obj);
  350. mdflush_bpf_obj = mdflush_bpf__open();
  351. if (!mdflush_bpf_obj)
  352. ret = -1;
  353. else {
  354. em->targets[NETDATA_MD_FLUSH_REQUEST].mode = EBPF_LOAD_PROBE;
  355. ret = ebpf_mdflush_load_and_attach(mdflush_bpf_obj, em);
  356. }
  357. }
  358. }
  359. }
  360. #endif
  361. return ret;
  362. }
  363. /**
  364. * mdflush thread.
  365. *
  366. * @param ptr a `ebpf_module_t *`.
  367. * @return always NULL.
  368. */
  369. void *ebpf_mdflush_thread(void *ptr)
  370. {
  371. netdata_thread_cleanup_push(mdflush_exit, ptr);
  372. ebpf_module_t *em = (ebpf_module_t *)ptr;
  373. em->maps = mdflush_maps;
  374. char *md_flush_request = ebpf_find_symbol("md_flush_request");
  375. if (!md_flush_request) {
  376. netdata_log_error("Cannot monitor MD devices, because md is not loaded.");
  377. goto endmdflush;
  378. }
  379. #ifdef LIBBPF_MAJOR_VERSION
  380. ebpf_define_map_type(em->maps, em->maps_per_core, running_on_kernel);
  381. ebpf_adjust_thread_load(em, default_btf);
  382. #endif
  383. if (ebpf_mdflush_load_bpf(em)) {
  384. netdata_log_error("Cannot load eBPF software.");
  385. goto endmdflush;
  386. }
  387. mdflush_collector(em);
  388. endmdflush:
  389. freez(md_flush_request);
  390. ebpf_update_disabled_plugin_stats(em);
  391. netdata_thread_cleanup_pop(1);
  392. return NULL;
  393. }