functions.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. # bootparam_hotplug_cpu bootparam-string
  23. #
  24. # Returns 1 if the specified boot-parameter string tells rcutorture to
  25. # test CPU-hotplug operations.
  26. bootparam_hotplug_cpu () {
  27. echo "$1" | grep -q "rcutorture\.onoff_"
  28. }
  29. # checkarg --argname argtype $# arg mustmatch cannotmatch
  30. #
  31. # Checks the specified argument "arg" against the mustmatch and cannotmatch
  32. # patterns.
  33. checkarg () {
  34. if test $3 -le 1
  35. then
  36. echo $1 needs argument $2 matching \"$5\"
  37. usage
  38. fi
  39. if echo "$4" | grep -q -e "$5"
  40. then
  41. :
  42. else
  43. echo $1 $2 \"$4\" must match \"$5\"
  44. usage
  45. fi
  46. if echo "$4" | grep -q -e "$6"
  47. then
  48. echo $1 $2 \"$4\" must not match \"$6\"
  49. usage
  50. fi
  51. }
  52. # configfrag_boot_params bootparam-string config-fragment-file
  53. #
  54. # Adds boot parameters from the .boot file, if any.
  55. configfrag_boot_params () {
  56. if test -r "$2.boot"
  57. then
  58. echo $1 `grep -v '^#' "$2.boot" | tr '\012' ' '`
  59. else
  60. echo $1
  61. fi
  62. }
  63. # configfrag_boot_cpus bootparam-string config-fragment-file config-cpus
  64. #
  65. # Decreases number of CPUs based on any nr_cpus= boot parameters specified.
  66. configfrag_boot_cpus () {
  67. local bootargs="`configfrag_boot_params "$1" "$2"`"
  68. local nr_cpus
  69. if echo "${bootargs}" | grep -q 'nr_cpus=[0-9]'
  70. then
  71. nr_cpus="`echo "${bootargs}" | sed -e 's/^.*nr_cpus=\([0-9]*\).*$/\1/'`"
  72. if test "$3" -gt "$nr_cpus"
  73. then
  74. echo $nr_cpus
  75. else
  76. echo $3
  77. fi
  78. else
  79. echo $3
  80. fi
  81. }
  82. # configfrag_boot_maxcpus bootparam-string config-fragment-file config-cpus
  83. #
  84. # Decreases number of CPUs based on any maxcpus= boot parameters specified.
  85. # This allows tests where additional CPUs come online later during the
  86. # test run. However, the torture parameters will be set based on the
  87. # number of CPUs initially present, so the scripting should schedule
  88. # test runs based on the maxcpus= boot parameter controlling the initial
  89. # number of CPUs instead of on the ultimate number of CPUs.
  90. configfrag_boot_maxcpus () {
  91. local bootargs="`configfrag_boot_params "$1" "$2"`"
  92. local maxcpus
  93. if echo "${bootargs}" | grep -q 'maxcpus=[0-9]'
  94. then
  95. maxcpus="`echo "${bootargs}" | sed -e 's/^.*maxcpus=\([0-9]*\).*$/\1/'`"
  96. if test "$3" -gt "$maxcpus"
  97. then
  98. echo $maxcpus
  99. else
  100. echo $3
  101. fi
  102. else
  103. echo $3
  104. fi
  105. }
  106. # configfrag_hotplug_cpu config-fragment-file
  107. #
  108. # Returns 1 if the config fragment specifies hotplug CPU.
  109. configfrag_hotplug_cpu () {
  110. if test ! -r "$1"
  111. then
  112. echo Unreadable config fragment "$1" 1>&2
  113. exit -1
  114. fi
  115. grep -q '^CONFIG_HOTPLUG_CPU=y$' "$1"
  116. }
  117. # identify_boot_image qemu-cmd
  118. #
  119. # Returns the relative path to the kernel build image. This will be
  120. # arch/<arch>/boot/bzImage or vmlinux if bzImage is not a target for the
  121. # architecture, unless overridden with the TORTURE_BOOT_IMAGE environment
  122. # variable.
  123. identify_boot_image () {
  124. if test -n "$TORTURE_BOOT_IMAGE"
  125. then
  126. echo $TORTURE_BOOT_IMAGE
  127. else
  128. case "$1" in
  129. qemu-system-x86_64|qemu-system-i386)
  130. echo arch/x86/boot/bzImage
  131. ;;
  132. qemu-system-aarch64)
  133. echo arch/arm64/boot/Image
  134. ;;
  135. *)
  136. echo vmlinux
  137. ;;
  138. esac
  139. fi
  140. }
  141. # identify_qemu builddir
  142. #
  143. # Returns our best guess as to which qemu command is appropriate for
  144. # the kernel at hand. Override with the TORTURE_QEMU_CMD environment variable.
  145. identify_qemu () {
  146. local u="`file "$1"`"
  147. if test -n "$TORTURE_QEMU_CMD"
  148. then
  149. echo $TORTURE_QEMU_CMD
  150. elif echo $u | grep -q x86-64
  151. then
  152. echo qemu-system-x86_64
  153. elif echo $u | grep -q "Intel 80386"
  154. then
  155. echo qemu-system-i386
  156. elif echo $u | grep -q aarch64
  157. then
  158. echo qemu-system-aarch64
  159. elif uname -a | grep -q ppc64
  160. then
  161. echo qemu-system-ppc64
  162. else
  163. echo Cannot figure out what qemu command to use! 1>&2
  164. echo file $1 output: $u
  165. # Usually this will be one of /usr/bin/qemu-system-*
  166. # Use TORTURE_QEMU_CMD environment variable or appropriate
  167. # argument to top-level script.
  168. exit 1
  169. fi
  170. }
  171. # identify_qemu_append qemu-cmd
  172. #
  173. # Output arguments for the qemu "-append" string based on CPU type
  174. # and the TORTURE_QEMU_INTERACTIVE environment variable.
  175. identify_qemu_append () {
  176. local console=ttyS0
  177. case "$1" in
  178. qemu-system-x86_64|qemu-system-i386)
  179. echo noapic selinux=0 initcall_debug debug
  180. ;;
  181. qemu-system-aarch64)
  182. console=ttyAMA0
  183. ;;
  184. esac
  185. if test -n "$TORTURE_QEMU_INTERACTIVE"
  186. then
  187. echo root=/dev/sda
  188. else
  189. echo console=$console
  190. fi
  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-aarch64)
  201. echo -machine virt,gic-version=host -cpu host
  202. ;;
  203. qemu-system-ppc64)
  204. echo -enable-kvm -M pseries -nodefaults
  205. echo -device spapr-vscsi
  206. if test -n "$TORTURE_QEMU_INTERACTIVE" -a -n "$TORTURE_QEMU_MAC"
  207. then
  208. echo -device spapr-vlan,netdev=net0,mac=$TORTURE_QEMU_MAC
  209. echo -netdev bridge,br=br0,id=net0
  210. elif test -n "$TORTURE_QEMU_INTERACTIVE"
  211. then
  212. echo -net nic -net user
  213. fi
  214. ;;
  215. esac
  216. if test -n "$TORTURE_QEMU_INTERACTIVE"
  217. then
  218. echo -monitor stdio -serial pty -S
  219. else
  220. echo -serial file:$2
  221. fi
  222. }
  223. # identify_qemu_vcpus
  224. #
  225. # Returns the number of virtual CPUs available to the aggregate of the
  226. # guest OSes.
  227. identify_qemu_vcpus () {
  228. lscpu | grep '^CPU(s):' | sed -e 's/CPU(s)://'
  229. }
  230. # print_bug
  231. #
  232. # Prints "BUG: " in red followed by remaining arguments
  233. print_bug () {
  234. printf '\033[031mBUG: \033[m'
  235. echo $*
  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. # specify_qemu_cpus qemu-cmd qemu-args #cpus
  245. #
  246. # Appends a string containing "-smp XXX" to qemu-args, unless the incoming
  247. # qemu-args already contains "-smp".
  248. specify_qemu_cpus () {
  249. local nt;
  250. if echo $2 | grep -q -e -smp
  251. then
  252. echo $2
  253. else
  254. case "$1" in
  255. qemu-system-x86_64|qemu-system-i386|qemu-system-aarch64)
  256. echo $2 -smp $3
  257. ;;
  258. qemu-system-ppc64)
  259. nt="`lscpu | grep '^NUMA node0' | sed -e 's/^[^,]*,\([0-9]*\),.*$/\1/'`"
  260. echo $2 -smp cores=`expr \( $3 + $nt - 1 \) / $nt`,threads=$nt
  261. ;;
  262. esac
  263. fi
  264. }