| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-bus.h" |
| 4 |
#include "sd-daemon.h" |
| 5 |
#include "sd-event.h" |
| 6 |
#include "sd-json.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "bus-log-control-api.h" |
| 10 |
#include "bus-object.h" |
| 11 |
#include "bus-util.h" |
| 12 |
#include "cgroup-util.h" |
| 13 |
#include "constants.h" |
| 14 |
#include "daemon-util.h" |
| 15 |
#include "fd-util.h" |
| 16 |
#include "format-util.h" |
| 17 |
#include "json-util.h" |
| 18 |
#include "memstream-util.h" |
| 19 |
#include "oomd-conf.h" |
| 20 |
#include "oomd-manager.h" |
| 21 |
#include "oomd-manager-bus.h" |
| 22 |
#include "parse-util.h" |
| 23 |
#include "path-util.h" |
| 24 |
#include "percent-util.h" |
| 25 |
#include "set.h" |
| 26 |
#include "string-util.h" |
| 27 |
#include "strv.h" |
| 28 |
#include "time-util.h" |
| 29 |
#include "varlink-io.systemd.oom.h" |
| 30 |
#include "varlink-io.systemd.service.h" |
| 31 |
#include "varlink-util.h" |
| 32 |
|
| 33 |
typedef struct ManagedOOMMessage { |
| 34 |
ManagedOOMMode mode; |
| 35 |
char *path; |
| 36 |
char *property; |
| 37 |
uint32_t limit; |
| 38 |
usec_t duration; |
| 39 |
char **rules; |
| 40 |
} ManagedOOMMessage; |
| 41 |
|
| 42 |
static void managed_oom_message_destroy(ManagedOOMMessage *message) { |
| 43 |
assert(message); |
| 44 |
free(message->path); |
| 45 |
free(message->property); |
| 46 |
strv_free(message->rules); |
| 47 |
} |
| 48 |
|
| 49 |
static JSON_DISPATCH_ENUM_DEFINE(dispatch_managed_oom_mode, ManagedOOMMode, managed_oom_mode_from_string); |
| 50 |
|
| 51 |
static int process_managed_oom_message(Manager *m, uid_t uid, sd_json_variant *parameters) { |
| 52 |
sd_json_variant *c, *cgroups; |
| 53 |
int r; |
| 54 |
|
| 55 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 56 |
{ "mode", SD_JSON_VARIANT_STRING, dispatch_managed_oom_mode, offsetof(ManagedOOMMessage, mode), SD_JSON_MANDATORY }, |
| 57 |
{ "path", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(ManagedOOMMessage, path), SD_JSON_MANDATORY }, |
| 58 |
{ "property", SD_JSON_VARIANT_STRING, sd_json_dispatch_string, offsetof(ManagedOOMMessage, property), SD_JSON_MANDATORY }, |
| 59 |
{ "limit", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint32, offsetof(ManagedOOMMessage, limit), 0 }, |
| 60 |
{ "duration", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(ManagedOOMMessage, duration), 0 }, |
| 61 |
{ "rules", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_strv, offsetof(ManagedOOMMessage, rules), 0 }, |
| 62 |
{}, |
| 63 |
}; |
| 64 |
|
| 65 |
assert(m); |
| 66 |
assert(parameters); |
| 67 |
|
| 68 |
cgroups = sd_json_variant_by_key(parameters, "cgroups"); |
| 69 |
if (!cgroups) |
| 70 |
return -EINVAL; |
| 71 |
|
| 72 |
/* Skip malformed elements and keep processing in case the others are good */ |
| 73 |
JSON_VARIANT_ARRAY_FOREACH(c, cgroups) { |
| 74 |
_cleanup_(managed_oom_message_destroy) ManagedOOMMessage message = { |
| 75 |
.duration = USEC_INFINITY, |
| 76 |
}; |
| 77 |
OomdCGroupContext *ctx; |
| 78 |
Hashmap *monitor_hm; |
| 79 |
loadavg_t limit; |
| 80 |
usec_t duration; |
| 81 |
|
| 82 |
if (!sd_json_variant_is_object(c)) |
| 83 |
continue; |
| 84 |
|
| 85 |
r = sd_json_dispatch(c, dispatch_table, 0, &message); |
| 86 |
if (r == -ENOMEM) |
| 87 |
return r; |
| 88 |
if (r < 0) |
| 89 |
continue; |
| 90 |
|
| 91 |
if (!path_is_normalized(empty_to_root(message.path))) { |
| 92 |
log_debug("Received non-normalized cgroup path '%s', ignoring.", message.path); |
| 93 |
continue; |
| 94 |
} |
| 95 |
|
| 96 |
if (uid != 0) { |
| 97 |
uid_t cg_uid; |
| 98 |
|
| 99 |
r = cg_path_get_owner_uid(message.path, &cg_uid); |
| 100 |
if (r < 0) { |
| 101 |
log_debug_errno(r, "Failed to get cgroup %s owner uid: %m", message.path); |
| 102 |
continue; |
| 103 |
} |
| 104 |
|
| 105 |
/* Let's not be lenient for permission errors and skip processing if we receive an |
| 106 |
* update for a cgroup that doesn't belong to the user. */ |
| 107 |
if (uid != cg_uid) |
| 108 |
return log_error_errno(SYNTHETIC_ERRNO(EPERM), |
| 109 |
"cgroup path owner UID does not match sender uid " |
| 110 |
"(" UID_FMT " != " UID_FMT ")", uid, cg_uid); |
| 111 |
} |
| 112 |
|
| 113 |
if (streq(message.property, "ManagedOOMSwap")) |
| 114 |
monitor_hm = m->monitored_swap_cgroup_contexts; |
| 115 |
else if (streq(message.property, "OOMRules")) |
| 116 |
monitor_hm = m->monitored_rules_cgroup_contexts; |
| 117 |
else if (streq(message.property, "ManagedOOMMemoryPressure")) |
| 118 |
monitor_hm = m->monitored_mem_pressure_cgroup_contexts; |
| 119 |
else { |
| 120 |
log_debug("Unknown property '%s', ignoring.", message.property); |
| 121 |
continue; |
| 122 |
} |
| 123 |
|
| 124 |
if (message.mode == MANAGED_OOM_AUTO) { |
| 125 |
(void) oomd_cgroup_context_unref(hashmap_remove(monitor_hm, empty_to_root(message.path))); |
| 126 |
|
| 127 |
/* Clean up start_times entries for this cgroup across all rulesets |
| 128 |
* to prevent stale timers from causing premature action triggers |
| 129 |
* if the cgroup re-subscribes later. */ |
| 130 |
if (streq(message.property, "OOMRules")) { |
| 131 |
OomdRuleset *ruleset; |
| 132 |
HASHMAP_FOREACH(ruleset, m->rulesets) { |
| 133 |
_cleanup_free_ char *key = NULL; |
| 134 |
free(hashmap_remove2(ruleset->start_times, empty_to_root(message.path), (void **) &key)); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
continue; |
| 139 |
} |
| 140 |
|
| 141 |
limit = m->default_mem_pressure_limit; |
| 142 |
|
| 143 |
if (streq(message.property, "ManagedOOMMemoryPressure") && message.limit > 0) { |
| 144 |
int permyriad = UINT32_SCALE_TO_PERMYRIAD(message.limit); |
| 145 |
|
| 146 |
r = store_loadavg_fixed_point(permyriad / 100LU, permyriad % 100LU, &limit); |
| 147 |
if (r < 0) |
| 148 |
continue; |
| 149 |
} |
| 150 |
|
| 151 |
if (streq(message.property, "ManagedOOMMemoryPressure") && message.duration != USEC_INFINITY) |
| 152 |
duration = message.duration; |
| 153 |
else |
| 154 |
duration = m->default_mem_pressure_duration_usec; |
| 155 |
|
| 156 |
/* For OOMRules, only insert/update if rules are actually provided */ |
| 157 |
if (streq(message.property, "OOMRules")) { |
| 158 |
if (strv_isempty(message.rules)) |
| 159 |
continue; |
| 160 |
|
| 161 |
/* Avoid re-reading memory.pressure/pgscan/etc. on every OOMRules update for a |
| 162 |
* cgroup we already track — fetch the existing context first and only acquire |
| 163 |
* a fresh one if the cgroup is new. */ |
| 164 |
ctx = hashmap_get(monitor_hm, empty_to_root(message.path)); |
| 165 |
if (!ctx) { |
| 166 |
r = oomd_insert_cgroup_context(NULL, monitor_hm, message.path); |
| 167 |
if (r == -ENOMEM) |
| 168 |
return r; |
| 169 |
if (r < 0) { |
| 170 |
log_debug_errno(r, "Failed to insert message, ignoring: %m"); |
| 171 |
continue; |
| 172 |
} |
| 173 |
ctx = hashmap_get(monitor_hm, empty_to_root(message.path)); |
| 174 |
} |
| 175 |
|
| 176 |
if (ctx) { |
| 177 |
/* For each rule being dropped from this cgroup's subscription, |
| 178 |
* remove its start_times entry so the timer doesn't linger. */ |
| 179 |
STRV_FOREACH(old_rule, ctx->rules) { |
| 180 |
if (strv_contains(message.rules, *old_rule)) |
| 181 |
continue; |
| 182 |
OomdRuleset *dropped = hashmap_get(m->rulesets, *old_rule); |
| 183 |
if (!dropped) |
| 184 |
continue; |
| 185 |
_cleanup_free_ char *key = NULL; |
| 186 |
free(hashmap_remove2(dropped->start_times, empty_to_root(message.path), (void**) &key)); |
| 187 |
} |
| 188 |
|
| 189 |
strv_free_and_replace(ctx->rules, message.rules); |
| 190 |
|
| 191 |
/* Defensively deduplicate: the DBus setter and config parser both |
| 192 |
* dedupe, but another varlink client could in principle send |
| 193 |
* duplicates, which would cause redundant per-interval evaluation. */ |
| 194 |
strv_uniq(ctx->rules); |
| 195 |
|
| 196 |
/* Warn about any referenced rules that don't exist. Done here |
| 197 |
* (once per subscription change) rather than per-interval to avoid |
| 198 |
* log spam when a unit references a missing ruleset. */ |
| 199 |
STRV_FOREACH(new_rule, ctx->rules) |
| 200 |
if (!hashmap_contains(m->rulesets, *new_rule)) |
| 201 |
log_warning("Cgroup %s references undefined ruleset '%s', it will be ignored.", |
| 202 |
ctx->path, *new_rule); |
| 203 |
} |
| 204 |
continue; |
| 205 |
} |
| 206 |
|
| 207 |
r = oomd_insert_cgroup_context(NULL, monitor_hm, message.path); |
| 208 |
if (r == -ENOMEM) |
| 209 |
return r; |
| 210 |
if (r < 0 && r != -EEXIST) |
| 211 |
log_debug_errno(r, "Failed to insert message, ignoring: %m"); |
| 212 |
|
| 213 |
/* Always update the limit in case it was changed. For non-memory pressure detection the value is |
| 214 |
* ignored so always updating it here is not a problem. */ |
| 215 |
ctx = hashmap_get(monitor_hm, empty_to_root(message.path)); |
| 216 |
if (ctx) { |
| 217 |
ctx->mem_pressure_limit = limit; |
| 218 |
ctx->mem_pressure_duration_usec = duration; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
/* Toggle wake-ups for "ManagedOOMSwap" if entries are present. */ |
| 223 |
r = sd_event_source_set_enabled(m->swap_context_event_source, |
| 224 |
hashmap_isempty(m->monitored_swap_cgroup_contexts) ? SD_EVENT_OFF : SD_EVENT_ON); |
| 225 |
if (r < 0) |
| 226 |
return log_error_errno(r, "Failed to toggle enabled state of swap context source: %m"); |
| 227 |
|
| 228 |
/* Toggle wake-ups for "OOMRules" if entries are present. */ |
| 229 |
r = sd_event_source_set_enabled(m->rules_context_event_source, |
| 230 |
hashmap_isempty(m->monitored_rules_cgroup_contexts) ? SD_EVENT_OFF : SD_EVENT_ON); |
| 231 |
if (r < 0) |
| 232 |
return log_error_errno(r, "Failed to toggle enabled state of rules context source: %m"); |
| 233 |
|
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
static int process_managed_oom_request( |
| 238 |
sd_varlink *link, |
| 239 |
sd_json_variant *parameters, |
| 240 |
sd_varlink_method_flags_t flags, |
| 241 |
void *userdata) { |
| 242 |
Manager *m = ASSERT_PTR(userdata); |
| 243 |
uid_t uid; |
| 244 |
int r; |
| 245 |
|
| 246 |
r = sd_varlink_get_peer_uid(link, &uid); |
| 247 |
if (r < 0) |
| 248 |
return log_error_errno(r, "Failed to get varlink peer uid: %m"); |
| 249 |
|
| 250 |
return process_managed_oom_message(m, uid, parameters); |
| 251 |
} |
| 252 |
|
| 253 |
static int process_managed_oom_reply( |
| 254 |
sd_varlink *link, |
| 255 |
sd_json_variant *parameters, |
| 256 |
const char *error_id, |
| 257 |
sd_varlink_reply_flags_t flags, |
| 258 |
void *userdata) { |
| 259 |
Manager *m = ASSERT_PTR(userdata); |
| 260 |
uid_t uid; |
| 261 |
int r; |
| 262 |
|
| 263 |
if (error_id) { |
| 264 |
r = -EIO; |
| 265 |
log_debug("Error getting ManagedOOM cgroups: %s", error_id); |
| 266 |
goto finish; |
| 267 |
} |
| 268 |
|
| 269 |
r = sd_varlink_get_peer_uid(link, &uid); |
| 270 |
if (r < 0) { |
| 271 |
log_error_errno(r, "Failed to get varlink peer uid: %m"); |
| 272 |
goto finish; |
| 273 |
} |
| 274 |
|
| 275 |
r = process_managed_oom_message(m, uid, parameters); |
| 276 |
|
| 277 |
finish: |
| 278 |
if (!FLAGS_SET(flags, SD_VARLINK_REPLY_CONTINUES)) |
| 279 |
m->varlink_client = sd_varlink_close_unref(link); |
| 280 |
|
| 281 |
return r; |
| 282 |
} |
| 283 |
|
| 284 |
/* Fill 'new_h' with 'path's descendant OomdCGroupContexts. Only include descendant cgroups that are possible |
| 285 |
* candidates for action. That is, only leaf cgroups or cgroups with memory.oom.group set to "1". |
| 286 |
* |
| 287 |
* This function ignores most errors in order to handle cgroups that may have been cleaned up while |
| 288 |
* populating the hashmap. |
| 289 |
* |
| 290 |
* 'new_h' is of the form { key: cgroup paths -> value: OomdCGroupContext } */ |
| 291 |
static int recursively_get_cgroup_context(Hashmap *new_h, const char *path) { |
| 292 |
_cleanup_free_ char *subpath = NULL; |
| 293 |
_cleanup_closedir_ DIR *d = NULL; |
| 294 |
int r; |
| 295 |
|
| 296 |
assert(new_h); |
| 297 |
assert(path); |
| 298 |
|
| 299 |
r = cg_enumerate_subgroups(path, &d); |
| 300 |
if (r < 0) |
| 301 |
return r; |
| 302 |
|
| 303 |
r = cg_read_subgroup(d, &subpath); |
| 304 |
if (r < 0) |
| 305 |
return r; |
| 306 |
else if (r == 0) { /* No subgroups? We're a leaf node */ |
| 307 |
r = oomd_insert_cgroup_context(NULL, new_h, path); |
| 308 |
if (r == -ENOMEM) |
| 309 |
return r; |
| 310 |
if (r < 0) |
| 311 |
log_debug_errno(r, "Failed to insert context for %s, ignoring: %m", path); |
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
do { |
| 316 |
_cleanup_free_ char *cg_path = NULL; |
| 317 |
|
| 318 |
cg_path = path_join(empty_to_root(path), subpath); |
| 319 |
if (!cg_path) |
| 320 |
return -ENOMEM; |
| 321 |
|
| 322 |
subpath = mfree(subpath); |
| 323 |
|
| 324 |
r = cg_get_attribute_as_bool(cg_path, "memory.oom.group"); |
| 325 |
/* The cgroup might be gone. Skip it as a candidate since we can't get information on it. */ |
| 326 |
if (r == -ENOMEM) |
| 327 |
return r; |
| 328 |
if (r < 0) { |
| 329 |
log_debug_errno(r, "Failed to read memory.oom.group from %s, ignoring: %m", cg_path); |
| 330 |
continue; |
| 331 |
} |
| 332 |
if (r > 0) |
| 333 |
r = oomd_insert_cgroup_context(NULL, new_h, cg_path); |
| 334 |
else |
| 335 |
r = recursively_get_cgroup_context(new_h, cg_path); |
| 336 |
if (r == -ENOMEM) |
| 337 |
return r; |
| 338 |
if (r < 0) |
| 339 |
log_debug_errno(r, "Failed to insert or recursively get from %s, ignoring: %m", cg_path); |
| 340 |
} while ((r = cg_read_subgroup(d, &subpath)) > 0); |
| 341 |
|
| 342 |
return 0; |
| 343 |
} |
| 344 |
|
| 345 |
static int update_monitored_cgroup_contexts(Hashmap **monitored_cgroups) { |
| 346 |
_cleanup_hashmap_free_ Hashmap *new_base = NULL; |
| 347 |
OomdCGroupContext *ctx; |
| 348 |
int r; |
| 349 |
|
| 350 |
assert(monitored_cgroups); |
| 351 |
|
| 352 |
new_base = hashmap_new(&oomd_cgroup_ctx_hash_ops); |
| 353 |
if (!new_base) |
| 354 |
return -ENOMEM; |
| 355 |
|
| 356 |
HASHMAP_FOREACH(ctx, *monitored_cgroups) { |
| 357 |
/* Skip most errors since the cgroup we're trying to update might not exist anymore. */ |
| 358 |
r = oomd_insert_cgroup_context(*monitored_cgroups, new_base, ctx->path); |
| 359 |
if (r == -ENOMEM) |
| 360 |
return r; |
| 361 |
if (r < 0 && !IN_SET(r, -EEXIST, -ENOENT)) |
| 362 |
log_debug_errno(r, "Failed to insert context for %s, ignoring: %m", ctx->path); |
| 363 |
} |
| 364 |
|
| 365 |
hashmap_free(*monitored_cgroups); |
| 366 |
*monitored_cgroups = TAKE_PTR(new_base); |
| 367 |
|
| 368 |
return 0; |
| 369 |
} |
| 370 |
|
| 371 |
static int get_monitored_cgroup_contexts_candidates(Hashmap *monitored_cgroups, Hashmap **ret_candidates) { |
| 372 |
_cleanup_hashmap_free_ Hashmap *candidates = NULL; |
| 373 |
OomdCGroupContext *ctx; |
| 374 |
int r; |
| 375 |
|
| 376 |
assert(monitored_cgroups); |
| 377 |
assert(ret_candidates); |
| 378 |
|
| 379 |
candidates = hashmap_new(&oomd_cgroup_ctx_hash_ops); |
| 380 |
if (!candidates) |
| 381 |
return -ENOMEM; |
| 382 |
|
| 383 |
HASHMAP_FOREACH(ctx, monitored_cgroups) { |
| 384 |
r = recursively_get_cgroup_context(candidates, ctx->path); |
| 385 |
if (r == -ENOMEM) |
| 386 |
return r; |
| 387 |
if (r < 0) |
| 388 |
log_debug_errno(r, "Failed to recursively get contexts for %s, ignoring: %m", ctx->path); |
| 389 |
} |
| 390 |
|
| 391 |
*ret_candidates = TAKE_PTR(candidates); |
| 392 |
|
| 393 |
return 0; |
| 394 |
} |
| 395 |
|
| 396 |
static int update_monitored_cgroup_contexts_candidates(Hashmap *monitored_cgroups, Hashmap **candidates) { |
| 397 |
_cleanup_hashmap_free_ Hashmap *new_candidates = NULL; |
| 398 |
int r; |
| 399 |
|
| 400 |
assert(monitored_cgroups); |
| 401 |
assert(candidates); |
| 402 |
assert(*candidates); |
| 403 |
|
| 404 |
r = get_monitored_cgroup_contexts_candidates(monitored_cgroups, &new_candidates); |
| 405 |
if (r < 0) |
| 406 |
return log_debug_errno(r, "Failed to get candidate contexts: %m"); |
| 407 |
|
| 408 |
oomd_update_cgroup_contexts_between_hashmaps(*candidates, new_candidates); |
| 409 |
|
| 410 |
hashmap_free(*candidates); |
| 411 |
*candidates = TAKE_PTR(new_candidates); |
| 412 |
|
| 413 |
return 0; |
| 414 |
} |
| 415 |
|
| 416 |
static int acquire_managed_oom_connect(Manager *m) { |
| 417 |
_cleanup_(sd_varlink_close_unrefp) sd_varlink *link = NULL; |
| 418 |
int r; |
| 419 |
|
| 420 |
assert(m); |
| 421 |
assert(m->event); |
| 422 |
|
| 423 |
r = sd_varlink_connect_address(&link, VARLINK_PATH_MANAGED_OOM_SYSTEM); |
| 424 |
if (r < 0) |
| 425 |
return log_error_errno(r, "Failed to connect to %s: %m", VARLINK_PATH_MANAGED_OOM_SYSTEM); |
| 426 |
|
| 427 |
(void) sd_varlink_set_userdata(link, m); |
| 428 |
(void) sd_varlink_set_description(link, "oomd"); |
| 429 |
(void) sd_varlink_set_relative_timeout(link, USEC_INFINITY); |
| 430 |
|
| 431 |
r = sd_varlink_attach_event(link, m->event, SD_EVENT_PRIORITY_NORMAL); |
| 432 |
if (r < 0) |
| 433 |
return log_error_errno(r, "Failed to attach varlink connection to event loop: %m"); |
| 434 |
|
| 435 |
r = sd_varlink_bind_reply(link, process_managed_oom_reply); |
| 436 |
if (r < 0) |
| 437 |
return log_error_errno(r, "Failed to bind reply callback: %m"); |
| 438 |
|
| 439 |
r = sd_varlink_observe(link, "io.systemd.ManagedOOM.SubscribeManagedOOMCGroups", NULL); |
| 440 |
if (r < 0) |
| 441 |
return log_error_errno(r, "Failed to observe varlink call: %m"); |
| 442 |
|
| 443 |
m->varlink_client = TAKE_PTR(link); |
| 444 |
return 0; |
| 445 |
} |
| 446 |
|
| 447 |
static int monitor_swap_contexts_handler(sd_event_source *s, uint64_t usec, void *userdata) { |
| 448 |
Manager *m = ASSERT_PTR(userdata); |
| 449 |
int r; |
| 450 |
|
| 451 |
assert(s); |
| 452 |
assert(!hashmap_isempty(m->monitored_swap_cgroup_contexts)); |
| 453 |
|
| 454 |
/* Reset timer */ |
| 455 |
r = sd_event_source_set_time_relative(s, SWAP_INTERVAL_USEC); |
| 456 |
if (r < 0) |
| 457 |
return log_error_errno(r, "Failed to set relative time for timer: %m"); |
| 458 |
|
| 459 |
/* Reconnect if our connection dropped */ |
| 460 |
if (!m->varlink_client) { |
| 461 |
r = acquire_managed_oom_connect(m); |
| 462 |
if (r < 0) |
| 463 |
return log_error_errno(r, "Failed to acquire varlink connection: %m"); |
| 464 |
} |
| 465 |
|
| 466 |
/* We still try to acquire system information for oomctl even if no units want swap monitoring */ |
| 467 |
r = oomd_system_context_acquire("/proc/meminfo", &m->system_context); |
| 468 |
/* If there are no units depending on swap actions, the only error we exit on is ENOMEM. */ |
| 469 |
if (r < 0) |
| 470 |
return log_error_errno(r, "Failed to acquire system context: %m"); |
| 471 |
|
| 472 |
/* Note that m->monitored_swap_cgroup_contexts does not need to be updated every interval because only the |
| 473 |
* system context is used for deciding whether the swap threshold is hit. m->monitored_swap_cgroup_contexts |
| 474 |
* is only used to decide which cgroups to kill (and even then only the resource usages of its descendent |
| 475 |
* nodes are the ones that matter). */ |
| 476 |
|
| 477 |
/* Check amount of memory available and swap free so we don't free up swap when memory is still available. */ |
| 478 |
if (oomd_mem_available_below(&m->system_context, 10000 - m->swap_used_limit_permyriad) && |
| 479 |
oomd_swap_free_below(&m->system_context, 10000 - m->swap_used_limit_permyriad)) { |
| 480 |
_cleanup_hashmap_free_ Hashmap *candidates = NULL; |
| 481 |
OomdCGroupContext *selected = NULL; |
| 482 |
uint64_t threshold; |
| 483 |
|
| 484 |
log_debug("Memory used (%"PRIu64") / total (%"PRIu64") and " |
| 485 |
"swap used (%"PRIu64") / total (%"PRIu64") is more than " PERMYRIAD_AS_PERCENT_FORMAT_STR, |
| 486 |
m->system_context.mem_used, m->system_context.mem_total, |
| 487 |
m->system_context.swap_used, m->system_context.swap_total, |
| 488 |
PERMYRIAD_AS_PERCENT_FORMAT_VAL(m->swap_used_limit_permyriad)); |
| 489 |
|
| 490 |
r = get_monitored_cgroup_contexts_candidates(m->monitored_swap_cgroup_contexts, &candidates); |
| 491 |
if (r == -ENOMEM) |
| 492 |
return log_oom(); |
| 493 |
if (r < 0) |
| 494 |
log_debug_errno(r, "Failed to get monitored swap cgroup candidates, ignoring: %m"); |
| 495 |
|
| 496 |
threshold = m->system_context.swap_total * THRESHOLD_SWAP_USED_PERCENT / 100; |
| 497 |
r = oomd_select_by_swap_usage(candidates, /* prefix= */ NULL, threshold, &selected); |
| 498 |
if (r < 0) |
| 499 |
return log_error_errno(r, "Failed to select any cgroups based on swap: %m"); |
| 500 |
if (r == 0) { |
| 501 |
log_debug("No cgroup candidates found for swap-based OOM action"); |
| 502 |
return 0; |
| 503 |
} |
| 504 |
|
| 505 |
r = oomd_cgroup_kill_mark(m, selected, "memory-used"); |
| 506 |
if (r == -ENOMEM) |
| 507 |
return log_oom(); |
| 508 |
if (r < 0) |
| 509 |
log_error_errno(r, "Failed to select any cgroups based on swap: %m"); |
| 510 |
else { |
| 511 |
if (selected && r > 0) |
| 512 |
log_notice("Marked %s for killing due to memory used (%"PRIu64") / total (%"PRIu64") and " |
| 513 |
"swap used (%"PRIu64") / total (%"PRIu64") being more than " |
| 514 |
PERMYRIAD_AS_PERCENT_FORMAT_STR, |
| 515 |
selected->path, |
| 516 |
m->system_context.mem_used, m->system_context.mem_total, |
| 517 |
m->system_context.swap_used, m->system_context.swap_total, |
| 518 |
PERMYRIAD_AS_PERCENT_FORMAT_VAL(m->swap_used_limit_permyriad)); |
| 519 |
return 0; |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
static void clear_candidate_hashmapp(Manager **m) { |
| 527 |
assert(m); |
| 528 |
|
| 529 |
if (*m) |
| 530 |
hashmap_clear((*m)->monitored_mem_pressure_cgroup_contexts_candidates); |
| 531 |
} |
| 532 |
|
| 533 |
static int monitor_memory_pressure_contexts_handler(sd_event_source *s, uint64_t usec, void *userdata) { |
| 534 |
/* Don't want to use stale candidate data. Setting this will clear the candidate hashmap on return unless we |
| 535 |
* update the candidate data (in which case clear_candidates will be NULL). */ |
| 536 |
_unused_ _cleanup_(clear_candidate_hashmapp) Manager *clear_candidates = userdata; |
| 537 |
_cleanup_set_free_ Set *targets = NULL; |
| 538 |
bool in_post_action_delay = false; |
| 539 |
Manager *m = ASSERT_PTR(userdata); |
| 540 |
usec_t usec_now; |
| 541 |
int r; |
| 542 |
|
| 543 |
assert(s); |
| 544 |
|
| 545 |
/* Reset timer */ |
| 546 |
r = sd_event_now(sd_event_source_get_event(s), CLOCK_MONOTONIC, &usec_now); |
| 547 |
if (r < 0) |
| 548 |
return log_error_errno(r, "Failed to reset event timer: %m"); |
| 549 |
|
| 550 |
r = sd_event_source_set_time_relative(s, MEM_PRESSURE_INTERVAL_USEC); |
| 551 |
if (r < 0) |
| 552 |
return log_error_errno(r, "Failed to set relative time for timer: %m"); |
| 553 |
|
| 554 |
/* Reconnect if our connection dropped */ |
| 555 |
if (!m->varlink_client) { |
| 556 |
r = acquire_managed_oom_connect(m); |
| 557 |
if (r < 0) |
| 558 |
return log_error_errno(r, "Failed to acquire varlink connection: %m"); |
| 559 |
} |
| 560 |
|
| 561 |
/* Return early if nothing is requesting memory pressure monitoring */ |
| 562 |
if (hashmap_isempty(m->monitored_mem_pressure_cgroup_contexts)) |
| 563 |
return 0; |
| 564 |
|
| 565 |
/* Update the cgroups used for detection/action */ |
| 566 |
r = update_monitored_cgroup_contexts(&m->monitored_mem_pressure_cgroup_contexts); |
| 567 |
if (r == -ENOMEM) |
| 568 |
return log_oom(); |
| 569 |
if (r < 0) |
| 570 |
log_debug_errno(r, "Failed to update monitored memory pressure cgroup contexts, ignoring: %m"); |
| 571 |
|
| 572 |
/* Since pressure counters are lagging, we need to wait a bit after a kill to ensure we don't read stale |
| 573 |
* values and go on a kill storm. */ |
| 574 |
if (m->mem_pressure_post_action_delay_start > 0) { |
| 575 |
if (m->mem_pressure_post_action_delay_start + POST_ACTION_DELAY_USEC > usec_now) |
| 576 |
in_post_action_delay = true; |
| 577 |
else |
| 578 |
m->mem_pressure_post_action_delay_start = 0; |
| 579 |
} |
| 580 |
|
| 581 |
r = oomd_pressure_above(m->monitored_mem_pressure_cgroup_contexts, &targets); |
| 582 |
if (r == -ENOMEM) |
| 583 |
return log_oom(); |
| 584 |
if (r < 0) |
| 585 |
log_debug_errno(r, "Failed to check if memory pressure exceeded limits, ignoring: %m"); |
| 586 |
else if (r == 1 && !in_post_action_delay) { |
| 587 |
OomdCGroupContext *t; |
| 588 |
SET_FOREACH(t, targets) { |
| 589 |
OomdCGroupContext *selected = NULL; |
| 590 |
|
| 591 |
/* Check if there was reclaim activity in the given interval. The concern is the following case: |
| 592 |
* Pressure climbed, a lot of high-frequency pages were reclaimed, and we killed the offending |
| 593 |
* cgroup. Even after this, well-behaved processes will fault in recently resident pages and |
| 594 |
* this will cause pressure to remain high. Thus if there isn't any reclaim pressure, no need |
| 595 |
* to kill something (it won't help anyways). */ |
| 596 |
if ((now(CLOCK_MONOTONIC) - t->last_had_mem_reclaim) > RECLAIM_DURATION_USEC) |
| 597 |
continue; |
| 598 |
|
| 599 |
log_debug("Memory pressure for %s is %lu.%02lu%% > %lu.%02lu%% for > %s with reclaim activity", |
| 600 |
t->path, |
| 601 |
LOADAVG_INT_SIDE(t->memory_pressure.avg10), LOADAVG_DECIMAL_SIDE(t->memory_pressure.avg10), |
| 602 |
LOADAVG_INT_SIDE(t->mem_pressure_limit), LOADAVG_DECIMAL_SIDE(t->mem_pressure_limit), |
| 603 |
FORMAT_TIMESPAN(t->mem_pressure_duration_usec, USEC_PER_SEC)); |
| 604 |
|
| 605 |
r = update_monitored_cgroup_contexts_candidates( |
| 606 |
m->monitored_mem_pressure_cgroup_contexts, &m->monitored_mem_pressure_cgroup_contexts_candidates); |
| 607 |
if (r == -ENOMEM) |
| 608 |
return log_oom(); |
| 609 |
if (r < 0) |
| 610 |
log_debug_errno(r, "Failed to update monitored memory pressure candidate cgroup contexts, ignoring: %m"); |
| 611 |
else |
| 612 |
clear_candidates = NULL; |
| 613 |
|
| 614 |
r = oomd_select_by_pgscan_rate(m->monitored_mem_pressure_cgroup_contexts_candidates, |
| 615 |
/* prefix= */ t->path, |
| 616 |
&selected); |
| 617 |
if (r < 0) |
| 618 |
return log_error_errno(r, "Failed to select any cgroups based on swap, ignoring: %m"); |
| 619 |
if (r == 0) { |
| 620 |
log_debug("No cgroup candidates found for memory pressure-based OOM action for %s", t->path); |
| 621 |
continue; |
| 622 |
} |
| 623 |
|
| 624 |
r = oomd_cgroup_kill_mark(m, selected, "memory-pressure"); |
| 625 |
if (r == -ENOMEM) |
| 626 |
return log_oom(); |
| 627 |
if (r < 0) |
| 628 |
log_error_errno(r, "Failed to select any cgroups under %s based on pressure, ignoring: %m", t->path); |
| 629 |
else if (r == 0) |
| 630 |
/* Already queued for kill by an earlier iteration, try next target without |
| 631 |
* resetting the delay timer. */ |
| 632 |
continue; |
| 633 |
else { |
| 634 |
/* Don't act on all the high pressure cgroups at once; return as soon as we kill one. */ |
| 635 |
m->mem_pressure_post_action_delay_start = usec_now; |
| 636 |
if (selected) |
| 637 |
log_notice("Marked %s for killing due to memory pressure for %s being %lu.%02lu%% > %lu.%02lu%%" |
| 638 |
" for > %s with reclaim activity", |
| 639 |
selected->path, t->path, |
| 640 |
LOADAVG_INT_SIDE(t->memory_pressure.avg10), LOADAVG_DECIMAL_SIDE(t->memory_pressure.avg10), |
| 641 |
LOADAVG_INT_SIDE(t->mem_pressure_limit), LOADAVG_DECIMAL_SIDE(t->mem_pressure_limit), |
| 642 |
FORMAT_TIMESPAN(t->mem_pressure_duration_usec, USEC_PER_SEC)); |
| 643 |
return 0; |
| 644 |
} |
| 645 |
} |
| 646 |
} else { |
| 647 |
/* If any monitored cgroup is over their pressure limit, get all the kill candidates for every |
| 648 |
* monitored cgroup. This saves CPU cycles from doing it every interval by only doing it when a kill |
| 649 |
* might happen. |
| 650 |
* Candidate cgroup data will continue to get updated during the post-action delay period in case |
| 651 |
* pressure continues to be high after a kill. */ |
| 652 |
OomdCGroupContext *c; |
| 653 |
HASHMAP_FOREACH(c, m->monitored_mem_pressure_cgroup_contexts) { |
| 654 |
if (c->mem_pressure_limit_hit_start == 0) |
| 655 |
continue; |
| 656 |
|
| 657 |
r = update_monitored_cgroup_contexts_candidates( |
| 658 |
m->monitored_mem_pressure_cgroup_contexts, &m->monitored_mem_pressure_cgroup_contexts_candidates); |
| 659 |
if (r == -ENOMEM) |
| 660 |
return log_oom(); |
| 661 |
if (r < 0) |
| 662 |
log_debug_errno(r, "Failed to update monitored memory pressure candidate cgroup contexts, ignoring: %m"); |
| 663 |
else { |
| 664 |
clear_candidates = NULL; |
| 665 |
break; |
| 666 |
} |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
return 0; |
| 671 |
} |
| 672 |
|
| 673 |
static int ruleset_execute_action( |
| 674 |
Manager *m, |
| 675 |
OomdCGroupContext *ctx, |
| 676 |
OomdRuleset *ruleset, |
| 677 |
const char *rule_name, |
| 678 |
usec_t usec_now) { |
| 679 |
|
| 680 |
_cleanup_free_ char *reason = NULL; |
| 681 |
int r; |
| 682 |
|
| 683 |
assert(m); |
| 684 |
assert(ctx); |
| 685 |
assert(ruleset); |
| 686 |
assert(rule_name); |
| 687 |
|
| 688 |
if (ruleset->lasting_usec > 0) |
| 689 |
log_notice("Rule '%s' conditions met for cgroup %s (lasting %s), taking action %s", |
| 690 |
rule_name, |
| 691 |
ctx->path, |
| 692 |
FORMAT_TIMESPAN(ruleset->lasting_usec, USEC_PER_SEC), |
| 693 |
oomd_action_to_string(ruleset->action)); |
| 694 |
else |
| 695 |
log_notice("Rule '%s' conditions met for cgroup %s, taking action %s", |
| 696 |
rule_name, |
| 697 |
ctx->path, |
| 698 |
oomd_action_to_string(ruleset->action)); |
| 699 |
|
| 700 |
reason = strjoin("rule ", rule_name); |
| 701 |
if (!reason) |
| 702 |
return log_oom(); |
| 703 |
|
| 704 |
if (ruleset->action == OOMD_ACTION_KILL_ALL) { |
| 705 |
r = oomd_cgroup_kill_mark(m, ctx, reason); |
| 706 |
if (r == -ENOMEM) |
| 707 |
return log_oom(); |
| 708 |
if (r < 0) { |
| 709 |
log_notice_errno(r, "Failed to kill all processes in %s: %m", ctx->path); |
| 710 |
return 0; |
| 711 |
} |
| 712 |
} else if (ruleset->action == OOMD_ACTION_KILL_BY_PGSCAN) { |
| 713 |
OomdCGroupContext *selected = NULL; |
| 714 |
|
| 715 |
/* Check if there was reclaim activity in the given interval. If there isn't any reclaim |
| 716 |
* pressure, killing won't help — well-behaved processes faulting in recently resident |
| 717 |
* pages will keep pressure high even after the offending cgroup is killed. */ |
| 718 |
if (usec_sub_unsigned(usec_now, ctx->last_had_mem_reclaim) > RECLAIM_DURATION_USEC) { |
| 719 |
log_debug("No reclaim activity for %s, skipping pgscan-based action", ctx->path); |
| 720 |
return 0; |
| 721 |
} |
| 722 |
|
| 723 |
r = oomd_select_by_pgscan_rate(m->monitored_rules_cgroup_contexts_candidates, ctx->path, &selected); |
| 724 |
if (r < 0) { |
| 725 |
log_notice_errno(r, "Failed to select cgroup by pgscan rate for %s: %m", ctx->path); |
| 726 |
return 0; |
| 727 |
} |
| 728 |
if (r == 0) { |
| 729 |
log_debug("No cgroup candidates found for pgscan-based action for %s", ctx->path); |
| 730 |
return 0; |
| 731 |
} |
| 732 |
|
| 733 |
r = oomd_cgroup_kill_mark(m, selected, reason); |
| 734 |
if (r == -ENOMEM) |
| 735 |
return log_oom(); |
| 736 |
if (r < 0) { |
| 737 |
log_notice_errno(r, "Failed to kill processes in %s: %m", selected->path); |
| 738 |
return 0; |
| 739 |
} |
| 740 |
} else if (ruleset->action == OOMD_ACTION_KILL_BY_SWAP) { |
| 741 |
OomdCGroupContext *selected = NULL; |
| 742 |
uint64_t threshold; |
| 743 |
|
| 744 |
if (m->system_context.swap_total == 0) { |
| 745 |
if (!ruleset->warned_no_swap) { |
| 746 |
log_warning("Rule '%s' uses kill-by-swap action but no swap is configured, skipping (further occurrences suppressed)", rule_name); |
| 747 |
ruleset->warned_no_swap = true; |
| 748 |
} |
| 749 |
return 0; |
| 750 |
} |
| 751 |
|
| 752 |
/* Swap came back — clear the latch so re-disabling swap warns again. */ |
| 753 |
ruleset->warned_no_swap = false; |
| 754 |
|
| 755 |
threshold = m->system_context.swap_total * THRESHOLD_SWAP_USED_PERCENT / 100; |
| 756 |
r = oomd_select_by_swap_usage(m->monitored_rules_cgroup_contexts_candidates, ctx->path, threshold, &selected); |
| 757 |
if (r < 0) { |
| 758 |
log_notice_errno(r, "Failed to select cgroup by swap usage for %s: %m", ctx->path); |
| 759 |
return 0; |
| 760 |
} |
| 761 |
if (r == 0) { |
| 762 |
log_debug("No cgroup candidates found for swap-based action for %s", ctx->path); |
| 763 |
return 0; |
| 764 |
} |
| 765 |
|
| 766 |
r = oomd_cgroup_kill_mark(m, selected, reason); |
| 767 |
if (r == -ENOMEM) |
| 768 |
return log_oom(); |
| 769 |
if (r < 0) { |
| 770 |
log_notice_errno(r, "Failed to kill processes in %s: %m", selected->path); |
| 771 |
return 0; |
| 772 |
} |
| 773 |
} else |
| 774 |
assert_not_reached(); |
| 775 |
|
| 776 |
return 1; |
| 777 |
} |
| 778 |
|
| 779 |
static int ruleset_check_conditions( |
| 780 |
Manager *m, |
| 781 |
OomdCGroupContext *ctx, |
| 782 |
OomdRuleset *ruleset, |
| 783 |
const char *rule_name, |
| 784 |
usec_t usec_now) { |
| 785 |
|
| 786 |
int r; |
| 787 |
|
| 788 |
assert(m); |
| 789 |
assert(ctx); |
| 790 |
assert(ruleset); |
| 791 |
assert(rule_name); |
| 792 |
|
| 793 |
/* Check memory pressure condition. |
| 794 |
* memory_pressure_above is in permyriad (0-10000, i.e. 6050 = 60.50%). |
| 795 |
* store_loadavg_fixed_point takes integer and decimal parts of a percentage, |
| 796 |
* so divide/modulo by 100 to split permyriad into percent + centipercent. */ |
| 797 |
if (ruleset->memory_pressure_above >= 0) { |
| 798 |
loadavg_t threshold; |
| 799 |
r = store_loadavg_fixed_point(ruleset->memory_pressure_above / 100, |
| 800 |
ruleset->memory_pressure_above % 100, |
| 801 |
&threshold); |
| 802 |
if (r < 0) |
| 803 |
return log_debug_errno(r, "Failed to convert pressure threshold for rule '%s': %m", rule_name); |
| 804 |
|
| 805 |
if (ctx->memory_pressure.avg10 <= threshold) |
| 806 |
goto reset; |
| 807 |
} |
| 808 |
|
| 809 |
/* swap_above means take action when swap usage is above threshold. |
| 810 |
* oomd_swap_free_below returns true when swap free is below threshold, |
| 811 |
* so if swap_above is X%, check if swap free is below (100-X)%. |
| 812 |
* When no swap is configured, the condition cannot be meaningfully evaluated. */ |
| 813 |
if (ruleset->swap_above >= 0) { |
| 814 |
if (m->system_context.swap_total == 0 || |
| 815 |
!oomd_swap_free_below(&m->system_context, 10000 - ruleset->swap_above)) |
| 816 |
goto reset; |
| 817 |
} |
| 818 |
|
| 819 |
/* All conditions met, check if LastingSec requirement is satisfied */ |
| 820 |
usec_t *start_time = hashmap_get(ruleset->start_times, ctx->path); |
| 821 |
if (!start_time) { |
| 822 |
/* First time seeing this condition - record the start time */ |
| 823 |
_cleanup_free_ usec_t *new_start_time = new(usec_t, 1); |
| 824 |
if (!new_start_time) |
| 825 |
return log_oom(); |
| 826 |
|
| 827 |
*new_start_time = usec_now; |
| 828 |
|
| 829 |
_cleanup_free_ char *path_copy = strdup(ctx->path); |
| 830 |
if (!path_copy) |
| 831 |
return log_oom(); |
| 832 |
|
| 833 |
r = hashmap_ensure_put(&ruleset->start_times, &string_hash_ops_free_free, path_copy, new_start_time); |
| 834 |
if (r < 0) |
| 835 |
return log_error_errno(r, "Failed to record start time for rule '%s' on %s: %m", |
| 836 |
rule_name, ctx->path); |
| 837 |
TAKE_PTR(path_copy); |
| 838 |
TAKE_PTR(new_start_time); |
| 839 |
|
| 840 |
/* If lasting_usec is 0, take action immediately */ |
| 841 |
if (ruleset->lasting_usec == 0) |
| 842 |
return true; |
| 843 |
|
| 844 |
log_debug("Rule '%s' conditions met for cgroup %s, waiting for %s", |
| 845 |
rule_name, ctx->path, |
| 846 |
FORMAT_TIMESPAN(ruleset->lasting_usec, USEC_PER_SEC)); |
| 847 |
return false; |
| 848 |
} |
| 849 |
|
| 850 |
/* Check if the condition has been true for long enough */ |
| 851 |
usec_t duration = usec_sub_unsigned(usec_now, *start_time); |
| 852 |
if (duration >= ruleset->lasting_usec) |
| 853 |
return true; |
| 854 |
|
| 855 |
log_debug("Rule '%s' conditions met for cgroup %s for %s (need %s)", |
| 856 |
rule_name, ctx->path, |
| 857 |
FORMAT_TIMESPAN(duration, USEC_PER_SEC), |
| 858 |
FORMAT_TIMESPAN(ruleset->lasting_usec, USEC_PER_SEC)); |
| 859 |
return false; |
| 860 |
|
| 861 |
reset: |
| 862 |
/* Conditions no longer met — remove start time if it exists. */ |
| 863 |
{ |
| 864 |
_cleanup_free_ char *old_key = NULL; |
| 865 |
_cleanup_free_ usec_t *old_start_time = |
| 866 |
hashmap_remove2(ruleset->start_times, ctx->path, (void**) &old_key); |
| 867 |
if (old_start_time) |
| 868 |
log_debug("Rule '%s' conditions no longer met for cgroup %s, resetting timer", |
| 869 |
rule_name, ctx->path); |
| 870 |
} |
| 871 |
return false; |
| 872 |
} |
| 873 |
|
| 874 |
/* After a reload, some cgroups may reference rulesets that no longer exist (or didn't exist yet |
| 875 |
* when the cgroup subscribed). Warn once per (cgroup, rule) pair so the operator sees the mismatch, |
| 876 |
* without spamming the per-interval evaluation loop. */ |
| 877 |
static void warn_missing_rulesets(Manager *m) { |
| 878 |
OomdCGroupContext *ctx; |
| 879 |
|
| 880 |
assert(m); |
| 881 |
|
| 882 |
HASHMAP_FOREACH(ctx, m->monitored_rules_cgroup_contexts) |
| 883 |
STRV_FOREACH(rule, ctx->rules) |
| 884 |
if (!hashmap_contains(m->rulesets, *rule)) |
| 885 |
log_warning("Cgroup %s references undefined ruleset '%s', it will be ignored.", |
| 886 |
ctx->path, *rule); |
| 887 |
} |
| 888 |
|
| 889 |
/* Remove start_times entries for cgroups that are no longer in monitored_rules_cgroup_contexts. |
| 890 |
* Cgroups can vanish silently (unit stops, cgroup destroyed) without an explicit unsubscribe |
| 891 |
* message, so we periodically reconcile to prevent unbounded growth of start_times. */ |
| 892 |
static int prune_stale_ruleset_start_times(Manager *m) { |
| 893 |
OomdRuleset *ruleset; |
| 894 |
int r; |
| 895 |
|
| 896 |
assert(m); |
| 897 |
|
| 898 |
HASHMAP_FOREACH(ruleset, m->rulesets) { |
| 899 |
_cleanup_strv_free_ char **to_remove = NULL; |
| 900 |
const char *path; |
| 901 |
void *v; |
| 902 |
|
| 903 |
HASHMAP_FOREACH_KEY(v, path, ruleset->start_times) |
| 904 |
if (!hashmap_contains(m->monitored_rules_cgroup_contexts, path)) { |
| 905 |
r = strv_extend(&to_remove, path); |
| 906 |
if (r < 0) |
| 907 |
return log_oom(); |
| 908 |
} |
| 909 |
|
| 910 |
STRV_FOREACH(p, to_remove) { |
| 911 |
_cleanup_free_ char *key = NULL; |
| 912 |
free(hashmap_remove2(ruleset->start_times, *p, (void**) &key)); |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
return 0; |
| 917 |
} |
| 918 |
|
| 919 |
static int process_rules_cgroup_context(Manager *m, OomdCGroupContext *ctx, usec_t usec_now) { |
| 920 |
int r; |
| 921 |
|
| 922 |
assert(m); |
| 923 |
assert(ctx); |
| 924 |
|
| 925 |
if (strv_isempty(ctx->rules)) |
| 926 |
return 0; |
| 927 |
|
| 928 |
STRV_FOREACH(rule_name, ctx->rules) { |
| 929 |
OomdRuleset *ruleset = hashmap_get(m->rulesets, *rule_name); |
| 930 |
if (!ruleset) |
| 931 |
/* Silently skip: already warned once when the subscription was attached or when |
| 932 |
* rulesets were loaded. Repeating here would fire every interval. */ |
| 933 |
continue; |
| 934 |
|
| 935 |
r = ruleset_check_conditions(m, ctx, ruleset, *rule_name, usec_now); |
| 936 |
if (r < 0) |
| 937 |
continue; |
| 938 |
if (r == 0) |
| 939 |
continue; |
| 940 |
|
| 941 |
r = ruleset_execute_action(m, ctx, ruleset, *rule_name, usec_now); |
| 942 |
if (r < 0) |
| 943 |
return r; |
| 944 |
|
| 945 |
/* Only remove start time if the action actually killed something, so that |
| 946 |
* LastingSec must be satisfied again before re-triggering. If the action |
| 947 |
* failed to kill, keep the timer running to retry on the next interval. */ |
| 948 |
if (r > 0) { |
| 949 |
_cleanup_free_ char *action_key = NULL; |
| 950 |
free(hashmap_remove2(ruleset->start_times, ctx->path, (void **) &action_key)); |
| 951 |
|
| 952 |
/* Global (not per-cgroup/per-ruleset) post-action delay: after any |
| 953 |
* successful ruleset kill we suppress *all* subsequent rule evaluations |
| 954 |
* until POST_ACTION_DELAY_USEC elapses. This is intentional — pressure |
| 955 |
* and swap metrics need time to reflect the effect of a kill before we |
| 956 |
* act again, otherwise a single overload could cascade into multiple |
| 957 |
* unrelated kills across sibling cgroups within the same interval. */ |
| 958 |
m->rules_post_action_delay_start = usec_now; |
| 959 |
return 0; |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
return 0; |
| 964 |
} |
| 965 |
|
| 966 |
static int monitor_rules_contexts_handler(sd_event_source *s, uint64_t usec, void *userdata) { |
| 967 |
Manager *m = ASSERT_PTR(userdata); |
| 968 |
bool in_post_action_delay = false; |
| 969 |
usec_t usec_now; |
| 970 |
int r; |
| 971 |
|
| 972 |
assert(s); |
| 973 |
|
| 974 |
/* Reset timer */ |
| 975 |
r = sd_event_now(sd_event_source_get_event(s), CLOCK_MONOTONIC, &usec_now); |
| 976 |
if (r < 0) |
| 977 |
return log_error_errno(r, "Failed to reset event timer: %m"); |
| 978 |
|
| 979 |
r = sd_event_source_set_time_relative(s, RULESETS_INTERVAL_USEC); |
| 980 |
if (r < 0) |
| 981 |
return log_error_errno(r, "Failed to set relative time for timer: %m"); |
| 982 |
|
| 983 |
/* Reconnect if our connection dropped */ |
| 984 |
if (!m->varlink_client) { |
| 985 |
r = acquire_managed_oom_connect(m); |
| 986 |
if (r < 0) |
| 987 |
return log_error_errno(r, "Failed to acquire varlink connection: %m"); |
| 988 |
} |
| 989 |
|
| 990 |
/* Return early if no rules are set */ |
| 991 |
if (hashmap_isempty(m->monitored_rules_cgroup_contexts)) |
| 992 |
return 0; |
| 993 |
|
| 994 |
/* Determine whether we're still inside the post-action delay window before doing any |
| 995 |
* heavy lifting, so we can short-circuit the expensive descendant walk below. */ |
| 996 |
if (m->rules_post_action_delay_start > 0) { |
| 997 |
if (usec_add(m->rules_post_action_delay_start, POST_ACTION_DELAY_USEC) > usec_now) |
| 998 |
in_post_action_delay = true; |
| 999 |
else |
| 1000 |
m->rules_post_action_delay_start = 0; |
| 1001 |
} |
| 1002 |
|
| 1003 |
/* Always keep the subscribed (parent) cgroup contexts fresh so pgscan rate differentials |
| 1004 |
* stay accurate across intervals, even during the post-action delay. Only suppress the |
| 1005 |
* kill action itself. |
| 1006 |
* |
| 1007 |
* Note: update_monitored_cgroup_contexts() rebuilds the hashmap by calling |
| 1008 |
* oomd_insert_cgroup_context(), which also carries over the per-cgroup 'rules' strv |
| 1009 |
* from the old context. We rely on that implicit rule propagation here — the |
| 1010 |
* rules attached to each cgroup context persist across refreshes. */ |
| 1011 |
r = update_monitored_cgroup_contexts(&m->monitored_rules_cgroup_contexts); |
| 1012 |
if (r == -ENOMEM) |
| 1013 |
return log_oom(); |
| 1014 |
if (r < 0) |
| 1015 |
log_debug_errno(r, "Failed to update monitored rules cgroup contexts, ignoring: %m"); |
| 1016 |
|
| 1017 |
/* The candidate refresh is the expensive part — it recursively walks descendants of every |
| 1018 |
* monitored cgroup. Since candidates are only consumed by kill-by-pgscan / kill-by-swap |
| 1019 |
* (both suppressed during the delay), skip the walk while we're not going to act. */ |
| 1020 |
if (!in_post_action_delay) { |
| 1021 |
r = update_monitored_cgroup_contexts_candidates( |
| 1022 |
m->monitored_rules_cgroup_contexts, &m->monitored_rules_cgroup_contexts_candidates); |
| 1023 |
if (r == -ENOMEM) |
| 1024 |
return log_oom(); |
| 1025 |
if (r < 0) |
| 1026 |
log_debug_errno(r, "Failed to update monitored rules cgroup candidates, ignoring: %m"); |
| 1027 |
} |
| 1028 |
|
| 1029 |
r = prune_stale_ruleset_start_times(m); |
| 1030 |
if (r < 0) |
| 1031 |
return r; |
| 1032 |
|
| 1033 |
if (in_post_action_delay) |
| 1034 |
return 0; |
| 1035 |
|
| 1036 |
/* Only read /proc/meminfo if at least one ruleset actually needs swap info — either as |
| 1037 |
* a SwapUsageMax= condition or as a kill-by-swap action (which uses swap_total to |
| 1038 |
* compute the per-cgroup selection threshold). */ |
| 1039 |
OomdRuleset *ruleset; |
| 1040 |
HASHMAP_FOREACH(ruleset, m->rulesets) |
| 1041 |
if (ruleset->swap_above >= 0 || ruleset->action == OOMD_ACTION_KILL_BY_SWAP) { |
| 1042 |
r = oomd_system_context_acquire("/proc/meminfo", &m->system_context); |
| 1043 |
if (r < 0) |
| 1044 |
return log_error_errno(r, "Failed to acquire system context: %m"); |
| 1045 |
break; |
| 1046 |
} |
| 1047 |
|
| 1048 |
OomdCGroupContext *ctx; |
| 1049 |
HASHMAP_FOREACH(ctx, m->monitored_rules_cgroup_contexts) { |
| 1050 |
r = process_rules_cgroup_context(m, ctx, usec_now); |
| 1051 |
if (r < 0) |
| 1052 |
return r; |
| 1053 |
|
| 1054 |
/* process_rules_cgroup_context() sets rules_post_action_delay_start when it queues |
| 1055 |
* a kill. Honor the delay *within the same tick* too: otherwise a single overload |
| 1056 |
* could cascade into kills across unrelated sibling cgroups before pressure metrics |
| 1057 |
* have a chance to reflect the first kill. */ |
| 1058 |
if (m->rules_post_action_delay_start > 0) |
| 1059 |
break; |
| 1060 |
} |
| 1061 |
|
| 1062 |
return 0; |
| 1063 |
} |
| 1064 |
|
| 1065 |
static int monitor_swap_contexts(Manager *m) { |
| 1066 |
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
| 1067 |
int r; |
| 1068 |
|
| 1069 |
assert(m); |
| 1070 |
assert(m->event); |
| 1071 |
|
| 1072 |
r = sd_event_add_time(m->event, &s, CLOCK_MONOTONIC, 0, 0, monitor_swap_contexts_handler, m); |
| 1073 |
if (r < 0) |
| 1074 |
return r; |
| 1075 |
|
| 1076 |
r = sd_event_source_set_exit_on_failure(s, true); |
| 1077 |
if (r < 0) |
| 1078 |
return r; |
| 1079 |
|
| 1080 |
r = sd_event_source_set_enabled(s, SD_EVENT_OFF); |
| 1081 |
if (r < 0) |
| 1082 |
return r; |
| 1083 |
|
| 1084 |
(void) sd_event_source_set_description(s, "oomd-swap-timer"); |
| 1085 |
|
| 1086 |
m->swap_context_event_source = TAKE_PTR(s); |
| 1087 |
return 0; |
| 1088 |
} |
| 1089 |
|
| 1090 |
static int monitor_memory_pressure_contexts(Manager *m) { |
| 1091 |
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
| 1092 |
int r; |
| 1093 |
|
| 1094 |
assert(m); |
| 1095 |
assert(m->event); |
| 1096 |
|
| 1097 |
r = sd_event_add_time(m->event, &s, CLOCK_MONOTONIC, 0, 0, monitor_memory_pressure_contexts_handler, m); |
| 1098 |
if (r < 0) |
| 1099 |
return r; |
| 1100 |
|
| 1101 |
r = sd_event_source_set_exit_on_failure(s, true); |
| 1102 |
if (r < 0) |
| 1103 |
return r; |
| 1104 |
|
| 1105 |
r = sd_event_source_set_enabled(s, SD_EVENT_ON); |
| 1106 |
if (r < 0) |
| 1107 |
return r; |
| 1108 |
|
| 1109 |
(void) sd_event_source_set_description(s, "oomd-memory-pressure-timer"); |
| 1110 |
|
| 1111 |
m->mem_pressure_context_event_source = TAKE_PTR(s); |
| 1112 |
return 0; |
| 1113 |
} |
| 1114 |
|
| 1115 |
static int monitor_rules_contexts(Manager *m) { |
| 1116 |
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
| 1117 |
int r; |
| 1118 |
|
| 1119 |
assert(m); |
| 1120 |
assert(m->event); |
| 1121 |
|
| 1122 |
r = sd_event_add_time(m->event, &s, CLOCK_MONOTONIC, 0, 0, monitor_rules_contexts_handler, m); |
| 1123 |
if (r < 0) |
| 1124 |
return r; |
| 1125 |
|
| 1126 |
r = sd_event_source_set_exit_on_failure(s, true); |
| 1127 |
if (r < 0) |
| 1128 |
return r; |
| 1129 |
|
| 1130 |
r = sd_event_source_set_enabled(s, SD_EVENT_OFF); |
| 1131 |
if (r < 0) |
| 1132 |
return r; |
| 1133 |
|
| 1134 |
(void) sd_event_source_set_description(s, "oomd-rules-timer"); |
| 1135 |
|
| 1136 |
m->rules_context_event_source = TAKE_PTR(s); |
| 1137 |
return 0; |
| 1138 |
} |
| 1139 |
|
| 1140 |
Manager* manager_free(Manager *m) { |
| 1141 |
assert(m); |
| 1142 |
|
| 1143 |
sd_varlink_server_unref(m->varlink_server); |
| 1144 |
sd_varlink_close_unref(m->varlink_client); |
| 1145 |
sd_event_source_unref(m->swap_context_event_source); |
| 1146 |
sd_event_source_unref(m->mem_pressure_context_event_source); |
| 1147 |
sd_event_source_unref(m->rules_context_event_source); |
| 1148 |
sd_event_unref(m->event); |
| 1149 |
|
| 1150 |
hashmap_free(m->polkit_registry); |
| 1151 |
sd_bus_flush_close_unref(m->bus); |
| 1152 |
|
| 1153 |
hashmap_free(m->monitored_swap_cgroup_contexts); |
| 1154 |
hashmap_free(m->monitored_mem_pressure_cgroup_contexts); |
| 1155 |
hashmap_free(m->monitored_mem_pressure_cgroup_contexts_candidates); |
| 1156 |
hashmap_free(m->monitored_rules_cgroup_contexts); |
| 1157 |
hashmap_free(m->monitored_rules_cgroup_contexts_candidates); |
| 1158 |
|
| 1159 |
set_free(m->kill_states); |
| 1160 |
|
| 1161 |
hashmap_free(m->rulesets); |
| 1162 |
|
| 1163 |
return mfree(m); |
| 1164 |
} |
| 1165 |
|
| 1166 |
static int manager_dispatch_reload_signal(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
| 1167 |
Manager *m = ASSERT_PTR(userdata); |
| 1168 |
|
| 1169 |
(void) notify_reloading(); |
| 1170 |
|
| 1171 |
manager_set_defaults(m); |
| 1172 |
manager_parse_config_file(m); |
| 1173 |
warn_missing_rulesets(m); |
| 1174 |
|
| 1175 |
(void) sd_notify(/* unset_environment= */ false, NOTIFY_READY_MESSAGE); |
| 1176 |
return 0; |
| 1177 |
} |
| 1178 |
|
| 1179 |
int manager_new(Manager **ret) { |
| 1180 |
_cleanup_(manager_freep) Manager *m = NULL; |
| 1181 |
int r; |
| 1182 |
|
| 1183 |
assert(ret); |
| 1184 |
|
| 1185 |
m = new0(Manager, 1); |
| 1186 |
if (!m) |
| 1187 |
return -ENOMEM; |
| 1188 |
|
| 1189 |
manager_set_defaults(m); |
| 1190 |
manager_parse_config_file(m); |
| 1191 |
|
| 1192 |
r = sd_event_default(&m->event); |
| 1193 |
if (r < 0) |
| 1194 |
return r; |
| 1195 |
|
| 1196 |
(void) sd_event_set_watchdog(m->event, true); |
| 1197 |
|
| 1198 |
r = sd_event_add_signal(m->event, /* ret= */ NULL, SIGHUP | SD_EVENT_SIGNAL_PROCMASK, manager_dispatch_reload_signal, m); |
| 1199 |
if (r < 0) |
| 1200 |
return r; |
| 1201 |
|
| 1202 |
r = sd_event_set_signal_exit(m->event, true); |
| 1203 |
if (r < 0) |
| 1204 |
return r; |
| 1205 |
|
| 1206 |
m->monitored_swap_cgroup_contexts = hashmap_new(&oomd_cgroup_ctx_hash_ops); |
| 1207 |
if (!m->monitored_swap_cgroup_contexts) |
| 1208 |
return -ENOMEM; |
| 1209 |
|
| 1210 |
m->monitored_mem_pressure_cgroup_contexts = hashmap_new(&oomd_cgroup_ctx_hash_ops); |
| 1211 |
if (!m->monitored_mem_pressure_cgroup_contexts) |
| 1212 |
return -ENOMEM; |
| 1213 |
|
| 1214 |
m->monitored_mem_pressure_cgroup_contexts_candidates = hashmap_new(&oomd_cgroup_ctx_hash_ops); |
| 1215 |
if (!m->monitored_mem_pressure_cgroup_contexts_candidates) |
| 1216 |
return -ENOMEM; |
| 1217 |
|
| 1218 |
m->monitored_rules_cgroup_contexts = hashmap_new(&oomd_cgroup_ctx_hash_ops); |
| 1219 |
if (!m->monitored_rules_cgroup_contexts) |
| 1220 |
return -ENOMEM; |
| 1221 |
|
| 1222 |
m->monitored_rules_cgroup_contexts_candidates = hashmap_new(&oomd_cgroup_ctx_hash_ops); |
| 1223 |
if (!m->monitored_rules_cgroup_contexts_candidates) |
| 1224 |
return -ENOMEM; |
| 1225 |
|
| 1226 |
*ret = TAKE_PTR(m); |
| 1227 |
return 0; |
| 1228 |
} |
| 1229 |
|
| 1230 |
static int manager_connect_bus(Manager *m) { |
| 1231 |
int r; |
| 1232 |
|
| 1233 |
assert(m); |
| 1234 |
assert(!m->bus); |
| 1235 |
|
| 1236 |
r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-oom"); |
| 1237 |
if (r < 0) |
| 1238 |
return log_error_errno(r, "Failed to connect to bus: %m"); |
| 1239 |
|
| 1240 |
r = bus_add_implementation(m->bus, &manager_object, m); |
| 1241 |
if (r < 0) |
| 1242 |
return r; |
| 1243 |
|
| 1244 |
r = bus_log_control_api_register(m->bus); |
| 1245 |
if (r < 0) |
| 1246 |
return r; |
| 1247 |
|
| 1248 |
r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.oom1", 0, NULL, NULL); |
| 1249 |
if (r < 0) |
| 1250 |
return log_error_errno(r, "Failed to request name: %m"); |
| 1251 |
|
| 1252 |
r = sd_bus_attach_event(m->bus, m->event, 0); |
| 1253 |
if (r < 0) |
| 1254 |
return log_error_errno(r, "Failed to attach bus to event loop: %m"); |
| 1255 |
|
| 1256 |
return 0; |
| 1257 |
} |
| 1258 |
|
| 1259 |
static int manager_varlink_init(Manager *m, int fd) { |
| 1260 |
_cleanup_(sd_varlink_server_unrefp) sd_varlink_server *s = NULL; |
| 1261 |
int r; |
| 1262 |
|
| 1263 |
assert(m); |
| 1264 |
assert(!m->varlink_server); |
| 1265 |
|
| 1266 |
r = varlink_server_new(&s, SD_VARLINK_SERVER_ACCOUNT_UID|SD_VARLINK_SERVER_INHERIT_USERDATA, m); |
| 1267 |
if (r < 0) |
| 1268 |
return log_error_errno(r, "Failed to allocate varlink server object: %m"); |
| 1269 |
|
| 1270 |
r = sd_varlink_server_add_interface_many( |
| 1271 |
s, |
| 1272 |
&vl_interface_io_systemd_oom, |
| 1273 |
&vl_interface_io_systemd_service); |
| 1274 |
if (r < 0) |
| 1275 |
return log_error_errno(r, "Failed to add Varlink interfaces to varlink server: %m"); |
| 1276 |
|
| 1277 |
r = sd_varlink_server_bind_method_many( |
| 1278 |
s, |
| 1279 |
"io.systemd.oom.ReportManagedOOMCGroups", process_managed_oom_request, |
| 1280 |
"io.systemd.service.Ping", varlink_method_ping, |
| 1281 |
"io.systemd.service.SetLogLevel", varlink_method_set_log_level, |
| 1282 |
"io.systemd.service.GetEnvironment", varlink_method_get_environment); |
| 1283 |
if (r < 0) |
| 1284 |
return log_error_errno(r, "Failed to register varlink methods: %m"); |
| 1285 |
|
| 1286 |
if (fd < 0) |
| 1287 |
r = sd_varlink_server_listen_address(s, VARLINK_PATH_MANAGED_OOM_USER, 0666); |
| 1288 |
else |
| 1289 |
r = sd_varlink_server_listen_fd(s, fd); |
| 1290 |
if (r < 0) |
| 1291 |
return log_error_errno(r, "Failed to bind to varlink socket %s: %m", |
| 1292 |
VARLINK_PATH_MANAGED_OOM_USER); |
| 1293 |
|
| 1294 |
r = sd_varlink_server_attach_event(s, m->event, SD_EVENT_PRIORITY_NORMAL); |
| 1295 |
if (r < 0) |
| 1296 |
return log_error_errno(r, "Failed to attach varlink connection to event loop: %m"); |
| 1297 |
|
| 1298 |
log_debug("Initialized systemd-oomd varlink server"); |
| 1299 |
|
| 1300 |
m->varlink_server = TAKE_PTR(s); |
| 1301 |
return 0; |
| 1302 |
} |
| 1303 |
|
| 1304 |
int manager_start( |
| 1305 |
Manager *m, |
| 1306 |
bool dry_run, |
| 1307 |
int fd) { |
| 1308 |
|
| 1309 |
int r; |
| 1310 |
|
| 1311 |
assert(m); |
| 1312 |
|
| 1313 |
m->dry_run = dry_run; |
| 1314 |
|
| 1315 |
r = manager_connect_bus(m); |
| 1316 |
if (r < 0) |
| 1317 |
return r; |
| 1318 |
|
| 1319 |
r = acquire_managed_oom_connect(m); |
| 1320 |
if (r < 0) |
| 1321 |
return r; |
| 1322 |
|
| 1323 |
r = manager_varlink_init(m, fd); |
| 1324 |
if (r < 0) |
| 1325 |
return r; |
| 1326 |
|
| 1327 |
r = monitor_memory_pressure_contexts(m); |
| 1328 |
if (r < 0) |
| 1329 |
return r; |
| 1330 |
|
| 1331 |
r = monitor_swap_contexts(m); |
| 1332 |
if (r < 0) |
| 1333 |
return r; |
| 1334 |
|
| 1335 |
r = monitor_rules_contexts(m); |
| 1336 |
if (r < 0) |
| 1337 |
return r; |
| 1338 |
|
| 1339 |
return 0; |
| 1340 |
} |
| 1341 |
|
| 1342 |
int manager_get_dump_string(Manager *m, char **ret) { |
| 1343 |
_cleanup_(memstream_done) MemStream ms = {}; |
| 1344 |
_cleanup_free_ OomdCGroupContext **sorted = NULL; |
| 1345 |
size_t n; |
| 1346 |
FILE *f; |
| 1347 |
int r; |
| 1348 |
|
| 1349 |
assert(m); |
| 1350 |
assert(ret); |
| 1351 |
|
| 1352 |
/* Always reread memory/swap info here. Otherwise it may be outdated if swap monitoring is off. |
| 1353 |
* Let's make sure to always report up-to-date data. */ |
| 1354 |
r = oomd_system_context_acquire("/proc/meminfo", &m->system_context); |
| 1355 |
if (r < 0) |
| 1356 |
log_debug_errno(r, "Failed to acquire system context, ignoring: %m"); |
| 1357 |
|
| 1358 |
f = memstream_init(&ms); |
| 1359 |
if (!f) |
| 1360 |
return -ENOMEM; |
| 1361 |
|
| 1362 |
fprintf(f, |
| 1363 |
"Dry Run: %s\n" |
| 1364 |
"Swap Used Limit: " PERMYRIAD_AS_PERCENT_FORMAT_STR "\n" |
| 1365 |
"Default Memory Pressure Limit: %lu.%02lu%%\n" |
| 1366 |
"Default Memory Pressure Duration: %s\n" |
| 1367 |
"System Context:\n", |
| 1368 |
yes_no(m->dry_run), |
| 1369 |
PERMYRIAD_AS_PERCENT_FORMAT_VAL(m->swap_used_limit_permyriad), |
| 1370 |
LOADAVG_INT_SIDE(m->default_mem_pressure_limit), LOADAVG_DECIMAL_SIDE(m->default_mem_pressure_limit), |
| 1371 |
FORMAT_TIMESPAN(m->default_mem_pressure_duration_usec, USEC_PER_SEC)); |
| 1372 |
oomd_dump_system_context(&m->system_context, f, "\t"); |
| 1373 |
|
| 1374 |
r = hashmap_dump_sorted(m->monitored_swap_cgroup_contexts, (void***) &sorted, &n); |
| 1375 |
if (r < 0) |
| 1376 |
return r; |
| 1377 |
|
| 1378 |
fprintf(f, "Swap Monitored CGroups:\n"); |
| 1379 |
FOREACH_ARRAY(c, sorted, n) |
| 1380 |
oomd_dump_swap_cgroup_context(*c, f, "\t"); |
| 1381 |
|
| 1382 |
sorted = mfree(sorted); |
| 1383 |
r = hashmap_dump_sorted(m->monitored_mem_pressure_cgroup_contexts, (void***) &sorted, &n); |
| 1384 |
if (r < 0) |
| 1385 |
return r; |
| 1386 |
|
| 1387 |
fprintf(f, "Memory Pressure Monitored CGroups:\n"); |
| 1388 |
FOREACH_ARRAY(c, sorted, n) |
| 1389 |
oomd_dump_memory_pressure_cgroup_context(*c, f, "\t"); |
| 1390 |
|
| 1391 |
return memstream_finalize(&ms, ret, NULL); |
| 1392 |
} |