Debian Codegraph @

src/oom/oomd-manager.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "conf-parser-forward.h"
5 #include "constants.h"
6 #include "shared-forward.h"
7 #include "oomd-conf.h"
8 #include "oomd-util.h"
9
10 #define RULESET_DIRS ((const char* const*) CONF_PATHS_STRV("systemd/oomd/rules.d"))
11
12 /* Polling interval for monitoring stats */
13 #define SWAP_INTERVAL_USEC 150000 /* 0.15 seconds */
14 /* Pressure counters are lagging (~2 seconds) compared to swap so polling too frequently just wastes CPU */
15 #define MEM_PRESSURE_INTERVAL_USEC (1 * USEC_PER_SEC)
16
17 /* Rules evaluate both pressure and swap metrics; align on the slower-moving metric
18 * (pressure counters lag ~2s), so polling faster than 1s just wastes CPU. */
19 #define RULESETS_INTERVAL_USEC MEM_PRESSURE_INTERVAL_USEC
20
21 /* Take action if 10s of memory pressure > 60 for more than 30s. We use the "full" value from PSI so this is the
22 * percentage of time all tasks were delayed (i.e. unproductive).
23 * Generally 60 or higher might be acceptable for something like system.slice with no memory.high set; processes in
24 * system.slice are assumed to be less latency sensitive. */
25 #define DEFAULT_MEM_PRESSURE_DURATION_USEC (30 * USEC_PER_SEC)
26 #define DEFAULT_MEM_PRESSURE_LIMIT_PERCENT 60
27 #define DEFAULT_SWAP_USED_LIMIT_PERCENT 90
28
29 /* Only tackle candidates with large swap usage. */
30 #define THRESHOLD_SWAP_USED_PERCENT 5
31
32 #define RECLAIM_DURATION_USEC (30 * USEC_PER_SEC)
33 #define POST_ACTION_DELAY_USEC (15 * USEC_PER_SEC)
34
35 typedef enum OomdAction {
36 OOMD_ACTION_NONE,
37 OOMD_ACTION_KILL_ALL,
38 OOMD_ACTION_KILL_BY_PGSCAN,
39 OOMD_ACTION_KILL_BY_SWAP,
40 _OOMD_ACTION_MAX,
41 _OOMD_ACTION_INVALID = -EINVAL,
42 } OomdAction;
43
44 typedef struct OomdRuleset {
45 char *name;
46 int memory_pressure_above; /* permyriad (0-10000), or -1 for unset */
47 int swap_above; /* permyriad (0-10000), or -1 for unset */
48 OomdAction action;
49 usec_t lasting_usec;
50 Hashmap *start_times; /* key: cgroup path (char*) -> value: heap-allocated timestamp (usec_t*) */
51 bool warned_no_swap; /* latched once we've warned that kill-by-swap is misconfigured */
52 } OomdRuleset;
53
54 typedef struct Manager {
55 sd_bus *bus;
56 sd_event *event;
57
58 Hashmap *polkit_registry;
59
60 bool dry_run;
61 int swap_used_limit_permyriad;
62 loadavg_t default_mem_pressure_limit;
63 usec_t default_mem_pressure_duration_usec;
64
65 /* k: cgroup paths -> v: OomdCGroupContext
66 * Used to detect when to take action. */
67 Hashmap *monitored_swap_cgroup_contexts;
68 Hashmap *monitored_mem_pressure_cgroup_contexts;
69 Hashmap *monitored_mem_pressure_cgroup_contexts_candidates;
70 Hashmap *monitored_rules_cgroup_contexts;
71 Hashmap *monitored_rules_cgroup_contexts_candidates;
72
73 OomdSystemContext system_context;
74
75 usec_t mem_pressure_post_action_delay_start;
76 usec_t rules_post_action_delay_start;
77
78 sd_event_source *swap_context_event_source;
79 sd_event_source *mem_pressure_context_event_source;
80 sd_event_source *rules_context_event_source;
81
82 /* This varlink object is used to manage the subscription from systemd-oomd to PID1 which it uses to
83 * listen for changes in ManagedOOM settings (oomd client - systemd server). */
84 sd_varlink *varlink_client;
85 /* This varlink server object is used to manage systemd-oomd's varlink server which is used by user
86 * managers to report changes in ManagedOOM settings (oomd server - systemd client). */
87 sd_varlink_server *varlink_server;
88
89 usec_t prekill_timeout;
90 Set *kill_states; /* currently ongoing OomdKillState operations */
91 Hashmap *rulesets;
92 } Manager;
93
94 Manager* manager_free(Manager *m);
95 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
96
97 int manager_new(Manager **ret);
98
99 int manager_start(Manager *m, bool dry_run, int fd);
100
101 int manager_get_dump_string(Manager *m, char **ret);
102
103 CONFIG_PARSER_PROTOTYPE(config_parse_oomd_default);