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