ebpf_mdflush.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. },
  18. /* end */
  19. {
  20. .name = NULL,
  21. .internal_input = 0,
  22. .user_input = 0,
  23. .type = NETDATA_EBPF_MAP_CONTROLLER,
  24. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED
  25. }
  26. };
  27. // store for "published" data from the reader thread, which the collector
  28. // thread will write to netdata agent.
  29. static avl_tree_lock mdflush_pub;
  30. // tmp store for mdflush values we get from a per-CPU eBPF map.
  31. static mdflush_ebpf_val_t *mdflush_ebpf_vals = NULL;
  32. static struct netdata_static_thread mdflush_threads = {"MDFLUSH KERNEL",
  33. NULL, NULL, 1, NULL,
  34. NULL, NULL };
  35. static enum ebpf_threads_status ebpf_mdflush_exited = NETDATA_THREAD_EBPF_RUNNING;
  36. /**
  37. * MDflush exit
  38. *
  39. * Cancel thread and exit.
  40. *
  41. * @param ptr thread data.
  42. */
  43. static void mdflush_exit(void *ptr)
  44. {
  45. ebpf_module_t *em = (ebpf_module_t *)ptr;
  46. if (!em->enabled) {
  47. em->enabled = NETDATA_MAIN_THREAD_EXITED;
  48. return;
  49. }
  50. ebpf_mdflush_exited = NETDATA_THREAD_EBPF_STOPPING;
  51. }
  52. /**
  53. * CLeanup
  54. *
  55. * Clean allocated memory.
  56. *
  57. * @param ptr thread data.
  58. */
  59. static void mdflush_cleanup(void *ptr)
  60. {
  61. ebpf_module_t *em = (ebpf_module_t *)ptr;
  62. if (ebpf_mdflush_exited != NETDATA_THREAD_EBPF_STOPPED)
  63. return;
  64. freez(mdflush_ebpf_vals);
  65. freez(mdflush_threads.thread);
  66. mdflush_threads.enabled = NETDATA_MAIN_THREAD_EXITED;
  67. em->enabled = NETDATA_MAIN_THREAD_EXITED;
  68. }
  69. /**
  70. * Compare mdflush values.
  71. *
  72. * @param a `netdata_mdflush_t *`.
  73. * @param b `netdata_mdflush_t *`.
  74. *
  75. * @return 0 if a==b, 1 if a>b, -1 if a<b.
  76. */
  77. static int mdflush_val_cmp(void *a, void *b)
  78. {
  79. netdata_mdflush_t *ptr1 = a;
  80. netdata_mdflush_t *ptr2 = b;
  81. if (ptr1->unit > ptr2->unit) {
  82. return 1;
  83. }
  84. else if (ptr1->unit < ptr2->unit) {
  85. return -1;
  86. }
  87. else {
  88. return 0;
  89. }
  90. }
  91. static void mdflush_read_count_map()
  92. {
  93. int mapfd = mdflush_maps[MDFLUSH_MAP_COUNT].map_fd;
  94. mdflush_ebpf_key_t curr_key = (uint32_t)-1;
  95. mdflush_ebpf_key_t key = (uint32_t)-1;
  96. netdata_mdflush_t search_v;
  97. netdata_mdflush_t *v = NULL;
  98. while (bpf_map_get_next_key(mapfd, &curr_key, &key) == 0) {
  99. curr_key = key;
  100. // get val for this key.
  101. int test = bpf_map_lookup_elem(mapfd, &key, mdflush_ebpf_vals);
  102. if (unlikely(test < 0)) {
  103. continue;
  104. }
  105. // is this record saved yet?
  106. //
  107. // if not, make a new one, mark it as unsaved for now, and continue; we
  108. // will insert it at the end after all of its values are correctly set,
  109. // so that we can safely publish it to the collector within a single,
  110. // short locked operation.
  111. //
  112. // otherwise simply continue; we will only update the flush count,
  113. // which can be republished safely without a lock.
  114. //
  115. // NOTE: lock isn't strictly necessary for this initial search, as only
  116. // this thread does writing, but the AVL is using a read-write lock so
  117. // there is no congestion.
  118. bool v_is_new = false;
  119. search_v.unit = key;
  120. v = (netdata_mdflush_t *)avl_search_lock(
  121. &mdflush_pub,
  122. (avl_t *)&search_v
  123. );
  124. if (unlikely(v == NULL)) {
  125. // flush count can only be added reliably at a later time.
  126. // when they're added, only then will we AVL insert.
  127. v = callocz(1, sizeof(netdata_mdflush_t));
  128. v->unit = key;
  129. sprintf(v->disk_name, "md%u", key);
  130. v->dim_exists = false;
  131. v_is_new = true;
  132. }
  133. // we must add up count value for this record across all CPUs.
  134. uint64_t total_cnt = 0;
  135. int i;
  136. int end = (running_on_kernel < NETDATA_KERNEL_V4_15) ? 1 : ebpf_nprocs;
  137. for (i = 0; i < end; i++) {
  138. total_cnt += mdflush_ebpf_vals[i];
  139. }
  140. // can now safely publish count for existing records.
  141. v->cnt = total_cnt;
  142. // can now safely publish new record.
  143. if (v_is_new) {
  144. avl_t *check = avl_insert_lock(&mdflush_pub, (avl_t *)v);
  145. if (check != (avl_t *)v) {
  146. error("Internal error, cannot insert the AVL tree.");
  147. }
  148. }
  149. }
  150. }
  151. /**
  152. * Read eBPF maps for mdflush.
  153. */
  154. static void *mdflush_reader(void *ptr)
  155. {
  156. netdata_thread_cleanup_push(mdflush_cleanup, ptr);
  157. heartbeat_t hb;
  158. heartbeat_init(&hb);
  159. ebpf_module_t *em = (ebpf_module_t *)ptr;
  160. usec_t step = NETDATA_MDFLUSH_SLEEP_MS * em->update_every;
  161. while (ebpf_mdflush_exited == NETDATA_THREAD_EBPF_RUNNING) {
  162. usec_t dt = heartbeat_next(&hb, step);
  163. UNUSED(dt);
  164. if (ebpf_mdflush_exited == NETDATA_THREAD_EBPF_STOPPING)
  165. break;
  166. mdflush_read_count_map();
  167. }
  168. ebpf_mdflush_exited = NETDATA_THREAD_EBPF_STOPPED;
  169. netdata_thread_cleanup_pop(1);
  170. return NULL;
  171. }
  172. static void mdflush_create_charts(int update_every)
  173. {
  174. ebpf_create_chart(
  175. "mdstat",
  176. "mdstat_flush",
  177. "MD flushes",
  178. "flushes",
  179. "flush (eBPF)",
  180. "md.flush",
  181. NETDATA_EBPF_CHART_TYPE_STACKED,
  182. NETDATA_CHART_PRIO_MDSTAT_FLUSH,
  183. NULL, NULL, 0, update_every,
  184. NETDATA_EBPF_MODULE_NAME_MDFLUSH
  185. );
  186. fflush(stdout);
  187. }
  188. // callback for avl tree traversal on `mdflush_pub`.
  189. static int mdflush_write_dims(void *entry, void *data)
  190. {
  191. UNUSED(data);
  192. netdata_mdflush_t *v = entry;
  193. // records get dynamically added in, so add the dim if we haven't yet.
  194. if (!v->dim_exists) {
  195. ebpf_write_global_dimension(
  196. v->disk_name, v->disk_name,
  197. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX]
  198. );
  199. v->dim_exists = true;
  200. }
  201. write_chart_dimension(v->disk_name, v->cnt);
  202. return 1;
  203. }
  204. /**
  205. * Main loop for this collector.
  206. */
  207. static void mdflush_collector(ebpf_module_t *em)
  208. {
  209. mdflush_ebpf_vals = callocz(ebpf_nprocs, sizeof(mdflush_ebpf_val_t));
  210. avl_init_lock(&mdflush_pub, mdflush_val_cmp);
  211. // create reader thread.
  212. mdflush_threads.thread = mallocz(sizeof(netdata_thread_t));
  213. mdflush_threads.start_routine = mdflush_reader;
  214. netdata_thread_create(
  215. mdflush_threads.thread,
  216. mdflush_threads.name,
  217. NETDATA_THREAD_OPTION_DEFAULT,
  218. mdflush_reader,
  219. em
  220. );
  221. // create chart and static dims.
  222. pthread_mutex_lock(&lock);
  223. mdflush_create_charts(em->update_every);
  224. ebpf_update_stats(&plugin_statistics, em);
  225. pthread_mutex_unlock(&lock);
  226. // loop and read from published data until ebpf plugin is closed.
  227. heartbeat_t hb;
  228. heartbeat_init(&hb);
  229. usec_t step = em->update_every * USEC_PER_SEC;
  230. while (!ebpf_exit_plugin) {
  231. (void)heartbeat_next(&hb, step);
  232. if (ebpf_exit_plugin)
  233. break;
  234. // write dims now for all hitherto discovered devices.
  235. write_begin_chart("mdstat", "mdstat_flush");
  236. avl_traverse_lock(&mdflush_pub, mdflush_write_dims, NULL);
  237. write_end_chart();
  238. pthread_mutex_unlock(&lock);
  239. }
  240. }
  241. /**
  242. * mdflush thread.
  243. *
  244. * @param ptr a `ebpf_module_t *`.
  245. * @return always NULL.
  246. */
  247. void *ebpf_mdflush_thread(void *ptr)
  248. {
  249. netdata_thread_cleanup_push(mdflush_exit, ptr);
  250. ebpf_module_t *em = (ebpf_module_t *)ptr;
  251. em->maps = mdflush_maps;
  252. char *md_flush_request = ebpf_find_symbol("md_flush_request");
  253. if (!md_flush_request) {
  254. em->enabled = CONFIG_BOOLEAN_NO;
  255. error("Cannot monitor MD devices, because md is not loaded.");
  256. }
  257. freez(md_flush_request);
  258. if (!em->enabled) {
  259. goto endmdflush;
  260. }
  261. em->probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &em->objects);
  262. if (!em->probe_links) {
  263. em->enabled = CONFIG_BOOLEAN_NO;
  264. goto endmdflush;
  265. }
  266. mdflush_collector(em);
  267. endmdflush:
  268. if (!em->enabled)
  269. ebpf_update_disabled_plugin_stats(em);
  270. netdata_thread_cleanup_pop(1);
  271. return NULL;
  272. }