3 # Shell functions for the rest of the scripts.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, you can access it online at
17 # http://www.gnu.org/licenses/gpl-2.0.html.
19 # Copyright (C) IBM Corporation, 2013
21 # Authors: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
23 # bootparam_hotplug_cpu bootparam-string
25 # Returns 1 if the specified boot-parameter string tells rcutorture to
26 # test CPU-hotplug operations.
27 bootparam_hotplug_cpu () {
28 echo "$1" | grep -q "rcutorture\.onoff_"
31 # checkarg --argname argtype $# arg mustmatch cannotmatch
33 # Checks the specified argument "arg" against the mustmatch and cannotmatch
38 echo $1 needs argument $2 matching \"$5\"
41 if echo "$4" | grep -q -e "$5"
45 echo $1 $2 \"$4\" must match \"$5\"
48 if echo "$4" | grep -q -e "$6"
50 echo $1 $2 \"$4\" must not match \"$6\"
55 # configfrag_boot_params bootparam-string config-fragment-file
57 # Adds boot parameters from the .boot file, if any.
58 configfrag_boot_params () {
61 echo $1 `grep -v '^#' "$2.boot" | tr '\012' ' '`
67 # configfrag_boot_cpus bootparam-string config-fragment-file config-cpus
69 # Decreases number of CPUs based on any nr_cpus= boot parameters specified.
70 configfrag_boot_cpus () {
71 local bootargs="`configfrag_boot_params "$1" "$2"`"
73 if echo "${bootargs}" | grep -q 'nr_cpus=[0-9]'
75 nr_cpus="`echo "${bootargs}" | sed -e 's/^.*nr_cpus=\([0-9]*\).*$/\1/'`"
76 if test "$3" -gt "$nr_cpus"
87 # configfrag_boot_maxcpus bootparam-string config-fragment-file config-cpus
89 # Decreases number of CPUs based on any maxcpus= boot parameters specified.
90 # This allows tests where additional CPUs come online later during the
91 # test run. However, the torture parameters will be set based on the
92 # number of CPUs initially present, so the scripting should schedule
93 # test runs based on the maxcpus= boot parameter controlling the initial
94 # number of CPUs instead of on the ultimate number of CPUs.
95 configfrag_boot_maxcpus () {
96 local bootargs="`configfrag_boot_params "$1" "$2"`"
98 if echo "${bootargs}" | grep -q 'maxcpus=[0-9]'
100 maxcpus="`echo "${bootargs}" | sed -e 's/^.*maxcpus=\([0-9]*\).*$/\1/'`"
101 if test "$3" -gt "$maxcpus"
112 # configfrag_hotplug_cpu config-fragment-file
114 # Returns 1 if the config fragment specifies hotplug CPU.
115 configfrag_hotplug_cpu () {
118 echo Unreadable config fragment "$1" 1>&2
121 grep -q '^CONFIG_HOTPLUG_CPU=y$' "$1"
124 # identify_boot_image qemu-cmd
126 # Returns the relative path to the kernel build image. This will be
127 # arch/<arch>/boot/bzImage or vmlinux if bzImage is not a target for the
128 # architecture, unless overridden with the TORTURE_BOOT_IMAGE environment
130 identify_boot_image () {
131 if test -n "$TORTURE_BOOT_IMAGE"
133 echo $TORTURE_BOOT_IMAGE
136 qemu-system-x86_64|qemu-system-i386)
137 echo arch/x86/boot/bzImage
140 echo arch/arm64/boot/Image
149 # identify_qemu builddir
151 # Returns our best guess as to which qemu command is appropriate for
152 # the kernel at hand. Override with the TORTURE_QEMU_CMD environment variable.
154 local u="`file "$1"`"
155 if test -n "$TORTURE_QEMU_CMD"
157 echo $TORTURE_QEMU_CMD
158 elif echo $u | grep -q x86-64
160 echo qemu-system-x86_64
161 elif echo $u | grep -q "Intel 80386"
163 echo qemu-system-i386
164 elif echo $u | grep -q aarch64
166 echo qemu-system-aarch64
167 elif uname -a | grep -q ppc64
169 echo qemu-system-ppc64
171 echo Cannot figure out what qemu command to use! 1>&2
172 echo file $1 output: $u
173 # Usually this will be one of /usr/bin/qemu-system-*
174 # Use TORTURE_QEMU_CMD environment variable or appropriate
175 # argument to top-level script.
180 # identify_qemu_append qemu-cmd
182 # Output arguments for the qemu "-append" string based on CPU type
183 # and the TORTURE_QEMU_INTERACTIVE environment variable.
184 identify_qemu_append () {
187 qemu-system-x86_64|qemu-system-i386)
188 echo noapic selinux=0 initcall_debug debug
194 if test -n "$TORTURE_QEMU_INTERACTIVE"
198 echo console=$console
202 # identify_qemu_args qemu-cmd serial-file
204 # Output arguments for qemu arguments based on the TORTURE_QEMU_MAC
205 # and TORTURE_QEMU_INTERACTIVE environment variables.
206 identify_qemu_args () {
208 qemu-system-x86_64|qemu-system-i386)
211 echo -machine virt,gic-version=host -cpu host
214 echo -enable-kvm -M pseries -nodefaults
215 echo -device spapr-vscsi
216 if test -n "$TORTURE_QEMU_INTERACTIVE" -a -n "$TORTURE_QEMU_MAC"
218 echo -device spapr-vlan,netdev=net0,mac=$TORTURE_QEMU_MAC
219 echo -netdev bridge,br=br0,id=net0
220 elif test -n "$TORTURE_QEMU_INTERACTIVE"
222 echo -net nic -net user
226 if test -n "$TORTURE_QEMU_INTERACTIVE"
228 echo -monitor stdio -serial pty -S
234 # identify_qemu_vcpus
236 # Returns the number of virtual CPUs available to the aggregate of the
238 identify_qemu_vcpus () {
239 lscpu | grep '^CPU(s):' | sed -e 's/CPU(s)://'
244 # Prints "BUG: " in red followed by remaining arguments
246 printf '\033[031mBUG: \033[m'
252 # Prints "WARNING: " in yellow followed by remaining arguments
254 printf '\033[033mWARNING: \033[m'
258 # specify_qemu_cpus qemu-cmd qemu-args #cpus
260 # Appends a string containing "-smp XXX" to qemu-args, unless the incoming
261 # qemu-args already contains "-smp".
262 specify_qemu_cpus () {
265 if echo $2 | grep -q -e -smp
270 qemu-system-x86_64|qemu-system-i386|qemu-system-aarch64)
274 nt="`lscpu | grep '^NUMA node0' | sed -e 's/^[^,]*,\([0-9]*\),.*$/\1/'`"
275 echo $2 -smp cores=`expr \( $3 + $nt - 1 \) / $nt`,threads=$nt