Allow disabling of unmapped I/O on FreeBSD
[zfs.git] / scripts / zloop.sh
blob4a572ebab1fcd1b3f93521cdbdd75eea248ea8a3
1 #!/usr/bin/env 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.
21 # Copyright (c) 2017, Intel Corporation.
24 BASE_DIR=$(dirname "$0")
25 SCRIPT_COMMON=common.sh
26 if [ -f "${BASE_DIR}/${SCRIPT_COMMON}" ]; then
27 . "${BASE_DIR}/${SCRIPT_COMMON}"
28 else
29 echo "Missing helper script ${SCRIPT_COMMON}" && exit 1
32 # shellcheck disable=SC2034
33 PROG=zloop.sh
34 GDB=${GDB:-gdb}
36 DEFAULTWORKDIR=/var/tmp
37 DEFAULTCOREDIR=/var/tmp/zloop
39 function usage
41 cat >&2 <<EOF
43 $0 [-hl] [-c <dump directory>] [-f <vdev directory>]
44 [-m <max core dumps>] [-s <vdev size>] [-t <timeout>]
45 [-I <max iterations>] [-- [extra ztest parameters]]
47 This script runs ztest repeatedly with randomized arguments.
48 If a crash is encountered, the ztest logs, any associated
49 vdev files, and core file (if one exists) are moved to the
50 output directory ($DEFAULTCOREDIR by default). Any options
51 after the -- end-of-options marker will be passed to ztest.
53 Options:
54 -c Specify a core dump directory to use.
55 -f Specify working directory for ztest vdev files.
56 -h Print this help message.
57 -l Create 'ztest.core.N' symlink to core directory.
58 -m Max number of core dumps to allow before exiting.
59 -s Size of vdev devices.
60 -t Total time to loop for, in seconds. If not provided,
61 zloop runs forever.
62 -I Max number of iterations to loop before exiting.
64 EOF
67 function or_die
69 # shellcheck disable=SC2068
70 if ! $@; then
71 echo "Command failed: $*"
72 exit 1
76 case $(uname) in
77 FreeBSD)
78 coreglob="z*.core"
80 Linux)
81 # core file helpers
82 origcorepattern="$(cat /proc/sys/kernel/core_pattern)"
83 coreglob="$(grep -E -o '^([^|%[:space:]]*)' /proc/sys/kernel/core_pattern)*"
85 if [[ $coreglob = "*" ]]; then
86 echo "Setting core file pattern..."
87 echo "core" > /proc/sys/kernel/core_pattern
88 coreglob="$(grep -E -o '^([^|%[:space:]]*)' \
89 /proc/sys/kernel/core_pattern)*"
93 exit 1
95 esac
97 function core_file
99 # shellcheck disable=SC2012,SC2086
100 ls -tr1 $coreglob 2>/dev/null | head -1
103 function core_prog
105 prog=$ZTEST
106 core_id=$($GDB --batch -c "$1" | grep "Core was generated by" | \
107 tr \' ' ')
108 if [[ "$core_id" == *"zdb "* ]]; then
109 prog=$ZDB
111 printf "%s" "$prog"
114 function store_core
116 core="$(core_file)"
117 if [[ $ztrc -ne 0 ]] || [[ -f "$core" ]]; then
118 df -h "$workdir" >>ztest.out
119 coreid=$(date "+zloop-%y%m%d-%H%M%S")
120 foundcrashes=$((foundcrashes + 1))
122 # zdb debugging
123 zdbcmd="$ZDB -U "$workdir/zpool.cache" -dddMmDDG ztest"
124 zdbdebug=$($zdbcmd 2>&1)
125 echo -e "$zdbcmd\n" >>ztest.zdb
126 echo "$zdbdebug" >>ztest.zdb
128 dest=$coredir/$coreid
129 or_die mkdir -p "$dest"
130 or_die mkdir -p "$dest/vdev"
132 if [[ $symlink -ne 0 ]]; then
133 or_die ln -sf "$dest" ztest.core.$foundcrashes
136 echo "*** ztest crash found - moving logs to $dest"
138 or_die mv ztest.history "$dest/"
139 or_die mv ztest.zdb "$dest/"
140 or_die mv ztest.out "$dest/"
141 or_die mv "$workdir/ztest*" "$dest/vdev/"
143 if [[ -e "$workdir/zpool.cache" ]]; then
144 or_die mv "$workdir/zpool.cache" "$dest/vdev/"
147 # check for core
148 if [[ -f "$core" ]]; then
149 coreprog=$(core_prog "$core")
150 coredebug=$($GDB --batch --quiet \
151 -ex "set print thread-events off" \
152 -ex "printf \"*\n* Backtrace \n*\n\"" \
153 -ex "bt" \
154 -ex "printf \"*\n* Libraries \n*\n\"" \
155 -ex "info sharedlib" \
156 -ex "printf \"*\n* Threads (full) \n*\n\"" \
157 -ex "info threads" \
158 -ex "printf \"*\n* Backtraces \n*\n\"" \
159 -ex "thread apply all bt" \
160 -ex "printf \"*\n* Backtraces (full) \n*\n\"" \
161 -ex "thread apply all bt full" \
162 -ex "quit" "$coreprog" "$core" 2>&1 | \
163 grep -v "New LWP")
165 # Dump core + logs to stored directory
166 echo "$coredebug" >>"$dest/ztest.gdb"
167 or_die mv "$core" "$dest/"
169 # Record info in cores logfile
170 echo "*** core @ $coredir/$coreid/$core:" | \
171 tee -a ztest.cores
174 if [[ $coremax -gt 0 ]] &&
175 [[ $foundcrashes -ge $coremax ]]; then
176 echo "exiting... max $coremax allowed cores"
177 exit 1
178 else
179 echo "continuing..."
184 # parse arguments
185 # expected format: zloop [-t timeout] [-c coredir] [-- extra ztest args]
186 coredir=$DEFAULTCOREDIR
187 basedir=$DEFAULTWORKDIR
188 rundir="zloop-run"
189 timeout=0
190 size="512m"
191 coremax=0
192 symlink=0
193 iterations=0
194 while getopts ":ht:m:I:s:c:f:l" opt; do
195 case $opt in
196 t ) [[ $OPTARG -gt 0 ]] && timeout=$OPTARG ;;
197 m ) [[ $OPTARG -gt 0 ]] && coremax=$OPTARG ;;
198 I ) [[ $OPTARG ]] && iterations=$OPTARG ;;
199 s ) [[ $OPTARG ]] && size=$OPTARG ;;
200 c ) [[ $OPTARG ]] && coredir=$OPTARG ;;
201 f ) [[ $OPTARG ]] && basedir=$(readlink -f "$OPTARG") ;;
202 l ) symlink=1 ;;
203 h ) usage
204 exit 2
206 * ) echo "Invalid argument: -$OPTARG";
207 usage
208 exit 1
209 esac
210 done
211 # pass remaining arguments on to ztest
212 shift $((OPTIND - 1))
214 # enable core dumps
215 ulimit -c unlimited
216 export ASAN_OPTIONS=abort_on_error=1:disable_coredump=0
218 if [[ -f "$(core_file)" ]]; then
219 echo -n "There's a core dump here you might want to look at first... "
220 core_file
221 echo
222 exit 1
225 if [[ ! -d $coredir ]]; then
226 echo "core dump directory ($coredir) does not exist, creating it."
227 or_die mkdir -p "$coredir"
230 if [[ ! -w $coredir ]]; then
231 echo "core dump directory ($coredir) is not writable."
232 exit 1
235 or_die rm -f ztest.history
236 or_die rm -f ztest.zdb
237 or_die rm -f ztest.cores
239 ztrc=0 # ztest return value
240 foundcrashes=0 # number of crashes found so far
241 starttime=$(date +%s)
242 curtime=$starttime
243 iteration=0
245 # if no timeout was specified, loop forever.
246 while (( timeout == 0 )) || (( curtime <= (starttime + timeout) )); do
247 if (( iterations > 0 )) && (( iteration++ == iterations )); then
248 break
251 zopt="-G -VVVVV"
253 # start each run with an empty directory
254 workdir="$basedir/$rundir"
255 or_die rm -rf "$workdir"
256 or_die mkdir "$workdir"
258 # switch between three types of configs
259 # 1/3 basic, 1/3 raidz mix, and 1/3 draid mix
260 choice=$((RANDOM % 3))
262 # ashift range 9 - 15
263 align=$(((RANDOM % 2) * 3 + 9))
265 # randomly use special classes
266 class="special=random"
268 if [[ $choice -eq 0 ]]; then
269 # basic mirror only
270 parity=1
271 mirrors=2
272 draid_data=0
273 draid_spares=0
274 raid_children=0
275 vdevs=2
276 raid_type="raidz"
277 elif [[ $choice -eq 1 ]]; then
278 # fully randomized mirror/raidz (sans dRAID)
279 parity=$(((RANDOM % 3) + 1))
280 mirrors=$(((RANDOM % 3) * 1))
281 draid_data=0
282 draid_spares=0
283 raid_children=$((((RANDOM % 9) + parity + 1) * (RANDOM % 2)))
284 vdevs=$(((RANDOM % 3) + 3))
285 raid_type="raidz"
286 else
287 # fully randomized dRAID (sans mirror/raidz)
288 parity=$(((RANDOM % 3) + 1))
289 mirrors=0
290 draid_data=$(((RANDOM % 8) + 3))
291 draid_spares=$(((RANDOM % 2) + parity))
292 stripe=$((draid_data + parity))
293 extra=$((draid_spares + (RANDOM % 4)))
294 raid_children=$(((((RANDOM % 4) + 1) * stripe) + extra))
295 vdevs=$((RANDOM % 3))
296 raid_type="draid"
299 zopt="$zopt -K $raid_type"
300 zopt="$zopt -m $mirrors"
301 zopt="$zopt -r $raid_children"
302 zopt="$zopt -D $draid_data"
303 zopt="$zopt -S $draid_spares"
304 zopt="$zopt -R $parity"
305 zopt="$zopt -v $vdevs"
306 zopt="$zopt -a $align"
307 zopt="$zopt -C $class"
308 zopt="$zopt -s $size"
309 zopt="$zopt -f $workdir"
311 cmd="$ZTEST $zopt $*"
312 desc="$(date '+%m/%d %T') $cmd"
313 echo "$desc" | tee -a ztest.history
314 echo "$desc" >>ztest.out
315 $cmd >>ztest.out 2>&1
316 ztrc=$?
317 grep -E '===|WARNING' ztest.out >>ztest.history
319 store_core
321 curtime=$(date +%s)
322 done
324 echo "zloop finished, $foundcrashes crashes found"
326 # restore core pattern.
327 case $(uname) in
328 Linux)
329 echo "$origcorepattern" > /proc/sys/kernel/core_pattern
333 esac
335 uptime >>ztest.out
337 if [[ $foundcrashes -gt 0 ]]; then
338 exit 1