xref: /linux/tools/testing/selftests/rcutorture/bin/kvm-test-1-run-qemu.sh (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0+
3#
4# Carry out a kvm-based run for the specified qemu-cmd file, which might
5# have been generated by --build-only kvm.sh run.
6#
7# Usage: kvm-test-1-run-qemu.sh qemu-cmd-dir
8#
9# qemu-cmd-dir provides the directory containing qemu-cmd file.
10#	This is assumed to be of the form prefix/ds/scenario, where
11#	"ds" is the top-level date-stamped directory and "scenario"
12#	is the scenario name.  Any required adjustments to this file
13#	must have been made by the caller.  The shell-command comments
14#	at the end of the qemu-cmd file are not optional.
15#
16# Copyright (C) 2021 Facebook, Inc.
17#
18# Authors: Paul E. McKenney <paulmck@kernel.org>
19
20T="`mktemp -d ${TMPDIR-/tmp}/kvm-test-1-run-qemu.sh.XXXXXX`"
21trap 'rm -rf $T' 0
22
23resdir="$1"
24if ! test -d "$resdir"
25then
26	echo $0: Nonexistent directory: $resdir
27	exit 1
28fi
29if ! test -f "$resdir/qemu-cmd"
30then
31	echo $0: Nonexistent qemu-cmd file: $resdir/qemu-cmd
32	exit 1
33fi
34
35echo ' ---' `date`: Starting kernel, PID $$
36
37# Obtain settings from the qemu-cmd file.
38grep '^#' $resdir/qemu-cmd | sed -e 's/^# //' > $T/qemu-cmd-settings
39. $T/qemu-cmd-settings
40
41# Decorate qemu-cmd with affinity, redirection, backgrounding, and PID capture
42taskset_command=
43if test -n "$TORTURE_AFFINITY"
44then
45	taskset_command="taskset -c $TORTURE_AFFINITY "
46fi
47sed -e 's/^[^#].*$/'"$taskset_command"'& 2>\&1 \&/' < $resdir/qemu-cmd > $T/qemu-cmd
48echo 'qemu_pid=$!' >> $T/qemu-cmd
49echo 'echo $qemu_pid > $resdir/qemu-pid' >> $T/qemu-cmd
50echo 'taskset -c -p $qemu_pid > $resdir/qemu-affinity' >> $T/qemu-cmd
51
52# In case qemu refuses to run...
53echo "NOTE: $QEMU either did not run or was interactive" > $resdir/console.log
54
55# Attempt to run qemu
56kstarttime=`gawk 'BEGIN { print systime() }' < /dev/null`
57( . $T/qemu-cmd; wait `cat  $resdir/qemu-pid`; echo $? > $resdir/qemu-retval ) &
58commandcompleted=0
59if test -z "$TORTURE_KCONFIG_GDB_ARG"
60then
61	sleep 10 # Give qemu's pid a chance to reach the file
62	if test -s "$resdir/qemu-pid"
63	then
64		qemu_pid=`cat "$resdir/qemu-pid"`
65		echo Monitoring qemu job at pid $qemu_pid `date`
66	else
67		qemu_pid=""
68		echo Monitoring qemu job at yet-as-unknown pid `date`
69	fi
70fi
71if test -n "$TORTURE_KCONFIG_GDB_ARG"
72then
73	base_resdir=`echo $resdir | sed -e 's/\.[0-9]\+$//'`
74	if ! test -f $base_resdir/vmlinux
75	then
76		base_resdir="`cat re-run`/$resdir"
77		if ! test -f $base_resdir/vmlinux
78		then
79			base_resdir=/path/to
80		fi
81	fi
82	echo Waiting for you to attach a debug session, for example: > /dev/tty
83	echo "    gdb $base_resdir/vmlinux" > /dev/tty
84	echo 'After symbols load and the "(gdb)" prompt appears:' > /dev/tty
85	echo "    target remote :1234" > /dev/tty
86	echo "    continue" > /dev/tty
87	kstarttime=`gawk 'BEGIN { print systime() }' < /dev/null`
88fi
89while :
90do
91	if test -z "$qemu_pid" && test -s "$resdir/qemu-pid"
92	then
93		qemu_pid=`cat "$resdir/qemu-pid"`
94	fi
95	kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
96	if test -z "$qemu_pid" || kill -0 "$qemu_pid" > /dev/null 2>&1
97	then
98		if test -n "$TORTURE_KCONFIG_GDB_ARG"
99		then
100			:
101		elif test $kruntime -ge $seconds || test -f "$resdir/../STOP.1"
102		then
103			break;
104		fi
105		sleep 1
106	else
107		commandcompleted=1
108		if test $kruntime -lt $seconds
109		then
110			echo Completed in $kruntime vs. $seconds >> $resdir/Warnings 2>&1
111			grep "^(qemu) qemu:" $resdir/kvm-test-1-run*.sh.out >> $resdir/Warnings 2>&1
112			killpid="`sed -n "s/^(qemu) qemu: terminating on signal [0-9]* from pid \([0-9]*\).*$/\1/p" $resdir/Warnings`"
113			if test -n "$killpid"
114			then
115				echo "ps -fp $killpid" >> $resdir/Warnings 2>&1
116				ps -fp $killpid >> $resdir/Warnings 2>&1
117			fi
118		else
119			echo ' ---' `date`: "Kernel done"
120		fi
121		break
122	fi
123done
124if test -z "$qemu_pid" && test -s "$resdir/qemu-pid"
125then
126	qemu_pid=`cat "$resdir/qemu-pid"`
127fi
128if test $commandcompleted -eq 0 && test -n "$qemu_pid"
129then
130	if ! test -f "$resdir/../STOP.1"
131	then
132		echo Grace period for qemu job at pid $qemu_pid `date`
133	fi
134	oldline="`tail $resdir/console.log`"
135	while :
136	do
137		if test -f "$resdir/../STOP.1"
138		then
139			echo "PID $qemu_pid killed due to run STOP.1 request `date`" >> $resdir/Warnings 2>&1
140			kill -KILL $qemu_pid
141			break
142		fi
143		kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
144		if kill -0 $qemu_pid > /dev/null 2>&1
145		then
146			:
147		else
148			break
149		fi
150		must_continue=no
151		newline="`tail $resdir/console.log`"
152		if test "$newline" != "$oldline" && echo $newline | grep -q ' [0-9]\+us : '
153		then
154			must_continue=yes
155		fi
156		last_ts="`tail $resdir/console.log | grep '^\[ *[0-9]\+\.[0-9]\+]' | tail -1 | sed -e 's/^\[ *//' -e 's/\..*$//'`"
157		if test -z "$last_ts"
158		then
159			last_ts=0
160		fi
161		if test "$newline" != "$oldline" && test "$last_ts" -lt $((seconds + $TORTURE_SHUTDOWN_GRACE)) && test "$last_ts" -gt "$TORTURE_SHUTDOWN_GRACE"
162		then
163			must_continue=yes
164			if test $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE))
165			then
166				echo Continuing at console.log time $last_ts \"`tail -n 1 $resdir/console.log`\" `date`
167			fi
168		fi
169		if test $must_continue = no && test $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE))
170		then
171			echo "!!! PID $qemu_pid hung at $kruntime vs. $seconds seconds `date`" >> $resdir/Warnings 2>&1
172			kill -KILL $qemu_pid
173			break
174		fi
175		oldline=$newline
176		sleep 10
177	done
178elif test -z "$qemu_pid"
179then
180	echo Unknown PID, cannot kill qemu command
181fi
182
183# Tell the script that this run is done.
184rm -f $resdir/build.run
185