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