2 # SPDX-License-Identifier: GPL-2.0+
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.
8 # Usage: kvm.sh [ options ]
10 # Copyright (C) IBM Corporation, 2011
12 # Authors: Paul E. McKenney <paulmck@linux.ibm.com>
17 T=${TMPDIR-/tmp}/kvm.sh.$$
21 cd `dirname $scriptname`/../../../../../
23 # This script knows only English.
24 LANG=en_US.UTF-8; export LANG
28 KVM="`pwd`/tools/testing/selftests/rcutorture"; export KVM
29 PATH=${KVM}/bin:$PATH; export PATH
32 TORTURE_ALLOTED_CPUS="`identify_qemu_vcpus`"
33 TORTURE_DEFCONFIG=defconfig
36 TORTURE_INITRD="$KVM/initrd"; export TORTURE_INITRD
37 TORTURE_KCONFIG_ARG=""
38 TORTURE_KCONFIG_GDB_ARG=""
39 TORTURE_BOOT_GDB_ARG=""
40 TORTURE_QEMU_GDB_ARG=""
41 TORTURE_JITTER_START=""
42 TORTURE_JITTER_STOP=""
43 TORTURE_KCONFIG_KASAN_ARG=""
44 TORTURE_KCONFIG_KCSAN_ARG=""
48 TORTURE_SHUTDOWN_GRACE=180
50 TORTURE_MOD=rcutorture
55 ds=`date +%Y.%m.%d-%H.%M.%S`
59 starttime="`get_starttime`"
62 echo "Usage: $scriptname optional arguments:"
64 echo " --bootargs kernel-boot-arguments"
65 echo " --bootimage relative-path-to-kernel-boot-image"
67 echo " --configs \"config-file list w/ repeat factor (3*TINY01)\""
69 echo " --datestamp string"
70 echo " --defconfig string"
71 echo " --dryrun batches|scenarios|sched|script"
72 echo " --duration minutes | <seconds>s | <hours>h | <days>d"
76 echo " --jitter N [ maxsleep (us) [ maxspin (us) ] ]"
77 echo " --kconfig Kconfig-options"
78 echo " --kmake-arg kernel-make-arguments"
79 echo " --mac nn:nn:nn:nn:nn:nn"
80 echo " --memory megabytes|nnnG"
82 echo " --qemu-args qemu-arguments"
83 echo " --qemu-cmd qemu-system-..."
85 echo " --results absolute-pathname"
86 echo " --torture lock|rcu|rcuscale|refscale|scf"
95 cpus=$TORTURE_ALLOTED_CPUS
96 max_cpus=$TORTURE_ALLOTED_CPUS
99 checkarg --bootargs "(list of kernel boot arguments)" "$#" "$2" '.*' '^--'
100 TORTURE_BOOTARGS="$TORTURE_BOOTARGS $2"
104 checkarg --bootimage "(relative path to kernel boot image)" "$#" "$2" '[a-zA-Z0-9][a-zA-Z0-9_]*' '^--'
105 TORTURE_BOOT_IMAGE="$2"
108 --buildonly|--build-only)
112 checkarg --configs "(list of config files)" "$#" "$2" '^[^/.a-z]\+$' '^--'
113 configs="$configs $2"
117 checkarg --cpus "(number)" "$#" "$2" '^[0-9]*$' '^--'
119 TORTURE_ALLOTED_CPUS="$2"
120 if test -z "$TORTURE_REMOTE"
122 max_cpus="`identify_qemu_vcpus`"
123 if test "$TORTURE_ALLOTED_CPUS" -gt "$max_cpus"
125 TORTURE_ALLOTED_CPUS=$max_cpus
131 checkarg --datestamp "(relative pathname)" "$#" "$2" '^[a-zA-Z0-9._/-]*$' '^--'
136 checkarg --defconfig "defconfigtype" "$#" "$2" '^[^/][^/]*$' '^--'
141 checkarg --dryrun "batches|sched|script" $# "$2" 'batches\|scenarios\|sched\|script' '^--'
146 checkarg --duration "(minutes)" $# "$2" '^[0-9][0-9]*\(s\|m\|h\|d\|\)$' '^error'
148 if echo "$2" | grep -q 's$'
151 elif echo "$2" | grep -q 'h$'
154 elif echo "$2" | grep -q 'd$'
158 ts=`echo $2 | sed -e 's/[smhd]$//'`
163 TORTURE_KCONFIG_GDB_ARG="CONFIG_DEBUG_INFO=y"; export TORTURE_KCONFIG_GDB_ARG
164 TORTURE_BOOT_GDB_ARG="nokaslr"; export TORTURE_BOOT_GDB_ARG
165 TORTURE_QEMU_GDB_ARG="-s -S"; export TORTURE_QEMU_GDB_ARG
171 TORTURE_QEMU_INTERACTIVE=1; export TORTURE_QEMU_INTERACTIVE
174 checkarg --jitter "(# threads [ sleep [ spin ] ])" $# "$2" '^-\{,1\}[0-9]\+\( \+[0-9]\+\)\{,2\} *$' '^error$'
178 --kconfig|--kconfigs)
179 checkarg --kconfig "(Kconfig options)" $# "$2" '^CONFIG_[A-Z0-9_]\+=\([ynm]\|[0-9]\+\)\( CONFIG_[A-Z0-9_]\+=\([ynm]\|[0-9]\+\)\)*$' '^error$'
180 TORTURE_KCONFIG_ARG="`echo "$TORTURE_KCONFIG_ARG $2" | sed -e 's/^ *//' -e 's/ *$//'`"
184 TORTURE_KCONFIG_KASAN_ARG="CONFIG_DEBUG_INFO=y CONFIG_KASAN=y"; export TORTURE_KCONFIG_KASAN_ARG
187 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_INTERRUPT_WATCHER=y CONFIG_KCSAN_VERBOSE=y CONFIG_DEBUG_LOCK_ALLOC=y CONFIG_PROVE_LOCKING=y"; export TORTURE_KCONFIG_KCSAN_ARG
189 --kmake-arg|--kmake-args)
190 checkarg --kmake-arg "(kernel make arguments)" $# "$2" '.*' '^error$'
191 TORTURE_KMAKE_ARG="`echo "$TORTURE_KMAKE_ARG $2" | sed -e 's/^ *//' -e 's/ *$//'`"
195 checkarg --mac "(MAC address)" $# "$2" '^\([0-9a-fA-F]\{2\}:\)\{5\}[0-9a-fA-F]\{2\}$' error
200 checkarg --memory "(memory size)" $# "$2" '^[0-9]\+[MG]\?$' error
205 TORTURE_INITRD=""; export TORTURE_INITRD
207 --qemu-args|--qemu-arg)
208 checkarg --qemu-args "(qemu arguments)" $# "$2" '^-' '^error'
209 TORTURE_QEMU_ARG="`echo "$TORTURE_QEMU_ARG $2" | sed -e 's/^ *//' -e 's/ *$//'`"
213 checkarg --qemu-cmd "(qemu-system-...)" $# "$2" 'qemu-system-' '^--'
214 TORTURE_QEMU_CMD="$2"
221 checkarg --results "(absolute pathname)" "$#" "$2" '^/' '^error'
226 checkarg --shutdown-grace "(seconds)" "$#" "$2" '^[0-9]*$' '^error'
227 TORTURE_SHUTDOWN_GRACE=$2
231 checkarg --torture "(suite name)" "$#" "$2" '^\(lock\|rcu\|rcuscale\|refscale\|scf\)$' '^--'
233 TORTURE_MOD="`echo $TORTURE_SUITE | sed -e 's/^\(lock\|rcu\|scf\)$/\1torture/'`"
235 if test "$TORTURE_SUITE" = rcuscale || test "$TORTURE_SUITE" = refscale
237 # If you really want jitter for refscale or
238 # rcuscale, specify it after specifying the rcuscale
239 # or the refscale. (But why jitter in these cases?)
244 TORTURE_TRUST_MAKE="y"
247 echo Unknown argument $1
254 if test -n "$dryrun" || test -z "$TORTURE_INITRD" || tools/testing/selftests/rcutorture/bin/mkinitrd.sh
258 echo No initrd and unable to create one, aborting test >&2
262 CONFIGFRAG=${KVM}/configs/${TORTURE_SUITE}; export CONFIGFRAG
264 defaultconfigs="`tr '\012' ' ' < $CONFIGFRAG/CFLIST`"
265 if test -z "$configs"
267 configs=$defaultconfigs
275 # Create a file of test-name/#cpus pairs, sorted by decreasing #cpus.
280 [0-9]\**|[0-9][0-9]\**|[0-9][0-9][0-9]\**)
281 config_reps=`echo $CF | sed -e 's/\*.*$//'`
282 CF1=`echo $CF | sed -e 's/^[^*]*\*//'`
289 for ((cur_rep=0;cur_rep<$config_reps;cur_rep++))
291 configs_derep="$configs_derep $CF1"
295 configs_derep="`echo $configs_derep | sed -e "s/\<CFLIST\>/$defaultconfigs/g"`"
296 if test -n "$TORTURE_KCONFIG_GDB_ARG"
298 if test "`echo $configs_derep | wc -w`" -gt 1
300 echo "The --config list is: $configs_derep."
301 echo "Only one --config permitted with --gdb, terminating."
305 echo 'BEGIN {' > $T/cfgcpu.awk
306 for CF1 in `echo $configs_derep | tr -s ' ' '\012' | sort -u`
308 if test -f "$CONFIGFRAG/$CF1"
310 if echo "$TORTURE_KCONFIG_ARG" | grep -q '\<CONFIG_NR_CPUS='
312 echo "$TORTURE_KCONFIG_ARG" | tr -s ' ' | tr ' ' '\012' > $T/KCONFIG_ARG
313 cpu_count=`configNR_CPUS.sh $T/KCONFIG_ARG`
315 cpu_count=`configNR_CPUS.sh $CONFIGFRAG/$CF1`
317 cpu_count=`configfrag_boot_cpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
318 cpu_count=`configfrag_boot_maxcpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
319 echo 'scenariocpu["'"$CF1"'"] = '"$cpu_count"';' >> $T/cfgcpu.awk
321 echo "The --configs file $CF1 does not exist, terminating."
325 cat << '___EOF___' >> $T/cfgcpu.awk
328 for (i = 1; i <= NF; i++)
329 print $i, scenariocpu[$i];
332 echo $configs_derep | awk -f $T/cfgcpu.awk > $T/cfgcpu
333 sort -k2nr $T/cfgcpu -T="$T" > $T/cfgcpu.sort
335 # Use a greedy bin-packing algorithm, sorting the list accordingly.
336 awk < $T/cfgcpu.sort > $T/cfgcpu.pack -v ncpus=$cpus '
342 # Read file of tests and corresponding required numbers of CPUs.
352 # Each pass through the following loop creates on test batch that
353 # can be executed concurrently given ncpus. Note that a given test
354 # that requires more than the available CPUs will run in its own
355 # batch. Such tests just have to make do with what is available.
356 while (nc != ncpus) {
360 # Each pass through the following loop considers one
361 # test for inclusion in the current batch.
362 for (i = 0; i < njobs; i++) {
364 continue; # Already part of a batch.
365 if (nc >= cpus[i] || nc == ncpus) {
367 # This test fits into the current batch.
371 break; # Too-big test in its own batch.
376 # Dump out the tests in batch order.
377 for (b = 1; b <= batch; b++)
378 for (i = 0; i < njobs; i++)
380 print cf[i], cpus[i];
383 # Generate a script to execute the tests in appropriate batches.
384 cat << ___EOF___ > $T/script
385 CONFIGFRAG="$CONFIGFRAG"; export CONFIGFRAG
386 KVM="$KVM"; export KVM
387 PATH="$PATH"; export PATH
388 TORTURE_ALLOTED_CPUS="$TORTURE_ALLOTED_CPUS"; export TORTURE_ALLOTED_CPUS
389 TORTURE_BOOT_IMAGE="$TORTURE_BOOT_IMAGE"; export TORTURE_BOOT_IMAGE
390 TORTURE_BUILDONLY="$TORTURE_BUILDONLY"; export TORTURE_BUILDONLY
391 TORTURE_DEFCONFIG="$TORTURE_DEFCONFIG"; export TORTURE_DEFCONFIG
392 TORTURE_INITRD="$TORTURE_INITRD"; export TORTURE_INITRD
393 TORTURE_KCONFIG_ARG="$TORTURE_KCONFIG_ARG"; export TORTURE_KCONFIG_ARG
394 TORTURE_KCONFIG_GDB_ARG="$TORTURE_KCONFIG_GDB_ARG"; export TORTURE_KCONFIG_GDB_ARG
395 TORTURE_BOOT_GDB_ARG="$TORTURE_BOOT_GDB_ARG"; export TORTURE_BOOT_GDB_ARG
396 TORTURE_QEMU_GDB_ARG="$TORTURE_QEMU_GDB_ARG"; export TORTURE_QEMU_GDB_ARG
397 TORTURE_KCONFIG_KASAN_ARG="$TORTURE_KCONFIG_KASAN_ARG"; export TORTURE_KCONFIG_KASAN_ARG
398 TORTURE_KCONFIG_KCSAN_ARG="$TORTURE_KCONFIG_KCSAN_ARG"; export TORTURE_KCONFIG_KCSAN_ARG
399 TORTURE_KMAKE_ARG="$TORTURE_KMAKE_ARG"; export TORTURE_KMAKE_ARG
400 TORTURE_MOD="$TORTURE_MOD"; export TORTURE_MOD
401 TORTURE_QEMU_CMD="$TORTURE_QEMU_CMD"; export TORTURE_QEMU_CMD
402 TORTURE_QEMU_INTERACTIVE="$TORTURE_QEMU_INTERACTIVE"; export TORTURE_QEMU_INTERACTIVE
403 TORTURE_QEMU_MAC="$TORTURE_QEMU_MAC"; export TORTURE_QEMU_MAC
404 TORTURE_QEMU_MEM="$TORTURE_QEMU_MEM"; export TORTURE_QEMU_MEM
405 TORTURE_SHUTDOWN_GRACE="$TORTURE_SHUTDOWN_GRACE"; export TORTURE_SHUTDOWN_GRACE
406 TORTURE_SUITE="$TORTURE_SUITE"; export TORTURE_SUITE
407 TORTURE_TRUST_MAKE="$TORTURE_TRUST_MAKE"; export TORTURE_TRUST_MAKE
410 mkdir -p "$resdir" || :
413 TORTURE_RESDIR="$resdir/$ds"; export TORTURE_RESDIR
414 TORTURE_STOPFILE="$resdir/$ds/STOP.1"; export TORTURE_STOPFILE
415 echo Results directory: $resdir/$ds
416 echo $scriptname $args
417 touch $resdir/$ds/log
418 echo $scriptname $args >> $resdir/$ds/log
419 echo ${TORTURE_SUITE} > $resdir/$ds/torture_suite
420 echo Build directory: `pwd` > $resdir/$ds/testid.txt
423 echo Current commit: `git rev-parse HEAD` >> $resdir/$ds/testid.txt
424 echo >> $resdir/$ds/testid.txt
425 echo ' ---' Output of "'"git status"'": >> $resdir/$ds/testid.txt
426 git status >> $resdir/$ds/testid.txt
427 echo >> $resdir/$ds/testid.txt
428 echo >> $resdir/$ds/testid.txt
429 echo ' ---' Output of "'"git diff HEAD"'": >> $resdir/$ds/testid.txt
430 git diff HEAD >> $resdir/$ds/testid.txt
433 awk < $T/cfgcpu.pack \
434 -v TORTURE_BUILDONLY="$TORTURE_BUILDONLY" \
435 -v CONFIGDIR="$CONFIGFRAG/" \
438 -v jitter="$jitter" \
441 -v TORTURE_QEMU_ARG="$TORTURE_QEMU_ARG" \
442 -v TORTURE_BOOTARGS="$TORTURE_BOOTARGS" \
453 # Dump out the scripting required to run one test batch.
454 function dump(first, pastlast, batchnum)
456 print "echo ----Start batch " batchnum ": `date` | tee -a " rd "log";
461 if (ja[1] == -1 && ncpus == 0)
463 else if (ja[1] == -1)
467 print "TORTURE_JITTER_START=\". jitterstart.sh " njitter " " rd " " dur " " ja[2] " " ja[3] "\"; export TORTURE_JITTER_START";
468 print "TORTURE_JITTER_STOP=\". jitterstop.sh " rd " \"; export TORTURE_JITTER_STOP"
469 for (j = first; j < pastlast; j++) {
471 if (cfrep[cf[j]] == "") {
476 cfr[jn] = cf[j] "." cfrep[cf[j]];
478 builddir=rd cfr[jn] "/build";
479 if (cpusr[jn] > ncpus && ncpus != 0)
483 print "echo ", cfr[jn], cpusr[jn] ovf ": Starting build. `date` | tee -a " rd "log";
484 print "mkdir " rd cfr[jn] " || :";
485 print "touch " builddir ".wait";
486 print "kvm-test-1-run.sh " CONFIGDIR cf[j], rd cfr[jn], dur " \"" TORTURE_QEMU_ARG "\" \"" TORTURE_BOOTARGS "\" > " rd cfr[jn] "/kvm-test-1-run.sh.out 2>&1 &"
487 print "echo ", cfr[jn], cpusr[jn] ovf ": Waiting for build to complete. `date` | tee -a " rd "log";
488 print "while test -f " builddir ".wait"
492 print "echo ", cfr[jn], cpusr[jn] ovf ": Build complete. `date` | tee -a " rd "log";
496 for (j = 1; j < jn; j++) {
497 builddir=rd cfr[j] "/build";
498 if (TORTURE_BUILDONLY)
499 print "rm -f " builddir ".ready"
501 print "mv " builddir ".ready " builddir ".run"
502 print "runfiles=\"$runfiles " builddir ".run\""
504 print "if test -f \"" rd cfr[j] "/builtkernel\""
506 print "\techo ----", cfr[j], cpusr[j] ovf ": Kernel present. `date` | tee -a " rd "log";
507 print "\tneedqemurun=1"
510 if (TORTURE_BUILDONLY && njitter != 0) {
512 print "echo Build-only run, so suppressing jitter | tee -a " rd "log"
514 if (TORTURE_BUILDONLY) {
517 print "if test -n \"$needqemurun\""
519 print "\techo ---- Starting kernels. `date` | tee -a " rd "log";
520 print "\t$TORTURE_JITTER_START";
521 print "\twhile ls $runfiles > /dev/null 2>&1"
525 print "\t$TORTURE_JITTER_STOP";
526 print "\techo ---- All kernel runs complete. `date` | tee -a " rd "log";
529 print "\techo ---- No kernel runs. `date` | tee -a " rd "log";
531 for (j = 1; j < jn; j++) {
532 print "echo ----", cfr[j], cpusr[j] ovf ": Build/run results: | tee -a " rd "log";
533 print "cat " rd cfr[j] "/kvm-test-1-run.sh.out | tee -a " rd "log";
543 # Each pass through the following loop considers one test.
544 for (i = 0; i < njobs; i++) {
546 # Sequential test specified, each test its own batch.
547 dump(i, i + 1, batchnum);
550 } else if (nc < cpus[i] && i != 0) {
551 # Out of CPUs, dump out a batch.
552 dump(first, i, batchnum);
557 # Account for the CPUs needed by the current test.
560 # Dump the last batch.
562 dump(first, i, batchnum);
564 echo kvm-end-run-stats.sh "$resdir/$ds" "$starttime" >> $T/script
566 # Extract the tests and their batches from the script.
567 egrep 'Start batch|Starting build\.' $T/script | grep -v ">>" |
568 sed -e 's/:.*$//' -e 's/^echo //' -e 's/-ovf//' |
575 print batchno, $1, $2
578 # As above, but one line per batch.
579 grep -v '^#' $T/batches | awk '
585 if (oldbatch != $1) {
586 print ++n ". " curbatch;
590 curbatch = curbatch " " $2;
594 print ++n ". " curbatch;
597 if test "$dryrun" = script
601 elif test "$dryrun" = sched
603 # Extract the test run schedule from the script.
604 egrep 'Start batch|Starting build\.' $T/script | grep -v ">>" |
605 sed -e 's/:.*$//' -e 's/^echo //'
606 nbuilds="`grep 'Starting build\.' $T/script |
607 grep -v ">>" | sed -e 's/:.*$//' -e 's/^echo //' |
608 awk '{ print $1 }' | grep -v '\.' | wc -l`"
609 echo Total number of builds: $nbuilds
610 nbatches="`grep 'Start batch' $T/script | grep -v ">>" | wc -l`"
611 echo Total number of batches: $nbatches
613 elif test "$dryrun" = batches
617 elif test "$dryrun" = scenarios
622 # Not a dryrun. Record the batches and the number of CPUs, then run the script.
625 cp $T/batches $resdir/$ds/batches
626 cp $T/scenarios $resdir/$ds/scenarios
627 echo '#' cpus=$cpus >> $resdir/$ds/batches
631 # 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
632 # Function-graph tracing: ftrace=function_graph ftrace_graph_filter=sched_setaffinity,migration_cpu_stop
633 # Also --kconfig "CONFIG_FUNCTION_TRACER=y CONFIG_FUNCTION_GRAPH_TRACER=y"
634 # Control buffer size: --bootargs trace_buf_size=3k
635 # Get trace-buffer dumps on all oopses: --bootargs ftrace_dump_on_oops
636 # Ditto, but dump only the oopsing CPU: --bootargs ftrace_dump_on_oops=orig_cpu
637 # Heavy-handed way to also dump on warnings: --bootargs panic_on_warn