1#!/bin/bash
2#
3# Run a series of 14 tests under KVM.  These are not particularly
4# well-selected or well-tuned, but are the current set.  Run from the
5# top level of the source tree.
6#
7# Edit the definitions below to set the locations of the various directories,
8# as well as the test duration.
9#
10# Usage: sh kvm.sh [ options ]
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, you can access it online at
24# http://www.gnu.org/licenses/gpl-2.0.html.
25#
26# Copyright (C) IBM Corporation, 2011
27#
28# Authors: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
29
30scriptname=$0
31args="$*"
32
33T=/tmp/kvm.sh.$$
34trap 'rm -rf $T' 0
35mkdir $T
36
37dur=30
38dryrun=""
39KVM="`pwd`/tools/testing/selftests/rcutorture"; export KVM
40PATH=${KVM}/bin:$PATH; export PATH
41builddir="${KVM}/b1"
42RCU_INITRD="$KVM/initrd"; export RCU_INITRD
43RCU_KMAKE_ARG=""; export RCU_KMAKE_ARG
44TORTURE_SUITE=rcu
45resdir=""
46configs=""
47cpus=0
48ds=`date +%Y.%m.%d-%H:%M:%S`
49kversion=""
50
51. functions.sh
52
53usage () {
54	echo "Usage: $scriptname optional arguments:"
55	echo "       --bootargs kernel-boot-arguments"
56	echo "       --builddir absolute-pathname"
57	echo "       --buildonly"
58	echo "       --configs \"config-file list\""
59	echo "       --cpus N"
60	echo "       --datestamp string"
61	echo "       --dryrun sched|script"
62	echo "       --duration minutes"
63	echo "       --interactive"
64	echo "       --kmake-arg kernel-make-arguments"
65	echo "       --kversion vN.NN"
66	echo "       --mac nn:nn:nn:nn:nn:nn"
67	echo "       --no-initrd"
68	echo "       --qemu-args qemu-system-..."
69	echo "       --qemu-cmd qemu-system-..."
70	echo "       --relbuilddir relative-pathname"
71	echo "       --results absolute-pathname"
72	echo "       --torture rcu"
73	exit 1
74}
75
76while test $# -gt 0
77do
78	case "$1" in
79	--bootargs)
80		checkarg --bootargs "(list of kernel boot arguments)" "$#" "$2" '.*' '^--'
81		RCU_BOOTARGS="$2"
82		shift
83		;;
84	--builddir)
85		checkarg --builddir "(absolute pathname)" "$#" "$2" '^/' '^error'
86		builddir=$2
87		gotbuilddir=1
88		shift
89		;;
90	--buildonly)
91		RCU_BUILDONLY=1; export RCU_BUILDONLY
92		;;
93	--configs)
94		checkarg --configs "(list of config files)" "$#" "$2" '^[^/]*$' '^--'
95		configs="$2"
96		shift
97		;;
98	--cpus)
99		checkarg --cpus "(number)" "$#" "$2" '^[0-9]*$' '^--'
100		cpus=$2
101		shift
102		;;
103	--datestamp)
104		checkarg --datestamp "(relative pathname)" "$#" "$2" '^[^/]*$' '^--'
105		ds=$2
106		shift
107		;;
108	--dryrun)
109		checkarg --dryrun "sched|script" $# "$2" 'sched\|script' '^--'
110		dryrun=$2
111		shift
112		;;
113	--duration)
114		checkarg --duration "(minutes)" $# "$2" '^[0-9]*$' '^error'
115		dur=$2
116		shift
117		;;
118	--interactive)
119		RCU_QEMU_INTERACTIVE=1; export RCU_QEMU_INTERACTIVE
120		;;
121	--kmake-arg)
122		checkarg --kmake-arg "(kernel make arguments)" $# "$2" '.*' '^error$'
123		RCU_KMAKE_ARG="$2"; export RCU_KMAKE_ARG
124		shift
125		;;
126	--kversion)
127		checkarg --kversion "(kernel version)" $# "$2" '^v[0-9.]*$' '^error'
128		kversion=$2
129		shift
130		;;
131	--mac)
132		checkarg --mac "(MAC address)" $# "$2" '^\([0-9a-fA-F]\{2\}:\)\{5\}[0-9a-fA-F]\{2\}$' error
133		RCU_QEMU_MAC=$2; export RCU_QEMU_MAC
134		shift
135		;;
136	--no-initrd)
137		RCU_INITRD=""; export RCU_INITRD
138		;;
139	--qemu-args)
140		checkarg --qemu-args "-qemu args" $# "$2" '^-' '^error'
141		RCU_QEMU_ARG="$2"
142		shift
143		;;
144	--qemu-cmd)
145		checkarg --qemu-cmd "(qemu-system-...)" $# "$2" 'qemu-system-' '^--'
146		RCU_QEMU_CMD="$2"; export RCU_QEMU_CMD
147		shift
148		;;
149	--relbuilddir)
150		checkarg --relbuilddir "(relative pathname)" "$#" "$2" '^[^/]*$' '^--'
151		relbuilddir=$2
152		gotrelbuilddir=1
153		builddir=${KVM}/${relbuilddir}
154		shift
155		;;
156	--results)
157		checkarg --results "(absolute pathname)" "$#" "$2" '^/' '^error'
158		resdir=$2
159		shift
160		;;
161	--torture)
162		checkarg --torture "(suite name)" "$#" "$2" '^\(lock\|rcu\)$' '^--'
163		TORTURE_SUITE=$2
164		shift
165		;;
166	*)
167		echo Unknown argument $1
168		usage
169		;;
170	esac
171	shift
172done
173
174CONFIGFRAG=${KVM}/configs/${TORTURE_SUITE}; export CONFIGFRAG
175KVPATH=${CONFIGFRAG}/$kversion; export KVPATH
176
177if test -z "$configs"
178then
179	configs="`cat $CONFIGFRAG/$kversion/CFLIST`"
180fi
181
182if test -z "$resdir"
183then
184	resdir=$KVM/res
185fi
186
187if test "$dryrun" = ""
188then
189	if ! test -e $resdir
190	then
191		mkdir -p "$resdir" || :
192	fi
193	mkdir $resdir/$ds
194
195	# Be noisy only if running the script.
196	echo Results directory: $resdir/$ds
197	echo $scriptname $args
198
199	touch $resdir/$ds/log
200	echo $scriptname $args >> $resdir/$ds/log
201	echo ${TORTURE_SUITE} > $resdir/$ds/TORTURE_SUITE
202
203	pwd > $resdir/$ds/testid.txt
204	if test -d .git
205	then
206		git status >> $resdir/$ds/testid.txt
207		git rev-parse HEAD >> $resdir/$ds/testid.txt
208	fi
209fi
210
211# Create a file of test-name/#cpus pairs, sorted by decreasing #cpus.
212touch $T/cfgcpu
213for CF in $configs
214do
215	if test -f "$CONFIGFRAG/$kversion/$CF"
216	then
217		echo $CF `configNR_CPUS.sh $CONFIGFRAG/$kversion/$CF` >> $T/cfgcpu
218	else
219		echo "The --configs file $CF does not exist, terminating."
220		exit 1
221	fi
222done
223sort -k2nr $T/cfgcpu > $T/cfgcpu.sort
224
225# Use a greedy bin-packing algorithm, sorting the list accordingly.
226awk < $T/cfgcpu.sort > $T/cfgcpu.pack -v ncpus=$cpus '
227BEGIN {
228	njobs = 0;
229}
230
231{
232	# Read file of tests and corresponding required numbers of CPUs.
233	cf[njobs] = $1;
234	cpus[njobs] = $2;
235	njobs++;
236}
237
238END {
239	alldone = 0;
240	batch = 0;
241	nc = -1;
242
243	# Each pass through the following loop creates on test batch
244	# that can be executed concurrently given ncpus.  Note that a
245	# given test that requires more than the available CPUs will run in
246	# their own batch.  Such tests just have to make do with what
247	# is available.
248	while (nc != ncpus) {
249		batch++;
250		nc = ncpus;
251
252		# Each pass through the following loop considers one
253		# test for inclusion in the current batch.
254		for (i = 0; i < njobs; i++) {
255			if (done[i])
256				continue; # Already part of a batch.
257			if (nc >= cpus[i] || nc == ncpus) {
258
259				# This test fits into the current batch.
260				done[i] = batch;
261				nc -= cpus[i];
262				if (nc <= 0)
263					break; # Too-big test in its own batch.
264			}
265		}
266	}
267
268	# Dump out the tests in batch order.
269	for (b = 1; b <= batch; b++)
270		for (i = 0; i < njobs; i++)
271			if (done[i] == b)
272				print cf[i], cpus[i];
273}'
274
275# Generate a script to execute the tests in appropriate batches.
276cat << ___EOF___ > $T/script
277TORTURE_SUITE="$TORTURE_SUITE"; export TORTURE_SUITE
278___EOF___
279awk < $T/cfgcpu.pack \
280	-v CONFIGDIR="$CONFIGFRAG/$kversion/" \
281	-v KVM="$KVM" \
282	-v ncpus=$cpus \
283	-v rd=$resdir/$ds/ \
284	-v dur=$dur \
285	-v RCU_QEMU_ARG=$RCU_QEMU_ARG \
286	-v RCU_BOOTARGS=$RCU_BOOTARGS \
287'BEGIN {
288	i = 0;
289}
290
291{
292	cf[i] = $1;
293	cpus[i] = $2;
294	i++;
295}
296
297# Dump out the scripting required to run one test batch.
298function dump(first, pastlast)
299{
300	print "echo ----Start batch: `date`";
301	print "echo ----Start batch: `date` >> " rd "/log";
302	jn=1
303	for (j = first; j < pastlast; j++) {
304		builddir=KVM "/b" jn
305		cpusr[jn] = cpus[j];
306		if (cfrep[cf[j]] == "") {
307			cfr[jn] = cf[j];
308			cfrep[cf[j]] = 1;
309		} else {
310			cfrep[cf[j]]++;
311			cfr[jn] = cf[j] "." cfrep[cf[j]];
312		}
313		if (cpusr[jn] > ncpus && ncpus != 0)
314			ovf = "(!)";
315		else
316			ovf = "";
317		print "echo ", cfr[jn], cpusr[jn] ovf ": Starting build. `date`";
318		print "echo ", cfr[jn], cpusr[jn] ovf ": Starting build. `date` >> " rd "/log";
319		print "rm -f " builddir ".*";
320		print "touch " builddir ".wait";
321		print "mkdir " builddir " > /dev/null 2>&1 || :";
322		print "mkdir " rd cfr[jn] " || :";
323		print "kvm-test-1-run.sh " CONFIGDIR cf[j], builddir, rd cfr[jn], dur " \"" RCU_QEMU_ARG "\" \"" RCU_BOOTARGS "\" > " rd cfr[jn]  "/kvm-test-1-run.sh.out 2>&1 &"
324		print "echo ", cfr[jn], cpusr[jn] ovf ": Waiting for build to complete. `date`";
325		print "echo ", cfr[jn], cpusr[jn] ovf ": Waiting for build to complete. `date` >> " rd "/log";
326		print "while test -f " builddir ".wait"
327		print "do"
328		print "\tsleep 1"
329		print "done"
330		print "echo ", cfr[jn], cpusr[jn] ovf ": Build complete. `date`";
331		print "echo ", cfr[jn], cpusr[jn] ovf ": Build complete. `date` >> " rd "/log";
332		jn++;
333	}
334	for (j = 1; j < jn; j++) {
335		builddir=KVM "/b" j
336		print "rm -f " builddir ".ready"
337		print "echo ----", cfr[j], cpusr[j] ovf ": Starting kernel. `date`";
338		print "echo ----", cfr[j], cpusr[j] ovf ": Starting kernel. `date` >> " rd "/log";
339	}
340	print "wait"
341	print "echo ---- All kernel runs complete. `date`";
342	print "echo ---- All kernel runs complete. `date` >> " rd "/log";
343	for (j = 1; j < jn; j++) {
344		builddir=KVM "/b" j
345		print "echo ----", cfr[j], cpusr[j] ovf ": Build/run results:";
346		print "echo ----", cfr[j], cpusr[j] ovf ": Build/run results: >> " rd "/log";
347		print "cat " rd cfr[j]  "/kvm-test-1-run.sh.out";
348		print "cat " rd cfr[j]  "/kvm-test-1-run.sh.out >> " rd "/log";
349	}
350}
351
352END {
353	njobs = i;
354	nc = ncpus;
355	first = 0;
356
357	# Each pass through the following loop considers one test.
358	for (i = 0; i < njobs; i++) {
359		if (ncpus == 0) {
360			# Sequential test specified, each test its own batch.
361			dump(i, i + 1);
362			first = i;
363		} else if (nc < cpus[i] && i != 0) {
364			# Out of CPUs, dump out a batch.
365			dump(first, i);
366			first = i;
367			nc = ncpus;
368		}
369		# Account for the CPUs needed by the current test.
370		nc -= cpus[i];
371	}
372	# Dump the last batch.
373	if (ncpus != 0)
374		dump(first, i);
375}' >> $T/script
376
377if test "$dryrun" = script
378then
379	# Dump out the script, but define the environment variables that
380	# it needs to run standalone.
381	echo CONFIGFRAG="$CONFIGFRAG; export CONFIGFRAG"
382	echo KVM="$KVM; export KVM"
383	echo KVPATH="$KVPATH; export KVPATH"
384	echo PATH="$PATH; export PATH"
385	echo RCU_BUILDONLY="$RCU_BUILDONLY; export RCU_BUILDONLY"
386	echo RCU_INITRD="$RCU_INITRD; export RCU_INITRD"
387	echo RCU_KMAKE_ARG="$RCU_KMAKE_ARG; export RCU_KMAKE_ARG"
388	echo RCU_QEMU_CMD="$RCU_QEMU_CMD; export RCU_QEMU_CMD"
389	echo RCU_QEMU_INTERACTIVE="$RCU_QEMU_INTERACTIVE; export RCU_QEMU_INTERACTIVE"
390	echo RCU_QEMU_MAC="$RCU_QEMU_MAC; export RCU_QEMU_MAC"
391	echo "mkdir -p "$resdir" || :"
392	echo "mkdir $resdir/$ds"
393	cat $T/script
394	exit 0
395elif test "$dryrun" = sched
396then
397	# Extract the test run schedule from the script.
398	egrep 'start batch|Starting build\.' $T/script |
399		sed -e 's/:.*$//' -e 's/^echo //'
400	exit 0
401else
402	# Not a dryru, so run the script.
403	sh $T/script
404fi
405
406# Tracing: trace_event=rcu:rcu_grace_period,rcu:rcu_future_grace_period,rcu:rcu_grace_period_init,rcu:rcu_nocb_wake,rcu:rcu_preempt_task,rcu:rcu_unlock_preempted_task,rcu:rcu_quiescent_state_report,rcu:rcu_fqs,rcu:rcu_callback,rcu:rcu_kfree_callback,rcu:rcu_batch_start,rcu:rcu_invoke_callback,rcu:rcu_invoke_kfree_callback,rcu:rcu_batch_end,rcu:rcu_torture_read,rcu:rcu_barrier
407
408echo
409echo
410echo " --- `date` Test summary:"
411echo Results directory: $resdir/$ds
412kvm-recheck.sh $resdir/$ds
413