1#!/bin/sh 2# SPDX-License-Identifier: GPL-2.0-or-later 3# Copyright (c) 2009 FUJITSU LIMITED 4# Copyright (c) 2018-2019 ARM Ltd. All Rights Reserved. 5# Copyright (c) 2019-2022 Petr Vorel <[email protected]> 6# 7# Author: Li Zefan <[email protected]> 8# Restructure for LTP: Shi Weihua <[email protected]> 9# Added memcg enable/disable functionality: Rishikesh K Rajak <[email protected]> 10 11TST_TESTFUNC=test 12TST_SETUP=setup 13TST_CLEANUP=cleanup 14TST_CNT=2 15TST_NEEDS_ROOT=1 16TST_NEEDS_CMDS="mount umount cat kill mkdir rmdir grep awk cut" 17 18# Each test case runs for 900 secs when everything fine 19# therefore the default 5 mins timeout is not enough. 20TST_TIMEOUT=2100 21 22setup() 23{ 24 cgroup_require "memory" 25 cgroup_version=$(cgroup_get_version "memory") 26 test_path=$(cgroup_get_test_path "memory") 27 task_list=$(cgroup_get_task_list "memory") 28 if [ "$cgroup_version" = "2" ]; then 29 ROD echo "+memory" \> "$test_path/cgroup.subtree_control" 30 fi 31 32 tst_res TINFO "test starts with cgroup version $cgroup_version" 33 34 echo 3 > /proc/sys/vm/drop_caches 35 sleep 2 36 local mem_free=$(awk '/MemFree/ {print $2}' /proc/meminfo) 37 local mem_available=$(awk '/MemAvailable/ {print $2}' /proc/meminfo) 38 local swap_free=$(awk '/SwapFree/ {print $2}' /proc/meminfo) 39 local mem_min=$(cat /proc/sys/vm/min_free_kbytes) 40 41 mem_min=$(( $mem_min + $mem_min/10 )) 42 [ $swap_free -gt $mem_min ] && RESERVED_MEM=0 || RESERVED_MEM=$mem_min 43 [ $mem_free -lt $mem_available ] && MEM=$mem_free || MEM=$mem_available 44 MEM=$(( $MEM - $RESERVED_MEM )) 45 MEM=$(( $MEM / 1024 )) 46 RUN_TIME=$(( 15 * 60 )) 47 48 tst_res TINFO "Calculated available memory $MEM MB" 49} 50 51cleanup() 52{ 53 cgroup_cleanup 54} 55 56# $1 Number of cgroups 57# $2 Allocated MB memory in one process 58# $3 The interval to touch memory in a process 59# $4 Test duration (sec) 60run_stress() 61{ 62 local cgroups="$1" 63 local mem_size="$2" 64 local interval="$3" 65 local timeout="$4" 66 local i pid pids 67 68 tst_res TINFO "Testing $cgroups cgroups, using $mem_size MB, interval $interval" 69 70 tst_res TINFO "Starting cgroups" 71 for i in $(seq 0 $(($cgroups-1))); do 72 ROD mkdir "$test_path/$i" 73 memcg_process_stress $mem_size $interval & 74 ROD echo $! \> "$test_path/$i/$task_list" 75 pids="$pids $!" 76 done 77 78 for pid in $pids; do 79 kill -USR1 $pid 2> /dev/null 80 done 81 82 tst_res TINFO "Testing cgroups for ${timeout}s" 83 sleep $timeout 84 85 tst_res TINFO "Killing groups" 86 i=0 87 for pid in $pids; do 88 kill -KILL $pid 2> /dev/null 89 wait $pid 2> /dev/null 90 ROD rmdir "$test_path/$i" 91 i=$((i+1)) 92 done 93 94 tst_res TPASS "Test passed" 95} 96 97test1() 98{ 99 run_stress 150 $(( $MEM / 150 )) 5 $RUN_TIME 100} 101 102test2() 103{ 104 run_stress 1 $MEM 5 $RUN_TIME 105} 106 107. cgroup_lib.sh 108tst_run 109