]>
Commit | Line | Data |
---|---|---|
54007c79 | 1 | #!/usr/bin/env bash |
541a0901 BB |
2 | |
3 | # | |
4 | # CDDL HEADER START | |
5 | # | |
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. | |
10 | # | |
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. | |
14 | # | |
15 | # CDDL HEADER END | |
16 | # | |
17 | ||
18 | # | |
19 | # Copyright (c) 2015 by Delphix. All rights reserved. | |
20 | # Copyright (C) 2016 Lawrence Livermore National Security, LLC. | |
b2255edc | 21 | # Copyright (c) 2017, Intel Corporation. |
541a0901 BB |
22 | # |
23 | ||
c8f9061f | 24 | BASE_DIR=$(dirname "$0") |
541a0901 | 25 | SCRIPT_COMMON=common.sh |
c8f9061f BB |
26 | if [ -f "${BASE_DIR}/${SCRIPT_COMMON}" ]; then |
27 | . "${BASE_DIR}/${SCRIPT_COMMON}" | |
541a0901 BB |
28 | else |
29 | echo "Missing helper script ${SCRIPT_COMMON}" && exit 1 | |
30 | fi | |
31 | ||
c552fbc5 | 32 | # shellcheck disable=SC2034 |
541a0901 | 33 | PROG=zloop.sh |
fed90353 | 34 | GDB=${GDB:-gdb} |
541a0901 BB |
35 | |
36 | DEFAULTWORKDIR=/var/tmp | |
37 | DEFAULTCOREDIR=/var/tmp/zloop | |
38 | ||
39 | function usage | |
40 | { | |
aea899a6 | 41 | echo -e "\n$0 [-t <timeout>] [ -s <vdev size> ] [-c <dump directory>]" \ |
541a0901 BB |
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" \ | |
aea899a6 | 53 | " -s Size of vdev devices.\n" \ |
541a0901 BB |
54 | " -f Specify working directory for ztest vdev files.\n" \ |
55 | " -c Specify a core dump directory to use.\n" \ | |
cd0a89de BB |
56 | " -m Max number of core dumps to allow before exiting.\n" \ |
57 | " -l Create 'ztest.core.N' symlink to core directory.\n" \ | |
541a0901 BB |
58 | " -h Print this help message.\n" \ |
59 | "" >&2 | |
60 | } | |
61 | ||
62 | function or_die | |
63 | { | |
c552fbc5 | 64 | # shellcheck disable=SC2068 |
541a0901 | 65 | $@ |
c552fbc5 | 66 | # shellcheck disable=SC2181 |
541a0901 | 67 | if [[ $? -ne 0 ]]; then |
c552fbc5 | 68 | # shellcheck disable=SC2145 |
541a0901 BB |
69 | echo "Command failed: $@" |
70 | exit 1 | |
71 | fi | |
72 | } | |
73 | ||
54007c79 RM |
74 | case $(uname) in |
75 | FreeBSD) | |
76 | coreglob="z*.core" | |
77 | ;; | |
78 | Linux) | |
79 | # core file helpers | |
80 | origcorepattern="$(cat /proc/sys/kernel/core_pattern)" | |
81 | coreglob="$(grep -E -o '^([^|%[:space:]]*)' /proc/sys/kernel/core_pattern)*" | |
82 | ||
83 | if [[ $coreglob = "*" ]]; then | |
84 | echo "Setting core file pattern..." | |
85 | echo "core" > /proc/sys/kernel/core_pattern | |
86 | coreglob="$(grep -E -o '^([^|%[:space:]]*)' \ | |
87 | /proc/sys/kernel/core_pattern)*" | |
88 | fi | |
89 | ;; | |
90 | *) | |
91 | exit 1 | |
92 | ;; | |
93 | esac | |
20da0566 GN |
94 | |
95 | function core_file | |
96 | { | |
c552fbc5 | 97 | # shellcheck disable=SC2012 disable=2086 |
20da0566 GN |
98 | printf "%s" "$(ls -tr1 $coreglob 2> /dev/null | head -1)" |
99 | } | |
100 | ||
0c313d2f GN |
101 | function core_prog |
102 | { | |
103 | prog=$ZTEST | |
c552fbc5 | 104 | core_id=$($GDB --batch -c "$1" | grep "Core was generated by" | \ |
0c313d2f | 105 | tr \' ' ') |
c552fbc5 | 106 | # shellcheck disable=SC2076 |
0c313d2f GN |
107 | if [[ "$core_id" =~ "zdb " ]]; then |
108 | prog=$ZDB | |
109 | fi | |
110 | printf "%s" "$prog" | |
111 | } | |
112 | ||
541a0901 BB |
113 | function store_core |
114 | { | |
20da0566 GN |
115 | core="$(core_file)" |
116 | if [[ $ztrc -ne 0 ]] || [[ -f "$core" ]]; then | |
5df5d06a | 117 | df -h "$workdir" >>ztest.out |
541a0901 | 118 | coreid=$(date "+zloop-%y%m%d-%H%M%S") |
c552fbc5 | 119 | foundcrashes=$((foundcrashes + 1)) |
541a0901 | 120 | |
cd0a89de | 121 | # zdb debugging |
61871518 | 122 | zdbcmd="$ZDB -U "$workdir/zpool.cache" -dddMmDDG ztest" |
cd0a89de BB |
123 | zdbdebug=$($zdbcmd 2>&1) |
124 | echo -e "$zdbcmd\n" >>ztest.zdb | |
125 | echo "$zdbdebug" >>ztest.zdb | |
126 | ||
541a0901 | 127 | dest=$coredir/$coreid |
c552fbc5 GDN |
128 | or_die mkdir -p "$dest" |
129 | or_die mkdir -p "$dest/vdev" | |
541a0901 | 130 | |
cd0a89de BB |
131 | if [[ $symlink -ne 0 ]]; then |
132 | or_die ln -sf "$dest" ztest.core.$foundcrashes | |
133 | fi | |
134 | ||
541a0901 BB |
135 | echo "*** ztest crash found - moving logs to $dest" |
136 | ||
c552fbc5 | 137 | or_die mv ztest.history "$dest/" |
cd0a89de | 138 | or_die mv ztest.zdb "$dest/" |
c552fbc5 GDN |
139 | or_die mv ztest.out "$dest/" |
140 | or_die mv "$workdir/ztest*" "$dest/vdev/" | |
8fb1ede1 BB |
141 | |
142 | if [[ -e "$workdir/zpool.cache" ]]; then | |
143 | or_die mv "$workdir/zpool.cache" "$dest/vdev/" | |
144 | fi | |
541a0901 BB |
145 | |
146 | # check for core | |
20da0566 | 147 | if [[ -f "$core" ]]; then |
c552fbc5 | 148 | coreprog=$(core_prog "$core") |
cd0a89de | 149 | coredebug=$($GDB --batch --quiet \ |
541a0901 BB |
150 | -ex "set print thread-events off" \ |
151 | -ex "printf \"*\n* Backtrace \n*\n\"" \ | |
152 | -ex "bt" \ | |
153 | -ex "printf \"*\n* Libraries \n*\n\"" \ | |
154 | -ex "info sharedlib" \ | |
155 | -ex "printf \"*\n* Threads (full) \n*\n\"" \ | |
156 | -ex "info threads" \ | |
157 | -ex "printf \"*\n* Backtraces \n*\n\"" \ | |
158 | -ex "thread apply all bt" \ | |
159 | -ex "printf \"*\n* Backtraces (full) \n*\n\"" \ | |
160 | -ex "thread apply all bt full" \ | |
cd0a89de BB |
161 | -ex "quit" "$coreprog" "$core" 2>&1 | \ |
162 | grep -v "New LWP") | |
541a0901 BB |
163 | |
164 | # Dump core + logs to stored directory | |
cd0a89de | 165 | echo "$coredebug" >>"$dest/ztest.gdb" |
c552fbc5 | 166 | or_die mv "$core" "$dest/" |
541a0901 BB |
167 | |
168 | # Record info in cores logfile | |
20da0566 | 169 | echo "*** core @ $coredir/$coreid/$core:" | \ |
541a0901 | 170 | tee -a ztest.cores |
541a0901 | 171 | fi |
cd0a89de BB |
172 | |
173 | if [[ $coremax -gt 0 ]] && | |
174 | [[ $foundcrashes -ge $coremax ]]; then | |
175 | echo "exiting... max $coremax allowed cores" | |
176 | exit 1 | |
177 | else | |
178 | echo "continuing..." | |
179 | fi | |
541a0901 BB |
180 | fi |
181 | } | |
182 | ||
183 | # parse arguments | |
184 | # expected format: zloop [-t timeout] [-c coredir] [-- extra ztest args] | |
185 | coredir=$DEFAULTCOREDIR | |
5df5d06a DB |
186 | basedir=$DEFAULTWORKDIR |
187 | rundir="zloop-run" | |
541a0901 | 188 | timeout=0 |
aea899a6 | 189 | size="512m" |
cd0a89de BB |
190 | coremax=0 |
191 | symlink=0 | |
192 | while getopts ":ht:m:s:c:f:l" opt; do | |
541a0901 BB |
193 | case $opt in |
194 | t ) [[ $OPTARG -gt 0 ]] && timeout=$OPTARG ;; | |
cd0a89de | 195 | m ) [[ $OPTARG -gt 0 ]] && coremax=$OPTARG ;; |
aea899a6 | 196 | s ) [[ $OPTARG ]] && size=$OPTARG ;; |
541a0901 | 197 | c ) [[ $OPTARG ]] && coredir=$OPTARG ;; |
5df5d06a | 198 | f ) [[ $OPTARG ]] && basedir=$(readlink -f "$OPTARG") ;; |
cd0a89de | 199 | l ) symlink=1 ;; |
541a0901 BB |
200 | h ) usage |
201 | exit 2 | |
202 | ;; | |
203 | * ) echo "Invalid argument: -$OPTARG"; | |
204 | usage | |
205 | exit 1 | |
206 | esac | |
207 | done | |
208 | # pass remaining arguments on to ztest | |
209 | shift $((OPTIND - 1)) | |
210 | ||
211 | # enable core dumps | |
212 | ulimit -c unlimited | |
fed90353 | 213 | export ASAN_OPTIONS=abort_on_error=1:disable_coredump=0 |
541a0901 | 214 | |
20da0566 GN |
215 | if [[ -f "$(core_file)" ]]; then |
216 | echo -n "There's a core dump here you might want to look at first... " | |
c552fbc5 | 217 | core_file |
cd0a89de | 218 | echo |
541a0901 BB |
219 | exit 1 |
220 | fi | |
221 | ||
222 | if [[ ! -d $coredir ]]; then | |
223 | echo "core dump directory ($coredir) does not exist, creating it." | |
c552fbc5 | 224 | or_die mkdir -p "$coredir" |
541a0901 BB |
225 | fi |
226 | ||
227 | if [[ ! -w $coredir ]]; then | |
228 | echo "core dump directory ($coredir) is not writable." | |
229 | exit 1 | |
230 | fi | |
231 | ||
232 | or_die rm -f ztest.history | |
cd0a89de | 233 | or_die rm -f ztest.zdb |
541a0901 BB |
234 | or_die rm -f ztest.cores |
235 | ||
236 | ztrc=0 # ztest return value | |
237 | foundcrashes=0 # number of crashes found so far | |
238 | starttime=$(date +%s) | |
239 | curtime=$starttime | |
240 | ||
241 | # if no timeout was specified, loop forever. | |
c552fbc5 | 242 | while [[ $timeout -eq 0 ]] || [[ $curtime -le $((starttime + timeout)) ]]; do |
8fb1ede1 | 243 | zopt="-G -VVVVV" |
541a0901 | 244 | |
5df5d06a DB |
245 | # start each run with an empty directory |
246 | workdir="$basedir/$rundir" | |
247 | or_die rm -rf "$workdir" | |
248 | or_die mkdir "$workdir" | |
249 | ||
b2255edc BB |
250 | # switch between three types of configs |
251 | # 1/3 basic, 1/3 raidz mix, and 1/3 draid mix | |
252 | choice=$((RANDOM % 3)) | |
253 | ||
254 | # ashift range 9 - 15 | |
255 | align=$(((RANDOM % 2) * 3 + 9)) | |
256 | ||
257 | # randomly use special classes | |
258 | class="special=random" | |
259 | ||
260 | if [[ $choice -eq 0 ]]; then | |
261 | # basic mirror only | |
541a0901 | 262 | parity=1 |
b2255edc BB |
263 | mirrors=2 |
264 | draid_data=0 | |
265 | draid_spares=0 | |
266 | raid_children=0 | |
541a0901 | 267 | vdevs=2 |
b2255edc BB |
268 | raid_type="raidz" |
269 | elif [[ $choice -eq 1 ]]; then | |
270 | # fully randomized mirror/raidz (sans dRAID) | |
541a0901 | 271 | parity=$(((RANDOM % 3) + 1)) |
b2255edc BB |
272 | mirrors=$(((RANDOM % 3) * 1)) |
273 | draid_data=0 | |
274 | draid_spares=0 | |
275 | raid_children=$((((RANDOM % 9) + parity + 1) * (RANDOM % 2))) | |
541a0901 | 276 | vdevs=$(((RANDOM % 3) + 3)) |
b2255edc BB |
277 | raid_type="raidz" |
278 | else | |
279 | # fully randomized dRAID (sans mirror/raidz) | |
280 | parity=$(((RANDOM % 3) + 1)) | |
281 | mirrors=0 | |
282 | draid_data=$(((RANDOM % 8) + 3)) | |
283 | draid_spares=$(((RANDOM % 2) + parity)) | |
284 | stripe=$((draid_data + parity)) | |
285 | extra=$((draid_spares + (RANDOM % 4))) | |
286 | raid_children=$(((((RANDOM % 4) + 1) * stripe) + extra)) | |
287 | vdevs=$((RANDOM % 3)) | |
288 | raid_type="draid" | |
541a0901 | 289 | fi |
b2255edc BB |
290 | |
291 | # run from 30 to 120 seconds | |
292 | runtime=$(((RANDOM % 90) + 30)) | |
541a0901 | 293 | passtime=$((RANDOM % (runtime / 3 + 1) + 10)) |
541a0901 | 294 | |
b2255edc | 295 | zopt="$zopt -K $raid_type" |
541a0901 | 296 | zopt="$zopt -m $mirrors" |
b2255edc BB |
297 | zopt="$zopt -r $raid_children" |
298 | zopt="$zopt -D $draid_data" | |
299 | zopt="$zopt -S $draid_spares" | |
541a0901 BB |
300 | zopt="$zopt -R $parity" |
301 | zopt="$zopt -v $vdevs" | |
302 | zopt="$zopt -a $align" | |
b2255edc | 303 | zopt="$zopt -C $class" |
541a0901 BB |
304 | zopt="$zopt -T $runtime" |
305 | zopt="$zopt -P $passtime" | |
306 | zopt="$zopt -s $size" | |
307 | zopt="$zopt -f $workdir" | |
308 | ||
c552fbc5 | 309 | # shellcheck disable=SC2124 |
541a0901 BB |
310 | cmd="$ZTEST $zopt $@" |
311 | desc="$(date '+%m/%d %T') $cmd" | |
312 | echo "$desc" | tee -a ztest.history | |
313 | echo "$desc" >>ztest.out | |
314 | $cmd >>ztest.out 2>&1 | |
315 | ztrc=$? | |
3da3488e | 316 | grep -E '===|WARNING' ztest.out >>ztest.history |
541a0901 BB |
317 | |
318 | store_core | |
319 | ||
320 | curtime=$(date +%s) | |
321 | done | |
322 | ||
323 | echo "zloop finished, $foundcrashes crashes found" | |
324 | ||
54007c79 RM |
325 | # restore core pattern. |
326 | case $(uname) in | |
327 | Linux) | |
328 | echo "$origcorepattern" > /proc/sys/kernel/core_pattern | |
329 | ;; | |
330 | *) | |
331 | ;; | |
332 | esac | |
20da0566 | 333 | |
541a0901 BB |
334 | uptime >>ztest.out |
335 | ||
336 | if [[ $foundcrashes -gt 0 ]]; then | |
337 | exit 1 | |
338 | fi |