virLXCCgroupSetupMemTune() and qemuSetupMemoryCgroup() shares
duplicated code that can be put in a new helper to avoid
code repetition.

Signed-off-by: Daniel Henrique Barboza <danielhb...@gmail.com>
---
 src/libvirt_private.syms |  1 +
 src/lxc/lxc_cgroup.c     | 14 +-------------
 src/qemu/qemu_cgroup.c   | 14 +-------------
 src/util/vircgroup.c     | 19 +++++++++++++++++++
 src/util/vircgroup.h     |  1 +
 5 files changed, 23 insertions(+), 26 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 64163a5e56..075bb8b3a1 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1729,6 +1729,7 @@ virCgroupSetMemorySoftLimit;
 virCgroupSetMemSwapHardLimit;
 virCgroupSetOwner;
 virCgroupSetupBlkioTune;
+virCgroupSetupMemtune;
 virCgroupSupportsCpuBW;
 virCgroupTerminateMachine;
 
diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c
index 96d43b06f2..b156a3273a 100644
--- a/src/lxc/lxc_cgroup.c
+++ b/src/lxc/lxc_cgroup.c
@@ -111,19 +111,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 virCgroupSetupMemtune(cgroup, def->mem);
 }
 
 
diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index a5eaa24d90..0b78cebd34 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -624,19 +624,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 virCgroupSetupMemtune(priv->cgroup, vm->def->mem);
 }
 
 
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 0d83e2094f..814c954284 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -3636,3 +3636,22 @@ virCgroupSetupBlkioTune(virCgroupPtr cgroup, 
virDomainBlkiotune blkio)
 
     return 0;
 }
+
+
+int
+virCgroupSetupMemtune(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/util/vircgroup.h b/src/util/vircgroup.h
index d2d7e7ab51..3cb9bc24f9 100644
--- a/src/util/vircgroup.h
+++ b/src/util/vircgroup.h
@@ -288,3 +288,4 @@ int virCgroupHasEmptyTasks(virCgroupPtr cgroup, int 
controller);
 bool virCgroupControllerAvailable(int controller);
 
 int virCgroupSetupBlkioTune(virCgroupPtr cgroup, virDomainBlkiotune blkio);
+int virCgroupSetupMemtune(virCgroupPtr cgroup, virDomainMemtune mem);
-- 
2.24.1


Reply via email to