Skip to contentSkip to conversation

lib: add new cgroup test API

RFC PATCH New Li Wang 2 participants 7 messages May 22, 2020 – May 27, 2020 Library code

lore
Share & tools
lore

Li Wang

author
#0 lore raw

RFC PATCH 1/4 lib: add new cgroup test API

Many of our LTP tests need Control Group in the configuration, this patch makes cgroup unified mounting at setup phase to be possible. The method?is extracted from mem.h with the purpose of?extendible for further developing, and trying?to compatible the current two versions of cgroup,

It's hard to make all APIs be strictly consistent because there are many differences between v1 and v2. But it?capsulate the detail of cgroup mounting in high-level functions, which will be easier to use cgroup without considering too much technical thing.? ?

Btw, test get passed on RHEL7(x86_64), RHEL8(ppc64le), Fedora32(x86_64).

---
 include/tst_cgroup.h | 108 ++++++++++++++++++++
 include/tst_test.h   |   1 +
 lib/tst_cgroup.c     | 232 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 341 insertions(+)
 create mode 100644 include/tst_cgroup.h
 create mode 100644 lib/tst_cgroup.c
include/tst_cgroup.h new file +108 -0
new file mode 100644
index 000000000..78e646c2e
@@ -0,0 +1,108 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Red Hat, Inc.
+ */
+
+#ifndef TST_CGROUP_H
+#define TST_CGROUP_H
+
+#define PATH_SYS_CGROUP		"/sys/fs/cgroup"
+
+enum tst_cgroup_ver {
+	TST_CGROUP_V1 = 1,
+	TST_CGROUP_V2 = 2,
+};
+
+static enum tst_cgroup_ver tst_cg_ver;
+
+enum tst_cgroup_ver tst_cgroup_version(void);
+
+/* cgroup v1 */
+#define PATH_TMP_CG1_MEM	"/tmp/cgroup1_mem"
+#define PATH_TMP_CG1_CST	"/tmp/cgroup1_cpuset"
+#define PATH_CG1_MEM_LTP	PATH_TMP_CG1_MEM "/ltp"
+#define PATH_CG1_CST_LTP	PATH_TMP_CG1_CST "/ltp"
+#define PATH_MEMORY_LIMIT	PATH_CG1_MEM_LTP "/memory.limit_in_bytes"
+#define PATH_MEMORY_SW_LIMIT	PATH_CG1_MEM_LTP "/memory.memsw.limit_in_bytes"
+
+void tst_mount_cgroup1(const char *name, const char *option,
+			const char *mnt_path, const char *new_path);
+void tst_umount_cgroup1(const char *mnt_path, const char *new_path);
+
+void tst_mount_memcg1(void);
+void tst_umount_memcg1(void);
+void tst_write_memcg1(long memsz);
+
+void tst_mount_cpusetcg1(void);
+void tst_umount_cpusetcg1(void);
+void tst_write_cpusetcg1(void);
+/* cgroup v1 */
+
+/* cgroup v2 */
+#define PATH_TMP_CGROUP2	"/tmp/cgroup2"	/* cgroup v2 has only single hierarchy */
+#define PATH_TMP_CG2_LTP	PATH_TMP_CGROUP2 "/ltp"
+#define PATH_MEMORY_MAX		PATH_TMP_CG2_LTP "/memory.max"
+#define PATH_MEMORY_SW_MAX	PATH_TMP_CG2_LTP "/memory.swap.max"
+
+void tst_mount_cgroup2(const char *mnt_path, const char *new_path);
+void tst_umount_cgroup2(const char *mnt_path, const char *new_path);
+
+void tst_mount_memcg2(void);
+void tst_umount_memcg2(void);
+void tst_write_memcg2(long memsz);
+
+void tst_mount_cpusetcg2(void);
+/* cgroup v2 */
+
+static inline void tst_mount_memcg(void)
+{
+	tst_cg_ver = tst_cgroup_version();
+
+	if (tst_cg_ver & TST_CGROUP_V1)
+		tst_mount_memcg1();
+
+	if (tst_cg_ver & TST_CGROUP_V2)
+		tst_mount_memcg2();
+}
+
+static inline void tst_umount_memcg(void)
+{
+	if (tst_cg_ver & TST_CGROUP_V1)
+		tst_umount_memcg1();
+
+	if (tst_cg_ver & TST_CGROUP_V2)
+		tst_umount_memcg2();
+}
+
+static inline void tst_write_memcg(long memsz)
+{
+	if (tst_cg_ver & TST_CGROUP_V1)
+		tst_write_memcg1(memsz);
+
+	if (tst_cg_ver & TST_CGROUP_V2)
+		tst_write_memcg2(memsz);
+}
+
+static inline void tst_mount_cpusetcg(void)
+{
+	tst_cg_ver = tst_cgroup_version();
+
+	if (tst_cg_ver & TST_CGROUP_V1)
+		tst_mount_cpusetcg1();
+
+	if (tst_cg_ver & TST_CGROUP_V2)
+		tst_mount_cpusetcg2();
+}
+
+static inline void tst_umount_cpusetcg(void)
+{
+	if (tst_cg_ver & TST_CGROUP_V1)
+		tst_umount_cpusetcg1();
+}
+
+static inline void tst_write_cpusetcg(void)
+{
+	if (tst_cg_ver & TST_CGROUP_V1)
+		tst_write_cpusetcg1();
+}
+#endif /* TST_CGROUP_H */
include/tst_test.h +1 -0
index 5bedb4f6f..b84f7b9dd 100644
@@ -39,6 +39,7 @@
 #include "tst_capability.h"
 #include "tst_hugepage.h"
 #include "tst_assert.h"
+#include "tst_cgroup.h"
 
 /*
  * Reports testcase result.
lib/tst_cgroup.c new file +232 -0

232 lines not on this page · raw message

Signature · 1 line
2.21.1
author
#1 lore raw

RFC PATCH 2/4 mem: take use of new cgroup API

For issue #611:
https://github.com/linux-test-project/ltp/issues/611

---
 testcases/kernel/mem/cpuset/cpuset01.c | 19 +++++++++--------
 testcases/kernel/mem/include/mem.h     |  1 +
 testcases/kernel/mem/ksm/ksm02.c       |  6 ++----
 testcases/kernel/mem/ksm/ksm03.c       | 11 +++-------
 testcases/kernel/mem/ksm/ksm04.c       | 14 +++++--------
 testcases/kernel/mem/lib/mem.c         |  8 +++----
 testcases/kernel/mem/oom/oom03.c       | 20 +++++++++---------
 testcases/kernel/mem/oom/oom04.c       |  8 +++----
 testcases/kernel/mem/oom/oom05.c       | 29 ++++++++++++++------------
 9 files changed, 54 insertions(+), 62 deletions(-)
testcases/kernel/mem/cpuset/cpuset01.c +10 -9
index 853f7fe55..0db919121 100644
@@ -51,11 +51,11 @@ static void test_cpuset(void)
 	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
 	char mems[BUFSIZ], buf[BUFSIZ];
 
-	read_cpuset_files(CPATH, "cpus", buf);
-	write_cpuset_files(CPATH_NEW, "cpus", buf);
-	read_cpuset_files(CPATH, "mems", mems);
-	write_cpuset_files(CPATH_NEW, "mems", mems);
-	SAFE_FILE_PRINTF(CPATH_NEW "/tasks", "%d", getpid());
+	read_cpuset_files(PATH_TMP_CG1_CST, "cpus", buf);
+	write_cpuset_files(PATH_CG1_CST_LTP, "cpus", buf);
+	read_cpuset_files(PATH_TMP_CG1_CST, "mems", mems);
+	write_cpuset_files(PATH_CG1_CST_LTP, "mems", mems);
+	SAFE_FILE_PRINTF(PATH_CG1_CST_LTP "/tasks", "%d", getpid());
 
 	child = SAFE_FORK();
 	if (child == 0) {
@@ -70,9 +70,9 @@ static void test_cpuset(void)
 	}
 
 	snprintf(buf, BUFSIZ, "%d", nodes[0]);
-	write_cpuset_files(CPATH_NEW, "mems", buf);
+	write_cpuset_files(PATH_CG1_CST_LTP, "mems", buf);
 	snprintf(buf, BUFSIZ, "%d", nodes[1]);
-	write_cpuset_files(CPATH_NEW, "mems", buf);
+	write_cpuset_files(PATH_CG1_CST_LTP, "mems", buf);
 
 	SAFE_WAITPID(child, &status, WUNTRACED | WCONTINUED);
 	if (WEXITSTATUS(status) != 0) {
@@ -85,7 +85,7 @@ static void test_cpuset(void)
 
 static void setup(void)
 {
-	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
+	tst_mount_cpusetcg();
 	ncpus = count_cpu();
 	if (get_allowed_nodes_arr(NH_MEMS | NH_CPUS, &nnodes, &nodes) < 0)
 		tst_brk(TBROK | TERRNO, "get_allowed_nodes_arr");
@@ -95,7 +95,7 @@ static void setup(void)
 
 static void cleanup(void)
 {
-	umount_mem(CPATH, CPATH_NEW);
+	tst_umount_cpusetcg();
 }
 
 static void sighandler(int signo LTP_ATTRIBUTE_UNUSED)
@@ -183,6 +183,7 @@ static long count_cpu(void)
 
 static struct tst_test test = {
 	.needs_root = 1,
+	.forks_child = 1,
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_all = test_cpuset,
testcases/kernel/mem/include/mem.h +1 -0
index cce9c0497..f553651c2 100644
@@ -2,6 +2,7 @@
 #define _MEM_H
 #include "config.h"
 #include "tst_test.h"
+#include "tst_cgroup.h"
 #include "ksm_helper.h"
 
 #if defined(__powerpc__) || defined(__powerpc64__)
testcases/kernel/mem/ksm/ksm02.c +2 -4
index 3d4c19deb..dffafc412 100644
@@ -88,8 +88,7 @@ static void cleanup(void)
 		FILE_PRINTF(PATH_KSM "merge_across_nodes",
 				 "%d", merge_across_nodes);
 
-	if (cpuset_mounted)
-		umount_mem(CPATH, CPATH_NEW);
+	tst_umount_cpusetcg();
 }
 
 static void setup(void)
@@ -105,8 +104,7 @@ static void setup(void)
 		SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "1");
 	}
 
-	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
-	cpuset_mounted = 1;
+	tst_mount_cpusetcg();
 }
 
 static struct tst_test test = {
testcases/kernel/mem/ksm/ksm03.c +3 -8
index 0e5131654..8dd794a75 100644
@@ -59,11 +59,9 @@
 #include "mem.h"
 #include "ksm_common.h"
 
-static int memcg_mounted;
-
 static void verify_ksm(void)
 {
-	write_memcg();
+	tst_write_memcg(TESTMEM);
 	create_same_memory(size, num, unit);
 }
 
@@ -79,8 +77,7 @@ static void setup(void)
 	}
 
 	parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
-	mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
-	memcg_mounted = 1;
+	tst_mount_memcg();
 }
 
 static void cleanup(void)
@@ -88,9 +85,7 @@ static void cleanup(void)
 	if (access(PATH_KSM "merge_across_nodes", F_OK) == 0)
 		FILE_PRINTF(PATH_KSM "merge_across_nodes",
 				 "%d", merge_across_nodes);
-
-	if (memcg_mounted)
-		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
+	tst_umount_memcg();
 }
 
 static struct tst_test test = {
testcases/kernel/mem/ksm/ksm04.c +5 -9
index e393dbd40..0a6fbd8a4 100644
@@ -70,7 +70,7 @@ static void verify_ksm(void)
 	node = get_a_numa_node();
 	set_node(nmask, node);
 
-	write_memcg();
+	tst_write_memcg(TESTMEM);
 
 	if (set_mempolicy(MPOL_BIND, nmask, MAXNODES) == -1) {
 		if (errno != ENOSYS)
@@ -91,10 +91,8 @@ static void cleanup(void)
 		FILE_PRINTF(PATH_KSM "merge_across_nodes",
 				 "%d", merge_across_nodes);
 
-	if (cpuset_mounted)
-		umount_mem(CPATH, CPATH_NEW);
-	if (memcg_mounted)
-		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
+	tst_umount_cpusetcg();
+	tst_umount_memcg();
 }
 
 static void setup(void)
@@ -110,10 +108,8 @@ static void setup(void)
 
 	parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
 
-	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
-	cpuset_mounted = 1;
-	mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
-	memcg_mounted = 1;
+	tst_mount_memcg();
+	tst_mount_cpusetcg();
 }
 
 static struct tst_test test = {
testcases/kernel/mem/lib/mem.c +4 -4
index eca4c61c8..df1baa42a 100644
@@ -692,7 +692,7 @@ void write_cpusets(long nd)
 	char cpus[BUFSIZ] = "";
 
 	snprintf(buf, BUFSIZ, "%ld", nd);
-	write_cpuset_files(CPATH_NEW, "mems", buf);
+	write_cpuset_files(PATH_CG1_CST_LTP, "mems", buf);
 
 	gather_node_cpus(cpus, nd);
 	/*
@@ -701,14 +701,14 @@ void write_cpusets(long nd)
 	 * the value of cpuset.cpus.
 	 */
 	if (strlen(cpus) != 0) {
-		write_cpuset_files(CPATH_NEW, "cpus", cpus);
+		write_cpuset_files(PATH_CG1_CST_LTP, "cpus", cpus);
 	} else {
 		tst_res(TINFO, "No CPUs in the node%ld; "
 				"using only CPU0", nd);
-		write_cpuset_files(CPATH_NEW, "cpus", "0");
+		write_cpuset_files(PATH_CG1_CST_LTP, "cpus", "0");
 	}
 
-	SAFE_FILE_PRINTF(CPATH_NEW "/tasks", "%d", getpid());
+	SAFE_FILE_PRINTF(PATH_CG1_CST_LTP "/tasks", "%d", getpid());
 }
 
 void umount_mem(char *path, char *path_new)
testcases/kernel/mem/oom/oom03.c +10 -10
index ce0b34c31..b437952f3 100644
@@ -36,27 +36,29 @@
 
 #ifdef HAVE_NUMA_V2
 
-static int memcg_mounted;
-
 static void verify_oom(void)
 {
 #ifdef TST_ABI32
 	tst_brk(TCONF, "test is not designed for 32-bit system.");
 #endif
 
-	SAFE_FILE_PRINTF(MEMCG_PATH_NEW "/tasks", "%d", getpid());
-	SAFE_FILE_PRINTF(MEMCG_LIMIT, "%ld", TESTMEM);
+	tst_write_memcg(TESTMEM);
 
 	testoom(0, 0, ENOMEM, 1);
 
-	if (access(MEMCG_SW_LIMIT, F_OK) == -1) {
+	if ((access(PATH_MEMORY_SW_LIMIT, F_OK) == -1) ||
+			(access(PATH_MEMORY_SW_MAX, F_OK) == -1)) {
 		if (errno == ENOENT)
 			tst_res(TCONF,
 				"memcg swap accounting is disabled");
 		else
 			tst_brk(TBROK | TERRNO, "access");
 	} else {
-		SAFE_FILE_PRINTF(MEMCG_SW_LIMIT, "%ld", TESTMEM);
+		if (tst_cg_ver & TST_CGROUP_V1)
+			SAFE_FILE_PRINTF(PATH_MEMORY_SW_LIMIT, "%ld", TESTMEM);
+		if (tst_cg_ver & TST_CGROUP_V2)
+			SAFE_FILE_PRINTF(PATH_MEMORY_SW_MAX, "%ld", TESTMEM);
+
 		testoom(0, 1, ENOMEM, 1);
 	}
 
@@ -73,16 +75,14 @@ static void setup(void)
 {
 	overcommit = get_sys_tune("overcommit_memory");
 	set_sys_tune("overcommit_memory", 1, 1);
-	mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
-	memcg_mounted = 1;
+	tst_mount_memcg();
 }
 
 static void cleanup(void)
 {
 	if (overcommit != -1)
 		set_sys_tune("overcommit_memory", overcommit, 0);
-	if (memcg_mounted)
-		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
+	tst_umount_memcg();
 }
 
 static struct tst_test test = {
testcases/kernel/mem/oom/oom04.c +3 -5
index 7185ef973..0ce4c81ae 100644
@@ -53,7 +53,7 @@ static void verify_oom(void)
 		 * is in charge of cpuset.memory_migrate, we can write
 		 * 1 to cpuset.memory_migrate to enable the migration.
 		 */
-		write_cpuset_files(CPATH_NEW, "memory_migrate", "1");
+		write_cpuset_files(PATH_CG1_CST_LTP, "memory_migrate", "1");
 		tst_res(TINFO, "OOM on CPUSET with mem migrate:");
 		testoom(0, 0, ENOMEM, 1);
 	}
@@ -69,8 +69,7 @@ static void setup(void)
 	overcommit = get_sys_tune("overcommit_memory");
 	set_sys_tune("overcommit_memory", 1, 1);
 
-	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
-	cpuset_mounted = 1;
+	tst_mount_cpusetcg();
 
 	/*
 	 * Some nodes do not contain memory, so use
@@ -89,8 +88,7 @@ static void cleanup(void)
 {
 	if (overcommit != -1)
 		set_sys_tune("overcommit_memory", overcommit, 0);
-	if (cpuset_mounted)
-		umount_mem(CPATH, CPATH_NEW);
+	tst_umount_cpusetcg();
 }
 
 static struct tst_test test = {
testcases/kernel/mem/oom/oom05.c +16 -13
index db24df6de..1ab272655 100644
@@ -56,13 +56,14 @@ static void verify_oom(void)
 	 * 1 to cpuset.memory_migrate to enable the migration.
 	 */
 	if (is_numa(NULL, NH_MEMS, 2)) {
-		write_cpuset_files(CPATH_NEW, "memory_migrate", "1");
+		write_cpuset_files(PATH_CG1_CST_LTP, "memory_migrate", "1");
 		tst_res(TINFO, "OOM on CPUSET & MEMCG with "
 				"cpuset.memory_migrate=1");
 		testoom(0, 0, ENOMEM, 1);
 	}
 
-	if (access(MEMCG_SW_LIMIT, F_OK) == -1) {
+	if ((access(PATH_MEMORY_SW_LIMIT, F_OK) == -1) ||
+			(access(PATH_MEMORY_SW_MAX, F_OK) == -1)) {
 		if (errno == ENOENT) {
 			tst_res(TCONF, "memcg swap accounting is disabled");
 			swap_acc_on = 0;
@@ -73,12 +74,18 @@ static void verify_oom(void)
 	if (swap_acc_on) {
 		tst_res(TINFO, "OOM on CPUSET & MEMCG with "
 				"special memswap limitation:");
-		SAFE_FILE_PRINTF(MEMCG_SW_LIMIT, "%ld", TESTMEM);
+		if (tst_cg_ver & TST_CGROUP_V1)
+			SAFE_FILE_PRINTF(PATH_MEMORY_SW_LIMIT, "%ld", TESTMEM);
+		if (tst_cg_ver & TST_CGROUP_V2)
+			SAFE_FILE_PRINTF(PATH_MEMORY_SW_MAX, "%ld", TESTMEM);
 		testoom(0, 0, ENOMEM, 1);
 
 		tst_res(TINFO, "OOM on CPUSET & MEMCG with "
 				"disabled memswap limitation:");
-		SAFE_FILE_PRINTF(MEMCG_SW_LIMIT, "-1");
+		if (tst_cg_ver & TST_CGROUP_V1)
+			SAFE_FILE_PRINTF(PATH_MEMORY_SW_LIMIT, "%ld", -1);
+		if (tst_cg_ver & TST_CGROUP_V2)
+			SAFE_FILE_PRINTF(PATH_MEMORY_SW_MAX, "%ld", -1);
 		testoom(0, 0, ENOMEM, 1);
 	}
 }
@@ -93,11 +100,9 @@ void setup(void)
 	overcommit = get_sys_tune("overcommit_memory");
 	set_sys_tune("overcommit_memory", 1, 1);
 
-	mount_mem("memcg", "cgroup", "memory", MEMCG_PATH, MEMCG_PATH_NEW);
-	memcg_mounted = 1;
-	mount_mem("cpuset", "cpuset", NULL, CPATH, CPATH_NEW);
-	cpuset_mounted = 1;
-	write_memcg();
+	tst_mount_memcg();
+	tst_mount_cpusetcg();
+	tst_write_memcg(TESTMEM);
 
 	/*
 	 * Some nodes do not contain memory, so use
@@ -116,10 +121,8 @@ void cleanup(void)
 {
 	if (overcommit != -1)
 		set_sys_tune("overcommit_memory", overcommit, 0);
-	if (cpuset_mounted)
-		umount_mem(CPATH, CPATH_NEW);
-	if (memcg_mounted)
-		umount_mem(MEMCG_PATH, MEMCG_PATH_NEW);
+	tst_umount_cpusetcg();
+	tst_umount_memcg();
 }
 
 static struct tst_test test = {
Signature · 1 line
2.21.1
author
#2 lore raw

RFC PATCH 3/4 mem: remove the old cgroup API

---
 testcases/kernel/mem/include/mem.h | 16 +--------
 testcases/kernel/mem/lib/mem.c     | 58 ------------------------------
 2 files changed, 1 insertion(+), 73 deletions(-)
testcases/kernel/mem/include/mem.h +1 -15
index f553651c2..0e089fae2 100644
@@ -60,24 +60,10 @@ void test_ksm_merge_across_nodes(unsigned long nr_pages);
 void check_hugepage(void);
 void write_memcg(void);
 
-/* cpuset/memcg */
-
-#define CPATH			"/dev/cpuset"
-#define CPATH_NEW		CPATH "/1"
-#define MEMCG_PATH		"/dev/cgroup"
-#define MEMCG_PATH_NEW		MEMCG_PATH "/1"
-#define MEMCG_LIMIT		MEMCG_PATH_NEW "/memory.limit_in_bytes"
-#define MEMCG_SW_LIMIT		MEMCG_PATH_NEW "/memory.memsw.limit_in_bytes"
-#if HAVE_SYS_EVENTFD_H
-#define PATH_OOMCTRL		MEMCG_PATH_NEW "/memory.oom_control"
-#define PATH_EVTCTRL		MEMCG_PATH_NEW "/cgroup.event_control"
-#endif
-
+/* cpuset/memcg - include/tst_cgroup.h */
 void read_cpuset_files(char *prefix, char *filename, char *retbuf);
 void write_cpuset_files(char *prefix, char *filename, char *buf);
 void write_cpusets(long nd);
-void mount_mem(char *name, char *fs, char *options, char *path, char *path_new);
-void umount_mem(char *path, char *path_new);
 
 /* shared */
 unsigned int get_a_numa_node(void);
testcases/kernel/mem/lib/mem.c +0 -58

70 lines not on this page · raw message

Signature · 1 line
2.21.1
author
#3 lore raw

RFC PATCH 4/4 mm: add cpuset01 to runtest file

This test was moved to stress test since commit ae8fa55a8, but stress runtest has been removed from commit e752f7c19674 :).

Here adding back it as a general memory test without '-I 3600'.

---
 runtest/mm | 2 ++
 1 file changed, 2 insertions(+)
runtest/mm +2 -0
index a09f39c1e..612a4d066 100644
@@ -73,6 +73,8 @@ ksm06 ksm06
 ksm06_1 ksm06 -n 10
 ksm06_2 ksm06 -n 10000
 
+cpuset01 cpuset01
+
 oom01 oom01
 oom02 oom02
 oom03 oom03
Signature · 1 line
2.21.1
#4 lore raw

RFC PATCH 1/4 lib: add new cgroup test API

In reply to Li Wang

----- Original Message -----

include/tst_cgroup.h line 22

Quoted 4 lines
include/tst_cgroup.h new file +4 -0
+
+/* cgroup v1 */
+#define PATH_TMP_CG1_MEM	"/tmp/cgroup1_mem"
+#define PATH_TMP_CG1_CST	"/tmp/cgroup1_cpuset"

It's only used for mount, so not sure if making it relative to TMPDIR buys us anything.

lib/tst_cgroup.c line 52

Quoted 4 lines
lib/tst_cgroup.c new file +4 -0
+
+/* cgroup v1 */
+void tst_mount_cgroup1(const char *name, const char *option,
+			const char *mnt_path, const char *new_path)

I'd make all cgroup API start with tst_cgroup*.

Is the intent to provide API for both v1 and v2, or just higher level API that hides the details?

lib/tst_cgroup.c line 64

Quoted 12 lines
lib/tst_cgroup.c new file +12 -0
+{
+	if (tst_is_mounted(mnt_path))
+		goto out;
+
+	SAFE_MKDIR(mnt_path, 0777);
+	if (mount(name, mnt_path, "cgroup", 0, option) == -1) {
+		if (errno == ENODEV) {
+			if (rmdir(mnt_path) == -1)
+				tst_res(TWARN | TERRNO, "rmdir %s failed", mnt_path);
+			tst_brk(TCONF,
+				 "Cgroup v1 is not configured in kernel");
+		}

We should probably handle also EBUSY, for cases when controller is already part of existing hierarchy. E.g. cpu,cpuacct is mounted together, and someone
tries to mount just cpu:
mount("none", "/mnt/cgroup", "cgroup", MS_MGC_VAL, "cpu") = -1 EBUSY (Device or resource busy)

lib/tst_cgroup.c line 123

Quoted 2 lines
lib/tst_cgroup.c new file +2 -0
+
+void tst_write_memcg1(long memsz)

This should at least say memmax or something similar, if we add functions for more knobs later.

I'm thinking if it would be worth having API more parametrized, something like:

enum tst_cgroup_ctrl {
TST_CGROUP_MEMCG = 1,
TST_CGROUP_CPUSET = 2,
};

tst_cgroup_mount(enum tst_cgroup_ctrl) tst_cgroup_umount(enum tst_cgroup_ctrl)
// tests wouldn't need to use these ones directly // would be probably internal functions

tst_cgroup_version()
// to get/check cgroup support in setup()

tst_cgroup_create(enum tst_cgroup_ctrl, const char *dir)
// mounts cgroup if not already mounted
// creates "dir", sets up subtree_control, etc.

tst_cgroup_cleanup()
// cleans up everything, removes dirs, umounts what was mounted

tst_cgroup_move_current(enum tst_cgroup_ctrl, const char *dir)
// writes getpid() to dir/"tasks"

tst_cgroup_mem_set_maxbytes(const char *dir, long memsz)
// memcg specific function

author
#5 lore raw

RFC PATCH 1/4 lib: add new cgroup test API

Quoted 11 lines · Jan Stancek

----- Original Message -----

include/tst_cgroup.h line 22

Quoted 4 lines
include/tst_cgroup.h new file +4 -0
+
+/* cgroup v1 */
+#define PATH_TMP_CG1_MEM     "/tmp/cgroup1_mem"
+#define PATH_TMP_CG1_CST     "/tmp/cgroup1_cpuset"

It's only used for mount, so not sure if making it relative to TMPDIR buys us anything.

I choose to use /tmp dir just because the cgroup is mounted temporarily for LTP test and destroyed after using. And I have no preference to use other places(i.e. /mnt or /dev).

Quoted 8 lines

lib/tst_cgroup.c line 52

Quoted 5 lines
lib/tst_cgroup.c new file +4 -0
+
+/* cgroup v1 */
+void tst_mount_cgroup1(const char *name, const char *option,
+                     const char *mnt_path, const char *new_path)

I'd make all cgroup API start with tst_cgroup*.

Good point. I agree to make the function start with that prefix.

The logic in this patch looks like:

tst_cgroup1_mount(...)
tst_cgroup2_mount(...)
// these two API as the basic/internal function for a different version of cgroup mounting

tst_cgroup1_mem(){
tst_cgroup1_mount("with mem para")
}
tst_cgroup2_mem(){
tst_cgroup2_mem("no need set mem, becase v2 has only one heierarchy")
}
// these two API mount different cgroup hierarchy via tst_cgroup*_mount()

tst_cgroup_mem()

{
    //if v1
    tst_cgroup1_mem()
    // if v2
    tst_cgroup2_mem()
}
tst_cgroup_cpu()
{
    // if v1
    tst_cgroup1_cpuset();
    // if v2
    tst_cgroup2_cpuset("no cpuset, skip this invoke");
}

// actully, we only use tst_cgroup_*() in testcase, they as the finial API to export to

Quoted 4 lines

Is the intent to provide API for both v1 and v2, or just higher level API that hides the details?

The former. My original purpose is to provide unified APIs for both v1 and v2. Testcase author does not need to care about the difference of two versions, if he/she want to set max bytes in tests, just invoke:

//in setup()
tst_cgroup_mem()
//in main()
tst_cgroup_mem_set_maxbytes()
//in cleanup()
tst_cgroup_umount()

Quoted 15 lines

lib/tst_cgroup.c

Quoted 10 lines
lib/tst_cgroup.c new file +9 -0
+{
+     if (tst_is_mounted(mnt_path))
+             goto out;
+
+     SAFE_MKDIR(mnt_path, 0777);
+     if (mount(name, mnt_path, "cgroup", 0, option) == -1) {
+             if (errno == ENODEV) {
+                     if (rmdir(mnt_path) == -1)
+                             tst_res(TWARN | TERRNO, "rmdir %s failed",

mnt_path);

lib/tst_cgroup.c line 64

Quoted 3 lines
lib/tst_cgroup.c new file +3 -0
+                     tst_brk(TCONF,
+                              "Cgroup v1 is not configured in kernel");
+             }
Quoted 7 lines

We should probably handle also EBUSY, for cases when controller is already part
of existing hierarchy. E.g. cpu,cpuacct is mounted together, and someone tries to mount just cpu:
mount("none", "/mnt/cgroup", "cgroup", MS_MGC_VAL, "cpu") = -1 EBUSY
(Device or resource busy)

That's true.

But in general, people are not permitted to use tst_cgroup*_mount() directly, it is only as the low-level/internal function to hide details we mount cgroup. My previous thought is that, in v1, cpu,cpuacct are bound together(as system way) in tst_croup_cpu().

Quoted 7 lines

lib/tst_cgroup.c line 123

Quoted 3 lines
lib/tst_cgroup.c new file +2 -0
+
+void tst_write_memcg1(long memsz)

This should at least say memmax or something similar, if we add functions for more knobs later.

Good suggestion.

Quoted 4 lines

I'm thinking if it would be worth having API more parametrized, something like:

Sounds neat, we could have a try and then there is no need to export too many functions with _mem() or _cpu() suffixes.

I'd like to merge your method with my original basic functions, it seems not very hard to achieve patch v2:

tst_cgroup_create(enum tst_cgroup_ctrl ctrl)

{
    tst_cgroup_version();

    switch(ctrl) {
    case TST_CGROUP_MEMCG:
        // if v1
        tst_cgroup1_mount(TST_CGROUP_MEMCG);
        // if v2
        tst_cgroup2_mount()
    break;
    case TST_CGROUP_CPUSET:
          //if v1
          tst_cgroup1_mount(TST_CGROUP_CPUSET);
         // if v2
         TCONF here...
    break;
    }
}
Quoted 11 lines

enum tst_cgroup_ctrl {
TST_CGROUP_MEMCG = 1,
TST_CGROUP_CPUSET = 2,
};

tst_cgroup_mount(enum tst_cgroup_ctrl) tst_cgroup_umount(enum tst_cgroup_ctrl)
// tests wouldn't need to use these ones directly // would be probably internal functions

Quoted 5 lines

tst_cgroup_version()
// to get/check cgroup support in setup()

tst_cgroup_create(enum tst_cgroup_ctrl, const char *dir)

Maybe we can drop the second parameter "dir", the mount functions are internal and we just use path macros in sub-function which like what I did.

Quoted 13 lines

// mounts cgroup if not already mounted
// creates "dir", sets up subtree_control, etc.

tst_cgroup_cleanup()
// cleans up everything, removes dirs, umounts what was mounted

tst_cgroup_move_current(enum tst_cgroup_ctrl, const char *dir)
// writes getpid() to dir/"tasks"

tst_cgroup_mem_set_maxbytes(const char *dir, long memsz)
// memcg specific function

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200526/d4d6589c/attachment.htm>
#6 lore raw

RFC PATCH 1/4 lib: add new cgroup test API

----- Original Message -----

Quoted 8 lines
Quoted 7 lines

We should probably handle also EBUSY, for cases when controller is already part
of existing hierarchy. E.g. cpu,cpuacct is mounted together, and someone tries to mount just cpu:
mount("none", "/mnt/cgroup", "cgroup", MS_MGC_VAL, "cpu") = -1 EBUSY
(Device or resource busy)

Quoted 6 lines

That's true.

But in general, people are not permitted to use tst_cgroup*_mount() directly, it is only as the low-level/internal function to hide details we mount cgroup. My previous thought is that, in v1, cpu,cpuacct are bound together(as system way) in tst_croup_cpu().

They don't need to use tst_cgroup*_mount() directly, they could change their system config and mount cpu,cpuacct,memcg together. Though chances of that happening are low.

Quoted 6 lines
Quoted 2 lines

tst_cgroup_create(enum tst_cgroup_ctrl, const char *dir)

Maybe we can drop the second parameter "dir", the mount functions are internal and we just use path macros in sub-function which like what I did.

I wanted to keep some flexibility in case test needs multiple cgroups. I'll have a look at v1 you posted today.