ebpf_cachestat.c 21 KB

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