162306a36Sopenharmony_ci#!/bin/bash 262306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0+ 362306a36Sopenharmony_ci# 462306a36Sopenharmony_ci# Carry out a kvm-based run for the specified qemu-cmd file, which might 562306a36Sopenharmony_ci# have been generated by --build-only kvm.sh run. 662306a36Sopenharmony_ci# 762306a36Sopenharmony_ci# Usage: kvm-test-1-run-qemu.sh qemu-cmd-dir 862306a36Sopenharmony_ci# 962306a36Sopenharmony_ci# qemu-cmd-dir provides the directory containing qemu-cmd file. 1062306a36Sopenharmony_ci# This is assumed to be of the form prefix/ds/scenario, where 1162306a36Sopenharmony_ci# "ds" is the top-level date-stamped directory and "scenario" 1262306a36Sopenharmony_ci# is the scenario name. Any required adjustments to this file 1362306a36Sopenharmony_ci# must have been made by the caller. The shell-command comments 1462306a36Sopenharmony_ci# at the end of the qemu-cmd file are not optional. 1562306a36Sopenharmony_ci# 1662306a36Sopenharmony_ci# Copyright (C) 2021 Facebook, Inc. 1762306a36Sopenharmony_ci# 1862306a36Sopenharmony_ci# Authors: Paul E. McKenney <paulmck@kernel.org> 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ciT="`mktemp -d ${TMPDIR-/tmp}/kvm-test-1-run-qemu.sh.XXXXXX`" 2162306a36Sopenharmony_citrap 'rm -rf $T' 0 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ciresdir="$1" 2462306a36Sopenharmony_ciif ! test -d "$resdir" 2562306a36Sopenharmony_cithen 2662306a36Sopenharmony_ci echo $0: Nonexistent directory: $resdir 2762306a36Sopenharmony_ci exit 1 2862306a36Sopenharmony_cifi 2962306a36Sopenharmony_ciif ! test -f "$resdir/qemu-cmd" 3062306a36Sopenharmony_cithen 3162306a36Sopenharmony_ci echo $0: Nonexistent qemu-cmd file: $resdir/qemu-cmd 3262306a36Sopenharmony_ci exit 1 3362306a36Sopenharmony_cifi 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ciecho ' ---' `date`: Starting kernel, PID $$ 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci# Obtain settings from the qemu-cmd file. 3862306a36Sopenharmony_cigrep '^#' $resdir/qemu-cmd | sed -e 's/^# //' > $T/qemu-cmd-settings 3962306a36Sopenharmony_ci. $T/qemu-cmd-settings 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci# Decorate qemu-cmd with affinity, redirection, backgrounding, and PID capture 4262306a36Sopenharmony_citaskset_command= 4362306a36Sopenharmony_ciif test -n "$TORTURE_AFFINITY" 4462306a36Sopenharmony_cithen 4562306a36Sopenharmony_ci taskset_command="taskset -c $TORTURE_AFFINITY " 4662306a36Sopenharmony_cifi 4762306a36Sopenharmony_cised -e 's/^[^#].*$/'"$taskset_command"'& 2>\&1 \&/' < $resdir/qemu-cmd > $T/qemu-cmd 4862306a36Sopenharmony_ciecho 'qemu_pid=$!' >> $T/qemu-cmd 4962306a36Sopenharmony_ciecho 'echo $qemu_pid > $resdir/qemu-pid' >> $T/qemu-cmd 5062306a36Sopenharmony_ciecho 'taskset -c -p $qemu_pid > $resdir/qemu-affinity' >> $T/qemu-cmd 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci# In case qemu refuses to run... 5362306a36Sopenharmony_ciecho "NOTE: $QEMU either did not run or was interactive" > $resdir/console.log 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci# Attempt to run qemu 5662306a36Sopenharmony_cikstarttime=`gawk 'BEGIN { print systime() }' < /dev/null` 5762306a36Sopenharmony_ci( . $T/qemu-cmd; wait `cat $resdir/qemu-pid`; echo $? > $resdir/qemu-retval ) & 5862306a36Sopenharmony_cicommandcompleted=0 5962306a36Sopenharmony_ciif test -z "$TORTURE_KCONFIG_GDB_ARG" 6062306a36Sopenharmony_cithen 6162306a36Sopenharmony_ci sleep 10 # Give qemu's pid a chance to reach the file 6262306a36Sopenharmony_ci if test -s "$resdir/qemu-pid" 6362306a36Sopenharmony_ci then 6462306a36Sopenharmony_ci qemu_pid=`cat "$resdir/qemu-pid"` 6562306a36Sopenharmony_ci echo Monitoring qemu job at pid $qemu_pid `date` 6662306a36Sopenharmony_ci else 6762306a36Sopenharmony_ci qemu_pid="" 6862306a36Sopenharmony_ci echo Monitoring qemu job at yet-as-unknown pid `date` 6962306a36Sopenharmony_ci fi 7062306a36Sopenharmony_cifi 7162306a36Sopenharmony_ciif test -n "$TORTURE_KCONFIG_GDB_ARG" 7262306a36Sopenharmony_cithen 7362306a36Sopenharmony_ci base_resdir=`echo $resdir | sed -e 's/\.[0-9]\+$//'` 7462306a36Sopenharmony_ci if ! test -f $base_resdir/vmlinux 7562306a36Sopenharmony_ci then 7662306a36Sopenharmony_ci base_resdir="`cat re-run`/$resdir" 7762306a36Sopenharmony_ci if ! test -f $base_resdir/vmlinux 7862306a36Sopenharmony_ci then 7962306a36Sopenharmony_ci base_resdir=/path/to 8062306a36Sopenharmony_ci fi 8162306a36Sopenharmony_ci fi 8262306a36Sopenharmony_ci echo Waiting for you to attach a debug session, for example: > /dev/tty 8362306a36Sopenharmony_ci echo " gdb $base_resdir/vmlinux" > /dev/tty 8462306a36Sopenharmony_ci echo 'After symbols load and the "(gdb)" prompt appears:' > /dev/tty 8562306a36Sopenharmony_ci echo " target remote :1234" > /dev/tty 8662306a36Sopenharmony_ci echo " continue" > /dev/tty 8762306a36Sopenharmony_ci kstarttime=`gawk 'BEGIN { print systime() }' < /dev/null` 8862306a36Sopenharmony_cifi 8962306a36Sopenharmony_ciwhile : 9062306a36Sopenharmony_cido 9162306a36Sopenharmony_ci if test -z "$qemu_pid" && test -s "$resdir/qemu-pid" 9262306a36Sopenharmony_ci then 9362306a36Sopenharmony_ci qemu_pid=`cat "$resdir/qemu-pid"` 9462306a36Sopenharmony_ci fi 9562306a36Sopenharmony_ci kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null` 9662306a36Sopenharmony_ci if test -z "$qemu_pid" || kill -0 "$qemu_pid" > /dev/null 2>&1 9762306a36Sopenharmony_ci then 9862306a36Sopenharmony_ci if test -n "$TORTURE_KCONFIG_GDB_ARG" 9962306a36Sopenharmony_ci then 10062306a36Sopenharmony_ci : 10162306a36Sopenharmony_ci elif test $kruntime -ge $seconds || test -f "$resdir/../STOP.1" 10262306a36Sopenharmony_ci then 10362306a36Sopenharmony_ci break; 10462306a36Sopenharmony_ci fi 10562306a36Sopenharmony_ci sleep 1 10662306a36Sopenharmony_ci else 10762306a36Sopenharmony_ci commandcompleted=1 10862306a36Sopenharmony_ci if test $kruntime -lt $seconds 10962306a36Sopenharmony_ci then 11062306a36Sopenharmony_ci echo Completed in $kruntime vs. $seconds >> $resdir/Warnings 2>&1 11162306a36Sopenharmony_ci grep "^(qemu) qemu:" $resdir/kvm-test-1-run*.sh.out >> $resdir/Warnings 2>&1 11262306a36Sopenharmony_ci killpid="`sed -n "s/^(qemu) qemu: terminating on signal [0-9]* from pid \([0-9]*\).*$/\1/p" $resdir/Warnings`" 11362306a36Sopenharmony_ci if test -n "$killpid" 11462306a36Sopenharmony_ci then 11562306a36Sopenharmony_ci echo "ps -fp $killpid" >> $resdir/Warnings 2>&1 11662306a36Sopenharmony_ci ps -fp $killpid >> $resdir/Warnings 2>&1 11762306a36Sopenharmony_ci fi 11862306a36Sopenharmony_ci else 11962306a36Sopenharmony_ci echo ' ---' `date`: "Kernel done" 12062306a36Sopenharmony_ci fi 12162306a36Sopenharmony_ci break 12262306a36Sopenharmony_ci fi 12362306a36Sopenharmony_cidone 12462306a36Sopenharmony_ciif test -z "$qemu_pid" && test -s "$resdir/qemu-pid" 12562306a36Sopenharmony_cithen 12662306a36Sopenharmony_ci qemu_pid=`cat "$resdir/qemu-pid"` 12762306a36Sopenharmony_cifi 12862306a36Sopenharmony_ciif test $commandcompleted -eq 0 && test -n "$qemu_pid" 12962306a36Sopenharmony_cithen 13062306a36Sopenharmony_ci if ! test -f "$resdir/../STOP.1" 13162306a36Sopenharmony_ci then 13262306a36Sopenharmony_ci echo Grace period for qemu job at pid $qemu_pid `date` 13362306a36Sopenharmony_ci fi 13462306a36Sopenharmony_ci oldline="`tail $resdir/console.log`" 13562306a36Sopenharmony_ci while : 13662306a36Sopenharmony_ci do 13762306a36Sopenharmony_ci if test -f "$resdir/../STOP.1" 13862306a36Sopenharmony_ci then 13962306a36Sopenharmony_ci echo "PID $qemu_pid killed due to run STOP.1 request `date`" >> $resdir/Warnings 2>&1 14062306a36Sopenharmony_ci kill -KILL $qemu_pid 14162306a36Sopenharmony_ci break 14262306a36Sopenharmony_ci fi 14362306a36Sopenharmony_ci kruntime=`gawk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null` 14462306a36Sopenharmony_ci if kill -0 $qemu_pid > /dev/null 2>&1 14562306a36Sopenharmony_ci then 14662306a36Sopenharmony_ci : 14762306a36Sopenharmony_ci else 14862306a36Sopenharmony_ci break 14962306a36Sopenharmony_ci fi 15062306a36Sopenharmony_ci must_continue=no 15162306a36Sopenharmony_ci newline="`tail $resdir/console.log`" 15262306a36Sopenharmony_ci if test "$newline" != "$oldline" && echo $newline | grep -q ' [0-9]\+us : ' 15362306a36Sopenharmony_ci then 15462306a36Sopenharmony_ci must_continue=yes 15562306a36Sopenharmony_ci fi 15662306a36Sopenharmony_ci last_ts="`tail $resdir/console.log | grep '^\[ *[0-9]\+\.[0-9]\+]' | tail -1 | sed -e 's/^\[ *//' -e 's/\..*$//'`" 15762306a36Sopenharmony_ci if test -z "$last_ts" 15862306a36Sopenharmony_ci then 15962306a36Sopenharmony_ci last_ts=0 16062306a36Sopenharmony_ci fi 16162306a36Sopenharmony_ci if test "$newline" != "$oldline" && test "$last_ts" -lt $((seconds + $TORTURE_SHUTDOWN_GRACE)) && test "$last_ts" -gt "$TORTURE_SHUTDOWN_GRACE" 16262306a36Sopenharmony_ci then 16362306a36Sopenharmony_ci must_continue=yes 16462306a36Sopenharmony_ci if test $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE)) 16562306a36Sopenharmony_ci then 16662306a36Sopenharmony_ci echo Continuing at console.log time $last_ts \"`tail -n 1 $resdir/console.log`\" `date` 16762306a36Sopenharmony_ci fi 16862306a36Sopenharmony_ci fi 16962306a36Sopenharmony_ci if test $must_continue = no && test $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE)) 17062306a36Sopenharmony_ci then 17162306a36Sopenharmony_ci echo "!!! PID $qemu_pid hung at $kruntime vs. $seconds seconds `date`" >> $resdir/Warnings 2>&1 17262306a36Sopenharmony_ci kill -KILL $qemu_pid 17362306a36Sopenharmony_ci break 17462306a36Sopenharmony_ci fi 17562306a36Sopenharmony_ci oldline=$newline 17662306a36Sopenharmony_ci sleep 10 17762306a36Sopenharmony_ci done 17862306a36Sopenharmony_cielif test -z "$qemu_pid" 17962306a36Sopenharmony_cithen 18062306a36Sopenharmony_ci echo Unknown PID, cannot kill qemu command 18162306a36Sopenharmony_cifi 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ci# Tell the script that this run is done. 18462306a36Sopenharmony_cirm -f $resdir/build.run 185