1#!/bin/bash
2#
3# Run a series of tests under KVM.  By default, this series is specified
4# by the relevant CFLIST file, but can be overridden by the --configs
5# command-line argument.
6#
7# Usage: kvm.sh [ options ]
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, you can access it online at
21# http://www.gnu.org/licenses/gpl-2.0.html.
22#
23# Copyright (C) IBM Corporation, 2011
24#
25# Authors: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
26
27scriptname=$0
28args="$*"
29
30T=${TMPDIR-/tmp}/kvm.sh.$$
31trap 'rm -rf $T' 0
32mkdir $T
33
34cd `dirname $scriptname`/../../../../../
35
36dur=$((30*60))
37dryrun=""
38KVM="`pwd`/tools/testing/selftests/rcutorture"; export KVM
39PATH=${KVM}/bin:$PATH; export PATH
40TORTURE_DEFCONFIG=defconfig
41TORTURE_BOOT_IMAGE=""
42TORTURE_INITRD="$KVM/initrd"; export TORTURE_INITRD
43TORTURE_KCONFIG_ARG=""
44TORTURE_KMAKE_ARG=""
45TORTURE_QEMU_MEM=512
46TORTURE_SHUTDOWN_GRACE=180
47TORTURE_SUITE=rcu
48resdir=""
49configs=""
50cpus=0
51ds=`date +%Y.%m.%d-%H:%M:%S`
52jitter="-1"
53
54. functions.sh
55
56usage () {
57	echo "Usage: $scriptname optional arguments:"
58	echo "       --bootargs kernel-boot-arguments"
59	echo "       --bootimage relative-path-to-kernel-boot-image"
60	echo "       --buildonly"
61	echo "       --configs \"config-file list w/ repeat factor (3*TINY01)\""
62	echo "       --cpus N"
63	echo "       --datestamp string"
64	echo "       --defconfig string"
65	echo "       --dryrun sched|script"
66	echo "       --duration minutes"
67	echo "       --interactive"
68	echo "       --jitter N [ maxsleep (us) [ maxspin (us) ] ]"
69	echo "       --kconfig Kconfig-options"
70	echo "       --kmake-arg kernel-make-arguments"
71	echo "       --mac nn:nn:nn:nn:nn:nn"
72	echo "       --memory megabytes | nnnG"
73	echo "       --no-initrd"
74	echo "       --qemu-args qemu-arguments"
75	echo "       --qemu-cmd qemu-system-..."
76	echo "       --results absolute-pathname"
77	echo "       --torture rcu"
78	exit 1
79}
80
81while test $# -gt 0
82do
83	case "$1" in
84	--bootargs|--bootarg)
85		checkarg --bootargs "(list of kernel boot arguments)" "$#" "$2" '.*' '^--'
86		TORTURE_BOOTARGS="$2"
87		shift
88		;;
89	--bootimage)
90		checkarg --bootimage "(relative path to kernel boot image)" "$#" "$2" '[a-zA-Z0-9][a-zA-Z0-9_]*' '^--'
91		TORTURE_BOOT_IMAGE="$2"
92		shift
93		;;
94	--buildonly)
95		TORTURE_BUILDONLY=1
96		;;
97	--configs|--config)
98		checkarg --configs "(list of config files)" "$#" "$2" '^[^/]*$' '^--'
99		configs="$2"
100		shift
101		;;
102	--cpus)
103		checkarg --cpus "(number)" "$#" "$2" '^[0-9]*$' '^--'
104		cpus=$2
105		shift
106		;;
107	--datestamp)
108		checkarg --datestamp "(relative pathname)" "$#" "$2" '^[^/]*$' '^--'
109		ds=$2
110		shift
111		;;
112	--defconfig)
113		checkarg --defconfig "defconfigtype" "$#" "$2" '^[^/][^/]*$' '^--'
114		TORTURE_DEFCONFIG=$2
115		shift
116		;;
117	--dryrun)
118		checkarg --dryrun "sched|script" $# "$2" 'sched\|script' '^--'
119		dryrun=$2
120		shift
121		;;
122	--duration)
123		checkarg --duration "(minutes)" $# "$2" '^[0-9]*$' '^error'
124		dur=$(($2*60))
125		shift
126		;;
127	--interactive)
128		TORTURE_QEMU_INTERACTIVE=1; export TORTURE_QEMU_INTERACTIVE
129		;;
130	--jitter)
131		checkarg --jitter "(# threads [ sleep [ spin ] ])" $# "$2" '^-\{,1\}[0-9]\+\( \+[0-9]\+\)\{,2\} *$' '^error$'
132		jitter="$2"
133		shift
134		;;
135	--kconfig)
136		checkarg --kconfig "(Kconfig options)" $# "$2" '^CONFIG_[A-Z0-9_]\+=\([ynm]\|[0-9]\+\)\( CONFIG_[A-Z0-9_]\+=\([ynm]\|[0-9]\+\)\)*$' '^error$'
137		TORTURE_KCONFIG_ARG="$2"
138		shift
139		;;
140	--kmake-arg)
141		checkarg --kmake-arg "(kernel make arguments)" $# "$2" '.*' '^error$'
142		TORTURE_KMAKE_ARG="$2"
143		shift
144		;;
145	--mac)
146		checkarg --mac "(MAC address)" $# "$2" '^\([0-9a-fA-F]\{2\}:\)\{5\}[0-9a-fA-F]\{2\}$' error
147		TORTURE_QEMU_MAC=$2
148		shift
149		;;
150	--memory)
151		checkarg --memory "(memory size)" $# "$2" '^[0-9]\+[MG]\?$' error
152		TORTURE_QEMU_MEM=$2
153		shift
154		;;
155	--no-initrd)
156		TORTURE_INITRD=""; export TORTURE_INITRD
157		;;
158	--qemu-args|--qemu-arg)
159		checkarg --qemu-args "(qemu arguments)" $# "$2" '^-' '^error'
160		TORTURE_QEMU_ARG="$2"
161		shift
162		;;
163	--qemu-cmd)
164		checkarg --qemu-cmd "(qemu-system-...)" $# "$2" 'qemu-system-' '^--'
165		TORTURE_QEMU_CMD="$2"
166		shift
167		;;
168	--results)
169		checkarg --results "(absolute pathname)" "$#" "$2" '^/' '^error'
170		resdir=$2
171		shift
172		;;
173	--shutdown-grace)
174		checkarg --shutdown-grace "(seconds)" "$#" "$2" '^[0-9]*$' '^error'
175		TORTURE_SHUTDOWN_GRACE=$2
176		shift
177		;;
178	--torture)
179		checkarg --torture "(suite name)" "$#" "$2" '^\(lock\|rcu\|rcuperf\)$' '^--'
180		TORTURE_SUITE=$2
181		shift
182		if test "$TORTURE_SUITE" = rcuperf
183		then
184			# If you really want jitter for rcuperf, specify
185			# it after specifying rcuperf.  (But why?)
186			jitter=0
187		fi
188		;;
189	*)
190		echo Unknown argument $1
191		usage
192		;;
193	esac
194	shift
195done
196
197if test -z "$TORTURE_INITRD" || tools/testing/selftests/rcutorture/bin/mkinitrd.sh
198then
199	:
200else
201	echo No initrd and unable to create one, aborting test >&2
202	exit 1
203fi
204
205CONFIGFRAG=${KVM}/configs/${TORTURE_SUITE}; export CONFIGFRAG
206
207if test -z "$configs"
208then
209	configs="`cat $CONFIGFRAG/CFLIST`"
210fi
211
212if test -z "$resdir"
213then
214	resdir=$KVM/res
215fi
216
217# Create a file of test-name/#cpus pairs, sorted by decreasing #cpus.
218touch $T/cfgcpu
219for CF in $configs
220do
221	case $CF in
222	[0-9]\**|[0-9][0-9]\**|[0-9][0-9][0-9]\**)
223		config_reps=`echo $CF | sed -e 's/\*.*$//'`
224		CF1=`echo $CF | sed -e 's/^[^*]*\*//'`
225		;;
226	*)
227		config_reps=1
228		CF1=$CF
229		;;
230	esac
231	if test -f "$CONFIGFRAG/$CF1"
232	then
233		cpu_count=`configNR_CPUS.sh $CONFIGFRAG/$CF1`
234		cpu_count=`configfrag_boot_cpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
235		cpu_count=`configfrag_boot_maxcpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
236		for ((cur_rep=0;cur_rep<$config_reps;cur_rep++))
237		do
238			echo $CF1 $cpu_count >> $T/cfgcpu
239		done
240	else
241		echo "The --configs file $CF1 does not exist, terminating."
242		exit 1
243	fi
244done
245sort -k2nr $T/cfgcpu -T="$T" > $T/cfgcpu.sort
246
247# Use a greedy bin-packing algorithm, sorting the list accordingly.
248awk < $T/cfgcpu.sort > $T/cfgcpu.pack -v ncpus=$cpus '
249BEGIN {
250	njobs = 0;
251}
252
253{
254	# Read file of tests and corresponding required numbers of CPUs.
255	cf[njobs] = $1;
256	cpus[njobs] = $2;
257	njobs++;
258}
259
260END {
261	batch = 0;
262	nc = -1;
263
264	# Each pass through the following loop creates on test batch
265	# that can be executed concurrently given ncpus.  Note that a
266	# given test that requires more than the available CPUs will run in
267	# their own batch.  Such tests just have to make do with what
268	# is available.
269	while (nc != ncpus) {
270		batch++;
271		nc = ncpus;
272
273		# Each pass through the following loop considers one
274		# test for inclusion in the current batch.
275		for (i = 0; i < njobs; i++) {
276			if (done[i])
277				continue; # Already part of a batch.
278			if (nc >= cpus[i] || nc == ncpus) {
279
280				# This test fits into the current batch.
281				done[i] = batch;
282				nc -= cpus[i];
283				if (nc <= 0)
284					break; # Too-big test in its own batch.
285			}
286		}
287	}
288
289	# Dump out the tests in batch order.
290	for (b = 1; b <= batch; b++)
291		for (i = 0; i < njobs; i++)
292			if (done[i] == b)
293				print cf[i], cpus[i];
294}'
295
296# Generate a script to execute the tests in appropriate batches.
297cat << ___EOF___ > $T/script
298CONFIGFRAG="$CONFIGFRAG"; export CONFIGFRAG
299KVM="$KVM"; export KVM
300PATH="$PATH"; export PATH
301TORTURE_BOOT_IMAGE="$TORTURE_BOOT_IMAGE"; export TORTURE_BOOT_IMAGE
302TORTURE_BUILDONLY="$TORTURE_BUILDONLY"; export TORTURE_BUILDONLY
303TORTURE_DEFCONFIG="$TORTURE_DEFCONFIG"; export TORTURE_DEFCONFIG
304TORTURE_INITRD="$TORTURE_INITRD"; export TORTURE_INITRD
305TORTURE_KCONFIG_ARG="$TORTURE_KCONFIG_ARG"; export TORTURE_KCONFIG_ARG
306TORTURE_KMAKE_ARG="$TORTURE_KMAKE_ARG"; export TORTURE_KMAKE_ARG
307TORTURE_QEMU_CMD="$TORTURE_QEMU_CMD"; export TORTURE_QEMU_CMD
308TORTURE_QEMU_INTERACTIVE="$TORTURE_QEMU_INTERACTIVE"; export TORTURE_QEMU_INTERACTIVE
309TORTURE_QEMU_MAC="$TORTURE_QEMU_MAC"; export TORTURE_QEMU_MAC
310TORTURE_QEMU_MEM="$TORTURE_QEMU_MEM"; export TORTURE_QEMU_MEM
311TORTURE_SHUTDOWN_GRACE="$TORTURE_SHUTDOWN_GRACE"; export TORTURE_SHUTDOWN_GRACE
312TORTURE_SUITE="$TORTURE_SUITE"; export TORTURE_SUITE
313if ! test -e $resdir
314then
315	mkdir -p "$resdir" || :
316fi
317mkdir $resdir/$ds
318echo Results directory: $resdir/$ds
319echo $scriptname $args
320touch $resdir/$ds/log
321echo $scriptname $args >> $resdir/$ds/log
322echo ${TORTURE_SUITE} > $resdir/$ds/TORTURE_SUITE
323pwd > $resdir/$ds/testid.txt
324if test -d .git
325then
326	git status >> $resdir/$ds/testid.txt
327	git rev-parse HEAD >> $resdir/$ds/testid.txt
328	git diff HEAD >> $resdir/$ds/testid.txt
329fi
330___EOF___
331awk < $T/cfgcpu.pack \
332	-v TORTURE_BUILDONLY="$TORTURE_BUILDONLY" \
333	-v CONFIGDIR="$CONFIGFRAG/" \
334	-v KVM="$KVM" \
335	-v ncpus=$cpus \
336	-v jitter="$jitter" \
337	-v rd=$resdir/$ds/ \
338	-v dur=$dur \
339	-v TORTURE_QEMU_ARG="$TORTURE_QEMU_ARG" \
340	-v TORTURE_BOOTARGS="$TORTURE_BOOTARGS" \
341'BEGIN {
342	i = 0;
343}
344
345{
346	cf[i] = $1;
347	cpus[i] = $2;
348	i++;
349}
350
351# Dump out the scripting required to run one test batch.
352function dump(first, pastlast, batchnum)
353{
354	print "echo ----Start batch " batchnum ": `date` | tee -a " rd "log";
355	print "needqemurun="
356	jn=1
357	for (j = first; j < pastlast; j++) {
358		builddir=KVM "/b1"
359		cpusr[jn] = cpus[j];
360		if (cfrep[cf[j]] == "") {
361			cfr[jn] = cf[j];
362			cfrep[cf[j]] = 1;
363		} else {
364			cfrep[cf[j]]++;
365			cfr[jn] = cf[j] "." cfrep[cf[j]];
366		}
367		if (cpusr[jn] > ncpus && ncpus != 0)
368			ovf = "-ovf";
369		else
370			ovf = "";
371		print "echo ", cfr[jn], cpusr[jn] ovf ": Starting build. `date` | tee -a " rd "log";
372		print "rm -f " builddir ".*";
373		print "touch " builddir ".wait";
374		print "mkdir " builddir " > /dev/null 2>&1 || :";
375		print "mkdir " rd cfr[jn] " || :";
376		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 &"
377		print "echo ", cfr[jn], cpusr[jn] ovf ": Waiting for build to complete. `date` | tee -a " rd "log";
378		print "while test -f " builddir ".wait"
379		print "do"
380		print "\tsleep 1"
381		print "done"
382		print "echo ", cfr[jn], cpusr[jn] ovf ": Build complete. `date` | tee -a " rd "log";
383		jn++;
384	}
385	for (j = 1; j < jn; j++) {
386		builddir=KVM "/b" j
387		print "rm -f " builddir ".ready"
388		print "if test -f \"" rd cfr[j] "/builtkernel\""
389		print "then"
390		print "\techo ----", cfr[j], cpusr[j] ovf ": Kernel present. `date` | tee -a " rd "log";
391		print "\tneedqemurun=1"
392		print "fi"
393	}
394	njitter = 0;
395	split(jitter, ja);
396	if (ja[1] == -1 && ncpus == 0)
397		njitter = 1;
398	else if (ja[1] == -1)
399		njitter = ncpus;
400	else
401		njitter = ja[1];
402	if (TORTURE_BUILDONLY && njitter != 0) {
403		njitter = 0;
404		print "echo Build-only run, so suppressing jitter | tee -a " rd "log"
405	}
406	if (TORTURE_BUILDONLY) {
407		print "needqemurun="
408	}
409	print "if test -n \"$needqemurun\""
410	print "then"
411	print "\techo ---- Starting kernels. `date` | tee -a " rd "log";
412	for (j = 0; j < njitter; j++)
413		print "\tjitter.sh " j " " dur " " ja[2] " " ja[3] "&"
414	print "\twait"
415	print "\techo ---- All kernel runs complete. `date` | tee -a " rd "log";
416	print "else"
417	print "\twait"
418	print "\techo ---- No kernel runs. `date` | tee -a " rd "log";
419	print "fi"
420	for (j = 1; j < jn; j++) {
421		builddir=KVM "/b" j
422		print "echo ----", cfr[j], cpusr[j] ovf ": Build/run results: | tee -a " rd "log";
423		print "cat " rd cfr[j]  "/kvm-test-1-run.sh.out | tee -a " rd "log";
424	}
425}
426
427END {
428	njobs = i;
429	nc = ncpus;
430	first = 0;
431	batchnum = 1;
432
433	# Each pass through the following loop considers one test.
434	for (i = 0; i < njobs; i++) {
435		if (ncpus == 0) {
436			# Sequential test specified, each test its own batch.
437			dump(i, i + 1, batchnum);
438			first = i;
439			batchnum++;
440		} else if (nc < cpus[i] && i != 0) {
441			# Out of CPUs, dump out a batch.
442			dump(first, i, batchnum);
443			first = i;
444			nc = ncpus;
445			batchnum++;
446		}
447		# Account for the CPUs needed by the current test.
448		nc -= cpus[i];
449	}
450	# Dump the last batch.
451	if (ncpus != 0)
452		dump(first, i, batchnum);
453}' >> $T/script
454
455cat << ___EOF___ >> $T/script
456echo
457echo
458echo " --- `date` Test summary:"
459echo Results directory: $resdir/$ds
460kvm-recheck.sh $resdir/$ds
461___EOF___
462
463if test "$dryrun" = script
464then
465	cat $T/script
466	exit 0
467elif test "$dryrun" = sched
468then
469	# Extract the test run schedule from the script.
470	egrep 'Start batch|Starting build\.' $T/script |
471		grep -v ">>" |
472		sed -e 's/:.*$//' -e 's/^echo //'
473	exit 0
474else
475	# Not a dryrun, so run the script.
476	sh $T/script
477fi
478
479# 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
480