GCC 7.1 fixes
[zfs.git] / scripts / zloop.sh
blob3a6db40cf0faea3507356b44fcd021418cc51bca
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 by Delphix. All rights reserved.
20 # Copyright (C) 2016 Lawrence Livermore National Security, LLC.
23 basedir=$(dirname "$0")
25 SCRIPT_COMMON=common.sh
26 if [ -f "${basedir}/${SCRIPT_COMMON}" ]; then
27 . "${basedir}/${SCRIPT_COMMON}"
28 else
29 echo "Missing helper script ${SCRIPT_COMMON}" && exit 1
32 # shellcheck disable=SC2034
33 PROG=zloop.sh
35 DEFAULTWORKDIR=/var/tmp
36 DEFAULTCOREDIR=/var/tmp/zloop
38 function usage
40 echo -e "\n$0 [-t <timeout>] [-c <dump directory>]" \
41 "[ -- [extra ztest parameters]]\n" \
42 "\n" \
43 " This script runs ztest repeatedly with randomized arguments.\n" \
44 " If a crash is encountered, the ztest logs, any associated\n" \
45 " vdev files, and core file (if one exists) are moved to the\n" \
46 " output directory ($DEFAULTCOREDIR by default). Any options\n" \
47 " after the -- end-of-options marker will be passed to ztest.\n" \
48 "\n" \
49 " Options:\n" \
50 " -t Total time to loop for, in seconds. If not provided,\n" \
51 " zloop runs forever.\n" \
52 " -f Specify working directory for ztest vdev files.\n" \
53 " -c Specify a core dump directory to use.\n" \
54 " -h Print this help message.\n" \
55 "" >&2
58 function or_die
60 # shellcheck disable=SC2068
62 # shellcheck disable=SC2181
63 if [[ $? -ne 0 ]]; then
64 # shellcheck disable=SC2145
65 echo "Command failed: $@"
66 exit 1
70 # core file helpers
71 origcorepattern="$(cat /proc/sys/kernel/core_pattern)"
72 coreglob="$(egrep -o '^([^|%[:space:]]*)' /proc/sys/kernel/core_pattern)*"
74 if [[ $coreglob = "*" ]]; then
75 echo "Setting core file pattern..."
76 echo "core" > /proc/sys/kernel/core_pattern
77 coreglob="$(egrep -o '^([^|%[:space:]]*)' \
78 /proc/sys/kernel/core_pattern)*"
81 function core_file
83 # shellcheck disable=SC2012 disable=2086
84 printf "%s" "$(ls -tr1 $coreglob 2> /dev/null | head -1)"
87 function core_prog
89 prog=$ZTEST
90 core_id=$($GDB --batch -c "$1" | grep "Core was generated by" | \
91 tr \' ' ')
92 # shellcheck disable=SC2076
93 if [[ "$core_id" =~ "zdb " ]]; then
94 prog=$ZDB
96 printf "%s" "$prog"
99 function store_core
101 core="$(core_file)"
102 if [[ $ztrc -ne 0 ]] || [[ -f "$core" ]]; then
103 coreid=$(date "+zloop-%y%m%d-%H%M%S")
104 foundcrashes=$((foundcrashes + 1))
106 dest=$coredir/$coreid
107 or_die mkdir -p "$dest"
108 or_die mkdir -p "$dest/vdev"
110 echo "*** ztest crash found - moving logs to $dest"
112 or_die mv ztest.history "$dest/"
113 or_die mv ztest.ddt "$dest/"
114 or_die mv ztest.out "$dest/"
115 or_die mv "$workdir/ztest*" "$dest/vdev/"
116 or_die mv "$workdir/zpool.cache" "$dest/vdev/"
118 # check for core
119 if [[ -f "$core" ]]; then
120 coreprog=$(core_prog "$core")
121 corestatus=$($GDB --batch --quiet \
122 -ex "set print thread-events off" \
123 -ex "printf \"*\n* Backtrace \n*\n\"" \
124 -ex "bt" \
125 -ex "printf \"*\n* Libraries \n*\n\"" \
126 -ex "info sharedlib" \
127 -ex "printf \"*\n* Threads (full) \n*\n\"" \
128 -ex "info threads" \
129 -ex "printf \"*\n* Backtraces \n*\n\"" \
130 -ex "thread apply all bt" \
131 -ex "printf \"*\n* Backtraces (full) \n*\n\"" \
132 -ex "thread apply all bt full" \
133 -ex "quit" "$coreprog" "$core" | grep -v "New LWP")
135 # Dump core + logs to stored directory
136 echo "$corestatus" >>"$dest/status"
137 or_die mv "$core" "$dest/"
139 # Record info in cores logfile
140 echo "*** core @ $coredir/$coreid/$core:" | \
141 tee -a ztest.cores
142 echo "$corestatus" | tee -a ztest.cores
143 echo "" | tee -a ztest.cores
145 echo "continuing..."
149 # parse arguments
150 # expected format: zloop [-t timeout] [-c coredir] [-- extra ztest args]
151 coredir=$DEFAULTCOREDIR
152 workdir=$DEFAULTWORKDIR
153 timeout=0
154 while getopts ":ht:c:f:" opt; do
155 case $opt in
156 t ) [[ $OPTARG -gt 0 ]] && timeout=$OPTARG ;;
157 c ) [[ $OPTARG ]] && coredir=$OPTARG ;;
158 f ) [[ $OPTARG ]] && workdir=$(readlink -f "$OPTARG") ;;
159 h ) usage
160 exit 2
162 * ) echo "Invalid argument: -$OPTARG";
163 usage
164 exit 1
165 esac
166 done
167 # pass remaining arguments on to ztest
168 shift $((OPTIND - 1))
170 # enable core dumps
171 ulimit -c unlimited
173 if [[ -f "$(core_file)" ]]; then
174 echo -n "There's a core dump here you might want to look at first... "
175 core_file
176 exit 1
179 if [[ ! -d $coredir ]]; then
180 echo "core dump directory ($coredir) does not exist, creating it."
181 or_die mkdir -p "$coredir"
184 if [[ ! -w $coredir ]]; then
185 echo "core dump directory ($coredir) is not writable."
186 exit 1
189 or_die rm -f ztest.history
190 or_die rm -f ztest.ddt
191 or_die rm -f ztest.cores
193 ztrc=0 # ztest return value
194 foundcrashes=0 # number of crashes found so far
195 starttime=$(date +%s)
196 curtime=$starttime
198 # if no timeout was specified, loop forever.
199 while [[ $timeout -eq 0 ]] || [[ $curtime -le $((starttime + timeout)) ]]; do
200 zopt="-VVVVV"
202 # switch between common arrangements & fully randomized
203 if [[ $((RANDOM % 2)) -eq 0 ]]; then
204 mirrors=2
205 raidz=0
206 parity=1
207 vdevs=2
208 else
209 mirrors=$(((RANDOM % 3) * 1))
210 parity=$(((RANDOM % 3) + 1))
211 raidz=$((((RANDOM % 9) + parity + 1) * (RANDOM % 2)))
212 vdevs=$(((RANDOM % 3) + 3))
214 align=$(((RANDOM % 2) * 3 + 9))
215 runtime=$((RANDOM % 100))
216 passtime=$((RANDOM % (runtime / 3 + 1) + 10))
217 size=128m
219 zopt="$zopt -m $mirrors"
220 zopt="$zopt -r $raidz"
221 zopt="$zopt -R $parity"
222 zopt="$zopt -v $vdevs"
223 zopt="$zopt -a $align"
224 zopt="$zopt -T $runtime"
225 zopt="$zopt -P $passtime"
226 zopt="$zopt -s $size"
227 zopt="$zopt -f $workdir"
229 # shellcheck disable=SC2124
230 cmd="$ZTEST $zopt $@"
231 desc="$(date '+%m/%d %T') $cmd"
232 echo "$desc" | tee -a ztest.history
233 echo "$desc" >>ztest.out
234 $cmd >>ztest.out 2>&1
235 ztrc=$?
236 egrep '===|WARNING' ztest.out >>ztest.history
237 $ZDB -U "$workdir/zpool.cache" -DD ztest >>ztest.ddt
239 store_core
241 curtime=$(date +%s)
242 done
244 echo "zloop finished, $foundcrashes crashes found"
246 #restore core pattern
247 echo "$origcorepattern" > /proc/sys/kernel/core_pattern
249 uptime >>ztest.out
251 if [[ $foundcrashes -gt 0 ]]; then
252 exit 1