1 #!/bin/bash 1 #!/bin/bash 2 # SPDX-License-Identifier: GPL-2.0+ 2 # SPDX-License-Identifier: GPL-2.0+ 3 # 3 # 4 # Shell functions for the rest of the scripts. 4 # Shell functions for the rest of the scripts. 5 # 5 # 6 # Copyright (C) IBM Corporation, 2013 6 # Copyright (C) IBM Corporation, 2013 7 # 7 # 8 # Authors: Paul E. McKenney <paulmck@linux.ibm. 8 # Authors: Paul E. McKenney <paulmck@linux.ibm.com> 9 9 10 # bootparam_hotplug_cpu bootparam-string 10 # bootparam_hotplug_cpu bootparam-string 11 # 11 # 12 # Returns 1 if the specified boot-parameter st 12 # Returns 1 if the specified boot-parameter string tells rcutorture to 13 # test CPU-hotplug operations. 13 # test CPU-hotplug operations. 14 bootparam_hotplug_cpu () { 14 bootparam_hotplug_cpu () { 15 echo "$1" | grep -q "torture\.onoff_" 15 echo "$1" | grep -q "torture\.onoff_" 16 } 16 } 17 17 18 # checkarg --argname argtype $# arg mustmatch 18 # checkarg --argname argtype $# arg mustmatch cannotmatch 19 # 19 # 20 # Checks the specified argument "arg" against 20 # Checks the specified argument "arg" against the mustmatch and cannotmatch 21 # patterns. 21 # patterns. 22 checkarg () { 22 checkarg () { 23 if test $3 -le 1 23 if test $3 -le 1 24 then 24 then 25 echo $1 needs argument $2 matc 25 echo $1 needs argument $2 matching \"$5\" 26 usage 26 usage 27 fi 27 fi 28 if echo "$4" | grep -q -e "$5" 28 if echo "$4" | grep -q -e "$5" 29 then 29 then 30 : 30 : 31 else 31 else 32 echo $1 $2 \"$4\" must match \ 32 echo $1 $2 \"$4\" must match \"$5\" 33 usage 33 usage 34 fi 34 fi 35 if echo "$4" | grep -q -e "$6" 35 if echo "$4" | grep -q -e "$6" 36 then 36 then 37 echo $1 $2 \"$4\" must not mat 37 echo $1 $2 \"$4\" must not match \"$6\" 38 usage 38 usage 39 fi 39 fi 40 } 40 } 41 41 42 # configfrag_boot_params bootparam-string conf 42 # configfrag_boot_params bootparam-string config-fragment-file 43 # 43 # 44 # Adds boot parameters from the .boot file, if 44 # Adds boot parameters from the .boot file, if any. 45 configfrag_boot_params () { 45 configfrag_boot_params () { 46 if test -r "$2.boot" 46 if test -r "$2.boot" 47 then 47 then 48 echo `grep -v '^#' "$2.boot" | 48 echo `grep -v '^#' "$2.boot" | tr '\012' ' '` $1 49 else 49 else 50 echo $1 50 echo $1 51 fi 51 fi 52 } 52 } 53 53 54 # configfrag_boot_cpus bootparam-string config 54 # configfrag_boot_cpus bootparam-string config-fragment-file config-cpus 55 # 55 # 56 # Decreases number of CPUs based on any nr_cpu 56 # Decreases number of CPUs based on any nr_cpus= boot parameters specified. 57 configfrag_boot_cpus () { 57 configfrag_boot_cpus () { 58 local bootargs="`configfrag_boot_param 58 local bootargs="`configfrag_boot_params "$1" "$2"`" 59 local nr_cpus 59 local nr_cpus 60 if echo "${bootargs}" | grep -q 'nr_cp 60 if echo "${bootargs}" | grep -q 'nr_cpus=[0-9]' 61 then 61 then 62 nr_cpus="`echo "${bootargs}" | 62 nr_cpus="`echo "${bootargs}" | sed -e 's/^.*nr_cpus=\([0-9]*\).*$/\1/'`" 63 if test "$3" -gt "$nr_cpus" 63 if test "$3" -gt "$nr_cpus" 64 then 64 then 65 echo $nr_cpus 65 echo $nr_cpus 66 else 66 else 67 echo $3 67 echo $3 68 fi 68 fi 69 else 69 else 70 echo $3 70 echo $3 71 fi 71 fi 72 } 72 } 73 73 74 # configfrag_boot_maxcpus bootparam-string con 74 # configfrag_boot_maxcpus bootparam-string config-fragment-file config-cpus 75 # 75 # 76 # Decreases number of CPUs based on any maxcpu 76 # Decreases number of CPUs based on any maxcpus= boot parameters specified. 77 # This allows tests where additional CPUs come 77 # This allows tests where additional CPUs come online later during the 78 # test run. However, the torture parameters w 78 # test run. However, the torture parameters will be set based on the 79 # number of CPUs initially present, so the scr 79 # number of CPUs initially present, so the scripting should schedule 80 # test runs based on the maxcpus= boot paramet 80 # test runs based on the maxcpus= boot parameter controlling the initial 81 # number of CPUs instead of on the ultimate nu 81 # number of CPUs instead of on the ultimate number of CPUs. 82 configfrag_boot_maxcpus () { 82 configfrag_boot_maxcpus () { 83 local bootargs="`configfrag_boot_param 83 local bootargs="`configfrag_boot_params "$1" "$2"`" 84 local maxcpus 84 local maxcpus 85 if echo "${bootargs}" | grep -q 'maxcp 85 if echo "${bootargs}" | grep -q 'maxcpus=[0-9]' 86 then 86 then 87 maxcpus="`echo "${bootargs}" | 87 maxcpus="`echo "${bootargs}" | sed -e 's/^.*maxcpus=\([0-9]*\).*$/\1/'`" 88 if test "$3" -gt "$maxcpus" 88 if test "$3" -gt "$maxcpus" 89 then 89 then 90 echo $maxcpus 90 echo $maxcpus 91 else 91 else 92 echo $3 92 echo $3 93 fi 93 fi 94 else 94 else 95 echo $3 95 echo $3 96 fi 96 fi 97 } 97 } 98 98 99 # configfrag_hotplug_cpu config-fragment-file 99 # configfrag_hotplug_cpu config-fragment-file 100 # 100 # 101 # Returns 1 if the config fragment specifies h 101 # Returns 1 if the config fragment specifies hotplug CPU. 102 configfrag_hotplug_cpu () { 102 configfrag_hotplug_cpu () { 103 if test ! -r "$1" 103 if test ! -r "$1" 104 then 104 then 105 echo Unreadable config fragmen 105 echo Unreadable config fragment "$1" 1>&2 106 exit -1 106 exit -1 107 fi 107 fi 108 grep -q '^CONFIG_HOTPLUG_CPU=y$' "$1" 108 grep -q '^CONFIG_HOTPLUG_CPU=y$' "$1" 109 } 109 } 110 110 111 # get_starttime 111 # get_starttime 112 # 112 # 113 # Returns a cookie identifying the current tim 113 # Returns a cookie identifying the current time. 114 get_starttime () { 114 get_starttime () { 115 awk 'BEGIN { print systime() }' < /dev 115 awk 'BEGIN { print systime() }' < /dev/null 116 } 116 } 117 117 118 # get_starttime_duration starttime 118 # get_starttime_duration starttime 119 # 119 # 120 # Given the return value from get_starttime, c 120 # Given the return value from get_starttime, compute a human-readable 121 # string denoting the time since get_starttime 121 # string denoting the time since get_starttime. 122 get_starttime_duration () { 122 get_starttime_duration () { 123 awk -v starttime=$1 ' 123 awk -v starttime=$1 ' 124 BEGIN { 124 BEGIN { 125 ts = systime() - starttime; 125 ts = systime() - starttime; 126 tm = int(ts / 60); 126 tm = int(ts / 60); 127 th = int(ts / 3600); 127 th = int(ts / 3600); 128 td = int(ts / 86400); 128 td = int(ts / 86400); 129 d = td; 129 d = td; 130 h = th - td * 24; 130 h = th - td * 24; 131 m = tm - th * 60; 131 m = tm - th * 60; 132 s = ts - tm * 60; 132 s = ts - tm * 60; 133 if (d >= 1) 133 if (d >= 1) 134 printf "%dd %d:%02d:%0 134 printf "%dd %d:%02d:%02d\n", d, h, m, s 135 else if (h >= 1) 135 else if (h >= 1) 136 printf "%d:%02d:%02d\n 136 printf "%d:%02d:%02d\n", h, m, s 137 else if (m >= 1) 137 else if (m >= 1) 138 printf "%d:%02d.0\n", 138 printf "%d:%02d.0\n", m, s 139 else 139 else 140 print s " seconds" 140 print s " seconds" 141 }' < /dev/null 141 }' < /dev/null 142 } 142 } 143 143 144 # identify_boot_image qemu-cmd 144 # identify_boot_image qemu-cmd 145 # 145 # 146 # Returns the relative path to the kernel buil 146 # Returns the relative path to the kernel build image. This will be 147 # arch/<arch>/boot/bzImage or vmlinux if bzIma 147 # arch/<arch>/boot/bzImage or vmlinux if bzImage is not a target for the 148 # architecture, unless overridden with the TOR 148 # architecture, unless overridden with the TORTURE_BOOT_IMAGE environment 149 # variable. 149 # variable. 150 identify_boot_image () { 150 identify_boot_image () { 151 if test -n "$TORTURE_BOOT_IMAGE" 151 if test -n "$TORTURE_BOOT_IMAGE" 152 then 152 then 153 echo $TORTURE_BOOT_IMAGE 153 echo $TORTURE_BOOT_IMAGE 154 else 154 else 155 case "$1" in 155 case "$1" in 156 qemu-system-x86_64|qemu-system 156 qemu-system-x86_64|qemu-system-i386) 157 echo arch/x86/boot/bzI 157 echo arch/x86/boot/bzImage 158 ;; 158 ;; 159 qemu-system-aarch64) 159 qemu-system-aarch64) 160 echo arch/arm64/boot/I 160 echo arch/arm64/boot/Image 161 ;; 161 ;; 162 qemu-system-s390x) 162 qemu-system-s390x) 163 echo arch/s390/boot/bz 163 echo arch/s390/boot/bzImage 164 ;; 164 ;; 165 *) 165 *) 166 echo vmlinux 166 echo vmlinux 167 ;; 167 ;; 168 esac 168 esac 169 fi 169 fi 170 } 170 } 171 171 172 # identify_qemu builddir 172 # identify_qemu builddir 173 # 173 # 174 # Returns our best guess as to which qemu comm 174 # Returns our best guess as to which qemu command is appropriate for 175 # the kernel at hand. Override with the TORTU 175 # the kernel at hand. Override with the TORTURE_QEMU_CMD environment variable. 176 identify_qemu () { 176 identify_qemu () { 177 local u="`file "$1"`" 177 local u="`file "$1"`" 178 if test -n "$TORTURE_QEMU_CMD" 178 if test -n "$TORTURE_QEMU_CMD" 179 then 179 then 180 echo $TORTURE_QEMU_CMD 180 echo $TORTURE_QEMU_CMD 181 elif echo $u | grep -q x86-64 181 elif echo $u | grep -q x86-64 182 then 182 then 183 echo qemu-system-x86_64 183 echo qemu-system-x86_64 184 elif echo $u | grep -q "Intel 80386" 184 elif echo $u | grep -q "Intel 80386" 185 then 185 then 186 echo qemu-system-i386 186 echo qemu-system-i386 187 elif echo $u | grep -q aarch64 187 elif echo $u | grep -q aarch64 188 then 188 then 189 echo qemu-system-aarch64 189 echo qemu-system-aarch64 190 elif echo $u | grep -q 'IBM S/390' 190 elif echo $u | grep -q 'IBM S/390' 191 then 191 then 192 echo qemu-system-s390x 192 echo qemu-system-s390x 193 elif uname -a | grep -q ppc64 193 elif uname -a | grep -q ppc64 194 then 194 then 195 echo qemu-system-ppc64 195 echo qemu-system-ppc64 196 else 196 else 197 echo Cannot figure out what qe 197 echo Cannot figure out what qemu command to use! 1>&2 198 echo file $1 output: $u 198 echo file $1 output: $u 199 # Usually this will be one of 199 # Usually this will be one of /usr/bin/qemu-system-* 200 # Use TORTURE_QEMU_CMD environ 200 # Use TORTURE_QEMU_CMD environment variable or appropriate 201 # argument to top-level script 201 # argument to top-level script. 202 exit 1 202 exit 1 203 fi 203 fi 204 } 204 } 205 205 206 # identify_qemu_append qemu-cmd 206 # identify_qemu_append qemu-cmd 207 # 207 # 208 # Output arguments for the qemu "-append" stri 208 # Output arguments for the qemu "-append" string based on CPU type 209 # and the TORTURE_QEMU_INTERACTIVE environment 209 # and the TORTURE_QEMU_INTERACTIVE environment variable. 210 identify_qemu_append () { 210 identify_qemu_append () { 211 echo debug_boot_weak_hash 211 echo debug_boot_weak_hash 212 echo panic=-1 212 echo panic=-1 213 local console=ttyS0 213 local console=ttyS0 214 case "$1" in 214 case "$1" in 215 qemu-system-x86_64|qemu-system-i386) 215 qemu-system-x86_64|qemu-system-i386) 216 echo selinux=0 initcall_debug 216 echo selinux=0 initcall_debug debug 217 ;; 217 ;; 218 qemu-system-aarch64) 218 qemu-system-aarch64) 219 console=ttyAMA0 219 console=ttyAMA0 220 ;; 220 ;; 221 esac 221 esac 222 if test -n "$TORTURE_QEMU_INTERACTIVE" 222 if test -n "$TORTURE_QEMU_INTERACTIVE" 223 then 223 then 224 echo root=/dev/sda 224 echo root=/dev/sda 225 else 225 else 226 echo console=$console 226 echo console=$console 227 fi 227 fi 228 } 228 } 229 229 230 # identify_qemu_args qemu-cmd serial-file 230 # identify_qemu_args qemu-cmd serial-file 231 # 231 # 232 # Output arguments for qemu arguments based on 232 # Output arguments for qemu arguments based on the TORTURE_QEMU_MAC 233 # and TORTURE_QEMU_INTERACTIVE environment var 233 # and TORTURE_QEMU_INTERACTIVE environment variables. 234 identify_qemu_args () { 234 identify_qemu_args () { 235 local KVM_CPU="" 235 local KVM_CPU="" 236 case "$1" in 236 case "$1" in 237 qemu-system-x86_64) 237 qemu-system-x86_64) 238 KVM_CPU=kvm64 238 KVM_CPU=kvm64 239 ;; 239 ;; 240 qemu-system-i386) 240 qemu-system-i386) 241 KVM_CPU=kvm32 241 KVM_CPU=kvm32 242 ;; 242 ;; 243 esac 243 esac 244 case "$1" in 244 case "$1" in 245 qemu-system-x86_64|qemu-system-i386) 245 qemu-system-x86_64|qemu-system-i386) 246 echo -machine q35,accel=kvm 246 echo -machine q35,accel=kvm 247 echo -cpu ${KVM_CPU} 247 echo -cpu ${KVM_CPU} 248 ;; 248 ;; 249 qemu-system-aarch64) 249 qemu-system-aarch64) 250 echo -machine virt,gic-version 250 echo -machine virt,gic-version=host -cpu host 251 ;; 251 ;; 252 qemu-system-ppc64) 252 qemu-system-ppc64) 253 echo -M pseries -nodefaults 253 echo -M pseries -nodefaults 254 echo -device spapr-vscsi 254 echo -device spapr-vscsi 255 if test -n "$TORTURE_QEMU_INTE 255 if test -n "$TORTURE_QEMU_INTERACTIVE" -a -n "$TORTURE_QEMU_MAC" 256 then 256 then 257 echo -device spapr-vla 257 echo -device spapr-vlan,netdev=net0,mac=$TORTURE_QEMU_MAC 258 echo -netdev bridge,br 258 echo -netdev bridge,br=br0,id=net0 259 fi 259 fi 260 ;; 260 ;; 261 esac 261 esac 262 if test -n "$TORTURE_QEMU_INTERACTIVE" 262 if test -n "$TORTURE_QEMU_INTERACTIVE" 263 then 263 then 264 echo -monitor stdio -serial pt 264 echo -monitor stdio -serial pty -S 265 else 265 else 266 echo -serial file:$2 266 echo -serial file:$2 267 fi 267 fi 268 } 268 } 269 269 270 # identify_qemu_vcpus 270 # identify_qemu_vcpus 271 # 271 # 272 # Returns the number of virtual CPUs available 272 # Returns the number of virtual CPUs available to the aggregate of the 273 # guest OSes. 273 # guest OSes. 274 identify_qemu_vcpus () { 274 identify_qemu_vcpus () { 275 getconf _NPROCESSORS_ONLN 275 getconf _NPROCESSORS_ONLN 276 } 276 } 277 277 278 # print_bug 278 # print_bug 279 # 279 # 280 # Prints "BUG: " in red followed by remaining 280 # Prints "BUG: " in red followed by remaining arguments 281 print_bug () { 281 print_bug () { 282 printf '\033[031mBUG: \033[m' 282 printf '\033[031mBUG: \033[m' 283 echo $* 283 echo $* 284 } 284 } 285 285 286 # print_warning 286 # print_warning 287 # 287 # 288 # Prints "WARNING: " in yellow followed by rem 288 # Prints "WARNING: " in yellow followed by remaining arguments 289 print_warning () { 289 print_warning () { 290 printf '\033[033mWARNING: \033[m' 290 printf '\033[033mWARNING: \033[m' 291 echo $* 291 echo $* 292 } 292 } 293 293 294 # specify_qemu_cpus qemu-cmd qemu-args #cpus 294 # specify_qemu_cpus qemu-cmd qemu-args #cpus 295 # 295 # 296 # Appends a string containing "-smp XXX" to qe 296 # Appends a string containing "-smp XXX" to qemu-args, unless the incoming 297 # qemu-args already contains "-smp". 297 # qemu-args already contains "-smp". 298 specify_qemu_cpus () { 298 specify_qemu_cpus () { 299 local nt; 299 local nt; 300 300 301 if echo $2 | grep -q -e -smp 301 if echo $2 | grep -q -e -smp 302 then 302 then 303 echo $2 303 echo $2 304 else 304 else 305 case "$1" in 305 case "$1" in 306 qemu-system-x86_64|qemu-system 306 qemu-system-x86_64|qemu-system-i386|qemu-system-aarch64) 307 echo $2 -smp $3 307 echo $2 -smp $3 308 ;; 308 ;; 309 qemu-system-ppc64) 309 qemu-system-ppc64) 310 nt="`lscpu | sed -n 's 310 nt="`lscpu | sed -n 's/^Thread(s) per core:\s*//p'`" 311 echo $2 -smp cores=`ex 311 echo $2 -smp cores=`expr \( $3 + $nt - 1 \) / $nt`,threads=$nt 312 ;; 312 ;; 313 esac 313 esac 314 fi 314 fi 315 } 315 } 316 316 317 # specify_qemu_net qemu-args 317 # specify_qemu_net qemu-args 318 # 318 # 319 # Appends a string containing "-net none" to q 319 # Appends a string containing "-net none" to qemu-args, unless the incoming 320 # qemu-args already contains "-smp" or unless 320 # qemu-args already contains "-smp" or unless the TORTURE_QEMU_INTERACTIVE 321 # environment variable is set, in which case t 321 # environment variable is set, in which case the string that is be added is 322 # instead "-net nic -net user". 322 # instead "-net nic -net user". 323 specify_qemu_net () { 323 specify_qemu_net () { 324 if echo $1 | grep -q -e -net 324 if echo $1 | grep -q -e -net 325 then 325 then 326 echo $1 326 echo $1 327 elif test -n "$TORTURE_QEMU_INTERACTIV 327 elif test -n "$TORTURE_QEMU_INTERACTIVE" 328 then 328 then 329 echo $1 -net nic -net user 329 echo $1 -net nic -net user 330 else 330 else 331 echo $1 -net none 331 echo $1 -net none 332 fi 332 fi 333 } 333 } 334 334 335 # Extract the ftrace output from the console l 335 # Extract the ftrace output from the console log output 336 # The ftrace output in the original logs look 336 # The ftrace output in the original logs look like: 337 # Dumping ftrace buffer: 337 # Dumping ftrace buffer: 338 # --------------------------------- 338 # --------------------------------- 339 # [...] 339 # [...] 340 # --------------------------------- 340 # --------------------------------- 341 extract_ftrace_from_console() { 341 extract_ftrace_from_console() { 342 awk < "$1" ' 342 awk < "$1" ' 343 343 344 /Dumping ftrace buffer:/ { 344 /Dumping ftrace buffer:/ { 345 buffer_count++ 345 buffer_count++ 346 print "Ftrace dump " buffer_co 346 print "Ftrace dump " buffer_count ":" 347 capture = 1 347 capture = 1 348 next 348 next 349 } 349 } 350 350 351 /---------------------------------/ { 351 /---------------------------------/ { 352 if(capture == 1) { 352 if(capture == 1) { 353 capture = 2 353 capture = 2 354 next 354 next 355 } else if(capture == 2) { 355 } else if(capture == 2) { 356 capture = 0 356 capture = 0 357 print "" 357 print "" 358 } 358 } 359 } 359 } 360 360 361 capture == 2' 361 capture == 2' 362 } 362 }
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.