render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
Blob Blame History Raw
From 22e8736edafd1ac369adead17d924bd6b9536817 Mon Sep 17 00:00:00 2001
Message-Id: <22e8736edafd1ac369adead17d924bd6b9536817@dist-git>
From: Pavel Hrdina <phrdina@redhat.com>
Date: Mon, 1 Jul 2019 17:06:50 +0200
Subject: [PATCH] vircgroup: extract virCgroupV1GetMemoryStat
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
(cherry picked from commit 64bfbd7ceb328d27becf98e0539d0ab4bcda8163)

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Message-Id: <cafcbd973766d6e41a57c23e83b34912fe67e3f5.1561993100.git.phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/util/vircgroup.c        | 67 ++------------------------------
 src/util/vircgroupbackend.h | 10 +++++
 src/util/vircgroupv1.c      | 76 +++++++++++++++++++++++++++++++++++++
 3 files changed, 90 insertions(+), 63 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 4053c65939..786034d555 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -1619,69 +1619,10 @@ virCgroupGetMemoryStat(virCgroupPtr group,
                        unsigned long long *inactiveFile,
                        unsigned long long *unevictable)
 {
-    int ret = -1;
-    char *stat = NULL;
-    char *line = NULL;
-    unsigned long long cacheVal = 0;
-    unsigned long long activeAnonVal = 0;
-    unsigned long long inactiveAnonVal = 0;
-    unsigned long long activeFileVal = 0;
-    unsigned long long inactiveFileVal = 0;
-    unsigned long long unevictableVal = 0;
-
-    if (virCgroupGetValueStr(group,
-                             VIR_CGROUP_CONTROLLER_MEMORY,
-                             "memory.stat",
-                             &stat) < 0) {
-        return -1;
-    }
-
-    line = stat;
-
-    while (line) {
-        char *newLine = strchr(line, '\n');
-        char *valueStr = strchr(line, ' ');
-        unsigned long long value;
-
-        if (newLine)
-            *newLine = '\0';
-
-        if (!valueStr) {
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                           _("Cannot parse 'memory.stat' cgroup file."));
-            goto cleanup;
-        }
-        *valueStr = '\0';
-
-        if (virStrToLong_ull(valueStr + 1, NULL, 10, &value) < 0)
-            goto cleanup;
-
-        if (STREQ(line, "cache"))
-            cacheVal = value >> 10;
-        else if (STREQ(line, "active_anon"))
-            activeAnonVal = value >> 10;
-        else if (STREQ(line, "inactive_anon"))
-            inactiveAnonVal = value >> 10;
-        else if (STREQ(line, "active_file"))
-            activeFileVal = value >> 10;
-        else if (STREQ(line, "inactive_file"))
-            inactiveFileVal = value >> 10;
-        else if (STREQ(line, "unevictable"))
-            unevictableVal = value >> 10;
-    }
-
-    *cache = cacheVal;
-    *activeAnon = activeAnonVal;
-    *inactiveAnon = inactiveAnonVal;
-    *activeFile = activeFileVal;
-    *inactiveFile = inactiveFileVal;
-    *unevictable = unevictableVal;
-
-    ret = 0;
-
- cleanup:
-    VIR_FREE(stat);
-    return ret;
+    VIR_CGROUP_BACKEND_CALL(group, getMemoryStat, -1, cache,
+                            activeAnon, inactiveAnon,
+                            activeFile, inactiveFile,
+                            unevictable);
 }
 
 
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
index e1f75c5c31..99754a6310 100644
--- a/src/util/vircgroupbackend.h
+++ b/src/util/vircgroupbackend.h
@@ -214,6 +214,15 @@ typedef int
 (*virCgroupSetMemoryCB)(virCgroupPtr group,
                         unsigned long long kb);
 
+typedef int
+(*virCgroupGetMemoryStatCB)(virCgroupPtr group,
+                            unsigned long long *cache,
+                            unsigned long long *activeAnon,
+                            unsigned long long *inactiveAnon,
+                            unsigned long long *activeFile,
+                            unsigned long long *inactiveFile,
+                            unsigned long long *unevictable);
+
 struct _virCgroupBackend {
     virCgroupBackendType type;
 
@@ -254,6 +263,7 @@ struct _virCgroupBackend {
     virCgroupGetBlkioDeviceWriteBpsCB getBlkioDeviceWriteBps;
 
     virCgroupSetMemoryCB setMemory;
+    virCgroupGetMemoryStatCB getMemoryStat;
 };
 typedef struct _virCgroupBackend virCgroupBackend;
 typedef virCgroupBackend *virCgroupBackendPtr;
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
index 17a4d67972..fcb2607e26 100644
--- a/src/util/vircgroupv1.c
+++ b/src/util/vircgroupv1.c
@@ -1404,6 +1404,81 @@ virCgroupV1SetMemory(virCgroupPtr group,
 }
 
 
+static int
+virCgroupV1GetMemoryStat(virCgroupPtr group,
+                         unsigned long long *cache,
+                         unsigned long long *activeAnon,
+                         unsigned long long *inactiveAnon,
+                         unsigned long long *activeFile,
+                         unsigned long long *inactiveFile,
+                         unsigned long long *unevictable)
+{
+    int ret = -1;
+    char *stat = NULL;
+    char *line = NULL;
+    unsigned long long cacheVal = 0;
+    unsigned long long activeAnonVal = 0;
+    unsigned long long inactiveAnonVal = 0;
+    unsigned long long activeFileVal = 0;
+    unsigned long long inactiveFileVal = 0;
+    unsigned long long unevictableVal = 0;
+
+    if (virCgroupGetValueStr(group,
+                             VIR_CGROUP_CONTROLLER_MEMORY,
+                             "memory.stat",
+                             &stat) < 0) {
+        return -1;
+    }
+
+    line = stat;
+
+    while (line) {
+        char *newLine = strchr(line, '\n');
+        char *valueStr = strchr(line, ' ');
+        unsigned long long value;
+
+        if (newLine)
+            *newLine = '\0';
+
+        if (!valueStr) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("Cannot parse 'memory.stat' cgroup file."));
+            goto cleanup;
+        }
+        *valueStr = '\0';
+
+        if (virStrToLong_ull(valueStr + 1, NULL, 10, &value) < 0)
+            goto cleanup;
+
+        if (STREQ(line, "cache"))
+            cacheVal = value >> 10;
+        else if (STREQ(line, "active_anon"))
+            activeAnonVal = value >> 10;
+        else if (STREQ(line, "inactive_anon"))
+            inactiveAnonVal = value >> 10;
+        else if (STREQ(line, "active_file"))
+            activeFileVal = value >> 10;
+        else if (STREQ(line, "inactive_file"))
+            inactiveFileVal = value >> 10;
+        else if (STREQ(line, "unevictable"))
+            unevictableVal = value >> 10;
+    }
+
+    *cache = cacheVal;
+    *activeAnon = activeAnonVal;
+    *inactiveAnon = inactiveAnonVal;
+    *activeFile = activeFileVal;
+    *inactiveFile = inactiveFileVal;
+    *unevictable = unevictableVal;
+
+    ret = 0;
+
+ cleanup:
+    VIR_FREE(stat);
+    return ret;
+}
+
+
 virCgroupBackend virCgroupV1Backend = {
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
 
@@ -1442,6 +1517,7 @@ virCgroupBackend virCgroupV1Backend = {
     .getBlkioDeviceWriteBps = virCgroupV1GetBlkioDeviceWriteBps,
 
     .setMemory = virCgroupV1SetMemory,
+    .getMemoryStat = virCgroupV1GetMemoryStat,
 };
 
 
-- 
2.22.0