1*e4624435SJonathan Corbet.. SPDX-License-Identifier: GPL-2.0 2*e4624435SJonathan Corbet 3*e4624435SJonathan Corbet.. _perf_index: 4*e4624435SJonathan Corbet 5*e4624435SJonathan Corbet==== 6*e4624435SJonathan CorbetPerf 7*e4624435SJonathan Corbet==== 8*e4624435SJonathan Corbet 9*e4624435SJonathan CorbetPerf Event Attributes 10*e4624435SJonathan Corbet===================== 11*e4624435SJonathan Corbet 12*e4624435SJonathan Corbet:Author: Andrew Murray <andrew.murray@arm.com> 13*e4624435SJonathan Corbet:Date: 2019-03-06 14*e4624435SJonathan Corbet 15*e4624435SJonathan Corbetexclude_user 16*e4624435SJonathan Corbet------------ 17*e4624435SJonathan Corbet 18*e4624435SJonathan CorbetThis attribute excludes userspace. 19*e4624435SJonathan Corbet 20*e4624435SJonathan CorbetUserspace always runs at EL0 and thus this attribute will exclude EL0. 21*e4624435SJonathan Corbet 22*e4624435SJonathan Corbet 23*e4624435SJonathan Corbetexclude_kernel 24*e4624435SJonathan Corbet-------------- 25*e4624435SJonathan Corbet 26*e4624435SJonathan CorbetThis attribute excludes the kernel. 27*e4624435SJonathan Corbet 28*e4624435SJonathan CorbetThe kernel runs at EL2 with VHE and EL1 without. Guest kernels always run 29*e4624435SJonathan Corbetat EL1. 30*e4624435SJonathan Corbet 31*e4624435SJonathan CorbetFor the host this attribute will exclude EL1 and additionally EL2 on a VHE 32*e4624435SJonathan Corbetsystem. 33*e4624435SJonathan Corbet 34*e4624435SJonathan CorbetFor the guest this attribute will exclude EL1. Please note that EL2 is 35*e4624435SJonathan Corbetnever counted within a guest. 36*e4624435SJonathan Corbet 37*e4624435SJonathan Corbet 38*e4624435SJonathan Corbetexclude_hv 39*e4624435SJonathan Corbet---------- 40*e4624435SJonathan Corbet 41*e4624435SJonathan CorbetThis attribute excludes the hypervisor. 42*e4624435SJonathan Corbet 43*e4624435SJonathan CorbetFor a VHE host this attribute is ignored as we consider the host kernel to 44*e4624435SJonathan Corbetbe the hypervisor. 45*e4624435SJonathan Corbet 46*e4624435SJonathan CorbetFor a non-VHE host this attribute will exclude EL2 as we consider the 47*e4624435SJonathan Corbethypervisor to be any code that runs at EL2 which is predominantly used for 48*e4624435SJonathan Corbetguest/host transitions. 49*e4624435SJonathan Corbet 50*e4624435SJonathan CorbetFor the guest this attribute has no effect. Please note that EL2 is 51*e4624435SJonathan Corbetnever counted within a guest. 52*e4624435SJonathan Corbet 53*e4624435SJonathan Corbet 54*e4624435SJonathan Corbetexclude_host / exclude_guest 55*e4624435SJonathan Corbet---------------------------- 56*e4624435SJonathan Corbet 57*e4624435SJonathan CorbetThese attributes exclude the KVM host and guest, respectively. 58*e4624435SJonathan Corbet 59*e4624435SJonathan CorbetThe KVM host may run at EL0 (userspace), EL1 (non-VHE kernel) and EL2 (VHE 60*e4624435SJonathan Corbetkernel or non-VHE hypervisor). 61*e4624435SJonathan Corbet 62*e4624435SJonathan CorbetThe KVM guest may run at EL0 (userspace) and EL1 (kernel). 63*e4624435SJonathan Corbet 64*e4624435SJonathan CorbetDue to the overlapping exception levels between host and guests we cannot 65*e4624435SJonathan Corbetexclusively rely on the PMU's hardware exception filtering - therefore we 66*e4624435SJonathan Corbetmust enable/disable counting on the entry and exit to the guest. This is 67*e4624435SJonathan Corbetperformed differently on VHE and non-VHE systems. 68*e4624435SJonathan Corbet 69*e4624435SJonathan CorbetFor non-VHE systems we exclude EL2 for exclude_host - upon entering and 70*e4624435SJonathan Corbetexiting the guest we disable/enable the event as appropriate based on the 71*e4624435SJonathan Corbetexclude_host and exclude_guest attributes. 72*e4624435SJonathan Corbet 73*e4624435SJonathan CorbetFor VHE systems we exclude EL1 for exclude_guest and exclude both EL0,EL2 74*e4624435SJonathan Corbetfor exclude_host. Upon entering and exiting the guest we modify the event 75*e4624435SJonathan Corbetto include/exclude EL0 as appropriate based on the exclude_host and 76*e4624435SJonathan Corbetexclude_guest attributes. 77*e4624435SJonathan Corbet 78*e4624435SJonathan CorbetThe statements above also apply when these attributes are used within a 79*e4624435SJonathan Corbetnon-VHE guest however please note that EL2 is never counted within a guest. 80*e4624435SJonathan Corbet 81*e4624435SJonathan Corbet 82*e4624435SJonathan CorbetAccuracy 83*e4624435SJonathan Corbet-------- 84*e4624435SJonathan Corbet 85*e4624435SJonathan CorbetOn non-VHE hosts we enable/disable counters on the entry/exit of host/guest 86*e4624435SJonathan Corbettransition at EL2 - however there is a period of time between 87*e4624435SJonathan Corbetenabling/disabling the counters and entering/exiting the guest. We are 88*e4624435SJonathan Corbetable to eliminate counters counting host events on the boundaries of guest 89*e4624435SJonathan Corbetentry/exit when counting guest events by filtering out EL2 for 90*e4624435SJonathan Corbetexclude_host. However when using !exclude_hv there is a small blackout 91*e4624435SJonathan Corbetwindow at the guest entry/exit where host events are not captured. 92*e4624435SJonathan Corbet 93*e4624435SJonathan CorbetOn VHE systems there are no blackout windows. 94*e4624435SJonathan Corbet 95*e4624435SJonathan CorbetPerf Userspace PMU Hardware Counter Access 96*e4624435SJonathan Corbet========================================== 97*e4624435SJonathan Corbet 98*e4624435SJonathan CorbetOverview 99*e4624435SJonathan Corbet-------- 100*e4624435SJonathan CorbetThe perf userspace tool relies on the PMU to monitor events. It offers an 101*e4624435SJonathan Corbetabstraction layer over the hardware counters since the underlying 102*e4624435SJonathan Corbetimplementation is cpu-dependent. 103*e4624435SJonathan CorbetArm64 allows userspace tools to have access to the registers storing the 104*e4624435SJonathan Corbethardware counters' values directly. 105*e4624435SJonathan Corbet 106*e4624435SJonathan CorbetThis targets specifically self-monitoring tasks in order to reduce the overhead 107*e4624435SJonathan Corbetby directly accessing the registers without having to go through the kernel. 108*e4624435SJonathan Corbet 109*e4624435SJonathan CorbetHow-to 110*e4624435SJonathan Corbet------ 111*e4624435SJonathan CorbetThe focus is set on the armv8 PMUv3 which makes sure that the access to the pmu 112*e4624435SJonathan Corbetregisters is enabled and that the userspace has access to the relevant 113*e4624435SJonathan Corbetinformation in order to use them. 114*e4624435SJonathan Corbet 115*e4624435SJonathan CorbetIn order to have access to the hardware counters, the global sysctl 116*e4624435SJonathan Corbetkernel/perf_user_access must first be enabled: 117*e4624435SJonathan Corbet 118*e4624435SJonathan Corbet.. code-block:: sh 119*e4624435SJonathan Corbet 120*e4624435SJonathan Corbet echo 1 > /proc/sys/kernel/perf_user_access 121*e4624435SJonathan Corbet 122*e4624435SJonathan CorbetIt is necessary to open the event using the perf tool interface with config1:1 123*e4624435SJonathan Corbetattr bit set: the sys_perf_event_open syscall returns a fd which can 124*e4624435SJonathan Corbetsubsequently be used with the mmap syscall in order to retrieve a page of memory 125*e4624435SJonathan Corbetcontaining information about the event. The PMU driver uses this page to expose 126*e4624435SJonathan Corbetto the user the hardware counter's index and other necessary data. Using this 127*e4624435SJonathan Corbetindex enables the user to access the PMU registers using the `mrs` instruction. 128*e4624435SJonathan CorbetAccess to the PMU registers is only valid while the sequence lock is unchanged. 129*e4624435SJonathan CorbetIn particular, the PMSELR_EL0 register is zeroed each time the sequence lock is 130*e4624435SJonathan Corbetchanged. 131*e4624435SJonathan Corbet 132*e4624435SJonathan CorbetThe userspace access is supported in libperf using the perf_evsel__mmap() 133*e4624435SJonathan Corbetand perf_evsel__read() functions. See `tools/lib/perf/tests/test-evsel.c`_ for 134*e4624435SJonathan Corbetan example. 135*e4624435SJonathan Corbet 136*e4624435SJonathan CorbetAbout heterogeneous systems 137*e4624435SJonathan Corbet--------------------------- 138*e4624435SJonathan CorbetOn heterogeneous systems such as big.LITTLE, userspace PMU counter access can 139*e4624435SJonathan Corbetonly be enabled when the tasks are pinned to a homogeneous subset of cores and 140*e4624435SJonathan Corbetthe corresponding PMU instance is opened by specifying the 'type' attribute. 141*e4624435SJonathan CorbetThe use of generic event types is not supported in this case. 142*e4624435SJonathan Corbet 143*e4624435SJonathan CorbetHave a look at `tools/perf/arch/arm64/tests/user-events.c`_ for an example. It 144*e4624435SJonathan Corbetcan be run using the perf tool to check that the access to the registers works 145*e4624435SJonathan Corbetcorrectly from userspace: 146*e4624435SJonathan Corbet 147*e4624435SJonathan Corbet.. code-block:: sh 148*e4624435SJonathan Corbet 149*e4624435SJonathan Corbet perf test -v user 150*e4624435SJonathan Corbet 151*e4624435SJonathan CorbetAbout chained events and counter sizes 152*e4624435SJonathan Corbet-------------------------------------- 153*e4624435SJonathan CorbetThe user can request either a 32-bit (config1:0 == 0) or 64-bit (config1:0 == 1) 154*e4624435SJonathan Corbetcounter along with userspace access. The sys_perf_event_open syscall will fail 155*e4624435SJonathan Corbetif a 64-bit counter is requested and the hardware doesn't support 64-bit 156*e4624435SJonathan Corbetcounters. Chained events are not supported in conjunction with userspace counter 157*e4624435SJonathan Corbetaccess. If a 32-bit counter is requested on hardware with 64-bit counters, then 158*e4624435SJonathan Corbetuserspace must treat the upper 32-bits read from the counter as UNKNOWN. The 159*e4624435SJonathan Corbet'pmc_width' field in the user page will indicate the valid width of the counter 160*e4624435SJonathan Corbetand should be used to mask the upper bits as needed. 161*e4624435SJonathan Corbet 162*e4624435SJonathan Corbet.. Links 163*e4624435SJonathan Corbet.. _tools/perf/arch/arm64/tests/user-events.c: 164*e4624435SJonathan Corbet https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/perf/arch/arm64/tests/user-events.c 165*e4624435SJonathan Corbet.. _tools/lib/perf/tests/test-evsel.c: 166*e4624435SJonathan Corbet https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/tools/lib/perf/tests/test-evsel.c 167