]> xenbits.xensource.com Git - libvirt.git/commitdiff
Introduce the function virCgroupMoveTask
authorHu Tao <hutao@cn.fujitsu.com>
Tue, 21 Aug 2012 09:18:25 +0000 (17:18 +0800)
committerDaniel Veillard <veillard@redhat.com>
Wed, 22 Aug 2012 06:33:28 +0000 (14:33 +0800)
Introduce a new API to move tasks of one controller from a cgroup to another cgroup

Signed-off-by: Wen Congyang <wency@cn.fujitsu.com>
Signed-off-by: Tang Chen <tangchen@cn.fujitsu.com>
Signed-off-by: Hu Tao <hutao@cn.fujitsu.com>
src/libvirt_private.syms
src/util/cgroup.c
src/util/cgroup.h

index a51aa91e9d5ab5993526efc93a0a0b05412352f3..b48fff0be016fea129e18274ab80ac664032a62d 100644 (file)
@@ -60,6 +60,7 @@ virCapabilitiesSetMacPrefix;
 
 # cgroup.h
 virCgroupAddTask;
+virCgroupAddTaskController;
 virCgroupAllowDevice;
 virCgroupAllowDeviceMajor;
 virCgroupAllowDevicePath;
@@ -91,6 +92,7 @@ virCgroupKill;
 virCgroupKillPainfully;
 virCgroupKillRecursive;
 virCgroupMounted;
+virCgroupMoveTask;
 virCgroupPathOfController;
 virCgroupRemove;
 virCgroupSetBlkioDeviceWeight;
index 169b56a37b69d154f99b431127dc90e79abe16e9..470fc5dd71dc14ccfed6b6ecdf594f122290a1e8 100644 (file)
@@ -802,6 +802,115 @@ int virCgroupAddTask(virCgroupPtr group, pid_t pid)
     return rc;
 }
 
+/**
+ * virCgroupAddTaskController:
+ *
+ * @group: The cgroup to add a task to
+ * @pid: The pid of the task to add
+ * @controller: The cgroup controller to be operated on
+ *
+ * Returns: 0 on success or -errno on failure
+ */
+int virCgroupAddTaskController(virCgroupPtr group, pid_t pid, int controller)
+{
+    if (controller < 0 || controller > VIR_CGROUP_CONTROLLER_LAST)
+        return -EINVAL;
+
+    if (!group->controllers[controller].mountPoint)
+        return -EINVAL;
+
+    return virCgroupSetValueU64(group, controller, "tasks",
+                                (unsigned long long)pid);
+}
+
+
+static int virCgroupAddTaskStrController(virCgroupPtr group,
+                                        const char *pidstr,
+                                        int controller)
+{
+    char *str = NULL, *cur = NULL, *next = NULL;
+    unsigned long long p = 0;
+    int rc = 0;
+    char *endp;
+
+    if (virAsprintf(&str, "%s", pidstr) < 0)
+        return -1;
+
+    cur = str;
+    while (*cur != '\0') {
+        rc = virStrToLong_ull(cur, &endp, 10, &p);
+        if (rc != 0)
+            goto cleanup;
+
+        rc = virCgroupAddTaskController(group, p, controller);
+        if (rc != 0)
+            goto cleanup;
+
+        next = strchr(cur, '\n');
+        if (next) {
+            cur = next + 1;
+            *next = '\0';
+        } else {
+            break;
+        }
+    }
+
+cleanup:
+    VIR_FREE(str);
+    return rc;
+}
+
+/**
+ * virCgroupMoveTask:
+ *
+ * @src_group: The source cgroup where all tasks are removed from
+ * @dest_group: The destination where all tasks are added to
+ * @controller: The cgroup controller to be operated on
+ *
+ * Returns: 0 on success or -errno on failure
+ */
+int virCgroupMoveTask(virCgroupPtr src_group, virCgroupPtr dest_group,
+                      int controller)
+{
+    int rc = 0, err = 0;
+    char *content = NULL;
+
+    if (controller < VIR_CGROUP_CONTROLLER_CPU ||
+        controller > VIR_CGROUP_CONTROLLER_BLKIO)
+        return -EINVAL;
+
+    if (!src_group->controllers[controller].mountPoint ||
+        !dest_group->controllers[controller].mountPoint) {
+        VIR_WARN("no vm cgroup in controller %d", controller);
+        return 0;
+    }
+
+    rc = virCgroupGetValueStr(src_group, controller, "tasks", &content);
+    if (rc != 0)
+        return rc;
+
+    rc = virCgroupAddTaskStrController(dest_group, content, controller);
+    if (rc != 0)
+        goto cleanup;
+
+    VIR_FREE(content);
+
+    return 0;
+
+cleanup:
+    /*
+     * We don't need to recover dest_cgroup because cgroup will make sure
+     * that one task only resides in one cgroup of the same controller.
+     */
+    err = virCgroupAddTaskStrController(src_group, content, controller);
+    if (err != 0)
+        VIR_ERROR(_("Cannot recover cgroup %s from %s"),
+                  src_group->controllers[controller].mountPoint,
+                  dest_group->controllers[controller].mountPoint);
+    VIR_FREE(content);
+
+    return rc;
+}
 
 /**
  * virCgroupForDriver:
index 9f803a511154e764fc64614f5617b2bd21c32c49..727e5367d06c5ef5995cb442430d6d5b26dce700 100644 (file)
@@ -70,6 +70,14 @@ int virCgroupPathOfController(virCgroupPtr group,
 
 int virCgroupAddTask(virCgroupPtr group, pid_t pid);
 
+int virCgroupAddTaskController(virCgroupPtr group,
+                               pid_t pid,
+                               int controller);
+
+int virCgroupMoveTask(virCgroupPtr src_group,
+                      virCgroupPtr dest_group,
+                      int controller);
+
 int virCgroupSetBlkioWeight(virCgroupPtr group, unsigned int weight);
 int virCgroupGetBlkioWeight(virCgroupPtr group, unsigned int *weight);