render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
Pablo Greco 40546a
From 5b4d8244ee6361111903a956843b03739f006b4a Mon Sep 17 00:00:00 2001
Pablo Greco 40546a
Message-Id: <5b4d8244ee6361111903a956843b03739f006b4a@dist-git>
Pablo Greco 40546a
From: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Date: Mon, 1 Jul 2019 17:07:55 +0200
Pablo Greco 40546a
Subject: [PATCH] vircgroupmock: add support to test cgroup v2
Pablo Greco 40546a
MIME-Version: 1.0
Pablo Greco 40546a
Content-Type: text/plain; charset=UTF-8
Pablo Greco 40546a
Content-Transfer-Encoding: 8bit
Pablo Greco 40546a
Pablo Greco 40546a
We need to create the cgroup v2 sysfs the same way as we do for
Pablo Greco 40546a
cgroup v1.
Pablo Greco 40546a
Pablo Greco 40546a
This introduces new VIR_CGROUP_MOCK_MODE env variable which will
Pablo Greco 40546a
configure which cgroup mode each test requires.  There are three
Pablo Greco 40546a
different modes:
Pablo Greco 40546a
Pablo Greco 40546a
    - legacy: only cgroup v1 is available and it's the default mode
Pablo Greco 40546a
    - hybrid: both cgroup v1 and cgroup v2 are available and have some
Pablo Greco 40546a
        controllers
Pablo Greco 40546a
    - unified: only cgroup v2 is available
Pablo Greco 40546a
Pablo Greco 40546a
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
(cherry picked from commit 61ff6021d842a3baf2f6cbc3f37dd272f6c86563)
Pablo Greco 40546a
Pablo Greco 40546a
Conflicts:
Pablo Greco 40546a
    src/util/vircgroupv2.c: missing commit c0abcca417
Pablo Greco 40546a
Pablo Greco 40546a
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
Pablo Greco 40546a
Pablo Greco 40546a
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Pablo Greco 40546a
Message-Id: <26937fa61f83f373ff46d4a7d6cc8cb2dfd3c66d.1561993100.git.phrdina@redhat.com>
Pablo Greco 40546a
Reviewed-by: Ján Tomko <jtomko@redhat.com>
Pablo Greco 40546a
---
Pablo Greco 40546a
 tests/vircgroupmock.c | 162 ++++++++++++++++++++++++++++++++++++++----
Pablo Greco 40546a
 1 file changed, 147 insertions(+), 15 deletions(-)
Pablo Greco 40546a
Pablo Greco 40546a
diff --git a/tests/vircgroupmock.c b/tests/vircgroupmock.c
Pablo Greco 40546a
index fcc00a7a7b..2041b1e970 100644
Pablo Greco 40546a
--- a/tests/vircgroupmock.c
Pablo Greco 40546a
+++ b/tests/vircgroupmock.c
Pablo Greco 40546a
@@ -36,6 +36,7 @@
Pablo Greco 40546a
 
Pablo Greco 40546a
 # include <stdarg.h>
Pablo Greco 40546a
 # include "testutilslxc.h"
Pablo Greco 40546a
+# include "viralloc.h"
Pablo Greco 40546a
 # include "virstring.h"
Pablo Greco 40546a
 # include "virfile.h"
Pablo Greco 40546a
 
Pablo Greco 40546a
@@ -107,7 +108,8 @@ static int make_file(const char *path,
Pablo Greco 40546a
     return ret;
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
-static int make_controller(const char *path, mode_t mode)
Pablo Greco 40546a
+
Pablo Greco 40546a
+static int make_controller_v1(const char *path, mode_t mode)
Pablo Greco 40546a
 {
Pablo Greco 40546a
     int ret = -1;
Pablo Greco 40546a
     const char *controller;
Pablo Greco 40546a
@@ -233,11 +235,86 @@ static int make_controller(const char *path, mode_t mode)
Pablo Greco 40546a
         goto cleanup;
Pablo Greco 40546a
     }
Pablo Greco 40546a
 
Pablo Greco 40546a
+# undef MAKE_FILE
Pablo Greco 40546a
+
Pablo Greco 40546a
     ret = 0;
Pablo Greco 40546a
  cleanup:
Pablo Greco 40546a
     return ret;
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
+
Pablo Greco 40546a
+static int make_controller_v2(const char *path, mode_t mode)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    if (!STRPREFIX(path, fakesysfscgroupdir)) {
Pablo Greco 40546a
+        errno = EINVAL;
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (real_mkdir(path, mode) < 0 && errno != EEXIST)
Pablo Greco 40546a
+        return -1;
Pablo Greco 40546a
+
Pablo Greco 40546a
+# define MAKE_FILE(name, value) \
Pablo Greco 40546a
+    do { \
Pablo Greco 40546a
+        if (make_file(path, name, value) < 0) \
Pablo Greco 40546a
+            return -1; \
Pablo Greco 40546a
+    } while (0)
Pablo Greco 40546a
+
Pablo Greco 40546a
+    MAKE_FILE("cgroup.controllers", "cpu io memory\n");
Pablo Greco 40546a
+    MAKE_FILE("cgroup.subtree_control", "");
Pablo Greco 40546a
+    MAKE_FILE("cgroup.type", "domain\n");
Pablo Greco 40546a
+    MAKE_FILE("cpu.max", "max 100000\n");
Pablo Greco 40546a
+    MAKE_FILE("cpu.stat",
Pablo Greco 40546a
+              "usage_usec 0\n"
Pablo Greco 40546a
+              "user_usec 0\n"
Pablo Greco 40546a
+              "system_usec 0\n"
Pablo Greco 40546a
+              "nr_periods 0\n"
Pablo Greco 40546a
+              "nr_throttled 0\n"
Pablo Greco 40546a
+              "throttled_usec 0\n");
Pablo Greco 40546a
+    MAKE_FILE("cpu.weight", "100\n");
Pablo Greco 40546a
+    MAKE_FILE("memory.current", "1455321088\n");
Pablo Greco 40546a
+    MAKE_FILE("memory.high", "max\n");
Pablo Greco 40546a
+    MAKE_FILE("memory.max", "max\n");
Pablo Greco 40546a
+    MAKE_FILE("memory.stat",
Pablo Greco 40546a
+              "anon 0\n"
Pablo Greco 40546a
+              "file 0\n"
Pablo Greco 40546a
+              "kernel_stack 0\n"
Pablo Greco 40546a
+              "slab 0\n"
Pablo Greco 40546a
+              "sock 0\n"
Pablo Greco 40546a
+              "shmem 0\n"
Pablo Greco 40546a
+              "file_mapped 0\n"
Pablo Greco 40546a
+              "file_dirty 0\n"
Pablo Greco 40546a
+              "file_writeback 0\n"
Pablo Greco 40546a
+              "inactive_anon 0\n"
Pablo Greco 40546a
+              "active_anon 0\n"
Pablo Greco 40546a
+              "inactive_file 0\n"
Pablo Greco 40546a
+              "active_file 0\n"
Pablo Greco 40546a
+              "unevictable 0\n"
Pablo Greco 40546a
+              "slab_reclaimable 0\n"
Pablo Greco 40546a
+              "slab_unreclaimable 0\n"
Pablo Greco 40546a
+              "pgfault 0\n"
Pablo Greco 40546a
+              "pgmajfault 0\n"
Pablo Greco 40546a
+              "pgrefill 0\n"
Pablo Greco 40546a
+              "pgscan 0\n"
Pablo Greco 40546a
+              "pgsteal 0\n"
Pablo Greco 40546a
+              "pgactivate 0\n"
Pablo Greco 40546a
+              "pgdeactivate 0\n"
Pablo Greco 40546a
+              "pglazyfree 0\n"
Pablo Greco 40546a
+              "pglazyfreed 0\n"
Pablo Greco 40546a
+              "workingset_refault 0\n"
Pablo Greco 40546a
+              "workingset_activate 0\n"
Pablo Greco 40546a
+              "workingset_nodereclaim 0\n");
Pablo Greco 40546a
+    MAKE_FILE("memory.swap.current", "0\n");
Pablo Greco 40546a
+    MAKE_FILE("memory.swap.max", "max\n");
Pablo Greco 40546a
+    MAKE_FILE("io.stat", "8:0 rbytes=26828800 wbytes=77062144 rios=2256 wios=7849 dbytes=0 dios=0\n");
Pablo Greco 40546a
+    MAKE_FILE("io.max", "");
Pablo Greco 40546a
+    MAKE_FILE("io.weight", "default 100\n");
Pablo Greco 40546a
+
Pablo Greco 40546a
+# undef MAKE_FILE
Pablo Greco 40546a
+
Pablo Greco 40546a
+    return 0;
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
 static void init_syms(void)
Pablo Greco 40546a
 {
Pablo Greco 40546a
     if (real_fopen)
Pablo Greco 40546a
@@ -251,16 +328,64 @@ static void init_syms(void)
Pablo Greco 40546a
     VIR_MOCK_REAL_INIT(open);
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
+
Pablo Greco 40546a
+static int make_controller(const char *path, mode_t mode)
Pablo Greco 40546a
+{
Pablo Greco 40546a
+    const char *mock;
Pablo Greco 40546a
+    bool unified = false;
Pablo Greco 40546a
+    bool hybrid = false;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    mock = getenv("VIR_CGROUP_MOCK_MODE");
Pablo Greco 40546a
+    if (mock) {
Pablo Greco 40546a
+        if (STREQ(mock, "unified")) {
Pablo Greco 40546a
+            unified = true;
Pablo Greco 40546a
+        } else if (STREQ(mock, "hybrid")) {
Pablo Greco 40546a
+            hybrid = true;
Pablo Greco 40546a
+        } else {
Pablo Greco 40546a
+            fprintf(stderr, "invalid mode '%s'\n", mock);
Pablo Greco 40546a
+            abort();
Pablo Greco 40546a
+        }
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    if (unified || (hybrid && strstr(path, "unified"))) {
Pablo Greco 40546a
+        return make_controller_v2(path, mode);
Pablo Greco 40546a
+    } else {
Pablo Greco 40546a
+        return make_controller_v1(path, mode);
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+}
Pablo Greco 40546a
+
Pablo Greco 40546a
+
Pablo Greco 40546a
 static void init_sysfs(void)
Pablo Greco 40546a
 {
Pablo Greco 40546a
-    if (fakerootdir && fakesysfscgroupdir)
Pablo Greco 40546a
-        return;
Pablo Greco 40546a
+    const char *mock;
Pablo Greco 40546a
+    char *newfakerootdir;
Pablo Greco 40546a
+    bool unified = false;
Pablo Greco 40546a
+    bool hybrid = false;
Pablo Greco 40546a
 
Pablo Greco 40546a
-    if (!(fakerootdir = getenv("LIBVIRT_FAKE_ROOT_DIR"))) {
Pablo Greco 40546a
+    if (!(newfakerootdir = getenv("LIBVIRT_FAKE_ROOT_DIR"))) {
Pablo Greco 40546a
         fprintf(stderr, "Missing LIBVIRT_FAKE_ROOT_DIR env variable\n");
Pablo Greco 40546a
         abort();
Pablo Greco 40546a
     }
Pablo Greco 40546a
 
Pablo Greco 40546a
+    if (fakerootdir && STREQ(fakerootdir, newfakerootdir))
Pablo Greco 40546a
+        return;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    fakerootdir = newfakerootdir;
Pablo Greco 40546a
+
Pablo Greco 40546a
+    mock = getenv("VIR_CGROUP_MOCK_MODE");
Pablo Greco 40546a
+    if (mock) {
Pablo Greco 40546a
+        if (STREQ(mock, "unified")) {
Pablo Greco 40546a
+            unified = true;
Pablo Greco 40546a
+        } else if (STREQ(mock, "hybrid")) {
Pablo Greco 40546a
+            hybrid = true;
Pablo Greco 40546a
+        } else {
Pablo Greco 40546a
+            fprintf(stderr, "invalid mode '%s'\n", mock);
Pablo Greco 40546a
+            abort();
Pablo Greco 40546a
+        }
Pablo Greco 40546a
+    }
Pablo Greco 40546a
+
Pablo Greco 40546a
+    VIR_FREE(fakesysfscgroupdir);
Pablo Greco 40546a
+
Pablo Greco 40546a
     if (virAsprintfQuiet(&fakesysfscgroupdir, "%s%s",
Pablo Greco 40546a
                          fakerootdir, SYSFS_CGROUP_PREFIX) < 0)
Pablo Greco 40546a
         abort();
Pablo Greco 40546a
@@ -283,18 +408,25 @@ static void init_sysfs(void)
Pablo Greco 40546a
         free(path); \
Pablo Greco 40546a
     } while (0)
Pablo Greco 40546a
 
Pablo Greco 40546a
-    MAKE_CONTROLLER("cpu");
Pablo Greco 40546a
-    MAKE_CONTROLLER("cpuacct");
Pablo Greco 40546a
-    MAKE_CONTROLLER("cpu,cpuacct");
Pablo Greco 40546a
-    MAKE_CONTROLLER("cpu,cpuacct/system");
Pablo Greco 40546a
-    MAKE_CONTROLLER("cpuset");
Pablo Greco 40546a
-    MAKE_CONTROLLER("blkio");
Pablo Greco 40546a
-    MAKE_CONTROLLER("memory");
Pablo Greco 40546a
-    MAKE_CONTROLLER("freezer");
Pablo Greco 40546a
+    if (unified) {
Pablo Greco 40546a
+        MAKE_CONTROLLER("");
Pablo Greco 40546a
+    } else if (hybrid) {
Pablo Greco 40546a
+        MAKE_CONTROLLER("unified");
Pablo Greco 40546a
+        MAKE_CONTROLLER("cpuset");
Pablo Greco 40546a
+        MAKE_CONTROLLER("freezer");
Pablo Greco 40546a
+    } else {
Pablo Greco 40546a
+        MAKE_CONTROLLER("cpu");
Pablo Greco 40546a
+        MAKE_CONTROLLER("cpuacct");
Pablo Greco 40546a
+        MAKE_CONTROLLER("cpu,cpuacct");
Pablo Greco 40546a
+        MAKE_CONTROLLER("cpuset");
Pablo Greco 40546a
+        MAKE_CONTROLLER("blkio");
Pablo Greco 40546a
+        MAKE_CONTROLLER("memory");
Pablo Greco 40546a
+        MAKE_CONTROLLER("freezer");
Pablo Greco 40546a
 
Pablo Greco 40546a
-    if (make_file(fakesysfscgroupdir,
Pablo Greco 40546a
-                  SYSFS_CPU_PRESENT_MOCKED, "8-23,48-159\n") < 0)
Pablo Greco 40546a
-        abort();
Pablo Greco 40546a
+        if (make_file(fakesysfscgroupdir,
Pablo Greco 40546a
+                      SYSFS_CPU_PRESENT_MOCKED, "8-23,48-159\n") < 0)
Pablo Greco 40546a
+            abort();
Pablo Greco 40546a
+    }
Pablo Greco 40546a
 }
Pablo Greco 40546a
 
Pablo Greco 40546a
 
Pablo Greco 40546a
-- 
Pablo Greco 40546a
2.22.0
Pablo Greco 40546a