render / rpms / libvirt

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