ebpf_oomkill.c 13 KB

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