ebpf_oomkill.c 13 KB

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