[PATCH v2 08/14] vircgroup.c: add virCgroupSetupCpuPeriodQuota()

Daniel Henrique Barboza posted 14 patches 4 years, 9 months ago
[PATCH v2 08/14] vircgroup.c: add virCgroupSetupCpuPeriodQuota()
Posted by Daniel Henrique Barboza 4 years, 9 months ago
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>
---
 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     |  2 ++
 6 files changed, 45 insertions(+), 70 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 8be5fe7d9f..2f958b1c6f 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1730,6 +1730,7 @@ virCgroupSetupBlkioDeviceReadIops;
 virCgroupSetupBlkioDeviceWeight;
 virCgroupSetupBlkioDeviceWriteBps;
 virCgroupSetupBlkioDeviceWriteIops;
+virCgroupSetupCpuPeriodQuota;
 virCgroupSetupCpusetCpus;
 virCgroupSetupCpuShares;
 virCgroupSupportsCpuBW;
diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c
index 4c8464bd97..4ebe5ef467 100644
--- a/src/lxc/lxc_cgroup.c
+++ b/src/lxc/lxc_cgroup.c
@@ -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);
 }
 
 
diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c
index e66aa7b8f5..d6d0f031a5 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 f969469931..548c5ec274 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -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);
 }
 
 
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index a1093e410d..1f853beb73 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -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;
+}
diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h
index 305d5c9853..cc53bae258 100644
--- a/src/util/vircgroup.h
+++ b/src/util/vircgroup.h
@@ -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);
-- 
2.24.1


Re: [PATCH v2 08/14] vircgroup.c: add virCgroupSetupCpuPeriodQuota()
Posted by Ján Tomko 4 years, 9 months ago
On Mon, Feb 17, 2020 at 04:29:15PM -0500, Daniel Henrique Barboza wrote:
>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>
>---
> 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     |  2 ++
> 6 files changed, 45 insertions(+), 70 deletions(-)
>

Reviewed-by: Ján Tomko <jtomko@redhat.com>

Jano