functions.sh 8.0 KB

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