mirror of
https://github.com/systemd/systemd.git
synced 2024-11-08 11:27:32 +03:00
efdb02375b
This patch set adds full support the new unified cgroup hierarchy logic of modern kernels. A new kernel command line option "systemd.unified_cgroup_hierarchy=1" is added. If specified the unified hierarchy is mounted to /sys/fs/cgroup instead of a tmpfs. No further hierarchies are mounted. The kernel command line option defaults to off. We can turn it on by default as soon as the kernel's APIs regarding this are stabilized (but even then downstream distros might want to turn this off, as this will break any tools that access cgroupfs directly). It is possibly to choose for each boot individually whether the unified or the legacy hierarchy is used. nspawn will by default provide the legacy hierarchy to containers if the host is using it, and the unified otherwise. However it is possible to run containers with the unified hierarchy on a legacy host and vice versa, by setting the $UNIFIED_CGROUP_HIERARCHY environment variable for nspawn to 1 or 0, respectively. The unified hierarchy provides reliable cgroup empty notifications for the first time, via inotify. To make use of this we maintain one manager-wide inotify fd, and each cgroup to it. This patch also removes cg_delete() which is unused now. On kernel 4.2 only the "memory" controller is compatible with the unified hierarchy, hence that's the only controller systemd exposes when booted in unified heirarchy mode. This introduces a new enum for enumerating supported controllers, plus a related enum for the mask bits mapping to it. The core is changed to make use of this everywhere. This moves PID 1 into a new "init.scope" implicit scope unit in the root slice. This is necessary since on the unified hierarchy cgroups may either contain subgroups or processes but not both. PID 1 hence has to move out of the root cgroup (strictly speaking the root cgroup is the only one where processes and subgroups are still allowed, but in order to support containers nicey, we move PID 1 into the new scope in all cases.) This new unit is also used on legacy hierarchy setups. It's actually pretty useful on all systems, as it can then be used to filter journal messages coming from PID 1, and so on. The root slice ("-.slice") is now implicitly created and started (and does not require a unit file on disk anymore), since that's where "init.scope" is located and the slice needs to be started before the scope can. To check whether we are in unified or legacy hierarchy mode we use statfs() on /sys/fs/cgroup. If the .f_type field reports tmpfs we are in legacy mode, if it reports cgroupfs we are in unified mode. This patch set carefuly makes sure that cgls and cgtop continue to work as desired. When invoking nspawn as a service it will implicitly create two subcgroups in the cgroup it is using, one to move the nspawn process into, the other to move the actual container processes into. This is done because of the requirement that cgroups may either contain processes or other subgroups.
105 lines
4.1 KiB
C
105 lines
4.1 KiB
C
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
|
|
/***
|
|
This file is part of systemd.
|
|
|
|
Copyright 2010 Lennart Poettering
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
(at your option) any later version.
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
***/
|
|
|
|
#include <unistd.h>
|
|
#include <string.h>
|
|
|
|
#include "cgroup-util.h"
|
|
#include "path-util.h"
|
|
#include "util.h"
|
|
|
|
int main(int argc, char*argv[]) {
|
|
char *path;
|
|
char *c, *p;
|
|
|
|
assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, "/test-a") == 0);
|
|
assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, "/test-a") == 0);
|
|
assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, "/test-b") == 0);
|
|
assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, "/test-b/test-c") == 0);
|
|
assert_se(cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, "/test-b", 0) == 0);
|
|
|
|
assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid(), &path) == 0);
|
|
assert_se(streq(path, "/test-b"));
|
|
free(path);
|
|
|
|
assert_se(cg_attach(SYSTEMD_CGROUP_CONTROLLER, "/test-a", 0) == 0);
|
|
|
|
assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid(), &path) == 0);
|
|
assert_se(path_equal(path, "/test-a"));
|
|
free(path);
|
|
|
|
assert_se(cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, "/test-b/test-d", 0) == 0);
|
|
|
|
assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid(), &path) == 0);
|
|
assert_se(path_equal(path, "/test-b/test-d"));
|
|
free(path);
|
|
|
|
assert_se(cg_get_path(SYSTEMD_CGROUP_CONTROLLER, "/test-b/test-d", NULL, &path) == 0);
|
|
assert_se(path_equal(path, "/sys/fs/cgroup/systemd/test-b/test-d"));
|
|
free(path);
|
|
|
|
assert_se(cg_is_empty(SYSTEMD_CGROUP_CONTROLLER, "/test-a") > 0);
|
|
assert_se(cg_is_empty(SYSTEMD_CGROUP_CONTROLLER, "/test-b") > 0);
|
|
assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-a") > 0);
|
|
assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b") == 0);
|
|
|
|
assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-a", 0, false, false, false, NULL) == 0);
|
|
assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", 0, false, false, false, NULL) > 0);
|
|
|
|
assert_se(cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", SYSTEMD_CGROUP_CONTROLLER, "/test-a", false, false) > 0);
|
|
|
|
assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-a") == 0);
|
|
assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b") > 0);
|
|
|
|
assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-a", 0, false, false, false, NULL) > 0);
|
|
assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", 0, false, false, false, NULL) == 0);
|
|
|
|
cg_trim(SYSTEMD_CGROUP_CONTROLLER, "/", false);
|
|
|
|
assert_se(cg_rmdir(SYSTEMD_CGROUP_CONTROLLER, "/test-b") < 0);
|
|
assert_se(cg_rmdir(SYSTEMD_CGROUP_CONTROLLER, "/test-a") >= 0);
|
|
|
|
assert_se(cg_split_spec("foobar:/", &c, &p) == 0);
|
|
assert_se(streq(c, "foobar"));
|
|
assert_se(streq(p, "/"));
|
|
free(c);
|
|
free(p);
|
|
|
|
assert_se(cg_split_spec("foobar:", &c, &p) < 0);
|
|
assert_se(cg_split_spec("foobar:asdfd", &c, &p) < 0);
|
|
assert_se(cg_split_spec(":///", &c, &p) < 0);
|
|
assert_se(cg_split_spec(":", &c, &p) < 0);
|
|
assert_se(cg_split_spec("", &c, &p) < 0);
|
|
assert_se(cg_split_spec("fo/obar:/", &c, &p) < 0);
|
|
|
|
assert_se(cg_split_spec("/", &c, &p) >= 0);
|
|
assert_se(c == NULL);
|
|
assert_se(streq(p, "/"));
|
|
free(p);
|
|
|
|
assert_se(cg_split_spec("foo", &c, &p) >= 0);
|
|
assert_se(streq(c, "foo"));
|
|
assert_se(p == NULL);
|
|
free(c);
|
|
|
|
return 0;
|
|
}
|