Linux-libre 5.3.12-gnu
[librecmc/linux-libre.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 resdir=${3}
40 if test -z "$resdir" -o ! -d "$resdir" -o ! -w "$resdir"
41 then
42         echo "kvm-test-1-run.sh :$resdir: Not a writable directory, cannot store results into it"
43         exit 1
44 fi
45 echo ' ---' `date`: Starting build
46 echo ' ---' Kconfig fragment at: $config_template >> $resdir/log
47 touch $resdir/ConfigFragment.input $resdir/ConfigFragment
48 if test -r "$config_dir/CFcommon"
49 then
50         echo " --- $config_dir/CFcommon" >> $resdir/ConfigFragment.input
51         cat < $config_dir/CFcommon >> $resdir/ConfigFragment.input
52         config_override.sh $config_dir/CFcommon $config_template > $T/Kc1
53         grep '#CHECK#' $config_dir/CFcommon >> $resdir/ConfigFragment
54 else
55         cp $config_template $T/Kc1
56 fi
57 echo " --- $config_template" >> $resdir/ConfigFragment.input
58 cat $config_template >> $resdir/ConfigFragment.input
59 grep '#CHECK#' $config_template >> $resdir/ConfigFragment
60 if test -n "$TORTURE_KCONFIG_ARG"
61 then
62         echo $TORTURE_KCONFIG_ARG | tr -s " " "\012" > $T/cmdline
63         echo " --- --kconfig argument" >> $resdir/ConfigFragment.input
64         cat $T/cmdline >> $resdir/ConfigFragment.input
65         config_override.sh $T/Kc1 $T/cmdline > $T/Kc2
66         # Note that "#CHECK#" is not permitted on commandline.
67 else
68         cp $T/Kc1 $T/Kc2
69 fi
70 cat $T/Kc2 >> $resdir/ConfigFragment
71
72 base_resdir=`echo $resdir | sed -e 's/\.[0-9]\+$//'`
73 if test "$base_resdir" != "$resdir" -a -f $base_resdir/bzImage -a -f $base_resdir/vmlinux
74 then
75         # Rerunning previous test, so use that test's kernel.
76         QEMU="`identify_qemu $base_resdir/vmlinux`"
77         BOOT_IMAGE="`identify_boot_image $QEMU`"
78         KERNEL=$base_resdir/${BOOT_IMAGE##*/} # use the last component of ${BOOT_IMAGE}
79         ln -s $base_resdir/Make*.out $resdir  # for kvm-recheck.sh
80         ln -s $base_resdir/.config $resdir  # for kvm-recheck.sh
81         # Arch-independent indicator
82         touch $resdir/builtkernel
83 elif kvm-build.sh $T/Kc2 $resdir
84 then
85         # Had to build a kernel for this test.
86         QEMU="`identify_qemu vmlinux`"
87         BOOT_IMAGE="`identify_boot_image $QEMU`"
88         cp vmlinux $resdir
89         cp .config $resdir
90         cp Module.symvers $resdir > /dev/null || :
91         cp System.map $resdir > /dev/null || :
92         if test -n "$BOOT_IMAGE"
93         then
94                 cp $BOOT_IMAGE $resdir
95                 KERNEL=$resdir/${BOOT_IMAGE##*/}
96                 # Arch-independent indicator
97                 touch $resdir/builtkernel
98         else
99                 echo No identifiable boot image, not running KVM, see $resdir.
100                 echo Do the torture scripts know about your architecture?
101         fi
102         parse-build.sh $resdir/Make.out $title
103 else
104         # Build failed.
105         cp .config $resdir || :
106         echo Build failed, not running KVM, see $resdir.
107         if test -f $builddir.wait
108         then
109                 mv $builddir.wait $builddir.ready
110         fi
111         exit 1
112 fi
113 if test -f $builddir.wait
114 then
115         mv $builddir.wait $builddir.ready
116 fi
117 while test -f $builddir.ready
118 do
119         sleep 1
120 done
121 seconds=$4
122 qemu_args=$5
123 boot_args=$6
124
125 cd $KVM
126 kstarttime=`awk 'BEGIN { print systime() }' < /dev/null`
127 if test -z "$TORTURE_BUILDONLY"
128 then
129         echo ' ---' `date`: Starting kernel
130 fi
131
132 # Generate -smp qemu argument.
133 qemu_args="-enable-kvm -nographic $qemu_args"
134 cpu_count=`configNR_CPUS.sh $resdir/ConfigFragment`
135 cpu_count=`configfrag_boot_cpus "$boot_args" "$config_template" "$cpu_count"`
136 vcpus=`identify_qemu_vcpus`
137 if test $cpu_count -gt $vcpus
138 then
139         echo CPU count limited from $cpu_count to $vcpus | tee -a $resdir/Warnings
140         cpu_count=$vcpus
141 fi
142 qemu_args="`specify_qemu_cpus "$QEMU" "$qemu_args" "$cpu_count"`"
143
144 # Generate architecture-specific and interaction-specific qemu arguments
145 qemu_args="$qemu_args `identify_qemu_args "$QEMU" "$resdir/console.log"`"
146
147 # Generate qemu -append arguments
148 qemu_append="`identify_qemu_append "$QEMU"`"
149
150 # Pull in Kconfig-fragment boot parameters
151 boot_args="`configfrag_boot_params "$boot_args" "$config_template"`"
152 # Generate kernel-version-specific boot parameters
153 boot_args="`per_version_boot_params "$boot_args" $resdir/.config $seconds`"
154
155 if test -n "$TORTURE_BUILDONLY"
156 then
157         echo Build-only run specified, boot/test omitted.
158         touch $resdir/buildonly
159         exit 0
160 fi
161 echo "NOTE: $QEMU either did not run or was interactive" > $resdir/console.log
162 echo $QEMU $qemu_args -m $TORTURE_QEMU_MEM -kernel $KERNEL -append \"$qemu_append $boot_args\" > $resdir/qemu-cmd
163 ( $QEMU $qemu_args -m $TORTURE_QEMU_MEM -kernel $KERNEL -append "$qemu_append $boot_args" > $resdir/qemu-output 2>&1 & echo $! > $resdir/qemu_pid; wait `cat  $resdir/qemu_pid`; echo $? > $resdir/qemu-retval ) &
164 commandcompleted=0
165 sleep 10 # Give qemu's pid a chance to reach the file
166 if test -s "$resdir/qemu_pid"
167 then
168         qemu_pid=`cat "$resdir/qemu_pid"`
169         echo Monitoring qemu job at pid $qemu_pid
170 else
171         qemu_pid=""
172         echo Monitoring qemu job at yet-as-unknown pid
173 fi
174 while :
175 do
176         if test -z "$qemu_pid" -a -s "$resdir/qemu_pid"
177         then
178                 qemu_pid=`cat "$resdir/qemu_pid"`
179         fi
180         kruntime=`awk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
181         if test -z "$qemu_pid" || kill -0 "$qemu_pid" > /dev/null 2>&1
182         then
183                 if test $kruntime -ge $seconds
184                 then
185                         break;
186                 fi
187                 sleep 1
188         else
189                 commandcompleted=1
190                 if test $kruntime -lt $seconds
191                 then
192                         echo Completed in $kruntime vs. $seconds >> $resdir/Warnings 2>&1
193                         grep "^(qemu) qemu:" $resdir/kvm-test-1-run.sh.out >> $resdir/Warnings 2>&1
194                         killpid="`sed -n "s/^(qemu) qemu: terminating on signal [0-9]* from pid \([0-9]*\).*$/\1/p" $resdir/Warnings`"
195                         if test -n "$killpid"
196                         then
197                                 echo "ps -fp $killpid" >> $resdir/Warnings 2>&1
198                                 ps -fp $killpid >> $resdir/Warnings 2>&1
199                         fi
200                 else
201                         echo ' ---' `date`: "Kernel done"
202                 fi
203                 break
204         fi
205 done
206 if test -z "$qemu_pid" -a -s "$resdir/qemu_pid"
207 then
208         qemu_pid=`cat "$resdir/qemu_pid"`
209 fi
210 if test $commandcompleted -eq 0 -a -n "$qemu_pid"
211 then
212         echo Grace period for qemu job at pid $qemu_pid
213         oldline="`tail $resdir/console.log`"
214         while :
215         do
216                 kruntime=`awk 'BEGIN { print systime() - '"$kstarttime"' }' < /dev/null`
217                 if kill -0 $qemu_pid > /dev/null 2>&1
218                 then
219                         :
220                 else
221                         break
222                 fi
223                 must_continue=no
224                 newline="`tail $resdir/console.log`"
225                 if test "$newline" != "$oldline" && echo $newline | grep -q ' [0-9]\+us : '
226                 then
227                         must_continue=yes
228                 fi
229                 last_ts="`tail $resdir/console.log | grep '^\[ *[0-9]\+\.[0-9]\+]' | tail -1 | sed -e 's/^\[ *//' -e 's/\..*$//'`"
230                 if test -z "last_ts"
231                 then
232                         last_ts=0
233                 fi
234                 if test "$newline" != "$oldline" -a "$last_ts" -lt $((seconds + $TORTURE_SHUTDOWN_GRACE))
235                 then
236                         must_continue=yes
237                 fi
238                 if test $must_continue = no -a $kruntime -ge $((seconds + $TORTURE_SHUTDOWN_GRACE))
239                 then
240                         echo "!!! PID $qemu_pid hung at $kruntime vs. $seconds seconds" >> $resdir/Warnings 2>&1
241                         kill -KILL $qemu_pid
242                         break
243                 fi
244                 oldline=$newline
245                 sleep 10
246         done
247 elif test -z "$qemu_pid"
248 then
249         echo Unknown PID, cannot kill qemu command
250 fi
251
252 parse-console.sh $resdir/console.log $title