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