1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-02-18 17:57:27 +03:00

oomd: Dump top offenders after a kill action

This hopefully makes it more transparent why a specific cgroup was
killed by systemd-oomd.
This commit is contained in:
Benjamin Berg 2022-02-03 15:50:31 +01:00
parent f83da717d9
commit 29f4185a9c
2 changed files with 41 additions and 2 deletions

View File

@ -216,9 +216,36 @@ int oomd_cgroup_kill(const char *path, bool recurse, bool dry_run) {
return set_size(pids_killed) != 0;
}
typedef void (*dump_candidate_func)(const OomdCGroupContext *ctx, FILE *f, const char *prefix);
static int dump_kill_candidates(OomdCGroupContext **sorted, int n, int dump_until, dump_candidate_func dump_func) {
/* Try dumping top offendors, ignoring any errors that might happen. */
_cleanup_free_ char *dump = NULL;
_cleanup_fclose_ FILE *f = NULL;
int r;
size_t size;
f = open_memstream_unlocked(&dump, &size);
if (!f)
return -errno;;
fprintf(f, "Considered %d cgroups for killing, top candidates were:\n", n);
for (int i = 0; i < dump_until; i++)
dump_func(sorted[i], f, "\t");
r = fflush_and_check(f);
if (r < 0)
return r;
f = safe_fclose(f);
return log_dump(LOG_INFO, dump);
}
int oomd_kill_by_pgscan_rate(Hashmap *h, const char *prefix, bool dry_run, char **ret_selected) {
_cleanup_free_ OomdCGroupContext **sorted = NULL;
int n, r, ret = 0;
int dump_until;
assert(h);
assert(ret_selected);
@ -227,6 +254,7 @@ int oomd_kill_by_pgscan_rate(Hashmap *h, const char *prefix, bool dry_run, char
if (n < 0)
return n;
dump_until = MIN(n, DUMP_ON_KILL_COUNT);
for (int i = 0; i < n; i++) {
/* Skip cgroups with no reclaim and memory usage; it won't alleviate pressure.
* Continue since there might be "avoid" cgroups at the end. */
@ -242,19 +270,24 @@ int oomd_kill_by_pgscan_rate(Hashmap *h, const char *prefix, bool dry_run, char
continue; /* Try to find something else to kill */
}
dump_until = MAX(dump_until, i);
char *selected = strdup(sorted[i]->path);
if (!selected)
return -ENOMEM;
*ret_selected = selected;
return r;
ret = r;
break;
}
dump_kill_candidates(sorted, n, dump_until, oomd_dump_memory_pressure_cgroup_context);
return ret;
}
int oomd_kill_by_swap_usage(Hashmap *h, uint64_t threshold_usage, bool dry_run, char **ret_selected) {
_cleanup_free_ OomdCGroupContext **sorted = NULL;
int n, r, ret = 0;
int dump_until;
assert(h);
assert(ret_selected);
@ -263,6 +296,7 @@ int oomd_kill_by_swap_usage(Hashmap *h, uint64_t threshold_usage, bool dry_run,
if (n < 0)
return n;
dump_until = MIN(n, DUMP_ON_KILL_COUNT);
/* Try to kill cgroups with non-zero swap usage until we either succeed in killing or we get to a cgroup with
* no swap usage. Threshold killing only cgroups with more than threshold swap usage. */
for (int i = 0; i < n; i++) {
@ -280,13 +314,17 @@ int oomd_kill_by_swap_usage(Hashmap *h, uint64_t threshold_usage, bool dry_run,
continue; /* Try to find something else to kill */
}
dump_until = MAX(dump_until, i);
char *selected = strdup(sorted[i]->path);
if (!selected)
return -ENOMEM;
*ret_selected = selected;
return r;
ret = r;
break;
}
dump_kill_candidates(sorted, n, dump_until, oomd_dump_swap_cgroup_context);
return ret;
}

View File

@ -7,6 +7,7 @@
#include "hashmap.h"
#include "psi-util.h"
#define DUMP_ON_KILL_COUNT 10
#define GROWING_SIZE_PERCENTILE 80
extern const struct hash_ops oomd_cgroup_ctx_hash_ops;