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