Skip to contentSkip to conversation

perf_events: support for Intel bus-cycles/unhalted_ref_cycles events

PATCH New Stephane Eranian 1 participant 1 message Feb 8, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf_events: support for Intel bus-cycles/unhalted_ref_cycles events

On Intel Core, Nehalem, Atom, there are multiple events to count elapsed cycles:

- unhalted_core_cycles: elapsed cycles, correlation to time not maintained with time when frequency scaling operates

- unhalted_reference_cycles: elasped cycles, correlation to time constant, regardless of frequency scaling.

- cpu_clk_unhalted:bus: counts bus cycles. Ratio with unhalted_reference_cycles constant.

Perf_events relies on event codes to identify events and map them onto the correct counter. There is an issue with fixed counter only events because they have no specific event codes. Instead, the code relies on them being measurable also on generic counters, therefore having an event code. The event scheduling first tries to use the generic counter, then the fixed counter.

The problem is that for bus-cycles (code 0x013c), measuring the event in a generic counter or in fixed counter 2 does not measure the same thing. In a generic counter, 0x13c measures bus cycles, but when fixed counter is used it measurs unhalted_reference_cycles. Thus, there is an issue to name the event to measure in fixed counter 2, yet it is quite useful.

This patch enables the distinction of bus-cycles and ref-cycles leveraging the generic PMU events and in particular PERF_COUNT_HW_BUS_CYCLES. The event is encoded specially by the kernel such that, though it uses 0x013c, the scheduling code will force it onto the generic counters, thereby measuring actual bus cycles. Then, the regular 0x13c code is constrained to the fixed counter 2 only, thereby measuring ref-cycles. The special is also alvailable from user mode. It leverages unused Intel config bits (bit 32).

Note that an alternative approach would be to introduce a new generic PMU event to distinguish between CORE_CYCLES and REF_CYCLES.

The results with perf is:

$ perf stat -e cycles,bus-cycles,r10000013c noploop 10 noploop for 10 seconds

Performance counter stats for 'noploop 10':

    	23695472504 cycles                  
     	2632830132  bus-cycles              
     	2632830132  raw 0x10000013c         

10.000692965 seconds time elapsed

	With a libpfm4 tool which understand symbol PMU-specific events:

	$ task -g -e unhalted_reference_cycles,unhalted_core_cycles,\
		     instructions_retired,perf_count_hw_bus_cycles noploop 10
	 noploop for 10 seconds
         23565758184 unhalted_reference_cycles
         23565787569 unhalted_core_cycles
         23560689959 instructions_retired
          2618418665 perf_count_hw_bus_cycles

	Signed-off-by: Stephane Eranian <[email protected]>
--
 include/asm/perf_event.h |    5 --
 kernel/cpu/perf_event.c  |   91 +++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 82 insertions(+), 14 deletions(-)
arch/x86/include/asm/perf_event.h +1 -4
index befd172..3df219d 100644
@@ -50,7 +50,7 @@
 	 INTEL_ARCH_INV_MASK| \
 	 INTEL_ARCH_EDGE_MASK|\
 	 INTEL_ARCH_UNIT_MASK|\
-	 INTEL_ARCH_EVTSEL_MASK)
+	 INTEL_ARCH_EVENT_MASK)
 
 #define ARCH_PERFMON_UNHALTED_CORE_CYCLES_SEL		      0x3c
 #define ARCH_PERFMON_UNHALTED_CORE_CYCLES_UMASK		(0x00 << 8)
@@ -98,15 +98,12 @@ union cpuid10_edx {
 
 /* Instr_Retired.Any: */
 #define MSR_ARCH_PERFMON_FIXED_CTR0			0x309
-#define X86_PMC_IDX_FIXED_INSTRUCTIONS			(X86_PMC_IDX_FIXED + 0)
 
 /* CPU_CLK_Unhalted.Core: */
 #define MSR_ARCH_PERFMON_FIXED_CTR1			0x30a
-#define X86_PMC_IDX_FIXED_CPU_CYCLES			(X86_PMC_IDX_FIXED + 1)
 
 /* CPU_CLK_Unhalted.Ref: */
 #define MSR_ARCH_PERFMON_FIXED_CTR2			0x30b
-#define X86_PMC_IDX_FIXED_BUS_CYCLES			(X86_PMC_IDX_FIXED + 2)
 
 /*
  * We model BTS tracing as another fixed-mode PMC.
arch/x86/kernel/cpu/perf_event.c +81 -10

177 lines not on this page · raw message