c313de
From 785b8c27bef20182789d1715d0b3c1d3fe28a9f9 Mon Sep 17 00:00:00 2001
c313de
Message-Id: <785b8c27bef20182789d1715d0b3c1d3fe28a9f9@dist-git>
c313de
From: Pavel Hrdina <phrdina@redhat.com>
c313de
Date: Tue, 2 Jul 2019 15:13:27 +0200
c313de
Subject: [PATCH] util: vircgroupv2: add support for BFQ files
c313de
MIME-Version: 1.0
c313de
Content-Type: text/plain; charset=UTF-8
c313de
Content-Transfer-Encoding: 8bit
c313de
c313de
In kernel 4.12 there was introduced new BFQ scheduler and in kernel
c313de
5.0 the old CFQ scheduler was removed.  This has an implication on
c313de
the cgroups file names.
c313de
c313de
If the CFQ controller is enabled we use one file:
c313de
c313de
    io.weight
c313de
c313de
The new BFQ controller expose one file with different name:
c313de
c313de
    io.bfq.weight
c313de
c313de
Except for different name they have different syntax.
c313de
c313de
io.weight:
c313de
c313de
    default $val
c313de
    major:minor $val
c313de
c313de
io.bfq.weight:
c313de
c313de
    $val
c313de
c313de
The difference is that BFQ doesn't support per-device weight.
c313de
c313de
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c313de
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c313de
(cherry picked from commit 7e8a1a6e21cee67f6fa5bd2126ec17f96e5857d6)
c313de
c313de
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1658890
c313de
c313de
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c313de
Message-Id: <d41b50b6f3d3ef064b7f3274c762a7c41bf8e1ec.1562073117.git.phrdina@redhat.com>
c313de
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c313de
---
c313de
 src/util/vircgroupv2.c | 101 +++++++++++++++++++++++++++++++----------
c313de
 1 file changed, 78 insertions(+), 23 deletions(-)
c313de
c313de
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
c313de
index e9bb331dd4..b4e90ed46d 100644
c313de
--- a/src/util/vircgroupv2.c
c313de
+++ b/src/util/vircgroupv2.c
c313de
@@ -591,15 +591,35 @@ static int
c313de
 virCgroupV2SetBlkioWeight(virCgroupPtr group,
c313de
                           unsigned int weight)
c313de
 {
c313de
+    VIR_AUTOFREE(char *) path = NULL;
c313de
     VIR_AUTOFREE(char *) value = NULL;
c313de
+    const char *format = "%u";
c313de
 
c313de
-    if (virAsprintf(&value, "default %u", weight) < 0)
c313de
+    if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
c313de
+                                    "io.bfq.weight", &path) < 0) {
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    if (!virFileExists(path)) {
c313de
+        VIR_FREE(path);
c313de
+        format = "default %u";
c313de
+
c313de
+        if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
c313de
+                                        "io.weight", &path) < 0) {
c313de
+            return -1;
c313de
+        }
c313de
+    }
c313de
+
c313de
+    if (!virFileExists(path)) {
c313de
+        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
c313de
+                       _("blkio weight is valid only for bfq or cfq scheduler"));
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    if (virAsprintf(&value, format, weight) < 0)
c313de
         return -1;
c313de
 
c313de
-    return virCgroupSetValueStr(group,
c313de
-                                VIR_CGROUP_CONTROLLER_BLKIO,
c313de
-                                "io.weight",
c313de
-                                value);
c313de
+    return virCgroupSetValueRaw(path, value);
c313de
 }
c313de
 
c313de
 
c313de
@@ -607,20 +627,38 @@ static int
c313de
 virCgroupV2GetBlkioWeight(virCgroupPtr group,
c313de
                           unsigned int *weight)
c313de
 {
c313de
+    VIR_AUTOFREE(char *) path = NULL;
c313de
     VIR_AUTOFREE(char *) value = NULL;
c313de
     char *tmp;
c313de
 
c313de
-    if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_BLKIO,
c313de
-                             "io.weight", &value) < 0) {
c313de
+    if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
c313de
+                                    "io.bfq.weight", &path) < 0) {
c313de
         return -1;
c313de
     }
c313de
 
c313de
-    if (!(tmp = strstr(value, "default "))) {
c313de
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c313de
-                       _("Cannot find default io weight."));
c313de
+    if (!virFileExists(path)) {
c313de
+        VIR_FREE(path);
c313de
+
c313de
+        if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
c313de
+                                        "io.weight", &path) < 0) {
c313de
+            return -1;
c313de
+        }
c313de
+    }
c313de
+
c313de
+    if (!virFileExists(path)) {
c313de
+        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
c313de
+                       _("blkio weight is valid only for bfq or cfq scheduler"));
c313de
         return -1;
c313de
     }
c313de
-    tmp += strlen("default ");
c313de
+
c313de
+    if (virCgroupGetValueRaw(path, &value) < 0)
c313de
+        return -1;
c313de
+
c313de
+    if ((tmp = strstr(value, "default "))) {
c313de
+        tmp += strlen("default ");
c313de
+    } else {
c313de
+        tmp = value;
c313de
+    }
c313de
 
c313de
     if (virStrToLong_ui(tmp, NULL, 10, weight) < 0) {
c313de
         virReportError(VIR_ERR_INTERNAL_ERROR,
c313de
@@ -762,41 +800,58 @@ virCgroupV2GetBlkioIoDeviceServiced(virCgroupPtr group,
c313de
 
c313de
 static int
c313de
 virCgroupV2SetBlkioDeviceWeight(virCgroupPtr group,
c313de
-                                const char *path,
c313de
+                                const char *devPath,
c313de
                                 unsigned int weight)
c313de
 {
c313de
+    VIR_AUTOFREE(char *) path = NULL;
c313de
     VIR_AUTOFREE(char *) str = NULL;
c313de
     VIR_AUTOFREE(char *) blkstr = NULL;
c313de
 
c313de
-    if (!(blkstr = virCgroupGetBlockDevString(path)))
c313de
+    if (!(blkstr = virCgroupGetBlockDevString(devPath)))
c313de
         return -1;
c313de
 
c313de
     if (virAsprintf(&str, "%s%d", blkstr, weight) < 0)
c313de
         return -1;
c313de
 
c313de
-    return virCgroupSetValueStr(group,
c313de
-                                VIR_CGROUP_CONTROLLER_BLKIO,
c313de
-                                "io.weight",
c313de
-                                str);
c313de
+    if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
c313de
+                                    "io.weight", &path) < 0) {
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    if (!virFileExists(path)) {
c313de
+        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
c313de
+                       _("blkio device weight is valid only for cfq scheduler"));
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    return virCgroupSetValueRaw(path, str);
c313de
 }
c313de
 
c313de
 
c313de
 static int
c313de
 virCgroupV2GetBlkioDeviceWeight(virCgroupPtr group,
c313de
-                                const char *path,
c313de
+                                const char *devPath,
c313de
                                 unsigned int *weight)
c313de
 {
c313de
+    VIR_AUTOFREE(char *) path = NULL;
c313de
     VIR_AUTOFREE(char *) str = NULL;
c313de
     VIR_AUTOFREE(char *) value = NULL;
c313de
 
c313de
-    if (virCgroupGetValueStr(group,
c313de
-                             VIR_CGROUP_CONTROLLER_BLKIO,
c313de
-                             "io.weight",
c313de
-                             &value) < 0) {
c313de
+    if (virCgroupV2PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
c313de
+                                    "io.weight", &path) < 0) {
c313de
         return -1;
c313de
     }
c313de
 
c313de
-    if (virCgroupGetValueForBlkDev(value, path, &str) < 0)
c313de
+    if (!virFileExists(path)) {
c313de
+        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
c313de
+                       _("blkio device weight is valid only for cfq scheduler"));
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    if (virCgroupGetValueRaw(path, &value) < 0)
c313de
+        return -1;
c313de
+
c313de
+    if (virCgroupGetValueForBlkDev(value, devPath, &str) < 0)
c313de
         return -1;
c313de
 
c313de
     if (!str) {
c313de
-- 
c313de
2.22.0
c313de