c480ed
From 518ed664601680039546889ea3ebb9a957f26e16 Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <518ed664601680039546889ea3ebb9a957f26e16@dist-git>
c480ed
From: Pavel Hrdina <phrdina@redhat.com>
c480ed
Date: Mon, 1 Jul 2019 17:07:48 +0200
c480ed
Subject: [PATCH] vircgroup: introduce virCgroupV2(Set|Get)CpuCfsQuota
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 8e2c887ffa552b59a3dd80fa73ee60791c960565)
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: <7fe344c65f7394026a2d9feb45920cd177f5f71c.1561993100.git.phrdina@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/util/vircgroupv2.c | 52 ++++++++++++++++++++++++++++++++++++++++++
c480ed
 1 file changed, 52 insertions(+)
c480ed
c480ed
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
c480ed
index 876785e4e1..a0a7e55493 100644
c480ed
--- a/src/util/vircgroupv2.c
c480ed
+++ b/src/util/vircgroupv2.c
c480ed
@@ -1391,6 +1391,56 @@ virCgroupV2GetCpuCfsPeriod(virCgroupPtr group,
c480ed
 }
c480ed
 
c480ed
 
c480ed
+static int
c480ed
+virCgroupV2SetCpuCfsQuota(virCgroupPtr group,
c480ed
+                          long long cfs_quota)
c480ed
+{
c480ed
+    /* The cfs_quota should be greater or equal than 1ms */
c480ed
+    if (cfs_quota >= 0 &&
c480ed
+        (cfs_quota < 1000 ||
c480ed
+         cfs_quota > ULLONG_MAX / 1000)) {
c480ed
+        virReportError(VIR_ERR_INVALID_ARG,
c480ed
+                       _("cfs_quota '%lld' must be in range (1000, %llu)"),
c480ed
+                       cfs_quota, ULLONG_MAX / 1000);
c480ed
+        return -1;
c480ed
+    }
c480ed
+
c480ed
+    if (cfs_quota == ULLONG_MAX / 1000) {
c480ed
+        return virCgroupSetValueStr(group,
c480ed
+                                    VIR_CGROUP_CONTROLLER_CPU,
c480ed
+                                    "cpu.max", "max");
c480ed
+    }
c480ed
+
c480ed
+    return virCgroupSetValueI64(group,
c480ed
+                                VIR_CGROUP_CONTROLLER_CPU,
c480ed
+                                "cpu.max", cfs_quota);
c480ed
+}
c480ed
+
c480ed
+
c480ed
+static int
c480ed
+virCgroupV2GetCpuCfsQuota(virCgroupPtr group,
c480ed
+                          long long *cfs_quota)
c480ed
+{
c480ed
+    VIR_AUTOFREE(char *) str = NULL;
c480ed
+
c480ed
+    if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPU,
c480ed
+                             "cpu.max", &str) < 0) {
c480ed
+        return -1;
c480ed
+    }
c480ed
+
c480ed
+    if (STREQLEN(str, "max", 3))
c480ed
+        *cfs_quota = ULLONG_MAX / 1000;
c480ed
+
c480ed
+    if (virStrToLong_ll(str, NULL, 10, cfs_quota) < 0) {
c480ed
+        virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
+                       _("Failed to parse value '%s' from cpu.max."), str);
c480ed
+        return -1;
c480ed
+    }
c480ed
+
c480ed
+    return 0;
c480ed
+}
c480ed
+
c480ed
+
c480ed
 virCgroupBackend virCgroupV2Backend = {
c480ed
     .type = VIR_CGROUP_BACKEND_TYPE_V2,
c480ed
 
c480ed
@@ -1443,6 +1493,8 @@ virCgroupBackend virCgroupV2Backend = {
c480ed
     .getCpuShares = virCgroupV2GetCpuShares,
c480ed
     .setCpuCfsPeriod = virCgroupV2SetCpuCfsPeriod,
c480ed
     .getCpuCfsPeriod = virCgroupV2GetCpuCfsPeriod,
c480ed
+    .setCpuCfsQuota = virCgroupV2SetCpuCfsQuota,
c480ed
+    .getCpuCfsQuota = virCgroupV2GetCpuCfsQuota,
c480ed
 };
c480ed
 
c480ed
 
c480ed
-- 
c480ed
2.22.0
c480ed