On Tue, Aug 21, 2012 at 02:43:52PM +0800, Hu Tao wrote:
Introduce a new API to move tasks of one controller from a cgroup to
another cgroup
Signed-off-by: Wen Congyang <wency(a)cn.fujitsu.com>
Signed-off-by: Tang Chen <tangchen(a)cn.fujitsu.com>
Signed-off-by: Hu Tao <hutao(a)cn.fujitsu.com>
---
src/libvirt_private.syms | 2 +
src/util/cgroup.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++
src/util/cgroup.h | 8 ++++
3 files changed, 119 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 681f526..0934dba 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -60,6 +60,7 @@ virCapabilitiesSetMacPrefix;
# cgroup.h
virCgroupAddTask;
+virCgroupAddTaskController;
virCgroupAllowDevice;
virCgroupAllowDeviceMajor;
virCgroupAllowDevicePath;
@@ -91,6 +92,7 @@ virCgroupKill;
virCgroupKillPainfully;
virCgroupKillRecursive;
virCgroupMounted;
+virCgroupMoveTask;
virCgroupPathOfController;
virCgroupRemove;
virCgroupSetBlkioDeviceWeight;
diff --git a/src/util/cgroup.c b/src/util/cgroup.c
index 169b56a..374516a 100644
--- a/src/util/cgroup.c
+++ b/src/util/cgroup.c
@@ -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 < VIR_CGROUP_CONTROLLER_CPU ||
+ controller > VIR_CGROUP_CONTROLLER_BLKIO)
+ 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;
+
+ if (virAsprintf(&str, "%s", pidstr) < 0)
+ return -1;
+
+ cur = str;
+ while (*cur != '\0') {
+ rc = virStrToLong_ull(cur, NULL, 10, &p);
The terminating '\n' in str will cause a failure here.
It can be fixed by(can you squash this in?):
diff --git a/src/util/cgroup.c b/src/util/cgroup.c
index 840a952..1808911 100644
--- a/src/util/cgroup.c
+++ b/src/util/cgroup.c
@@ -833,13 +833,14 @@ static int virCgroupAddTaskStrController(virCgroupPtr group,
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, NULL, 10, &p);
+ rc = virStrToLong_ull(cur, &endp, 10, &p);
if (rc != 0)
goto cleanup;
+ 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;
+}
+
--
Thanks,
Hu Tao