ebpf_softirq.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. /**
  51. * Cachestat Free
  52. *
  53. * Cleanup variables after child threads to stop
  54. *
  55. * @param ptr thread data.
  56. */
  57. static void ebpf_softirq_free(ebpf_module_t *em)
  58. {
  59. pthread_mutex_lock(&ebpf_exit_cleanup);
  60. em->thread->enabled = NETDATA_THREAD_EBPF_STOPPING;
  61. pthread_mutex_unlock(&ebpf_exit_cleanup);
  62. for (int i = 0; softirq_tracepoints[i].class != NULL; i++) {
  63. ebpf_disable_tracepoint(&softirq_tracepoints[i]);
  64. }
  65. freez(softirq_ebpf_vals);
  66. pthread_mutex_lock(&ebpf_exit_cleanup);
  67. em->thread->enabled = NETDATA_THREAD_EBPF_STOPPED;
  68. pthread_mutex_unlock(&ebpf_exit_cleanup);
  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. ebpf_softirq_free(em);
  81. }
  82. /*****************************************************************
  83. * MAIN LOOP
  84. *****************************************************************/
  85. static void softirq_read_latency_map()
  86. {
  87. int fd = softirq_maps[SOFTIRQ_MAP_LATENCY].map_fd;
  88. int i;
  89. for (i = 0; i < NETDATA_SOFTIRQ_MAX_IRQS; i++) {
  90. int test = bpf_map_lookup_elem(fd, &i, softirq_ebpf_vals);
  91. if (unlikely(test < 0)) {
  92. continue;
  93. }
  94. uint64_t total_latency = 0;
  95. int cpu_i;
  96. int end = ebpf_nprocs;
  97. for (cpu_i = 0; cpu_i < end; cpu_i++) {
  98. total_latency += softirq_ebpf_vals[cpu_i].latency/1000;
  99. }
  100. softirq_vals[i].latency = total_latency;
  101. }
  102. }
  103. static void softirq_create_charts(int update_every)
  104. {
  105. ebpf_create_chart(
  106. NETDATA_EBPF_SYSTEM_GROUP,
  107. "softirq_latency",
  108. "Software IRQ latency",
  109. EBPF_COMMON_DIMENSION_MILLISECONDS,
  110. "softirqs",
  111. NULL,
  112. NETDATA_EBPF_CHART_TYPE_STACKED,
  113. NETDATA_CHART_PRIO_SYSTEM_SOFTIRQS+1,
  114. NULL, NULL, 0, update_every,
  115. NETDATA_EBPF_MODULE_NAME_SOFTIRQ
  116. );
  117. fflush(stdout);
  118. }
  119. static void softirq_create_dims()
  120. {
  121. uint32_t i;
  122. for (i = 0; i < NETDATA_SOFTIRQ_MAX_IRQS; i++) {
  123. ebpf_write_global_dimension(
  124. softirq_vals[i].name, softirq_vals[i].name,
  125. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX]
  126. );
  127. }
  128. }
  129. static inline void softirq_write_dims()
  130. {
  131. uint32_t i;
  132. for (i = 0; i < NETDATA_SOFTIRQ_MAX_IRQS; i++) {
  133. write_chart_dimension(softirq_vals[i].name, softirq_vals[i].latency);
  134. }
  135. }
  136. /**
  137. * Main loop for this collector.
  138. */
  139. static void softirq_collector(ebpf_module_t *em)
  140. {
  141. softirq_ebpf_vals = callocz(ebpf_nprocs, sizeof(softirq_ebpf_val_t));
  142. // create chart and static dims.
  143. pthread_mutex_lock(&lock);
  144. softirq_create_charts(em->update_every);
  145. softirq_create_dims();
  146. ebpf_update_stats(&plugin_statistics, em);
  147. pthread_mutex_unlock(&lock);
  148. // loop and read from published data until ebpf plugin is closed.
  149. heartbeat_t hb;
  150. heartbeat_init(&hb);
  151. int update_every = em->update_every;
  152. int counter = update_every - 1;
  153. //This will be cancelled by its parent
  154. while (!ebpf_exit_plugin) {
  155. (void)heartbeat_next(&hb, USEC_PER_SEC);
  156. if (ebpf_exit_plugin || ++counter != update_every)
  157. continue;
  158. counter = 0;
  159. softirq_read_latency_map();
  160. pthread_mutex_lock(&lock);
  161. // write dims now for all hitherto discovered IRQs.
  162. write_begin_chart(NETDATA_EBPF_SYSTEM_GROUP, "softirq_latency");
  163. softirq_write_dims();
  164. write_end_chart();
  165. pthread_mutex_unlock(&lock);
  166. }
  167. }
  168. /*****************************************************************
  169. * EBPF SOFTIRQ THREAD
  170. *****************************************************************/
  171. /**
  172. * Soft IRQ latency thread.
  173. *
  174. * @param ptr a `ebpf_module_t *`.
  175. * @return always NULL.
  176. */
  177. void *ebpf_softirq_thread(void *ptr)
  178. {
  179. netdata_thread_cleanup_push(softirq_cleanup, ptr);
  180. ebpf_module_t *em = (ebpf_module_t *)ptr;
  181. em->maps = softirq_maps;
  182. if (ebpf_enable_tracepoints(softirq_tracepoints) == 0) {
  183. em->thread->enabled = NETDATA_THREAD_EBPF_STOPPED;
  184. goto endsoftirq;
  185. }
  186. em->probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &em->objects);
  187. if (!em->probe_links) {
  188. em->thread->enabled = NETDATA_THREAD_EBPF_STOPPED;
  189. goto endsoftirq;
  190. }
  191. softirq_collector(em);
  192. endsoftirq:
  193. ebpf_update_disabled_plugin_stats(em);
  194. netdata_thread_cleanup_pop(1);
  195. return NULL;
  196. }