mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-28 11:31:16 +00:00
domain_cgroup.c: add virDomainCgroupSetMemoryLimitParameters()
lxcDomainSetMemoryParameters() and qemuDomainSetMemoryParameters() has duplicated chunks of code that can be put in a new helper. Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com> Signed-off-by: Ján Tomko <jtomko@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
parent
309a8305b7
commit
8c83c5e136
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -1391,6 +1391,7 @@ virSetConnectStorage;
|
||||
|
||||
|
||||
# hypervisor/domain_cgroup.h
|
||||
virDomainCgroupSetMemoryLimitParameters;
|
||||
virDomainCgroupSetupBlkio;
|
||||
virDomainCgroupSetupDomainBlkioParameters;
|
||||
virDomainCgroupSetupMemtune;
|
||||
|
@ -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"));
|
||||
if (virDomainCgroupSetMemoryLimitParameters(priv->cgroup, vm, def,
|
||||
persistentDef,
|
||||
params, nparams) < 0)
|
||||
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 (def &&
|
||||
virDomainObjSave(vm, driver->xmlopt, cfg->stateDir) < 0)
|
||||
|
@ -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"));
|
||||
if (virDomainCgroupSetMemoryLimitParameters(priv->cgroup, vm, def,
|
||||
persistentDef,
|
||||
params, nparams) < 0)
|
||||
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 (def &&
|
||||
virDomainObjSave(vm, driver->xmlopt, cfg->stateDir) < 0)
|
||||
|
Loading…
Reference in New Issue
Block a user