render / rpms / libvirt

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