Merge tag 's390-5.2-1' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[linux-2.6-microblaze.git] / tools / testing / selftests / rcutorture / bin / kvm-test-1-run.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0+
3 #
4 # Run a kvm-based test of the specified tree on the specified configs.
5 # Fully automated run and error checking, no graphics console.
6 #
7 # Execute this in the source tree.  Do not run it as a background task
8 # because qemu does not seem to like that much.
9 #
10 # Usage: kvm-test-1-run.sh config builddir resdir seconds qemu-args boot_args
11 #
12 # qemu-args defaults to "-enable-kvm -nographic", along with arguments
13 #                       specifying the number of CPUs and other options
14 #                       generated from the underlying CPU architecture.
15 # boot_args defaults to value returned by the per_version_boot_params
16 #                       shell function.
17 #
18 # Anything you specify for either qemu-args or boot_args is appended to
19 # the default values.  The "-smp" value is deduced from the contents of
20 # the config fragment.
21 #
22 # More sophisticated argument parsing is clearly needed.
23 #
24 # Copyright (C) IBM Corporation, 2011
25 #
26 # Authors: Paul E. McKenney <paulmck@linux.ibm.com>
27
28 T=${TMPDIR-/tmp}/kvm-test-1-run.sh.$$
29 trap 'rm -rf $T' 0
30 mkdir $T
31
32 . functions.sh
33 . $CONFIGFRAG/ver_functions.sh
34
35 config_template=${1}
36 config_dir=`echo $config_template | sed -e 's,/[^/]*$,,'`
37 title=`echo $config_template | sed -e 's/^.*\///'`
38 builddir=${2}
39 if test -z "$builddir" -o ! -d "$builddir" -o ! -w "$builddir"
40 then
41         echo "kvm-test-1-run.sh :$builddir: Not a writable directory, cannot build into it"
42         exit 1
43 fi
44 resdir=${3}
45 if test -z "$resdir" -o ! -d "$resdir" -o ! -w "$resdir"
46 then
47         echo "kvm-test-1-run.sh :$resdir: Not a writable directory, cannot store results into it"
48         exit 1
49 fi
50 echo ' ---' `date`: Starting build
51 echo ' ---' Kconfig fragment at: $config_template >> $resdir/log
52 touch $resdir/ConfigFragment.input $resdir/ConfigFragment
53 if test -r "$config_dir/CFcommon"
54 then
55         echo " --- $config_dir/CFcommon" >> $resdir/ConfigFragment.input
56         cat < $config_dir/CFcommon >> $resdir/ConfigFragment.input
57         config_override.sh $config_dir/CFcommon $config_template > $T/Kc1
58         grep '#CHECK#' $config_dir/CFcommon >> $resdir/ConfigFragment
59 else
60         cp $config_template $T/Kc1
61 fi
62 echo " --- $config_template" >> $resdir/ConfigFragment.input
63 cat $config_template >> $resdir/ConfigFragment.input
64 grep '#CHECK#' $config_template >> $resdir/ConfigFragment
65 if test -n "$TORTURE_KCONFIG_ARG"
66 then
67         echo $TORTURE_KCONFIG_ARG | tr -s " " "\012" > $T/cmdline
68         echo " --- --kconfig argument" >> $resdir/ConfigFragment.input
69         cat $T/cmdline >> $resdir/ConfigFragment.input
70         config_override.sh $T/Kc1 $T/cmdline > $T/Kc2
71         # Note that "#CHECK#" is not permitted on commandline.
72 else
73         cp $T/Kc1 $T/Kc2
74 fi
75 cat $T/Kc2 >> $resdir/ConfigFragment
76
77 base_resdir=`echo $resdir | sed -e 's/\.[0-9]\+$//'`
78 if test "$base_resdir" != "$resdir" -a -f $base_resdir/bzImage -a -f $base_resdir/vmlinux
79 then
80         # Rerunning previous test, so use that test's kernel.
81         QEMU="`identify_qemu $base_resdir/vmlinux`"
82         BOOT_IMAGE="`identify_boot_image $QEMU`"
83         KERNEL=$base_resdir/${BOOT_IMAGE##*/} # use the last component of ${BOOT_IMAGE}
84         ln -s $base_resdir/Make*.out $resdir  # for kvm-recheck.sh
85         ln -s $base_resdir/.config $resdir  # for kvm-recheck.sh
86         # Arch-independent indicator
87         touch $resdir/builtkernel
88 elif kvm-build.sh $T/Kc2 $builddir $resdir
89 then
90         # Had to build a kernel for this test.
91         QEMU="`identify_qemu $builddir/vmlinux`"
92         BOOT_IMAGE="`identify_boot_image $QEMU`"
93         cp $builddir/vmlinux $resdir
94         cp $builddir/.config $resdir
95         cp $builddir/Module.symvers $resdir > /dev/null || :
96         cp $builddir/System.map $resdir > /dev/null || :
97         if test -n "$BOOT_IMAGE"
98         then
99                 cp $builddir/$BOOT_IMAGE $resdir
100                 KERNEL=$resdir/${BOOT_IMAGE##*/}
101                 # Arch-independent indicator
102                 touch $resdir/builtkernel
103         else
104                 echo No identifiable boot image, not running KVM, see $resdir.
105                 echo Do the torture scripts know about your architecture?
106         fi
107         parse-build.sh $resdir/Make.out $title
108 else
109         # Build failed.
110         cp $builddir/.config $resdir || :
111         echo Build failed, not running KVM, see $resdir.
112         if test -f $builddir.wait
113         then
114                 mv $builddir.wait $builddir.ready
115         fi
116         exit 1
117 fi
118 if test -f $builddir.wait
119 then
120         mv $builddir.wait $builddir.ready
121 fi
122 while test -f $builddir.ready
123 do
124         sleep 1
125 done
126 seconds=$4
127 qemu_args=$5
128 boot_args=$6
129
130 cd $KVM
131 kstarttime=`awk 'BEGIN { print systime() }' < /dev/null`
132 if test -z "$TORTURE_BUILDONLY"
133 then
134         echo ' ---' `date`: Starting kernel
135 fi
136
137 # Generate -smp qemu argument.
138 qemu_args="-enable-kvm -nographic $qemu_args"
139 cpu_count=`configNR_CPUS.sh $resdir/ConfigFragment`
140 cpu_count=`configfrag_boot_cpus "$boot_args" "$config_template" "$cpu_count"`
141 vcpus=`identify_qemu_vcpus`
142 if test $cpu_count -gt $vcpus
143 then
144         echo CPU count limited from $cpu_count to $vcpus | tee -a $resdir/Warnings
145         cpu_count=$vcpus
146 fi
147 qemu_args="`specify_qemu_cpus "$QEMU" "$qemu_args" "$cpu_count"`"
148
149 # Generate architecture-specific and interaction-specific qemu arguments
150 qemu_args="$qemu_args `identify_qemu_args "$QEMU" "$resdir/console.log"`"
151
152 # Generate qemu -append arguments
153 qemu_append="`identify_qemu_append "$QEMU"`"
154
155 # Pull in Kconfig-fragment boot parameters
156 boot_args="`configfrag_boot_params "$boot_args" "$config_template"`"
157 # Generate kernel-version-specific boot parameters
158 boot_args="`per_version_boot_params "$boot_args" $resdir/.config $seconds`"
159
160 if test -n "$TORTURE_BUILDONLY"
161 then
162         echo Build-only run specified, boot/test omitted.
163         touch $resdir/buildonly
164         exit 0
165 fi
166 echo "NOTE: $QEMU either did not run or was interactive" > $resdir/console.log
167 echo $QEMU $qemu_args -m $TORTURE_QEMU_MEM -kernel $KERNEL -append \"$qemu_append $boot_args\" > $resdir/qemu-cmd
168 ( $QEMU $qemu_args -m $TORTURE_QEMU_MEM -kernel $KERNEL -append "$qemu_append $boot_args"& echo $! > $resdir/qemu_pid; wait `cat  $resdir/qemu_pid`; echo $? > $resdir/qemu-retval ) &
169 commandcompleted=0
170 sleep 10 # Give qemu's pid a chance to reach the file
171 if test -s "$resdir/qemu_pid"
172 then
173         qemu_pid=`cat "$resdir/qemu_pid"`
174         echo Monitoring qemu job at pid $qemu_pid
175 else
176         qemu_pid=""
177         echo Monitoring qemu job at yet-as-unknown pid
178 fi
179 while :
180 do
181         if test -z "$qemu_pid" -a -s "$resdir/qemu_pid"
182         then
183                 qemu_pid=`cat "$resdir/qemu_pid"`
184         fi
185         kruntime=`awk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
186         if test -z "$qemu_pid" || kill -0 "$qemu_pid" > /dev/null 2>&1
187         then
188                 if test $kruntime -ge $seconds
189                 then
190                         break;
191                 fi
192                 sleep 1
193         else
194                 commandcompleted=1
195                 if test $kruntime -lt $seconds
196                 then
197                         echo Completed in $kruntime vs. $seconds >> $resdir/Warnings 2>&1
198                         grep "^(qemu) qemu:" $resdir/kvm-test-1-run.sh.out >> $resdir/Warnings 2>&1
199                         killpid="`sed -n "s/^(qemu) qemu: terminating on signal [0-9]* from pid \([0-9]*\).*$/\1/p" $resdir/Warnings`"
200                         if test -n "$killpid"
201                         then
202                                 echo "ps -fp $killpid" >> $resdir/Warnings 2>&1
203                                 ps -fp $killpid >> $resdir/Warnings 2>&1
204                         fi
205                 else
206                         echo ' ---' `date`: "Kernel done"
207                 fi
208                 break
209         fi
210 done
211 if test -z "$qemu_pid" -a -s "$resdir/qemu_pid"
212 then
213         qemu_pid=`cat "$resdir/qemu_pid"`
214 fi
215 if test $commandcompleted -eq 0 -a -n "$qemu_pid"
216 then
217         echo Grace period for qemu job at pid $qemu_pid
218         oldline="`tail $resdir/console.log`"
219         while :
220         do
221                 kruntime=`awk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
222                 if kill -0 $qemu_pid > /dev/null 2>&1
223                 then
224                         :
225                 else
226                         break
227                 fi
228                 must_continue=no
229                 newline="`tail $resdir/console.log`"
230                 if test "$newline" != "$oldline" && echo $newline | grep -q ' [0-9]\+us : '
231                 then
232                         must_continue=yes
233                 fi
234                 last_ts="`tail $resdir/console.log | grep '^\[ *[0-9]\+\.[0-9]\+]' | tail -1 | sed -e 's/^\[ *//' -e 's/\..*$//'`"
235                 if test -z "last_ts"
236                 then
237                         last_ts=0
238                 fi
239                 if test "$newline" != "$oldline" -a "$last_ts" -lt $((seconds + $TORTURE_SHUTDOWN_GRACE))
240                 then
241                         must_continue=yes
242                 fi
243                 if test $must_continue = no -a $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE))
244                 then
245                         echo "!!! PID $qemu_pid hung at $kruntime vs. $seconds seconds" >> $resdir/Warnings 2>&1
246                         kill -KILL $qemu_pid
247                         break
248                 fi
249                 oldline=$newline
250                 sleep 10
251         done
252 elif test -z "$qemu_pid"
253 then
254         echo Unknown PID, cannot kill qemu command
255 fi
256
257 parse-console.sh $resdir/console.log $title