c313de
From 22e8736edafd1ac369adead17d924bd6b9536817 Mon Sep 17 00:00:00 2001
c313de
Message-Id: <22e8736edafd1ac369adead17d924bd6b9536817@dist-git>
c313de
From: Pavel Hrdina <phrdina@redhat.com>
c313de
Date: Mon, 1 Jul 2019 17:06:50 +0200
c313de
Subject: [PATCH] vircgroup: extract virCgroupV1GetMemoryStat
c313de
MIME-Version: 1.0
c313de
Content-Type: text/plain; charset=UTF-8
c313de
Content-Transfer-Encoding: 8bit
c313de
c313de
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
c313de
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c313de
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c313de
(cherry picked from commit 64bfbd7ceb328d27becf98e0539d0ab4bcda8163)
c313de
c313de
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
c313de
c313de
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c313de
Message-Id: <cafcbd973766d6e41a57c23e83b34912fe67e3f5.1561993100.git.phrdina@redhat.com>
c313de
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c313de
---
c313de
 src/util/vircgroup.c        | 67 ++------------------------------
c313de
 src/util/vircgroupbackend.h | 10 +++++
c313de
 src/util/vircgroupv1.c      | 76 +++++++++++++++++++++++++++++++++++++
c313de
 3 files changed, 90 insertions(+), 63 deletions(-)
c313de
c313de
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
c313de
index 4053c65939..786034d555 100644
c313de
--- a/src/util/vircgroup.c
c313de
+++ b/src/util/vircgroup.c
c313de
@@ -1619,69 +1619,10 @@ virCgroupGetMemoryStat(virCgroupPtr group,
c313de
                        unsigned long long *inactiveFile,
c313de
                        unsigned long long *unevictable)
c313de
 {
c313de
-    int ret = -1;
c313de
-    char *stat = NULL;
c313de
-    char *line = NULL;
c313de
-    unsigned long long cacheVal = 0;
c313de
-    unsigned long long activeAnonVal = 0;
c313de
-    unsigned long long inactiveAnonVal = 0;
c313de
-    unsigned long long activeFileVal = 0;
c313de
-    unsigned long long inactiveFileVal = 0;
c313de
-    unsigned long long unevictableVal = 0;
c313de
-
c313de
-    if (virCgroupGetValueStr(group,
c313de
-                             VIR_CGROUP_CONTROLLER_MEMORY,
c313de
-                             "memory.stat",
c313de
-                             &stat) < 0) {
c313de
-        return -1;
c313de
-    }
c313de
-
c313de
-    line = stat;
c313de
-
c313de
-    while (line) {
c313de
-        char *newLine = strchr(line, '\n');
c313de
-        char *valueStr = strchr(line, ' ');
c313de
-        unsigned long long value;
c313de
-
c313de
-        if (newLine)
c313de
-            *newLine = '\0';
c313de
-
c313de
-        if (!valueStr) {
c313de
-            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c313de
-                           _("Cannot parse 'memory.stat' cgroup file."));
c313de
-            goto cleanup;
c313de
-        }
c313de
-        *valueStr = '\0';
c313de
-
c313de
-        if (virStrToLong_ull(valueStr + 1, NULL, 10, &value) < 0)
c313de
-            goto cleanup;
c313de
-
c313de
-        if (STREQ(line, "cache"))
c313de
-            cacheVal = value >> 10;
c313de
-        else if (STREQ(line, "active_anon"))
c313de
-            activeAnonVal = value >> 10;
c313de
-        else if (STREQ(line, "inactive_anon"))
c313de
-            inactiveAnonVal = value >> 10;
c313de
-        else if (STREQ(line, "active_file"))
c313de
-            activeFileVal = value >> 10;
c313de
-        else if (STREQ(line, "inactive_file"))
c313de
-            inactiveFileVal = value >> 10;
c313de
-        else if (STREQ(line, "unevictable"))
c313de
-            unevictableVal = value >> 10;
c313de
-    }
c313de
-
c313de
-    *cache = cacheVal;
c313de
-    *activeAnon = activeAnonVal;
c313de
-    *inactiveAnon = inactiveAnonVal;
c313de
-    *activeFile = activeFileVal;
c313de
-    *inactiveFile = inactiveFileVal;
c313de
-    *unevictable = unevictableVal;
c313de
-
c313de
-    ret = 0;
c313de
-
c313de
- cleanup:
c313de
-    VIR_FREE(stat);
c313de
-    return ret;
c313de
+    VIR_CGROUP_BACKEND_CALL(group, getMemoryStat, -1, cache,
c313de
+                            activeAnon, inactiveAnon,
c313de
+                            activeFile, inactiveFile,
c313de
+                            unevictable);
c313de
 }
c313de
 
c313de
 
c313de
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
c313de
index e1f75c5c31..99754a6310 100644
c313de
--- a/src/util/vircgroupbackend.h
c313de
+++ b/src/util/vircgroupbackend.h
c313de
@@ -214,6 +214,15 @@ typedef int
c313de
 (*virCgroupSetMemoryCB)(virCgroupPtr group,
c313de
                         unsigned long long kb);
c313de
 
c313de
+typedef int
c313de
+(*virCgroupGetMemoryStatCB)(virCgroupPtr group,
c313de
+                            unsigned long long *cache,
c313de
+                            unsigned long long *activeAnon,
c313de
+                            unsigned long long *inactiveAnon,
c313de
+                            unsigned long long *activeFile,
c313de
+                            unsigned long long *inactiveFile,
c313de
+                            unsigned long long *unevictable);
c313de
+
c313de
 struct _virCgroupBackend {
c313de
     virCgroupBackendType type;
c313de
 
c313de
@@ -254,6 +263,7 @@ struct _virCgroupBackend {
c313de
     virCgroupGetBlkioDeviceWriteBpsCB getBlkioDeviceWriteBps;
c313de
 
c313de
     virCgroupSetMemoryCB setMemory;
c313de
+    virCgroupGetMemoryStatCB getMemoryStat;
c313de
 };
c313de
 typedef struct _virCgroupBackend virCgroupBackend;
c313de
 typedef virCgroupBackend *virCgroupBackendPtr;
c313de
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
c313de
index 17a4d67972..fcb2607e26 100644
c313de
--- a/src/util/vircgroupv1.c
c313de
+++ b/src/util/vircgroupv1.c
c313de
@@ -1404,6 +1404,81 @@ virCgroupV1SetMemory(virCgroupPtr group,
c313de
 }
c313de
 
c313de
 
c313de
+static int
c313de
+virCgroupV1GetMemoryStat(virCgroupPtr group,
c313de
+                         unsigned long long *cache,
c313de
+                         unsigned long long *activeAnon,
c313de
+                         unsigned long long *inactiveAnon,
c313de
+                         unsigned long long *activeFile,
c313de
+                         unsigned long long *inactiveFile,
c313de
+                         unsigned long long *unevictable)
c313de
+{
c313de
+    int ret = -1;
c313de
+    char *stat = NULL;
c313de
+    char *line = NULL;
c313de
+    unsigned long long cacheVal = 0;
c313de
+    unsigned long long activeAnonVal = 0;
c313de
+    unsigned long long inactiveAnonVal = 0;
c313de
+    unsigned long long activeFileVal = 0;
c313de
+    unsigned long long inactiveFileVal = 0;
c313de
+    unsigned long long unevictableVal = 0;
c313de
+
c313de
+    if (virCgroupGetValueStr(group,
c313de
+                             VIR_CGROUP_CONTROLLER_MEMORY,
c313de
+                             "memory.stat",
c313de
+                             &stat) < 0) {
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    line = stat;
c313de
+
c313de
+    while (line) {
c313de
+        char *newLine = strchr(line, '\n');
c313de
+        char *valueStr = strchr(line, ' ');
c313de
+        unsigned long long value;
c313de
+
c313de
+        if (newLine)
c313de
+            *newLine = '\0';
c313de
+
c313de
+        if (!valueStr) {
c313de
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c313de
+                           _("Cannot parse 'memory.stat' cgroup file."));
c313de
+            goto cleanup;
c313de
+        }
c313de
+        *valueStr = '\0';
c313de
+
c313de
+        if (virStrToLong_ull(valueStr + 1, NULL, 10, &value) < 0)
c313de
+            goto cleanup;
c313de
+
c313de
+        if (STREQ(line, "cache"))
c313de
+            cacheVal = value >> 10;
c313de
+        else if (STREQ(line, "active_anon"))
c313de
+            activeAnonVal = value >> 10;
c313de
+        else if (STREQ(line, "inactive_anon"))
c313de
+            inactiveAnonVal = value >> 10;
c313de
+        else if (STREQ(line, "active_file"))
c313de
+            activeFileVal = value >> 10;
c313de
+        else if (STREQ(line, "inactive_file"))
c313de
+            inactiveFileVal = value >> 10;
c313de
+        else if (STREQ(line, "unevictable"))
c313de
+            unevictableVal = value >> 10;
c313de
+    }
c313de
+
c313de
+    *cache = cacheVal;
c313de
+    *activeAnon = activeAnonVal;
c313de
+    *inactiveAnon = inactiveAnonVal;
c313de
+    *activeFile = activeFileVal;
c313de
+    *inactiveFile = inactiveFileVal;
c313de
+    *unevictable = unevictableVal;
c313de
+
c313de
+    ret = 0;
c313de
+
c313de
+ cleanup:
c313de
+    VIR_FREE(stat);
c313de
+    return ret;
c313de
+}
c313de
+
c313de
+
c313de
 virCgroupBackend virCgroupV1Backend = {
c313de
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
c313de
 
c313de
@@ -1442,6 +1517,7 @@ virCgroupBackend virCgroupV1Backend = {
c313de
     .getBlkioDeviceWriteBps = virCgroupV1GetBlkioDeviceWriteBps,
c313de
 
c313de
     .setMemory = virCgroupV1SetMemory,
c313de
+    .getMemoryStat = virCgroupV1GetMemoryStat,
c313de
 };
c313de
 
c313de
 
c313de
-- 
c313de
2.22.0
c313de