GNU Linux-libre 4.9.337-gnu1
[releases.git] / tools / testing / selftests / rcutorture / bin / kvm.sh
1 #!/bin/bash
2 #
3 # Run a series of 14 tests under KVM.  These are not particularly
4 # well-selected or well-tuned, but are the current set.  Run from the
5 # top level of the source tree.
6 #
7 # Edit the definitions below to set the locations of the various directories,
8 # as well as the test duration.
9 #
10 # Usage: kvm.sh [ options ]
11 #
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, you can access it online at
24 # http://www.gnu.org/licenses/gpl-2.0.html.
25 #
26 # Copyright (C) IBM Corporation, 2011
27 #
28 # Authors: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
29
30 scriptname=$0
31 args="$*"
32
33 T=/tmp/kvm.sh.$$
34 trap 'rm -rf $T' 0
35 mkdir $T
36
37 dur=$((30*60))
38 dryrun=""
39 KVM="`pwd`/tools/testing/selftests/rcutorture"; export KVM
40 PATH=${KVM}/bin:$PATH; export PATH
41 TORTURE_DEFCONFIG=defconfig
42 TORTURE_BOOT_IMAGE=""
43 TORTURE_INITRD="$KVM/initrd"; export TORTURE_INITRD
44 TORTURE_KMAKE_ARG=""
45 TORTURE_SHUTDOWN_GRACE=180
46 TORTURE_SUITE=rcu
47 resdir=""
48 configs=""
49 cpus=0
50 ds=`date +%Y.%m.%d-%H:%M:%S`
51 jitter="-1"
52
53 . functions.sh
54
55 usage () {
56         echo "Usage: $scriptname optional arguments:"
57         echo "       --bootargs kernel-boot-arguments"
58         echo "       --bootimage relative-path-to-kernel-boot-image"
59         echo "       --buildonly"
60         echo "       --configs \"config-file list w/ repeat factor (3*TINY01)\""
61         echo "       --cpus N"
62         echo "       --datestamp string"
63         echo "       --defconfig string"
64         echo "       --dryrun sched|script"
65         echo "       --duration minutes"
66         echo "       --interactive"
67         echo "       --jitter N [ maxsleep (us) [ maxspin (us) ] ]"
68         echo "       --kmake-arg kernel-make-arguments"
69         echo "       --mac nn:nn:nn:nn:nn:nn"
70         echo "       --no-initrd"
71         echo "       --qemu-args qemu-system-..."
72         echo "       --qemu-cmd qemu-system-..."
73         echo "       --results absolute-pathname"
74         echo "       --torture rcu"
75         exit 1
76 }
77
78 while test $# -gt 0
79 do
80         case "$1" in
81         --bootargs|--bootarg)
82                 checkarg --bootargs "(list of kernel boot arguments)" "$#" "$2" '.*' '^--'
83                 TORTURE_BOOTARGS="$2"
84                 shift
85                 ;;
86         --bootimage)
87                 checkarg --bootimage "(relative path to kernel boot image)" "$#" "$2" '[a-zA-Z0-9][a-zA-Z0-9_]*' '^--'
88                 TORTURE_BOOT_IMAGE="$2"
89                 shift
90                 ;;
91         --buildonly)
92                 TORTURE_BUILDONLY=1
93                 ;;
94         --configs|--config)
95                 checkarg --configs "(list of config files)" "$#" "$2" '^[^/]*$' '^--'
96                 configs="$2"
97                 shift
98                 ;;
99         --cpus)
100                 checkarg --cpus "(number)" "$#" "$2" '^[0-9]*$' '^--'
101                 cpus=$2
102                 shift
103                 ;;
104         --datestamp)
105                 checkarg --datestamp "(relative pathname)" "$#" "$2" '^[^/]*$' '^--'
106                 ds=$2
107                 shift
108                 ;;
109         --defconfig)
110                 checkarg --defconfig "defconfigtype" "$#" "$2" '^[^/][^/]*$' '^--'
111                 TORTURE_DEFCONFIG=$2
112                 shift
113                 ;;
114         --dryrun)
115                 checkarg --dryrun "sched|script" $# "$2" 'sched\|script' '^--'
116                 dryrun=$2
117                 shift
118                 ;;
119         --duration)
120                 checkarg --duration "(minutes)" $# "$2" '^[0-9]*$' '^error'
121                 dur=$(($2*60))
122                 shift
123                 ;;
124         --interactive)
125                 TORTURE_QEMU_INTERACTIVE=1; export TORTURE_QEMU_INTERACTIVE
126                 ;;
127         --jitter)
128                 checkarg --jitter "(# threads [ sleep [ spin ] ])" $# "$2" '^-\{,1\}[0-9]\+\( \+[0-9]\+\)\{,2\} *$' '^error$'
129                 jitter="$2"
130                 shift
131                 ;;
132         --kmake-arg)
133                 checkarg --kmake-arg "(kernel make arguments)" $# "$2" '.*' '^error$'
134                 TORTURE_KMAKE_ARG="$2"
135                 shift
136                 ;;
137         --mac)
138                 checkarg --mac "(MAC address)" $# "$2" '^\([0-9a-fA-F]\{2\}:\)\{5\}[0-9a-fA-F]\{2\}$' error
139                 TORTURE_QEMU_MAC=$2
140                 shift
141                 ;;
142         --no-initrd)
143                 TORTURE_INITRD=""; export TORTURE_INITRD
144                 ;;
145         --qemu-args|--qemu-arg)
146                 checkarg --qemu-args "-qemu args" $# "$2" '^-' '^error'
147                 TORTURE_QEMU_ARG="$2"
148                 shift
149                 ;;
150         --qemu-cmd)
151                 checkarg --qemu-cmd "(qemu-system-...)" $# "$2" 'qemu-system-' '^--'
152                 TORTURE_QEMU_CMD="$2"
153                 shift
154                 ;;
155         --results)
156                 checkarg --results "(absolute pathname)" "$#" "$2" '^/' '^error'
157                 resdir=$2
158                 shift
159                 ;;
160         --shutdown-grace)
161                 checkarg --shutdown-grace "(seconds)" "$#" "$2" '^[0-9]*$' '^error'
162                 TORTURE_SHUTDOWN_GRACE=$2
163                 shift
164                 ;;
165         --torture)
166                 checkarg --torture "(suite name)" "$#" "$2" '^\(lock\|rcu\|rcuperf\)$' '^--'
167                 TORTURE_SUITE=$2
168                 shift
169                 ;;
170         *)
171                 echo Unknown argument $1
172                 usage
173                 ;;
174         esac
175         shift
176 done
177
178 if test -z "$TORTURE_INITRD" || tools/testing/selftests/rcutorture/bin/mkinitrd.sh
179 then
180         :
181 else
182         echo No initrd and unable to create one, aborting test >&2
183         exit 1
184 fi
185
186 CONFIGFRAG=${KVM}/configs/${TORTURE_SUITE}; export CONFIGFRAG
187
188 if test -z "$configs"
189 then
190         configs="`cat $CONFIGFRAG/CFLIST`"
191 fi
192
193 if test -z "$resdir"
194 then
195         resdir=$KVM/res
196 fi
197
198 # Create a file of test-name/#cpus pairs, sorted by decreasing #cpus.
199 touch $T/cfgcpu
200 for CF in $configs
201 do
202         case $CF in
203         [0-9]\**|[0-9][0-9]\**|[0-9][0-9][0-9]\**)
204                 config_reps=`echo $CF | sed -e 's/\*.*$//'`
205                 CF1=`echo $CF | sed -e 's/^[^*]*\*//'`
206                 ;;
207         *)
208                 config_reps=1
209                 CF1=$CF
210                 ;;
211         esac
212         if test -f "$CONFIGFRAG/$CF1"
213         then
214                 cpu_count=`configNR_CPUS.sh $CONFIGFRAG/$CF1`
215                 cpu_count=`configfrag_boot_cpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
216                 for ((cur_rep=0;cur_rep<$config_reps;cur_rep++))
217                 do
218                         echo $CF1 $cpu_count >> $T/cfgcpu
219                 done
220         else
221                 echo "The --configs file $CF1 does not exist, terminating."
222                 exit 1
223         fi
224 done
225 sort -k2nr $T/cfgcpu > $T/cfgcpu.sort
226
227 # Use a greedy bin-packing algorithm, sorting the list accordingly.
228 awk < $T/cfgcpu.sort > $T/cfgcpu.pack -v ncpus=$cpus '
229 BEGIN {
230         njobs = 0;
231 }
232
233 {
234         # Read file of tests and corresponding required numbers of CPUs.
235         cf[njobs] = $1;
236         cpus[njobs] = $2;
237         njobs++;
238 }
239
240 END {
241         alldone = 0;
242         batch = 0;
243         nc = -1;
244
245         # Each pass through the following loop creates on test batch
246         # that can be executed concurrently given ncpus.  Note that a
247         # given test that requires more than the available CPUs will run in
248         # their own batch.  Such tests just have to make do with what
249         # is available.
250         while (nc != ncpus) {
251                 batch++;
252                 nc = ncpus;
253
254                 # Each pass through the following loop considers one
255                 # test for inclusion in the current batch.
256                 for (i = 0; i < njobs; i++) {
257                         if (done[i])
258                                 continue; # Already part of a batch.
259                         if (nc >= cpus[i] || nc == ncpus) {
260
261                                 # This test fits into the current batch.
262                                 done[i] = batch;
263                                 nc -= cpus[i];
264                                 if (nc <= 0)
265                                         break; # Too-big test in its own batch.
266                         }
267                 }
268         }
269
270         # Dump out the tests in batch order.
271         for (b = 1; b <= batch; b++)
272                 for (i = 0; i < njobs; i++)
273                         if (done[i] == b)
274                                 print cf[i], cpus[i];
275 }'
276
277 # Generate a script to execute the tests in appropriate batches.
278 cat << ___EOF___ > $T/script
279 CONFIGFRAG="$CONFIGFRAG"; export CONFIGFRAG
280 KVM="$KVM"; export KVM
281 PATH="$PATH"; export PATH
282 TORTURE_BOOT_IMAGE="$TORTURE_BOOT_IMAGE"; export TORTURE_BOOT_IMAGE
283 TORTURE_BUILDONLY="$TORTURE_BUILDONLY"; export TORTURE_BUILDONLY
284 TORTURE_DEFCONFIG="$TORTURE_DEFCONFIG"; export TORTURE_DEFCONFIG
285 TORTURE_INITRD="$TORTURE_INITRD"; export TORTURE_INITRD
286 TORTURE_KMAKE_ARG="$TORTURE_KMAKE_ARG"; export TORTURE_KMAKE_ARG
287 TORTURE_QEMU_CMD="$TORTURE_QEMU_CMD"; export TORTURE_QEMU_CMD
288 TORTURE_QEMU_INTERACTIVE="$TORTURE_QEMU_INTERACTIVE"; export TORTURE_QEMU_INTERACTIVE
289 TORTURE_QEMU_MAC="$TORTURE_QEMU_MAC"; export TORTURE_QEMU_MAC
290 TORTURE_SHUTDOWN_GRACE="$TORTURE_SHUTDOWN_GRACE"; export TORTURE_SHUTDOWN_GRACE
291 TORTURE_SUITE="$TORTURE_SUITE"; export TORTURE_SUITE
292 if ! test -e $resdir
293 then
294         mkdir -p "$resdir" || :
295 fi
296 mkdir $resdir/$ds
297 echo Results directory: $resdir/$ds
298 echo $scriptname $args
299 touch $resdir/$ds/log
300 echo $scriptname $args >> $resdir/$ds/log
301 echo ${TORTURE_SUITE} > $resdir/$ds/TORTURE_SUITE
302 pwd > $resdir/$ds/testid.txt
303 if test -d .git
304 then
305         git status >> $resdir/$ds/testid.txt
306         git rev-parse HEAD >> $resdir/$ds/testid.txt
307         if ! git diff HEAD > $T/git-diff 2>&1
308         then
309                 cp $T/git-diff $resdir/$ds
310         fi
311 fi
312 ___EOF___
313 awk < $T/cfgcpu.pack \
314         -v CONFIGDIR="$CONFIGFRAG/" \
315         -v KVM="$KVM" \
316         -v ncpus=$cpus \
317         -v jitter="$jitter" \
318         -v rd=$resdir/$ds/ \
319         -v dur=$dur \
320         -v TORTURE_QEMU_ARG="$TORTURE_QEMU_ARG" \
321         -v TORTURE_BOOTARGS="$TORTURE_BOOTARGS" \
322 'BEGIN {
323         i = 0;
324 }
325
326 {
327         cf[i] = $1;
328         cpus[i] = $2;
329         i++;
330 }
331
332 # Dump out the scripting required to run one test batch.
333 function dump(first, pastlast, batchnum)
334 {
335         print "echo ----Start batch " batchnum ": `date`";
336         print "echo ----Start batch " batchnum ": `date` >> " rd "/log";
337         jn=1
338         for (j = first; j < pastlast; j++) {
339                 builddir=KVM "/b" jn
340                 cpusr[jn] = cpus[j];
341                 if (cfrep[cf[j]] == "") {
342                         cfr[jn] = cf[j];
343                         cfrep[cf[j]] = 1;
344                 } else {
345                         cfrep[cf[j]]++;
346                         cfr[jn] = cf[j] "." cfrep[cf[j]];
347                 }
348                 if (cpusr[jn] > ncpus && ncpus != 0)
349                         ovf = "-ovf";
350                 else
351                         ovf = "";
352                 print "echo ", cfr[jn], cpusr[jn] ovf ": Starting build. `date`";
353                 print "echo ", cfr[jn], cpusr[jn] ovf ": Starting build. `date` >> " rd "/log";
354                 print "rm -f " builddir ".*";
355                 print "touch " builddir ".wait";
356                 print "mkdir " builddir " > /dev/null 2>&1 || :";
357                 print "mkdir " rd cfr[jn] " || :";
358                 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 &"
359                 print "echo ", cfr[jn], cpusr[jn] ovf ": Waiting for build to complete. `date`";
360                 print "echo ", cfr[jn], cpusr[jn] ovf ": Waiting for build to complete. `date` >> " rd "/log";
361                 print "while test -f " builddir ".wait"
362                 print "do"
363                 print "\tsleep 1"
364                 print "done"
365                 print "echo ", cfr[jn], cpusr[jn] ovf ": Build complete. `date`";
366                 print "echo ", cfr[jn], cpusr[jn] ovf ": Build complete. `date` >> " rd "/log";
367                 jn++;
368         }
369         for (j = 1; j < jn; j++) {
370                 builddir=KVM "/b" j
371                 print "rm -f " builddir ".ready"
372                 print "if test -z \"$TORTURE_BUILDONLY\""
373                 print "then"
374                 print "\techo ----", cfr[j], cpusr[j] ovf ": Starting kernel. `date`";
375                 print "\techo ----", cfr[j], cpusr[j] ovf ": Starting kernel. `date` >> " rd "/log";
376                 print "fi"
377         }
378         njitter = 0;
379         split(jitter, ja);
380         if (ja[1] == -1 && ncpus == 0)
381                 njitter = 1;
382         else if (ja[1] == -1)
383                 njitter = ncpus;
384         else
385                 njitter = ja[1];
386         for (j = 0; j < njitter; j++)
387                 print "jitter.sh " j " " dur " " ja[2] " " ja[3] "&"
388         print "wait"
389         print "if test -z \"$TORTURE_BUILDONLY\""
390         print "then"
391         print "\techo ---- All kernel runs complete. `date`";
392         print "\techo ---- All kernel runs complete. `date` >> " rd "/log";
393         print "fi"
394         for (j = 1; j < jn; j++) {
395                 builddir=KVM "/b" j
396                 print "echo ----", cfr[j], cpusr[j] ovf ": Build/run results:";
397                 print "echo ----", cfr[j], cpusr[j] ovf ": Build/run results: >> " rd "/log";
398                 print "cat " rd cfr[j]  "/kvm-test-1-run.sh.out";
399                 print "cat " rd cfr[j]  "/kvm-test-1-run.sh.out >> " rd "/log";
400         }
401 }
402
403 END {
404         njobs = i;
405         nc = ncpus;
406         first = 0;
407         batchnum = 1;
408
409         # Each pass through the following loop considers one test.
410         for (i = 0; i < njobs; i++) {
411                 if (ncpus == 0) {
412                         # Sequential test specified, each test its own batch.
413                         dump(i, i + 1, batchnum);
414                         first = i;
415                         batchnum++;
416                 } else if (nc < cpus[i] && i != 0) {
417                         # Out of CPUs, dump out a batch.
418                         dump(first, i, batchnum);
419                         first = i;
420                         nc = ncpus;
421                         batchnum++;
422                 }
423                 # Account for the CPUs needed by the current test.
424                 nc -= cpus[i];
425         }
426         # Dump the last batch.
427         if (ncpus != 0)
428                 dump(first, i, batchnum);
429 }' >> $T/script
430
431 cat << ___EOF___ >> $T/script
432 echo
433 echo
434 echo " --- `date` Test summary:"
435 echo Results directory: $resdir/$ds
436 kvm-recheck.sh $resdir/$ds
437 ___EOF___
438
439 if test "$dryrun" = script
440 then
441         cat $T/script
442         exit 0
443 elif test "$dryrun" = sched
444 then
445         # Extract the test run schedule from the script.
446         egrep 'Start batch|Starting build\.' $T/script |
447                 grep -v ">>" |
448                 sed -e 's/:.*$//' -e 's/^echo //'
449         exit 0
450 else
451         # Not a dryrun, so run the script.
452         sh $T/script
453 fi
454
455 # 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