render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
c480ed
From 6a1f3fdcf13ff1fdcbd1f63443a1e5d05b2ddd1c Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <6a1f3fdcf13ff1fdcbd1f63443a1e5d05b2ddd1c@dist-git>
c480ed
From: Pavel Hrdina <phrdina@redhat.com>
c480ed
Date: Mon, 1 Jul 2019 17:07:43 +0200
c480ed
Subject: [PATCH] vircgroup: introduce virCgroupV2(Set|Get)MemorySoftLimit
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 94db4bf86ea10e9853f62c4ea079a432482a58df)
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: <a59c442f9098898c0196a5fb2a0d3527ce277153.1561993100.git.phrdina@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/util/vircgroupv2.c | 61 ++++++++++++++++++++++++++++++++++++++++++
c480ed
 1 file changed, 61 insertions(+)
c480ed
c480ed
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
c480ed
index 81a577d28f..d8a1d7712f 100644
c480ed
--- a/src/util/vircgroupv2.c
c480ed
+++ b/src/util/vircgroupv2.c
c480ed
@@ -1171,6 +1171,65 @@ virCgroupV2GetMemoryHardLimit(virCgroupPtr group,
c480ed
 }
c480ed
 
c480ed
 
c480ed
+static int
c480ed
+virCgroupV2SetMemorySoftLimit(virCgroupPtr group,
c480ed
+                              unsigned long long kb)
c480ed
+{
c480ed
+    unsigned long long maxkb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
c480ed
+
c480ed
+    if (kb > maxkb) {
c480ed
+        virReportError(VIR_ERR_INVALID_ARG,
c480ed
+                       _("Memory '%llu' must be less than %llu"),
c480ed
+                       kb, maxkb);
c480ed
+        return -1;
c480ed
+    }
c480ed
+
c480ed
+    if (kb == maxkb) {
c480ed
+        return virCgroupSetValueStr(group,
c480ed
+                                    VIR_CGROUP_CONTROLLER_MEMORY,
c480ed
+                                    "memory.high",
c480ed
+                                    "max");
c480ed
+    } else {
c480ed
+        return virCgroupSetValueU64(group,
c480ed
+                                    VIR_CGROUP_CONTROLLER_MEMORY,
c480ed
+                                    "memory.high",
c480ed
+                                    kb << 10);
c480ed
+    }
c480ed
+}
c480ed
+
c480ed
+
c480ed
+static int
c480ed
+virCgroupV2GetMemorySoftLimit(virCgroupPtr group,
c480ed
+                              unsigned long long *kb)
c480ed
+{
c480ed
+    VIR_AUTOFREE(char *) value = NULL;
c480ed
+    unsigned long long high;
c480ed
+
c480ed
+    if (virCgroupGetValueStr(group,
c480ed
+                             VIR_CGROUP_CONTROLLER_MEMORY,
c480ed
+                             "memory.high", &value) < 0)
c480ed
+        return -1;
c480ed
+
c480ed
+    if (STREQ(value, "max")) {
c480ed
+        *kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
c480ed
+        return 0;
c480ed
+    }
c480ed
+
c480ed
+    if (virStrToLong_ull(value, NULL, 10, &high) < 0) {
c480ed
+        virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
+                       _("Failed to parse value '%s' as number."),
c480ed
+                       value);
c480ed
+        return -1;
c480ed
+    }
c480ed
+
c480ed
+    *kb = high >> 10;
c480ed
+    if (*kb >= VIR_DOMAIN_MEMORY_PARAM_UNLIMITED)
c480ed
+        *kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
c480ed
+
c480ed
+    return 0;
c480ed
+}
c480ed
+
c480ed
+
c480ed
 virCgroupBackend virCgroupV2Backend = {
c480ed
     .type = VIR_CGROUP_BACKEND_TYPE_V2,
c480ed
 
c480ed
@@ -1213,6 +1272,8 @@ virCgroupBackend virCgroupV2Backend = {
c480ed
     .getMemoryUsage = virCgroupV2GetMemoryUsage,
c480ed
     .setMemoryHardLimit = virCgroupV2SetMemoryHardLimit,
c480ed
     .getMemoryHardLimit = virCgroupV2GetMemoryHardLimit,
c480ed
+    .setMemorySoftLimit = virCgroupV2SetMemorySoftLimit,
c480ed
+    .getMemorySoftLimit = virCgroupV2GetMemorySoftLimit,
c480ed
 };
c480ed
 
c480ed
 
c480ed
-- 
c480ed
2.22.0
c480ed