f6483609ebc246f38d268dc97a9ddfa4072cbf7b
[kvmfornfv.git] / kernel / 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
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_SUITE=rcu
46 resdir=""
47 configs=""
48 cpus=0
49 ds=`date +%Y.%m.%d-%H:%M:%S`
50
51 . functions.sh
52
53 usage () {
54         echo "Usage: $scriptname optional arguments:"
55         echo "       --bootargs kernel-boot-arguments"
56         echo "       --bootimage relative-path-to-kernel-boot-image"
57         echo "       --buildonly"
58         echo "       --configs \"config-file list w/ repeat factor (3*TINY01)\""
59         echo "       --cpus N"
60         echo "       --datestamp string"
61         echo "       --defconfig string"
62         echo "       --dryrun sched|script"
63         echo "       --duration minutes"
64         echo "       --interactive"
65         echo "       --kmake-arg kernel-make-arguments"
66         echo "       --mac nn:nn:nn:nn:nn:nn"
67         echo "       --no-initrd"
68         echo "       --qemu-args qemu-system-..."
69         echo "       --qemu-cmd qemu-system-..."
70         echo "       --results absolute-pathname"
71         echo "       --torture rcu"
72         exit 1
73 }
74
75 while test $# -gt 0
76 do
77         case "$1" in
78         --bootargs|--bootarg)
79                 checkarg --bootargs "(list of kernel boot arguments)" "$#" "$2" '.*' '^--'
80                 TORTURE_BOOTARGS="$2"
81                 shift
82                 ;;
83         --bootimage)
84                 checkarg --bootimage "(relative path to kernel boot image)" "$#" "$2" '[a-zA-Z0-9][a-zA-Z0-9_]*' '^--'
85                 TORTURE_BOOT_IMAGE="$2"
86                 shift
87                 ;;
88         --buildonly)
89                 TORTURE_BUILDONLY=1
90                 ;;
91         --configs|--config)
92                 checkarg --configs "(list of config files)" "$#" "$2" '^[^/]*$' '^--'
93                 configs="$2"
94                 shift
95                 ;;
96         --cpus)
97                 checkarg --cpus "(number)" "$#" "$2" '^[0-9]*$' '^--'
98                 cpus=$2
99                 shift
100                 ;;
101         --datestamp)
102                 checkarg --datestamp "(relative pathname)" "$#" "$2" '^[^/]*$' '^--'
103                 ds=$2
104                 shift
105                 ;;
106         --defconfig)
107                 checkarg --defconfig "defconfigtype" "$#" "$2" '^[^/][^/]*$' '^--'
108                 TORTURE_DEFCONFIG=$2
109                 shift
110                 ;;
111         --dryrun)
112                 checkarg --dryrun "sched|script" $# "$2" 'sched\|script' '^--'
113                 dryrun=$2
114                 shift
115                 ;;
116         --duration)
117                 checkarg --duration "(minutes)" $# "$2" '^[0-9]*$' '^error'
118                 dur=$2
119                 shift
120                 ;;
121         --interactive)
122                 TORTURE_QEMU_INTERACTIVE=1; export TORTURE_QEMU_INTERACTIVE
123                 ;;
124         --kmake-arg)
125                 checkarg --kmake-arg "(kernel make arguments)" $# "$2" '.*' '^error$'
126                 TORTURE_KMAKE_ARG="$2"
127                 shift
128                 ;;
129         --mac)
130                 checkarg --mac "(MAC address)" $# "$2" '^\([0-9a-fA-F]\{2\}:\)\{5\}[0-9a-fA-F]\{2\}$' error
131                 TORTURE_QEMU_MAC=$2
132                 shift
133                 ;;
134         --no-initrd)
135                 TORTURE_INITRD=""; export TORTURE_INITRD
136                 ;;
137         --qemu-args|--qemu-arg)
138                 checkarg --qemu-args "-qemu args" $# "$2" '^-' '^error'
139                 TORTURE_QEMU_ARG="$2"
140                 shift
141                 ;;
142         --qemu-cmd)
143                 checkarg --qemu-cmd "(qemu-system-...)" $# "$2" 'qemu-system-' '^--'
144                 TORTURE_QEMU_CMD="$2"
145                 shift
146                 ;;
147         --results)
148                 checkarg --results "(absolute pathname)" "$#" "$2" '^/' '^error'
149                 resdir=$2
150                 shift
151                 ;;
152         --torture)
153                 checkarg --torture "(suite name)" "$#" "$2" '^\(lock\|rcu\)$' '^--'
154                 TORTURE_SUITE=$2
155                 shift
156                 ;;
157         *)
158                 echo Unknown argument $1
159                 usage
160                 ;;
161         esac
162         shift
163 done
164
165 CONFIGFRAG=${KVM}/configs/${TORTURE_SUITE}; export CONFIGFRAG
166
167 if test -z "$configs"
168 then
169         configs="`cat $CONFIGFRAG/CFLIST`"
170 fi
171
172 if test -z "$resdir"
173 then
174         resdir=$KVM/res
175 fi
176
177 # Create a file of test-name/#cpus pairs, sorted by decreasing #cpus.
178 touch $T/cfgcpu
179 for CF in $configs
180 do
181         case $CF in
182         [0-9]\**|[0-9][0-9]\**|[0-9][0-9][0-9]\**)
183                 config_reps=`echo $CF | sed -e 's/\*.*$//'`
184                 CF1=`echo $CF | sed -e 's/^[^*]*\*//'`
185                 ;;
186         *)
187                 config_reps=1
188                 CF1=$CF
189                 ;;
190         esac
191         if test -f "$CONFIGFRAG/$CF1"
192         then
193                 cpu_count=`configNR_CPUS.sh $CONFIGFRAG/$CF1`
194                 cpu_count=`configfrag_boot_cpus "$TORTURE_BOOTARGS" "$CONFIGFRAG/$CF1" "$cpu_count"`
195                 for ((cur_rep=0;cur_rep<$config_reps;cur_rep++))
196                 do
197                         echo $CF1 $cpu_count >> $T/cfgcpu
198                 done
199         else
200                 echo "The --configs file $CF1 does not exist, terminating."
201                 exit 1
202         fi
203 done
204 sort -k2nr $T/cfgcpu > $T/cfgcpu.sort
205
206 # Use a greedy bin-packing algorithm, sorting the list accordingly.
207 awk < $T/cfgcpu.sort > $T/cfgcpu.pack -v ncpus=$cpus '
208 BEGIN {
209         njobs = 0;
210 }
211
212 {
213         # Read file of tests and corresponding required numbers of CPUs.
214         cf[njobs] = $1;
215         cpus[njobs] = $2;
216         njobs++;
217 }
218
219 END {
220         alldone = 0;
221         batch = 0;
222         nc = -1;
223
224         # Each pass through the following loop creates on test batch
225         # that can be executed concurrently given ncpus.  Note that a
226         # given test that requires more than the available CPUs will run in
227         # their own batch.  Such tests just have to make do with what
228         # is available.
229         while (nc != ncpus) {
230                 batch++;
231                 nc = ncpus;
232
233                 # Each pass through the following loop considers one
234                 # test for inclusion in the current batch.
235                 for (i = 0; i < njobs; i++) {
236                         if (done[i])
237                                 continue; # Already part of a batch.
238                         if (nc >= cpus[i] || nc == ncpus) {
239
240                                 # This test fits into the current batch.
241                                 done[i] = batch;
242                                 nc -= cpus[i];
243                                 if (nc <= 0)
244                                         break; # Too-big test in its own batch.
245                         }
246                 }
247         }
248
249         # Dump out the tests in batch order.
250         for (b = 1; b <= batch; b++)
251                 for (i = 0; i < njobs; i++)
252                         if (done[i] == b)
253                                 print cf[i], cpus[i];
254 }'
255
256 # Generate a script to execute the tests in appropriate batches.
257 cat << ___EOF___ > $T/script
258 CONFIGFRAG="$CONFIGFRAG"; export CONFIGFRAG
259 KVM="$KVM"; export KVM
260 PATH="$PATH"; export PATH
261 TORTURE_BOOT_IMAGE="$TORTURE_BOOT_IMAGE"; export TORTURE_BOOT_IMAGE
262 TORTURE_BUILDONLY="$TORTURE_BUILDONLY"; export TORTURE_BUILDONLY
263 TORTURE_DEFCONFIG="$TORTURE_DEFCONFIG"; export TORTURE_DEFCONFIG
264 TORTURE_INITRD="$TORTURE_INITRD"; export TORTURE_INITRD
265 TORTURE_KMAKE_ARG="$TORTURE_KMAKE_ARG"; export TORTURE_KMAKE_ARG
266 TORTURE_QEMU_CMD="$TORTURE_QEMU_CMD"; export TORTURE_QEMU_CMD
267 TORTURE_QEMU_INTERACTIVE="$TORTURE_QEMU_INTERACTIVE"; export TORTURE_QEMU_INTERACTIVE
268 TORTURE_QEMU_MAC="$TORTURE_QEMU_MAC"; export TORTURE_QEMU_MAC
269 TORTURE_SUITE="$TORTURE_SUITE"; export TORTURE_SUITE
270 if ! test -e $resdir
271 then
272         mkdir -p "$resdir" || :
273 fi
274 mkdir $resdir/$ds
275 echo Results directory: $resdir/$ds
276 echo $scriptname $args
277 touch $resdir/$ds/log
278 echo $scriptname $args >> $resdir/$ds/log
279 echo ${TORTURE_SUITE} > $resdir/$ds/TORTURE_SUITE
280 pwd > $resdir/$ds/testid.txt
281 if test -d .git
282 then
283         git status >> $resdir/$ds/testid.txt
284         git rev-parse HEAD >> $resdir/$ds/testid.txt
285         if ! git diff HEAD > $T/git-diff 2>&1
286         then
287                 cp $T/git-diff $resdir/$ds
288         fi
289 fi
290 ___EOF___
291 awk < $T/cfgcpu.pack \
292         -v CONFIGDIR="$CONFIGFRAG/" \
293         -v KVM="$KVM" \
294         -v ncpus=$cpus \
295         -v rd=$resdir/$ds/ \
296         -v dur=$dur \
297         -v TORTURE_QEMU_ARG="$TORTURE_QEMU_ARG" \
298         -v TORTURE_BOOTARGS="$TORTURE_BOOTARGS" \
299 'BEGIN {
300         i = 0;
301 }
302
303 {
304         cf[i] = $1;
305         cpus[i] = $2;
306         i++;
307 }
308
309 # Dump out the scripting required to run one test batch.
310 function dump(first, pastlast)
311 {
312         print "echo ----Start batch: `date`";
313         print "echo ----Start batch: `date` >> " rd "/log";
314         jn=1
315         for (j = first; j < pastlast; j++) {
316                 builddir=KVM "/b" jn
317                 cpusr[jn] = cpus[j];
318                 if (cfrep[cf[j]] == "") {
319                         cfr[jn] = cf[j];
320                         cfrep[cf[j]] = 1;
321                 } else {
322                         cfrep[cf[j]]++;
323                         cfr[jn] = cf[j] "." cfrep[cf[j]];
324                 }
325                 if (cpusr[jn] > ncpus && ncpus != 0)
326                         ovf = "-ovf";
327                 else
328                         ovf = "";
329                 print "echo ", cfr[jn], cpusr[jn] ovf ": Starting build. `date`";
330                 print "echo ", cfr[jn], cpusr[jn] ovf ": Starting build. `date` >> " rd "/log";
331                 print "rm -f " builddir ".*";
332                 print "touch " builddir ".wait";
333                 print "mkdir " builddir " > /dev/null 2>&1 || :";
334                 print "mkdir " rd cfr[jn] " || :";
335                 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 &"
336                 print "echo ", cfr[jn], cpusr[jn] ovf ": Waiting for build to complete. `date`";
337                 print "echo ", cfr[jn], cpusr[jn] ovf ": Waiting for build to complete. `date` >> " rd "/log";
338                 print "while test -f " builddir ".wait"
339                 print "do"
340                 print "\tsleep 1"
341                 print "done"
342                 print "echo ", cfr[jn], cpusr[jn] ovf ": Build complete. `date`";
343                 print "echo ", cfr[jn], cpusr[jn] ovf ": Build complete. `date` >> " rd "/log";
344                 jn++;
345         }
346         for (j = 1; j < jn; j++) {
347                 builddir=KVM "/b" j
348                 print "rm -f " builddir ".ready"
349                 print "if test -z \"$TORTURE_BUILDONLY\""
350                 print "then"
351                 print "\techo ----", cfr[j], cpusr[j] ovf ": Starting kernel. `date`";
352                 print "\techo ----", cfr[j], cpusr[j] ovf ": Starting kernel. `date` >> " rd "/log";
353                 print "fi"
354         }
355         print "wait"
356         print "if test -z \"$TORTURE_BUILDONLY\""
357         print "then"
358         print "\techo ---- All kernel runs complete. `date`";
359         print "\techo ---- All kernel runs complete. `date` >> " rd "/log";
360         print "fi"
361         for (j = 1; j < jn; j++) {
362                 builddir=KVM "/b" j
363                 print "echo ----", cfr[j], cpusr[j] ovf ": Build/run results:";
364                 print "echo ----", cfr[j], cpusr[j] ovf ": Build/run results: >> " rd "/log";
365                 print "cat " rd cfr[j]  "/kvm-test-1-run.sh.out";
366                 print "cat " rd cfr[j]  "/kvm-test-1-run.sh.out >> " rd "/log";
367         }
368 }
369
370 END {
371         njobs = i;
372         nc = ncpus;
373         first = 0;
374
375         # Each pass through the following loop considers one test.
376         for (i = 0; i < njobs; i++) {
377                 if (ncpus == 0) {
378                         # Sequential test specified, each test its own batch.
379                         dump(i, i + 1);
380                         first = i;
381                 } else if (nc < cpus[i] && i != 0) {
382                         # Out of CPUs, dump out a batch.
383                         dump(first, i);
384                         first = i;
385                         nc = ncpus;
386                 }
387                 # Account for the CPUs needed by the current test.
388                 nc -= cpus[i];
389         }
390         # Dump the last batch.
391         if (ncpus != 0)
392                 dump(first, i);
393 }' >> $T/script
394
395 cat << ___EOF___ >> $T/script
396 echo
397 echo
398 echo " --- `date` Test summary:"
399 echo Results directory: $resdir/$ds
400 kvm-recheck.sh $resdir/$ds
401 ___EOF___
402
403 if test "$dryrun" = script
404 then
405         cat $T/script
406         exit 0
407 elif test "$dryrun" = sched
408 then
409         # Extract the test run schedule from the script.
410         egrep 'Start batch|Starting build\.' $T/script |
411                 grep -v ">>" |
412                 sed -e 's/:.*$//' -e 's/^echo //'
413         exit 0
414 else
415         # Not a dryrun, so run the script.
416         sh $T/script
417 fi
418
419 # 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