Both qemu and lxc drivers have some duplicate code when dealing with cgroups, such as virLXCCgroupSetupMemTune() and qemuSetupMemoryCgroup(), duplicate code over 80, so consolidate the same chunk into a separate routine. Signed-off-by: Mao Zhongyi <maozhongyi@xxxxxxxxxxxxxxxxxxxx> Signed-off-by: Zhang Shengju <zhangshengju@xxxxxxxxxxxxxxxxxxxx> --- src/libvirt_private.syms | 1 + src/lxc/lxc_cgroup.c | 28 +--------------------------- src/qemu/qemu_cgroup.c | 14 +------------- src/util/vircgroup.c | 30 ++++++++++++++++++++++++++++++ src/util/vircgroup.h | 1 + 5 files changed, 34 insertions(+), 40 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 3df2af55bd..ff002fc60b 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1686,6 +1686,7 @@ virCgroupSetMemorySoftLimit; virCgroupSetMemSwapHardLimit; virCgroupSetOwner; virCgroupSetupBlkio; +virCgroupSetupMemory; virCgroupSupportsCpuBW; virCgroupTerminateMachine; diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c index fdf8df79aa..ea6c14dfef 100644 --- a/src/lxc/lxc_cgroup.c +++ b/src/lxc/lxc_cgroup.c @@ -102,32 +102,6 @@ static int virLXCCgroupSetupCpusetTune(virDomainDefPtr def, } -static int virLXCCgroupSetupMemTune(virDomainDefPtr def, - virCgroupPtr cgroup) -{ - int ret = -1; - - if (virCgroupSetMemory(cgroup, virDomainDefGetMemoryInitial(def)) < 0) - goto cleanup; - - if (virMemoryLimitIsSet(def->mem.hard_limit)) - if (virCgroupSetMemoryHardLimit(cgroup, def->mem.hard_limit) < 0) - goto cleanup; - - if (virMemoryLimitIsSet(def->mem.soft_limit)) - if (virCgroupSetMemorySoftLimit(cgroup, def->mem.soft_limit) < 0) - goto cleanup; - - if (virMemoryLimitIsSet(def->mem.swap_hard_limit)) - if (virCgroupSetMemSwapHardLimit(cgroup, def->mem.swap_hard_limit) < 0) - goto cleanup; - - ret = 0; - cleanup: - return ret; -} - - static int virLXCCgroupGetMemSwapUsage(virCgroupPtr cgroup, virLXCMeminfoPtr meminfo) { @@ -438,7 +412,7 @@ int virLXCCgroupSetup(virDomainDefPtr def, if (virCgroupSetupBlkio(cgroup, def) < 0) goto cleanup; - if (virLXCCgroupSetupMemTune(def, cgroup) < 0) + if (virCgroupSetupMemory(cgroup, def) < 0) goto cleanup; if (virLXCCgroupSetupDeviceACL(def, cgroup) < 0) diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c index c9925ee5ca..22f77a03ad 100644 --- a/src/qemu/qemu_cgroup.c +++ b/src/qemu/qemu_cgroup.c @@ -554,19 +554,7 @@ qemuSetupMemoryCgroup(virDomainObjPtr vm) } } - if (virMemoryLimitIsSet(vm->def->mem.hard_limit)) - if (virCgroupSetMemoryHardLimit(priv->cgroup, vm->def->mem.hard_limit) < 0) - return -1; - - if (virMemoryLimitIsSet(vm->def->mem.soft_limit)) - if (virCgroupSetMemorySoftLimit(priv->cgroup, vm->def->mem.soft_limit) < 0) - return -1; - - if (virMemoryLimitIsSet(vm->def->mem.swap_hard_limit)) - if (virCgroupSetMemSwapHardLimit(priv->cgroup, vm->def->mem.swap_hard_limit) < 0) - return -1; - - return 0; + return virCgroupSetupMemory(priv->cgroup, vm->def); } diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 165e5f4854..12af8759f7 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -1670,6 +1670,36 @@ virCgroupSetMemory(virCgroupPtr group, unsigned long long kb) setMemory, -1, kb); } +/** + * virCgroupSetupMemory + * + * @group: The cgroup to change memory for + * @def: pointer to domian def + * + * Returns: 0 on success, -1 on error. + */ +int +virCgroupSetupMemory(virCgroupPtr group, virDomainDefPtr def) +{ + int ret = -1; + + if (virMemoryLimitIsSet(def->mem.hard_limit)) + if (virCgroupSetMemoryHardLimit(group, def->mem.hard_limit) < 0) + goto cleanup; + + if (virMemoryLimitIsSet(def->mem.soft_limit)) + if (virCgroupSetMemorySoftLimit(group, def->mem.soft_limit) < 0) + goto cleanup; + + if (virMemoryLimitIsSet(def->mem.swap_hard_limit)) + if (virCgroupSetMemSwapHardLimit(group, def->mem.swap_hard_limit) < 0) + goto cleanup; + + ret = 0; + cleanup: + return ret; +} + /** * virCgroupGetMemoryStat: diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h index 9815c51fda..17f07ea53c 100644 --- a/src/util/vircgroup.h +++ b/src/util/vircgroup.h @@ -179,6 +179,7 @@ int virCgroupGetBlkioDeviceWriteBps(virCgroupPtr group, unsigned long long *wbps); int virCgroupSetMemory(virCgroupPtr group, unsigned long long kb); +int virCgroupSetupMemory(virCgroupPtr group, virDomainDefPtr def); int virCgroupGetMemoryStat(virCgroupPtr group, unsigned long long *cache, unsigned long long *activeAnon, -- 2.17.1 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list