1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0+
3#
4# Run a kvm-based test of the specified tree on the specified configs.
5# Fully automated run and error checking, no graphics console.
6#
7# Execute this in the source tree.  Do not run it as a background task
8# because qemu does not seem to like that much.
9#
10# Usage: kvm-test-1-run.sh config builddir resdir seconds qemu-args boot_args
11#
12# qemu-args defaults to "-enable-kvm -nographic", along with arguments
13#			specifying the number of CPUs and other options
14#			generated from the underlying CPU architecture.
15# boot_args defaults to value returned by the per_version_boot_params
16#			shell function.
17#
18# Anything you specify for either qemu-args or boot_args is appended to
19# the default values.  The "-smp" value is deduced from the contents of
20# the config fragment.
21#
22# More sophisticated argument parsing is clearly needed.
23#
24# Copyright (C) IBM Corporation, 2011
25#
26# Authors: Paul E. McKenney <paulmck@linux.ibm.com>
27
28T=${TMPDIR-/tmp}/kvm-test-1-run.sh.$$
29trap 'rm -rf $T' 0
30mkdir $T
31
32. functions.sh
33. $CONFIGFRAG/ver_functions.sh
34
35config_template=${1}
36config_dir=`echo $config_template | sed -e 's,/[^/]*$,,'`
37title=`echo $config_template | sed -e 's/^.*\///'`
38builddir=${2}
39resdir=${3}
40if test -z "$resdir" -o ! -d "$resdir" -o ! -w "$resdir"
41then
42	echo "kvm-test-1-run.sh :$resdir: Not a writable directory, cannot store results into it"
43	exit 1
44fi
45echo ' ---' `date`: Starting build
46echo ' ---' Kconfig fragment at: $config_template >> $resdir/log
47touch $resdir/ConfigFragment.input $resdir/ConfigFragment
48if test -r "$config_dir/CFcommon"
49then
50	echo " --- $config_dir/CFcommon" >> $resdir/ConfigFragment.input
51	cat < $config_dir/CFcommon >> $resdir/ConfigFragment.input
52	config_override.sh $config_dir/CFcommon $config_template > $T/Kc1
53	grep '#CHECK#' $config_dir/CFcommon >> $resdir/ConfigFragment
54else
55	cp $config_template $T/Kc1
56fi
57echo " --- $config_template" >> $resdir/ConfigFragment.input
58cat $config_template >> $resdir/ConfigFragment.input
59grep '#CHECK#' $config_template >> $resdir/ConfigFragment
60if test -n "$TORTURE_KCONFIG_ARG"
61then
62	echo $TORTURE_KCONFIG_ARG | tr -s " " "\012" > $T/cmdline
63	echo " --- --kconfig argument" >> $resdir/ConfigFragment.input
64	cat $T/cmdline >> $resdir/ConfigFragment.input
65	config_override.sh $T/Kc1 $T/cmdline > $T/Kc2
66	# Note that "#CHECK#" is not permitted on commandline.
67else
68	cp $T/Kc1 $T/Kc2
69fi
70cat $T/Kc2 >> $resdir/ConfigFragment
71
72base_resdir=`echo $resdir | sed -e 's/\.[0-9]\+$//'`
73if test "$base_resdir" != "$resdir" -a -f $base_resdir/bzImage -a -f $base_resdir/vmlinux
74then
75	# Rerunning previous test, so use that test's kernel.
76	QEMU="`identify_qemu $base_resdir/vmlinux`"
77	BOOT_IMAGE="`identify_boot_image $QEMU`"
78	KERNEL=$base_resdir/${BOOT_IMAGE##*/} # use the last component of ${BOOT_IMAGE}
79	ln -s $base_resdir/Make*.out $resdir  # for kvm-recheck.sh
80	ln -s $base_resdir/.config $resdir  # for kvm-recheck.sh
81	# Arch-independent indicator
82	touch $resdir/builtkernel
83elif kvm-build.sh $T/Kc2 $resdir
84then
85	# Had to build a kernel for this test.
86	QEMU="`identify_qemu vmlinux`"
87	BOOT_IMAGE="`identify_boot_image $QEMU`"
88	cp vmlinux $resdir
89	cp .config $resdir
90	cp Module.symvers $resdir > /dev/null || :
91	cp System.map $resdir > /dev/null || :
92	if test -n "$BOOT_IMAGE"
93	then
94		cp $BOOT_IMAGE $resdir
95		KERNEL=$resdir/${BOOT_IMAGE##*/}
96		# Arch-independent indicator
97		touch $resdir/builtkernel
98	else
99		echo No identifiable boot image, not running KVM, see $resdir.
100		echo Do the torture scripts know about your architecture?
101	fi
102	parse-build.sh $resdir/Make.out $title
103else
104	# Build failed.
105	cp .config $resdir || :
106	echo Build failed, not running KVM, see $resdir.
107	if test -f $builddir.wait
108	then
109		mv $builddir.wait $builddir.ready
110	fi
111	exit 1
112fi
113if test -f $builddir.wait
114then
115	mv $builddir.wait $builddir.ready
116fi
117while test -f $builddir.ready
118do
119	sleep 1
120done
121seconds=$4
122qemu_args=$5
123boot_args=$6
124
125cd $KVM
126kstarttime=`gawk 'BEGIN { print systime() }' < /dev/null`
127if test -z "$TORTURE_BUILDONLY"
128then
129	echo ' ---' `date`: Starting kernel
130fi
131
132# Generate -smp qemu argument.
133qemu_args="-enable-kvm -nographic $qemu_args"
134cpu_count=`configNR_CPUS.sh $resdir/ConfigFragment`
135cpu_count=`configfrag_boot_cpus "$boot_args" "$config_template" "$cpu_count"`
136if test "$cpu_count" -gt "$TORTURE_ALLOTED_CPUS"
137then
138	echo CPU count limited from $cpu_count to $TORTURE_ALLOTED_CPUS | tee -a $resdir/Warnings
139	cpu_count=$TORTURE_ALLOTED_CPUS
140fi
141qemu_args="`specify_qemu_cpus "$QEMU" "$qemu_args" "$cpu_count"`"
142
143# Generate architecture-specific and interaction-specific qemu arguments
144qemu_args="$qemu_args `identify_qemu_args "$QEMU" "$resdir/console.log"`"
145
146# Generate qemu -append arguments
147qemu_append="`identify_qemu_append "$QEMU"`"
148
149# Pull in Kconfig-fragment boot parameters
150boot_args="`configfrag_boot_params "$boot_args" "$config_template"`"
151# Generate kernel-version-specific boot parameters
152boot_args="`per_version_boot_params "$boot_args" $resdir/.config $seconds`"
153
154if test -n "$TORTURE_BUILDONLY"
155then
156	echo Build-only run specified, boot/test omitted.
157	touch $resdir/buildonly
158	exit 0
159fi
160echo "NOTE: $QEMU either did not run or was interactive" > $resdir/console.log
161echo $QEMU $qemu_args -m $TORTURE_QEMU_MEM -kernel $KERNEL -append \"$qemu_append $boot_args\" > $resdir/qemu-cmd
162( $QEMU $qemu_args -m $TORTURE_QEMU_MEM -kernel $KERNEL -append "$qemu_append $boot_args" > $resdir/qemu-output 2>&1 & echo $! > $resdir/qemu_pid; wait `cat  $resdir/qemu_pid`; echo $? > $resdir/qemu-retval ) &
163commandcompleted=0
164sleep 10 # Give qemu's pid a chance to reach the file
165if test -s "$resdir/qemu_pid"
166then
167	qemu_pid=`cat "$resdir/qemu_pid"`
168	echo Monitoring qemu job at pid $qemu_pid
169else
170	qemu_pid=""
171	echo Monitoring qemu job at yet-as-unknown pid
172fi
173while :
174do
175	if test -z "$qemu_pid" -a -s "$resdir/qemu_pid"
176	then
177		qemu_pid=`cat "$resdir/qemu_pid"`
178	fi
179	kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
180	if test -z "$qemu_pid" || kill -0 "$qemu_pid" > /dev/null 2>&1
181	then
182		if test $kruntime -ge $seconds
183		then
184			break;
185		fi
186		sleep 1
187	else
188		commandcompleted=1
189		if test $kruntime -lt $seconds
190		then
191			echo Completed in $kruntime vs. $seconds >> $resdir/Warnings 2>&1
192			grep "^(qemu) qemu:" $resdir/kvm-test-1-run.sh.out >> $resdir/Warnings 2>&1
193			killpid="`sed -n "s/^(qemu) qemu: terminating on signal [0-9]* from pid \([0-9]*\).*$/\1/p" $resdir/Warnings`"
194			if test -n "$killpid"
195			then
196				echo "ps -fp $killpid" >> $resdir/Warnings 2>&1
197				ps -fp $killpid >> $resdir/Warnings 2>&1
198			fi
199		else
200			echo ' ---' `date`: "Kernel done"
201		fi
202		break
203	fi
204done
205if test -z "$qemu_pid" -a -s "$resdir/qemu_pid"
206then
207	qemu_pid=`cat "$resdir/qemu_pid"`
208fi
209if test $commandcompleted -eq 0 -a -n "$qemu_pid"
210then
211	echo Grace period for qemu job at pid $qemu_pid
212	oldline="`tail $resdir/console.log`"
213	while :
214	do
215		kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
216		if kill -0 $qemu_pid > /dev/null 2>&1
217		then
218			:
219		else
220			break
221		fi
222		must_continue=no
223		newline="`tail $resdir/console.log`"
224		if test "$newline" != "$oldline" && echo $newline | grep -q ' [0-9]\+us : '
225		then
226			must_continue=yes
227		fi
228		last_ts="`tail $resdir/console.log | grep '^\[ *[0-9]\+\.[0-9]\+]' | tail -1 | sed -e 's/^\[ *//' -e 's/\..*$//'`"
229		if test -z "$last_ts"
230		then
231			last_ts=0
232		fi
233		if test "$newline" != "$oldline" -a "$last_ts" -lt $((seconds + $TORTURE_SHUTDOWN_GRACE))
234		then
235			must_continue=yes
236		fi
237		if test $must_continue = no -a $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE))
238		then
239			echo "!!! PID $qemu_pid hung at $kruntime vs. $seconds seconds" >> $resdir/Warnings 2>&1
240			kill -KILL $qemu_pid
241			break
242		fi
243		oldline=$newline
244		sleep 10
245	done
246elif test -z "$qemu_pid"
247then
248	echo Unknown PID, cannot kill qemu command
249fi
250
251parse-console.sh $resdir/console.log $title
252