Skip to contentSkip to conversation

Sharing PMU counters across compatible events

RFC In review Tejun Heo 3 participants 9 messages Dec 1, 2017 – Dec 13, 2017

Subsystems (2)
lore
Share & tools
lore

Tejun Heo

author
#0 lore raw

RFC 1/1 Sharing PMU counters across compatible events

Hello,

There are cases where a single PMU event, let's say instructions, is interesting for different subsets of the system. For example, it could be interesting to monitor instructions system-wide, at cgroup level and per each thread.

This could easily be me not knowing better but I can't think of a way to do the above without consuming multiple PMU counters to monitor instructions, which quickly gets out of hand with the product of multiple domains and counters. Getting broken down numbers and adding up doesn't work at cgroup (the numbers aren't mutually exclusive) or thread level.

If there's a way to achieve this using the existing features, I'd be glad to learn about it. If not, the patch at the bottom is a crude half-working proof-of-concept to share PMU counters across compatible events.

In a nutshell, while adding events, it looks whether there already is a compatible event. If so, instead of enabling the new event, it gets linked to the already enabled one (the master event) and count of the dup event is updated by adding the delta of the master event.

That patch is just a proof of concept. It's missing counter propagation somewhere and the dup counts end up somewhat lower than they should be. The master selection and switching are half-broken. Event compatibility testing is barely implemented, so on and so forth. However, it does allow gathering events for different targets without consuming multiple PMU counters.

What do you think? Would this be something worth pursuing?

Thank you very much.

---
 include/linux/perf_event.h |    7 ++
 kernel/events/core.c       |  129 +++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 126 insertions(+), 10 deletions(-)
include/linux/perf_event.h +7 -0
@@ -583,6 +583,13 @@ struct perf_event {
 	local64_t			count;
 	atomic64_t			child_count;
 
+	struct list_head		active_event_entry;
+
+	struct perf_event		*dup_master;
+	struct list_head		dup_list;
+	u64				dup_base_count;
+	u64				dup_base_child_count;
+
 	/*
 	 * These are the total time in nanoseconds that the event
 	 * has been enabled (i.e. eligible to run, and the task has
kernel/events/core.c +119 -10

237 lines not on this page · raw message

#1 lore raw
Quoted 24 lines · Tejun Heo

Hello,

There are cases where a single PMU event, let's say instructions, is interesting for different subsets of the system. For example, it could be interesting to monitor instructions system-wide, at cgroup level and per each thread.

This could easily be me not knowing better but I can't think of a way to do the above without consuming multiple PMU counters to monitor instructions, which quickly gets out of hand with the product of multiple domains and counters. Getting broken down numbers and adding up doesn't work at cgroup (the numbers aren't mutually exclusive) or thread level.

If there's a way to achieve this using the existing features, I'd be glad to learn about it. If not, the patch at the bottom is a crude half-working proof-of-concept to share PMU counters across compatible events.

In a nutshell, while adding events, it looks whether there already is a compatible event. If so, instead of enabling the new event, it gets linked to the already enabled one (the master event) and count of the dup event is updated by adding the delta of the master event.

Quoted 8 lines

That patch is just a proof of concept. It's missing counter propagation somewhere and the dup counts end up somewhat lower than they should be. The master selection and switching are half-broken. Event compatibility testing is barely implemented, so on and so forth. However, it does allow gathering events for different targets without consuming multiple PMU counters.

What do you think? Would this be something worth pursuing?

I see this rather on the hw level, since it concerns HW counters

I think we could detect same (alias) events at the time counters are added/removed on/from the cpu and share their HW part like counter idx, regs and such (struct hw_perf_event_cpu in my changes)

this way it'd be completely transparent for generic code

I made some untested (just compile) changes and attaching the top patch, that has the main logic, please check all changes in here:
git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
perf/alias_2

there's big change due to changing the perf_event::hw stuff but the rest is quite simple (attached).. not completely sure yet if I'm missing some functionality which would break due to this change

thoughts?

thanks,
jirka

---
 arch/x86/events/core.c     | 58 +++++++++++++++++++++++++++++++++++++++++++++-
 include/linux/perf_event.h |  4 ++++
 kernel/events/core.c       |  1 +
 3 files changed, 62 insertions(+), 1 deletion(-)
arch/x86/events/core.c +57 -1
index 2bb5d1b2a622..737857f07881 100644
@@ -1189,6 +1189,54 @@ void x86_pmu_enable_event(struct perf_event *event)
 				       ARCH_PERFMON_EVENTSEL_ENABLE);
 }
 
+static bool is_alias(struct perf_event *alias, struct perf_event *event)
+{
+	if (is_sampling_event(alias))
+		return false;
+
+	return memcmp(&alias->attr, &event->attr, sizeof(alias->attr));
+}
+
+static int alias_add(struct cpu_hw_events *cpuc, struct perf_event *event)
+{
+	struct perf_event *master;
+	int n;
+
+	for (n = 0; n < cpuc->n_events; n++) {
+		if (is_alias(event, cpuc->event_list[n]))
+			break;
+	}
+
+	if (n == cpuc->n_events)
+		return 0;
+
+	master = cpuc->event_list[n];
+	event->hw.cpu = master->hw.cpu;
+	list_add_tail(&event->hw.alias, &master->hw.master);
+	return 1;
+}
+
+static int alias_del(struct cpu_hw_events *cpuc, struct perf_event *event)
+{
+	if (event->hw.cpu != &event->hw.cpu_local)
+		return 0;
+
+	event->hw.cpu = &event->hw.cpu_local;
+	list_del(&event->hw.alias);
+	return 1;
+}
+
+static void alias_update(struct perf_event *event)
+{
+	struct perf_event *alias;
+
+	list_for_each_entry(alias, &event->hw.master, hw.alias) {
+		alias->count          = event->count;
+		alias->hw.prev_count  = event->hw.prev_count;
+		alias->hw.period_left = event->hw.period_left;
+	}
+}
+
 /*
  * Add a single event to the PMU.
  *
@@ -1200,7 +1248,10 @@ static int x86_pmu_add(struct perf_event *event, int flags)
 	struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
 	struct hw_perf_event *hwc;
 	int assign[X86_PMC_IDX_MAX];
-	int n, n0, ret;
+	int n, n0, ret = 0;
+
+	if (alias_add(cpuc, event))
+		goto out;
 
 	hwc = &event->hw;
 
@@ -1383,11 +1434,16 @@ static void x86_pmu_del(struct perf_event *event, int flags)
 	if (cpuc->txn_flags & PERF_PMU_TXN_ADD)
 		goto do_del;
 
+	if (alias_del(cpuc, event))
+		goto do_del;
+
 	/*
 	 * Not a TXN, therefore cleanup properly.
 	 */
 	x86_pmu_stop(event, PERF_EF_UPDATE);
 
+	alias_update(event);
+
 	for (i = 0; i < cpuc->n_events; i++) {
 		if (event == cpuc->event_list[i])
 			break;
include/linux/perf_event.h +4 -0
index b8bc5ddfbffd..6a09914a3555 100644
@@ -136,6 +136,10 @@ struct hw_perf_event {
 		struct { /* hardware */
 			struct hw_perf_event_cpu *cpu;
 			struct hw_perf_event_cpu  cpu_local;
+			union {
+				struct list_head master;
+				struct list_head alias;
+			};
 		};
 		struct { /* software */
 			struct hrtimer	hrtimer;
kernel/events/core.c +1 -0
index 5879c67d90e4..9cf36b2b533f 100644
@@ -9513,6 +9513,7 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
 	INIT_LIST_HEAD(&event->active_event_entry);
 	INIT_LIST_HEAD(&event->dup_list);
 	INIT_LIST_HEAD(&event->addr_filters.list);
+	INIT_LIST_HEAD(&event->hw.master);
 	INIT_HLIST_NODE(&event->hlist_entry);
 
 
Signature · 1 line
2.13.6
author
#2 lore raw

Hello, Jiri.

Quoted 7 lines · Jiri Olsa

I see this rather on the hw level, since it concerns HW counters

I think we could detect same (alias) events at the time counters are added/removed on/from the cpu and share their HW part like counter idx, regs and such (struct hw_perf_event_cpu in my changes)

this way it'd be completely transparent for generic code

I don't quite follow why doing this in arch code is better than generic. Doing this in arch means we'd need to do the same thing multiple times for different archs. Why is that better?

Thanks.

Signature · 1 line
tejun
#3 lore raw
Quoted 9 lines · Tejun Heo

Hello, Jiri.

Quoted 6 lines · Jiri Olsa

I see this rather on the hw level, since it concerns HW counters

I think we could detect same (alias) events at the time counters are added/removed on/from the cpu and share their HW part like counter idx, regs and such (struct hw_perf_event_cpu in my changes)

Quoted 5 lines
Quoted 1 line

this way it'd be completely transparent for generic code

I don't quite follow why doing this in arch code is better than generic. Doing this in arch means we'd need to do the same thing multiple times for different archs. Why is that better?

so I can see this to be useful for HW conters only, because of limited number of regs

as for the higher level on which this could be implemented I see some pitfals with event rotations as Peter mentioned and task/cpu contexts scheduling.. while the hw-level implementation seems pretty straight forward

I'll test the code and let's see ;-)

jirka

author
#4 lore raw

Hello, Jiri.

Quoted 7 lines · Jiri Olsa

so I can see this to be useful for HW conters only, because of limited number of regs

as for the higher level on which this could be implemented I see some pitfals with event rotations as Peter mentioned and task/cpu contexts scheduling.. while the hw-level implementation seems pretty straight forward

Heh, yeah, maybe.

Quoted 1 line

I'll test the code and let's see ;-)

Thanks. I'll think more about it and see whether this can be evolved into something useable.

Thanks.

Signature · 1 line
tejun
maintainer
#5 lore raw
In reply to Tejun Heo
Quoted 1 line · Tejun Heo

What do you think? Would this be something worth pursuing?

My worry with the whole thing is that it makes PMU scheduling _far_ more expensive.

Currently HW PMU scheduling is 'bounded' by the fact that we have bounded hardware resources (actually placing the events on these resources is already very complex because not every event can go on every counter).

We also stop trying to schedule HW events when we find we cannot place more.

If we were to support this sharing thing (and you were correct in noting that the specific conditions for matching events is going to be very tricky indeed), both the above go out the window.

And this is already an area people are optimizing; see for example the patches here:

git://git.kernel.org/pub/scm/linux/kernel/git/peterz/queue.git perf/core

(which are WIP, but I got distracted with that whole KAISER stuff) they make PMU scheduling up to 4x faster for some workload for not having to iterate through the list finding events for the 'wrong' CPU.

With sharing we can no longer say the hardware is full, we done. But we have to exhaustively try each and every event, just in case it can share.

I realize we already have an unbounded case with software events; and we really should do something about that, instead of making it worse :/

author
#6 lore raw

Hello, Peter.

Quoted 12 lines · Peter Zijlstra
Quoted 1 line · Tejun Heo

What do you think? Would this be something worth pursuing?

My worry with the whole thing is that it makes PMU scheduling _far_ more expensive.

Currently HW PMU scheduling is 'bounded' by the fact that we have bounded hardware resources (actually placing the events on these resources is already very complex because not every event can go on every counter).

Quoted 6 lines

We also stop trying to schedule HW events when we find we cannot place more.

If we were to support this sharing thing (and you were correct in noting that the specific conditions for matching events is going to be very tricky indeed), both the above go out the window.

Understood, but I wonder whether something like this can be made significantly cheaper and, hopefully, bound. I could easily be getting the details wrong, but it doesn't seem like we'd need to compute much of these dynamically on context switch.

Let's say that we can pre-compute most of mergeable detections and the value propagation can be pushed to the read time rather than event time and thus that we can have the same functionality with insiginficant hot path overhead. Does that sound like something acceptable to you?

Thanks.

Signature · 1 line
tejun
maintainer
#7 lore raw
Quoted 27 lines · Tejun Heo

Hello, Peter.

Quoted 12 lines · Peter Zijlstra
Quoted 1 line · Tejun Heo

What do you think? Would this be something worth pursuing?

My worry with the whole thing is that it makes PMU scheduling _far_ more expensive.

Currently HW PMU scheduling is 'bounded' by the fact that we have bounded hardware resources (actually placing the events on these resources is already very complex because not every event can go on every counter).

Quoted 6 lines

We also stop trying to schedule HW events when we find we cannot place more.

If we were to support this sharing thing (and you were correct in noting that the specific conditions for matching events is going to be very tricky indeed), both the above go out the window.

Understood, but I wonder whether something like this can be made significantly cheaper and, hopefully, bound. I could easily be getting the details wrong, but it doesn't seem like we'd need to compute much of these dynamically on context switch.

Quoted 5 lines

Let's say that we can pre-compute most of mergeable detections and the value propagation can be pushed to the read time rather than event time and thus that we can have the same functionality with insiginficant hot path overhead. Does that sound like something acceptable to you?

That would be a fairly massive change from how perf works today. And the obvious pain point would be changing the per-cpu event set, which would mean recomputing all possible combinations of task sets.

Also note that each context (cpu,task) is allowed to have more events than fit on the PMU, at which point we'll start rotating events. Do we also pre-compute all possible rotation sets?

Just not quite seeing this..

author
#8 lore raw

Hello, Peter.

Quoted 9 lines · Peter Zijlstra

That would be a fairly massive change from how perf works today. And the obvious pain point would be changing the per-cpu event set, which would mean recomputing all possible combinations of task sets.

Also note that each context (cpu,task) is allowed to have more events than fit on the PMU, at which point we'll start rotating events. Do we also pre-compute all possible rotation sets?

Just not quite seeing this..

Yeah, right. Will think more about it

Thanks.

Signature · 1 line
tejun