185 lines · bash
1#!/bin/bash2# SPDX-License-Identifier: GPL-2.0+3#4# Carry out a kvm-based run for the specified qemu-cmd file, which might5# have been generated by --build-only kvm.sh run.6#7# Usage: kvm-test-1-run-qemu.sh qemu-cmd-dir8#9# qemu-cmd-dir provides the directory containing qemu-cmd file.10# This is assumed to be of the form prefix/ds/scenario, where11# "ds" is the top-level date-stamped directory and "scenario"12# is the scenario name. Any required adjustments to this file13# must have been made by the caller. The shell-command comments14# 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' 022 23resdir="$1"24if ! test -d "$resdir"25then26 echo $0: Nonexistent directory: $resdir27 exit 128fi29if ! test -f "$resdir/qemu-cmd"30then31 echo $0: Nonexistent qemu-cmd file: $resdir/qemu-cmd32 exit 133fi34 35echo ' ---' `date`: Starting kernel, PID $$36 37# Obtain settings from the qemu-cmd file.38grep '^#' $resdir/qemu-cmd | sed -e 's/^# //' > $T/qemu-cmd-settings39. $T/qemu-cmd-settings40 41# Decorate qemu-cmd with affinity, redirection, backgrounding, and PID capture42taskset_command=43if test -n "$TORTURE_AFFINITY"44then45 taskset_command="taskset -c $TORTURE_AFFINITY "46fi47sed -e 's/^[^#].*$/'"$taskset_command"'& 2>\&1 \&/' < $resdir/qemu-cmd > $T/qemu-cmd48echo 'qemu_pid=$!' >> $T/qemu-cmd49echo 'echo $qemu_pid > $resdir/qemu-pid' >> $T/qemu-cmd50echo 'taskset -c -p $qemu_pid > $resdir/qemu-affinity' >> $T/qemu-cmd51 52# In case qemu refuses to run...53echo "NOTE: $QEMU either did not run or was interactive" > $resdir/console.log54 55# Attempt to run qemu56kstarttime=`gawk 'BEGIN { print systime() }' < /dev/null`57( . $T/qemu-cmd; wait `cat $resdir/qemu-pid`; echo $? > $resdir/qemu-retval ) &58commandcompleted=059if test -z "$TORTURE_KCONFIG_GDB_ARG"60then61 sleep 10 # Give qemu's pid a chance to reach the file62 if test -s "$resdir/qemu-pid"63 then64 qemu_pid=`cat "$resdir/qemu-pid"`65 echo Monitoring qemu job at pid $qemu_pid `date`66 else67 qemu_pid=""68 echo Monitoring qemu job at yet-as-unknown pid `date`69 fi70fi71if test -n "$TORTURE_KCONFIG_GDB_ARG"72then73 base_resdir=`echo $resdir | sed -e 's/\.[0-9]\+$//'`74 if ! test -f $base_resdir/vmlinux75 then76 base_resdir="`cat re-run`/$resdir"77 if ! test -f $base_resdir/vmlinux78 then79 base_resdir=/path/to80 fi81 fi82 echo Waiting for you to attach a debug session, for example: > /dev/tty83 echo " gdb $base_resdir/vmlinux" > /dev/tty84 echo 'After symbols load and the "(gdb)" prompt appears:' > /dev/tty85 echo " target remote :1234" > /dev/tty86 echo " continue" > /dev/tty87 kstarttime=`gawk 'BEGIN { print systime() }' < /dev/null`88fi89while :90do91 if test -z "$qemu_pid" && test -s "$resdir/qemu-pid"92 then93 qemu_pid=`cat "$resdir/qemu-pid"`94 fi95 kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`96 if test -z "$qemu_pid" || kill -0 "$qemu_pid" > /dev/null 2>&197 then98 if test -n "$TORTURE_KCONFIG_GDB_ARG"99 then100 :101 elif test $kruntime -ge $seconds || test -f "$resdir/../STOP.1"102 then103 break;104 fi105 sleep 1106 else107 commandcompleted=1108 if test $kruntime -lt $seconds109 then110 echo Completed in $kruntime vs. $seconds >> $resdir/Warnings 2>&1111 grep "^(qemu) qemu:" $resdir/kvm-test-1-run*.sh.out >> $resdir/Warnings 2>&1112 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 then115 echo "ps -fp $killpid" >> $resdir/Warnings 2>&1116 ps -fp $killpid >> $resdir/Warnings 2>&1117 fi118 else119 echo ' ---' `date`: "Kernel done"120 fi121 break122 fi123done124if test -z "$qemu_pid" && test -s "$resdir/qemu-pid"125then126 qemu_pid=`cat "$resdir/qemu-pid"`127fi128if test $commandcompleted -eq 0 && test -n "$qemu_pid"129then130 if ! test -f "$resdir/../STOP.1"131 then132 echo Grace period for qemu job at pid $qemu_pid `date`133 fi134 oldline="`tail $resdir/console.log`"135 while :136 do137 if test -f "$resdir/../STOP.1"138 then139 echo "PID $qemu_pid killed due to run STOP.1 request `date`" >> $resdir/Warnings 2>&1140 kill -KILL $qemu_pid141 break142 fi143 kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`144 if kill -0 $qemu_pid > /dev/null 2>&1145 then146 :147 else148 break149 fi150 must_continue=no151 newline="`tail $resdir/console.log`"152 if test "$newline" != "$oldline" && echo $newline | grep -q ' [0-9]\+us : '153 then154 must_continue=yes155 fi156 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 then159 last_ts=0160 fi161 if test "$newline" != "$oldline" && test "$last_ts" -lt $((seconds + $TORTURE_SHUTDOWN_GRACE)) && test "$last_ts" -gt "$TORTURE_SHUTDOWN_GRACE"162 then163 must_continue=yes164 if test $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE))165 then166 echo Continuing at console.log time $last_ts \"`tail -n 1 $resdir/console.log`\" `date`167 fi168 fi169 if test $must_continue = no && test $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE))170 then171 echo "!!! PID $qemu_pid hung at $kruntime vs. $seconds seconds `date`" >> $resdir/Warnings 2>&1172 kill -KILL $qemu_pid173 break174 fi175 oldline=$newline176 sleep 10177 done178elif test -z "$qemu_pid"179then180 echo Unknown PID, cannot kill qemu command181fi182 183# Tell the script that this run is done.184rm -f $resdir/build.run185