ebpf_softirq.c 7.4 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 netdata_static_thread softirq_threads = {"SOFTIRQ KERNEL",
  51. NULL, NULL, 1, NULL,
  52. NULL, NULL };
  53. static enum ebpf_threads_status ebpf_softirq_exited = NETDATA_THREAD_EBPF_RUNNING;
  54. /**
  55. * Exit
  56. *
  57. * Cancel thread.
  58. *
  59. * @param ptr thread data.
  60. */
  61. static void softirq_exit(void *ptr)
  62. {
  63. ebpf_module_t *em = (ebpf_module_t *)ptr;
  64. if (!em->enabled) {
  65. em->enabled = NETDATA_MAIN_THREAD_EXITED;
  66. return;
  67. }
  68. ebpf_softirq_exited = NETDATA_THREAD_EBPF_STOPPING;
  69. }
  70. /**
  71. * Cleanup
  72. *
  73. * Clean up allocated memory.
  74. *
  75. * @param ptr thread data.
  76. */
  77. static void softirq_cleanup(void *ptr)
  78. {
  79. ebpf_module_t *em = (ebpf_module_t *)ptr;
  80. if (ebpf_softirq_exited != NETDATA_THREAD_EBPF_STOPPED)
  81. return;
  82. freez(softirq_threads.thread);
  83. for (int i = 0; softirq_tracepoints[i].class != NULL; i++) {
  84. ebpf_disable_tracepoint(&softirq_tracepoints[i]);
  85. }
  86. freez(softirq_ebpf_vals);
  87. softirq_threads.enabled = NETDATA_MAIN_THREAD_EXITED;
  88. em->enabled = NETDATA_MAIN_THREAD_EXITED;
  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. netdata_thread_cleanup_push(softirq_exit, ptr);
  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 (ebpf_softirq_exited == NETDATA_THREAD_EBPF_RUNNING) {
  122. usec_t dt = heartbeat_next(&hb, step);
  123. UNUSED(dt);
  124. if (ebpf_softirq_exited == NETDATA_THREAD_EBPF_STOPPING)
  125. break;
  126. softirq_read_latency_map();
  127. }
  128. ebpf_softirq_exited = NETDATA_THREAD_EBPF_STOPPED;
  129. netdata_thread_cleanup_pop(1);
  130. return NULL;
  131. }
  132. static void softirq_create_charts(int update_every)
  133. {
  134. ebpf_create_chart(
  135. NETDATA_EBPF_SYSTEM_GROUP,
  136. "softirq_latency",
  137. "Software IRQ latency",
  138. EBPF_COMMON_DIMENSION_MILLISECONDS,
  139. "softirqs",
  140. NULL,
  141. NETDATA_EBPF_CHART_TYPE_STACKED,
  142. NETDATA_CHART_PRIO_SYSTEM_SOFTIRQS+1,
  143. NULL, NULL, 0, update_every,
  144. NETDATA_EBPF_MODULE_NAME_SOFTIRQ
  145. );
  146. fflush(stdout);
  147. }
  148. static void softirq_create_dims()
  149. {
  150. uint32_t i;
  151. for (i = 0; i < NETDATA_SOFTIRQ_MAX_IRQS; i++) {
  152. ebpf_write_global_dimension(
  153. softirq_vals[i].name, softirq_vals[i].name,
  154. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX]
  155. );
  156. }
  157. }
  158. static inline void softirq_write_dims()
  159. {
  160. uint32_t i;
  161. for (i = 0; i < NETDATA_SOFTIRQ_MAX_IRQS; i++) {
  162. write_chart_dimension(softirq_vals[i].name, softirq_vals[i].latency);
  163. }
  164. }
  165. /**
  166. * Main loop for this collector.
  167. */
  168. static void softirq_collector(ebpf_module_t *em)
  169. {
  170. softirq_ebpf_vals = callocz(ebpf_nprocs, sizeof(softirq_ebpf_val_t));
  171. // create reader thread.
  172. softirq_threads.thread = mallocz(sizeof(netdata_thread_t));
  173. softirq_threads.start_routine = softirq_reader;
  174. netdata_thread_create(
  175. softirq_threads.thread,
  176. softirq_threads.name,
  177. NETDATA_THREAD_OPTION_DEFAULT,
  178. softirq_reader,
  179. em
  180. );
  181. // create chart and static dims.
  182. pthread_mutex_lock(&lock);
  183. softirq_create_charts(em->update_every);
  184. softirq_create_dims();
  185. ebpf_update_stats(&plugin_statistics, em);
  186. pthread_mutex_unlock(&lock);
  187. // loop and read from published data until ebpf plugin is closed.
  188. heartbeat_t hb;
  189. heartbeat_init(&hb);
  190. usec_t step = em->update_every * USEC_PER_SEC;
  191. //This will be cancelled by its parent
  192. while (!ebpf_exit_plugin) {
  193. (void)heartbeat_next(&hb, step);
  194. if (ebpf_exit_plugin)
  195. break;
  196. pthread_mutex_lock(&lock);
  197. // write dims now for all hitherto discovered IRQs.
  198. write_begin_chart(NETDATA_EBPF_SYSTEM_GROUP, "softirq_latency");
  199. softirq_write_dims();
  200. write_end_chart();
  201. pthread_mutex_unlock(&lock);
  202. }
  203. }
  204. /*****************************************************************
  205. * EBPF SOFTIRQ THREAD
  206. *****************************************************************/
  207. /**
  208. * Soft IRQ latency thread.
  209. *
  210. * @param ptr a `ebpf_module_t *`.
  211. * @return always NULL.
  212. */
  213. void *ebpf_softirq_thread(void *ptr)
  214. {
  215. netdata_thread_cleanup_push(softirq_cleanup, ptr);
  216. ebpf_module_t *em = (ebpf_module_t *)ptr;
  217. em->maps = softirq_maps;
  218. if (!em->enabled) {
  219. goto endsoftirq;
  220. }
  221. if (ebpf_enable_tracepoints(softirq_tracepoints) == 0) {
  222. em->enabled = CONFIG_BOOLEAN_NO;
  223. goto endsoftirq;
  224. }
  225. em->probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &em->objects);
  226. if (!em->probe_links) {
  227. em->enabled = CONFIG_BOOLEAN_NO;
  228. goto endsoftirq;
  229. }
  230. softirq_collector(em);
  231. endsoftirq:
  232. if (!em->enabled)
  233. ebpf_update_disabled_plugin_stats(em);
  234. netdata_thread_cleanup_pop(1);
  235. return NULL;
  236. }