render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
Blob Blame History Raw
From 518ed664601680039546889ea3ebb9a957f26e16 Mon Sep 17 00:00:00 2001
Message-Id: <518ed664601680039546889ea3ebb9a957f26e16@dist-git>
From: Pavel Hrdina <phrdina@redhat.com>
Date: Mon, 1 Jul 2019 17:07:48 +0200
Subject: [PATCH] vircgroup: introduce virCgroupV2(Set|Get)CpuCfsQuota
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 8e2c887ffa552b59a3dd80fa73ee60791c960565)

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

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

diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
index 876785e4e1..a0a7e55493 100644
--- a/src/util/vircgroupv2.c
+++ b/src/util/vircgroupv2.c
@@ -1391,6 +1391,56 @@ virCgroupV2GetCpuCfsPeriod(virCgroupPtr group,
 }
 
 
+static int
+virCgroupV2SetCpuCfsQuota(virCgroupPtr group,
+                          long long cfs_quota)
+{
+    /* The cfs_quota should be greater or equal than 1ms */
+    if (cfs_quota >= 0 &&
+        (cfs_quota < 1000 ||
+         cfs_quota > ULLONG_MAX / 1000)) {
+        virReportError(VIR_ERR_INVALID_ARG,
+                       _("cfs_quota '%lld' must be in range (1000, %llu)"),
+                       cfs_quota, ULLONG_MAX / 1000);
+        return -1;
+    }
+
+    if (cfs_quota == ULLONG_MAX / 1000) {
+        return virCgroupSetValueStr(group,
+                                    VIR_CGROUP_CONTROLLER_CPU,
+                                    "cpu.max", "max");
+    }
+
+    return virCgroupSetValueI64(group,
+                                VIR_CGROUP_CONTROLLER_CPU,
+                                "cpu.max", cfs_quota);
+}
+
+
+static int
+virCgroupV2GetCpuCfsQuota(virCgroupPtr group,
+                          long long *cfs_quota)
+{
+    VIR_AUTOFREE(char *) str = NULL;
+
+    if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPU,
+                             "cpu.max", &str) < 0) {
+        return -1;
+    }
+
+    if (STREQLEN(str, "max", 3))
+        *cfs_quota = ULLONG_MAX / 1000;
+
+    if (virStrToLong_ll(str, NULL, 10, cfs_quota) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Failed to parse value '%s' from cpu.max."), str);
+        return -1;
+    }
+
+    return 0;
+}
+
+
 virCgroupBackend virCgroupV2Backend = {
     .type = VIR_CGROUP_BACKEND_TYPE_V2,
 
@@ -1443,6 +1493,8 @@ virCgroupBackend virCgroupV2Backend = {
     .getCpuShares = virCgroupV2GetCpuShares,
     .setCpuCfsPeriod = virCgroupV2SetCpuCfsPeriod,
     .getCpuCfsPeriod = virCgroupV2GetCpuCfsPeriod,
+    .setCpuCfsQuota = virCgroupV2SetCpuCfsQuota,
+    .getCpuCfsQuota = virCgroupV2GetCpuCfsQuota,
 };
 
 
-- 
2.22.0