Pablo Greco 40546a
From abf2cfc52b828aa892b446385875381f735ff947 Mon Sep 17 00:00:00 2001
Pablo Greco 40546a
Message-Id: <abf2cfc52b828aa892b446385875381f735ff947@dist-git>
Pablo Greco 40546a
From: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Date: Mon, 1 Jul 2019 17:07:32 +0200
Pablo Greco 40546a
Subject: [PATCH] vircgroup: introduce virCgroupV2GetBlkioIoServiced
Pablo Greco 40546a
MIME-Version: 1.0
Pablo Greco 40546a
Content-Type: text/plain; charset=UTF-8
Pablo Greco 40546a
Content-Transfer-Encoding: 8bit
Pablo Greco 40546a
Pablo Greco 40546a
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
(cherry picked from commit 709260add941a247b3829cf940624333cb445bb7)
Pablo Greco 40546a
Pablo Greco 40546a
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
Pablo Greco 40546a
Pablo Greco 40546a
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Message-Id: <f178386744bb4d74f41acecb82807d451c255fc9.1561993100.git.phrdina@redhat.com>
Pablo Greco 40546a
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Pablo Greco 40546a
---
Pablo Greco 40546a
 src/util/vircgroupv2.c | 65 ++++++++++++++++++++++++++++++++++++++++++
Pablo Greco 40546a
 1 file changed, 65 insertions(+)
Pablo Greco 40546a
Pablo Greco 40546a
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
Pablo Greco 40546a
index f7863a5690..000fd4c747 100644
Pablo Greco 40546a
--- a/src/util/vircgroupv2.c
Pablo Greco 40546a
+++ b/src/util/vircgroupv2.c
Pablo Greco 40546a
@@ -567,6 +567,70 @@ virCgroupV2GetBlkioWeight(virCgroupPtr group,
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
+static int
Pablo Greco 40546a
+virCgroupV2GetBlkioIoServiced(virCgroupPtr group,
Pablo Greco 40546a
+                              long long *bytes_read,
Pablo Greco 40546a
+                              long long *bytes_write,
Pablo Greco 40546a
+                              long long *requests_read,
Pablo Greco 40546a
+                              long long *requests_write)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    long long stats_val;
Pablo Greco 40546a
+    VIR_AUTOFREE(char *) str1 = NULL;
Pablo Greco 40546a
+    char *p1;
Pablo Greco 40546a
+    size_t i;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    const char *value_names[] = {
Pablo Greco 40546a
+        "rbytes=",
Pablo Greco 40546a
+        "wbytes=",
Pablo Greco 40546a
+        "rios=",
Pablo Greco 40546a
+        "wios=",
Pablo Greco 40546a
+    };
Pablo Greco 40546a
+    long long *value_ptrs[] = {
Pablo Greco 40546a
+        bytes_read,
Pablo Greco 40546a
+        bytes_write,
Pablo Greco 40546a
+        requests_read,
Pablo Greco 40546a
+        requests_write
Pablo Greco 40546a
+    };
Pablo Greco 40546a
+
Pablo Greco 40546a
+    *bytes_read = 0;
Pablo Greco 40546a
+    *bytes_write = 0;
Pablo Greco 40546a
+    *requests_read = 0;
Pablo Greco 40546a
+    *requests_write = 0;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (virCgroupGetValueStr(group,
Pablo Greco 40546a
+                             VIR_CGROUP_CONTROLLER_BLKIO,
Pablo Greco 40546a
+                             "io.stat", &str1) < 0) {
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    /* sum up all entries of the same kind, from all devices */
Pablo Greco 40546a
+    for (i = 0; i < ARRAY_CARDINALITY(value_names); i++) {
Pablo Greco 40546a
+        p1 = str1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+        while ((p1 = strstr(p1, value_names[i]))) {
Pablo Greco 40546a
+            p1 += strlen(value_names[i]);
Pablo Greco 40546a
+            if (virStrToLong_ll(p1, &p1, 10, &stats_val) < 0) {
Pablo Greco 40546a
+                virReportError(VIR_ERR_INTERNAL_ERROR,
Pablo Greco 40546a
+                               _("Cannot parse byte '%s' stat '%s'"),
Pablo Greco 40546a
+                               value_names[i], p1);
Pablo Greco 40546a
+                return -1;
Pablo Greco 40546a
+            }
Pablo Greco 40546a
+
Pablo Greco 40546a
+            if (stats_val < 0 ||
Pablo Greco 40546a
+                (stats_val > 0 && *value_ptrs[i] > (LLONG_MAX - stats_val))) {
Pablo Greco 40546a
+                virReportError(VIR_ERR_OVERFLOW,
Pablo Greco 40546a
+                               _("Sum of byte '%s' stat overflows"),
Pablo Greco 40546a
+                               value_names[i]);
Pablo Greco 40546a
+                return -1;
Pablo Greco 40546a
+            }
Pablo Greco 40546a
+            *value_ptrs[i] += stats_val;
Pablo Greco 40546a
+        }
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    return 0;
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
 virCgroupBackend virCgroupV2Backend = {
Pablo Greco 40546a
     .type = VIR_CGROUP_BACKEND_TYPE_V2,
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -591,6 +655,7 @@ virCgroupBackend virCgroupV2Backend = {
Pablo Greco 40546a
 
Pablo Greco 40546a
     .setBlkioWeight = virCgroupV2SetBlkioWeight,
Pablo Greco 40546a
     .getBlkioWeight = virCgroupV2GetBlkioWeight,
Pablo Greco 40546a
+    .getBlkioIoServiced = virCgroupV2GetBlkioIoServiced,
Pablo Greco 40546a
 };
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
-- 
Pablo Greco 40546a
2.22.0
Pablo Greco 40546a