c313de
From d91df2c6090f79ef0135f2240181b8dbf87597b0 Mon Sep 17 00:00:00 2001
c313de
Message-Id: <d91df2c6090f79ef0135f2240181b8dbf87597b0@dist-git>
c313de
From: Pavel Hrdina <phrdina@redhat.com>
c313de
Date: Mon, 1 Jul 2019 17:06:01 +0200
c313de
Subject: [PATCH] vircgroup: Introduce virCgroupGetMemoryStat
c313de
MIME-Version: 1.0
c313de
Content-Type: text/plain; charset=UTF-8
c313de
Content-Transfer-Encoding: 8bit
c313de
c313de
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c313de
(cherry picked from commit 901d2b9c8716f2717439d5843cab412ab8d7b247)
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: <b70cd9890f218f943b094bc6b040867c6d69bc6b.1561993099.git.phrdina@redhat.com>
c313de
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c313de
---
c313de
 src/libvirt_private.syms |  1 +
c313de
 src/util/vircgroup.c     | 88 ++++++++++++++++++++++++++++++++++++++++
c313de
 src/util/vircgroup.h     |  7 ++++
c313de
 3 files changed, 96 insertions(+)
c313de
c313de
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
c313de
index 47695eb507..2ec9d8f4bf 100644
c313de
--- a/src/libvirt_private.syms
c313de
+++ b/src/libvirt_private.syms
c313de
@@ -1551,6 +1551,7 @@ virCgroupGetDomainTotalCpuStats;
c313de
 virCgroupGetFreezerState;
c313de
 virCgroupGetMemoryHardLimit;
c313de
 virCgroupGetMemorySoftLimit;
c313de
+virCgroupGetMemoryStat;
c313de
 virCgroupGetMemoryUsage;
c313de
 virCgroupGetMemSwapHardLimit;
c313de
 virCgroupGetMemSwapUsage;
c313de
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
c313de
index a94f958d75..0ed83932ac 100644
c313de
--- a/src/util/vircgroup.c
c313de
+++ b/src/util/vircgroup.c
c313de
@@ -2427,6 +2427,94 @@ virCgroupSetMemory(virCgroupPtr group, unsigned long long kb)
c313de
 }
c313de
 
c313de
 
c313de
+/**
c313de
+ * virCgroupGetMemoryStat:
c313de
+ *
c313de
+ * @group: The cgroup to change memory for
c313de
+ * @cache: page cache memory in KiB
c313de
+ * @activeAnon: anonymous and swap cache memory in KiB
c313de
+ * @inactiveAnon: anonymous and swap cache memory in KiB
c313de
+ * @activeFile: file-backed memory in KiB
c313de
+ * @inactiveFile: file-backed memory in KiB
c313de
+ * @unevictable: memory that cannot be reclaimed KiB
c313de
+ *
c313de
+ * Returns: 0 on success, -1 on error
c313de
+ */
c313de
+int
c313de
+virCgroupGetMemoryStat(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
 /**
c313de
  * virCgroupGetMemoryUsage:
c313de
  *
c313de
diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h
c313de
index 48be077aba..c7fdaaede4 100644
c313de
--- a/src/util/vircgroup.h
c313de
+++ b/src/util/vircgroup.h
c313de
@@ -177,6 +177,13 @@ int virCgroupGetBlkioDeviceWriteBps(virCgroupPtr group,
c313de
                                     unsigned long long *wbps);
c313de
 
c313de
 int virCgroupSetMemory(virCgroupPtr group, unsigned long long kb);
c313de
+int virCgroupGetMemoryStat(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
 int virCgroupGetMemoryUsage(virCgroupPtr group, unsigned long *kb);
c313de
 
c313de
 int virCgroupSetMemoryHardLimit(virCgroupPtr group, unsigned long long kb);
c313de
-- 
c313de
2.22.0
c313de