introduce a new API to move all tasks from a cgroup to another cgroup
---
src/util/cgroup.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++
src/util/cgroup.h | 2 +
2 files changed, 57 insertions(+), 0 deletions(-)
diff --git a/src/util/cgroup.c b/src/util/cgroup.c
index 901a841..724cc6e 100644
--- a/src/util/cgroup.c
+++ b/src/util/cgroup.c
@@ -789,6 +789,61 @@ int virCgroupAddTask(virCgroupPtr group, pid_t pid)
return rc;
}
+static int virCgrouAddTaskStr(virCgroupPtr group, const char *pidstr)
+{
+ unsigned long long value;
+
+ if (virStrToLong_ull(pidstr, NULL, 10, &value) < 0)
+ return -EINVAL;
+
+ return virCgroupAddTask(group, value);
+}
+
+/**
+ * virCgroupMoveTask:
+ *
+ * @src_group: The source cgroup where all tasks are removed from
+ * @dest_group: The destination where all tasks are added to
+ *
+ * Returns: 0 on success
+ */
+int virCgroupMoveTask(virCgroupPtr src_group, virCgroupPtr dest_group)
+{
+ int rc = 0;
+ int i;
+ char *content, *value, *next;
+
+ for (i = 0 ; i < VIR_CGROUP_CONTROLLER_LAST ; i++) {
+ /* Skip over controllers not mounted */
+ if (!src_group->controllers[i].mountPoint ||
+ !dest_group->controllers[i].mountPoint)
+ continue;
+
+ rc = virCgroupGetValueStr(src_group, i, "tasks", &content);
+ if (rc != 0)
+ break;
+
+ value = content;
+ while((next = strchr(value, '\n')) != NULL) {
+ *next = '\0';
+ if ((rc = virCgrouAddTaskStr(dest_group, value) < 0))
+ goto cleanup;
+ value = next + 1;
+ }
+ if (*value != '\0') {
+ if ((rc = virCgrouAddTaskStr(dest_group, value) < 0))
+ goto cleanup;
+ }
+
+ VIR_FREE(content);
+ }
+
+ return 0;
+
+cleanup:
+ virCgroupMoveTask(dest_group, src_group);
+ return rc;
+}
/**
* virCgroupForDriver:
diff --git a/src/util/cgroup.h b/src/util/cgroup.h
index 34fcbf5..2419ef6 100644
--- a/src/util/cgroup.h
+++ b/src/util/cgroup.h
@@ -56,6 +56,8 @@ int virCgroupPathOfController(virCgroupPtr group,
int virCgroupAddTask(virCgroupPtr group, pid_t pid);
+int virCgroupMoveTask(virCgroupPtr src_group, virCgroupPtr dest_group);
+
int virCgroupSetBlkioWeight(virCgroupPtr group, unsigned int weight);
int virCgroupGetBlkioWeight(virCgroupPtr group, unsigned int *weight);
--
1.7.1