8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / ztest / zloop.bash
blob7203356d3dc136c01c9fe38b6296ede09a2e19a3
1 #!/bin/bash
4 # CDDL HEADER START
6 # This file and its contents are supplied under the terms of the
7 # Common Development and Distribution License ("CDDL"), version 1.0.
8 # You may only use this file in accordance with the terms of version
9 # 1.0 of the CDDL.
11 # A full copy of the text of the CDDL should have accompanied this
12 # source. A copy of the CDDL is also available via the Internet at
13 # http://www.illumos.org/license/CDDL.
15 # CDDL HEADER END
19 # Copyright (c) 2015, 2016 by Delphix. All rights reserved.
22 set -x
23 export BITS=64
24 export UMEM_DEBUG=default,verbose
25 export UMEM_LOGGING=transaction,contents
26 set +x
28 sparc_32=sparc
29 sparc_64=sparcv9
30 i386_32=i86
31 i386_64=amd64
32 ARCH=`uname -p`
33 eval 'ARCHBITS=${'"${ARCH}_${BITS}"'}'
34 BIN=$ROOT/usr/bin/${ARCHBITS}
35 SBIN=$ROOT/usr/sbin/${ARCHBITS}
36 DEFAULTWORKDIR=/var/tmp
37 DEFAULTCOREDIR=/var/tmp/zloop
39 function usage
41 echo -e "\n$0 [-t <timeout>] [-c <dump directory>]" \
42 "[ -- [extra ztest parameters]]\n" \
43 "\n" \
44 " This script runs ztest repeatedly with randomized arguments.\n" \
45 " If a crash is encountered, the ztest logs, any associated\n" \
46 " vdev files, and core file (if one exists) are moved to the\n" \
47 " output directory ($DEFAULTCOREDIR by default). Any options\n" \
48 " after the -- end-of-options marker will be passed to ztest.\n" \
49 "\n" \
50 " Options:\n" \
51 " -t Total time to loop for, in seconds. If not provided,\n" \
52 " zloop runs forever.\n" \
53 " -f Specify working directory for ztest vdev files.\n" \
54 " -c Specify a core dump directory to use.\n" \
55 " -h Print this help message.\n" \
56 "" >&2
59 function or_die
62 if [[ $? -ne 0 ]]; then
63 echo "Command failed: $@"
64 exit 1
68 function store_core
70 if [[ $ztrc -ne 0 ]] || [[ -f core ]]; then
71 coreid=$(/bin/date "+zloop-%y%m%d-%H%M%S")
72 foundcrashes=$(($foundcrashes + 1))
74 dest=$coredir/$coreid
75 or_die /bin/mkdir $dest
76 or_die /bin/mkdir $dest/vdev
78 echo "*** ztest crash found - moving logs to $coredir/$coreid"
80 or_die /bin/mv ztest.history $dest/
81 or_die /bin/mv ztest.out $dest/
82 or_die /bin/mv $workdir/ztest* $dest/vdev/
83 or_die /bin/mv $workdir/zpool.cache $dest/vdev/
85 # check for core
86 if [[ -f core ]]; then
87 corestatus=$(mdb -e "::status" core)
88 corestack=$(mdb -e "::stack" core)
90 # Dump core + logs to stored directory
91 echo "$corestatus" >>$dest/status
92 echo "$corestack" >>$dest/status
93 or_die /bin/mv core $dest/
95 # Record info in cores logfile
96 echo "*** core @ $coredir/$coreid/core:" | /bin/tee -a ztest.cores
97 echo "$corestatus" | /bin/tee -a ztest.cores
98 echo "$corestack" | /bin/tee -a ztest.cores
99 echo "" | /bin/tee -a ztest.cores
101 echo "continuing..."
105 set -x
106 export PATH=${BIN}:${SBIN}
107 export LD_LIBRARY_PATH=$ROOT/lib/$BITS:$ROOT/usr/lib/$BITS
108 set +x
110 # parse arguments
111 # expected format: zloop [-t timeout] [-c coredir] [-- extra ztest args]
112 coredir=$DEFAULTCOREDIR
113 workdir=$DEFAULTWORKDIR
114 timeout=0
115 while getopts ":ht:c:f:" opt; do
116 case $opt in
117 t ) [[ $OPTARG -gt 0 ]] && timeout=$OPTARG ;;
118 c ) [[ $OPTARG ]] && coredir=$OPTARG ;;
119 f ) [[ $OPTARG ]] && workdir=$(/usr/bin/readlink -f $OPTARG) ;;
120 h ) usage
121 exit 2
123 * ) echo "Invalid argument: -$OPTARG";
124 usage
125 exit 1
126 esac
127 done
128 # pass remaining arguments on to ztest
129 shift $((OPTIND - 1))
131 if [[ -f core ]]; then
132 echo "There's a core dump here you might want to look at first."
133 exit 1
136 if [[ ! -d $coredir ]]; then
137 echo "core dump directory ($coredir) does not exist, creating it."
138 or_die /bin/mkdir -p $coredir
141 if [[ ! -w $coredir ]]; then
142 echo "core dump directory ($coredir) is not writable."
143 exit 1
146 or_die /bin/rm -f ztest.history
147 or_die /bin/rm -f ztest.cores
149 ztrc=0 # ztest return value
150 foundcrashes=0 # number of crashes found so far
151 starttime=$(/bin/date +%s)
152 curtime=$starttime
154 # if no timeout was specified, loop forever.
155 while [[ $timeout -eq 0 ]] || [[ $curtime -le $(($starttime + $timeout)) ]]; do
156 zopt="-VVVVV"
158 # switch between common arrangements & fully randomized
159 if [[ $((RANDOM % 2)) -eq 0 ]]; then
160 mirrors=2
161 raidz=0
162 parity=1
163 vdevs=2
164 else
165 mirrors=$(((RANDOM % 3) * 1))
166 parity=$(((RANDOM % 3) + 1))
167 raidz=$((((RANDOM % 9) + parity + 1) * (RANDOM % 2)))
168 vdevs=$(((RANDOM % 3) + 3))
170 align=$(((RANDOM % 2) * 3 + 9))
171 runtime=$((RANDOM % 100))
172 passtime=$((RANDOM % (runtime / 3 + 1) + 10))
173 size=128m
175 zopt="$zopt -m $mirrors"
176 zopt="$zopt -r $raidz"
177 zopt="$zopt -R $parity"
178 zopt="$zopt -v $vdevs"
179 zopt="$zopt -a $align"
180 zopt="$zopt -T $runtime"
181 zopt="$zopt -P $passtime"
182 zopt="$zopt -s $size"
183 zopt="$zopt -f $workdir"
185 cmd="ztest $zopt $@"
186 desc="$(/bin/date '+%m/%d %T') $cmd"
187 echo "$desc" | /bin/tee -a ztest.history
188 echo "$desc" >>ztest.out
189 $BIN/$cmd >>ztest.out 2>&1
190 ztrc=$?
191 /bin/egrep '===|WARNING' ztest.out >>ztest.history
193 store_core
195 curtime=$(/bin/date +%s)
196 done
198 echo "zloop finished, $foundcrashes crashes found"
200 /bin/uptime >>ztest.out
202 if [[ $foundcrashes -gt 0 ]]; then
203 exit 1