[CRIU] [PATCH 1/2] cg: properties -- Move properties handling into separate file

Cyrill Gorcunov gorcunov at openvz.org
Tue Apr 12 02:40:39 PDT 2016


Will need it to support dynamic properties.

Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 criu/Makefile.crtools       |   1 +
 criu/cgroup-props.c         | 111 ++++++++++++++++++++++++++++++++++++++++++++
 criu/cgroup.c               | 102 ++++------------------------------------
 criu/include/cgroup-props.h |  13 ++++++
 4 files changed, 133 insertions(+), 94 deletions(-)
 create mode 100644 criu/cgroup-props.c
 create mode 100644 criu/include/cgroup-props.h

diff --git a/criu/Makefile.crtools b/criu/Makefile.crtools
index d610d2f3375f..1bfb799796f3 100644
--- a/criu/Makefile.crtools
+++ b/criu/Makefile.crtools
@@ -5,6 +5,7 @@ obj-y			+= aio.o
 obj-y			+= bfd.o
 obj-y			+= bitmap.o
 obj-y			+= cgroup.o
+obj-y			+= cgroup-props.o
 obj-y			+= cr-check.o
 obj-y			+= cr-dedup.o
 obj-y			+= cr-dump.o
diff --git a/criu/cgroup-props.c b/criu/cgroup-props.c
new file mode 100644
index 000000000000..2f6ed2594cb3
--- /dev/null
+++ b/criu/cgroup-props.c
@@ -0,0 +1,111 @@
+#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 char *____criu_global_props____[] = {
+	"cgroup.clone_children",
+	"notify_on_release",
+	"cgroup.procs",
+	"tasks",
+};
+
+cgp_t cgp_global = {
+	.name		= "____criu_global_props____",
+	.nr_props	= ARRAY_SIZE(____criu_global_props____),
+	.props		= ____criu_global_props____,
+};
+
+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/criu/cgroup.c b/criu/cgroup.c
index e1d50230360c..7f41aae09b06 100644
--- a/criu/cgroup.c
+++ b/criu/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"
@@ -25,73 +26,6 @@
 #include "images/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
-};
-
-static const char *global_props[] = {
-	"cgroup.clone_children",
-	"notify_on_release",
-	"cgroup.procs",
-	"tasks",
-	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
@@ -421,33 +355,14 @@ 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 dump_cg_props_array(const char *fpath, struct cgroup_dir *ncd,
-			       const char **prop_arr)
+static int dump_cg_props_array(const char *fpath, struct cgroup_dir *ncd, const cgp_t *cgp)
 {
 	int j;
 	char buf[PATH_MAX];
 	struct cgroup_prop *prop;
 
-	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\n");
 			return -1;
 		}
@@ -457,7 +372,7 @@ static int dump_cg_props_array(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;
@@ -483,15 +398,14 @@ static int add_cgroup_properties(const char *fpath, struct cgroup_dir *ncd,
 	int i;
 
 	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]);
-
-		if (dump_cg_props_array(fpath, ncd, prop_arr) < 0) {
+		if (dump_cg_props_array(fpath, ncd, cgp) < 0) {
 			pr_err("dumping known properties failed");
 			return -1;
 		}
 
-		if (dump_cg_props_array(fpath, ncd, global_props) < 0) {
+		if (dump_cg_props_array(fpath, ncd, &cgp_global) < 0) {
 			pr_err("dumping global properties failed");
 			return -1;
 		}
diff --git a/criu/include/cgroup-props.h b/criu/include/cgroup-props.h
new file mode 100644
index 000000000000..1259e0cdc27f
--- /dev/null
+++ b/criu/include/cgroup-props.h
@@ -0,0 +1,13 @@
+#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 cgp_t cgp_global;
+extern const cgp_t *cgp_get_props(const char *name);
+
+#endif /* __CR_CGROUP_PROPS_H__ */
-- 
2.5.5



More information about the CRIU mailing list