[PATCH 41/47] vircgroup: extract virCgroupV1GetCpuacctStat

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Pavel Hrdina <phrdina@xxxxxxxxxx>
---
 src/util/vircgroup.c        | 39 +-----------------------------
 src/util/vircgroupbackend.h |  6 +++++
 src/util/vircgroupv1.c      | 47 +++++++++++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+), 38 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 598147acf7..9ba2318c0b 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -2598,44 +2598,7 @@ int
 virCgroupGetCpuacctStat(virCgroupPtr group, unsigned long long *user,
                         unsigned long long *sys)
 {
-    VIR_AUTOFREE(char *) str = NULL;
-    char *p;
-    static double scale = -1.0;
-
-    if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPUACCT,
-                             "cpuacct.stat", &str) < 0)
-        return -1;
-
-    if (!(p = STRSKIP(str, "user ")) ||
-        virStrToLong_ull(p, &p, 10, user) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Cannot parse user stat '%s'"),
-                       p);
-        return -1;
-    }
-    if (!(p = STRSKIP(p, "\nsystem ")) ||
-        virStrToLong_ull(p, NULL, 10, sys) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Cannot parse sys stat '%s'"),
-                       p);
-        return -1;
-    }
-    /* times reported are in system ticks (generally 100 Hz), but that
-     * rate can theoretically vary between machines.  Scale things
-     * into approximate nanoseconds.  */
-    if (scale < 0) {
-        long ticks_per_sec = sysconf(_SC_CLK_TCK);
-        if (ticks_per_sec == -1) {
-            virReportSystemError(errno, "%s",
-                                 _("Cannot determine system clock HZ"));
-            return -1;
-        }
-        scale = 1000000000.0 / ticks_per_sec;
-    }
-    *user *= scale;
-    *sys *= scale;
-
-    return 0;
+    VIR_CGROUP_BACKEND_CALL(group, getCpuacctStat, -1, user, sys);
 }
 
 
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
index 380ab9850d..a80ad0b5d3 100644
--- a/src/util/vircgroupbackend.h
+++ b/src/util/vircgroupbackend.h
@@ -303,6 +303,11 @@ typedef int
 (*virCgroupGetCpuacctPercpuUsageCB)(virCgroupPtr group,
                                     char **usage);
 
+typedef int
+(*virCgroupGetCpuacctStatCB)(virCgroupPtr group,
+                             unsigned long long *user,
+                             unsigned long long *sys);
+
 struct _virCgroupBackend {
     virCgroupBackendType type;
 
@@ -368,6 +373,7 @@ struct _virCgroupBackend {
 
     virCgroupGetCpuacctUsageCB getCpuacctUsage;
     virCgroupGetCpuacctPercpuUsageCB getCpuacctPercpuUsage;
+    virCgroupGetCpuacctStatCB getCpuacctStat;
 };
 typedef struct _virCgroupBackend virCgroupBackend;
 typedef virCgroupBackend *virCgroupBackendPtr;
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
index a827917db1..f99cf5c6d6 100644
--- a/src/util/vircgroupv1.c
+++ b/src/util/vircgroupv1.c
@@ -1877,6 +1877,52 @@ virCgroupV1GetCpuacctPercpuUsage(virCgroupPtr group,
 }
 
 
+static int
+virCgroupV1GetCpuacctStat(virCgroupPtr group,
+                          unsigned long long *user,
+                          unsigned long long *sys)
+{
+    VIR_AUTOFREE(char *) str = NULL;
+    char *p;
+    static double scale = -1.0;
+
+    if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPUACCT,
+                             "cpuacct.stat", &str) < 0)
+        return -1;
+
+    if (!(p = STRSKIP(str, "user ")) ||
+        virStrToLong_ull(p, &p, 10, user) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Cannot parse user stat '%s'"),
+                       p);
+        return -1;
+    }
+    if (!(p = STRSKIP(p, "\nsystem ")) ||
+        virStrToLong_ull(p, NULL, 10, sys) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Cannot parse sys stat '%s'"),
+                       p);
+        return -1;
+    }
+    /* times reported are in system ticks (generally 100 Hz), but that
+     * rate can theoretically vary between machines.  Scale things
+     * into approximate nanoseconds.  */
+    if (scale < 0) {
+        long ticks_per_sec = sysconf(_SC_CLK_TCK);
+        if (ticks_per_sec == -1) {
+            virReportSystemError(errno, "%s",
+                                 _("Cannot determine system clock HZ"));
+            return -1;
+        }
+        scale = 1000000000.0 / ticks_per_sec;
+    }
+    *user *= scale;
+    *sys *= scale;
+
+    return 0;
+}
+
+
 virCgroupBackend virCgroupV1Backend = {
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
 
@@ -1940,6 +1986,7 @@ virCgroupBackend virCgroupV1Backend = {
 
     .getCpuacctUsage = virCgroupV1GetCpuacctUsage,
     .getCpuacctPercpuUsage = virCgroupV1GetCpuacctPercpuUsage,
+    .getCpuacctStat = virCgroupV1GetCpuacctStat,
 };
 
 
-- 
2.17.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux