• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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=${TMPDIR-/tmp}/kvm-test-1-run-qemu.sh.$$
21trap 'rm -rf $T' 0
22mkdir $T
23
24resdir="$1"
25if ! test -d "$resdir"
26then
27	echo $0: Nonexistent directory: $resdir
28	exit 1
29fi
30if ! test -f "$resdir/qemu-cmd"
31then
32	echo $0: Nonexistent qemu-cmd file: $resdir/qemu-cmd
33	exit 1
34fi
35
36echo ' ---' `date`: Starting kernel, PID $$
37
38# Obtain settings from the qemu-cmd file.
39grep '^#' $resdir/qemu-cmd | sed -e 's/^# //' > $T/qemu-cmd-settings
40. $T/qemu-cmd-settings
41
42# Decorate qemu-cmd with affinity, redirection, backgrounding, and PID capture
43taskset_command=
44if test -n "$TORTURE_AFFINITY"
45then
46	taskset_command="taskset -c $TORTURE_AFFINITY "
47fi
48sed -e 's/^[^#].*$/'"$taskset_command"'& 2>\&1 \&/' < $resdir/qemu-cmd > $T/qemu-cmd
49echo 'qemu_pid=$!' >> $T/qemu-cmd
50echo 'echo $qemu_pid > $resdir/qemu-pid' >> $T/qemu-cmd
51echo 'taskset -c -p $qemu_pid > $resdir/qemu-affinity' >> $T/qemu-cmd
52
53# In case qemu refuses to run...
54echo "NOTE: $QEMU either did not run or was interactive" > $resdir/console.log
55
56# Attempt to run qemu
57kstarttime=`gawk 'BEGIN { print systime() }' < /dev/null`
58( . $T/qemu-cmd; wait `cat  $resdir/qemu-pid`; echo $? > $resdir/qemu-retval ) &
59commandcompleted=0
60if test -z "$TORTURE_KCONFIG_GDB_ARG"
61then
62	sleep 10 # Give qemu's pid a chance to reach the file
63	if test -s "$resdir/qemu-pid"
64	then
65		qemu_pid=`cat "$resdir/qemu-pid"`
66		echo Monitoring qemu job at pid $qemu_pid `date`
67	else
68		qemu_pid=""
69		echo Monitoring qemu job at yet-as-unknown pid `date`
70	fi
71fi
72if test -n "$TORTURE_KCONFIG_GDB_ARG"
73then
74	base_resdir=`echo $resdir | sed -e 's/\.[0-9]\+$//'`
75	if ! test -f $base_resdir/vmlinux
76	then
77		base_resdir="`cat re-run`/$resdir"
78		if ! test -f $base_resdir/vmlinux
79		then
80			base_resdir=/path/to
81		fi
82	fi
83	echo Waiting for you to attach a debug session, for example: > /dev/tty
84	echo "    gdb $base_resdir/vmlinux" > /dev/tty
85	echo 'After symbols load and the "(gdb)" prompt appears:' > /dev/tty
86	echo "    target remote :1234" > /dev/tty
87	echo "    continue" > /dev/tty
88	kstarttime=`gawk 'BEGIN { print systime() }' < /dev/null`
89fi
90while :
91do
92	if test -z "$qemu_pid" && test -s "$resdir/qemu-pid"
93	then
94		qemu_pid=`cat "$resdir/qemu-pid"`
95	fi
96	kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
97	if test -z "$qemu_pid" || kill -0 "$qemu_pid" > /dev/null 2>&1
98	then
99		if test -n "$TORTURE_KCONFIG_GDB_ARG"
100		then
101			:
102		elif test $kruntime -ge $seconds || test -f "$resdir/../STOP.1"
103		then
104			break;
105		fi
106		sleep 1
107	else
108		commandcompleted=1
109		if test $kruntime -lt $seconds
110		then
111			echo Completed in $kruntime vs. $seconds >> $resdir/Warnings 2>&1
112			grep "^(qemu) qemu:" $resdir/kvm-test-1-run.sh.out >> $resdir/Warnings 2>&1
113			killpid="`sed -n "s/^(qemu) qemu: terminating on signal [0-9]* from pid \([0-9]*\).*$/\1/p" $resdir/Warnings`"
114			if test -n "$killpid"
115			then
116				echo "ps -fp $killpid" >> $resdir/Warnings 2>&1
117				ps -fp $killpid >> $resdir/Warnings 2>&1
118			fi
119		else
120			echo ' ---' `date`: "Kernel done"
121		fi
122		break
123	fi
124done
125if test -z "$qemu_pid" && test -s "$resdir/qemu-pid"
126then
127	qemu_pid=`cat "$resdir/qemu-pid"`
128fi
129if test $commandcompleted -eq 0 && test -n "$qemu_pid"
130then
131	if ! test -f "$resdir/../STOP.1"
132	then
133		echo Grace period for qemu job at pid $qemu_pid `date`
134	fi
135	oldline="`tail $resdir/console.log`"
136	while :
137	do
138		if test -f "$resdir/../STOP.1"
139		then
140			echo "PID $qemu_pid killed due to run STOP.1 request `date`" >> $resdir/Warnings 2>&1
141			kill -KILL $qemu_pid
142			break
143		fi
144		kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
145		if kill -0 $qemu_pid > /dev/null 2>&1
146		then
147			:
148		else
149			break
150		fi
151		must_continue=no
152		newline="`tail $resdir/console.log`"
153		if test "$newline" != "$oldline" && echo $newline | grep -q ' [0-9]\+us : '
154		then
155			must_continue=yes
156		fi
157		last_ts="`tail $resdir/console.log | grep '^\[ *[0-9]\+\.[0-9]\+]' | tail -1 | sed -e 's/^\[ *//' -e 's/\..*$//'`"
158		if test -z "$last_ts"
159		then
160			last_ts=0
161		fi
162		if test "$newline" != "$oldline" && test "$last_ts" -lt $((seconds + $TORTURE_SHUTDOWN_GRACE)) && test "$last_ts" -gt "$TORTURE_SHUTDOWN_GRACE"
163		then
164			must_continue=yes
165			if test $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE))
166			then
167				echo Continuing at console.log time $last_ts \"`tail -n 1 $resdir/console.log`\" `date`
168			fi
169		fi
170		if test $must_continue = no && test $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE))
171		then
172			echo "!!! PID $qemu_pid hung at $kruntime vs. $seconds seconds `date`" >> $resdir/Warnings 2>&1
173			kill -KILL $qemu_pid
174			break
175		fi
176		oldline=$newline
177		sleep 10
178	done
179elif test -z "$qemu_pid"
180then
181	echo Unknown PID, cannot kill qemu command
182fi
183
184# Tell the script that this run is done.
185rm -f $resdir/build.run
186