]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/oom/test-oomd-util.c
Merge pull request #18701 from bugaevc/mdns-unicast
[thirdparty/systemd.git] / src / oom / test-oomd-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "alloc-util.h"
6 #include "cgroup-setup.h"
7 #include "cgroup-util.h"
8 #include "fileio.h"
9 #include "fs-util.h"
10 #include "oomd-util.h"
11 #include "parse-util.h"
12 #include "path-util.h"
13 #include "string-util.h"
14 #include "strv.h"
15 #include "tests.h"
16
17 static int fork_and_sleep(unsigned sleep_min) {
18 usec_t n, timeout, ts;
19
20 pid_t pid = fork();
21 assert_se(pid >= 0);
22
23 if (pid == 0) {
24 timeout = sleep_min * USEC_PER_MINUTE;
25 ts = now(CLOCK_MONOTONIC);
26 for (;;) {
27 n = now(CLOCK_MONOTONIC);
28 if (ts + timeout < n) {
29 log_error("Child timed out waiting to be killed");
30 abort();
31 }
32 sleep(1);
33 }
34 }
35
36 return pid;
37 }
38
39 static void test_oomd_cgroup_kill(void) {
40 _cleanup_free_ char *cgroup_root = NULL, *cgroup = NULL;
41 int pid[2];
42 int r;
43
44 if (geteuid() != 0)
45 return (void) log_tests_skipped("not root");
46
47 if (cg_all_unified() <= 0)
48 return (void) log_tests_skipped("cgroups are not running in unified mode");
49
50 assert_se(cg_pid_get_path(NULL, 0, &cgroup_root) >= 0);
51
52 /* Create another cgroup below this one for the pids we forked off. We need this to be managed
53 * by the test so that pid1 doesn't delete it before we can read the xattrs. */
54 cgroup = path_join(cgroup_root, "oomdkilltest");
55 assert(cgroup);
56 assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, cgroup) >= 0);
57
58 /* If we don't have permissions to set xattrs we're likely in a userns or missing capabilities */
59 r = cg_set_xattr(SYSTEMD_CGROUP_CONTROLLER, cgroup, "user.oomd_test", "test", 4, 0);
60 if (ERRNO_IS_PRIVILEGE(r) || ERRNO_IS_NOT_SUPPORTED(r))
61 return (void) log_tests_skipped("Cannot set user xattrs");
62
63 /* Do this twice to also check the increment behavior on the xattrs */
64 for (int i = 0; i < 2; i++) {
65 _cleanup_free_ char *v = NULL;
66
67 for (int j = 0; j < 2; j++) {
68 pid[j] = fork_and_sleep(5);
69 assert_se(cg_attach(SYSTEMD_CGROUP_CONTROLLER, cgroup, pid[j]) >= 0);
70 }
71
72 r = oomd_cgroup_kill(cgroup, false /* recurse */, false /* dry run */);
73 if (r <= 0) {
74 log_debug_errno(r, "Failed to kill processes under %s: %m", cgroup);
75 abort();
76 }
77
78 /* Wait a bit since processes may take some time to be cleaned up. */
79 sleep(2);
80 assert_se(cg_is_empty(SYSTEMD_CGROUP_CONTROLLER, cgroup) == true);
81
82 assert_se(cg_get_xattr_malloc(SYSTEMD_CGROUP_CONTROLLER, cgroup, "user.oomd_kill", &v) >= 0);
83 assert_se(memcmp(v, i == 0 ? "2" : "4", 2) == 0);
84 }
85 }
86
87 static void test_oomd_cgroup_context_acquire_and_insert(void) {
88 _cleanup_hashmap_free_ Hashmap *h1 = NULL, *h2 = NULL;
89 _cleanup_(oomd_cgroup_context_freep) OomdCGroupContext *ctx = NULL;
90 _cleanup_free_ char *cgroup = NULL;
91 ManagedOOMPreference root_pref;
92 OomdCGroupContext *c1, *c2;
93 bool test_xattrs;
94 int root_xattrs, r;
95
96 if (geteuid() != 0)
97 return (void) log_tests_skipped("not root");
98
99 if (!is_pressure_supported())
100 return (void) log_tests_skipped("system does not support pressure");
101
102 if (cg_all_unified() <= 0)
103 return (void) log_tests_skipped("cgroups are not running in unified mode");
104
105 assert_se(cg_pid_get_path(NULL, 0, &cgroup) >= 0);
106
107 /* If we don't have permissions to set xattrs we're likely in a userns or missing capabilities
108 * so skip the xattr portions of the test. */
109 r = cg_set_xattr(SYSTEMD_CGROUP_CONTROLLER, cgroup, "user.oomd_test", "1", 1, 0);
110 test_xattrs = !ERRNO_IS_PRIVILEGE(r) && !ERRNO_IS_NOT_SUPPORTED(r);
111
112 if (test_xattrs) {
113 assert_se(cg_set_xattr(SYSTEMD_CGROUP_CONTROLLER, cgroup, "user.oomd_omit", "1", 1, 0) >= 0);
114 assert_se(cg_set_xattr(SYSTEMD_CGROUP_CONTROLLER, cgroup, "user.oomd_avoid", "1", 1, 0) >= 0);
115 }
116
117 assert_se(oomd_cgroup_context_acquire(cgroup, &ctx) == 0);
118
119 assert_se(streq(ctx->path, cgroup));
120 assert_se(ctx->current_memory_usage > 0);
121 assert_se(ctx->memory_min == 0);
122 assert_se(ctx->memory_low == 0);
123 assert_se(ctx->swap_usage == 0);
124 assert_se(ctx->last_pgscan == 0);
125 assert_se(ctx->pgscan == 0);
126 /* omit takes precedence over avoid when both are set to true */
127 if (test_xattrs)
128 assert_se(ctx->preference == MANAGED_OOM_PREFERENCE_OMIT);
129 else
130 assert_se(ctx->preference == MANAGED_OOM_PREFERENCE_NONE);
131 ctx = oomd_cgroup_context_free(ctx);
132
133 /* also check when only avoid is set to true */
134 if (test_xattrs) {
135 assert_se(cg_set_xattr(SYSTEMD_CGROUP_CONTROLLER, cgroup, "user.oomd_omit", "0", 1, 0) >= 0);
136 assert_se(cg_set_xattr(SYSTEMD_CGROUP_CONTROLLER, cgroup, "user.oomd_avoid", "1", 1, 0) >= 0);
137 }
138 assert_se(oomd_cgroup_context_acquire(cgroup, &ctx) == 0);
139 if (test_xattrs)
140 assert_se(ctx->preference == MANAGED_OOM_PREFERENCE_AVOID);
141 ctx = oomd_cgroup_context_free(ctx);
142
143 /* Test the root cgroup */
144 /* Root cgroup is live and not made on demand like the cgroup the test runs in. It can have varying
145 * xattrs set already so let's read in the booleans first to get the final preference value. */
146 root_xattrs = cg_get_xattr_bool(SYSTEMD_CGROUP_CONTROLLER, "", "user.oomd_omit");
147 root_pref = root_xattrs > 0 ? MANAGED_OOM_PREFERENCE_OMIT : MANAGED_OOM_PREFERENCE_NONE;
148 root_xattrs = cg_get_xattr_bool(SYSTEMD_CGROUP_CONTROLLER, "", "user.oomd_avoid");
149 root_pref = root_xattrs > 0 ? MANAGED_OOM_PREFERENCE_AVOID : MANAGED_OOM_PREFERENCE_NONE;
150 assert_se(oomd_cgroup_context_acquire("", &ctx) == 0);
151 assert_se(streq(ctx->path, "/"));
152 assert_se(ctx->current_memory_usage > 0);
153 assert_se(ctx->preference == root_pref);
154
155 /* Test hashmap inserts */
156 assert_se(h1 = hashmap_new(&oomd_cgroup_ctx_hash_ops));
157 assert_se(oomd_insert_cgroup_context(NULL, h1, cgroup) == 0);
158 c1 = hashmap_get(h1, cgroup);
159 assert_se(c1);
160 assert_se(oomd_insert_cgroup_context(NULL, h1, cgroup) == -EEXIST);
161
162 /* make sure certain values from h1 get updated in h2 */
163 c1->pgscan = UINT64_MAX;
164 c1->mem_pressure_limit = 6789;
165 c1->mem_pressure_limit_hit_start = 42;
166 c1->last_had_mem_reclaim = 888;
167 assert_se(h2 = hashmap_new(&oomd_cgroup_ctx_hash_ops));
168 assert_se(oomd_insert_cgroup_context(h1, h2, cgroup) == 0);
169 c1 = hashmap_get(h1, cgroup);
170 c2 = hashmap_get(h2, cgroup);
171 assert_se(c1);
172 assert_se(c2);
173 assert_se(c1 != c2);
174 assert_se(c2->last_pgscan == UINT64_MAX);
175 assert_se(c2->mem_pressure_limit == 6789);
176 assert_se(c2->mem_pressure_limit_hit_start == 42);
177 assert_se(c2->last_had_mem_reclaim == 888); /* assumes the live pgscan is less than UINT64_MAX */
178
179 /* Assert that avoid/omit are not set if the cgroup is not owned by root */
180 if (test_xattrs) {
181 ctx = oomd_cgroup_context_free(ctx);
182 assert_se(cg_set_access(SYSTEMD_CGROUP_CONTROLLER, cgroup, 65534, 0) >= 0);
183 assert_se(oomd_cgroup_context_acquire(cgroup, &ctx) == 0);
184 assert_se(ctx->preference == MANAGED_OOM_PREFERENCE_NONE);
185 }
186 }
187
188 static void test_oomd_update_cgroup_contexts_between_hashmaps(void) {
189 _cleanup_hashmap_free_ Hashmap *h_old = NULL, *h_new = NULL;
190 OomdCGroupContext *c_old, *c_new;
191 char **paths = STRV_MAKE("/0.slice",
192 "/1.slice");
193
194 OomdCGroupContext ctx_old[2] = {
195 { .path = paths[0],
196 .mem_pressure_limit = 5,
197 .mem_pressure_limit_hit_start = 777,
198 .last_had_mem_reclaim = 888,
199 .pgscan = 57 },
200 { .path = paths[1],
201 .mem_pressure_limit = 6,
202 .mem_pressure_limit_hit_start = 888,
203 .last_had_mem_reclaim = 888,
204 .pgscan = 42 },
205 };
206
207 OomdCGroupContext ctx_new[2] = {
208 { .path = paths[0],
209 .pgscan = 57 },
210 { .path = paths[1],
211 .pgscan = 101 },
212 };
213
214 assert_se(h_old = hashmap_new(&string_hash_ops));
215 assert_se(hashmap_put(h_old, paths[0], &ctx_old[0]) >= 0);
216 assert_se(hashmap_put(h_old, paths[1], &ctx_old[1]) >= 0);
217
218 assert_se(h_new = hashmap_new(&string_hash_ops));
219 assert_se(hashmap_put(h_new, paths[0], &ctx_new[0]) >= 0);
220 assert_se(hashmap_put(h_new, paths[1], &ctx_new[1]) >= 0);
221
222 oomd_update_cgroup_contexts_between_hashmaps(h_old, h_new);
223
224 assert_se(c_old = hashmap_get(h_old, "/0.slice"));
225 assert_se(c_new = hashmap_get(h_new, "/0.slice"));
226 assert_se(c_old->pgscan == c_new->last_pgscan);
227 assert_se(c_old->mem_pressure_limit == c_new->mem_pressure_limit);
228 assert_se(c_old->mem_pressure_limit_hit_start == c_new->mem_pressure_limit_hit_start);
229 assert_se(c_old->last_had_mem_reclaim == c_new->last_had_mem_reclaim);
230
231 assert_se(c_old = hashmap_get(h_old, "/1.slice"));
232 assert_se(c_new = hashmap_get(h_new, "/1.slice"));
233 assert_se(c_old->pgscan == c_new->last_pgscan);
234 assert_se(c_old->mem_pressure_limit == c_new->mem_pressure_limit);
235 assert_se(c_old->mem_pressure_limit_hit_start == c_new->mem_pressure_limit_hit_start);
236 assert_se(c_new->last_had_mem_reclaim > c_old->last_had_mem_reclaim);
237 }
238
239 static void test_oomd_system_context_acquire(void) {
240 _cleanup_(unlink_tempfilep) char path[] = "/oomdgetsysctxtestXXXXXX";
241 OomdSystemContext ctx;
242
243 if (geteuid() != 0)
244 return (void) log_tests_skipped("not root");
245
246 assert_se(mkstemp(path));
247
248 assert_se(oomd_system_context_acquire("/verylikelynonexistentpath", &ctx) == -ENOENT);
249
250 assert_se(oomd_system_context_acquire(path, &ctx) == 0);
251 assert_se(ctx.swap_total == 0);
252 assert_se(ctx.swap_used == 0);
253
254 assert_se(write_string_file(path, "some\nwords\nacross\nmultiple\nlines", WRITE_STRING_FILE_CREATE) == 0);
255 assert_se(oomd_system_context_acquire(path, &ctx) == 0);
256 assert_se(ctx.swap_total == 0);
257 assert_se(ctx.swap_used == 0);
258
259 assert_se(write_string_file(path, "Filename Type Size Used Priority", WRITE_STRING_FILE_CREATE) == 0);
260 assert_se(oomd_system_context_acquire(path, &ctx) == 0);
261 assert_se(ctx.swap_total == 0);
262 assert_se(ctx.swap_used == 0);
263
264 assert_se(write_string_file(path, "Filename Type Size Used Priority\n"
265 "/swapvol/swapfile file 18971644 0 -3\n"
266 "/dev/vda2 partition 1999868 993780 -2", WRITE_STRING_FILE_CREATE) == 0);
267 assert_se(oomd_system_context_acquire(path, &ctx) == 0);
268 assert_se(ctx.swap_total == 21474828288);
269 assert_se(ctx.swap_used == 1017630720);
270 }
271
272 static void test_oomd_pressure_above(void) {
273 _cleanup_hashmap_free_ Hashmap *h1 = NULL, *h2 = NULL;
274 _cleanup_set_free_ Set *t1 = NULL, *t2 = NULL, *t3 = NULL;
275 OomdCGroupContext ctx[2], *c;
276 loadavg_t threshold;
277
278 assert_se(store_loadavg_fixed_point(80, 0, &threshold) == 0);
279
280 /* /herp.slice */
281 assert_se(store_loadavg_fixed_point(99, 99, &(ctx[0].memory_pressure.avg10)) == 0);
282 assert_se(store_loadavg_fixed_point(99, 99, &(ctx[0].memory_pressure.avg60)) == 0);
283 assert_se(store_loadavg_fixed_point(99, 99, &(ctx[0].memory_pressure.avg300)) == 0);
284 ctx[0].mem_pressure_limit = threshold;
285
286 /* /derp.slice */
287 assert_se(store_loadavg_fixed_point(1, 11, &(ctx[1].memory_pressure.avg10)) == 0);
288 assert_se(store_loadavg_fixed_point(1, 11, &(ctx[1].memory_pressure.avg60)) == 0);
289 assert_se(store_loadavg_fixed_point(1, 11, &(ctx[1].memory_pressure.avg300)) == 0);
290 ctx[1].mem_pressure_limit = threshold;
291
292
293 /* High memory pressure */
294 assert_se(h1 = hashmap_new(&string_hash_ops));
295 assert_se(hashmap_put(h1, "/herp.slice", &ctx[0]) >= 0);
296 assert_se(oomd_pressure_above(h1, 0 /* duration */, &t1) == 1);
297 assert_se(set_contains(t1, &ctx[0]) == true);
298 assert_se(c = hashmap_get(h1, "/herp.slice"));
299 assert_se(c->mem_pressure_limit_hit_start > 0);
300
301 /* Low memory pressure */
302 assert_se(h2 = hashmap_new(&string_hash_ops));
303 assert_se(hashmap_put(h2, "/derp.slice", &ctx[1]) >= 0);
304 assert_se(oomd_pressure_above(h2, 0 /* duration */, &t2) == 0);
305 assert_se(t2 == NULL);
306 assert_se(c = hashmap_get(h2, "/derp.slice"));
307 assert_se(c->mem_pressure_limit_hit_start == 0);
308
309 /* High memory pressure w/ multiple cgroups */
310 assert_se(hashmap_put(h1, "/derp.slice", &ctx[1]) >= 0);
311 assert_se(oomd_pressure_above(h1, 0 /* duration */, &t3) == 1);
312 assert_se(set_contains(t3, &ctx[0]) == true);
313 assert_se(set_size(t3) == 1);
314 assert_se(c = hashmap_get(h1, "/herp.slice"));
315 assert_se(c->mem_pressure_limit_hit_start > 0);
316 assert_se(c = hashmap_get(h1, "/derp.slice"));
317 assert_se(c->mem_pressure_limit_hit_start == 0);
318 }
319
320 static void test_oomd_swap_free_below(void) {
321 OomdSystemContext ctx = (OomdSystemContext) {
322 .swap_total = 20971512 * 1024U,
323 .swap_used = 20971440 * 1024U,
324 };
325 assert_se(oomd_swap_free_below(&ctx, 2000) == true);
326
327 ctx = (OomdSystemContext) {
328 .swap_total = 20971512 * 1024U,
329 .swap_used = 3310136 * 1024U,
330 };
331 assert_se(oomd_swap_free_below(&ctx, 2000) == false);
332
333 ctx = (OomdSystemContext) {
334 .swap_total = 0,
335 .swap_used = 0,
336 };
337 assert_se(oomd_swap_free_below(&ctx, 2000) == false);
338 }
339
340 static void test_oomd_sort_cgroups(void) {
341 _cleanup_hashmap_free_ Hashmap *h = NULL;
342 _cleanup_free_ OomdCGroupContext **sorted_cgroups;
343 char **paths = STRV_MAKE("/herp.slice",
344 "/herp.slice/derp.scope",
345 "/herp.slice/derp.scope/sheep.service",
346 "/zupa.slice",
347 "/boop.slice",
348 "/omitted.slice",
349 "/avoid.slice");
350
351 OomdCGroupContext ctx[7] = {
352 { .path = paths[0],
353 .swap_usage = 20,
354 .last_pgscan = 0,
355 .pgscan = 33,
356 .current_memory_usage = 10 },
357 { .path = paths[1],
358 .swap_usage = 60,
359 .last_pgscan = 33,
360 .pgscan = 1,
361 .current_memory_usage = 20 },
362 { .path = paths[2],
363 .swap_usage = 40,
364 .last_pgscan = 1,
365 .pgscan = 33,
366 .current_memory_usage = 40 },
367 { .path = paths[3],
368 .swap_usage = 10,
369 .last_pgscan = 33,
370 .pgscan = 2,
371 .current_memory_usage = 10 },
372 { .path = paths[4],
373 .swap_usage = 11,
374 .last_pgscan = 33,
375 .pgscan = 33,
376 .current_memory_usage = 10 },
377 { .path = paths[5],
378 .swap_usage = 90,
379 .last_pgscan = 0,
380 .pgscan = UINT64_MAX,
381 .preference = MANAGED_OOM_PREFERENCE_OMIT },
382 { .path = paths[6],
383 .swap_usage = 99,
384 .last_pgscan = 0,
385 .pgscan = UINT64_MAX,
386 .preference = MANAGED_OOM_PREFERENCE_AVOID },
387 };
388
389 assert_se(h = hashmap_new(&string_hash_ops));
390
391 assert_se(hashmap_put(h, "/herp.slice", &ctx[0]) >= 0);
392 assert_se(hashmap_put(h, "/herp.slice/derp.scope", &ctx[1]) >= 0);
393 assert_se(hashmap_put(h, "/herp.slice/derp.scope/sheep.service", &ctx[2]) >= 0);
394 assert_se(hashmap_put(h, "/zupa.slice", &ctx[3]) >= 0);
395 assert_se(hashmap_put(h, "/boop.slice", &ctx[4]) >= 0);
396 assert_se(hashmap_put(h, "/omitted.slice", &ctx[5]) >= 0);
397 assert_se(hashmap_put(h, "/avoid.slice", &ctx[6]) >= 0);
398
399 assert_se(oomd_sort_cgroup_contexts(h, compare_swap_usage, NULL, &sorted_cgroups) == 6);
400 assert_se(sorted_cgroups[0] == &ctx[1]);
401 assert_se(sorted_cgroups[1] == &ctx[2]);
402 assert_se(sorted_cgroups[2] == &ctx[0]);
403 assert_se(sorted_cgroups[3] == &ctx[4]);
404 assert_se(sorted_cgroups[4] == &ctx[3]);
405 assert_se(sorted_cgroups[5] == &ctx[6]);
406 sorted_cgroups = mfree(sorted_cgroups);
407
408 assert_se(oomd_sort_cgroup_contexts(h, compare_pgscan_rate_and_memory_usage, NULL, &sorted_cgroups) == 6);
409 assert_se(sorted_cgroups[0] == &ctx[0]);
410 assert_se(sorted_cgroups[1] == &ctx[2]);
411 assert_se(sorted_cgroups[2] == &ctx[3]);
412 assert_se(sorted_cgroups[3] == &ctx[1]);
413 assert_se(sorted_cgroups[4] == &ctx[4]);
414 assert_se(sorted_cgroups[5] == &ctx[6]);
415 sorted_cgroups = mfree(sorted_cgroups);
416
417 assert_se(oomd_sort_cgroup_contexts(h, compare_pgscan_rate_and_memory_usage, "/herp.slice/derp.scope", &sorted_cgroups) == 2);
418 assert_se(sorted_cgroups[0] == &ctx[2]);
419 assert_se(sorted_cgroups[1] == &ctx[1]);
420 assert_se(sorted_cgroups[2] == 0);
421 assert_se(sorted_cgroups[3] == 0);
422 assert_se(sorted_cgroups[4] == 0);
423 assert_se(sorted_cgroups[5] == 0);
424 assert_se(sorted_cgroups[6] == 0);
425 sorted_cgroups = mfree(sorted_cgroups);
426 }
427
428 int main(void) {
429 int r;
430
431 test_setup_logging(LOG_DEBUG);
432
433 test_oomd_update_cgroup_contexts_between_hashmaps();
434 test_oomd_system_context_acquire();
435 test_oomd_pressure_above();
436 test_oomd_swap_free_below();
437 test_oomd_sort_cgroups();
438
439 /* The following tests operate on live cgroups */
440
441 r = enter_cgroup_root(NULL);
442 if (r < 0)
443 return log_tests_skipped_errno(r, "failed to enter a test cgroup scope");
444
445 test_oomd_cgroup_kill();
446 test_oomd_cgroup_context_acquire_and_insert();
447
448 return 0;
449 }