]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/oom/oomd-util.c
6309d2c4739907c3f81dac56e8eb91eaabb5f05f
[thirdparty/systemd.git] / src / oom / oomd-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/xattr.h>
4 #include <unistd.h>
5
6 #include "errno-util.h"
7 #include "fd-util.h"
8 #include "fileio.h"
9 #include "format-util.h"
10 #include "oomd-util.h"
11 #include "parse-util.h"
12 #include "path-util.h"
13 #include "procfs-util.h"
14 #include "signal-util.h"
15 #include "sort-util.h"
16 #include "stat-util.h"
17 #include "stdio-util.h"
18 #include "user-util.h"
19
20 DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(
21 oomd_cgroup_ctx_hash_ops,
22 char,
23 string_hash_func,
24 string_compare_func,
25 OomdCGroupContext,
26 oomd_cgroup_context_free);
27
28 static int log_kill(pid_t pid, int sig, void *userdata) {
29 log_debug("oomd attempting to kill " PID_FMT " with %s", pid, signal_to_string(sig));
30 return 0;
31 }
32
33 static int increment_oomd_xattr(const char *path, const char *xattr, uint64_t num_procs_killed) {
34 _cleanup_free_ char *value = NULL;
35 char buf[DECIMAL_STR_MAX(uint64_t) + 1];
36 uint64_t curr_count = 0;
37 int r;
38
39 assert(path);
40 assert(xattr);
41
42 r = cg_get_xattr_malloc(SYSTEMD_CGROUP_CONTROLLER, path, xattr, &value);
43 if (r < 0 && !ERRNO_IS_XATTR_ABSENT(r))
44 return r;
45
46 if (!isempty(value)) {
47 r = safe_atou64(value, &curr_count);
48 if (r < 0)
49 return r;
50 }
51
52 if (curr_count > UINT64_MAX - num_procs_killed)
53 return -EOVERFLOW;
54
55 xsprintf(buf, "%"PRIu64, curr_count + num_procs_killed);
56 r = cg_set_xattr(SYSTEMD_CGROUP_CONTROLLER, path, xattr, buf, strlen(buf), 0);
57 if (r < 0)
58 return r;
59
60 return 0;
61 }
62
63 OomdCGroupContext *oomd_cgroup_context_free(OomdCGroupContext *ctx) {
64 if (!ctx)
65 return NULL;
66
67 free(ctx->path);
68 return mfree(ctx);
69 }
70
71 int oomd_pressure_above(Hashmap *h, usec_t duration, Set **ret) {
72 _cleanup_set_free_ Set *targets = NULL;
73 OomdCGroupContext *ctx;
74 char *key;
75 int r;
76
77 assert(h);
78 assert(ret);
79
80 targets = set_new(NULL);
81 if (!targets)
82 return -ENOMEM;
83
84 HASHMAP_FOREACH_KEY(ctx, key, h) {
85 if (ctx->memory_pressure.avg10 > ctx->mem_pressure_limit) {
86 usec_t diff;
87
88 if (ctx->mem_pressure_limit_hit_start == 0)
89 ctx->mem_pressure_limit_hit_start = now(CLOCK_MONOTONIC);
90
91 diff = now(CLOCK_MONOTONIC) - ctx->mem_pressure_limit_hit_start;
92 if (diff >= duration) {
93 r = set_put(targets, ctx);
94 if (r < 0)
95 return -ENOMEM;
96 }
97 } else
98 ctx->mem_pressure_limit_hit_start = 0;
99 }
100
101 if (!set_isempty(targets)) {
102 *ret = TAKE_PTR(targets);
103 return 1;
104 }
105
106 *ret = NULL;
107 return 0;
108 }
109
110 uint64_t oomd_pgscan_rate(const OomdCGroupContext *c) {
111 uint64_t last_pgscan;
112
113 assert(c);
114
115 /* If last_pgscan > pgscan, assume the cgroup was recreated and reset last_pgscan to zero.
116 * pgscan is monotonic and in practice should not decrease (except in the recreation case). */
117 last_pgscan = c->last_pgscan;
118 if (c->last_pgscan > c->pgscan) {
119 log_debug("Last pgscan %"PRIu64" greater than current pgscan %"PRIu64" for %s. Using last pgscan of zero.",
120 c->last_pgscan, c->pgscan, c->path);
121 last_pgscan = 0;
122 }
123
124 return c->pgscan - last_pgscan;
125 }
126
127 bool oomd_mem_available_below(const OomdSystemContext *ctx, int threshold_permyriad) {
128 uint64_t mem_threshold;
129
130 assert(ctx);
131 assert(threshold_permyriad <= 10000);
132
133 mem_threshold = ctx->mem_total * threshold_permyriad / (uint64_t) 10000;
134 return LESS_BY(ctx->mem_total, ctx->mem_used) < mem_threshold;
135 }
136
137 bool oomd_swap_free_below(const OomdSystemContext *ctx, int threshold_permyriad) {
138 uint64_t swap_threshold;
139
140 assert(ctx);
141 assert(threshold_permyriad <= 10000);
142
143 swap_threshold = ctx->swap_total * threshold_permyriad / (uint64_t) 10000;
144 return (ctx->swap_total - ctx->swap_used) < swap_threshold;
145 }
146
147 int oomd_fetch_cgroup_oom_preference(OomdCGroupContext *ctx, const char *prefix) {
148 uid_t uid;
149 int r;
150
151 assert(ctx);
152
153 prefix = empty_to_root(prefix);
154
155 if (!path_startswith(ctx->path, prefix))
156 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
157 "%s is not a descendant of %s", ctx->path, prefix);
158
159 r = cg_get_owner(SYSTEMD_CGROUP_CONTROLLER, ctx->path, &uid);
160 if (r < 0)
161 return log_debug_errno(r, "Failed to get owner/group from %s: %m", ctx->path);
162
163 if (uid != 0) {
164 uid_t prefix_uid;
165
166 r = cg_get_owner(SYSTEMD_CGROUP_CONTROLLER, prefix, &prefix_uid);
167 if (r < 0)
168 return log_debug_errno(r, "Failed to get owner/group from %s: %m", prefix);
169
170 if (uid != prefix_uid) {
171 ctx->preference = MANAGED_OOM_PREFERENCE_NONE;
172 return 0;
173 }
174 }
175
176 /* Ignore most errors when reading the xattr since it is usually unset and cgroup xattrs are only used
177 * as an optional feature of systemd-oomd (and the system might not even support them). */
178 r = cg_get_xattr_bool(SYSTEMD_CGROUP_CONTROLLER, ctx->path, "user.oomd_avoid");
179 if (r == -ENOMEM)
180 return log_oom_debug();
181 if (r < 0 && !ERRNO_IS_XATTR_ABSENT(r))
182 log_debug_errno(r, "Failed to get xattr user.oomd_avoid, ignoring: %m");
183 ctx->preference = r > 0 ? MANAGED_OOM_PREFERENCE_AVOID : ctx->preference;
184
185 r = cg_get_xattr_bool(SYSTEMD_CGROUP_CONTROLLER, ctx->path, "user.oomd_omit");
186 if (r == -ENOMEM)
187 return log_oom_debug();
188 if (r < 0 && !ERRNO_IS_XATTR_ABSENT(r))
189 log_debug_errno(r, "Failed to get xattr user.oomd_omit, ignoring: %m");
190 ctx->preference = r > 0 ? MANAGED_OOM_PREFERENCE_OMIT : ctx->preference;
191
192 return 0;
193 }
194
195 int oomd_sort_cgroup_contexts(Hashmap *h, oomd_compare_t compare_func, const char *prefix, OomdCGroupContext ***ret) {
196 _cleanup_free_ OomdCGroupContext **sorted = NULL;
197 OomdCGroupContext *item;
198 size_t k = 0;
199 int r;
200
201 assert(h);
202 assert(compare_func);
203 assert(ret);
204
205 sorted = new0(OomdCGroupContext*, hashmap_size(h));
206 if (!sorted)
207 return -ENOMEM;
208
209 HASHMAP_FOREACH(item, h) {
210 /* Skip over cgroups that are not valid candidates or are explicitly marked for omission */
211 if (item->path && prefix && !path_startswith(item->path, prefix))
212 continue;
213
214 r = oomd_fetch_cgroup_oom_preference(item, prefix);
215 if (r == -ENOMEM)
216 return r;
217
218 if (item->preference == MANAGED_OOM_PREFERENCE_OMIT)
219 continue;
220
221 sorted[k++] = item;
222 }
223
224 typesafe_qsort(sorted, k, compare_func);
225
226 *ret = TAKE_PTR(sorted);
227
228 assert(k <= INT_MAX);
229 return (int) k;
230 }
231
232 int oomd_cgroup_kill(const char *path, bool recurse, bool dry_run) {
233 _cleanup_set_free_ Set *pids_killed = NULL;
234 int r;
235
236 assert(path);
237
238 if (dry_run) {
239 _cleanup_free_ char *cg_path = NULL;
240
241 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, path, NULL, &cg_path);
242 if (r < 0)
243 return r;
244
245 log_info("oomd dry-run: Would have tried to kill %s with recurse=%s", cg_path, true_false(recurse));
246 return 0;
247 }
248
249 pids_killed = set_new(NULL);
250 if (!pids_killed)
251 return -ENOMEM;
252
253 r = increment_oomd_xattr(path, "user.oomd_ooms", 1);
254 if (r < 0)
255 log_debug_errno(r, "Failed to set user.oomd_ooms before kill: %m");
256
257 if (recurse)
258 r = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, path, SIGKILL, CGROUP_IGNORE_SELF, pids_killed, log_kill, NULL);
259 else
260 r = cg_kill(SYSTEMD_CGROUP_CONTROLLER, path, SIGKILL, CGROUP_IGNORE_SELF, pids_killed, log_kill, NULL);
261
262 /* The cgroup could have been cleaned up after we have sent SIGKILL to all of the processes, but before
263 * we could do one last iteration of cgroup.procs to check. Or the service unit could have exited and
264 * was removed between picking candidates and coming into this function. In either case, let's log
265 * about it let the caller decide what to do once they know how many PIDs were killed. */
266 if (IN_SET(r, -ENOENT, -ENODEV))
267 log_debug_errno(r, "Error when sending SIGKILL to processes in cgroup path %s, ignoring: %m", path);
268 else if (r < 0)
269 return r;
270
271 if (set_isempty(pids_killed))
272 log_debug("Nothing killed when attempting to kill %s", path);
273
274 r = increment_oomd_xattr(path, "user.oomd_kill", set_size(pids_killed));
275 if (r < 0)
276 log_debug_errno(r, "Failed to set user.oomd_kill on kill: %m");
277
278 return set_size(pids_killed) != 0;
279 }
280
281 typedef void (*dump_candidate_func)(const OomdCGroupContext *ctx, FILE *f, const char *prefix);
282
283 static int dump_kill_candidates(OomdCGroupContext **sorted, int n, int dump_until, dump_candidate_func dump_func) {
284 /* Try dumping top offendors, ignoring any errors that might happen. */
285 _cleanup_free_ char *dump = NULL;
286 _cleanup_fclose_ FILE *f = NULL;
287 int r;
288 size_t size;
289
290 f = open_memstream_unlocked(&dump, &size);
291 if (!f)
292 return -errno;
293
294 fprintf(f, "Considered %d cgroups for killing, top candidates were:\n", n);
295 for (int i = 0; i < dump_until; i++)
296 dump_func(sorted[i], f, "\t");
297
298 r = fflush_and_check(f);
299 if (r < 0)
300 return r;
301
302 f = safe_fclose(f);
303
304 if (!dump)
305 return -ENOMEM;
306
307 return log_dump(LOG_INFO, dump);
308 }
309
310 int oomd_kill_by_pgscan_rate(Hashmap *h, const char *prefix, bool dry_run, char **ret_selected) {
311 _cleanup_free_ OomdCGroupContext **sorted = NULL;
312 int n, r, ret = 0;
313 int dump_until;
314
315 assert(h);
316 assert(ret_selected);
317
318 n = oomd_sort_cgroup_contexts(h, compare_pgscan_rate_and_memory_usage, prefix, &sorted);
319 if (n < 0)
320 return n;
321
322 dump_until = MIN(n, DUMP_ON_KILL_COUNT);
323 for (int i = 0; i < n; i++) {
324 /* Skip cgroups with no reclaim and memory usage; it won't alleviate pressure.
325 * Continue since there might be "avoid" cgroups at the end. */
326 if (sorted[i]->pgscan == 0 && sorted[i]->current_memory_usage == 0)
327 continue;
328
329 r = oomd_cgroup_kill(sorted[i]->path, /* recurse= */ true, /* dry_run= */ dry_run);
330 if (r == -ENOMEM)
331 return r; /* Treat oom as a hard error */
332 if (r < 0) {
333 if (ret == 0)
334 ret = r;
335 continue; /* Try to find something else to kill */
336 }
337
338 dump_until = MAX(dump_until, i + 1);
339 char *selected = strdup(sorted[i]->path);
340 if (!selected)
341 return -ENOMEM;
342 *ret_selected = selected;
343 ret = r;
344 break;
345 }
346
347 dump_kill_candidates(sorted, n, dump_until, oomd_dump_memory_pressure_cgroup_context);
348
349 return ret;
350 }
351
352 int oomd_kill_by_swap_usage(Hashmap *h, uint64_t threshold_usage, bool dry_run, char **ret_selected) {
353 _cleanup_free_ OomdCGroupContext **sorted = NULL;
354 int n, r, ret = 0;
355 int dump_until;
356
357 assert(h);
358 assert(ret_selected);
359
360 n = oomd_sort_cgroup_contexts(h, compare_swap_usage, NULL, &sorted);
361 if (n < 0)
362 return n;
363
364 dump_until = MIN(n, DUMP_ON_KILL_COUNT);
365 /* Try to kill cgroups with non-zero swap usage until we either succeed in killing or we get to a cgroup with
366 * no swap usage. Threshold killing only cgroups with more than threshold swap usage. */
367 for (int i = 0; i < n; i++) {
368 /* Skip over cgroups with not enough swap usage. Don't break since there might be "avoid"
369 * cgroups at the end. */
370 if (sorted[i]->swap_usage <= threshold_usage)
371 continue;
372
373 r = oomd_cgroup_kill(sorted[i]->path, /* recurse= */ true, /* dry_run= */ dry_run);
374 if (r == -ENOMEM)
375 return r; /* Treat oom as a hard error */
376 if (r < 0) {
377 if (ret == 0)
378 ret = r;
379 continue; /* Try to find something else to kill */
380 }
381
382 dump_until = MAX(dump_until, i + 1);
383 char *selected = strdup(sorted[i]->path);
384 if (!selected)
385 return -ENOMEM;
386 *ret_selected = selected;
387 ret = r;
388 break;
389 }
390
391 dump_kill_candidates(sorted, n, dump_until, oomd_dump_swap_cgroup_context);
392
393 return ret;
394 }
395
396 int oomd_cgroup_context_acquire(const char *path, OomdCGroupContext **ret) {
397 _cleanup_(oomd_cgroup_context_freep) OomdCGroupContext *ctx = NULL;
398 _cleanup_free_ char *p = NULL, *val = NULL;
399 bool is_root;
400 int r;
401
402 assert(path);
403 assert(ret);
404
405 ctx = new0(OomdCGroupContext, 1);
406 if (!ctx)
407 return -ENOMEM;
408
409 is_root = empty_or_root(path);
410 ctx->preference = MANAGED_OOM_PREFERENCE_NONE;
411
412 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, path, "memory.pressure", &p);
413 if (r < 0)
414 return log_debug_errno(r, "Error getting cgroup memory pressure path from %s: %m", path);
415
416 r = read_resource_pressure(p, PRESSURE_TYPE_FULL, &ctx->memory_pressure);
417 if (r < 0)
418 return log_debug_errno(r, "Error parsing memory pressure from %s: %m", p);
419
420 if (is_root) {
421 r = procfs_memory_get_used(&ctx->current_memory_usage);
422 if (r < 0)
423 return log_debug_errno(r, "Error getting memory used from procfs: %m");
424 } else {
425 r = cg_get_attribute_as_uint64(SYSTEMD_CGROUP_CONTROLLER, path, "memory.current", &ctx->current_memory_usage);
426 if (r < 0)
427 return log_debug_errno(r, "Error getting memory.current from %s: %m", path);
428
429 r = cg_get_attribute_as_uint64(SYSTEMD_CGROUP_CONTROLLER, path, "memory.min", &ctx->memory_min);
430 if (r < 0)
431 return log_debug_errno(r, "Error getting memory.min from %s: %m", path);
432
433 r = cg_get_attribute_as_uint64(SYSTEMD_CGROUP_CONTROLLER, path, "memory.low", &ctx->memory_low);
434 if (r < 0)
435 return log_debug_errno(r, "Error getting memory.low from %s: %m", path);
436
437 r = cg_get_attribute_as_uint64(SYSTEMD_CGROUP_CONTROLLER, path, "memory.swap.current", &ctx->swap_usage);
438 if (r == -ENODATA)
439 /* The kernel can be compiled without support for memory.swap.* files,
440 * or it can be disabled with boot param 'swapaccount=0' */
441 log_once(LOG_WARNING, "No kernel support for memory.swap.current from %s (try boot param swapaccount=1), ignoring.", path);
442 else if (r < 0)
443 return log_debug_errno(r, "Error getting memory.swap.current from %s: %m", path);
444
445 r = cg_get_keyed_attribute(SYSTEMD_CGROUP_CONTROLLER, path, "memory.stat", STRV_MAKE("pgscan"), &val);
446 if (r < 0)
447 return log_debug_errno(r, "Error getting pgscan from memory.stat under %s: %m", path);
448
449 r = safe_atou64(val, &ctx->pgscan);
450 if (r < 0)
451 return log_debug_errno(r, "Error converting pgscan value to uint64_t: %m");
452 }
453
454 ctx->path = strdup(empty_to_root(path));
455 if (!ctx->path)
456 return -ENOMEM;
457
458 *ret = TAKE_PTR(ctx);
459 return 0;
460 }
461
462 int oomd_system_context_acquire(const char *proc_meminfo_path, OomdSystemContext *ret) {
463 _cleanup_fclose_ FILE *f = NULL;
464 unsigned field_filled = 0;
465 OomdSystemContext ctx = {};
466 uint64_t mem_available, swap_free;
467 int r;
468
469 enum {
470 MEM_TOTAL = 1U << 0,
471 MEM_AVAILABLE = 1U << 1,
472 SWAP_TOTAL = 1U << 2,
473 SWAP_FREE = 1U << 3,
474 ALL = MEM_TOTAL|MEM_AVAILABLE|SWAP_TOTAL|SWAP_FREE,
475 };
476
477 assert(proc_meminfo_path);
478 assert(ret);
479
480 f = fopen(proc_meminfo_path, "re");
481 if (!f)
482 return -errno;
483
484 for (;;) {
485 _cleanup_free_ char *line = NULL;
486 char *word;
487
488 r = read_line(f, LONG_LINE_MAX, &line);
489 if (r < 0)
490 return r;
491 if (r == 0)
492 return -EINVAL;
493
494 if ((word = startswith(line, "MemTotal:"))) {
495 field_filled |= MEM_TOTAL;
496 r = convert_meminfo_value_to_uint64_bytes(word, &ctx.mem_total);
497 } else if ((word = startswith(line, "MemAvailable:"))) {
498 field_filled |= MEM_AVAILABLE;
499 r = convert_meminfo_value_to_uint64_bytes(word, &mem_available);
500 } else if ((word = startswith(line, "SwapTotal:"))) {
501 field_filled |= SWAP_TOTAL;
502 r = convert_meminfo_value_to_uint64_bytes(word, &ctx.swap_total);
503 } else if ((word = startswith(line, "SwapFree:"))) {
504 field_filled |= SWAP_FREE;
505 r = convert_meminfo_value_to_uint64_bytes(word, &swap_free);
506 } else
507 continue;
508
509 if (r < 0)
510 return log_debug_errno(r, "Error converting '%s' from %s to uint64_t: %m", line, proc_meminfo_path);
511
512 if (field_filled == ALL)
513 break;
514 }
515
516 if (field_filled != ALL)
517 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "%s is missing expected fields", proc_meminfo_path);
518
519 if (mem_available > ctx.mem_total)
520 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
521 "MemAvailable (%" PRIu64 ") cannot be greater than MemTotal (%" PRIu64 ") %m",
522 mem_available,
523 ctx.mem_total);
524
525 if (swap_free > ctx.swap_total)
526 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
527 "SwapFree (%" PRIu64 ") cannot be greater than SwapTotal (%" PRIu64 ") %m",
528 swap_free,
529 ctx.swap_total);
530
531 ctx.mem_used = ctx.mem_total - mem_available;
532 ctx.swap_used = ctx.swap_total - swap_free;
533
534 *ret = ctx;
535 return 0;
536 }
537
538 int oomd_insert_cgroup_context(Hashmap *old_h, Hashmap *new_h, const char *path) {
539 _cleanup_(oomd_cgroup_context_freep) OomdCGroupContext *curr_ctx = NULL;
540 OomdCGroupContext *old_ctx;
541 int r;
542
543 assert(new_h);
544 assert(path);
545
546 path = empty_to_root(path);
547
548 r = oomd_cgroup_context_acquire(path, &curr_ctx);
549 if (r < 0)
550 return log_debug_errno(r, "Failed to get OomdCGroupContext for %s: %m", path);
551
552 assert_se(streq(path, curr_ctx->path));
553
554 old_ctx = hashmap_get(old_h, path);
555 if (old_ctx) {
556 curr_ctx->last_pgscan = old_ctx->pgscan;
557 curr_ctx->mem_pressure_limit = old_ctx->mem_pressure_limit;
558 curr_ctx->mem_pressure_limit_hit_start = old_ctx->mem_pressure_limit_hit_start;
559 curr_ctx->last_had_mem_reclaim = old_ctx->last_had_mem_reclaim;
560 }
561
562 if (oomd_pgscan_rate(curr_ctx) > 0)
563 curr_ctx->last_had_mem_reclaim = now(CLOCK_MONOTONIC);
564
565 r = hashmap_put(new_h, curr_ctx->path, curr_ctx);
566 if (r < 0)
567 return r;
568
569 TAKE_PTR(curr_ctx);
570 return 0;
571 }
572
573 void oomd_update_cgroup_contexts_between_hashmaps(Hashmap *old_h, Hashmap *curr_h) {
574 OomdCGroupContext *ctx;
575
576 assert(old_h);
577 assert(curr_h);
578
579 HASHMAP_FOREACH(ctx, curr_h) {
580 OomdCGroupContext *old_ctx;
581
582 old_ctx = hashmap_get(old_h, ctx->path);
583 if (!old_ctx)
584 continue;
585
586 ctx->last_pgscan = old_ctx->pgscan;
587 ctx->mem_pressure_limit = old_ctx->mem_pressure_limit;
588 ctx->mem_pressure_limit_hit_start = old_ctx->mem_pressure_limit_hit_start;
589 ctx->last_had_mem_reclaim = old_ctx->last_had_mem_reclaim;
590
591 if (oomd_pgscan_rate(ctx) > 0)
592 ctx->last_had_mem_reclaim = now(CLOCK_MONOTONIC);
593 }
594 }
595
596 void oomd_dump_swap_cgroup_context(const OomdCGroupContext *ctx, FILE *f, const char *prefix) {
597 assert(ctx);
598 assert(f);
599
600 if (!empty_or_root(ctx->path))
601 fprintf(f,
602 "%sPath: %s\n"
603 "%s\tSwap Usage: %s\n",
604 strempty(prefix), ctx->path,
605 strempty(prefix), FORMAT_BYTES(ctx->swap_usage));
606 else
607 fprintf(f,
608 "%sPath: %s\n"
609 "%s\tSwap Usage: (see System Context)\n",
610 strempty(prefix), ctx->path,
611 strempty(prefix));
612 }
613
614 void oomd_dump_memory_pressure_cgroup_context(const OomdCGroupContext *ctx, FILE *f, const char *prefix) {
615 assert(ctx);
616 assert(f);
617
618 fprintf(f,
619 "%sPath: %s\n"
620 "%s\tMemory Pressure Limit: %lu.%02lu%%\n"
621 "%s\tPressure: Avg10: %lu.%02lu Avg60: %lu.%02lu Avg300: %lu.%02lu Total: %s\n"
622 "%s\tCurrent Memory Usage: %s\n",
623 strempty(prefix), ctx->path,
624 strempty(prefix), LOADAVG_INT_SIDE(ctx->mem_pressure_limit), LOADAVG_DECIMAL_SIDE(ctx->mem_pressure_limit),
625 strempty(prefix),
626 LOADAVG_INT_SIDE(ctx->memory_pressure.avg10), LOADAVG_DECIMAL_SIDE(ctx->memory_pressure.avg10),
627 LOADAVG_INT_SIDE(ctx->memory_pressure.avg60), LOADAVG_DECIMAL_SIDE(ctx->memory_pressure.avg60),
628 LOADAVG_INT_SIDE(ctx->memory_pressure.avg300), LOADAVG_DECIMAL_SIDE(ctx->memory_pressure.avg300),
629 FORMAT_TIMESPAN(ctx->memory_pressure.total, USEC_PER_SEC),
630 strempty(prefix), FORMAT_BYTES(ctx->current_memory_usage));
631
632 if (!empty_or_root(ctx->path))
633 fprintf(f,
634 "%s\tMemory Min: %s\n"
635 "%s\tMemory Low: %s\n"
636 "%s\tPgscan: %" PRIu64 "\n"
637 "%s\tLast Pgscan: %" PRIu64 "\n",
638 strempty(prefix), FORMAT_BYTES_CGROUP_PROTECTION(ctx->memory_min),
639 strempty(prefix), FORMAT_BYTES_CGROUP_PROTECTION(ctx->memory_low),
640 strempty(prefix), ctx->pgscan,
641 strempty(prefix), ctx->last_pgscan);
642 }
643
644 void oomd_dump_system_context(const OomdSystemContext *ctx, FILE *f, const char *prefix) {
645 assert(ctx);
646 assert(f);
647
648 fprintf(f,
649 "%sMemory: Used: %s Total: %s\n"
650 "%sSwap: Used: %s Total: %s\n",
651 strempty(prefix),
652 FORMAT_BYTES(ctx->mem_used),
653 FORMAT_BYTES(ctx->mem_total),
654 strempty(prefix),
655 FORMAT_BYTES(ctx->swap_used),
656 FORMAT_BYTES(ctx->swap_total));
657 }