render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Pablo Greco 40546a
From 97f3e1cef9707ac7b1e744aef51880d4538e286a Mon Sep 17 00:00:00 2001
Pablo Greco 40546a
Message-Id: <97f3e1cef9707ac7b1e744aef51880d4538e286a@dist-git>
Pablo Greco 40546a
From: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Date: Mon, 1 Jul 2019 17:07:01 +0200
Pablo Greco 40546a
Subject: [PATCH] vircgroup: extract virCgroupV1GetCpuacctStat
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
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
Pablo Greco 40546a
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Pablo Greco 40546a
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
(cherry picked from commit e294615f9d849ee4923621db2224fce713018458)
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: <06921474355374c68d49725851f915e9bf44feeb.1561993100.git.phrdina@redhat.com>
Pablo Greco 40546a
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Pablo Greco 40546a
---
Pablo Greco 40546a
 src/util/vircgroup.c        | 39 +-----------------------------
Pablo Greco 40546a
 src/util/vircgroupbackend.h |  6 +++++
Pablo Greco 40546a
 src/util/vircgroupv1.c      | 47 +++++++++++++++++++++++++++++++++++++
Pablo Greco 40546a
 3 files changed, 54 insertions(+), 38 deletions(-)
Pablo Greco 40546a
Pablo Greco 40546a
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
Pablo Greco 40546a
index 4f791204d3..090fe140bb 100644
Pablo Greco 40546a
--- a/src/util/vircgroup.c
Pablo Greco 40546a
+++ b/src/util/vircgroup.c
Pablo Greco 40546a
@@ -2596,44 +2596,7 @@ int
Pablo Greco 40546a
 virCgroupGetCpuacctStat(virCgroupPtr group, unsigned long long *user,
Pablo Greco 40546a
                         unsigned long long *sys)
Pablo Greco 40546a
 {
Pablo Greco 40546a
-    VIR_AUTOFREE(char *) str = NULL;
Pablo Greco 40546a
-    char *p;
Pablo Greco 40546a
-    static double scale = -1.0;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPUACCT,
Pablo Greco 40546a
-                             "cpuacct.stat", &str) < 0)
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    if (!(p = STRSKIP(str, "user ")) ||
Pablo Greco 40546a
-        virStrToLong_ull(p, &p, 10, user) < 0) {
Pablo Greco 40546a
-        virReportError(VIR_ERR_INTERNAL_ERROR,
Pablo Greco 40546a
-                       _("Cannot parse user stat '%s'"),
Pablo Greco 40546a
-                       p);
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-    }
Pablo Greco 40546a
-    if (!(p = STRSKIP(p, "\nsystem ")) ||
Pablo Greco 40546a
-        virStrToLong_ull(p, NULL, 10, sys) < 0) {
Pablo Greco 40546a
-        virReportError(VIR_ERR_INTERNAL_ERROR,
Pablo Greco 40546a
-                       _("Cannot parse sys stat '%s'"),
Pablo Greco 40546a
-                       p);
Pablo Greco 40546a
-        return -1;
Pablo Greco 40546a
-    }
Pablo Greco 40546a
-    /* times reported are in system ticks (generally 100 Hz), but that
Pablo Greco 40546a
-     * rate can theoretically vary between machines.  Scale things
Pablo Greco 40546a
-     * into approximate nanoseconds.  */
Pablo Greco 40546a
-    if (scale < 0) {
Pablo Greco 40546a
-        long ticks_per_sec = sysconf(_SC_CLK_TCK);
Pablo Greco 40546a
-        if (ticks_per_sec == -1) {
Pablo Greco 40546a
-            virReportSystemError(errno, "%s",
Pablo Greco 40546a
-                                 _("Cannot determine system clock HZ"));
Pablo Greco 40546a
-            return -1;
Pablo Greco 40546a
-        }
Pablo Greco 40546a
-        scale = 1000000000.0 / ticks_per_sec;
Pablo Greco 40546a
-    }
Pablo Greco 40546a
-    *user *= scale;
Pablo Greco 40546a
-    *sys *= scale;
Pablo Greco 40546a
-
Pablo Greco 40546a
-    return 0;
Pablo Greco 40546a
+    VIR_CGROUP_BACKEND_CALL(group, getCpuacctStat, -1, user, sys);
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
Pablo Greco 40546a
index 39a75a69f4..3e3a73777d 100644
Pablo Greco 40546a
--- a/src/util/vircgroupbackend.h
Pablo Greco 40546a
+++ b/src/util/vircgroupbackend.h
Pablo Greco 40546a
@@ -311,6 +311,11 @@ typedef int
Pablo Greco 40546a
 (*virCgroupGetCpuacctPercpuUsageCB)(virCgroupPtr group,
Pablo Greco 40546a
                                     char **usage);
Pablo Greco 40546a
 
Pablo Greco 40546a
+typedef int
Pablo Greco 40546a
+(*virCgroupGetCpuacctStatCB)(virCgroupPtr group,
Pablo Greco 40546a
+                             unsigned long long *user,
Pablo Greco 40546a
+                             unsigned long long *sys);
Pablo Greco 40546a
+
Pablo Greco 40546a
 struct _virCgroupBackend {
Pablo Greco 40546a
     virCgroupBackendType type;
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -376,6 +381,7 @@ struct _virCgroupBackend {
Pablo Greco 40546a
 
Pablo Greco 40546a
     virCgroupGetCpuacctUsageCB getCpuacctUsage;
Pablo Greco 40546a
     virCgroupGetCpuacctPercpuUsageCB getCpuacctPercpuUsage;
Pablo Greco 40546a
+    virCgroupGetCpuacctStatCB getCpuacctStat;
Pablo Greco 40546a
 };
Pablo Greco 40546a
 typedef struct _virCgroupBackend virCgroupBackend;
Pablo Greco 40546a
 typedef virCgroupBackend *virCgroupBackendPtr;
Pablo Greco 40546a
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
Pablo Greco 40546a
index 9fb0c5cb96..23a91adf60 100644
Pablo Greco 40546a
--- a/src/util/vircgroupv1.c
Pablo Greco 40546a
+++ b/src/util/vircgroupv1.c
Pablo Greco 40546a
@@ -1883,6 +1883,52 @@ virCgroupV1GetCpuacctPercpuUsage(virCgroupPtr group,
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
+static int
Pablo Greco 40546a
+virCgroupV1GetCpuacctStat(virCgroupPtr group,
Pablo Greco 40546a
+                          unsigned long long *user,
Pablo Greco 40546a
+                          unsigned long long *sys)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    VIR_AUTOFREE(char *) str = NULL;
Pablo Greco 40546a
+    char *p;
Pablo Greco 40546a
+    static double scale = -1.0;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPUACCT,
Pablo Greco 40546a
+                             "cpuacct.stat", &str) < 0)
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (!(p = STRSKIP(str, "user ")) ||
Pablo Greco 40546a
+        virStrToLong_ull(p, &p, 10, user) < 0) {
Pablo Greco 40546a
+        virReportError(VIR_ERR_INTERNAL_ERROR,
Pablo Greco 40546a
+                       _("Cannot parse user stat '%s'"),
Pablo Greco 40546a
+                       p);
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+    if (!(p = STRSKIP(p, "\nsystem ")) ||
Pablo Greco 40546a
+        virStrToLong_ull(p, NULL, 10, sys) < 0) {
Pablo Greco 40546a
+        virReportError(VIR_ERR_INTERNAL_ERROR,
Pablo Greco 40546a
+                       _("Cannot parse sys stat '%s'"),
Pablo Greco 40546a
+                       p);
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+    /* times reported are in system ticks (generally 100 Hz), but that
Pablo Greco 40546a
+     * rate can theoretically vary between machines.  Scale things
Pablo Greco 40546a
+     * into approximate nanoseconds.  */
Pablo Greco 40546a
+    if (scale < 0) {
Pablo Greco 40546a
+        long ticks_per_sec = sysconf(_SC_CLK_TCK);
Pablo Greco 40546a
+        if (ticks_per_sec == -1) {
Pablo Greco 40546a
+            virReportSystemError(errno, "%s",
Pablo Greco 40546a
+                                 _("Cannot determine system clock HZ"));
Pablo Greco 40546a
+            return -1;
Pablo Greco 40546a
+        }
Pablo Greco 40546a
+        scale = 1000000000.0 / ticks_per_sec;
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+    *user *= scale;
Pablo Greco 40546a
+    *sys *= scale;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    return 0;
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
 virCgroupBackend virCgroupV1Backend = {
Pablo Greco 40546a
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -1946,6 +1992,7 @@ virCgroupBackend virCgroupV1Backend = {
Pablo Greco 40546a
 
Pablo Greco 40546a
     .getCpuacctUsage = virCgroupV1GetCpuacctUsage,
Pablo Greco 40546a
     .getCpuacctPercpuUsage = virCgroupV1GetCpuacctPercpuUsage,
Pablo Greco 40546a
+    .getCpuacctStat = virCgroupV1GetCpuacctStat,
Pablo Greco 40546a
 };
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
-- 
Pablo Greco 40546a
2.22.0
Pablo Greco 40546a