lxcDomainSetMemoryParameters() and qemuDomainSetMemoryParameters()
has duplicated chunks of code that can be put in a new
helper.
Signed-off-by: Daniel Henrique Barboza <danielhb413(a)gmail.com>
---
src/libvirt_private.syms | 1 +
src/lxc/lxc_driver.c | 70 ++---------------------------------
src/qemu/qemu_driver.c | 70 ++---------------------------------
src/util/vircgroup.c | 79 ++++++++++++++++++++++++++++++++++++++++
src/util/vircgroup.h | 6 +++
5 files changed, 92 insertions(+), 134 deletions(-)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 1422fbc3d9..7143e12838 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1729,6 +1729,7 @@ virCgroupSetCpuShares;
virCgroupSetFreezerState;
virCgroupSetMemory;
virCgroupSetMemoryHardLimit;
+virCgroupSetMemoryLimitParameters;
virCgroupSetMemorySoftLimit;
virCgroupSetMemSwapHardLimit;
virCgroupSetOwner;
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index 1fabba40bd..b5c02b3ccb 100644
--- a/src/lxc/lxc_driver.c
+++ b/src/lxc/lxc_driver.c
@@ -747,13 +747,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 |
@@ -792,66 +785,9 @@ 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 (virCgroupSetMemoryLimitParameters(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 c154efbaf6..d2177d73a2 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -9500,14 +9500,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;
@@ -9554,66 +9547,9 @@ 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 (virCgroupSetMemoryLimitParameters(priv->cgroup, vm, def, persistentDef,
+ params, nparams) < 0)
+ goto endjob;
if (def &&
virDomainObjSave(vm, driver->xmlopt, cfg->stateDir) < 0)
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 67855b685f..530f1b7223 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -3844,3 +3844,82 @@ virCgroupSetupDomainBlkioParameters(virCgroupPtr cgroup,
virDomainDefPtr def,
return ret;
}
+
+
+int
+virCgroupSetMemoryLimitParameters(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/util/vircgroup.h b/src/util/vircgroup.h
index 85309e800d..5d238937d2 100644
--- a/src/util/vircgroup.h
+++ b/src/util/vircgroup.h
@@ -300,3 +300,9 @@ int virCgroupSetupDomainBlkioParameters(virCgroupPtr cgroup,
virDomainDefPtr def,
virTypedParameterPtr params,
int nparams);
+int virCgroupSetMemoryLimitParameters(virCgroupPtr cgroup,
+ virDomainObjPtr vm,
+ virDomainDefPtr liveDef,
+ virDomainDefPtr persistentDef,
+ virTypedParameterPtr params,
+ int nparams);
--
2.24.1