Pablo Greco 40546a
From 86212853c9f16bbed77c2310aed1795fdb04f59d Mon Sep 17 00:00:00 2001
Pablo Greco 40546a
Message-Id: <86212853c9f16bbed77c2310aed1795fdb04f59d@dist-git>
Pablo Greco 40546a
From: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Date: Mon, 1 Jul 2019 17:07:44 +0200
Pablo Greco 40546a
Subject: [PATCH] vircgroup: introduce virCgroupV2(Set|Get)MemSwapHardLimit
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 df63fd1f8f17354c5f81c991ff1bb7d12f96d986)
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: <39af05bcaea91a81740ba6d0b766f773f0e8580b.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 | 62 ++++++++++++++++++++++++++++++++++++++++++
Pablo Greco 40546a
 1 file changed, 62 insertions(+)
Pablo Greco 40546a
Pablo Greco 40546a
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
Pablo Greco 40546a
index d8a1d7712f..fed6792e16 100644
Pablo Greco 40546a
--- a/src/util/vircgroupv2.c
Pablo Greco 40546a
+++ b/src/util/vircgroupv2.c
Pablo Greco 40546a
@@ -1230,6 +1230,66 @@ virCgroupV2GetMemorySoftLimit(virCgroupPtr group,
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
+static int
Pablo Greco 40546a
+virCgroupV2SetMemSwapHardLimit(virCgroupPtr group,
Pablo Greco 40546a
+                               unsigned long long kb)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    unsigned long long maxkb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (kb > maxkb) {
Pablo Greco 40546a
+        virReportError(VIR_ERR_INVALID_ARG,
Pablo Greco 40546a
+                       _("Memory '%llu' must be less than %llu"),
Pablo Greco 40546a
+                       kb, maxkb);
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (kb == maxkb) {
Pablo Greco 40546a
+        return virCgroupSetValueStr(group,
Pablo Greco 40546a
+                                    VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
+                                    "memory.swap.max",
Pablo Greco 40546a
+                                    "max");
Pablo Greco 40546a
+    } else {
Pablo Greco 40546a
+        return virCgroupSetValueU64(group,
Pablo Greco 40546a
+                                    VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
+                                    "memory.swap.max",
Pablo Greco 40546a
+                                    kb << 10);
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
+static int
Pablo Greco 40546a
+virCgroupV2GetMemSwapHardLimit(virCgroupPtr group,
Pablo Greco 40546a
+                               unsigned long long *kb)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    VIR_AUTOFREE(char *) value = NULL;
Pablo Greco 40546a
+    unsigned long long max;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (virCgroupGetValueStr(group,
Pablo Greco 40546a
+                             VIR_CGROUP_CONTROLLER_MEMORY,
Pablo Greco 40546a
+                             "memory.swap.max", &value) < 0) {
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (STREQ(value, "max")) {
Pablo Greco 40546a
+        *kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
Pablo Greco 40546a
+        return 0;
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (virStrToLong_ull(value, NULL, 10, &max) < 0) {
Pablo Greco 40546a
+        virReportError(VIR_ERR_INTERNAL_ERROR,
Pablo Greco 40546a
+                       _("Failed to parse value '%s' as number."),
Pablo Greco 40546a
+                       value);
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    *kb = max >> 10;
Pablo Greco 40546a
+    if (*kb >= VIR_DOMAIN_MEMORY_PARAM_UNLIMITED)
Pablo Greco 40546a
+        *kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
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
@@ -1274,6 +1334,8 @@ virCgroupBackend virCgroupV2Backend = {
Pablo Greco 40546a
     .getMemoryHardLimit = virCgroupV2GetMemoryHardLimit,
Pablo Greco 40546a
     .setMemorySoftLimit = virCgroupV2SetMemorySoftLimit,
Pablo Greco 40546a
     .getMemorySoftLimit = virCgroupV2GetMemorySoftLimit,
Pablo Greco 40546a
+    .setMemSwapHardLimit = virCgroupV2SetMemSwapHardLimit,
Pablo Greco 40546a
+    .getMemSwapHardLimit = virCgroupV2GetMemSwapHardLimit,
Pablo Greco 40546a
 };
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
-- 
Pablo Greco 40546a
2.22.0
Pablo Greco 40546a