ebpf_swap.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "ebpf.h"
  3. #include "ebpf_swap.h"
  4. static char *swap_dimension_name[NETDATA_SWAP_END] = { "read", "write" };
  5. static netdata_syscall_stat_t swap_aggregated_data[NETDATA_SWAP_END];
  6. static netdata_publish_syscall_t swap_publish_aggregated[NETDATA_SWAP_END];
  7. static int read_thread_closed = 1;
  8. netdata_publish_swap_t *swap_vector = NULL;
  9. static netdata_idx_t swap_hash_values[NETDATA_SWAP_END];
  10. static netdata_idx_t *swap_values = NULL;
  11. netdata_publish_swap_t **swap_pid = NULL;
  12. struct config swap_config = { .first_section = NULL,
  13. .last_section = NULL,
  14. .mutex = NETDATA_MUTEX_INITIALIZER,
  15. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  16. .rwlock = AVL_LOCK_INITIALIZER } };
  17. static ebpf_local_maps_t swap_maps[] = {{.name = "tbl_pid_swap", .internal_input = ND_EBPF_DEFAULT_PID_SIZE,
  18. .user_input = 0,
  19. .type = NETDATA_EBPF_MAP_RESIZABLE | NETDATA_EBPF_MAP_PID,
  20. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  21. {.name = "swap_ctrl", .internal_input = NETDATA_CONTROLLER_END,
  22. .user_input = 0,
  23. .type = NETDATA_EBPF_MAP_CONTROLLER,
  24. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  25. {.name = "tbl_swap", .internal_input = NETDATA_SWAP_END,
  26. .user_input = 0,
  27. .type = NETDATA_EBPF_MAP_STATIC,
  28. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  29. {.name = NULL, .internal_input = 0, .user_input = 0}};
  30. static struct bpf_link **probe_links = NULL;
  31. static struct bpf_object *objects = NULL;
  32. struct netdata_static_thread swap_threads = {"SWAP KERNEL", NULL, NULL, 1,
  33. NULL, NULL, NULL};
  34. netdata_ebpf_targets_t swap_targets[] = { {.name = "swap_readpage", .mode = EBPF_LOAD_TRAMPOLINE},
  35. {.name = "swap_writepage", .mode = EBPF_LOAD_TRAMPOLINE},
  36. {.name = NULL, .mode = EBPF_LOAD_TRAMPOLINE}};
  37. #ifdef LIBBPF_MAJOR_VERSION
  38. #include "includes/swap.skel.h" // BTF code
  39. static struct swap_bpf *bpf_obj = NULL;
  40. /**
  41. * Disable probe
  42. *
  43. * Disable all probes to use exclusively another method.
  44. *
  45. * @param obj is the main structure for bpf objects
  46. */
  47. static void ebpf_swap_disable_probe(struct swap_bpf *obj)
  48. {
  49. bpf_program__set_autoload(obj->progs.netdata_swap_readpage_probe, false);
  50. bpf_program__set_autoload(obj->progs.netdata_swap_writepage_probe, false);
  51. }
  52. /*
  53. * Disable trampoline
  54. *
  55. * Disable all trampoline to use exclusively another method.
  56. *
  57. * @param obj is the main structure for bpf objects.
  58. */
  59. static void ebpf_swap_disable_trampoline(struct swap_bpf *obj)
  60. {
  61. bpf_program__set_autoload(obj->progs.netdata_swap_readpage_fentry, false);
  62. bpf_program__set_autoload(obj->progs.netdata_swap_writepage_fentry, false);
  63. }
  64. /**
  65. * Set trampoline target
  66. *
  67. * Set the targets we will monitor.
  68. *
  69. * @param obj is the main structure for bpf objects.
  70. */
  71. static void ebpf_swap_set_trampoline_target(struct swap_bpf *obj)
  72. {
  73. bpf_program__set_attach_target(obj->progs.netdata_swap_readpage_fentry, 0,
  74. swap_targets[NETDATA_KEY_SWAP_READPAGE_CALL].name);
  75. bpf_program__set_attach_target(obj->progs.netdata_swap_writepage_fentry, 0,
  76. swap_targets[NETDATA_KEY_SWAP_WRITEPAGE_CALL].name);
  77. }
  78. /**
  79. * Mount Attach Probe
  80. *
  81. * Attach probes to target
  82. *
  83. * @param obj is the main structure for bpf objects.
  84. *
  85. * @return It returns 0 on success and -1 otherwise.
  86. */
  87. static int ebpf_swap_attach_kprobe(struct swap_bpf *obj)
  88. {
  89. obj->links.netdata_swap_readpage_probe = bpf_program__attach_kprobe(obj->progs.netdata_swap_readpage_probe,
  90. false,
  91. swap_targets[NETDATA_KEY_SWAP_READPAGE_CALL].name);
  92. int ret = libbpf_get_error(obj->links.netdata_swap_readpage_probe);
  93. if (ret)
  94. return -1;
  95. obj->links.netdata_swap_writepage_probe = bpf_program__attach_kprobe(obj->progs.netdata_swap_writepage_probe,
  96. false,
  97. swap_targets[NETDATA_KEY_SWAP_WRITEPAGE_CALL].name);
  98. ret = libbpf_get_error(obj->links.netdata_swap_writepage_probe);
  99. if (ret)
  100. return -1;
  101. return 0;
  102. }
  103. /**
  104. * Set hash tables
  105. *
  106. * Set the values for maps according the value given by kernel.
  107. *
  108. * @param obj is the main structure for bpf objects.
  109. */
  110. static void ebpf_swap_set_hash_tables(struct swap_bpf *obj)
  111. {
  112. swap_maps[NETDATA_PID_SWAP_TABLE].map_fd = bpf_map__fd(obj->maps.tbl_pid_swap);
  113. swap_maps[NETDATA_SWAP_CONTROLLER].map_fd = bpf_map__fd(obj->maps.swap_ctrl);
  114. swap_maps[NETDATA_SWAP_GLOBAL_TABLE].map_fd = bpf_map__fd(obj->maps.tbl_swap);
  115. }
  116. /**
  117. * Adjust Map Size
  118. *
  119. * Resize maps according input from users.
  120. *
  121. * @param obj is the main structure for bpf objects.
  122. * @param em structure with configuration
  123. */
  124. static void ebpf_swap_adjust_map_size(struct swap_bpf *obj, ebpf_module_t *em)
  125. {
  126. ebpf_update_map_size(obj->maps.tbl_pid_swap, &swap_maps[NETDATA_PID_SWAP_TABLE],
  127. em, bpf_map__name(obj->maps.tbl_pid_swap));
  128. }
  129. /**
  130. * Load and attach
  131. *
  132. * Load and attach the eBPF code in kernel.
  133. *
  134. * @param obj is the main structure for bpf objects.
  135. * @param em structure with configuration
  136. *
  137. * @return it returns 0 on succes and -1 otherwise
  138. */
  139. static inline int ebpf_swap_load_and_attach(struct swap_bpf *obj, ebpf_module_t *em)
  140. {
  141. netdata_ebpf_targets_t *mt = em->targets;
  142. netdata_ebpf_program_loaded_t test = mt[NETDATA_KEY_SWAP_READPAGE_CALL].mode;
  143. if (test == EBPF_LOAD_TRAMPOLINE) {
  144. ebpf_swap_disable_probe(obj);
  145. ebpf_swap_set_trampoline_target(obj);
  146. } else {
  147. ebpf_swap_disable_trampoline(obj);
  148. }
  149. int ret = swap_bpf__load(obj);
  150. if (ret) {
  151. return ret;
  152. }
  153. ebpf_swap_adjust_map_size(obj, em);
  154. ret = (test == EBPF_LOAD_TRAMPOLINE) ? swap_bpf__attach(obj) : ebpf_swap_attach_kprobe(obj);
  155. if (!ret) {
  156. ebpf_swap_set_hash_tables(obj);
  157. ebpf_update_controller(swap_maps[NETDATA_SWAP_CONTROLLER].map_fd, em);
  158. }
  159. return ret;
  160. }
  161. #endif
  162. /*****************************************************************
  163. *
  164. * FUNCTIONS TO CLOSE THE THREAD
  165. *
  166. *****************************************************************/
  167. /**
  168. * Clean swap structure
  169. */
  170. void clean_swap_pid_structures() {
  171. struct pid_stat *pids = root_of_pids;
  172. while (pids) {
  173. freez(swap_pid[pids->pid]);
  174. pids = pids->next;
  175. }
  176. }
  177. /**
  178. * Clean up the main thread.
  179. *
  180. * @param ptr thread data.
  181. */
  182. static void ebpf_swap_cleanup(void *ptr)
  183. {
  184. ebpf_module_t *em = (ebpf_module_t *)ptr;
  185. if (!em->enabled)
  186. return;
  187. heartbeat_t hb;
  188. heartbeat_init(&hb);
  189. uint32_t tick = 2 * USEC_PER_MS;
  190. while (!read_thread_closed) {
  191. usec_t dt = heartbeat_next(&hb, tick);
  192. UNUSED(dt);
  193. }
  194. ebpf_cleanup_publish_syscall(swap_publish_aggregated);
  195. freez(swap_vector);
  196. freez(swap_values);
  197. if (probe_links) {
  198. struct bpf_program *prog;
  199. size_t i = 0 ;
  200. bpf_object__for_each_program(prog, objects) {
  201. bpf_link__destroy(probe_links[i]);
  202. i++;
  203. }
  204. bpf_object__close(objects);
  205. }
  206. #ifdef LIBBPF_MAJOR_VERSION
  207. else if (bpf_obj)
  208. swap_bpf__destroy(bpf_obj);
  209. #endif
  210. }
  211. /*****************************************************************
  212. *
  213. * COLLECTOR THREAD
  214. *
  215. *****************************************************************/
  216. /**
  217. * Apps Accumulator
  218. *
  219. * Sum all values read from kernel and store in the first address.
  220. *
  221. * @param out the vector with read values.
  222. */
  223. static void swap_apps_accumulator(netdata_publish_swap_t *out)
  224. {
  225. int i, end = (running_on_kernel >= NETDATA_KERNEL_V4_15) ? ebpf_nprocs : 1;
  226. netdata_publish_swap_t *total = &out[0];
  227. for (i = 1; i < end; i++) {
  228. netdata_publish_swap_t *w = &out[i];
  229. total->write += w->write;
  230. total->read += w->read;
  231. }
  232. }
  233. /**
  234. * Fill PID
  235. *
  236. * Fill PID structures
  237. *
  238. * @param current_pid pid that we are collecting data
  239. * @param out values read from hash tables;
  240. */
  241. static void swap_fill_pid(uint32_t current_pid, netdata_publish_swap_t *publish)
  242. {
  243. netdata_publish_swap_t *curr = swap_pid[current_pid];
  244. if (!curr) {
  245. curr = callocz(1, sizeof(netdata_publish_swap_t));
  246. swap_pid[current_pid] = curr;
  247. }
  248. memcpy(curr, publish, sizeof(netdata_publish_swap_t));
  249. }
  250. /**
  251. * Update cgroup
  252. *
  253. * Update cgroup data based in
  254. */
  255. static void ebpf_update_swap_cgroup()
  256. {
  257. ebpf_cgroup_target_t *ect ;
  258. netdata_publish_swap_t *cv = swap_vector;
  259. int fd = swap_maps[NETDATA_PID_SWAP_TABLE].map_fd;
  260. size_t length = sizeof(netdata_publish_swap_t)*ebpf_nprocs;
  261. pthread_mutex_lock(&mutex_cgroup_shm);
  262. for (ect = ebpf_cgroup_pids; ect; ect = ect->next) {
  263. struct pid_on_target2 *pids;
  264. for (pids = ect->pids; pids; pids = pids->next) {
  265. int pid = pids->pid;
  266. netdata_publish_swap_t *out = &pids->swap;
  267. if (likely(swap_pid) && swap_pid[pid]) {
  268. netdata_publish_swap_t *in = swap_pid[pid];
  269. memcpy(out, in, sizeof(netdata_publish_swap_t));
  270. } else {
  271. memset(cv, 0, length);
  272. if (!bpf_map_lookup_elem(fd, &pid, cv)) {
  273. swap_apps_accumulator(cv);
  274. memcpy(out, cv, sizeof(netdata_publish_swap_t));
  275. }
  276. }
  277. }
  278. }
  279. pthread_mutex_unlock(&mutex_cgroup_shm);
  280. }
  281. /**
  282. * Read APPS table
  283. *
  284. * Read the apps table and store data inside the structure.
  285. */
  286. static void read_apps_table()
  287. {
  288. netdata_publish_swap_t *cv = swap_vector;
  289. uint32_t key;
  290. struct pid_stat *pids = root_of_pids;
  291. int fd = swap_maps[NETDATA_PID_SWAP_TABLE].map_fd;
  292. size_t length = sizeof(netdata_publish_swap_t)*ebpf_nprocs;
  293. while (pids) {
  294. key = pids->pid;
  295. if (bpf_map_lookup_elem(fd, &key, cv)) {
  296. pids = pids->next;
  297. continue;
  298. }
  299. swap_apps_accumulator(cv);
  300. swap_fill_pid(key, cv);
  301. // We are cleaning to avoid passing data read from one process to other.
  302. memset(cv, 0, length);
  303. pids = pids->next;
  304. }
  305. }
  306. /**
  307. * Send global
  308. *
  309. * Send global charts to Netdata
  310. */
  311. static void swap_send_global()
  312. {
  313. write_io_chart(NETDATA_MEM_SWAP_CHART, NETDATA_EBPF_SYSTEM_GROUP,
  314. swap_publish_aggregated[NETDATA_KEY_SWAP_WRITEPAGE_CALL].dimension,
  315. (long long) swap_hash_values[NETDATA_KEY_SWAP_WRITEPAGE_CALL],
  316. swap_publish_aggregated[NETDATA_KEY_SWAP_READPAGE_CALL].dimension,
  317. (long long) swap_hash_values[NETDATA_KEY_SWAP_READPAGE_CALL]);
  318. }
  319. /**
  320. * Read global counter
  321. *
  322. * Read the table with number of calls to all functions
  323. */
  324. static void read_global_table()
  325. {
  326. netdata_idx_t *stored = swap_values;
  327. netdata_idx_t *val = swap_hash_values;
  328. int fd = swap_maps[NETDATA_SWAP_GLOBAL_TABLE].map_fd;
  329. uint32_t i, end = NETDATA_SWAP_END;
  330. for (i = NETDATA_KEY_SWAP_READPAGE_CALL; i < end; i++) {
  331. if (!bpf_map_lookup_elem(fd, &i, stored)) {
  332. int j;
  333. int last = ebpf_nprocs;
  334. netdata_idx_t total = 0;
  335. for (j = 0; j < last; j++)
  336. total += stored[j];
  337. val[i] = total;
  338. }
  339. }
  340. }
  341. /**
  342. * Swap read hash
  343. *
  344. * This is the thread callback.
  345. *
  346. * @param ptr It is a NULL value for this thread.
  347. *
  348. * @return It always returns NULL.
  349. */
  350. void *ebpf_swap_read_hash(void *ptr)
  351. {
  352. read_thread_closed = 0;
  353. heartbeat_t hb;
  354. heartbeat_init(&hb);
  355. ebpf_module_t *em = (ebpf_module_t *)ptr;
  356. usec_t step = NETDATA_SWAP_SLEEP_MS * em->update_every;
  357. while (!close_ebpf_plugin) {
  358. usec_t dt = heartbeat_next(&hb, step);
  359. (void)dt;
  360. read_global_table();
  361. }
  362. read_thread_closed = 1;
  363. return NULL;
  364. }
  365. /**
  366. * Sum PIDs
  367. *
  368. * Sum values for all targets.
  369. *
  370. * @param swap
  371. * @param root
  372. */
  373. static void ebpf_swap_sum_pids(netdata_publish_swap_t *swap, struct pid_on_target *root)
  374. {
  375. uint64_t local_read = 0;
  376. uint64_t local_write = 0;
  377. while (root) {
  378. int32_t pid = root->pid;
  379. netdata_publish_swap_t *w = swap_pid[pid];
  380. if (w) {
  381. local_write += w->write;
  382. local_read += w->read;
  383. }
  384. root = root->next;
  385. }
  386. // These conditions were added, because we are using incremental algorithm
  387. swap->write = (local_write >= swap->write) ? local_write : swap->write;
  388. swap->read = (local_read >= swap->read) ? local_read : swap->read;
  389. }
  390. /**
  391. * Send data to Netdata calling auxiliary functions.
  392. *
  393. * @param root the target list.
  394. */
  395. void ebpf_swap_send_apps_data(struct target *root)
  396. {
  397. struct target *w;
  398. for (w = root; w; w = w->next) {
  399. if (unlikely(w->exposed && w->processes)) {
  400. ebpf_swap_sum_pids(&w->swap, w->root_pid);
  401. }
  402. }
  403. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_MEM_SWAP_READ_CHART);
  404. for (w = root; w; w = w->next) {
  405. if (unlikely(w->exposed && w->processes)) {
  406. write_chart_dimension(w->name, (long long) w->swap.read);
  407. }
  408. }
  409. write_end_chart();
  410. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_MEM_SWAP_WRITE_CHART);
  411. for (w = root; w; w = w->next) {
  412. if (unlikely(w->exposed && w->processes)) {
  413. write_chart_dimension(w->name, (long long) w->swap.write);
  414. }
  415. }
  416. write_end_chart();
  417. }
  418. /**
  419. * Sum PIDs
  420. *
  421. * Sum values for all targets.
  422. *
  423. * @param swap
  424. * @param root
  425. */
  426. static void ebpf_swap_sum_cgroup_pids(netdata_publish_swap_t *swap, struct pid_on_target2 *pids)
  427. {
  428. uint64_t local_read = 0;
  429. uint64_t local_write = 0;
  430. while (pids) {
  431. netdata_publish_swap_t *w = &pids->swap;
  432. local_write += w->write;
  433. local_read += w->read;
  434. pids = pids->next;
  435. }
  436. // These conditions were added, because we are using incremental algorithm
  437. swap->write = (local_write >= swap->write) ? local_write : swap->write;
  438. swap->read = (local_read >= swap->read) ? local_read : swap->read;
  439. }
  440. /**
  441. * Send Systemd charts
  442. *
  443. * Send collected data to Netdata.
  444. *
  445. * @return It returns the status for chart creation, if it is necessary to remove a specific dimension, zero is returned
  446. * otherwise function returns 1 to avoid chart recreation
  447. */
  448. static int ebpf_send_systemd_swap_charts()
  449. {
  450. int ret = 1;
  451. ebpf_cgroup_target_t *ect;
  452. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_MEM_SWAP_READ_CHART);
  453. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  454. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  455. write_chart_dimension(ect->name, (long long) ect->publish_systemd_swap.read);
  456. } else
  457. ret = 0;
  458. }
  459. write_end_chart();
  460. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_MEM_SWAP_WRITE_CHART);
  461. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  462. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  463. write_chart_dimension(ect->name, (long long) ect->publish_systemd_swap.write);
  464. }
  465. }
  466. write_end_chart();
  467. return ret;
  468. }
  469. /**
  470. * Create specific swap charts
  471. *
  472. * Create charts for cgroup/application.
  473. *
  474. * @param type the chart type.
  475. * @param update_every value to overwrite the update frequency set by the server.
  476. */
  477. static void ebpf_create_specific_swap_charts(char *type, int update_every)
  478. {
  479. ebpf_create_chart(type, NETDATA_MEM_SWAP_READ_CHART,
  480. "Calls to function <code>swap_readpage</code>.",
  481. EBPF_COMMON_DIMENSION_CALL, NETDATA_SYSTEM_CGROUP_SWAP_SUBMENU,
  482. NETDATA_CGROUP_SWAP_READ_CONTEXT, NETDATA_EBPF_CHART_TYPE_LINE,
  483. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5100,
  484. ebpf_create_global_dimension,
  485. swap_publish_aggregated, 1, update_every, NETDATA_EBPF_MODULE_NAME_SWAP);
  486. ebpf_create_chart(type, NETDATA_MEM_SWAP_WRITE_CHART,
  487. "Calls to function <code>swap_writepage</code>.",
  488. EBPF_COMMON_DIMENSION_CALL, NETDATA_SYSTEM_CGROUP_SWAP_SUBMENU,
  489. NETDATA_CGROUP_SWAP_WRITE_CONTEXT, NETDATA_EBPF_CHART_TYPE_LINE,
  490. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5101,
  491. ebpf_create_global_dimension,
  492. &swap_publish_aggregated[NETDATA_KEY_SWAP_WRITEPAGE_CALL], 1,
  493. update_every, NETDATA_EBPF_MODULE_NAME_SWAP);
  494. }
  495. /**
  496. * Create specific swap charts
  497. *
  498. * Create charts for cgroup/application.
  499. *
  500. * @param type the chart type.
  501. * @param update_every value to overwrite the update frequency set by the server.
  502. */
  503. static void ebpf_obsolete_specific_swap_charts(char *type, int update_every)
  504. {
  505. ebpf_write_chart_obsolete(type, NETDATA_MEM_SWAP_READ_CHART,"Calls to function <code>swap_readpage</code>.",
  506. EBPF_COMMON_DIMENSION_CALL, NETDATA_SYSTEM_CGROUP_SWAP_SUBMENU,
  507. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_CGROUP_SWAP_READ_CONTEXT,
  508. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5100, update_every);
  509. ebpf_write_chart_obsolete(type, NETDATA_MEM_SWAP_WRITE_CHART, "Calls to function <code>swap_writepage</code>.",
  510. EBPF_COMMON_DIMENSION_CALL, NETDATA_SYSTEM_CGROUP_SWAP_SUBMENU,
  511. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_CGROUP_SWAP_WRITE_CONTEXT,
  512. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5101, update_every);
  513. }
  514. /*
  515. * Send Specific Swap data
  516. *
  517. * Send data for specific cgroup/apps.
  518. *
  519. * @param type chart type
  520. * @param values structure with values that will be sent to netdata
  521. */
  522. static void ebpf_send_specific_swap_data(char *type, netdata_publish_swap_t *values)
  523. {
  524. write_begin_chart(type, NETDATA_MEM_SWAP_READ_CHART);
  525. write_chart_dimension(swap_publish_aggregated[NETDATA_KEY_SWAP_READPAGE_CALL].name, (long long) values->read);
  526. write_end_chart();
  527. write_begin_chart(type, NETDATA_MEM_SWAP_WRITE_CHART);
  528. write_chart_dimension(swap_publish_aggregated[NETDATA_KEY_SWAP_WRITEPAGE_CALL].name, (long long) values->write);
  529. write_end_chart();
  530. }
  531. /**
  532. * Create Systemd Swap Charts
  533. *
  534. * Create charts when systemd is enabled
  535. *
  536. * @param update_every value to overwrite the update frequency set by the server.
  537. **/
  538. static void ebpf_create_systemd_swap_charts(int update_every)
  539. {
  540. ebpf_create_charts_on_systemd(NETDATA_MEM_SWAP_READ_CHART,
  541. "Calls to <code>swap_readpage</code>.",
  542. EBPF_COMMON_DIMENSION_CALL, NETDATA_SYSTEM_CGROUP_SWAP_SUBMENU,
  543. NETDATA_EBPF_CHART_TYPE_STACKED, 20191,
  544. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX], NETDATA_SYSTEMD_SWAP_READ_CONTEXT,
  545. NETDATA_EBPF_MODULE_NAME_SWAP, update_every);
  546. ebpf_create_charts_on_systemd(NETDATA_MEM_SWAP_WRITE_CHART,
  547. "Calls to function <code>swap_writepage</code>.",
  548. EBPF_COMMON_DIMENSION_CALL, NETDATA_SYSTEM_CGROUP_SWAP_SUBMENU,
  549. NETDATA_EBPF_CHART_TYPE_STACKED, 20192,
  550. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX], NETDATA_SYSTEMD_SWAP_WRITE_CONTEXT,
  551. NETDATA_EBPF_MODULE_NAME_SWAP, update_every);
  552. }
  553. /**
  554. * Send data to Netdata calling auxiliary functions.
  555. *
  556. * @param update_every value to overwrite the update frequency set by the server.
  557. */
  558. void ebpf_swap_send_cgroup_data(int update_every)
  559. {
  560. if (!ebpf_cgroup_pids)
  561. return;
  562. pthread_mutex_lock(&mutex_cgroup_shm);
  563. ebpf_cgroup_target_t *ect;
  564. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  565. ebpf_swap_sum_cgroup_pids(&ect->publish_systemd_swap, ect->pids);
  566. }
  567. int has_systemd = shm_ebpf_cgroup.header->systemd_enabled;
  568. if (has_systemd) {
  569. static int systemd_charts = 0;
  570. if (!systemd_charts) {
  571. ebpf_create_systemd_swap_charts(update_every);
  572. systemd_charts = 1;
  573. fflush(stdout);
  574. }
  575. systemd_charts = ebpf_send_systemd_swap_charts();
  576. }
  577. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  578. if (ect->systemd)
  579. continue;
  580. if (!(ect->flags & NETDATA_EBPF_CGROUP_HAS_SWAP_CHART) && ect->updated) {
  581. ebpf_create_specific_swap_charts(ect->name, update_every);
  582. ect->flags |= NETDATA_EBPF_CGROUP_HAS_SWAP_CHART;
  583. }
  584. if (ect->flags & NETDATA_EBPF_CGROUP_HAS_SWAP_CHART) {
  585. if (ect->updated) {
  586. ebpf_send_specific_swap_data(ect->name, &ect->publish_systemd_swap);
  587. } else {
  588. ebpf_obsolete_specific_swap_charts(ect->name, update_every);
  589. ect->flags &= ~NETDATA_EBPF_CGROUP_HAS_SWAP_CHART;
  590. }
  591. }
  592. }
  593. pthread_mutex_unlock(&mutex_cgroup_shm);
  594. }
  595. /**
  596. * Main loop for this collector.
  597. */
  598. static void swap_collector(ebpf_module_t *em)
  599. {
  600. swap_threads.thread = mallocz(sizeof(netdata_thread_t));
  601. swap_threads.start_routine = ebpf_swap_read_hash;
  602. netdata_thread_create(swap_threads.thread, swap_threads.name, NETDATA_THREAD_OPTION_JOINABLE,
  603. ebpf_swap_read_hash, em);
  604. int apps = em->apps_charts;
  605. int cgroup = em->cgroup_charts;
  606. int update_every = em->update_every;
  607. int counter = update_every - 1;
  608. while (!close_ebpf_plugin) {
  609. pthread_mutex_lock(&collect_data_mutex);
  610. pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
  611. if (++counter == update_every) {
  612. counter = 0;
  613. if (apps)
  614. read_apps_table();
  615. if (cgroup)
  616. ebpf_update_swap_cgroup();
  617. pthread_mutex_lock(&lock);
  618. swap_send_global();
  619. if (apps)
  620. ebpf_swap_send_apps_data(apps_groups_root_target);
  621. if (cgroup)
  622. ebpf_swap_send_cgroup_data(update_every);
  623. pthread_mutex_unlock(&lock);
  624. }
  625. pthread_mutex_unlock(&collect_data_mutex);
  626. }
  627. }
  628. /*****************************************************************
  629. *
  630. * INITIALIZE THREAD
  631. *
  632. *****************************************************************/
  633. /**
  634. * Create apps charts
  635. *
  636. * Call ebpf_create_chart to create the charts on apps submenu.
  637. *
  638. * @param em a pointer to the structure with the default values.
  639. */
  640. void ebpf_swap_create_apps_charts(struct ebpf_module *em, void *ptr)
  641. {
  642. struct target *root = ptr;
  643. ebpf_create_charts_on_apps(NETDATA_MEM_SWAP_READ_CHART,
  644. "Calls to function <code>swap_readpage</code>.",
  645. EBPF_COMMON_DIMENSION_CALL,
  646. NETDATA_SWAP_SUBMENU,
  647. NETDATA_EBPF_CHART_TYPE_STACKED,
  648. 20191,
  649. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  650. root, em->update_every, NETDATA_EBPF_MODULE_NAME_SWAP);
  651. ebpf_create_charts_on_apps(NETDATA_MEM_SWAP_WRITE_CHART,
  652. "Calls to function <code>swap_writepage</code>.",
  653. EBPF_COMMON_DIMENSION_CALL,
  654. NETDATA_SWAP_SUBMENU,
  655. NETDATA_EBPF_CHART_TYPE_STACKED,
  656. 20192,
  657. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  658. root, em->update_every, NETDATA_EBPF_MODULE_NAME_SWAP);
  659. }
  660. /**
  661. * Allocate vectors used with this thread.
  662. *
  663. * We are not testing the return, because callocz does this and shutdown the software
  664. * case it was not possible to allocate.
  665. *
  666. * @param apps is apps enabled?
  667. */
  668. static void ebpf_swap_allocate_global_vectors(int apps)
  669. {
  670. if (apps)
  671. swap_pid = callocz((size_t)pid_max, sizeof(netdata_publish_swap_t *));
  672. swap_vector = callocz((size_t)ebpf_nprocs, sizeof(netdata_publish_swap_t));
  673. swap_values = callocz((size_t)ebpf_nprocs, sizeof(netdata_idx_t));
  674. memset(swap_hash_values, 0, sizeof(swap_hash_values));
  675. }
  676. /*****************************************************************
  677. *
  678. * MAIN THREAD
  679. *
  680. *****************************************************************/
  681. /**
  682. * Create global charts
  683. *
  684. * Call ebpf_create_chart to create the charts for the collector.
  685. *
  686. * @param update_every value to overwrite the update frequency set by the server.
  687. */
  688. static void ebpf_create_swap_charts(int update_every)
  689. {
  690. ebpf_create_chart(NETDATA_EBPF_SYSTEM_GROUP, NETDATA_MEM_SWAP_CHART,
  691. "Calls to access swap memory",
  692. EBPF_COMMON_DIMENSION_CALL, NETDATA_SYSTEM_SWAP_SUBMENU,
  693. NULL,
  694. NETDATA_EBPF_CHART_TYPE_LINE,
  695. 202,
  696. ebpf_create_global_dimension,
  697. swap_publish_aggregated, NETDATA_SWAP_END,
  698. update_every, NETDATA_EBPF_MODULE_NAME_SWAP);
  699. }
  700. /*
  701. * Load BPF
  702. *
  703. * Load BPF files.
  704. *
  705. * @param em the structure with configuration
  706. */
  707. static int ebpf_swap_load_bpf(ebpf_module_t *em)
  708. {
  709. int ret = 0;
  710. if (em->load == EBPF_LOAD_LEGACY) {
  711. probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &objects);
  712. if (!probe_links) {
  713. ret = -1;
  714. }
  715. }
  716. #ifdef LIBBPF_MAJOR_VERSION
  717. else {
  718. bpf_obj = swap_bpf__open();
  719. if (!bpf_obj)
  720. ret = -1;
  721. else
  722. ret = ebpf_swap_load_and_attach(bpf_obj, em);
  723. }
  724. #endif
  725. if (ret)
  726. error("%s %s", EBPF_DEFAULT_ERROR_MSG, em->thread_name);
  727. return ret;
  728. }
  729. /**
  730. * SWAP thread
  731. *
  732. * Thread used to make swap thread
  733. *
  734. * @param ptr a pointer to `struct ebpf_module`
  735. *
  736. * @return It always return NULL
  737. */
  738. void *ebpf_swap_thread(void *ptr)
  739. {
  740. netdata_thread_cleanup_push(ebpf_swap_cleanup, ptr);
  741. ebpf_module_t *em = (ebpf_module_t *)ptr;
  742. em->maps = swap_maps;
  743. ebpf_update_pid_table(&swap_maps[NETDATA_PID_SWAP_TABLE], em);
  744. if (!em->enabled)
  745. goto endswap;
  746. #ifdef LIBBPF_MAJOR_VERSION
  747. ebpf_adjust_thread_load(em, default_btf);
  748. #endif
  749. if (ebpf_swap_load_bpf(em)) {
  750. em->enabled = CONFIG_BOOLEAN_NO;
  751. goto endswap;
  752. }
  753. ebpf_swap_allocate_global_vectors(em->apps_charts);
  754. int algorithms[NETDATA_SWAP_END] = { NETDATA_EBPF_INCREMENTAL_IDX, NETDATA_EBPF_INCREMENTAL_IDX };
  755. ebpf_global_labels(swap_aggregated_data, swap_publish_aggregated, swap_dimension_name, swap_dimension_name,
  756. algorithms, NETDATA_SWAP_END);
  757. pthread_mutex_lock(&lock);
  758. ebpf_create_swap_charts(em->update_every);
  759. ebpf_update_stats(&plugin_statistics, em);
  760. pthread_mutex_unlock(&lock);
  761. swap_collector(em);
  762. endswap:
  763. if (!em->enabled)
  764. ebpf_update_disabled_plugin_stats(em);
  765. netdata_thread_cleanup_pop(1);
  766. return NULL;
  767. }