| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "cgroup-util.h" |
| 5 |
#include "shared-forward.h" |
| 6 |
#include "psi-util.h" |
| 7 |
|
| 8 |
#define DUMP_ON_KILL_COUNT 10u |
| 9 |
#define GROWING_SIZE_PERCENTILE 80 |
| 10 |
|
| 11 |
extern const struct hash_ops oomd_cgroup_ctx_hash_ops; |
| 12 |
|
| 13 |
struct Manager; |
| 14 |
|
| 15 |
typedef struct OomdCGroupContext OomdCGroupContext; |
| 16 |
typedef struct OomdSystemContext OomdSystemContext; |
| 17 |
typedef struct Manager Manager; |
| 18 |
|
| 19 |
typedef int (oomd_compare_t)(OomdCGroupContext * const *, OomdCGroupContext * const *); |
| 20 |
|
| 21 |
struct OomdCGroupContext { |
| 22 |
unsigned n_ref; |
| 23 |
char *path; |
| 24 |
|
| 25 |
ResourcePressure memory_pressure; |
| 26 |
|
| 27 |
uint64_t current_memory_usage; |
| 28 |
|
| 29 |
uint64_t memory_min; |
| 30 |
uint64_t memory_low; |
| 31 |
uint64_t swap_usage; |
| 32 |
|
| 33 |
uint64_t last_pgscan; |
| 34 |
uint64_t pgscan; |
| 35 |
|
| 36 |
ManagedOOMPreference preference; |
| 37 |
|
| 38 |
/* These are only used for acting on high memory pressure. */ |
| 39 |
loadavg_t mem_pressure_limit; |
| 40 |
usec_t mem_pressure_limit_hit_start; |
| 41 |
usec_t last_had_mem_reclaim; |
| 42 |
usec_t mem_pressure_duration_usec; |
| 43 |
char **rules; |
| 44 |
}; |
| 45 |
|
| 46 |
struct OomdSystemContext { |
| 47 |
uint64_t mem_total; |
| 48 |
uint64_t mem_used; |
| 49 |
uint64_t swap_total; |
| 50 |
uint64_t swap_used; |
| 51 |
}; |
| 52 |
|
| 53 |
OomdCGroupContext *oomd_cgroup_context_ref(OomdCGroupContext *p); |
| 54 |
OomdCGroupContext *oomd_cgroup_context_unref(OomdCGroupContext *p); |
| 55 |
DEFINE_TRIVIAL_CLEANUP_FUNC(OomdCGroupContext*, oomd_cgroup_context_unref); |
| 56 |
|
| 57 |
/* All hashmaps used with these functions are expected to be of the form |
| 58 |
* key: cgroup paths -> value: OomdCGroupContext. */ |
| 59 |
|
| 60 |
/* Scans all the OomdCGroupContexts in `h` and returns 1 and a set of pointers to those OomdCGroupContexts in `ret` |
| 61 |
* if any of them have exceeded their supplied memory pressure limits for the `ctx->mem_pressure_duration_usec` length of time. |
| 62 |
* `mem_pressure_limit_hit_start` is updated accordingly for the first time the limit is exceeded, and when it returns |
| 63 |
* below the limit. |
| 64 |
* Returns 0 and sets `ret` to an empty set if no entries exceeded limits for `ctx->mem_pressure_duration_usec`. |
| 65 |
* Returns -ENOMEM for allocation errors. */ |
| 66 |
int oomd_pressure_above(Hashmap *h, Set **ret); |
| 67 |
|
| 68 |
/* Returns true if the amount of memory available (see proc(5)) is below the permyriad of memory specified by `threshold_permyriad`. */ |
| 69 |
bool oomd_mem_available_below(const OomdSystemContext *ctx, int threshold_permyriad); |
| 70 |
|
| 71 |
/* Returns true if the amount of swap free is below the permyriad of swap specified by `threshold_permyriad`. */ |
| 72 |
bool oomd_swap_free_below(const OomdSystemContext *ctx, int threshold_permyriad); |
| 73 |
|
| 74 |
/* Returns pgscan - last_pgscan, accounting for corner cases. */ |
| 75 |
uint64_t oomd_pgscan_rate(const OomdCGroupContext *c); |
| 76 |
|
| 77 |
/* The compare functions will sort from largest to smallest, putting all the contexts with "avoid" at the end |
| 78 |
* (after the smallest values). */ |
| 79 |
static inline int compare_pgscan_rate_and_memory_usage(OomdCGroupContext * const *c1, OomdCGroupContext * const *c2) { |
| 80 |
uint64_t diff1, diff2; |
| 81 |
int r; |
| 82 |
|
| 83 |
assert(c1); |
| 84 |
assert(c2); |
| 85 |
|
| 86 |
r = CMP((*c1)->preference, (*c2)->preference); |
| 87 |
if (r != 0) |
| 88 |
return r; |
| 89 |
|
| 90 |
diff1 = oomd_pgscan_rate(*c1); |
| 91 |
diff2 = oomd_pgscan_rate(*c2); |
| 92 |
r = CMP(diff2, diff1); |
| 93 |
if (r != 0) |
| 94 |
return r; |
| 95 |
|
| 96 |
return CMP((*c2)->current_memory_usage, (*c1)->current_memory_usage); |
| 97 |
} |
| 98 |
|
| 99 |
static inline int compare_swap_usage(OomdCGroupContext * const *c1, OomdCGroupContext * const *c2) { |
| 100 |
int r; |
| 101 |
|
| 102 |
assert(c1); |
| 103 |
assert(c2); |
| 104 |
|
| 105 |
r = CMP((*c1)->preference, (*c2)->preference); |
| 106 |
if (r != 0) |
| 107 |
return r; |
| 108 |
|
| 109 |
return CMP((*c2)->swap_usage, (*c1)->swap_usage); |
| 110 |
} |
| 111 |
|
| 112 |
/* Get an array of OomdCGroupContexts from `h`, qsorted from largest to smallest values according to `compare_func`. |
| 113 |
* If `prefix` is not NULL, only include OomdCGroupContexts whose paths start with prefix. Otherwise all paths are sorted. |
| 114 |
* Returns the number of sorted items; negative on error. */ |
| 115 |
int oomd_sort_cgroup_contexts(Hashmap *h, oomd_compare_t compare_func, const char *prefix, OomdCGroupContext ***ret); |
| 116 |
|
| 117 |
/* If the cgroup is owned by root, or the cgroups represented by `ctx` and |
| 118 |
* `prefix` are owned by the same user, then set `ctx->preference` using the |
| 119 |
* `user.oomd_avoid` and `user.oomd_omit` xattrs. Otherwise, set |
| 120 |
* `ctx->preference` to MANAGED_OOM_PREFERENCE_NONE. |
| 121 |
* |
| 122 |
* If `prefix` is NULL or the empty string, it is treated as root. If `prefix` |
| 123 |
* does not specify an ancestor cgroup of `ctx`, -EINVAL is returned. Returns |
| 124 |
* negative on all other errors. */ |
| 125 |
int oomd_fetch_cgroup_oom_preference(OomdCGroupContext *ctx, const char *prefix); |
| 126 |
|
| 127 |
/* Returns a negative value on error, 0 if no processes were killed, or 1 if processes were killed. */ |
| 128 |
int oomd_cgroup_kill(Manager *m, OomdCGroupContext *ctx, bool recurse, const char *reason); |
| 129 |
int oomd_cgroup_kill_mark(Manager *m, OomdCGroupContext *ctx, const char *reason); |
| 130 |
|
| 131 |
/* The following oomd_kill_by_* functions return 1 if processes were killed, or negative otherwise. */ |
| 132 |
/* If `prefix` is supplied, only cgroups whose paths start with `prefix` are eligible candidates. Otherwise, |
| 133 |
* everything in `h` is a candidate. |
| 134 |
* Returns the killed cgroup in ret_selected. */ |
| 135 |
int oomd_select_by_pgscan_rate(Hashmap *h, const char *prefix, OomdCGroupContext **ret_selected); |
| 136 |
int oomd_select_by_swap_usage(Hashmap *h, const char *prefix, uint64_t threshold_usage, OomdCGroupContext **ret_selected); |
| 137 |
|
| 138 |
int oomd_cgroup_context_acquire(const char *path, OomdCGroupContext **ret); |
| 139 |
int oomd_system_context_acquire(const char *proc_meminfo_path, OomdSystemContext *ret); |
| 140 |
|
| 141 |
/* Get the OomdCGroupContext of `path` and insert it into `new_h`. The key for the inserted context will be `path`. |
| 142 |
* |
| 143 |
* `old_h` is used to get data used to calculate prior interval information. `old_h` can be NULL in which case there |
| 144 |
* was no prior data to reference. */ |
| 145 |
int oomd_insert_cgroup_context(Hashmap *old_h, Hashmap *new_h, const char *path); |
| 146 |
|
| 147 |
/* Update each OomdCGroupContext in `curr_h` with prior interval information from `old_h`. */ |
| 148 |
void oomd_update_cgroup_contexts_between_hashmaps(Hashmap *old_h, Hashmap *curr_h); |
| 149 |
|
| 150 |
void oomd_dump_swap_cgroup_context(const OomdCGroupContext *ctx, FILE *f, const char *prefix); |
| 151 |
void oomd_dump_memory_pressure_cgroup_context(const OomdCGroupContext *ctx, FILE *f, const char *prefix); |
| 152 |
void oomd_dump_system_context(const OomdSystemContext *ctx, FILE *f, const char *prefix); |