c313de
From a6c731357b9c74e5cc298f04267312ad3bb635fa Mon Sep 17 00:00:00 2001
c313de
Message-Id: <a6c731357b9c74e5cc298f04267312ad3bb635fa@dist-git>
c313de
From: Pavel Hrdina <phrdina@redhat.com>
c313de
Date: Tue, 2 Jul 2019 15:13:26 +0200
c313de
Subject: [PATCH] util: vircgroupv1: add support for BFQ blkio 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 these two files:
c313de
c313de
    blkio.weight
c313de
    blkio.weight_device
c313de
c313de
The new BFQ controller expose only one file with different name:
c313de
c313de
    blkio.bfq.weight
c313de
c313de
The reason is that BFQ controller 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 035ebe9390a630964f391816f05c9cc7792212ad)
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: <b66aa3f8ec343e90f9ccd2e458707051c65b4d0d.1562073117.git.phrdina@redhat.com>
c313de
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c313de
---
c313de
 src/util/vircgroupv1.c | 114 ++++++++++++++++++++++++++++++++---------
c313de
 1 file changed, 90 insertions(+), 24 deletions(-)
c313de
c313de
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
c313de
index e51db6ee1f..a7d6c92e4c 100644
c313de
--- a/src/util/vircgroupv1.c
c313de
+++ b/src/util/vircgroupv1.c
c313de
@@ -949,10 +949,33 @@ static int
c313de
 virCgroupV1SetBlkioWeight(virCgroupPtr group,
c313de
                           unsigned int weight)
c313de
 {
c313de
-    return virCgroupSetValueU64(group,
c313de
-                                VIR_CGROUP_CONTROLLER_BLKIO,
c313de
-                                "blkio.weight",
c313de
-                                weight);
c313de
+    VIR_AUTOFREE(char *) path = NULL;
c313de
+    VIR_AUTOFREE(char *) value = NULL;
c313de
+
c313de
+    if (virCgroupV1PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
c313de
+                                    "blkio.bfq.weight", &path) < 0) {
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    if (!virFileExists(path)) {
c313de
+        VIR_FREE(path);
c313de
+
c313de
+        if (virCgroupV1PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
c313de
+                                        "blkio.weight", &path) < 0) {
c313de
+            return -1;
c313de
+        }
c313de
+    }
c313de
+
c313de
+    if (!virFileExists(path)) {
c313de
+        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
c313de
+                       _("blkio device weight is valid only for bfq or cfq scheduler"));
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    if (virAsprintf(&value, "%u", weight) < 0)
c313de
+        return -1;
c313de
+
c313de
+    return virCgroupSetValueRaw(path, value);
c313de
 }
c313de
 
c313de
 
c313de
@@ -960,14 +983,40 @@ static int
c313de
 virCgroupV1GetBlkioWeight(virCgroupPtr group,
c313de
                           unsigned int *weight)
c313de
 {
c313de
-    unsigned long long tmp;
c313de
-    int ret;
c313de
-    ret = virCgroupGetValueU64(group,
c313de
-                               VIR_CGROUP_CONTROLLER_BLKIO,
c313de
-                               "blkio.weight", &tmp);
c313de
-    if (ret == 0)
c313de
-        *weight = tmp;
c313de
-    return ret;
c313de
+    VIR_AUTOFREE(char *) path = NULL;
c313de
+    VIR_AUTOFREE(char *) value = NULL;
c313de
+
c313de
+    if (virCgroupV1PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
c313de
+                                    "blkio.bfq.weight", &path) < 0) {
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    if (!virFileExists(path)) {
c313de
+        VIR_FREE(path);
c313de
+
c313de
+        if (virCgroupV1PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
c313de
+                                        "blkio.weight", &path) < 0) {
c313de
+            return -1;
c313de
+        }
c313de
+    }
c313de
+
c313de
+    if (!virFileExists(path)) {
c313de
+        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
c313de
+                       _("blkio device weight is valid only for bfq or cfq scheduler"));
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    if (virCgroupGetValueRaw(path, &value) < 0)
c313de
+        return -1;
c313de
+
c313de
+    if (virStrToLong_ui(value, NULL, 10, weight) < 0) {
c313de
+        virReportError(VIR_ERR_INTERNAL_ERROR,
c313de
+                       _("Unable to parse '%s' as an integer"),
c313de
+                       value);
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    return 0;
c313de
 }
c313de
 
c313de
 
c313de
@@ -1156,41 +1205,58 @@ virCgroupV1GetBlkioIoDeviceServiced(virCgroupPtr group,
c313de
 
c313de
 static int
c313de
 virCgroupV1SetBlkioDeviceWeight(virCgroupPtr group,
c313de
-                                const char *path,
c313de
+                                const char *devPath,
c313de
                                 unsigned int weight)
c313de
 {
c313de
     VIR_AUTOFREE(char *) str = NULL;
c313de
     VIR_AUTOFREE(char *) blkstr = NULL;
c313de
+    VIR_AUTOFREE(char *) path = 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
-                                "blkio.weight_device",
c313de
-                                str);
c313de
+    if (virCgroupV1PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
c313de
+                                    "blkio.weight_device", &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
 virCgroupV1GetBlkioDeviceWeight(virCgroupPtr group,
c313de
-                                const char *path,
c313de
+                                const char *devPath,
c313de
                                 unsigned int *weight)
c313de
 {
c313de
     VIR_AUTOFREE(char *) str = NULL;
c313de
     VIR_AUTOFREE(char *) value = NULL;
c313de
+    VIR_AUTOFREE(char *) path = NULL;
c313de
 
c313de
-    if (virCgroupGetValueStr(group,
c313de
-                             VIR_CGROUP_CONTROLLER_BLKIO,
c313de
-                             "blkio.weight_device",
c313de
-                             &value) < 0) {
c313de
+    if (virCgroupV1PathOfController(group, VIR_CGROUP_CONTROLLER_BLKIO,
c313de
+                                    "blkio.weight_device", &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