ebpf_cachestat.c 51 KB

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