c313de
From 05610561d752380d3f75780056fea927d723b53b Mon Sep 17 00:00:00 2001
c313de
Message-Id: <05610561d752380d3f75780056fea927d723b53b@dist-git>
c313de
From: Pavel Hrdina <phrdina@redhat.com>
c313de
Date: Mon, 1 Jul 2019 17:07:47 +0200
c313de
Subject: [PATCH] vircgroup: introduce virCgroupV2(Set|Get)CpuCfsPeriod
c313de
MIME-Version: 1.0
c313de
Content-Type: text/plain; charset=UTF-8
c313de
Content-Transfer-Encoding: 8bit
c313de
c313de
In order to set CPU cfs period using cgroup v2 'cpu.max' interface
c313de
we need to load the current value of CPU cfs quota first because
c313de
format of 'cpu.max' interface is '$quota $period' and in order to
c313de
change 'period' we need to write 'quota' as well.  Writing only one
c313de
number changes only 'quota'.
c313de
c313de
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c313de
(cherry picked from commit 832422457214421211304bfb0c92b00546dcec53)
c313de
c313de
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
c313de
c313de
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c313de
Message-Id: <21d457977390fd50b26a2ea5f7f904946578accb.1561993100.git.phrdina@redhat.com>
c313de
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c313de
---
c313de
 src/util/vircgroupv2.c | 68 ++++++++++++++++++++++++++++++++++++++++++
c313de
 1 file changed, 68 insertions(+)
c313de
c313de
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
c313de
index cb9dd3d8e8..876785e4e1 100644
c313de
--- a/src/util/vircgroupv2.c
c313de
+++ b/src/util/vircgroupv2.c
c313de
@@ -1325,6 +1325,72 @@ virCgroupV2GetCpuShares(virCgroupPtr group,
c313de
 }
c313de
 
c313de
 
c313de
+static int
c313de
+virCgroupV2SetCpuCfsPeriod(virCgroupPtr group,
c313de
+                           unsigned long long cfs_period)
c313de
+{
c313de
+    VIR_AUTOFREE(char *) value = NULL;
c313de
+    VIR_AUTOFREE(char *) str = NULL;
c313de
+    char *tmp;
c313de
+
c313de
+    /* The cfs_period should be greater or equal than 1ms, and less or equal
c313de
+     * than 1s.
c313de
+     */
c313de
+    if (cfs_period < 1000 || cfs_period > 1000000) {
c313de
+        virReportError(VIR_ERR_INVALID_ARG,
c313de
+                       _("cfs_period '%llu' must be in range (1000, 1000000)"),
c313de
+                       cfs_period);
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPU,
c313de
+                             "cpu.max", &str) < 0) {
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    if (!(tmp = strchr(str, ' '))) {
c313de
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c313de
+                       _("Invalid 'cpu.max' data."));
c313de
+        return -1;
c313de
+    }
c313de
+    *tmp = '\n';
c313de
+
c313de
+    if (virAsprintf(&value, "%s %llu", str, cfs_period) < 0)
c313de
+        return -1;
c313de
+
c313de
+    return virCgroupSetValueStr(group, VIR_CGROUP_CONTROLLER_CPU,
c313de
+                                "cpu.max", value);
c313de
+}
c313de
+
c313de
+
c313de
+static int
c313de
+virCgroupV2GetCpuCfsPeriod(virCgroupPtr group,
c313de
+                           unsigned long long *cfs_period)
c313de
+{
c313de
+    VIR_AUTOFREE(char *) str = NULL;
c313de
+    char *tmp;
c313de
+
c313de
+    if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPU,
c313de
+                             "cpu.max", &str) < 0) {
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    if (!(tmp = strchr(str, ' '))) {
c313de
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c313de
+                       _("Invalid 'cpu.max' data."));
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    if (virStrToLong_ull(tmp, NULL, 10, cfs_period) < 0) {
c313de
+        virReportError(VIR_ERR_INTERNAL_ERROR,
c313de
+                       _("Failed to parse value '%s' from cpu.max."), str);
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    return 0;
c313de
+}
c313de
+
c313de
+
c313de
 virCgroupBackend virCgroupV2Backend = {
c313de
     .type = VIR_CGROUP_BACKEND_TYPE_V2,
c313de
 
c313de
@@ -1375,6 +1441,8 @@ virCgroupBackend virCgroupV2Backend = {
c313de
 
c313de
     .setCpuShares = virCgroupV2SetCpuShares,
c313de
     .getCpuShares = virCgroupV2GetCpuShares,
c313de
+    .setCpuCfsPeriod = virCgroupV2SetCpuCfsPeriod,
c313de
+    .getCpuCfsPeriod = virCgroupV2GetCpuCfsPeriod,
c313de
 };
c313de
 
c313de
 
c313de
-- 
c313de
2.22.0
c313de