]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
oomd: Dump top offenders after a kill action 22395/head
authorBenjamin Berg <bberg@redhat.com>
Thu, 3 Feb 2022 14:50:31 +0000 (15:50 +0100)
committerBenjamin Berg <bberg@redhat.com>
Fri, 4 Feb 2022 19:00:35 +0000 (20:00 +0100)
This hopefully makes it more transparent why a specific cgroup was
killed by systemd-oomd.

src/oom/oomd-util.c
src/oom/oomd-util.h

index cef7519a74bee3ee3eed687f30922851940757d7..4d05f57a6923645c7d22c830cecb049aad565bb7 100644 (file)
@@ -216,9 +216,36 @@ int oomd_cgroup_kill(const char *path, bool recurse, bool dry_run) {
         return set_size(pids_killed) != 0;
 }
 
+typedef void (*dump_candidate_func)(const OomdCGroupContext *ctx, FILE *f, const char *prefix);
+
+static int dump_kill_candidates(OomdCGroupContext **sorted, int n, int dump_until, dump_candidate_func dump_func) {
+        /* Try dumping top offendors, ignoring any errors that might happen. */
+        _cleanup_free_ char *dump = NULL;
+        _cleanup_fclose_ FILE *f = NULL;
+        int r;
+        size_t size;
+
+        f = open_memstream_unlocked(&dump, &size);
+        if (!f)
+                return -errno;;
+
+        fprintf(f, "Considered %d cgroups for killing, top candidates were:\n", n);
+        for (int i = 0; i < dump_until; i++)
+                dump_func(sorted[i], f, "\t");
+
+        r = fflush_and_check(f);
+        if (r < 0)
+                return r;
+
+        f = safe_fclose(f);
+
+        return log_dump(LOG_INFO, dump);
+}
+
 int oomd_kill_by_pgscan_rate(Hashmap *h, const char *prefix, bool dry_run, char **ret_selected) {
         _cleanup_free_ OomdCGroupContext **sorted = NULL;
         int n, r, ret = 0;
+        int dump_until;
 
         assert(h);
         assert(ret_selected);
@@ -227,6 +254,7 @@ int oomd_kill_by_pgscan_rate(Hashmap *h, const char *prefix, bool dry_run, char
         if (n < 0)
                 return n;
 
+        dump_until = MIN(n, DUMP_ON_KILL_COUNT);
         for (int i = 0; i < n; i++) {
                 /* Skip cgroups with no reclaim and memory usage; it won't alleviate pressure.
                  * Continue since there might be "avoid" cgroups at the end. */
@@ -242,19 +270,24 @@ int oomd_kill_by_pgscan_rate(Hashmap *h, const char *prefix, bool dry_run, char
                         continue; /* Try to find something else to kill */
                 }
 
+                dump_until = MAX(dump_until, i);
                 char *selected = strdup(sorted[i]->path);
                 if (!selected)
                         return -ENOMEM;
                 *ret_selected = selected;
-                return r;
+                ret = r;
+                break;
         }
 
+        dump_kill_candidates(sorted, n, dump_until, oomd_dump_memory_pressure_cgroup_context);
+
         return ret;
 }
 
 int oomd_kill_by_swap_usage(Hashmap *h, uint64_t threshold_usage, bool dry_run, char **ret_selected) {
         _cleanup_free_ OomdCGroupContext **sorted = NULL;
         int n, r, ret = 0;
+        int dump_until;
 
         assert(h);
         assert(ret_selected);
@@ -263,6 +296,7 @@ int oomd_kill_by_swap_usage(Hashmap *h, uint64_t threshold_usage, bool dry_run,
         if (n < 0)
                 return n;
 
+        dump_until = MIN(n, DUMP_ON_KILL_COUNT);
         /* Try to kill cgroups with non-zero swap usage until we either succeed in killing or we get to a cgroup with
          * no swap usage. Threshold killing only cgroups with more than threshold swap usage. */
         for (int i = 0; i < n; i++) {
@@ -280,13 +314,17 @@ int oomd_kill_by_swap_usage(Hashmap *h, uint64_t threshold_usage, bool dry_run,
                         continue; /* Try to find something else to kill */
                 }
 
+                dump_until = MAX(dump_until, i);
                 char *selected = strdup(sorted[i]->path);
                 if (!selected)
                         return -ENOMEM;
                 *ret_selected = selected;
-                return r;
+                ret = r;
+                break;
         }
 
+        dump_kill_candidates(sorted, n, dump_until, oomd_dump_swap_cgroup_context);
+
         return ret;
 }
 
index 3a91a31352e985610e583720c2fa4be878341a79..6aada9034414f117e637233b5a1e4555cffb80d7 100644 (file)
@@ -7,6 +7,7 @@
 #include "hashmap.h"
 #include "psi-util.h"
 
+#define DUMP_ON_KILL_COUNT 10
 #define GROWING_SIZE_PERCENTILE 80
 
 extern const struct hash_ops oomd_cgroup_ctx_hash_ops;