ebpf_swap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. static ebpf_data_t swap_data;
  13. struct config swap_config = { .first_section = NULL,
  14. .last_section = NULL,
  15. .mutex = NETDATA_MUTEX_INITIALIZER,
  16. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  17. .rwlock = AVL_LOCK_INITIALIZER } };
  18. static ebpf_local_maps_t swap_maps[] = {{.name = "tbl_pid_swap", .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 = "swap_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 = "tbl_swap", .internal_input = NETDATA_SWAP_END,
  27. .user_input = 0,
  28. .type = NETDATA_EBPF_MAP_STATIC,
  29. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED},
  30. {.name = NULL, .internal_input = 0, .user_input = 0}};
  31. static struct bpf_link **probe_links = NULL;
  32. static struct bpf_object *objects = NULL;
  33. struct netdata_static_thread swap_threads = {"SWAP KERNEL", NULL, NULL, 1,
  34. NULL, NULL, NULL};
  35. /*****************************************************************
  36. *
  37. * FUNCTIONS TO CLOSE THE THREAD
  38. *
  39. *****************************************************************/
  40. /**
  41. * Clean swap structure
  42. */
  43. void clean_swap_pid_structures() {
  44. struct pid_stat *pids = root_of_pids;
  45. while (pids) {
  46. freez(swap_pid[pids->pid]);
  47. pids = pids->next;
  48. }
  49. }
  50. /**
  51. * Clean up the main thread.
  52. *
  53. * @param ptr thread data.
  54. */
  55. static void ebpf_swap_cleanup(void *ptr)
  56. {
  57. ebpf_module_t *em = (ebpf_module_t *)ptr;
  58. if (!em->enabled)
  59. return;
  60. heartbeat_t hb;
  61. heartbeat_init(&hb);
  62. uint32_t tick = 2 * USEC_PER_MS;
  63. while (!read_thread_closed) {
  64. usec_t dt = heartbeat_next(&hb, tick);
  65. UNUSED(dt);
  66. }
  67. ebpf_cleanup_publish_syscall(swap_publish_aggregated);
  68. freez(swap_vector);
  69. freez(swap_values);
  70. if (probe_links) {
  71. struct bpf_program *prog;
  72. size_t i = 0 ;
  73. bpf_object__for_each_program(prog, objects) {
  74. bpf_link__destroy(probe_links[i]);
  75. i++;
  76. }
  77. bpf_object__close(objects);
  78. }
  79. }
  80. /*****************************************************************
  81. *
  82. * COLLECTOR THREAD
  83. *
  84. *****************************************************************/
  85. /**
  86. * Apps Accumulator
  87. *
  88. * Sum all values read from kernel and store in the first address.
  89. *
  90. * @param out the vector with read values.
  91. */
  92. static void swap_apps_accumulator(netdata_publish_swap_t *out)
  93. {
  94. int i, end = (running_on_kernel >= NETDATA_KERNEL_V4_15) ? ebpf_nprocs : 1;
  95. netdata_publish_swap_t *total = &out[0];
  96. for (i = 1; i < end; i++) {
  97. netdata_publish_swap_t *w = &out[i];
  98. total->write += w->write;
  99. total->read += w->read;
  100. }
  101. }
  102. /**
  103. * Fill PID
  104. *
  105. * Fill PID structures
  106. *
  107. * @param current_pid pid that we are collecting data
  108. * @param out values read from hash tables;
  109. */
  110. static void swap_fill_pid(uint32_t current_pid, netdata_publish_swap_t *publish)
  111. {
  112. netdata_publish_swap_t *curr = swap_pid[current_pid];
  113. if (!curr) {
  114. curr = callocz(1, sizeof(netdata_publish_swap_t));
  115. swap_pid[current_pid] = curr;
  116. }
  117. memcpy(curr, publish, sizeof(netdata_publish_swap_t));
  118. }
  119. /**
  120. * Read APPS table
  121. *
  122. * Read the apps table and store data inside the structure.
  123. */
  124. static void read_apps_table()
  125. {
  126. netdata_publish_swap_t *cv = swap_vector;
  127. uint32_t key;
  128. struct pid_stat *pids = root_of_pids;
  129. int fd = swap_maps[NETDATA_PID_SWAP_TABLE].map_fd;
  130. size_t length = sizeof(netdata_publish_swap_t)*ebpf_nprocs;
  131. while (pids) {
  132. key = pids->pid;
  133. if (bpf_map_lookup_elem(fd, &key, cv)) {
  134. pids = pids->next;
  135. continue;
  136. }
  137. swap_apps_accumulator(cv);
  138. swap_fill_pid(key, cv);
  139. // We are cleaning to avoid passing data read from one process to other.
  140. memset(cv, 0, length);
  141. pids = pids->next;
  142. }
  143. }
  144. /**
  145. * Send global
  146. *
  147. * Send global charts to Netdata
  148. */
  149. static void swap_send_global()
  150. {
  151. write_io_chart(NETDATA_MEM_SWAP_CHART, NETDATA_EBPF_SYSTEM_GROUP,
  152. swap_publish_aggregated[NETDATA_KEY_SWAP_WRITEPAGE_CALL].dimension,
  153. (long long) swap_hash_values[NETDATA_KEY_SWAP_WRITEPAGE_CALL],
  154. swap_publish_aggregated[NETDATA_KEY_SWAP_READPAGE_CALL].dimension,
  155. (long long) swap_hash_values[NETDATA_KEY_SWAP_READPAGE_CALL]);
  156. }
  157. /**
  158. * Read global counter
  159. *
  160. * Read the table with number of calls for all functions
  161. */
  162. static void read_global_table()
  163. {
  164. netdata_idx_t *stored = swap_values;
  165. netdata_idx_t *val = swap_hash_values;
  166. int fd = swap_maps[NETDATA_SWAP_GLOBAL_TABLE].map_fd;
  167. uint32_t i, end = NETDATA_SWAP_END;
  168. for (i = NETDATA_KEY_SWAP_READPAGE_CALL; i < end; i++) {
  169. if (!bpf_map_lookup_elem(fd, &i, stored)) {
  170. int j;
  171. int last = ebpf_nprocs;
  172. netdata_idx_t total = 0;
  173. for (j = 0; j < last; j++)
  174. total += stored[j];
  175. val[i] = total;
  176. }
  177. }
  178. }
  179. /**
  180. * Socket read hash
  181. *
  182. * This is the thread callback.
  183. * This thread is necessary, because we cannot freeze the whole plugin to read the data on very busy socket.
  184. *
  185. * @param ptr It is a NULL value for this thread.
  186. *
  187. * @return It always returns NULL.
  188. */
  189. void *ebpf_swap_read_hash(void *ptr)
  190. {
  191. read_thread_closed = 0;
  192. heartbeat_t hb;
  193. heartbeat_init(&hb);
  194. ebpf_module_t *em = (ebpf_module_t *)ptr;
  195. usec_t step = NETDATA_SWAP_SLEEP_MS * em->update_time;
  196. while (!close_ebpf_plugin) {
  197. usec_t dt = heartbeat_next(&hb, step);
  198. (void)dt;
  199. read_global_table();
  200. }
  201. read_thread_closed = 1;
  202. return NULL;
  203. }
  204. /**
  205. * Sum PIDs
  206. *
  207. * Sum values for all targets.
  208. *
  209. * @param swap
  210. * @param root
  211. */
  212. static void ebpf_swap_sum_pids(netdata_publish_swap_t *swap, struct pid_on_target *root)
  213. {
  214. uint64_t local_read = 0;
  215. uint64_t local_write = 0;
  216. while (root) {
  217. int32_t pid = root->pid;
  218. netdata_publish_swap_t *w = swap_pid[pid];
  219. if (w) {
  220. local_write += w->write;
  221. local_read += w->read;
  222. }
  223. root = root->next;
  224. }
  225. // These conditions were added, because we are using incremental algorithm
  226. swap->write = (local_write >= swap->write) ? local_write : swap->write;
  227. swap->read = (local_read >= swap->read) ? local_read : swap->read;
  228. }
  229. /**
  230. * Send data to Netdata calling auxiliar functions.
  231. *
  232. * @param root the target list.
  233. */
  234. void ebpf_swap_send_apps_data(struct target *root)
  235. {
  236. struct target *w;
  237. for (w = root; w; w = w->next) {
  238. if (unlikely(w->exposed && w->processes)) {
  239. ebpf_swap_sum_pids(&w->swap, w->root_pid);
  240. }
  241. }
  242. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_MEM_SWAP_READ_CHART);
  243. for (w = root; w; w = w->next) {
  244. if (unlikely(w->exposed && w->processes)) {
  245. write_chart_dimension(w->name, (long long) w->swap.read);
  246. }
  247. }
  248. write_end_chart();
  249. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_MEM_SWAP_WRITE_CHART);
  250. for (w = root; w; w = w->next) {
  251. if (unlikely(w->exposed && w->processes)) {
  252. write_chart_dimension(w->name, (long long) w->swap.write);
  253. }
  254. }
  255. write_end_chart();
  256. }
  257. /**
  258. * Main loop for this collector.
  259. */
  260. static void swap_collector(ebpf_module_t *em)
  261. {
  262. swap_threads.thread = mallocz(sizeof(netdata_thread_t));
  263. swap_threads.start_routine = ebpf_swap_read_hash;
  264. netdata_thread_create(swap_threads.thread, swap_threads.name, NETDATA_THREAD_OPTION_JOINABLE,
  265. ebpf_swap_read_hash, em);
  266. int apps = em->apps_charts;
  267. while (!close_ebpf_plugin) {
  268. pthread_mutex_lock(&collect_data_mutex);
  269. pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
  270. if (apps)
  271. read_apps_table();
  272. pthread_mutex_lock(&lock);
  273. swap_send_global();
  274. if (apps)
  275. ebpf_swap_send_apps_data(apps_groups_root_target);
  276. pthread_mutex_unlock(&lock);
  277. pthread_mutex_unlock(&collect_data_mutex);
  278. }
  279. }
  280. /*****************************************************************
  281. *
  282. * INITIALIZE THREAD
  283. *
  284. *****************************************************************/
  285. /**
  286. * Create apps charts
  287. *
  288. * Call ebpf_create_chart to create the charts on apps submenu.
  289. *
  290. * @param em a pointer to the structure with the default values.
  291. */
  292. void ebpf_swap_create_apps_charts(struct ebpf_module *em, void *ptr)
  293. {
  294. UNUSED(em);
  295. struct target *root = ptr;
  296. ebpf_create_charts_on_apps(NETDATA_MEM_SWAP_READ_CHART,
  297. "Calls for function <code>swap_readpage</code>.",
  298. EBPF_COMMON_DIMENSION_CALL,
  299. NETDATA_SWAP_SUBMENU,
  300. NETDATA_EBPF_CHART_TYPE_STACKED,
  301. 20191,
  302. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  303. root);
  304. ebpf_create_charts_on_apps(NETDATA_MEM_SWAP_WRITE_CHART,
  305. "Calls for function <code>swap_writepage</code>.",
  306. EBPF_COMMON_DIMENSION_CALL,
  307. NETDATA_SWAP_SUBMENU,
  308. NETDATA_EBPF_CHART_TYPE_STACKED,
  309. 20192,
  310. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX],
  311. root);
  312. }
  313. /**
  314. * Allocate vectors used with this thread.
  315. *
  316. * We are not testing the return, because callocz does this and shutdown the software
  317. * case it was not possible to allocate.
  318. *
  319. * @param length is the length for the vectors used inside the collector.
  320. */
  321. static void ebpf_swap_allocate_global_vectors()
  322. {
  323. swap_pid = callocz((size_t)pid_max, sizeof(netdata_publish_swap_t *));
  324. swap_vector = callocz((size_t)ebpf_nprocs, sizeof(netdata_publish_swap_t));
  325. swap_values = callocz((size_t)ebpf_nprocs, sizeof(netdata_idx_t));
  326. memset(swap_hash_values, 0, sizeof(swap_hash_values));
  327. }
  328. /*****************************************************************
  329. *
  330. * MAIN THREAD
  331. *
  332. *****************************************************************/
  333. /**
  334. * Create global charts
  335. *
  336. * Call ebpf_create_chart to create the charts for the collector.
  337. */
  338. static void ebpf_create_swap_charts()
  339. {
  340. ebpf_create_chart(NETDATA_EBPF_SYSTEM_GROUP, NETDATA_MEM_SWAP_CHART,
  341. "Calls for internal functions used to access swap.",
  342. EBPF_COMMON_DIMENSION_CALL, NETDATA_SYSTEM_SWAP_SUBMENU,
  343. NULL,
  344. NETDATA_EBPF_CHART_TYPE_LINE,
  345. 202,
  346. ebpf_create_global_dimension,
  347. swap_publish_aggregated, NETDATA_SWAP_END);
  348. }
  349. /**
  350. * SWAP thread
  351. *
  352. * Thread used to make swap thread
  353. *
  354. * @param ptr a pointer to `struct ebpf_module`
  355. *
  356. * @return It always return NULL
  357. */
  358. void *ebpf_swap_thread(void *ptr)
  359. {
  360. netdata_thread_cleanup_push(ebpf_swap_cleanup, ptr);
  361. ebpf_module_t *em = (ebpf_module_t *)ptr;
  362. em->maps = swap_maps;
  363. fill_ebpf_data(&swap_data);
  364. ebpf_update_pid_table(&swap_maps[NETDATA_PID_SWAP_TABLE], em);
  365. if (!em->enabled)
  366. goto endswap;
  367. if (ebpf_update_kernel(&swap_data)) {
  368. goto endswap;
  369. }
  370. probe_links = ebpf_load_program(ebpf_plugin_dir, em, kernel_string, &objects, swap_data.map_fd);
  371. if (!probe_links) {
  372. goto endswap;
  373. }
  374. ebpf_swap_allocate_global_vectors();
  375. int algorithms[NETDATA_SWAP_END] = { NETDATA_EBPF_INCREMENTAL_IDX, NETDATA_EBPF_INCREMENTAL_IDX };
  376. ebpf_global_labels(swap_aggregated_data, swap_publish_aggregated, swap_dimension_name, swap_dimension_name,
  377. algorithms, NETDATA_SWAP_END);
  378. pthread_mutex_lock(&lock);
  379. ebpf_create_swap_charts();
  380. pthread_mutex_unlock(&lock);
  381. swap_collector(em);
  382. endswap:
  383. netdata_thread_cleanup_pop(1);
  384. return NULL;
  385. }