Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf docs: Add info on AMD raw event encoding

AMD processors have events with event select codes and unit masks larger
than a byte. The core PMU, for example, uses 12-bit event select codes
split between bits 0-7 and 32-35 of the PERF_CTL MSRs as can be seen
from /sys/bus/event_sources/devices/cpu/format/*.

The Processor Programming Reference (PPR) lists the event codes as
unified 12-bit hexadecimal values instead and the split between the bits
is not apparent to someone who is not aware of the layout of the
PERF_CTL MSRs.

8-bit event select codes continue to work as the layout matches that of
the PERF_CTL MSRs i.e. bits 0-7 for event select and 8-15 for unit mask.

This adds more details in the perf man pages about using
/sys/bus/event_sources/devices/*/format/* for determining the correct
raw event encoding scheme.

E.g. the "op_cache_hit_miss.op_cache_hit" event with code 0x28f and
umask 0x03 can be programmed using its symbolic name as:

$ sudo perf --debug perf-event-open stat -e op_cache_hit_miss.op_cache_hit sleep 1
------------------------------------------------------------
perf_event_attr:
type 4
size 128
config 0x20000038f
sample_type IDENTIFIER
read_format TOTAL_TIME_ENABLED|TOTAL_TIME_RUNNING
disabled 1
inherit 1
enable_on_exec 1
exclude_guest 1
------------------------------------------------------------
[...]

One might use a simple eventsel+umask combination based on what the
current man pages say and incorrectly program the event as:

$ sudo perf --debug perf-event-open stat -e r0328f sleep 1
------------------------------------------------------------
perf_event_attr:
type 4
size 128
config 0x328f
sample_type IDENTIFIER
read_format TOTAL_TIME_ENABLED|TOTAL_TIME_RUNNING
disabled 1
inherit 1
enable_on_exec 1
exclude_guest 1
------------------------------------------------------------
[...]

When it should have been based on the format from sysfs:

$ cat /sys/bus/event_source/devices/cpu/format/event
config:0-7,32-35

$ sudo perf --debug perf-event-open stat -e r20000038f sleep 1
------------------------------------------------------------
perf_event_attr:
type 4
size 128
config 0x20000038f
sample_type IDENTIFIER
read_format TOTAL_TIME_ENABLED|TOTAL_TIME_RUNNING
disabled 1
inherit 1
enable_on_exec 1
exclude_guest 1
------------------------------------------------------------
[...]

Reviewed-by: Kajol Jain <kjain@linux.ibm.com>
Signed-off-by: Sandipan Das <sandipan.das@amd.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: Ananth Narayan <ananth.narayan@amd.com>
Cc: Kim Phillips <kim.phillips@amd.com>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Robert Richter <rrichter@amd.com>
Cc: Santosh Shukla <santosh.shukla@amd.com>
Link: https://lore.kernel.org/r/20211123084613.243792-1-sandipan.das@amd.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Sandipan Das and committed by
Arnaldo Carvalho de Melo
4edb117e a7f3713f

+45 -8
+33 -1
tools/perf/Documentation/perf-list.txt
··· 94 94 Even when an event is not available in a symbolic form within perf right now, 95 95 it can be encoded in a per processor specific way. 96 96 97 - For instance For x86 CPUs NNN represents the raw register encoding with the 97 + For instance on x86 CPUs, N is a hexadecimal value that represents the raw register encoding with the 98 98 layout of IA32_PERFEVTSELx MSRs (see [Intel® 64 and IA-32 Architectures Software Developer's Manual Volume 3B: System Programming Guide] Figure 30-1 Layout 99 99 of IA32_PERFEVTSELx MSRs) or AMD's PerfEvtSeln (see [AMD64 Architecture Programmer’s Manual Volume 2: System Programming], Page 344, 100 100 Figure 13-7 Performance Event-Select Register (PerfEvtSeln)). ··· 125 125 perf record -e r1a8 -a sleep 1 126 126 perf record -e cpu/r1a8/ ... 127 127 perf record -e cpu/r0x1a8/ ... 128 + 129 + Some processors, like those from AMD, support event codes and unit masks 130 + larger than a byte. In such cases, the bits corresponding to the event 131 + configuration parameters can be seen with: 132 + 133 + cat /sys/bus/event_source/devices/<pmu>/format/<config> 134 + 135 + Example: 136 + 137 + If the AMD docs for an EPYC 7713 processor describe an event as: 138 + 139 + Event Umask Event Mask 140 + Num. Value Mnemonic Description 141 + 142 + 28FH 03H op_cache_hit_miss.op_cache_hit Counts Op Cache micro-tag 143 + hit events. 144 + 145 + raw encoding of 0x0328F cannot be used since the upper nibble of the 146 + EventSelect bits have to be specified via bits 32-35 as can be seen with: 147 + 148 + cat /sys/bus/event_source/devices/cpu/format/event 149 + 150 + raw encoding of 0x20000038F should be used instead: 151 + 152 + perf stat -e r20000038f -a sleep 1 153 + perf record -e r20000038f ... 154 + 155 + It's also possible to use pmu syntax: 156 + 157 + perf record -e r20000038f -a sleep 1 158 + perf record -e cpu/r20000038f/ ... 159 + perf record -e cpu/r0x20000038f/ ... 128 160 129 161 You should refer to the processor specific documentation for getting these 130 162 details. Some of them are referenced in the SEE ALSO section below.
+4 -2
tools/perf/Documentation/perf-record.txt
··· 30 30 31 31 - a symbolic event name (use 'perf list' to list all events) 32 32 33 - - a raw PMU event (eventsel+umask) in the form of rNNN where NNN is a 34 - hexadecimal event descriptor. 33 + - a raw PMU event in the form of rN where N is a hexadecimal value 34 + that represents the raw register encoding with the layout of the 35 + event control registers as described by entries in 36 + /sys/bus/event_sources/devices/cpu/format/*. 35 37 36 38 - a symbolic or raw PMU event followed by an optional colon 37 39 and a list of event modifiers, e.g., cpu-cycles:p. See the
+4 -2
tools/perf/Documentation/perf-stat.txt
··· 36 36 37 37 - a symbolic event name (use 'perf list' to list all events) 38 38 39 - - a raw PMU event (eventsel+umask) in the form of rNNN where NNN is a 40 - hexadecimal event descriptor. 39 + - a raw PMU event in the form of rN where N is a hexadecimal value 40 + that represents the raw register encoding with the layout of the 41 + event control registers as described by entries in 42 + /sys/bus/event_sources/devices/cpu/format/*. 41 43 42 44 - a symbolic or raw PMU event followed by an optional colon 43 45 and a list of event modifiers, e.g., cpu-cycles:p. See the
+4 -3
tools/perf/Documentation/perf-top.txt
··· 38 38 -e <event>:: 39 39 --event=<event>:: 40 40 Select the PMU event. Selection can be a symbolic event name 41 - (use 'perf list' to list all events) or a raw PMU 42 - event (eventsel+umask) in the form of rNNN where NNN is a 43 - hexadecimal event descriptor. 41 + (use 'perf list' to list all events) or a raw PMU event in the form 42 + of rN where N is a hexadecimal value that represents the raw register 43 + encoding with the layout of the event control registers as described 44 + by entries in /sys/bus/event_sources/devices/cpu/format/*. 44 45 45 46 -E <entries>:: 46 47 --entries=<entries>::