xref: /linux/tools/testing/selftests/rcutorture/bin/kvm-test-1-run.sh (revision 0883c2c06fb5bcf5b9e008270827e63c09a88c1e)
1#!/bin/bash
2#
3# Run a kvm-based test of the specified tree on the specified configs.
4# Fully automated run and error checking, no graphics console.
5#
6# Execute this in the source tree.  Do not run it as a background task
7# because qemu does not seem to like that much.
8#
9# Usage: kvm-test-1-run.sh config builddir resdir seconds qemu-args boot_args
10#
11# qemu-args defaults to "-enable-kvm -soundhw pcspk -nographic", along with
12#			arguments specifying the number of CPUs and other
13#			options generated from the underlying CPU architecture.
14# boot_args defaults to value returned by the per_version_boot_params
15#			shell function.
16#
17# Anything you specify for either qemu-args or boot_args is appended to
18# the default values.  The "-smp" value is deduced from the contents of
19# the config fragment.
20#
21# More sophisticated argument parsing is clearly needed.
22#
23# This program is free software; you can redistribute it and/or modify
24# it under the terms of the GNU General Public License as published by
25# the Free Software Foundation; either version 2 of the License, or
26# (at your option) any later version.
27#
28# This program is distributed in the hope that it will be useful,
29# but WITHOUT ANY WARRANTY; without even the implied warranty of
30# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31# GNU General Public License for more details.
32#
33# You should have received a copy of the GNU General Public License
34# along with this program; if not, you can access it online at
35# http://www.gnu.org/licenses/gpl-2.0.html.
36#
37# Copyright (C) IBM Corporation, 2011
38#
39# Authors: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
40
41T=/tmp/kvm-test-1-run.sh.$$
42trap 'rm -rf $T' 0
43touch $T
44
45. $KVM/bin/functions.sh
46. $CONFIGFRAG/ver_functions.sh
47
48config_template=${1}
49config_dir=`echo $config_template | sed -e 's,/[^/]*$,,'`
50title=`echo $config_template | sed -e 's/^.*\///'`
51builddir=${2}
52if test -z "$builddir" -o ! -d "$builddir" -o ! -w "$builddir"
53then
54	echo "kvm-test-1-run.sh :$builddir: Not a writable directory, cannot build into it"
55	exit 1
56fi
57resdir=${3}
58if test -z "$resdir" -o ! -d "$resdir" -o ! -w "$resdir"
59then
60	echo "kvm-test-1-run.sh :$resdir: Not a writable directory, cannot store results into it"
61	exit 1
62fi
63cp $config_template $resdir/ConfigFragment
64echo ' ---' `date`: Starting build
65echo ' ---' Kconfig fragment at: $config_template >> $resdir/log
66if test -r "$config_dir/CFcommon"
67then
68	cat < $config_dir/CFcommon >> $T
69fi
70# Optimizations below this point
71# CONFIG_USB=n
72# CONFIG_SECURITY=n
73# CONFIG_NFS_FS=n
74# CONFIG_SOUND=n
75# CONFIG_INPUT_JOYSTICK=n
76# CONFIG_INPUT_TABLET=n
77# CONFIG_INPUT_TOUCHSCREEN=n
78# CONFIG_INPUT_MISC=n
79# CONFIG_INPUT_MOUSE=n
80# # CONFIG_NET=n # disables console access, so accept the slower build.
81# CONFIG_SCSI=n
82# CONFIG_ATA=n
83# CONFIG_FAT_FS=n
84# CONFIG_MSDOS_FS=n
85# CONFIG_VFAT_FS=n
86# CONFIG_ISO9660_FS=n
87# CONFIG_QUOTA=n
88# CONFIG_HID=n
89# CONFIG_CRYPTO=n
90# CONFIG_PCCARD=n
91# CONFIG_PCMCIA=n
92# CONFIG_CARDBUS=n
93# CONFIG_YENTA=n
94base_resdir=`echo $resdir | sed -e 's/\.[0-9]\+$//'`
95if test "$base_resdir" != "$resdir" -a -f $base_resdir/bzImage -a -f $base_resdir/vmlinux
96then
97	# Rerunning previous test, so use that test's kernel.
98	QEMU="`identify_qemu $base_resdir/vmlinux`"
99	KERNEL=$base_resdir/bzImage
100	ln -s $base_resdir/Make*.out $resdir  # for kvm-recheck.sh
101	ln -s $base_resdir/.config $resdir  # for kvm-recheck.sh
102elif kvm-build.sh $config_template $builddir $T
103then
104	# Had to build a kernel for this test.
105	QEMU="`identify_qemu $builddir/vmlinux`"
106	BOOT_IMAGE="`identify_boot_image $QEMU`"
107	cp $builddir/Make*.out $resdir
108	cp $builddir/vmlinux $resdir
109	cp $builddir/.config $resdir
110	if test -n "$BOOT_IMAGE"
111	then
112		cp $builddir/$BOOT_IMAGE $resdir
113		KERNEL=$resdir/bzImage
114	else
115		echo No identifiable boot image, not running KVM, see $resdir.
116		echo Do the torture scripts know about your architecture?
117	fi
118	parse-build.sh $resdir/Make.out $title
119else
120	# Build failed.
121	cp $builddir/Make*.out $resdir
122	cp $builddir/.config $resdir || :
123	echo Build failed, not running KVM, see $resdir.
124	if test -f $builddir.wait
125	then
126		mv $builddir.wait $builddir.ready
127	fi
128	exit 1
129fi
130if test -f $builddir.wait
131then
132	mv $builddir.wait $builddir.ready
133fi
134while test -f $builddir.ready
135do
136	sleep 1
137done
138seconds=$4
139qemu_args=$5
140boot_args=$6
141
142cd $KVM
143kstarttime=`awk 'BEGIN { print systime() }' < /dev/null`
144if test -z "$TORTURE_BUILDONLY"
145then
146	echo ' ---' `date`: Starting kernel
147fi
148
149# Generate -smp qemu argument.
150qemu_args="-enable-kvm -soundhw pcspk -nographic $qemu_args"
151cpu_count=`configNR_CPUS.sh $config_template`
152cpu_count=`configfrag_boot_cpus "$boot_args" "$config_template" "$cpu_count"`
153vcpus=`identify_qemu_vcpus`
154if test $cpu_count -gt $vcpus
155then
156	echo CPU count limited from $cpu_count to $vcpus
157	touch $resdir/Warnings
158	echo CPU count limited from $cpu_count to $vcpus >> $resdir/Warnings
159	cpu_count=$vcpus
160fi
161qemu_args="`specify_qemu_cpus "$QEMU" "$qemu_args" "$cpu_count"`"
162
163# Generate architecture-specific and interaction-specific qemu arguments
164qemu_args="$qemu_args `identify_qemu_args "$QEMU" "$resdir/console.log"`"
165
166# Generate qemu -append arguments
167qemu_append="`identify_qemu_append "$QEMU"`"
168
169# Pull in Kconfig-fragment boot parameters
170boot_args="`configfrag_boot_params "$boot_args" "$config_template"`"
171# Generate kernel-version-specific boot parameters
172boot_args="`per_version_boot_params "$boot_args" $builddir/.config $seconds`"
173
174if test -n "$TORTURE_BUILDONLY"
175then
176	echo Build-only run specified, boot/test omitted.
177	touch $resdir/buildonly
178	exit 0
179fi
180echo "NOTE: $QEMU either did not run or was interactive" > $resdir/console.log
181echo $QEMU $qemu_args -m 512 -kernel $KERNEL -append \"$qemu_append $boot_args\" > $resdir/qemu-cmd
182( $QEMU $qemu_args -m 512 -kernel $KERNEL -append "$qemu_append $boot_args"& echo $! > $resdir/qemu_pid; wait `cat  $resdir/qemu_pid`; echo $? > $resdir/qemu-retval ) &
183commandcompleted=0
184sleep 10 # Give qemu's pid a chance to reach the file
185if test -s "$resdir/qemu_pid"
186then
187	qemu_pid=`cat "$resdir/qemu_pid"`
188	echo Monitoring qemu job at pid $qemu_pid
189else
190	qemu_pid=""
191	echo Monitoring qemu job at yet-as-unknown pid
192fi
193while :
194do
195	if test -z "$qemu_pid" -a -s "$resdir/qemu_pid"
196	then
197		qemu_pid=`cat "$resdir/qemu_pid"`
198	fi
199	kruntime=`awk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
200	if test -z "$qemu_pid" || kill -0 "$qemu_pid" > /dev/null 2>&1
201	then
202		if test $kruntime -ge $seconds
203		then
204			break;
205		fi
206		sleep 1
207	else
208		commandcompleted=1
209		if test $kruntime -lt $seconds
210		then
211			echo Completed in $kruntime vs. $seconds >> $resdir/Warnings 2>&1
212			grep "^(qemu) qemu:" $resdir/kvm-test-1-run.sh.out >> $resdir/Warnings 2>&1
213			killpid="`sed -n "s/^(qemu) qemu: terminating on signal [0-9]* from pid \([0-9]*\).*$/\1/p" $resdir/Warnings`"
214			if test -n "$killpid"
215			then
216				echo "ps -fp $killpid" >> $resdir/Warnings 2>&1
217				ps -fp $killpid >> $resdir/Warnings 2>&1
218			fi
219		else
220			echo ' ---' `date`: "Kernel done"
221		fi
222		break
223	fi
224done
225if test -z "$qemu_pid" -a -s "$resdir/qemu_pid"
226then
227	qemu_pid=`cat "$resdir/qemu_pid"`
228fi
229if test $commandcompleted -eq 0 -a -n "$qemu_pid"
230then
231	echo Grace period for qemu job at pid $qemu_pid
232	while :
233	do
234		kruntime=`awk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
235		if kill -0 $qemu_pid > /dev/null 2>&1
236		then
237			:
238		else
239			break
240		fi
241		if test $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE))
242		then
243			echo "!!! PID $qemu_pid hung at $kruntime vs. $seconds seconds" >> $resdir/Warnings 2>&1
244			kill -KILL $qemu_pid
245			break
246		fi
247		sleep 1
248	done
249elif test -z "$qemu_pid"
250then
251	echo Unknown PID, cannot kill qemu command
252fi
253
254parse-torture.sh $resdir/console.log $title
255parse-console.sh $resdir/console.log $title
256