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