]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/oom/oomd-util.h
oom: use CMP() macro
[thirdparty/systemd.git] / src / oom / oomd-util.h
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
61ff7397
AZ
2#pragma once
3
4#include <stdbool.h>
5
6#include "hashmap.h"
7#include "psi-util.h"
8
9#define GROWING_SIZE_PERCENTILE 80
10
11extern const struct hash_ops oomd_cgroup_ctx_hash_ops;
12
13typedef struct OomdCGroupContext OomdCGroupContext;
14typedef struct OomdSystemContext OomdSystemContext;
15
16typedef int (oomd_compare_t)(OomdCGroupContext * const *, OomdCGroupContext * const *);
17
18struct OomdCGroupContext {
19 char *path;
20
21 ResourcePressure memory_pressure;
22
23 uint64_t current_memory_usage;
24
25 uint64_t memory_min;
26 uint64_t memory_low;
27 uint64_t swap_usage;
28
29 uint64_t last_pgscan;
30 uint64_t pgscan;
31
32 /* These are only used by oomd_pressure_above for acting on high memory pressure. */
33 loadavg_t mem_pressure_limit;
34 usec_t last_hit_mem_pressure_limit;
35};
36
37struct OomdSystemContext {
38 uint64_t swap_total;
39 uint64_t swap_used;
40};
41
42OomdCGroupContext *oomd_cgroup_context_free(OomdCGroupContext *ctx);
43DEFINE_TRIVIAL_CLEANUP_FUNC(OomdCGroupContext*, oomd_cgroup_context_free);
44
45/* All hashmaps used with these functions are expected to be of the form
46 * key: cgroup paths -> value: OomdCGroupContext. */
47
48/* Scans all the OomdCGroupContexts in `h` and returns 1 and a set of pointers to those OomdCGroupContexts in `ret`
49 * if any of them have exceeded their supplied memory pressure limits for the `duration` length of time.
50 * `last_hit_mem_pressure_limit` is updated accordingly for each entry when the limit is exceeded, and when it returns
51 * below the limit.
52 * Returns 0 and sets `ret` to an empty set if no entries exceeded limits for `duration`.
53 * Returns -ENOMEM for allocation errors. */
54int oomd_pressure_above(Hashmap *h, usec_t duration, Set **ret);
55
56/* Sum up current OomdCGroupContexts' pgscan values and last interval's pgscan values in `h`. Returns true if the
57 * current sum is higher than the last interval's sum (there was some reclaim activity). */
58bool oomd_memory_reclaim(Hashmap *h);
59
60/* Returns true if the amount of swap free is below the percentage of swap specified by `threshold_percent`. */
61bool oomd_swap_free_below(const OomdSystemContext *ctx, uint64_t threshold_percent);
62
63static inline int compare_pgscan(OomdCGroupContext * const *c1, OomdCGroupContext * const *c2) {
64 assert(c1);
65 assert(c2);
66
95ca39f0 67 return CMP((*c2)->pgscan, (*c1)->pgscan);
61ff7397
AZ
68}
69
70static inline int compare_swap_usage(OomdCGroupContext * const *c1, OomdCGroupContext * const *c2) {
71 assert(c1);
72 assert(c2);
73
95ca39f0 74 return CMP((*c2)->swap_usage, (*c1)->swap_usage);
61ff7397
AZ
75}
76
77/* Get an array of OomdCGroupContexts from `h`, qsorted from largest to smallest values according to `compare_func`.
78 * If `prefix` is not NULL, only include OomdCGroupContexts whose paths start with prefix. Otherwise all paths are sorted.
79 * Returns the number of sorted items; negative on error. */
80int oomd_sort_cgroup_contexts(Hashmap *h, oomd_compare_t compare_func, const char *prefix, OomdCGroupContext ***ret);
81
82/* Returns a negative value on error, 0 if no processes were killed, or 1 if processes were killed. */
83int oomd_cgroup_kill(const char *path, bool recurse, bool dry_run);
84
85/* The following oomd_kill_by_* functions return 1 if processes were killed, or negative otherwise. */
86/* If `prefix` is supplied, only cgroups whose paths start with `prefix` are eligible candidates. Otherwise,
87 * everything in `h` is a candidate. */
88int oomd_kill_by_pgscan(Hashmap *h, const char *prefix, bool dry_run);
89int oomd_kill_by_swap_usage(Hashmap *h, bool dry_run);
90
91int oomd_cgroup_context_acquire(const char *path, OomdCGroupContext **ret);
92int oomd_system_context_acquire(const char *proc_swaps_path, OomdSystemContext *ret);
93
94/* Get the OomdCGroupContext of `path` and insert it into `new_h`. The key for the inserted context will be `path`.
95 *
96 * `old_h` is used to get data used to calculate prior interval information. `old_h` can be NULL in which case there
97 * was no prior data to reference. */
98int oomd_insert_cgroup_context(Hashmap *old_h, Hashmap *new_h, const char *path);
5c616ecf
AZ
99
100void oomd_dump_swap_cgroup_context(const OomdCGroupContext *ctx, FILE *f, const char *prefix);
101void oomd_dump_memory_pressure_cgroup_context(const OomdCGroupContext *ctx, FILE *f, const char *prefix);
102void oomd_dump_system_context(const OomdSystemContext *ctx, FILE *f, const char *prefix);