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