[libvirt] [PATCH 30/47] vircgroup: extract virCgroupV1GetMemoryStat

Pavel Hrdina phrdina at redhat.com
Tue Sep 18 15:45:51 UTC 2018


Signed-off-by: Pavel Hrdina <phrdina at redhat.com>
---
 src/util/vircgroup.c        | 67 +++-------------------------------
 src/util/vircgroupbackend.h | 10 ++++++
 src/util/vircgroupv1.c      | 71 +++++++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+), 63 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index b1c1bc9cf0..22db0a4bf1 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -1621,69 +1621,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 2d59e4af20..5ccda8e044 100644
--- a/src/util/vircgroupbackend.h
+++ b/src/util/vircgroupbackend.h
@@ -206,6 +206,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;
 
@@ -246,6 +255,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 3235ef3d2d..cfe29649fb 100644
--- a/src/util/vircgroupv1.c
+++ b/src/util/vircgroupv1.c
@@ -1403,6 +1403,76 @@ 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)
+{
+    VIR_AUTOFREE(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."));
+            return -1;
+        }
+        *valueStr = '\0';
+
+        if (virStrToLong_ull(valueStr + 1, NULL, 10, &value) < 0)
+            return -1;
+
+        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;
+
+    return 0;
+}
+
+
 virCgroupBackend virCgroupV1Backend = {
     .type = VIR_CGROUP_BACKEND_TYPE_V1,
 
@@ -1441,6 +1511,7 @@ virCgroupBackend virCgroupV1Backend = {
     .getBlkioDeviceWriteBps = virCgroupV1GetBlkioDeviceWriteBps,
 
     .setMemory = virCgroupV1SetMemory,
+    .getMemoryStat = virCgroupV1GetMemoryStat,
 };
 
 
-- 
2.17.1




More information about the libvir-list mailing list