On Tue, Jul 10, 2012 at 05:13:15PM +0800, tangchen wrote: > Introduce a new API to move tasks of one controller from a cgroup to another cgroup > > Signed-off-by: Wen Congyang <wency@xxxxxxxxxxxxxx> > --- > src/libvirt_private.syms | 2 + > src/util/cgroup.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++ > src/util/cgroup.h | 8 +++ > 3 files changed, 137 insertions(+) > > diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms > index 8925267..812cf1d 100644 > --- a/src/libvirt_private.syms > +++ b/src/libvirt_private.syms > @@ -60,6 +60,7 @@ virCapabilitiesSetMacPrefix; > > # cgroup.h > virCgroupAddTask; > +virCgroupAddTaskController; > virCgroupAllowDeviceMajor; > virCgroupAllowDevicePath; > virCgroupControllerTypeFromString; > @@ -88,6 +89,7 @@ virCgroupKill; > virCgroupKillPainfully; > virCgroupKillRecursive; > virCgroupMounted; > +virCgroupMoveTask; > virCgroupPathOfController; > virCgroupRemove; > virCgroupSetBlkioDeviceWeight; > diff --git a/src/util/cgroup.c b/src/util/cgroup.c > index 1ac8278..3e9ba4e 100644 > --- a/src/util/cgroup.c > +++ b/src/util/cgroup.c > @@ -791,6 +791,133 @@ 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) > +{ > + int rc = 0; unused variable. > + > + if (controller < VIR_CGROUP_CONTROLLER_CPU || > + controller > VIR_CGROUP_CONTROLLER_BLKIO) This can be improved by: VIR_CGROUP_CONTROLLER_IS_VALID(controller) > + return -EINVAL; > + > + if (!group->controllers[controller].mountPoint) > + return -EINVAL; > + > + return virCgroupSetValueU64(group, controller, "tasks", > + (unsigned long long)pid); > +} > + > +static int virCgroupAddTaskStr(virCgroupPtr group, const char *pidstr) > +{ > + unsigned long long value; > + > + if (virStrToLong_ull(pidstr, NULL, 10, &value) < 0) > + return -EINVAL; > + > + return virCgroupAddTask(group, value); > +} Unused static function can be removed harmlessly. > + > +static int virCgroupAddTaskStrController(virCgroupPtr group, > + const char *pidstr, > + int controller) > +{ > + char *str = NULL, *cur = NULL, *next = NULL; > + unsigned long long pid = 0; > + int len = 0, rc = 0; > + > + len = strlen(pidstr); > + VIR_ALLOC_N(str, len); > + if (str == NULL) { > + VIR_ERROR(_("No more memory.")); > + return -1; > + } > + rc = strcpy(str, pidstr); > + if (rc != 0) > + return rc; virAsprintf > + > + cur = str; > + while ((next = strchr(cur, '\n')) != NULL) { > + *next = '\0'; > + rc = virStrToLong_ull(cur, NULL, 10, &pid); > + if (rc != 0) > + goto cleanup; > + cur = next + 1; > + > + rc = virCgroupAddTaskController(group, pid, controller); > + if (rc != 0) > + goto cleanup; > + } > + if (cur != '\0') { > + rc = virStrToLong_ull(cur, NULL, 10, &pid); > + if (rc != 0) > + goto cleanup; > + rc = virCgroupAddTaskController(group, pid, controller); > + if (rc != 0) > + goto cleanup; > + } > + > +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) > + return -EINVAL; > + > + 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: > diff --git a/src/util/cgroup.h b/src/util/cgroup.h > index 315ebd6..f14c167 100644 > --- a/src/util/cgroup.h > +++ b/src/util/cgroup.h > @@ -58,6 +58,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); > > -- > 1.7.10.2 > > -- > libvir-list mailing list > libvir-list@xxxxxxxxxx > https://www.redhat.com/mailman/listinfo/libvir-list -- Thanks, Hu Tao -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list