lock_contention.sh 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #!/bin/sh
  2. # kernel lock contention analysis test
  3. # SPDX-License-Identifier: GPL-2.0
  4. set -e
  5. err=0
  6. perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
  7. result=$(mktemp /tmp/__perf_test.result.XXXXX)
  8. cleanup() {
  9. rm -f ${perfdata}
  10. rm -f ${result}
  11. trap - EXIT TERM INT
  12. }
  13. trap_cleanup() {
  14. cleanup
  15. exit ${err}
  16. }
  17. trap trap_cleanup EXIT TERM INT
  18. check() {
  19. if [ "$(id -u)" != 0 ]; then
  20. echo "[Skip] No root permission"
  21. err=2
  22. exit
  23. fi
  24. if ! perf list | grep -q lock:contention_begin; then
  25. echo "[Skip] No lock contention tracepoints"
  26. err=2
  27. exit
  28. fi
  29. # shellcheck disable=SC2046
  30. if [ `nproc` -lt 4 ]; then
  31. echo "[Skip] Low number of CPUs (`nproc`), lock event cannot be triggered certainly"
  32. err=2
  33. exit
  34. fi
  35. }
  36. test_record()
  37. {
  38. echo "Testing perf lock record and perf lock contention"
  39. perf lock record -o ${perfdata} -- perf bench sched messaging > /dev/null 2>&1
  40. # the output goes to the stderr and we expect only 1 output (-E 1)
  41. perf lock contention -i ${perfdata} -E 1 -q 2> ${result}
  42. if [ "$(cat "${result}" | wc -l)" != "1" ]; then
  43. echo "[Fail] Recorded result count is not 1:" "$(cat "${result}" | wc -l)"
  44. err=1
  45. exit
  46. fi
  47. }
  48. test_bpf()
  49. {
  50. echo "Testing perf lock contention --use-bpf"
  51. if ! perf lock con -b true > /dev/null 2>&1 ; then
  52. echo "[Skip] No BPF support"
  53. return
  54. fi
  55. # the perf lock contention output goes to the stderr
  56. perf lock con -a -b -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
  57. if [ "$(cat "${result}" | wc -l)" != "1" ]; then
  58. echo "[Fail] BPF result count is not 1:" "$(cat "${result}" | wc -l)"
  59. err=1
  60. exit
  61. fi
  62. }
  63. test_record_concurrent()
  64. {
  65. echo "Testing perf lock record and perf lock contention at the same time"
  66. perf lock record -o- -- perf bench sched messaging 2> /dev/null | \
  67. perf lock contention -i- -E 1 -q 2> ${result}
  68. if [ "$(cat "${result}" | wc -l)" != "1" ]; then
  69. echo "[Fail] Recorded result count is not 1:" "$(cat "${result}" | wc -l)"
  70. err=1
  71. exit
  72. fi
  73. }
  74. test_aggr_task()
  75. {
  76. echo "Testing perf lock contention --threads"
  77. perf lock contention -i ${perfdata} -t -E 1 -q 2> ${result}
  78. if [ "$(cat "${result}" | wc -l)" != "1" ]; then
  79. echo "[Fail] Recorded result count is not 1:" "$(cat "${result}" | wc -l)"
  80. err=1
  81. exit
  82. fi
  83. if ! perf lock con -b true > /dev/null 2>&1 ; then
  84. return
  85. fi
  86. # the perf lock contention output goes to the stderr
  87. perf lock con -a -b -t -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
  88. if [ "$(cat "${result}" | wc -l)" != "1" ]; then
  89. echo "[Fail] BPF result count is not 1:" "$(cat "${result}" | wc -l)"
  90. err=1
  91. exit
  92. fi
  93. }
  94. test_aggr_addr()
  95. {
  96. echo "Testing perf lock contention --lock-addr"
  97. perf lock contention -i ${perfdata} -l -E 1 -q 2> ${result}
  98. if [ "$(cat "${result}" | wc -l)" != "1" ]; then
  99. echo "[Fail] Recorded result count is not 1:" "$(cat "${result}" | wc -l)"
  100. err=1
  101. exit
  102. fi
  103. if ! perf lock con -b true > /dev/null 2>&1 ; then
  104. return
  105. fi
  106. # the perf lock contention output goes to the stderr
  107. perf lock con -a -b -l -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
  108. if [ "$(cat "${result}" | wc -l)" != "1" ]; then
  109. echo "[Fail] BPF result count is not 1:" "$(cat "${result}" | wc -l)"
  110. err=1
  111. exit
  112. fi
  113. }
  114. test_aggr_cgroup()
  115. {
  116. echo "Testing perf lock contention --lock-cgroup"
  117. if ! perf lock con -b true > /dev/null 2>&1 ; then
  118. echo "[Skip] No BPF support"
  119. return
  120. fi
  121. # the perf lock contention output goes to the stderr
  122. perf lock con -a -b -g -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
  123. if [ "$(cat "${result}" | wc -l)" != "1" ]; then
  124. echo "[Fail] BPF result count is not 1:" "$(cat "${result}" | wc -l)"
  125. err=1
  126. exit
  127. fi
  128. }
  129. test_type_filter()
  130. {
  131. echo "Testing perf lock contention --type-filter (w/ spinlock)"
  132. perf lock contention -i ${perfdata} -Y spinlock -q 2> ${result}
  133. if [ "$(grep -c -v spinlock "${result}")" != "0" ]; then
  134. echo "[Fail] Recorded result should not have non-spinlocks:" "$(cat "${result}")"
  135. err=1
  136. exit
  137. fi
  138. if ! perf lock con -b true > /dev/null 2>&1 ; then
  139. return
  140. fi
  141. perf lock con -a -b -Y spinlock -q -- perf bench sched messaging > /dev/null 2> ${result}
  142. if [ "$(grep -c -v spinlock "${result}")" != "0" ]; then
  143. echo "[Fail] BPF result should not have non-spinlocks:" "$(cat "${result}")"
  144. err=1
  145. exit
  146. fi
  147. }
  148. test_lock_filter()
  149. {
  150. echo "Testing perf lock contention --lock-filter (w/ tasklist_lock)"
  151. perf lock contention -i ${perfdata} -l -q 2> ${result}
  152. if [ "$(grep -c tasklist_lock "${result}")" != "1" ]; then
  153. echo "[Skip] Could not find 'tasklist_lock'"
  154. return
  155. fi
  156. perf lock contention -i ${perfdata} -L tasklist_lock -q 2> ${result}
  157. # find out the type of tasklist_lock
  158. test_lock_filter_type=$(head -1 "${result}" | awk '{ print $8 }' | sed -e 's/:.*//')
  159. if [ "$(grep -c -v "${test_lock_filter_type}" "${result}")" != "0" ]; then
  160. echo "[Fail] Recorded result should not have non-${test_lock_filter_type} locks:" "$(cat "${result}")"
  161. err=1
  162. exit
  163. fi
  164. if ! perf lock con -b true > /dev/null 2>&1 ; then
  165. return
  166. fi
  167. perf lock con -a -b -L tasklist_lock -q -- perf bench sched messaging > /dev/null 2> ${result}
  168. if [ "$(grep -c -v "${test_lock_filter_type}" "${result}")" != "0" ]; then
  169. echo "[Fail] BPF result should not have non-${test_lock_filter_type} locks:" "$(cat "${result}")"
  170. err=1
  171. exit
  172. fi
  173. }
  174. test_stack_filter()
  175. {
  176. echo "Testing perf lock contention --callstack-filter (w/ unix_stream)"
  177. perf lock contention -i ${perfdata} -v -q 2> ${result}
  178. if [ "$(grep -c unix_stream "${result}")" = "0" ]; then
  179. echo "[Skip] Could not find 'unix_stream'"
  180. return
  181. fi
  182. perf lock contention -i ${perfdata} -E 1 -S unix_stream -q 2> ${result}
  183. if [ "$(cat "${result}" | wc -l)" != "1" ]; then
  184. echo "[Fail] Recorded result should have a lock from unix_stream:" "$(cat "${result}")"
  185. err=1
  186. exit
  187. fi
  188. if ! perf lock con -b true > /dev/null 2>&1 ; then
  189. return
  190. fi
  191. perf lock con -a -b -S unix_stream -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
  192. if [ "$(cat "${result}" | wc -l)" != "1" ]; then
  193. echo "[Fail] BPF result should have a lock from unix_stream:" "$(cat "${result}")"
  194. err=1
  195. exit
  196. fi
  197. }
  198. test_aggr_task_stack_filter()
  199. {
  200. echo "Testing perf lock contention --callstack-filter with task aggregation"
  201. perf lock contention -i ${perfdata} -v -q 2> ${result}
  202. if [ "$(grep -c unix_stream "${result}")" = "0" ]; then
  203. echo "[Skip] Could not find 'unix_stream'"
  204. return
  205. fi
  206. perf lock contention -i ${perfdata} -t -E 1 -S unix_stream -q 2> ${result}
  207. if [ "$(cat "${result}" | wc -l)" != "1" ]; then
  208. echo "[Fail] Recorded result should have a task from unix_stream:" "$(cat "${result}")"
  209. err=1
  210. exit
  211. fi
  212. if ! perf lock con -b true > /dev/null 2>&1 ; then
  213. return
  214. fi
  215. perf lock con -a -b -t -S unix_stream -E 1 -q -- perf bench sched messaging > /dev/null 2> ${result}
  216. if [ "$(cat "${result}" | wc -l)" != "1" ]; then
  217. echo "[Fail] BPF result should have a task from unix_stream:" "$(cat "${result}")"
  218. err=1
  219. exit
  220. fi
  221. }
  222. test_cgroup_filter()
  223. {
  224. echo "Testing perf lock contention --cgroup-filter"
  225. if ! perf lock con -b true > /dev/null 2>&1 ; then
  226. echo "[Skip] No BPF support"
  227. return
  228. fi
  229. perf lock con -a -b -g -E 1 -F wait_total -q -- perf bench sched messaging > /dev/null 2> ${result}
  230. if [ "$(cat "${result}" | wc -l)" != "1" ]; then
  231. echo "[Fail] BPF result should have a cgroup result:" "$(cat "${result}")"
  232. err=1
  233. exit
  234. fi
  235. cgroup=$(cat "${result}" | awk '{ print $3 }')
  236. perf lock con -a -b -g -E 1 -G "${cgroup}" -q -- perf bench sched messaging > /dev/null 2> ${result}
  237. if [ "$(cat "${result}" | wc -l)" != "1" ]; then
  238. echo "[Fail] BPF result should have a result with cgroup filter:" "$(cat "${cgroup}")"
  239. err=1
  240. exit
  241. fi
  242. }
  243. test_csv_output()
  244. {
  245. echo "Testing perf lock contention CSV output"
  246. perf lock contention -i ${perfdata} -E 1 -x , --output ${result}
  247. # count the number of commas in the header
  248. # it should have 5: contended, total-wait, max-wait, avg-wait, type, caller
  249. header=$(grep "# output:" ${result} | tr -d -c , | wc -c)
  250. if [ "${header}" != "5" ]; then
  251. echo "[Fail] Recorded result does not have enough output columns: ${header} != 5"
  252. err=1
  253. exit
  254. fi
  255. # count the number of commas in the output
  256. output=$(grep -v "^#" ${result} | tr -d -c , | wc -c)
  257. if [ "${header}" != "${output}" ]; then
  258. echo "[Fail] Recorded result does not match the number of commas: ${header} != ${output}"
  259. err=1
  260. exit
  261. fi
  262. if ! perf lock con -b true > /dev/null 2>&1 ; then
  263. echo "[Skip] No BPF support"
  264. return
  265. fi
  266. # the perf lock contention output goes to the stderr
  267. perf lock con -a -b -E 1 -x , --output ${result} -- perf bench sched messaging > /dev/null 2>&1
  268. output=$(grep -v "^#" ${result} | tr -d -c , | wc -c)
  269. if [ "${header}" != "${output}" ]; then
  270. echo "[Fail] BPF result does not match the number of commas: ${header} != ${output}"
  271. err=1
  272. exit
  273. fi
  274. }
  275. check
  276. test_record
  277. test_bpf
  278. test_record_concurrent
  279. test_aggr_task
  280. test_aggr_addr
  281. test_aggr_cgroup
  282. test_type_filter
  283. test_lock_filter
  284. test_stack_filter
  285. test_aggr_task_stack_filter
  286. test_cgroup_filter
  287. test_csv_output
  288. exit ${err}