diff options
author | Anita Zhang <the.anitazha@gmail.com> | 2021-03-15 16:06:42 -0700 |
---|---|---|
committer | Anita Zhang <the.anitazha@gmail.com> | 2021-03-16 18:10:57 -0700 |
commit | b037a6da3162f5de75a675ce0cd85facfbd8d403 (patch) | |
tree | ef9c0e5ac2ef5e5d0897bc91c4e3096766e9d801 /src/oom | |
parent | 301e7cd047c8d07715d5dc37f713e8aa031581b4 (diff) | |
download | systemd-b037a6da3162f5de75a675ce0cd85facfbd8d403.tar.gz |
oomd: new helper oomd_update_cgroup_contexts_between_hashmaps
Diffstat (limited to 'src/oom')
-rw-r--r-- | src/oom/oomd-util.c | 19 | ||||
-rw-r--r-- | src/oom/oomd-util.h | 3 | ||||
-rw-r--r-- | src/oom/test-oomd-util.c | 48 |
3 files changed, 70 insertions, 0 deletions
diff --git a/src/oom/oomd-util.c b/src/oom/oomd-util.c index d8dbb75013..0c63b20dc6 100644 --- a/src/oom/oomd-util.c +++ b/src/oom/oomd-util.c @@ -413,6 +413,25 @@ int oomd_insert_cgroup_context(Hashmap *old_h, Hashmap *new_h, const char *path) return 0; } +void oomd_update_cgroup_contexts_between_hashmaps(Hashmap *old_h, Hashmap *curr_h) { + OomdCGroupContext *ctx; + + assert(old_h); + assert(curr_h); + + HASHMAP_FOREACH(ctx, curr_h) { + OomdCGroupContext *old_ctx; + + old_ctx = hashmap_get(old_h, ctx->path); + if (!old_ctx) + continue; + + ctx->last_pgscan = old_ctx->pgscan; + ctx->mem_pressure_limit = old_ctx->mem_pressure_limit; + ctx->last_hit_mem_pressure_limit = old_ctx->last_hit_mem_pressure_limit; + } +} + void oomd_dump_swap_cgroup_context(const OomdCGroupContext *ctx, FILE *f, const char *prefix) { char swap[FORMAT_BYTES_MAX]; diff --git a/src/oom/oomd-util.h b/src/oom/oomd-util.h index 181443ae7a..cf9f00dccc 100644 --- a/src/oom/oomd-util.h +++ b/src/oom/oomd-util.h @@ -119,6 +119,9 @@ int oomd_system_context_acquire(const char *proc_swaps_path, OomdSystemContext * * was no prior data to reference. */ int oomd_insert_cgroup_context(Hashmap *old_h, Hashmap *new_h, const char *path); +/* Update each OomdCGroupContext in `curr_h` with prior interval information from `old_h`. */ +void oomd_update_cgroup_contexts_between_hashmaps(Hashmap *old_h, Hashmap *curr_h); + void oomd_dump_swap_cgroup_context(const OomdCGroupContext *ctx, FILE *f, const char *prefix); void oomd_dump_memory_pressure_cgroup_context(const OomdCGroupContext *ctx, FILE *f, const char *prefix); void oomd_dump_system_context(const OomdSystemContext *ctx, FILE *f, const char *prefix); diff --git a/src/oom/test-oomd-util.c b/src/oom/test-oomd-util.c index 0b1a3adfcc..748753188d 100644 --- a/src/oom/test-oomd-util.c +++ b/src/oom/test-oomd-util.c @@ -176,6 +176,53 @@ static void test_oomd_cgroup_context_acquire_and_insert(void) { } } +static void test_oomd_update_cgroup_contexts_between_hashmaps(void) { + _cleanup_hashmap_free_ Hashmap *h_old = NULL, *h_new = NULL; + OomdCGroupContext *c_old, *c_new; + char **paths = STRV_MAKE("/0.slice", + "/1.slice"); + + OomdCGroupContext ctx_old[3] = { + { .path = paths[0], + .mem_pressure_limit = 5, + .last_hit_mem_pressure_limit = 777, + .pgscan = 57 }, + { .path = paths[1], + .mem_pressure_limit = 6, + .last_hit_mem_pressure_limit = 888, + .pgscan = 42 }, + }; + + OomdCGroupContext ctx_new[3] = { + { .path = paths[0], + .pgscan = 100 }, + { .path = paths[1], + .pgscan = 101 }, + }; + + assert_se(h_old = hashmap_new(&string_hash_ops)); + assert_se(hashmap_put(h_old, paths[0], &ctx_old[0]) >= 0); + assert_se(hashmap_put(h_old, paths[1], &ctx_old[1]) >= 0); + + assert_se(h_new = hashmap_new(&string_hash_ops)); + assert_se(hashmap_put(h_new, paths[0], &ctx_new[0]) >= 0); + assert_se(hashmap_put(h_new, paths[1], &ctx_new[1]) >= 0); + + oomd_update_cgroup_contexts_between_hashmaps(h_old, h_new); + + assert_se(c_old = hashmap_get(h_old, "/0.slice")); + assert_se(c_new = hashmap_get(h_new, "/0.slice")); + assert_se(c_old->pgscan == c_new->last_pgscan); + assert_se(c_old->mem_pressure_limit == c_new->mem_pressure_limit); + assert_se(c_old->last_hit_mem_pressure_limit == c_new->last_hit_mem_pressure_limit); + + assert_se(c_old = hashmap_get(h_old, "/1.slice")); + assert_se(c_new = hashmap_get(h_new, "/1.slice")); + assert_se(c_old->pgscan == c_new->last_pgscan); + assert_se(c_old->mem_pressure_limit == c_new->mem_pressure_limit); + assert_se(c_old->last_hit_mem_pressure_limit == c_new->last_hit_mem_pressure_limit); +} + static void test_oomd_system_context_acquire(void) { _cleanup_(unlink_tempfilep) char path[] = "/oomdgetsysctxtestXXXXXX"; OomdSystemContext ctx; @@ -395,6 +442,7 @@ int main(void) { test_setup_logging(LOG_DEBUG); + test_oomd_update_cgroup_contexts_between_hashmaps(); test_oomd_system_context_acquire(); test_oomd_pressure_above(); test_oomd_memory_reclaim(); |