ebpf_cachestat.c 51 KB

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