vircgroup.c: add virCgroupSetupCpuShares()

The code that calls virCgroupSetCpuShares() and virCgroupGetCpuShares()
is repeated in 4 different places. Let's put it in a new
virCgroupSetupCpuShares() to avoid code repetition.

There's a reason of why we execute a Get in the same value we
just executed Set, explained in detail by commit 97814d8ab3.
Let's add a gist of the reasoning behind it as a comment in
this new function as well.

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:
Daniel Henrique Barboza 2020-02-17 16:29:14 -05:00 committed by Ján Tomko
parent 867c554e52
commit d8e5b97500
7 changed files with 29 additions and 15 deletions

View File

@ -1732,6 +1732,7 @@ virCgroupSetupBlkioDeviceWeight;
virCgroupSetupBlkioDeviceWriteBps;
virCgroupSetupBlkioDeviceWriteIops;
virCgroupSetupCpusetCpus;
virCgroupSetupCpuShares;
virCgroupSupportsCpuBW;
virCgroupTerminateMachine;

View File

@ -40,10 +40,7 @@ static int virLXCCgroupSetupCpuTune(virDomainDefPtr def,
{
if (def->cputune.sharesSpecified) {
unsigned long long val;
if (virCgroupSetCpuShares(cgroup, def->cputune.shares) < 0)
return -1;
if (virCgroupGetCpuShares(cgroup, &val) < 0)
if (virCgroupSetupCpuShares(cgroup, def->cputune.shares, &val) < 0)
return -1;
def->cputune.shares = val;
}

View File

@ -1959,10 +1959,8 @@ lxcDomainSetSchedulerParametersFlags(virDomainPtr dom,
if (STREQ(param->field, VIR_DOMAIN_SCHEDULER_CPU_SHARES)) {
if (def) {
unsigned long long val;
if (virCgroupSetCpuShares(priv->cgroup, params[i].value.ul) < 0)
goto endjob;
if (virCgroupGetCpuShares(priv->cgroup, &val) < 0)
if (virCgroupSetupCpuShares(priv->cgroup, params[i].value.ul,
&val) < 0)
goto endjob;
def->cputune.shares = val;

View File

@ -894,11 +894,10 @@ qemuSetupCpuCgroup(virDomainObjPtr vm)
if (vm->def->cputune.sharesSpecified) {
unsigned long long val;
if (virCgroupSetCpuShares(priv->cgroup, vm->def->cputune.shares) < 0)
if (virCgroupSetupCpuShares(priv->cgroup, vm->def->cputune.shares,
&val) < 0)
return -1;
if (virCgroupGetCpuShares(priv->cgroup, &val) < 0)
return -1;
if (vm->def->cputune.shares != val) {
vm->def->cputune.shares = val;
if (virTypedParamsAddULLong(&eventParams, &eventNparams,

View File

@ -10648,10 +10648,7 @@ qemuDomainSetSchedulerParametersFlags(virDomainPtr dom,
if (STREQ(param->field, VIR_DOMAIN_SCHEDULER_CPU_SHARES)) {
if (def) {
unsigned long long val;
if (virCgroupSetCpuShares(priv->cgroup, value_ul) < 0)
goto endjob;
if (virCgroupGetCpuShares(priv->cgroup, &val) < 0)
if (virCgroupSetupCpuShares(priv->cgroup, value_ul, &val) < 0)
goto endjob;
def->cputune.shares = val;

View File

@ -3681,3 +3681,23 @@ virCgroupSetupCpusetCpus(virCgroupPtr cgroup, virBitmapPtr cpumask)
return 0;
}
/* Per commit 97814d8ab3, the Linux kernel can consider a 'shares'
* value of '0' and '1' as 2, and any value larger than a maximum
* is reduced to maximum.
*
* The 'realValue' pointer holds the actual 'shares' value set by
* the kernel if the function returned success. */
int
virCgroupSetupCpuShares(virCgroupPtr cgroup, unsigned long long shares,
unsigned long long *realValue)
{
if (virCgroupSetCpuShares(cgroup, shares) < 0)
return -1;
if (virCgroupGetCpuShares(cgroup, realValue) < 0)
return -1;
return 0;
}

View File

@ -222,6 +222,8 @@ virCgroupGetDomainTotalCpuStats(virCgroupPtr group,
int virCgroupSetCpuShares(virCgroupPtr group, unsigned long long shares);
int virCgroupGetCpuShares(virCgroupPtr group, unsigned long long *shares);
int virCgroupSetupCpuShares(virCgroupPtr cgroup, unsigned long long shares,
unsigned long long *realValue);
int virCgroupSetCpuCfsPeriod(virCgroupPtr group, unsigned long long cfs_period);
int virCgroupGetCpuCfsPeriod(virCgroupPtr group, unsigned long long *cfs_period);