vircgroup.c: add virCgroupSetupCpuPeriodQuota()

qemuSetupCgroupVcpuBW() and lxcSetVcpuBWLive() shares the
same code to set CPU CFS period and quota. This code can be
moved to a new virCgroupSetupCpuPeriodQuota() helper to
avoid code repetition.

A similar code is also executed in virLXCCgroupSetupCpuTune(),
but without the rollback on error. Use the new helper in this
function as well since the 'period' rollback, if not a
straight improvement for virLXCCgroupSetupCpuTune(), is
benign. And we end up cutting more code repetition.

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:15 -05:00 committed by Ján Tomko
parent d8e5b97500
commit e039341cf2
6 changed files with 45 additions and 70 deletions

View File

@ -1731,6 +1731,7 @@ virCgroupSetupBlkioDeviceReadIops;
virCgroupSetupBlkioDeviceWeight;
virCgroupSetupBlkioDeviceWriteBps;
virCgroupSetupBlkioDeviceWriteIops;
virCgroupSetupCpuPeriodQuota;
virCgroupSetupCpusetCpus;
virCgroupSetupCpuShares;
virCgroupSupportsCpuBW;

View File

@ -45,15 +45,8 @@ static int virLXCCgroupSetupCpuTune(virDomainDefPtr def,
def->cputune.shares = val;
}
if (def->cputune.quota != 0 &&
virCgroupSetCpuCfsQuota(cgroup, def->cputune.quota) < 0)
return -1;
if (def->cputune.period != 0 &&
virCgroupSetCpuCfsPeriod(cgroup, def->cputune.period) < 0)
return -1;
return 0;
return virCgroupSetupCpuPeriodQuota(cgroup, def->cputune.period,
def->cputune.quota);
}

View File

@ -1857,37 +1857,7 @@ lxcGetVcpuBWLive(virCgroupPtr cgroup, unsigned long long *period,
static int lxcSetVcpuBWLive(virCgroupPtr cgroup, unsigned long long period,
long long quota)
{
unsigned long long old_period;
if (period == 0 && quota == 0)
return 0;
if (period) {
/* get old period, and we can rollback if set quota failed */
if (virCgroupGetCpuCfsPeriod(cgroup, &old_period) < 0)
return -1;
if (virCgroupSetCpuCfsPeriod(cgroup, period) < 0)
return -1;
}
if (quota) {
if (virCgroupSetCpuCfsQuota(cgroup, quota) < 0)
goto error;
}
return 0;
error:
if (period) {
virErrorPtr saved;
virErrorPreserveLast(&saved);
virCgroupSetCpuCfsPeriod(cgroup, old_period);
virErrorRestore(&saved);
}
return -1;
return virCgroupSetupCpuPeriodQuota(cgroup, period, quota);
}

View File

@ -1127,36 +1127,7 @@ qemuSetupCgroupVcpuBW(virCgroupPtr cgroup,
unsigned long long period,
long long quota)
{
unsigned long long old_period;
if (period == 0 && quota == 0)
return 0;
if (period) {
/* get old period, and we can rollback if set quota failed */
if (virCgroupGetCpuCfsPeriod(cgroup, &old_period) < 0)
return -1;
if (virCgroupSetCpuCfsPeriod(cgroup, period) < 0)
return -1;
}
if (quota &&
virCgroupSetCpuCfsQuota(cgroup, quota) < 0)
goto error;
return 0;
error:
if (period) {
virErrorPtr saved;
virErrorPreserveLast(&saved);
ignore_value(virCgroupSetCpuCfsPeriod(cgroup, old_period));
virErrorRestore(&saved);
}
return -1;
return virCgroupSetupCpuPeriodQuota(cgroup, period, quota);
}

View File

@ -3701,3 +3701,41 @@ virCgroupSetupCpuShares(virCgroupPtr cgroup, unsigned long long shares,
return 0;
}
int
virCgroupSetupCpuPeriodQuota(virCgroupPtr cgroup,
unsigned long long period,
long long quota)
{
unsigned long long old_period;
if (period == 0 && quota == 0)
return 0;
if (period) {
/* get old period, and we can rollback if set quota failed */
if (virCgroupGetCpuCfsPeriod(cgroup, &old_period) < 0)
return -1;
if (virCgroupSetCpuCfsPeriod(cgroup, period) < 0)
return -1;
}
if (quota &&
virCgroupSetCpuCfsQuota(cgroup, quota) < 0)
goto error;
return 0;
error:
if (period) {
virErrorPtr saved;
virErrorPreserveLast(&saved);
ignore_value(virCgroupSetCpuCfsPeriod(cgroup, old_period));
virErrorRestore(&saved);
}
return -1;
}

View File

@ -227,6 +227,8 @@ int virCgroupSetupCpuShares(virCgroupPtr cgroup, unsigned long long shares,
int virCgroupSetCpuCfsPeriod(virCgroupPtr group, unsigned long long cfs_period);
int virCgroupGetCpuCfsPeriod(virCgroupPtr group, unsigned long long *cfs_period);
int virCgroupSetupCpuPeriodQuota(virCgroupPtr cgroup, unsigned long long period,
long long quota);
int virCgroupSetCpuCfsQuota(virCgroupPtr group, long long cfs_quota);
int virCgroupGetCpuCfsQuota(virCgroupPtr group, long long *cfs_quota);