1 #!/bin/sh 2 3 ################################################################################ 4 ## ## 5 ## Copyright (c) 2010 Mohamed Naufal Basheer ## 6 ## ## 7 ## This program is free software; you can redistribute it and/or modify ## 8 ## it under the terms of the GNU General Public License as published by ## 9 ## the Free Software Foundation; either version 2 of the License, or ## 10 ## (at your option) any later version. ## 11 ## ## 12 ## This program is distributed in the hope that it will be useful, ## 13 ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## 14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See ## 15 ## the GNU General Public License for more details. ## 16 ## ## 17 ## You should have received a copy of the GNU General Public License ## 18 ## along with this program; if not, write to the Free Software ## 19 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ## 20 ## ## 21 ## ## 22 ## File: memcg_control_test.sh ## 23 ## ## 24 ## Purpose: Implement various memory controller tests ## 25 ## ## 26 ## Author: Mohamed Naufal Basheer <naufal11 (at] gmail.com> ## 27 ## ## 28 ################################################################################ 29 30 if [ "x$(grep -w memory /proc/cgroups | cut -f4)" != "x1" ]; then 31 echo "WARNING:" 32 echo "Either kernel does not support memory resource controller or feature not enabled" 33 echo "Skipping all memcg_control testcases...." 34 exit 0 35 fi 36 37 export TCID="memcg_control" 38 export TST_TOTAL=1 39 export TST_COUNT=0 40 41 export TMP=${TMP:-/tmp} 42 cd $TMP 43 44 TOT_MEM_LIMIT=$1 45 ACTIVE_MEM_LIMIT=$2 46 PROC_MEM=$3 47 48 TST_PATH=$PWD 49 STATUS_PIPE="$TMP/status_pipe" 50 51 PASS=0 52 FAIL=1 53 54 # Check if the test process is killed on crossing boundary 55 test_proc_kill() 56 { 57 cd $TMP 58 mem_process -m $PROC_MEM & 59 cd $OLDPWD 60 sleep 1 61 echo $! > tasks 62 63 #Instruct the test process to start acquiring memory 64 echo m > $STATUS_PIPE 65 sleep 5 66 67 #Check if killed 68 ps -p $! > /dev/null 2> /dev/null 69 if [ $? -eq 0 ]; then 70 echo m > $STATUS_PIPE 71 echo x > $STATUS_PIPE 72 else 73 : $((KILLED_CNT += 1)) 74 fi 75 } 76 77 # Validate the memory usage limit imposed by the hierarchically topmost group 78 testcase_1() 79 { 80 TST_COUNT=1 81 tst_resm TINFO "Test #1: Checking if the memory usage limit imposed by the topmost group is enforced" 82 83 echo "$ACTIVE_MEM_LIMIT" > $TST_PATH/mnt/$TST_NUM/memory.limit_in_bytes 84 echo "$TOT_MEM_LIMIT" > $TST_PATH/mnt/$TST_NUM/memory.memsw.limit_in_bytes 85 86 mkdir sub 87 (cd sub 88 KILLED_CNT=0 89 test_proc_kill 90 91 if [ $PROC_MEM -gt $TOT_MEM_LIMIT ] && [ $KILLED_CNT -eq 0 ]; then 92 result $FAIL "Test #1: failed" 93 else 94 result $PASS "Test #1: passed" 95 fi) 96 rmdir sub 97 } 98 99 # Record the test results 100 # 101 # $1: Result of the test case, $PASS or $FAIL 102 # $2: Output information 103 result() 104 { 105 RES=$1 106 INFO=$2 107 108 if [ $RES -eq $PASS ]; then 109 tst_resm TPASS "$INFO" 110 else 111 : $((FAILED_CNT += 1)) 112 tst_resm TFAIL "$INFO" 113 fi 114 } 115 116 cleanup() 117 { 118 if [ -e $TST_PATH/mnt ]; then 119 umount $TST_PATH/mnt 2> /dev/null 120 rm -rf $TST_PATH/mnt 121 fi 122 } 123 124 do_mount() 125 { 126 cleanup 127 128 mkdir $TST_PATH/mnt 129 mount -t cgroup -o memory cgroup $TST_PATH/mnt 2> /dev/null 130 if [ $? -ne 0 ]; then 131 tst_brkm TBROK NULL "Mounting cgroup to temp dir failed" 132 rmdir $TST_PATH/mnt 133 exit 1 134 fi 135 } 136 137 do_mount 138 139 echo 1 > mnt/memory.use_hierarchy 2> /dev/null 140 141 FAILED_CNT=0 142 143 TST_NUM=1 144 while [ $TST_NUM -le $TST_TOTAL ]; do 145 mkdir $TST_PATH/mnt/$TST_NUM 146 (cd $TST_PATH/mnt/$TST_NUM && testcase_$TST_NUM) 147 rmdir $TST_PATH/mnt/$TST_NUM 148 : $((TST_NUM += 1)) 149 done 150 151 echo 0 > mnt/memory.use_hierarchy 2> /dev/null 152 153 cleanup 154 155 if [ "$FAILED_CNT" -ne 0 ]; then 156 tst_resm TFAIL "memcg_control: failed" 157 exit 1 158 else 159 tst_resm TPASS "memcg_control: passed" 160 exit 0 161 fi 162