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