ebpf_cachestat.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "ebpf.h"
  3. #include "ebpf_cachestat.h"
  4. static ebpf_data_t cachestat_data;
  5. netdata_publish_cachestat_t **cachestat_pid;
  6. static struct bpf_link **probe_links = NULL;
  7. static struct bpf_object *objects = NULL;
  8. static char *cachestat_counter_dimension_name[NETDATA_CACHESTAT_END] = { "ratio", "dirty", "hit",
  9. "miss" };
  10. static netdata_syscall_stat_t cachestat_counter_aggregated_data[NETDATA_CACHESTAT_END];
  11. static netdata_publish_syscall_t cachestat_counter_publish_aggregated[NETDATA_CACHESTAT_END];
  12. netdata_cachestat_pid_t *cachestat_vector = NULL;
  13. static netdata_idx_t *cachestat_hash_values = NULL;
  14. static int read_thread_closed = 1;
  15. struct netdata_static_thread cachestat_threads = {"CACHESTAT KERNEL",
  16. NULL, NULL, 1, NULL,
  17. NULL, NULL};
  18. static ebpf_local_maps_t cachestat_maps[] = {{.name = "cstat_pid", .internal_input = ND_EBPF_DEFAULT_PID_SIZE,
  19. .user_input = 0},
  20. {.name = NULL, .internal_input = 0, .user_input = 0}};
  21. static int *map_fd = NULL;
  22. struct config cachestat_config = { .first_section = NULL,
  23. .last_section = NULL,
  24. .mutex = NETDATA_MUTEX_INITIALIZER,
  25. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  26. .rwlock = AVL_LOCK_INITIALIZER } };
  27. /*****************************************************************
  28. *
  29. * FUNCTIONS TO CLOSE THE THREAD
  30. *
  31. *****************************************************************/
  32. /**
  33. * Clean PID structures
  34. *
  35. * Clean the allocated structures.
  36. */
  37. void clean_cachestat_pid_structures() {
  38. struct pid_stat *pids = root_of_pids;
  39. while (pids) {
  40. freez(cachestat_pid[pids->pid]);
  41. pids = pids->next;
  42. }
  43. }
  44. /**
  45. * Clean up the main thread.
  46. *
  47. * @param ptr thread data.
  48. */
  49. static void ebpf_cachestat_cleanup(void *ptr)
  50. {
  51. ebpf_module_t *em = (ebpf_module_t *)ptr;
  52. if (!em->enabled)
  53. return;
  54. heartbeat_t hb;
  55. heartbeat_init(&hb);
  56. uint32_t tick = 2*USEC_PER_MS;
  57. while (!read_thread_closed) {
  58. usec_t dt = heartbeat_next(&hb, tick);
  59. UNUSED(dt);
  60. }
  61. ebpf_cleanup_publish_syscall(cachestat_counter_publish_aggregated);
  62. freez(cachestat_vector);
  63. freez(cachestat_hash_values);
  64. if (probe_links) {
  65. struct bpf_program *prog;
  66. size_t i = 0 ;
  67. bpf_object__for_each_program(prog, objects) {
  68. bpf_link__destroy(probe_links[i]);
  69. i++;
  70. }
  71. bpf_object__close(objects);
  72. }
  73. }
  74. /*****************************************************************
  75. *
  76. * COMMON FUNCTIONS
  77. *
  78. *****************************************************************/
  79. /**
  80. * Update publish
  81. *
  82. * Update publish values before to write dimension.
  83. *
  84. * @param out strcuture that will receive data.
  85. * @param mpa calls for mark_page_accessed during the last second.
  86. * @param mbd calls for mark_buffer_dirty during the last second.
  87. * @param apcl calls for add_to_page_cache_lru during the last second.
  88. * @param apd calls for account_page_dirtied during the last second.
  89. */
  90. void cachestat_update_publish(netdata_publish_cachestat_t *out, uint64_t mpa, uint64_t mbd,
  91. uint64_t apcl, uint64_t apd)
  92. {
  93. // Adapted algorithm from https://github.com/iovisor/bcc/blob/master/tools/cachestat.py#L126-L138
  94. calculated_number total = (calculated_number) (((long long)mpa) - ((long long)mbd));
  95. if (total < 0)
  96. total = 0;
  97. calculated_number misses = (calculated_number) ( ((long long) apcl) - ((long long) apd) );
  98. if (misses < 0)
  99. misses = 0;
  100. // If hits are < 0, then its possible misses are overestimate due to possibly page cache read ahead adding
  101. // more pages than needed. In this case just assume misses as total and reset hits.
  102. calculated_number hits = total - misses;
  103. if (hits < 0 ) {
  104. misses = total;
  105. hits = 0;
  106. }
  107. calculated_number ratio = (total > 0) ? hits/total : 1;
  108. out->ratio = (long long )(ratio*100);
  109. out->hit = (long long)hits;
  110. out->miss = (long long)misses;
  111. }
  112. /**
  113. * Save previous values
  114. *
  115. * Save values used this time.
  116. *
  117. * @param publish
  118. */
  119. static void save_previous_values(netdata_publish_cachestat_t *publish) {
  120. publish->prev.mark_page_accessed = cachestat_hash_values[NETDATA_KEY_CALLS_MARK_PAGE_ACCESSED];
  121. publish->prev.account_page_dirtied = cachestat_hash_values[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED];
  122. publish->prev.add_to_page_cache_lru = cachestat_hash_values[NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU];
  123. publish->prev.mark_buffer_dirty = cachestat_hash_values[NETDATA_KEY_CALLS_MARK_BUFFER_DIRTY];
  124. }
  125. /**
  126. * Calculate statistics
  127. *
  128. * @param publish the structure where we will store the data.
  129. */
  130. static void calculate_stats(netdata_publish_cachestat_t *publish) {
  131. if (!publish->prev.mark_page_accessed) {
  132. save_previous_values(publish);
  133. return;
  134. }
  135. uint64_t mpa = cachestat_hash_values[NETDATA_KEY_CALLS_MARK_PAGE_ACCESSED] - publish->prev.mark_page_accessed;
  136. uint64_t mbd = cachestat_hash_values[NETDATA_KEY_CALLS_MARK_BUFFER_DIRTY] - publish->prev.mark_buffer_dirty;
  137. uint64_t apcl = cachestat_hash_values[NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU] - publish->prev.add_to_page_cache_lru;
  138. uint64_t apd = cachestat_hash_values[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED] - publish->prev.account_page_dirtied;
  139. save_previous_values(publish);
  140. // We are changing the original algorithm to have a smooth ratio.
  141. cachestat_update_publish(publish, mpa, mbd, apcl, apd);
  142. }
  143. /*****************************************************************
  144. *
  145. * APPS
  146. *
  147. *****************************************************************/
  148. /**
  149. * Apps Accumulator
  150. *
  151. * Sum all values read from kernel and store in the first address.
  152. *
  153. * @param out the vector with read values.
  154. */
  155. static void cachestat_apps_accumulator(netdata_cachestat_pid_t *out)
  156. {
  157. int i, end = (running_on_kernel >= NETDATA_KERNEL_V4_15) ? ebpf_nprocs : 1;
  158. netdata_cachestat_pid_t *total = &out[0];
  159. for (i = 1; i < end; i++) {
  160. netdata_cachestat_pid_t *w = &out[i];
  161. total->account_page_dirtied += w->account_page_dirtied;
  162. total->add_to_page_cache_lru += w->add_to_page_cache_lru;
  163. total->mark_buffer_dirty += w->mark_buffer_dirty;
  164. total->mark_page_accessed += w->mark_page_accessed;
  165. }
  166. }
  167. /**
  168. * Save Pid values
  169. *
  170. * Save the current values inside the structure
  171. *
  172. * @param out vector used to plot charts
  173. * @param publish vector with values read from hash tables.
  174. */
  175. static inline void cachestat_save_pid_values(netdata_publish_cachestat_t *out, netdata_cachestat_pid_t *publish)
  176. {
  177. if (!out->current.mark_page_accessed) {
  178. memcpy(&out->current, &publish[0], sizeof(netdata_cachestat_pid_t));
  179. return;
  180. }
  181. memcpy(&out->prev, &out->current, sizeof(netdata_cachestat_pid_t));
  182. memcpy(&out->current, &publish[0], sizeof(netdata_cachestat_pid_t));
  183. }
  184. /**
  185. * Fill PID
  186. *
  187. * Fill PID structures
  188. *
  189. * @param current_pid pid that we are collecting data
  190. * @param out values read from hash tables;
  191. */
  192. static void cachestat_fill_pid(uint32_t current_pid, netdata_cachestat_pid_t *publish)
  193. {
  194. netdata_publish_cachestat_t *curr = cachestat_pid[current_pid];
  195. if (!curr) {
  196. curr = callocz(1, sizeof(netdata_publish_cachestat_t));
  197. cachestat_pid[current_pid] = curr;
  198. cachestat_save_pid_values(curr, publish);
  199. return;
  200. }
  201. cachestat_save_pid_values(curr, publish);
  202. }
  203. /**
  204. * Read APPS table
  205. *
  206. * Read the apps table and store data inside the structure.
  207. */
  208. static void read_apps_table()
  209. {
  210. netdata_cachestat_pid_t *cv = cachestat_vector;
  211. uint32_t key;
  212. struct pid_stat *pids = root_of_pids;
  213. int fd = map_fd[NETDATA_CACHESTAT_PID_STATS];
  214. size_t length = sizeof(netdata_cachestat_pid_t)*ebpf_nprocs;
  215. while (pids) {
  216. key = pids->pid;
  217. if (bpf_map_lookup_elem(fd, &key, cv)) {
  218. pids = pids->next;
  219. continue;
  220. }
  221. cachestat_apps_accumulator(cv);
  222. cachestat_fill_pid(key, cv);
  223. // We are cleaning to avoid passing data read from one process to other.
  224. memset(cv, 0, length);
  225. pids = pids->next;
  226. }
  227. }
  228. /**
  229. * Create apps charts
  230. *
  231. * Call ebpf_create_chart to create the charts on apps submenu.
  232. *
  233. * @param em a pointer to the structure with the default values.
  234. */
  235. void ebpf_cachestat_create_apps_charts(struct ebpf_module *em, void *ptr)
  236. {
  237. UNUSED(em);
  238. struct target *root = ptr;
  239. ebpf_create_charts_on_apps(NETDATA_CACHESTAT_HIT_RATIO_CHART,
  240. "The ratio is calculated dividing the Hit pages per total cache accesses without counting dirties.",
  241. EBPF_COMMON_DIMENSION_PERCENTAGE,
  242. NETDATA_APPS_CACHESTAT_GROUP,
  243. NETDATA_EBPF_CHART_TYPE_LINE,
  244. 20090,
  245. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  246. root);
  247. ebpf_create_charts_on_apps(NETDATA_CACHESTAT_DIRTY_CHART,
  248. "Number of pages marked as dirty. When a page is called dirty, this means that the data stored inside the page needs to be written to devices.",
  249. EBPF_CACHESTAT_DIMENSION_PAGE,
  250. NETDATA_APPS_CACHESTAT_GROUP,
  251. NETDATA_EBPF_CHART_TYPE_STACKED,
  252. 20091,
  253. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  254. root);
  255. ebpf_create_charts_on_apps(NETDATA_CACHESTAT_HIT_CHART,
  256. "Number of cache access without counting dirty pages and page additions.",
  257. EBPF_CACHESTAT_DIMENSION_HITS,
  258. NETDATA_APPS_CACHESTAT_GROUP,
  259. NETDATA_EBPF_CHART_TYPE_STACKED,
  260. 20092,
  261. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  262. root);
  263. ebpf_create_charts_on_apps(NETDATA_CACHESTAT_MISSES_CHART,
  264. "Page caches added without counting dirty pages",
  265. EBPF_CACHESTAT_DIMENSION_MISSES,
  266. NETDATA_APPS_CACHESTAT_GROUP,
  267. NETDATA_EBPF_CHART_TYPE_STACKED,
  268. 20093,
  269. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  270. root);
  271. }
  272. /*****************************************************************
  273. *
  274. * MAIN LOOP
  275. *
  276. *****************************************************************/
  277. /**
  278. * Read global counter
  279. *
  280. * Read the table with number of calls for all functions
  281. */
  282. static void read_global_table()
  283. {
  284. uint32_t idx;
  285. netdata_idx_t *val = cachestat_hash_values;
  286. netdata_idx_t stored;
  287. int fd = map_fd[NETDATA_CACHESTAT_GLOBAL_STATS];
  288. for (idx = NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU; idx < NETDATA_CACHESTAT_END; idx++) {
  289. if (!bpf_map_lookup_elem(fd, &idx, &stored)) {
  290. val[idx] = stored;
  291. }
  292. }
  293. }
  294. /**
  295. * Socket read hash
  296. *
  297. * This is the thread callback.
  298. * This thread is necessary, because we cannot freeze the whole plugin to read the data on very busy socket.
  299. *
  300. * @param ptr It is a NULL value for this thread.
  301. *
  302. * @return It always returns NULL.
  303. */
  304. void *ebpf_cachestat_read_hash(void *ptr)
  305. {
  306. read_thread_closed = 0;
  307. heartbeat_t hb;
  308. heartbeat_init(&hb);
  309. ebpf_module_t *em = (ebpf_module_t *)ptr;
  310. usec_t step = NETDATA_LATENCY_CACHESTAT_SLEEP_MS * em->update_time;
  311. while (!close_ebpf_plugin) {
  312. usec_t dt = heartbeat_next(&hb, step);
  313. (void)dt;
  314. read_global_table();
  315. }
  316. read_thread_closed = 1;
  317. return NULL;
  318. }
  319. /**
  320. * Send global
  321. *
  322. * Send global charts to Netdata
  323. */
  324. static void cachestat_send_global(netdata_publish_cachestat_t *publish)
  325. {
  326. calculate_stats(publish);
  327. netdata_publish_syscall_t *ptr = cachestat_counter_publish_aggregated;
  328. ebpf_one_dimension_write_charts(
  329. NETDATA_EBPF_MEMORY_GROUP, NETDATA_CACHESTAT_HIT_RATIO_CHART, ptr[NETDATA_CACHESTAT_IDX_RATIO].dimension,
  330. publish->ratio);
  331. ebpf_one_dimension_write_charts(
  332. NETDATA_EBPF_MEMORY_GROUP, NETDATA_CACHESTAT_DIRTY_CHART, ptr[NETDATA_CACHESTAT_IDX_DIRTY].dimension,
  333. cachestat_hash_values[NETDATA_KEY_CALLS_MARK_BUFFER_DIRTY]);
  334. ebpf_one_dimension_write_charts(
  335. NETDATA_EBPF_MEMORY_GROUP, NETDATA_CACHESTAT_HIT_CHART, ptr[NETDATA_CACHESTAT_IDX_HIT].dimension, publish->hit);
  336. ebpf_one_dimension_write_charts(
  337. NETDATA_EBPF_MEMORY_GROUP, NETDATA_CACHESTAT_MISSES_CHART, ptr[NETDATA_CACHESTAT_IDX_MISS].dimension,
  338. publish->miss);
  339. }
  340. /**
  341. * Cachestat sum PIDs
  342. *
  343. * Sum values for all PIDs associated to a group
  344. *
  345. * @param publish output structure.
  346. * @param root structure with listed IPs
  347. */
  348. void ebpf_cachestat_sum_pids(netdata_publish_cachestat_t *publish, struct pid_on_target *root)
  349. {
  350. memcpy(&publish->prev, &publish->current,sizeof(publish->current));
  351. memset(&publish->current, 0, sizeof(publish->current));
  352. netdata_cachestat_pid_t *dst = &publish->current;
  353. while (root) {
  354. int32_t pid = root->pid;
  355. netdata_publish_cachestat_t *w = cachestat_pid[pid];
  356. if (w) {
  357. netdata_cachestat_pid_t *src = &w->current;
  358. dst->account_page_dirtied += src->account_page_dirtied;
  359. dst->add_to_page_cache_lru += src->add_to_page_cache_lru;
  360. dst->mark_buffer_dirty += src->mark_buffer_dirty;
  361. dst->mark_page_accessed += src->mark_page_accessed;
  362. }
  363. root = root->next;
  364. }
  365. }
  366. /**
  367. * Send data to Netdata calling auxiliar functions.
  368. *
  369. * @param root the target list.
  370. */
  371. void ebpf_cache_send_apps_data(struct target *root)
  372. {
  373. struct target *w;
  374. collected_number value;
  375. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_CACHESTAT_HIT_RATIO_CHART);
  376. for (w = root; w; w = w->next) {
  377. if (unlikely(w->exposed && w->processes)) {
  378. ebpf_cachestat_sum_pids(&w->cachestat, w->root_pid);
  379. netdata_cachestat_pid_t *current = &w->cachestat.current;
  380. netdata_cachestat_pid_t *prev = &w->cachestat.prev;
  381. uint64_t mpa = current->mark_page_accessed - prev->mark_page_accessed;
  382. uint64_t mbd = current->mark_buffer_dirty - prev->mark_buffer_dirty;
  383. w->cachestat.dirty = current->mark_buffer_dirty;
  384. uint64_t apcl = current->add_to_page_cache_lru - prev->add_to_page_cache_lru;
  385. uint64_t apd = current->account_page_dirtied - prev->account_page_dirtied;
  386. cachestat_update_publish(&w->cachestat, mpa, mbd, apcl, apd);
  387. value = (collected_number) w->cachestat.ratio;
  388. // Here we are using different approach to have a chart more smooth
  389. write_chart_dimension(w->name, value);
  390. }
  391. }
  392. write_end_chart();
  393. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_CACHESTAT_DIRTY_CHART);
  394. for (w = root; w; w = w->next) {
  395. if (unlikely(w->exposed && w->processes)) {
  396. value = (collected_number) w->cachestat.dirty;
  397. write_chart_dimension(w->name, value);
  398. }
  399. }
  400. write_end_chart();
  401. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_CACHESTAT_HIT_CHART);
  402. for (w = root; w; w = w->next) {
  403. if (unlikely(w->exposed && w->processes)) {
  404. value = (collected_number) w->cachestat.hit;
  405. write_chart_dimension(w->name, value);
  406. }
  407. }
  408. write_end_chart();
  409. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_CACHESTAT_MISSES_CHART);
  410. for (w = root; w; w = w->next) {
  411. if (unlikely(w->exposed && w->processes)) {
  412. value = (collected_number) w->cachestat.miss;
  413. write_chart_dimension(w->name, value);
  414. }
  415. }
  416. write_end_chart();
  417. }
  418. /**
  419. * Main loop for this collector.
  420. */
  421. static void cachestat_collector(ebpf_module_t *em)
  422. {
  423. cachestat_threads.thread = mallocz(sizeof(netdata_thread_t));
  424. cachestat_threads.start_routine = ebpf_cachestat_read_hash;
  425. map_fd = cachestat_data.map_fd;
  426. netdata_thread_create(cachestat_threads.thread, cachestat_threads.name, NETDATA_THREAD_OPTION_JOINABLE,
  427. ebpf_cachestat_read_hash, em);
  428. netdata_publish_cachestat_t publish;
  429. memset(&publish, 0, sizeof(publish));
  430. int apps = em->apps_charts;
  431. while (!close_ebpf_plugin) {
  432. pthread_mutex_lock(&collect_data_mutex);
  433. pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
  434. if (apps)
  435. read_apps_table();
  436. pthread_mutex_lock(&lock);
  437. cachestat_send_global(&publish);
  438. if (apps)
  439. ebpf_cache_send_apps_data(apps_groups_root_target);
  440. pthread_mutex_unlock(&lock);
  441. pthread_mutex_unlock(&collect_data_mutex);
  442. }
  443. }
  444. /*****************************************************************
  445. *
  446. * INITIALIZE THREAD
  447. *
  448. *****************************************************************/
  449. /**
  450. * Create global charts
  451. *
  452. * Call ebpf_create_chart to create the charts for the collector.
  453. */
  454. static void ebpf_create_memory_charts()
  455. {
  456. ebpf_create_chart(NETDATA_EBPF_MEMORY_GROUP, NETDATA_CACHESTAT_HIT_RATIO_CHART,
  457. "Hit is calculating using total cache added without dirties per total added because of red misses.",
  458. EBPF_COMMON_DIMENSION_PERCENTAGE, NETDATA_CACHESTAT_SUBMENU,
  459. NULL,
  460. NETDATA_EBPF_CHART_TYPE_LINE,
  461. 21100,
  462. ebpf_create_global_dimension,
  463. cachestat_counter_publish_aggregated, 1);
  464. ebpf_create_chart(NETDATA_EBPF_MEMORY_GROUP, NETDATA_CACHESTAT_DIRTY_CHART,
  465. "Number of dirty pages added to the page cache.",
  466. EBPF_CACHESTAT_DIMENSION_PAGE, NETDATA_CACHESTAT_SUBMENU,
  467. NULL,
  468. NETDATA_EBPF_CHART_TYPE_LINE,
  469. 21101,
  470. ebpf_create_global_dimension,
  471. &cachestat_counter_publish_aggregated[NETDATA_CACHESTAT_IDX_DIRTY], 1);
  472. ebpf_create_chart(NETDATA_EBPF_MEMORY_GROUP, NETDATA_CACHESTAT_HIT_CHART,
  473. "Hits are function calls that Netdata counts.",
  474. EBPF_CACHESTAT_DIMENSION_HITS, NETDATA_CACHESTAT_SUBMENU,
  475. NULL,
  476. NETDATA_EBPF_CHART_TYPE_LINE,
  477. 21102,
  478. ebpf_create_global_dimension,
  479. &cachestat_counter_publish_aggregated[NETDATA_CACHESTAT_IDX_HIT], 1);
  480. ebpf_create_chart(NETDATA_EBPF_MEMORY_GROUP, NETDATA_CACHESTAT_MISSES_CHART,
  481. "Misses are function calls that Netdata counts.",
  482. EBPF_CACHESTAT_DIMENSION_MISSES, NETDATA_CACHESTAT_SUBMENU,
  483. NULL,
  484. NETDATA_EBPF_CHART_TYPE_LINE,
  485. 21103,
  486. ebpf_create_global_dimension,
  487. &cachestat_counter_publish_aggregated[NETDATA_CACHESTAT_IDX_MISS], 1);
  488. fflush(stdout);
  489. }
  490. /**
  491. * Allocate vectors used with this thread.
  492. *
  493. * We are not testing the return, because callocz does this and shutdown the software
  494. * case it was not possible to allocate.
  495. *
  496. * @param length is the length for the vectors used inside the collector.
  497. */
  498. static void ebpf_cachestat_allocate_global_vectors(size_t length)
  499. {
  500. cachestat_pid = callocz((size_t)pid_max, sizeof(netdata_publish_cachestat_t *));
  501. cachestat_vector = callocz((size_t)ebpf_nprocs, sizeof(netdata_cachestat_pid_t));
  502. cachestat_hash_values = callocz(length, sizeof(netdata_idx_t));
  503. memset(cachestat_counter_aggregated_data, 0, length * sizeof(netdata_syscall_stat_t));
  504. memset(cachestat_counter_publish_aggregated, 0, length * sizeof(netdata_publish_syscall_t));
  505. }
  506. /*****************************************************************
  507. *
  508. * MAIN THREAD
  509. *
  510. *****************************************************************/
  511. /**
  512. * Cachestat thread
  513. *
  514. * Thread used to make cachestat thread
  515. *
  516. * @param ptr a pointer to `struct ebpf_module`
  517. *
  518. * @return It always return NULL
  519. */
  520. void *ebpf_cachestat_thread(void *ptr)
  521. {
  522. netdata_thread_cleanup_push(ebpf_cachestat_cleanup, ptr);
  523. ebpf_module_t *em = (ebpf_module_t *)ptr;
  524. em->maps = cachestat_maps;
  525. fill_ebpf_data(&cachestat_data);
  526. ebpf_update_pid_table(&cachestat_maps[0], em);
  527. if (!em->enabled)
  528. goto endcachestat;
  529. pthread_mutex_lock(&lock);
  530. ebpf_cachestat_allocate_global_vectors(NETDATA_CACHESTAT_END);
  531. if (ebpf_update_kernel(&cachestat_data)) {
  532. pthread_mutex_unlock(&lock);
  533. goto endcachestat;
  534. }
  535. probe_links = ebpf_load_program(ebpf_plugin_dir, em, kernel_string, &objects, cachestat_data.map_fd);
  536. if (!probe_links) {
  537. pthread_mutex_unlock(&lock);
  538. goto endcachestat;
  539. }
  540. int algorithms[NETDATA_CACHESTAT_END] = {
  541. NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_INCREMENTAL_IDX, NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_ABSOLUTE_IDX
  542. };
  543. ebpf_global_labels(cachestat_counter_aggregated_data, cachestat_counter_publish_aggregated,
  544. cachestat_counter_dimension_name, cachestat_counter_dimension_name,
  545. algorithms, NETDATA_CACHESTAT_END);
  546. ebpf_create_memory_charts();
  547. pthread_mutex_unlock(&lock);
  548. cachestat_collector(em);
  549. endcachestat:
  550. netdata_thread_cleanup_pop(1);
  551. return NULL;
  552. }