]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
oom: sort by pgscan and memory usage
authorAnita Zhang <the.anitazha@gmail.com>
Thu, 28 Jan 2021 07:43:13 +0000 (23:43 -0800)
committerAnita Zhang <the.anitazha@gmail.com>
Tue, 9 Feb 2021 09:29:57 +0000 (01:29 -0800)
If 2 candidates have the same pgscan, prioritize the one with the larger
memory usage.

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

index 80b9583440c210c54fc2c8c61965712e57b1c880..8f138d64c6ccef801d0dd1d516d28ac82657682f 100644 (file)
@@ -214,7 +214,7 @@ int oomd_kill_by_pgscan(Hashmap *h, const char *prefix, bool dry_run) {
 
         assert(h);
 
-        r = oomd_sort_cgroup_contexts(h, compare_pgscan, prefix, &sorted);
+        r = oomd_sort_cgroup_contexts(h, compare_pgscan_and_memory_usage, prefix, &sorted);
         if (r < 0)
                 return r;
 
index d7a9890e7a2d6566372d298effa2a474251ac3f5..650b4efb9c9cb0a224dd324f2dd2efc681810cd3 100644 (file)
@@ -61,11 +61,17 @@ bool oomd_memory_reclaim(Hashmap *h);
 /* Returns true if the amount of swap free is below the percentage of swap specified by `threshold_percent`. */
 bool oomd_swap_free_below(const OomdSystemContext *ctx, uint64_t threshold_percent);
 
-static inline int compare_pgscan(OomdCGroupContext * const *c1, OomdCGroupContext * const *c2) {
+static inline int compare_pgscan_and_memory_usage(OomdCGroupContext * const *c1, OomdCGroupContext * const *c2) {
+        int r;
+
         assert(c1);
         assert(c2);
 
-        return CMP((*c2)->pgscan, (*c1)->pgscan);
+        r = CMP((*c2)->pgscan, (*c1)->pgscan);
+        if (r != 0)
+                return r;
+
+        return CMP((*c2)->current_memory_usage, (*c1)->current_memory_usage);
 }
 
 static inline int compare_swap_usage(OomdCGroupContext * const *c1, OomdCGroupContext * const *c2) {
index 3dec4f0ff06f311674042d71aa10b13a09c0986d..a1fe78806a1546d27b9f069ee9cce9dc9f53c3d6 100644 (file)
@@ -292,16 +292,20 @@ static void test_oomd_sort_cgroups(void) {
         OomdCGroupContext ctx[4] = {
                 { .path = paths[0],
                   .swap_usage = 20,
-                  .pgscan = 60 },
+                  .pgscan = 60,
+                  .current_memory_usage = 10 },
                 { .path = paths[1],
                   .swap_usage = 60,
-                  .pgscan = 40 },
+                  .pgscan = 40,
+                  .current_memory_usage = 20 },
                 { .path = paths[2],
                   .swap_usage = 40,
-                  .pgscan = 20 },
+                  .pgscan = 40,
+                  .current_memory_usage = 40 },
                 { .path = paths[3],
                   .swap_usage = 10,
-                  .pgscan = 80 },
+                  .pgscan = 80,
+                  .current_memory_usage = 10 },
         };
 
         assert_se(h = hashmap_new(&string_hash_ops));
@@ -318,16 +322,16 @@ static void test_oomd_sort_cgroups(void) {
         assert_se(sorted_cgroups[3] == &ctx[3]);
         sorted_cgroups = mfree(sorted_cgroups);
 
-        assert_se(oomd_sort_cgroup_contexts(h, compare_pgscan, NULL, &sorted_cgroups) == 4);
+        assert_se(oomd_sort_cgroup_contexts(h, compare_pgscan_and_memory_usage, NULL, &sorted_cgroups) == 4);
         assert_se(sorted_cgroups[0] == &ctx[3]);
         assert_se(sorted_cgroups[1] == &ctx[0]);
-        assert_se(sorted_cgroups[2] == &ctx[1]);
-        assert_se(sorted_cgroups[3] == &ctx[2]);
+        assert_se(sorted_cgroups[2] == &ctx[2]);
+        assert_se(sorted_cgroups[3] == &ctx[1]);
         sorted_cgroups = mfree(sorted_cgroups);
 
-        assert_se(oomd_sort_cgroup_contexts(h, compare_pgscan, "/herp.slice/derp.scope", &sorted_cgroups) == 2);
-        assert_se(sorted_cgroups[0] == &ctx[1]);
-        assert_se(sorted_cgroups[1] == &ctx[2]);
+        assert_se(oomd_sort_cgroup_contexts(h, compare_pgscan_and_memory_usage, "/herp.slice/derp.scope", &sorted_cgroups) == 2);
+        assert_se(sorted_cgroups[0] == &ctx[2]);
+        assert_se(sorted_cgroups[1] == &ctx[1]);
         assert_se(sorted_cgroups[2] == 0);
         assert_se(sorted_cgroups[3] == 0);
         sorted_cgroups = mfree(sorted_cgroups);