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 -nographic", along with arguments
12#			specifying the number of CPUs and other options
13#			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=${TMPDIR-/tmp}/kvm-test-1-run.sh.$$
42trap 'rm -rf $T' 0
43mkdir $T
44
45. 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
63echo ' ---' `date`: Starting build
64echo ' ---' Kconfig fragment at: $config_template >> $resdir/log
65touch $resdir/ConfigFragment.input $resdir/ConfigFragment
66if test -r "$config_dir/CFcommon"
67then
68	echo " --- $config_dir/CFcommon" >> $resdir/ConfigFragment.input
69	cat < $config_dir/CFcommon >> $resdir/ConfigFragment.input
70	config_override.sh $config_dir/CFcommon $config_template > $T/Kc1
71	grep '#CHECK#' $config_dir/CFcommon >> $resdir/ConfigFragment
72else
73	cp $config_template $T/Kc1
74fi
75echo " --- $config_template" >> $resdir/ConfigFragment.input
76cat $config_template >> $resdir/ConfigFragment.input
77grep '#CHECK#' $config_template >> $resdir/ConfigFragment
78if test -n "$TORTURE_KCONFIG_ARG"
79then
80	echo $TORTURE_KCONFIG_ARG | tr -s " " "\012" > $T/cmdline
81	echo " --- --kconfig argument" >> $resdir/ConfigFragment.input
82	cat $T/cmdline >> $resdir/ConfigFragment.input
83	config_override.sh $T/Kc1 $T/cmdline > $T/Kc2
84	# Note that "#CHECK#" is not permitted on commandline.
85else
86	cp $T/Kc1 $T/Kc2
87fi
88cat $T/Kc2 >> $resdir/ConfigFragment
89
90base_resdir=`echo $resdir | sed -e 's/\.[0-9]\+$//'`
91if test "$base_resdir" != "$resdir" -a -f $base_resdir/bzImage -a -f $base_resdir/vmlinux
92then
93	# Rerunning previous test, so use that test's kernel.
94	QEMU="`identify_qemu $base_resdir/vmlinux`"
95	BOOT_IMAGE="`identify_boot_image $QEMU`"
96	KERNEL=$base_resdir/${BOOT_IMAGE##*/} # use the last component of ${BOOT_IMAGE}
97	ln -s $base_resdir/Make*.out $resdir  # for kvm-recheck.sh
98	ln -s $base_resdir/.config $resdir  # for kvm-recheck.sh
99	# Arch-independent indicator
100	touch $resdir/builtkernel
101elif kvm-build.sh $T/Kc2 $builddir $resdir
102then
103	# Had to build a kernel for this test.
104	QEMU="`identify_qemu $builddir/vmlinux`"
105	BOOT_IMAGE="`identify_boot_image $QEMU`"
106	cp $builddir/vmlinux $resdir
107	cp $builddir/.config $resdir
108	cp $builddir/Module.symvers $resdir > /dev/null || :
109	cp $builddir/System.map $resdir > /dev/null || :
110	if test -n "$BOOT_IMAGE"
111	then
112		cp $builddir/$BOOT_IMAGE $resdir
113		KERNEL=$resdir/${BOOT_IMAGE##*/}
114		# Arch-independent indicator
115		touch $resdir/builtkernel
116	else
117		echo No identifiable boot image, not running KVM, see $resdir.
118		echo Do the torture scripts know about your architecture?
119	fi
120	parse-build.sh $resdir/Make.out $title
121else
122	# Build failed.
123	cp $builddir/Make*.out $resdir
124	cp $builddir/.config $resdir || :
125	echo Build failed, not running KVM, see $resdir.
126	if test -f $builddir.wait
127	then
128		mv $builddir.wait $builddir.ready
129	fi
130	exit 1
131fi
132if test -f $builddir.wait
133then
134	mv $builddir.wait $builddir.ready
135fi
136while test -f $builddir.ready
137do
138	sleep 1
139done
140seconds=$4
141qemu_args=$5
142boot_args=$6
143
144cd $KVM
145kstarttime=`awk 'BEGIN { print systime() }' < /dev/null`
146if test -z "$TORTURE_BUILDONLY"
147then
148	echo ' ---' `date`: Starting kernel
149fi
150
151# Generate -smp qemu argument.
152qemu_args="-enable-kvm -nographic $qemu_args"
153cpu_count=`configNR_CPUS.sh $resdir/ConfigFragment`
154cpu_count=`configfrag_boot_cpus "$boot_args" "$config_template" "$cpu_count"`
155vcpus=`identify_qemu_vcpus`
156if test $cpu_count -gt $vcpus
157then
158	echo CPU count limited from $cpu_count to $vcpus | tee -a $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" $resdir/.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 $TORTURE_QEMU_MEM -kernel $KERNEL -append \"$qemu_append $boot_args\" > $resdir/qemu-cmd
182( $QEMU $qemu_args -m $TORTURE_QEMU_MEM -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	oldline="`tail $resdir/console.log`"
233	while :
234	do
235		kruntime=`awk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
236		if kill -0 $qemu_pid > /dev/null 2>&1
237		then
238			:
239		else
240			break
241		fi
242		must_continue=no
243		newline="`tail $resdir/console.log`"
244		if test "$newline" != "$oldline" && echo $newline | grep -q ' [0-9]\+us : '
245		then
246			must_continue=yes
247		fi
248		last_ts="`tail $resdir/console.log | grep '^\[ *[0-9]\+\.[0-9]\+]' | tail -1 | sed -e 's/^\[ *//' -e 's/\..*$//'`"
249		if test -z "last_ts"
250		then
251			last_ts=0
252		fi
253		if test "$newline" != "$oldline" -a "$last_ts" -lt $((seconds + $TORTURE_SHUTDOWN_GRACE))
254		then
255			must_continue=yes
256		fi
257		if test $must_continue = no -a $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE))
258		then
259			echo "!!! PID $qemu_pid hung at $kruntime vs. $seconds seconds" >> $resdir/Warnings 2>&1
260			kill -KILL $qemu_pid
261			break
262		fi
263		oldline=$newline
264		sleep 10
265	done
266elif test -z "$qemu_pid"
267then
268	echo Unknown PID, cannot kill qemu command
269fi
270
271parse-console.sh $resdir/console.log $title
272