ebpf_hardirq.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "ebpf.h"
  3. #include "ebpf_hardirq.h"
  4. struct config hardirq_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 HARDIRQ_MAP_LATENCY 0
  10. #define HARDIRQ_MAP_LATENCY_STATIC 1
  11. static ebpf_local_maps_t hardirq_maps[] = {
  12. {
  13. .name = "tbl_hardirq",
  14. .internal_input = NETDATA_HARDIRQ_MAX_IRQS,
  15. .user_input = 0,
  16. .type = NETDATA_EBPF_MAP_STATIC,
  17. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED
  18. },
  19. {
  20. .name = "tbl_hardirq_static",
  21. .internal_input = HARDIRQ_EBPF_STATIC_END,
  22. .user_input = 0,
  23. .type = NETDATA_EBPF_MAP_STATIC,
  24. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED
  25. },
  26. /* end */
  27. {
  28. .name = NULL,
  29. .internal_input = 0,
  30. .user_input = 0,
  31. .type = NETDATA_EBPF_MAP_CONTROLLER,
  32. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED
  33. }
  34. };
  35. #define HARDIRQ_TP_CLASS_IRQ "irq"
  36. #define HARDIRQ_TP_CLASS_IRQ_VECTORS "irq_vectors"
  37. static ebpf_tracepoint_t hardirq_tracepoints[] = {
  38. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ, .event = "irq_handler_entry"},
  39. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ, .event = "irq_handler_exit"},
  40. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "thermal_apic_entry"},
  41. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "thermal_apic_exit"},
  42. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "threshold_apic_entry"},
  43. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "threshold_apic_exit"},
  44. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "error_apic_entry"},
  45. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "error_apic_exit"},
  46. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "deferred_error_apic_entry"},
  47. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "deferred_error_apic_exit"},
  48. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "spurious_apic_entry"},
  49. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "spurious_apic_exit"},
  50. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "call_function_entry"},
  51. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "call_function_exit"},
  52. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "call_function_single_entry"},
  53. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "call_function_single_exit"},
  54. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "reschedule_entry"},
  55. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "reschedule_exit"},
  56. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "local_timer_entry"},
  57. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "local_timer_exit"},
  58. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "irq_work_entry"},
  59. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "irq_work_exit"},
  60. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "x86_platform_ipi_entry"},
  61. {.enabled = false, .class = HARDIRQ_TP_CLASS_IRQ_VECTORS, .event = "x86_platform_ipi_exit"},
  62. /* end */
  63. {.enabled = false, .class = NULL, .event = NULL}
  64. };
  65. static hardirq_static_val_t hardirq_static_vals[] = {
  66. {
  67. .idx = HARDIRQ_EBPF_STATIC_APIC_THERMAL,
  68. .name = "apic_thermal",
  69. .latency = 0
  70. },
  71. {
  72. .idx = HARDIRQ_EBPF_STATIC_APIC_THRESHOLD,
  73. .name = "apic_threshold",
  74. .latency = 0
  75. },
  76. {
  77. .idx = HARDIRQ_EBPF_STATIC_APIC_ERROR,
  78. .name = "apic_error",
  79. .latency = 0
  80. },
  81. {
  82. .idx = HARDIRQ_EBPF_STATIC_APIC_DEFERRED_ERROR,
  83. .name = "apic_deferred_error",
  84. .latency = 0
  85. },
  86. {
  87. .idx = HARDIRQ_EBPF_STATIC_APIC_SPURIOUS,
  88. .name = "apic_spurious",
  89. .latency = 0
  90. },
  91. {
  92. .idx = HARDIRQ_EBPF_STATIC_FUNC_CALL,
  93. .name = "func_call",
  94. .latency = 0
  95. },
  96. {
  97. .idx = HARDIRQ_EBPF_STATIC_FUNC_CALL_SINGLE,
  98. .name = "func_call_single",
  99. .latency = 0
  100. },
  101. {
  102. .idx = HARDIRQ_EBPF_STATIC_RESCHEDULE,
  103. .name = "reschedule",
  104. .latency = 0
  105. },
  106. {
  107. .idx = HARDIRQ_EBPF_STATIC_LOCAL_TIMER,
  108. .name = "local_timer",
  109. .latency = 0
  110. },
  111. {
  112. .idx = HARDIRQ_EBPF_STATIC_IRQ_WORK,
  113. .name = "irq_work",
  114. .latency = 0
  115. },
  116. {
  117. .idx = HARDIRQ_EBPF_STATIC_X86_PLATFORM_IPI,
  118. .name = "x86_platform_ipi",
  119. .latency = 0
  120. },
  121. };
  122. // store for "published" data from the reader thread, which the collector
  123. // thread will write to netdata agent.
  124. static avl_tree_lock hardirq_pub;
  125. /*****************************************************************
  126. *
  127. * ARAL SECTION
  128. *
  129. *****************************************************************/
  130. // ARAL vectors used to speed up processing
  131. ARAL *ebpf_aral_hardirq = NULL;
  132. /**
  133. * eBPF hardirq Aral init
  134. *
  135. * Initiallize array allocator that will be used when integration with apps is enabled.
  136. */
  137. static inline void ebpf_hardirq_aral_init()
  138. {
  139. ebpf_aral_hardirq = ebpf_allocate_pid_aral(NETDATA_EBPF_HARDIRQ_ARAL_NAME, sizeof(hardirq_val_t));
  140. }
  141. /**
  142. * eBPF hardirq get
  143. *
  144. * Get a hardirq_val_t entry to be used with a specific IRQ.
  145. *
  146. * @return it returns the address on success.
  147. */
  148. hardirq_val_t *ebpf_hardirq_get(void)
  149. {
  150. hardirq_val_t *target = aral_mallocz(ebpf_aral_hardirq);
  151. memset(target, 0, sizeof(hardirq_val_t));
  152. return target;
  153. }
  154. /**
  155. * eBPF hardirq release
  156. *
  157. * @param stat Release a target after usage.
  158. */
  159. void ebpf_hardirq_release(hardirq_val_t *stat)
  160. {
  161. aral_freez(ebpf_aral_hardirq, stat);
  162. }
  163. /*****************************************************************
  164. *
  165. * EXIT FUNCTIONS
  166. *
  167. *****************************************************************/
  168. /**
  169. * Hardirq Free
  170. *
  171. * Cleanup variables after child threads to stop
  172. *
  173. * @param ptr thread data.
  174. */
  175. static void ebpf_hardirq_free(ebpf_module_t *em)
  176. {
  177. pthread_mutex_lock(&ebpf_exit_cleanup);
  178. em->thread->enabled = NETDATA_THREAD_EBPF_STOPPING;
  179. pthread_mutex_unlock(&ebpf_exit_cleanup);
  180. for (int i = 0; hardirq_tracepoints[i].class != NULL; i++) {
  181. ebpf_disable_tracepoint(&hardirq_tracepoints[i]);
  182. }
  183. pthread_mutex_lock(&ebpf_exit_cleanup);
  184. em->thread->enabled = NETDATA_THREAD_EBPF_STOPPED;
  185. pthread_mutex_unlock(&ebpf_exit_cleanup);
  186. }
  187. /**
  188. * Hardirq Exit
  189. *
  190. * Cancel child and exit.
  191. *
  192. * @param ptr thread data.
  193. */
  194. static void hardirq_exit(void *ptr)
  195. {
  196. ebpf_module_t *em = (ebpf_module_t *)ptr;
  197. ebpf_hardirq_free(em);
  198. }
  199. /*****************************************************************
  200. * MAIN LOOP
  201. *****************************************************************/
  202. /**
  203. * Compare hard IRQ values.
  204. *
  205. * @param a `hardirq_val_t *`.
  206. * @param b `hardirq_val_t *`.
  207. *
  208. * @return 0 if a==b, 1 if a>b, -1 if a<b.
  209. */
  210. static int hardirq_val_cmp(void *a, void *b)
  211. {
  212. hardirq_val_t *ptr1 = a;
  213. hardirq_val_t *ptr2 = b;
  214. if (ptr1->irq > ptr2->irq) {
  215. return 1;
  216. }
  217. else if (ptr1->irq < ptr2->irq) {
  218. return -1;
  219. }
  220. else {
  221. return 0;
  222. }
  223. }
  224. /**
  225. * Parse interrupts
  226. *
  227. * Parse /proc/interrupts to get names used in metrics
  228. *
  229. * @param irq_name vector to store data.
  230. * @param irq irq value
  231. *
  232. * @return It returns 0 on success and -1 otherwise
  233. */
  234. static int hardirq_parse_interrupts(char *irq_name, int irq)
  235. {
  236. static procfile *ff = NULL;
  237. static int cpus = -1;
  238. if(unlikely(!ff)) {
  239. char filename[FILENAME_MAX + 1];
  240. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/interrupts");
  241. ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
  242. }
  243. if(unlikely(!ff))
  244. return -1;
  245. ff = procfile_readall(ff);
  246. if(unlikely(!ff))
  247. return -1; // we return 0, so that we will retry to open it next time
  248. size_t words = procfile_linewords(ff, 0);
  249. if(unlikely(cpus == -1)) {
  250. uint32_t w;
  251. cpus = 0;
  252. for(w = 0; w < words ; w++) {
  253. if(likely(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0))
  254. cpus++;
  255. }
  256. }
  257. size_t lines = procfile_lines(ff), l;
  258. if(unlikely(!lines)) {
  259. collector_error("Cannot read /proc/interrupts, zero lines reported.");
  260. return -1;
  261. }
  262. for(l = 1; l < lines ;l++) {
  263. words = procfile_linewords(ff, l);
  264. if(unlikely(!words)) continue;
  265. const char *id = procfile_lineword(ff, l, 0);
  266. if (!isdigit(id[0]))
  267. continue;
  268. int cmp = str2i(id);
  269. if (cmp != irq)
  270. continue;
  271. if(unlikely((uint32_t)(cpus + 2) < words)) {
  272. const char *name = procfile_lineword(ff, l, words - 1);
  273. // On some motherboards IRQ can have the same name, so we append IRQ id to differentiate.
  274. snprintfz(irq_name, NETDATA_HARDIRQ_NAME_LEN - 1, "%d_%s", irq, name);
  275. }
  276. }
  277. return 0;
  278. }
  279. /**
  280. * Read Latency MAP
  281. *
  282. * Read data from kernel ring to user ring.
  283. *
  284. * @param mapfd hash map id.
  285. *
  286. * @return it returns 0 on success and -1 otherwise
  287. */
  288. static int hardirq_read_latency_map(int mapfd)
  289. {
  290. hardirq_ebpf_static_val_t hardirq_ebpf_vals[ebpf_nprocs + 1];
  291. hardirq_ebpf_key_t key = {};
  292. hardirq_ebpf_key_t next_key = {};
  293. hardirq_val_t search_v = {};
  294. hardirq_val_t *v = NULL;
  295. while (bpf_map_get_next_key(mapfd, &key, &next_key) == 0) {
  296. // get val for this key.
  297. int test = bpf_map_lookup_elem(mapfd, &key, hardirq_ebpf_vals);
  298. if (unlikely(test < 0)) {
  299. key = next_key;
  300. continue;
  301. }
  302. // is this IRQ saved yet?
  303. //
  304. // if not, make a new one, mark it as unsaved for now, and continue; we
  305. // will insert it at the end after all of its values are correctly set,
  306. // so that we can safely publish it to the collector within a single,
  307. // short locked operation.
  308. //
  309. // otherwise simply continue; we will only update the latency, which
  310. // can be republished safely without a lock.
  311. //
  312. // NOTE: lock isn't strictly necessary for this initial search, as only
  313. // this thread does writing, but the AVL is using a read-write lock so
  314. // there is no congestion.
  315. bool v_is_new = false;
  316. search_v.irq = key.irq;
  317. v = (hardirq_val_t *)avl_search_lock(&hardirq_pub, (avl_t *)&search_v);
  318. if (unlikely(v == NULL)) {
  319. // latency/name can only be added reliably at a later time.
  320. // when they're added, only then will we AVL insert.
  321. v = ebpf_hardirq_get();
  322. v->irq = key.irq;
  323. v->dim_exists = false;
  324. v_is_new = true;
  325. }
  326. // note two things:
  327. // 1. we must add up latency value for this IRQ across all CPUs.
  328. // 2. the name is unfortunately *not* available on all CPU maps - only
  329. // a single map contains the name, so we must find it. we only need
  330. // to copy it though if the IRQ is new for us.
  331. uint64_t total_latency = 0;
  332. int i;
  333. for (i = 0; i < ebpf_nprocs; i++) {
  334. total_latency += hardirq_ebpf_vals[i].latency/1000;
  335. }
  336. // can now safely publish latency for existing IRQs.
  337. v->latency = total_latency;
  338. // can now safely publish new IRQ.
  339. if (v_is_new) {
  340. if (hardirq_parse_interrupts(v->name, v->irq)) {
  341. ebpf_hardirq_release(v);
  342. return -1;
  343. }
  344. avl_t *check = avl_insert_lock(&hardirq_pub, (avl_t *)v);
  345. if (check != (avl_t *)v) {
  346. error("Internal error, cannot insert the AVL tree.");
  347. }
  348. }
  349. key = next_key;
  350. }
  351. return 0;
  352. }
  353. static void hardirq_read_latency_static_map(int mapfd)
  354. {
  355. hardirq_ebpf_static_val_t hardirq_ebpf_static_vals[ebpf_nprocs + 1];
  356. uint32_t i;
  357. for (i = 0; i < HARDIRQ_EBPF_STATIC_END; i++) {
  358. uint32_t map_i = hardirq_static_vals[i].idx;
  359. int test = bpf_map_lookup_elem(mapfd, &map_i, hardirq_ebpf_static_vals);
  360. if (unlikely(test < 0)) {
  361. continue;
  362. }
  363. uint64_t total_latency = 0;
  364. int cpu_i;
  365. int end = (running_on_kernel < NETDATA_KERNEL_V4_15) ? 1 : ebpf_nprocs;
  366. for (cpu_i = 0; cpu_i < end; cpu_i++) {
  367. total_latency += hardirq_ebpf_static_vals[cpu_i].latency/1000;
  368. }
  369. hardirq_static_vals[i].latency = total_latency;
  370. }
  371. }
  372. /**
  373. * Read eBPF maps for hard IRQ.
  374. *
  375. * @return When it is not possible to parse /proc, it returns -1, on success it returns 0;
  376. */
  377. static int hardirq_reader()
  378. {
  379. if (hardirq_read_latency_map(hardirq_maps[HARDIRQ_MAP_LATENCY].map_fd))
  380. return -1;
  381. hardirq_read_latency_static_map(hardirq_maps[HARDIRQ_MAP_LATENCY_STATIC].map_fd);
  382. return 0;
  383. }
  384. static void hardirq_create_charts(int update_every)
  385. {
  386. ebpf_create_chart(
  387. NETDATA_EBPF_SYSTEM_GROUP,
  388. "hardirq_latency",
  389. "Hardware IRQ latency",
  390. EBPF_COMMON_DIMENSION_MILLISECONDS,
  391. "interrupts",
  392. NULL,
  393. NETDATA_EBPF_CHART_TYPE_STACKED,
  394. NETDATA_CHART_PRIO_HARDIRQ_LATENCY,
  395. NULL, NULL, 0, update_every,
  396. NETDATA_EBPF_MODULE_NAME_HARDIRQ
  397. );
  398. fflush(stdout);
  399. }
  400. static void hardirq_create_static_dims()
  401. {
  402. uint32_t i;
  403. for (i = 0; i < HARDIRQ_EBPF_STATIC_END; i++) {
  404. ebpf_write_global_dimension(
  405. hardirq_static_vals[i].name, hardirq_static_vals[i].name,
  406. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX]
  407. );
  408. }
  409. }
  410. // callback for avl tree traversal on `hardirq_pub`.
  411. static int hardirq_write_dims(void *entry, void *data)
  412. {
  413. UNUSED(data);
  414. hardirq_val_t *v = entry;
  415. // IRQs get dynamically added in, so add the dimension if we haven't yet.
  416. if (!v->dim_exists) {
  417. ebpf_write_global_dimension(
  418. v->name, v->name,
  419. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX]
  420. );
  421. v->dim_exists = true;
  422. }
  423. write_chart_dimension(v->name, v->latency);
  424. return 1;
  425. }
  426. static inline void hardirq_write_static_dims()
  427. {
  428. uint32_t i;
  429. for (i = 0; i < HARDIRQ_EBPF_STATIC_END; i++) {
  430. write_chart_dimension(
  431. hardirq_static_vals[i].name,
  432. hardirq_static_vals[i].latency
  433. );
  434. }
  435. }
  436. /**
  437. * Main loop for this collector.
  438. */
  439. static void hardirq_collector(ebpf_module_t *em)
  440. {
  441. avl_init_lock(&hardirq_pub, hardirq_val_cmp);
  442. ebpf_hardirq_aral_init();
  443. // create chart and static dims.
  444. pthread_mutex_lock(&lock);
  445. hardirq_create_charts(em->update_every);
  446. hardirq_create_static_dims();
  447. ebpf_update_stats(&plugin_statistics, em);
  448. ebpf_update_kernel_memory_with_vector(&plugin_statistics, em->maps);
  449. pthread_mutex_unlock(&lock);
  450. // loop and read from published data until ebpf plugin is closed.
  451. heartbeat_t hb;
  452. heartbeat_init(&hb);
  453. int update_every = em->update_every;
  454. int counter = update_every - 1;
  455. //This will be cancelled by its parent
  456. while (!ebpf_exit_plugin) {
  457. (void)heartbeat_next(&hb, USEC_PER_SEC);
  458. if (ebpf_exit_plugin || ++counter != update_every)
  459. continue;
  460. counter = 0;
  461. if (hardirq_reader())
  462. break;
  463. pthread_mutex_lock(&lock);
  464. // write dims now for all hitherto discovered IRQs.
  465. write_begin_chart(NETDATA_EBPF_SYSTEM_GROUP, "hardirq_latency");
  466. avl_traverse_lock(&hardirq_pub, hardirq_write_dims, NULL);
  467. hardirq_write_static_dims();
  468. write_end_chart();
  469. pthread_mutex_unlock(&lock);
  470. }
  471. }
  472. /*****************************************************************
  473. * EBPF HARDIRQ THREAD
  474. *****************************************************************/
  475. /**
  476. * Hard IRQ latency thread.
  477. *
  478. * @param ptr a `ebpf_module_t *`.
  479. * @return always NULL.
  480. */
  481. void *ebpf_hardirq_thread(void *ptr)
  482. {
  483. netdata_thread_cleanup_push(hardirq_exit, ptr);
  484. ebpf_module_t *em = (ebpf_module_t *)ptr;
  485. em->maps = hardirq_maps;
  486. if (ebpf_enable_tracepoints(hardirq_tracepoints) == 0) {
  487. em->thread->enabled = NETDATA_THREAD_EBPF_STOPPED;
  488. goto endhardirq;
  489. }
  490. em->probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &em->objects);
  491. if (!em->probe_links) {
  492. em->thread->enabled = NETDATA_THREAD_EBPF_STOPPED;
  493. goto endhardirq;
  494. }
  495. hardirq_collector(em);
  496. endhardirq:
  497. ebpf_update_disabled_plugin_stats(em);
  498. netdata_thread_cleanup_pop(1);
  499. return NULL;
  500. }