xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/rcutorture/bin/kvm-test-1-run.sh (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#!/bin/bash
2*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0+
3*4882a593Smuzhiyun#
4*4882a593Smuzhiyun# Run a kvm-based test of the specified tree on the specified configs.
5*4882a593Smuzhiyun# Fully automated run and error checking, no graphics console.
6*4882a593Smuzhiyun#
7*4882a593Smuzhiyun# Execute this in the source tree.  Do not run it as a background task
8*4882a593Smuzhiyun# because qemu does not seem to like that much.
9*4882a593Smuzhiyun#
10*4882a593Smuzhiyun# Usage: kvm-test-1-run.sh config builddir resdir seconds qemu-args boot_args
11*4882a593Smuzhiyun#
12*4882a593Smuzhiyun# qemu-args defaults to "-enable-kvm -nographic", along with arguments
13*4882a593Smuzhiyun#			specifying the number of CPUs and other options
14*4882a593Smuzhiyun#			generated from the underlying CPU architecture.
15*4882a593Smuzhiyun# boot_args defaults to value returned by the per_version_boot_params
16*4882a593Smuzhiyun#			shell function.
17*4882a593Smuzhiyun#
18*4882a593Smuzhiyun# Anything you specify for either qemu-args or boot_args is appended to
19*4882a593Smuzhiyun# the default values.  The "-smp" value is deduced from the contents of
20*4882a593Smuzhiyun# the config fragment.
21*4882a593Smuzhiyun#
22*4882a593Smuzhiyun# More sophisticated argument parsing is clearly needed.
23*4882a593Smuzhiyun#
24*4882a593Smuzhiyun# Copyright (C) IBM Corporation, 2011
25*4882a593Smuzhiyun#
26*4882a593Smuzhiyun# Authors: Paul E. McKenney <paulmck@linux.ibm.com>
27*4882a593Smuzhiyun
28*4882a593SmuzhiyunT=${TMPDIR-/tmp}/kvm-test-1-run.sh.$$
29*4882a593Smuzhiyuntrap 'rm -rf $T' 0
30*4882a593Smuzhiyunmkdir $T
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun. functions.sh
33*4882a593Smuzhiyun. $CONFIGFRAG/ver_functions.sh
34*4882a593Smuzhiyun
35*4882a593Smuzhiyunconfig_template=${1}
36*4882a593Smuzhiyunconfig_dir=`echo $config_template | sed -e 's,/[^/]*$,,'`
37*4882a593Smuzhiyuntitle=`echo $config_template | sed -e 's/^.*\///'`
38*4882a593Smuzhiyunbuilddir=${2}
39*4882a593Smuzhiyunresdir=${3}
40*4882a593Smuzhiyunif test -z "$resdir" -o ! -d "$resdir" -o ! -w "$resdir"
41*4882a593Smuzhiyunthen
42*4882a593Smuzhiyun	echo "kvm-test-1-run.sh :$resdir: Not a writable directory, cannot store results into it"
43*4882a593Smuzhiyun	exit 1
44*4882a593Smuzhiyunfi
45*4882a593Smuzhiyunecho ' ---' `date`: Starting build
46*4882a593Smuzhiyunecho ' ---' Kconfig fragment at: $config_template >> $resdir/log
47*4882a593Smuzhiyuntouch $resdir/ConfigFragment.input
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun# Combine additional Kconfig options into an existing set such that
50*4882a593Smuzhiyun# newer options win.  The first argument is the Kconfig source ID, the
51*4882a593Smuzhiyun# second the to-be-updated file within $T, and the third and final the
52*4882a593Smuzhiyun# list of additional Kconfig options.  Note that a $2.tmp file is
53*4882a593Smuzhiyun# created when doing the update.
54*4882a593Smuzhiyunconfig_override_param () {
55*4882a593Smuzhiyun	if test -n "$3"
56*4882a593Smuzhiyun	then
57*4882a593Smuzhiyun		echo $3 | sed -e 's/^ *//' -e 's/ *$//' | tr -s " " "\012" > $T/Kconfig_args
58*4882a593Smuzhiyun		echo " --- $1" >> $resdir/ConfigFragment.input
59*4882a593Smuzhiyun		cat $T/Kconfig_args >> $resdir/ConfigFragment.input
60*4882a593Smuzhiyun		config_override.sh $T/$2 $T/Kconfig_args > $T/$2.tmp
61*4882a593Smuzhiyun		mv $T/$2.tmp $T/$2
62*4882a593Smuzhiyun		# Note that "#CHECK#" is not permitted on commandline.
63*4882a593Smuzhiyun	fi
64*4882a593Smuzhiyun}
65*4882a593Smuzhiyun
66*4882a593Smuzhiyunecho > $T/KcList
67*4882a593Smuzhiyunconfig_override_param "$config_dir/CFcommon" KcList "`cat $config_dir/CFcommon 2> /dev/null`"
68*4882a593Smuzhiyunconfig_override_param "$config_template" KcList "`cat $config_template 2> /dev/null`"
69*4882a593Smuzhiyunconfig_override_param "--gdb options" KcList "$TORTURE_KCONFIG_GDB_ARG"
70*4882a593Smuzhiyunconfig_override_param "--kasan options" KcList "$TORTURE_KCONFIG_KASAN_ARG"
71*4882a593Smuzhiyunconfig_override_param "--kcsan options" KcList "$TORTURE_KCONFIG_KCSAN_ARG"
72*4882a593Smuzhiyunconfig_override_param "--kconfig argument" KcList "$TORTURE_KCONFIG_ARG"
73*4882a593Smuzhiyuncp $T/KcList $resdir/ConfigFragment
74*4882a593Smuzhiyun
75*4882a593Smuzhiyunbase_resdir=`echo $resdir | sed -e 's/\.[0-9]\+$//'`
76*4882a593Smuzhiyunif test "$base_resdir" != "$resdir" -a -f $base_resdir/bzImage -a -f $base_resdir/vmlinux
77*4882a593Smuzhiyunthen
78*4882a593Smuzhiyun	# Rerunning previous test, so use that test's kernel.
79*4882a593Smuzhiyun	QEMU="`identify_qemu $base_resdir/vmlinux`"
80*4882a593Smuzhiyun	BOOT_IMAGE="`identify_boot_image $QEMU`"
81*4882a593Smuzhiyun	KERNEL=$base_resdir/${BOOT_IMAGE##*/} # use the last component of ${BOOT_IMAGE}
82*4882a593Smuzhiyun	ln -s $base_resdir/Make*.out $resdir  # for kvm-recheck.sh
83*4882a593Smuzhiyun	ln -s $base_resdir/.config $resdir  # for kvm-recheck.sh
84*4882a593Smuzhiyun	# Arch-independent indicator
85*4882a593Smuzhiyun	touch $resdir/builtkernel
86*4882a593Smuzhiyunelif kvm-build.sh $T/KcList $resdir
87*4882a593Smuzhiyunthen
88*4882a593Smuzhiyun	# Had to build a kernel for this test.
89*4882a593Smuzhiyun	QEMU="`identify_qemu vmlinux`"
90*4882a593Smuzhiyun	BOOT_IMAGE="`identify_boot_image $QEMU`"
91*4882a593Smuzhiyun	cp vmlinux $resdir
92*4882a593Smuzhiyun	cp .config $resdir
93*4882a593Smuzhiyun	cp Module.symvers $resdir > /dev/null || :
94*4882a593Smuzhiyun	cp System.map $resdir > /dev/null || :
95*4882a593Smuzhiyun	if test -n "$BOOT_IMAGE"
96*4882a593Smuzhiyun	then
97*4882a593Smuzhiyun		cp $BOOT_IMAGE $resdir
98*4882a593Smuzhiyun		KERNEL=$resdir/${BOOT_IMAGE##*/}
99*4882a593Smuzhiyun		# Arch-independent indicator
100*4882a593Smuzhiyun		touch $resdir/builtkernel
101*4882a593Smuzhiyun	else
102*4882a593Smuzhiyun		echo No identifiable boot image, not running KVM, see $resdir.
103*4882a593Smuzhiyun		echo Do the torture scripts know about your architecture?
104*4882a593Smuzhiyun	fi
105*4882a593Smuzhiyun	parse-build.sh $resdir/Make.out $title
106*4882a593Smuzhiyunelse
107*4882a593Smuzhiyun	# Build failed.
108*4882a593Smuzhiyun	cp .config $resdir || :
109*4882a593Smuzhiyun	echo Build failed, not running KVM, see $resdir.
110*4882a593Smuzhiyun	if test -f $builddir.wait
111*4882a593Smuzhiyun	then
112*4882a593Smuzhiyun		mv $builddir.wait $builddir.ready
113*4882a593Smuzhiyun	fi
114*4882a593Smuzhiyun	exit 1
115*4882a593Smuzhiyunfi
116*4882a593Smuzhiyunif test -f $builddir.wait
117*4882a593Smuzhiyunthen
118*4882a593Smuzhiyun	mv $builddir.wait $builddir.ready
119*4882a593Smuzhiyunfi
120*4882a593Smuzhiyunwhile test -f $builddir.ready
121*4882a593Smuzhiyundo
122*4882a593Smuzhiyun	sleep 1
123*4882a593Smuzhiyundone
124*4882a593Smuzhiyunseconds=$4
125*4882a593Smuzhiyunqemu_args=$5
126*4882a593Smuzhiyunboot_args=$6
127*4882a593Smuzhiyun
128*4882a593Smuzhiyunkstarttime=`gawk 'BEGIN { print systime() }' < /dev/null`
129*4882a593Smuzhiyunif test -z "$TORTURE_BUILDONLY"
130*4882a593Smuzhiyunthen
131*4882a593Smuzhiyun	echo ' ---' `date`: Starting kernel
132*4882a593Smuzhiyunfi
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun# Generate -smp qemu argument.
135*4882a593Smuzhiyunqemu_args="-enable-kvm -nographic $qemu_args"
136*4882a593Smuzhiyuncpu_count=`configNR_CPUS.sh $resdir/ConfigFragment`
137*4882a593Smuzhiyuncpu_count=`configfrag_boot_cpus "$boot_args" "$config_template" "$cpu_count"`
138*4882a593Smuzhiyunif test "$cpu_count" -gt "$TORTURE_ALLOTED_CPUS"
139*4882a593Smuzhiyunthen
140*4882a593Smuzhiyun	echo CPU count limited from $cpu_count to $TORTURE_ALLOTED_CPUS | tee -a $resdir/Warnings
141*4882a593Smuzhiyun	cpu_count=$TORTURE_ALLOTED_CPUS
142*4882a593Smuzhiyunfi
143*4882a593Smuzhiyunqemu_args="`specify_qemu_cpus "$QEMU" "$qemu_args" "$cpu_count"`"
144*4882a593Smuzhiyunqemu_args="`specify_qemu_net "$qemu_args"`"
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun# Generate architecture-specific and interaction-specific qemu arguments
147*4882a593Smuzhiyunqemu_args="$qemu_args `identify_qemu_args "$QEMU" "$resdir/console.log"`"
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun# Generate qemu -append arguments
150*4882a593Smuzhiyunqemu_append="`identify_qemu_append "$QEMU"`"
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun# Pull in Kconfig-fragment boot parameters
153*4882a593Smuzhiyunboot_args="`configfrag_boot_params "$boot_args" "$config_template"`"
154*4882a593Smuzhiyun# Generate kernel-version-specific boot parameters
155*4882a593Smuzhiyunboot_args="`per_version_boot_params "$boot_args" $resdir/.config $seconds`"
156*4882a593Smuzhiyunif test -n "$TORTURE_BOOT_GDB_ARG"
157*4882a593Smuzhiyunthen
158*4882a593Smuzhiyun	boot_args="$boot_args $TORTURE_BOOT_GDB_ARG"
159*4882a593Smuzhiyunfi
160*4882a593Smuzhiyunecho $QEMU $qemu_args -m $TORTURE_QEMU_MEM -kernel $KERNEL -append \"$qemu_append $boot_args\" $TORTURE_QEMU_GDB_ARG > $resdir/qemu-cmd
161*4882a593Smuzhiyun
162*4882a593Smuzhiyunif test -n "$TORTURE_BUILDONLY"
163*4882a593Smuzhiyunthen
164*4882a593Smuzhiyun	echo Build-only run specified, boot/test omitted.
165*4882a593Smuzhiyun	touch $resdir/buildonly
166*4882a593Smuzhiyun	exit 0
167*4882a593Smuzhiyunfi
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun# Decorate qemu-cmd with redirection, backgrounding, and PID capture
170*4882a593Smuzhiyunsed -e 's/$/ 2>\&1 \&/' < $resdir/qemu-cmd > $T/qemu-cmd
171*4882a593Smuzhiyunecho 'echo $! > $resdir/qemu_pid' >> $T/qemu-cmd
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun# In case qemu refuses to run...
174*4882a593Smuzhiyunecho "NOTE: $QEMU either did not run or was interactive" > $resdir/console.log
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun# Attempt to run qemu
177*4882a593Smuzhiyun( . $T/qemu-cmd; wait `cat  $resdir/qemu_pid`; echo $? > $resdir/qemu-retval ) &
178*4882a593Smuzhiyuncommandcompleted=0
179*4882a593Smuzhiyunif test -z "$TORTURE_KCONFIG_GDB_ARG"
180*4882a593Smuzhiyunthen
181*4882a593Smuzhiyun	sleep 10 # Give qemu's pid a chance to reach the file
182*4882a593Smuzhiyun	if test -s "$resdir/qemu_pid"
183*4882a593Smuzhiyun	then
184*4882a593Smuzhiyun		qemu_pid=`cat "$resdir/qemu_pid"`
185*4882a593Smuzhiyun		echo Monitoring qemu job at pid $qemu_pid
186*4882a593Smuzhiyun	else
187*4882a593Smuzhiyun		qemu_pid=""
188*4882a593Smuzhiyun		echo Monitoring qemu job at yet-as-unknown pid
189*4882a593Smuzhiyun	fi
190*4882a593Smuzhiyunfi
191*4882a593Smuzhiyunif test -n "$TORTURE_KCONFIG_GDB_ARG"
192*4882a593Smuzhiyunthen
193*4882a593Smuzhiyun	echo Waiting for you to attach a debug session, for example: > /dev/tty
194*4882a593Smuzhiyun	echo "    gdb $base_resdir/vmlinux" > /dev/tty
195*4882a593Smuzhiyun	echo 'After symbols load and the "(gdb)" prompt appears:' > /dev/tty
196*4882a593Smuzhiyun	echo "    target remote :1234" > /dev/tty
197*4882a593Smuzhiyun	echo "    continue" > /dev/tty
198*4882a593Smuzhiyun	kstarttime=`gawk 'BEGIN { print systime() }' < /dev/null`
199*4882a593Smuzhiyunfi
200*4882a593Smuzhiyunwhile :
201*4882a593Smuzhiyundo
202*4882a593Smuzhiyun	if test -z "$qemu_pid" -a -s "$resdir/qemu_pid"
203*4882a593Smuzhiyun	then
204*4882a593Smuzhiyun		qemu_pid=`cat "$resdir/qemu_pid"`
205*4882a593Smuzhiyun	fi
206*4882a593Smuzhiyun	kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
207*4882a593Smuzhiyun	if test -z "$qemu_pid" || kill -0 "$qemu_pid" > /dev/null 2>&1
208*4882a593Smuzhiyun	then
209*4882a593Smuzhiyun		if test $kruntime -ge $seconds -o -f "$TORTURE_STOPFILE"
210*4882a593Smuzhiyun		then
211*4882a593Smuzhiyun			break;
212*4882a593Smuzhiyun		fi
213*4882a593Smuzhiyun		sleep 1
214*4882a593Smuzhiyun	else
215*4882a593Smuzhiyun		commandcompleted=1
216*4882a593Smuzhiyun		if test $kruntime -lt $seconds
217*4882a593Smuzhiyun		then
218*4882a593Smuzhiyun			echo Completed in $kruntime vs. $seconds >> $resdir/Warnings 2>&1
219*4882a593Smuzhiyun			grep "^(qemu) qemu:" $resdir/kvm-test-1-run.sh.out >> $resdir/Warnings 2>&1
220*4882a593Smuzhiyun			killpid="`sed -n "s/^(qemu) qemu: terminating on signal [0-9]* from pid \([0-9]*\).*$/\1/p" $resdir/Warnings`"
221*4882a593Smuzhiyun			if test -n "$killpid"
222*4882a593Smuzhiyun			then
223*4882a593Smuzhiyun				echo "ps -fp $killpid" >> $resdir/Warnings 2>&1
224*4882a593Smuzhiyun				ps -fp $killpid >> $resdir/Warnings 2>&1
225*4882a593Smuzhiyun			fi
226*4882a593Smuzhiyun		else
227*4882a593Smuzhiyun			echo ' ---' `date`: "Kernel done"
228*4882a593Smuzhiyun		fi
229*4882a593Smuzhiyun		break
230*4882a593Smuzhiyun	fi
231*4882a593Smuzhiyundone
232*4882a593Smuzhiyunif test -z "$qemu_pid" -a -s "$resdir/qemu_pid"
233*4882a593Smuzhiyunthen
234*4882a593Smuzhiyun	qemu_pid=`cat "$resdir/qemu_pid"`
235*4882a593Smuzhiyunfi
236*4882a593Smuzhiyunif test $commandcompleted -eq 0 -a -n "$qemu_pid"
237*4882a593Smuzhiyunthen
238*4882a593Smuzhiyun	if ! test -f "$TORTURE_STOPFILE"
239*4882a593Smuzhiyun	then
240*4882a593Smuzhiyun		echo Grace period for qemu job at pid $qemu_pid
241*4882a593Smuzhiyun	fi
242*4882a593Smuzhiyun	oldline="`tail $resdir/console.log`"
243*4882a593Smuzhiyun	while :
244*4882a593Smuzhiyun	do
245*4882a593Smuzhiyun		if test -f "$TORTURE_STOPFILE"
246*4882a593Smuzhiyun		then
247*4882a593Smuzhiyun			echo "PID $qemu_pid killed due to run STOP request" >> $resdir/Warnings 2>&1
248*4882a593Smuzhiyun			kill -KILL $qemu_pid
249*4882a593Smuzhiyun			break
250*4882a593Smuzhiyun		fi
251*4882a593Smuzhiyun		kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
252*4882a593Smuzhiyun		if kill -0 $qemu_pid > /dev/null 2>&1
253*4882a593Smuzhiyun		then
254*4882a593Smuzhiyun			:
255*4882a593Smuzhiyun		else
256*4882a593Smuzhiyun			break
257*4882a593Smuzhiyun		fi
258*4882a593Smuzhiyun		must_continue=no
259*4882a593Smuzhiyun		newline="`tail $resdir/console.log`"
260*4882a593Smuzhiyun		if test "$newline" != "$oldline" && echo $newline | grep -q ' [0-9]\+us : '
261*4882a593Smuzhiyun		then
262*4882a593Smuzhiyun			must_continue=yes
263*4882a593Smuzhiyun		fi
264*4882a593Smuzhiyun		last_ts="`tail $resdir/console.log | grep '^\[ *[0-9]\+\.[0-9]\+]' | tail -1 | sed -e 's/^\[ *//' -e 's/\..*$//'`"
265*4882a593Smuzhiyun		if test -z "$last_ts"
266*4882a593Smuzhiyun		then
267*4882a593Smuzhiyun			last_ts=0
268*4882a593Smuzhiyun		fi
269*4882a593Smuzhiyun		if test "$newline" != "$oldline" -a "$last_ts" -lt $((seconds + $TORTURE_SHUTDOWN_GRACE))
270*4882a593Smuzhiyun		then
271*4882a593Smuzhiyun			must_continue=yes
272*4882a593Smuzhiyun		fi
273*4882a593Smuzhiyun		if test $must_continue = no -a $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE))
274*4882a593Smuzhiyun		then
275*4882a593Smuzhiyun			echo "!!! PID $qemu_pid hung at $kruntime vs. $seconds seconds" >> $resdir/Warnings 2>&1
276*4882a593Smuzhiyun			kill -KILL $qemu_pid
277*4882a593Smuzhiyun			break
278*4882a593Smuzhiyun		fi
279*4882a593Smuzhiyun		oldline=$newline
280*4882a593Smuzhiyun		sleep 10
281*4882a593Smuzhiyun	done
282*4882a593Smuzhiyunelif test -z "$qemu_pid"
283*4882a593Smuzhiyunthen
284*4882a593Smuzhiyun	echo Unknown PID, cannot kill qemu command
285*4882a593Smuzhiyunfi
286*4882a593Smuzhiyun
287*4882a593Smuzhiyunparse-console.sh $resdir/console.log $title
288