1#!/bin/sh
2
3#    Copyright (c) International Business Machines  Corp., 2003
4#
5#    This program is free software;  you can redistribute it and/or modify
6#    it under the terms of the GNU General Public License as published by
7#    the Free Software Foundation; either version 2 of the License, or
8#    (at your option) any later version.
9#
10#    This program is distributed in the hope that it will be useful,
11#    but WITHOUT ANY WARRANTY;  without even the implied warranty of
12#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13#    the GNU General Public License for more details.
14#
15#    You should have received a copy of the GNU General Public License
16#    along with this program;  if not, write to the Free Software
17#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18#
19#   FILE        : ltpstress.sh
20#   DESCRIPTION : A script that will stress your system using the LTP testsuite.
21#   REQUIREMENTS:
22#                 1) The 'rsh' daemon must be running and NFS (versions 2 &3) must be
23#                    configured into the kernel and installed for networking tests.
24#		  2) The 'sar' application must be installed to use the "-S" option
25#   HISTORY     :
26#       02/11/2003 Robbie Williamson (robbiew@austin.ibm.com)
27#               written
28#	11/20/2008 Aime Le Rouzic (aime.lerouzic@bull.net)
29#		adapt script to work with portmap and rpcbind
30##############################################################
31
32export LTPROOT=${PWD}
33echo $LTPROOT | grep testscripts > /dev/null 2>&1
34if [ $? -eq 0 ]; then
35 cd ..
36 export LTPROOT=${PWD}
37fi
38export TMPBASE="/tmp"
39export PATH=$LTPROOT/testcases/bin:$PATH
40memsize=0
41hours=24
42PROC_NUM=0
43leftover_memsize=0
44duration=86400
45datafile="/tmp/ltpstress.data"
46iofile="/tmp/ltpstress.iodata"
47logfile="/tmp/ltpstress.log"
48interval=10
49Sar=0
50Top=0
51Iostat=0
52LOGGING=0
53PRETTY_PRT=""
54QUIET_MODE=""
55NO_NETWORK=0
56
57usage()
58{
59
60	cat <<-END >&2
61    usage: ${0##*/} [ -d datafile ] [ -i # (in seconds) ] [ -I iofile ] [ -l logfile ] [ -m # (in Mb) ]
62    [ -n ] [ -p ] [ -q ] [ -t duration ] [ -x TMPDIR ] [-b DEVICE] [-B LTP_DEV_FS_TYPE] [ [-S]|[-T] ]
63
64    -d datafile     Data file for 'sar' or 'top' to log to. Default is "/tmp/ltpstress.data".
65    -i # (in sec)   Interval that 'sar' or 'top' should take snapshots. Default is 10 seconds.
66    -I iofile       Log results of 'iostat' to a file every interval. Default is "/tmp/ltpstress.iodata".
67    -l logfile      Log results of test in a logfile. Default is "/tmp/ltpstress.log"
68    -m # (in Mb)    Specify the _minimum_ memory load of # megabytes in background. Default is all the RAM + 1/2 swap.
69    -n              Disable networking stress.
70    -p              Human readable format logfiles.
71    -q              Print less verbose output to the output files.
72    -S              Use 'sar' to measure data.
73    -T              Use LTP's modified 'top' tool to measure data.
74    -t duration     Execute the testsuite for given duration in hours. Default is 24.
75    -x TMPDIR       Directory where temporary files will be created.
76    -b DEVICE       Some tests require an unmounted block device
77                    to run correctly. If DEVICE is not set, a loop device is
78                    created and used automatically.
79    -B LTP_DEV_FS_TYPE The file system of DEVICE.
80
81	example: ${0##*/} -d /tmp/sardata -l /tmp/ltplog.$$ -m 128 -t 24 -S
82	END
83exit
84}
85
86check_memsize()
87{
88  while [ $memsize -gt 1048576 ]   #if greater than 1GB
89  do
90    PROC_NUM=$(( PROC_NUM + 1 ))
91    memsize=$(( $memsize - 1048576 ))
92  done
93  leftover_memsize=$memsize
94}
95
96while getopts d:hi:I:l:STt:m:npqx:b:B:\? arg
97do  case $arg in
98
99	d)	datafile="$OPTARG";;
100
101        h)      echo "Help info:"
102		usage;;
103
104	i)	interval=$OPTARG;;
105
106	I)	Iostat=1
107		iofile=$OPTARG;;
108
109        l)      logfile=$OPTARG
110		LOGGING=1;;
111
112        m)	memsize=$(($OPTARG * 1024))
113		check_memsize;;
114
115	n)	NO_NETWORK=1;;
116
117	p)	PRETTY_PRT=" -p ";;
118
119	q)	QUIET_MODE=" -q ";;
120
121        S)      if [ $Top -eq 0 ]; then
122                  Sar=1
123                else
124                  echo "Cannot specify -S and -T...exiting"
125                  exit
126                fi;;
127
128	T)	if [ $Sar -eq 0 ]; then
129                  $LTPROOT/testcases/bin/top -h 2>&1 | grep "\-f filename" >/dev/null
130		  if [ $? -eq 0 ]; then
131                    Top=1
132                  else
133		    echo "ERROR: Please build and install the version of top in the /tools dir"
134		    exit
135 		  fi
136                else
137                  echo "Cannot specify -S and -T...exiting"
138                  exit
139                fi;;
140
141        t)      hours=$OPTARG
142		duration=$(($hours * 60 * 60));;
143
144	x)	export TMPBASE=$(readlink -f ${OPTARG});;
145
146	b)	export LTP_DEV=${OPTARG};;
147
148	B)	export LTP_DEV_FS_TYPE=${OPTARG};;
149
150        \?)     echo "Help info:"
151		usage;;
152        esac
153done
154
155export TMP="${TMPBASE}/ltpstress-$$"
156export TMPDIR=${TMP}
157mkdir -p ${TMP}
158
159# to write as user nobody into tst_tmpdir()
160chmod 777 $TMP || \
161{
162	echo "unable to chmod 777 $TMP ... aborting"
163	exit 1
164}
165
166cd $TMP || \
167{
168	echo "could not cd ${TMP} ... exiting"
169	exit 1
170}
171
172if [ $NO_NETWORK -eq 0 ];then
173  # Networking setup
174  echo `hostname` >> /root/.rhosts
175  chmod 644 /root/.rhosts
176
177  netstat -an | grep 514
178  if [ $? -eq 1 ];then
179    echo "Error: 'rsh' daemon not active on this machine."
180    exit 1
181  fi
182
183  ps -ef | grep portmap | grep -v grep
184  if [ $? -eq 1 ];then
185    ps -ef | grep rpcbind | grep -v grep
186    if [ $? -eq 1 ];then
187      echo "Portmap and rpcbind not running"
188      echo "Let's start portmap"
189      /sbin/portmap &
190      sleep 1
191      ps -ef | grep portmap | grep -v grep
192      if [ $? -eq 1 ];then
193        echo "Could not start portmap, Let's start rpcbind"
194        /sbin/rpcbind &
195        sleep 1
196        ps -ef | grep rpcbind | grep -v grep
197        if [ $? -eq 1 ];then
198          Echo "Error: Could not start rpcbind daemon."
199          exit 1
200        else
201          echo "The RPC test suite is using rpcbind"
202        fi
203      else
204	echo "The RPC test suite is using portmap"
205      fi
206    else
207      echo "The RPC test suite is using rpcbind"
208    fi
209  else
210    echo "The RPC test suite is using portmap"
211  fi
212
213  ps -e | grep nfsd
214  if [ $? -eq 1 ];then
215    /usr/sbin/rpc.nfsd
216  fi
217  sleep 1
218  ps -e | grep nfsd
219  if [ $? -eq 1 ];then
220    echo "Error: Could not start nfs server daemon."
221    exit 1
222  fi
223
224  ps -e | grep rpc.statd
225  if [ $? -eq 1 ];then
226    /sbin/rpc.statd
227  fi
228  sleep 1
229  ps -e | grep rpc.statd
230  if [ $? -eq 1 ];then
231    echo "Error: Could not start statd daemon."
232    exit 1
233  fi
234
235  ps -e | grep rpc.mountd
236  if [ $? -eq 1 ];then
237    /usr/sbin/rpc.mountd
238  fi
239  sleep 1
240  ps -e | grep rpc.mountd
241  if [ $? -eq 1 ];then
242    echo "Error: Could not start mountd daemon."
243    exit 1
244  fi
245  # End of network setup
246fi
247
248#If -m not set, use all the RAM + 1/2 swapspace
249if [ $memsize -eq 0 ]; then
250  TOTALRAM=$(free -m | grep Mem: | awk {'print $2'})
251  TOTALSWAP=$(free -m | grep Swap: | awk {'print $2'})
252  TESTSWAP=$(($TOTALSWAP / 2))
253  if [ $TESTSWAP -eq 0 ]; then
254       #if there is no swap in the system, use only the free RAM
255       TESTMEM=$(free -m | grep Mem: | awk {'print $4'})
256  else
257       TESTMEM=$(($TESTSWAP + $TOTALRAM))
258  fi
259 #Convert to kilobytes
260  memsize=$(($TESTMEM * 1024))
261  check_memsize
262fi
263
264# Set max processes to unlimited.
265ulimit -u unlimited
266
267if [ $PROC_NUM -gt 0 ];then
268  genload --vm $PROC_NUM --vm-bytes 1073741824 >/dev/null 2>&1 &
269fi
270if [ $leftover_memsize -gt 0 ];then
271  genload --vm 1 --vm-bytes $(($leftover_memsize * 1024)) >/dev/null 2>&1 &
272fi
273
274if [ $NO_NETWORK -eq 0 ];then
275 netpipe.sh >/dev/null 2>/dev/null &
276fi
277${LTPROOT}/bin/rand_lines -g ${LTPROOT}/runtest/stress.part1 > ${TMP}/stress.part1
278${LTPROOT}/bin/rand_lines -g ${LTPROOT}/runtest/stress.part2 > ${TMP}/stress.part2
279${LTPROOT}/bin/rand_lines -g ${LTPROOT}/runtest/stress.part3 > ${TMP}/stress.part3
280
281sleep 2
282
283if [ $Sar -eq 1 ]; then
284  sar -o $datafile $interval > /dev/null &
285fi
286
287if [ $Top -eq 1 ]; then
288  screen -d -m $LTPROOT/testcases/bin/top -o $datafile -d $interval &
289  SCREEN_PID=$(ps -e | grep screen | awk {'print $1'})
290fi
291
292sleep 2
293
294if [ $Iostat -eq 1 ]; then
295  while [ 0 = 0 ];do iostat -dt >> $iofile; sleep $interval;done &
296  Iostat_PID=$?
297fi
298
299sleep 2
300
301output1=${TMPBASE}/ltpstress.$$.output1
302output2=${TMPBASE}/ltpstress.$$.output2
303output3=${TMPBASE}/ltpstress.$$.output3
304
305${LTPROOT}/bin/ltp-pan -e ${PRETTY_PRT} ${QUIET_MODE} -S -t ${hours}h -a stress1 -n stress1 -l $logfile -f ${TMP}/stress.part1 -o $output1 &
306${LTPROOT}/bin/ltp-pan -e ${PRETTY_PRT} ${QUIET_MODE} -S -t ${hours}h -a stress2 -n stress2 -l $logfile -f ${TMP}/stress.part2 -o $output2 &
307${LTPROOT}/bin/ltp-pan -e ${PRETTY_PRT} ${QUIET_MODE} -S -t ${hours}h -a stress3 -n stress3 -l $logfile -f ${TMP}/stress.part3 -o $output3 &
308
309echo "Running LTP Stress for $hours hour(s) using $(($memsize/1024)) Mb"
310echo ""
311echo "Test output recorded in:"
312echo "        $output1"
313echo "        $output2"
314echo "        $output3"
315
316# Sleep a little longer than duration to let ltp-pan "try" to gracefully end itself.
317sleep $(($duration + 10))
318
319if [ $Sar -eq 1 ]; then
320  killall -9 sadc >/dev/null 2>&1
321fi
322if [ $Top -eq 1 ]; then
323  kill $SCREEN_PID >/dev/null 2>&1
324fi
325killall -9 ltp-pan >/dev/null 2>&1
326killall -9 genload >/dev/null 2>&1
327if [ $NO_NETWORK -eq 0 ];then
328  killall -9 netpipe.sh >/dev/null 2>&1
329  killall -9 NPtcp >/dev/null 2>&1
330fi
331if [ $Iostat -eq 1 ];then
332  kill -9 $Iostat_PID >/dev/null 2>&1
333fi
334rm -rf ${TMP}
335echo "Testing done"
336if [ $LOGGING -eq 1 ];then
337  if [ ! -z $PRETTY_PRT ]; then
338    grep FAIL $logfile > /dev/null 2>&1
339  else
340    grep 'stat=' $logfile | grep -v 'stat=0' > /dev/null 2>&1
341  fi
342
343  if [ $? -eq 1 ]; then
344    echo "All Tests PASSED!"
345  else
346    echo "Testing yielded failures. See logfile: $logfile"
347    if [ $NO_NETWORK -eq 1 ];then
348      echo "The NFS related tests should fail because network stress was disabled"
349    fi
350  fi
351fi
352
353
354