ebpf_softirq.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "ebpf.h"
  3. #include "ebpf_softirq.h"
  4. struct config softirq_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 SOFTIRQ_MAP_LATENCY 0
  10. static ebpf_local_maps_t softirq_maps[] = {
  11. {
  12. .name = "tbl_softirq",
  13. .internal_input = NETDATA_SOFTIRQ_MAX_IRQS,
  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. #define SOFTIRQ_TP_CLASS_IRQ "irq"
  28. static ebpf_tracepoint_t softirq_tracepoints[] = {
  29. {.enabled = false, .class = SOFTIRQ_TP_CLASS_IRQ, .event = "softirq_entry"},
  30. {.enabled = false, .class = SOFTIRQ_TP_CLASS_IRQ, .event = "softirq_exit"},
  31. /* end */
  32. {.enabled = false, .class = NULL, .event = NULL}
  33. };
  34. // these must be in the order defined by the kernel:
  35. // https://elixir.bootlin.com/linux/v5.12.19/source/include/trace/events/irq.h#L13
  36. static softirq_val_t softirq_vals[] = {
  37. {.name = "HI", .latency = 0},
  38. {.name = "TIMER", .latency = 0},
  39. {.name = "NET_TX", .latency = 0},
  40. {.name = "NET_RX", .latency = 0},
  41. {.name = "BLOCK", .latency = 0},
  42. {.name = "IRQ_POLL", .latency = 0},
  43. {.name = "TASKLET", .latency = 0},
  44. {.name = "SCHED", .latency = 0},
  45. {.name = "HRTIMER", .latency = 0},
  46. {.name = "RCU", .latency = 0},
  47. };
  48. // tmp store for soft IRQ values we get from a per-CPU eBPF map.
  49. static softirq_ebpf_val_t *softirq_ebpf_vals = NULL;
  50. static struct bpf_link **probe_links = NULL;
  51. static struct bpf_object *objects = NULL;
  52. static int read_thread_closed = 1;
  53. static struct netdata_static_thread softirq_threads = {"SOFTIRQ KERNEL",
  54. NULL, NULL, 1, NULL,
  55. NULL, NULL };
  56. /**
  57. * Clean up the main thread.
  58. *
  59. * @param ptr thread data.
  60. */
  61. static void softirq_cleanup(void *ptr)
  62. {
  63. for (int i = 0; softirq_tracepoints[i].class != NULL; i++) {
  64. ebpf_disable_tracepoint(&softirq_tracepoints[i]);
  65. }
  66. ebpf_module_t *em = (ebpf_module_t *)ptr;
  67. if (!em->enabled) {
  68. return;
  69. }
  70. heartbeat_t hb;
  71. heartbeat_init(&hb);
  72. uint32_t tick = 1 * USEC_PER_MS;
  73. while (!read_thread_closed) {
  74. usec_t dt = heartbeat_next(&hb, tick);
  75. UNUSED(dt);
  76. }
  77. freez(softirq_ebpf_vals);
  78. freez(softirq_threads.thread);
  79. if (probe_links) {
  80. struct bpf_program *prog;
  81. size_t i = 0 ;
  82. bpf_object__for_each_program(prog, objects) {
  83. bpf_link__destroy(probe_links[i]);
  84. i++;
  85. }
  86. if (objects)
  87. bpf_object__close(objects);
  88. }
  89. }
  90. /*****************************************************************
  91. * MAIN LOOP
  92. *****************************************************************/
  93. static void softirq_read_latency_map()
  94. {
  95. int fd = softirq_maps[SOFTIRQ_MAP_LATENCY].map_fd;
  96. int i;
  97. for (i = 0; i < NETDATA_SOFTIRQ_MAX_IRQS; i++) {
  98. int test = bpf_map_lookup_elem(fd, &i, softirq_ebpf_vals);
  99. if (unlikely(test < 0)) {
  100. continue;
  101. }
  102. uint64_t total_latency = 0;
  103. int cpu_i;
  104. int end = ebpf_nprocs;
  105. for (cpu_i = 0; cpu_i < end; cpu_i++) {
  106. total_latency += softirq_ebpf_vals[cpu_i].latency/1000;
  107. }
  108. softirq_vals[i].latency = total_latency;
  109. }
  110. }
  111. /**
  112. * Read eBPF maps for soft IRQ.
  113. */
  114. static void *softirq_reader(void *ptr)
  115. {
  116. read_thread_closed = 0;
  117. heartbeat_t hb;
  118. heartbeat_init(&hb);
  119. ebpf_module_t *em = (ebpf_module_t *)ptr;
  120. usec_t step = NETDATA_SOFTIRQ_SLEEP_MS * em->update_every;
  121. while (!close_ebpf_plugin) {
  122. usec_t dt = heartbeat_next(&hb, step);
  123. UNUSED(dt);
  124. softirq_read_latency_map();
  125. }
  126. read_thread_closed = 1;
  127. return NULL;
  128. }
  129. static void softirq_create_charts(int update_every)
  130. {
  131. ebpf_create_chart(
  132. NETDATA_EBPF_SYSTEM_GROUP,
  133. "softirq_latency",
  134. "Software IRQ latency",
  135. EBPF_COMMON_DIMENSION_MILLISECONDS,
  136. "softirqs",
  137. NULL,
  138. NETDATA_EBPF_CHART_TYPE_STACKED,
  139. NETDATA_CHART_PRIO_SYSTEM_SOFTIRQS+1,
  140. NULL, NULL, 0, update_every,
  141. NETDATA_EBPF_MODULE_NAME_SOFTIRQ
  142. );
  143. fflush(stdout);
  144. }
  145. static void softirq_create_dims()
  146. {
  147. uint32_t i;
  148. for (i = 0; i < NETDATA_SOFTIRQ_MAX_IRQS; i++) {
  149. ebpf_write_global_dimension(
  150. softirq_vals[i].name, softirq_vals[i].name,
  151. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX]
  152. );
  153. }
  154. }
  155. static inline void softirq_write_dims()
  156. {
  157. uint32_t i;
  158. for (i = 0; i < NETDATA_SOFTIRQ_MAX_IRQS; i++) {
  159. write_chart_dimension(softirq_vals[i].name, softirq_vals[i].latency);
  160. }
  161. }
  162. /**
  163. * Main loop for this collector.
  164. */
  165. static void softirq_collector(ebpf_module_t *em)
  166. {
  167. softirq_ebpf_vals = callocz(ebpf_nprocs, sizeof(softirq_ebpf_val_t));
  168. // create reader thread.
  169. softirq_threads.thread = mallocz(sizeof(netdata_thread_t));
  170. softirq_threads.start_routine = softirq_reader;
  171. netdata_thread_create(
  172. softirq_threads.thread,
  173. softirq_threads.name,
  174. NETDATA_THREAD_OPTION_JOINABLE,
  175. softirq_reader,
  176. em
  177. );
  178. // create chart and static dims.
  179. pthread_mutex_lock(&lock);
  180. softirq_create_charts(em->update_every);
  181. softirq_create_dims();
  182. ebpf_update_stats(&plugin_statistics, em);
  183. pthread_mutex_unlock(&lock);
  184. // loop and read from published data until ebpf plugin is closed.
  185. int update_every = em->update_every;
  186. int counter = update_every - 1;
  187. while (!close_ebpf_plugin) {
  188. pthread_mutex_lock(&collect_data_mutex);
  189. pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
  190. if (++counter == update_every) {
  191. counter = 0;
  192. pthread_mutex_lock(&lock);
  193. // write dims now for all hitherto discovered IRQs.
  194. write_begin_chart(NETDATA_EBPF_SYSTEM_GROUP, "softirq_latency");
  195. softirq_write_dims();
  196. write_end_chart();
  197. pthread_mutex_unlock(&lock);
  198. }
  199. pthread_mutex_unlock(&collect_data_mutex);
  200. }
  201. }
  202. /*****************************************************************
  203. * EBPF SOFTIRQ THREAD
  204. *****************************************************************/
  205. /**
  206. * Soft IRQ latency thread.
  207. *
  208. * @param ptr a `ebpf_module_t *`.
  209. * @return always NULL.
  210. */
  211. void *ebpf_softirq_thread(void *ptr)
  212. {
  213. netdata_thread_cleanup_push(softirq_cleanup, ptr);
  214. ebpf_module_t *em = (ebpf_module_t *)ptr;
  215. em->maps = softirq_maps;
  216. if (!em->enabled) {
  217. goto endsoftirq;
  218. }
  219. if (ebpf_enable_tracepoints(softirq_tracepoints) == 0) {
  220. em->enabled = CONFIG_BOOLEAN_NO;
  221. goto endsoftirq;
  222. }
  223. probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &objects);
  224. if (!probe_links) {
  225. em->enabled = CONFIG_BOOLEAN_NO;
  226. goto endsoftirq;
  227. }
  228. softirq_collector(em);
  229. endsoftirq:
  230. if (!em->enabled)
  231. ebpf_update_disabled_plugin_stats(em);
  232. netdata_thread_cleanup_pop(1);
  233. return NULL;
  234. }