render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Blob Blame History Raw
From 86212853c9f16bbed77c2310aed1795fdb04f59d Mon Sep 17 00:00:00 2001
Message-Id: <86212853c9f16bbed77c2310aed1795fdb04f59d@dist-git>
From: Pavel Hrdina <phrdina@redhat.com>
Date: Mon, 1 Jul 2019 17:07:44 +0200
Subject: [PATCH] vircgroup: introduce virCgroupV2(Set|Get)MemSwapHardLimit
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
(cherry picked from commit df63fd1f8f17354c5f81c991ff1bb7d12f96d986)

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Message-Id: <39af05bcaea91a81740ba6d0b766f773f0e8580b.1561993100.git.phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/util/vircgroupv2.c | 62 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)

diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
index d8a1d7712f..fed6792e16 100644
--- a/src/util/vircgroupv2.c
+++ b/src/util/vircgroupv2.c
@@ -1230,6 +1230,66 @@ virCgroupV2GetMemorySoftLimit(virCgroupPtr group,
 }
 
 
+static int
+virCgroupV2SetMemSwapHardLimit(virCgroupPtr group,
+                               unsigned long long kb)
+{
+    unsigned long long maxkb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
+
+    if (kb > maxkb) {
+        virReportError(VIR_ERR_INVALID_ARG,
+                       _("Memory '%llu' must be less than %llu"),
+                       kb, maxkb);
+        return -1;
+    }
+
+    if (kb == maxkb) {
+        return virCgroupSetValueStr(group,
+                                    VIR_CGROUP_CONTROLLER_MEMORY,
+                                    "memory.swap.max",
+                                    "max");
+    } else {
+        return virCgroupSetValueU64(group,
+                                    VIR_CGROUP_CONTROLLER_MEMORY,
+                                    "memory.swap.max",
+                                    kb << 10);
+    }
+}
+
+
+static int
+virCgroupV2GetMemSwapHardLimit(virCgroupPtr group,
+                               unsigned long long *kb)
+{
+    VIR_AUTOFREE(char *) value = NULL;
+    unsigned long long max;
+
+    if (virCgroupGetValueStr(group,
+                             VIR_CGROUP_CONTROLLER_MEMORY,
+                             "memory.swap.max", &value) < 0) {
+        return -1;
+    }
+
+    if (STREQ(value, "max")) {
+        *kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
+        return 0;
+    }
+
+    if (virStrToLong_ull(value, NULL, 10, &max) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Failed to parse value '%s' as number."),
+                       value);
+        return -1;
+    }
+
+    *kb = max >> 10;
+    if (*kb >= VIR_DOMAIN_MEMORY_PARAM_UNLIMITED)
+        *kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
+
+    return 0;
+}
+
+
 virCgroupBackend virCgroupV2Backend = {
     .type = VIR_CGROUP_BACKEND_TYPE_V2,
 
@@ -1274,6 +1334,8 @@ virCgroupBackend virCgroupV2Backend = {
     .getMemoryHardLimit = virCgroupV2GetMemoryHardLimit,
     .setMemorySoftLimit = virCgroupV2SetMemorySoftLimit,
     .getMemorySoftLimit = virCgroupV2GetMemorySoftLimit,
+    .setMemSwapHardLimit = virCgroupV2SetMemSwapHardLimit,
+    .getMemSwapHardLimit = virCgroupV2GetMemSwapHardLimit,
 };
 
 
-- 
2.22.0