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 redirection, backgrounding, and PID capture 43sed -e 's/$/ 2>\&1 \&/' < $resdir/qemu-cmd > $T/qemu-cmd 44echo 'echo $! > $resdir/qemu_pid' >> $T/qemu-cmd 45 46# In case qemu refuses to run... 47echo "NOTE: $QEMU either did not run or was interactive" > $resdir/console.log 48 49# Attempt to run qemu 50kstarttime=`gawk 'BEGIN { print systime() }' < /dev/null` 51( . $T/qemu-cmd; wait `cat $resdir/qemu_pid`; echo $? > $resdir/qemu-retval ) & 52commandcompleted=0 53if test -z "$TORTURE_KCONFIG_GDB_ARG" 54then 55 sleep 10 # Give qemu's pid a chance to reach the file 56 if test -s "$resdir/qemu_pid" 57 then 58 qemu_pid=`cat "$resdir/qemu_pid"` 59 echo Monitoring qemu job at pid $qemu_pid 60 else 61 qemu_pid="" 62 echo Monitoring qemu job at yet-as-unknown pid 63 fi 64fi 65if test -n "$TORTURE_KCONFIG_GDB_ARG" 66then 67 base_resdir=`echo $resdir | sed -e 's/\.[0-9]\+$//'` 68 if ! test -f $base_resdir/vmlinux 69 then 70 base_resdir="`cat re-run`/$resdir" 71 if ! test -f $base_resdir/vmlinux 72 then 73 base_resdir=/path/to 74 fi 75 fi 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` 82fi 83while : 84do 85 if test -z "$qemu_pid" -a -s "$resdir/qemu_pid" 86 then 87 qemu_pid=`cat "$resdir/qemu_pid"` 88 fi 89 kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null` 90 if test -z "$qemu_pid" || kill -0 "$qemu_pid" > /dev/null 2>&1 91 then 92 if test -n "$TORTURE_KCONFIG_GDB_ARG" 93 then 94 : 95 elif test $kruntime -ge $seconds || test -f "$resdir/../STOP.1" 96 then 97 break; 98 fi 99 sleep 1 100 else 101 commandcompleted=1 102 if test $kruntime -lt $seconds 103 then 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" 108 then 109 echo "ps -fp $killpid" >> $resdir/Warnings 2>&1 110 ps -fp $killpid >> $resdir/Warnings 2>&1 111 fi 112 else 113 echo ' ---' `date`: "Kernel done" 114 fi 115 break 116 fi 117done 118if test -z "$qemu_pid" -a -s "$resdir/qemu_pid" 119then 120 qemu_pid=`cat "$resdir/qemu_pid"` 121fi 122if test $commandcompleted -eq 0 -a -n "$qemu_pid" 123then 124 if ! test -f "$resdir/../STOP.1" 125 then 126 echo Grace period for qemu job at pid $qemu_pid 127 fi 128 oldline="`tail $resdir/console.log`" 129 while : 130 do 131 if test -f "$resdir/../STOP.1" 132 then 133 echo "PID $qemu_pid killed due to run STOP.1 request" >> $resdir/Warnings 2>&1 134 kill -KILL $qemu_pid 135 break 136 fi 137 kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null` 138 if kill -0 $qemu_pid > /dev/null 2>&1 139 then 140 : 141 else 142 break 143 fi 144 must_continue=no 145 newline="`tail $resdir/console.log`" 146 if test "$newline" != "$oldline" && echo $newline | grep -q ' [0-9]\+us : ' 147 then 148 must_continue=yes 149 fi 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" 152 then 153 last_ts=0 154 fi 155 if test "$newline" != "$oldline" -a "$last_ts" -lt $((seconds + $TORTURE_SHUTDOWN_GRACE)) 156 then 157 must_continue=yes 158 fi 159 if test $must_continue = no -a $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE)) 160 then 161 echo "!!! PID $qemu_pid hung at $kruntime vs. $seconds seconds" >> $resdir/Warnings 2>&1 162 kill -KILL $qemu_pid 163 break 164 fi 165 oldline=$newline 166 sleep 10 167 done 168elif test -z "$qemu_pid" 169then 170 echo Unknown PID, cannot kill qemu command 171fi 172 173# Tell the script that this run is done. 174rm -f $resdir/build.run 175 176parse-console.sh $resdir/console.log $title 177