kvm.sh 13.6 KB
Newer Older
1
#!/bin/bash
2
# SPDX-License-Identifier: GPL-2.0+
3
#
4 5 6
# Run a series of tests under KVM.  By default, this series is specified
# by the relevant CFLIST file, but can be overridden by the --configs
# command-line argument.
7
#
8
# Usage: kvm.sh [ options ]
9 10 11
#
# Copyright (C) IBM Corporation, 2011
#
12
# Authors: Paul E. McKenney <paulmck@linux.ibm.com>
13 14

scriptname=$0
15
args="$*"
16

17
T=${TMPDIR-/tmp}/kvm.sh.$$
18 19 20
trap 'rm -rf $T' 0
mkdir $T

21 22
cd `dirname $scriptname`/../../../../../

23
dur=$((30*60))
24
dryrun=""
25
KVM="`pwd`/tools/testing/selftests/rcutorture"; export KVM
26
PATH=${KVM}/bin:$PATH; export PATH
27 28 29
. functions.sh

TORTURE_ALLOTED_CPUS="`identify_qemu_vcpus`"
30
TORTURE_DEFCONFIG=defconfig
31
TORTURE_BOOT_IMAGE=""
32
TORTURE_INITRD="$KVM/initrd"; export TORTURE_INITRD
33
TORTURE_KCONFIG_ARG=""
34
TORTURE_KCONFIG_KASAN_ARG=""
35
TORTURE_KCONFIG_KCSAN_ARG=""
36
TORTURE_KMAKE_ARG=""
37
TORTURE_QEMU_MEM=512
38
TORTURE_SHUTDOWN_GRACE=180
39
TORTURE_SUITE=rcu
40
TORTURE_TRUST_MAKE=""
41
resdir=""
42
configs=""
43
cpus=0
44
ds=`date +%Y.%m.%d-%H.%M.%S`
45
jitter="-1"
46 47 48

usage () {
	echo "Usage: $scriptname optional arguments:"
49
	echo "       --bootargs kernel-boot-arguments"
50
	echo "       --bootimage relative-path-to-kernel-boot-image"
51
	echo "       --buildonly"
52
	echo "       --configs \"config-file list w/ repeat factor (3*TINY01)\""
53
	echo "       --cpus N"
54
	echo "       --datestamp string"
55
	echo "       --defconfig string"
56
	echo "       --dryrun sched|script"
57
	echo "       --duration minutes"
58
	echo "       --interactive"
59
	echo "       --jitter N [ maxsleep (us) [ maxspin (us) ] ]"
60
	echo "       --kconfig Kconfig-options"
61
	echo "       --kmake-arg kernel-make-arguments"
62
	echo "       --mac nn:nn:nn:nn:nn:nn"
63
	echo "       --memory megabytes | nnnG"
64
	echo "       --no-initrd"
65
	echo "       --qemu-args qemu-arguments"
66
	echo "       --qemu-cmd qemu-system-..."
67 68
	echo "       --results absolute-pathname"
	echo "       --torture rcu"
69
	echo "       --trust-make"
70 71 72 73 74 75
	exit 1
}

while test $# -gt 0
do
	case "$1" in
76 77 78 79
	--allcpus)
		cpus=$TORTURE_ALLOTED_CPUS
		max_cpus=$TORTURE_ALLOTED_CPUS
		;;
80
	--bootargs|--bootarg)
81
		checkarg --bootargs "(list of kernel boot arguments)" "$#" "$2" '.*' '^--'
82
		TORTURE_BOOTARGS="$2"
83 84
		shift
		;;
85 86 87 88 89
	--bootimage)
		checkarg --bootimage "(relative path to kernel boot image)" "$#" "$2" '[a-zA-Z0-9][a-zA-Z0-9_]*' '^--'
		TORTURE_BOOT_IMAGE="$2"
		shift
		;;
90
	--buildonly)
91
		TORTURE_BUILDONLY=1
92
		;;
93
	--configs|--config)
94 95 96 97
		checkarg --configs "(list of config files)" "$#" "$2" '^[^/]*$' '^--'
		configs="$2"
		shift
		;;
98 99 100
	--cpus)
		checkarg --cpus "(number)" "$#" "$2" '^[0-9]*$' '^--'
		cpus=$2
101
		TORTURE_ALLOTED_CPUS="$2"
102 103 104 105 106
		max_cpus="`identify_qemu_vcpus`"
		if test "$TORTURE_ALLOTED_CPUS" -gt "$max_cpus"
		then
			TORTURE_ALLOTED_CPUS=$max_cpus
		fi
107 108
		shift
		;;
109 110 111 112 113
	--datestamp)
		checkarg --datestamp "(relative pathname)" "$#" "$2" '^[^/]*$' '^--'
		ds=$2
		shift
		;;
114 115 116 117 118
	--defconfig)
		checkarg --defconfig "defconfigtype" "$#" "$2" '^[^/][^/]*$' '^--'
		TORTURE_DEFCONFIG=$2
		shift
		;;
119 120 121 122 123
	--dryrun)
		checkarg --dryrun "sched|script" $# "$2" 'sched\|script' '^--'
		dryrun=$2
		shift
		;;
124
	--duration)
125
		checkarg --duration "(minutes)" $# "$2" '^[0-9]*$' '^error'
126
		dur=$(($2*60))
127 128
		shift
		;;
129
	--interactive)
130
		TORTURE_QEMU_INTERACTIVE=1; export TORTURE_QEMU_INTERACTIVE
131
		;;
132 133 134 135 136
	--jitter)
		checkarg --jitter "(# threads [ sleep [ spin ] ])" $# "$2" '^-\{,1\}[0-9]\+\( \+[0-9]\+\)\{,2\} *$' '^error$'
		jitter="$2"
		shift
		;;
137 138 139 140 141
	--kconfig)
		checkarg --kconfig "(Kconfig options)" $# "$2" '^CONFIG_[A-Z0-9_]\+=\([ynm]\|[0-9]\+\)\( CONFIG_[A-Z0-9_]\+=\([ynm]\|[0-9]\+\)\)*$' '^error$'
		TORTURE_KCONFIG_ARG="$2"
		shift
		;;
142 143 144
	--kasan)
		TORTURE_KCONFIG_KASAN_ARG="CONFIG_DEBUG_INFO=y CONFIG_KASAN=y"; export TORTURE_KCONFIG_KASAN_ARG
		;;
145 146 147
	--kcsan)
		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_VERBOSE=y CONFIG_KCSAN_INTERRUPT_WATCHER=y"; export TORTURE_KCONFIG_KCSAN_ARG
		;;
148 149
	--kmake-arg)
		checkarg --kmake-arg "(kernel make arguments)" $# "$2" '.*' '^error$'
150
		TORTURE_KMAKE_ARG="$2"
151 152
		shift
		;;
153 154
	--mac)
		checkarg --mac "(MAC address)" $# "$2" '^\([0-9a-fA-F]\{2\}:\)\{5\}[0-9a-fA-F]\{2\}$' error
155
		TORTURE_QEMU_MAC=$2
156 157
		shift
		;;
158 159 160 161 162
	--memory)
		checkarg --memory "(memory size)" $# "$2" '^[0-9]\+[MG]\?$' error
		TORTURE_QEMU_MEM=$2
		shift
		;;
163
	--no-initrd)
164
		TORTURE_INITRD=""; export TORTURE_INITRD
165
		;;
166
	--qemu-args|--qemu-arg)
167
		checkarg --qemu-args "(qemu arguments)" $# "$2" '^-' '^error'
168
		TORTURE_QEMU_ARG="$2"
169 170
		shift
		;;
171 172
	--qemu-cmd)
		checkarg --qemu-cmd "(qemu-system-...)" $# "$2" 'qemu-system-' '^--'
173
		TORTURE_QEMU_CMD="$2"
174 175
		shift
		;;
176
	--results)
177
		checkarg --results "(absolute pathname)" "$#" "$2" '^/' '^error'
178 179 180
		resdir=$2
		shift
		;;
181 182 183 184 185
	--shutdown-grace)
		checkarg --shutdown-grace "(seconds)" "$#" "$2" '^[0-9]*$' '^error'
		TORTURE_SHUTDOWN_GRACE=$2
		shift
		;;
186
	--torture)
187
		checkarg --torture "(suite name)" "$#" "$2" '^\(lock\|rcu\|rcuperf\)$' '^--'
188 189
		TORTURE_SUITE=$2
		shift
190 191 192 193 194 195
		if test "$TORTURE_SUITE" = rcuperf
		then
			# If you really want jitter for rcuperf, specify
			# it after specifying rcuperf.  (But why?)
			jitter=0
		fi
196
		;;
197 198 199
	--trust-make)
		TORTURE_TRUST_MAKE="y"
		;;
200
	*)
201
		echo Unknown argument $1
202 203 204 205 206 207
		usage
		;;
	esac
	shift
done

208 209 210 211 212 213 214 215
if test -z "$TORTURE_INITRD" || tools/testing/selftests/rcutorture/bin/mkinitrd.sh
then
	:
else
	echo No initrd and unable to create one, aborting test >&2
	exit 1
fi

216
CONFIGFRAG=${KVM}/configs/${TORTURE_SUITE}; export CONFIGFRAG
217

218
defaultconfigs="`tr '\012' ' ' < $CONFIGFRAG/CFLIST`"
219 220
if test -z "$configs"
then
221
	configs=$defaultconfigs
222
fi
223 224 225 226

if test -z "$resdir"
then
	resdir=$KVM/res
227 228
fi

229
# Create a file of test-name/#cpus pairs, sorted by decreasing #cpus.
230
configs_derep=
231 232
for CF in $configs
do
233 234 235 236 237 238 239 240 241 242
	case $CF in
	[0-9]\**|[0-9][0-9]\**|[0-9][0-9][0-9]\**)
		config_reps=`echo $CF | sed -e 's/\*.*$//'`
		CF1=`echo $CF | sed -e 's/^[^*]*\*//'`
		;;
	*)
		config_reps=1
		CF1=$CF
		;;
	esac
243 244 245 246 247 248 249 250 251
	for ((cur_rep=0;cur_rep<$config_reps;cur_rep++))
	do
		configs_derep="$configs_derep $CF1"
	done
done
touch $T/cfgcpu
configs_derep="`echo $configs_derep | sed -e "s/\<CFLIST\>/$defaultconfigs/g"`"
for CF1 in $configs_derep
do
252
	if test -f "$CONFIGFRAG/$CF1"
253
	then
254 255
		cpu_count=`configNR_CPUS.sh $CONFIGFRAG/$CF1`
		cpu_count=`configfrag_boot_cpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
256
		cpu_count=`configfrag_boot_maxcpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
257
		echo $CF1 $cpu_count >> $T/cfgcpu
258
	else
259
		echo "The --configs file $CF1 does not exist, terminating."
260
		exit 1
261
	fi
262
done
263
sort -k2nr $T/cfgcpu -T="$T" > $T/cfgcpu.sort
264

265
# Use a greedy bin-packing algorithm, sorting the list accordingly.
266 267 268 269 270 271
awk < $T/cfgcpu.sort > $T/cfgcpu.pack -v ncpus=$cpus '
BEGIN {
	njobs = 0;
}

{
272
	# Read file of tests and corresponding required numbers of CPUs.
273 274 275 276 277 278 279 280
	cf[njobs] = $1;
	cpus[njobs] = $2;
	njobs++;
}

END {
	batch = 0;
	nc = -1;
281 282 283 284 285 286

	# Each pass through the following loop creates on test batch
	# that can be executed concurrently given ncpus.  Note that a
	# given test that requires more than the available CPUs will run in
	# their own batch.  Such tests just have to make do with what
	# is available.
287 288 289
	while (nc != ncpus) {
		batch++;
		nc = ncpus;
290 291 292

		# Each pass through the following loop considers one
		# test for inclusion in the current batch.
293 294
		for (i = 0; i < njobs; i++) {
			if (done[i])
295
				continue; # Already part of a batch.
296
			if (nc >= cpus[i] || nc == ncpus) {
297 298

				# This test fits into the current batch.
299 300 301
				done[i] = batch;
				nc -= cpus[i];
				if (nc <= 0)
302
					break; # Too-big test in its own batch.
303 304 305
			}
		}
	}
306 307

	# Dump out the tests in batch order.
308 309 310 311 312 313
	for (b = 1; b <= batch; b++)
		for (i = 0; i < njobs; i++)
			if (done[i] == b)
				print cf[i], cpus[i];
}'

314
# Generate a script to execute the tests in appropriate batches.
315
cat << ___EOF___ > $T/script
316 317 318
CONFIGFRAG="$CONFIGFRAG"; export CONFIGFRAG
KVM="$KVM"; export KVM
PATH="$PATH"; export PATH
319
TORTURE_ALLOTED_CPUS="$TORTURE_ALLOTED_CPUS"; export TORTURE_ALLOTED_CPUS
320
TORTURE_BOOT_IMAGE="$TORTURE_BOOT_IMAGE"; export TORTURE_BOOT_IMAGE
321
TORTURE_BUILDONLY="$TORTURE_BUILDONLY"; export TORTURE_BUILDONLY
322
TORTURE_DEFCONFIG="$TORTURE_DEFCONFIG"; export TORTURE_DEFCONFIG
323
TORTURE_INITRD="$TORTURE_INITRD"; export TORTURE_INITRD
324
TORTURE_KCONFIG_ARG="$TORTURE_KCONFIG_ARG"; export TORTURE_KCONFIG_ARG
325
TORTURE_KCONFIG_KASAN_ARG="$TORTURE_KCONFIG_KASAN_ARG"; export TORTURE_KCONFIG_KASAN_ARG
326
TORTURE_KCONFIG_KCSAN_ARG="$TORTURE_KCONFIG_KCSAN_ARG"; export TORTURE_KCONFIG_KCSAN_ARG
327 328 329 330
TORTURE_KMAKE_ARG="$TORTURE_KMAKE_ARG"; export TORTURE_KMAKE_ARG
TORTURE_QEMU_CMD="$TORTURE_QEMU_CMD"; export TORTURE_QEMU_CMD
TORTURE_QEMU_INTERACTIVE="$TORTURE_QEMU_INTERACTIVE"; export TORTURE_QEMU_INTERACTIVE
TORTURE_QEMU_MAC="$TORTURE_QEMU_MAC"; export TORTURE_QEMU_MAC
331
TORTURE_QEMU_MEM="$TORTURE_QEMU_MEM"; export TORTURE_QEMU_MEM
332
TORTURE_SHUTDOWN_GRACE="$TORTURE_SHUTDOWN_GRACE"; export TORTURE_SHUTDOWN_GRACE
333
TORTURE_SUITE="$TORTURE_SUITE"; export TORTURE_SUITE
334
TORTURE_TRUST_MAKE="$TORTURE_TRUST_MAKE"; export TORTURE_TRUST_MAKE
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
if ! test -e $resdir
then
	mkdir -p "$resdir" || :
fi
mkdir $resdir/$ds
echo Results directory: $resdir/$ds
echo $scriptname $args
touch $resdir/$ds/log
echo $scriptname $args >> $resdir/$ds/log
echo ${TORTURE_SUITE} > $resdir/$ds/TORTURE_SUITE
pwd > $resdir/$ds/testid.txt
if test -d .git
then
	git status >> $resdir/$ds/testid.txt
	git rev-parse HEAD >> $resdir/$ds/testid.txt
350
	git diff HEAD >> $resdir/$ds/testid.txt
351
fi
352
___EOF___
353
awk < $T/cfgcpu.pack \
354
	-v TORTURE_BUILDONLY="$TORTURE_BUILDONLY" \
355
	-v CONFIGDIR="$CONFIGFRAG/" \
356 357
	-v KVM="$KVM" \
	-v ncpus=$cpus \
358
	-v jitter="$jitter" \
359 360
	-v rd=$resdir/$ds/ \
	-v dur=$dur \
361 362
	-v TORTURE_QEMU_ARG="$TORTURE_QEMU_ARG" \
	-v TORTURE_BOOTARGS="$TORTURE_BOOTARGS" \
363 364 365 366 367 368 369 370 371 372
'BEGIN {
	i = 0;
}

{
	cf[i] = $1;
	cpus[i] = $2;
	i++;
}

373
# Dump out the scripting required to run one test batch.
374
function dump(first, pastlast, batchnum)
375
{
376
	print "echo ----Start batch " batchnum ": `date` | tee -a " rd "log";
377
	print "needqemurun="
378 379
	jn=1
	for (j = first; j < pastlast; j++) {
380
		builddir=KVM "/b" j - first + 1
381
		cpusr[jn] = cpus[j];
382
		if (cfrep[cf[j]] == "") {
383
			cfr[jn] = cf[j];
384 385 386
			cfrep[cf[j]] = 1;
		} else {
			cfrep[cf[j]]++;
387
			cfr[jn] = cf[j] "." cfrep[cf[j]];
388
		}
389
		if (cpusr[jn] > ncpus && ncpus != 0)
390
			ovf = "-ovf";
391 392
		else
			ovf = "";
393
		print "echo ", cfr[jn], cpusr[jn] ovf ": Starting build. `date` | tee -a " rd "log";
394 395 396
		print "rm -f " builddir ".*";
		print "touch " builddir ".wait";
		print "mkdir " rd cfr[jn] " || :";
397
		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 &"
398
		print "echo ", cfr[jn], cpusr[jn] ovf ": Waiting for build to complete. `date` | tee -a " rd "log";
399 400 401 402
		print "while test -f " builddir ".wait"
		print "do"
		print "\tsleep 1"
		print "done"
403
		print "echo ", cfr[jn], cpusr[jn] ovf ": Build complete. `date` | tee -a " rd "log";
404 405 406 407 408
		jn++;
	}
	for (j = 1; j < jn; j++) {
		builddir=KVM "/b" j
		print "rm -f " builddir ".ready"
409
		print "if test -f \"" rd cfr[j] "/builtkernel\""
410
		print "then"
411
		print "\techo ----", cfr[j], cpusr[j] ovf ": Kernel present. `date` | tee -a " rd "log";
412
		print "\tneedqemurun=1"
413
		print "fi"
414
	}
415 416 417 418 419 420 421 422
	njitter = 0;
	split(jitter, ja);
	if (ja[1] == -1 && ncpus == 0)
		njitter = 1;
	else if (ja[1] == -1)
		njitter = ncpus;
	else
		njitter = ja[1];
423 424
	if (TORTURE_BUILDONLY && njitter != 0) {
		njitter = 0;
425
		print "echo Build-only run, so suppressing jitter | tee -a " rd "log"
426
	}
427 428 429 430
	if (TORTURE_BUILDONLY) {
		print "needqemurun="
	}
	print "if test -n \"$needqemurun\""
431
	print "then"
432
	print "\techo ---- Starting kernels. `date` | tee -a " rd "log";
433 434 435
	for (j = 0; j < njitter; j++)
		print "\tjitter.sh " j " " dur " " ja[2] " " ja[3] "&"
	print "\twait"
436
	print "\techo ---- All kernel runs complete. `date` | tee -a " rd "log";
437 438
	print "else"
	print "\twait"
439
	print "\techo ---- No kernel runs. `date` | tee -a " rd "log";
440
	print "fi"
441 442
	for (j = 1; j < jn; j++) {
		builddir=KVM "/b" j
443 444
		print "echo ----", cfr[j], cpusr[j] ovf ": Build/run results: | tee -a " rd "log";
		print "cat " rd cfr[j]  "/kvm-test-1-run.sh.out | tee -a " rd "log";
445 446 447 448 449 450 451
	}
}

END {
	njobs = i;
	nc = ncpus;
	first = 0;
452
	batchnum = 1;
453 454

	# Each pass through the following loop considers one test.
455 456
	for (i = 0; i < njobs; i++) {
		if (ncpus == 0) {
457
			# Sequential test specified, each test its own batch.
458
			dump(i, i + 1, batchnum);
459
			first = i;
460
			batchnum++;
461
		} else if (nc < cpus[i] && i != 0) {
462
			# Out of CPUs, dump out a batch.
463
			dump(first, i, batchnum);
464 465
			first = i;
			nc = ncpus;
466
			batchnum++;
467
		}
468
		# Account for the CPUs needed by the current test.
469 470
		nc -= cpus[i];
	}
471
	# Dump the last batch.
472
	if (ncpus != 0)
473
		dump(first, i, batchnum);
474
}' >> $T/script
475

476 477 478 479 480
cat << ___EOF___ >> $T/script
echo
echo
echo " --- `date` Test summary:"
echo Results directory: $resdir/$ds
481
kcsan-collapse.sh $resdir/$ds
482
kvm-recheck.sh $resdir/$ds
483 484
___EOF___

485 486 487 488 489 490
if test "$dryrun" = script
then
	cat $T/script
	exit 0
elif test "$dryrun" = sched
then
491
	# Extract the test run schedule from the script.
492 493
	egrep 'Start batch|Starting build\.' $T/script |
		grep -v ">>" |
494 495 496
		sed -e 's/:.*$//' -e 's/^echo //'
	exit 0
else
497
	# Not a dryrun, so run the script.
498 499
	sh $T/script
fi
500

501
# 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
502 503
# Function-graph tracing: ftrace=function_graph ftrace_graph_filter=sched_setaffinity,migration_cpu_stop
# Also --kconfig "CONFIG_FUNCTION_TRACER=y CONFIG_FUNCTION_GRAPH_TRACER=y"