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