ebpf_cachestat.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "ebpf.h"
  3. #include "ebpf_cachestat.h"
  4. static char *cachestat_counter_dimension_name[NETDATA_CACHESTAT_END] = { "ratio", "dirty", "hit",
  5. "miss" };
  6. static netdata_syscall_stat_t cachestat_counter_aggregated_data[NETDATA_CACHESTAT_END];
  7. static netdata_publish_syscall_t cachestat_counter_publish_aggregated[NETDATA_CACHESTAT_END];
  8. netdata_cachestat_pid_t *cachestat_vector = NULL;
  9. static netdata_idx_t cachestat_hash_values[NETDATA_CACHESTAT_END];
  10. static netdata_idx_t *cachestat_values = NULL;
  11. ebpf_local_maps_t cachestat_maps[] = {{.name = "cstat_global", .internal_input = NETDATA_CACHESTAT_END,
  12. .user_input = 0, .type = NETDATA_EBPF_MAP_STATIC,
  13. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED,
  14. #ifdef LIBBPF_MAJOR_VERSION
  15. .map_type = BPF_MAP_TYPE_PERCPU_ARRAY
  16. #endif
  17. },
  18. {.name = "cstat_pid", .internal_input = ND_EBPF_DEFAULT_PID_SIZE,
  19. .user_input = 0,
  20. .type = NETDATA_EBPF_MAP_RESIZABLE | NETDATA_EBPF_MAP_PID,
  21. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED,
  22. #ifdef LIBBPF_MAJOR_VERSION
  23. .map_type = BPF_MAP_TYPE_PERCPU_HASH
  24. #endif
  25. },
  26. {.name = "cstat_ctrl", .internal_input = NETDATA_CONTROLLER_END,
  27. .user_input = 0,
  28. .type = NETDATA_EBPF_MAP_CONTROLLER,
  29. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED,
  30. #ifdef LIBBPF_MAJOR_VERSION
  31. .map_type = BPF_MAP_TYPE_PERCPU_ARRAY
  32. #endif
  33. },
  34. {.name = NULL, .internal_input = 0, .user_input = 0,
  35. .type = NETDATA_EBPF_MAP_CONTROLLER,
  36. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED,
  37. #ifdef LIBBPF_MAJOR_VERSION
  38. #endif
  39. }};
  40. struct config cachestat_config = { .first_section = NULL,
  41. .last_section = NULL,
  42. .mutex = NETDATA_MUTEX_INITIALIZER,
  43. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  44. .rwlock = AVL_LOCK_INITIALIZER } };
  45. netdata_ebpf_targets_t cachestat_targets[] = { {.name = "add_to_page_cache_lru", .mode = EBPF_LOAD_TRAMPOLINE},
  46. {.name = "mark_page_accessed", .mode = EBPF_LOAD_TRAMPOLINE},
  47. {.name = NULL, .mode = EBPF_LOAD_TRAMPOLINE},
  48. {.name = "mark_buffer_dirty", .mode = EBPF_LOAD_TRAMPOLINE},
  49. {.name = NULL, .mode = EBPF_LOAD_TRAMPOLINE}};
  50. static char *account_page[NETDATA_CACHESTAT_ACCOUNT_DIRTY_END] ={ "account_page_dirtied",
  51. "__set_page_dirty", "__folio_mark_dirty" };
  52. #ifdef LIBBPF_MAJOR_VERSION
  53. /**
  54. * Disable probe
  55. *
  56. * Disable all probes to use exclusively another method.
  57. *
  58. * @param obj is the main structure for bpf objects
  59. */
  60. static void ebpf_cachestat_disable_probe(struct cachestat_bpf *obj)
  61. {
  62. bpf_program__set_autoload(obj->progs.netdata_add_to_page_cache_lru_kprobe, false);
  63. bpf_program__set_autoload(obj->progs.netdata_mark_page_accessed_kprobe, false);
  64. bpf_program__set_autoload(obj->progs.netdata_folio_mark_dirty_kprobe, false);
  65. bpf_program__set_autoload(obj->progs.netdata_set_page_dirty_kprobe, false);
  66. bpf_program__set_autoload(obj->progs.netdata_account_page_dirtied_kprobe, false);
  67. bpf_program__set_autoload(obj->progs.netdata_mark_buffer_dirty_kprobe, false);
  68. bpf_program__set_autoload(obj->progs.netdata_release_task_kprobe, false);
  69. }
  70. /*
  71. * Disable specific probe
  72. *
  73. * Disable probes according the kernel version
  74. *
  75. * @param obj is the main structure for bpf objects
  76. */
  77. static void ebpf_cachestat_disable_specific_probe(struct cachestat_bpf *obj)
  78. {
  79. if (!strcmp(cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name,
  80. account_page[NETDATA_CACHESTAT_FOLIO_DIRTY])) {
  81. bpf_program__set_autoload(obj->progs.netdata_account_page_dirtied_kprobe, false);
  82. bpf_program__set_autoload(obj->progs.netdata_set_page_dirty_kprobe, false);
  83. } else if (!strcmp(cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name,
  84. account_page[NETDATA_CACHESTAT_SET_PAGE_DIRTY])) {
  85. bpf_program__set_autoload(obj->progs.netdata_folio_mark_dirty_kprobe, false);
  86. bpf_program__set_autoload(obj->progs.netdata_account_page_dirtied_kprobe, false);
  87. } else {
  88. bpf_program__set_autoload(obj->progs.netdata_folio_mark_dirty_kprobe, false);
  89. bpf_program__set_autoload(obj->progs.netdata_set_page_dirty_kprobe, false);
  90. }
  91. }
  92. /*
  93. * Disable trampoline
  94. *
  95. * Disable all trampoline to use exclusively another method.
  96. *
  97. * @param obj is the main structure for bpf objects.
  98. */
  99. static void ebpf_cachestat_disable_trampoline(struct cachestat_bpf *obj)
  100. {
  101. bpf_program__set_autoload(obj->progs.netdata_add_to_page_cache_lru_fentry, false);
  102. bpf_program__set_autoload(obj->progs.netdata_mark_page_accessed_fentry, false);
  103. bpf_program__set_autoload(obj->progs.netdata_folio_mark_dirty_fentry, false);
  104. bpf_program__set_autoload(obj->progs.netdata_set_page_dirty_fentry, false);
  105. bpf_program__set_autoload(obj->progs.netdata_account_page_dirtied_fentry, false);
  106. bpf_program__set_autoload(obj->progs.netdata_mark_buffer_dirty_fentry, false);
  107. bpf_program__set_autoload(obj->progs.netdata_release_task_fentry, false);
  108. }
  109. /*
  110. * Disable specific trampoline
  111. *
  112. * Disable trampoline according to kernel version.
  113. *
  114. * @param obj is the main structure for bpf objects.
  115. */
  116. static void ebpf_cachestat_disable_specific_trampoline(struct cachestat_bpf *obj)
  117. {
  118. if (!strcmp(cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name,
  119. account_page[NETDATA_CACHESTAT_FOLIO_DIRTY])) {
  120. bpf_program__set_autoload(obj->progs.netdata_account_page_dirtied_fentry, false);
  121. bpf_program__set_autoload(obj->progs.netdata_set_page_dirty_fentry, false);
  122. } else if (!strcmp(cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name,
  123. account_page[NETDATA_CACHESTAT_SET_PAGE_DIRTY])) {
  124. bpf_program__set_autoload(obj->progs.netdata_folio_mark_dirty_fentry, false);
  125. bpf_program__set_autoload(obj->progs.netdata_account_page_dirtied_fentry, false);
  126. } else {
  127. bpf_program__set_autoload(obj->progs.netdata_folio_mark_dirty_fentry, false);
  128. bpf_program__set_autoload(obj->progs.netdata_set_page_dirty_fentry, false);
  129. }
  130. }
  131. /**
  132. * Set trampoline target
  133. *
  134. * Set the targets we will monitor.
  135. *
  136. * @param obj is the main structure for bpf objects.
  137. */
  138. static inline void netdata_set_trampoline_target(struct cachestat_bpf *obj)
  139. {
  140. bpf_program__set_attach_target(obj->progs.netdata_add_to_page_cache_lru_fentry, 0,
  141. cachestat_targets[NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU].name);
  142. bpf_program__set_attach_target(obj->progs.netdata_mark_page_accessed_fentry, 0,
  143. cachestat_targets[NETDATA_KEY_CALLS_MARK_PAGE_ACCESSED].name);
  144. if (!strcmp(cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name,
  145. account_page[NETDATA_CACHESTAT_FOLIO_DIRTY])) {
  146. bpf_program__set_attach_target(obj->progs.netdata_folio_mark_dirty_fentry, 0,
  147. cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name);
  148. } else if (!strcmp(cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name,
  149. account_page[NETDATA_CACHESTAT_SET_PAGE_DIRTY])) {
  150. bpf_program__set_attach_target(obj->progs.netdata_set_page_dirty_fentry, 0,
  151. cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name);
  152. } else {
  153. bpf_program__set_attach_target(obj->progs.netdata_account_page_dirtied_fentry, 0,
  154. cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name);
  155. }
  156. bpf_program__set_attach_target(obj->progs.netdata_mark_buffer_dirty_fentry, 0,
  157. cachestat_targets[NETDATA_KEY_CALLS_MARK_BUFFER_DIRTY].name);
  158. bpf_program__set_attach_target(obj->progs.netdata_release_task_fentry, 0,
  159. EBPF_COMMON_FNCT_CLEAN_UP);
  160. }
  161. /**
  162. * Mount Attach Probe
  163. *
  164. * Attach probes to target
  165. *
  166. * @param obj is the main structure for bpf objects.
  167. *
  168. * @return It returns 0 on success and -1 otherwise.
  169. */
  170. static int ebpf_cachestat_attach_probe(struct cachestat_bpf *obj)
  171. {
  172. obj->links.netdata_add_to_page_cache_lru_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_add_to_page_cache_lru_kprobe,
  173. false,
  174. cachestat_targets[NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU].name);
  175. int ret = libbpf_get_error(obj->links.netdata_add_to_page_cache_lru_kprobe);
  176. if (ret)
  177. return -1;
  178. obj->links.netdata_mark_page_accessed_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_mark_page_accessed_kprobe,
  179. false,
  180. cachestat_targets[NETDATA_KEY_CALLS_MARK_PAGE_ACCESSED].name);
  181. ret = libbpf_get_error(obj->links.netdata_mark_page_accessed_kprobe);
  182. if (ret)
  183. return -1;
  184. if (!strcmp(cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name,
  185. account_page[NETDATA_CACHESTAT_FOLIO_DIRTY])) {
  186. obj->links.netdata_folio_mark_dirty_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_folio_mark_dirty_kprobe,
  187. false,
  188. cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name);
  189. ret = libbpf_get_error(obj->links.netdata_folio_mark_dirty_kprobe);
  190. } else if (!strcmp(cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name,
  191. account_page[NETDATA_CACHESTAT_SET_PAGE_DIRTY])) {
  192. obj->links.netdata_set_page_dirty_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_set_page_dirty_kprobe,
  193. false,
  194. cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name);
  195. ret = libbpf_get_error(obj->links.netdata_set_page_dirty_kprobe);
  196. } else {
  197. obj->links.netdata_account_page_dirtied_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_account_page_dirtied_kprobe,
  198. false,
  199. cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name);
  200. ret = libbpf_get_error(obj->links.netdata_account_page_dirtied_kprobe);
  201. }
  202. if (ret)
  203. return -1;
  204. obj->links.netdata_mark_buffer_dirty_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_mark_buffer_dirty_kprobe,
  205. false,
  206. cachestat_targets[NETDATA_KEY_CALLS_MARK_BUFFER_DIRTY].name);
  207. ret = libbpf_get_error(obj->links.netdata_mark_buffer_dirty_kprobe);
  208. if (ret)
  209. return -1;
  210. obj->links.netdata_release_task_kprobe = bpf_program__attach_kprobe(obj->progs.netdata_release_task_kprobe,
  211. false,
  212. EBPF_COMMON_FNCT_CLEAN_UP);
  213. ret = libbpf_get_error(obj->links.netdata_release_task_kprobe);
  214. if (ret)
  215. return -1;
  216. return 0;
  217. }
  218. /**
  219. * Adjust Map Size
  220. *
  221. * Resize maps according input from users.
  222. *
  223. * @param obj is the main structure for bpf objects.
  224. * @param em structure with configuration
  225. */
  226. static void ebpf_cachestat_adjust_map(struct cachestat_bpf *obj, ebpf_module_t *em)
  227. {
  228. ebpf_update_map_size(obj->maps.cstat_pid, &cachestat_maps[NETDATA_CACHESTAT_PID_STATS],
  229. em, bpf_map__name(obj->maps.cstat_pid));
  230. ebpf_update_map_type(obj->maps.cstat_global, &cachestat_maps[NETDATA_CACHESTAT_GLOBAL_STATS]);
  231. ebpf_update_map_type(obj->maps.cstat_pid, &cachestat_maps[NETDATA_CACHESTAT_PID_STATS]);
  232. ebpf_update_map_type(obj->maps.cstat_ctrl, &cachestat_maps[NETDATA_CACHESTAT_CTRL]);
  233. }
  234. /**
  235. * Set hash tables
  236. *
  237. * Set the values for maps according the value given by kernel.
  238. *
  239. * @param obj is the main structure for bpf objects.
  240. */
  241. static void ebpf_cachestat_set_hash_tables(struct cachestat_bpf *obj)
  242. {
  243. cachestat_maps[NETDATA_CACHESTAT_GLOBAL_STATS].map_fd = bpf_map__fd(obj->maps.cstat_global);
  244. cachestat_maps[NETDATA_CACHESTAT_PID_STATS].map_fd = bpf_map__fd(obj->maps.cstat_pid);
  245. cachestat_maps[NETDATA_CACHESTAT_CTRL].map_fd = bpf_map__fd(obj->maps.cstat_ctrl);
  246. }
  247. /**
  248. * Disable Release Task
  249. *
  250. * Disable release task when apps is not enabled.
  251. *
  252. * @param obj is the main structure for bpf objects.
  253. */
  254. static void ebpf_cachestat_disable_release_task(struct cachestat_bpf *obj)
  255. {
  256. bpf_program__set_autoload(obj->progs.netdata_release_task_kprobe, false);
  257. bpf_program__set_autoload(obj->progs.netdata_release_task_fentry, false);
  258. }
  259. /**
  260. * Load and attach
  261. *
  262. * Load and attach the eBPF code in kernel.
  263. *
  264. * @param obj is the main structure for bpf objects.
  265. * @param em structure with configuration
  266. *
  267. * @return it returns 0 on success and -1 otherwise
  268. */
  269. static inline int ebpf_cachestat_load_and_attach(struct cachestat_bpf *obj, ebpf_module_t *em)
  270. {
  271. netdata_ebpf_targets_t *mt = em->targets;
  272. netdata_ebpf_program_loaded_t test = mt[NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU].mode;
  273. if (test == EBPF_LOAD_TRAMPOLINE) {
  274. ebpf_cachestat_disable_probe(obj);
  275. ebpf_cachestat_disable_specific_trampoline(obj);
  276. netdata_set_trampoline_target(obj);
  277. } else {
  278. ebpf_cachestat_disable_trampoline(obj);
  279. ebpf_cachestat_disable_specific_probe(obj);
  280. }
  281. ebpf_cachestat_adjust_map(obj, em);
  282. if (!em->apps_charts && !em->cgroup_charts)
  283. ebpf_cachestat_disable_release_task(obj);
  284. int ret = cachestat_bpf__load(obj);
  285. if (ret) {
  286. return ret;
  287. }
  288. ret = (test == EBPF_LOAD_TRAMPOLINE) ? cachestat_bpf__attach(obj) : ebpf_cachestat_attach_probe(obj);
  289. if (!ret) {
  290. ebpf_cachestat_set_hash_tables(obj);
  291. ebpf_update_controller(cachestat_maps[NETDATA_CACHESTAT_CTRL].map_fd, em);
  292. }
  293. return ret;
  294. }
  295. #endif
  296. /*****************************************************************
  297. *
  298. * FUNCTIONS TO CLOSE THE THREAD
  299. *
  300. *****************************************************************/
  301. /**
  302. * Cachestat exit.
  303. *
  304. * Cancel child and exit.
  305. *
  306. * @param ptr thread data.
  307. */
  308. static void ebpf_cachestat_exit(void *ptr)
  309. {
  310. ebpf_module_t *em = (ebpf_module_t *)ptr;
  311. #ifdef LIBBPF_MAJOR_VERSION
  312. if (cachestat_bpf_obj)
  313. cachestat_bpf__destroy(cachestat_bpf_obj);
  314. #endif
  315. if (em->objects) {
  316. ebpf_unload_legacy_code(em->objects, em->probe_links);
  317. }
  318. pthread_mutex_lock(&ebpf_exit_cleanup);
  319. em->enabled = NETDATA_THREAD_EBPF_STOPPED;
  320. pthread_mutex_unlock(&ebpf_exit_cleanup);
  321. }
  322. /*****************************************************************
  323. *
  324. * COMMON FUNCTIONS
  325. *
  326. *****************************************************************/
  327. /**
  328. * Update publish
  329. *
  330. * Update publish values before to write dimension.
  331. *
  332. * @param out structure that will receive data.
  333. * @param mpa calls for mark_page_accessed during the last second.
  334. * @param mbd calls for mark_buffer_dirty during the last second.
  335. * @param apcl calls for add_to_page_cache_lru during the last second.
  336. * @param apd calls for account_page_dirtied during the last second.
  337. */
  338. void cachestat_update_publish(netdata_publish_cachestat_t *out, uint64_t mpa, uint64_t mbd,
  339. uint64_t apcl, uint64_t apd)
  340. {
  341. // Adapted algorithm from https://github.com/iovisor/bcc/blob/master/tools/cachestat.py#L126-L138
  342. NETDATA_DOUBLE total = (NETDATA_DOUBLE) (((long long)mpa) - ((long long)mbd));
  343. if (total < 0)
  344. total = 0;
  345. NETDATA_DOUBLE misses = (NETDATA_DOUBLE) ( ((long long) apcl) - ((long long) apd) );
  346. if (misses < 0)
  347. misses = 0;
  348. // If hits are < 0, then its possible misses are overestimate due to possibly page cache read ahead adding
  349. // more pages than needed. In this case just assume misses as total and reset hits.
  350. NETDATA_DOUBLE hits = total - misses;
  351. if (hits < 0 ) {
  352. misses = total;
  353. hits = 0;
  354. }
  355. NETDATA_DOUBLE ratio = (total > 0) ? hits/total : 1;
  356. out->ratio = (long long )(ratio*100);
  357. out->hit = (long long)hits;
  358. out->miss = (long long)misses;
  359. }
  360. /**
  361. * Save previous values
  362. *
  363. * Save values used this time.
  364. *
  365. * @param publish
  366. */
  367. static void save_previous_values(netdata_publish_cachestat_t *publish) {
  368. publish->prev.mark_page_accessed = cachestat_hash_values[NETDATA_KEY_CALLS_MARK_PAGE_ACCESSED];
  369. publish->prev.account_page_dirtied = cachestat_hash_values[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED];
  370. publish->prev.add_to_page_cache_lru = cachestat_hash_values[NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU];
  371. publish->prev.mark_buffer_dirty = cachestat_hash_values[NETDATA_KEY_CALLS_MARK_BUFFER_DIRTY];
  372. }
  373. /**
  374. * Calculate statistics
  375. *
  376. * @param publish the structure where we will store the data.
  377. */
  378. static void calculate_stats(netdata_publish_cachestat_t *publish) {
  379. if (!publish->prev.mark_page_accessed) {
  380. save_previous_values(publish);
  381. return;
  382. }
  383. uint64_t mpa = cachestat_hash_values[NETDATA_KEY_CALLS_MARK_PAGE_ACCESSED] - publish->prev.mark_page_accessed;
  384. uint64_t mbd = cachestat_hash_values[NETDATA_KEY_CALLS_MARK_BUFFER_DIRTY] - publish->prev.mark_buffer_dirty;
  385. uint64_t apcl = cachestat_hash_values[NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU] - publish->prev.add_to_page_cache_lru;
  386. uint64_t apd = cachestat_hash_values[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED] - publish->prev.account_page_dirtied;
  387. save_previous_values(publish);
  388. // We are changing the original algorithm to have a smooth ratio.
  389. cachestat_update_publish(publish, mpa, mbd, apcl, apd);
  390. }
  391. /*****************************************************************
  392. *
  393. * APPS
  394. *
  395. *****************************************************************/
  396. /**
  397. * Apps Accumulator
  398. *
  399. * Sum all values read from kernel and store in the first address.
  400. *
  401. * @param out the vector with read values.
  402. * @param maps_per_core do I need to read all cores?
  403. */
  404. static void cachestat_apps_accumulator(netdata_cachestat_pid_t *out, int maps_per_core)
  405. {
  406. int i, end = (maps_per_core) ? ebpf_nprocs : 1;
  407. netdata_cachestat_pid_t *total = &out[0];
  408. for (i = 1; i < end; i++) {
  409. netdata_cachestat_pid_t *w = &out[i];
  410. total->account_page_dirtied += w->account_page_dirtied;
  411. total->add_to_page_cache_lru += w->add_to_page_cache_lru;
  412. total->mark_buffer_dirty += w->mark_buffer_dirty;
  413. total->mark_page_accessed += w->mark_page_accessed;
  414. }
  415. }
  416. /**
  417. * Save Pid values
  418. *
  419. * Save the current values inside the structure
  420. *
  421. * @param out vector used to plot charts
  422. * @param publish vector with values read from hash tables.
  423. */
  424. static inline void cachestat_save_pid_values(netdata_publish_cachestat_t *out, netdata_cachestat_pid_t *publish)
  425. {
  426. if (!out->current.mark_page_accessed) {
  427. memcpy(&out->current, &publish[0], sizeof(netdata_cachestat_pid_t));
  428. return;
  429. }
  430. memcpy(&out->prev, &out->current, sizeof(netdata_cachestat_pid_t));
  431. memcpy(&out->current, &publish[0], sizeof(netdata_cachestat_pid_t));
  432. }
  433. /**
  434. * Fill PID
  435. *
  436. * Fill PID structures
  437. *
  438. * @param current_pid pid that we are collecting data
  439. * @param out values read from hash tables;
  440. */
  441. static void cachestat_fill_pid(uint32_t current_pid, netdata_cachestat_pid_t *publish)
  442. {
  443. netdata_publish_cachestat_t *curr = cachestat_pid[current_pid];
  444. if (!curr) {
  445. curr = ebpf_publish_cachestat_get();
  446. cachestat_pid[current_pid] = curr;
  447. cachestat_save_pid_values(curr, publish);
  448. return;
  449. }
  450. cachestat_save_pid_values(curr, publish);
  451. }
  452. /**
  453. * Read APPS table
  454. *
  455. * Read the apps table and store data inside the structure.
  456. *
  457. * @param maps_per_core do I need to read all cores?
  458. */
  459. static void ebpf_read_cachestat_apps_table(int maps_per_core)
  460. {
  461. netdata_cachestat_pid_t *cv = cachestat_vector;
  462. uint32_t key;
  463. struct ebpf_pid_stat *pids = ebpf_root_of_pids;
  464. int fd = cachestat_maps[NETDATA_CACHESTAT_PID_STATS].map_fd;
  465. size_t length = sizeof(netdata_cachestat_pid_t);
  466. if (maps_per_core)
  467. length *= ebpf_nprocs;
  468. while (pids) {
  469. key = pids->pid;
  470. if (bpf_map_lookup_elem(fd, &key, cv)) {
  471. pids = pids->next;
  472. continue;
  473. }
  474. cachestat_apps_accumulator(cv, maps_per_core);
  475. cachestat_fill_pid(key, cv);
  476. // We are cleaning to avoid passing data read from one process to other.
  477. memset(cv, 0, length);
  478. pids = pids->next;
  479. }
  480. }
  481. /**
  482. * Update cgroup
  483. *
  484. * Update cgroup data based in
  485. *
  486. * @param maps_per_core do I need to read all cores?
  487. */
  488. static void ebpf_update_cachestat_cgroup(int maps_per_core)
  489. {
  490. netdata_cachestat_pid_t *cv = cachestat_vector;
  491. int fd = cachestat_maps[NETDATA_CACHESTAT_PID_STATS].map_fd;
  492. size_t length = sizeof(netdata_cachestat_pid_t);
  493. if (maps_per_core)
  494. length *= ebpf_nprocs;
  495. ebpf_cgroup_target_t *ect;
  496. pthread_mutex_lock(&mutex_cgroup_shm);
  497. for (ect = ebpf_cgroup_pids; ect; ect = ect->next) {
  498. struct pid_on_target2 *pids;
  499. for (pids = ect->pids; pids; pids = pids->next) {
  500. int pid = pids->pid;
  501. netdata_cachestat_pid_t *out = &pids->cachestat;
  502. if (likely(cachestat_pid) && cachestat_pid[pid]) {
  503. netdata_publish_cachestat_t *in = cachestat_pid[pid];
  504. memcpy(out, &in->current, sizeof(netdata_cachestat_pid_t));
  505. } else {
  506. memset(cv, 0, length);
  507. if (bpf_map_lookup_elem(fd, &pid, cv)) {
  508. continue;
  509. }
  510. cachestat_apps_accumulator(cv, maps_per_core);
  511. memcpy(out, cv, sizeof(netdata_cachestat_pid_t));
  512. }
  513. }
  514. }
  515. pthread_mutex_unlock(&mutex_cgroup_shm);
  516. }
  517. /**
  518. * Create apps charts
  519. *
  520. * Call ebpf_create_chart to create the charts on apps submenu.
  521. *
  522. * @param em a pointer to the structure with the default values.
  523. */
  524. void ebpf_cachestat_create_apps_charts(struct ebpf_module *em, void *ptr)
  525. {
  526. struct ebpf_target *root = ptr;
  527. ebpf_create_charts_on_apps(NETDATA_CACHESTAT_HIT_RATIO_CHART,
  528. "Hit ratio",
  529. EBPF_COMMON_DIMENSION_PERCENTAGE,
  530. NETDATA_CACHESTAT_SUBMENU,
  531. NETDATA_EBPF_CHART_TYPE_LINE,
  532. 20090,
  533. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  534. root, em->update_every, NETDATA_EBPF_MODULE_NAME_CACHESTAT);
  535. ebpf_create_charts_on_apps(NETDATA_CACHESTAT_DIRTY_CHART,
  536. "Number of dirty pages",
  537. EBPF_CACHESTAT_DIMENSION_PAGE,
  538. NETDATA_CACHESTAT_SUBMENU,
  539. NETDATA_EBPF_CHART_TYPE_STACKED,
  540. 20091,
  541. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  542. root, em->update_every, NETDATA_EBPF_MODULE_NAME_CACHESTAT);
  543. ebpf_create_charts_on_apps(NETDATA_CACHESTAT_HIT_CHART,
  544. "Number of accessed files",
  545. EBPF_CACHESTAT_DIMENSION_HITS,
  546. NETDATA_CACHESTAT_SUBMENU,
  547. NETDATA_EBPF_CHART_TYPE_STACKED,
  548. 20092,
  549. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  550. root, em->update_every, NETDATA_EBPF_MODULE_NAME_CACHESTAT);
  551. ebpf_create_charts_on_apps(NETDATA_CACHESTAT_MISSES_CHART,
  552. "Files out of page cache",
  553. EBPF_CACHESTAT_DIMENSION_MISSES,
  554. NETDATA_CACHESTAT_SUBMENU,
  555. NETDATA_EBPF_CHART_TYPE_STACKED,
  556. 20093,
  557. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  558. root, em->update_every, NETDATA_EBPF_MODULE_NAME_CACHESTAT);
  559. em->apps_charts |= NETDATA_EBPF_APPS_FLAG_CHART_CREATED;
  560. }
  561. /*****************************************************************
  562. *
  563. * MAIN LOOP
  564. *
  565. *****************************************************************/
  566. /**
  567. * Read global counter
  568. *
  569. * Read the table with number of calls for all functions
  570. *
  571. * @param maps_per_core do I need to read all cores?
  572. */
  573. static void ebpf_cachestat_read_global_table(int maps_per_core)
  574. {
  575. uint32_t idx;
  576. netdata_idx_t *val = cachestat_hash_values;
  577. netdata_idx_t *stored = cachestat_values;
  578. int fd = cachestat_maps[NETDATA_CACHESTAT_GLOBAL_STATS].map_fd;
  579. for (idx = NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU; idx < NETDATA_CACHESTAT_END; idx++) {
  580. if (!bpf_map_lookup_elem(fd, &idx, stored)) {
  581. int i;
  582. int end = (maps_per_core) ? ebpf_nprocs: 1;
  583. netdata_idx_t total = 0;
  584. for (i = 0; i < end; i++)
  585. total += stored[i];
  586. val[idx] = total;
  587. }
  588. }
  589. }
  590. /**
  591. * Send global
  592. *
  593. * Send global charts to Netdata
  594. */
  595. static void cachestat_send_global(netdata_publish_cachestat_t *publish)
  596. {
  597. calculate_stats(publish);
  598. netdata_publish_syscall_t *ptr = cachestat_counter_publish_aggregated;
  599. ebpf_one_dimension_write_charts(
  600. NETDATA_EBPF_MEMORY_GROUP, NETDATA_CACHESTAT_HIT_RATIO_CHART, ptr[NETDATA_CACHESTAT_IDX_RATIO].dimension,
  601. publish->ratio);
  602. ebpf_one_dimension_write_charts(
  603. NETDATA_EBPF_MEMORY_GROUP, NETDATA_CACHESTAT_DIRTY_CHART, ptr[NETDATA_CACHESTAT_IDX_DIRTY].dimension,
  604. cachestat_hash_values[NETDATA_KEY_CALLS_MARK_BUFFER_DIRTY]);
  605. ebpf_one_dimension_write_charts(
  606. NETDATA_EBPF_MEMORY_GROUP, NETDATA_CACHESTAT_HIT_CHART, ptr[NETDATA_CACHESTAT_IDX_HIT].dimension, publish->hit);
  607. ebpf_one_dimension_write_charts(
  608. NETDATA_EBPF_MEMORY_GROUP, NETDATA_CACHESTAT_MISSES_CHART, ptr[NETDATA_CACHESTAT_IDX_MISS].dimension,
  609. publish->miss);
  610. }
  611. /**
  612. * Cachestat sum PIDs
  613. *
  614. * Sum values for all PIDs associated to a group
  615. *
  616. * @param publish output structure.
  617. * @param root structure with listed IPs
  618. */
  619. void ebpf_cachestat_sum_pids(netdata_publish_cachestat_t *publish, struct ebpf_pid_on_target *root)
  620. {
  621. memcpy(&publish->prev, &publish->current,sizeof(publish->current));
  622. memset(&publish->current, 0, sizeof(publish->current));
  623. netdata_cachestat_pid_t *dst = &publish->current;
  624. while (root) {
  625. int32_t pid = root->pid;
  626. netdata_publish_cachestat_t *w = cachestat_pid[pid];
  627. if (w) {
  628. netdata_cachestat_pid_t *src = &w->current;
  629. dst->account_page_dirtied += src->account_page_dirtied;
  630. dst->add_to_page_cache_lru += src->add_to_page_cache_lru;
  631. dst->mark_buffer_dirty += src->mark_buffer_dirty;
  632. dst->mark_page_accessed += src->mark_page_accessed;
  633. }
  634. root = root->next;
  635. }
  636. }
  637. /**
  638. * Send data to Netdata calling auxiliary functions.
  639. *
  640. * @param root the target list.
  641. */
  642. void ebpf_cache_send_apps_data(struct ebpf_target *root)
  643. {
  644. struct ebpf_target *w;
  645. collected_number value;
  646. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_CACHESTAT_HIT_RATIO_CHART);
  647. for (w = root; w; w = w->next) {
  648. if (unlikely(w->exposed && w->processes)) {
  649. ebpf_cachestat_sum_pids(&w->cachestat, w->root_pid);
  650. netdata_cachestat_pid_t *current = &w->cachestat.current;
  651. netdata_cachestat_pid_t *prev = &w->cachestat.prev;
  652. uint64_t mpa = current->mark_page_accessed - prev->mark_page_accessed;
  653. uint64_t mbd = current->mark_buffer_dirty - prev->mark_buffer_dirty;
  654. w->cachestat.dirty = mbd;
  655. uint64_t apcl = current->add_to_page_cache_lru - prev->add_to_page_cache_lru;
  656. uint64_t apd = current->account_page_dirtied - prev->account_page_dirtied;
  657. cachestat_update_publish(&w->cachestat, mpa, mbd, apcl, apd);
  658. value = (collected_number) w->cachestat.ratio;
  659. // Here we are using different approach to have a chart more smooth
  660. write_chart_dimension(w->name, value);
  661. }
  662. }
  663. write_end_chart();
  664. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_CACHESTAT_DIRTY_CHART);
  665. for (w = root; w; w = w->next) {
  666. if (unlikely(w->exposed && w->processes)) {
  667. value = (collected_number) w->cachestat.dirty;
  668. write_chart_dimension(w->name, value);
  669. }
  670. }
  671. write_end_chart();
  672. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_CACHESTAT_HIT_CHART);
  673. for (w = root; w; w = w->next) {
  674. if (unlikely(w->exposed && w->processes)) {
  675. value = (collected_number) w->cachestat.hit;
  676. write_chart_dimension(w->name, value);
  677. }
  678. }
  679. write_end_chart();
  680. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_CACHESTAT_MISSES_CHART);
  681. for (w = root; w; w = w->next) {
  682. if (unlikely(w->exposed && w->processes)) {
  683. value = (collected_number) w->cachestat.miss;
  684. write_chart_dimension(w->name, value);
  685. }
  686. }
  687. write_end_chart();
  688. }
  689. /**
  690. * Cachestat sum PIDs
  691. *
  692. * Sum values for all PIDs associated to a group
  693. *
  694. * @param publish output structure.
  695. * @param root structure with listed IPs
  696. */
  697. void ebpf_cachestat_sum_cgroup_pids(netdata_publish_cachestat_t *publish, struct pid_on_target2 *root)
  698. {
  699. memcpy(&publish->prev, &publish->current,sizeof(publish->current));
  700. memset(&publish->current, 0, sizeof(publish->current));
  701. netdata_cachestat_pid_t *dst = &publish->current;
  702. while (root) {
  703. netdata_cachestat_pid_t *src = &root->cachestat;
  704. dst->account_page_dirtied += src->account_page_dirtied;
  705. dst->add_to_page_cache_lru += src->add_to_page_cache_lru;
  706. dst->mark_buffer_dirty += src->mark_buffer_dirty;
  707. dst->mark_page_accessed += src->mark_page_accessed;
  708. root = root->next;
  709. }
  710. }
  711. /**
  712. * Calc chart values
  713. *
  714. * Do necessary math to plot charts.
  715. */
  716. void ebpf_cachestat_calc_chart_values()
  717. {
  718. ebpf_cgroup_target_t *ect;
  719. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  720. ebpf_cachestat_sum_cgroup_pids(&ect->publish_cachestat, ect->pids);
  721. netdata_cachestat_pid_t *current = &ect->publish_cachestat.current;
  722. netdata_cachestat_pid_t *prev = &ect->publish_cachestat.prev;
  723. uint64_t mpa = current->mark_page_accessed - prev->mark_page_accessed;
  724. uint64_t mbd = current->mark_buffer_dirty - prev->mark_buffer_dirty;
  725. ect->publish_cachestat.dirty = mbd;
  726. uint64_t apcl = current->add_to_page_cache_lru - prev->add_to_page_cache_lru;
  727. uint64_t apd = current->account_page_dirtied - prev->account_page_dirtied;
  728. cachestat_update_publish(&ect->publish_cachestat, mpa, mbd, apcl, apd);
  729. }
  730. }
  731. /**
  732. * Create Systemd cachestat Charts
  733. *
  734. * Create charts when systemd is enabled
  735. *
  736. * @param update_every value to overwrite the update frequency set by the server.
  737. **/
  738. static void ebpf_create_systemd_cachestat_charts(int update_every)
  739. {
  740. ebpf_create_charts_on_systemd(NETDATA_CACHESTAT_HIT_RATIO_CHART,
  741. "Hit ratio",
  742. EBPF_COMMON_DIMENSION_PERCENTAGE, NETDATA_CACHESTAT_SUBMENU,
  743. NETDATA_EBPF_CHART_TYPE_LINE, 21100,
  744. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  745. NETDATA_SYSTEMD_CACHESTAT_HIT_RATIO_CONTEXT, NETDATA_EBPF_MODULE_NAME_CACHESTAT,
  746. update_every);
  747. ebpf_create_charts_on_systemd(NETDATA_CACHESTAT_DIRTY_CHART,
  748. "Number of dirty pages",
  749. EBPF_CACHESTAT_DIMENSION_PAGE, NETDATA_CACHESTAT_SUBMENU,
  750. NETDATA_EBPF_CHART_TYPE_LINE, 21101,
  751. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  752. NETDATA_SYSTEMD_CACHESTAT_MODIFIED_CACHE_CONTEXT, NETDATA_EBPF_MODULE_NAME_CACHESTAT,
  753. update_every);
  754. ebpf_create_charts_on_systemd(NETDATA_CACHESTAT_HIT_CHART, "Number of accessed files",
  755. EBPF_CACHESTAT_DIMENSION_HITS, NETDATA_CACHESTAT_SUBMENU,
  756. NETDATA_EBPF_CHART_TYPE_LINE, 21102,
  757. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  758. NETDATA_SYSTEMD_CACHESTAT_HIT_FILE_CONTEXT, NETDATA_EBPF_MODULE_NAME_CACHESTAT,
  759. update_every);
  760. ebpf_create_charts_on_systemd(NETDATA_CACHESTAT_MISSES_CHART, "Files out of page cache",
  761. EBPF_CACHESTAT_DIMENSION_MISSES, NETDATA_CACHESTAT_SUBMENU,
  762. NETDATA_EBPF_CHART_TYPE_LINE, 21103,
  763. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  764. NETDATA_SYSTEMD_CACHESTAT_MISS_FILES_CONTEXT, NETDATA_EBPF_MODULE_NAME_CACHESTAT,
  765. update_every);
  766. }
  767. /**
  768. * Send Cache Stat charts
  769. *
  770. * Send collected data to Netdata.
  771. */
  772. static void ebpf_send_systemd_cachestat_charts()
  773. {
  774. ebpf_cgroup_target_t *ect;
  775. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_CACHESTAT_HIT_RATIO_CHART);
  776. for (ect = ebpf_cgroup_pids; ect; ect = ect->next) {
  777. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  778. write_chart_dimension(ect->name, (long long)ect->publish_cachestat.ratio);
  779. }
  780. }
  781. write_end_chart();
  782. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_CACHESTAT_DIRTY_CHART);
  783. for (ect = ebpf_cgroup_pids; ect; ect = ect->next) {
  784. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  785. write_chart_dimension(ect->name, (long long)ect->publish_cachestat.dirty);
  786. }
  787. }
  788. write_end_chart();
  789. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_CACHESTAT_HIT_CHART);
  790. for (ect = ebpf_cgroup_pids; ect; ect = ect->next) {
  791. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  792. write_chart_dimension(ect->name, (long long)ect->publish_cachestat.hit);
  793. }
  794. }
  795. write_end_chart();
  796. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_CACHESTAT_MISSES_CHART);
  797. for (ect = ebpf_cgroup_pids; ect; ect = ect->next) {
  798. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  799. write_chart_dimension(ect->name, (long long)ect->publish_cachestat.miss);
  800. }
  801. }
  802. write_end_chart();
  803. }
  804. /**
  805. * Send Directory Cache charts
  806. *
  807. * Send collected data to Netdata.
  808. */
  809. static void ebpf_send_specific_cachestat_data(char *type, netdata_publish_cachestat_t *npc)
  810. {
  811. write_begin_chart(type, NETDATA_CACHESTAT_HIT_RATIO_CHART);
  812. write_chart_dimension(cachestat_counter_publish_aggregated[NETDATA_CACHESTAT_IDX_RATIO].name, (long long)npc->ratio);
  813. write_end_chart();
  814. write_begin_chart(type, NETDATA_CACHESTAT_DIRTY_CHART);
  815. write_chart_dimension(cachestat_counter_publish_aggregated[NETDATA_CACHESTAT_IDX_DIRTY].name, (long long)npc->dirty);
  816. write_end_chart();
  817. write_begin_chart(type, NETDATA_CACHESTAT_HIT_CHART);
  818. write_chart_dimension(cachestat_counter_publish_aggregated[NETDATA_CACHESTAT_IDX_HIT].name, (long long)npc->hit);
  819. write_end_chart();
  820. write_begin_chart(type, NETDATA_CACHESTAT_MISSES_CHART);
  821. write_chart_dimension(cachestat_counter_publish_aggregated[NETDATA_CACHESTAT_IDX_MISS].name, (long long)npc->miss);
  822. write_end_chart();
  823. }
  824. /**
  825. * Create specific cache Stat charts
  826. *
  827. * Create charts for cgroup/application.
  828. *
  829. * @param type the chart type.
  830. * @param update_every value to overwrite the update frequency set by the server.
  831. */
  832. static void ebpf_create_specific_cachestat_charts(char *type, int update_every)
  833. {
  834. ebpf_create_chart(type, NETDATA_CACHESTAT_HIT_RATIO_CHART,
  835. "Hit ratio",
  836. EBPF_COMMON_DIMENSION_PERCENTAGE, NETDATA_CACHESTAT_CGROUP_SUBMENU,
  837. NETDATA_CGROUP_CACHESTAT_HIT_RATIO_CONTEXT,
  838. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5200,
  839. ebpf_create_global_dimension,
  840. cachestat_counter_publish_aggregated, 1, update_every, NETDATA_EBPF_MODULE_NAME_CACHESTAT);
  841. ebpf_create_chart(type, NETDATA_CACHESTAT_DIRTY_CHART,
  842. "Number of dirty pages",
  843. EBPF_CACHESTAT_DIMENSION_PAGE, NETDATA_CACHESTAT_CGROUP_SUBMENU,
  844. NETDATA_CGROUP_CACHESTAT_MODIFIED_CACHE_CONTEXT,
  845. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5201,
  846. ebpf_create_global_dimension,
  847. &cachestat_counter_publish_aggregated[NETDATA_CACHESTAT_IDX_DIRTY], 1,
  848. update_every, NETDATA_EBPF_MODULE_NAME_CACHESTAT);
  849. ebpf_create_chart(type, NETDATA_CACHESTAT_HIT_CHART,
  850. "Number of accessed files",
  851. EBPF_CACHESTAT_DIMENSION_HITS, NETDATA_CACHESTAT_CGROUP_SUBMENU,
  852. NETDATA_CGROUP_CACHESTAT_HIT_FILES_CONTEXT,
  853. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5202,
  854. ebpf_create_global_dimension,
  855. &cachestat_counter_publish_aggregated[NETDATA_CACHESTAT_IDX_HIT], 1,
  856. update_every, NETDATA_EBPF_MODULE_NAME_CACHESTAT);
  857. ebpf_create_chart(type, NETDATA_CACHESTAT_MISSES_CHART,
  858. "Files out of page cache",
  859. EBPF_CACHESTAT_DIMENSION_MISSES, NETDATA_CACHESTAT_CGROUP_SUBMENU,
  860. NETDATA_CGROUP_CACHESTAT_MISS_FILES_CONTEXT,
  861. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5203,
  862. ebpf_create_global_dimension,
  863. &cachestat_counter_publish_aggregated[NETDATA_CACHESTAT_IDX_MISS], 1,
  864. update_every, NETDATA_EBPF_MODULE_NAME_CACHESTAT);
  865. }
  866. /**
  867. * Obsolete specific cache stat charts
  868. *
  869. * Obsolete charts for cgroup/application.
  870. *
  871. * @param type the chart type.
  872. * @param update_every value to overwrite the update frequency set by the server.
  873. */
  874. static void ebpf_obsolete_specific_cachestat_charts(char *type, int update_every)
  875. {
  876. ebpf_write_chart_obsolete(type, NETDATA_CACHESTAT_HIT_RATIO_CHART,
  877. "Hit ratio",
  878. EBPF_COMMON_DIMENSION_PERCENTAGE, NETDATA_CACHESTAT_SUBMENU,
  879. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_CGROUP_CACHESTAT_HIT_RATIO_CONTEXT,
  880. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5200, update_every);
  881. ebpf_write_chart_obsolete(type, NETDATA_CACHESTAT_DIRTY_CHART,
  882. "Number of dirty pages",
  883. EBPF_CACHESTAT_DIMENSION_PAGE, NETDATA_CACHESTAT_SUBMENU,
  884. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_CGROUP_CACHESTAT_MODIFIED_CACHE_CONTEXT,
  885. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5201, update_every);
  886. ebpf_write_chart_obsolete(type, NETDATA_CACHESTAT_HIT_CHART,
  887. "Number of accessed files",
  888. EBPF_CACHESTAT_DIMENSION_HITS, NETDATA_CACHESTAT_SUBMENU,
  889. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_CGROUP_CACHESTAT_HIT_FILES_CONTEXT,
  890. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5202, update_every);
  891. ebpf_write_chart_obsolete(type, NETDATA_CACHESTAT_MISSES_CHART,
  892. "Files out of page cache",
  893. EBPF_CACHESTAT_DIMENSION_MISSES, NETDATA_CACHESTAT_SUBMENU,
  894. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_CGROUP_CACHESTAT_MISS_FILES_CONTEXT,
  895. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5203, update_every);
  896. }
  897. /**
  898. * Send data to Netdata calling auxiliary functions.
  899. *
  900. * @param update_every value to overwrite the update frequency set by the server.
  901. */
  902. void ebpf_cachestat_send_cgroup_data(int update_every)
  903. {
  904. if (!ebpf_cgroup_pids)
  905. return;
  906. pthread_mutex_lock(&mutex_cgroup_shm);
  907. ebpf_cgroup_target_t *ect;
  908. ebpf_cachestat_calc_chart_values();
  909. int has_systemd = shm_ebpf_cgroup.header->systemd_enabled;
  910. if (has_systemd) {
  911. if (send_cgroup_chart) {
  912. ebpf_create_systemd_cachestat_charts(update_every);
  913. }
  914. ebpf_send_systemd_cachestat_charts();
  915. }
  916. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  917. if (ect->systemd)
  918. continue;
  919. if (!(ect->flags & NETDATA_EBPF_CGROUP_HAS_CACHESTAT_CHART) && ect->updated) {
  920. ebpf_create_specific_cachestat_charts(ect->name, update_every);
  921. ect->flags |= NETDATA_EBPF_CGROUP_HAS_CACHESTAT_CHART;
  922. }
  923. if (ect->flags & NETDATA_EBPF_CGROUP_HAS_CACHESTAT_CHART) {
  924. if (ect->updated) {
  925. ebpf_send_specific_cachestat_data(ect->name, &ect->publish_cachestat);
  926. } else {
  927. ebpf_obsolete_specific_cachestat_charts(ect->name, update_every);
  928. ect->flags &= ~NETDATA_EBPF_CGROUP_HAS_CACHESTAT_CHART;
  929. }
  930. }
  931. }
  932. pthread_mutex_unlock(&mutex_cgroup_shm);
  933. }
  934. /**
  935. * Main loop for this collector.
  936. */
  937. static void cachestat_collector(ebpf_module_t *em)
  938. {
  939. netdata_publish_cachestat_t publish;
  940. memset(&publish, 0, sizeof(publish));
  941. int cgroups = em->cgroup_charts;
  942. int update_every = em->update_every;
  943. int maps_per_core = em->maps_per_core;
  944. heartbeat_t hb;
  945. heartbeat_init(&hb);
  946. int counter = update_every - 1;
  947. //This will be cancelled by its parent
  948. while (!ebpf_exit_plugin) {
  949. (void)heartbeat_next(&hb, USEC_PER_SEC);
  950. if (ebpf_exit_plugin || ++counter != update_every)
  951. continue;
  952. counter = 0;
  953. netdata_apps_integration_flags_t apps = em->apps_charts;
  954. ebpf_cachestat_read_global_table(maps_per_core);
  955. pthread_mutex_lock(&collect_data_mutex);
  956. if (apps)
  957. ebpf_read_cachestat_apps_table(maps_per_core);
  958. if (cgroups)
  959. ebpf_update_cachestat_cgroup(maps_per_core);
  960. pthread_mutex_lock(&lock);
  961. cachestat_send_global(&publish);
  962. if (apps & NETDATA_EBPF_APPS_FLAG_CHART_CREATED)
  963. ebpf_cache_send_apps_data(apps_groups_root_target);
  964. #ifdef NETDATA_DEV_MODE
  965. if (ebpf_aral_cachestat_pid)
  966. ebpf_send_data_aral_chart(ebpf_aral_cachestat_pid, em);
  967. #endif
  968. if (cgroups)
  969. ebpf_cachestat_send_cgroup_data(update_every);
  970. pthread_mutex_unlock(&lock);
  971. pthread_mutex_unlock(&collect_data_mutex);
  972. }
  973. }
  974. /*****************************************************************
  975. *
  976. * INITIALIZE THREAD
  977. *
  978. *****************************************************************/
  979. /**
  980. * Create global charts
  981. *
  982. * Call ebpf_create_chart to create the charts for the collector.
  983. *
  984. * @param em a pointer to `struct ebpf_module`
  985. */
  986. static void ebpf_create_memory_charts(ebpf_module_t *em)
  987. {
  988. ebpf_create_chart(NETDATA_EBPF_MEMORY_GROUP, NETDATA_CACHESTAT_HIT_RATIO_CHART,
  989. "Hit ratio",
  990. EBPF_COMMON_DIMENSION_PERCENTAGE, NETDATA_CACHESTAT_SUBMENU,
  991. NULL,
  992. NETDATA_EBPF_CHART_TYPE_LINE,
  993. 21100,
  994. ebpf_create_global_dimension,
  995. cachestat_counter_publish_aggregated, 1, em->update_every, NETDATA_EBPF_MODULE_NAME_CACHESTAT);
  996. ebpf_create_chart(NETDATA_EBPF_MEMORY_GROUP, NETDATA_CACHESTAT_DIRTY_CHART,
  997. "Number of dirty pages",
  998. EBPF_CACHESTAT_DIMENSION_PAGE, NETDATA_CACHESTAT_SUBMENU,
  999. NULL,
  1000. NETDATA_EBPF_CHART_TYPE_LINE,
  1001. 21101,
  1002. ebpf_create_global_dimension,
  1003. &cachestat_counter_publish_aggregated[NETDATA_CACHESTAT_IDX_DIRTY], 1,
  1004. em->update_every, NETDATA_EBPF_MODULE_NAME_CACHESTAT);
  1005. ebpf_create_chart(NETDATA_EBPF_MEMORY_GROUP, NETDATA_CACHESTAT_HIT_CHART,
  1006. "Number of accessed files",
  1007. EBPF_CACHESTAT_DIMENSION_HITS, NETDATA_CACHESTAT_SUBMENU,
  1008. NULL,
  1009. NETDATA_EBPF_CHART_TYPE_LINE,
  1010. 21102,
  1011. ebpf_create_global_dimension,
  1012. &cachestat_counter_publish_aggregated[NETDATA_CACHESTAT_IDX_HIT], 1,
  1013. em->update_every, NETDATA_EBPF_MODULE_NAME_CACHESTAT);
  1014. ebpf_create_chart(NETDATA_EBPF_MEMORY_GROUP, NETDATA_CACHESTAT_MISSES_CHART,
  1015. "Files out of page cache",
  1016. EBPF_CACHESTAT_DIMENSION_MISSES, NETDATA_CACHESTAT_SUBMENU,
  1017. NULL,
  1018. NETDATA_EBPF_CHART_TYPE_LINE,
  1019. 21103,
  1020. ebpf_create_global_dimension,
  1021. &cachestat_counter_publish_aggregated[NETDATA_CACHESTAT_IDX_MISS], 1,
  1022. em->update_every, NETDATA_EBPF_MODULE_NAME_CACHESTAT);
  1023. fflush(stdout);
  1024. }
  1025. /**
  1026. * Allocate vectors used with this thread.
  1027. *
  1028. * We are not testing the return, because callocz does this and shutdown the software
  1029. * case it was not possible to allocate.
  1030. *
  1031. * @param apps is apps enabled?
  1032. */
  1033. static void ebpf_cachestat_allocate_global_vectors(int apps)
  1034. {
  1035. if (apps) {
  1036. cachestat_pid = callocz((size_t)pid_max, sizeof(netdata_publish_cachestat_t *));
  1037. ebpf_cachestat_aral_init();
  1038. cachestat_vector = callocz((size_t)ebpf_nprocs, sizeof(netdata_cachestat_pid_t));
  1039. }
  1040. cachestat_values = callocz((size_t)ebpf_nprocs, sizeof(netdata_idx_t));
  1041. memset(cachestat_hash_values, 0, NETDATA_CACHESTAT_END * sizeof(netdata_idx_t));
  1042. memset(cachestat_counter_aggregated_data, 0, NETDATA_CACHESTAT_END * sizeof(netdata_syscall_stat_t));
  1043. memset(cachestat_counter_publish_aggregated, 0, NETDATA_CACHESTAT_END * sizeof(netdata_publish_syscall_t));
  1044. }
  1045. /*****************************************************************
  1046. *
  1047. * MAIN THREAD
  1048. *
  1049. *****************************************************************/
  1050. /**
  1051. * Update Internal value
  1052. *
  1053. * Update values used during runtime.
  1054. *
  1055. * @return It returns 0 when one of the functions is present and -1 otherwise.
  1056. */
  1057. static int ebpf_cachestat_set_internal_value()
  1058. {
  1059. ebpf_addresses_t address = {.function = NULL, .hash = 0, .addr = 0};
  1060. int i;
  1061. for (i = 0; i < NETDATA_CACHESTAT_ACCOUNT_DIRTY_END ; i++) {
  1062. address.function = account_page[i];
  1063. ebpf_load_addresses(&address, -1);
  1064. if (address.addr)
  1065. break;
  1066. }
  1067. if (!address.addr) {
  1068. error("%s cachestat.", NETDATA_EBPF_DEFAULT_FNT_NOT_FOUND);
  1069. return -1;
  1070. }
  1071. cachestat_targets[NETDATA_KEY_CALLS_ACCOUNT_PAGE_DIRTIED].name = address.function;
  1072. return 0;
  1073. }
  1074. /*
  1075. * Load BPF
  1076. *
  1077. * Load BPF files.
  1078. *
  1079. * @param em the structure with configuration
  1080. */
  1081. static int ebpf_cachestat_load_bpf(ebpf_module_t *em)
  1082. {
  1083. #ifdef LIBBPF_MAJOR_VERSION
  1084. ebpf_define_map_type(cachestat_maps, em->maps_per_core, running_on_kernel);
  1085. #endif
  1086. int ret = 0;
  1087. ebpf_adjust_apps_cgroup(em, em->targets[NETDATA_KEY_CALLS_ADD_TO_PAGE_CACHE_LRU].mode);
  1088. if (em->load & EBPF_LOAD_LEGACY) {
  1089. em->probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &em->objects);
  1090. if (!em->probe_links) {
  1091. ret = -1;
  1092. }
  1093. }
  1094. #ifdef LIBBPF_MAJOR_VERSION
  1095. else {
  1096. cachestat_bpf_obj = cachestat_bpf__open();
  1097. if (!cachestat_bpf_obj)
  1098. ret = -1;
  1099. else
  1100. ret = ebpf_cachestat_load_and_attach(cachestat_bpf_obj, em);
  1101. }
  1102. #endif
  1103. if (ret)
  1104. error("%s %s", EBPF_DEFAULT_ERROR_MSG, em->thread_name);
  1105. return ret;
  1106. }
  1107. /**
  1108. * Cachestat thread
  1109. *
  1110. * Thread used to make cachestat thread
  1111. *
  1112. * @param ptr a pointer to `struct ebpf_module`
  1113. *
  1114. * @return It always return NULL
  1115. */
  1116. void *ebpf_cachestat_thread(void *ptr)
  1117. {
  1118. netdata_thread_cleanup_push(ebpf_cachestat_exit, ptr);
  1119. ebpf_module_t *em = (ebpf_module_t *)ptr;
  1120. em->maps = cachestat_maps;
  1121. ebpf_update_pid_table(&cachestat_maps[NETDATA_CACHESTAT_PID_STATS], em);
  1122. if (ebpf_cachestat_set_internal_value()) {
  1123. goto endcachestat;
  1124. }
  1125. #ifdef LIBBPF_MAJOR_VERSION
  1126. ebpf_adjust_thread_load(em, default_btf);
  1127. #endif
  1128. if (ebpf_cachestat_load_bpf(em)) {
  1129. goto endcachestat;
  1130. }
  1131. ebpf_cachestat_allocate_global_vectors(em->apps_charts);
  1132. int algorithms[NETDATA_CACHESTAT_END] = {
  1133. NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_INCREMENTAL_IDX, NETDATA_EBPF_ABSOLUTE_IDX, NETDATA_EBPF_ABSOLUTE_IDX
  1134. };
  1135. ebpf_global_labels(cachestat_counter_aggregated_data, cachestat_counter_publish_aggregated,
  1136. cachestat_counter_dimension_name, cachestat_counter_dimension_name,
  1137. algorithms, NETDATA_CACHESTAT_END);
  1138. pthread_mutex_lock(&lock);
  1139. ebpf_update_stats(&plugin_statistics, em);
  1140. ebpf_update_kernel_memory_with_vector(&plugin_statistics, em->maps);
  1141. ebpf_create_memory_charts(em);
  1142. #ifdef NETDATA_DEV_MODE
  1143. if (ebpf_aral_cachestat_pid)
  1144. ebpf_statistic_create_aral_chart(NETDATA_EBPF_CACHESTAT_ARAL_NAME, em);
  1145. #endif
  1146. pthread_mutex_unlock(&lock);
  1147. cachestat_collector(em);
  1148. endcachestat:
  1149. ebpf_update_disabled_plugin_stats(em);
  1150. netdata_thread_cleanup_pop(1);
  1151. return NULL;
  1152. }