X Tutup
Skip to content

Commit b037a6d

Browse files
committed
oomd: new helper oomd_update_cgroup_contexts_between_hashmaps
1 parent 301e7cd commit b037a6d

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/oom/oomd-util.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,25 @@ int oomd_insert_cgroup_context(Hashmap *old_h, Hashmap *new_h, const char *path)
413413
return 0;
414414
}
415415

416+
void oomd_update_cgroup_contexts_between_hashmaps(Hashmap *old_h, Hashmap *curr_h) {
417+
OomdCGroupContext *ctx;
418+
419+
assert(old_h);
420+
assert(curr_h);
421+
422+
HASHMAP_FOREACH(ctx, curr_h) {
423+
OomdCGroupContext *old_ctx;
424+
425+
old_ctx = hashmap_get(old_h, ctx->path);
426+
if (!old_ctx)
427+
continue;
428+
429+
ctx->last_pgscan = old_ctx->pgscan;
430+
ctx->mem_pressure_limit = old_ctx->mem_pressure_limit;
431+
ctx->last_hit_mem_pressure_limit = old_ctx->last_hit_mem_pressure_limit;
432+
}
433+
}
434+
416435
void oomd_dump_swap_cgroup_context(const OomdCGroupContext *ctx, FILE *f, const char *prefix) {
417436
char swap[FORMAT_BYTES_MAX];
418437

src/oom/oomd-util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ int oomd_system_context_acquire(const char *proc_swaps_path, OomdSystemContext *
119119
* was no prior data to reference. */
120120
int oomd_insert_cgroup_context(Hashmap *old_h, Hashmap *new_h, const char *path);
121121

122+
/* Update each OomdCGroupContext in `curr_h` with prior interval information from `old_h`. */
123+
void oomd_update_cgroup_contexts_between_hashmaps(Hashmap *old_h, Hashmap *curr_h);
124+
122125
void oomd_dump_swap_cgroup_context(const OomdCGroupContext *ctx, FILE *f, const char *prefix);
123126
void oomd_dump_memory_pressure_cgroup_context(const OomdCGroupContext *ctx, FILE *f, const char *prefix);
124127
void oomd_dump_system_context(const OomdSystemContext *ctx, FILE *f, const char *prefix);

src/oom/test-oomd-util.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,53 @@ static void test_oomd_cgroup_context_acquire_and_insert(void) {
176176
}
177177
}
178178

179+
static void test_oomd_update_cgroup_contexts_between_hashmaps(void) {
180+
_cleanup_hashmap_free_ Hashmap *h_old = NULL, *h_new = NULL;
181+
OomdCGroupContext *c_old, *c_new;
182+
char **paths = STRV_MAKE("/0.slice",
183+
"/1.slice");
184+
185+
OomdCGroupContext ctx_old[3] = {
186+
{ .path = paths[0],
187+
.mem_pressure_limit = 5,
188+
.last_hit_mem_pressure_limit = 777,
189+
.pgscan = 57 },
190+
{ .path = paths[1],
191+
.mem_pressure_limit = 6,
192+
.last_hit_mem_pressure_limit = 888,
193+
.pgscan = 42 },
194+
};
195+
196+
OomdCGroupContext ctx_new[3] = {
197+
{ .path = paths[0],
198+
.pgscan = 100 },
199+
{ .path = paths[1],
200+
.pgscan = 101 },
201+
};
202+
203+
assert_se(h_old = hashmap_new(&string_hash_ops));
204+
assert_se(hashmap_put(h_old, paths[0], &ctx_old[0]) >= 0);
205+
assert_se(hashmap_put(h_old, paths[1], &ctx_old[1]) >= 0);
206+
207+
assert_se(h_new = hashmap_new(&string_hash_ops));
208+
assert_se(hashmap_put(h_new, paths[0], &ctx_new[0]) >= 0);
209+
assert_se(hashmap_put(h_new, paths[1], &ctx_new[1]) >= 0);
210+
211+
oomd_update_cgroup_contexts_between_hashmaps(h_old, h_new);
212+
213+
assert_se(c_old = hashmap_get(h_old, "/0.slice"));
214+
assert_se(c_new = hashmap_get(h_new, "/0.slice"));
215+
assert_se(c_old->pgscan == c_new->last_pgscan);
216+
assert_se(c_old->mem_pressure_limit == c_new->mem_pressure_limit);
217+
assert_se(c_old->last_hit_mem_pressure_limit == c_new->last_hit_mem_pressure_limit);
218+
219+
assert_se(c_old = hashmap_get(h_old, "/1.slice"));
220+
assert_se(c_new = hashmap_get(h_new, "/1.slice"));
221+
assert_se(c_old->pgscan == c_new->last_pgscan);
222+
assert_se(c_old->mem_pressure_limit == c_new->mem_pressure_limit);
223+
assert_se(c_old->last_hit_mem_pressure_limit == c_new->last_hit_mem_pressure_limit);
224+
}
225+
179226
static void test_oomd_system_context_acquire(void) {
180227
_cleanup_(unlink_tempfilep) char path[] = "/oomdgetsysctxtestXXXXXX";
181228
OomdSystemContext ctx;
@@ -395,6 +442,7 @@ int main(void) {
395442

396443
test_setup_logging(LOG_DEBUG);
397444

445+
test_oomd_update_cgroup_contexts_between_hashmaps();
398446
test_oomd_system_context_acquire();
399447
test_oomd_pressure_above();
400448
test_oomd_memory_reclaim();

0 commit comments

Comments
 (0)
X Tutup