ebpf_oomkill.c 13 KB

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