]> xenbits.xensource.com Git - libvirt.git/commitdiff
vircgroup.c: add virCgroupSetupCpuPeriodQuota()
authorDaniel Henrique Barboza <danielhb413@gmail.com>
Mon, 17 Feb 2020 21:29:15 +0000 (16:29 -0500)
committerJán Tomko <jtomko@redhat.com>
Sun, 23 Feb 2020 13:02:23 +0000 (14:02 +0100)
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>
src/libvirt_private.syms
src/lxc/lxc_cgroup.c
src/lxc/lxc_driver.c
src/qemu/qemu_cgroup.c
src/util/vircgroup.c
src/util/vircgroup.h

index 2580cad253bc76864cc2945bba22944dc3cf6e8f..f2124731e739e0b44a5d1d3dccb742b178c9b8ad 100644 (file)
@@ -1731,6 +1731,7 @@ virCgroupSetupBlkioDeviceReadIops;
 virCgroupSetupBlkioDeviceWeight;
 virCgroupSetupBlkioDeviceWriteBps;
 virCgroupSetupBlkioDeviceWriteIops;
+virCgroupSetupCpuPeriodQuota;
 virCgroupSetupCpusetCpus;
 virCgroupSetupCpuShares;
 virCgroupSupportsCpuBW;
index 4c8464bd97c99198b739ea943c31db9c3f4a0936..4ebe5ef467eed920c84ed2e499f7c886082ce8f4 100644 (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);
 }
 
 
index e66aa7b8f506dd4d5f6bb3ef65dc12b3a8799d3e..d6d0f031a5c0fb3ee20988aec7565b7e5d5296e8 100644 (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);
 }
 
 
index f969469931e9ffc32168a9d3ba4f3f610c6957a9..548c5ec27421492ea8124908e6e467b559f13121 100644 (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);
 }
 
 
index a1093e410d4031ccd1bd74e76ec60c1f2741ee53..1f853beb7371433292aa30a7834f0750269ac3ea 100644 (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;
+}
index 305d5c9853560a1d50749b506fb1597ed0f937f4..cc53bae258fc55af8a3e814aabef1b254a6db531 100644 (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);