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 PAGE_SIZE=$(getconf PAGESIZE) 45 46 TOT_MEM_LIMIT=$PAGE_SIZE 47 ACTIVE_MEM_LIMIT=$PAGE_SIZE 48 PROC_MEM=$((PAGE_SIZE * 2)) 49 50 TST_PATH=$PWD 51 STATUS_PIPE="$TMP/status_pipe" 52 53 PASS=0 54 FAIL=1 55 56 # Check if the test process is killed on crossing boundary 57 test_proc_kill() 58 { 59 cd $TMP 60 mem_process -m $PROC_MEM & 61 cd $OLDPWD 62 sleep 1 63 echo $! > tasks 64 65 #Instruct the test process to start acquiring memory 66 echo m > $STATUS_PIPE 67 sleep 5 68 69 #Check if killed 70 ps -p $! > /dev/null 2> /dev/null 71 if [ $? -eq 0 ]; then 72 echo m > $STATUS_PIPE 73 echo x > $STATUS_PIPE 74 else 75 : $((KILLED_CNT += 1)) 76 fi 77 } 78 79 # Validate the memory usage limit imposed by the hierarchically topmost group 80 testcase_1() 81 { 82 TST_COUNT=1 83 tst_resm TINFO "Test #1: Checking if the memory usage limit imposed by the topmost group is enforced" 84 85 echo "$ACTIVE_MEM_LIMIT" > $TST_PATH/mnt/$TST_NUM/memory.limit_in_bytes 86 echo "$TOT_MEM_LIMIT" > $TST_PATH/mnt/$TST_NUM/memory.memsw.limit_in_bytes 87 88 mkdir sub 89 (cd sub 90 KILLED_CNT=0 91 test_proc_kill 92 93 if [ $PROC_MEM -gt $TOT_MEM_LIMIT ] && [ $KILLED_CNT -eq 0 ]; then 94 result $FAIL "Test #1: failed" 95 else 96 result $PASS "Test #1: passed" 97 fi) 98 rmdir sub 99 } 100 101 # Record the test results 102 # 103 # $1: Result of the test case, $PASS or $FAIL 104 # $2: Output information 105 result() 106 { 107 RES=$1 108 INFO=$2 109 110 if [ $RES -eq $PASS ]; then 111 tst_resm TPASS "$INFO" 112 else 113 : $((FAILED_CNT += 1)) 114 tst_resm TFAIL "$INFO" 115 fi 116 } 117 118 cleanup() 119 { 120 if [ -e $TST_PATH/mnt ]; then 121 umount $TST_PATH/mnt 2> /dev/null 122 rm -rf $TST_PATH/mnt 123 fi 124 } 125 126 do_mount() 127 { 128 cleanup 129 130 mkdir $TST_PATH/mnt 131 mount -t cgroup -o memory cgroup $TST_PATH/mnt 2> /dev/null 132 if [ $? -ne 0 ]; then 133 tst_brkm TBROK NULL "Mounting cgroup to temp dir failed" 134 rmdir $TST_PATH/mnt 135 exit 1 136 fi 137 } 138 139 do_mount 140 141 echo 1 > mnt/memory.use_hierarchy 2> /dev/null 142 143 FAILED_CNT=0 144 145 TST_NUM=1 146 while [ $TST_NUM -le $TST_TOTAL ]; do 147 mkdir $TST_PATH/mnt/$TST_NUM 148 (cd $TST_PATH/mnt/$TST_NUM && testcase_$TST_NUM) 149 rmdir $TST_PATH/mnt/$TST_NUM 150 : $((TST_NUM += 1)) 151 done 152 153 echo 0 > mnt/memory.use_hierarchy 2> /dev/null 154 155 cleanup 156 157 if [ "$FAILED_CNT" -ne 0 ]; then 158 tst_resm TFAIL "memcg_control: failed" 159 exit 1 160 else 161 tst_resm TPASS "memcg_control: passed" 162 exit 0 163 fi 164