]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
vircgroup: extract virCgroupV1GetMemoryStat
authorPavel Hrdina <phrdina@redhat.com>
Fri, 17 Aug 2018 13:55:47 +0000 (15:55 +0200)
committerPavel Hrdina <phrdina@redhat.com>
Tue, 25 Sep 2018 11:40:22 +0000 (13:40 +0200)
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
src/util/vircgroup.c
src/util/vircgroupbackend.h
src/util/vircgroupv1.c

index 19c82b67bc693d5dc9b2c6ff649821a2bf7372d0..55a17acae39677d57c7debf94529b9723acb8fc2 100644 (file)
@@ -1615,69 +1615,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);
 }
 
 
index e1f75c5c315f150779772ec251ca6a0db348110f..99754a63102894abf0a174ba6fd6e28c655764ad 100644 (file)
@@ -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;
index af92df3d8f6977f5c6ec5fe5a13560a1c28bb6e9..9ecdd253cb4c9290a87153dce7d5dc9a9d7c8cea 100644 (file)
@@ -1403,6 +1403,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,
 
@@ -1441,6 +1516,7 @@ virCgroupBackend virCgroupV1Backend = {
     .getBlkioDeviceWriteBps = virCgroupV1GetBlkioDeviceWriteBps,
 
     .setMemory = virCgroupV1SetMemory,
+    .getMemoryStat = virCgroupV1GetMemoryStat,
 };