kvm.sh 12.3 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
TORTURE_DEFCONFIG=defconfig
28
TORTURE_BOOT_IMAGE=""
29
TORTURE_INITRD="$KVM/initrd"; export TORTURE_INITRD
30
TORTURE_KCONFIG_ARG=""
31
TORTURE_KMAKE_ARG=""
32
TORTURE_QEMU_MEM=512
33
TORTURE_SHUTDOWN_GRACE=180
34
TORTURE_SUITE=rcu
35
resdir=""
36
configs=""
37
cpus=0
38
ds=`date +%Y.%m.%d-%H:%M:%S`
39
jitter="-1"
40

41 42
. functions.sh

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

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

184 185 186 187 188 189 190 191
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

192
CONFIGFRAG=${KVM}/configs/${TORTURE_SUITE}; export CONFIGFRAG
193 194 195

if test -z "$configs"
then
196
	configs="`cat $CONFIGFRAG/CFLIST`"
197
fi
198 199 200 201

if test -z "$resdir"
then
	resdir=$KVM/res
202 203
fi

204
# Create a file of test-name/#cpus pairs, sorted by decreasing #cpus.
205
touch $T/cfgcpu
206 207
for CF in $configs
do
208 209 210 211 212 213 214 215 216 217 218
	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
	if test -f "$CONFIGFRAG/$CF1"
219
	then
220 221
		cpu_count=`configNR_CPUS.sh $CONFIGFRAG/$CF1`
		cpu_count=`configfrag_boot_cpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
222
		cpu_count=`configfrag_boot_maxcpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
223 224 225 226
		for ((cur_rep=0;cur_rep<$config_reps;cur_rep++))
		do
			echo $CF1 $cpu_count >> $T/cfgcpu
		done
227
	else
228
		echo "The --configs file $CF1 does not exist, terminating."
229
		exit 1
230
	fi
231
done
232
sort -k2nr $T/cfgcpu -T="$T" > $T/cfgcpu.sort
233

234
# Use a greedy bin-packing algorithm, sorting the list accordingly.
235 236 237 238 239 240
awk < $T/cfgcpu.sort > $T/cfgcpu.pack -v ncpus=$cpus '
BEGIN {
	njobs = 0;
}

{
241
	# Read file of tests and corresponding required numbers of CPUs.
242 243 244 245 246 247 248 249
	cf[njobs] = $1;
	cpus[njobs] = $2;
	njobs++;
}

END {
	batch = 0;
	nc = -1;
250 251 252 253 254 255

	# 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.
256 257 258
	while (nc != ncpus) {
		batch++;
		nc = ncpus;
259 260 261

		# Each pass through the following loop considers one
		# test for inclusion in the current batch.
262 263
		for (i = 0; i < njobs; i++) {
			if (done[i])
264
				continue; # Already part of a batch.
265
			if (nc >= cpus[i] || nc == ncpus) {
266 267

				# This test fits into the current batch.
268 269 270
				done[i] = batch;
				nc -= cpus[i];
				if (nc <= 0)
271
					break; # Too-big test in its own batch.
272 273 274
			}
		}
	}
275 276

	# Dump out the tests in batch order.
277 278 279 280 281 282
	for (b = 1; b <= batch; b++)
		for (i = 0; i < njobs; i++)
			if (done[i] == b)
				print cf[i], cpus[i];
}'

283
# Generate a script to execute the tests in appropriate batches.
284
cat << ___EOF___ > $T/script
285 286 287
CONFIGFRAG="$CONFIGFRAG"; export CONFIGFRAG
KVM="$KVM"; export KVM
PATH="$PATH"; export PATH
288
TORTURE_BOOT_IMAGE="$TORTURE_BOOT_IMAGE"; export TORTURE_BOOT_IMAGE
289
TORTURE_BUILDONLY="$TORTURE_BUILDONLY"; export TORTURE_BUILDONLY
290
TORTURE_DEFCONFIG="$TORTURE_DEFCONFIG"; export TORTURE_DEFCONFIG
291
TORTURE_INITRD="$TORTURE_INITRD"; export TORTURE_INITRD
292
TORTURE_KCONFIG_ARG="$TORTURE_KCONFIG_ARG"; export TORTURE_KCONFIG_ARG
293 294 295 296
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
297
TORTURE_QEMU_MEM="$TORTURE_QEMU_MEM"; export TORTURE_QEMU_MEM
298
TORTURE_SHUTDOWN_GRACE="$TORTURE_SHUTDOWN_GRACE"; export TORTURE_SHUTDOWN_GRACE
299
TORTURE_SUITE="$TORTURE_SUITE"; export TORTURE_SUITE
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
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
315
	git diff HEAD >> $resdir/$ds/testid.txt
316
fi
317
___EOF___
318
awk < $T/cfgcpu.pack \
319
	-v TORTURE_BUILDONLY="$TORTURE_BUILDONLY" \
320
	-v CONFIGDIR="$CONFIGFRAG/" \
321 322
	-v KVM="$KVM" \
	-v ncpus=$cpus \
323
	-v jitter="$jitter" \
324 325
	-v rd=$resdir/$ds/ \
	-v dur=$dur \
326 327
	-v TORTURE_QEMU_ARG="$TORTURE_QEMU_ARG" \
	-v TORTURE_BOOTARGS="$TORTURE_BOOTARGS" \
328 329 330 331 332 333 334 335 336 337
'BEGIN {
	i = 0;
}

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

338
# Dump out the scripting required to run one test batch.
339
function dump(first, pastlast, batchnum)
340
{
341
	print "echo ----Start batch " batchnum ": `date` | tee -a " rd "log";
342
	print "needqemurun="
343 344
	jn=1
	for (j = first; j < pastlast; j++) {
345
		builddir=KVM "/b1"
346
		cpusr[jn] = cpus[j];
347
		if (cfrep[cf[j]] == "") {
348
			cfr[jn] = cf[j];
349 350 351
			cfrep[cf[j]] = 1;
		} else {
			cfrep[cf[j]]++;
352
			cfr[jn] = cf[j] "." cfrep[cf[j]];
353
		}
354
		if (cpusr[jn] > ncpus && ncpus != 0)
355
			ovf = "-ovf";
356 357
		else
			ovf = "";
358
		print "echo ", cfr[jn], cpusr[jn] ovf ": Starting build. `date` | tee -a " rd "log";
359 360 361 362
		print "rm -f " builddir ".*";
		print "touch " builddir ".wait";
		print "mkdir " builddir " > /dev/null 2>&1 || :";
		print "mkdir " rd cfr[jn] " || :";
363
		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 &"
364
		print "echo ", cfr[jn], cpusr[jn] ovf ": Waiting for build to complete. `date` | tee -a " rd "log";
365 366 367 368
		print "while test -f " builddir ".wait"
		print "do"
		print "\tsleep 1"
		print "done"
369
		print "echo ", cfr[jn], cpusr[jn] ovf ": Build complete. `date` | tee -a " rd "log";
370 371 372 373 374
		jn++;
	}
	for (j = 1; j < jn; j++) {
		builddir=KVM "/b" j
		print "rm -f " builddir ".ready"
375
		print "if test -f \"" rd cfr[j] "/builtkernel\""
376
		print "then"
377
		print "\techo ----", cfr[j], cpusr[j] ovf ": Kernel present. `date` | tee -a " rd "log";
378
		print "\tneedqemurun=1"
379
		print "fi"
380
	}
381 382 383 384 385 386 387 388
	njitter = 0;
	split(jitter, ja);
	if (ja[1] == -1 && ncpus == 0)
		njitter = 1;
	else if (ja[1] == -1)
		njitter = ncpus;
	else
		njitter = ja[1];
389 390
	if (TORTURE_BUILDONLY && njitter != 0) {
		njitter = 0;
391
		print "echo Build-only run, so suppressing jitter | tee -a " rd "log"
392
	}
393 394 395 396
	if (TORTURE_BUILDONLY) {
		print "needqemurun="
	}
	print "if test -n \"$needqemurun\""
397
	print "then"
398
	print "\techo ---- Starting kernels. `date` | tee -a " rd "log";
399 400 401
	for (j = 0; j < njitter; j++)
		print "\tjitter.sh " j " " dur " " ja[2] " " ja[3] "&"
	print "\twait"
402
	print "\techo ---- All kernel runs complete. `date` | tee -a " rd "log";
403 404
	print "else"
	print "\twait"
405
	print "\techo ---- No kernel runs. `date` | tee -a " rd "log";
406
	print "fi"
407 408
	for (j = 1; j < jn; j++) {
		builddir=KVM "/b" j
409 410
		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";
411 412 413 414 415 416 417
	}
}

END {
	njobs = i;
	nc = ncpus;
	first = 0;
418
	batchnum = 1;
419 420

	# Each pass through the following loop considers one test.
421 422
	for (i = 0; i < njobs; i++) {
		if (ncpus == 0) {
423
			# Sequential test specified, each test its own batch.
424
			dump(i, i + 1, batchnum);
425
			first = i;
426
			batchnum++;
427
		} else if (nc < cpus[i] && i != 0) {
428
			# Out of CPUs, dump out a batch.
429
			dump(first, i, batchnum);
430 431
			first = i;
			nc = ncpus;
432
			batchnum++;
433
		}
434
		# Account for the CPUs needed by the current test.
435 436
		nc -= cpus[i];
	}
437
	# Dump the last batch.
438
	if (ncpus != 0)
439
		dump(first, i, batchnum);
440
}' >> $T/script
441

442 443 444 445 446
cat << ___EOF___ >> $T/script
echo
echo
echo " --- `date` Test summary:"
echo Results directory: $resdir/$ds
447
kvm-recheck.sh $resdir/$ds
448 449
___EOF___

450 451 452 453 454 455
if test "$dryrun" = script
then
	cat $T/script
	exit 0
elif test "$dryrun" = sched
then
456
	# Extract the test run schedule from the script.
457 458
	egrep 'Start batch|Starting build\.' $T/script |
		grep -v ">>" |
459 460 461
		sed -e 's/:.*$//' -e 's/^echo //'
	exit 0
else
462
	# Not a dryrun, so run the script.
463 464
	sh $T/script
fi
465

466
# 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
467 468
# 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"