stat+json_output.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #!/bin/bash
  2. # perf stat JSON output linter
  3. # SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
  4. # Checks various perf stat JSON output commands for the
  5. # correct number of fields.
  6. set -e
  7. skip_test=0
  8. shelldir=$(dirname "$0")
  9. # shellcheck source=lib/setup_python.sh
  10. . "${shelldir}"/lib/setup_python.sh
  11. pythonchecker=$(dirname $0)/lib/perf_json_output_lint.py
  12. stat_output=$(mktemp /tmp/__perf_test.stat_output.json.XXXXX)
  13. cleanup() {
  14. rm -f "${stat_output}"
  15. trap - EXIT TERM INT
  16. }
  17. trap_cleanup() {
  18. cleanup
  19. exit 1
  20. }
  21. trap trap_cleanup EXIT TERM INT
  22. # Return true if perf_event_paranoid is > $1 and not running as root.
  23. function ParanoidAndNotRoot()
  24. {
  25. [ "$(id -u)" != 0 ] && [ "$(cat /proc/sys/kernel/perf_event_paranoid)" -gt $1 ]
  26. }
  27. check_no_args()
  28. {
  29. echo -n "Checking json output: no args "
  30. perf stat -j -o "${stat_output}" true
  31. $PYTHON $pythonchecker --no-args --file "${stat_output}"
  32. echo "[Success]"
  33. }
  34. check_system_wide()
  35. {
  36. echo -n "Checking json output: system wide "
  37. if ParanoidAndNotRoot 0
  38. then
  39. echo "[Skip] paranoia and not root"
  40. return
  41. fi
  42. perf stat -j -a -o "${stat_output}" true
  43. $PYTHON $pythonchecker --system-wide --file "${stat_output}"
  44. echo "[Success]"
  45. }
  46. check_system_wide_no_aggr()
  47. {
  48. echo -n "Checking json output: system wide no aggregation "
  49. if ParanoidAndNotRoot 0
  50. then
  51. echo "[Skip] paranoia and not root"
  52. return
  53. fi
  54. perf stat -j -A -a --no-merge -o "${stat_output}" true
  55. $PYTHON $pythonchecker --system-wide-no-aggr --file "${stat_output}"
  56. echo "[Success]"
  57. }
  58. check_interval()
  59. {
  60. echo -n "Checking json output: interval "
  61. perf stat -j -I 1000 -o "${stat_output}" true
  62. $PYTHON $pythonchecker --interval --file "${stat_output}"
  63. echo "[Success]"
  64. }
  65. check_event()
  66. {
  67. echo -n "Checking json output: event "
  68. perf stat -j -e cpu-clock -o "${stat_output}" true
  69. $PYTHON $pythonchecker --event --file "${stat_output}"
  70. echo "[Success]"
  71. }
  72. check_per_core()
  73. {
  74. echo -n "Checking json output: per core "
  75. if ParanoidAndNotRoot 0
  76. then
  77. echo "[Skip] paranoia and not root"
  78. return
  79. fi
  80. perf stat -j --per-core -a -o "${stat_output}" true
  81. $PYTHON $pythonchecker --per-core --file "${stat_output}"
  82. echo "[Success]"
  83. }
  84. check_per_thread()
  85. {
  86. echo -n "Checking json output: per thread "
  87. if ParanoidAndNotRoot 0
  88. then
  89. echo "[Skip] paranoia and not root"
  90. return
  91. fi
  92. perf stat -j --per-thread -p $$ -o "${stat_output}" true
  93. $PYTHON $pythonchecker --per-thread --file "${stat_output}"
  94. echo "[Success]"
  95. }
  96. check_per_cache_instance()
  97. {
  98. echo -n "Checking json output: per cache_instance "
  99. if ParanoidAndNotRoot 0
  100. then
  101. echo "[Skip] paranoia and not root"
  102. return
  103. fi
  104. perf stat -j --per-cache -a true 2>&1 | $PYTHON $pythonchecker --per-cache
  105. echo "[Success]"
  106. }
  107. check_per_cluster()
  108. {
  109. echo -n "Checking json output: per cluster "
  110. if ParanoidAndNotRoot 0
  111. then
  112. echo "[Skip] paranoia and not root"
  113. return
  114. fi
  115. perf stat -j --per-cluster -a true 2>&1 | $PYTHON $pythonchecker --per-cluster
  116. echo "[Success]"
  117. }
  118. check_per_die()
  119. {
  120. echo -n "Checking json output: per die "
  121. if ParanoidAndNotRoot 0
  122. then
  123. echo "[Skip] paranoia and not root"
  124. return
  125. fi
  126. perf stat -j --per-die -a -o "${stat_output}" true
  127. $PYTHON $pythonchecker --per-die --file "${stat_output}"
  128. echo "[Success]"
  129. }
  130. check_per_node()
  131. {
  132. echo -n "Checking json output: per node "
  133. if ParanoidAndNotRoot 0
  134. then
  135. echo "[Skip] paranoia and not root"
  136. return
  137. fi
  138. perf stat -j --per-node -a -o "${stat_output}" true
  139. $PYTHON $pythonchecker --per-node --file "${stat_output}"
  140. echo "[Success]"
  141. }
  142. check_per_socket()
  143. {
  144. echo -n "Checking json output: per socket "
  145. if ParanoidAndNotRoot 0
  146. then
  147. echo "[Skip] paranoia and not root"
  148. return
  149. fi
  150. perf stat -j --per-socket -a -o "${stat_output}" true
  151. $PYTHON $pythonchecker --per-socket --file "${stat_output}"
  152. echo "[Success]"
  153. }
  154. # The perf stat options for per-socket, per-core, per-die
  155. # and -A ( no_aggr mode ) uses the info fetched from this
  156. # directory: "/sys/devices/system/cpu/cpu*/topology". For
  157. # example, socket value is fetched from "physical_package_id"
  158. # file in topology directory.
  159. # Reference: cpu__get_topology_int in util/cpumap.c
  160. # If the platform doesn't expose topology information, values
  161. # will be set to -1. For example, incase of pSeries platform
  162. # of powerpc, value for "physical_package_id" is restricted
  163. # and set to -1. Check here validates the socket-id read from
  164. # topology file before proceeding further
  165. FILE_LOC="/sys/devices/system/cpu/cpu*/topology/"
  166. FILE_NAME="physical_package_id"
  167. check_for_topology()
  168. {
  169. if ! ParanoidAndNotRoot 0
  170. then
  171. socket_file=`ls $FILE_LOC/$FILE_NAME | head -n 1`
  172. [ -z $socket_file ] && return 0
  173. socket_id=`cat $socket_file`
  174. [ $socket_id == -1 ] && skip_test=1
  175. return 0
  176. fi
  177. }
  178. check_for_topology
  179. check_no_args
  180. check_system_wide
  181. check_interval
  182. check_event
  183. check_per_thread
  184. check_per_node
  185. if [ $skip_test -ne 1 ]
  186. then
  187. check_system_wide_no_aggr
  188. check_per_core
  189. check_per_cache_instance
  190. check_per_cluster
  191. check_per_die
  192. check_per_socket
  193. else
  194. echo "[Skip] Skipping tests for system_wide_no_aggr, per_core, per_die and per_socket since socket id exposed via topology is invalid"
  195. fi
  196. cleanup
  197. exit 0