diff --git a/src/oom/oomd-util.c b/src/oom/oomd-util.c index 80b9583440c..8f138d64c6c 100644 --- a/src/oom/oomd-util.c +++ b/src/oom/oomd-util.c @@ -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; diff --git a/src/oom/oomd-util.h b/src/oom/oomd-util.h index d7a9890e7a2..650b4efb9c9 100644 --- a/src/oom/oomd-util.h +++ b/src/oom/oomd-util.h @@ -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) { diff --git a/src/oom/test-oomd-util.c b/src/oom/test-oomd-util.c index 3dec4f0ff06..a1fe78806a1 100644 --- a/src/oom/test-oomd-util.c +++ b/src/oom/test-oomd-util.c @@ -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);