Skip to contentSkip to conversation

P4 PMU early draft

RFC In review Cyrill Gorcunov 6 participants 20 messages Feb 8, 2010 – Feb 26, 2010

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

RFC 1/1 P4 PMU early draft

Hi all,

first of all the patches are NOT for any kind of inclusion. It's not ready yet. More likely I'm asking for glance review, ideas, criticism.

The main problem in implementing P4 PMU is that it has much more restrictions for event to MSR mapping. So to fit into current perf_events model I made the following:

1) Event representation. P4 uses a tuple of ESCR+CCCR+COUNTER as an "event". Since every CCCR register mapped directly to counter itself and ESCR and CCCR uses only 32bits of their appropriate MSRs, I decided to use "packed" config in in hw_perf_event::config. So that upper 31 bits are ESCR and lower 32 bits are CCCR values. The bit 64 is for HT flag.

So the base idea here is to pack into 64bit hw_perf_event::config as much info as possible.

Due to difference in bitfields I needed to implement hw_perf_event::config helper which unbind hw_perf_event::config field from processor specifics and allow to use it in P4 PMU.

2) ESCR mapping. Since ESCR MSRs are not sequential in address space I introduced a kind of "event template" structure which contains MSR for particular event. This MSRs are tupled in array for 2 values and bind to CCCR index so that HT machine with thread 0 should pick first entry and thread 1 -- second entry.

If HT is disabled or there is an absence of HT support -- we just use only first counter all the time.

Ideally would be to modify x86_pmu field values that way that in case of absence of HT we may use all 18 counters at once. Actually there is erratum pointing out that CCCR1 may not have "enable" bit properly working so we should just not use it at all. Ie we may use 17 counters at once.

3) Dependant events. Some events (such as counting retired instructions) requires additional "upstream" counter to be armed (for retired instructions this is tagging FSB). Not sure how to handle it properly. Perhaps at moment of scheduling events in we may check out if there is dependant event and then we try to reserve the counter.

4) Cascading counters. Well, I suppose we may just not support it for some time.

5) Searching the events. Event templates contain (among other fields) "opcode" which is just "ESCR event + CCCR selector" packed so every time I need to find out restriction for particular event I need to walk over templates array and check for same opcode. I guess it will be too slow (well, at moment there a few events, but as only array grow it may become a problem, also iirc some opcodes may intersect).

So the scheme I had in mind was like that:

1) As only caller asking for some generalized event we pack ESCR+CCCR into "config", check for HT support and put HT bit into config as well.

2) As events go to schedule for execution need to find out proper hw_perf_event::config into proper template and set proper hw_perf_event::idx (which is just CCCR MSR number\offset) from template. Mark this template as borrowed in global p4_pmu_config space. This allow to find out quickly which ESCR MSR is to be used in event enabling routing.

Schematically this mapping is

	hw_perf_event::config -> template -> hw_perf_event::idx

And back at moment of event enabling

	hw_perf_event::idx -> p4_pmu_config[idx] -> template -> ESCR MSR

3) I've started unbinding x86_schedule_events into per x86_pmu::schedule_events and there I hit hardness in binding HT bit. Have to think...

All in one -- If you have some spare minutes, please take a glance. I can't say I like this code -- it's overcomplicated and I fear hard to understand. and still a bit buggy. Complains are welcome!

So any idea would be great. I'm fine in just DROPPING this approach if we may make code simplier. Perhaps I used wrong way from the very beginning.

Stephane, I know you've been (and still is, right?) working on P4 driver for libpmu (not sure if I remember correctly this name). Perhaps we should take some ideas from there?

Ideally would be to track all information hw_perf_event needs in this stucture itself (but I fear it's already bloated too much) and as only event is going "schedule in" -- we may check if events have using non-intersected ESCR+CCCR.

Also I fear I have no that much spare time for this (quite interesting) task, so if someone would like to beat me on this and find some code snippets from this patches usefull -- feel free to take any code snippet you like, though I doubt :-)

-- Cyrill
---
perf,x86: Introduce hw_config helper into x86_pmu

In case if a particular processor (say Netburst) has a completely different way of configuration we may use the separate hw_config helper to unbind processor specifics from __hw_perf_event_init.

Note that the test of BTS support has been changed for attr->exclude_kernel for the very same reason.

---
 arch/x86/kernel/cpu/perf_event.c |   42 ++++++++++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 14 deletions(-)
arch/x86/kernel/cpu/perf_event.c +28 -14
Index: linux-2.6.git/arch/x86/kernel/cpu/perf_event.c
=====================================================================
@@ -139,6 +139,7 @@ struct x86_pmu {
 	int		apic;
 	u64		max_period;
 	u64		intel_ctrl;
+	int		(*hw_config)(struct perf_event_attr *attr, struct hw_perf_event *hwc);
 	void		(*enable_bts)(u64 config);
 	void		(*disable_bts)(void);
 
@@ -923,6 +924,25 @@ static void release_pmc_hardware(void)
 #endif
 }
 
+static int intel_hw_config(struct perf_event_attr *attr, struct hw_perf_event *hwc)
+{
+	/*
+	 * Generate PMC IRQs:
+	 * (keep 'enabled' bit clear for now)
+	 */
+	hwc->config = ARCH_PERFMON_EVENTSEL_INT;
+
+	/*
+	 * Count user and OS events unless requested not to
+	 */
+	if (!attr->exclude_user)
+		hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
+	if (!attr->exclude_kernel)
+		hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
+
+	return 0;
+}
+
 static inline bool bts_available(void)
 {
 	return x86_pmu.enable_bts != NULL;
@@ -1136,23 +1156,13 @@ static int __hw_perf_event_init(struct p
 
 	event->destroy = hw_perf_event_destroy;
 
-	/*
-	 * Generate PMC IRQs:
-	 * (keep 'enabled' bit clear for now)
-	 */
-	hwc->config = ARCH_PERFMON_EVENTSEL_INT;
-
 	hwc->idx = -1;
 	hwc->last_cpu = -1;
 	hwc->last_tag = ~0ULL;
 
-	/*
-	 * Count user and OS events unless requested not to.
-	 */
-	if (!attr->exclude_user)
-		hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
-	if (!attr->exclude_kernel)
-		hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
+	/* Processor specifics */
+	if (x86_pmu.hw_config(attr, hwc))
+		return -EOPNOTSUPP;
 
 	if (!hwc->sample_period) {
 		hwc->sample_period = x86_pmu.max_period;
@@ -1204,7 +1214,7 @@ static int __hw_perf_event_init(struct p
 			return -EOPNOTSUPP;
 
 		/* BTS is currently only allowed for user-mode. */
-		if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
+		if (!attr->exclude_kernel)
 			return -EOPNOTSUPP;
 	}
 
@@ -2371,6 +2381,7 @@ static __initconst struct x86_pmu p6_pmu
 	.max_events		= ARRAY_SIZE(p6_perfmon_event_map),
 	.apic			= 1,
 	.max_period		= (1ULL << 31) - 1,
+	.hw_config		= intel_hw_config,
 	.version		= 0,
 	.num_events		= 2,
 	/*
@@ -2405,6 +2416,7 @@ static __initconst struct x86_pmu core_p
 	 * the generic event period:
 	 */
 	.max_period		= (1ULL << 31) - 1,
+	.hw_config		= intel_hw_config,
 	.get_event_constraints	= intel_get_event_constraints,
 	.event_constraints	= intel_core_event_constraints,
 };
@@ -2428,6 +2440,7 @@ static __initconst struct x86_pmu intel_
 	 * the generic event period:
 	 */
 	.max_period		= (1ULL << 31) - 1,
+	.hw_config		= intel_hw_config,
 	.enable_bts		= intel_pmu_enable_bts,
 	.disable_bts		= intel_pmu_disable_bts,
 	.get_event_constraints	= intel_get_event_constraints
@@ -2451,6 +2464,7 @@ static __initconst struct x86_pmu amd_pm
 	.apic			= 1,
 	/* use highest bit to detect overflow */
 	.max_period		= (1ULL << 47) - 1,
+	.hw_config		= intel_hw_config,
 	.get_event_constraints	= amd_get_event_constraints
 };

=====================================================================

perf,x86: Introduce minimal Netburst PMU driver

---
 arch/x86/include/asm/perf_p4.h   |  672 +++++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/cpu/perf_event.c |  386 +++++++++++++++++++++-
 2 files changed, 1050 insertions(+), 8 deletions(-)
arch/x86/include/asm/perf_p4.h new file +672 -0

672 lines not on this page · raw message

arch/x86/kernel/cpu/perf_event.c +378 -8

459 lines not on this page · raw message

#1 lore raw
Quoted 2 lines · Cyrill Gorcunov

Hi all,

...

Quoted 4 lines

All in one -- If you have some spare minutes, please take a glance. I can't say I like this code -- it's overcomplicated and I fear hard to understand. and still a bit buggy. Complains are welcome!

...

Just updated some snippets, here is an interdiff on top of previous post (just to not post too much). The key moment is to use cpu in new x86_pmu.schedule_events routine. This will allow to find if hw_perf_event::config has been migrated to a different logical cpu if HT is turned on. On non-HT machine it will have no effect. As result we should swap the ESCR+CCCR thread specific flags I think.

-- Cyrill
---

arch/x86/include/asm/perf_p4.h +3 -2
diff -u linux-2.6.git/arch/x86/include/asm/perf_p4.h linux-2.6.git/arch/x86/include/asm/perf_p4.h
@@ -120,7 +120,7 @@
 	return !!((P4_EVENT_UNPACK_SELECTOR(config)) & P4_CCCR_CASCADE);
 }
 
-static inline bool p4_is_ht_slave(u64 config)
+static inline int p4_is_ht_slave(u64 config)
 {
 	return !!(config & P4_CONFIG_HT);
 }
@@ -146,7 +146,8 @@
 static inline int p4_ht_thread(int cpu)
 {
 #ifdef CONFIG_SMP
-	return cpu != cpumask_first(__get_cpu_var(cpu_sibling_map));
+	if (smp_num_siblings == 2)
+		return cpu != cpumask_first(__get_cpu_var(cpu_sibling_map));
 #endif
 	return 0;
 }
arch/x86/kernel/cpu/perf_event.c +32 -21

126 lines not on this page · raw message

#2 lore raw
In reply to Cyrill Gorcunov
Quoted 4 lines · Cyrill Gorcunov

Hi all,

first of all the patches are NOT for any kind of inclusion. It's not ready yet. More likely I'm asking for glance review, ideas, criticism.

A quick question: does the code produce something on a real P4? (possibly only running with a single event - but even that would be enough.)

Quoted 2 lines

The main problem in implementing P4 PMU is that it has much more restrictions for event to MSR mapping. [...]

One possibly simpler approach might be to represent the P4 PMU via a maximum _two_ generic events only.

Last i looked at the many P4 events, i've noticed that generally you can create any two events. (with a few exceptions) Once you start trying to take advantage of the more than a dozen seemingly separate counters, additional non-trivial constraints apply.

So if we only allowed a maximum of _two_ generic events (like say a simple Core2 has, so it's not a big restriction at all), we wouldnt have to map all the constraints, we'd only have to encode the specific event-to-MSR details. (which alone is quite a bit of work as well.)

We could also use the new constraints code to map them all, of course - it will certainly be more complex to implement.

Hm?

Ingo

#3 lore raw
Quoted 11 lines · Ingo Molnar
Quoted 5 lines · Cyrill Gorcunov

Hi all,

first of all the patches are NOT for any kind of inclusion. It's not ready

yet. More likely I'm asking for glance review, ideas, criticism.

A quick question: does the code produce something on a real P4? (possibly only running with a single event - but even that would be enough.)

not yet, a few code snippets need to be added into scheduling routine, hope to finish this today evening

Quoted 7 lines
Quoted 3 lines

The main problem in implementing P4 PMU is that it has much more restrictions for event to MSR mapping. [...]

One possibly simpler approach might be to represent the P4 PMU via a maximum _two_ generic events only.

yeah, good idea!

Quoted 5 lines

Last i looked at the many P4 events, i've noticed that generally you can create any two events. (with a few exceptions) Once you start trying to take advantage of the more than a dozen seemingly separate counters, additional non-trivial constraints apply.

Quoted 8 lines

So if we only allowed a maximum of _two_ generic events (like say a simple Core2 has, so it's not a big restriction at all), we wouldnt have to map all the constraints, we'd only have to encode the specific event-to-MSR details. (which alone is quite a bit of work as well.)

We could also use the new constraints code to map them all, of course - it will certainly be more complex to implement.

i thought about it, but i didn't like the result code ;) will think about it.

Quoted 4 lines

Hm?

Ingo

#4 lore raw
In reply to Ingo Molnar
Quoted 11 lines · Ingo Molnar
Quoted 4 lines · Cyrill Gorcunov

Hi all,

first of all the patches are NOT for any kind of inclusion. It's not ready yet. More likely I'm asking for glance review, ideas, criticism.

A quick question: does the code produce something on a real P4? (possibly only running with a single event - but even that would be enough.)

Ingo, here is updated version. NOT TESTED completely, I have only Core2 laptops and pretty old AMD Athlon box somewhere in closed /not sure if it still works/ :) But it's supposed to handle 7 generalized events, except retired instructions counting which requires upstream MSR programming (dependant events). Not hard to implement but I didn't manage today.

Quoted 6 lines
Quoted 2 lines

The main problem in implementing P4 PMU is that it has much more restrictions for event to MSR mapping. [...]

One possibly simpler approach might be to represent the P4 PMU via a maximum _two_ generic events only.

By two generic events you mean events generalized as PERF_COUNT_HW_CPU_CYCLES and friends, or you mean .num_event field which restrict numbers of simultaneously counting events?

Quoted 5 lines

Last i looked at the many P4 events, i've noticed that generally you can create any two events. (with a few exceptions) Once you start trying to take advantage of the more than a dozen seemingly separate counters, additional non-trivial constraints apply.

Hmm... I need to analize MSR matrix more close. Also it seems I forgot about matrix of tags, we may need them one day. Sigh...

Quoted 5 lines

So if we only allowed a maximum of _two_ generic events (like say a simple Core2 has, so it's not a big restriction at all), we wouldnt have to map all the constraints, we'd only have to encode the specific event-to-MSR details. (which alone is quite a bit of work as well.)

Yes, the scheme would be pretty simple and fast (if I understand you right). Two events -- two possible templates (or four combinations in HT case)

Quoted 2 lines

We could also use the new constraints code to map them all, of course - it will certainly be more complex to implement.

Yes, I will need to encode ESCR MSR's to constraints somehow. The current perf_event code with event_constraint is pretty elegant so I'll think how to manage p4 model into it :)

Quoted 5 lines

Hm?

Ingo

So the (both) patches are below (not sure if LKML allow to pass that big patches). Note that it's COMPLETELY untested and I have a gut feeling it will fails at first run, but getting backtrace will help :)

As I mentioned I don't have this kind of cpu neither much time for this task though I suppose it's not urgent and we may do it in a small steps. (and just to justify myself somehow -- I know that code is look ugly and
it's but nothing better comes to mind at moment :)

All-on-one: please take a look but don't waste too much time on it. Complains (which is more important for me!) are welcome!!! I hope I didn't MESS too much.

-- Cyrill
---
perf,x86: Introduce hw_config helper into x86_pmu

In case if a particular processor (say Netburst) has a completely different way of configuration we may use the separate hw_config helper to unbind processor specifics from __hw_perf_event_init.

Note that the test of BTS support has been changed for attr->exclude_kernel for the very same reason.

---
 arch/x86/kernel/cpu/perf_event.c |   42 ++++++++++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 14 deletions(-)
arch/x86/kernel/cpu/perf_event.c +28 -15
Index: linux-2.6.git/arch/x86/kernel/cpu/perf_event.c
=====================================================================
@@ -139,6 +139,7 @@ struct x86_pmu {
 	int		apic;
 	u64		max_period;
 	u64		intel_ctrl;
+	int		(*hw_config)(struct perf_event_attr *attr, struct hw_perf_event *hwc);
 	void		(*enable_bts)(u64 config);
 	void		(*disable_bts)(void);
 
@@ -923,6 +924,25 @@ static void release_pmc_hardware(void)
 #endif
 }
 
+static int intel_hw_config(struct perf_event_attr *attr, struct hw_perf_event *hwc)
+{
+	/*
+	 * Generate PMC IRQs:
+	 * (keep 'enabled' bit clear for now)
+	 */
+	hwc->config = ARCH_PERFMON_EVENTSEL_INT;
+
+	/*
+	 * Count user and OS events unless requested not to
+	 */
+	if (!attr->exclude_user)
+		hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
+	if (!attr->exclude_kernel)
+		hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
+
+	return 0;
+}
+
 static inline bool bts_available(void)
 {
 	return x86_pmu.enable_bts != NULL;
@@ -1136,23 +1156,13 @@ static int __hw_perf_event_init(struct p
 
 	event->destroy = hw_perf_event_destroy;
 
-	/*
-	 * Generate PMC IRQs:
-	 * (keep 'enabled' bit clear for now)
-	 */
-	hwc->config = ARCH_PERFMON_EVENTSEL_INT;
-
 	hwc->idx = -1;
 	hwc->last_cpu = -1;
 	hwc->last_tag = ~0ULL;
 
-	/*
-	 * Count user and OS events unless requested not to.
-	 */
-	if (!attr->exclude_user)
-		hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
-	if (!attr->exclude_kernel)
-		hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
+	/* Processor specifics */
+	if (x86_pmu.hw_config(attr, hwc))
+		return -EOPNOTSUPP;
 
 	if (!hwc->sample_period) {
 		hwc->sample_period = x86_pmu.max_period;
@@ -1204,7 +1214,7 @@ static int __hw_perf_event_init(struct p
 			return -EOPNOTSUPP;
 
 		/* BTS is currently only allowed for user-mode. */
-		if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
+		if (!attr->exclude_kernel)
 			return -EOPNOTSUPP;
 	}
 
@@ -2371,6 +2381,7 @@ static __initconst struct x86_pmu p6_pmu
 	.max_events		= ARRAY_SIZE(p6_perfmon_event_map),
 	.apic			= 1,
 	.max_period		= (1ULL << 31) - 1,
+	.hw_config		= intel_hw_config,
 	.version		= 0,
 	.num_events		= 2,
 	/*
@@ -2405,6 +2416,7 @@ static __initconst struct x86_pmu core_p
 	 * the generic event period:
 	 */
 	.max_period		= (1ULL << 31) - 1,
+	.hw_config		= intel_hw_config,
 	.get_event_constraints	= intel_get_event_constraints,
 	.event_constraints	= intel_core_event_constraints,
 };
@@ -2428,6 +2440,7 @@ static __initconst struct x86_pmu intel_
 	 * the generic event period:
 	 */
 	.max_period		= (1ULL << 31) - 1,
+	.hw_config		= intel_hw_config,
 	.enable_bts		= intel_pmu_enable_bts,
 	.disable_bts		= intel_pmu_disable_bts,
 	.get_event_constraints	= intel_get_event_constraints
@@ -2451,6 +2464,7 @@ static __initconst struct x86_pmu amd_pm
 	.apic			= 1,
 	/* use highest bit to detect overflow */
 	.max_period		= (1ULL << 47) - 1,
+	.hw_config		= intel_hw_config,
 	.get_event_constraints	= amd_get_event_constraints
 };
---

perf,x86: Introduce minimal Netburst PMU driver v2

This is a first approach into functional P4 PMU perf_event driver.

Restrictions:

- No cascaded counters support
- No dependant events support (so PERF_COUNT_HW_INSTRUCTIONS is turned off for now)
- PERF_COUNT_HW_CACHE_REFERENCES on thread 1 issue warning and may not trigger IRQ

Completely not tested :)

---
 arch/x86/include/asm/perf_p4.h   |  678 +++++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/cpu/perf_event.c |  553 +++++++++++++++++++++++++++++++
 2 files changed, 1221 insertions(+), 10 deletions(-)
arch/x86/include/asm/perf_p4.h new file +678 -0

678 lines not on this page · raw message

arch/x86/kernel/cpu/perf_event.c +543 -10

644 lines not on this page · raw message

maintainer
#5 lore raw
Quoted 40 lines · Cyrill Gorcunov

Index: linux-2.6.git/arch/x86/kernel/cpu/perf_event.c
=====================================================================

arch/x86/kernel/cpu/perf_event.c +11 -1
@@ -26,6 +26,7 @@
 #include <linux/bitops.h>
 
 #include <asm/apic.h>
+#include <asm/perf_p4.h>
 #include <asm/stacktrace.h>
 #include <asm/nmi.h>
 
@@ -140,6 +141,7 @@ struct x86_pmu {
 	u64		max_period;
 	u64		intel_ctrl;
 	int		(*hw_config)(struct perf_event_attr *attr, struct hw_perf_event *hwc);
+	int		(*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign, int cpu);
 	void		(*enable_bts)(u64 config);
 	void		(*disable_bts)(void);
 
 
+/*
+ * This is the most important routine of Netburst PMU actually
+ * and need a huge speedup!
+ */
+static int p4_pmu_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign, int cpu)
+{
 
+}
 
 
-static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign)
+/* we don't use cpu argument here at all */
+static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int *assign, int cpu)
 {
 	struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
 	unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];

arch/x86/kernel/cpu/perf_event.c static int x86_pmu_enable(struct perf_ev · line 2311

Quoted 8 lines
arch/x86/kernel/cpu/perf_event.c +1 -1
@@ -1796,7 +2305,7 @@ static int x86_pmu_enable(struct perf_ev
 	if (n < 0)
 		return n;
 
-	ret = x86_schedule_events(cpuc, n, assign);
+	ret = x86_pmu.schedule_events(cpuc, n, assign, 0);
 	if (ret)
 		return ret;
 	/*

This look like a bug, surely we can run on !cpu0.

arch/x86/kernel/cpu/perf_event.c int hw_perf_group_sched_in(struct perf_e · line 2828

Quoted 9 lines
arch/x86/kernel/cpu/perf_event.c +1 -1
@@ -2313,7 +2822,7 @@ int hw_perf_group_sched_in(struct perf_e
 	if (n0 < 0)
 		return n0;
 
-	ret = x86_schedule_events(cpuc, n0, assign);
+	ret = x86_pmu.schedule_events(cpuc, n0, assign, cpu);
 	if (ret)
 		return ret;
 

I'd try BUG_ON(cpu != smp_processor_id()) and scrap passing that cpu thing around.

Quoted 17 lines
arch/x86/kernel/cpu/perf_event.c +2 -1
@@ -2700,6 +3232,7 @@ static int validate_group(struct perf_ev
 {
 	struct perf_event *leader = event->group_leader;
 	struct cpu_hw_events *fake_cpuc;
+	int cpu = smp_processor_id();
 	int ret, n;
 
 	ret = -ENOMEM;
@@ -2725,7 +3258,7 @@ static int validate_group(struct perf_ev
 
 	fake_cpuc->n_events = n;
 
-	ret = x86_schedule_events(fake_cpuc, n, NULL);
+	ret = x86_pmu.schedule_events(fake_cpuc, n, NULL, cpu);
 
 out_free:
 	kfree(fake_cpuc);
#6 lore raw
Quoted 51 lines · Peter Zijlstra
Quoted 48 lines · Cyrill Gorcunov

Index: linux-2.6.git/arch/x86/kernel/cpu/perf_event.c
=====================================================================

arch/x86/kernel/cpu/perf_event.c +2 -0
@@ -26,6 +26,7 @@
 #include <linux/bitops.h>
 
 #include <asm/apic.h>
+#include <asm/perf_p4.h>
 #include <asm/stacktrace.h>
 #include <asm/nmi.h>
 
@@ -140,6 +141,7 @@ struct x86_pmu {
 	u64		max_period;
 	u64		intel_ctrl;
 	int		(*hw_config)(struct perf_event_attr *attr, struct hw_perf_event
 *hwc);
+	int		(*schedule_events)(struct cpu_hw_events *cpuc, int n, int *assign,
 int cpu);
 	void		(*enable_bts)(u64 config);
arch/x86/kernel/cpu/perf_event.c +5 -0
 	void		(*disable_bts)(void);
 
 
+/*
+ * This is the most important routine of Netburst PMU actually
+ * and need a huge speedup!
+ */
+static int p4_pmu_schedule_events(struct cpu_hw_events *cpuc, int n, int

*assign, int cpu)

arch/x86/kernel/cpu/perf_event.c +2 -1
+{
 
+}
 
 
-static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int

*assign)

arch/x86/kernel/cpu/perf_event.c +2 -0
+/* we don't use cpu argument here at all */
+static int x86_schedule_events(struct cpu_hw_events *cpuc, int n, int

*assign, int cpu)
{

arch/x86/kernel/cpu/perf_event.c +0 -0
 	struct event_constraint *c, *constraints[X86_PMC_IDX_MAX];
 	unsigned long used_mask[BITS_TO_LONGS(X86_PMC_IDX_MAX)];
@@ -1796,7 +2305,7 @@ static int x86_pmu_enable(struct perf_ev
 	if (n < 0)
 		return n;
 
Quoted 7 lines

arch/x86/kernel/cpu/perf_event.c static int x86_pmu_enable(struct perf_ev · line 2311

Quoted 5 lines
arch/x86/kernel/cpu/perf_event.c +1 -1
-	ret = x86_schedule_events(cpuc, n, assign);
+	ret = x86_pmu.schedule_events(cpuc, n, assign, 0);
 	if (ret)
 		return ret;
 	/*

This look like a bug, surely we can run on !cpu0.

Definitely! Thanks! It should be smp_processor_id()

Quoted 5 lines
Quoted 5 lines
arch/x86/kernel/cpu/perf_event.c +0 -0
@@ -2313,7 +2822,7 @@ int hw_perf_group_sched_in(struct perf_e
 	if (n0 < 0)
 		return n0;
 
Quoted 9 lines

arch/x86/kernel/cpu/perf_event.c int hw_perf_group_sched_in(struct perf_e · line 2828

Quoted 5 lines
arch/x86/kernel/cpu/perf_event.c +1 -1
-	ret = x86_schedule_events(cpuc, n0, assign);
+	ret = x86_pmu.schedule_events(cpuc, n0, assign, cpu);
 	if (ret)
 		return ret;
 

I'd try BUG_ON(cpu != smp_processor_id()) and scrap passing that cpu thing around.

no, i need cpu to find out if event has migrated from other thread and then i switch
some thread dependant flags in hw::config (ie escr and cccr), or i miss something and events in one cpu just can't migrate to another cpu?

Quoted 20 lines
Quoted 17 lines
arch/x86/kernel/cpu/perf_event.c +2 -1
@@ -2700,6 +3232,7 @@ static int validate_group(struct perf_ev
 {
 	struct perf_event *leader = event->group_leader;
 	struct cpu_hw_events *fake_cpuc;
+	int cpu = smp_processor_id();
 	int ret, n;
 
 	ret = -ENOMEM;
@@ -2725,7 +3258,7 @@ static int validate_group(struct perf_ev
 
 	fake_cpuc->n_events = n;
 
-	ret = x86_schedule_events(fake_cpuc, n, NULL);
+	ret = x86_pmu.schedule_events(fake_cpuc, n, NULL, cpu);
 
 out_free:
 	kfree(fake_cpuc);
maintainer
#7 lore raw
Quoted 9 lines · Cyrill Gorcunov
Quoted 3 lines

I'd try BUG_ON(cpu != smp_processor_id()) and scrap passing that cpu thing around.

no, i need cpu to find out if event has migrated from other thread and then i switch
some thread dependant flags in hw::config (ie escr and cccr), or i miss something and events in one cpu just can't migrate to another cpu?

Well, if we validate that cpu == smp_processor_id() (looking at kernel/perf_event.c that does indeed seem true for hw_perf_group_sched_in() -- which suggests we should simply remove that cpu argument), and that cpu will stay constant throughout the whole callchain (it does, its a local variable), we can remove it and substitute smp_processor_id(), right?

As to migration of the event, its tied to a task, we're now installing the event for a task it wouldn't make sense to allow that to be preemptible.

#8 lore raw
Quoted 11 lines · Peter Zijlstra
Quoted 9 lines · Cyrill Gorcunov
Quoted 3 lines

I'd try BUG_ON(cpu != smp_processor_id()) and scrap passing that cpu thing around.

no, i need cpu to find out if event has migrated from other thread and then i switch
some thread dependant flags in hw::config (ie escr and cccr), or i miss something and events in one cpu just can't migrate to another cpu?

Quoted 7 lines

Well, if we validate that cpu == smp_processor_id() (looking at kernel/perf_event.c that does indeed seem true for hw_perf_group_sched_in() -- which suggests we should simply remove that cpu argument), and that cpu will stay constant throughout the whole callchain (it does, its a local variable), we can remove it and substitute smp_processor_id(), right?

yeah, seems so!

Quoted 5 lines

As to migration of the event, its tied to a task, we're now installing the event for a task it wouldn't make sense to allow that to be preemptible.

ok, thanks for explanation, Peter! I'll take a closer look as only get back from office.

maintainer
#9 lore raw
In reply to Peter Zijlstra
Quoted 1 line · Peter Zijlstra

which suggests we should simply remove that cpu argument

The below patch does so for all in-tree bits.

Doing this patch reminded me of the mess that is hw_perf_group_sched_in(), so I'm going to try and fix that next.

---
 arch/powerpc/kernel/perf_event.c |   10 +++++-----
 arch/sparc/kernel/perf_event.c   |   10 +++++-----
 arch/x86/kernel/cpu/perf_event.c |   18 +++++++++---------
 include/linux/perf_event.h       |    2 +-
 kernel/perf_event.c              |    4 ++--
 5 files changed, 22 insertions(+), 22 deletions(-)
arch/powerpc/kernel/perf_event.c +5 -5
Index: linux-2.6/arch/powerpc/kernel/perf_event.c
===================================================================
@@ -718,10 +718,10 @@
 	return n;
 }
 
-static void event_sched_in(struct perf_event *event, int cpu)
+static void event_sched_in(struct perf_event *event)
 {
 	event->state = PERF_EVENT_STATE_ACTIVE;
-	event->oncpu = cpu;
+	event->oncpu = smp_processor_id();
 	event->tstamp_running += event->ctx->time - event->tstamp_stopped;
 	if (is_software_event(event))
 		event->pmu->enable(event);
@@ -735,7 +735,7 @@
  */
 int hw_perf_group_sched_in(struct perf_event *group_leader,
 	       struct perf_cpu_context *cpuctx,
-	       struct perf_event_context *ctx, int cpu)
+	       struct perf_event_context *ctx)
 {
 	struct cpu_hw_events *cpuhw;
 	long i, n, n0;
@@ -766,10 +766,10 @@
 		cpuhw->event[i]->hw.config = cpuhw->events[i];
 	cpuctx->active_oncpu += n;
 	n = 1;
-	event_sched_in(group_leader, cpu);
+	event_sched_in(group_leader);
 	list_for_each_entry(sub, &group_leader->sibling_list, group_entry) {
 		if (sub->state != PERF_EVENT_STATE_OFF) {
-			event_sched_in(sub, cpu);
+			event_sched_in(sub);
 			++n;
 		}
 	}
arch/sparc/kernel/perf_event.c +5 -5
Index: linux-2.6/arch/sparc/kernel/perf_event.c
===================================================================
@@ -980,10 +980,10 @@
 	return n;
 }
 
-static void event_sched_in(struct perf_event *event, int cpu)
+static void event_sched_in(struct perf_event *event)
 {
 	event->state = PERF_EVENT_STATE_ACTIVE;
-	event->oncpu = cpu;
+	event->oncpu = smp_processor_id();
 	event->tstamp_running += event->ctx->time - event->tstamp_stopped;
 	if (is_software_event(event))
 		event->pmu->enable(event);
@@ -991,7 +991,7 @@
 
 int hw_perf_group_sched_in(struct perf_event *group_leader,
 			   struct perf_cpu_context *cpuctx,
-			   struct perf_event_context *ctx, int cpu)
+			   struct perf_event_context *ctx)
 {
 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
 	struct perf_event *sub;
@@ -1015,10 +1015,10 @@
 
 	cpuctx->active_oncpu += n;
 	n = 1;
-	event_sched_in(group_leader, cpu);
+	event_sched_in(group_leader);
 	list_for_each_entry(sub, &group_leader->sibling_list, group_entry) {
 		if (sub->state != PERF_EVENT_STATE_OFF) {
-			event_sched_in(sub, cpu);
+			event_sched_in(sub);
 			n++;
 		}
 	}
arch/x86/kernel/cpu/perf_event.c +9 -9
Index: linux-2.6/arch/x86/kernel/cpu/perf_event.c
===================================================================
@@ -2386,12 +2386,12 @@
 }
 
 static int x86_event_sched_in(struct perf_event *event,
-			  struct perf_cpu_context *cpuctx, int cpu)
+			  struct perf_cpu_context *cpuctx)
 {
 	int ret = 0;
 
 	event->state = PERF_EVENT_STATE_ACTIVE;
-	event->oncpu = cpu;
+	event->oncpu = smp_processor_id();
 	event->tstamp_running += event->ctx->time - event->tstamp_stopped;
 
 	if (!is_x86_event(event))
@@ -2407,7 +2407,7 @@
 }
 
 static void x86_event_sched_out(struct perf_event *event,
-			    struct perf_cpu_context *cpuctx, int cpu)
+			    struct perf_cpu_context *cpuctx)
 {
 	event->state = PERF_EVENT_STATE_INACTIVE;
 	event->oncpu = -1;
@@ -2435,9 +2435,9 @@
  */
 int hw_perf_group_sched_in(struct perf_event *leader,
 	       struct perf_cpu_context *cpuctx,
-	       struct perf_event_context *ctx, int cpu)
+	       struct perf_event_context *ctx)
 {
-	struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
+	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
 	struct perf_event *sub;
 	int assign[X86_PMC_IDX_MAX];
 	int n0, n1, ret;
@@ -2451,14 +2451,14 @@
 	if (ret)
 		return ret;
 
-	ret = x86_event_sched_in(leader, cpuctx, cpu);
+	ret = x86_event_sched_in(leader, cpuctx);
 	if (ret)
 		return ret;
 
 	n1 = 1;
 	list_for_each_entry(sub, &leader->sibling_list, group_entry) {
 		if (sub->state > PERF_EVENT_STATE_OFF) {
-			ret = x86_event_sched_in(sub, cpuctx, cpu);
+			ret = x86_event_sched_in(sub, cpuctx);
 			if (ret)
 				goto undo;
 			++n1;
@@ -2483,11 +2483,11 @@
 	 */
 	return 1;
 undo:
-	x86_event_sched_out(leader, cpuctx, cpu);
+	x86_event_sched_out(leader, cpuctx);
 	n0  = 1;
 	list_for_each_entry(sub, &leader->sibling_list, group_entry) {
 		if (sub->state == PERF_EVENT_STATE_ACTIVE) {
-			x86_event_sched_out(sub, cpuctx, cpu);
+			x86_event_sched_out(sub, cpuctx);
 			if (++n0 == n1)
 				break;
 		}
include/linux/perf_event.h +1 -1
Index: linux-2.6/include/linux/perf_event.h
===================================================================
@@ -768,7 +768,7 @@
 extern int perf_event_task_enable(void);
 extern int hw_perf_group_sched_in(struct perf_event *group_leader,
 	       struct perf_cpu_context *cpuctx,
-	       struct perf_event_context *ctx, int cpu);
+	       struct perf_event_context *ctx);
 extern void perf_event_update_userpage(struct perf_event *event);
 extern int perf_event_release_kernel(struct perf_event *event);
 extern struct perf_event *
kernel/perf_event.c +18 -27

147 lines not on this page · raw message

#10 lore raw
Quoted 8 lines · Peter Zijlstra
Quoted 1 line · Peter Zijlstra

which suggests we should simply remove that cpu argument

The below patch does so for all in-tree bits.

Doing this patch reminded me of the mess that is hw_perf_group_sched_in(), so I'm going to try and fix that next.

Yes Peter, thanks! At least for me this patch makes code more understandable :)

-- Cyrill

#11 lore raw

[tip:perf/core] perf_events: Simplify code by removing cpu argument to hw_perf_group_sched_in()

In reply to Peter Zijlstra
Commit-ID:  6e37738a2fac964583debe91099bc3248554f6e5
Gitweb:     http://git.kernel.org/tip/6e37738a2fac964583debe91099bc3248554f6e5
Author:     Peter Zijlstra <[email protected]>
AuthorDate: Thu, 11 Feb 2010 13:21:58 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 26 Feb 2010 10:56:53 +0100

perf_events: Simplify code by removing cpu argument to hw_perf_group_sched_in()

Since the cpu argument to hw_perf_group_sched_in() is always smp_processor_id(), simplify the code a little by removing this argument and using the current cpu where needed.

Cc David Miller <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Benjamin Herrenschmidt <[email protected]>
Cc Frederic Weisbecker <[email protected]>

LKML-Reference: <1265890918.5396.3.camel@laptop>

---
 arch/powerpc/kernel/perf_event.c |   10 ++++----
 arch/sparc/kernel/perf_event.c   |   10 ++++----
 arch/x86/kernel/cpu/perf_event.c |   18 +++++++-------
 include/linux/perf_event.h       |    2 +-
 kernel/perf_event.c              |   45 +++++++++++++++----------------------
 5 files changed, 38 insertions(+), 47 deletions(-)
arch/powerpc/kernel/perf_event.c +5 -5
index 1eb85fb..b6cf8f1 100644
@@ -718,10 +718,10 @@ static int collect_events(struct perf_event *group, int max_count,
 	return n;
 }
 
-static void event_sched_in(struct perf_event *event, int cpu)
+static void event_sched_in(struct perf_event *event)
 {
 	event->state = PERF_EVENT_STATE_ACTIVE;
-	event->oncpu = cpu;
+	event->oncpu = smp_processor_id();
 	event->tstamp_running += event->ctx->time - event->tstamp_stopped;
 	if (is_software_event(event))
 		event->pmu->enable(event);
@@ -735,7 +735,7 @@ static void event_sched_in(struct perf_event *event, int cpu)
  */
 int hw_perf_group_sched_in(struct perf_event *group_leader,
 	       struct perf_cpu_context *cpuctx,
-	       struct perf_event_context *ctx, int cpu)
+	       struct perf_event_context *ctx)
 {
 	struct cpu_hw_events *cpuhw;
 	long i, n, n0;
@@ -766,10 +766,10 @@ int hw_perf_group_sched_in(struct perf_event *group_leader,
 		cpuhw->event[i]->hw.config = cpuhw->events[i];
 	cpuctx->active_oncpu += n;
 	n = 1;
-	event_sched_in(group_leader, cpu);
+	event_sched_in(group_leader);
 	list_for_each_entry(sub, &group_leader->sibling_list, group_entry) {
 		if (sub->state != PERF_EVENT_STATE_OFF) {
-			event_sched_in(sub, cpu);
+			event_sched_in(sub);
 			++n;
 		}
 	}
arch/sparc/kernel/perf_event.c +5 -5
index e856456..9f2b2ba 100644
@@ -980,10 +980,10 @@ static int collect_events(struct perf_event *group, int max_count,
 	return n;
 }
 
-static void event_sched_in(struct perf_event *event, int cpu)
+static void event_sched_in(struct perf_event *event)
 {
 	event->state = PERF_EVENT_STATE_ACTIVE;
-	event->oncpu = cpu;
+	event->oncpu = smp_processor_id();
 	event->tstamp_running += event->ctx->time - event->tstamp_stopped;
 	if (is_software_event(event))
 		event->pmu->enable(event);
@@ -991,7 +991,7 @@ static void event_sched_in(struct perf_event *event, int cpu)
 
 int hw_perf_group_sched_in(struct perf_event *group_leader,
 			   struct perf_cpu_context *cpuctx,
-			   struct perf_event_context *ctx, int cpu)
+			   struct perf_event_context *ctx)
 {
 	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
 	struct perf_event *sub;
@@ -1015,10 +1015,10 @@ int hw_perf_group_sched_in(struct perf_event *group_leader,
 
 	cpuctx->active_oncpu += n;
 	n = 1;
-	event_sched_in(group_leader, cpu);
+	event_sched_in(group_leader);
 	list_for_each_entry(sub, &group_leader->sibling_list, group_entry) {
 		if (sub->state != PERF_EVENT_STATE_OFF) {
-			event_sched_in(sub, cpu);
+			event_sched_in(sub);
 			n++;
 		}
 	}
arch/x86/kernel/cpu/perf_event.c +9 -9
index aa12f36..ad09656 100644
@@ -2403,12 +2403,12 @@ done:
 }
 
 static int x86_event_sched_in(struct perf_event *event,
-			  struct perf_cpu_context *cpuctx, int cpu)
+			  struct perf_cpu_context *cpuctx)
 {
 	int ret = 0;
 
 	event->state = PERF_EVENT_STATE_ACTIVE;
-	event->oncpu = cpu;
+	event->oncpu = smp_processor_id();
 	event->tstamp_running += event->ctx->time - event->tstamp_stopped;
 
 	if (!is_x86_event(event))
@@ -2424,7 +2424,7 @@ static int x86_event_sched_in(struct perf_event *event,
 }
 
 static void x86_event_sched_out(struct perf_event *event,
-			    struct perf_cpu_context *cpuctx, int cpu)
+			    struct perf_cpu_context *cpuctx)
 {
 	event->state = PERF_EVENT_STATE_INACTIVE;
 	event->oncpu = -1;
@@ -2452,9 +2452,9 @@ static void x86_event_sched_out(struct perf_event *event,
  */
 int hw_perf_group_sched_in(struct perf_event *leader,
 	       struct perf_cpu_context *cpuctx,
-	       struct perf_event_context *ctx, int cpu)
+	       struct perf_event_context *ctx)
 {
-	struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu);
+	struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
 	struct perf_event *sub;
 	int assign[X86_PMC_IDX_MAX];
 	int n0, n1, ret;
@@ -2468,14 +2468,14 @@ int hw_perf_group_sched_in(struct perf_event *leader,
 	if (ret)
 		return ret;
 
-	ret = x86_event_sched_in(leader, cpuctx, cpu);
+	ret = x86_event_sched_in(leader, cpuctx);
 	if (ret)
 		return ret;
 
 	n1 = 1;
 	list_for_each_entry(sub, &leader->sibling_list, group_entry) {
 		if (sub->state > PERF_EVENT_STATE_OFF) {
-			ret = x86_event_sched_in(sub, cpuctx, cpu);
+			ret = x86_event_sched_in(sub, cpuctx);
 			if (ret)
 				goto undo;
 			++n1;
@@ -2500,11 +2500,11 @@ int hw_perf_group_sched_in(struct perf_event *leader,
 	 */
 	return 1;
 undo:
-	x86_event_sched_out(leader, cpuctx, cpu);
+	x86_event_sched_out(leader, cpuctx);
 	n0  = 1;
 	list_for_each_entry(sub, &leader->sibling_list, group_entry) {
 		if (sub->state == PERF_EVENT_STATE_ACTIVE) {
-			x86_event_sched_out(sub, cpuctx, cpu);
+			x86_event_sched_out(sub, cpuctx);
 			if (++n0 == n1)
 				break;
 		}
include/linux/perf_event.h +1 -1
index b08dfda..d0e072c 100644
@@ -772,7 +772,7 @@ extern int perf_event_task_disable(void);
 extern int perf_event_task_enable(void);
 extern int hw_perf_group_sched_in(struct perf_event *group_leader,
 	       struct perf_cpu_context *cpuctx,
-	       struct perf_event_context *ctx, int cpu);
+	       struct perf_event_context *ctx);
 extern void perf_event_update_userpage(struct perf_event *event);
 extern int perf_event_release_kernel(struct perf_event *event);
 extern struct perf_event *
kernel/perf_event.c +18 -27

147 lines not on this page · raw message

#12 lore raw
In reply to Cyrill Gorcunov
Quoted 3 lines · Cyrill Gorcunov

The main problem in implementing P4 PMU is that it has much more restrictions for event to MSR mapping. So to fit into current perf_events model I made the following:

Is there somewhere accessible on the web where I can read about the P4 PMU? I'm interested to see if the constraint representation and search I used on the POWER processors would be applicable.

Paul.

#13 lore raw
Quoted 6 lines · Paul Mackerras
Quoted 3 lines · Cyrill Gorcunov

The main problem in implementing P4 PMU is that it has much more restrictions for event to MSR mapping. So to fit into current perf_events model I made the following:

Quoted 6 lines

Is there somewhere accessible on the web where I can read about the P4 PMU? I'm interested to see if the constraint representation and search I used on the POWER processors would be applicable.

Paul.

I've been using intel sdm, from intel.com

maintainer
#14 lore raw
In reply to Paul Mackerras
Quoted 9 lines · Paul Mackerras
Quoted 3 lines · Cyrill Gorcunov

The main problem in implementing P4 PMU is that it has much more restrictions for event to MSR mapping. So to fit into current perf_events model I made the following:

Is there somewhere accessible on the web where I can read about the P4 PMU? I'm interested to see if the constraint representation and search I used on the POWER processors would be applicable.

Mostly:

http://www.intel.com/Assets/PDF/manual/253669.pdf

Section 30.8 PERFORMANCE MONITORING (PROCESSORS BASED ON INTEL NETBURST MICROARCHITECTURE)

Section 30.9 PERFORMANCE MONITORING AND INTEL HYPER- THREADING TECHNOLOGY IN PROCESSORS BASED
ON INTEL NETBURST MICROARCHITECTURE

Section A.8 PENTIUM 4 AND INTEL XEON PROCESSOR PERFORMANCE-MONITORING EVENTS

#15 lore raw
In reply to Cyrill Gorcunov

Cyrill,

Quoted 81 lines · Cyrill Gorcunov

Hi all,

first of all the patches are NOT for any kind of inclusion. It's not ready yet. More likely I'm asking for glance review, ideas, criticism.

The main problem in implementing P4 PMU is that it has much more restrictions for event to MSR mapping. So to fit into current perf_events model I made the following:

1) Event representation. P4 uses a tuple of ESCR+CCCR+COUNTER as an "event". Since every CCCR register mapped directly to counter itself and ESCR and CCCR uses only 32bits of their appropriate MSRs, I decided to use "packed" config in in hw_perf_event::config. So that upper 31 bits are ESCR and lower 32 bits are CCCR values. The bit 64 is for HT flag.

  So the base idea here is to pack into 64bit hw_perf_event::config as much info as possible.

  Due to difference in bitfields I needed to implement hw_perf_event::config helper which unbind hw_perf_event::config field from processor specifics and allow to use it in P4 PMU.

2) ESCR mapping. Since ESCR MSRs are not sequential in address space I introduced a kind of "event template" structure which contains MSR for particular event. This MSRs are tupled in array for 2 values and bind to CCCR index so that HT machine with thread 0 should pick first entry and thread 1 -- second entry.

  If HT is disabled or there is an absence of HT support -- we just use only first counter all the time.

  Ideally would be to modify x86_pmu field values that way that in case of absence of HT we may use all 18 counters at once. Actually there is erratum pointing out that CCCR1 may not have "enable" bit properly working so we should just not use it at all. Ie we may use 17 counters at once.

3)  Dependant events. Some events (such as counting retired instructions) requires additional "upstream" counter to be armed (for retired instructions this is tagging FSB). Not sure how to handle it properly. Perhaps at moment of scheduling events in we may check out if there is dependant event and then we try to reserve the counter.

4)  Cascading counters. Well, I suppose we may just not support it for some time.

5)  Searching the events. Event templates contain (among other fields) "opcode" which is just "ESCR event + CCCR selector" packed so every time I need to find out restriction for particular event I need to walk over templates array and check for same opcode. I guess it will be too slow (well, at moment there a few events, but as only array grow it may become a problem, also iirc some opcodes may intersect).

So the scheme I had in mind was like that:

1) As only caller asking for some generalized event we pack ESCR+CCCR into "config", check for HT support and put HT bit into config as well.

2) As events go to schedule for execution need to find out proper hw_perf_event::config into proper template and set proper hw_perf_event::idx (which is just CCCR MSR number\offset) from template. Mark this template as borrowed in global p4_pmu_config space. This allow to find out quickly which ESCR MSR is to be used in event enabling routing.

  Schematically this mapping is

       hw_perf_event::config -> template -> hw_perf_event::idx

  And back at moment of event enabling

       hw_perf_event::idx -> p4_pmu_config[idx] -> template -> ESCR MSR

3) I've started unbinding x86_schedule_events into per x86_pmu::schedule_events and there I hit hardness in binding HT bit. Have to think...

All in one -- If you have some spare minutes, please take a glance. I can't say I like this code -- it's overcomplicated and I fear hard to understand. and still a bit buggy. Complains are welcome!

Quoted 7 lines

So any idea would be great. I'm fine in just DROPPING this approach if we may make code simplier. Perhaps I used wrong way from the very beginning.

Stephane, I know you've been (and still is, right?) working on P4 driver for libpmu (not sure if I remember correctly this name). Perhaps we should take some ideas from there?

The library is called libpfm (perfmon) and libpfm4 (perf_events). It has support for Netburst (pentium4). It does the assignment of events to counters in user mode. I did not write the P4 support and it was a very long time ago. I don't recall all the details, except that this was really difficult. You may want to take a look at pfmlib_pentium4.c.

I am still actively developing libpfm4 for perf_events. The P4 support is not available because I don't know how perf_events is going to support it. I'll update the library once that is settled.

#16 lore raw
Quoted 3 lines · Stephane Eranian

Cyrill,

On Mon, Feb 8, 2010 at 7:45 PM, Cyrill Gorcunov <[email protected]> wrote:

...

Quoted 10 lines
Quoted 5 lines

Stephane, I know you've been (and still is, right?) working on P4 driver for libpmu (not sure if I remember correctly this name). Perhaps we should take some ideas from there?

The library is called libpfm (perfmon) and libpfm4 (perf_events). It has support for Netburst (pentium4). It does the assignment of events to counters in user mode. I did not write the P4 support and it was a very long time ago. I don't recall all the details, except that this was really difficult. You may want to take a look at pfmlib_pentium4.c.

Yeah, I've a look taken on it. Interesting. Thanks! Will take more precise look as only get spare time slot :)

Quoted 5 lines

I am still actively developing libpfm4 for perf_events. The P4 support is not available because I don't know how perf_events is going to support it. I'll update the library once that is settled.

-- Cyrill

#17 lore raw
In reply to Cyrill Gorcunov
Quoted 16 lines · Cyrill Gorcunov

Hi all,

first of all the patches are NOT for any kind of inclusion. It's not ready yet. More likely I'm asking for glance review, ideas, criticism.

The main problem in implementing P4 PMU is that it has much more restrictions for event to MSR mapping. So to fit into current perf_events model I made the following:

1) Event representation. P4 uses a tuple of ESCR+CCCR+COUNTER as an "event". Since every CCCR register mapped directly to counter itself and ESCR and CCCR uses only 32bits of their appropriate MSRs, I decided to use "packed" config in in hw_perf_event::config. So that upper 31 bits are ESCR and lower 32 bits are CCCR values. The bit 64 is for HT flag.

Quoted 6 lines

So the base idea here is to pack into 64bit hw_perf_event::config as much info as possible.

Due to difference in bitfields I needed to implement hw_perf_event::config helper which unbind hw_perf_event::config field from processor specifics and allow to use it in P4 PMU.

If we introduce model specific configuration, we should put more model specific code in here and then remove

  u64             (*raw_event)(u64);

in struct x86_pmu.

Quoted 2 lines

3) I've started unbinding x86_schedule_events into per x86_pmu::schedule_events and there I hit hardness in binding HT bit. Have to think...

Instead of implemting x86_pmu.schedule_events() you should rather abstract x86_pmu_enable(). This will be much more flexible to implement other model spcific features.

-Robert

Signature · 3 lines
Advanced Micro Devices, Inc.
Operating System Research Center
email: [email protected]
#18 lore raw
Quoted 24 lines · Robert Richter
Quoted 16 lines · Cyrill Gorcunov

Hi all,

first of all the patches are NOT for any kind of inclusion. It's not ready yet. More likely I'm asking for glance review, ideas, criticism.

The main problem in implementing P4 PMU is that it has much more restrictions for event to MSR mapping. So to fit into current perf_events model I made the following:

1) Event representation. P4 uses a tuple of ESCR+CCCR+COUNTER as an "event". Since every CCCR register mapped directly to counter itself and ESCR and CCCR uses only 32bits of their appropriate MSRs, I decided to use "packed" config in in hw_perf_event::config. So that upper 31 bits are ESCR and lower 32 bits are CCCR values. The bit 64 is for HT flag.

Quoted 6 lines

So the base idea here is to pack into 64bit hw_perf_event::config as much info as possible.

Due to difference in bitfields I needed to implement hw_perf_event::config helper which unbind hw_perf_event::config field from processor specifics and allow to use it in P4 PMU.

Quoted 7 lines

If we introduce model specific configuration, we should put more model specific code in here and then remove

  u64             (*raw_event)(u64);

in struct x86_pmu.

It seems we should still support raw_events, if I understand the idea right -- raw_events could be used for say OProfile (if we are going to substitute oprofile with perfevents subsystem). So the only difference with other architectural events is that raw_event need to be "packed" before being set into.

Putting/packing more specific code into config could make code even more complex. Dunno Robert...

Quoted 6 lines
Quoted 2 lines

3) I've started unbinding x86_schedule_events into per x86_pmu::schedule_events and there I hit hardness in binding HT bit. Have to think...

Instead of implemting x86_pmu.schedule_events() you should rather abstract x86_pmu_enable(). This will be much more flexible to implement other model spcific features.

But I would need collect events and so on -- ie code duplication will be there. Or you mean something else?

Quoted 8 lines

-Robert

Signature · 3 lines
Advanced Micro Devices, Inc.
Operating System Research Center
email: [email protected]

-- Cyrill

#19 lore raw
In reply to Robert Richter
Quoted 24 lines · Robert Richter
Quoted 16 lines · Cyrill Gorcunov

Hi all,

first of all the patches are NOT for any kind of inclusion. It's not ready yet. More likely I'm asking for glance review, ideas, criticism.

The main problem in implementing P4 PMU is that it has much more restrictions for event to MSR mapping. So to fit into current perf_events model I made the following:

1) Event representation. P4 uses a tuple of ESCR+CCCR+COUNTER as an "event". Since every CCCR register mapped directly to counter itself and ESCR and CCCR uses only 32bits of their appropriate MSRs, I decided to use "packed" config in in hw_perf_event::config. So that upper 31 bits are ESCR and lower 32 bits are CCCR values. The bit 64 is for HT flag.

Quoted 6 lines

So the base idea here is to pack into 64bit hw_perf_event::config as much info as possible.

Due to difference in bitfields I needed to implement hw_perf_event::config helper which unbind hw_perf_event::config field from processor specifics and allow to use it in P4 PMU.

Quoted 7 lines

If we introduce model specific configuration, we should put more model specific code in here and then remove

  u64             (*raw_event)(u64);

in struct x86_pmu.

Hi Robert,

On the other hand -- perhaps you're right, though this is become unclear what to do if user wanna put some specific bit into configuration. perf tool may use same opcodes as we do here but slighly modified (for some particular purpose).

Quoted 6 lines
Quoted 2 lines

3) I've started unbinding x86_schedule_events into per x86_pmu::schedule_events and there I hit hardness in binding HT bit. Have to think...

Instead of implemting x86_pmu.schedule_events() you should rather abstract x86_pmu_enable(). This will be much more flexible to implement other model spcific features.

Actually I starting to think that maybe completely separate PMU is needed for P4, just to not bring mess into perf_event.c file.

Quoted 3 lines

-Robert

Anyway, I put updated version of P4 PMU here. The only difference with previous -- cpu parameter get eliminated from function arguments (schedule function).

Any ideas and (especially) complains are appreciated. Completely not tested, since I don;t have such a box, so I'm just curious will it work at all :) I put both patches here just in case if soneone would like (for some reason) to try them :)

-- Cyrill
---
perf,x86: Introduce hw_config helper into x86_pmu

In case if a particular processor (say Netburst) has a completely different way of configuration we may use the separate hw_config helper to unbind processor specifics from __hw_perf_event_init.

Note that the test of BTS support has been changed for attr->exclude_kernel for the very same reason.

---
 arch/x86/kernel/cpu/perf_event.c |   42 ++++++++++++++++++++++++++-------------
 1 file changed, 28 insertions(+), 14 deletions(-)
arch/x86/kernel/cpu/perf_event.c +28 -15
Index: linux-2.6.git/arch/x86/kernel/cpu/perf_event.c
=====================================================================
@@ -139,6 +139,7 @@ struct x86_pmu {
 	int		apic;
 	u64		max_period;
 	u64		intel_ctrl;
+	int		(*hw_config)(struct perf_event_attr *attr, struct hw_perf_event *hwc);
 	void		(*enable_bts)(u64 config);
 	void		(*disable_bts)(void);
 
@@ -923,6 +924,25 @@ static void release_pmc_hardware(void)
 #endif
 }
 
+static int intel_hw_config(struct perf_event_attr *attr, struct hw_perf_event *hwc)
+{
+	/*
+	 * Generate PMC IRQs:
+	 * (keep 'enabled' bit clear for now)
+	 */
+	hwc->config = ARCH_PERFMON_EVENTSEL_INT;
+
+	/*
+	 * Count user and OS events unless requested not to
+	 */
+	if (!attr->exclude_user)
+		hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
+	if (!attr->exclude_kernel)
+		hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
+
+	return 0;
+}
+
 static inline bool bts_available(void)
 {
 	return x86_pmu.enable_bts != NULL;
@@ -1136,23 +1156,13 @@ static int __hw_perf_event_init(struct p
 
 	event->destroy = hw_perf_event_destroy;
 
-	/*
-	 * Generate PMC IRQs:
-	 * (keep 'enabled' bit clear for now)
-	 */
-	hwc->config = ARCH_PERFMON_EVENTSEL_INT;
-
 	hwc->idx = -1;
 	hwc->last_cpu = -1;
 	hwc->last_tag = ~0ULL;
 
-	/*
-	 * Count user and OS events unless requested not to.
-	 */
-	if (!attr->exclude_user)
-		hwc->config |= ARCH_PERFMON_EVENTSEL_USR;
-	if (!attr->exclude_kernel)
-		hwc->config |= ARCH_PERFMON_EVENTSEL_OS;
+	/* Processor specifics */
+	if (x86_pmu.hw_config(attr, hwc))
+		return -EOPNOTSUPP;
 
 	if (!hwc->sample_period) {
 		hwc->sample_period = x86_pmu.max_period;
@@ -1204,7 +1214,7 @@ static int __hw_perf_event_init(struct p
 			return -EOPNOTSUPP;
 
 		/* BTS is currently only allowed for user-mode. */
-		if (hwc->config & ARCH_PERFMON_EVENTSEL_OS)
+		if (!attr->exclude_kernel)
 			return -EOPNOTSUPP;
 	}
 
@@ -2371,6 +2381,7 @@ static __initconst struct x86_pmu p6_pmu
 	.max_events		= ARRAY_SIZE(p6_perfmon_event_map),
 	.apic			= 1,
 	.max_period		= (1ULL << 31) - 1,
+	.hw_config		= intel_hw_config,
 	.version		= 0,
 	.num_events		= 2,
 	/*
@@ -2405,6 +2416,7 @@ static __initconst struct x86_pmu core_p
 	 * the generic event period:
 	 */
 	.max_period		= (1ULL << 31) - 1,
+	.hw_config		= intel_hw_config,
 	.get_event_constraints	= intel_get_event_constraints,
 	.event_constraints	= intel_core_event_constraints,
 };
@@ -2428,6 +2440,7 @@ static __initconst struct x86_pmu intel_
 	 * the generic event period:
 	 */
 	.max_period		= (1ULL << 31) - 1,
+	.hw_config		= intel_hw_config,
 	.enable_bts		= intel_pmu_enable_bts,
 	.disable_bts		= intel_pmu_disable_bts,
 	.get_event_constraints	= intel_get_event_constraints
@@ -2451,6 +2464,7 @@ static __initconst struct x86_pmu amd_pm
 	.apic			= 1,
 	/* use highest bit to detect overflow */
 	.max_period		= (1ULL << 47) - 1,
+	.hw_config		= intel_hw_config,
 	.get_event_constraints	= amd_get_event_constraints
 };
---

[RFC] perf,x86: Introduce minimal Netburst PMU driver v2

Netburst PMU is a way differ from "architectural perfomance monitoring" specfication. P4 uses a tuple of ESCR+CCCR+COUNTER MSR registers to handle perfomance monitoring events.

A few implementation details:

1) hw_perf_event::config consists of packed ESCR+CCCR value. It's allowed since in real both registers use only a half of their size. Of course before make a real write into a particular MSR we need to unpack value and extend it to a proper size.

2) The tuple of packed ESCR+CCCR in hw_perf_event::config doesn't describe the memory address of ESCR MSR register so that we need to keep a mapping between this tuples used and available ESCR (various P4 events may use same ESCRs but not simultaneously), for this sake every active event has per-cpu map of hw_perf_event::idx <--> ESCR address.

Restrictions:

- No cascaded counters support
- No dependant events support (so PERF_COUNT_HW_INSTRUCTIONS is turned off for now)
- PERF_COUNT_HW_CACHE_REFERENCES on thread 1 issue warning and may not trigger IRQ, need to rearrange counter indexes that way so either no usage of counter 1 at all or with minimal intersection

Todo:

- Implement dependant events
- Need proper hashing for event opcodes (no linear search please)
- Some events counted during a clock cycle -- need to set threshold for them and count every clock cycle just to get summary statistics (ie to behave the same as other pmu do)
- Consider using event_constraints
- Perhaps need a completely separate PMU, since there is too much difference between architectural spec and netburst events

---
 arch/x86/include/asm/perf_p4.h   |  678 +++++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/cpu/perf_event.c |  548 +++++++++++++++++++++++++++++++
 2 files changed, 1218 insertions(+), 8 deletions(-)
arch/x86/include/asm/perf_p4.h new file +678 -0

678 lines not on this page · raw message

arch/x86/kernel/cpu/perf_event.c +540 -8

621 lines not on this page · raw message