Skip to contentSkip to conversation

Begin converting kselftest memcg tests

PATCH In review Richard Palethorpe via ltp 3 participants 10 messages Dec 14, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Begin converting kselftest memcg tests

Hello,

This is the begining of a piecmeal conversion of the memcg kselftests. The first test is fairly redundant. However it allows for some organizational discussion.

The motivation for converting these tests is as follows:

1. It's easier to run these tests across our full product range as part of the LTP.
2. The LTP CGroup library allows testing on V1 and V2. The kselftest's only support V2.
3. The kselftests appear to cover some things which existing LTP tests do not.

Richard Palethorpe (4):
API: cgroup: Add safe_cgroup_occursin
API: cgroup: Add cgroup.controllers
memcontrol01: Import first memcg kselftest scripts/coccinelle: Helper for converting CGroup selftests

 include/tst_cgroup.h                          |  8 ++
 lib/tst_cgroup.c                              | 13 ++++
 runtest/controllers                           |  3 +
 .../coccinelle/kselftest-cgroup-to-ltp.cocci  | 40 ++++++++++
 testcases/kernel/controllers/memcg/.gitignore |  1 +
 .../kernel/controllers/memcg/memcontrol01.c   | 77 +++++++++++++++++++
 6 files changed, 142 insertions(+)
 create mode 100644 scripts/coccinelle/kselftest-cgroup-to-ltp.cocci
 create mode 100644 testcases/kernel/controllers/memcg/memcontrol01.c
Signature · 5 lines
2.34.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH 1/4 API: cgroup: Add safe_cgroup_occursin

Similar to cg_read_strstr from kselftests. The name was changed because strstr from string.h returns a ptr and this does not.

---
 include/tst_cgroup.h |  8 ++++++++
 lib/tst_cgroup.c     | 12 ++++++++++++
 2 files changed, 20 insertions(+)
include/tst_cgroup.h +8 -0
index 9e34321e4..632050e86 100644
@@ -191,5 +191,13 @@ void safe_cgroup_scanf(const char *file, const int lineno,
 		       const char *fmt, ...)
 		       __attribute__ ((format (scanf, 5, 6), nonnull));
 
+#define SAFE_CGROUP_OCCURSIN(cg, file_name, needle)		\
+	safe_cgroup_occursin(__FILE__, __LINE__,		\
+			     (cg), (file_name), (needle))
+
+int safe_cgroup_occursin(const char *file, const int lineno,
+			 const struct tst_cgroup_group *const cg,
+			 const char *const file_name,
+			 const char *const needle);
 
 #endif /* TST_CGROUP_H */
lib/tst_cgroup.c +12 -0
index 61cc02fa7..f85dc4fdd 100644
@@ -1153,3 +1153,15 @@ void safe_cgroup_scanf(const char *const file, const int lineno,
 		 "'%s': vsscanf('%s', '%s', ..): Less conversions than expected: %d != %d",
 		 file_name, buf, fmt, ret, conv_cnt);
 }
+
+int safe_cgroup_occursin(const char *const file, const int lineno,
+			 const struct tst_cgroup_group *const cg,
+			 const char *const file_name,
+			 const char *const needle)
+{
+	char buf[BUFSIZ];
+
+	safe_cgroup_read(file, lineno, cg, file_name, buf, sizeof(buf));
+
+	return !!strstr(buf, needle);
+}
Signature · 5 lines
2.34.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH 2/4 API: cgroup: Add cgroup.controllers

In reply to Richard Palethorpe via ltp
---
 lib/tst_cgroup.c | 1 +
 1 file changed, 1 insertion(+)
lib/tst_cgroup.c +1 -0
index f85dc4fdd..eee89f3d5 100644
@@ -147,6 +147,7 @@ typedef struct cgroup_file files_t[];
 static const files_t cgroup_ctrl_files = {
 	/* procs exists on V1, however it was read-only until kernel v3.0. */
 	{ "cgroup.procs", "tasks", 0 },
+	{ "cgroup.controllers", NULL, 0 },
 	{ "cgroup.subtree_control", NULL, 0 },
 	{ "cgroup.clone_children", "cgroup.clone_children", 0 },
 	{ }
Signature · 5 lines
2.34.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

PATCH 3/4 memcontrol01: Import first memcg kselftest

In reply to Richard Palethorpe via ltp

This is a conversion of the first test in the memory cgroup kselftest. There will be a number of (more important) tests to follow; i.e. memcontrol02...

---
 runtest/controllers                           |  3 +
 testcases/kernel/controllers/memcg/.gitignore |  1 +
 .../kernel/controllers/memcg/memcontrol01.c   | 77 +++++++++++++++++++
 3 files changed, 81 insertions(+)
 create mode 100644 testcases/kernel/controllers/memcg/memcontrol01.c
runtest/controllers +3 -0
index e3d0243f1..2b41a94d3 100644
@@ -16,6 +16,9 @@ memcg_usage_in_bytes	memcg_usage_in_bytes_test.sh
 memcg_stress		memcg_stress_test.sh
 memcg_control		memcg_control_test.sh
 
+# kselftest ports
+memcontrol01 memcontrol01
+
 cgroup_fj_function_debug cgroup_fj_function.sh debug
 cgroup_fj_function_cpuset cgroup_fj_function.sh cpuset
 cgroup_fj_function_cpu cgroup_fj_function.sh cpu
testcases/kernel/controllers/memcg/.gitignore +1 -0
index c0b6d0714..c3565f85c 100644
@@ -5,3 +5,4 @@
 /regression/memcg_test_3
 /regression/memcg_test_4
 /stress/memcg_process_stress
+memcontrol01
testcases/kernel/controllers/memcg/memcontrol01.c new file +77 -0

77 lines not on this page · raw message

Signature · 5 lines
2.34.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 3/4
#6 lore raw

Re: [LTP] [PATCH 3/4] memcontrol01: Import first memcg kselftest

Hi!

testcases/kernel/controllers/memcg/memcontrol01.c line 6

Quoted 6 lines
testcases/kernel/controllers/memcg/memcontrol01.c new file +6 -0
+// SPDX-License-Identifier: GPL-2.0
+/*\
+ *
+ * [Description]
+ *
+ * Conversion of first kselftest in cgroup/test_memcontrol.c.

Can we please have a better description of the test here?

Quoted 5 lines

+ */

testcases/kernel/controllers/memcg/memcontrol01.c new file +4 -0
+#define _GNU_SOURCE
+
+#include <stdio.h>
+

testcases/kernel/controllers/memcg/memcontrol01.c line 22

Quoted 10 lines
testcases/kernel/controllers/memcg/memcontrol01.c new file +10 -0
+#include "tst_test.h"
+#include "tst_cgroup.h"
+
+static const struct tst_cgroup_group *cg_test;
+static struct tst_cgroup_group *parent, *child;
+static struct tst_cgroup_group *parent2, *child2;
+
+/*
+ * This test creates two nested cgroups with and without enabling
+ * the memory controller.

I guess that this part of the comment should really be in the description right?

Other than that the test is really straightforward:

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

PATCH 4/4 scripts/coccinelle: Helper for converting CGroup selftests

In reply to Richard Palethorpe via ltp

Helps with bulk rewriting.

---
 .../coccinelle/kselftest-cgroup-to-ltp.cocci  | 40 +++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 scripts/coccinelle/kselftest-cgroup-to-ltp.cocci
scripts/coccinelle/kselftest-cgroup-to-ltp.cocci new file +40 -0
new file mode 100644
index 000000000..f91c2c467
@@ -0,0 +1,40 @@
+@@
+expression cgn, cgns;
+@@
+
+- cgn = cg_name(..., cgns);
++ cgn = tst_cgroup_group_mk(cg_test, cgns);
+
+@@
+expression cg, fname, data;
+@@
+
+- if (cg_write(cg, fname, data)) {
+-    ...
+- }
++ SAFE_CGROUP_PRINT(cg, fname, data);
+
+@@
+expression cg;
+@@
+
+... when != TST_CGROUP_VER(...)
+
+- SAFE_CGROUP_PRINT(cg, "cgroup.subtree_control", "+memory");
++ if (TST_CGROUP_VER(cg, "memory") != TST_CGROUP_V1)
++    SAFE_CGROUP_PRINT(cg, "cgroup.subtree_control", "+memory");
+
+@@
+expression cg, fname, needle;
+@@
+
+- cg_read_strstr(cg, fname, needle)
++ !SAFE_CGROUP_OCCURSIN(cg, fname, needle)
+
+@@
+identifier l;
+expression cg, fname;
+@@
+
+- l = cg_read_long(cg, fname);
++ SAFE_CGROUP_SCANF(cg, fname, "%ld", &l);
Signature · 5 lines
2.34.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 4/4
#8 lore raw

Re: [LTP] [PATCH 4/4] scripts/coccinelle: Helper for converting CGroup selftests

Hi!

scripts/coccinelle/kselftest-cgroup-to-ltp.cocci line 25

Quoted 9 lines

+@@

scripts/coccinelle/kselftest-cgroup-to-ltp.cocci new file +8 -0
+expression cg;
+@@
+
+... when != TST_CGROUP_VER(...)
+
+- SAFE_CGROUP_PRINT(cg, "cgroup.subtree_control", "+memory");
++ if (TST_CGROUP_VER(cg, "memory") != TST_CGROUP_V1)
++    SAFE_CGROUP_PRINT(cg, "cgroup.subtree_control", "+memory");

I guess that here we operat on already partially converted code right?

Other than this the rest is really straightforward:

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#9 lore raw

Re: [LTP] [PATCH 4/4] scripts/coccinelle: Helper for converting CGroup selftests

Hello,

Quoted 12 lines · Cyril Hrubis

Hi!

scripts/coccinelle/kselftest-cgroup-to-ltp.cocci line 25

Quoted 9 lines

+@@

scripts/coccinelle/kselftest-cgroup-to-ltp.cocci new file +8 -0
+expression cg;
+@@
+
+... when != TST_CGROUP_VER(...)
+
+- SAFE_CGROUP_PRINT(cg, "cgroup.subtree_control", "+memory");
++ if (TST_CGROUP_VER(cg, "memory") != TST_CGROUP_V1)
++    SAFE_CGROUP_PRINT(cg, "cgroup.subtree_control", "+memory");

I guess that here we operat on already partially converted code right?

Correct, in general cg_write is converted to safe_cgroup_print. Then we have this special rule for subtree_control to introduce the version check (if there isn't one already present).

Quoted 5 lines

Other than this the rest is really straightforward:

Signature · 5 lines
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp