ebpf_hardirq.c 17 KB

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