Debian Codegraph @

src/oom/test-oomd-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdlib.h>
4 #include <sys/socket.h>
5 #include <unistd.h>
6
7 #include "sd-bus.h"
8
9 #include "alloc-util.h"
10 #include "cgroup-setup.h"
11 #include "cgroup-util.h"
12 #include "errno-util.h"
13 #include "fd-util.h"
14 #include "fileio.h"
15 #include "oomd-manager.h"
16 #include "oomd-util.h"
17 #include "parse-util.h"
18 #include "path-util.h"
19 #include "pidref.h"
20 #include "process-util.h"
21 #include "set.h"
22 #include "string-util.h"
23 #include "tests.h"
24 #include "time-util.h"
25 #include "tmpfile-util.h"
26
27 static char *cgroup = NULL;
28
29 STATIC_DESTRUCTOR_REGISTER(cgroup, freep);
30
31 static int enter_cgroup_root_cached(void) {
32 static int saved_result = 0; /* 0: not entered, 1: success, negative errno: error */
33 int r;
34
35 if (saved_result != 0)
36 return saved_result;
37
38 r = enter_cgroup_root(&cgroup);
39 if (r < 0) {
40 log_tests_skipped_errno(r, "Failed to enter a test cgroup scope");
41 saved_result = r;
42 } else
43 saved_result = 1;
44
45 return saved_result;
46 }
47
48 static int fork_and_sleep(unsigned sleep_min, PidRef *ret) {
49 int r;
50
51 r = pidref_safe_fork("(test-oom-child)", FORK_LOG|FORK_DEATHSIG_SIGKILL, ret);
52 if (r == 0) {
53 usec_t timeout = usec_add(now(CLOCK_MONOTONIC), sleep_min * USEC_PER_MINUTE);
54 for (;;) {
55 usec_t n = now(CLOCK_MONOTONIC);
56 if (timeout < n) {
57 log_error("Child timed out waiting to be killed");
58 abort();
59 }
60 sleep(1);
61 }
62 }
63
64 return r;
65 }
66
67 static int setup_local_oomd_bus(sd_bus **ret_server, sd_bus **ret_client) {
68 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *server = NULL, *client = NULL;
69 _cleanup_close_pair_ int pair[2] = EBADF_PAIR;
70 int r;
71
72 assert(ret_server);
73 assert(ret_client);
74
75 r = socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0, pair);
76 if (r < 0)
77 return -errno;
78
79 r = sd_bus_new(&server);
80 if (r < 0)
81 return r;
82
83 r = sd_bus_set_fd(server, pair[0], pair[0]);
84 if (r < 0)
85 return r;
86 pair[0] = -EBADF;
87
88 r = sd_bus_set_server(server, true, SD_ID128_NULL);
89 if (r < 0)
90 return r;
91
92 r = sd_bus_set_anonymous(server, true);
93 if (r < 0)
94 return r;
95
96 r = sd_bus_start(server);
97 if (r < 0)
98 return r;
99
100 r = sd_bus_new(&client);
101 if (r < 0)
102 return r;
103
104 r = sd_bus_set_fd(client, pair[1], pair[1]);
105 if (r < 0)
106 return r;
107 pair[1] = -EBADF;
108
109 r = sd_bus_set_anonymous(client, true);
110 if (r < 0)
111 return r;
112
113 r = sd_bus_start(client);
114 if (r < 0)
115 return r;
116
117 *ret_server = TAKE_PTR(server);
118 *ret_client = TAKE_PTR(client);
119 return 0;
120 }
121
122 static int wait_for_killed_signal(sd_bus *server, sd_bus *client, const char *cgroup_path, const char *reason) {
123 int r;
124
125 assert(server);
126 assert(client);
127 assert(cgroup_path);
128 assert(reason);
129
130 for (size_t i = 0; i < 200; i++) {
131 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m_client = NULL;
132
133 r = sd_bus_process(server, NULL);
134 if (r < 0)
135 return r;
136
137 r = sd_bus_process(client, &m_client);
138 if (r < 0)
139 return r;
140
141 if (m_client && sd_bus_message_is_signal(m_client, "org.freedesktop.oom1.Manager", "Killed")) {
142 const char *got_cgroup, *got_reason;
143
144 r = sd_bus_message_read(m_client, "ss", &got_cgroup, &got_reason);
145 if (r < 0)
146 return r;
147
148 if (!streq(got_cgroup, cgroup_path))
149 return -ENOMSG;
150 if (!streq(got_reason, reason))
151 return -ENOMSG;
152
153 return 0;
154 }
155
156 r = sd_bus_wait(client, 50 * USEC_PER_MSEC);
157 if (r < 0)
158 return r;
159 }
160
161 return -ETIMEDOUT;
162 }
163
164 TEST(oomd_cgroup_kill) {
165 _cleanup_free_ char *subcgroup = NULL;
166 int r;
167
168 if (enter_cgroup_root_cached() < 0)
169 return;
170
171 /* Create another cgroup below this one for the pids we forked off. We need this to be managed
172 * by the test so that pid1 doesn't delete it before we can read the xattrs. */
173 ASSERT_NOT_NULL(subcgroup = path_join(cgroup, "oomdkilltest"));
174 /* Always start clean, in case of repeated runs and failures */
175 ASSERT_OK(cg_trim(subcgroup, /* delete_root */ true));
176 ASSERT_OK(cg_create(subcgroup));
177
178 /* If we don't have permissions to set xattrs we're likely in a userns or missing capabilities */
179 r = cg_set_xattr(subcgroup, "user.oomd_test", "test", 4, 0);
180 if (ERRNO_IS_PRIVILEGE(r) || ERRNO_IS_NOT_SUPPORTED(r))
181 return (void) log_tests_skipped("Cannot set user xattrs");
182
183 /* Do this twice to also check the increment behavior on the xattrs */
184 for (size_t i = 0; i < 2; i++) {
185 _cleanup_free_ char *v = NULL;
186 _cleanup_(pidref_done) PidRef one = PIDREF_NULL, two = PIDREF_NULL;
187
188 ASSERT_OK(fork_and_sleep(5, &one));
189 ASSERT_OK(cg_attach(subcgroup, one.pid));
190 ASSERT_OK(fork_and_sleep(5, &two));
191 ASSERT_OK(cg_attach(subcgroup, two.pid));
192
193 ASSERT_OK_POSITIVE(oomd_cgroup_kill(
194 /* m= */ NULL,
195 &(OomdCGroupContext){ .path = subcgroup },
196 /* recurse= */ false,
197 /* reason= */ NULL));
198
199 ASSERT_OK(cg_get_xattr(subcgroup, "user.oomd_ooms", &v, /* ret_size= */ NULL));
200 ASSERT_STREQ(v, i == 0 ? "1" : "2");
201 v = mfree(v);
202
203 /* Wait a bit since processes may take some time to be cleaned up. */
204 bool empty = false;
205 for (size_t t = 0; t < 100; t++) {
206 usleep_safe(100 * USEC_PER_MSEC);
207 ASSERT_OK(r = cg_is_empty(subcgroup));
208 if (r > 0) {
209 empty = true;
210 break;
211 }
212 }
213 ASSERT_TRUE(empty);
214
215 ASSERT_OK(cg_get_xattr(subcgroup, "user.oomd_kill", &v, /* ret_size= */ NULL));
216 ASSERT_STREQ(v, i == 0 ? "2" : "4");
217 }
218
219 ASSERT_OK(cg_trim(subcgroup, /* delete_root */ true));
220 }
221
222 TEST(oomd_cgroup_kill_signal_reason) {
223 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *server = NULL, *client = NULL;
224 _cleanup_free_ char *subcgroup = NULL;
225 static const char *reasons[] = {
226 "memory-used",
227 "memory-pressure",
228 };
229 Manager m = {};
230 int r;
231
232 r = enter_cgroup_root_cached();
233 if (r < 0)
234 return (void) log_tests_skipped_errno(r, "Failed to enter cgroup root");
235
236 ASSERT_NOT_NULL(subcgroup = path_join(cgroup, "oomdkillsignaltest"));
237 ASSERT_OK(cg_trim(subcgroup, /* delete_root= */ true));
238 ASSERT_OK(cg_create(subcgroup));
239
240 ASSERT_OK(setup_local_oomd_bus(&server, &client));
241 m.bus = server;
242
243 for (size_t i = 0; i < ELEMENTSOF(reasons); i++) {
244 _cleanup_(pidref_done) PidRef one = PIDREF_NULL, two = PIDREF_NULL;
245 const char *reason = reasons[i];
246
247 ASSERT_OK(fork_and_sleep(5, &one));
248 ASSERT_OK(cg_attach(subcgroup, one.pid));
249 ASSERT_OK(fork_and_sleep(5, &two));
250 ASSERT_OK(cg_attach(subcgroup, two.pid));
251
252 ASSERT_OK_POSITIVE(oomd_cgroup_kill(&m, &(OomdCGroupContext) { .path = subcgroup }, /* recurse= */ false, reason));
253 ASSERT_OK(wait_for_killed_signal(server, client, subcgroup, reason));
254
255 /* Cleanup isn't instantaneous, so give it some grace */
256 bool empty = false;
257 for (size_t t = 0; t < 100; t++) {
258 usleep_safe(100 * USEC_PER_MSEC);
259 if (ASSERT_OK(cg_is_empty(subcgroup)) > 0) {
260 empty = true;
261 break;
262 }
263 }
264 ASSERT_TRUE(empty);
265 }
266
267 ASSERT_OK(cg_trim(subcgroup, /* delete_root= */ true));
268 }
269
270 TEST(oomd_cgroup_context_acquire_and_insert) {
271 _cleanup_hashmap_free_ Hashmap *h1 = NULL, *h2 = NULL;
272 _cleanup_(oomd_cgroup_context_unrefp) OomdCGroupContext *ctx = NULL;
273 OomdCGroupContext *c1, *c2;
274 CGroupMask mask;
275
276 if (!is_pressure_supported())
277 return (void) log_tests_skipped("system does not support pressure");
278
279 if (enter_cgroup_root_cached() < 0)
280 return;
281
282 ASSERT_OK(cg_mask_supported(&mask));
283 if (!FLAGS_SET(mask, CGROUP_MASK_MEMORY))
284 return (void) log_tests_skipped("cgroup memory controller is not available");
285
286 ASSERT_OK(oomd_cgroup_context_acquire(cgroup, &ctx));
287
288 ASSERT_STREQ(ctx->path, cgroup);
289 ASSERT_GT(ctx->current_memory_usage, 0u);
290 ASSERT_EQ(ctx->memory_min, 0u);
291 ASSERT_EQ(ctx->memory_low, 0u);
292 ASSERT_EQ(ctx->swap_usage, 0u);
293 ASSERT_EQ(ctx->last_pgscan, 0u);
294 ASSERT_LT(ctx->pgscan, 10u);
295 ASSERT_NULL(ctx = oomd_cgroup_context_unref(ctx));
296
297 ASSERT_OK(oomd_cgroup_context_acquire("", &ctx));
298 ASSERT_STREQ(ctx->path, "/");
299 ASSERT_GT(ctx->current_memory_usage, 0u);
300
301 /* Test hashmap inserts */
302 ASSERT_NOT_NULL(h1 = hashmap_new(&oomd_cgroup_ctx_hash_ops));
303 ASSERT_OK(oomd_insert_cgroup_context(NULL, h1, cgroup));
304 ASSERT_NOT_NULL(c1 = hashmap_get(h1, cgroup));
305 ASSERT_ERROR(oomd_insert_cgroup_context(NULL, h1, cgroup), EEXIST);
306
307 /* make sure certain values from h1 get updated in h2 */
308 c1->pgscan = UINT64_MAX;
309 c1->mem_pressure_limit = 6789;
310 c1->mem_pressure_limit_hit_start = 42;
311 c1->mem_pressure_duration_usec = 1234;
312 ASSERT_NOT_NULL(h2 = hashmap_new(&oomd_cgroup_ctx_hash_ops));
313 ASSERT_OK(oomd_insert_cgroup_context(h1, h2, cgroup));
314 ASSERT_NOT_NULL(c1 = hashmap_get(h1, cgroup));
315 ASSERT_NOT_NULL(c2 = hashmap_get(h2, cgroup));
316 ASSERT_TRUE(c1 != c2);
317 ASSERT_EQ(c2->last_pgscan, UINT64_MAX);
318 ASSERT_EQ(c2->mem_pressure_limit, 6789u);
319 ASSERT_EQ(c2->mem_pressure_limit_hit_start, 42u);
320 ASSERT_EQ(c2->mem_pressure_duration_usec, 1234u);
321 }
322
323 TEST(oomd_update_cgroup_contexts_between_hashmaps) {
324 _cleanup_hashmap_free_ Hashmap *h_old = NULL, *h_new = NULL;
325 OomdCGroupContext *c_old, *c_new;
326 char **paths = STRV_MAKE("/0.slice",
327 "/1.slice");
328
329 OomdCGroupContext ctx_old[2] = {
330 { .path = paths[0],
331 .mem_pressure_limit = 5,
332 .mem_pressure_limit_hit_start = 777,
333 .mem_pressure_duration_usec = 111,
334 .last_had_mem_reclaim = 888,
335 .pgscan = 57 },
336 { .path = paths[1],
337 .mem_pressure_limit = 6,
338 .mem_pressure_limit_hit_start = 888,
339 .mem_pressure_duration_usec = 222,
340 .last_had_mem_reclaim = 888,
341 .pgscan = 42 },
342 };
343
344 OomdCGroupContext ctx_new[2] = {
345 { .path = paths[0],
346 .pgscan = 57 },
347 { .path = paths[1],
348 .pgscan = 101 },
349 };
350
351 ASSERT_NOT_NULL(h_old = hashmap_new(&string_hash_ops));
352 ASSERT_OK(hashmap_put(h_old, paths[0], &ctx_old[0]));
353 ASSERT_OK(hashmap_put(h_old, paths[1], &ctx_old[1]));
354
355 ASSERT_NOT_NULL(h_new = hashmap_new(&string_hash_ops));
356 ASSERT_OK(hashmap_put(h_new, paths[0], &ctx_new[0]));
357 ASSERT_OK(hashmap_put(h_new, paths[1], &ctx_new[1]));
358
359 oomd_update_cgroup_contexts_between_hashmaps(h_old, h_new);
360
361 ASSERT_NOT_NULL(c_old = hashmap_get(h_old, "/0.slice"));
362 ASSERT_NOT_NULL(c_new = hashmap_get(h_new, "/0.slice"));
363 ASSERT_EQ(c_old->pgscan, c_new->last_pgscan);
364 ASSERT_EQ(c_old->mem_pressure_limit, c_new->mem_pressure_limit);
365 ASSERT_EQ(c_old->mem_pressure_limit_hit_start, c_new->mem_pressure_limit_hit_start);
366 ASSERT_EQ(c_old->mem_pressure_duration_usec, c_new->mem_pressure_duration_usec);
367 ASSERT_EQ(c_old->last_had_mem_reclaim, c_new->last_had_mem_reclaim);
368
369 ASSERT_NOT_NULL(c_old = hashmap_get(h_old, "/1.slice"));
370 ASSERT_NOT_NULL(c_new = hashmap_get(h_new, "/1.slice"));
371 ASSERT_EQ(c_old->pgscan, c_new->last_pgscan);
372 ASSERT_EQ(c_old->mem_pressure_limit, c_new->mem_pressure_limit);
373 ASSERT_EQ(c_old->mem_pressure_limit_hit_start, c_new->mem_pressure_limit_hit_start);
374 ASSERT_EQ(c_old->mem_pressure_duration_usec, c_new->mem_pressure_duration_usec);
375 ASSERT_LT(c_old->last_had_mem_reclaim, c_new->last_had_mem_reclaim);
376 }
377
378 TEST(oomd_system_context_acquire) {
379 _cleanup_(unlink_tempfilep) char path[] = "/tmp/oomdgetsysctxtestXXXXXX";
380 _cleanup_close_ int fd = -EBADF;
381 OomdSystemContext ctx;
382
383 ASSERT_OK(fd = mkostemp_safe(path));
384
385 ASSERT_ERROR(oomd_system_context_acquire("/verylikelynonexistentpath", &ctx), ENOENT);
386
387 ASSERT_ERROR(oomd_system_context_acquire(path, &ctx), EINVAL);
388
389 ASSERT_OK(write_string_file(path, "some\nwords\nacross\nmultiple\nlines", WRITE_STRING_FILE_CREATE));
390 ASSERT_ERROR(oomd_system_context_acquire(path, &ctx), EINVAL);
391
392 ASSERT_OK(write_string_file(path, "MemTotal: 32495256 kB trailing\n"
393 "MemFree: 9880512 kB data\n"
394 "SwapTotal: 8388604 kB is\n"
395 "SwapFree: 7604 kB bad\n",
396 WRITE_STRING_FILE_CREATE));
397 ASSERT_ERROR(oomd_system_context_acquire(path, &ctx), EINVAL);
398
399 ASSERT_OK(write_string_file(path, "MemTotal: 32495256 kB\n"
400 "MemFree: 9880512 kB\n"
401 "MemAvailable: 21777088 kB\n"
402 "Buffers: 5968 kB\n"
403 "Cached: 14344796 kB\n"
404 "Unevictable: 740004 kB\n"
405 "Mlocked: 4484 kB\n"
406 "SwapTotal: 8388604 kB\n"
407 "SwapFree: 7604 kB\n",
408 WRITE_STRING_FILE_CREATE));
409 ASSERT_OK(oomd_system_context_acquire(path, &ctx));
410 ASSERT_EQ(ctx.mem_total, 33275142144u);
411 ASSERT_EQ(ctx.mem_used, 10975404032u);
412 ASSERT_EQ(ctx.swap_total, 8589930496u);
413 ASSERT_EQ(ctx.swap_used, 8582144000u);
414 }
415
416 TEST(oomd_pressure_above) {
417 _cleanup_hashmap_free_ Hashmap *h1 = NULL, *h2 = NULL;
418 _cleanup_set_free_ Set *t1 = NULL, *t2 = NULL, *t3 = NULL;
419 OomdCGroupContext ctx[2] = {}, *c;
420 loadavg_t threshold;
421
422 ASSERT_OK(store_loadavg_fixed_point(80, 0, &threshold));
423
424 /* /herp.slice */
425 ASSERT_OK(store_loadavg_fixed_point(99, 99, &(ctx[0].memory_pressure.avg10)));
426 ASSERT_OK(store_loadavg_fixed_point(99, 99, &(ctx[0].memory_pressure.avg60)));
427 ASSERT_OK(store_loadavg_fixed_point(99, 99, &(ctx[0].memory_pressure.avg300)));
428 ctx[0].mem_pressure_limit = threshold;
429 /* Set memory pressure duration to 0 since we use the real system monotonic clock
430 * in oomd_pressure_above() and we want to avoid this test depending on timing. */
431 ctx[0].mem_pressure_duration_usec = 0;
432
433 /* /derp.slice */
434 ASSERT_OK(store_loadavg_fixed_point(1, 11, &(ctx[1].memory_pressure.avg10)));
435 ASSERT_OK(store_loadavg_fixed_point(1, 11, &(ctx[1].memory_pressure.avg60)));
436 ASSERT_OK(store_loadavg_fixed_point(1, 11, &(ctx[1].memory_pressure.avg300)));
437 ctx[1].mem_pressure_limit = threshold;
438 ctx[1].mem_pressure_duration_usec = 0;
439
440 /* High memory pressure */
441 ASSERT_NOT_NULL(h1 = hashmap_new(&string_hash_ops));
442 ASSERT_OK(hashmap_put(h1, "/herp.slice", &ctx[0]));
443 ASSERT_OK_POSITIVE(oomd_pressure_above(h1, &t1));
444 ASSERT_TRUE(set_contains(t1, &ctx[0]));
445 ASSERT_NOT_NULL(c = hashmap_get(h1, "/herp.slice"));
446 ASSERT_GT(c->mem_pressure_limit_hit_start, 0u);
447
448 /* Low memory pressure */
449 ASSERT_NOT_NULL(h2 = hashmap_new(&string_hash_ops));
450 ASSERT_OK(hashmap_put(h2, "/derp.slice", &ctx[1]));
451 ASSERT_OK_ZERO(oomd_pressure_above(h2, &t2));
452 ASSERT_NULL(t2);
453 ASSERT_NOT_NULL(c = hashmap_get(h2, "/derp.slice"));
454 ASSERT_EQ(c->mem_pressure_limit_hit_start, 0u);
455
456 /* High memory pressure w/ multiple cgroups */
457 ASSERT_OK(hashmap_put(h1, "/derp.slice", &ctx[1]));
458 ASSERT_OK_POSITIVE(oomd_pressure_above(h1, &t3));
459 ASSERT_TRUE(set_contains(t3, &ctx[0]));
460 ASSERT_EQ(set_size(t3), 1u);
461 ASSERT_NOT_NULL(c = hashmap_get(h1, "/herp.slice"));
462 ASSERT_GT(c->mem_pressure_limit_hit_start, 0u);
463 ASSERT_NOT_NULL(c = hashmap_get(h1, "/derp.slice"));
464 ASSERT_EQ(c->mem_pressure_limit_hit_start, 0u);
465 }
466
467 TEST(oomd_mem_and_swap_free_below) {
468 OomdSystemContext ctx = (OomdSystemContext) {
469 .mem_total = UINT64_C(20971512) * 1024U,
470 .mem_used = UINT64_C(3310136) * 1024U,
471 .swap_total = UINT64_C(20971512) * 1024U,
472 .swap_used = UINT64_C(20971440) * 1024U,
473 };
474 ASSERT_FALSE(oomd_mem_available_below(&ctx, 2000));
475 ASSERT_TRUE(oomd_swap_free_below(&ctx, 2000));
476
477 ctx = (OomdSystemContext) {
478 .mem_total = UINT64_C(20971512) * 1024U,
479 .mem_used = UINT64_C(20971440) * 1024U,
480 .swap_total = UINT64_C(20971512) * 1024U,
481 .swap_used = UINT64_C(3310136) * 1024U,
482 };
483 ASSERT_TRUE(oomd_mem_available_below(&ctx, 2000));
484 ASSERT_FALSE(oomd_swap_free_below(&ctx, 2000));
485
486 ctx = (OomdSystemContext) {
487 .mem_total = 0,
488 .mem_used = 0,
489 .swap_total = 0,
490 .swap_used = 0,
491 };
492 ASSERT_FALSE(oomd_mem_available_below(&ctx, 2000));
493 ASSERT_FALSE(oomd_swap_free_below(&ctx, 2000));
494 }
495
496 TEST(oomd_sort_cgroups) {
497 _cleanup_hashmap_free_ Hashmap *h = NULL;
498 _cleanup_free_ OomdCGroupContext **sorted_cgroups = NULL;
499 char **paths = STRV_MAKE("/herp.slice",
500 "/herp.slice/derp.scope",
501 "/herp.slice/derp.scope/sheep.service",
502 "/zupa.slice",
503 "/boop.slice",
504 "/omitted.slice",
505 "/avoid.slice");
506
507 OomdCGroupContext ctx[7] = {
508 { .path = paths[0],
509 .swap_usage = 20,
510 .last_pgscan = 0,
511 .pgscan = 33,
512 .current_memory_usage = 10 },
513 { .path = paths[1],
514 .swap_usage = 60,
515 .last_pgscan = 33,
516 .pgscan = 1,
517 .current_memory_usage = 20 },
518 { .path = paths[2],
519 .swap_usage = 40,
520 .last_pgscan = 1,
521 .pgscan = 33,
522 .current_memory_usage = 40 },
523 { .path = paths[3],
524 .swap_usage = 10,
525 .last_pgscan = 33,
526 .pgscan = 2,
527 .current_memory_usage = 10 },
528 { .path = paths[4],
529 .swap_usage = 11,
530 .last_pgscan = 33,
531 .pgscan = 33,
532 .current_memory_usage = 10 },
533 { .path = paths[5],
534 .swap_usage = 90,
535 .last_pgscan = 0,
536 .pgscan = UINT64_MAX,
537 .preference = MANAGED_OOM_PREFERENCE_OMIT },
538 { .path = paths[6],
539 .swap_usage = 99,
540 .last_pgscan = 0,
541 .pgscan = UINT64_MAX,
542 .preference = MANAGED_OOM_PREFERENCE_AVOID },
543 };
544
545 ASSERT_NOT_NULL(h = hashmap_new(&string_hash_ops));
546
547 ASSERT_OK(hashmap_put(h, "/herp.slice", &ctx[0]));
548 ASSERT_OK(hashmap_put(h, "/herp.slice/derp.scope", &ctx[1]));
549 ASSERT_OK(hashmap_put(h, "/herp.slice/derp.scope/sheep.service", &ctx[2]));
550 ASSERT_OK(hashmap_put(h, "/zupa.slice", &ctx[3]));
551 ASSERT_OK(hashmap_put(h, "/boop.slice", &ctx[4]));
552 ASSERT_OK(hashmap_put(h, "/omitted.slice", &ctx[5]));
553 ASSERT_OK(hashmap_put(h, "/avoid.slice", &ctx[6]));
554
555 ASSERT_OK_EQ(oomd_sort_cgroup_contexts(h, compare_swap_usage, NULL, &sorted_cgroups), 6);
556 ASSERT_PTR_EQ(sorted_cgroups[0], &ctx[1]);
557 ASSERT_PTR_EQ(sorted_cgroups[1], &ctx[2]);
558 ASSERT_PTR_EQ(sorted_cgroups[2], &ctx[0]);
559 ASSERT_PTR_EQ(sorted_cgroups[3], &ctx[4]);
560 ASSERT_PTR_EQ(sorted_cgroups[4], &ctx[3]);
561 ASSERT_PTR_EQ(sorted_cgroups[5], &ctx[6]);
562 sorted_cgroups = mfree(sorted_cgroups);
563
564 ASSERT_OK_EQ(oomd_sort_cgroup_contexts(h, compare_pgscan_rate_and_memory_usage, NULL, &sorted_cgroups), 6);
565 ASSERT_PTR_EQ(sorted_cgroups[0], &ctx[0]);
566 ASSERT_PTR_EQ(sorted_cgroups[1], &ctx[2]);
567 ASSERT_PTR_EQ(sorted_cgroups[2], &ctx[3]);
568 ASSERT_PTR_EQ(sorted_cgroups[3], &ctx[1]);
569 ASSERT_PTR_EQ(sorted_cgroups[4], &ctx[4]);
570 ASSERT_PTR_EQ(sorted_cgroups[5], &ctx[6]);
571 sorted_cgroups = mfree(sorted_cgroups);
572
573 ASSERT_OK_EQ(oomd_sort_cgroup_contexts(h, compare_pgscan_rate_and_memory_usage, "/herp.slice/derp.scope", &sorted_cgroups), 2);
574 ASSERT_PTR_EQ(sorted_cgroups[0], &ctx[2]);
575 ASSERT_PTR_EQ(sorted_cgroups[1], &ctx[1]);
576 ASSERT_NULL(sorted_cgroups[2]);
577 ASSERT_NULL(sorted_cgroups[3]);
578 ASSERT_NULL(sorted_cgroups[4]);
579 ASSERT_NULL(sorted_cgroups[5]);
580 ASSERT_NULL(sorted_cgroups[6]);
581 }
582
583 TEST(oomd_fetch_cgroup_oom_preference) {
584 _cleanup_(oomd_cgroup_context_unrefp) OomdCGroupContext *ctx = NULL;
585 ManagedOOMPreference root_pref;
586 CGroupMask mask;
587 bool test_xattrs;
588 int r;
589
590 if (!is_pressure_supported())
591 return (void) log_tests_skipped("system does not support pressure");
592
593 if (enter_cgroup_root_cached() < 0)
594 return;
595
596 ASSERT_OK(cg_mask_supported(&mask));
597 if (!FLAGS_SET(mask, CGROUP_MASK_MEMORY))
598 return (void) log_tests_skipped("cgroup memory controller is not available");
599
600 ASSERT_OK(oomd_cgroup_context_acquire(cgroup, &ctx));
601
602 /* If we don't have permissions to set xattrs we're likely in a userns or missing capabilities
603 * so skip the xattr portions of the test. */
604 r = cg_set_xattr(cgroup, "user.oomd_test", "1", 1, 0);
605 test_xattrs = !ERRNO_IS_PRIVILEGE(r) && !ERRNO_IS_NOT_SUPPORTED(r);
606
607 if (test_xattrs) {
608 ASSERT_OK(oomd_fetch_cgroup_oom_preference(ctx, NULL));
609 ASSERT_OK(cg_set_xattr(cgroup, "user.oomd_omit", "1", 1, 0));
610 ASSERT_OK(cg_set_xattr(cgroup, "user.oomd_avoid", "1", 1, 0));
611
612 /* omit takes precedence over avoid when both are set to true */
613 ASSERT_OK(oomd_fetch_cgroup_oom_preference(ctx, NULL));
614 ASSERT_EQ(ctx->preference, geteuid() == 0 ? MANAGED_OOM_PREFERENCE_OMIT : MANAGED_OOM_PREFERENCE_NONE);
615 } else {
616 ASSERT_FAIL(oomd_fetch_cgroup_oom_preference(ctx, NULL));
617 ASSERT_EQ(ctx->preference, MANAGED_OOM_PREFERENCE_NONE);
618 }
619 ctx = oomd_cgroup_context_unref(ctx);
620
621 /* also check when only avoid is set to true */
622 if (test_xattrs) {
623 ASSERT_OK(cg_set_xattr(cgroup, "user.oomd_omit", "0", 1, 0));
624 ASSERT_OK(cg_set_xattr(cgroup, "user.oomd_avoid", "1", 1, 0));
625 ASSERT_OK(oomd_cgroup_context_acquire(cgroup, &ctx));
626 ASSERT_OK(oomd_fetch_cgroup_oom_preference(ctx, NULL));
627 ASSERT_EQ(ctx->preference, geteuid() == 0 ? MANAGED_OOM_PREFERENCE_AVOID : MANAGED_OOM_PREFERENCE_NONE);
628 ctx = oomd_cgroup_context_unref(ctx);
629 }
630
631 /* Test the root cgroup */
632 /* Root cgroup is live and not made on demand like the cgroup the test runs in. It can have varying
633 * xattrs set already so let's read in the booleans first to get the final preference value. */
634 ASSERT_OK(oomd_cgroup_context_acquire("", &ctx));
635 root_pref = MANAGED_OOM_PREFERENCE_NONE;
636 if (cg_get_xattr_bool("", "user.oomd_avoid") > 0)
637 root_pref = MANAGED_OOM_PREFERENCE_AVOID;
638 if (cg_get_xattr_bool("", "user.oomd_omit") > 0)
639 root_pref = MANAGED_OOM_PREFERENCE_OMIT;
640 ASSERT_OK(oomd_fetch_cgroup_oom_preference(ctx, NULL));
641 ASSERT_EQ(ctx->preference, root_pref);
642
643 ASSERT_ERROR(oomd_fetch_cgroup_oom_preference(ctx, "/herp.slice/derp.scope"), EINVAL);
644
645 /* Assert that avoid/omit are not set if the cgroup and prefix are not
646 * owned by the same user. */
647 if (test_xattrs && !empty_or_root(cgroup) && geteuid() == 0) {
648 ctx = oomd_cgroup_context_unref(ctx);
649 ASSERT_OK(cg_set_access(cgroup, 61183, 0));
650 ASSERT_OK(oomd_cgroup_context_acquire(cgroup, &ctx));
651
652 ASSERT_OK(oomd_fetch_cgroup_oom_preference(ctx, NULL));
653 ASSERT_EQ(ctx->preference, MANAGED_OOM_PREFERENCE_NONE);
654
655 ASSERT_OK(oomd_fetch_cgroup_oom_preference(ctx, ctx->path));
656 ASSERT_EQ(ctx->preference, MANAGED_OOM_PREFERENCE_AVOID);
657 }
658 }
659
660 DEFINE_TEST_MAIN(LOG_DEBUG);