c313de
From 079999bce280ca42a47a1521bb3b7bd06f7985f2 Mon Sep 17 00:00:00 2001
c313de
Message-Id: <079999bce280ca42a47a1521bb3b7bd06f7985f2@dist-git>
c313de
From: Pavel Hrdina <phrdina@redhat.com>
c313de
Date: Mon, 1 Jul 2019 17:07:21 +0200
c313de
Subject: [PATCH] vircgroup: introduce virCgroupV2DetectControllers
c313de
MIME-Version: 1.0
c313de
Content-Type: text/plain; charset=UTF-8
c313de
Content-Transfer-Encoding: 8bit
c313de
c313de
Cgroup v2 has only single mount point for all controllers.  The list
c313de
of controllers is stored in cgroup.controllers file, name of controllers
c313de
are separated by space.
c313de
c313de
In cgroup v2 there is no cpuacct controller, the cpu.stat file always
c313de
exists with usage stats.
c313de
c313de
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c313de
(cherry picked from commit e1bb7fffe2571e6a337bd87a9c5ed487ee6ee046)
c313de
c313de
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
c313de
c313de
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c313de
Message-Id: <2186bc99cdcfc420b1aaf516fc808dfbf582a1d0.1561993100.git.phrdina@redhat.com>
c313de
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c313de
---
c313de
 src/util/vircgroupv2.c | 69 ++++++++++++++++++++++++++++++++++++++++++
c313de
 1 file changed, 69 insertions(+)
c313de
c313de
diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
c313de
index 270a6fb305..c8f2573864 100644
c313de
--- a/src/util/vircgroupv2.c
c313de
+++ b/src/util/vircgroupv2.c
c313de
@@ -33,6 +33,7 @@
c313de
 #include "vircgroup.h"
c313de
 #include "vircgroupbackend.h"
c313de
 #include "vircgroupv2.h"
c313de
+#include "virerror.h"
c313de
 #include "virfile.h"
c313de
 #include "virlog.h"
c313de
 #include "virstring.h"
c313de
@@ -231,6 +232,73 @@ virCgroupV2StealPlacement(virCgroupPtr group)
c313de
 }
c313de
 
c313de
 
c313de
+static int
c313de
+virCgroupV2ParseControllersFile(virCgroupPtr group)
c313de
+{
c313de
+    int rc;
c313de
+    VIR_AUTOFREE(char *) contStr = NULL;
c313de
+    VIR_AUTOFREE(char *) contFile = NULL;
c313de
+    char **contList = NULL;
c313de
+    char **tmp;
c313de
+
c313de
+    if (virAsprintf(&contFile, "%s/cgroup.controllers",
c313de
+                    group->unified.mountPoint) < 0)
c313de
+        return -1;
c313de
+
c313de
+    rc = virFileReadAll(contFile, 1024 * 1024, &contStr);
c313de
+    if (rc < 0) {
c313de
+        virReportSystemError(errno, _("Unable to read from '%s'"), contFile);
c313de
+        return -1;
c313de
+    }
c313de
+
c313de
+    virTrimSpaces(contStr, NULL);
c313de
+
c313de
+    contList = virStringSplit(contStr, " ", 20);
c313de
+    if (!contList)
c313de
+        return -1;
c313de
+
c313de
+    tmp = contList;
c313de
+
c313de
+    while (*tmp) {
c313de
+        int type = virCgroupV2ControllerTypeFromString(*tmp);
c313de
+
c313de
+        if (type >= 0)
c313de
+            group->unified.controllers |= 1 << type;
c313de
+
c313de
+        tmp++;
c313de
+    }
c313de
+
c313de
+    virStringListFree(contList);
c313de
+
c313de
+    return 0;
c313de
+}
c313de
+
c313de
+
c313de
+static int
c313de
+virCgroupV2DetectControllers(virCgroupPtr group,
c313de
+                             int controllers)
c313de
+{
c313de
+    size_t i;
c313de
+
c313de
+    if (virCgroupV2ParseControllersFile(group) < 0)
c313de
+        return -1;
c313de
+
c313de
+    /* In cgroup v2 there is no cpuacct controller, the cpu.stat file always
c313de
+     * exists with usage stats. */
c313de
+    group->unified.controllers |= 1 << VIR_CGROUP_CONTROLLER_CPUACCT;
c313de
+
c313de
+    for (i = 0; i < VIR_CGROUP_CONTROLLER_LAST; i++)
c313de
+        VIR_DEBUG("Controller '%s' present=%s",
c313de
+                  virCgroupV2ControllerTypeToString(i),
c313de
+                  (group->unified.controllers & 1 << i) ? "yes" : "no");
c313de
+
c313de
+    if (controllers >= 0)
c313de
+        return controllers & group->unified.controllers;
c313de
+    else
c313de
+        return group->unified.controllers;
c313de
+}
c313de
+
c313de
+
c313de
 virCgroupBackend virCgroupV2Backend = {
c313de
     .type = VIR_CGROUP_BACKEND_TYPE_V2,
c313de
 
c313de
@@ -242,6 +310,7 @@ virCgroupBackend virCgroupV2Backend = {
c313de
     .detectPlacement = virCgroupV2DetectPlacement,
c313de
     .validatePlacement = virCgroupV2ValidatePlacement,
c313de
     .stealPlacement = virCgroupV2StealPlacement,
c313de
+    .detectControllers = virCgroupV2DetectControllers,
c313de
 };
c313de
 
c313de
 
c313de
-- 
c313de
2.22.0
c313de