1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-01 00:51:24 +03:00

oom: sort by pgscan and memory usage

If 2 candidates have the same pgscan, prioritize the one with the larger
memory usage.
This commit is contained in:
Anita Zhang 2021-01-27 23:43:13 -08:00
parent df7f3eab52
commit 1f76411bd6
3 changed files with 23 additions and 13 deletions

View 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;

View 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) {

View 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);