render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
c480ed
From d91df2c6090f79ef0135f2240181b8dbf87597b0 Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <d91df2c6090f79ef0135f2240181b8dbf87597b0@dist-git>
c480ed
From: Pavel Hrdina <phrdina@redhat.com>
c480ed
Date: Mon, 1 Jul 2019 17:06:01 +0200
c480ed
Subject: [PATCH] vircgroup: Introduce virCgroupGetMemoryStat
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
(cherry picked from commit 901d2b9c8716f2717439d5843cab412ab8d7b247)
c480ed
c480ed
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
c480ed
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
Message-Id: <b70cd9890f218f943b094bc6b040867c6d69bc6b.1561993099.git.phrdina@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/libvirt_private.syms |  1 +
c480ed
 src/util/vircgroup.c     | 88 ++++++++++++++++++++++++++++++++++++++++
c480ed
 src/util/vircgroup.h     |  7 ++++
c480ed
 3 files changed, 96 insertions(+)
c480ed
c480ed
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
c480ed
index 47695eb507..2ec9d8f4bf 100644
c480ed
--- a/src/libvirt_private.syms
c480ed
+++ b/src/libvirt_private.syms
c480ed
@@ -1551,6 +1551,7 @@ virCgroupGetDomainTotalCpuStats;
c480ed
 virCgroupGetFreezerState;
c480ed
 virCgroupGetMemoryHardLimit;
c480ed
 virCgroupGetMemorySoftLimit;
c480ed
+virCgroupGetMemoryStat;
c480ed
 virCgroupGetMemoryUsage;
c480ed
 virCgroupGetMemSwapHardLimit;
c480ed
 virCgroupGetMemSwapUsage;
c480ed
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
c480ed
index a94f958d75..0ed83932ac 100644
c480ed
--- a/src/util/vircgroup.c
c480ed
+++ b/src/util/vircgroup.c
c480ed
@@ -2427,6 +2427,94 @@ virCgroupSetMemory(virCgroupPtr group, unsigned long long kb)
c480ed
 }
c480ed
 
c480ed
 
c480ed
+/**
c480ed
+ * virCgroupGetMemoryStat:
c480ed
+ *
c480ed
+ * @group: The cgroup to change memory for
c480ed
+ * @cache: page cache memory in KiB
c480ed
+ * @activeAnon: anonymous and swap cache memory in KiB
c480ed
+ * @inactiveAnon: anonymous and swap cache memory in KiB
c480ed
+ * @activeFile: file-backed memory in KiB
c480ed
+ * @inactiveFile: file-backed memory in KiB
c480ed
+ * @unevictable: memory that cannot be reclaimed KiB
c480ed
+ *
c480ed
+ * Returns: 0 on success, -1 on error
c480ed
+ */
c480ed
+int
c480ed
+virCgroupGetMemoryStat(virCgroupPtr group,
c480ed
+                       unsigned long long *cache,
c480ed
+                       unsigned long long *activeAnon,
c480ed
+                       unsigned long long *inactiveAnon,
c480ed
+                       unsigned long long *activeFile,
c480ed
+                       unsigned long long *inactiveFile,
c480ed
+                       unsigned long long *unevictable)
c480ed
+{
c480ed
+    int ret = -1;
c480ed
+    char *stat = NULL;
c480ed
+    char *line = NULL;
c480ed
+    unsigned long long cacheVal = 0;
c480ed
+    unsigned long long activeAnonVal = 0;
c480ed
+    unsigned long long inactiveAnonVal = 0;
c480ed
+    unsigned long long activeFileVal = 0;
c480ed
+    unsigned long long inactiveFileVal = 0;
c480ed
+    unsigned long long unevictableVal = 0;
c480ed
+
c480ed
+    if (virCgroupGetValueStr(group,
c480ed
+                             VIR_CGROUP_CONTROLLER_MEMORY,
c480ed
+                             "memory.stat",
c480ed
+                             &stat) < 0) {
c480ed
+        return -1;
c480ed
+    }
c480ed
+
c480ed
+    line = stat;
c480ed
+
c480ed
+    while (line) {
c480ed
+        char *newLine = strchr(line, '\n');
c480ed
+        char *valueStr = strchr(line, ' ');
c480ed
+        unsigned long long value;
c480ed
+
c480ed
+        if (newLine)
c480ed
+            *newLine = '\0';
c480ed
+
c480ed
+        if (!valueStr) {
c480ed
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c480ed
+                           _("Cannot parse 'memory.stat' cgroup file."));
c480ed
+            goto cleanup;
c480ed
+        }
c480ed
+        *valueStr = '\0';
c480ed
+
c480ed
+        if (virStrToLong_ull(valueStr + 1, NULL, 10, &value) < 0)
c480ed
+            goto cleanup;
c480ed
+
c480ed
+        if (STREQ(line, "cache"))
c480ed
+            cacheVal = value >> 10;
c480ed
+        else if (STREQ(line, "active_anon"))
c480ed
+            activeAnonVal = value >> 10;
c480ed
+        else if (STREQ(line, "inactive_anon"))
c480ed
+            inactiveAnonVal = value >> 10;
c480ed
+        else if (STREQ(line, "active_file"))
c480ed
+            activeFileVal = value >> 10;
c480ed
+        else if (STREQ(line, "inactive_file"))
c480ed
+            inactiveFileVal = value >> 10;
c480ed
+        else if (STREQ(line, "unevictable"))
c480ed
+            unevictableVal = value >> 10;
c480ed
+    }
c480ed
+
c480ed
+    *cache = cacheVal;
c480ed
+    *activeAnon = activeAnonVal;
c480ed
+    *inactiveAnon = inactiveAnonVal;
c480ed
+    *activeFile = activeFileVal;
c480ed
+    *inactiveFile = inactiveFileVal;
c480ed
+    *unevictable = unevictableVal;
c480ed
+
c480ed
+    ret = 0;
c480ed
+
c480ed
+ cleanup:
c480ed
+    VIR_FREE(stat);
c480ed
+    return ret;
c480ed
+}
c480ed
+
c480ed
+
c480ed
 /**
c480ed
  * virCgroupGetMemoryUsage:
c480ed
  *
c480ed
diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h
c480ed
index 48be077aba..c7fdaaede4 100644
c480ed
--- a/src/util/vircgroup.h
c480ed
+++ b/src/util/vircgroup.h
c480ed
@@ -177,6 +177,13 @@ int virCgroupGetBlkioDeviceWriteBps(virCgroupPtr group,
c480ed
                                     unsigned long long *wbps);
c480ed
 
c480ed
 int virCgroupSetMemory(virCgroupPtr group, unsigned long long kb);
c480ed
+int virCgroupGetMemoryStat(virCgroupPtr group,
c480ed
+                           unsigned long long *cache,
c480ed
+                           unsigned long long *activeAnon,
c480ed
+                           unsigned long long *inactiveAnon,
c480ed
+                           unsigned long long *activeFile,
c480ed
+                           unsigned long long *inactiveFile,
c480ed
+                           unsigned long long *unevictable);
c480ed
 int virCgroupGetMemoryUsage(virCgroupPtr group, unsigned long *kb);
c480ed
 
c480ed
 int virCgroupSetMemoryHardLimit(virCgroupPtr group, unsigned long long kb);
c480ed
-- 
c480ed
2.22.0
c480ed