stat.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/bin/sh
  2. # perf stat tests
  3. # SPDX-License-Identifier: GPL-2.0
  4. set -e
  5. err=0
  6. test_default_stat() {
  7. echo "Basic stat command test"
  8. if ! perf stat true 2>&1 | grep -E -q "Performance counter stats for 'true':"
  9. then
  10. echo "Basic stat command test [Failed]"
  11. err=1
  12. return
  13. fi
  14. echo "Basic stat command test [Success]"
  15. }
  16. test_stat_record_report() {
  17. echo "stat record and report test"
  18. if ! perf stat record -o - true | perf stat report -i - 2>&1 | \
  19. grep -E -q "Performance counter stats for 'pipe':"
  20. then
  21. echo "stat record and report test [Failed]"
  22. err=1
  23. return
  24. fi
  25. echo "stat record and report test [Success]"
  26. }
  27. test_stat_record_script() {
  28. echo "stat record and script test"
  29. if ! perf stat record -o - true | perf script -i - 2>&1 | \
  30. grep -E -q "CPU[[:space:]]+THREAD[[:space:]]+VAL[[:space:]]+ENA[[:space:]]+RUN[[:space:]]+TIME[[:space:]]+EVENT"
  31. then
  32. echo "stat record and script test [Failed]"
  33. err=1
  34. return
  35. fi
  36. echo "stat record and script test [Success]"
  37. }
  38. test_stat_repeat_weak_groups() {
  39. echo "stat repeat weak groups test"
  40. if ! perf stat -e '{cycles,cycles,cycles,cycles,cycles,cycles,cycles,cycles,cycles,cycles}' \
  41. true 2>&1 | grep -q 'seconds time elapsed'
  42. then
  43. echo "stat repeat weak groups test [Skipped event parsing failed]"
  44. return
  45. fi
  46. if ! perf stat -r2 -e '{cycles,cycles,cycles,cycles,cycles,cycles,cycles,cycles,cycles,cycles}:W' \
  47. true > /dev/null 2>&1
  48. then
  49. echo "stat repeat weak groups test [Failed]"
  50. err=1
  51. return
  52. fi
  53. echo "stat repeat weak groups test [Success]"
  54. }
  55. test_topdown_groups() {
  56. # Topdown events must be grouped with the slots event first. Test that
  57. # parse-events reorders this.
  58. echo "Topdown event group test"
  59. if ! perf stat -e '{slots,topdown-retiring}' true > /dev/null 2>&1
  60. then
  61. echo "Topdown event group test [Skipped event parsing failed]"
  62. return
  63. fi
  64. if perf stat -e '{slots,topdown-retiring}' true 2>&1 | grep -E -q "<not supported>"
  65. then
  66. echo "Topdown event group test [Failed events not supported]"
  67. err=1
  68. return
  69. fi
  70. if perf stat -e '{topdown-retiring,slots}' true 2>&1 | grep -E -q "<not supported>"
  71. then
  72. echo "Topdown event group test [Failed slots not reordered first]"
  73. err=1
  74. return
  75. fi
  76. echo "Topdown event group test [Success]"
  77. }
  78. test_topdown_weak_groups() {
  79. # Weak groups break if the perf_event_open of multiple grouped events
  80. # fails. Breaking a topdown group causes the events to fail. Test a very large
  81. # grouping to see that the topdown events aren't broken out.
  82. echo "Topdown weak groups test"
  83. ok_grouping="{slots,topdown-bad-spec,topdown-be-bound,topdown-fe-bound,topdown-retiring},branch-instructions,branch-misses,bus-cycles,cache-misses,cache-references,cpu-cycles,instructions,mem-loads,mem-stores,ref-cycles,cache-misses,cache-references"
  84. if ! perf stat --no-merge -e "$ok_grouping" true > /dev/null 2>&1
  85. then
  86. echo "Topdown weak groups test [Skipped event parsing failed]"
  87. return
  88. fi
  89. group_needs_break="{slots,topdown-bad-spec,topdown-be-bound,topdown-fe-bound,topdown-retiring,branch-instructions,branch-misses,bus-cycles,cache-misses,cache-references,cpu-cycles,instructions,mem-loads,mem-stores,ref-cycles,cache-misses,cache-references}:W"
  90. if perf stat --no-merge -e "$group_needs_break" true 2>&1 | grep -E -q "<not supported>"
  91. then
  92. echo "Topdown weak groups test [Failed events not supported]"
  93. err=1
  94. return
  95. fi
  96. echo "Topdown weak groups test [Success]"
  97. }
  98. test_cputype() {
  99. # Test --cputype argument.
  100. echo "cputype test"
  101. # Bogus PMU should fail.
  102. if perf stat --cputype="123" -e instructions true > /dev/null 2>&1
  103. then
  104. echo "cputype test [Bogus PMU didn't fail]"
  105. err=1
  106. return
  107. fi
  108. # Find a known PMU for cputype.
  109. pmu=""
  110. for i in cpu cpu_atom armv8_pmuv3_0
  111. do
  112. if test -d "/sys/devices/$i"
  113. then
  114. pmu="$i"
  115. break
  116. fi
  117. if perf stat -e "$i/instructions/" true > /dev/null 2>&1
  118. then
  119. pmu="$i"
  120. break
  121. fi
  122. done
  123. if test "x$pmu" = "x"
  124. then
  125. echo "cputype test [Skipped known PMU not found]"
  126. return
  127. fi
  128. # Test running with cputype produces output.
  129. if ! perf stat --cputype="$pmu" -e instructions true 2>&1 | grep -E -q "instructions"
  130. then
  131. echo "cputype test [Failed count missed with given filter]"
  132. err=1
  133. return
  134. fi
  135. echo "cputype test [Success]"
  136. }
  137. test_hybrid() {
  138. # Test the default stat command on hybrid devices opens one cycles event for
  139. # each CPU type.
  140. echo "hybrid test"
  141. # Count the number of core PMUs, assume minimum of 1
  142. pmus=$(ls /sys/bus/event_source/devices/*/cpus 2>/dev/null | wc -l)
  143. if [ "$pmus" -lt 1 ]
  144. then
  145. pmus=1
  146. fi
  147. # Run default Perf stat
  148. cycles_events=$(perf stat -- true 2>&1 | grep -E "/cycles/[uH]*| cycles[:uH]* " -c)
  149. # The expectation is that default output will have a cycles events on each
  150. # hybrid PMU. In situations with no cycles PMU events, like virtualized, this
  151. # can fall back to task-clock and so the end count may be 0. Fail if neither
  152. # condition holds.
  153. if [ "$pmus" -ne "$cycles_events" ] && [ "0" -ne "$cycles_events" ]
  154. then
  155. echo "hybrid test [Found $pmus PMUs but $cycles_events cycles events. Failed]"
  156. err=1
  157. return
  158. fi
  159. echo "hybrid test [Success]"
  160. }
  161. test_default_stat
  162. test_stat_record_report
  163. test_stat_record_script
  164. test_stat_repeat_weak_groups
  165. test_topdown_groups
  166. test_topdown_weak_groups
  167. test_cputype
  168. test_hybrid
  169. exit $err