ebpf_oomkill.c 17 KB

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