[CRIU] [PATCH v2] cg: Add ability to dump custom cgroup properties
Cyrill Gorcunov
gorcunov at openvz.org
Thu Apr 14 14:30:03 PDT 2016
We have some common predefined properties such as "cpuset.cpus" and
etc gathered in @cgp_predefined set, but there might be situation
when only predefined ones are not enough, so add ability to specify
additional properties via --cgroup-props and/or --cgroup-props-file
options.
For example one may pass
--cgroup-props "{\"controller name 1\" : [ \"property-1\", \"property-2\", ... ]}"
to dump custom properties.
At moment we require Jansson library to be present in the system to support
JSON parsing of --cgroup-props. But if someone doesn't need custom properties
lets don't force him to setup Jansson library for nothing, simply compile
criu without this feature support.
FIXME: Add documentation with example.
Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
Guys, take a look please. I think better to do that in one patch
for easier review. Now controller passed via command line overwrites
predefined one.
criu/Makefile.config | 9 ++
criu/Makefile.crtools | 1 +
criu/cgroup-props.c | 344 ++++++++++++++++++++++++++++++++++++++++++++
criu/cgroup.c | 102 ++-----------
criu/cr-dump.c | 8 ++
criu/crtools.c | 19 +++
criu/include/cgroup-props.h | 16 +++
criu/include/cr_options.h | 2 +
scripts/feature-tests.mak | 26 ++++
9 files changed, 433 insertions(+), 94 deletions(-)
create mode 100644 criu/cgroup-props.c
create mode 100644 criu/include/cgroup-props.h
diff --git a/criu/Makefile.config b/criu/Makefile.config
index 326356e1e220..ce0be7e21dbf 100644
--- a/criu/Makefile.config
+++ b/criu/Makefile.config
@@ -18,6 +18,11 @@ ifeq ($(call try-cc,$(FEATURE_TEST_UFFD)),y)
export UFFD := 1
endif
+ifeq ($(call try-cc,$(FEATURE_TEST_LIBJANSSON),-ljansson),y)
+ LIBS += -ljansson
+ JANSSON := 1
+endif
+
FEATURES_LIST := TCP_REPAIR STRLCPY STRLCAT PTRACE_PEEKSIGINFO \
SETPROCTITLE_INIT MEMFD
@@ -46,6 +51,10 @@ ifeq ($$(UFFD),1)
$(Q) @echo '#define CONFIG_HAS_UFFD' >> $$@
$(Q) @echo '' >> $$@
endif
+ifeq ($$(JANSSON),1)
+ $(Q) @echo '#define CONFIG_HAS_LIBJANSSON' >> $$@
+ $(Q) @echo '' >> $$@
+endif
ifeq ($$(piegen-y),y)
$(Q) @echo '#define CONFIG_PIEGEN' >> $$@
$(Q) @echo '' >> $$@
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..c84c9471e58e
--- /dev/null
+++ b/criu/cgroup-props.c
@@ -0,0 +1,344 @@
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "compiler.h"
+#include "cgroup-props.h"
+#include "config.h"
+#include "xmalloc.h"
+#include "util.h"
+#include "list.h"
+#include "log.h"
+#include "bug.h"
+
+#ifdef CONFIG_HAS_LIBJANSSON
+# include <jansson.h>
+#endif
+
+#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____,
+};
+
+typedef struct {
+ struct list_head list;
+ cgp_t cgp;
+} cgp_list_entry_t;
+
+static cgp_list_entry_t cgp_predefined[5];
+
+static struct list_head cgp_predefined_list = {
+ .next = &cgp_predefined[0].list,
+ .prev = &cgp_predefined[4].list,
+};
+
+static cgp_list_entry_t cgp_predefined[5] = {
+ {
+ .list.next = &cgp_predefined[1].list,
+ .list.prev = &cgp_predefined_list,
+ .cgp = {
+ .name = "cpu",
+ .nr_props = ARRAY_SIZE(cpu_props),
+ .props = cpu_props,
+ },
+ }, {
+ .list.next = &cgp_predefined[2].list,
+ .list.prev = &cgp_predefined[1].list,
+ .cgp = {
+ .name = "memory",
+ .nr_props = ARRAY_SIZE(memory_props),
+ .props = memory_props,
+ },
+ }, {
+ .list.next = &cgp_predefined[3].list,
+ .list.prev = &cgp_predefined[2].list,
+ .cgp = {
+ .name = "cpuset",
+ .nr_props = ARRAY_SIZE(cpuset_props),
+ .props = cpuset_props,
+ },
+ }, {
+ .list.next = &cgp_predefined[4].list,
+ .list.prev = &cgp_predefined[3].list,
+ .cgp = {
+ .name = "blkio",
+ .nr_props = ARRAY_SIZE(blkio_props),
+ .props = blkio_props,
+ },
+ }, {
+ .list.next = &cgp_predefined_list,
+ .list.prev = &cgp_predefined[4].list,
+ .cgp = {
+ .name = "freezer",
+ .nr_props = ARRAY_SIZE(freezer_props),
+ .props = freezer_props,
+ },
+ },
+};
+
+static LIST_HEAD(cgp_list);
+
+static void cgp_free(cgp_list_entry_t *p)
+{
+ size_t i;
+
+ if (p) {
+ for (i = 0; i < p->cgp.nr_props; i++)
+ xfree((void *)p->cgp.props[i]);
+ xfree((void *)p->cgp.name);
+ xfree((void *)p->cgp.props);
+ xfree(p);
+ }
+}
+
+static cgp_list_entry_t *cgp_get_predefined(const char *name)
+{
+ cgp_list_entry_t *p;
+
+ list_for_each_entry(p, &cgp_predefined_list, list) {
+ if (!strcmp(p->cgp.name, name))
+ return p;
+ }
+
+ return NULL;
+}
+
+#ifdef CONFIG_HAS_LIBJANSSON
+static int cgp_parse_stream(char *stream, size_t len)
+{
+ json_t *root, *controller_obj;
+ json_error_t error;
+
+ const char *controller;
+ int ret = -1;
+
+ cgp_list_entry_t *cgp_p = NULL, *predefined;
+
+ root = json_loadb(stream, len, JSON_DISABLE_EOF_CHECK, &error);
+ if (!root) {
+ pr_err("json_loadb error: %s\n", error.text);
+ goto err;
+ }
+
+ /*
+ * The format we expect here is
+ * {
+ * "controller name 1" : [ "property-1", "property-2", ... ],
+ * "controller name 2" : [ "property-1", "property-2", ... ]
+ * }
+ */
+
+ json_object_foreach(root, controller, controller_obj) {
+ json_t *v;
+ size_t i;
+
+ if (!json_is_array(controller_obj)) {
+ pr_err("Expected array props for controller %s\n", controller);
+ goto err;
+ }
+
+ if (!cgp_p) {
+ cgp_p = xzalloc(sizeof(*cgp_p));
+ if (!cgp_p)
+ goto err;
+ INIT_LIST_HEAD(&cgp_p->list);
+ }
+
+ cgp_p->cgp.name = xstrdup(controller);
+ if (!cgp_p->cgp.name)
+ goto err;
+
+ pr_debug("Parsing dynamic controller '%s'\n", controller);
+
+ if (json_array_size(controller_obj) < 1) {
+ pr_err("Expected array of props for controller %s\n", controller);
+ goto err;
+ }
+
+ cgp_p->cgp.props = xzalloc(json_array_size(controller_obj) * sizeof(cgp_p->cgp.props));
+ if (!cgp_p->cgp.props)
+ goto err;
+ cgp_p->cgp.nr_props = json_array_size(controller_obj);
+
+ for (i = 0; i < json_array_size(controller_obj); i++) {
+ v = json_array_get(controller_obj, i);
+
+ if (!json_is_string(v)) {
+ pr_err("Expected strings array props for controller %s\n", controller);
+ goto err;
+ }
+
+ pr_debug("\tproperty '%s'\n", json_string_value(v));
+
+ cgp_p->cgp.props[i] = xstrdup(json_string_value(v));
+ if (!cgp_p->cgp.props[i])
+ goto err;
+ }
+
+ predefined = cgp_get_predefined(cgp_p->cgp.name);
+ if (predefined)
+ list_del(&predefined->list);
+ list_add(&cgp_p->list, &cgp_list);
+ cgp_p = NULL;
+ }
+ ret = 0;
+
+err:
+ if (root)
+ json_decref(root);
+ cgp_free(cgp_p);
+ return ret;
+}
+
+static int cgp_parse_file(char *path)
+{
+ void *mem = MAP_FAILED;
+ int fd = -1, ret = -1;
+ struct stat st;
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0) {
+ pr_perror("Can't open file %s\n", path);
+ goto err;
+ }
+
+ if (fstat(fd, &st)) {
+ pr_perror("Can't stat file %s\n", path);
+ goto err;
+ }
+
+ mem = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FILE, fd, 0);
+ if (mem == MAP_FAILED) {
+ pr_perror("Can't mmap file %s\n", path);
+ goto err;
+ }
+
+ if (cgp_parse_stream(mem, st.st_size)) {
+ pr_err("Failed to parse file `%s'\n", path);
+ goto err;
+ }
+
+ ret = 0;
+err:
+ if (mem != MAP_FAILED)
+ munmap(mem, st.st_size);
+ close_safe(&fd);
+ return ret;
+}
+#else /* CONFIG_HAS_LIBJANSSON */
+static int cgp_parse_stream(char *stream, size_t len)
+{
+ pr_err("Compiled without JSON parser support.\n");
+ return -1;
+}
+static int cgp_parse_file(char *path)
+{
+ return cgp_parse_stream(NULL, 0);
+}
+#endif /* CONFIG_HAS_LIBJANSSON */
+
+int cgp_init(char *stream, size_t len, char *path)
+{
+ int ret = 0;
+
+ if (stream && len) {
+ ret = cgp_parse_stream(stream, len);
+ if (ret)
+ goto err;
+ }
+
+ if (path)
+ ret = cgp_parse_file(path);
+err:
+ return ret;
+}
+
+const cgp_t *cgp_get_props(const char *name)
+{
+ cgp_list_entry_t *p;
+
+ p = cgp_get_predefined(name);
+ if (p)
+ return &p->cgp;
+
+ list_for_each_entry(p, &cgp_list, list) {
+ if (!strcmp(p->cgp.name, name))
+ return &p->cgp;
+ }
+
+ return NULL;
+}
+
+void cgp_fini(void)
+{
+ cgp_list_entry_t *p, *t;
+
+ list_for_each_entry_safe(p, t, &cgp_list, list)
+ cgp_free(p);
+ INIT_LIST_HEAD(&cgp_list);
+}
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/cr-dump.c b/criu/cr-dump.c
index 5ac9fd041e4e..b5531e43d115 100644
--- a/criu/cr-dump.c
+++ b/criu/cr-dump.c
@@ -61,6 +61,7 @@
#include "cpu.h"
#include "elf.h"
#include "cgroup.h"
+#include "cgroup-props.h"
#include "file-lock.h"
#include "page-xfer.h"
#include "kerndat.h"
@@ -1554,6 +1555,7 @@ static int cr_dump_finish(int ret)
ret = -1;
cr_plugin_fini(CR_PLUGIN_STAGE__DUMP, ret);
+ cgp_fini();
if (!ret) {
/*
@@ -1653,6 +1655,12 @@ int cr_dump_tasks(pid_t pid)
if (vdso_init())
goto err;
+ if (cgp_init(opts.cgroup_props,
+ opts.cgroup_props ?
+ strlen(opts.cgroup_props) : 0,
+ opts.cgroup_props_file))
+ goto err;
+
if (parse_cg_info())
goto err;
diff --git a/criu/crtools.c b/criu/crtools.c
index 5d9647ad25c8..b8b457a2f69a 100644
--- a/criu/crtools.c
+++ b/criu/crtools.c
@@ -24,6 +24,7 @@
#include "compiler.h"
#include "crtools.h"
#include "cr_options.h"
+#include "config.h"
#include "sockets.h"
#include "files.h"
#include "sk-inet.h"
@@ -38,6 +39,7 @@
#include "mount.h"
#include "namespaces.h"
#include "cgroup.h"
+#include "cgroup-props.h"
#include "cpu.h"
#include "action-scripts.h"
#include "irmap.h"
@@ -323,6 +325,8 @@ int main(int argc, char *argv[], char *envp[])
{ "extra", no_argument, 0, 1077 },
{ "experimental", no_argument, 0, 1078 },
{ "all", no_argument, 0, 1079 },
+ { "cgroup-props", required_argument, 0, 1080 },
+ { "cgroup-props-file", required_argument, 0, 1081 },
{ },
};
@@ -627,6 +631,12 @@ int main(int argc, char *argv[], char *envp[])
opts.check_extra_features = true;
opts.check_experimental_features = true;
break;
+ case 1080:
+ opts.cgroup_props = optarg;
+ break;
+ case 1081:
+ opts.cgroup_props_file = optarg;
+ break;
case 'V':
pr_msg("Version: %s\n", CRIU_VERSION);
if (strcmp(CRIU_GITID, "0"))
@@ -882,6 +892,15 @@ usage:
" change the root cgroup the controller will be\n"
" installed into. No controller means that root is the\n"
" default for all controllers not specified.\n"
+#ifdef CONFIG_HAS_LIBJANSSON
+" --cgroup-props STRING\n"
+" define cgroup controllers and properties\n"
+" to be checkpointed, which are described\n"
+" via STRING using JSON format.\n"
+" --cgroup-props-file FILE\n"
+" same as --cgroup-props but taking descrition\n"
+" from the path specified.\n"
+#endif
" --skip-mnt PATH ignore this mountpoint when dumping the mount namespace.\n"
" --enable-fs FSNAMES a comma separated list of filesystem names or \"all\".\n"
" force criu to (try to) dump/restore these filesystem's\n"
diff --git a/criu/include/cgroup-props.h b/criu/include/cgroup-props.h
new file mode 100644
index 000000000000..abd315dbc081
--- /dev/null
+++ b/criu/include/cgroup-props.h
@@ -0,0 +1,16 @@
+#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);
+
+extern int cgp_init(char *stream, size_t len, char *path);
+extern void cgp_fini(void);
+
+#endif /* __CR_CGROUP_PROPS_H__ */
diff --git a/criu/include/cr_options.h b/criu/include/cr_options.h
index b6ae3a146b8d..19b45a4a3d11 100644
--- a/criu/include/cr_options.h
+++ b/criu/include/cr_options.h
@@ -97,6 +97,8 @@ struct cr_options {
char **exec_cmd;
unsigned int manage_cgroups;
char *new_global_cg_root;
+ char *cgroup_props;
+ char *cgroup_props_file;
struct list_head new_cgroup_roots;
bool autodetect_ext_mounts;
bool enable_external_sharing;
diff --git a/scripts/feature-tests.mak b/scripts/feature-tests.mak
index c48b52e7a7ad..55fe12c3d640 100644
--- a/scripts/feature-tests.mak
+++ b/scripts/feature-tests.mak
@@ -105,3 +105,29 @@ int main(void)
}
endef
+
+define FEATURE_TEST_LIBJANSSON
+
+#include <jansson.h>
+
+int main(int argc, char *argv[], char *envp[])
+{
+ json_error_t error;
+ const char *v;
+ json_t *obj;
+
+ (void)json_loadb(v, 0, 0, &error);
+ json_object_foreach(obj, v, obj);
+ (void)json_is_array(obj);
+ (void)json_array_size(obj);
+ (void)json_array_get(obj, 0);
+ (void)json_is_string(obj);
+ (void)json_decref(obj);
+
+ (void)error;
+ (void)v;
+ (void)obj;
+
+ return 0;
+}
+endef
--
2.5.5
More information about the CRIU
mailing list