virLXCCgroupSetupMemTune() and qemuSetupMemoryCgroup() shares
duplicated code that can be put in a new helper to avoid
code repetition.
Signed-off-by: Daniel Henrique Barboza <danielhb413(a)gmail.com>
---
src/hypervisor/domain_cgroup.c | 19 +++++++++++++++++++
src/hypervisor/domain_cgroup.h | 1 +
src/libvirt_private.syms | 1 +
src/lxc/lxc_cgroup.c | 14 +-------------
src/qemu/qemu_cgroup.c | 14 +-------------
5 files changed, 23 insertions(+), 26 deletions(-)
diff --git a/src/hypervisor/domain_cgroup.c b/src/hypervisor/domain_cgroup.c
index bef60f56c5..e60abd536c 100644
--- a/src/hypervisor/domain_cgroup.c
+++ b/src/hypervisor/domain_cgroup.c
@@ -65,3 +65,22 @@ virDomainCgroupSetupBlkio(virCgroupPtr cgroup, virDomainBlkiotune
blkio)
return 0;
}
+
+
+int
+virDomainCgroupSetupMemtune(virCgroupPtr cgroup, virDomainMemtune mem)
+{
+ if (virMemoryLimitIsSet(mem.hard_limit))
+ if (virCgroupSetMemoryHardLimit(cgroup, mem.hard_limit) < 0)
+ return -1;
+
+ if (virMemoryLimitIsSet(mem.soft_limit))
+ if (virCgroupSetMemorySoftLimit(cgroup, mem.soft_limit) < 0)
+ return -1;
+
+ if (virMemoryLimitIsSet(mem.swap_hard_limit))
+ if (virCgroupSetMemSwapHardLimit(cgroup, mem.swap_hard_limit) < 0)
+ return -1;
+
+ return 0;
+}
diff --git a/src/hypervisor/domain_cgroup.h b/src/hypervisor/domain_cgroup.h
index af244bd2d2..82ba47e85f 100644
--- a/src/hypervisor/domain_cgroup.h
+++ b/src/hypervisor/domain_cgroup.h
@@ -25,3 +25,4 @@
int virDomainCgroupSetupBlkio(virCgroupPtr cgroup, virDomainBlkiotune blkio);
+int virDomainCgroupSetupMemtune(virCgroupPtr cgroup, virDomainMemtune mem);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index dc06b1ae24..1bc5d17000 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1391,6 +1391,7 @@ virSetConnectStorage;
# hypervisor/domain_cgroup.h
virDomainCgroupSetupBlkio;
+virDomainCgroupSetupMemtune;
# libvirt_internal.h
diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c
index 96a89256a1..eac1ee1ee0 100644
--- a/src/lxc/lxc_cgroup.c
+++ b/src/lxc/lxc_cgroup.c
@@ -112,19 +112,7 @@ static int virLXCCgroupSetupMemTune(virDomainDefPtr def,
if (virCgroupSetMemory(cgroup, virDomainDefGetMemoryInitial(def)) < 0)
return -1;
- if (virMemoryLimitIsSet(def->mem.hard_limit))
- if (virCgroupSetMemoryHardLimit(cgroup, def->mem.hard_limit) < 0)
- return -1;
-
- if (virMemoryLimitIsSet(def->mem.soft_limit))
- if (virCgroupSetMemorySoftLimit(cgroup, def->mem.soft_limit) < 0)
- return -1;
-
- if (virMemoryLimitIsSet(def->mem.swap_hard_limit))
- if (virCgroupSetMemSwapHardLimit(cgroup, def->mem.swap_hard_limit) < 0)
- return -1;
-
- return 0;
+ return virDomainCgroupSetupMemtune(cgroup, def->mem);
}
diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index 475c063823..0c2f5f1b25 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -625,19 +625,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 virDomainCgroupSetupMemtune(priv->cgroup, vm->def->mem);
}
--
2.24.1