2 # SPDX-License-Identifier: GPL-2.0+
4 # Shell functions for the rest of the scripts.
6 # Copyright (C) IBM Corporation, 2013
8 # Authors: Paul E. McKenney <paulmck@linux.ibm.com>
10 # bootparam_hotplug_cpu bootparam-string
12 # Returns 1 if the specified boot-parameter string tells rcutorture to
13 # test CPU-hotplug operations.
14 bootparam_hotplug_cpu
() {
15 echo "$1" |
grep -q "torture\.onoff_"
18 # checkarg --argname argtype $# arg mustmatch cannotmatch
20 # Checks the specified argument "arg" against the mustmatch and cannotmatch
25 echo $1 needs argument
$2 matching
\"$5\"
28 if echo "$4" |
grep -q -e "$5"
32 echo $1 $2 \"$4\" must match
\"$5\"
35 if echo "$4" |
grep -q -e "$6"
37 echo $1 $2 \"$4\" must not match
\"$6\"
42 # configfrag_boot_params bootparam-string config-fragment-file
44 # Adds boot parameters from the .boot file, if any.
45 configfrag_boot_params
() {
48 echo `grep -v '^#' "$2.boot" | tr '\012' ' '` $1
54 # configfrag_boot_cpus bootparam-string config-fragment-file config-cpus
56 # Decreases number of CPUs based on any nr_cpus= boot parameters specified.
57 configfrag_boot_cpus
() {
58 local bootargs
="`configfrag_boot_params "$1" "$2"`"
60 if echo "${bootargs}" |
grep -q 'nr_cpus=[0-9]'
62 nr_cpus
="`echo "${bootargs}" | sed -e 's/^.*nr_cpus=\([0-9]*\).*$/\1/'`"
63 if test "$3" -gt "$nr_cpus"
74 # configfrag_boot_maxcpus bootparam-string config-fragment-file config-cpus
76 # Decreases number of CPUs based on any maxcpus= boot parameters specified.
77 # This allows tests where additional CPUs come online later during the
78 # test run. However, the torture parameters will be set based on the
79 # number of CPUs initially present, so the scripting should schedule
80 # test runs based on the maxcpus= boot parameter controlling the initial
81 # number of CPUs instead of on the ultimate number of CPUs.
82 configfrag_boot_maxcpus
() {
83 local bootargs
="`configfrag_boot_params "$1" "$2"`"
85 if echo "${bootargs}" |
grep -q 'maxcpus=[0-9]'
87 maxcpus
="`echo "${bootargs}" | sed -e 's/^.*maxcpus=\([0-9]*\).*$/\1/'`"
88 if test "$3" -gt "$maxcpus"
99 # configfrag_hotplug_cpu config-fragment-file
101 # Returns 1 if the config fragment specifies hotplug CPU.
102 configfrag_hotplug_cpu
() {
105 echo Unreadable config fragment
"$1" 1>&2
108 grep -q '^CONFIG_HOTPLUG_CPU=y$' "$1"
113 # Returns a cookie identifying the current time.
115 awk 'BEGIN { print systime() }' < /dev
/null
118 # get_starttime_duration starttime
120 # Given the return value from get_starttime, compute a human-readable
121 # string denoting the time since get_starttime.
122 get_starttime_duration
() {
123 awk -v starttime
=$1 '
125 ts = systime() - starttime;
128 td = int(ts / 86400);
134 printf "%dd %d:%02d:%02d\n", d, h, m, s
136 printf "%d:%02d:%02d\n", h, m, s
138 printf "%d:%02d.0\n", m, s
144 # identify_boot_image qemu-cmd
146 # Returns the relative path to the kernel build image. This will be
147 # arch/<arch>/boot/bzImage or vmlinux if bzImage is not a target for the
148 # architecture, unless overridden with the TORTURE_BOOT_IMAGE environment
150 identify_boot_image
() {
151 if test -n "$TORTURE_BOOT_IMAGE"
153 echo $TORTURE_BOOT_IMAGE
156 qemu-system-x86_64|qemu-system-i386
)
157 echo arch
/x86
/boot
/bzImage
160 echo arch
/arm64
/boot
/Image
163 echo arch
/s390
/boot
/bzImage
172 # identify_qemu builddir
174 # Returns our best guess as to which qemu command is appropriate for
175 # the kernel at hand. Override with the TORTURE_QEMU_CMD environment variable.
177 local u
="`file "$1"`"
178 if test -n "$TORTURE_QEMU_CMD"
180 echo $TORTURE_QEMU_CMD
181 elif echo $u |
grep -q x86-64
183 echo qemu-system-x86_64
184 elif echo $u |
grep -q "Intel 80386"
186 echo qemu-system-i386
187 elif echo $u |
grep -q aarch64
189 echo qemu-system-aarch64
190 elif echo $u |
grep -q 'IBM S/390'
192 echo qemu-system-s390x
193 elif uname
-a |
grep -q ppc64
195 echo qemu-system-ppc64
197 echo Cannot figure out what qemu
command to use
! 1>&2
198 echo file $1 output
: $u
199 # Usually this will be one of /usr/bin/qemu-system-*
200 # Use TORTURE_QEMU_CMD environment variable or appropriate
201 # argument to top-level script.
206 # identify_qemu_append qemu-cmd
208 # Output arguments for the qemu "-append" string based on CPU type
209 # and the TORTURE_QEMU_INTERACTIVE environment variable.
210 identify_qemu_append
() {
211 echo debug_boot_weak_hash
215 qemu-system-x86_64|qemu-system-i386
)
216 echo selinux
=0 initcall_debug debug
222 if test -n "$TORTURE_QEMU_INTERACTIVE"
226 echo console
=$console
230 # identify_qemu_args qemu-cmd serial-file
232 # Output arguments for qemu arguments based on the TORTURE_QEMU_MAC
233 # and TORTURE_QEMU_INTERACTIVE environment variables.
234 identify_qemu_args
() {
245 qemu-system-x86_64|qemu-system-i386
)
246 echo -machine q35
,accel
=kvm
250 echo -machine virt
,gic-version
=host -cpu host
253 echo -M pseries
-nodefaults
254 echo -device spapr-vscsi
255 if test -n "$TORTURE_QEMU_INTERACTIVE" -a -n "$TORTURE_QEMU_MAC"
257 echo -device spapr-vlan
,netdev
=net0
,mac
=$TORTURE_QEMU_MAC
258 echo -netdev bridge
,br
=br0
,id
=net0
262 if test -n "$TORTURE_QEMU_INTERACTIVE"
264 echo -monitor stdio
-serial pty
-S
270 # identify_qemu_vcpus
272 # Returns the number of virtual CPUs available to the aggregate of the
274 identify_qemu_vcpus
() {
275 getconf _NPROCESSORS_ONLN
280 # Prints "BUG: " in red followed by remaining arguments
282 printf '\033[031mBUG: \033[m'
288 # Prints "WARNING: " in yellow followed by remaining arguments
290 printf '\033[033mWARNING: \033[m'
294 # specify_qemu_cpus qemu-cmd qemu-args #cpus
296 # Appends a string containing "-smp XXX" to qemu-args, unless the incoming
297 # qemu-args already contains "-smp".
298 specify_qemu_cpus
() {
301 if echo $2 |
grep -q -e -smp
306 qemu-system-x86_64|qemu-system-i386|qemu-system-aarch64
)
310 nt
="`lscpu | sed -n 's/^Thread(s) per core:\s*//p'`"
311 echo $2 -smp cores
=`expr \( $3 + $nt - 1 \) / $nt`,threads
=$nt
317 # specify_qemu_net qemu-args
319 # Appends a string containing "-net none" to qemu-args, unless the incoming
320 # qemu-args already contains "-smp" or unless the TORTURE_QEMU_INTERACTIVE
321 # environment variable is set, in which case the string that is be added is
322 # instead "-net nic -net user".
323 specify_qemu_net
() {
324 if echo $1 |
grep -q -e -net
327 elif test -n "$TORTURE_QEMU_INTERACTIVE"
329 echo $1 -net nic
-net user
335 # Extract the ftrace output from the console log output
336 # The ftrace output in the original logs look like:
337 # Dumping ftrace buffer:
338 # ---------------------------------
340 # ---------------------------------
341 extract_ftrace_from_console
() {
344 /Dumping ftrace buffer:/ {
346 print "Ftrace dump " buffer_count ":"
351 /---------------------------------/ {
355 } else if(capture == 2) {