cgroup v2介绍及测试

参考:
https://blog.csdn.net/yevvzi/article/details/103374504

即: 在内核启动参数里设置cgroup_no_v1=all

以下内容来自Documentation\admin-guide\kernel-parameters.txt:

	cgroup_disable=	[KNL] Disable a particular controller Format: {name of the controller(s) to disable}
			The effects of cgroup_disable=foo are:
			- foo isn't auto-mounted if you mount all cgroups in a single hierarchy
			- foo isn't visible as an individually mountable subsystem {Currently only "memory" controller deal with this and
			cut the overhead, others just disable the usage. So
			only cgroup_disable=memory is actually worthy}
	cgroup_no_v1=	[KNL] Disable cgroup controllers and named hierarchies in v1 Format: { { controller | "all" | "named" } [,{ controller | "all" | "named" }...] } Like cgroup_disable, but only applies to cgroup v1;
			the blacklisted controllers remain available in cgroup2.
			"all" blacklists all controllers and "named" disables
			named mounts. Specifying both "all" and "named" disables
			all v1 hierarchies.
	cgroup.memory=	[KNL] Pass options to the cgroup memory controller. Format: <string>
			nosocket -- Disable socket memory accounting.
			nokmem -- Disable kernel memory accounting.

下面是内核中对这个参数的解析:kernel\cgroup\cgroup-v1.c

static int __init cgroup_no_v1(char *str)
{
	struct cgroup_subsys *ss;
	char *token;
	int i;

	while ((token = strsep(&str, ",")) != NULL) {
		if (!*token)
			continue;

		if (!strcmp(token, "all")) {
			cgroup_no_v1_mask = U16_MAX;
			continue;
		}

		if (!strcmp(token, "named")) {
			cgroup_no_v1_named = true;
			continue;
		}

		for_each_subsys(ss, i) {
			if (strcmp(token, ss->name) &&
			    strcmp(token, ss->legacy_name))
				continue;

			cgroup_no_v1_mask |= 1 << i;
		}
	}
	return 1;
}
__setup("cgroup_no_v1=", cgroup_no_v1);

代码中会通过函数cgroup1_ssid_disabled来判断某个cgroup子系统是否在cgroupv1里被关闭了:

bool cgroup1_ssid_disabled(int ssid)
{
	return cgroup_no_v1_mask & (1 << ssid);
}

此外,systemd在启动时会解析/proc/cmdline,如果发现cgroup_no_v1被设置为了 "all" ,那么就不会去挂载cgroupv1文件系统了:
参考:https://www.cnblogs.com/pengdonglin137/p/16150827.html

bool cg_is_unified_wanted(void) {
        static thread_local int wanted = -1;
        bool b;
        const bool is_default = DEFAULT_HIERARCHY == CGROUP_UNIFIED_ALL;
        _cleanup_free_ char *c = NULL;
        int r;

        /* If we have a cached value, return that. */
        if (wanted >= 0)
                return wanted;

        /* If the hierarchy is already mounted, then follow whatever was chosen for it. */
        r = cg_unified_cached(true);
        if (r >= 0)
                return (wanted = r >= CGROUP_UNIFIED_ALL);

        /* If we were explicitly passed systemd.unified_cgroup_hierarchy, respect that. */
        r = proc_cmdline_get_bool("systemd.unified_cgroup_hierarchy", &b);
        if (r > 0)
                return (wanted = b);

        /* If we passed cgroup_no_v1=all with no other instructions, it seems highly unlikely that we want to
         * use hybrid or legacy hierarchy. */
        r = proc_cmdline_get_key("cgroup_no_v1", 0, &c);
        if (r > 0 && streq_ptr(c, "all"))
                return (wanted = true);

        /* If any controller is in use as v1, don't use unified. */
        if (cg_any_controller_used_for_v1() > 0)
                return (wanted = false);

        return (wanted = is_default);
}
posted @ 2022-04-19 16:38  dolinux  阅读(760)  评论(0)    收藏  举报