ebpf_softirq.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. bpf_object__close(objects);
  87. }
  88. }
  89. /*****************************************************************
  90. * MAIN LOOP
  91. *****************************************************************/
  92. static void softirq_read_latency_map()
  93. {
  94. int fd = softirq_maps[SOFTIRQ_MAP_LATENCY].map_fd;
  95. int i;
  96. for (i = 0; i < NETDATA_SOFTIRQ_MAX_IRQS; i++) {
  97. int test = bpf_map_lookup_elem(fd, &i, softirq_ebpf_vals);
  98. if (unlikely(test < 0)) {
  99. continue;
  100. }
  101. uint64_t total_latency = 0;
  102. int cpu_i;
  103. int end = ebpf_nprocs;
  104. for (cpu_i = 0; cpu_i < end; cpu_i++) {
  105. total_latency += softirq_ebpf_vals[cpu_i].latency/1000;
  106. }
  107. softirq_vals[i].latency = total_latency;
  108. }
  109. }
  110. /**
  111. * Read eBPF maps for soft IRQ.
  112. */
  113. static void *softirq_reader(void *ptr)
  114. {
  115. read_thread_closed = 0;
  116. heartbeat_t hb;
  117. heartbeat_init(&hb);
  118. ebpf_module_t *em = (ebpf_module_t *)ptr;
  119. usec_t step = NETDATA_SOFTIRQ_SLEEP_MS * em->update_every;
  120. while (!close_ebpf_plugin) {
  121. usec_t dt = heartbeat_next(&hb, step);
  122. UNUSED(dt);
  123. softirq_read_latency_map();
  124. }
  125. read_thread_closed = 1;
  126. return NULL;
  127. }
  128. static void softirq_create_charts(int update_every)
  129. {
  130. ebpf_create_chart(
  131. NETDATA_EBPF_SYSTEM_GROUP,
  132. "softirq_latency",
  133. "Software IRQ latency",
  134. EBPF_COMMON_DIMENSION_MILLISECONDS,
  135. "softirqs",
  136. NULL,
  137. NETDATA_EBPF_CHART_TYPE_STACKED,
  138. NETDATA_CHART_PRIO_SYSTEM_SOFTIRQS+1,
  139. NULL, NULL, 0, update_every,
  140. NETDATA_EBPF_MODULE_NAME_SOFTIRQ
  141. );
  142. fflush(stdout);
  143. }
  144. static void softirq_create_dims()
  145. {
  146. uint32_t i;
  147. for (i = 0; i < NETDATA_SOFTIRQ_MAX_IRQS; i++) {
  148. ebpf_write_global_dimension(
  149. softirq_vals[i].name, softirq_vals[i].name,
  150. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX]
  151. );
  152. }
  153. }
  154. static inline void softirq_write_dims()
  155. {
  156. uint32_t i;
  157. for (i = 0; i < NETDATA_SOFTIRQ_MAX_IRQS; i++) {
  158. write_chart_dimension(softirq_vals[i].name, softirq_vals[i].latency);
  159. }
  160. }
  161. /**
  162. * Main loop for this collector.
  163. */
  164. static void softirq_collector(ebpf_module_t *em)
  165. {
  166. softirq_ebpf_vals = callocz(ebpf_nprocs, sizeof(softirq_ebpf_val_t));
  167. // create reader thread.
  168. softirq_threads.thread = mallocz(sizeof(netdata_thread_t));
  169. softirq_threads.start_routine = softirq_reader;
  170. netdata_thread_create(
  171. softirq_threads.thread,
  172. softirq_threads.name,
  173. NETDATA_THREAD_OPTION_JOINABLE,
  174. softirq_reader,
  175. em
  176. );
  177. // create chart and static dims.
  178. pthread_mutex_lock(&lock);
  179. softirq_create_charts(em->update_every);
  180. softirq_create_dims();
  181. ebpf_update_stats(&plugin_statistics, em);
  182. pthread_mutex_unlock(&lock);
  183. // loop and read from published data until ebpf plugin is closed.
  184. int update_every = em->update_every;
  185. int counter = update_every - 1;
  186. while (!close_ebpf_plugin) {
  187. pthread_mutex_lock(&collect_data_mutex);
  188. pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
  189. if (++counter == update_every) {
  190. counter = 0;
  191. pthread_mutex_lock(&lock);
  192. // write dims now for all hitherto discovered IRQs.
  193. write_begin_chart(NETDATA_EBPF_SYSTEM_GROUP, "softirq_latency");
  194. softirq_write_dims();
  195. write_end_chart();
  196. pthread_mutex_unlock(&lock);
  197. }
  198. pthread_mutex_unlock(&collect_data_mutex);
  199. }
  200. }
  201. /*****************************************************************
  202. * EBPF SOFTIRQ THREAD
  203. *****************************************************************/
  204. /**
  205. * Soft IRQ latency thread.
  206. *
  207. * @param ptr a `ebpf_module_t *`.
  208. * @return always NULL.
  209. */
  210. void *ebpf_softirq_thread(void *ptr)
  211. {
  212. netdata_thread_cleanup_push(softirq_cleanup, ptr);
  213. ebpf_module_t *em = (ebpf_module_t *)ptr;
  214. em->maps = softirq_maps;
  215. if (!em->enabled) {
  216. goto endsoftirq;
  217. }
  218. if (ebpf_enable_tracepoints(softirq_tracepoints) == 0) {
  219. em->enabled = CONFIG_BOOLEAN_NO;
  220. goto endsoftirq;
  221. }
  222. probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &objects);
  223. if (!probe_links) {
  224. em->enabled = CONFIG_BOOLEAN_NO;
  225. goto endsoftirq;
  226. }
  227. softirq_collector(em);
  228. endsoftirq:
  229. if (!em->enabled)
  230. ebpf_update_disabled_plugin_stats(em);
  231. netdata_thread_cleanup_pop(1);
  232. return NULL;
  233. }