update memcg controller regression tests
cover update memcg controller regression tests
These patches aim to update old tests in controllers/memcg to use new test infastructure for maintainability as well as to update the tests to work when the memory controller is mounted on the cgroup2 hierarchy.
I will more or less be using these changes as a template to update other tests to work under cgroup2 (or at least report something reasonable instead of failing to mount), so I would appreciate any suggestions anyone has.
Luke Nowakowski-Krijger (2):
controllers/memcg: Update regression tests to newer test infastructure controllers/memcg: Add cgroup2 support for regression tests
.../memcg/regression/memcg_regression_test.sh | 175 +++++++++++------- .../memcg/regression/memcg_test_1.c | 12 +- .../memcg/regression/memcg_test_3.c | 35 ++-- .../memcg/regression/memcg_test_4.sh | 25 ++- 4 files changed, 145 insertions(+), 102 deletions(-)
Signature · 5 lines
2.32.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 1/2 controllers/memcg: Update regression tests to newer test infastructure
Update tests to use newer test infastructure for better maintainability and clarity.
Mount cgroup hierarchy to absolute path /dev/memcg for easier compatibility across tests and to avoid creating files in the test directory.
--- .../memcg/regression/memcg_regression_test.sh | 132 ++++++++---------- .../memcg/regression/memcg_test_1.c | 2 +- .../memcg/regression/memcg_test_4.sh | 16 +-- 3 files changed, 70 insertions(+), 80 deletions(-)
testcases/kernel/controllers/memcg/regression/memcg_regression_test.sh +61 -71
@@ -24,27 +24,15 @@ ## ## ################################################################################ -cd $LTPROOT/testcases/bin +TST_ID="memcg_regression_test" +TST_CLEANUP=cleanup +TST_SETUP=setup +TST_TESTFUNC=test_ +TST_NEEDS_ROOT=1 +TST_NEEDS_CMDS="killall kill" +TST_CNT=4 -export TCID="memcg_regression_test" -export TST_TOTAL=4 -export TST_COUNT=1 - -if [ "$(id -ru)" != 0 ]; then - tst_brkm TBROK ignored "Test must be run as root" - exit 0 -fi - -if [ "x$(grep -w memory /proc/cgroups | cut -f4)" != "x1" ]; then - tst_resm TCONF "Either memory resource controller kernel support absent" - tst_resm TCONF "or feature is not enabled; skipping all memcgroup testcases." - exit 0 -fi - -if tst_kvcmp -lt "2.6.30"; then - tst_brkm TBROK ignored "Test should be run with kernel 2.6.30 or newer" - exit 0 -fi +. tst_test.sh #buffer can rotate and number of found bugs can actually go down #so clear the buffer to avoid this
@@ -70,16 +58,16 @@ check_kernel_bug() # some kernel bug is detected if [ $new_bug -gt $nr_bug ]; then - tst_resm TFAIL "kernel BUG was detected!" + tst_res TFAIL "kernel BUG was detected!" fi if [ $new_warning -gt $nr_warning ]; then - tst_resm TFAIL "kernel WARNING was detected!" + tst_res TFAIL "kernel WARNING was detected!" fi if [ $new_null -gt $nr_null ]; then - tst_resm "kernel NULL pointer dereference!" + tst_res TWARN "kernel NULL pointer dereference!" fi if [ $new_lockdep -gt $nr_lockdep ]; then - tst_resm "kernel lockdep warning was detected!" + tst_res TWARN "kernel lockdep warning was detected!" fi nr_bug=$new_bug
@@ -89,10 +77,34 @@ check_kernel_bug() echo "check_kernel_bug found something!" dmesg - failed=1 return 0 } +setup() +{ + mount_point="/dev/memcg" + test_path="$mount_point/0" + + if tst_kvcmp -lt "2.6.30"; then + tst_brk TBROK "Test should be run with kernel 2.6.30 or newer" + fi + + exist=$(grep -w memory /proc/cgroups | cut -f1); + if [ "$exist" = "" ]; then + tst_brk TCONF "memory controller not supported" + fi + + ROD mkdir -p $mount_point + ROD mount -t cgroup -o memory none $mount_point +} + +cleanup() +{ + rmdir "$test_path" + umount "$mount_point" + rmdir "$mount_point" +} + #--------------------------------------------------------------------------- # Bug: The bug was, while forking mass processes, trigger memcgroup OOM, # then NULL pointer dereference may be hit.
@@ -102,16 +114,16 @@ check_kernel_bug() #--------------------------------------------------------------------------- test_1() { - mkdir memcg/0/ - echo 0 > memcg/0/memory.limit_in_bytes + ROD mkdir -p "$test_path" + ROD echo 0 > "$test_path"/memory.limit_in_bytes ./memcg_test_1 - rmdir memcg/0/ + ROD rmdir "$test_path" check_kernel_bug if [ $? -eq 1 ]; then - tst_resm TPASS "no kernel bug was found" + tst_res TPASS "no kernel bug was found" fi }
@@ -128,15 +140,15 @@ test_2() pid1=$! sleep 1 - mkdir memcg/0 - echo $pid1 > memcg/0/tasks + ROD mkdir -p "$test_path" + ROD echo $pid1 > "$test_path"/tasks # let pid1 'test_2' allocate memory /bin/kill -SIGUSR1 $pid1 sleep 1 # shrink memory - echo 1 > memcg/0/memory.limit_in_bytes 2>&1 & + echo 1 > "$test_path"/memory.limit_in_bytes 2>&1 & pid2=$! # check if 'echo' will exit and exit with failure
@@ -146,26 +158,25 @@ test_2() if [ $? -ne 0 ]; then wait $pid2 if [ $? -eq 0 ]; then - tst_resm TFAIL "echo should return failure" - failed=1 + tst_res TFAIL "echo should return failure" kill -9 $pid1 $pid2 > /dev/null 2>&1 wait $pid1 $pid2 - rmdir memcg/0 + ROD rmdir "$test_path" + return fi break fi done if [ $tmp -eq 5 ]; then - tst_resm TFAIL "'echo' doesn't exit!" - failed=1 + tst_res TFAIL "'echo' doesn't exit!" else - tst_resm TPASS "EBUSY was returned as expected" + tst_res TPASS "EBUSY was returned as expected" fi kill -9 $pid1 $pid2 > /dev/null 2>&1 wait $pid1 $pid2 > /dev/null 2>&1 - rmdir memcg/0 + ROD rmdir "$test_path" } #---------------------------------------------------------------------------
@@ -176,19 +187,19 @@ test_2() #--------------------------------------------------------------------------- test_3() { - mkdir memcg/0 - for pid in `cat memcg/tasks`; do - echo $pid > memcg/0/tasks 2> /dev/null + ROD mkdir -p "$test_path" + for pid in $(cat "$mount_point"/tasks); do + echo $pid > "$test_path"/tasks 2> /dev/null done - for pid in `cat memcg/0/tasks`; do - echo $pid > memcg/tasks 2> /dev/null + for pid in $(cat "$test_path"/tasks); do + echo $pid > "$mount_point"/tasks 2> /dev/null done - rmdir memcg/0 + rmdir "$test_path" check_kernel_bug if [ $? -eq 1 ]; then - tst_resm TPASS "no kernel bug was found" + tst_res TPASS "no kernel bug was found" fi }
@@ -200,11 +211,12 @@ test_3() #--------------------------------------------------------------------------- test_4() { + ROD mkdir -p "$test_path" ./memcg_test_4.sh check_kernel_bug if [ $? -eq 1 ]; then - tst_resm TPASS "no kernel bug was found" + tst_res TPASS "no kernel bug was found" fi # test_4.sh might be killed by oom, so do clean up here
@@ -212,31 +224,9 @@ test_4() killall -9 memcg_test_4.sh 2> /dev/null # if test_4.sh gets killed, it won't clean cgroup it created - rmdir memcg/0 2> /dev/null + rmdir "$test_path" 2> /dev/null swapon -a } -# main -failed=0 -mkdir memcg/ - -for cur in $(seq 1 $TST_TOTAL); do - export TST_COUNT=$cur - - mount -t cgroup -o memory xxx memcg/ - if [ $? -ne 0 ]; then - tst_resm TFAIL "failed to mount memory subsystem" - failed=1 - continue - fi - - test_$cur - - umount memcg/ -done - -rmdir memcg/ - -exit $failed - +tst_run
testcases/kernel/controllers/memcg/regression/memcg_test_1.c +1 -1
@@ -40,7 +40,7 @@ int main(void)int loop;int pid;int size =getpagesize (); -int fd =open ("memcg/0/tasks" ,O_WRONLY ); +int fd =open ("/dev/memcg/0/tasks" ,O_WRONLY );if (fd <0 )return 1 ;
testcases/kernel/controllers/memcg/regression/memcg_test_4.sh +8 -8
@@ -22,9 +22,10 @@ ## ## ################################################################################ -# attach current task to memcg/0/ -mkdir memcg/0 -echo $$ > memcg/0/tasks +mount_point="/dev/memcg" +test_path="$mount_point"/0 + +echo $$ > "$test_path"/tasks ./memcg_test_4 & pid=$!
@@ -35,14 +36,13 @@ sleep 1 sleep 1 # shrink memory, and then 80M will be swapped -echo 40M > memcg/0/memory.limit_in_bytes +echo 40M > "$test_path"/memory.limit_in_bytes # turn off swap, and swapoff will be killed swapoff -a sleep 1 -echo $pid > memcg/tasks 2> /dev/null -echo $$ > memcg/tasks 2> /dev/null +echo $pid > "$mount_point"/tasks 2> /dev/null +echo $$ > "$mount_point"/tasks 2> /dev/null # now remove the cgroup -rmdir memcg/0 - +rmdir "$test_path"
Signature · 5 lines
2.32.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 2/2 controllers/memcg: Add cgroup2 support for regression tests
In reply to Luke Nowakowski-KrijgerAdd support for regression tests when memory controller is mounted on cgroup2 hierarchy.
Most of the tests are still useful and require changing some parameters to match the cgroup2 memory interface.
--- .../memcg/regression/memcg_regression_test.sh | 63 +++++++++++++++---- .../memcg/regression/memcg_test_1.c | 12 +++- .../memcg/regression/memcg_test_3.c | 35 +++++------ .../memcg/regression/memcg_test_4.sh | 17 +++-- 4 files changed, 90 insertions(+), 37 deletions(-)
testcases/kernel/controllers/memcg/regression/memcg_regression_test.sh +52 -11
@@ -80,6 +80,37 @@ check_kernel_bug() return 0 } +setup_cgroupv2() +{ + cgroup_mount_point=$(grep -w cgroup2 /proc/mounts | cut -f 2 | cut -d " " -f2) + if ! grep -q memory "$cgroup_mount_point"/cgroup.controllers; then + tst_res TINFO "memory controller not supported on cgroup v2." + return + fi + + ROD mkdir -p $mount_point + ROD mount -t cgroup2 none $mount_point + + task_list="cgroup.procs" + memory_limit="memory.max" + cgroup_v="v2" +} + +setup_cgroupv1() +{ + exist=$(grep -w memory /proc/cgroups | cut -f1); + if [ "$exist" = "" ]; then + tst_brk TCONF NULL "memory controller not supported" + fi + + ROD mkdir -p $mount_point + ROD mount -t cgroup -o memory none $mount_point + + task_list="tasks" + memory_limit="memory.limit_in_bytes" + cgroup_v="v1" +} + setup() { mount_point="/dev/memcg"
@@ -89,13 +120,15 @@ setup() tst_brk TBROK "Test should be run with kernel 2.6.30 or newer" fi - exist=$(grep -w memory /proc/cgroups | cut -f1); - if [ "$exist" = "" ]; then - tst_brk TCONF "memory controller not supported" + if grep -q cgroup2 /proc/mounts; then + setup_cgroupv2 fi - ROD mkdir -p $mount_point - ROD mount -t cgroup -o memory none $mount_point + if [ -z "$cgroup_v" ]; then + setup_cgroupv1 + fi + + tst_res TINFO "test starts with cgroup $cgroup_v" } cleanup()
@@ -115,7 +148,7 @@ cleanup() test_1() { ROD mkdir -p "$test_path" - ROD echo 0 > "$test_path"/memory.limit_in_bytes + ROD echo 0 > "$test_path/$memory_limit" ./memcg_test_1
@@ -136,6 +169,14 @@ test_1() #--------------------------------------------------------------------------- test_2() { + # for cgroup2 writing to memory.max first checks the new limit against the + # current usage and will start killing processes if oom, therefore we do not + # expect EBUSY to be returned by the shrink operation. + if [ "$cgroup_v" = "v2" ]; then + tst_res TCONF "Cgroup v2 found, skipping test" + return + fi + ./memcg_test_2 & pid1=$! sleep 1
@@ -188,12 +229,12 @@ test_2() test_3() { ROD mkdir -p "$test_path" - for pid in $(cat "$mount_point"/tasks); do - echo $pid > "$test_path"/tasks 2> /dev/null + for pid in $(cat "$mount_point/$task_list"); do + echo $pid > "$test_path/$task_list" 2> /dev/null done - for pid in $(cat "$test_path"/tasks); do - echo $pid > "$mount_point"/tasks 2> /dev/null + for pid in $(cat "$test_path/$task_list"); do + echo $pid > "$mount_point/$task_list" 2> /dev/null done rmdir "$test_path"
@@ -212,7 +253,7 @@ test_3() test_4() { ROD mkdir -p "$test_path" - ./memcg_test_4.sh + ./memcg_test_4.sh "$cgroup_v" check_kernel_bug if [ $? -eq 1 ]; then
testcases/kernel/controllers/memcg/regression/memcg_test_1.c +11 -1
@@ -33,6 +33,16 @@#define FORKED_PROC_COUNT 10 +static int open_cgroup_tasklist (void ) +{ +int fd =open ("/dev/memcg/0/tasks" ,O_WRONLY ); +if (fd >=0 ) +return fd; + + fd =open ("/dev/memcg/0/cgroup.procs" ,O_WRONLY ); +return fd; +} +int main (void ) {char buf[10 ];
@@ -40,7 +50,7 @@ int main(void)int loop;int pid;int size =getpagesize (); -int fd =open ("/dev/memcg/0/tasks" ,O_WRONLY ); +int fd =open_cgroup_tasklist ();if (fd <0 )return 1 ;
testcases/kernel/controllers/memcg/regression/memcg_test_3.c +14 -21
@@ -17,12 +17,12 @@#include <sys/types.h> #include <sys/mount.h> #include "tst_test.h" +#include "tst_cgroup.h" -#define MNTPOINT "memcg" -#define SUBDIR "memcg/testdir" -static int mount_flag;static volatile int sigcounter; +static const struct tst_cgroup_group *cg; +static struct tst_cgroup_group *test_cg; static void sighandler(int sig LTP_ATTRIBUTE_UNUSED) {
@@ -48,9 +48,10 @@ static void do_test(void)do_child ();while (sigcounter <50000 ) { -if (access (SUBDIR ,F_OK )) -SAFE_MKDIR (SUBDIR ,0644 ); -rmdir (SUBDIR ); + test_cg =tst_cgroup_group_mk (cg,"test" ); + +if (test_cg) + test_cg =tst_cgroup_group_rm (test_cg); }SAFE_KILL (cpid,SIGKILL );
@@ -61,32 +62,24 @@ static void do_test(void)static void setup (void ) { -int ret; - -SAFE_MKDIR (MNTPOINT ,0644 ); +tst_cgroup_require ("memory" ,NULL ); - ret =mount ("memcg" ,MNTPOINT ,"cgroup" ,0 ,"memory" ); -if (ret) { -if (errno ==ENOENT ) -tst_brk (TCONF |TERRNO ,"memcg not supported" ); + cg =tst_cgroup_get_test_group (); -tst_brk (TCONF |TERRNO ,"mounting memcg failed" ); - } - mount_flag =1 ; +if (TST_CGROUP_VER (cg,"memory" ) !=TST_CGROUP_V1 ) +SAFE_CGROUP_PRINT (cg,"cgroup.subtree_control" ,"+memory" ); }static void cleanup (void ) { -if (!access (SUBDIR ,F_OK )) -SAFE_RMDIR (SUBDIR ); +if (test_cg) + test_cg =tst_cgroup_group_rm (test_cg); -if (mount_flag) -tst_umount (MNTPOINT ); +tst_cgroup_cleanup (); }static struct tst_test test = { .needs_root =1 , - .needs_tmpdir =1 , .forks_child =1 , .min_kver ="2.6.24" , .setup = setup,
testcases/kernel/controllers/memcg/regression/memcg_test_4.sh +13 -4
@@ -22,10 +22,19 @@ ## ## ################################################################################ +cgroup_v=$1 mount_point="/dev/memcg" test_path="$mount_point"/0 -echo $$ > "$test_path"/tasks +if [ $cgroup_v = "v2" ]; then + task_list="cgroup.procs" + memory_limit="memory.max" +else + task_list="tasks" + memory_limit="memory.limit_in_bytes" +fi + +echo $$ > "$test_path/$memory_limit" ./memcg_test_4 & pid=$!
@@ -36,13 +45,13 @@ sleep 1 sleep 1 # shrink memory, and then 80M will be swapped -echo 40M > "$test_path"/memory.limit_in_bytes +echo 40M > "$test_path/$memory_limit" # turn off swap, and swapoff will be killed swapoff -a sleep 1 -echo $pid > "$mount_point"/tasks 2> /dev/null -echo $$ > "$mount_point"/tasks 2> /dev/null +echo $pid > "$mount_point/$task_list" 2> /dev/null +echo $$ > "$mount_point/$task_list" 2> /dev/null # now remove the cgroup rmdir "$test_path"
Signature · 5 lines
2.32.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp