render / rpms / libvirt

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