Skip to contentSkip to conversation

perf/core: x86 and AMD pmu updates

PATCH New Robert Richter 1 participant 13 messages Jan 22, 2010

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

PATCH 1/12 perf/core: x86 and AMD pmu updates

I know there is some ongoing work by Stephane in this code too, but I want to submit the patches anyway for review. If necessary I will rebase the patch set on top of Stephanes patches. Anyway, this patch set applies to tip/perf/core and can be pulled from here too:

git://git.kernel.org/pub/scm/linux/kernel/git/rric/oprofile.git perf

The patches contain changes, updates and code reworks I made during reviewing the code and implementing IBS for perf_events. Patches for IBS I will submit in a later series. These patches will include an implementation that extends the ABI to support model specific hardware pmu features.

In this patch set I made some model specific pmu code generic and vice versa. The event constraint handler works now for AMD pmu too. Fixed counter code that is only need on Intel cpus is now model specific. Also the MSR masks were unified, where the hardware implementation is the same for all cpus.

-Robert

#1 lore raw

PATCH 1/12 perf/core: correct files in MAINTAINERS entry

This corrects the file entries for perf_events. The following files are catched now:

arch/frv/include/asm/perf_event.h
arch/frv/lib/perf_event.c
arch/parisc/include/asm/perf_event.h
arch/powerpc/include/asm/perf_event.h
arch/powerpc/kernel/perf_callchain.c
arch/powerpc/kernel/perf_event.c
arch/s390/include/asm/perf_event.h
arch/sh/include/asm/perf_event.h
arch/sh/kernel/cpu/sh4a/perf_event.c
arch/sh/kernel/cpu/sh4/perf_event.c
arch/sh/kernel/perf_callchain.c
arch/sh/kernel/perf_event.c
arch/sparc/include/asm/perf_event.h
arch/sparc/kernel/perf_event.c
arch/x86/include/asm/perf_event.h
arch/x86/kernel/cpu/perf_event.c
include/linux/perf_event.h
kernel/perf_event.c
tools/perf/

---
 MAINTAINERS |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
MAINTAINERS +3 -1
index c8f47bf..350efc3 100644
@@ -4224,7 +4224,9 @@ M:	Ingo Molnar <[email protected]>
 S:	Supported
 F:	kernel/perf_event.c
 F:	include/linux/perf_event.h
-F:	arch/*/*/kernel/perf_event.c
+F:	arch/*/kernel/perf_event.c
+F:	arch/*/kernel/*/perf_event.c
+F:	arch/*/kernel/*/*/perf_event.c
 F:	arch/*/include/asm/perf_event.h
 F:	arch/*/lib/perf_event.c
 F:	arch/*/kernel/perf_callchain.c
Signature · 1 line
1.6.6
#2 lore raw

PATCH 2/12 perf/core, x86: undo some some *_counter* -> *_event* renames

In reply to Robert Richter

The big rename

cdd6c48 perf: Do the big rename: Performance Counters -> Performance Events

accidentally renamed some members of stucts that were named after registers in the spec. To avoid confusion this patch reverts some changes. The related specs are MSR descriptions in AMD's BKDGs and the ARCHITECTURAL PERFORMANCE MONITORING section in the Intel 64 and IA-32 Architectures Software Developer's Manuals.

---
 arch/x86/include/asm/perf_event.h |    4 +-
 arch/x86/kernel/cpu/perf_event.c  |   96 ++++++++++++++++++------------------
 arch/x86/oprofile/op_model_ppro.c |    4 +-
 3 files changed, 52 insertions(+), 52 deletions(-)
arch/x86/include/asm/perf_event.h +2 -2
index 8d9f854..1f50cfc 100644
@@ -54,7 +54,7 @@
 union cpuid10_eax {
 	struct {
 		unsigned int version_id:8;
-		unsigned int num_events:8;
+		unsigned int num_counters:8;
 		unsigned int bit_width:8;
 		unsigned int mask_length:8;
 	} split;
@@ -63,7 +63,7 @@ union cpuid10_eax {
 
 union cpuid10_edx {
 	struct {
-		unsigned int num_events_fixed:4;
+		unsigned int num_counters_fixed:4;
 		unsigned int reserved:28;
 	} split;
 	unsigned int full;
arch/x86/kernel/cpu/perf_event.c +48 -48

266 lines not on this page · raw message

arch/x86/oprofile/op_model_ppro.c +2 -2
index 8eb0587..4899215 100644
@@ -234,11 +234,11 @@ static void arch_perfmon_setup_counters(void)
 	if (eax.split.version_id == 0 && current_cpu_data.x86 == 6 &&
 		current_cpu_data.x86_model == 15) {
 		eax.split.version_id = 2;
-		eax.split.num_events = 2;
+		eax.split.num_counters = 2;
 		eax.split.bit_width = 40;
 	}
 
-	num_counters = eax.split.num_events;
+	num_counters = eax.split.num_counters;
 
 	op_arch_perfmon_spec.num_counters = num_counters;
 	op_arch_perfmon_spec.num_controls = num_counters;
Signature · 1 line
1.6.6
#3 lore raw

PATCH 3/12 perf/core, x86: remove duplicate perf_event_mask variable

In reply to Robert Richter

The same information is stored also in x86_pmu.intel_ctrl. This patch removes perf_event_mask and instead uses x86_pmu.intel_ctrl directly.

---
 arch/x86/kernel/cpu/perf_event.c |    9 +++------
 1 files changed, 3 insertions(+), 6 deletions(-)
arch/x86/kernel/cpu/perf_event.c +3 -6
index c3f920a..4291f86 100644
@@ -27,8 +27,6 @@
 #include <asm/stacktrace.h>
 #include <asm/nmi.h>
 
-static u64 perf_event_mask __read_mostly;
-
 /* The maximal number of PEBS events: */
 #define MAX_PEBS_EVENTS	4
 
@@ -2197,7 +2195,7 @@ void __init init_hw_perf_events(void)
 		     x86_pmu.num_counters, X86_PMC_MAX_GENERIC);
 		x86_pmu.num_counters = X86_PMC_MAX_GENERIC;
 	}
-	perf_event_mask = (1 << x86_pmu.num_counters) - 1;
+	x86_pmu.intel_ctrl = (1 << x86_pmu.num_counters) - 1;
 	perf_max_events = x86_pmu.num_counters;
 
 	if (x86_pmu.num_counters_fixed > X86_PMC_MAX_FIXED) {
@@ -2206,9 +2204,8 @@ void __init init_hw_perf_events(void)
 		x86_pmu.num_counters_fixed = X86_PMC_MAX_FIXED;
 	}
 
-	perf_event_mask |=
+	x86_pmu.intel_ctrl |=
 		((1LL << x86_pmu.num_counters_fixed)-1) << X86_PMC_IDX_FIXED;
-	x86_pmu.intel_ctrl = perf_event_mask;
 
 	perf_events_lapic_init();
 	register_die_notifier(&perf_event_nmi_notifier);
@@ -2219,7 +2216,7 @@ void __init init_hw_perf_events(void)
 	pr_info("... value mask:             %016Lx\n", x86_pmu.cntval_mask);
 	pr_info("... max period:             %016Lx\n", x86_pmu.max_period);
 	pr_info("... fixed-purpose events:   %d\n",     x86_pmu.num_counters_fixed);
-	pr_info("... event mask:             %016Lx\n", perf_event_mask);
+	pr_info("... event mask:             %016Lx\n", x86_pmu.intel_ctrl);
 }
 
 static inline void x86_pmu_read(struct perf_event *event)
Signature · 1 line
1.6.6
#4 lore raw

PATCH 4/12 perf/core, x86: remove cpu_hw_events.interrupts

In reply to Robert Richter

This member in the struct is not used anymore and can be removed.

---
 arch/x86/kernel/cpu/perf_event.c |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)
arch/x86/kernel/cpu/perf_event.c +0 -1
index 4291f86..c49221b 100644
@@ -70,7 +70,6 @@ struct cpu_hw_events {
 	struct perf_event	*events[X86_PMC_IDX_MAX];
 	unsigned long		used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
 	unsigned long		active_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
-	unsigned long		interrupts;
 	int			enabled;
 	struct debug_store	*ds;
 };
Signature · 1 line
1.6.6
#5 lore raw

PATCH 5/12 perf/core, x86: reduce number of CONFIG_X86_LOCAL_APIC macros

In reply to Robert Richter

The function reserve_pmc_hardware() and release_pmc_hardware() were hard to read. This patch improves readablity of the code by removing most of the CONFIG_X86_LOCAL_APIC macros.

---
 arch/x86/kernel/cpu/perf_event.c |   15 +++++++++------
 1 files changed, 9 insertions(+), 6 deletions(-)
arch/x86/kernel/cpu/perf_event.c +9 -6
index c49221b..5c6244b 100644
@@ -711,9 +711,10 @@ again:
 static atomic_t active_events;
 static DEFINE_MUTEX(pmc_reserve_mutex);
 
+#ifdef CONFIG_X86_LOCAL_APIC
+
 static bool reserve_pmc_hardware(void)
 {
-#ifdef CONFIG_X86_LOCAL_APIC
 	int i;
 
 	if (nmi_watchdog == NMI_LOCAL_APIC)
@@ -728,11 +729,9 @@ static bool reserve_pmc_hardware(void)
 		if (!reserve_evntsel_nmi(x86_pmu.eventsel + i))
 			goto eventsel_fail;
 	}
-#endif
 
 	return true;
 
-#ifdef CONFIG_X86_LOCAL_APIC
 eventsel_fail:
 	for (i--; i >= 0; i--)
 		release_evntsel_nmi(x86_pmu.eventsel + i);
@@ -747,12 +746,10 @@ perfctr_fail:
 		enable_lapic_nmi_watchdog();
 
 	return false;
-#endif
 }
 
 static void release_pmc_hardware(void)
 {
-#ifdef CONFIG_X86_LOCAL_APIC
 	int i;
 
 	for (i = 0; i < x86_pmu.num_counters; i++) {
@@ -762,9 +759,15 @@ static void release_pmc_hardware(void)
 
 	if (nmi_watchdog == NMI_LOCAL_APIC)
 		enable_lapic_nmi_watchdog();
-#endif
 }
 
+#else
+
+static bool reserve_pmc_hardware(void) { return true; }
+static void release_pmc_hardware(void) {}
+
+#endif
+
 static inline bool bts_available(void)
 {
 	return x86_pmu.enable_bts != NULL;
Signature · 1 line
1.6.6
#6 lore raw

PATCH 6/12 perf/core, x86: removing p6_pmu_raw_event()

In reply to Robert Richter

The function is the same as intel_pmu_raw_event(). This patch removes the duplicate code.

---
 arch/x86/kernel/cpu/perf_event.c |   20 +-------------------
 1 files changed, 1 insertions(+), 19 deletions(-)
arch/x86/kernel/cpu/perf_event.c +1 -19
index 5c6244b..1863cda 100644
@@ -150,24 +150,6 @@ static u64 p6_pmu_event_map(int hw_event)
  */
 #define P6_NOP_EVENT			0x0000002EULL
 
-static u64 p6_pmu_raw_event(u64 hw_event)
-{
-#define P6_EVNTSEL_EVENT_MASK		0x000000FFULL
-#define P6_EVNTSEL_UNIT_MASK		0x0000FF00ULL
-#define P6_EVNTSEL_EDGE_MASK		0x00040000ULL
-#define P6_EVNTSEL_INV_MASK		0x00800000ULL
-#define P6_EVNTSEL_REG_MASK		0xFF000000ULL
-
-#define P6_EVNTSEL_MASK			\
-	(P6_EVNTSEL_EVENT_MASK |	\
-	 P6_EVNTSEL_UNIT_MASK  |	\
-	 P6_EVNTSEL_EDGE_MASK  |	\
-	 P6_EVNTSEL_INV_MASK   |	\
-	 P6_EVNTSEL_REG_MASK)
-
-	return hw_event & P6_EVNTSEL_MASK;
-}
-
 static const struct event_constraint intel_p6_event_constraints[] =
 {
 	EVENT_CONSTRAINT(0xc1, 0x1),	/* FLOPS */
@@ -1978,7 +1960,7 @@ static __initconst struct x86_pmu p6_pmu = {
 	.eventsel		= MSR_P6_EVNTSEL0,
 	.perfctr		= MSR_P6_PERFCTR0,
 	.event_map		= p6_pmu_event_map,
-	.raw_event		= p6_pmu_raw_event,
+	.raw_event		= intel_pmu_raw_event,
 	.max_events		= ARRAY_SIZE(p6_perfmon_event_map),
 	.apic			= 1,
 	.max_period		= (1ULL << 31) - 1,
Signature · 1 line
1.6.6
#7 lore raw

PATCH 7/12 perf/core, x86: rename macro in ARCH_PERFMON_EVENTSEL_ENABLE

In reply to Robert Richter

For consistency reasons this patch renames ARCH_PERFMON_EVENTSEL0_ENABLE to ARCH_PERFMON_EVENTSEL_ENABLE.

---
 arch/x86/include/asm/perf_event.h      |    2 +-
 arch/x86/kernel/cpu/perf_event.c       |   16 ++++++++--------
 arch/x86/kernel/cpu/perfctr-watchdog.c |    2 +-
 arch/x86/oprofile/op_model_amd.c       |    4 ++--
 arch/x86/oprofile/op_model_ppro.c      |    4 ++--
 5 files changed, 14 insertions(+), 14 deletions(-)
arch/x86/include/asm/perf_event.h +1 -1
index 1f50cfc..c7f723a 100644
@@ -18,7 +18,7 @@
 #define MSR_ARCH_PERFMON_EVENTSEL0			     0x186
 #define MSR_ARCH_PERFMON_EVENTSEL1			     0x187
 
-#define ARCH_PERFMON_EVENTSEL0_ENABLE			  (1 << 22)
+#define ARCH_PERFMON_EVENTSEL_ENABLE			  (1 << 22)
 #define ARCH_PERFMON_EVENTSEL_INT			  (1 << 20)
 #define ARCH_PERFMON_EVENTSEL_OS			  (1 << 17)
 #define ARCH_PERFMON_EVENTSEL_USR			  (1 << 16)
arch/x86/kernel/cpu/perf_event.c +8 -8
index 1863cda..96cbfe6 100644
@@ -1051,7 +1051,7 @@ static void p6_pmu_disable_all(void)
 
 	/* p6 only has one enable register */
 	rdmsrl(MSR_P6_EVNTSEL0, val);
-	val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
+	val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
 	wrmsrl(MSR_P6_EVNTSEL0, val);
 }
 
@@ -1093,9 +1093,9 @@ static void amd_pmu_disable_all(void)
 		if (!test_bit(idx, cpuc->active_mask))
 			continue;
 		rdmsrl(MSR_K7_EVNTSEL0 + idx, val);
-		if (!(val & ARCH_PERFMON_EVENTSEL0_ENABLE))
+		if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE))
 			continue;
-		val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
+		val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
 		wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
 	}
 }
@@ -1120,7 +1120,7 @@ static void p6_pmu_enable_all(void)
 
 	/* p6 only has one enable register */
 	rdmsrl(MSR_P6_EVNTSEL0, val);
-	val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
+	val |= ARCH_PERFMON_EVENTSEL_ENABLE;
 	wrmsrl(MSR_P6_EVNTSEL0, val);
 }
 
@@ -1166,7 +1166,7 @@ static void amd_pmu_enable_all(void)
 			continue;
 
 		val = event->hw.config;
-		val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
+		val |= ARCH_PERFMON_EVENTSEL_ENABLE;
 		wrmsrl(MSR_K7_EVNTSEL0 + idx, val);
 	}
 }
@@ -1195,7 +1195,7 @@ static inline void intel_pmu_ack_status(u64 ack)
 static inline void x86_pmu_enable_event(struct hw_perf_event *hwc, int idx)
 {
 	(void)checking_wrmsrl(hwc->config_base + idx,
-			      hwc->config | ARCH_PERFMON_EVENTSEL0_ENABLE);
+			      hwc->config | ARCH_PERFMON_EVENTSEL_ENABLE);
 }
 
 static inline void x86_pmu_disable_event(struct hw_perf_event *hwc, int idx)
@@ -1223,7 +1223,7 @@ p6_pmu_disable_event(struct hw_perf_event *hwc, int idx)
 	u64 val = P6_NOP_EVENT;
 
 	if (cpuc->enabled)
-		val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
+		val |= ARCH_PERFMON_EVENTSEL_ENABLE;
 
 	(void)checking_wrmsrl(hwc->config_base + idx, val);
 }
@@ -1341,7 +1341,7 @@ static void p6_pmu_enable_event(struct hw_perf_event *hwc, int idx)
 
 	val = hwc->config;
 	if (cpuc->enabled)
-		val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
+		val |= ARCH_PERFMON_EVENTSEL_ENABLE;
 
 	(void)checking_wrmsrl(hwc->config_base + idx, val);
 }
arch/x86/kernel/cpu/perfctr-watchdog.c +1 -1
index 74f4e85..fb329e9 100644
@@ -680,7 +680,7 @@ static int setup_intel_arch_watchdog(unsigned nmi_hz)
 	cpu_nmi_set_wd_enabled();
 
 	apic_write(APIC_LVTPC, APIC_DM_NMI);
-	evntsel |= ARCH_PERFMON_EVENTSEL0_ENABLE;
+	evntsel |= ARCH_PERFMON_EVENTSEL_ENABLE;
 	wrmsr(evntsel_msr, evntsel, 0);
 	intel_arch_wd_ops.checkbit = 1ULL << (eax.split.bit_width - 1);
 	return 1;
arch/x86/oprofile/op_model_amd.c +2 -2
index 39686c2..729e361 100644
@@ -314,7 +314,7 @@ static void op_amd_start(struct op_msrs const * const msrs)
 		if (!reset_value[op_x86_phys_to_virt(i)])
 			continue;
 		rdmsrl(msrs->controls[i].addr, val);
-		val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
+		val |= ARCH_PERFMON_EVENTSEL_ENABLE;
 		wrmsrl(msrs->controls[i].addr, val);
 	}
 
@@ -334,7 +334,7 @@ static void op_amd_stop(struct op_msrs const * const msrs)
 		if (!reset_value[op_x86_phys_to_virt(i)])
 			continue;
 		rdmsrl(msrs->controls[i].addr, val);
-		val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
+		val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
 		wrmsrl(msrs->controls[i].addr, val);
 	}
 
arch/x86/oprofile/op_model_ppro.c +2 -2
index 4899215..be7f787 100644
@@ -161,7 +161,7 @@ static void ppro_start(struct op_msrs const * const msrs)
 	for (i = 0; i < num_counters; ++i) {
 		if (reset_value[i]) {
 			rdmsrl(msrs->controls[i].addr, val);
-			val |= ARCH_PERFMON_EVENTSEL0_ENABLE;
+			val |= ARCH_PERFMON_EVENTSEL_ENABLE;
 			wrmsrl(msrs->controls[i].addr, val);
 		}
 	}
@@ -179,7 +179,7 @@ static void ppro_stop(struct op_msrs const * const msrs)
 		if (!reset_value[i])
 			continue;
 		rdmsrl(msrs->controls[i].addr, val);
-		val &= ~ARCH_PERFMON_EVENTSEL0_ENABLE;
+		val &= ~ARCH_PERFMON_EVENTSEL_ENABLE;
 		wrmsrl(msrs->controls[i].addr, val);
 	}
 }
Signature · 1 line
1.6.6
#8 lore raw

PATCH 8/12 perf/core, x86: implement ARCH_PERFMON_EVENTSEL bit masks

In reply to Robert Richter

ARCH_PERFMON_EVENTSEL bit masks are offen used in the kernel. This patch adds macros for the bit masks and removes local defines.

---
 arch/x86/include/asm/perf_event.h |   16 ++++++++++---
 arch/x86/kernel/cpu/perf_event.c  |   42 +++++++++++-------------------------
 2 files changed, 25 insertions(+), 33 deletions(-)
arch/x86/include/asm/perf_event.h +12 -4
index c7f723a..448bcf5 100644
@@ -18,10 +18,18 @@
 #define MSR_ARCH_PERFMON_EVENTSEL0			     0x186
 #define MSR_ARCH_PERFMON_EVENTSEL1			     0x187
 
-#define ARCH_PERFMON_EVENTSEL_ENABLE			  (1 << 22)
-#define ARCH_PERFMON_EVENTSEL_INT			  (1 << 20)
-#define ARCH_PERFMON_EVENTSEL_OS			  (1 << 17)
-#define ARCH_PERFMON_EVENTSEL_USR			  (1 << 16)
+#define ARCH_PERFMON_EVENTSEL_EVENT			0x000000FFULL
+#define ARCH_PERFMON_EVENTSEL_UMASK			0x0000FF00ULL
+#define ARCH_PERFMON_EVENTSEL_USR			(1ULL << 16)
+#define ARCH_PERFMON_EVENTSEL_OS			(1ULL << 17)
+#define ARCH_PERFMON_EVENTSEL_EDGE			(1ULL << 18)
+#define ARCH_PERFMON_EVENTSEL_INT			(1ULL << 20)
+#define ARCH_PERFMON_EVENTSEL_ENABLE			(1ULL << 22)
+#define ARCH_PERFMON_EVENTSEL_INV			(1ULL << 23)
+#define ARCH_PERFMON_EVENTSEL_CMASK			0xFF000000ULL
+
+#define AMD64_EVENTSEL_EVENT	\
+	(ARCH_PERFMON_EVENTSEL_EVENT | 0x0FULL << 32)
 
 /*
  * Includes eventsel and unit mask as well:
arch/x86/kernel/cpu/perf_event.c +13 -29

66 lines not on this page · raw message

Signature · 1 line
1.6.6
#9 lore raw

PATCH 9/12 perf/core, x86: use test_and_set_bit() to when grabbing a counter

In reply to Robert Richter

Using test_and_set_bit() in the generic counter allocator makes this atomic. Otherwise a counter could have been allocated twice.

---
 arch/x86/kernel/cpu/perf_event.c |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/perf_event.c +6 -2
index 4eb0355..3f81f91 100644
@@ -1395,8 +1395,13 @@ gen_get_event_idx(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc)
 {
 	int idx;
 
+again:
 	idx = find_first_zero_bit(cpuc->used_mask, x86_pmu.num_counters);
-	return idx == x86_pmu.num_counters ? -1 : idx;
+	if (idx == x86_pmu.num_counters)
+		return -1;
+	if (test_and_set_bit(idx, cpuc->used_mask))
+		goto again;
+	return idx;
 }
 
 /*
@@ -1465,7 +1470,6 @@ try_generic:
 			if (idx == -1)
 				return -EAGAIN;
 
-			set_bit(idx, cpuc->used_mask);
 			hwc->idx = idx;
 		}
 		hwc->config_base = x86_pmu.eventsel;
Signature · 1 line
1.6.6
#10 lore raw

PATCH 10/12 perf/core, x86: removing fixed counter handling for AMD pmu

In reply to Robert Richter

The AMD pmu does not support fixed counters. Thus, fixed counters may not be considered for scheduling decissions. This patch implements an AMD specific event scheduler without fixed counter calculation that also improves code performance in the fast path.

---
 arch/x86/kernel/cpu/perf_event.c |   36 +++++++++++++++++++++++++++++++-----
 1 files changed, 31 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/perf_event.c +31 -5
index 3f81f91..3e0fc29 100644
@@ -1407,8 +1407,8 @@ again:
 /*
  * intel-specific counter allocator: check event constraints
  */
-static int
-intel_get_event_idx(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc)
+static inline int
+__intel_get_event_idx(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc)
 {
 	const struct event_constraint *event_constraint;
 	int i, code;
@@ -1432,7 +1432,7 @@ skip:
 }
 
 static int
-x86_schedule_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc)
+intel_get_event_idx(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc)
 {
 	int idx;
 
@@ -1466,7 +1466,7 @@ x86_schedule_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc)
 		/* Try to get the previous generic event again */
 		if (idx == -1 || test_and_set_bit(idx, cpuc->used_mask)) {
 try_generic:
-			idx = x86_pmu.get_event_idx(cpuc, hwc);
+			idx = __intel_get_event_idx(cpuc, hwc);
 			if (idx == -1)
 				return -EAGAIN;
 
@@ -1479,6 +1479,32 @@ try_generic:
 	return idx;
 }
 
+static int
+amd_get_event_idx(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc)
+{
+	int idx;
+
+	idx = hwc->idx;
+	/* Try to get the previous generic event again */
+	if (idx == -1 || test_and_set_bit(idx, cpuc->used_mask)) {
+		idx = gen_get_event_idx(cpuc, hwc);
+		if (idx == -1)
+			return -EAGAIN;
+
+		hwc->idx = idx;
+	}
+	hwc->config_base = x86_pmu.eventsel;
+	hwc->event_base  = x86_pmu.perfctr;
+
+	return idx;
+}
+
+static int
+x86_schedule_event(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc)
+{
+	return x86_pmu.get_event_idx(cpuc, hwc);
+}
+
 /*
  * Find a PMC slot for the freshly enabled / scheduled in event:
  */
@@ -2008,7 +2034,7 @@ static __initconst struct x86_pmu amd_pmu = {
 	.apic			= 1,
 	/* use highest bit to detect overflow */
 	.max_period		= (1ULL << 47) - 1,
-	.get_event_idx		= gen_get_event_idx,
+	.get_event_idx		= amd_get_event_idx,
 };
 
 static __init int p6_pmu_init(void)
Signature · 1 line
1.6.6
#11 lore raw

PATCH 11/12 perf/core, x86: make event_constraints a member of struct x86_pmu

In reply to Robert Richter

The event_constraints describe the x86 pmu and thus are better located in struct x86_pmu than in a separate static variable. This is also necessary to generalize some model specific functions such as intel_get_event_idx().

---
 arch/x86/kernel/cpu/perf_event.c |   24 ++++++++++--------------
 1 files changed, 10 insertions(+), 14 deletions(-)
arch/x86/kernel/cpu/perf_event.c +10 -14
index 3e0fc29..b893ee7 100644
@@ -123,6 +123,7 @@ struct x86_pmu {
 	void		(*disable_bts)(void);
 	int		(*get_event_idx)(struct cpu_hw_events *cpuc,
 					 struct hw_perf_event *hwc);
+	struct event_constraint *event_constraints;
 };
 
 static struct x86_pmu x86_pmu __read_mostly;
@@ -131,8 +132,6 @@ static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events) = {
 	.enabled = 1,
 };
 
-static const struct event_constraint *event_constraints;
-
 /*
  * Not sure about some of these
  */
@@ -160,7 +159,7 @@ static u64 p6_pmu_event_map(int hw_event)
  */
 #define P6_NOP_EVENT			0x0000002EULL
 
-static const struct event_constraint intel_p6_event_constraints[] =
+static struct event_constraint intel_p6_event_constraints[] =
 {
 	EVENT_CONSTRAINT(0xc1, 0x1),	/* FLOPS */
 	EVENT_CONSTRAINT(0x10, 0x1),	/* FP_COMP_OPS_EXE */
@@ -185,7 +184,7 @@ static const u64 intel_perfmon_event_map[] =
   [PERF_COUNT_HW_BUS_CYCLES]		= 0x013c,
 };
 
-static const struct event_constraint intel_core_event_constraints[] =
+static struct event_constraint intel_core_event_constraints[] =
 {
 	EVENT_CONSTRAINT(0x10, 0x1),	/* FP_COMP_OPS_EXE */
 	EVENT_CONSTRAINT(0x11, 0x2),	/* FP_ASSIST */
@@ -199,7 +198,7 @@ static const struct event_constraint intel_core_event_constraints[] =
 	EVENT_CONSTRAINT_END
 };
 
-static const struct event_constraint intel_nehalem_event_constraints[] =
+static struct event_constraint intel_nehalem_event_constraints[] =
 {
 	EVENT_CONSTRAINT(0x40, 0x3),	/* L1D_CACHE_LD */
 	EVENT_CONSTRAINT(0x41, 0x3),	/* L1D_CACHE_ST */
@@ -1413,12 +1412,12 @@ __intel_get_event_idx(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc)
 	const struct event_constraint *event_constraint;
 	int i, code;
 
-	if (!event_constraints)
+	if (!x86_pmu.event_constraints)
 		goto skip;
 
 	code = hwc->config & ARCH_PERFMON_EVENTSEL_EVENT;
 
-	for_each_event_constraint(event_constraint, event_constraints) {
+	for_each_event_constraint(event_constraint, x86_pmu.event_constraints) {
 		if (code == event_constraint->code) {
 			for_each_bit(i, event_constraint->idxmsk, X86_PMC_IDX_MAX) {
 				if (!test_and_set_bit(i, cpuc->used_mask))
@@ -1990,6 +1989,7 @@ static __initconst struct x86_pmu p6_pmu = {
 	.cntval_bits		= 32,
 	.cntval_mask		= (1ULL << 32) - 1,
 	.get_event_idx		= intel_get_event_idx,
+	.event_constraints	= intel_p6_event_constraints,
 };
 
 static __initconst struct x86_pmu intel_pmu = {
@@ -2047,12 +2047,8 @@ static __init int p6_pmu_init(void)
 	case 7:
 	case 8:
 	case 11: /* Pentium III */
-		event_constraints = intel_p6_event_constraints;
-		break;
 	case 9:
-	case 13:
-		/* Pentium M */
-		event_constraints = intel_p6_event_constraints;
+	case 13: /* Pentium M */
 		break;
 	default:
 		pr_cont("unsupported p6 CPU model %d ",
@@ -2118,14 +2114,14 @@ static __init int intel_pmu_init(void)
 		       sizeof(hw_cache_event_ids));
 
 		pr_cont("Core2 events, ");
-		event_constraints = intel_core_event_constraints;
+		x86_pmu.event_constraints = intel_core_event_constraints;
 		break;
 	default:
 	case 26:
 		memcpy(hw_cache_event_ids, nehalem_hw_cache_event_ids,
 		       sizeof(hw_cache_event_ids));
 
-		event_constraints = intel_nehalem_event_constraints;
+		x86_pmu.event_constraints = intel_nehalem_event_constraints;
 		pr_cont("Nehalem/Corei7 events, ");
 		break;
 	case 28:
Signature · 1 line
1.6.6
#12 lore raw

PATCH 12/12 perf/core, x86: make event constraint handler generic

In reply to Robert Richter

This patch makes the handler for event constraints that was implemented for Intel pmus general for all models. Now, an event constraint table can be implemented and used also for AMD models.

---
 arch/x86/kernel/cpu/perf_event.c |   43 +++++++++++++++-----------------------
 1 files changed, 17 insertions(+), 26 deletions(-)
arch/x86/kernel/cpu/perf_event.c +17 -26
index b893ee7..ac2d3a7 100644
@@ -1387,47 +1387,38 @@ static int fixed_mode_idx(struct hw_perf_event *hwc)
 }
 
 /*
- * generic counter allocator: get next free counter
+ * generic counter allocator
  */
 static int
 gen_get_event_idx(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc)
 {
-	int idx;
-
-again:
-	idx = find_first_zero_bit(cpuc->used_mask, x86_pmu.num_counters);
-	if (idx == x86_pmu.num_counters)
-		return -1;
-	if (test_and_set_bit(idx, cpuc->used_mask))
-		goto again;
-	return idx;
-}
-
-/*
- * intel-specific counter allocator: check event constraints
- */
-static inline int
-__intel_get_event_idx(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc)
-{
 	const struct event_constraint *event_constraint;
-	int i, code;
+	int idx, code;
 
 	if (!x86_pmu.event_constraints)
-		goto skip;
+		goto get_next_free;
 
 	code = hwc->config & ARCH_PERFMON_EVENTSEL_EVENT;
 
+	/* check event constraints */
 	for_each_event_constraint(event_constraint, x86_pmu.event_constraints) {
 		if (code == event_constraint->code) {
-			for_each_bit(i, event_constraint->idxmsk, X86_PMC_IDX_MAX) {
-				if (!test_and_set_bit(i, cpuc->used_mask))
-					return i;
+			for_each_bit(idx, event_constraint->idxmsk, X86_PMC_IDX_MAX) {
+				if (!test_and_set_bit(idx, cpuc->used_mask))
+					return idx;
 			}
 			return -1;
 		}
 	}
-skip:
-	return gen_get_event_idx(cpuc, hwc);
+
+get_next_free:
+	/* get next free counter */
+	idx = find_first_zero_bit(cpuc->used_mask, x86_pmu.num_counters);
+	if (idx == x86_pmu.num_counters)
+		return -1;
+	if (test_and_set_bit(idx, cpuc->used_mask))
+		goto get_next_free;
+	return idx;
 }
 
 static int
@@ -1465,7 +1456,7 @@ intel_get_event_idx(struct cpu_hw_events *cpuc, struct hw_perf_event *hwc)
 		/* Try to get the previous generic event again */
 		if (idx == -1 || test_and_set_bit(idx, cpuc->used_mask)) {
 try_generic:
-			idx = __intel_get_event_idx(cpuc, hwc);
+			idx = gen_get_event_idx(cpuc, hwc);
 			if (idx == -1)
 				return -EAGAIN;
 
Signature · 1 line
1.6.6