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 <danielhb...@gmail.com> --- src/libvirt_private.syms | 1 + src/lxc/lxc_cgroup.c | 11 ++--------- src/lxc/lxc_driver.c | 32 +------------------------------- src/qemu/qemu_cgroup.c | 31 +------------------------------ src/util/vircgroup.c | 38 ++++++++++++++++++++++++++++++++++++++ src/util/vircgroup.h | 3 +++ 6 files changed, 46 insertions(+), 70 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 6aa3f670db..38fe847cef 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1730,6 +1730,7 @@ virCgroupSetMemorySoftLimit; virCgroupSetMemSwapHardLimit; virCgroupSetOwner; virCgroupSetupBlkioTune; +virCgroupSetupCpuPeriodQuota; virCgroupSetupCpusetCpus; virCgroupSetupMemtune; virCgroupSupportsCpuBW; diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c index 2ccc1ae5a1..d520f9cb3b 100644 --- a/src/lxc/lxc_cgroup.c +++ b/src/lxc/lxc_cgroup.c @@ -47,15 +47,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); } diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 51f1284d56..69016881dc 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -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); } diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c index 0ca62ba3ee..636c531bd5 100644 --- a/src/qemu/qemu_cgroup.c +++ b/src/qemu/qemu_cgroup.c @@ -1128,36 +1128,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); } diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index cfb34a0f0e..9dc60f66c9 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -3691,3 +3691,41 @@ virCgroupSetAndRetrieveCpuShares(virCgroupPtr cgroup, 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; +} diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h index ed8ee30a58..97ffa3f7df 100644 --- a/src/util/vircgroup.h +++ b/src/util/vircgroup.h @@ -293,3 +293,6 @@ int virCgroupSetupCpusetCpus(virCgroupPtr cgroup, virBitmapPtr cpumask); int virCgroupSetAndRetrieveCpuShares(virCgroupPtr cgroup, unsigned long long shares, unsigned long long *realValue); +int virCgroupSetupCpuPeriodQuota(virCgroupPtr cgroup, + unsigned long long period, + long long quota); -- 2.24.1