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
197CONFIGFRAG=${KVM}/configs/${TORTURE_SUITE}; export CONFIGFRAG
198
199if test -z "$configs"
200then
201	configs="`cat $CONFIGFRAG/CFLIST`"
202fi
203
204if test -z "$resdir"
205then
206	resdir=$KVM/res
207fi
208
209# Create a file of test-name/#cpus pairs, sorted by decreasing #cpus.
210touch $T/cfgcpu
211for CF in $configs
212do
213	case $CF in
214	[0-9]\**|[0-9][0-9]\**|[0-9][0-9][0-9]\**)
215		config_reps=`echo $CF | sed -e 's/\*.*$//'`
216		CF1=`echo $CF | sed -e 's/^[^*]*\*//'`
217		;;
218	*)
219		config_reps=1
220		CF1=$CF
221		;;
222	esac
223	if test -f "$CONFIGFRAG/$CF1"
224	then
225		cpu_count=`configNR_CPUS.sh $CONFIGFRAG/$CF1`
226		cpu_count=`configfrag_boot_cpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
227		cpu_count=`configfrag_boot_maxcpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
228		for ((cur_rep=0;cur_rep<$config_reps;cur_rep++))
229		do
230			echo $CF1 $cpu_count >> $T/cfgcpu
231		done
232	else
233		echo "The --configs file $CF1 does not exist, terminating."
234		exit 1
235	fi
236done
237sort -k2nr $T/cfgcpu -T="$T" > $T/cfgcpu.sort
238
239# Use a greedy bin-packing algorithm, sorting the list accordingly.
240awk < $T/cfgcpu.sort > $T/cfgcpu.pack -v ncpus=$cpus '
241BEGIN {
242	njobs = 0;
243}
244
245{
246	# Read file of tests and corresponding required numbers of CPUs.
247	cf[njobs] = $1;
248	cpus[njobs] = $2;
249	njobs++;
250}
251
252END {
253	batch = 0;
254	nc = -1;
255
256	# Each pass through the following loop creates on test batch
257	# that can be executed concurrently given ncpus.  Note that a
258	# given test that requires more than the available CPUs will run in
259	# their own batch.  Such tests just have to make do with what
260	# is available.
261	while (nc != ncpus) {
262		batch++;
263		nc = ncpus;
264
265		# Each pass through the following loop considers one
266		# test for inclusion in the current batch.
267		for (i = 0; i < njobs; i++) {
268			if (done[i])
269				continue; # Already part of a batch.
270			if (nc >= cpus[i] || nc == ncpus) {
271
272				# This test fits into the current batch.
273				done[i] = batch;
274				nc -= cpus[i];
275				if (nc <= 0)
276					break; # Too-big test in its own batch.
277			}
278		}
279	}
280
281	# Dump out the tests in batch order.
282	for (b = 1; b <= batch; b++)
283		for (i = 0; i < njobs; i++)
284			if (done[i] == b)
285				print cf[i], cpus[i];
286}'
287
288# Generate a script to execute the tests in appropriate batches.
289cat << ___EOF___ > $T/script
290CONFIGFRAG="$CONFIGFRAG"; export CONFIGFRAG
291KVM="$KVM"; export KVM
292PATH="$PATH"; export PATH
293TORTURE_BOOT_IMAGE="$TORTURE_BOOT_IMAGE"; export TORTURE_BOOT_IMAGE
294TORTURE_BUILDONLY="$TORTURE_BUILDONLY"; export TORTURE_BUILDONLY
295TORTURE_DEFCONFIG="$TORTURE_DEFCONFIG"; export TORTURE_DEFCONFIG
296TORTURE_INITRD="$TORTURE_INITRD"; export TORTURE_INITRD
297TORTURE_KCONFIG_ARG="$TORTURE_KCONFIG_ARG"; export TORTURE_KCONFIG_ARG
298TORTURE_KMAKE_ARG="$TORTURE_KMAKE_ARG"; export TORTURE_KMAKE_ARG
299TORTURE_QEMU_CMD="$TORTURE_QEMU_CMD"; export TORTURE_QEMU_CMD
300TORTURE_QEMU_INTERACTIVE="$TORTURE_QEMU_INTERACTIVE"; export TORTURE_QEMU_INTERACTIVE
301TORTURE_QEMU_MAC="$TORTURE_QEMU_MAC"; export TORTURE_QEMU_MAC
302TORTURE_QEMU_MEM="$TORTURE_QEMU_MEM"; export TORTURE_QEMU_MEM
303TORTURE_SHUTDOWN_GRACE="$TORTURE_SHUTDOWN_GRACE"; export TORTURE_SHUTDOWN_GRACE
304TORTURE_SUITE="$TORTURE_SUITE"; export TORTURE_SUITE
305if ! test -e $resdir
306then
307	mkdir -p "$resdir" || :
308fi
309mkdir $resdir/$ds
310echo Results directory: $resdir/$ds
311echo $scriptname $args
312touch $resdir/$ds/log
313echo $scriptname $args >> $resdir/$ds/log
314echo ${TORTURE_SUITE} > $resdir/$ds/TORTURE_SUITE
315pwd > $resdir/$ds/testid.txt
316if test -d .git
317then
318	git status >> $resdir/$ds/testid.txt
319	git rev-parse HEAD >> $resdir/$ds/testid.txt
320	git diff HEAD >> $resdir/$ds/testid.txt
321fi
322___EOF___
323awk < $T/cfgcpu.pack \
324	-v TORTURE_BUILDONLY="$TORTURE_BUILDONLY" \
325	-v CONFIGDIR="$CONFIGFRAG/" \
326	-v KVM="$KVM" \
327	-v ncpus=$cpus \
328	-v jitter="$jitter" \
329	-v rd=$resdir/$ds/ \
330	-v dur=$dur \
331	-v TORTURE_QEMU_ARG="$TORTURE_QEMU_ARG" \
332	-v TORTURE_BOOTARGS="$TORTURE_BOOTARGS" \
333'BEGIN {
334	i = 0;
335}
336
337{
338	cf[i] = $1;
339	cpus[i] = $2;
340	i++;
341}
342
343# Dump out the scripting required to run one test batch.
344function dump(first, pastlast, batchnum)
345{
346	print "echo ----Start batch " batchnum ": `date` | tee -a " rd "log";
347	print "needqemurun="
348	jn=1
349	for (j = first; j < pastlast; j++) {
350		builddir=KVM "/b" jn
351		cpusr[jn] = cpus[j];
352		if (cfrep[cf[j]] == "") {
353			cfr[jn] = cf[j];
354			cfrep[cf[j]] = 1;
355		} else {
356			cfrep[cf[j]]++;
357			cfr[jn] = cf[j] "." cfrep[cf[j]];
358		}
359		if (cpusr[jn] > ncpus && ncpus != 0)
360			ovf = "-ovf";
361		else
362			ovf = "";
363		print "echo ", cfr[jn], cpusr[jn] ovf ": Starting build. `date` | tee -a " rd "log";
364		print "rm -f " builddir ".*";
365		print "touch " builddir ".wait";
366		print "mkdir " builddir " > /dev/null 2>&1 || :";
367		print "mkdir " rd cfr[jn] " || :";
368		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 &"
369		print "echo ", cfr[jn], cpusr[jn] ovf ": Waiting for build to complete. `date` | tee -a " rd "log";
370		print "while test -f " builddir ".wait"
371		print "do"
372		print "\tsleep 1"
373		print "done"
374		print "echo ", cfr[jn], cpusr[jn] ovf ": Build complete. `date` | tee -a " rd "log";
375		jn++;
376	}
377	for (j = 1; j < jn; j++) {
378		builddir=KVM "/b" j
379		print "rm -f " builddir ".ready"
380		print "if test -f \"" rd cfr[j] "/builtkernel\""
381		print "then"
382		print "\techo ----", cfr[j], cpusr[j] ovf ": Kernel present. `date` | tee -a " rd "log";
383		print "\tneedqemurun=1"
384		print "fi"
385	}
386	njitter = 0;
387	split(jitter, ja);
388	if (ja[1] == -1 && ncpus == 0)
389		njitter = 1;
390	else if (ja[1] == -1)
391		njitter = ncpus;
392	else
393		njitter = ja[1];
394	if (TORTURE_BUILDONLY && njitter != 0) {
395		njitter = 0;
396		print "echo Build-only run, so suppressing jitter | tee -a " rd "log"
397	}
398	if (TORTURE_BUILDONLY) {
399		print "needqemurun="
400	}
401	print "if test -n \"$needqemurun\""
402	print "then"
403	print "\techo ---- Starting kernels. `date` | tee -a " rd "log";
404	for (j = 0; j < njitter; j++)
405		print "\tjitter.sh " j " " dur " " ja[2] " " ja[3] "&"
406	print "\twait"
407	print "\techo ---- All kernel runs complete. `date` | tee -a " rd "log";
408	print "else"
409	print "\twait"
410	print "\techo ---- No kernel runs. `date` | tee -a " rd "log";
411	print "fi"
412	for (j = 1; j < jn; j++) {
413		builddir=KVM "/b" j
414		print "echo ----", cfr[j], cpusr[j] ovf ": Build/run results: | tee -a " rd "log";
415		print "cat " rd cfr[j]  "/kvm-test-1-run.sh.out | tee -a " rd "log";
416	}
417}
418
419END {
420	njobs = i;
421	nc = ncpus;
422	first = 0;
423	batchnum = 1;
424
425	# Each pass through the following loop considers one test.
426	for (i = 0; i < njobs; i++) {
427		if (ncpus == 0) {
428			# Sequential test specified, each test its own batch.
429			dump(i, i + 1, batchnum);
430			first = i;
431			batchnum++;
432		} else if (nc < cpus[i] && i != 0) {
433			# Out of CPUs, dump out a batch.
434			dump(first, i, batchnum);
435			first = i;
436			nc = ncpus;
437			batchnum++;
438		}
439		# Account for the CPUs needed by the current test.
440		nc -= cpus[i];
441	}
442	# Dump the last batch.
443	if (ncpus != 0)
444		dump(first, i, batchnum);
445}' >> $T/script
446
447cat << ___EOF___ >> $T/script
448echo
449echo
450echo " --- `date` Test summary:"
451echo Results directory: $resdir/$ds
452kvm-recheck.sh $resdir/$ds
453___EOF___
454
455if test "$dryrun" = script
456then
457	cat $T/script
458	exit 0
459elif test "$dryrun" = sched
460then
461	# Extract the test run schedule from the script.
462	egrep 'Start batch|Starting build\.' $T/script |
463		grep -v ">>" |
464		sed -e 's/:.*$//' -e 's/^echo //'
465	exit 0
466else
467	# Not a dryrun, so run the script.
468	sh $T/script
469fi
470
471# 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
472