1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0+
3#
4# Rerun a series of tests under KVM.
5#
6# Usage: kvm-again.sh /path/to/old/run [ options ]
7#
8# Copyright (C) 2021 Facebook, Inc.
9#
10# Authors: Paul E. McKenney <paulmck@kernel.org>
11
12scriptname=$0
13args="$*"
14
15T=${TMPDIR-/tmp}/kvm-again.sh.$$
16trap 'rm -rf $T' 0
17mkdir $T
18
19if ! test -d tools/testing/selftests/rcutorture/bin
20then
21	echo $scriptname must be run from top-level directory of kernel source tree.
22	exit 1
23fi
24
25oldrun=$1
26shift
27if ! test -d "$oldrun"
28then
29	echo "Usage: $scriptname /path/to/old/run [ options ]"
30	exit 1
31fi
32if ! cp "$oldrun/scenarios" $T/scenarios.oldrun
33then
34	# Later on, can reconstitute this from console.log files.
35	echo Prior run batches file does not exist: $oldrun/batches
36	exit 1
37fi
38
39if test -f "$oldrun/torture_suite"
40then
41	torture_suite="`cat $oldrun/torture_suite`"
42elif test -f "$oldrun/TORTURE_SUITE"
43then
44	torture_suite="`cat $oldrun/TORTURE_SUITE`"
45else
46	echo "Prior run torture_suite file does not exist: $oldrun/{torture_suite,TORTURE_SUITE}"
47	exit 1
48fi
49
50KVM="`pwd`/tools/testing/selftests/rcutorture"; export KVM
51PATH=${KVM}/bin:$PATH; export PATH
52. functions.sh
53
54dryrun=
55dur=
56default_link="cp -R"
57rundir="`pwd`/tools/testing/selftests/rcutorture/res/`date +%Y.%m.%d-%H.%M.%S-again`"
58
59startdate="`date`"
60starttime="`get_starttime`"
61
62usage () {
63	echo "Usage: $scriptname $oldrun [ arguments ]:"
64	echo "       --dryrun"
65	echo "       --duration minutes | <seconds>s | <hours>h | <days>d"
66	echo "       --link hard|soft|copy"
67	echo "       --remote"
68	echo "       --rundir /new/res/path"
69	exit 1
70}
71
72while test $# -gt 0
73do
74	case "$1" in
75	--dryrun)
76		dryrun=1
77		;;
78	--duration)
79		checkarg --duration "(minutes)" $# "$2" '^[0-9][0-9]*\(s\|m\|h\|d\|\)$' '^error'
80		mult=60
81		if echo "$2" | grep -q 's$'
82		then
83			mult=1
84		elif echo "$2" | grep -q 'h$'
85		then
86			mult=3600
87		elif echo "$2" | grep -q 'd$'
88		then
89			mult=86400
90		fi
91		ts=`echo $2 | sed -e 's/[smhd]$//'`
92		dur=$(($ts*mult))
93		shift
94		;;
95	--link)
96		checkarg --link "hard|soft|copy" "$#" "$2" 'hard\|soft\|copy' '^--'
97		case "$2" in
98		copy)
99			arg_link="cp -R"
100			;;
101		hard)
102			arg_link="cp -Rl"
103			;;
104		soft)
105			arg_link="cp -Rs"
106			;;
107		esac
108		shift
109		;;
110	--remote)
111		arg_remote=1
112		default_link="cp -as"
113		;;
114	--rundir)
115		checkarg --rundir "(absolute pathname)" "$#" "$2" '^/' '^error'
116		rundir=$2
117		if test -e "$rundir"
118		then
119			echo "--rundir $2: Already exists."
120			usage
121		fi
122		shift
123		;;
124	*)
125		echo Unknown argument $1
126		usage
127		;;
128	esac
129	shift
130done
131if test -z "$arg_link"
132then
133	arg_link="$default_link"
134fi
135
136echo ---- Re-run results directory: $rundir
137
138# Copy old run directory tree over and adjust.
139mkdir -p "`dirname "$rundir"`"
140if ! $arg_link "$oldrun" "$rundir"
141then
142	echo "Cannot copy from $oldrun to $rundir."
143	usage
144fi
145rm -f "$rundir"/*/{console.log,console.log.diags,qemu_pid,qemu-pid,qemu-retval,Warnings,kvm-test-1-run.sh.out,kvm-test-1-run-qemu.sh.out,vmlinux} "$rundir"/log
146touch "$rundir/log"
147echo $scriptname $args | tee -a "$rundir/log"
148echo $oldrun > "$rundir/re-run"
149if ! test -d "$rundir/../../bin"
150then
151	$arg_link "$oldrun/../../bin" "$rundir/../.."
152fi
153for i in $rundir/*/qemu-cmd
154do
155	cp "$i" $T
156	qemu_cmd_dir="`dirname "$i"`"
157	kernel_dir="`echo $qemu_cmd_dir | sed -e 's/\.[0-9]\+$//'`"
158	jitter_dir="`dirname "$kernel_dir"`"
159	kvm-transform.sh "$kernel_dir/bzImage" "$qemu_cmd_dir/console.log" "$jitter_dir" $dur < $T/qemu-cmd > $i
160	if test -n "$arg_remote"
161	then
162		echo "# TORTURE_KCONFIG_GDB_ARG=''" >> $i
163	fi
164done
165
166# Extract settings from the last qemu-cmd file transformed above.
167grep '^#' $i | sed -e 's/^# //' > $T/qemu-cmd-settings
168. $T/qemu-cmd-settings
169
170grep -v '^#' $T/scenarios.oldrun | awk '
171{
172	curbatch = "";
173	for (i = 2; i <= NF; i++)
174		curbatch = curbatch " " $i;
175	print "kvm-test-1-run-batch.sh" curbatch;
176}' > $T/runbatches.sh
177
178if test -n "$dryrun"
179then
180	echo ---- Dryrun complete, directory: $rundir | tee -a "$rundir/log"
181else
182	( cd "$rundir"; sh $T/runbatches.sh ) | tee -a "$rundir/log"
183	kvm-end-run-stats.sh "$rundir" "$starttime"
184fi
185