Skip to contentSkip to conversation

Basic perf PMU support for Haswell v4

PATCH In review Andi Kleen 3 participants 10 messages Feb 5, 2013 – Feb 6, 2013

Subsystems (2)
lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

This is based on v7 of the full Haswell PMU support, but ported to the latest perf/core and stripped down to the extreme "perf for dummies" edition as requested.

I removed some more patches, these will come later.
I moved parts of an later patch (counter constraints for qualifiers) into an earlier patch, to support the full raw bits at least.

For anything beyond basic usage this is not very useful.

Contains support for:
- Basic Haswell PMU and PEBS support
- Late unmasking of the PMI
- Support for wide counters

v2: Addressed Stephane's feedback. See individual patches for details.
v3: now even more bite-sized. Qualifier constraints merged earlier.
Add some Reviewed-bys.
v4: Rename some variables, add some comments and other minor changes in the first patch.

-Andi

author
#1 lore raw

PATCH 1/5 perf, x86: Add PEBSv2 record support v2

From: Andi Kleen <[email protected]>

Add support for the v2 PEBS format. It has a superset of the v1 PEBS fields, but has a longer record so we need to adjust the code paths.

The main advantage is the new "EventingRip" support which directly gives the instruction, not off-by-one instruction. So with precise == 2 we use that directly and don't try to use LBRs and walking basic blocks. This lowers the overhead significantly.

Some other features are added in later patches.

v2: Rename various identifiers. Add more comments. Get rid of a cast.

---
 arch/x86/kernel/cpu/perf_event.c          |    2 +-
 arch/x86/kernel/cpu/perf_event_intel_ds.c |  113 +++++++++++++++++++++++------
 2 files changed, 91 insertions(+), 24 deletions(-)
arch/x86/kernel/cpu/perf_event.c +1 -1
index 6774c17..c95290a 100644
@@ -397,7 +397,7 @@ int x86_pmu_hw_config(struct perf_event *event)
 		 * check that PEBS LBR correction does not conflict with
 		 * whatever the user is asking with attr->branch_sample_type
 		 */
-		if (event->attr.precise_ip > 1) {
+		if (event->attr.precise_ip > 1 && x86_pmu.intel_cap.pebs_format < 2) {
 			u64 *br_type = &event->attr.branch_sample_type;
 
 			if (has_branch_stack(event)) {
arch/x86/kernel/cpu/perf_event_intel_ds.c +90 -23

161 lines not on this page · raw message

Signature · 1 line
1.7.7.6
author
#2 lore raw

PATCH 2/5 perf, x86: Basic Haswell PMU support v4

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

Add basic Haswell PMU support.

Similar to SandyBridge, but has a few new events and two new counter bits.

There are some new counter flags that need to be prevented from being set on fixed counters, and allowed to be set for generic counters.

Also we add support for the counter 2 constraint to handle all raw events.

Contains fixes from Stephane Eranian

v2: Folded TSX bits into standard FIXED_EVENT_CONSTRAINTS
v3: Use SNB LBR init code. Comment fix (Stephane Eranian)
v4: Add the counter2 constraints. Fix comment in the right place.

---
 arch/x86/include/asm/perf_event.h      |    3 +
 arch/x86/kernel/cpu/perf_event.h       |    5 ++-
 arch/x86/kernel/cpu/perf_event_intel.c |   71 ++++++++++++++++++++++++++++++++
 3 files changed, 78 insertions(+), 1 deletions(-)
arch/x86/include/asm/perf_event.h +3 -0
index 4fabcdf..4003bb6 100644
@@ -29,6 +29,9 @@
 #define ARCH_PERFMON_EVENTSEL_INV			(1ULL << 23)
 #define ARCH_PERFMON_EVENTSEL_CMASK			0xFF000000ULL
 
+#define HSW_INTX					(1ULL << 32)
+#define HSW_INTX_CHECKPOINTED				(1ULL << 33)
+
 #define AMD_PERFMON_EVENTSEL_GUESTONLY			(1ULL << 40)
 #define AMD_PERFMON_EVENTSEL_HOSTONLY			(1ULL << 41)
 
arch/x86/kernel/cpu/perf_event.h +4 -1
index 115c1ea..8941899 100644
@@ -219,11 +219,14 @@ struct cpu_hw_events {
  *  - inv
  *  - edge
  *  - cnt-mask
+ *  - intx
+ *  - intx_cp
  *  The other filters are supported by fixed counters.
  *  The any-thread option is supported starting with v3.
  */
+#define FIXED_EVENT_FLAGS (X86_RAW_EVENT_MASK|HSW_INTX|HSW_INTX_CHECKPOINTED)
 #define FIXED_EVENT_CONSTRAINT(c, n)	\
-	EVENT_CONSTRAINT(c, (1ULL << (32+n)), X86_RAW_EVENT_MASK)
+	EVENT_CONSTRAINT(c, (1ULL << (32+n)), FIXED_EVENT_FLAGS)
 
 /*
  * Constraint on the Event code + UMask
arch/x86/kernel/cpu/perf_event_intel.c +71 -0

95 lines not on this page · raw message

Signature · 1 line
1.7.7.6
author
#3 lore raw

PATCH 3/5 perf, x86: Basic Haswell PEBS support v4

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

Add basic PEBS support for Haswell.
The constraints are similar to SandyBridge with a few new events.

v2: Readd missing pebs_aliases
v3: Readd missing hunk. Fix some constraints.
v4: Fix typo in PEBS event table (Stephane Eranian)

---
 arch/x86/kernel/cpu/perf_event.h          |    2 ++
 arch/x86/kernel/cpu/perf_event_intel.c    |    6 ++++--
 arch/x86/kernel/cpu/perf_event_intel_ds.c |   29 +++++++++++++++++++++++++++++
 3 files changed, 35 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/perf_event.h +2 -0
index 8941899..1567b0d 100644
@@ -596,6 +596,8 @@ extern struct event_constraint intel_snb_pebs_event_constraints[];
 
 extern struct event_constraint intel_ivb_pebs_event_constraints[];
 
+extern struct event_constraint intel_hsw_pebs_event_constraints[];
+
 struct event_constraint *intel_pebs_constraints(struct perf_event *event);
 
 void intel_pmu_pebs_enable(struct perf_event *event);
arch/x86/kernel/cpu/perf_event_intel.c +4 -2
index 58b737d..aa48048 100644
@@ -827,7 +827,8 @@ static inline bool intel_pmu_needs_lbr_smpl(struct perf_event *event)
 		return true;
 
 	/* implicit branch sampling to correct PEBS skid */
-	if (x86_pmu.intel_cap.pebs_trap && event->attr.precise_ip > 1)
+	if (x86_pmu.intel_cap.pebs_trap && event->attr.precise_ip > 1 &&
+	    x86_pmu.intel_cap.pebs_format < 2)
 		return true;
 
 	return false;
@@ -2167,8 +2168,9 @@ __init int intel_pmu_init(void)
 		intel_pmu_lbr_init_snb();
 
 		x86_pmu.event_constraints = intel_hsw_event_constraints;
-
+		x86_pmu.pebs_constraints = intel_hsw_pebs_event_constraints;
 		x86_pmu.extra_regs = intel_snb_extra_regs;
+		x86_pmu.pebs_aliases = intel_pebs_aliases_snb;
 		/* all extra regs are per-cpu when HT is on */
 		x86_pmu.er_flags |= ERF_HAS_RSP_1;
 		x86_pmu.er_flags |= ERF_NO_HT_SHARING;
arch/x86/kernel/cpu/perf_event_intel_ds.c +29 -0
index 3a828ed..bae2199 100644
@@ -437,6 +437,35 @@ struct event_constraint intel_ivb_pebs_event_constraints[] = {
         EVENT_CONSTRAINT_END
 };
 
+struct event_constraint intel_hsw_pebs_event_constraints[] = {
+	INTEL_UEVENT_CONSTRAINT(0x01c0, 0x2), /* INST_RETIRED.PRECDIST */
+	INTEL_UEVENT_CONSTRAINT(0x01c2, 0xf), /* UOPS_RETIRED.ALL */
+	INTEL_UEVENT_CONSTRAINT(0x02c2, 0xf), /* UOPS_RETIRED.RETIRE_SLOTS */
+	INTEL_EVENT_CONSTRAINT(0xc4, 0xf),    /* BR_INST_RETIRED.* */
+	INTEL_UEVENT_CONSTRAINT(0x01c5, 0xf), /* BR_MISP_RETIRED.CONDITIONAL */
+	INTEL_UEVENT_CONSTRAINT(0x04c5, 0xf), /* BR_MISP_RETIRED.ALL_BRANCHES */
+	INTEL_UEVENT_CONSTRAINT(0x20c5, 0xf), /* BR_MISP_RETIRED.NEAR_TAKEN */
+	INTEL_EVENT_CONSTRAINT(0xcd, 0x8),    /* MEM_TRANS_RETIRED.* */
+	INTEL_UEVENT_CONSTRAINT(0x11d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_LOADS */
+	INTEL_UEVENT_CONSTRAINT(0x12d0, 0xf), /* MEM_UOPS_RETIRED.STLB_MISS_STORES */
+	INTEL_UEVENT_CONSTRAINT(0x21d0, 0xf), /* MEM_UOPS_RETIRED.LOCK_LOADS */
+	INTEL_UEVENT_CONSTRAINT(0x41d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_LOADS */
+	INTEL_UEVENT_CONSTRAINT(0x42d0, 0xf), /* MEM_UOPS_RETIRED.SPLIT_STORES */
+	INTEL_UEVENT_CONSTRAINT(0x81d0, 0xf), /* MEM_UOPS_RETIRED.ALL_LOADS */
+	INTEL_UEVENT_CONSTRAINT(0x82d0, 0xf), /* MEM_UOPS_RETIRED.ALL_STORES */
+	INTEL_UEVENT_CONSTRAINT(0x01d1, 0xf), /* MEM_LOAD_UOPS_RETIRED.L1_HIT */
+	INTEL_UEVENT_CONSTRAINT(0x02d1, 0xf), /* MEM_LOAD_UOPS_RETIRED.L2_HIT */
+	INTEL_UEVENT_CONSTRAINT(0x04d1, 0xf), /* MEM_LOAD_UOPS_RETIRED.L3_HIT */
+	INTEL_UEVENT_CONSTRAINT(0x40d1, 0xf), /* MEM_LOAD_UOPS_RETIRED.HIT_LFB */
+	INTEL_UEVENT_CONSTRAINT(0x01d2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.XSNP_MISS */
+	INTEL_UEVENT_CONSTRAINT(0x02d2, 0xf), /* MEM_LOAD_UOPS_LLC_HIT_RETIRED.XSNP_HIT */
+	INTEL_UEVENT_CONSTRAINT(0x01d3, 0xf), /* MEM_LOAD_UOPS_LLC_MISS_RETIRED.LOCAL_DRAM */
+	INTEL_UEVENT_CONSTRAINT(0x04c8, 0xf), /* HLE_RETIRED.Abort */
+	INTEL_UEVENT_CONSTRAINT(0x04c9, 0xf), /* RTM_RETIRED.Abort */
+
+	EVENT_CONSTRAINT_END
+};
+
 struct event_constraint *intel_pebs_constraints(struct perf_event *event)
 {
 	struct event_constraint *c;
Signature · 1 line
1.7.7.6
author
#4 lore raw

PATCH 4/5 perf, x86: Support full width counting

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

Recent Intel CPUs have a new alternative MSR range for perfctrs that allows writing the full counter width. Enable this range if the hardware reports it using a new capability bit. This lowers overhead of perf stat slightly because it has to do less interrupts to accumulate the counter value. On Haswell it also avoids some problems with TSX aborting when the end of the counter range is reached.

---
 arch/x86/include/uapi/asm/msr-index.h  |    3 +++
 arch/x86/kernel/cpu/perf_event.h       |    1 +
 arch/x86/kernel/cpu/perf_event_intel.c |    6 ++++++
 3 files changed, 10 insertions(+), 0 deletions(-)
arch/x86/include/uapi/asm/msr-index.h +3 -0
index 433a59f..af41a77 100644
@@ -163,6 +163,9 @@
 #define MSR_KNC_EVNTSEL0               0x00000028
 #define MSR_KNC_EVNTSEL1               0x00000029
 
+/* Alternative perfctr range with full access. */
+#define MSR_IA32_PMC0			0x000004c1
+
 /* AMD64 MSRs. Not complete. See the architecture manual for a more
    complete list. */
 
arch/x86/kernel/cpu/perf_event.h +1 -0
index 1567b0d..ce2a863 100644
@@ -278,6 +278,7 @@ union perf_capabilities {
 		u64	pebs_arch_reg:1;
 		u64	pebs_format:4;
 		u64	smm_freeze:1;
+		u64	fw_write:1;
 	};
 	u64	capabilities;
 };
arch/x86/kernel/cpu/perf_event_intel.c +6 -0
index aa48048..d96010a 100644
@@ -2228,5 +2228,11 @@ __init int intel_pmu_init(void)
 		}
 	}
 
+	/* Support full width counters using alternative MSR range */
+	if (x86_pmu.intel_cap.fw_write) {
+		x86_pmu.max_period = x86_pmu.cntval_mask;
+		x86_pmu.perfctr = MSR_IA32_PMC0;
+	}
+
 	return 0;
 }
Signature · 1 line
1.7.7.6
#5 lore raw

Re: [PATCH 4/5] perf, x86: Support full width counting

Quoted 9 lines · Andi Kleen

From: Andi Kleen <[email protected]>

Recent Intel CPUs have a new alternative MSR range for perfctrs that allows writing the full counter width. Enable this range if the hardware reports it using a new capability bit. This lowers overhead of perf stat slightly because it has to do less interrupts to accumulate the counter value. On Haswell it also avoids some problems with TSX aborting when the end of the counter range is reached.

I would add that this patch mitigates overhead in counting from on SNB/IVB as well as HSW.

Quoted 38 lines
---
 arch/x86/include/uapi/asm/msr-index.h  |    3 +++
 arch/x86/kernel/cpu/perf_event.h       |    1 +
 arch/x86/kernel/cpu/perf_event_intel.c |    6 ++++++
 3 files changed, 10 insertions(+), 0 deletions(-)
arch/x86/include/uapi/asm/msr-index.h +3 -0
index 433a59f..af41a77 100644
@@ -163,6 +163,9 @@
 #define MSR_KNC_EVNTSEL0               0x00000028
 #define MSR_KNC_EVNTSEL1               0x00000029
 
+/* Alternative perfctr range with full access. */
+#define MSR_IA32_PMC0                  0x000004c1
+
 /* AMD64 MSRs. Not complete. See the architecture manual for a more
    complete list. */
 
arch/x86/kernel/cpu/perf_event.h +1 -0
index 1567b0d..ce2a863 100644
@@ -278,6 +278,7 @@ union perf_capabilities {
                u64     pebs_arch_reg:1;
                u64     pebs_format:4;
                u64     smm_freeze:1;
+               u64     fw_write:1;
        };
        u64     capabilities;
 };
arch/x86/kernel/cpu/perf_event_intel.c +0 -0
index aa48048..d96010a 100644

arch/x86/kernel/cpu/perf_event_intel.c __init int intel_pmu_init(void) · line 2233

Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel.c +3 -0
@@ -2228,5 +2228,11 @@ __init int intel_pmu_init(void)
                }
        }
 
+       /* Support full width counters using alternative MSR range */
+       if (x86_pmu.intel_cap.fw_write) {
+               x86_pmu.max_period = x86_pmu.cntval_mask;

Something is not clear to me: What happens to the fixed counters with full writes? Were they already full-width? The SDM does not explain what happens to them with this extension. Could you clarify?

arch/x86/kernel/cpu/perf_event_intel.c __init int intel_pmu_init(void) · line 2234

Quoted 1 line
arch/x86/kernel/cpu/perf_event_intel.c +1 -0
+               x86_pmu.perfctr = MSR_IA32_PMC0;

I would add here:
pr_cont("full-width counters, ");

Quoted 7 lines
arch/x86/kernel/cpu/perf_event_intel.c +2 -1
+       }
+
        return 0;
 }
--

1.7.7.6

author
#6 lore raw

Re: [PATCH 4/5] perf, x86: Support full width counting

Quoted 6 lines · Stephane Eranian
Quoted 6 lines
arch/x86/kernel/cpu/perf_event_intel.c +0 -0
@@ -2228,5 +2228,11 @@ __init int intel_pmu_init(void)
                }
        }
 
Quoted 7 lines

arch/x86/kernel/cpu/perf_event_intel.c __init int intel_pmu_init(void) · line 2233

Quoted 3 lines
arch/x86/kernel/cpu/perf_event_intel.c +3 -0
+       /* Support full width counters using alternative MSR range */
+       if (x86_pmu.intel_cap.fw_write) {
+               x86_pmu.max_period = x86_pmu.cntval_mask;

Something is not clear to me: What happens to the fixed counters with full writes? Were they already full-width? The SDM does not explain what happens to them with this extension. Could you clarify?

I tested and they always support the full reported width.

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only.
#7 lore raw

Re: [PATCH 4/5] perf, x86: Support full width counting

In reply to Stephane Eranian

Here's an updated patch. I'm not reposting the full series. Also cut down on cc to spare the innocents.

---

perf, x86: Support full width counting v2

Recent Intel CPUs like Haswell and IvyBridge have a new alternative MSR range for perfctrs that allows writing the full counter width. Enable this range if the hardware reports it using a new capability bit.

This lowers the overhead of perf stat slightly because it has to do less interrupts to accumulate the counter value. On Haswell it also avoids some problems with TSX aborting when the end of the counter range is reached.

v2: Print the feature at boot

arch/x86/include/uapi/asm/msr-index.h +3 -0
index 433a59f..af41a77 100644
@@ -163,6 +163,9 @@
 #define MSR_KNC_EVNTSEL0               0x00000028
 #define MSR_KNC_EVNTSEL1               0x00000029
 
+/* Alternative perfctr range with full access. */
+#define MSR_IA32_PMC0			0x000004c1
+
 /* AMD64 MSRs. Not complete. See the architecture manual for a more
    complete list. */
 
arch/x86/kernel/cpu/perf_event.h +1 -0
index 1567b0d..ce2a863 100644
@@ -278,6 +278,7 @@ union perf_capabilities {
 		u64	pebs_arch_reg:1;
 		u64	pebs_format:4;
 		u64	smm_freeze:1;
+		u64	fw_write:1;
 	};
 	u64	capabilities;
 };
arch/x86/kernel/cpu/perf_event_intel.c +7 -0
index aa48048..06dcc0c 100644
@@ -2228,5 +2228,12 @@ __init int intel_pmu_init(void)
 		}
 	}
 
+	/* Support full width counters using alternative MSR range */
+	if (x86_pmu.intel_cap.fw_write) {
+		x86_pmu.max_period = x86_pmu.cntval_mask;
+		x86_pmu.perfctr = MSR_IA32_PMC0;
+		pr_cont("full-width counters, ");
+	}
+
 	return 0;
 }
Signature · 1 line
[email protected] -- Speaking for myself only
Reviewed-by
#8 lore raw

Re: [PATCH 4/5] perf, x86: Support full width counting

Quoted 11 lines · Andi Kleen

Here's an updated patch. I'm not reposting the full series. Also cut down on cc to spare the innocents.

---

perf, x86: Support full width counting v2

Recent Intel CPUs like Haswell and IvyBridge have a new alternative MSR range for perfctrs that allows writing the full counter width. Enable this range if the hardware reports it using a new capability bit.

Quoted 5 lines

This lowers the overhead of perf stat slightly because it has to do less interrupts to accumulate the counter value. On Haswell it also avoids some problems with TSX aborting when the end of the counter range is reached.

v2: Print the feature at boot

Tested okay for me.

Quoted 48 lines
arch/x86/include/uapi/asm/msr-index.h +3 -0
index 433a59f..af41a77 100644
@@ -163,6 +163,9 @@
 #define MSR_KNC_EVNTSEL0               0x00000028
 #define MSR_KNC_EVNTSEL1               0x00000029
 
+/* Alternative perfctr range with full access. */
+#define MSR_IA32_PMC0                  0x000004c1
+
 /* AMD64 MSRs. Not complete. See the architecture manual for a more
    complete list. */
 
arch/x86/kernel/cpu/perf_event.h +1 -0
index 1567b0d..ce2a863 100644
@@ -278,6 +278,7 @@ union perf_capabilities {
                u64     pebs_arch_reg:1;
                u64     pebs_format:4;
                u64     smm_freeze:1;
+               u64     fw_write:1;
        };
        u64     capabilities;
 };
arch/x86/kernel/cpu/perf_event_intel.c +7 -1
index aa48048..06dcc0c 100644
@@ -2228,5 +2228,12 @@ __init int intel_pmu_init(void)
                }
        }
 
+       /* Support full width counters using alternative MSR range */
+       if (x86_pmu.intel_cap.fw_write) {
+               x86_pmu.max_period = x86_pmu.cntval_mask;
+               x86_pmu.perfctr = MSR_IA32_PMC0;
+               pr_cont("full-width counters, ");
+       }
+
        return 0;
 }
 
--

[email protected] -- Speaking for myself only

author
#9 lore raw

PATCH 5/5 perf, x86: Move NMI clearing to end of PMI handler after the counter registers are reset

In reply to Andi Kleen

From: Andi Kleen <[email protected]>

This avoids some problems with spurious PMIs on Haswell. Haswell seems to behave more like P4 in this regard. Do the same thing as the P4 perf handler by unmasking the NMI only at the end. Shouldn't make any difference for earlier non P4 cores.

---
 arch/x86/kernel/cpu/perf_event_intel.c |   16 ++++++----------
 1 files changed, 6 insertions(+), 10 deletions(-)
arch/x86/kernel/cpu/perf_event_intel.c +6 -10
index d96010a..6c0ec44 100644
@@ -1122,16 +1122,6 @@ static int intel_pmu_handle_irq(struct pt_regs *regs)
 
 	cpuc = &__get_cpu_var(cpu_hw_events);
 
-	/*
-	 * Some chipsets need to unmask the LVTPC in a particular spot
-	 * inside the nmi handler.  As a result, the unmasking was pushed
-	 * into all the nmi handlers.
-	 *
-	 * This handler doesn't seem to have any issues with the unmasking
-	 * so it was left at the top.
-	 */
-	apic_write(APIC_LVTPC, APIC_DM_NMI);
-
 	intel_pmu_disable_all();
 	handled = intel_pmu_drain_bts_buffer();
 	status = intel_pmu_get_status();
@@ -1191,6 +1181,12 @@ again:
 
 done:
 	intel_pmu_enable_all(0);
+	/*
+	 * Only unmask the NMI after the overflow counters
+	 * have been reset. This avoids spurious NMIs on
+	 * Haswell CPUs.
+	 */
+	apic_write(APIC_LVTPC, APIC_DM_NMI);
 	return handled;
 }
 
Signature · 1 line
1.7.7.6