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: 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
41TORTURE_DEFCONFIG=defconfig
42TORTURE_BOOT_IMAGE=""
43TORTURE_INITRD="$KVM/initrd"; export TORTURE_INITRD
44TORTURE_KMAKE_ARG=""
45TORTURE_SUITE=rcu
46resdir=""
47configs=""
48cpus=0
49ds=`date +%Y.%m.%d-%H:%M:%S`
50kversion=""
51
52. functions.sh
53
54usage () {
55	echo "Usage: $scriptname optional arguments:"
56	echo "       --bootargs kernel-boot-arguments"
57	echo "       --bootimage relative-path-to-kernel-boot-image"
58	echo "       --buildonly"
59	echo "       --configs \"config-file list\""
60	echo "       --cpus N"
61	echo "       --datestamp string"
62	echo "       --defconfig string"
63	echo "       --dryrun sched|script"
64	echo "       --duration minutes"
65	echo "       --interactive"
66	echo "       --kmake-arg kernel-make-arguments"
67	echo "       --kversion vN.NN"
68	echo "       --mac nn:nn:nn:nn:nn:nn"
69	echo "       --no-initrd"
70	echo "       --qemu-args qemu-system-..."
71	echo "       --qemu-cmd qemu-system-..."
72	echo "       --results absolute-pathname"
73	echo "       --torture rcu"
74	exit 1
75}
76
77while test $# -gt 0
78do
79	case "$1" in
80	--bootargs)
81		checkarg --bootargs "(list of kernel boot arguments)" "$#" "$2" '.*' '^--'
82		TORTURE_BOOTARGS="$2"
83		shift
84		;;
85	--bootimage)
86		checkarg --bootimage "(relative path to kernel boot image)" "$#" "$2" '[a-zA-Z0-9][a-zA-Z0-9_]*' '^--'
87		TORTURE_BOOT_IMAGE="$2"
88		shift
89		;;
90	--buildonly)
91		TORTURE_BUILDONLY=1
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	--defconfig)
109		checkarg --defconfig "defconfigtype" "$#" "$2" '^[^/][^/]*$' '^--'
110		TORTURE_DEFCONFIG=$2
111		shift
112		;;
113	--dryrun)
114		checkarg --dryrun "sched|script" $# "$2" 'sched\|script' '^--'
115		dryrun=$2
116		shift
117		;;
118	--duration)
119		checkarg --duration "(minutes)" $# "$2" '^[0-9]*$' '^error'
120		dur=$2
121		shift
122		;;
123	--interactive)
124		TORTURE_QEMU_INTERACTIVE=1; export TORTURE_QEMU_INTERACTIVE
125		;;
126	--kmake-arg)
127		checkarg --kmake-arg "(kernel make arguments)" $# "$2" '.*' '^error$'
128		TORTURE_KMAKE_ARG="$2"
129		shift
130		;;
131	--kversion)
132		checkarg --kversion "(kernel version)" $# "$2" '^v[0-9.]*$' '^error'
133		kversion=$2
134		shift
135		;;
136	--mac)
137		checkarg --mac "(MAC address)" $# "$2" '^\([0-9a-fA-F]\{2\}:\)\{5\}[0-9a-fA-F]\{2\}$' error
138		TORTURE_QEMU_MAC=$2
139		shift
140		;;
141	--no-initrd)
142		TORTURE_INITRD=""; export TORTURE_INITRD
143		;;
144	--qemu-args)
145		checkarg --qemu-args "-qemu args" $# "$2" '^-' '^error'
146		TORTURE_QEMU_ARG="$2"
147		shift
148		;;
149	--qemu-cmd)
150		checkarg --qemu-cmd "(qemu-system-...)" $# "$2" 'qemu-system-' '^--'
151		TORTURE_QEMU_CMD="$2"
152		shift
153		;;
154	--results)
155		checkarg --results "(absolute pathname)" "$#" "$2" '^/' '^error'
156		resdir=$2
157		shift
158		;;
159	--torture)
160		checkarg --torture "(suite name)" "$#" "$2" '^\(lock\|rcu\)$' '^--'
161		TORTURE_SUITE=$2
162		shift
163		;;
164	*)
165		echo Unknown argument $1
166		usage
167		;;
168	esac
169	shift
170done
171
172CONFIGFRAG=${KVM}/configs/${TORTURE_SUITE}; export CONFIGFRAG
173KVPATH=${CONFIGFRAG}/$kversion; export KVPATH
174
175if test -z "$configs"
176then
177	configs="`cat $CONFIGFRAG/$kversion/CFLIST`"
178fi
179
180if test -z "$resdir"
181then
182	resdir=$KVM/res
183fi
184
185# Create a file of test-name/#cpus pairs, sorted by decreasing #cpus.
186touch $T/cfgcpu
187for CF in $configs
188do
189	if test -f "$CONFIGFRAG/$kversion/$CF"
190	then
191		cpu_count=`configNR_CPUS.sh $CONFIGFRAG/$kversion/$CF`
192		cpu_count=`configfrag_boot_cpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$kversion/$CF" "$cpu_count"`
193		echo $CF $cpu_count >> $T/cfgcpu
194	else
195		echo "The --configs file $CF does not exist, terminating."
196		exit 1
197	fi
198done
199sort -k2nr $T/cfgcpu > $T/cfgcpu.sort
200
201# Use a greedy bin-packing algorithm, sorting the list accordingly.
202awk < $T/cfgcpu.sort > $T/cfgcpu.pack -v ncpus=$cpus '
203BEGIN {
204	njobs = 0;
205}
206
207{
208	# Read file of tests and corresponding required numbers of CPUs.
209	cf[njobs] = $1;
210	cpus[njobs] = $2;
211	njobs++;
212}
213
214END {
215	alldone = 0;
216	batch = 0;
217	nc = -1;
218
219	# Each pass through the following loop creates on test batch
220	# that can be executed concurrently given ncpus.  Note that a
221	# given test that requires more than the available CPUs will run in
222	# their own batch.  Such tests just have to make do with what
223	# is available.
224	while (nc != ncpus) {
225		batch++;
226		nc = ncpus;
227
228		# Each pass through the following loop considers one
229		# test for inclusion in the current batch.
230		for (i = 0; i < njobs; i++) {
231			if (done[i])
232				continue; # Already part of a batch.
233			if (nc >= cpus[i] || nc == ncpus) {
234
235				# This test fits into the current batch.
236				done[i] = batch;
237				nc -= cpus[i];
238				if (nc <= 0)
239					break; # Too-big test in its own batch.
240			}
241		}
242	}
243
244	# Dump out the tests in batch order.
245	for (b = 1; b <= batch; b++)
246		for (i = 0; i < njobs; i++)
247			if (done[i] == b)
248				print cf[i], cpus[i];
249}'
250
251# Generate a script to execute the tests in appropriate batches.
252cat << ___EOF___ > $T/script
253CONFIGFRAG="$CONFIGFRAG"; export CONFIGFRAG
254KVM="$KVM"; export KVM
255KVPATH="$KVPATH"; export KVPATH
256PATH="$PATH"; export PATH
257TORTURE_BOOT_IMAGE="$TORTURE_BOOT_IMAGE"; export TORTURE_BOOT_IMAGE
258TORTURE_BUILDONLY="$TORTURE_BUILDONLY"; export TORTURE_BUILDONLY
259TORTURE_DEFCONFIG="$TORTURE_DEFCONFIG"; export TORTURE_DEFCONFIG
260TORTURE_INITRD="$TORTURE_INITRD"; export TORTURE_INITRD
261TORTURE_KMAKE_ARG="$TORTURE_KMAKE_ARG"; export TORTURE_KMAKE_ARG
262TORTURE_QEMU_CMD="$TORTURE_QEMU_CMD"; export TORTURE_QEMU_CMD
263TORTURE_QEMU_INTERACTIVE="$TORTURE_QEMU_INTERACTIVE"; export TORTURE_QEMU_INTERACTIVE
264TORTURE_QEMU_MAC="$TORTURE_QEMU_MAC"; export TORTURE_QEMU_MAC
265TORTURE_SUITE="$TORTURE_SUITE"; export TORTURE_SUITE
266if ! test -e $resdir
267then
268	mkdir -p "$resdir" || :
269fi
270mkdir $resdir/$ds
271echo Results directory: $resdir/$ds
272echo $scriptname $args
273touch $resdir/$ds/log
274echo $scriptname $args >> $resdir/$ds/log
275echo ${TORTURE_SUITE} > $resdir/$ds/TORTURE_SUITE
276pwd > $resdir/$ds/testid.txt
277if test -d .git
278then
279	git status >> $resdir/$ds/testid.txt
280	git rev-parse HEAD >> $resdir/$ds/testid.txt
281	if ! git diff HEAD > $T/git-diff 2>&1
282	then
283		cp $T/git-diff $resdir/$ds
284	fi
285fi
286___EOF___
287awk < $T/cfgcpu.pack \
288	-v CONFIGDIR="$CONFIGFRAG/$kversion/" \
289	-v KVM="$KVM" \
290	-v ncpus=$cpus \
291	-v rd=$resdir/$ds/ \
292	-v dur=$dur \
293	-v TORTURE_QEMU_ARG="$TORTURE_QEMU_ARG" \
294	-v TORTURE_BOOTARGS="$TORTURE_BOOTARGS" \
295'BEGIN {
296	i = 0;
297}
298
299{
300	cf[i] = $1;
301	cpus[i] = $2;
302	i++;
303}
304
305# Dump out the scripting required to run one test batch.
306function dump(first, pastlast)
307{
308	print "echo ----Start batch: `date`";
309	print "echo ----Start batch: `date` >> " rd "/log";
310	jn=1
311	for (j = first; j < pastlast; j++) {
312		builddir=KVM "/b" jn
313		cpusr[jn] = cpus[j];
314		if (cfrep[cf[j]] == "") {
315			cfr[jn] = cf[j];
316			cfrep[cf[j]] = 1;
317		} else {
318			cfrep[cf[j]]++;
319			cfr[jn] = cf[j] "." cfrep[cf[j]];
320		}
321		if (cpusr[jn] > ncpus && ncpus != 0)
322			ovf = "(!)";
323		else
324			ovf = "";
325		print "echo ", cfr[jn], cpusr[jn] ovf ": Starting build. `date`";
326		print "echo ", cfr[jn], cpusr[jn] ovf ": Starting build. `date` >> " rd "/log";
327		print "rm -f " builddir ".*";
328		print "touch " builddir ".wait";
329		print "mkdir " builddir " > /dev/null 2>&1 || :";
330		print "mkdir " rd cfr[jn] " || :";
331		print "kvm-test-1-run.sh " CONFIGDIR cf[j], builddir, rd cfr[jn], dur " \"" TORTURE_QEMU_ARG "\" \"" TORTURE_BOOTARGS "\" > " rd cfr[jn]  "/kvm-test-1-run.sh.out 2>&1 &"
332		print "echo ", cfr[jn], cpusr[jn] ovf ": Waiting for build to complete. `date`";
333		print "echo ", cfr[jn], cpusr[jn] ovf ": Waiting for build to complete. `date` >> " rd "/log";
334		print "while test -f " builddir ".wait"
335		print "do"
336		print "\tsleep 1"
337		print "done"
338		print "echo ", cfr[jn], cpusr[jn] ovf ": Build complete. `date`";
339		print "echo ", cfr[jn], cpusr[jn] ovf ": Build complete. `date` >> " rd "/log";
340		jn++;
341	}
342	for (j = 1; j < jn; j++) {
343		builddir=KVM "/b" j
344		print "rm -f " builddir ".ready"
345		print "if test -z \"$TORTURE_BUILDONLY\""
346		print "then"
347		print "\techo ----", cfr[j], cpusr[j] ovf ": Starting kernel. `date`";
348		print "\techo ----", cfr[j], cpusr[j] ovf ": Starting kernel. `date` >> " rd "/log";
349		print "fi"
350	}
351	print "wait"
352	print "if test -z \"$TORTURE_BUILDONLY\""
353	print "then"
354	print "\techo ---- All kernel runs complete. `date`";
355	print "\techo ---- All kernel runs complete. `date` >> " rd "/log";
356	print "fi"
357	for (j = 1; j < jn; j++) {
358		builddir=KVM "/b" j
359		print "echo ----", cfr[j], cpusr[j] ovf ": Build/run results:";
360		print "echo ----", cfr[j], cpusr[j] ovf ": Build/run results: >> " rd "/log";
361		print "cat " rd cfr[j]  "/kvm-test-1-run.sh.out";
362		print "cat " rd cfr[j]  "/kvm-test-1-run.sh.out >> " rd "/log";
363	}
364}
365
366END {
367	njobs = i;
368	nc = ncpus;
369	first = 0;
370
371	# Each pass through the following loop considers one test.
372	for (i = 0; i < njobs; i++) {
373		if (ncpus == 0) {
374			# Sequential test specified, each test its own batch.
375			dump(i, i + 1);
376			first = i;
377		} else if (nc < cpus[i] && i != 0) {
378			# Out of CPUs, dump out a batch.
379			dump(first, i);
380			first = i;
381			nc = ncpus;
382		}
383		# Account for the CPUs needed by the current test.
384		nc -= cpus[i];
385	}
386	# Dump the last batch.
387	if (ncpus != 0)
388		dump(first, i);
389}' >> $T/script
390
391cat << ___EOF___ >> $T/script
392echo
393echo
394echo " --- `date` Test summary:"
395echo Results directory: $resdir/$ds
396kvm-recheck.sh $resdir/$ds
397___EOF___
398
399if test "$dryrun" = script
400then
401	cat $T/script
402	exit 0
403elif test "$dryrun" = sched
404then
405	# Extract the test run schedule from the script.
406	egrep 'Start batch|Starting build\.' $T/script |
407		grep -v ">>" |
408		sed -e 's/:.*$//' -e 's/^echo //'
409	exit 0
410else
411	# Not a dryrun, so run the script.
412	sh $T/script
413fi
414
415# 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
416