ebpf_oomkill.c 16 KB

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