GNU Linux-libre 4.14.290-gnu1
[releases.git] / tools / testing / selftests / rcutorture / bin / functions.sh
1 #!/bin/bash
2 #
3 # Shell functions for the rest of the scripts.
4 #
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.
9 #
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.
14 #
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.
18 #
19 # Copyright (C) IBM Corporation, 2013
20 #
21 # Authors: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
22
23 # bootparam_hotplug_cpu bootparam-string
24 #
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_"
29 }
30
31 # checkarg --argname argtype $# arg mustmatch cannotmatch
32 #
33 # Checks the specified argument "arg" against the mustmatch and cannotmatch
34 # patterns.
35 checkarg () {
36         if test $3 -le 1
37         then
38                 echo $1 needs argument $2 matching \"$5\"
39                 usage
40         fi
41         if echo "$4" | grep -q -e "$5"
42         then
43                 :
44         else
45                 echo $1 $2 \"$4\" must match \"$5\"
46                 usage
47         fi
48         if echo "$4" | grep -q -e "$6"
49         then
50                 echo $1 $2 \"$4\" must not match \"$6\"
51                 usage
52         fi
53 }
54
55 # configfrag_boot_params bootparam-string config-fragment-file
56 #
57 # Adds boot parameters from the .boot file, if any.
58 configfrag_boot_params () {
59         if test -r "$2.boot"
60         then
61                 echo $1 `grep -v '^#' "$2.boot" | tr '\012' ' '`
62         else
63                 echo $1
64         fi
65 }
66
67 # configfrag_boot_cpus bootparam-string config-fragment-file config-cpus
68 #
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"`"
72         local nr_cpus
73         if echo "${bootargs}" | grep -q 'nr_cpus=[0-9]'
74         then
75                 nr_cpus="`echo "${bootargs}" | sed -e 's/^.*nr_cpus=\([0-9]*\).*$/\1/'`"
76                 if test "$3" -gt "$nr_cpus"
77                 then
78                         echo $nr_cpus
79                 else
80                         echo $3
81                 fi
82         else
83                 echo $3
84         fi
85 }
86
87 # configfrag_boot_maxcpus bootparam-string config-fragment-file config-cpus
88 #
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"`"
97         local maxcpus
98         if echo "${bootargs}" | grep -q 'maxcpus=[0-9]'
99         then
100                 maxcpus="`echo "${bootargs}" | sed -e 's/^.*maxcpus=\([0-9]*\).*$/\1/'`"
101                 if test "$3" -gt "$maxcpus"
102                 then
103                         echo $maxcpus
104                 else
105                         echo $3
106                 fi
107         else
108                 echo $3
109         fi
110 }
111
112 # configfrag_hotplug_cpu config-fragment-file
113 #
114 # Returns 1 if the config fragment specifies hotplug CPU.
115 configfrag_hotplug_cpu () {
116         if test ! -r "$1"
117         then
118                 echo Unreadable config fragment "$1" 1>&2
119                 exit -1
120         fi
121         grep -q '^CONFIG_HOTPLUG_CPU=y$' "$1"
122 }
123
124 # identify_boot_image qemu-cmd
125 #
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
129 # variable.
130 identify_boot_image () {
131         if test -n "$TORTURE_BOOT_IMAGE"
132         then
133                 echo $TORTURE_BOOT_IMAGE
134         else
135                 case "$1" in
136                 qemu-system-x86_64|qemu-system-i386)
137                         echo arch/x86/boot/bzImage
138                         ;;
139                 *)
140                         echo vmlinux
141                         ;;
142                 esac
143         fi
144 }
145
146 # identify_qemu builddir
147 #
148 # Returns our best guess as to which qemu command is appropriate for
149 # the kernel at hand.  Override with the TORTURE_QEMU_CMD environment variable.
150 identify_qemu () {
151         local u="`file "$1"`"
152         if test -n "$TORTURE_QEMU_CMD"
153         then
154                 echo $TORTURE_QEMU_CMD
155         elif echo $u | grep -q x86-64
156         then
157                 echo qemu-system-x86_64
158         elif echo $u | grep -q "Intel 80386"
159         then
160                 echo qemu-system-i386
161         elif uname -a | grep -q ppc64
162         then
163                 echo qemu-system-ppc64
164         else
165                 echo Cannot figure out what qemu command to use! 1>&2
166                 echo file $1 output: $u
167                 # Usually this will be one of /usr/bin/qemu-system-*
168                 # Use TORTURE_QEMU_CMD environment variable or appropriate
169                 # argument to top-level script.
170                 exit 1
171         fi
172 }
173
174 # identify_qemu_append qemu-cmd
175 #
176 # Output arguments for the qemu "-append" string based on CPU type
177 # and the TORTURE_QEMU_INTERACTIVE environment variable.
178 identify_qemu_append () {
179         case "$1" in
180         qemu-system-x86_64|qemu-system-i386)
181                 echo noapic selinux=0 initcall_debug debug
182                 ;;
183         esac
184         if test -n "$TORTURE_QEMU_INTERACTIVE"
185         then
186                 echo root=/dev/sda
187         else
188                 echo console=ttyS0
189         fi
190 }
191
192 # identify_qemu_args qemu-cmd serial-file
193 #
194 # Output arguments for qemu arguments based on the TORTURE_QEMU_MAC
195 # and TORTURE_QEMU_INTERACTIVE environment variables.
196 identify_qemu_args () {
197         case "$1" in
198         qemu-system-x86_64|qemu-system-i386)
199                 ;;
200         qemu-system-ppc64)
201                 echo -enable-kvm -M pseries -nodefaults
202                 echo -device spapr-vscsi
203                 if test -n "$TORTURE_QEMU_INTERACTIVE" -a -n "$TORTURE_QEMU_MAC"
204                 then
205                         echo -device spapr-vlan,netdev=net0,mac=$TORTURE_QEMU_MAC
206                         echo -netdev bridge,br=br0,id=net0
207                 elif test -n "$TORTURE_QEMU_INTERACTIVE"
208                 then
209                         echo -net nic -net user
210                 fi
211                 ;;
212         esac
213         if test -n "$TORTURE_QEMU_INTERACTIVE"
214         then
215                 echo -monitor stdio -serial pty -S
216         else
217                 echo -serial file:$2
218         fi
219 }
220
221 # identify_qemu_vcpus
222 #
223 # Returns the number of virtual CPUs available to the aggregate of the
224 # guest OSes.
225 identify_qemu_vcpus () {
226         lscpu | grep '^CPU(s):' | sed -e 's/CPU(s)://'
227 }
228
229 # print_bug
230 #
231 # Prints "BUG: " in red followed by remaining arguments
232 print_bug () {
233         printf '\033[031mBUG: \033[m'
234         echo $*
235 }
236
237 # print_warning
238 #
239 # Prints "WARNING: " in yellow followed by remaining arguments
240 print_warning () {
241         printf '\033[033mWARNING: \033[m'
242         echo $*
243 }
244
245 # specify_qemu_cpus qemu-cmd qemu-args #cpus
246 #
247 # Appends a string containing "-smp XXX" to qemu-args, unless the incoming
248 # qemu-args already contains "-smp".
249 specify_qemu_cpus () {
250         local nt;
251
252         if echo $2 | grep -q -e -smp
253         then
254                 echo $2
255         else
256                 case "$1" in
257                 qemu-system-x86_64|qemu-system-i386)
258                         echo $2 -smp $3
259                         ;;
260                 qemu-system-ppc64)
261                         nt="`lscpu | grep '^NUMA node0' | sed -e 's/^[^,]*,\([0-9]*\),.*$/\1/'`"
262                         echo $2 -smp cores=`expr \( $3 + $nt - 1 \) / $nt`,threads=$nt
263                         ;;
264                 esac
265         fi
266 }