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