ebpf_hardirq.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. for (int i = 0; hardirq_tracepoints[i].class != NULL; i++) {
  178. ebpf_disable_tracepoint(&hardirq_tracepoints[i]);
  179. }
  180. pthread_mutex_lock(&ebpf_exit_cleanup);
  181. em->enabled = NETDATA_THREAD_EBPF_STOPPED;
  182. pthread_mutex_unlock(&ebpf_exit_cleanup);
  183. }
  184. /**
  185. * Hardirq Exit
  186. *
  187. * Cancel child and exit.
  188. *
  189. * @param ptr thread data.
  190. */
  191. static void hardirq_exit(void *ptr)
  192. {
  193. ebpf_module_t *em = (ebpf_module_t *)ptr;
  194. ebpf_hardirq_free(em);
  195. }
  196. /*****************************************************************
  197. * MAIN LOOP
  198. *****************************************************************/
  199. /**
  200. * Compare hard IRQ values.
  201. *
  202. * @param a `hardirq_val_t *`.
  203. * @param b `hardirq_val_t *`.
  204. *
  205. * @return 0 if a==b, 1 if a>b, -1 if a<b.
  206. */
  207. static int hardirq_val_cmp(void *a, void *b)
  208. {
  209. hardirq_val_t *ptr1 = a;
  210. hardirq_val_t *ptr2 = b;
  211. if (ptr1->irq > ptr2->irq) {
  212. return 1;
  213. }
  214. else if (ptr1->irq < ptr2->irq) {
  215. return -1;
  216. }
  217. else {
  218. return 0;
  219. }
  220. }
  221. /**
  222. * Parse interrupts
  223. *
  224. * Parse /proc/interrupts to get names used in metrics
  225. *
  226. * @param irq_name vector to store data.
  227. * @param irq irq value
  228. *
  229. * @return It returns 0 on success and -1 otherwise
  230. */
  231. static int hardirq_parse_interrupts(char *irq_name, int irq)
  232. {
  233. static procfile *ff = NULL;
  234. static int cpus = -1;
  235. if(unlikely(!ff)) {
  236. char filename[FILENAME_MAX + 1];
  237. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/interrupts");
  238. ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
  239. }
  240. if(unlikely(!ff))
  241. return -1;
  242. ff = procfile_readall(ff);
  243. if(unlikely(!ff))
  244. return -1; // we return 0, so that we will retry to open it next time
  245. size_t words = procfile_linewords(ff, 0);
  246. if(unlikely(cpus == -1)) {
  247. uint32_t w;
  248. cpus = 0;
  249. for(w = 0; w < words ; w++) {
  250. if(likely(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0))
  251. cpus++;
  252. }
  253. }
  254. size_t lines = procfile_lines(ff), l;
  255. if(unlikely(!lines)) {
  256. collector_error("Cannot read /proc/interrupts, zero lines reported.");
  257. return -1;
  258. }
  259. for(l = 1; l < lines ;l++) {
  260. words = procfile_linewords(ff, l);
  261. if(unlikely(!words)) continue;
  262. const char *id = procfile_lineword(ff, l, 0);
  263. if (!isdigit(id[0]))
  264. continue;
  265. int cmp = str2i(id);
  266. if (cmp != irq)
  267. continue;
  268. if(unlikely((uint32_t)(cpus + 2) < words)) {
  269. const char *name = procfile_lineword(ff, l, words - 1);
  270. // On some motherboards IRQ can have the same name, so we append IRQ id to differentiate.
  271. snprintfz(irq_name, NETDATA_HARDIRQ_NAME_LEN - 1, "%d_%s", irq, name);
  272. }
  273. }
  274. return 0;
  275. }
  276. /**
  277. * Read Latency MAP
  278. *
  279. * Read data from kernel ring to user ring.
  280. *
  281. * @param mapfd hash map id.
  282. *
  283. * @return it returns 0 on success and -1 otherwise
  284. */
  285. static int hardirq_read_latency_map(int mapfd)
  286. {
  287. static hardirq_ebpf_static_val_t *hardirq_ebpf_vals = NULL;
  288. if (!hardirq_ebpf_vals)
  289. hardirq_ebpf_vals = callocz(ebpf_nprocs + 1, sizeof(hardirq_ebpf_static_val_t));
  290. hardirq_ebpf_key_t key = {};
  291. hardirq_ebpf_key_t next_key = {};
  292. hardirq_val_t search_v = {};
  293. hardirq_val_t *v = NULL;
  294. while (bpf_map_get_next_key(mapfd, &key, &next_key) == 0) {
  295. // get val for this key.
  296. int test = bpf_map_lookup_elem(mapfd, &key, hardirq_ebpf_vals);
  297. if (unlikely(test < 0)) {
  298. key = next_key;
  299. continue;
  300. }
  301. // is this IRQ saved yet?
  302. //
  303. // if not, make a new one, mark it as unsaved for now, and continue; we
  304. // will insert it at the end after all of its values are correctly set,
  305. // so that we can safely publish it to the collector within a single,
  306. // short locked operation.
  307. //
  308. // otherwise simply continue; we will only update the latency, which
  309. // can be republished safely without a lock.
  310. //
  311. // NOTE: lock isn't strictly necessary for this initial search, as only
  312. // this thread does writing, but the AVL is using a read-write lock so
  313. // there is no congestion.
  314. bool v_is_new = false;
  315. search_v.irq = key.irq;
  316. v = (hardirq_val_t *)avl_search_lock(&hardirq_pub, (avl_t *)&search_v);
  317. if (unlikely(v == NULL)) {
  318. // latency/name can only be added reliably at a later time.
  319. // when they're added, only then will we AVL insert.
  320. v = ebpf_hardirq_get();
  321. v->irq = key.irq;
  322. v->dim_exists = false;
  323. v_is_new = true;
  324. }
  325. // note two things:
  326. // 1. we must add up latency value for this IRQ across all CPUs.
  327. // 2. the name is unfortunately *not* available on all CPU maps - only
  328. // a single map contains the name, so we must find it. we only need
  329. // to copy it though if the IRQ is new for us.
  330. uint64_t total_latency = 0;
  331. int i;
  332. for (i = 0; i < ebpf_nprocs; i++) {
  333. total_latency += hardirq_ebpf_vals[i].latency/1000;
  334. }
  335. // can now safely publish latency for existing IRQs.
  336. v->latency = total_latency;
  337. // can now safely publish new IRQ.
  338. if (v_is_new) {
  339. if (hardirq_parse_interrupts(v->name, v->irq)) {
  340. ebpf_hardirq_release(v);
  341. return -1;
  342. }
  343. avl_t *check = avl_insert_lock(&hardirq_pub, (avl_t *)v);
  344. if (check != (avl_t *)v) {
  345. error("Internal error, cannot insert the AVL tree.");
  346. }
  347. }
  348. key = next_key;
  349. }
  350. return 0;
  351. }
  352. static void hardirq_read_latency_static_map(int mapfd)
  353. {
  354. static hardirq_ebpf_static_val_t *hardirq_ebpf_static_vals = NULL;
  355. if (!hardirq_ebpf_static_vals)
  356. hardirq_ebpf_static_vals = callocz(ebpf_nprocs + 1, sizeof(hardirq_ebpf_static_val_t));
  357. uint32_t i;
  358. for (i = 0; i < HARDIRQ_EBPF_STATIC_END; i++) {
  359. uint32_t map_i = hardirq_static_vals[i].idx;
  360. int test = bpf_map_lookup_elem(mapfd, &map_i, hardirq_ebpf_static_vals);
  361. if (unlikely(test < 0)) {
  362. continue;
  363. }
  364. uint64_t total_latency = 0;
  365. int cpu_i;
  366. int end = (running_on_kernel < NETDATA_KERNEL_V4_15) ? 1 : ebpf_nprocs;
  367. for (cpu_i = 0; cpu_i < end; cpu_i++) {
  368. total_latency += hardirq_ebpf_static_vals[cpu_i].latency/1000;
  369. }
  370. hardirq_static_vals[i].latency = total_latency;
  371. }
  372. }
  373. /**
  374. * Read eBPF maps for hard IRQ.
  375. *
  376. * @return When it is not possible to parse /proc, it returns -1, on success it returns 0;
  377. */
  378. static int hardirq_reader()
  379. {
  380. if (hardirq_read_latency_map(hardirq_maps[HARDIRQ_MAP_LATENCY].map_fd))
  381. return -1;
  382. hardirq_read_latency_static_map(hardirq_maps[HARDIRQ_MAP_LATENCY_STATIC].map_fd);
  383. return 0;
  384. }
  385. static void hardirq_create_charts(int update_every)
  386. {
  387. ebpf_create_chart(
  388. NETDATA_EBPF_SYSTEM_GROUP,
  389. "hardirq_latency",
  390. "Hardware IRQ latency",
  391. EBPF_COMMON_DIMENSION_MILLISECONDS,
  392. "interrupts",
  393. NULL,
  394. NETDATA_EBPF_CHART_TYPE_STACKED,
  395. NETDATA_CHART_PRIO_HARDIRQ_LATENCY,
  396. NULL, NULL, 0, update_every,
  397. NETDATA_EBPF_MODULE_NAME_HARDIRQ
  398. );
  399. fflush(stdout);
  400. }
  401. static void hardirq_create_static_dims()
  402. {
  403. uint32_t i;
  404. for (i = 0; i < HARDIRQ_EBPF_STATIC_END; i++) {
  405. ebpf_write_global_dimension(
  406. hardirq_static_vals[i].name, hardirq_static_vals[i].name,
  407. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX]
  408. );
  409. }
  410. }
  411. // callback for avl tree traversal on `hardirq_pub`.
  412. static int hardirq_write_dims(void *entry, void *data)
  413. {
  414. UNUSED(data);
  415. hardirq_val_t *v = entry;
  416. // IRQs get dynamically added in, so add the dimension if we haven't yet.
  417. if (!v->dim_exists) {
  418. ebpf_write_global_dimension(
  419. v->name, v->name,
  420. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX]
  421. );
  422. v->dim_exists = true;
  423. }
  424. write_chart_dimension(v->name, v->latency);
  425. return 1;
  426. }
  427. static inline void hardirq_write_static_dims()
  428. {
  429. uint32_t i;
  430. for (i = 0; i < HARDIRQ_EBPF_STATIC_END; i++) {
  431. write_chart_dimension(
  432. hardirq_static_vals[i].name,
  433. hardirq_static_vals[i].latency
  434. );
  435. }
  436. }
  437. /**
  438. * Main loop for this collector.
  439. *
  440. * @param em the main thread structure.
  441. */
  442. static void hardirq_collector(ebpf_module_t *em)
  443. {
  444. memset(&hardirq_pub, 0, sizeof(hardirq_pub));
  445. avl_init_lock(&hardirq_pub, hardirq_val_cmp);
  446. ebpf_hardirq_aral_init();
  447. // create chart and static dims.
  448. pthread_mutex_lock(&lock);
  449. hardirq_create_charts(em->update_every);
  450. hardirq_create_static_dims();
  451. ebpf_update_stats(&plugin_statistics, em);
  452. ebpf_update_kernel_memory_with_vector(&plugin_statistics, em->maps);
  453. pthread_mutex_unlock(&lock);
  454. // loop and read from published data until ebpf plugin is closed.
  455. heartbeat_t hb;
  456. heartbeat_init(&hb);
  457. int update_every = em->update_every;
  458. int counter = update_every - 1;
  459. //This will be cancelled by its parent
  460. while (!ebpf_exit_plugin) {
  461. (void)heartbeat_next(&hb, USEC_PER_SEC);
  462. if (ebpf_exit_plugin || ++counter != update_every)
  463. continue;
  464. counter = 0;
  465. if (hardirq_reader())
  466. break;
  467. pthread_mutex_lock(&lock);
  468. // write dims now for all hitherto discovered IRQs.
  469. write_begin_chart(NETDATA_EBPF_SYSTEM_GROUP, "hardirq_latency");
  470. avl_traverse_lock(&hardirq_pub, hardirq_write_dims, NULL);
  471. hardirq_write_static_dims();
  472. write_end_chart();
  473. pthread_mutex_unlock(&lock);
  474. }
  475. }
  476. /*****************************************************************
  477. * EBPF HARDIRQ THREAD
  478. *****************************************************************/
  479. /**
  480. * Hard IRQ latency thread.
  481. *
  482. * @param ptr a `ebpf_module_t *`.
  483. * @return always NULL.
  484. */
  485. void *ebpf_hardirq_thread(void *ptr)
  486. {
  487. netdata_thread_cleanup_push(hardirq_exit, ptr);
  488. ebpf_module_t *em = (ebpf_module_t *)ptr;
  489. em->maps = hardirq_maps;
  490. if (ebpf_enable_tracepoints(hardirq_tracepoints) == 0) {
  491. goto endhardirq;
  492. }
  493. em->probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &em->objects);
  494. if (!em->probe_links) {
  495. goto endhardirq;
  496. }
  497. hardirq_collector(em);
  498. endhardirq:
  499. ebpf_update_disabled_plugin_stats(em);
  500. netdata_thread_cleanup_pop(1);
  501. return NULL;
  502. }