1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
|
#include "stress-ng.h"
#include "core-capabilities.h"
#include "core-killpid.h"
#if defined(HAVE_SYS_MOUNT_H)
#include <sys/mount.h>
#endif
static const stress_help_t help[] = {
{ NULL, "cgroup N", "start N workers exercising cgroup mount/read/write/umounts" },
{ NULL, "cgroup-ops N", "stop after N iterations of cgroup actions" },
{ NULL, NULL, NULL }
};
static int stress_cgroup_supported(const char *name)
{
if (!stress_check_capability(SHIM_CAP_SYS_ADMIN)) {
pr_inf_skip("%s stressor will be skipped, "
"need to be running with CAP_SYS_ADMIN "
"rights for this stressor\n", name);
return -1;
}
return 0;
}
#if defined(__linux__) && \
defined(HAVE_SYS_MOUNT_H)
#define STRESS_CGROUP_MOUNTED (1)
#define STRESS_CGROUP_UNMOUNTED (2)
#define STRESS_CGROUP_UNKNOWN (3)
typedef struct {
const char *name;
const char *value;
} stress_cgroup_values_t;
static void stress_cgroup_remove_nl(char *str)
{
char *ptr;
ptr = strchr(str, '\n');
if (ptr)
*ptr = '\0';
}
static int stress_cgroup_mounted_state(const char *path)
{
FILE *fp;
char buf[4096];
int ret = STRESS_CGROUP_UNMOUNTED;
fp = fopen("/proc/mounts", "r");
if (!fp)
return STRESS_CGROUP_UNKNOWN;
while (fgets(buf, sizeof(buf), fp)) {
const char *mnt, *type;
char *ptr;
ptr = buf;
while (*ptr && (*ptr != ' '))
ptr++;
if (*ptr == '\0')
break;
*ptr = '\0';
ptr++;
mnt = ptr;
while (*ptr && (*ptr != ' '))
ptr++;
if (*ptr == '\0')
break;
*ptr = '\0';
ptr++;
type = ptr;
while (*ptr && (*ptr != ' '))
ptr++;
if (*ptr == '\0')
break;
*ptr = '\0';
if ((strcmp(type, "cgroup2") == 0) &&
(strcmp(mnt, path) == 0)) {
ret = STRESS_CGROUP_MOUNTED;
break;
}
}
(void)fclose(fp);
return ret;
}
static void stress_group_sleep(uint64_t *counter)
{
const uint64_t ns = stress_mwc64modn(100000000) + 50000000;
(void)shim_nanosleep_uint64(ns);
(*counter)++;
}
static void stress_cgroup_umount(
stress_args_t *args,
const char *path,
uint64_t *umount_retry)
{
int i;
int ret;
for (i = 0; i < 128; i++) {
if (stress_cgroup_mounted_state(path) == STRESS_CGROUP_UNMOUNTED)
return;
#if defined(HAVE_UMOUNT2) && \
defined(MNT_FORCE)
if (stress_mwc1()) {
ret = umount2(path, MNT_FORCE);
} else {
ret = umount(path);
}
#else
ret = umount(path);
#endif
if (ret == 0) {
if (i > 1)
stress_group_sleep(umount_retry);
continue;
}
switch (errno) {
case EAGAIN:
case EBUSY:
case ENOMEM:
stress_group_sleep(umount_retry);
break;
case EINVAL:
return;
default:
pr_inf("%s: umount failed %s: %d %s\n", args->name,
path, errno, strerror(errno));
break;
}
}
}
static void stress_cgroup_read(const char *path)
{
int fd, i;
char buf[1024];
off_t len = 0;
struct stat statbuf;
fd = open(path, O_RDONLY);
if (fd < 0)
return;
VOID_RET(int, shim_fstat(fd, &statbuf));
for (;;) {
ssize_t ret;
ret = read(fd, buf, sizeof(buf));
if (ret > 0)
len += (off_t)ret;
else
break;
}
for (i = 0; (i < 2) && (i < len); i++) {
const off_t offset = (off_t)stress_mwc32modn((uint32_t)len);
if (lseek(fd, offset, SEEK_SET) >= 0)
VOID_RET(int, read(fd, buf, sizeof(buf)));
}
(void)close(fd);
}
static void stress_cgroup_controllers(const char *realpathname)
{
char path[PATH_MAX + 32];
char controllers[512];
const char *token;
char *ptr;
ssize_t ret;
(void)snprintf(path, sizeof(path), "%s/%s", realpathname, "cgroup.subtree_control");
ret = stress_system_read(path, controllers, sizeof(controllers));
if (ret < 0)
return;
stress_cgroup_remove_nl(controllers);
(void)snprintf(path, sizeof(path), "%s/%s", realpathname, "cgroup.subtree_control");
for (ptr = controllers; (token = strtok(ptr, " ")) != NULL; ptr = NULL) {
char controller[256];
ret = (ssize_t)snprintf(controller, sizeof(controller), "+%s\n", token);
stress_system_write(path, controller, ret);
}
}
static void stress_cgroup_read_files(const char *realpathname)
{
static const char * const filenames[] = {
"cgroup.type",
"cgroup.procs",
"cgroup.threads",
"cgroup.controllers",
"cgroup.subtree_control",
"cgroup.events",
"cgroup.max.descendants",
"cgroup.max.depth",
"cgroup.stat",
"cgroup.freeze",
"cgroup.kill",
"cgroup.pressure",
"irq.pressure",
};
size_t i;
for (i = 0; i < SIZEOF_ARRAY(filenames); i++) {
char path[PATH_MAX + 32];
(void)snprintf(path, sizeof(path), "%s/%s", realpathname, filenames[i]);
stress_cgroup_read(path);
}
}
static void stress_cgroup_add_pid(const char *realpathname, const pid_t pid)
{
char filename[PATH_MAX + 64], cmd[64];
ssize_t len;
len = (ssize_t)snprintf(cmd, sizeof(cmd), "%" PRIdMAX "\n", (intmax_t)pid);
(void)snprintf(filename, sizeof(filename), "%s/stress-ng-%" PRIdMAX "/cgroup.procs", realpathname, (intmax_t)pid);
stress_system_write(filename, cmd, len);
}
static void stress_cgroup_del_pid(const char *realpathname, const pid_t pid)
{
char filename[PATH_MAX + 64], cmd[64];
ssize_t len;
len = (ssize_t)snprintf(cmd, sizeof(cmd), "%" PRIdMAX "\n", (intmax_t)pid);
(void)snprintf(filename, sizeof(filename), "%s/cgroup.procs", realpathname);
stress_system_write(filename, cmd, len);
}
static void stress_cgroup_new_group(const char *realpathname)
{
pid_t pid;
pid = fork();
if (pid == 0) {
do {
void *ptr;
const size_t sz = MB;
ptr = mmap(NULL, sz, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_SHARED, -1, 0);
(void)shim_sched_yield();
if (ptr != MAP_FAILED)
(void)munmap(ptr, sz);
(void)shim_sched_yield();
} while (stress_continue_flag());
_exit(0);
} else {
int status;
size_t i;
char path[PATH_MAX + 64];
static const stress_cgroup_values_t values[] = {
{ "cpu.stat", NULL },
{ "cpu.weight", "90" },
{ "cpu.weight.nice", "-4" },
{ "cpu.max", NULL },
{ "cpu.max.burst", "50" },
{ "cpu.pressure", NULL },
{ "cpu.uclamp.min", "10.0" },
{ "cpu.uclamp.max", "95.0" },
{ "cpu.idle", "1" },
{ "cpu.idle", "0" },
{ "memory.current", NULL },
{ "memory.min", "1M" },
{ "memory.low", "2M" },
{ "memory.high", "32M" },
{ "memory.max", "128M" },
{ "memory.reclaim", "2M" },
{ "memory.peak", NULL },
{ "memory.oom.group", NULL },
{ "memory.events", NULL },
{ "memory.events.local", NULL },
{ "memory.stat", NULL },
{ "memory.numa_stat", NULL },
{ "memory.swap.current", NULL },
{ "memory.swap.high", NULL },
{ "memory.swap.peak", NULL },
{ "memory.swap.max", NULL },
{ "memory.swap.events", NULL },
{ "memory.zswap.current", NULL },
{ "memory.zswap.max", NULL },
{ "memory.zswap.writeback", "0" },
{ "memory.zswap.writeback", "1" },
{ "memory.pressure", NULL },
{ "io.stat", NULL },
{ "io.cost.qos", NULL },
{ "io.cost.model", NULL },
{ "io.weight", "default 90" },
{ "io.max", NULL },
{ "io.pressure", NULL },
{ "io.latency", NULL },
{ "io.stat", NULL },
{ "pids.max", "10000" },
{ "pids.current", NULL },
{ "pids.peak", NULL },
{ "pids.events", NULL },
{ "pids.events.local", NULL },
{ "cpuset.cpus", "0" },
{ "cpuset.cpus.effective", NULL },
{ "cpuset.mems", "0" },
{ "cpuset.mems.effective", NULL },
{ "cpuset.cpus.exclusive", NULL },
{ "cpuset.cpus.exclusive.effective", NULL },
{ "cpuset.cpus.isolated", NULL },
{ "cpuset.cpus.partition", NULL },
{ "rdma.max", NULL },
{ "rdma.current", NULL },
{ "hugetlb.1GB.current", NULL },
{ "hugetlb.1GB.events", NULL },
{ "hugetlb.1GB.events.local", NULL },
{ "hugetlb.1GB.max", NULL },
{ "hugetlb.1GB.numa_stat", NULL },
{ "hugetlb.1GB.rsvd.current", NULL },
{ "hugetlb.1GB.rsvd.max", NULL },
{ "hugetlb.2GB.current", NULL },
{ "hugetlb.2GB.events", NULL },
{ "hugetlb.2GB.events.local", NULL },
{ "hugetlb.2GB.max", NULL },
{ "hugetlb.2GB.numa_stat", NULL },
{ "hugetlb.2GB.rsvd.current", NULL },
{ "hugetlb.2GB.rsvd.max", NULL },
{ "misc.capacity", NULL },
{ "misc.current", NULL },
{ "misc.peak", NULL },
{ "misc.max", NULL },
{ "misc.events", NULL },
{ "misc.events.local", NULL },
{ "cgroup.type", NULL },
{ "cgroup.procs", NULL },
{ "cgroup.threads", NULL },
{ "cgroup.controllers", NULL },
{ "cgroup.subtree_control", NULL },
{ "cgroup.events", NULL },
{ "cgroup.max.descendants", NULL },
{ "cgroup.max.depth", NULL },
{ "cgroup.stat", NULL },
{ "cgroup.pressure", NULL },
{ "cgroup.freeze", "0" },
{ "cgroup.freeze", "1" },
};
(void)snprintf(path, sizeof(path), "%s/stress-ng-%" PRIdMAX, realpathname, (intmax_t)pid);
if (mkdir(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0) {
stress_kill_pid_wait(pid, &status);
(void)rmdir(path);
return;
}
for (i = 0; i < SIZEOF_ARRAY(values); i++) {
char filename[PATH_MAX + 64];
stress_cgroup_add_pid(realpathname, pid);
(void)snprintf(filename, sizeof(filename), "%s/stress-ng-%" PRIdMAX "/%s", realpathname, (intmax_t)pid, values[i].name);
stress_cgroup_read(filename);
if (values[i].value) {
(void)stress_system_write(filename, values[i].value, strlen(values[i].value));
stress_cgroup_read(filename);
}
stress_cgroup_del_pid(realpathname, pid);
}
stress_kill_pid_wait(pid, &status);
(void)rmdir(path);
}
}
static int stress_cgroup_child(stress_args_t *args)
{
char pathname[PATH_MAX], realpathname[PATH_MAX];
int rc = EXIT_SUCCESS;
uint64_t mount_retry = 0, umount_retry = 0;
stress_parent_died_alarm();
(void)sched_settings_apply(true);
stress_temp_dir(pathname, sizeof(pathname), args->name, args->pid, args->instance);
if (mkdir(pathname, S_IRGRP | S_IWGRP) < 0) {
pr_fail("%s: cannot mkdir %s, errno=%d (%s)\n",
args->name, pathname, errno, strerror(errno));
return EXIT_FAILURE;
}
if (!realpath(pathname, realpathname)) {
pr_fail("%s: cannot realpath %s, errno=%d (%s)\n",
args->name, pathname, errno, strerror(errno));
(void)stress_temp_dir_rm_args(args);
return EXIT_FAILURE;
}
do {
int ret;
ret = mount("none", realpathname, "cgroup2", 0, NULL);
if (ret < 0) {
if (errno == EBUSY) {
stress_group_sleep(&mount_retry);
continue;
} else if (errno == EPERM) {
pr_inf_skip("%s: mount failed, no permission, skipping stressor\n",
args->name);
rc = EXIT_NO_RESOURCE;
goto cleanup;
} else if ((errno != ENOSPC) &&
(errno != ENOMEM) &&
(errno != ENODEV)) {
pr_fail("%s: mount failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
rc = EXIT_FAILURE;
}
goto cleanup;
}
stress_cgroup_controllers(realpathname);
stress_cgroup_read_files(realpathname);
stress_cgroup_new_group(realpathname);
stress_cgroup_umount(args, realpathname, &umount_retry);
stress_bogo_inc(args);
} while (stress_continue(args));
cleanup:
stress_cgroup_umount(args, realpathname, &umount_retry);
if (stress_cgroup_mounted_state(realpathname) == STRESS_CGROUP_MOUNTED)
pr_dbg("%s: could not unmount of %s\n", args->name, realpathname);
(void)stress_temp_dir_rm_args(args);
if ((mount_retry + umount_retry) > 0) {
pr_dbg("%s: %" PRIu64 " mount retries, %" PRIu64 " umount retries\n",
args->name, mount_retry, umount_retry);
}
return rc;
}
static int stress_cgroup_mount(stress_args_t *args)
{
int pid, rc = EXIT_SUCCESS;
stress_set_proc_state(args->name, STRESS_STATE_SYNC_WAIT);
stress_sync_start_wait(args);
stress_set_proc_state(args->name, STRESS_STATE_RUN);
do {
again:
if (UNLIKELY(!stress_continue_flag()))
break;
pid = fork();
if (pid < 0) {
if (stress_redo_fork(args, errno))
goto again;
if (UNLIKELY(!stress_continue(args)))
goto finish;
pr_err("%s: fork failed: errno=%d (%s)\n",
args->name, errno, strerror(errno));
} else if (pid > 0) {
int status, waitret;
waitret = shim_waitpid(pid, &status, 0);
if (waitret < 0) {
if (errno != EINTR) {
pr_dbg("%s: waitpid(): errno=%d (%s)\n",
args->name, errno, strerror(errno));
(void)stress_kill_pid(pid);
}
(void)shim_waitpid(pid, &status, 0);
} else if (WIFSIGNALED(status)) {
pr_dbg("%s: child died: %s (instance %d)\n",
args->name, stress_strsignal(WTERMSIG(status)),
args->instance);
if (WTERMSIG(status) == SIGKILL) {
stress_log_system_mem_info();
pr_dbg("%s: assuming killed by OOM killer, "
"restarting again (instance %d)\n",
args->name, args->instance);
goto again;
}
} else if (WEXITSTATUS(status) == EXIT_FAILURE) {
pr_fail("%s: child mount/umount failed\n", args->name);
rc = EXIT_FAILURE;
break;
} else if (WEXITSTATUS(status) == EXIT_NO_RESOURCE) {
rc = EXIT_NO_RESOURCE;
break;
}
} else {
_exit(stress_cgroup_child(args));
}
} while (stress_continue(args));
finish:
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
return rc;
}
const stressor_info_t stress_cgroup_info = {
.stressor = stress_cgroup_mount,
.class = CLASS_OS,
.supported = stress_cgroup_supported,
.verify = VERIFY_ALWAYS,
.help = help
};
#else
const stressor_info_t stress_cgroup_info = {
.stressor = stress_unimplemented,
.class = CLASS_OS,
.supported = stress_cgroup_supported,
.verify = VERIFY_ALWAYS,
.help = help,
.unimplemented_reason = "only supported on Linux"
};
#endif
|