ebpf_oomkill.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. * @param update_every value to overwrite the update frequency set by the server.
  106. */
  107. static void ebpf_create_specific_oomkill_charts(char *type, int update_every)
  108. {
  109. ebpf_create_chart(type, NETDATA_OOMKILL_CHART, "OOM kills. This chart is provided by eBPF plugin.",
  110. EBPF_COMMON_DIMENSION_KILLS, NETDATA_EBPF_MEMORY_GROUP,
  111. NETDATA_CGROUP_OOMKILLS_CONTEXT, NETDATA_EBPF_CHART_TYPE_LINE,
  112. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5600,
  113. ebpf_create_global_dimension,
  114. &oomkill_publish_aggregated, 1, update_every, NETDATA_EBPF_MODULE_NAME_OOMKILL);
  115. }
  116. /**
  117. * Create Systemd OOMkill Charts
  118. *
  119. * Create charts when systemd is enabled
  120. *
  121. * @param update_every value to overwrite the update frequency set by the server.
  122. **/
  123. static void ebpf_create_systemd_oomkill_charts(int update_every)
  124. {
  125. ebpf_create_charts_on_systemd(NETDATA_OOMKILL_CHART, "OOM kills. This chart is provided by eBPF plugin.",
  126. EBPF_COMMON_DIMENSION_KILLS, NETDATA_EBPF_MEMORY_GROUP,
  127. NETDATA_EBPF_CHART_TYPE_LINE, 20191,
  128. ebpf_algorithms[NETDATA_EBPF_INCREMENTAL_IDX], NULL,
  129. NETDATA_EBPF_MODULE_NAME_OOMKILL, update_every);
  130. }
  131. /**
  132. * Send Systemd charts
  133. *
  134. * Send collected data to Netdata.
  135. *
  136. * @return It returns the status for chart creation, if it is necessary to remove a specific dimension, zero is returned
  137. * otherwise function returns 1 to avoid chart recreation
  138. */
  139. static int ebpf_send_systemd_oomkill_charts()
  140. {
  141. int ret = 1;
  142. ebpf_cgroup_target_t *ect;
  143. write_begin_chart(NETDATA_SERVICE_FAMILY, NETDATA_OOMKILL_CHART);
  144. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  145. if (unlikely(ect->systemd) && unlikely(ect->updated)) {
  146. write_chart_dimension(ect->name, (long long) ect->oomkill);
  147. ect->oomkill = 0;
  148. } else
  149. ret = 0;
  150. }
  151. write_end_chart();
  152. return ret;
  153. }
  154. /*
  155. * Send Specific OOMkill data
  156. *
  157. * Send data for specific cgroup/apps.
  158. *
  159. * @param type chart type
  160. * @param value value for oomkill
  161. */
  162. static void ebpf_send_specific_oomkill_data(char *type, int value)
  163. {
  164. write_begin_chart(type, NETDATA_OOMKILL_CHART);
  165. write_chart_dimension(oomkill_publish_aggregated.name, (long long)value);
  166. write_end_chart();
  167. }
  168. /**
  169. * Create specific OOMkill charts
  170. *
  171. * Create charts for cgroup/application.
  172. *
  173. * @param type the chart type.
  174. * @param update_every value to overwrite the update frequency set by the server.
  175. */
  176. static void ebpf_obsolete_specific_oomkill_charts(char *type, int update_every)
  177. {
  178. ebpf_write_chart_obsolete(type, NETDATA_OOMKILL_CHART, "OOM kills. This chart is provided by eBPF plugin.",
  179. EBPF_COMMON_DIMENSION_KILLS, NETDATA_EBPF_MEMORY_GROUP,
  180. NETDATA_EBPF_CHART_TYPE_LINE, NETDATA_CGROUP_OOMKILLS_CONTEXT,
  181. NETDATA_CHART_PRIO_CGROUPS_CONTAINERS + 5600, update_every);
  182. }
  183. /**
  184. * Send data to Netdata calling auxiliary functions.
  185. *
  186. * @param update_every value to overwrite the update frequency set by the server.
  187. */
  188. void ebpf_oomkill_send_cgroup_data(int update_every)
  189. {
  190. if (!ebpf_cgroup_pids)
  191. return;
  192. pthread_mutex_lock(&mutex_cgroup_shm);
  193. ebpf_cgroup_target_t *ect;
  194. int has_systemd = shm_ebpf_cgroup.header->systemd_enabled;
  195. if (has_systemd) {
  196. static int systemd_charts = 0;
  197. if (!systemd_charts) {
  198. ebpf_create_systemd_oomkill_charts(update_every);
  199. systemd_charts = 1;
  200. }
  201. systemd_charts = ebpf_send_systemd_oomkill_charts();
  202. }
  203. for (ect = ebpf_cgroup_pids; ect ; ect = ect->next) {
  204. if (ect->systemd)
  205. continue;
  206. if (!(ect->flags & NETDATA_EBPF_CGROUP_HAS_OOMKILL_CHART) && ect->updated) {
  207. ebpf_create_specific_oomkill_charts(ect->name, update_every);
  208. ect->flags |= NETDATA_EBPF_CGROUP_HAS_OOMKILL_CHART;
  209. }
  210. if (ect->flags & NETDATA_EBPF_CGROUP_HAS_OOMKILL_CHART && ect->updated) {
  211. ebpf_send_specific_oomkill_data(ect->name, ect->oomkill);
  212. } else {
  213. ebpf_obsolete_specific_oomkill_charts(ect->name, update_every);
  214. ect->flags &= ~NETDATA_EBPF_CGROUP_HAS_OOMKILL_CHART;
  215. }
  216. }
  217. pthread_mutex_unlock(&mutex_cgroup_shm);
  218. }
  219. /**
  220. * Read data
  221. *
  222. * Read OOMKILL events from table.
  223. *
  224. * @param keys vector where data will be stored
  225. *
  226. * @return It returns the number of read elements
  227. */
  228. static uint32_t oomkill_read_data(int32_t *keys)
  229. {
  230. // the first `i` entries of `keys` will contain the currently active PIDs
  231. // in the eBPF map.
  232. uint32_t i = 0;
  233. uint32_t curr_key = 0;
  234. uint32_t key = 0;
  235. int mapfd = oomkill_maps[OOMKILL_MAP_KILLCNT].map_fd;
  236. while (bpf_map_get_next_key(mapfd, &curr_key, &key) == 0) {
  237. curr_key = key;
  238. keys[i] = (int32_t)key;
  239. i += 1;
  240. // delete this key now that we've recorded its existence. there's no
  241. // race here, as the same PID will only get OOM killed once.
  242. int test = bpf_map_delete_elem(mapfd, &key);
  243. if (unlikely(test < 0)) {
  244. // since there's only 1 thread doing these deletions, it should be
  245. // impossible to get this condition.
  246. error("key unexpectedly not available for deletion.");
  247. }
  248. }
  249. return i;
  250. }
  251. /**
  252. * Update cgroup
  253. *
  254. * Update cgroup data based in
  255. *
  256. * @param keys vector with pids that had oomkill event
  257. * @param total number of elements in keys vector.
  258. */
  259. static void ebpf_update_oomkill_cgroup(int32_t *keys, uint32_t total)
  260. {
  261. ebpf_cgroup_target_t *ect;
  262. for (ect = ebpf_cgroup_pids; ect; ect = ect->next) {
  263. ect->oomkill = 0;
  264. struct pid_on_target2 *pids;
  265. for (pids = ect->pids; pids; pids = pids->next) {
  266. uint32_t j;
  267. int32_t pid = pids->pid;
  268. for (j = 0; j < total; j++) {
  269. if (pid == keys[j]) {
  270. ect->oomkill = 1;
  271. break;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. /**
  278. * Main loop for this collector.
  279. */
  280. static void oomkill_collector(ebpf_module_t *em)
  281. {
  282. int cgroups = em->cgroup_charts;
  283. int update_every = em->update_every;
  284. int counter = update_every - 1;
  285. int32_t keys[NETDATA_OOMKILL_MAX_ENTRIES];
  286. memset(keys, 0, sizeof(keys));
  287. // loop and read until ebpf plugin is closed.
  288. while (!close_ebpf_plugin) {
  289. pthread_mutex_lock(&collect_data_mutex);
  290. pthread_cond_wait(&collect_data_cond_var, &collect_data_mutex);
  291. if (++counter == update_every) {
  292. counter = 0;
  293. pthread_mutex_lock(&lock);
  294. uint32_t count = oomkill_read_data(keys);
  295. if (cgroups && count)
  296. ebpf_update_oomkill_cgroup(keys, count);
  297. // write everything from the ebpf map.
  298. if (cgroups)
  299. ebpf_oomkill_send_cgroup_data(update_every);
  300. write_begin_chart(NETDATA_APPS_FAMILY, NETDATA_OOMKILL_CHART);
  301. oomkill_write_data(keys, count);
  302. write_end_chart();
  303. pthread_mutex_unlock(&lock);
  304. }
  305. pthread_mutex_unlock(&collect_data_mutex);
  306. }
  307. }
  308. /**
  309. * Create apps charts
  310. *
  311. * Call ebpf_create_chart to create the charts on apps submenu.
  312. *
  313. * @param em a pointer to the structure with the default values.
  314. */
  315. void ebpf_oomkill_create_apps_charts(struct ebpf_module *em, void *ptr)
  316. {
  317. struct target *root = ptr;
  318. ebpf_create_charts_on_apps(NETDATA_OOMKILL_CHART,
  319. "OOM kills",
  320. EBPF_COMMON_DIMENSION_KILLS,
  321. "mem",
  322. NETDATA_EBPF_CHART_TYPE_STACKED,
  323. 20020,
  324. ebpf_algorithms[NETDATA_EBPF_ABSOLUTE_IDX],
  325. root, em->update_every, NETDATA_EBPF_MODULE_NAME_OOMKILL);
  326. }
  327. /**
  328. * OOM kill tracking thread.
  329. *
  330. * @param ptr a `ebpf_module_t *`.
  331. * @return always NULL.
  332. */
  333. void *ebpf_oomkill_thread(void *ptr)
  334. {
  335. netdata_thread_cleanup_push(oomkill_cleanup, ptr);
  336. ebpf_module_t *em = (ebpf_module_t *)ptr;
  337. em->maps = oomkill_maps;
  338. if (!em->enabled) {
  339. goto endoomkill;
  340. }
  341. if (ebpf_enable_tracepoints(oomkill_tracepoints) == 0) {
  342. em->enabled = CONFIG_BOOLEAN_NO;
  343. goto endoomkill;
  344. }
  345. probe_links = ebpf_load_program(ebpf_plugin_dir, em, running_on_kernel, isrh, &objects);
  346. if (!probe_links) {
  347. em->enabled = CONFIG_BOOLEAN_NO;
  348. goto endoomkill;
  349. }
  350. pthread_mutex_lock(&lock);
  351. ebpf_update_stats(&plugin_statistics, em);
  352. pthread_mutex_unlock(&lock);
  353. oomkill_collector(em);
  354. endoomkill:
  355. if (!em->enabled)
  356. ebpf_update_disabled_plugin_stats(em);
  357. netdata_thread_cleanup_pop(1);
  358. return NULL;
  359. }