1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0+
3#
4# Run a series of tests under KVM.  By default, this series is specified
5# by the relevant CFLIST file, but can be overridden by the --configs
6# command-line argument.
7#
8# Usage: kvm.sh [ options ]
9#
10# Copyright (C) IBM Corporation, 2011
11#
12# Authors: Paul E. McKenney <paulmck@linux.ibm.com>
13
14scriptname=$0
15args="$*"
16
17T=${TMPDIR-/tmp}/kvm.sh.$$
18trap 'rm -rf $T' 0
19mkdir $T
20
21cd `dirname $scriptname`/../../../../../
22
23# This script knows only English.
24LANG=en_US.UTF-8; export LANG
25
26dur=$((30*60))
27dryrun=""
28KVM="`pwd`/tools/testing/selftests/rcutorture"; export KVM
29PATH=${KVM}/bin:$PATH; export PATH
30. functions.sh
31
32TORTURE_ALLOTED_CPUS="`identify_qemu_vcpus`"
33TORTURE_DEFCONFIG=defconfig
34TORTURE_BOOT_IMAGE=""
35TORTURE_BUILDONLY=
36TORTURE_INITRD="$KVM/initrd"; export TORTURE_INITRD
37TORTURE_KCONFIG_ARG=""
38TORTURE_KCONFIG_GDB_ARG=""
39TORTURE_BOOT_GDB_ARG=""
40TORTURE_QEMU_GDB_ARG=""
41TORTURE_JITTER_START=""
42TORTURE_JITTER_STOP=""
43TORTURE_KCONFIG_KASAN_ARG=""
44TORTURE_KCONFIG_KCSAN_ARG=""
45TORTURE_KMAKE_ARG=""
46TORTURE_QEMU_MEM=512
47TORTURE_REMOTE=
48TORTURE_SHUTDOWN_GRACE=180
49TORTURE_SUITE=rcu
50TORTURE_MOD=rcutorture
51TORTURE_TRUST_MAKE=""
52resdir=""
53configs=""
54cpus=0
55ds=`date +%Y.%m.%d-%H.%M.%S`
56jitter="-1"
57
58startdate="`date`"
59starttime="`get_starttime`"
60
61usage () {
62	echo "Usage: $scriptname optional arguments:"
63	echo "       --allcpus"
64	echo "       --bootargs kernel-boot-arguments"
65	echo "       --bootimage relative-path-to-kernel-boot-image"
66	echo "       --buildonly"
67	echo "       --configs \"config-file list w/ repeat factor (3*TINY01)\""
68	echo "       --cpus N"
69	echo "       --datestamp string"
70	echo "       --defconfig string"
71	echo "       --dryrun batches|scenarios|sched|script"
72	echo "       --duration minutes | <seconds>s | <hours>h | <days>d"
73	echo "       --gdb"
74	echo "       --help"
75	echo "       --interactive"
76	echo "       --jitter N [ maxsleep (us) [ maxspin (us) ] ]"
77	echo "       --kconfig Kconfig-options"
78	echo "       --kmake-arg kernel-make-arguments"
79	echo "       --mac nn:nn:nn:nn:nn:nn"
80	echo "       --memory megabytes|nnnG"
81	echo "       --no-initrd"
82	echo "       --qemu-args qemu-arguments"
83	echo "       --qemu-cmd qemu-system-..."
84	echo "       --remote"
85	echo "       --results absolute-pathname"
86	echo "       --torture lock|rcu|rcuscale|refscale|scf"
87	echo "       --trust-make"
88	exit 1
89}
90
91while test $# -gt 0
92do
93	case "$1" in
94	--allcpus)
95		cpus=$TORTURE_ALLOTED_CPUS
96		max_cpus=$TORTURE_ALLOTED_CPUS
97		;;
98	--bootargs|--bootarg)
99		checkarg --bootargs "(list of kernel boot arguments)" "$#" "$2" '.*' '^--'
100		TORTURE_BOOTARGS="$TORTURE_BOOTARGS $2"
101		shift
102		;;
103	--bootimage)
104		checkarg --bootimage "(relative path to kernel boot image)" "$#" "$2" '[a-zA-Z0-9][a-zA-Z0-9_]*' '^--'
105		TORTURE_BOOT_IMAGE="$2"
106		shift
107		;;
108	--buildonly|--build-only)
109		TORTURE_BUILDONLY=1
110		;;
111	--configs|--config)
112		checkarg --configs "(list of config files)" "$#" "$2" '^[^/.a-z]\+$' '^--'
113		configs="$configs $2"
114		shift
115		;;
116	--cpus)
117		checkarg --cpus "(number)" "$#" "$2" '^[0-9]*$' '^--'
118		cpus=$2
119		TORTURE_ALLOTED_CPUS="$2"
120		if test -z "$TORTURE_REMOTE"
121		then
122			max_cpus="`identify_qemu_vcpus`"
123			if test "$TORTURE_ALLOTED_CPUS" -gt "$max_cpus"
124			then
125				TORTURE_ALLOTED_CPUS=$max_cpus
126			fi
127		fi
128		shift
129		;;
130	--datestamp)
131		checkarg --datestamp "(relative pathname)" "$#" "$2" '^[a-zA-Z0-9._/-]*$' '^--'
132		ds=$2
133		shift
134		;;
135	--defconfig)
136		checkarg --defconfig "defconfigtype" "$#" "$2" '^[^/][^/]*$' '^--'
137		TORTURE_DEFCONFIG=$2
138		shift
139		;;
140	--dryrun)
141		checkarg --dryrun "batches|sched|script" $# "$2" 'batches\|scenarios\|sched\|script' '^--'
142		dryrun=$2
143		shift
144		;;
145	--duration)
146		checkarg --duration "(minutes)" $# "$2" '^[0-9][0-9]*\(s\|m\|h\|d\|\)$' '^error'
147		mult=60
148		if echo "$2" | grep -q 's$'
149		then
150			mult=1
151		elif echo "$2" | grep -q 'h$'
152		then
153			mult=3600
154		elif echo "$2" | grep -q 'd$'
155		then
156			mult=86400
157		fi
158		ts=`echo $2 | sed -e 's/[smhd]$//'`
159		dur=$(($ts*mult))
160		shift
161		;;
162	--gdb)
163		TORTURE_KCONFIG_GDB_ARG="CONFIG_DEBUG_INFO=y"; export TORTURE_KCONFIG_GDB_ARG
164		TORTURE_BOOT_GDB_ARG="nokaslr"; export TORTURE_BOOT_GDB_ARG
165		TORTURE_QEMU_GDB_ARG="-s -S"; export TORTURE_QEMU_GDB_ARG
166		;;
167	--help|-h)
168		usage
169		;;
170	--interactive)
171		TORTURE_QEMU_INTERACTIVE=1; export TORTURE_QEMU_INTERACTIVE
172		;;
173	--jitter)
174		checkarg --jitter "(# threads [ sleep [ spin ] ])" $# "$2" '^-\{,1\}[0-9]\+\( \+[0-9]\+\)\{,2\} *$' '^error$'
175		jitter="$2"
176		shift
177		;;
178	--kconfig|--kconfigs)
179		checkarg --kconfig "(Kconfig options)" $# "$2" '^CONFIG_[A-Z0-9_]\+=\([ynm]\|[0-9]\+\)\( CONFIG_[A-Z0-9_]\+=\([ynm]\|[0-9]\+\)\)*$' '^error$'
180		TORTURE_KCONFIG_ARG="`echo "$TORTURE_KCONFIG_ARG $2" | sed -e 's/^ *//' -e 's/ *$//'`"
181		shift
182		;;
183	--kasan)
184		TORTURE_KCONFIG_KASAN_ARG="CONFIG_DEBUG_INFO=y CONFIG_KASAN=y"; export TORTURE_KCONFIG_KASAN_ARG
185		;;
186	--kcsan)
187		TORTURE_KCONFIG_KCSAN_ARG="CONFIG_DEBUG_INFO=y CONFIG_KCSAN=y CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC=n CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY=n CONFIG_KCSAN_REPORT_ONCE_IN_MS=100000 CONFIG_KCSAN_INTERRUPT_WATCHER=y CONFIG_KCSAN_VERBOSE=y CONFIG_DEBUG_LOCK_ALLOC=y CONFIG_PROVE_LOCKING=y"; export TORTURE_KCONFIG_KCSAN_ARG
188		;;
189	--kmake-arg|--kmake-args)
190		checkarg --kmake-arg "(kernel make arguments)" $# "$2" '.*' '^error$'
191		TORTURE_KMAKE_ARG="`echo "$TORTURE_KMAKE_ARG $2" | sed -e 's/^ *//' -e 's/ *$//'`"
192		shift
193		;;
194	--mac)
195		checkarg --mac "(MAC address)" $# "$2" '^\([0-9a-fA-F]\{2\}:\)\{5\}[0-9a-fA-F]\{2\}$' error
196		TORTURE_QEMU_MAC=$2
197		shift
198		;;
199	--memory)
200		checkarg --memory "(memory size)" $# "$2" '^[0-9]\+[MG]\?$' error
201		TORTURE_QEMU_MEM=$2
202		shift
203		;;
204	--no-initrd)
205		TORTURE_INITRD=""; export TORTURE_INITRD
206		;;
207	--qemu-args|--qemu-arg)
208		checkarg --qemu-args "(qemu arguments)" $# "$2" '^-' '^error'
209		TORTURE_QEMU_ARG="`echo "$TORTURE_QEMU_ARG $2" | sed -e 's/^ *//' -e 's/ *$//'`"
210		shift
211		;;
212	--qemu-cmd)
213		checkarg --qemu-cmd "(qemu-system-...)" $# "$2" 'qemu-system-' '^--'
214		TORTURE_QEMU_CMD="$2"
215		shift
216		;;
217	--remote)
218		TORTURE_REMOTE=1
219		;;
220	--results)
221		checkarg --results "(absolute pathname)" "$#" "$2" '^/' '^error'
222		resdir=$2
223		shift
224		;;
225	--shutdown-grace)
226		checkarg --shutdown-grace "(seconds)" "$#" "$2" '^[0-9]*$' '^error'
227		TORTURE_SHUTDOWN_GRACE=$2
228		shift
229		;;
230	--torture)
231		checkarg --torture "(suite name)" "$#" "$2" '^\(lock\|rcu\|rcuscale\|refscale\|scf\)$' '^--'
232		TORTURE_SUITE=$2
233		TORTURE_MOD="`echo $TORTURE_SUITE | sed -e 's/^\(lock\|rcu\|scf\)$/\1torture/'`"
234		shift
235		if test "$TORTURE_SUITE" = rcuscale || test "$TORTURE_SUITE" = refscale
236		then
237			# If you really want jitter for refscale or
238			# rcuscale, specify it after specifying the rcuscale
239			# or the refscale.  (But why jitter in these cases?)
240			jitter=0
241		fi
242		;;
243	--trust-make)
244		TORTURE_TRUST_MAKE="y"
245		;;
246	*)
247		echo Unknown argument $1
248		usage
249		;;
250	esac
251	shift
252done
253
254if test -n "$dryrun" || test -z "$TORTURE_INITRD" || tools/testing/selftests/rcutorture/bin/mkinitrd.sh
255then
256	:
257else
258	echo No initrd and unable to create one, aborting test >&2
259	exit 1
260fi
261
262CONFIGFRAG=${KVM}/configs/${TORTURE_SUITE}; export CONFIGFRAG
263
264defaultconfigs="`tr '\012' ' ' < $CONFIGFRAG/CFLIST`"
265if test -z "$configs"
266then
267	configs=$defaultconfigs
268fi
269
270if test -z "$resdir"
271then
272	resdir=$KVM/res
273fi
274
275# Create a file of test-name/#cpus pairs, sorted by decreasing #cpus.
276configs_derep=
277for CF in $configs
278do
279	case $CF in
280	[0-9]\**|[0-9][0-9]\**|[0-9][0-9][0-9]\**)
281		config_reps=`echo $CF | sed -e 's/\*.*$//'`
282		CF1=`echo $CF | sed -e 's/^[^*]*\*//'`
283		;;
284	*)
285		config_reps=1
286		CF1=$CF
287		;;
288	esac
289	for ((cur_rep=0;cur_rep<$config_reps;cur_rep++))
290	do
291		configs_derep="$configs_derep $CF1"
292	done
293done
294touch $T/cfgcpu
295configs_derep="`echo $configs_derep | sed -e "s/\<CFLIST\>/$defaultconfigs/g"`"
296if test -n "$TORTURE_KCONFIG_GDB_ARG"
297then
298	if test "`echo $configs_derep | wc -w`" -gt 1
299	then
300		echo "The --config list is: $configs_derep."
301		echo "Only one --config permitted with --gdb, terminating."
302		exit 1
303	fi
304fi
305echo 'BEGIN {' > $T/cfgcpu.awk
306for CF1 in `echo $configs_derep | tr -s ' ' '\012' | sort -u`
307do
308	if test -f "$CONFIGFRAG/$CF1"
309	then
310		if echo "$TORTURE_KCONFIG_ARG" | grep -q '\<CONFIG_NR_CPUS='
311		then
312			echo "$TORTURE_KCONFIG_ARG" | tr -s ' ' | tr ' ' '\012' > $T/KCONFIG_ARG
313			cpu_count=`configNR_CPUS.sh $T/KCONFIG_ARG`
314		else
315			cpu_count=`configNR_CPUS.sh $CONFIGFRAG/$CF1`
316		fi
317		cpu_count=`configfrag_boot_cpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
318		cpu_count=`configfrag_boot_maxcpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
319		echo 'scenariocpu["'"$CF1"'"] = '"$cpu_count"';' >> $T/cfgcpu.awk
320	else
321		echo "The --configs file $CF1 does not exist, terminating."
322		exit 1
323	fi
324done
325cat << '___EOF___' >> $T/cfgcpu.awk
326}
327{
328	for (i = 1; i <= NF; i++)
329		print $i, scenariocpu[$i];
330}
331___EOF___
332echo $configs_derep | awk -f $T/cfgcpu.awk > $T/cfgcpu
333sort -k2nr $T/cfgcpu -T="$T" > $T/cfgcpu.sort
334
335# Use a greedy bin-packing algorithm, sorting the list accordingly.
336awk < $T/cfgcpu.sort > $T/cfgcpu.pack -v ncpus=$cpus '
337BEGIN {
338	njobs = 0;
339}
340
341{
342	# Read file of tests and corresponding required numbers of CPUs.
343	cf[njobs] = $1;
344	cpus[njobs] = $2;
345	njobs++;
346}
347
348END {
349	batch = 0;
350	nc = -1;
351
352	# Each pass through the following loop creates on test batch that
353	# can be executed concurrently given ncpus.  Note that a given test
354	# that requires more than the available CPUs will run in its own
355	# batch.  Such tests just have to make do with what is available.
356	while (nc != ncpus) {
357		batch++;
358		nc = ncpus;
359
360		# Each pass through the following loop considers one
361		# test for inclusion in the current batch.
362		for (i = 0; i < njobs; i++) {
363			if (done[i])
364				continue; # Already part of a batch.
365			if (nc >= cpus[i] || nc == ncpus) {
366
367				# This test fits into the current batch.
368				done[i] = batch;
369				nc -= cpus[i];
370				if (nc <= 0)
371					break; # Too-big test in its own batch.
372			}
373		}
374	}
375
376	# Dump out the tests in batch order.
377	for (b = 1; b <= batch; b++)
378		for (i = 0; i < njobs; i++)
379			if (done[i] == b)
380				print cf[i], cpus[i];
381}'
382
383# Generate a script to execute the tests in appropriate batches.
384cat << ___EOF___ > $T/script
385CONFIGFRAG="$CONFIGFRAG"; export CONFIGFRAG
386KVM="$KVM"; export KVM
387PATH="$PATH"; export PATH
388TORTURE_ALLOTED_CPUS="$TORTURE_ALLOTED_CPUS"; export TORTURE_ALLOTED_CPUS
389TORTURE_BOOT_IMAGE="$TORTURE_BOOT_IMAGE"; export TORTURE_BOOT_IMAGE
390TORTURE_BUILDONLY="$TORTURE_BUILDONLY"; export TORTURE_BUILDONLY
391TORTURE_DEFCONFIG="$TORTURE_DEFCONFIG"; export TORTURE_DEFCONFIG
392TORTURE_INITRD="$TORTURE_INITRD"; export TORTURE_INITRD
393TORTURE_KCONFIG_ARG="$TORTURE_KCONFIG_ARG"; export TORTURE_KCONFIG_ARG
394TORTURE_KCONFIG_GDB_ARG="$TORTURE_KCONFIG_GDB_ARG"; export TORTURE_KCONFIG_GDB_ARG
395TORTURE_BOOT_GDB_ARG="$TORTURE_BOOT_GDB_ARG"; export TORTURE_BOOT_GDB_ARG
396TORTURE_QEMU_GDB_ARG="$TORTURE_QEMU_GDB_ARG"; export TORTURE_QEMU_GDB_ARG
397TORTURE_KCONFIG_KASAN_ARG="$TORTURE_KCONFIG_KASAN_ARG"; export TORTURE_KCONFIG_KASAN_ARG
398TORTURE_KCONFIG_KCSAN_ARG="$TORTURE_KCONFIG_KCSAN_ARG"; export TORTURE_KCONFIG_KCSAN_ARG
399TORTURE_KMAKE_ARG="$TORTURE_KMAKE_ARG"; export TORTURE_KMAKE_ARG
400TORTURE_MOD="$TORTURE_MOD"; export TORTURE_MOD
401TORTURE_QEMU_CMD="$TORTURE_QEMU_CMD"; export TORTURE_QEMU_CMD
402TORTURE_QEMU_INTERACTIVE="$TORTURE_QEMU_INTERACTIVE"; export TORTURE_QEMU_INTERACTIVE
403TORTURE_QEMU_MAC="$TORTURE_QEMU_MAC"; export TORTURE_QEMU_MAC
404TORTURE_QEMU_MEM="$TORTURE_QEMU_MEM"; export TORTURE_QEMU_MEM
405TORTURE_SHUTDOWN_GRACE="$TORTURE_SHUTDOWN_GRACE"; export TORTURE_SHUTDOWN_GRACE
406TORTURE_SUITE="$TORTURE_SUITE"; export TORTURE_SUITE
407TORTURE_TRUST_MAKE="$TORTURE_TRUST_MAKE"; export TORTURE_TRUST_MAKE
408if ! test -e $resdir
409then
410	mkdir -p "$resdir" || :
411fi
412mkdir -p $resdir/$ds
413TORTURE_RESDIR="$resdir/$ds"; export TORTURE_RESDIR
414TORTURE_STOPFILE="$resdir/$ds/STOP.1"; export TORTURE_STOPFILE
415echo Results directory: $resdir/$ds
416echo $scriptname $args
417touch $resdir/$ds/log
418echo $scriptname $args >> $resdir/$ds/log
419echo ${TORTURE_SUITE} > $resdir/$ds/torture_suite
420echo Build directory: `pwd` > $resdir/$ds/testid.txt
421if test -d .git
422then
423	echo Current commit: `git rev-parse HEAD` >> $resdir/$ds/testid.txt
424	echo >> $resdir/$ds/testid.txt
425	echo ' ---' Output of "'"git status"'": >> $resdir/$ds/testid.txt
426	git status >> $resdir/$ds/testid.txt
427	echo >> $resdir/$ds/testid.txt
428	echo >> $resdir/$ds/testid.txt
429	echo ' ---' Output of "'"git diff HEAD"'": >> $resdir/$ds/testid.txt
430	git diff HEAD >> $resdir/$ds/testid.txt
431fi
432___EOF___
433kvm-assign-cpus.sh /sys/devices/system/node > $T/cpuarray.awk
434kvm-get-cpus-script.sh $T/cpuarray.awk $T/dumpbatches.awk
435cat << '___EOF___' >> $T/dumpbatches.awk
436BEGIN {
437	i = 0;
438}
439
440{
441	cf[i] = $1;
442	cpus[i] = $2;
443	i++;
444}
445
446# Dump out the scripting required to run one test batch.
447function dump(first, pastlast, batchnum,  affinitylist)
448{
449	print "echo ----Start batch " batchnum ": `date` | tee -a " rd "log";
450	print "needqemurun="
451	jn=1
452	njitter = 0;
453	split(jitter, ja);
454	if (ja[1] == -1 && ncpus == 0)
455		njitter = 1;
456	else if (ja[1] == -1)
457		njitter = ncpus;
458	else
459		njitter = ja[1];
460	print "TORTURE_JITTER_START=\". jitterstart.sh " njitter " " rd " " dur " " ja[2] " " ja[3] "\"; export TORTURE_JITTER_START";
461	print "TORTURE_JITTER_STOP=\". jitterstop.sh " rd " \"; export TORTURE_JITTER_STOP"
462	for (j = first; j < pastlast; j++) {
463		cpusr[jn] = cpus[j];
464		if (cfrep[cf[j]] == "") {
465			cfr[jn] = cf[j];
466			cfrep[cf[j]] = 1;
467		} else {
468			cfrep[cf[j]]++;
469			cfr[jn] = cf[j] "." cfrep[cf[j]];
470		}
471		builddir=rd cfr[jn] "/build";
472		if (cpusr[jn] > ncpus && ncpus != 0)
473			ovf = "-ovf";
474		else
475			ovf = "";
476		print "echo ", cfr[jn], cpusr[jn] ovf ": Starting build. `date` | tee -a " rd "log";
477		print "mkdir " rd cfr[jn] " || :";
478		print "touch " builddir ".wait";
479		affinitylist = "";
480		if (gotcpus()) {
481			affinitylist = nextcpus(cpusr[jn]);
482		}
483		if (affinitylist ~ /^[0-9,-][0-9,-]*$/)
484			print "export TORTURE_AFFINITY=" affinitylist;
485		else
486			print "export TORTURE_AFFINITY=";
487		print "kvm-test-1-run.sh " CONFIGDIR cf[j], rd cfr[jn], dur " \"" TORTURE_QEMU_ARG "\" \"" TORTURE_BOOTARGS "\" > " rd cfr[jn]  "/kvm-test-1-run.sh.out 2>&1 &"
488		print "echo ", cfr[jn], cpusr[jn] ovf ": Waiting for build to complete. `date` | tee -a " rd "log";
489		print "while test -f " builddir ".wait"
490		print "do"
491		print "\tsleep 1"
492		print "done"
493		print "echo ", cfr[jn], cpusr[jn] ovf ": Build complete. `date` | tee -a " rd "log";
494		jn++;
495	}
496	print "runfiles="
497	for (j = 1; j < jn; j++) {
498		builddir=rd cfr[j] "/build";
499		if (TORTURE_BUILDONLY)
500			print "rm -f " builddir ".ready"
501		else
502			print "mv " builddir ".ready " builddir ".run"
503			print "runfiles=\"$runfiles " builddir ".run\""
504		fi
505		print "if test -f \"" rd cfr[j] "/builtkernel\""
506		print "then"
507		print "\techo ----", cfr[j], cpusr[j] ovf ": Kernel present. `date` | tee -a " rd "log";
508		print "\tneedqemurun=1"
509		print "fi"
510	}
511	if (TORTURE_BUILDONLY && njitter != 0) {
512		njitter = 0;
513		print "echo Build-only run, so suppressing jitter | tee -a " rd "log"
514	}
515	if (TORTURE_BUILDONLY) {
516		print "needqemurun="
517	}
518	print "if test -n \"$needqemurun\""
519	print "then"
520	print "\techo ---- Starting kernels. `date` | tee -a " rd "log";
521	print "\t$TORTURE_JITTER_START";
522	print "\twhile ls $runfiles > /dev/null 2>&1"
523	print "\tdo"
524	print "\t\t:"
525	print "\tdone"
526	print "\t$TORTURE_JITTER_STOP";
527	print "\techo ---- All kernel runs complete. `date` | tee -a " rd "log";
528	print "else"
529	print "\twait"
530	print "\techo ---- No kernel runs. `date` | tee -a " rd "log";
531	print "fi"
532	for (j = 1; j < jn; j++) {
533		print "echo ----", cfr[j], cpusr[j] ovf ": Build/run results: | tee -a " rd "log";
534		print "cat " rd cfr[j]  "/kvm-test-1-run.sh.out | tee -a " rd "log";
535	}
536}
537
538END {
539	njobs = i;
540	nc = ncpus;
541	first = 0;
542	batchnum = 1;
543
544	# Each pass through the following loop considers one test.
545	for (i = 0; i < njobs; i++) {
546		if (ncpus == 0) {
547			# Sequential test specified, each test its own batch.
548			dump(i, i + 1, batchnum);
549			first = i;
550			batchnum++;
551		} else if (nc < cpus[i] && i != 0) {
552			# Out of CPUs, dump out a batch.
553			dump(first, i, batchnum);
554			first = i;
555			nc = ncpus;
556			batchnum++;
557		}
558		# Account for the CPUs needed by the current test.
559		nc -= cpus[i];
560	}
561	# Dump the last batch.
562	if (ncpus != 0)
563		dump(first, i, batchnum);
564}
565___EOF___
566awk < $T/cfgcpu.pack \
567	-v TORTURE_BUILDONLY="$TORTURE_BUILDONLY" \
568	-v CONFIGDIR="$CONFIGFRAG/" \
569	-v KVM="$KVM" \
570	-v ncpus=$cpus \
571	-v jitter="$jitter" \
572	-v rd=$resdir/$ds/ \
573	-v dur=$dur \
574	-v TORTURE_QEMU_ARG="$TORTURE_QEMU_ARG" \
575	-v TORTURE_BOOTARGS="$TORTURE_BOOTARGS" \
576	-f $T/dumpbatches.awk >> $T/script
577echo kvm-end-run-stats.sh "$resdir/$ds" "$starttime" >> $T/script
578
579# Extract the tests and their batches from the script.
580egrep 'Start batch|Starting build\.' $T/script | grep -v ">>" |
581	sed -e 's/:.*$//' -e 's/^echo //' -e 's/-ovf//' |
582	awk '
583	/^----Start/ {
584		batchno = $3;
585		next;
586	}
587	{
588		print batchno, $1, $2
589	}' > $T/batches
590
591# As above, but one line per batch.
592grep -v '^#' $T/batches | awk '
593BEGIN {
594	oldbatch = 1;
595}
596
597{
598	if (oldbatch != $1) {
599		print ++n ". " curbatch;
600		curbatch = "";
601		oldbatch = $1;
602	}
603	curbatch = curbatch " " $2;
604}
605
606END {
607	print ++n ". " curbatch;
608}' > $T/scenarios
609
610if test "$dryrun" = script
611then
612	cat $T/script
613	exit 0
614elif test "$dryrun" = sched
615then
616	# Extract the test run schedule from the script.
617	egrep 'Start batch|Starting build\.' $T/script | grep -v ">>" |
618		sed -e 's/:.*$//' -e 's/^echo //'
619	nbuilds="`grep 'Starting build\.' $T/script |
620		  grep -v ">>" | sed -e 's/:.*$//' -e 's/^echo //' |
621		  awk '{ print $1 }' | grep -v '\.' | wc -l`"
622	echo Total number of builds: $nbuilds
623	nbatches="`grep 'Start batch' $T/script | grep -v ">>" | wc -l`"
624	echo Total number of batches: $nbatches
625	exit 0
626elif test "$dryrun" = batches
627then
628	cat $T/batches
629	exit 0
630elif test "$dryrun" = scenarios
631then
632	cat $T/scenarios
633	exit 0
634else
635	# Not a dryrun.  Record the batches and the number of CPUs, then run the script.
636	bash $T/script
637	ret=$?
638	cp $T/batches $resdir/$ds/batches
639	cp $T/scenarios $resdir/$ds/scenarios
640	echo '#' cpus=$cpus >> $resdir/$ds/batches
641	exit $ret
642fi
643
644# 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
645# Function-graph tracing: ftrace=function_graph ftrace_graph_filter=sched_setaffinity,migration_cpu_stop
646# Also --kconfig "CONFIG_FUNCTION_TRACER=y CONFIG_FUNCTION_GRAPH_TRACER=y"
647# Control buffer size: --bootargs trace_buf_size=3k
648# Get trace-buffer dumps on all oopses: --bootargs ftrace_dump_on_oops
649# Ditto, but dump only the oopsing CPU: --bootargs ftrace_dump_on_oops=orig_cpu
650# Heavy-handed way to also dump on warnings: --bootargs panic_on_warn
651