c480ed
From 3cb8992be4013fb3ae978c62f6a433080e74130a Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <3cb8992be4013fb3ae978c62f6a433080e74130a@dist-git>
c480ed
From: Pavel Hrdina <phrdina@redhat.com>
c480ed
Date: Mon, 1 Jul 2019 17:06:42 +0200
c480ed
Subject: [PATCH] vircgroup: extract virCgroupV1GetBlkioIoServiced
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
(cherry picked from commit 4baa08ace12d2db7e7023b2abe915360bc7dcddf)
c480ed
c480ed
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
c480ed
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
Message-Id: <aa03ae2eef4e9a09d28562f7218c302ec095a912.1561993100.git.phrdina@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/util/vircgroup.c        | 87 ++-------------------------------
c480ed
 src/util/vircgroupbackend.h |  8 ++++
c480ed
 src/util/vircgroupv1.c      | 95 +++++++++++++++++++++++++++++++++++++
c480ed
 3 files changed, 106 insertions(+), 84 deletions(-)
c480ed
c480ed
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
c480ed
index d2ffa8aefe..8e46777920 100644
c480ed
--- a/src/util/vircgroup.c
c480ed
+++ b/src/util/vircgroup.c
c480ed
@@ -1311,90 +1311,9 @@ virCgroupGetBlkioIoServiced(virCgroupPtr group,
c480ed
                             long long *requests_read,
c480ed
                             long long *requests_write)
c480ed
 {
c480ed
-    long long stats_val;
c480ed
-    VIR_AUTOFREE(char *) str1 = NULL;
c480ed
-    VIR_AUTOFREE(char *) str2 = NULL;
c480ed
-    char *p1 = NULL;
c480ed
-    char *p2 = NULL;
c480ed
-    size_t i;
c480ed
-
c480ed
-    const char *value_names[] = {
c480ed
-        "Read ",
c480ed
-        "Write "
c480ed
-    };
c480ed
-    long long *bytes_ptrs[] = {
c480ed
-        bytes_read,
c480ed
-        bytes_write
c480ed
-    };
c480ed
-    long long *requests_ptrs[] = {
c480ed
-        requests_read,
c480ed
-        requests_write
c480ed
-    };
c480ed
-
c480ed
-    *bytes_read = 0;
c480ed
-    *bytes_write = 0;
c480ed
-    *requests_read = 0;
c480ed
-    *requests_write = 0;
c480ed
-
c480ed
-    if (virCgroupGetValueStr(group,
c480ed
-                             VIR_CGROUP_CONTROLLER_BLKIO,
c480ed
-                             "blkio.throttle.io_service_bytes", &str1) < 0)
c480ed
-        return -1;
c480ed
-
c480ed
-    if (virCgroupGetValueStr(group,
c480ed
-                             VIR_CGROUP_CONTROLLER_BLKIO,
c480ed
-                             "blkio.throttle.io_serviced", &str2) < 0)
c480ed
-        return -1;
c480ed
-
c480ed
-    /* sum up all entries of the same kind, from all devices */
c480ed
-    for (i = 0; i < ARRAY_CARDINALITY(value_names); i++) {
c480ed
-        p1 = str1;
c480ed
-        p2 = str2;
c480ed
-
c480ed
-        while ((p1 = strstr(p1, value_names[i]))) {
c480ed
-            p1 += strlen(value_names[i]);
c480ed
-            if (virStrToLong_ll(p1, &p1, 10, &stats_val) < 0) {
c480ed
-                virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
-                               _("Cannot parse byte %sstat '%s'"),
c480ed
-                               value_names[i],
c480ed
-                               p1);
c480ed
-                return -1;
c480ed
-            }
c480ed
-
c480ed
-            if (stats_val < 0 ||
c480ed
-                (stats_val > 0 && *bytes_ptrs[i] > (LLONG_MAX - stats_val)))
c480ed
-            {
c480ed
-                virReportError(VIR_ERR_OVERFLOW,
c480ed
-                               _("Sum of byte %sstat overflows"),
c480ed
-                               value_names[i]);
c480ed
-                return -1;
c480ed
-            }
c480ed
-            *bytes_ptrs[i] += stats_val;
c480ed
-        }
c480ed
-
c480ed
-        while ((p2 = strstr(p2, value_names[i]))) {
c480ed
-            p2 += strlen(value_names[i]);
c480ed
-            if (virStrToLong_ll(p2, &p2, 10, &stats_val) < 0) {
c480ed
-                virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
-                               _("Cannot parse %srequest stat '%s'"),
c480ed
-                               value_names[i],
c480ed
-                               p2);
c480ed
-                return -1;
c480ed
-            }
c480ed
-
c480ed
-            if (stats_val < 0 ||
c480ed
-                (stats_val > 0 && *requests_ptrs[i] > (LLONG_MAX - stats_val)))
c480ed
-            {
c480ed
-                virReportError(VIR_ERR_OVERFLOW,
c480ed
-                               _("Sum of %srequest stat overflows"),
c480ed
-                               value_names[i]);
c480ed
-                return -1;
c480ed
-            }
c480ed
-            *requests_ptrs[i] += stats_val;
c480ed
-        }
c480ed
-    }
c480ed
-
c480ed
-    return 0;
c480ed
+    VIR_CGROUP_BACKEND_CALL(group, getBlkioIoServiced, -1,
c480ed
+                            bytes_read, bytes_write,
c480ed
+                            requests_read, requests_write);
c480ed
 }
c480ed
 
c480ed
 
c480ed
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
c480ed
index ccce65f1e2..585a2eb353 100644
c480ed
--- a/src/util/vircgroupbackend.h
c480ed
+++ b/src/util/vircgroupbackend.h
c480ed
@@ -145,6 +145,13 @@ typedef int
c480ed
 (*virCgroupGetBlkioWeightCB)(virCgroupPtr group,
c480ed
                              unsigned int *weight);
c480ed
 
c480ed
+typedef int
c480ed
+(*virCgroupGetBlkioIoServicedCB)(virCgroupPtr group,
c480ed
+                                 long long *bytes_read,
c480ed
+                                 long long *bytes_write,
c480ed
+                                 long long *requests_read,
c480ed
+                                 long long *requests_write);
c480ed
+
c480ed
 struct _virCgroupBackend {
c480ed
     virCgroupBackendType type;
c480ed
 
c480ed
@@ -171,6 +178,7 @@ struct _virCgroupBackend {
c480ed
     /* Optional cgroup controller specific callbacks. */
c480ed
     virCgroupSetBlkioWeightCB setBlkioWeight;
c480ed
     virCgroupGetBlkioWeightCB getBlkioWeight;
c480ed
+    virCgroupGetBlkioIoServicedCB getBlkioIoServiced;
c480ed
 };
c480ed
 typedef struct _virCgroupBackend virCgroupBackend;
c480ed
 typedef virCgroupBackend *virCgroupBackendPtr;
c480ed
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
c480ed
index d67b3164ce..1afdad4afc 100644
c480ed
--- a/src/util/vircgroupv1.c
c480ed
+++ b/src/util/vircgroupv1.c
c480ed
@@ -955,6 +955,100 @@ virCgroupV1GetBlkioWeight(virCgroupPtr group,
c480ed
 }
c480ed
 
c480ed
 
c480ed
+static int
c480ed
+virCgroupV1GetBlkioIoServiced(virCgroupPtr group,
c480ed
+                              long long *bytes_read,
c480ed
+                              long long *bytes_write,
c480ed
+                              long long *requests_read,
c480ed
+                              long long *requests_write)
c480ed
+{
c480ed
+    long long stats_val;
c480ed
+    VIR_AUTOFREE(char *) str1 = NULL;
c480ed
+    VIR_AUTOFREE(char *) str2 = NULL;
c480ed
+    char *p1 = NULL;
c480ed
+    char *p2 = NULL;
c480ed
+    size_t i;
c480ed
+
c480ed
+    const char *value_names[] = {
c480ed
+        "Read ",
c480ed
+        "Write "
c480ed
+    };
c480ed
+    long long *bytes_ptrs[] = {
c480ed
+        bytes_read,
c480ed
+        bytes_write
c480ed
+    };
c480ed
+    long long *requests_ptrs[] = {
c480ed
+        requests_read,
c480ed
+        requests_write
c480ed
+    };
c480ed
+
c480ed
+    *bytes_read = 0;
c480ed
+    *bytes_write = 0;
c480ed
+    *requests_read = 0;
c480ed
+    *requests_write = 0;
c480ed
+
c480ed
+    if (virCgroupGetValueStr(group,
c480ed
+                             VIR_CGROUP_CONTROLLER_BLKIO,
c480ed
+                             "blkio.throttle.io_service_bytes", &str1) < 0)
c480ed
+        return -1;
c480ed
+
c480ed
+    if (virCgroupGetValueStr(group,
c480ed
+                             VIR_CGROUP_CONTROLLER_BLKIO,
c480ed
+                             "blkio.throttle.io_serviced", &str2) < 0)
c480ed
+        return -1;
c480ed
+
c480ed
+    /* sum up all entries of the same kind, from all devices */
c480ed
+    for (i = 0; i < ARRAY_CARDINALITY(value_names); i++) {
c480ed
+        p1 = str1;
c480ed
+        p2 = str2;
c480ed
+
c480ed
+        while ((p1 = strstr(p1, value_names[i]))) {
c480ed
+            p1 += strlen(value_names[i]);
c480ed
+            if (virStrToLong_ll(p1, &p1, 10, &stats_val) < 0) {
c480ed
+                virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
+                               _("Cannot parse byte %sstat '%s'"),
c480ed
+                               value_names[i],
c480ed
+                               p1);
c480ed
+                return -1;
c480ed
+            }
c480ed
+
c480ed
+            if (stats_val < 0 ||
c480ed
+                (stats_val > 0 && *bytes_ptrs[i] > (LLONG_MAX - stats_val)))
c480ed
+            {
c480ed
+                virReportError(VIR_ERR_OVERFLOW,
c480ed
+                               _("Sum of byte %sstat overflows"),
c480ed
+                               value_names[i]);
c480ed
+                return -1;
c480ed
+            }
c480ed
+            *bytes_ptrs[i] += stats_val;
c480ed
+        }
c480ed
+
c480ed
+        while ((p2 = strstr(p2, value_names[i]))) {
c480ed
+            p2 += strlen(value_names[i]);
c480ed
+            if (virStrToLong_ll(p2, &p2, 10, &stats_val) < 0) {
c480ed
+                virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
+                               _("Cannot parse %srequest stat '%s'"),
c480ed
+                               value_names[i],
c480ed
+                               p2);
c480ed
+                return -1;
c480ed
+            }
c480ed
+
c480ed
+            if (stats_val < 0 ||
c480ed
+                (stats_val > 0 && *requests_ptrs[i] > (LLONG_MAX - stats_val)))
c480ed
+            {
c480ed
+                virReportError(VIR_ERR_OVERFLOW,
c480ed
+                               _("Sum of %srequest stat overflows"),
c480ed
+                               value_names[i]);
c480ed
+                return -1;
c480ed
+            }
c480ed
+            *requests_ptrs[i] += stats_val;
c480ed
+        }
c480ed
+    }
c480ed
+
c480ed
+    return 0;
c480ed
+}
c480ed
+
c480ed
+
c480ed
 virCgroupBackend virCgroupV1Backend = {
c480ed
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
c480ed
 
c480ed
@@ -979,6 +1073,7 @@ virCgroupBackend virCgroupV1Backend = {
c480ed
 
c480ed
     .setBlkioWeight = virCgroupV1SetBlkioWeight,
c480ed
     .getBlkioWeight = virCgroupV1GetBlkioWeight,
c480ed
+    .getBlkioIoServiced = virCgroupV1GetBlkioIoServiced,
c480ed
 };
c480ed
 
c480ed
 
c480ed
-- 
c480ed
2.22.0
c480ed