From: Anita Zhang Date: Mon, 15 Mar 2021 23:06:42 +0000 (-0700) Subject: oomd: new helper oomd_update_cgroup_contexts_between_hashmaps X-Git-Tag: v248-rc4^2~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b037a6da3162f5de75a675ce0cd85facfbd8d403;p=thirdparty%2Fsystemd.git oomd: new helper oomd_update_cgroup_contexts_between_hashmaps --- diff --git a/src/oom/oomd-util.c b/src/oom/oomd-util.c index d8dbb750139..0c63b20dc6c 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 181443ae7a6..cf9f00dccc5 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 0b1a3adfccf..748753188d0 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();