return c->pgscan - last_pgscan;
}
+bool oomd_mem_free_below(const OomdSystemContext *ctx, int threshold_permyriad) {
+ uint64_t mem_threshold;
+
+ assert(ctx);
+ assert(threshold_permyriad <= 10000);
+
+ mem_threshold = ctx->mem_total * threshold_permyriad / (uint64_t) 10000;
+ return (ctx->mem_total - ctx->mem_used) < mem_threshold;
+}
+
bool oomd_swap_free_below(const OomdSystemContext *ctx, int threshold_permyriad) {
uint64_t swap_threshold;
_cleanup_fclose_ FILE *f = NULL;
unsigned field_filled = 0;
OomdSystemContext ctx = {};
- uint64_t swap_free;
+ uint64_t mem_free, swap_free;
int r;
assert(proc_meminfo_path);
if (r == 0)
return -EINVAL;
- if ((word = startswith(line, "SwapTotal:"))) {
+ if ((word = startswith(line, "MemTotal:"))) {
field_filled |= 1U << 0;
+ r = convert_meminfo_value_to_uint64_bytes(word, &ctx.mem_total);
+ } else if ((word = startswith(line, "MemFree:"))) {
+ field_filled |= 1U << 1;
+ r = convert_meminfo_value_to_uint64_bytes(word, &mem_free);
+ } else if ((word = startswith(line, "SwapTotal:"))) {
+ field_filled |= 1U << 2;
r = convert_meminfo_value_to_uint64_bytes(word, &ctx.swap_total);
} else if ((word = startswith(line, "SwapFree:"))) {
- field_filled |= 1U << 1;
+ field_filled |= 1U << 3;
r = convert_meminfo_value_to_uint64_bytes(word, &swap_free);
} else
continue;
if (r < 0)
return log_debug_errno(r, "Error converting '%s' from %s to uint64_t: %m", line, proc_meminfo_path);
- if (field_filled == 3U)
+ if (field_filled == 15U)
break;
}
- if (field_filled != 3U)
+ if (field_filled != 15U)
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "%s is missing expected fields", proc_meminfo_path);
+ if (mem_free > ctx.mem_total)
+ return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
+ "MemFree (%" PRIu64 ") cannot be greater than MemTotal (%" PRIu64 ") %m",
+ mem_free,
+ ctx.mem_total);
+
if (swap_free > ctx.swap_total)
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
"SwapFree (%" PRIu64 ") cannot be greater than SwapTotal (%" PRIu64 ") %m",
swap_free,
ctx.swap_total);
+ ctx.mem_used = ctx.mem_total - mem_free;
ctx.swap_used = ctx.swap_total - swap_free;
*ret = ctx;
}
void oomd_dump_system_context(const OomdSystemContext *ctx, FILE *f, const char *prefix) {
- char used[FORMAT_BYTES_MAX], total[FORMAT_BYTES_MAX];
+ char mem_used[FORMAT_BYTES_MAX], mem_total[FORMAT_BYTES_MAX];
+ char swap_used[FORMAT_BYTES_MAX], swap_total[FORMAT_BYTES_MAX];
assert(ctx);
assert(f);
fprintf(f,
+ "%sMemory: Used: %s Total: %s\n"
"%sSwap: Used: %s Total: %s\n",
strempty(prefix),
- format_bytes(used, sizeof(used), ctx->swap_used),
- format_bytes(total, sizeof(total), ctx->swap_total));
+ format_bytes(mem_used, sizeof(mem_used), ctx->mem_used),
+ format_bytes(mem_total, sizeof(mem_total), ctx->mem_total),
+ strempty(prefix),
+ format_bytes(swap_used, sizeof(swap_used), ctx->swap_used),
+ format_bytes(swap_total, sizeof(swap_total), ctx->swap_total));
}
"SwapFree: 7604 kB bad\n", WRITE_STRING_FILE_CREATE) == 0);
assert_se(oomd_system_context_acquire(path, &ctx) == -EINVAL);
- assert_se(oomd_system_context_acquire("/proc/meminfo", &ctx) == 0);
- assert_se(ctx.swap_total > 0);
- assert_se(ctx.swap_used <= ctx.swap_total);
-
assert_se(write_string_file(path, "MemTotal: 32495256 kB\n"
"MemFree: 9880512 kB\n"
"MemAvailable: 21777088 kB\n"
"SwapTotal: 8388604 kB\n"
"SwapFree: 7604 kB\n", WRITE_STRING_FILE_CREATE) == 0);
assert_se(oomd_system_context_acquire(path, &ctx) == 0);
+ assert_se(ctx.mem_total == 33275142144);
+ assert_se(ctx.mem_used == 23157497856);
assert_se(ctx.swap_total == 8589930496);
assert_se(ctx.swap_used == 8582144000);
}
assert_se(c->mem_pressure_limit_hit_start == 0);
}
-static void test_oomd_swap_free_below(void) {
+static void test_oomd_mem_and_swap_free_below(void) {
OomdSystemContext ctx = (OomdSystemContext) {
+ .mem_total = 20971512 * 1024U,
+ .mem_used = 3310136 * 1024U,
.swap_total = 20971512 * 1024U,
.swap_used = 20971440 * 1024U,
};
+ assert_se(oomd_mem_free_below(&ctx, 2000) == false);
assert_se(oomd_swap_free_below(&ctx, 2000) == true);
ctx = (OomdSystemContext) {
+ .mem_total = 20971512 * 1024U,
+ .mem_used = 20971440 * 1024U,
.swap_total = 20971512 * 1024U,
.swap_used = 3310136 * 1024U,
};
+ assert_se(oomd_mem_free_below(&ctx, 2000) == true);
assert_se(oomd_swap_free_below(&ctx, 2000) == false);
ctx = (OomdSystemContext) {
+ .mem_total = 0,
+ .mem_used = 0,
.swap_total = 0,
.swap_used = 0,
};
+ assert_se(oomd_mem_free_below(&ctx, 2000) == false);
assert_se(oomd_swap_free_below(&ctx, 2000) == false);
}
test_oomd_update_cgroup_contexts_between_hashmaps();
test_oomd_system_context_acquire();
test_oomd_pressure_above();
- test_oomd_swap_free_below();
+ test_oomd_mem_and_swap_free_below();
test_oomd_sort_cgroups();
/* The following tests operate on live cgroups */