[PATCH v2 13/14] domain_cgroup.c: add virDomainCgroupSetMemoryLimitParameters()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



lxcDomainSetMemoryParameters() and qemuDomainSetMemoryParameters()
has duplicated chunks of code that can be put in a new
helper.

Signed-off-by: Daniel Henrique Barboza <danielhb413@xxxxxxxxx>
---
 src/hypervisor/domain_cgroup.c | 81 ++++++++++++++++++++++++++++++++++
 src/hypervisor/domain_cgroup.h |  6 +++
 src/libvirt_private.syms       |  1 +
 src/lxc/lxc_driver.c           | 71 ++---------------------------
 src/qemu/qemu_driver.c         | 71 ++---------------------------
 5 files changed, 96 insertions(+), 134 deletions(-)

diff --git a/src/hypervisor/domain_cgroup.c b/src/hypervisor/domain_cgroup.c
index 32b07be3d6..2b3e1d6956 100644
--- a/src/hypervisor/domain_cgroup.c
+++ b/src/hypervisor/domain_cgroup.c
@@ -185,3 +185,84 @@ virDomainCgroupSetupDomainBlkioParameters(virCgroupPtr cgroup,
 
     return ret;
 }
+
+
+int
+virDomainCgroupSetMemoryLimitParameters(virCgroupPtr cgroup,
+                                        virDomainObjPtr vm,
+                                        virDomainDefPtr liveDef,
+                                        virDomainDefPtr persistentDef,
+                                        virTypedParameterPtr params,
+                                        int nparams)
+{
+    unsigned long long swap_hard_limit;
+    unsigned long long hard_limit = 0;
+    unsigned long long soft_limit = 0;
+    bool set_swap_hard_limit = false;
+    bool set_hard_limit = false;
+    bool set_soft_limit = false;
+    int rc;
+
+#define VIR_GET_LIMIT_PARAMETER(PARAM, VALUE) \
+    if ((rc = virTypedParamsGetULLong(params, nparams, PARAM, &VALUE)) < 0) \
+        return -1; \
+ \
+    if (rc == 1) \
+        set_ ## VALUE = true;
+
+    VIR_GET_LIMIT_PARAMETER(VIR_DOMAIN_MEMORY_SWAP_HARD_LIMIT, swap_hard_limit)
+    VIR_GET_LIMIT_PARAMETER(VIR_DOMAIN_MEMORY_HARD_LIMIT, hard_limit)
+    VIR_GET_LIMIT_PARAMETER(VIR_DOMAIN_MEMORY_SOFT_LIMIT, soft_limit)
+
+#undef VIR_GET_LIMIT_PARAMETER
+
+    /* Swap hard limit must be greater than hard limit. */
+    if (set_swap_hard_limit || set_hard_limit) {
+        unsigned long long mem_limit = vm->def->mem.hard_limit;
+        unsigned long long swap_limit = vm->def->mem.swap_hard_limit;
+
+        if (set_swap_hard_limit)
+            swap_limit = swap_hard_limit;
+
+        if (set_hard_limit)
+            mem_limit = hard_limit;
+
+        if (mem_limit > swap_limit) {
+            virReportError(VIR_ERR_INVALID_ARG, "%s",
+                           _("memory hard_limit tunable value must be lower "
+                             "than or equal to swap_hard_limit"));
+            return -1;
+        }
+    }
+
+#define VIR_SET_MEM_PARAMETER(FUNC, VALUE) \
+    if (set_ ## VALUE) { \
+        if (liveDef) { \
+            if ((rc = FUNC(cgroup, VALUE)) < 0) \
+                return -1; \
+            liveDef->mem.VALUE = VALUE; \
+        } \
+ \
+        if (persistentDef) \
+            persistentDef->mem.VALUE = VALUE; \
+    }
+
+    /* Soft limit doesn't clash with the others */
+    VIR_SET_MEM_PARAMETER(virCgroupSetMemorySoftLimit, soft_limit);
+
+    /* set hard limit before swap hard limit if decreasing it */
+    if (liveDef && liveDef->mem.hard_limit > hard_limit) {
+        VIR_SET_MEM_PARAMETER(virCgroupSetMemoryHardLimit, hard_limit);
+        /* inhibit changing the limit a second time */
+        set_hard_limit = false;
+    }
+
+    VIR_SET_MEM_PARAMETER(virCgroupSetMemSwapHardLimit, swap_hard_limit);
+
+    /* otherwise increase it after swap hard limit */
+    VIR_SET_MEM_PARAMETER(virCgroupSetMemoryHardLimit, hard_limit);
+
+#undef VIR_SET_MEM_PARAMETER
+
+    return 0;
+}
diff --git a/src/hypervisor/domain_cgroup.h b/src/hypervisor/domain_cgroup.h
index 7f3a0d12f9..5a6498b1ce 100644
--- a/src/hypervisor/domain_cgroup.h
+++ b/src/hypervisor/domain_cgroup.h
@@ -30,3 +30,9 @@ int virDomainCgroupSetupDomainBlkioParameters(virCgroupPtr cgroup,
                                               virDomainDefPtr def,
                                               virTypedParameterPtr params,
                                               int nparams);
+int virDomainCgroupSetMemoryLimitParameters(virCgroupPtr cgroup,
+                                            virDomainObjPtr vm,
+                                            virDomainDefPtr liveDef,
+                                            virDomainDefPtr persistentDef,
+                                            virTypedParameterPtr params,
+                                            int nparams);
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index c157012707..679e32116c 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1390,6 +1390,7 @@ virSetConnectStorage;
 
 
 # hypervisor/domain_cgroup.h
+virDomainCgroupSetMemoryLimitParameters;
 virDomainCgroupSetupBlkio;
 virDomainCgroupSetupDomainBlkioParameters;
 virDomainCgroupSetupMemtune;
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 0332b7668a..fc559d736a 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -749,13 +749,6 @@ lxcDomainSetMemoryParameters(virDomainPtr dom,
     virLXCDomainObjPrivatePtr priv = NULL;
     virLXCDriverConfigPtr cfg = NULL;
     virLXCDriverPtr driver = dom->conn->privateData;
-    unsigned long long hard_limit;
-    unsigned long long soft_limit;
-    unsigned long long swap_hard_limit;
-    bool set_hard_limit = false;
-    bool set_soft_limit = false;
-    bool set_swap_hard_limit = false;
-    int rc;
     int ret = -1;
 
     virCheckFlags(VIR_DOMAIN_AFFECT_LIVE |
@@ -794,66 +787,10 @@ lxcDomainSetMemoryParameters(virDomainPtr dom,
         goto endjob;
     }
 
-#define VIR_GET_LIMIT_PARAMETER(PARAM, VALUE) \
-    if ((rc = virTypedParamsGetULLong(params, nparams, PARAM, &VALUE)) < 0) \
-        goto endjob; \
- \
-    if (rc == 1) \
-        set_ ## VALUE = true;
-
-    VIR_GET_LIMIT_PARAMETER(VIR_DOMAIN_MEMORY_SWAP_HARD_LIMIT, swap_hard_limit)
-    VIR_GET_LIMIT_PARAMETER(VIR_DOMAIN_MEMORY_HARD_LIMIT, hard_limit)
-    VIR_GET_LIMIT_PARAMETER(VIR_DOMAIN_MEMORY_SOFT_LIMIT, soft_limit)
-
-#undef VIR_GET_LIMIT_PARAMETER
-
-    /* Swap hard limit must be greater than hard limit. */
-    if (set_swap_hard_limit || set_hard_limit) {
-        unsigned long long mem_limit = vm->def->mem.hard_limit;
-        unsigned long long swap_limit = vm->def->mem.swap_hard_limit;
-
-        if (set_swap_hard_limit)
-            swap_limit = swap_hard_limit;
-
-        if (set_hard_limit)
-            mem_limit = hard_limit;
-
-        if (mem_limit > swap_limit) {
-            virReportError(VIR_ERR_INVALID_ARG, "%s",
-                           _("memory hard_limit tunable value must be lower "
-                             "than or equal to swap_hard_limit"));
-            goto endjob;
-        }
-    }
-
-#define VIR_SET_MEM_PARAMETER(FUNC, VALUE) \
-    if (set_ ## VALUE) { \
-        if (def) { \
-            if ((rc = FUNC(priv->cgroup, VALUE)) < 0) \
-                goto endjob; \
-            def->mem.VALUE = VALUE; \
-        } \
- \
-        if (persistentDef) \
-            persistentDef->mem.VALUE = VALUE; \
-    }
-
-    /* Soft limit doesn't clash with the others */
-    VIR_SET_MEM_PARAMETER(virCgroupSetMemorySoftLimit, soft_limit);
-
-    /* set hard limit before swap hard limit if decreasing it */
-    if (def && def->mem.hard_limit > hard_limit) {
-        VIR_SET_MEM_PARAMETER(virCgroupSetMemoryHardLimit, hard_limit);
-        /* inhibit changing the limit a second time */
-        set_hard_limit = false;
-    }
-
-    VIR_SET_MEM_PARAMETER(virCgroupSetMemSwapHardLimit, swap_hard_limit);
-
-    /* otherwise increase it after swap hard limit */
-    VIR_SET_MEM_PARAMETER(virCgroupSetMemoryHardLimit, hard_limit);
-
-#undef VIR_SET_MEM_PARAMETER
+    if (virDomainCgroupSetMemoryLimitParameters(priv->cgroup, vm, def,
+                                                persistentDef,
+                                                params, nparams) < 0)
+        goto endjob;
 
     if (def &&
         virDomainObjSave(vm, driver->xmlopt, cfg->stateDir) < 0)
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 44145a5f60..b65708385f 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -9502,14 +9502,7 @@ qemuDomainSetMemoryParameters(virDomainPtr dom,
     virDomainDefPtr def = NULL;
     virDomainDefPtr persistentDef = NULL;
     virDomainObjPtr vm = NULL;
-    unsigned long long swap_hard_limit;
-    unsigned long long hard_limit = 0;
-    unsigned long long soft_limit = 0;
-    bool set_swap_hard_limit = false;
-    bool set_hard_limit = false;
-    bool set_soft_limit = false;
     g_autoptr(virQEMUDriverConfig) cfg = NULL;
-    int rc;
     int ret = -1;
     qemuDomainObjPrivatePtr priv;
 
@@ -9556,66 +9549,10 @@ qemuDomainSetMemoryParameters(virDomainPtr dom,
         goto endjob;
     }
 
-#define VIR_GET_LIMIT_PARAMETER(PARAM, VALUE) \
-    if ((rc = virTypedParamsGetULLong(params, nparams, PARAM, &VALUE)) < 0) \
-        goto endjob; \
- \
-    if (rc == 1) \
-        set_ ## VALUE = true;
-
-    VIR_GET_LIMIT_PARAMETER(VIR_DOMAIN_MEMORY_SWAP_HARD_LIMIT, swap_hard_limit)
-    VIR_GET_LIMIT_PARAMETER(VIR_DOMAIN_MEMORY_HARD_LIMIT, hard_limit)
-    VIR_GET_LIMIT_PARAMETER(VIR_DOMAIN_MEMORY_SOFT_LIMIT, soft_limit)
-
-#undef VIR_GET_LIMIT_PARAMETER
-
-    /* Swap hard limit must be greater than hard limit. */
-    if (set_swap_hard_limit || set_hard_limit) {
-        unsigned long long mem_limit = vm->def->mem.hard_limit;
-        unsigned long long swap_limit = vm->def->mem.swap_hard_limit;
-
-        if (set_swap_hard_limit)
-            swap_limit = swap_hard_limit;
-
-        if (set_hard_limit)
-            mem_limit = hard_limit;
-
-        if (mem_limit > swap_limit) {
-            virReportError(VIR_ERR_INVALID_ARG, "%s",
-                           _("memory hard_limit tunable value must be lower "
-                             "than or equal to swap_hard_limit"));
-            goto endjob;
-        }
-    }
-
-#define VIR_SET_MEM_PARAMETER(FUNC, VALUE) \
-    if (set_ ## VALUE) { \
-        if (def) { \
-            if ((rc = FUNC(priv->cgroup, VALUE)) < 0) \
-                goto endjob; \
-            def->mem.VALUE = VALUE; \
-        } \
- \
-        if (persistentDef) \
-            persistentDef->mem.VALUE = VALUE; \
-    }
-
-    /* Soft limit doesn't clash with the others */
-    VIR_SET_MEM_PARAMETER(virCgroupSetMemorySoftLimit, soft_limit);
-
-    /* set hard limit before swap hard limit if decreasing it */
-    if (def && def->mem.hard_limit > hard_limit) {
-        VIR_SET_MEM_PARAMETER(virCgroupSetMemoryHardLimit, hard_limit);
-        /* inhibit changing the limit a second time */
-        set_hard_limit = false;
-    }
-
-    VIR_SET_MEM_PARAMETER(virCgroupSetMemSwapHardLimit, swap_hard_limit);
-
-    /* otherwise increase it after swap hard limit */
-    VIR_SET_MEM_PARAMETER(virCgroupSetMemoryHardLimit, hard_limit);
-
-#undef VIR_SET_MEM_PARAMETER
+    if (virDomainCgroupSetMemoryLimitParameters(priv->cgroup, vm, def,
+                                                persistentDef,
+                                                params, nparams) < 0)
+        goto endjob;
 
     if (def &&
         virDomainObjSave(vm, driver->xmlopt, cfg->stateDir) < 0)
-- 
2.24.1






[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux