ebpf_hardirq.c 20 KB

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