ebpf_swap.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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 up the main thread.
  169. *
  170. * @param ptr thread data.
  171. */
  172. static void ebpf_swap_cleanup(void *ptr)
  173. {
  174. ebpf_module_t *em = (ebpf_module_t *)ptr;
  175. if (!em->enabled)
  176. return;
  177. heartbeat_t hb;
  178. heartbeat_init(&hb);
  179. uint32_t tick = 2 * USEC_PER_MS;
  180. while (!read_thread_closed) {
  181. usec_t dt = heartbeat_next(&hb, tick);
  182. UNUSED(dt);
  183. }
  184. ebpf_cleanup_publish_syscall(swap_publish_aggregated);
  185. freez(swap_vector);
  186. freez(swap_values);
  187. if (probe_links) {
  188. struct bpf_program *prog;
  189. size_t i = 0 ;
  190. bpf_object__for_each_program(prog, objects) {
  191. bpf_link__destroy(probe_links[i]);
  192. i++;
  193. }
  194. if (objects)
  195. bpf_object__close(objects);
  196. }
  197. #ifdef LIBBPF_MAJOR_VERSION
  198. else if (bpf_obj)
  199. swap_bpf__destroy(bpf_obj);
  200. #endif
  201. }
  202. /*****************************************************************
  203. *
  204. * COLLECTOR THREAD
  205. *
  206. *****************************************************************/
  207. /**
  208. * Apps Accumulator
  209. *
  210. * Sum all values read from kernel and store in the first address.
  211. *
  212. * @param out the vector with read values.
  213. */
  214. static void swap_apps_accumulator(netdata_publish_swap_t *out)
  215. {
  216. int i, end = (running_on_kernel >= NETDATA_KERNEL_V4_15) ? ebpf_nprocs : 1;
  217. netdata_publish_swap_t *total = &out[0];
  218. for (i = 1; i < end; i++) {
  219. netdata_publish_swap_t *w = &out[i];
  220. total->write += w->write;
  221. total->read += w->read;
  222. }
  223. }
  224. /**
  225. * Fill PID
  226. *
  227. * Fill PID structures
  228. *
  229. * @param current_pid pid that we are collecting data
  230. * @param out values read from hash tables;
  231. */
  232. static void swap_fill_pid(uint32_t current_pid, netdata_publish_swap_t *publish)
  233. {
  234. netdata_publish_swap_t *curr = swap_pid[current_pid];
  235. if (!curr) {
  236. curr = callocz(1, sizeof(netdata_publish_swap_t));
  237. swap_pid[current_pid] = curr;
  238. }
  239. memcpy(curr, publish, sizeof(netdata_publish_swap_t));
  240. }
  241. /**
  242. * Update cgroup
  243. *
  244. * Update cgroup data based in
  245. */
  246. static void ebpf_update_swap_cgroup()
  247. {
  248. ebpf_cgroup_target_t *ect ;
  249. netdata_publish_swap_t *cv = swap_vector;
  250. int fd = swap_maps[NETDATA_PID_SWAP_TABLE].map_fd;
  251. size_t length = sizeof(netdata_publish_swap_t)*ebpf_nprocs;
  252. pthread_mutex_lock(&mutex_cgroup_shm);
  253. for (ect = ebpf_cgroup_pids; ect; ect = ect->next) {
  254. struct pid_on_target2 *pids;
  255. for (pids = ect->pids; pids; pids = pids->next) {
  256. int pid = pids->pid;
  257. netdata_publish_swap_t *out = &pids->swap;
  258. if (likely(swap_pid) && swap_pid[pid]) {
  259. netdata_publish_swap_t *in = swap_pid[pid];
  260. memcpy(out, in, sizeof(netdata_publish_swap_t));
  261. } else {
  262. memset(cv, 0, length);
  263. if (!bpf_map_lookup_elem(fd, &pid, cv)) {
  264. swap_apps_accumulator(cv);
  265. memcpy(out, cv, sizeof(netdata_publish_swap_t));
  266. }
  267. }
  268. }
  269. }
  270. pthread_mutex_unlock(&mutex_cgroup_shm);
  271. }
  272. /**
  273. * Read APPS table
  274. *
  275. * Read the apps table and store data inside the structure.
  276. */
  277. static void read_apps_table()
  278. {
  279. netdata_publish_swap_t *cv = swap_vector;
  280. uint32_t key;
  281. struct pid_stat *pids = root_of_pids;
  282. int fd = swap_maps[NETDATA_PID_SWAP_TABLE].map_fd;
  283. size_t length = sizeof(netdata_publish_swap_t)*ebpf_nprocs;
  284. while (pids) {
  285. key = pids->pid;
  286. if (bpf_map_lookup_elem(fd, &key, cv)) {
  287. pids = pids->next;
  288. continue;
  289. }
  290. swap_apps_accumulator(cv);
  291. swap_fill_pid(key, cv);
  292. // We are cleaning to avoid passing data read from one process to other.
  293. memset(cv, 0, length);
  294. pids = pids->next;
  295. }
  296. }
  297. /**
  298. * Send global
  299. *
  300. * Send global charts to Netdata
  301. */
  302. static void swap_send_global()
  303. {
  304. write_io_chart(NETDATA_MEM_SWAP_CHART, NETDATA_EBPF_SYSTEM_GROUP,
  305. swap_publish_aggregated[NETDATA_KEY_SWAP_WRITEPAGE_CALL].dimension,
  306. (long long) swap_hash_values[NETDATA_KEY_SWAP_WRITEPAGE_CALL],
  307. swap_publish_aggregated[NETDATA_KEY_SWAP_READPAGE_CALL].dimension,
  308. (long long) swap_hash_values[NETDATA_KEY_SWAP_READPAGE_CALL]);
  309. }
  310. /**
  311. * Read global counter
  312. *
  313. * Read the table with number of calls to all functions
  314. */
  315. static void read_global_table()
  316. {
  317. netdata_idx_t *stored = swap_values;
  318. netdata_idx_t *val = swap_hash_values;
  319. int fd = swap_maps[NETDATA_SWAP_GLOBAL_TABLE].map_fd;
  320. uint32_t i, end = NETDATA_SWAP_END;
  321. for (i = NETDATA_KEY_SWAP_READPAGE_CALL; i < end; i++) {
  322. if (!bpf_map_lookup_elem(fd, &i, stored)) {
  323. int j;
  324. int last = ebpf_nprocs;
  325. netdata_idx_t total = 0;
  326. for (j = 0; j < last; j++)
  327. total += stored[j];
  328. val[i] = total;
  329. }
  330. }
  331. }
  332. /**
  333. * Swap read hash
  334. *
  335. * This is the thread callback.
  336. *
  337. * @param ptr It is a NULL value for this thread.
  338. *
  339. * @return It always returns NULL.
  340. */
  341. void *ebpf_swap_read_hash(void *ptr)
  342. {
  343. read_thread_closed = 0;
  344. heartbeat_t hb;
  345. heartbeat_init(&hb);
  346. ebpf_module_t *em = (ebpf_module_t *)ptr;
  347. usec_t step = NETDATA_SWAP_SLEEP_MS * em->update_every;
  348. while (!close_ebpf_plugin) {
  349. usec_t dt = heartbeat_next(&hb, step);
  350. (void)dt;
  351. read_global_table();
  352. }
  353. read_thread_closed = 1;
  354. return NULL;
  355. }
  356. /**
  357. * Sum PIDs
  358. *
  359. * Sum values for all targets.
  360. *
  361. * @param swap
  362. * @param root
  363. */
  364. static void ebpf_swap_sum_pids(netdata_publish_swap_t *swap, struct pid_on_target *root)
  365. {
  366. uint64_t local_read = 0;
  367. uint64_t local_write = 0;
  368. while (root) {
  369. int32_t pid = root->pid;
  370. netdata_publish_swap_t *w = swap_pid[pid];
  371. if (w) {
  372. local_write += w->write;
  373. local_read += w->read;
  374. }
  375. root = root->next;
  376. }
  377. // These conditions were added, because we are using incremental algorithm
  378. swap->write = (local_write >= swap->write) ? local_write : swap->write;
  379. swap->read = (local_read >= swap->read) ? local_read : swap->read;
  380. }
  381. /**
  382. * Send data to Netdata calling auxiliary functions.
  383. *
  384. * @param root the target list.
  385. */
  386. void ebpf_swap_send_apps_data(struct target *root)
  387. {
  388. struct target *w;
  389. for (w = root; w; w = w->next) {
  390. if (unlikely(w->exposed && w->processes)) {
  391. ebpf_swap_sum_pids(&w->swap, w->root_pid);
  392. }
  393. }
  394. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_MEM_SWAP_READ_CHART);
  395. for (w = root; w; w = w->next) {
  396. if (unlikely(w->exposed && w->processes)) {
  397. write_chart_dimension(w->name, (long long) w->swap.read);
  398. }
  399. }
  400. write_end_chart();
  401. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_MEM_SWAP_WRITE_CHART);
  402. for (w = root; w; w = w->next) {
  403. if (unlikely(w->exposed && w->processes)) {
  404. write_chart_dimension(w->name, (long long) w->swap.write);
  405. }
  406. }
  407. write_end_chart();
  408. }
  409. /**
  410. * Sum PIDs
  411. *
  412. * Sum values for all targets.
  413. *
  414. * @param swap
  415. * @param root
  416. */
  417. static void ebpf_swap_sum_cgroup_pids(netdata_publish_swap_t *swap, struct pid_on_target2 *pids)
  418. {
  419. uint64_t local_read = 0;
  420. uint64_t local_write = 0;
  421. while (pids) {
  422. netdata_publish_swap_t *w = &pids->swap;
  423. local_write += w->write;
  424. local_read += w->read;
  425. pids = pids->next;
  426. }
  427. // These conditions were added, because we are using incremental algorithm
  428. swap->write = (local_write >= swap->write) ? local_write : swap->write;
  429. swap->read = (local_read >= swap->read) ? local_read : swap->read;
  430. }
  431. /**
  432. * Send Systemd charts
  433. *
  434. * Send collected data to Netdata.
  435. *
  436. * @return It returns the status for chart creation, if it is necessary to remove a specific dimension, zero is returned
  437. * otherwise function returns 1 to avoid chart recreation
  438. */
  439. static int ebpf_send_systemd_swap_charts()
  440. {
  441. int ret = 1;
  442. ebpf_cgroup_target_t *ect;
  443. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_MEM_SWAP_READ_CHART);
  444. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  445. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  446. write_chart_dimension(ect->name, (long long) ect->publish_systemd_swap.read);
  447. } else
  448. ret = 0;
  449. }
  450. write_end_chart();
  451. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_MEM_SWAP_WRITE_CHART);
  452. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  453. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  454. write_chart_dimension(ect->name, (long long) ect->publish_systemd_swap.write);
  455. }
  456. }
  457. write_end_chart();
  458. return ret;
  459. }
  460. /**
  461. * Create specific swap charts
  462. *
  463. * Create charts for cgroup/application.
  464. *
  465. * @param type the chart type.
  466. * @param update_every value to overwrite the update frequency set by the server.
  467. */
  468. static void ebpf_create_specific_swap_charts(char *type, int update_every)
  469. {
  470. ebpf_create_chart(type, NETDATA_MEM_SWAP_READ_CHART,
  471. "Calls to function <code>swap_readpage</code>.",
  472. EBPF_COMMON_DIMENSION_CALL, NETDATA_SYSTEM_CGROUP_SWAP_SUBMENU,
  473. NETDATA_CGROUP_SWAP_READ_CONTEXT, NETDATA_EBPF_CHART_TYPE_LINE,
  474. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5100,
  475. ebpf_create_global_dimension,
  476. swap_publish_aggregated, 1, update_every, NETDATA_EBPF_MODULE_NAME_SWAP);
  477. ebpf_create_chart(type, NETDATA_MEM_SWAP_WRITE_CHART,
  478. "Calls to function <code>swap_writepage</code>.",
  479. EBPF_COMMON_DIMENSION_CALL, NETDATA_SYSTEM_CGROUP_SWAP_SUBMENU,
  480. NETDATA_CGROUP_SWAP_WRITE_CONTEXT, NETDATA_EBPF_CHART_TYPE_LINE,
  481. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5101,
  482. ebpf_create_global_dimension,
  483. &swap_publish_aggregated[NETDATA_KEY_SWAP_WRITEPAGE_CALL], 1,
  484. update_every, NETDATA_EBPF_MODULE_NAME_SWAP);
  485. }
  486. /**
  487. * Create specific swap charts
  488. *
  489. * Create charts for cgroup/application.
  490. *
  491. * @param type the chart type.
  492. * @param update_every value to overwrite the update frequency set by the server.
  493. */
  494. static void ebpf_obsolete_specific_swap_charts(char *type, int update_every)
  495. {
  496. ebpf_write_chart_obsolete(type, NETDATA_MEM_SWAP_READ_CHART,"Calls to function <code>swap_readpage</code>.",
  497. EBPF_COMMON_DIMENSION_CALL, NETDATA_SYSTEM_CGROUP_SWAP_SUBMENU,
  498. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_CGROUP_SWAP_READ_CONTEXT,
  499. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5100, update_every);
  500. ebpf_write_chart_obsolete(type, NETDATA_MEM_SWAP_WRITE_CHART, "Calls to function <code>swap_writepage</code>.",
  501. EBPF_COMMON_DIMENSION_CALL, NETDATA_SYSTEM_CGROUP_SWAP_SUBMENU,
  502. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_CGROUP_SWAP_WRITE_CONTEXT,
  503. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5101, update_every);
  504. }
  505. /*
  506. * Send Specific Swap data
  507. *
  508. * Send data for specific cgroup/apps.
  509. *
  510. * @param type chart type
  511. * @param values structure with values that will be sent to netdata
  512. */
  513. static void ebpf_send_specific_swap_data(char *type, netdata_publish_swap_t *values)
  514. {
  515. write_begin_chart(type, NETDATA_MEM_SWAP_READ_CHART);
  516. write_chart_dimension(swap_publish_aggregated[NETDATA_KEY_SWAP_READPAGE_CALL].name, (long long) values->read);
  517. write_end_chart();
  518. write_begin_chart(type, NETDATA_MEM_SWAP_WRITE_CHART);
  519. write_chart_dimension(swap_publish_aggregated[NETDATA_KEY_SWAP_WRITEPAGE_CALL].name, (long long) values->write);
  520. write_end_chart();
  521. }
  522. /**
  523. * Create Systemd Swap Charts
  524. *
  525. * Create charts when systemd is enabled
  526. *
  527. * @param update_every value to overwrite the update frequency set by the server.
  528. **/
  529. static void ebpf_create_systemd_swap_charts(int update_every)
  530. {
  531. ebpf_create_charts_on_systemd(NETDATA_MEM_SWAP_READ_CHART,
  532. "Calls to <code>swap_readpage</code>.",
  533. EBPF_COMMON_DIMENSION_CALL, NETDATA_SYSTEM_CGROUP_SWAP_SUBMENU,
  534. NETDATA_EBPF_CHART_TYPE_STACKED, 20191,
  535. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX], NETDATA_SYSTEMD_SWAP_READ_CONTEXT,
  536. NETDATA_EBPF_MODULE_NAME_SWAP, update_every);
  537. ebpf_create_charts_on_systemd(NETDATA_MEM_SWAP_WRITE_CHART,
  538. "Calls to function <code>swap_writepage</code>.",
  539. EBPF_COMMON_DIMENSION_CALL, NETDATA_SYSTEM_CGROUP_SWAP_SUBMENU,
  540. NETDATA_EBPF_CHART_TYPE_STACKED, 20192,
  541. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX], NETDATA_SYSTEMD_SWAP_WRITE_CONTEXT,
  542. NETDATA_EBPF_MODULE_NAME_SWAP, update_every);
  543. }
  544. /**
  545. * Send data to Netdata calling auxiliary functions.
  546. *
  547. * @param update_every value to overwrite the update frequency set by the server.
  548. */
  549. void ebpf_swap_send_cgroup_data(int update_every)
  550. {
  551. if (!ebpf_cgroup_pids)
  552. return;
  553. pthread_mutex_lock(&mutex_cgroup_shm);
  554. ebpf_cgroup_target_t *ect;
  555. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  556. ebpf_swap_sum_cgroup_pids(&ect->publish_systemd_swap, ect->pids);
  557. }
  558. int has_systemd = shm_ebpf_cgroup.header->systemd_enabled;
  559. if (has_systemd) {
  560. static int systemd_charts = 0;
  561. if (!systemd_charts) {
  562. ebpf_create_systemd_swap_charts(update_every);
  563. systemd_charts = 1;
  564. fflush(stdout);
  565. }
  566. systemd_charts = ebpf_send_systemd_swap_charts();
  567. }
  568. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  569. if (ect->systemd)
  570. continue;
  571. if (!(ect->flags & NETDATA_EBPF_CGROUP_HAS_SWAP_CHART) && ect->updated) {
  572. ebpf_create_specific_swap_charts(ect->name, update_every);
  573. ect->flags |= NETDATA_EBPF_CGROUP_HAS_SWAP_CHART;
  574. }
  575. if (ect->flags & NETDATA_EBPF_CGROUP_HAS_SWAP_CHART) {
  576. if (ect->updated) {
  577. ebpf_send_specific_swap_data(ect->name, &ect->publish_systemd_swap);
  578. } else {
  579. ebpf_obsolete_specific_swap_charts(ect->name, update_every);
  580. ect->flags &= ~NETDATA_EBPF_CGROUP_HAS_SWAP_CHART;
  581. }
  582. }
  583. }
  584. pthread_mutex_unlock(&mutex_cgroup_shm);
  585. }
  586. /**
  587. * Main loop for this collector.
  588. */
  589. static void swap_collector(ebpf_module_t *em)
  590. {
  591. swap_threads.thread = mallocz(sizeof(netdata_thread_t));
  592. swap_threads.start_routine = ebpf_swap_read_hash;
  593. netdata_thread_create(swap_threads.thread, swap_threads.name, NETDATA_THREAD_OPTION_JOINABLE,
  594. ebpf_swap_read_hash, em);
  595. int apps = em->apps_charts;
  596. int cgroup = em->cgroup_charts;
  597. int update_every = em->update_every;
  598. int counter = update_every - 1;
  599. while (!close_ebpf_plugin) {
  600. pthread_mutex_lock(&collect_data_mutex);
  601. pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
  602. if (++counter == update_every) {
  603. counter = 0;
  604. if (apps)
  605. read_apps_table();
  606. if (cgroup)
  607. ebpf_update_swap_cgroup();
  608. pthread_mutex_lock(&lock);
  609. swap_send_global();
  610. if (apps)
  611. ebpf_swap_send_apps_data(apps_groups_root_target);
  612. if (cgroup)
  613. ebpf_swap_send_cgroup_data(update_every);
  614. pthread_mutex_unlock(&lock);
  615. }
  616. pthread_mutex_unlock(&collect_data_mutex);
  617. }
  618. }
  619. /*****************************************************************
  620. *
  621. * INITIALIZE THREAD
  622. *
  623. *****************************************************************/
  624. /**
  625. * Create apps charts
  626. *
  627. * Call ebpf_create_chart to create the charts on apps submenu.
  628. *
  629. * @param em a pointer to the structure with the default values.
  630. */
  631. void ebpf_swap_create_apps_charts(struct ebpf_module *em, void *ptr)
  632. {
  633. struct target *root = ptr;
  634. ebpf_create_charts_on_apps(NETDATA_MEM_SWAP_READ_CHART,
  635. "Calls to function <code>swap_readpage</code>.",
  636. EBPF_COMMON_DIMENSION_CALL,
  637. NETDATA_SWAP_SUBMENU,
  638. NETDATA_EBPF_CHART_TYPE_STACKED,
  639. 20191,
  640. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  641. root, em->update_every, NETDATA_EBPF_MODULE_NAME_SWAP);
  642. ebpf_create_charts_on_apps(NETDATA_MEM_SWAP_WRITE_CHART,
  643. "Calls to function <code>swap_writepage</code>.",
  644. EBPF_COMMON_DIMENSION_CALL,
  645. NETDATA_SWAP_SUBMENU,
  646. NETDATA_EBPF_CHART_TYPE_STACKED,
  647. 20192,
  648. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  649. root, em->update_every, NETDATA_EBPF_MODULE_NAME_SWAP);
  650. }
  651. /**
  652. * Allocate vectors used with this thread.
  653. *
  654. * We are not testing the return, because callocz does this and shutdown the software
  655. * case it was not possible to allocate.
  656. *
  657. * @param apps is apps enabled?
  658. */
  659. static void ebpf_swap_allocate_global_vectors(int apps)
  660. {
  661. if (apps)
  662. swap_pid = callocz((size_t)pid_max, sizeof(netdata_publish_swap_t *));
  663. swap_vector = callocz((size_t)ebpf_nprocs, sizeof(netdata_publish_swap_t));
  664. swap_values = callocz((size_t)ebpf_nprocs, sizeof(netdata_idx_t));
  665. memset(swap_hash_values, 0, sizeof(swap_hash_values));
  666. }
  667. /*****************************************************************
  668. *
  669. * MAIN THREAD
  670. *
  671. *****************************************************************/
  672. /**
  673. * Create global charts
  674. *
  675. * Call ebpf_create_chart to create the charts for the collector.
  676. *
  677. * @param update_every value to overwrite the update frequency set by the server.
  678. */
  679. static void ebpf_create_swap_charts(int update_every)
  680. {
  681. ebpf_create_chart(NETDATA_EBPF_SYSTEM_GROUP, NETDATA_MEM_SWAP_CHART,
  682. "Calls to access swap memory",
  683. EBPF_COMMON_DIMENSION_CALL, NETDATA_SYSTEM_SWAP_SUBMENU,
  684. NULL,
  685. NETDATA_EBPF_CHART_TYPE_LINE,
  686. 202,
  687. ebpf_create_global_dimension,
  688. swap_publish_aggregated, NETDATA_SWAP_END,
  689. update_every, NETDATA_EBPF_MODULE_NAME_SWAP);
  690. }
  691. /*
  692. * Load BPF
  693. *
  694. * Load BPF files.
  695. *
  696. * @param em the structure with configuration
  697. */
  698. static int ebpf_swap_load_bpf(ebpf_module_t *em)
  699. {
  700. int ret = 0;
  701. if (em->load == EBPF_LOAD_LEGACY) {
  702. probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &objects);
  703. if (!probe_links) {
  704. ret = -1;
  705. }
  706. }
  707. #ifdef LIBBPF_MAJOR_VERSION
  708. else {
  709. bpf_obj = swap_bpf__open();
  710. if (!bpf_obj)
  711. ret = -1;
  712. else
  713. ret = ebpf_swap_load_and_attach(bpf_obj, em);
  714. }
  715. #endif
  716. if (ret)
  717. error("%s %s", EBPF_DEFAULT_ERROR_MSG, em->thread_name);
  718. return ret;
  719. }
  720. /**
  721. * SWAP thread
  722. *
  723. * Thread used to make swap thread
  724. *
  725. * @param ptr a pointer to `struct ebpf_module`
  726. *
  727. * @return It always return NULL
  728. */
  729. void *ebpf_swap_thread(void *ptr)
  730. {
  731. netdata_thread_cleanup_push(ebpf_swap_cleanup, ptr);
  732. ebpf_module_t *em = (ebpf_module_t *)ptr;
  733. em->maps = swap_maps;
  734. ebpf_update_pid_table(&swap_maps[NETDATA_PID_SWAP_TABLE], em);
  735. if (!em->enabled)
  736. goto endswap;
  737. #ifdef LIBBPF_MAJOR_VERSION
  738. ebpf_adjust_thread_load(em, default_btf);
  739. #endif
  740. if (ebpf_swap_load_bpf(em)) {
  741. em->enabled = CONFIG_BOOLEAN_NO;
  742. goto endswap;
  743. }
  744. ebpf_swap_allocate_global_vectors(em->apps_charts);
  745. int algorithms[NETDATA_SWAP_END] = { NETDATA_EBPF_INCREMENTAL_IDX, NETDATA_EBPF_INCREMENTAL_IDX };
  746. ebpf_global_labels(swap_aggregated_data, swap_publish_aggregated, swap_dimension_name, swap_dimension_name,
  747. algorithms, NETDATA_SWAP_END);
  748. pthread_mutex_lock(&lock);
  749. ebpf_create_swap_charts(em->update_every);
  750. ebpf_update_stats(&plugin_statistics, em);
  751. pthread_mutex_unlock(&lock);
  752. swap_collector(em);
  753. endswap:
  754. if (!em->enabled)
  755. ebpf_update_disabled_plugin_stats(em);
  756. netdata_thread_cleanup_pop(1);
  757. return NULL;
  758. }