Pablo Greco 40546a
From 7b4215f8a38221fd90bf15a3ced42cce55adf105 Mon Sep 17 00:00:00 2001
Pablo Greco 40546a
Message-Id: <7b4215f8a38221fd90bf15a3ced42cce55adf105@dist-git>
Pablo Greco 40546a
From: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Date: Mon, 1 Jul 2019 17:07:40 +0200
Pablo Greco 40546a
Subject: [PATCH] vircgroup: introduce virCgroupV2GetMemoryStat
Pablo Greco 40546a
MIME-Version: 1.0
Pablo Greco 40546a
Content-Type: text/plain; charset=UTF-8
Pablo Greco 40546a
Content-Transfer-Encoding: 8bit
Pablo Greco 40546a
Pablo Greco 40546a
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
(cherry picked from commit d080c001660ed0f173066b3db63ecac4edb773fc)
Pablo Greco 40546a
Pablo Greco 40546a
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
Pablo Greco 40546a
Pablo Greco 40546a
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Message-Id: <9ebb30d0c01bdb6e356928701cd56501fe43af5e.1561993100.git.phrdina@redhat.com>
Pablo Greco 40546a
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Pablo Greco 40546a
---
Pablo Greco 40546a
 src/util/vircgroupv2.c | 75 ++++++++++++++++++++++++++++++++++++++++++
Pablo Greco 40546a
 1 file changed, 75 insertions(+)
Pablo Greco 40546a
Pablo Greco 40546a
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
Pablo Greco 40546a
index 1b7215d98b..1c7854909f 100644
Pablo Greco 40546a
--- a/src/util/vircgroupv2.c
Pablo Greco 40546a
+++ b/src/util/vircgroupv2.c
Pablo Greco 40546a
@@ -1042,6 +1042,80 @@ virCgroupV2SetMemory(virCgroupPtr group,
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
+static int
Pablo Greco 40546a
+virCgroupV2GetMemoryStat(virCgroupPtr group,
Pablo Greco 40546a
+                         unsigned long long *cache,
Pablo Greco 40546a
+                         unsigned long long *activeAnon,
Pablo Greco 40546a
+                         unsigned long long *inactiveAnon,
Pablo Greco 40546a
+                         unsigned long long *activeFile,
Pablo Greco 40546a
+                         unsigned long long *inactiveFile,
Pablo Greco 40546a
+                         unsigned long long *unevictable)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    VIR_AUTOFREE(char *) stat = NULL;
Pablo Greco 40546a
+    char *line = NULL;
Pablo Greco 40546a
+    unsigned long long cacheVal = 0;
Pablo Greco 40546a
+    unsigned long long activeAnonVal = 0;
Pablo Greco 40546a
+    unsigned long long inactiveAnonVal = 0;
Pablo Greco 40546a
+    unsigned long long activeFileVal = 0;
Pablo Greco 40546a
+    unsigned long long inactiveFileVal = 0;
Pablo Greco 40546a
+    unsigned long long unevictableVal = 0;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (virCgroupGetValueStr(group,
Pablo Greco 40546a
+                             VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
+                             "memory.stat",
Pablo Greco 40546a
+                             &stat) < 0) {
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    line = stat;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    while (line) {
Pablo Greco 40546a
+        char *newLine = strchr(line, '\n');
Pablo Greco 40546a
+        char *valueStr = strchr(line, ' ');
Pablo Greco 40546a
+        unsigned long long value;
Pablo Greco 40546a
+
Pablo Greco 40546a
+        if (newLine)
Pablo Greco 40546a
+            *newLine = '\0';
Pablo Greco 40546a
+
Pablo Greco 40546a
+        if (!valueStr) {
Pablo Greco 40546a
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
Pablo Greco 40546a
+                           _("Cannot parse 'memory.stat' cgroup file."));
Pablo Greco 40546a
+            return -1;
Pablo Greco 40546a
+        }
Pablo Greco 40546a
+        *valueStr = '\0';
Pablo Greco 40546a
+
Pablo Greco 40546a
+        if (virStrToLong_ull(valueStr + 1, NULL, 10, &value) < 0) {
Pablo Greco 40546a
+            virReportError(VIR_ERR_INTERNAL_ERROR,
Pablo Greco 40546a
+                           _("Unable to parse '%s' as an integer"),
Pablo Greco 40546a
+                           valueStr + 1);
Pablo Greco 40546a
+            return -1;
Pablo Greco 40546a
+        }
Pablo Greco 40546a
+
Pablo Greco 40546a
+        if (STREQ(line, "file"))
Pablo Greco 40546a
+            cacheVal = value >> 10;
Pablo Greco 40546a
+        else if (STREQ(line, "active_anon"))
Pablo Greco 40546a
+            activeAnonVal = value >> 10;
Pablo Greco 40546a
+        else if (STREQ(line, "inactive_anon"))
Pablo Greco 40546a
+            inactiveAnonVal = value >> 10;
Pablo Greco 40546a
+        else if (STREQ(line, "active_file"))
Pablo Greco 40546a
+            activeFileVal = value >> 10;
Pablo Greco 40546a
+        else if (STREQ(line, "inactive_file"))
Pablo Greco 40546a
+            inactiveFileVal = value >> 10;
Pablo Greco 40546a
+        else if (STREQ(line, "unevictable"))
Pablo Greco 40546a
+            unevictableVal = value >> 10;
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    *cache = cacheVal;
Pablo Greco 40546a
+    *activeAnon = activeAnonVal;
Pablo Greco 40546a
+    *inactiveAnon = inactiveAnonVal;
Pablo Greco 40546a
+    *activeFile = activeFileVal;
Pablo Greco 40546a
+    *inactiveFile = inactiveFileVal;
Pablo Greco 40546a
+    *unevictable = unevictableVal;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    return 0;
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
 virCgroupBackend virCgroupV2Backend = {
Pablo Greco 40546a
     .type = VIR_CGROUP_BACKEND_TYPE_V2,
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -1080,6 +1154,7 @@ virCgroupBackend virCgroupV2Backend = {
Pablo Greco 40546a
     .getBlkioDeviceWriteBps = virCgroupV2GetBlkioDeviceWriteBps,
Pablo Greco 40546a
 
Pablo Greco 40546a
     .setMemory = virCgroupV2SetMemory,
Pablo Greco 40546a
+    .getMemoryStat = virCgroupV2GetMemoryStat,
Pablo Greco 40546a
 };
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
-- 
Pablo Greco 40546a
2.22.0
Pablo Greco 40546a