2 # SPDX-License-Identifier: GPL-2.0+
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.
7 # Usage: kvm-test-1-run-qemu.sh qemu-cmd-dir
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.
16 # Copyright (C) 2021 Facebook, Inc.
18 # Authors: Paul E. McKenney <paulmck@kernel.org>
20 T=${TMPDIR-/tmp}/kvm-test-1-run-qemu.sh.$$
25 if ! test -d "$resdir"
27 echo $0: Nonexistent directory: $resdir
30 if ! test -f "$resdir/qemu-cmd"
32 echo $0: Nonexistent qemu-cmd file: $resdir/qemu-cmd
36 echo ' ---' `date`: Starting kernel, PID $$
38 # Obtain settings from the qemu-cmd file.
39 grep '^#' $resdir/qemu-cmd | sed -e 's/^# //' > $T/qemu-cmd-settings
40 . $T/qemu-cmd-settings
42 # Decorate qemu-cmd with redirection, backgrounding, and PID capture
43 sed -e 's/$/ 2>\&1 \&/' < $resdir/qemu-cmd > $T/qemu-cmd
44 echo 'echo $! > $resdir/qemu_pid' >> $T/qemu-cmd
46 # In case qemu refuses to run...
47 echo "NOTE: $QEMU either did not run or was interactive" > $resdir/console.log
50 kstarttime=`gawk 'BEGIN { print systime() }' < /dev/null`
51 ( . $T/qemu-cmd; wait `cat $resdir/qemu_pid`; echo $? > $resdir/qemu-retval ) &
53 if test -z "$TORTURE_KCONFIG_GDB_ARG"
55 sleep 10 # Give qemu's pid a chance to reach the file
56 if test -s "$resdir/qemu_pid"
58 qemu_pid=`cat "$resdir/qemu_pid"`
59 echo Monitoring qemu job at pid $qemu_pid
62 echo Monitoring qemu job at yet-as-unknown pid
65 if test -n "$TORTURE_KCONFIG_GDB_ARG"
67 base_resdir=`echo $resdir | sed -e 's/\.[0-9]\+$//'`
68 if ! test -f $base_resdir/vmlinux
70 base_resdir="`cat re-run`/$resdir"
71 if ! test -f $base_resdir/vmlinux
76 echo Waiting for you to attach a debug session, for example: > /dev/tty
77 echo " gdb $base_resdir/vmlinux" > /dev/tty
78 echo 'After symbols load and the "(gdb)" prompt appears:' > /dev/tty
79 echo " target remote :1234" > /dev/tty
80 echo " continue" > /dev/tty
81 kstarttime=`gawk 'BEGIN { print systime() }' < /dev/null`
85 if test -z "$qemu_pid" -a -s "$resdir/qemu_pid"
87 qemu_pid=`cat "$resdir/qemu_pid"`
89 kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
90 if test -z "$qemu_pid" || kill -0 "$qemu_pid" > /dev/null 2>&1
92 if test -n "$TORTURE_KCONFIG_GDB_ARG"
95 elif test $kruntime -ge $seconds || test -f "$resdir/../STOP.1"
102 if test $kruntime -lt $seconds
104 echo Completed in $kruntime vs. $seconds >> $resdir/Warnings 2>&1
105 grep "^(qemu) qemu:" $resdir/kvm-test-1-run.sh.out >> $resdir/Warnings 2>&1
106 killpid="`sed -n "s/^(qemu) qemu: terminating on signal [0-9]* from pid \([0-9]*\).*$/\1/p" $resdir/Warnings`"
107 if test -n "$killpid"
109 echo "ps -fp $killpid" >> $resdir/Warnings 2>&1
110 ps -fp $killpid >> $resdir/Warnings 2>&1
113 echo ' ---' `date`: "Kernel done"
118 if test -z "$qemu_pid" -a -s "$resdir/qemu_pid"
120 qemu_pid=`cat "$resdir/qemu_pid"`
122 if test $commandcompleted -eq 0 -a -n "$qemu_pid"
124 if ! test -f "$resdir/../STOP.1"
126 echo Grace period for qemu job at pid $qemu_pid
128 oldline="`tail $resdir/console.log`"
131 if test -f "$resdir/../STOP.1"
133 echo "PID $qemu_pid killed due to run STOP.1 request" >> $resdir/Warnings 2>&1
137 kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
138 if kill -0 $qemu_pid > /dev/null 2>&1
145 newline="`tail $resdir/console.log`"
146 if test "$newline" != "$oldline" && echo $newline | grep -q ' [0-9]\+us : '
150 last_ts="`tail $resdir/console.log | grep '^\[ *[0-9]\+\.[0-9]\+]' | tail -1 | sed -e 's/^\[ *//' -e 's/\..*$//'`"
151 if test -z "$last_ts"
155 if test "$newline" != "$oldline" -a "$last_ts" -lt $((seconds + $TORTURE_SHUTDOWN_GRACE))
159 if test $must_continue = no -a $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE))
161 echo "!!! PID $qemu_pid hung at $kruntime vs. $seconds seconds" >> $resdir/Warnings 2>&1
168 elif test -z "$qemu_pid"
170 echo Unknown PID, cannot kill qemu command
173 # Tell the script that this run is done.
174 rm -f $resdir/build.run
176 parse-console.sh $resdir/console.log $title