ebpf_oomkill.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "ebpf.h"
  3. #include "ebpf_oomkill.h"
  4. struct config oomkill_config = { .first_section = NULL,
  5. .last_section = NULL,
  6. .mutex = NETDATA_MUTEX_INITIALIZER,
  7. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  8. .rwlock = AVL_LOCK_INITIALIZER } };
  9. #define OOMKILL_MAP_KILLCNT 0
  10. static ebpf_local_maps_t oomkill_maps[] = {
  11. {
  12. .name = "tbl_oomkill",
  13. .internal_input = NETDATA_OOMKILL_MAX_ENTRIES,
  14. .user_input = 0,
  15. .type = NETDATA_EBPF_MAP_STATIC,
  16. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED
  17. },
  18. /* end */
  19. {
  20. .name = NULL,
  21. .internal_input = 0,
  22. .user_input = 0,
  23. .type = NETDATA_EBPF_MAP_CONTROLLER,
  24. .map_fd = ND_EBPF_MAP_FD_NOT_INITIALIZED
  25. }
  26. };
  27. static ebpf_tracepoint_t oomkill_tracepoints[] = {
  28. {.enabled = false, .class = "oom", .event = "mark_victim"},
  29. /* end */
  30. {.enabled = false, .class = NULL, .event = NULL}
  31. };
  32. static struct bpf_link **probe_links = NULL;
  33. static struct bpf_object *objects = NULL;
  34. static netdata_publish_syscall_t oomkill_publish_aggregated = {.name = "oomkill", .dimension = "oomkill",
  35. .algorithm = "absolute",
  36. .next = NULL};
  37. /**
  38. * Clean up the main thread.
  39. *
  40. * @param ptr thread data.
  41. */
  42. static void oomkill_cleanup(void *ptr)
  43. {
  44. ebpf_module_t *em = (ebpf_module_t *)ptr;
  45. if (!em->enabled) {
  46. return;
  47. }
  48. if (probe_links) {
  49. struct bpf_program *prog;
  50. size_t i = 0 ;
  51. bpf_object__for_each_program(prog, objects) {
  52. bpf_link__destroy(probe_links[i]);
  53. i++;
  54. }
  55. bpf_object__close(objects);
  56. }
  57. }
  58. static void oomkill_write_data(int32_t *keys, uint32_t total)
  59. {
  60. // for each app, see if it was OOM killed. record as 1 if so otherwise 0.
  61. struct target *w;
  62. for (w = apps_groups_root_target; w != NULL; w = w->next) {
  63. if (likely(w->exposed && w->processes)) {
  64. bool was_oomkilled = false;
  65. struct pid_on_target *pids = w->root_pid;
  66. while (pids) {
  67. uint32_t j;
  68. for (j = 0; j < total; j++) {
  69. if (pids->pid == keys[j]) {
  70. was_oomkilled = true;
  71. // set to 0 so we consider it "done".
  72. keys[j] = 0;
  73. goto write_dim;
  74. }
  75. }
  76. pids = pids->next;
  77. }
  78. write_dim:;
  79. write_chart_dimension(w->name, was_oomkilled);
  80. }
  81. }
  82. // for any remaining keys for which we couldn't find a group, this could be
  83. // for various reasons, but the primary one is that the PID has not yet
  84. // been picked up by the process thread when parsing the proc filesystem.
  85. // since it's been OOM killed, it will never be parsed in the future, so
  86. // we have no choice but to dump it into `other`.
  87. uint32_t j;
  88. uint32_t rem_count = 0;
  89. for (j = 0; j < total; j++) {
  90. int32_t key = keys[j];
  91. if (key != 0) {
  92. rem_count += 1;
  93. }
  94. }
  95. if (rem_count > 0) {
  96. write_chart_dimension("other", rem_count);
  97. }
  98. }
  99. /**
  100. * Create specific OOMkill charts
  101. *
  102. * Create charts for cgroup/application.
  103. *
  104. * @param type the chart type.
  105. */
  106. static void ebpf_create_specific_oomkill_charts(char *type)
  107. {
  108. ebpf_create_chart(type, NETDATA_OOMKILL_CHART, "OOM kills. This chart is provided by eBPF plugin.",
  109. EBPF_COMMON_DIMENSION_KILLS, NETDATA_EBPF_MEMORY_GROUP,
  110. NULL, NETDATA_EBPF_CHART_TYPE_LINE,
  111. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5600,
  112. ebpf_create_global_dimension,
  113. &oomkill_publish_aggregated, 1, NETDATA_EBPF_MODULE_NAME_OOMKILL);
  114. }
  115. /**
  116. * Create Systemd OOMkill Charts
  117. *
  118. * Create charts when systemd is enabled
  119. **/
  120. static void ebpf_create_systemd_oomkill_charts()
  121. {
  122. ebpf_create_charts_on_systemd(NETDATA_OOMKILL_CHART, "OOM kills. This chart is provided by eBPF plugin.",
  123. EBPF_COMMON_DIMENSION_KILLS, NETDATA_EBPF_MEMORY_GROUP,
  124. NETDATA_EBPF_CHART_TYPE_LINE, 20191,
  125. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX], NULL,
  126. NETDATA_EBPF_MODULE_NAME_OOMKILL);
  127. }
  128. /**
  129. * Send Systemd charts
  130. *
  131. * Send collected data to Netdata.
  132. *
  133. * @return It returns the status for chart creation, if it is necessary to remove a specific dimension, zero is returned
  134. * otherwise function returns 1 to avoid chart recreation
  135. */
  136. static int ebpf_send_systemd_oomkill_charts()
  137. {
  138. int ret = 1;
  139. ebpf_cgroup_target_t *ect;
  140. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_OOMKILL_CHART);
  141. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  142. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  143. write_chart_dimension(ect->name, (long long) ect->oomkill);
  144. ect->oomkill = 0;
  145. } else
  146. ret = 0;
  147. }
  148. write_end_chart();
  149. return ret;
  150. }
  151. /*
  152. * Send Specific OOMkill data
  153. *
  154. * Send data for specific cgroup/apps.
  155. *
  156. * @param type chart type
  157. * @param value value for oomkill
  158. */
  159. static void ebpf_send_specific_oomkill_data(char *type, int value)
  160. {
  161. write_begin_chart(type, NETDATA_OOMKILL_CHART);
  162. write_chart_dimension(oomkill_publish_aggregated.name, (long long)value);
  163. write_end_chart();
  164. }
  165. /**
  166. * Create specific OOMkill charts
  167. *
  168. * Create charts for cgroup/application.
  169. *
  170. * @param type the chart type.
  171. */
  172. static void ebpf_obsolete_specific_oomkill_charts(char *type)
  173. {
  174. ebpf_write_chart_obsolete(type, NETDATA_OOMKILL_CHART, "OOM kills. This chart is provided by eBPF plugin.",
  175. EBPF_COMMON_DIMENSION_KILLS, NETDATA_EBPF_MEMORY_GROUP,
  176. NETDATA_EBPF_CHART_TYPE_LINE, NULL,
  177. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5600);
  178. }
  179. /**
  180. * Send data to Netdata calling auxiliar functions.
  181. *
  182. * @param root the target list.
  183. */
  184. void ebpf_oomkill_send_cgroup_data()
  185. {
  186. if (!ebpf_cgroup_pids)
  187. return;
  188. pthread_mutex_lock(&mutex_cgroup_shm);
  189. ebpf_cgroup_target_t *ect;
  190. int has_systemd = shm_ebpf_cgroup.header->systemd_enabled;
  191. if (has_systemd) {
  192. static int systemd_charts = 0;
  193. if (!systemd_charts) {
  194. ebpf_create_systemd_oomkill_charts();
  195. systemd_charts = 1;
  196. }
  197. systemd_charts = ebpf_send_systemd_oomkill_charts();
  198. }
  199. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  200. if (ect->systemd)
  201. continue;
  202. if (!(ect->flags & NETDATA_EBPF_CGROUP_HAS_OOMKILL_CHART) && ect->updated) {
  203. ebpf_create_specific_oomkill_charts(ect->name);
  204. ect->flags |= NETDATA_EBPF_CGROUP_HAS_OOMKILL_CHART;
  205. }
  206. if (ect->flags & NETDATA_EBPF_CGROUP_HAS_OOMKILL_CHART && ect->updated) {
  207. ebpf_send_specific_oomkill_data(ect->name, ect->oomkill);
  208. } else {
  209. ebpf_obsolete_specific_oomkill_charts(ect->name);
  210. ect->flags &= ~NETDATA_EBPF_CGROUP_HAS_OOMKILL_CHART;
  211. }
  212. }
  213. pthread_mutex_unlock(&mutex_cgroup_shm);
  214. }
  215. /**
  216. * Read data
  217. *
  218. * Read OOMKILL events from table.
  219. *
  220. * @param keys vector where data will be stored
  221. *
  222. * @return It returns the number of read elements
  223. */
  224. static uint32_t oomkill_read_data(int32_t *keys)
  225. {
  226. // the first `i` entries of `keys` will contain the currently active PIDs
  227. // in the eBPF map.
  228. uint32_t i = 0;
  229. uint32_t curr_key = 0;
  230. uint32_t key = 0;
  231. int mapfd = oomkill_maps[OOMKILL_MAP_KILLCNT].map_fd;
  232. while (bpf_map_get_next_key(mapfd, &curr_key, &key) == 0) {
  233. curr_key = key;
  234. keys[i] = (int32_t)key;
  235. i += 1;
  236. // delete this key now that we've recorded its existence. there's no
  237. // race here, as the same PID will only get OOM killed once.
  238. int test = bpf_map_delete_elem(mapfd, &key);
  239. if (unlikely(test < 0)) {
  240. // since there's only 1 thread doing these deletions, it should be
  241. // impossible to get this condition.
  242. error("key unexpectedly not available for deletion.");
  243. }
  244. }
  245. return i;
  246. }
  247. /**
  248. * Update cgroup
  249. *
  250. * Update cgroup data based in
  251. *
  252. * @param keys vector with pids that had oomkill event
  253. * @param total number of elements in keys vector.
  254. */
  255. static void ebpf_update_oomkill_cgroup(int32_t *keys, uint32_t total)
  256. {
  257. ebpf_cgroup_target_t *ect;
  258. for (ect = ebpf_cgroup_pids; ect; ect = ect->next) {
  259. ect->oomkill = 0;
  260. struct pid_on_target2 *pids;
  261. for (pids = ect->pids; pids; pids = pids->next) {
  262. uint32_t j;
  263. int32_t pid = pids->pid;
  264. for (j = 0; j < total; j++) {
  265. if (pid == keys[j]) {
  266. ect->oomkill = 1;
  267. break;
  268. }
  269. }
  270. }
  271. }
  272. }
  273. /**
  274. * Main loop for this collector.
  275. */
  276. static void oomkill_collector(ebpf_module_t *em)
  277. {
  278. int cgroups = em->cgroup_charts;
  279. int32_t keys[NETDATA_OOMKILL_MAX_ENTRIES];
  280. memset(keys, 0, sizeof(keys));
  281. // loop and read until ebpf plugin is closed.
  282. while (!close_ebpf_plugin) {
  283. pthread_mutex_lock(&collect_data_mutex);
  284. pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
  285. pthread_mutex_lock(&lock);
  286. uint32_t count = oomkill_read_data(keys);
  287. if (cgroups && count)
  288. ebpf_update_oomkill_cgroup(keys, count);
  289. // write everything from the ebpf map.
  290. if (cgroups)
  291. ebpf_oomkill_send_cgroup_data();
  292. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_OOMKILL_CHART);
  293. oomkill_write_data(keys, count);
  294. write_end_chart();
  295. pthread_mutex_unlock(&lock);
  296. pthread_mutex_unlock(&collect_data_mutex);
  297. }
  298. }
  299. /**
  300. * Create apps charts
  301. *
  302. * Call ebpf_create_chart to create the charts on apps submenu.
  303. *
  304. * @param em a pointer to the structure with the default values.
  305. */
  306. void ebpf_oomkill_create_apps_charts(struct ebpf_module *em, void *ptr)
  307. {
  308. UNUSED(em);
  309. struct target *root = ptr;
  310. ebpf_create_charts_on_apps(NETDATA_OOMKILL_CHART,
  311. "OOM kills",
  312. EBPF_COMMON_DIMENSION_KILLS,
  313. "mem",
  314. NETDATA_EBPF_CHART_TYPE_STACKED,
  315. 20020,
  316. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  317. root, NETDATA_EBPF_MODULE_NAME_OOMKILL);
  318. }
  319. /**
  320. * OOM kill tracking thread.
  321. *
  322. * @param ptr a `ebpf_module_t *`.
  323. * @return always NULL.
  324. */
  325. void *ebpf_oomkill_thread(void *ptr)
  326. {
  327. netdata_thread_cleanup_push(oomkill_cleanup, ptr);
  328. ebpf_module_t *em = (ebpf_module_t *)ptr;
  329. em->maps = oomkill_maps;
  330. if (!em->enabled) {
  331. goto endoomkill;
  332. }
  333. if (ebpf_enable_tracepoints(oomkill_tracepoints) == 0) {
  334. em->enabled = CONFIG_BOOLEAN_NO;
  335. goto endoomkill;
  336. }
  337. probe_links = ebpf_load_program(ebpf_plugin_dir, em, kernel_string, &objects);
  338. if (!probe_links) {
  339. goto endoomkill;
  340. }
  341. oomkill_collector(em);
  342. endoomkill:
  343. netdata_thread_cleanup_pop(1);
  344. return NULL;
  345. }