[CRIU] [RFC 1/2] cgroup: properties -- Move properties handling into separate file

Cyrill Gorcunov gorcunov at openvz.org
Thu Apr 2 00:14:31 PDT 2015


We will support dynamic properties definitions later.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 Makefile.crtools       |  1 +
 cgroup-props.c         | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++
 cgroup.c               | 87 +++-----------------------------------------
 include/cgroup-props.h | 12 +++++++
 4 files changed, 116 insertions(+), 82 deletions(-)
 create mode 100644 cgroup-props.c
 create mode 100644 include/cgroup-props.h

diff --git a/Makefile.crtools b/Makefile.crtools
index 650b9b0c3b04..4345b162e40a 100644
--- a/Makefile.crtools
+++ b/Makefile.crtools
@@ -60,6 +60,7 @@ obj-y	+= pagemap-cache.o
 obj-y	+= kerndat.o
 obj-y	+= stats.o
 obj-y	+= cgroup.o
+obj-y	+= cgroup-props.o
 obj-y	+= timerfd.o
 obj-y	+= aio.o
 obj-y	+= string.o
diff --git a/cgroup-props.c b/cgroup-props.c
new file mode 100644
index 000000000000..08338667e012
--- /dev/null
+++ b/cgroup-props.c
@@ -0,0 +1,98 @@
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "compiler.h"
+#include "cgroup-props.h"
+
+#undef	LOG_PREFIX
+#define LOG_PREFIX "cg-prop: "
+
+/*
+ * Predefined properties.
+ */
+static const char *cpu_props[] = {
+	"cpu.shares",
+	"cpu.cfs_period_us",
+	"cpu.cfs_quota_us",
+	"cpu.rt_period_us",
+	"cpu.rt_runtime_us",
+	"notify_on_release",
+};
+
+static const char *memory_props[] = {
+	/* limit_in_bytes and memsw.limit_in_bytes must be set in this order */
+	"memory.limit_in_bytes",
+	"memory.memsw.limit_in_bytes",
+	"memory.use_hierarchy",
+	"notify_on_release",
+};
+
+static const char *cpuset_props[] = {
+	/*
+	 * cpuset.cpus and cpuset.mems must be set before the process moves
+	 * into its cgroup; they are "initialized" below to whatever the root
+	 * values are in copy_special_cg_props so as not to cause ENOSPC when
+	 * values are restored via this code.
+	 */
+	"cpuset.cpus",
+	"cpuset.mems",
+	"cpuset.memory_migrate",
+	"cpuset.cpu_exclusive",
+	"cpuset.mem_exclusive",
+	"cpuset.mem_hardwall",
+	"cpuset.memory_spread_page",
+	"cpuset.memory_spread_slab",
+	"cpuset.sched_load_balance",
+	"cpuset.sched_relax_domain_level",
+	"notify_on_release",
+};
+
+static const char *blkio_props[] = {
+	"blkio.weight",
+	"notify_on_release",
+};
+
+static const char *freezer_props[] = {
+	"notify_on_release",
+};
+
+static const cgp_t cgp_predefined[] = {
+	{
+		.name		= "cpu",
+		.nr_props	= ARRAY_SIZE(cpu_props),
+		.props		= cpu_props,
+	},
+	{
+		.name		= "memory",
+		.nr_props	= ARRAY_SIZE(memory_props),
+		.props		= memory_props,
+	},
+	{
+		.name		= "cpuset",
+		.nr_props	= ARRAY_SIZE(cpuset_props),
+		.props		= cpuset_props,
+	},
+	{
+		.name		= "blkio",
+		.nr_props	= ARRAY_SIZE(blkio_props),
+		.props		= blkio_props,
+	},
+	{
+		.name		= "freezer",
+		.nr_props	= ARRAY_SIZE(freezer_props),
+		.props		= freezer_props,
+	},
+};
+
+const cgp_t *cgp_get_props(const char *name)
+{
+	size_t i;
+
+	for (i = 0; i < ARRAY_SIZE(cgp_predefined); i++) {
+		if (!strcmp(cgp_predefined[i].name, name))
+			return &cgp_predefined[i];
+	}
+
+	return NULL;
+}
diff --git a/cgroup.c b/cgroup.c
index b1a2f927c863..a15d074ff938 100644
--- a/cgroup.c
+++ b/cgroup.c
@@ -10,6 +10,7 @@
 #include "list.h"
 #include "xmalloc.h"
 #include "cgroup.h"
+#include "cgroup-props.h"
 #include "cr_options.h"
 #include "pstree.h"
 #include "proc_parse.h"
@@ -21,65 +22,6 @@
 #include "protobuf/cgroup.pb-c.h"
 
 /*
- * These string arrays have the names of all the properties that will be
- * restored. To add a property for a cgroup type, add it to the
- * corresponding char array above the NULL terminator. If you are adding
- * a new cgroup family all together, you must also edit get_known_properties()
- * Currently the code only supports properties with 1 value
- */
-
-static const char *cpu_props[] = {
-	"cpu.shares",
-	"cpu.cfs_period_us",
-	"cpu.cfs_quota_us",
-	"cpu.rt_period_us",
-	"cpu.rt_runtime_us",
-	"notify_on_release",
-	NULL
-};
-
-static const char *memory_props[] = {
-	/* limit_in_bytes and memsw.limit_in_bytes must be set in this order */
-	"memory.limit_in_bytes",
-	"memory.memsw.limit_in_bytes",
-	"memory.use_hierarchy",
-	"notify_on_release",
-	NULL
-};
-
-static const char *cpuset_props[] = {
-	/*
-	 * cpuset.cpus and cpuset.mems must be set before the process moves
-	 * into its cgroup; they are "initialized" below to whatever the root
-	 * values are in copy_special_cg_props so as not to cause ENOSPC when
-	 * values are restored via this code.
-	 */
-	"cpuset.cpus",
-	"cpuset.mems",
-	"cpuset.memory_migrate",
-	"cpuset.cpu_exclusive",
-	"cpuset.mem_exclusive",
-	"cpuset.mem_hardwall",
-	"cpuset.memory_spread_page",
-	"cpuset.memory_spread_slab",
-	"cpuset.sched_load_balance",
-	"cpuset.sched_relax_domain_level",
-	"notify_on_release",
-	NULL
-};
-
-static const char *blkio_props[] = {
-	"blkio.weight",
-	"notify_on_release",
-	NULL
-};
-
-static const char *freezer_props[] = {
-	"notify_on_release",
-	NULL
-};
-
-/*
  * This structure describes set of controller groups
  * a task lives in. The cg_ctl entries are stored in
  * the @ctls list sorted by the .name field and then
@@ -372,24 +314,6 @@ static void free_all_cgroup_props(struct cgroup_dir *ncd)
 	ncd->n_properties = 0;
 }
 
-static const char **get_known_properties(char *controller)
-{
-	const char **prop_arr = NULL;
-
-	if (!strcmp(controller, "cpu"))
-		prop_arr = cpu_props;
-	else if (!strcmp(controller, "memory"))
-		prop_arr = memory_props;
-	else if (!strcmp(controller, "cpuset"))
-		prop_arr = cpuset_props;
-	else if (!strcmp(controller, "blkio"))
-		prop_arr = blkio_props;
-	else if (!strcmp(controller, "freezer"))
-		prop_arr = freezer_props;
-
-	return prop_arr;
-}
-
 static int add_cgroup_properties(const char *fpath, struct cgroup_dir *ncd,
 				 struct cg_controller *controller)
 {
@@ -398,11 +322,10 @@ static int add_cgroup_properties(const char *fpath, struct cgroup_dir *ncd,
 	struct cgroup_prop *prop;
 
 	for (i = 0; i < controller->n_controllers; ++i) {
+		const cgp_t *cgp = cgp_get_props(controller->controllers[i]);
 
-		const char **prop_arr = get_known_properties(controller->controllers[i]);
-
-		for (j = 0; prop_arr != NULL && prop_arr[j] != NULL; ++j) {
-			if (snprintf(buf, PATH_MAX, "%s/%s", fpath, prop_arr[j]) >= PATH_MAX) {
+		for (j = 0; cgp && j < cgp->nr_props; j++) {
+			if (snprintf(buf, PATH_MAX, "%s/%s", fpath, cgp->props[j]) >= PATH_MAX) {
 				pr_err("snprintf output was truncated");
 				return -1;
 			}
@@ -412,7 +335,7 @@ static int add_cgroup_properties(const char *fpath, struct cgroup_dir *ncd,
 				continue;
 			}
 
-			prop = create_cgroup_prop(prop_arr[j]);
+			prop = create_cgroup_prop(cgp->props[j]);
 			if (!prop) {
 				free_all_cgroup_props(ncd);
 				return -1;
diff --git a/include/cgroup-props.h b/include/cgroup-props.h
new file mode 100644
index 000000000000..2b87275122a7
--- /dev/null
+++ b/include/cgroup-props.h
@@ -0,0 +1,12 @@
+#ifndef __CR_CGROUP_PROPS_H__
+#define __CR_CGROUP_PROPS_H__
+
+typedef struct {
+	const char	*name;
+	size_t		nr_props;
+	const char	**props;
+} cgp_t;
+
+extern const cgp_t *cgp_get_props(const char *name);
+
+#endif /* __CR_CGROUP_PROPS_H__ */
-- 
1.9.3



More information about the CRIU mailing list