ebpf_hardirq.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. "Hardware IRQ latency",
  214. EBPF_COMMON_DIMENSION_MILLISECONDS,
  215. "interrupts",
  216. NETDATA_EBPF_CHART_TYPE_STACKED,
  217. NULL,
  218. NETDATA_CHART_PRIO_HARDIRQ_LATENCY,
  219. em->update_every
  220. );
  221. }
  222. /**
  223. * Hardirq Exit
  224. *
  225. * Cancel child and exit.
  226. *
  227. * @param ptr thread data.
  228. */
  229. static void hardirq_exit(void *ptr)
  230. {
  231. ebpf_module_t *em = (ebpf_module_t *)ptr;
  232. if (em->enabled == NETDATA_THREAD_EBPF_FUNCTION_RUNNING) {
  233. pthread_mutex_lock(&lock);
  234. ebpf_obsolete_hardirq_global(em);
  235. pthread_mutex_unlock(&lock);
  236. fflush(stdout);
  237. }
  238. ebpf_update_kernel_memory_with_vector(&plugin_statistics, em->maps, EBPF_ACTION_STAT_REMOVE);
  239. if (em->objects) {
  240. ebpf_unload_legacy_code(em->objects, em->probe_links);
  241. em->objects = NULL;
  242. em->probe_links = NULL;
  243. }
  244. for (int i = 0; hardirq_tracepoints[i].class != NULL; i++) {
  245. ebpf_disable_tracepoint(&hardirq_tracepoints[i]);
  246. }
  247. pthread_mutex_lock(&ebpf_exit_cleanup);
  248. em->enabled = NETDATA_THREAD_EBPF_STOPPED;
  249. ebpf_update_stats(&plugin_statistics, em);
  250. pthread_mutex_unlock(&ebpf_exit_cleanup);
  251. }
  252. /*****************************************************************
  253. * MAIN LOOP
  254. *****************************************************************/
  255. /**
  256. * Compare hard IRQ values.
  257. *
  258. * @param a `hardirq_val_t *`.
  259. * @param b `hardirq_val_t *`.
  260. *
  261. * @return 0 if a==b, 1 if a>b, -1 if a<b.
  262. */
  263. static int hardirq_val_cmp(void *a, void *b)
  264. {
  265. hardirq_val_t *ptr1 = a;
  266. hardirq_val_t *ptr2 = b;
  267. if (ptr1->irq > ptr2->irq) {
  268. return 1;
  269. }
  270. else if (ptr1->irq < ptr2->irq) {
  271. return -1;
  272. }
  273. else {
  274. return 0;
  275. }
  276. }
  277. /**
  278. * Parse interrupts
  279. *
  280. * Parse /proc/interrupts to get names used in metrics
  281. *
  282. * @param irq_name vector to store data.
  283. * @param irq irq value
  284. *
  285. * @return It returns 0 on success and -1 otherwise
  286. */
  287. static int hardirq_parse_interrupts(char *irq_name, int irq)
  288. {
  289. static procfile *ff = NULL;
  290. static int cpus = -1;
  291. if(unlikely(!ff)) {
  292. char filename[FILENAME_MAX + 1];
  293. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/interrupts");
  294. ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
  295. }
  296. if(unlikely(!ff))
  297. return -1;
  298. ff = procfile_readall(ff);
  299. if(unlikely(!ff))
  300. return -1; // we return 0, so that we will retry to open it next time
  301. size_t words = procfile_linewords(ff, 0);
  302. if(unlikely(cpus == -1)) {
  303. uint32_t w;
  304. cpus = 0;
  305. for(w = 0; w < words ; w++) {
  306. if(likely(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0))
  307. cpus++;
  308. }
  309. }
  310. size_t lines = procfile_lines(ff), l;
  311. if(unlikely(!lines)) {
  312. collector_error("Cannot read /proc/interrupts, zero lines reported.");
  313. return -1;
  314. }
  315. for(l = 1; l < lines ;l++) {
  316. words = procfile_linewords(ff, l);
  317. if(unlikely(!words)) continue;
  318. const char *id = procfile_lineword(ff, l, 0);
  319. if (!isdigit(id[0]))
  320. continue;
  321. int cmp = str2i(id);
  322. if (cmp != irq)
  323. continue;
  324. if(unlikely((uint32_t)(cpus + 2) < words)) {
  325. const char *name = procfile_lineword(ff, l, words - 1);
  326. // On some motherboards IRQ can have the same name, so we append IRQ id to differentiate.
  327. snprintfz(irq_name, NETDATA_HARDIRQ_NAME_LEN - 1, "%d_%s", irq, name);
  328. }
  329. }
  330. return 0;
  331. }
  332. /**
  333. * Read Latency MAP
  334. *
  335. * Read data from kernel ring to user ring.
  336. *
  337. * @param mapfd hash map id.
  338. *
  339. * @return it returns 0 on success and -1 otherwise
  340. */
  341. static int hardirq_read_latency_map(int mapfd)
  342. {
  343. static hardirq_ebpf_static_val_t *hardirq_ebpf_vals = NULL;
  344. if (!hardirq_ebpf_vals)
  345. hardirq_ebpf_vals = callocz(ebpf_nprocs + 1, sizeof(hardirq_ebpf_static_val_t));
  346. hardirq_ebpf_key_t key = {};
  347. hardirq_ebpf_key_t next_key = {};
  348. hardirq_val_t search_v = {};
  349. hardirq_val_t *v = NULL;
  350. while (bpf_map_get_next_key(mapfd, &key, &next_key) == 0) {
  351. // get val for this key.
  352. int test = bpf_map_lookup_elem(mapfd, &key, hardirq_ebpf_vals);
  353. if (unlikely(test < 0)) {
  354. key = next_key;
  355. continue;
  356. }
  357. // is this IRQ saved yet?
  358. //
  359. // if not, make a new one, mark it as unsaved for now, and continue; we
  360. // will insert it at the end after all of its values are correctly set,
  361. // so that we can safely publish it to the collector within a single,
  362. // short locked operation.
  363. //
  364. // otherwise simply continue; we will only update the latency, which
  365. // can be republished safely without a lock.
  366. //
  367. // NOTE: lock isn't strictly necessary for this initial search, as only
  368. // this thread does writing, but the AVL is using a read-write lock so
  369. // there is no congestion.
  370. bool v_is_new = false;
  371. search_v.irq = key.irq;
  372. v = (hardirq_val_t *)avl_search_lock(&hardirq_pub, (avl_t *)&search_v);
  373. if (unlikely(v == NULL)) {
  374. // latency/name can only be added reliably at a later time.
  375. // when they're added, only then will we AVL insert.
  376. v = ebpf_hardirq_get();
  377. v->irq = key.irq;
  378. v->dim_exists = false;
  379. v_is_new = true;
  380. }
  381. // note two things:
  382. // 1. we must add up latency value for this IRQ across all CPUs.
  383. // 2. the name is unfortunately *not* available on all CPU maps - only
  384. // a single map contains the name, so we must find it. we only need
  385. // to copy it though if the IRQ is new for us.
  386. uint64_t total_latency = 0;
  387. int i;
  388. for (i = 0; i < ebpf_nprocs; i++) {
  389. total_latency += hardirq_ebpf_vals[i].latency/1000;
  390. }
  391. // can now safely publish latency for existing IRQs.
  392. v->latency = total_latency;
  393. // can now safely publish new IRQ.
  394. if (v_is_new) {
  395. if (hardirq_parse_interrupts(v->name, v->irq)) {
  396. ebpf_hardirq_release(v);
  397. return -1;
  398. }
  399. avl_t *check = avl_insert_lock(&hardirq_pub, (avl_t *)v);
  400. if (check != (avl_t *)v) {
  401. netdata_log_error("Internal error, cannot insert the AVL tree.");
  402. }
  403. }
  404. key = next_key;
  405. }
  406. return 0;
  407. }
  408. static void hardirq_read_latency_static_map(int mapfd)
  409. {
  410. static hardirq_ebpf_static_val_t *hardirq_ebpf_static_vals = NULL;
  411. if (!hardirq_ebpf_static_vals)
  412. hardirq_ebpf_static_vals = callocz(ebpf_nprocs + 1, sizeof(hardirq_ebpf_static_val_t));
  413. uint32_t i;
  414. for (i = 0; i < HARDIRQ_EBPF_STATIC_END; i++) {
  415. uint32_t map_i = hardirq_static_vals[i].idx;
  416. int test = bpf_map_lookup_elem(mapfd, &map_i, hardirq_ebpf_static_vals);
  417. if (unlikely(test < 0)) {
  418. continue;
  419. }
  420. uint64_t total_latency = 0;
  421. int cpu_i;
  422. int end = (running_on_kernel < NETDATA_KERNEL_V4_15) ? 1 : ebpf_nprocs;
  423. for (cpu_i = 0; cpu_i < end; cpu_i++) {
  424. total_latency += hardirq_ebpf_static_vals[cpu_i].latency/1000;
  425. }
  426. hardirq_static_vals[i].latency = total_latency;
  427. }
  428. }
  429. /**
  430. * Read eBPF maps for hard IRQ.
  431. *
  432. * @return When it is not possible to parse /proc, it returns -1, on success it returns 0;
  433. */
  434. static int hardirq_reader()
  435. {
  436. if (hardirq_read_latency_map(hardirq_maps[HARDIRQ_MAP_LATENCY].map_fd))
  437. return -1;
  438. hardirq_read_latency_static_map(hardirq_maps[HARDIRQ_MAP_LATENCY_STATIC].map_fd);
  439. return 0;
  440. }
  441. static void hardirq_create_charts(int update_every)
  442. {
  443. ebpf_create_chart(
  444. NETDATA_EBPF_SYSTEM_GROUP,
  445. "hardirq_latency",
  446. "Hardware IRQ latency",
  447. EBPF_COMMON_DIMENSION_MILLISECONDS,
  448. "interrupts",
  449. NULL,
  450. NETDATA_EBPF_CHART_TYPE_STACKED,
  451. NETDATA_CHART_PRIO_HARDIRQ_LATENCY,
  452. NULL, NULL, 0, update_every,
  453. NETDATA_EBPF_MODULE_NAME_HARDIRQ
  454. );
  455. fflush(stdout);
  456. }
  457. static void hardirq_create_static_dims()
  458. {
  459. uint32_t i;
  460. for (i = 0; i < HARDIRQ_EBPF_STATIC_END; i++) {
  461. ebpf_write_global_dimension(
  462. hardirq_static_vals[i].name, hardirq_static_vals[i].name,
  463. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX]
  464. );
  465. }
  466. }
  467. // callback for avl tree traversal on `hardirq_pub`.
  468. static int hardirq_write_dims(void *entry, void *data)
  469. {
  470. UNUSED(data);
  471. hardirq_val_t *v = entry;
  472. // IRQs get dynamically added in, so add the dimension if we haven't yet.
  473. if (!v->dim_exists) {
  474. ebpf_write_global_dimension(
  475. v->name, v->name,
  476. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX]
  477. );
  478. v->dim_exists = true;
  479. }
  480. write_chart_dimension(v->name, v->latency);
  481. return 1;
  482. }
  483. static inline void hardirq_write_static_dims()
  484. {
  485. uint32_t i;
  486. for (i = 0; i < HARDIRQ_EBPF_STATIC_END; i++) {
  487. write_chart_dimension(
  488. hardirq_static_vals[i].name,
  489. hardirq_static_vals[i].latency
  490. );
  491. }
  492. }
  493. /**
  494. * Main loop for this collector.
  495. *
  496. * @param em the main thread structure.
  497. */
  498. static void hardirq_collector(ebpf_module_t *em)
  499. {
  500. memset(&hardirq_pub, 0, sizeof(hardirq_pub));
  501. avl_init_lock(&hardirq_pub, hardirq_val_cmp);
  502. ebpf_hardirq_aral_init();
  503. // create chart and static dims.
  504. pthread_mutex_lock(&lock);
  505. hardirq_create_charts(em->update_every);
  506. hardirq_create_static_dims();
  507. ebpf_update_stats(&plugin_statistics, em);
  508. ebpf_update_kernel_memory_with_vector(&plugin_statistics, em->maps, EBPF_ACTION_STAT_ADD);
  509. pthread_mutex_unlock(&lock);
  510. // loop and read from published data until ebpf plugin is closed.
  511. heartbeat_t hb;
  512. heartbeat_init(&hb);
  513. int update_every = em->update_every;
  514. int counter = update_every - 1;
  515. //This will be cancelled by its parent
  516. uint32_t running_time = 0;
  517. uint32_t lifetime = em->lifetime;
  518. while (!ebpf_exit_plugin && running_time < lifetime) {
  519. (void)heartbeat_next(&hb, USEC_PER_SEC);
  520. if (ebpf_exit_plugin || ++counter != update_every)
  521. continue;
  522. counter = 0;
  523. if (hardirq_reader())
  524. break;
  525. pthread_mutex_lock(&lock);
  526. // write dims now for all hitherto discovered IRQs.
  527. write_begin_chart(NETDATA_EBPF_SYSTEM_GROUP, "hardirq_latency");
  528. avl_traverse_lock(&hardirq_pub, hardirq_write_dims, NULL);
  529. hardirq_write_static_dims();
  530. write_end_chart();
  531. pthread_mutex_unlock(&lock);
  532. pthread_mutex_lock(&ebpf_exit_cleanup);
  533. if (running_time && !em->running_time)
  534. running_time = update_every;
  535. else
  536. running_time += update_every;
  537. em->running_time = running_time;
  538. pthread_mutex_unlock(&ebpf_exit_cleanup);
  539. }
  540. }
  541. /*****************************************************************
  542. * EBPF HARDIRQ THREAD
  543. *****************************************************************/
  544. /*
  545. * Load BPF
  546. *
  547. * Load BPF files.
  548. *
  549. * @param em the structure with configuration
  550. *
  551. * @return It returns 0 on success and -1 otherwise.
  552. */
  553. static int ebpf_hardirq_load_bpf(ebpf_module_t *em)
  554. {
  555. int ret = 0;
  556. if (em->load & EBPF_LOAD_LEGACY) {
  557. em->probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &em->objects);
  558. if (!em->probe_links) {
  559. ret = -1;
  560. }
  561. }
  562. #ifdef LIBBPF_MAJOR_VERSION
  563. else {
  564. hardirq_bpf_obj = hardirq_bpf__open();
  565. if (!hardirq_bpf_obj)
  566. ret = -1;
  567. else {
  568. ret = ebpf_hardirq_load_and_attach(hardirq_bpf_obj);
  569. if (!ret)
  570. ebpf_hardirq_set_hash_table(hardirq_bpf_obj);
  571. }
  572. }
  573. #endif
  574. return ret;
  575. }
  576. /**
  577. * Hard IRQ latency thread.
  578. *
  579. * @param ptr a `ebpf_module_t *`.
  580. * @return always NULL.
  581. */
  582. void *ebpf_hardirq_thread(void *ptr)
  583. {
  584. netdata_thread_cleanup_push(hardirq_exit, ptr);
  585. ebpf_module_t *em = (ebpf_module_t *)ptr;
  586. em->maps = hardirq_maps;
  587. if (ebpf_enable_tracepoints(hardirq_tracepoints) == 0) {
  588. goto endhardirq;
  589. }
  590. #ifdef LIBBPF_MAJOR_VERSION
  591. ebpf_define_map_type(em->maps, em->maps_per_core, running_on_kernel);
  592. ebpf_adjust_thread_load(em, default_btf);
  593. #endif
  594. if (ebpf_hardirq_load_bpf(em)) {
  595. goto endhardirq;
  596. }
  597. hardirq_collector(em);
  598. endhardirq:
  599. ebpf_update_disabled_plugin_stats(em);
  600. netdata_thread_cleanup_pop(1);
  601. return NULL;
  602. }