stat_output.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Return true if perf_event_paranoid is > $1 and not running as root.
  4. function ParanoidAndNotRoot()
  5. {
  6. [ "$(id -u)" != 0 ] && [ "$(cat /proc/sys/kernel/perf_event_paranoid)" -gt $1 ]
  7. }
  8. # $1 name $2 extra_opt
  9. check_no_args()
  10. {
  11. echo -n "Checking $1 output: no args "
  12. perf stat $2 true
  13. commachecker --no-args
  14. echo "[Success]"
  15. }
  16. check_system_wide()
  17. {
  18. echo -n "Checking $1 output: system wide "
  19. if ParanoidAndNotRoot 0
  20. then
  21. echo "[Skip] paranoid and not root"
  22. return
  23. fi
  24. perf stat -a $2 true
  25. commachecker --system-wide
  26. echo "[Success]"
  27. }
  28. check_system_wide_no_aggr()
  29. {
  30. echo -n "Checking $1 output: system wide no aggregation "
  31. if ParanoidAndNotRoot 0
  32. then
  33. echo "[Skip] paranoid and not root"
  34. return
  35. fi
  36. perf stat -A -a --no-merge $2 true
  37. commachecker --system-wide-no-aggr
  38. echo "[Success]"
  39. }
  40. check_interval()
  41. {
  42. echo -n "Checking $1 output: interval "
  43. perf stat -I 1000 $2 true
  44. commachecker --interval
  45. echo "[Success]"
  46. }
  47. check_event()
  48. {
  49. echo -n "Checking $1 output: event "
  50. perf stat -e cpu-clock $2 true
  51. commachecker --event
  52. echo "[Success]"
  53. }
  54. check_per_core()
  55. {
  56. echo -n "Checking $1 output: per core "
  57. if ParanoidAndNotRoot 0
  58. then
  59. echo "[Skip] paranoid and not root"
  60. return
  61. fi
  62. perf stat --per-core -a $2 true
  63. commachecker --per-core
  64. echo "[Success]"
  65. }
  66. check_per_thread()
  67. {
  68. echo -n "Checking $1 output: per thread "
  69. if ParanoidAndNotRoot 0
  70. then
  71. echo "[Skip] paranoid and not root"
  72. return
  73. fi
  74. perf stat --per-thread -p $$ $2 true
  75. commachecker --per-thread
  76. echo "[Success]"
  77. }
  78. check_per_cache_instance()
  79. {
  80. echo -n "Checking $1 output: per cache instance "
  81. if ParanoidAndNotRoot 0
  82. then
  83. echo "[Skip] paranoid and not root"
  84. return
  85. fi
  86. perf stat --per-cache -a $2 true
  87. commachecker --per-cache
  88. echo "[Success]"
  89. }
  90. check_per_cluster()
  91. {
  92. echo -n "Checking $1 output: per cluster "
  93. if ParanoidAndNotRoot 0
  94. then
  95. echo "[Skip] paranoid and not root"
  96. return
  97. fi
  98. perf stat --per-cluster -a $2 true
  99. echo "[Success]"
  100. }
  101. check_per_die()
  102. {
  103. echo -n "Checking $1 output: per die "
  104. if ParanoidAndNotRoot 0
  105. then
  106. echo "[Skip] paranoid and not root"
  107. return
  108. fi
  109. perf stat --per-die -a $2 true
  110. commachecker --per-die
  111. echo "[Success]"
  112. }
  113. check_per_node()
  114. {
  115. echo -n "Checking $1 output: per node "
  116. if ParanoidAndNotRoot 0
  117. then
  118. echo "[Skip] paranoid and not root"
  119. return
  120. fi
  121. perf stat --per-node -a $2 true
  122. commachecker --per-node
  123. echo "[Success]"
  124. }
  125. check_per_socket()
  126. {
  127. echo -n "Checking $1 output: per socket "
  128. if ParanoidAndNotRoot 0
  129. then
  130. echo "[Skip] paranoid and not root"
  131. return
  132. fi
  133. perf stat --per-socket -a $2 true
  134. commachecker --per-socket
  135. echo "[Success]"
  136. }
  137. # The perf stat options for per-socket, per-core, per-die
  138. # and -A ( no_aggr mode ) uses the info fetched from this
  139. # directory: "/sys/devices/system/cpu/cpu*/topology". For
  140. # example, socket value is fetched from "physical_package_id"
  141. # file in topology directory.
  142. # Reference: cpu__get_topology_int in util/cpumap.c
  143. # If the platform doesn't expose topology information, values
  144. # will be set to -1. For example, incase of pSeries platform
  145. # of powerpc, value for "physical_package_id" is restricted
  146. # and set to -1. Check here validates the socket-id read from
  147. # topology file before proceeding further
  148. FILE_LOC="/sys/devices/system/cpu/cpu*/topology/"
  149. FILE_NAME="physical_package_id"
  150. function check_for_topology()
  151. {
  152. if ! ParanoidAndNotRoot 0
  153. then
  154. socket_file=`ls $FILE_LOC/$FILE_NAME | head -n 1`
  155. [ -z $socket_file ] && {
  156. echo 0
  157. return
  158. }
  159. socket_id=`cat $socket_file`
  160. [ $socket_id == -1 ] && {
  161. echo 1
  162. return
  163. }
  164. fi
  165. echo 0
  166. }