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