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 RCUTORTURE="`pwd`/tools/testing/selftests/rcutorture"; export RCUTORTURE
29 PATH=${RCUTORTURE}/bin:$PATH; export PATH
32 TORTURE_ALLOTED_CPUS="`identify_qemu_vcpus`"
33 TORTURE_DEFCONFIG=defconfig
36 TORTURE_INITRD="$RCUTORTURE/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=""
47 torture_qemu_mem_default=1
49 TORTURE_SHUTDOWN_GRACE=180
51 TORTURE_MOD=rcutorture
56 ds=`date +%Y.%m.%d-%H.%M.%S`
60 starttime="`get_starttime`"
63 echo "Usage: $scriptname optional arguments:"
65 echo " --bootargs kernel-boot-arguments"
66 echo " --bootimage relative-path-to-kernel-boot-image"
68 echo " --configs \"config-file list w/ repeat factor (3*TINY01)\""
70 echo " --datestamp string"
71 echo " --defconfig string"
72 echo " --dryrun batches|scenarios|sched|script"
73 echo " --duration minutes | <seconds>s | <hours>h | <days>d"
77 echo " --jitter N [ maxsleep (us) [ maxspin (us) ] ]"
79 echo " --kconfig Kconfig-options"
81 echo " --kmake-arg kernel-make-arguments"
82 echo " --mac nn:nn:nn:nn:nn:nn"
83 echo " --memory megabytes|nnnG"
85 echo " --qemu-args qemu-arguments"
86 echo " --qemu-cmd qemu-system-..."
88 echo " --results absolute-pathname"
89 echo " --shutdown-grace seconds"
90 echo " --torture lock|rcu|rcuscale|refscale|scf|X*"
99 cpus=$TORTURE_ALLOTED_CPUS
100 max_cpus=$TORTURE_ALLOTED_CPUS
102 --bootargs|--bootarg)
103 checkarg --bootargs "(list of kernel boot arguments)" "$#" "$2" '.*' '^--'
104 TORTURE_BOOTARGS="$TORTURE_BOOTARGS $2"
108 checkarg --bootimage "(relative path to kernel boot image)" "$#" "$2" '[a-zA-Z0-9][a-zA-Z0-9_]*' '^--'
109 TORTURE_BOOT_IMAGE="$2"
112 --buildonly|--build-only)
116 checkarg --configs "(list of config files)" "$#" "$2" '^[^/.a-z]\+$' '^--'
117 configs="$configs $2"
121 checkarg --cpus "(number)" "$#" "$2" '^[0-9]*$' '^--'
123 TORTURE_ALLOTED_CPUS="$2"
124 if test -z "$TORTURE_REMOTE"
126 max_cpus="`identify_qemu_vcpus`"
127 if test "$TORTURE_ALLOTED_CPUS" -gt "$max_cpus"
129 TORTURE_ALLOTED_CPUS=$max_cpus
135 checkarg --datestamp "(relative pathname)" "$#" "$2" '^[a-zA-Z0-9._/-]*$' '^--'
140 checkarg --defconfig "defconfigtype" "$#" "$2" '^[^/][^/]*$' '^--'
145 checkarg --dryrun "batches|sched|script" $# "$2" 'batches\|scenarios\|sched\|script' '^--'
150 checkarg --duration "(minutes)" $# "$2" '^[0-9][0-9]*\(s\|m\|h\|d\|\)$' '^error'
152 if echo "$2" | grep -q 's$'
155 elif echo "$2" | grep -q 'h$'
158 elif echo "$2" | grep -q 'd$'
162 ts=`echo $2 | sed -e 's/[smhd]$//'`
167 TORTURE_KCONFIG_GDB_ARG="CONFIG_DEBUG_INFO=y"; export TORTURE_KCONFIG_GDB_ARG
168 TORTURE_BOOT_GDB_ARG="nokaslr"; export TORTURE_BOOT_GDB_ARG
169 TORTURE_QEMU_GDB_ARG="-s -S"; export TORTURE_QEMU_GDB_ARG
175 TORTURE_QEMU_INTERACTIVE=1; export TORTURE_QEMU_INTERACTIVE
178 checkarg --jitter "(# threads [ sleep [ spin ] ])" $# "$2" '^-\{,1\}[0-9]\+\( \+[0-9]\+\)\{,2\} *$' '^error$'
183 TORTURE_KCONFIG_KASAN_ARG="CONFIG_DEBUG_INFO=y CONFIG_KASAN=y"; export TORTURE_KCONFIG_KASAN_ARG
184 if test -n "$torture_qemu_mem_default"
189 --kconfig|--kconfigs)
190 checkarg --kconfig "(Kconfig options)" $# "$2" '^CONFIG_[A-Z0-9_]\+=\([ynm]\|[0-9]\+\)\( CONFIG_[A-Z0-9_]\+=\([ynm]\|[0-9]\+\)\)*$' '^error$'
191 TORTURE_KCONFIG_ARG="`echo "$TORTURE_KCONFIG_ARG $2" | sed -e 's/^ *//' -e 's/ *$//'`"
195 TORTURE_KCONFIG_KCSAN_ARG="CONFIG_DEBUG_INFO=y CONFIG_KCSAN=y CONFIG_KCSAN_STRICT=y CONFIG_KCSAN_REPORT_ONCE_IN_MS=100000 CONFIG_KCSAN_VERBOSE=y CONFIG_DEBUG_LOCK_ALLOC=y CONFIG_PROVE_LOCKING=y"; export TORTURE_KCONFIG_KCSAN_ARG
197 --kmake-arg|--kmake-args)
198 checkarg --kmake-arg "(kernel make arguments)" $# "$2" '.*' '^error$'
199 TORTURE_KMAKE_ARG="`echo "$TORTURE_KMAKE_ARG $2" | sed -e 's/^ *//' -e 's/ *$//'`"
203 checkarg --mac "(MAC address)" $# "$2" '^\([0-9a-fA-F]\{2\}:\)\{5\}[0-9a-fA-F]\{2\}$' error
208 checkarg --memory "(memory size)" $# "$2" '^[0-9]\+[MG]\?$' error
210 torture_qemu_mem_default=
214 TORTURE_INITRD=""; export TORTURE_INITRD
216 --qemu-args|--qemu-arg)
217 checkarg --qemu-args "(qemu arguments)" $# "$2" '^-' '^error'
218 TORTURE_QEMU_ARG="`echo "$TORTURE_QEMU_ARG $2" | sed -e 's/^ *//' -e 's/ *$//'`"
222 checkarg --qemu-cmd "(qemu-system-...)" $# "$2" 'qemu-system-' '^--'
223 TORTURE_QEMU_CMD="$2"
230 checkarg --results "(absolute pathname)" "$#" "$2" '^/' '^error'
235 checkarg --shutdown-grace "(seconds)" "$#" "$2" '^[0-9]*$' '^error'
236 TORTURE_SHUTDOWN_GRACE=$2
240 checkarg --torture "(suite name)" "$#" "$2" '^\(lock\|rcu\|rcuscale\|refscale\|scf\|X.*\)$' '^--'
242 TORTURE_MOD="`echo $TORTURE_SUITE | sed -e 's/^\(lock\|rcu\|scf\)$/\1torture/'`"
244 if test "$TORTURE_SUITE" = rcuscale || test "$TORTURE_SUITE" = refscale
246 # If you really want jitter for refscale or
247 # rcuscale, specify it after specifying the rcuscale
248 # or the refscale. (But why jitter in these cases?)
253 TORTURE_TRUST_MAKE="y"
256 echo Unknown argument $1
263 if test -n "$dryrun" || test -z "$TORTURE_INITRD" || tools/testing/selftests/rcutorture/bin/mkinitrd.sh
267 echo No initrd and unable to create one, aborting test >&2
271 CONFIGFRAG=${RCUTORTURE}/configs/${TORTURE_SUITE}; export CONFIGFRAG
273 defaultconfigs="`tr '\012' ' ' < $CONFIGFRAG/CFLIST`"
274 if test -z "$configs"
276 configs=$defaultconfigs
281 resdir=$RCUTORTURE/res
284 # Create a file of test-name/#cpus pairs, sorted by decreasing #cpus.
289 [0-9]\**|[0-9][0-9]\**|[0-9][0-9][0-9]\**|[0-9][0-9][0-9][0-9]\**)
290 config_reps=`echo $CF | sed -e 's/\*.*$//'`
291 CF1=`echo $CF | sed -e 's/^[^*]*\*//'`
298 for ((cur_rep=0;cur_rep<$config_reps;cur_rep++))
300 configs_derep="$configs_derep $CF1"
304 configs_derep="`echo $configs_derep | sed -e "s/\<CFLIST\>/$defaultconfigs/g"`"
305 if test -n "$TORTURE_KCONFIG_GDB_ARG"
307 if test "`echo $configs_derep | wc -w`" -gt 1
309 echo "The --config list is: $configs_derep."
310 echo "Only one --config permitted with --gdb, terminating."
314 echo 'BEGIN {' > $T/cfgcpu.awk
315 for CF1 in `echo $configs_derep | tr -s ' ' '\012' | sort -u`
317 if test -f "$CONFIGFRAG/$CF1"
319 if echo "$TORTURE_KCONFIG_ARG" | grep -q '\<CONFIG_NR_CPUS='
321 echo "$TORTURE_KCONFIG_ARG" | tr -s ' ' | tr ' ' '\012' > $T/KCONFIG_ARG
322 cpu_count=`configNR_CPUS.sh $T/KCONFIG_ARG`
324 cpu_count=`configNR_CPUS.sh $CONFIGFRAG/$CF1`
326 cpu_count=`configfrag_boot_cpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
327 cpu_count=`configfrag_boot_maxcpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
328 echo 'scenariocpu["'"$CF1"'"] = '"$cpu_count"';' >> $T/cfgcpu.awk
330 echo "The --configs file $CF1 does not exist, terminating."
334 cat << '___EOF___' >> $T/cfgcpu.awk
337 for (i = 1; i <= NF; i++)
338 print $i, scenariocpu[$i];
341 echo $configs_derep | awk -f $T/cfgcpu.awk > $T/cfgcpu
342 sort -k2nr $T/cfgcpu -T="$T" > $T/cfgcpu.sort
344 # Use a greedy bin-packing algorithm, sorting the list accordingly.
345 awk < $T/cfgcpu.sort > $T/cfgcpu.pack -v ncpus=$cpus '
351 # Read file of tests and corresponding required numbers of CPUs.
361 # Each pass through the following loop creates on test batch that
362 # can be executed concurrently given ncpus. Note that a given test
363 # that requires more than the available CPUs will run in its own
364 # batch. Such tests just have to make do with what is available.
365 while (nc != ncpus) {
369 # Each pass through the following loop considers one
370 # test for inclusion in the current batch.
371 for (i = 0; i < njobs; i++) {
373 continue; # Already part of a batch.
374 if (nc >= cpus[i] || nc == ncpus) {
376 # This test fits into the current batch.
380 break; # Too-big test in its own batch.
385 # Dump out the tests in batch order.
386 for (b = 1; b <= batch; b++)
387 for (i = 0; i < njobs; i++)
389 print cf[i], cpus[i];
392 # Generate a script to execute the tests in appropriate batches.
393 cat << ___EOF___ > $T/script
394 CONFIGFRAG="$CONFIGFRAG"; export CONFIGFRAG
395 RCUTORTURE="$RCUTORTURE"; export RCUTORTURE
396 PATH="$PATH"; export PATH
397 TORTURE_ALLOTED_CPUS="$TORTURE_ALLOTED_CPUS"; export TORTURE_ALLOTED_CPUS
398 TORTURE_BOOT_IMAGE="$TORTURE_BOOT_IMAGE"; export TORTURE_BOOT_IMAGE
399 TORTURE_BUILDONLY="$TORTURE_BUILDONLY"; export TORTURE_BUILDONLY
400 TORTURE_DEFCONFIG="$TORTURE_DEFCONFIG"; export TORTURE_DEFCONFIG
401 TORTURE_INITRD="$TORTURE_INITRD"; export TORTURE_INITRD
402 TORTURE_KCONFIG_ARG="$TORTURE_KCONFIG_ARG"; export TORTURE_KCONFIG_ARG
403 TORTURE_KCONFIG_GDB_ARG="$TORTURE_KCONFIG_GDB_ARG"; export TORTURE_KCONFIG_GDB_ARG
404 TORTURE_BOOT_GDB_ARG="$TORTURE_BOOT_GDB_ARG"; export TORTURE_BOOT_GDB_ARG
405 TORTURE_QEMU_GDB_ARG="$TORTURE_QEMU_GDB_ARG"; export TORTURE_QEMU_GDB_ARG
406 TORTURE_KCONFIG_KASAN_ARG="$TORTURE_KCONFIG_KASAN_ARG"; export TORTURE_KCONFIG_KASAN_ARG
407 TORTURE_KCONFIG_KCSAN_ARG="$TORTURE_KCONFIG_KCSAN_ARG"; export TORTURE_KCONFIG_KCSAN_ARG
408 TORTURE_KMAKE_ARG="$TORTURE_KMAKE_ARG"; export TORTURE_KMAKE_ARG
409 TORTURE_MOD="$TORTURE_MOD"; export TORTURE_MOD
410 TORTURE_QEMU_CMD="$TORTURE_QEMU_CMD"; export TORTURE_QEMU_CMD
411 TORTURE_QEMU_INTERACTIVE="$TORTURE_QEMU_INTERACTIVE"; export TORTURE_QEMU_INTERACTIVE
412 TORTURE_QEMU_MAC="$TORTURE_QEMU_MAC"; export TORTURE_QEMU_MAC
413 TORTURE_QEMU_MEM="$TORTURE_QEMU_MEM"; export TORTURE_QEMU_MEM
414 TORTURE_SHUTDOWN_GRACE="$TORTURE_SHUTDOWN_GRACE"; export TORTURE_SHUTDOWN_GRACE
415 TORTURE_SUITE="$TORTURE_SUITE"; export TORTURE_SUITE
416 TORTURE_TRUST_MAKE="$TORTURE_TRUST_MAKE"; export TORTURE_TRUST_MAKE
419 mkdir -p "$resdir" || :
422 TORTURE_RESDIR="$resdir/$ds"; export TORTURE_RESDIR
423 TORTURE_STOPFILE="$resdir/$ds/STOP.1"; export TORTURE_STOPFILE
424 echo Results directory: $resdir/$ds
425 echo $scriptname $args
426 touch $resdir/$ds/log
427 echo $scriptname $args >> $resdir/$ds/log
428 echo ${TORTURE_SUITE} > $resdir/$ds/torture_suite
429 echo Build directory: `pwd` > $resdir/$ds/testid.txt
432 echo Current commit: `git rev-parse HEAD` >> $resdir/$ds/testid.txt
433 echo >> $resdir/$ds/testid.txt
434 echo ' ---' Output of "'"git status"'": >> $resdir/$ds/testid.txt
435 git status >> $resdir/$ds/testid.txt
436 echo >> $resdir/$ds/testid.txt
437 echo >> $resdir/$ds/testid.txt
438 echo ' ---' Output of "'"git diff HEAD"'": >> $resdir/$ds/testid.txt
439 git diff HEAD >> $resdir/$ds/testid.txt
442 kvm-assign-cpus.sh /sys/devices/system/node > $T/cpuarray.awk
443 kvm-get-cpus-script.sh $T/cpuarray.awk $T/dumpbatches.awk
444 cat << '___EOF___' >> $T/dumpbatches.awk
455 # Dump out the scripting required to run one test batch.
456 function dump(first, pastlast, batchnum, affinitylist)
458 print "echo ----Start batch " batchnum ": `date` | tee -a " rd "log";
463 if (ja[1] == -1 && ncpus == 0)
465 else if (ja[1] == -1)
469 print "TORTURE_JITTER_START=\". jitterstart.sh " njitter " " rd " " dur " " ja[2] " " ja[3] "\"; export TORTURE_JITTER_START";
470 print "TORTURE_JITTER_STOP=\". jitterstop.sh " rd " \"; export TORTURE_JITTER_STOP"
471 for (j = first; j < pastlast; j++) {
473 if (cfrep[cf[j]] == "") {
478 cfr[jn] = cf[j] "." cfrep[cf[j]];
480 builddir=rd cfr[jn] "/build";
481 if (cpusr[jn] > ncpus && ncpus != 0)
485 print "echo ", cfr[jn], cpusr[jn] ovf ": Starting build. `date` | tee -a " rd "log";
486 print "mkdir " rd cfr[jn] " || :";
487 print "touch " builddir ".wait";
490 affinitylist = nextcpus(cpusr[jn]);
492 if (affinitylist ~ /^[0-9,-][0-9,-]*$/)
493 print "export TORTURE_AFFINITY=" affinitylist;
495 print "export TORTURE_AFFINITY=";
496 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 &"
497 print "echo ", cfr[jn], cpusr[jn] ovf ": Waiting for build to complete. `date` | tee -a " rd "log";
498 print "while test -f " builddir ".wait"
502 print "echo ", cfr[jn], cpusr[jn] ovf ": Build complete. `date` | tee -a " rd "log";
506 for (j = 1; j < jn; j++) {
507 builddir=rd cfr[j] "/build";
508 if (TORTURE_BUILDONLY)
509 print "rm -f " builddir ".ready"
511 print "mv " builddir ".ready " builddir ".run"
512 print "runfiles=\"$runfiles " builddir ".run\""
514 print "if test -f \"" rd cfr[j] "/builtkernel\""
516 print "\techo ----", cfr[j], cpusr[j] ovf ": Kernel present. `date` | tee -a " rd "log";
517 print "\tneedqemurun=1"
520 if (TORTURE_BUILDONLY && njitter != 0) {
522 print "echo Build-only run, so suppressing jitter | tee -a " rd "log"
524 if (TORTURE_BUILDONLY) {
527 print "if test -n \"$needqemurun\""
529 print "\techo ---- Starting kernels. `date` | tee -a " rd "log";
530 print "\t$TORTURE_JITTER_START";
531 print "\twhile ls $runfiles > /dev/null 2>&1"
535 print "\t$TORTURE_JITTER_STOP";
536 print "\techo ---- All kernel runs complete. `date` | tee -a " rd "log";
539 print "\techo ---- No kernel runs. `date` | tee -a " rd "log";
541 for (j = 1; j < jn; j++) {
542 print "echo ----", cfr[j], cpusr[j] ovf ": Build/run results: | tee -a " rd "log";
543 print "cat " rd cfr[j] "/kvm-test-1-run.sh.out | tee -a " rd "log";
553 # Each pass through the following loop considers one test.
554 for (i = 0; i < njobs; i++) {
556 # Sequential test specified, each test its own batch.
557 dump(i, i + 1, batchnum);
560 } else if (nc < cpus[i] && i != 0) {
561 # Out of CPUs, dump out a batch.
562 dump(first, i, batchnum);
567 # Account for the CPUs needed by the current test.
570 # Dump the last batch.
572 dump(first, i, batchnum);
575 awk < $T/cfgcpu.pack \
576 -v TORTURE_BUILDONLY="$TORTURE_BUILDONLY" \
577 -v CONFIGDIR="$CONFIGFRAG/" \
578 -v RCUTORTURE="$RCUTORTURE" \
580 -v jitter="$jitter" \
583 -v TORTURE_QEMU_ARG="$TORTURE_QEMU_ARG" \
584 -v TORTURE_BOOTARGS="$TORTURE_BOOTARGS" \
585 -f $T/dumpbatches.awk >> $T/script
586 echo kvm-end-run-stats.sh "$resdir/$ds" "$starttime" >> $T/script
588 # Extract the tests and their batches from the script.
589 egrep 'Start batch|Starting build\.' $T/script | grep -v ">>" |
590 sed -e 's/:.*$//' -e 's/^echo //' -e 's/-ovf//' |
597 print batchno, $1, $2
600 # As above, but one line per batch.
601 grep -v '^#' $T/batches | awk '
607 if (oldbatch != $1) {
608 print ++n ". " curbatch;
612 curbatch = curbatch " " $2;
616 print ++n ". " curbatch;
619 if test "$dryrun" = script
623 elif test "$dryrun" = sched
625 # Extract the test run schedule from the script.
626 egrep 'Start batch|Starting build\.' $T/script | grep -v ">>" |
627 sed -e 's/:.*$//' -e 's/^echo //'
628 nbuilds="`grep 'Starting build\.' $T/script |
629 grep -v ">>" | sed -e 's/:.*$//' -e 's/^echo //' |
630 awk '{ print $1 }' | grep -v '\.' | wc -l`"
631 echo Total number of builds: $nbuilds
632 nbatches="`grep 'Start batch' $T/script | grep -v ">>" | wc -l`"
633 echo Total number of batches: $nbatches
635 elif test "$dryrun" = batches
639 elif test "$dryrun" = scenarios
644 # Not a dryrun. Record the batches and the number of CPUs, then run the script.
647 cp $T/batches $resdir/$ds/batches
648 cp $T/scenarios $resdir/$ds/scenarios
649 echo '#' cpus=$cpus >> $resdir/$ds/batches
653 # 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
654 # Function-graph tracing: ftrace=function_graph ftrace_graph_filter=sched_setaffinity,migration_cpu_stop
655 # Also --kconfig "CONFIG_FUNCTION_TRACER=y CONFIG_FUNCTION_GRAPH_TRACER=y"
656 # Control buffer size: --bootargs trace_buf_size=3k
657 # Get trace-buffer dumps on all oopses: --bootargs ftrace_dump_on_oops
658 # Ditto, but dump only the oopsing CPU: --bootargs ftrace_dump_on_oops=orig_cpu
659 # Heavy-handed way to also dump on warnings: --bootargs panic_on_warn