| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <pthread.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <sys/mman.h> |
| 7 |
#include <sys/stat.h> |
| 8 |
#include <unistd.h> |
| 9 |
|
| 10 |
#include "sd-bus.h" |
| 11 |
#include "sd-event.h" |
| 12 |
|
| 13 |
#include "bus-error.h" |
| 14 |
#include "bus-locator.h" |
| 15 |
#include "bus-wait-for-jobs.h" |
| 16 |
#include "cgroup-util.h" |
| 17 |
#include "event-util.h" |
| 18 |
#include "fd-util.h" |
| 19 |
#include "format-util.h" |
| 20 |
#include "hashmap.h" |
| 21 |
#include "path-util.h" |
| 22 |
#include "pidref.h" |
| 23 |
#include "process-util.h" |
| 24 |
#include "random-util.h" |
| 25 |
#include "rm-rf.h" |
| 26 |
#include "signal-util.h" |
| 27 |
#include "socket-util.h" |
| 28 |
#include "tests.h" |
| 29 |
#include "time-util.h" |
| 30 |
#include "tmpfile-util.h" |
| 31 |
#include "unit-def.h" |
| 32 |
|
| 33 |
/* Shared infrastructure for fake pressure tests */ |
| 34 |
|
| 35 |
static int event_loop_with_timeout(sd_event *event) { |
| 36 |
ASSERT_NOT_NULL(event); |
| 37 |
|
| 38 |
/* Note, the default meson test timeout is 30 seconds. */ |
| 39 |
ASSERT_OK(sd_event_add_time_relative( |
| 40 |
event, |
| 41 |
/* ret= */ NULL, |
| 42 |
CLOCK_MONOTONIC, |
| 43 |
20 * USEC_PER_SEC, |
| 44 |
/* accuracy= */ 0, |
| 45 |
/* callback= */ NULL, |
| 46 |
INT_TO_PTR(-ETIMEDOUT))); |
| 47 |
|
| 48 |
if (ASSERT_OK_OR(sd_event_loop(event), -ETIMEDOUT) < 0) |
| 49 |
return log_tests_skipped("event loop timeout"); |
| 50 |
|
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
struct fake_pressure_context { |
| 55 |
int fifo_fd; |
| 56 |
int socket_fd; |
| 57 |
}; |
| 58 |
|
| 59 |
static void *fake_pressure_thread(void *p) { |
| 60 |
_cleanup_free_ struct fake_pressure_context *c = ASSERT_PTR(p); |
| 61 |
_cleanup_close_ int cfd = -EBADF; |
| 62 |
|
| 63 |
usleep_safe(150); |
| 64 |
|
| 65 |
ASSERT_EQ(write(c->fifo_fd, &(const char) { 'x' }, 1), 1); |
| 66 |
|
| 67 |
usleep_safe(150); |
| 68 |
|
| 69 |
cfd = accept4(c->socket_fd, NULL, NULL, SOCK_CLOEXEC); |
| 70 |
ASSERT_OK_ERRNO(cfd); |
| 71 |
char buf[STRLEN("hello")+1] = {}; |
| 72 |
ASSERT_EQ(read(cfd, buf, sizeof(buf)-1), (ssize_t) (sizeof(buf)-1)); |
| 73 |
ASSERT_STREQ(buf, "hello"); |
| 74 |
ASSERT_EQ(write(cfd, &(const char) { 'z' }, 1), 1); |
| 75 |
|
| 76 |
return NULL; |
| 77 |
} |
| 78 |
|
| 79 |
static int fake_pressure_callback(sd_event_source *s, void *userdata) { |
| 80 |
int *value = userdata; |
| 81 |
const char *d; |
| 82 |
|
| 83 |
ASSERT_NOT_NULL(s); |
| 84 |
ASSERT_OK(sd_event_source_get_description(s, &d)); |
| 85 |
|
| 86 |
*value *= d[0]; |
| 87 |
|
| 88 |
log_notice("pressure event: %s", d); |
| 89 |
|
| 90 |
if (*value == 7 * 'f' * 's') |
| 91 |
ASSERT_OK(sd_event_exit(sd_event_source_get_event(s), 0)); |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
typedef int (*event_add_pressure_t)(sd_event *, sd_event_source **, sd_event_handler_t, void *); |
| 97 |
|
| 98 |
static void test_fake_pressure( |
| 99 |
const char *resource, |
| 100 |
event_add_pressure_t add_pressure) { |
| 101 |
|
| 102 |
_cleanup_(sd_event_source_unrefp) sd_event_source *es = NULL, *ef = NULL; |
| 103 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 104 |
_cleanup_(rm_rf_physical_and_freep) char *tmp = NULL; |
| 105 |
_cleanup_close_ int fifo_fd = -EBADF, socket_fd = -EBADF; |
| 106 |
union sockaddr_union sa; |
| 107 |
pthread_t th; |
| 108 |
int value = 7; |
| 109 |
|
| 110 |
_cleanup_free_ char *resource_upper = ASSERT_NOT_NULL(strdup(resource)); |
| 111 |
ascii_strupper(resource_upper); |
| 112 |
|
| 113 |
_cleanup_free_ char *env_watch = ASSERT_NOT_NULL(strjoin(resource_upper, "_PRESSURE_WATCH")), |
| 114 |
*env_write = ASSERT_NOT_NULL(strjoin(resource_upper, "_PRESSURE_WRITE")); |
| 115 |
|
| 116 |
ASSERT_OK(sd_event_default(&e)); |
| 117 |
|
| 118 |
ASSERT_OK(mkdtemp_malloc(NULL, &tmp)); |
| 119 |
|
| 120 |
_cleanup_free_ char *j = ASSERT_NOT_NULL(path_join(tmp, "fifo")); |
| 121 |
ASSERT_OK_ERRNO(mkfifo(j, 0600)); |
| 122 |
fifo_fd = open(j, O_CLOEXEC|O_RDWR|O_NONBLOCK); |
| 123 |
ASSERT_OK_ERRNO(fifo_fd); |
| 124 |
|
| 125 |
_cleanup_free_ char *k = ASSERT_NOT_NULL(path_join(tmp, "sock")); |
| 126 |
socket_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); |
| 127 |
ASSERT_OK_ERRNO(socket_fd); |
| 128 |
ASSERT_OK(sockaddr_un_set_path(&sa.un, k)); |
| 129 |
ASSERT_OK_ERRNO(bind(socket_fd, &sa.sa, sockaddr_un_len(&sa.un))); |
| 130 |
ASSERT_OK_ERRNO(listen(socket_fd, 1)); |
| 131 |
|
| 132 |
/* Ideally we'd just allocate this on the stack, but AddressSanitizer doesn't like it if threads |
| 133 |
* access each other's stack */ |
| 134 |
struct fake_pressure_context *fp = new(struct fake_pressure_context, 1); |
| 135 |
ASSERT_NOT_NULL(fp); |
| 136 |
*fp = (struct fake_pressure_context) { |
| 137 |
.fifo_fd = fifo_fd, |
| 138 |
.socket_fd = socket_fd, |
| 139 |
}; |
| 140 |
|
| 141 |
ASSERT_EQ(pthread_create(&th, NULL, fake_pressure_thread, TAKE_PTR(fp)), 0); |
| 142 |
|
| 143 |
ASSERT_OK_ERRNO(setenv(env_watch, j, /* override= */ true)); |
| 144 |
ASSERT_OK_ERRNO(unsetenv(env_write)); |
| 145 |
|
| 146 |
ASSERT_OK(add_pressure(e, &es, fake_pressure_callback, &value)); |
| 147 |
ASSERT_OK(sd_event_source_set_description(es, "fifo event source")); |
| 148 |
|
| 149 |
ASSERT_OK_ERRNO(setenv(env_watch, k, /* override= */ true)); |
| 150 |
ASSERT_OK_ERRNO(setenv(env_write, "aGVsbG8K", /* override= */ true)); |
| 151 |
|
| 152 |
ASSERT_OK(add_pressure(e, &ef, fake_pressure_callback, &value)); |
| 153 |
ASSERT_OK(sd_event_source_set_description(ef, "socket event source")); |
| 154 |
|
| 155 |
if (event_loop_with_timeout(e) != 0) { |
| 156 |
(void) pthread_cancel(th); |
| 157 |
(void) pthread_join(th, /* retval= */ NULL); |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
ASSERT_EQ(value, 7 * 'f' * 's'); |
| 162 |
|
| 163 |
ASSERT_EQ(pthread_join(th, NULL), 0); |
| 164 |
} |
| 165 |
|
| 166 |
static int fake_pressure_wrapper(sd_event *e, sd_event_source **ret, sd_event_handler_t callback, void *userdata) { |
| 167 |
return sd_event_add_memory_pressure(e, ret, callback, userdata); |
| 168 |
} |
| 169 |
|
| 170 |
TEST(fake_memory_pressure) { |
| 171 |
test_fake_pressure("memory", fake_pressure_wrapper); |
| 172 |
} |
| 173 |
|
| 174 |
static int fake_cpu_pressure_wrapper(sd_event *e, sd_event_source **ret, sd_event_handler_t callback, void *userdata) { |
| 175 |
return sd_event_add_cpu_pressure(e, ret, callback, userdata); |
| 176 |
} |
| 177 |
|
| 178 |
TEST(fake_cpu_pressure) { |
| 179 |
test_fake_pressure("cpu", fake_cpu_pressure_wrapper); |
| 180 |
} |
| 181 |
|
| 182 |
static int fake_io_pressure_wrapper(sd_event *e, sd_event_source **ret, sd_event_handler_t callback, void *userdata) { |
| 183 |
return sd_event_add_io_pressure(e, ret, callback, userdata); |
| 184 |
} |
| 185 |
|
| 186 |
TEST(fake_io_pressure) { |
| 187 |
test_fake_pressure("io", fake_io_pressure_wrapper); |
| 188 |
} |
| 189 |
|
| 190 |
/* Shared infrastructure for real pressure tests */ |
| 191 |
|
| 192 |
static int controller_supported_on_unit(sd_bus *bus, const char *unit_name, const char *unit_path, CGroupMask controller) { |
| 193 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 194 |
_cleanup_free_ char *cg = NULL; |
| 195 |
CGroupMask mask; |
| 196 |
int r; |
| 197 |
|
| 198 |
r = sd_bus_get_property_string(bus, "org.freedesktop.systemd1", unit_path, |
| 199 |
unit_dbus_interface_from_name(unit_name), "ControlGroup", |
| 200 |
&error, &cg); |
| 201 |
if (r < 0) |
| 202 |
return log_notice_errno(r, "Failed to read ControlGroup property: %s", bus_error_message(&error, r)); |
| 203 |
|
| 204 |
if (isempty(cg)) |
| 205 |
return -ENODATA; |
| 206 |
|
| 207 |
r = cg_mask_supported_subtree(cg, &mask); |
| 208 |
if (r < 0) |
| 209 |
return log_notice_errno(r, "Failed to read supported controllers for %s: %m", cg); |
| 210 |
|
| 211 |
return FLAGS_SET(mask, controller); |
| 212 |
} |
| 213 |
|
| 214 |
struct real_pressure_context { |
| 215 |
sd_event_source *pid; |
| 216 |
}; |
| 217 |
|
| 218 |
static int real_pressure_child_callback(sd_event_source *s, const siginfo_t *si, void *userdata) { |
| 219 |
ASSERT_NOT_NULL(s); |
| 220 |
ASSERT_NOT_NULL(si); |
| 221 |
|
| 222 |
log_notice("child dead"); |
| 223 |
|
| 224 |
ASSERT_EQ(si->si_signo, SIGCHLD); |
| 225 |
ASSERT_EQ(si->si_status, SIGKILL); |
| 226 |
ASSERT_EQ(si->si_code, CLD_KILLED); |
| 227 |
|
| 228 |
ASSERT_OK(sd_event_exit(sd_event_source_get_event(s), 31)); |
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
/* Memory pressure real test */ |
| 233 |
|
| 234 |
static int real_memory_pressure_callback(sd_event_source *s, void *userdata) { |
| 235 |
struct real_pressure_context *c = ASSERT_PTR(userdata); |
| 236 |
const char *d; |
| 237 |
|
| 238 |
ASSERT_NOT_NULL(s); |
| 239 |
ASSERT_OK(sd_event_source_get_description(s, &d)); |
| 240 |
|
| 241 |
log_notice("real memory pressure event: %s", d); |
| 242 |
|
| 243 |
sd_event_trim_memory(); |
| 244 |
|
| 245 |
ASSERT_NOT_NULL(c->pid); |
| 246 |
ASSERT_OK(sd_event_source_send_child_signal(c->pid, SIGKILL, NULL, 0)); |
| 247 |
c->pid = NULL; |
| 248 |
|
| 249 |
return 0; |
| 250 |
} |
| 251 |
|
| 252 |
#define MMAP_SIZE (10 * 1024 * 1024) |
| 253 |
|
| 254 |
_noreturn_ static void real_pressure_eat_memory(int pipe_fd) { |
| 255 |
size_t ate = 0; |
| 256 |
|
| 257 |
/* Allocates and touches 10M at a time, until runs out of memory */ |
| 258 |
|
| 259 |
char x; |
| 260 |
ASSERT_EQ(read(pipe_fd, &x, 1), 1); /* Wait for the GO! */ |
| 261 |
|
| 262 |
for (;;) { |
| 263 |
void *p; |
| 264 |
|
| 265 |
p = mmap(NULL, MMAP_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 266 |
ASSERT_TRUE(p != MAP_FAILED); |
| 267 |
|
| 268 |
log_info("Eating another %s.", FORMAT_BYTES(MMAP_SIZE)); |
| 269 |
|
| 270 |
memset(p, random_u32() & 0xFF, MMAP_SIZE); |
| 271 |
ate += MMAP_SIZE; |
| 272 |
|
| 273 |
log_info("Ate %s in total.", FORMAT_BYTES(ate)); |
| 274 |
|
| 275 |
usleep_safe(50 * USEC_PER_MSEC); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
TEST(real_memory_pressure) { |
| 280 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
| 281 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 282 |
_cleanup_(sd_event_source_unrefp) sd_event_source *es = NULL, *cs = NULL; |
| 283 |
_cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; |
| 284 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 285 |
_cleanup_close_pair_ int pipe_fd[2] = EBADF_PAIR; |
| 286 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 287 |
_cleanup_free_ char *scope = NULL; |
| 288 |
const char *object; |
| 289 |
int r; |
| 290 |
|
| 291 |
if (getuid() == 0) |
| 292 |
r = sd_bus_open_system(&bus); |
| 293 |
else |
| 294 |
r = sd_bus_open_user(&bus); |
| 295 |
if (r < 0) |
| 296 |
return (void) log_tests_skipped_errno(r, "can't connect to bus"); |
| 297 |
|
| 298 |
ASSERT_OK(bus_wait_for_jobs_new(bus, &w)); |
| 299 |
|
| 300 |
ASSERT_OK(bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit")); |
| 301 |
ASSERT_OK(asprintf(&scope, "test-%" PRIu64 ".scope", random_u64())); |
| 302 |
ASSERT_OK(sd_bus_message_append(m, "ss", scope, "fail")); |
| 303 |
ASSERT_OK(sd_bus_message_open_container(m, 'a', "(sv)")); |
| 304 |
ASSERT_OK(sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, 0)); |
| 305 |
ASSERT_OK(sd_bus_message_append(m, "(sv)", "MemoryAccounting", "b", true)); |
| 306 |
ASSERT_OK(sd_bus_message_close_container(m)); |
| 307 |
ASSERT_OK(sd_bus_message_append(m, "a(sa(sv))", 0)); |
| 308 |
|
| 309 |
r = sd_bus_call(bus, m, 0, &error, &reply); |
| 310 |
if (r < 0) |
| 311 |
return (void) log_tests_skipped_errno(r, "can't issue transient unit call"); |
| 312 |
|
| 313 |
ASSERT_OK(sd_bus_message_read(reply, "o", &object)); |
| 314 |
|
| 315 |
ASSERT_OK(bus_wait_for_jobs_one(w, object, /* flags= */ BUS_WAIT_JOBS_LOG_ERROR, /* extra_args= */ NULL)); |
| 316 |
|
| 317 |
_cleanup_free_ char *uo = ASSERT_NOT_NULL(unit_dbus_path_from_name(scope)); |
| 318 |
r = controller_supported_on_unit(bus, scope, uo, CGROUP_MASK_MEMORY); |
| 319 |
if (r <= 0) |
| 320 |
return (void) log_tests_skipped("Memory controller not available on scope"); |
| 321 |
|
| 322 |
ASSERT_OK(sd_event_default(&e)); |
| 323 |
|
| 324 |
ASSERT_OK_ERRNO(pipe2(pipe_fd, O_CLOEXEC)); |
| 325 |
|
| 326 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 327 |
r = pidref_safe_fork("(eat-memory)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM, &pidref); |
| 328 |
ASSERT_OK(r); |
| 329 |
if (r == 0) { |
| 330 |
real_pressure_eat_memory(pipe_fd[0]); |
| 331 |
_exit(EXIT_SUCCESS); |
| 332 |
} |
| 333 |
|
| 334 |
ASSERT_OK(event_add_child_pidref(e, &cs, &pidref, WEXITED, real_pressure_child_callback, NULL)); |
| 335 |
ASSERT_OK(sd_event_source_set_child_process_own(cs, true)); |
| 336 |
|
| 337 |
ASSERT_OK_ERRNO(unsetenv("MEMORY_PRESSURE_WATCH")); |
| 338 |
ASSERT_OK_ERRNO(unsetenv("MEMORY_PRESSURE_WRITE")); |
| 339 |
|
| 340 |
struct real_pressure_context context = { |
| 341 |
.pid = cs, |
| 342 |
}; |
| 343 |
|
| 344 |
r = sd_event_add_memory_pressure(e, &es, real_memory_pressure_callback, &context); |
| 345 |
if (r < 0) |
| 346 |
return (void) log_tests_skipped_errno(r, "can't allocate memory pressure fd"); |
| 347 |
|
| 348 |
ASSERT_OK(sd_event_source_set_description(es, "real pressure event source")); |
| 349 |
ASSERT_OK_ZERO(sd_event_source_set_memory_pressure_type(es, "some")); |
| 350 |
ASSERT_OK_POSITIVE(sd_event_source_set_memory_pressure_type(es, "full")); |
| 351 |
ASSERT_OK_ZERO(sd_event_source_set_memory_pressure_type(es, "full")); |
| 352 |
ASSERT_OK_POSITIVE(sd_event_source_set_memory_pressure_type(es, "some")); |
| 353 |
ASSERT_OK_ZERO(sd_event_source_set_memory_pressure_type(es, "some")); |
| 354 |
/* Unprivileged writes require a minimum of 2s otherwise the kernel will refuse the write. */ |
| 355 |
ASSERT_OK_POSITIVE(sd_event_source_set_memory_pressure_period(es, 70 * USEC_PER_MSEC, 2 * USEC_PER_SEC)); |
| 356 |
ASSERT_OK_ZERO(sd_event_source_set_memory_pressure_period(es, 70 * USEC_PER_MSEC, 2 * USEC_PER_SEC)); |
| 357 |
ASSERT_OK(sd_event_source_set_enabled(es, SD_EVENT_ONESHOT)); |
| 358 |
|
| 359 |
uint64_t mcurrent = UINT64_MAX; |
| 360 |
ASSERT_OK(sd_bus_get_property_trivial(bus, "org.freedesktop.systemd1", uo, "org.freedesktop.systemd1.Scope", "MemoryCurrent", &error, 't', &mcurrent)); |
| 361 |
|
| 362 |
printf("current: %" PRIu64 "\n", mcurrent); |
| 363 |
if (mcurrent == UINT64_MAX) |
| 364 |
return (void) log_tests_skipped_errno(r, "memory accounting not available"); |
| 365 |
|
| 366 |
m = sd_bus_message_unref(m); |
| 367 |
|
| 368 |
ASSERT_OK(bus_message_new_method_call(bus, &m, bus_systemd_mgr, "SetUnitProperties")); |
| 369 |
ASSERT_OK(sd_bus_message_append(m, "sb", scope, true)); |
| 370 |
ASSERT_OK(sd_bus_message_open_container(m, 'a', "(sv)")); |
| 371 |
ASSERT_OK(sd_bus_message_append(m, "(sv)", "MemoryHigh", "t", mcurrent + (15 * 1024 * 1024))); |
| 372 |
ASSERT_OK(sd_bus_message_append(m, "(sv)", "MemoryMax", "t", mcurrent + (50 * 1024 * 1024))); |
| 373 |
ASSERT_OK(sd_bus_message_close_container(m)); |
| 374 |
|
| 375 |
ASSERT_OK(sd_bus_call(bus, m, 0, NULL, NULL)); |
| 376 |
|
| 377 |
/* Generate some memory allocations via mempool */ |
| 378 |
#define NN (1024) |
| 379 |
Hashmap **h = new(Hashmap*, NN); |
| 380 |
for (int i = 0; i < NN; i++) |
| 381 |
h[i] = hashmap_new(NULL); |
| 382 |
for (int i = 0; i < NN; i++) |
| 383 |
hashmap_free(h[i]); |
| 384 |
free(h); |
| 385 |
|
| 386 |
/* Now start eating memory */ |
| 387 |
ASSERT_EQ(write(pipe_fd[1], &(const char) { 'x' }, 1), 1); |
| 388 |
|
| 389 |
if (event_loop_with_timeout(e) != 0) |
| 390 |
return; |
| 391 |
|
| 392 |
int ex = 0; |
| 393 |
ASSERT_OK(sd_event_get_exit_code(e, &ex)); |
| 394 |
ASSERT_EQ(ex, 31); |
| 395 |
} |
| 396 |
|
| 397 |
/* CPU pressure real test */ |
| 398 |
|
| 399 |
static int real_cpu_pressure_callback(sd_event_source *s, void *userdata) { |
| 400 |
struct real_pressure_context *c = ASSERT_PTR(userdata); |
| 401 |
const char *d; |
| 402 |
|
| 403 |
ASSERT_NOT_NULL(s); |
| 404 |
ASSERT_OK(sd_event_source_get_description(s, &d)); |
| 405 |
|
| 406 |
log_notice("real cpu pressure event: %s", d); |
| 407 |
|
| 408 |
ASSERT_NOT_NULL(c->pid); |
| 409 |
ASSERT_OK(sd_event_source_send_child_signal(c->pid, SIGKILL, NULL, 0)); |
| 410 |
c->pid = NULL; |
| 411 |
|
| 412 |
return 0; |
| 413 |
} |
| 414 |
|
| 415 |
_noreturn_ static void real_pressure_eat_cpu(int pipe_fd) { |
| 416 |
char x; |
| 417 |
ASSERT_EQ(read(pipe_fd, &x, 1), 1); /* Wait for the GO! */ |
| 418 |
|
| 419 |
/* Busy-loop to generate CPU pressure */ |
| 420 |
for (;;) |
| 421 |
__asm__ volatile("" ::: "memory"); /* Prevent optimization */ |
| 422 |
} |
| 423 |
|
| 424 |
TEST(real_cpu_pressure) { |
| 425 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
| 426 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 427 |
_cleanup_(sd_event_source_unrefp) sd_event_source *es = NULL, *cs = NULL; |
| 428 |
_cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; |
| 429 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 430 |
_cleanup_close_pair_ int pipe_fd[2] = EBADF_PAIR; |
| 431 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 432 |
_cleanup_free_ char *scope = NULL; |
| 433 |
const char *object; |
| 434 |
int r; |
| 435 |
|
| 436 |
if (getuid() == 0) |
| 437 |
r = sd_bus_open_system(&bus); |
| 438 |
else |
| 439 |
r = sd_bus_open_user(&bus); |
| 440 |
if (r < 0) |
| 441 |
return (void) log_tests_skipped_errno(r, "can't connect to bus"); |
| 442 |
|
| 443 |
ASSERT_OK(bus_wait_for_jobs_new(bus, &w)); |
| 444 |
|
| 445 |
ASSERT_OK(bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit")); |
| 446 |
ASSERT_OK(asprintf(&scope, "test-%" PRIu64 ".scope", random_u64())); |
| 447 |
ASSERT_OK(sd_bus_message_append(m, "ss", scope, "fail")); |
| 448 |
ASSERT_OK(sd_bus_message_open_container(m, 'a', "(sv)")); |
| 449 |
ASSERT_OK(sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, 0)); |
| 450 |
ASSERT_OK(sd_bus_message_append(m, "(sv)", "CPUAccounting", "b", true)); |
| 451 |
ASSERT_OK(sd_bus_message_close_container(m)); |
| 452 |
ASSERT_OK(sd_bus_message_append(m, "a(sa(sv))", 0)); |
| 453 |
|
| 454 |
r = sd_bus_call(bus, m, 0, &error, &reply); |
| 455 |
if (r < 0) |
| 456 |
return (void) log_tests_skipped_errno(r, "can't issue transient unit call"); |
| 457 |
|
| 458 |
ASSERT_OK(sd_bus_message_read(reply, "o", &object)); |
| 459 |
|
| 460 |
ASSERT_OK(bus_wait_for_jobs_one(w, object, /* flags= */ BUS_WAIT_JOBS_LOG_ERROR, /* extra_args= */ NULL)); |
| 461 |
|
| 462 |
_cleanup_free_ char *uo = ASSERT_NOT_NULL(unit_dbus_path_from_name(scope)); |
| 463 |
r = controller_supported_on_unit(bus, scope, uo, CGROUP_MASK_CPU); |
| 464 |
if (r <= 0) |
| 465 |
return (void) log_tests_skipped("CPU controller not available on scope"); |
| 466 |
|
| 467 |
ASSERT_OK(sd_event_default(&e)); |
| 468 |
|
| 469 |
ASSERT_OK_ERRNO(pipe2(pipe_fd, O_CLOEXEC)); |
| 470 |
|
| 471 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 472 |
r = pidref_safe_fork("(eat-cpu)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM, &pidref); |
| 473 |
ASSERT_OK(r); |
| 474 |
if (r == 0) { |
| 475 |
real_pressure_eat_cpu(pipe_fd[0]); |
| 476 |
_exit(EXIT_SUCCESS); |
| 477 |
} |
| 478 |
|
| 479 |
ASSERT_OK(event_add_child_pidref(e, &cs, &pidref, WEXITED, real_pressure_child_callback, NULL)); |
| 480 |
ASSERT_OK(sd_event_source_set_child_process_own(cs, true)); |
| 481 |
|
| 482 |
ASSERT_OK_ERRNO(unsetenv("CPU_PRESSURE_WATCH")); |
| 483 |
ASSERT_OK_ERRNO(unsetenv("CPU_PRESSURE_WRITE")); |
| 484 |
|
| 485 |
struct real_pressure_context context = { |
| 486 |
.pid = cs, |
| 487 |
}; |
| 488 |
|
| 489 |
r = sd_event_add_cpu_pressure(e, &es, real_cpu_pressure_callback, &context); |
| 490 |
if (r < 0) |
| 491 |
return (void) log_tests_skipped_errno(r, "can't allocate cpu pressure fd"); |
| 492 |
|
| 493 |
ASSERT_OK(sd_event_source_set_description(es, "real pressure event source")); |
| 494 |
ASSERT_OK_ZERO(sd_event_source_set_cpu_pressure_type(es, "some")); |
| 495 |
/* Unprivileged writes require a minimum of 2s otherwise the kernel will refuse the write. */ |
| 496 |
ASSERT_OK_POSITIVE(sd_event_source_set_cpu_pressure_period(es, 70 * USEC_PER_MSEC, 2 * USEC_PER_SEC)); |
| 497 |
ASSERT_OK_ZERO(sd_event_source_set_cpu_pressure_period(es, 70 * USEC_PER_MSEC, 2 * USEC_PER_SEC)); |
| 498 |
ASSERT_OK(sd_event_source_set_enabled(es, SD_EVENT_ONESHOT)); |
| 499 |
|
| 500 |
m = sd_bus_message_unref(m); |
| 501 |
|
| 502 |
ASSERT_OK(bus_message_new_method_call(bus, &m, bus_systemd_mgr, "SetUnitProperties")); |
| 503 |
ASSERT_OK(sd_bus_message_append(m, "sb", scope, true)); |
| 504 |
ASSERT_OK(sd_bus_message_open_container(m, 'a', "(sv)")); |
| 505 |
ASSERT_OK(sd_bus_message_append(m, "(sv)", "CPUQuotaPerSecUSec", "t", (uint64_t) 1000)); /* 0.1% CPU */ |
| 506 |
ASSERT_OK(sd_bus_message_close_container(m)); |
| 507 |
|
| 508 |
ASSERT_OK(sd_bus_call(bus, m, 0, NULL, NULL)); |
| 509 |
|
| 510 |
/* Now start eating CPU */ |
| 511 |
ASSERT_EQ(write(pipe_fd[1], &(const char) { 'x' }, 1), 1); |
| 512 |
|
| 513 |
if (event_loop_with_timeout(e) != 0) |
| 514 |
return; |
| 515 |
|
| 516 |
int ex = 0; |
| 517 |
ASSERT_OK(sd_event_get_exit_code(e, &ex)); |
| 518 |
ASSERT_EQ(ex, 31); |
| 519 |
} |
| 520 |
|
| 521 |
/* IO pressure real test */ |
| 522 |
|
| 523 |
static int real_io_pressure_callback(sd_event_source *s, void *userdata) { |
| 524 |
struct real_pressure_context *c = ASSERT_PTR(userdata); |
| 525 |
const char *d; |
| 526 |
|
| 527 |
ASSERT_NOT_NULL(s); |
| 528 |
ASSERT_OK(sd_event_source_get_description(s, &d)); |
| 529 |
|
| 530 |
log_notice("real io pressure event: %s", d); |
| 531 |
|
| 532 |
ASSERT_NOT_NULL(c->pid); |
| 533 |
ASSERT_OK(sd_event_source_send_child_signal(c->pid, SIGKILL, NULL, 0)); |
| 534 |
c->pid = NULL; |
| 535 |
|
| 536 |
return 0; |
| 537 |
} |
| 538 |
|
| 539 |
_noreturn_ static void real_pressure_eat_io(int pipe_fd) { |
| 540 |
char x; |
| 541 |
ASSERT_EQ(read(pipe_fd, &x, 1), 1); /* Wait for the GO! */ |
| 542 |
|
| 543 |
/* Write and fsync in a loop to generate IO pressure */ |
| 544 |
for (;;) { |
| 545 |
_cleanup_close_ int fd = -EBADF; |
| 546 |
|
| 547 |
fd = open("/var/tmp/.io-pressure-test", O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0600); |
| 548 |
if (fd < 0) |
| 549 |
continue; |
| 550 |
|
| 551 |
char buf[4096]; |
| 552 |
memset(buf, 'x', sizeof(buf)); |
| 553 |
for (int i = 0; i < 256; i++) |
| 554 |
if (write(fd, buf, sizeof(buf)) < 0) |
| 555 |
break; |
| 556 |
(void) fsync(fd); |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
TEST(real_io_pressure) { |
| 561 |
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
| 562 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 563 |
_cleanup_(sd_event_source_unrefp) sd_event_source *es = NULL, *cs = NULL; |
| 564 |
_cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; |
| 565 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 566 |
_cleanup_close_pair_ int pipe_fd[2] = EBADF_PAIR; |
| 567 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 568 |
_cleanup_free_ char *scope = NULL; |
| 569 |
const char *object; |
| 570 |
int r; |
| 571 |
|
| 572 |
if (getuid() == 0) |
| 573 |
r = sd_bus_open_system(&bus); |
| 574 |
else |
| 575 |
r = sd_bus_open_user(&bus); |
| 576 |
if (r < 0) |
| 577 |
return (void) log_tests_skipped_errno(r, "can't connect to bus"); |
| 578 |
|
| 579 |
ASSERT_OK(bus_wait_for_jobs_new(bus, &w)); |
| 580 |
|
| 581 |
ASSERT_OK(bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit")); |
| 582 |
ASSERT_OK(asprintf(&scope, "test-%" PRIu64 ".scope", random_u64())); |
| 583 |
ASSERT_OK(sd_bus_message_append(m, "ss", scope, "fail")); |
| 584 |
ASSERT_OK(sd_bus_message_open_container(m, 'a', "(sv)")); |
| 585 |
ASSERT_OK(sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, 0)); |
| 586 |
ASSERT_OK(sd_bus_message_append(m, "(sv)", "IOAccounting", "b", true)); |
| 587 |
ASSERT_OK(sd_bus_message_close_container(m)); |
| 588 |
ASSERT_OK(sd_bus_message_append(m, "a(sa(sv))", 0)); |
| 589 |
|
| 590 |
r = sd_bus_call(bus, m, 0, &error, &reply); |
| 591 |
if (r < 0) |
| 592 |
return (void) log_tests_skipped_errno(r, "can't issue transient unit call"); |
| 593 |
|
| 594 |
ASSERT_OK(sd_bus_message_read(reply, "o", &object)); |
| 595 |
|
| 596 |
ASSERT_OK(bus_wait_for_jobs_one(w, object, /* flags= */ BUS_WAIT_JOBS_LOG_ERROR, /* extra_args= */ NULL)); |
| 597 |
|
| 598 |
_cleanup_free_ char *uo = ASSERT_NOT_NULL(unit_dbus_path_from_name(scope)); |
| 599 |
r = controller_supported_on_unit(bus, scope, uo, CGROUP_MASK_IO); |
| 600 |
if (r <= 0) |
| 601 |
return (void) log_tests_skipped("IO controller not available on scope"); |
| 602 |
|
| 603 |
ASSERT_OK(sd_event_default(&e)); |
| 604 |
|
| 605 |
ASSERT_OK_ERRNO(pipe2(pipe_fd, O_CLOEXEC)); |
| 606 |
|
| 607 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 608 |
r = pidref_safe_fork("(eat-io)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGTERM, &pidref); |
| 609 |
ASSERT_OK(r); |
| 610 |
if (r == 0) { |
| 611 |
real_pressure_eat_io(pipe_fd[0]); |
| 612 |
_exit(EXIT_SUCCESS); |
| 613 |
} |
| 614 |
|
| 615 |
ASSERT_OK(event_add_child_pidref(e, &cs, &pidref, WEXITED, real_pressure_child_callback, NULL)); |
| 616 |
ASSERT_OK(sd_event_source_set_child_process_own(cs, true)); |
| 617 |
|
| 618 |
ASSERT_OK_ERRNO(unsetenv("IO_PRESSURE_WATCH")); |
| 619 |
ASSERT_OK_ERRNO(unsetenv("IO_PRESSURE_WRITE")); |
| 620 |
|
| 621 |
struct real_pressure_context context = { |
| 622 |
.pid = cs, |
| 623 |
}; |
| 624 |
|
| 625 |
r = sd_event_add_io_pressure(e, &es, real_io_pressure_callback, &context); |
| 626 |
if (r < 0) |
| 627 |
return (void) log_tests_skipped_errno(r, "can't allocate io pressure fd"); |
| 628 |
|
| 629 |
ASSERT_OK(sd_event_source_set_description(es, "real pressure event source")); |
| 630 |
ASSERT_OK_ZERO(sd_event_source_set_io_pressure_type(es, "some")); |
| 631 |
/* Unprivileged writes require a minimum of 2s otherwise the kernel will refuse the write. */ |
| 632 |
ASSERT_OK_POSITIVE(sd_event_source_set_io_pressure_period(es, 70 * USEC_PER_MSEC, 2 * USEC_PER_SEC)); |
| 633 |
ASSERT_OK_ZERO(sd_event_source_set_io_pressure_period(es, 70 * USEC_PER_MSEC, 2 * USEC_PER_SEC)); |
| 634 |
ASSERT_OK(sd_event_source_set_enabled(es, SD_EVENT_ONESHOT)); |
| 635 |
|
| 636 |
m = sd_bus_message_unref(m); |
| 637 |
|
| 638 |
ASSERT_OK(bus_message_new_method_call(bus, &m, bus_systemd_mgr, "SetUnitProperties")); |
| 639 |
ASSERT_OK(sd_bus_message_append(m, "sb", scope, true)); |
| 640 |
ASSERT_OK(sd_bus_message_open_container(m, 'a', "(sv)")); |
| 641 |
ASSERT_OK(sd_bus_message_open_container(m, 'r', "sv")); |
| 642 |
ASSERT_OK(sd_bus_message_append(m, "s", "IOWriteBandwidthMax")); |
| 643 |
ASSERT_OK(sd_bus_message_open_container(m, 'v', "a(st)")); |
| 644 |
ASSERT_OK(sd_bus_message_append(m, "a(st)", 1, "/var/tmp", (uint64_t) 1024*1024)); /* 1M/s */ |
| 645 |
ASSERT_OK(sd_bus_message_close_container(m)); |
| 646 |
ASSERT_OK(sd_bus_message_close_container(m)); |
| 647 |
ASSERT_OK(sd_bus_message_close_container(m)); |
| 648 |
|
| 649 |
ASSERT_OK(sd_bus_call(bus, m, 0, NULL, NULL)); |
| 650 |
|
| 651 |
/* Now start eating IO */ |
| 652 |
ASSERT_EQ(write(pipe_fd[1], &(const char) { 'x' }, 1), 1); |
| 653 |
|
| 654 |
if (event_loop_with_timeout(e) != 0) |
| 655 |
return; |
| 656 |
|
| 657 |
int ex = 0; |
| 658 |
ASSERT_OK(sd_event_get_exit_code(e, &ex)); |
| 659 |
ASSERT_EQ(ex, 31); |
| 660 |
} |
| 661 |
|
| 662 |
static int outro(void) { |
| 663 |
(void) unlink("/var/tmp/.io-pressure-test"); |
| 664 |
hashmap_trim_pools(); |
| 665 |
return 0; |
| 666 |
} |
| 667 |
|
| 668 |
DEFINE_TEST_MAIN_FULL(LOG_DEBUG, NULL, outro); |