perf-arm-spe.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. perf-arm-spe(1)
  2. ================
  3. NAME
  4. ----
  5. perf-arm-spe - Support for Arm Statistical Profiling Extension within Perf tools
  6. SYNOPSIS
  7. --------
  8. [verse]
  9. 'perf record' -e arm_spe//
  10. DESCRIPTION
  11. -----------
  12. The SPE (Statistical Profiling Extension) feature provides accurate attribution of latencies and
  13. events down to individual instructions. Rather than being interrupt-driven, it picks an
  14. instruction to sample and then captures data for it during execution. Data includes execution time
  15. in cycles. For loads and stores it also includes data address, cache miss events, and data origin.
  16. The sampling has 5 stages:
  17. 1. Choose an operation
  18. 2. Collect data about the operation
  19. 3. Optionally discard the record based on a filter
  20. 4. Write the record to memory
  21. 5. Interrupt when the buffer is full
  22. Choose an operation
  23. ~~~~~~~~~~~~~~~~~~~
  24. This is chosen from a sample population, for SPE this is an IMPLEMENTATION DEFINED choice of all
  25. architectural instructions or all micro-ops. Sampling happens at a programmable interval. The
  26. architecture provides a mechanism for the SPE driver to infer the minimum interval at which it should
  27. sample. This minimum interval is used by the driver if no interval is specified. A pseudo-random
  28. perturbation is also added to the sampling interval by default.
  29. Collect data about the operation
  30. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  31. Program counter, PMU events, timings and data addresses related to the operation are recorded.
  32. Sampling ensures there is only one sampled operation is in flight.
  33. Optionally discard the record based on a filter
  34. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. Based on programmable criteria, choose whether to keep the record or discard it. If the record is
  36. discarded then the flow stops here for this sample.
  37. Write the record to memory
  38. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  39. The record is appended to a memory buffer
  40. Interrupt when the buffer is full
  41. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  42. When the buffer fills, an interrupt is sent and the driver signals Perf to collect the records.
  43. Perf saves the raw data in the perf.data file.
  44. Opening the file
  45. ----------------
  46. Up until this point no decoding of the SPE data was done by either the kernel or Perf. Only when the
  47. recorded file is opened with 'perf report' or 'perf script' does the decoding happen. When decoding
  48. the data, Perf generates "synthetic samples" as if these were generated at the time of the
  49. recording. These samples are the same as if normal sampling was done by Perf without using SPE,
  50. although they may have more attributes associated with them. For example a normal sample may have
  51. just the instruction pointer, but an SPE sample can have data addresses and latency attributes.
  52. Why Sampling?
  53. -------------
  54. - Sampling, rather than tracing, cuts down the profiling problem to something more manageable for
  55. hardware. Only one sampled operation is in flight at a time.
  56. - Allows precise attribution data, including: Full PC of instruction, data virtual and physical
  57. addresses.
  58. - Allows correlation between an instruction and events, such as TLB and cache miss. (Data source
  59. indicates which particular cache was hit, but the meaning is implementation defined because
  60. different implementations can have different cache configurations.)
  61. However, SPE does not provide any call-graph information, and relies on statistical methods.
  62. Collisions
  63. ----------
  64. When an operation is sampled while a previous sampled operation has not finished, a collision
  65. occurs. The new sample is dropped. Collisions affect the integrity of the data, so the sample rate
  66. should be set to avoid collisions.
  67. The 'sample_collision' PMU event can be used to determine the number of lost samples. Although this
  68. count is based on collisions _before_ filtering occurs. Therefore this can not be used as an exact
  69. number for samples dropped that would have made it through the filter, but can be a rough
  70. guide.
  71. The effect of microarchitectural sampling
  72. -----------------------------------------
  73. If an implementation samples micro-operations instead of instructions, the results of sampling must
  74. be weighted accordingly.
  75. For example, if a given instruction A is always converted into two micro-operations, A0 and A1, it
  76. becomes twice as likely to appear in the sample population.
  77. The coarse effect of conversions, and, if applicable, sampling of speculative operations, can be
  78. estimated from the 'sample_pop' and 'inst_retired' PMU events.
  79. Kernel Requirements
  80. -------------------
  81. The ARM_SPE_PMU config must be set to build as either a module or statically.
  82. Depending on CPU model, the kernel may need to be booted with page table isolation disabled
  83. (kpti=off). If KPTI needs to be disabled, this will fail with a console message "profiling buffer
  84. inaccessible. Try passing 'kpti=off' on the kernel command line".
  85. For the full criteria that determine whether KPTI needs to be forced off or not, see function
  86. unmap_kernel_at_el0() in the kernel sources. Common cases where it's not required
  87. are on the CPUs in kpti_safe_list, or on Arm v8.5+ where FEAT_E0PD is mandatory.
  88. The SPE interrupt must also be described by the firmware. If the module is loaded and KPTI is
  89. disabled (or isn't required to be disabled) but the SPE PMU still doesn't show in
  90. /sys/bus/event_source/devices/, then it's possible that the SPE interrupt isn't described by
  91. ACPI or DT. In this case no warning will be printed by the driver.
  92. Capturing SPE with perf command-line tools
  93. ------------------------------------------
  94. You can record a session with SPE samples:
  95. perf record -e arm_spe// -- ./mybench
  96. The sample period is set from the -c option, and because the minimum interval is used by default
  97. it's recommended to set this to a higher value. The value is written to PMSIRR.INTERVAL.
  98. Config parameters
  99. ~~~~~~~~~~~~~~~~~
  100. These are placed between the // in the event and comma separated. For example '-e
  101. arm_spe/load_filter=1,min_latency=10/'
  102. event_filter=<mask> - logical AND filter on specific events (PMSEVFR) - see bitfield description below
  103. inv_event_filter=<mask> - logical OR to filter out specific events (PMSNEVFR, FEAT_SPEv1p2) - see bitfield description below
  104. jitter=1 - use jitter to avoid resonance when sampling (PMSIRR.RND)
  105. min_latency=<n> - collect only samples with this latency or higher* (PMSLATFR)
  106. pa_enable=1 - collect physical address (as well as VA) of loads/stores (PMSCR.PA) - requires privilege
  107. pct_enable=1 - collect physical timestamp instead of virtual timestamp (PMSCR.PCT) - requires privilege
  108. ts_enable=1 - enable timestamping with value of generic timer (PMSCR.TS)
  109. discard=1 - enable SPE PMU events but don't collect sample data - see 'Discard mode' (PMBLIMITR.FM = DISCARD)
  110. inv_data_src_filter=<mask> - mask to filter from 0-63 possible data sources (PMSDSFR, FEAT_SPE_FDS) - See 'Data source filtering'
  111. +++*+++ Latency is the total latency from the point at which sampling started on that instruction, rather
  112. than only the execution latency.
  113. Only some events can be filtered on using 'event_filter' bits. The overall
  114. filter is the logical AND of these bits, for example if bits 3 and 5 are set
  115. only samples that have both 'L1D cache refill' AND 'TLB walk' are recorded. When
  116. FEAT_SPEv1p2 is implemented 'inv_event_filter' can also be used to exclude
  117. events that have any (OR) of the filter's bits set. For example setting bits 3
  118. and 5 in 'inv_event_filter' will exclude any events that are either L1D cache
  119. refill OR TLB walk. If the same bit is set in both filters it's UNPREDICTABLE
  120. whether the sample is included or excluded. Filter bits for both event_filter
  121. and inv_event_filter are:
  122. bit 1 - Instruction retired (i.e. omit speculative instructions)
  123. bit 2 - L1D access (FEAT_SPEv1p4)
  124. bit 3 - L1D refill
  125. bit 4 - TLB access (FEAT_SPEv1p4)
  126. bit 5 - TLB refill
  127. bit 6 - Not taken event (FEAT_SPEv1p2)
  128. bit 7 - Mispredict
  129. bit 8 - Last level cache access (FEAT_SPEv1p4)
  130. bit 9 - Last level cache miss (FEAT_SPEv1p4)
  131. bit 10 - Remote access (FEAT_SPEv1p4)
  132. bit 11 - Misaligned access (FEAT_SPEv1p1)
  133. bit 12-15 - IMPLEMENTATION DEFINED events (when implemented)
  134. bit 17 - Partial or empty SME or SVE predicate (FEAT_SPEv1p1)
  135. bit 18 - Empty SME or SVE predicate (FEAT_SPEv1p1)
  136. bit 19 - L2D access (FEAT_SPEv1p4)
  137. bit 20 - L2D miss (FEAT_SPEv1p4)
  138. bit 21 - Cache data modified (FEAT_SPEv1p4)
  139. bit 22 - Recently fetched (FEAT_SPEv1p4)
  140. bit 23 - Data snooped (FEAT_SPEv1p4)
  141. bit 24 - Streaming SVE mode event (when FEAT_SPE_SME is implemented), or
  142. IMPLEMENTATION DEFINED event 24 (when implemented, only versions
  143. less than FEAT_SPEv1p4)
  144. bit 25 - SMCU or external coprocessor operation event when FEAT_SPE_SME is
  145. implemented, or IMPLEMENTATION DEFINED event 25 (when implemented,
  146. only versions less than FEAT_SPEv1p4)
  147. bit 26-31 - IMPLEMENTATION DEFINED events (only versions less than FEAT_SPEv1p4)
  148. bit 48-63 - IMPLEMENTATION DEFINED events (when implemented)
  149. For IMPLEMENTATION DEFINED bits, refer to the CPU TRM if these bits are
  150. implemented.
  151. The driver will reject events if requested filter bits require unimplemented SPE
  152. versions, but will not reject filter bits for unimplemented IMPDEF bits or when
  153. their related feature is not present (e.g. SME). For example, if FEAT_SPEv1p2 is
  154. not implemented, filtering on "Not taken event" (bit 6) will be rejected.
  155. So to sample just retired instructions:
  156. perf record -e arm_spe/event_filter=2/ -- ./mybench
  157. or just mispredicted branches:
  158. perf record -e arm_spe/event_filter=0x80/ -- ./mybench
  159. When set, the following filters can be used to select samples that match any of
  160. the operation types (OR filtering). If only one is set then only samples of that
  161. type are collected:
  162. branch_filter=1 - Collect branches (PMSFCR.B)
  163. load_filter=1 - Collect loads (PMSFCR.LD)
  164. store_filter=1 - Collect stores (PMSFCR.ST)
  165. When extended filtering is supported (FEAT_SPE_EFT), SIMD and float
  166. pointer operations can also be selected:
  167. simd_filter=1 - Collect SIMD loads, stores and operations (PMSFCR.SIMD)
  168. float_filter=1 - Collect floating point loads, stores and operations (PMSFCR.FP)
  169. When extended filtering is supported (FEAT_SPE_EFT), operation type filters can
  170. be changed to AND using _mask fields. For example samples could be selected if
  171. they are store AND SIMD by setting 'store_filter=1,simd_filter=1,
  172. store_filter_mask=1,simd_filter_mask=1'. The new masks are as follows:
  173. branch_filter_mask=1 - Change branch filter behavior from OR to AND (PMSFCR.Bm)
  174. load_filter_mask=1 - Change load filter behavior from OR to AND (PMSFCR.LDm)
  175. store_filter_mask=1 - Change store filter behavior from OR to AND (PMSFCR.STm)
  176. simd_filter_mask=1 - Change SIMD filter behavior from OR to AND (PMSFCR.SIMDm)
  177. float_filter_mask=1 - Change floating point filter behavior from OR to AND (PMSFCR.FPm)
  178. Viewing the data
  179. ~~~~~~~~~~~~~~~~~
  180. By default perf report and perf script will assign samples to separate groups depending on the
  181. attributes/events of the SPE record. Because instructions can have multiple events associated with
  182. them, the samples in these groups are not necessarily unique. For example perf report shows these
  183. groups:
  184. Available samples
  185. 0 arm_spe//
  186. 0 dummy:u
  187. 21 l1d-miss
  188. 897 l1d-access
  189. 5 llc-miss
  190. 7 llc-access
  191. 2 tlb-miss
  192. 1K tlb-access
  193. 36 branch
  194. 0 remote-access
  195. 900 memory
  196. 1800 instructions
  197. The arm_spe// and dummy:u events are implementation details and are expected to be empty.
  198. The instructions group contains the full list of unique samples that are not
  199. sorted into other groups. To generate only this group use --itrace=i1i.
  200. 1i (1 instruction interval) signifies no further downsampling. Rather than an
  201. instruction interval, this generates a sample every n SPE samples. For example
  202. to generate the default set of events for every 100 SPE samples:
  203. perf report --itrace==bxofmtMai100i
  204. Other period types, for example nanoseconds (ns) are not currently supported.
  205. Memory access details are also stored on the samples and this can be viewed with:
  206. perf report --mem-mode
  207. The latency value from the SPE sample is stored in the 'weight' field of the
  208. Perf samples and can be displayed in Perf script and report outputs by enabling
  209. its display from the command line.
  210. Common errors
  211. ~~~~~~~~~~~~~
  212. - "Cannot find PMU `arm_spe'. Missing kernel support?"
  213. Module not built or loaded, KPTI not disabled, interrupt not described by firmware,
  214. or running on a VM. See 'Kernel Requirements' above.
  215. - "Arm SPE CONTEXT packets not found in the traces."
  216. Root privilege is required to collect context packets. But these only increase the accuracy of
  217. assigning PIDs to kernel samples. For userspace sampling this can be ignored.
  218. - Excessively large perf.data file size
  219. Increase sampling interval (see above)
  220. PMU events
  221. ~~~~~~~~~~
  222. SPE has events that can be counted on core PMUs. These are prefixed with
  223. SAMPLE_, for example SAMPLE_POP, SAMPLE_FEED, SAMPLE_COLLISION and
  224. SAMPLE_FEED_BR.
  225. These events will only count when an SPE event is running on the same core that
  226. the PMU event is opened on, otherwise they read as 0. There are various ways to
  227. ensure that the PMU event and SPE event are scheduled together depending on the
  228. way the event is opened. For example opening both events as per-process events
  229. on the same process, although it's not guaranteed that the PMU event is enabled
  230. first when context switching. For that reason it may be better to open the PMU
  231. event as a systemwide event and then open SPE on the process of interest.
  232. Discard mode
  233. ~~~~~~~~~~~~
  234. SPE related (SAMPLE_* etc) core PMU events can be used without the overhead of
  235. collecting sample data if discard mode is supported (optional from Armv8.6).
  236. First run a system wide SPE session (or on the core of interest) using options
  237. to minimize output. Then run perf stat:
  238. perf record -e arm_spe/discard/ -a -N -B --no-bpf-event -o - > /dev/null &
  239. perf stat -e SAMPLE_FEED_LD
  240. Data source filtering
  241. ~~~~~~~~~~~~~~~~~~~~~
  242. When FEAT_SPE_FDS is present, 'inv_data_src_filter' can be used as a mask to
  243. filter on a subset (0 - 63) of possible data source IDs. The full range of data
  244. sources is 0 - 65535 although these are unlikely to be used in practice. Data
  245. sources are IMPDEF so refer to the TRM for the mappings. Each bit N of the
  246. filter maps to data source N. The filter is an OR of all the bits, and the value
  247. provided inv_data_src_filter is inverted before writing to PMSDSFR_EL1 so that
  248. set bits exclude that data source and cleared bits include that data source.
  249. Therefore the default value of 0 is equivalent to no filtering (all data sources
  250. included).
  251. For example, to include only data sources 0 and 3, clear bits 0 and 3
  252. (0xFFFFFFFFFFFFFFF6)
  253. When 'inv_data_src_filter' is set to 0xFFFFFFFFFFFFFFFF, any samples with any
  254. data source set are excluded.
  255. SEE ALSO
  256. --------
  257. linkperf:perf-record[1], linkperf:perf-script[1], linkperf:perf-report[1],
  258. linkperf:perf-inject[1]