Skip to contentSkip to conversation

perf: Sharing PMU counters across compatible events

PATCH Superseded Song Liu 1 participant 3 messages Mar 24, 2020

Subsystems (2)

v11 of 13 Superseded Latest is v13 →

lore
Share & tools
lore

Song Liu

author
#0 lore raw

PATCH v11 1/1 perf: Sharing PMU counters across compatible events

This patch tries to enable PMU sharing. When multiple perf_events are counting the same metric, they can share the hardware PMU counter. We call these events as "compatible events".

The PMU sharing are limited to events within the same perf_event_context (ctx). When a event is installed or enabled, search the ctx for compatible events. This is implemented in perf_event_setup_dup(). One of these compatible events are picked as the master (stored in event->dup_master). Similarly, when the event is removed or disabled, perf_event_remove_dup() is used to clean up sharing.

If the master event is a cgroup event or a event in a group, it is possible that some slave events are ACTIVE, but the master event is not. To handle this scenario, we introduced PERF_EVENT_STATE_ENABLED. Also, since PMU drivers write into event->count, master event needs another variable (master_count) for the reading of this event.

On the critical paths (add, del read), sharing PMU counters doesn't increase the complexity. Helper functions event_pmu_[add|del|read]() are introduced to cover these cases. All these functions have O(1) time complexity.

Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Alexey Budankov <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Tejun Heo <[email protected]>

---
Changes in v12:
Fix new failures perf_event_tests. (kernel test robot)

Changes in v11:
Fold in major fixes by Peter.
In perf_event_remove_dup(), del() old master before add() new master.

Changes in v10:
Simplify logic that calls perf_event_setup_dup() and perf_event_remove_dup(). (Peter)
Other small fixes. (Peter)

Changes in v9:
Avoid ctx_resched() on remove/disable event (Peter). Compare the whole perf_event_attr in perf_event_compatible(). Small fixes/improvements (Peter).

Changes in v8:
Fix issues with task event (Jiri).
Fix issues with event inherit.
Fix mmap'ed events, i.e. perf test 4 (kernel test bot).

Changes in v7:
Major rewrite to avoid allocating extra master event. (cherry picked from commit 8b98afc0ec100b017a5e80c184809939d4f01f8d)

---
 arch/x86/events/core.c     |   7 +
 include/linux/perf_event.h |  21 ++-
 kernel/events/core.c       | 375 ++++++++++++++++++++++++++++++++++---
 3 files changed, 373 insertions(+), 30 deletions(-)
arch/x86/events/core.c +7 -0
index 3bb738f5a472..32f836dbfae3 100644
@@ -2341,6 +2341,11 @@ static int x86_pmu_aux_output_match(struct perf_event *event)
 	return 0;
 }
 
+static void x86_copy_hw_config(struct perf_event *old, struct perf_event *new)
+{
+	new->hw.idx = old->hw.idx;
+}
+
 static struct pmu pmu = {
 	.pmu_enable		= x86_pmu_enable,
 	.pmu_disable		= x86_pmu_disable,
@@ -2369,6 +2374,8 @@ static struct pmu pmu = {
 	.check_period		= x86_pmu_check_period,
 
 	.aux_output_match	= x86_pmu_aux_output_match,
+
+	.copy_hw_config		= x86_copy_hw_config,
 };
 
 void arch_perf_update_userpage(struct perf_event *event,
include/linux/perf_event.h +20 -1
index 8768a39b5258..14b94535e213 100644
@@ -503,6 +503,13 @@ struct pmu {
 	 * Check period value for PERF_EVENT_IOC_PERIOD ioctl.
 	 */
 	int (*check_period)		(struct perf_event *event, u64 value); /* optional */
+
+	/*
+	 * Copy hw configuration from one event to another. This is used
+	 * to make switching master faster in PMC sharing.
+	 */
+	void (*copy_hw_config)		(struct perf_event *old,
+					 struct perf_event *new); /* optional */
 };
 
 enum perf_addr_filter_action_t {
@@ -552,6 +559,10 @@ struct perf_addr_filter_range {
 
 /**
  * enum perf_event_state - the states of an event:
+ *
+ * PERF_EVENT_STATE_ENABLED:	Special state for PMC sharing: the hw PMC
+ *				is enabled, but this event is not counting.
+ *				See perf_event_init_dup_master().
  */
 enum perf_event_state {
 	PERF_EVENT_STATE_DEAD		= -4,
@@ -559,7 +570,8 @@ enum perf_event_state {
 	PERF_EVENT_STATE_ERROR		= -2,
 	PERF_EVENT_STATE_OFF		= -1,
 	PERF_EVENT_STATE_INACTIVE	=  0,
-	PERF_EVENT_STATE_ACTIVE		=  1,
+	PERF_EVENT_STATE_ENABLED	=  1,
+	PERF_EVENT_STATE_ACTIVE		=  2,
 };
 
 struct file;
@@ -645,6 +657,7 @@ struct perf_event {
 	int				group_caps;
 
 	struct perf_event		*group_leader;
+	struct perf_event		*dup_master;  /* for PMU sharing */
 	struct pmu			*pmu;
 	void				*pmu_private;
 
@@ -762,6 +775,12 @@ struct perf_event {
 	void *security;
 #endif
 	struct list_head		sb_list;
+
+	int				dup_active;
+	/* See event_pmu_read_dup() */
+	local64_t			dup_count;
+	/* See perf_event_init_dup_master() */
+	local64_t			master_count;
 #endif /* CONFIG_PERF_EVENTS */
 };
 
kernel/events/core.c +346 -29

536 lines not on this page · raw message

Signature · 1 line
2.17.1
author
#1 lore raw
Quoted 35 lines · Song Liu

This patch tries to enable PMU sharing. When multiple perf_events are counting the same metric, they can share the hardware PMU counter. We call these events as "compatible events".

The PMU sharing are limited to events within the same perf_event_context (ctx). When a event is installed or enabled, search the ctx for compatible events. This is implemented in perf_event_setup_dup(). One of these compatible events are picked as the master (stored in event->dup_master). Similarly, when the event is removed or disabled, perf_event_remove_dup() is used to clean up sharing.

If the master event is a cgroup event or a event in a group, it is possible that some slave events are ACTIVE, but the master event is not. To handle this scenario, we introduced PERF_EVENT_STATE_ENABLED. Also, since PMU drivers write into event->count, master event needs another variable (master_count) for the reading of this event.

On the critical paths (add, del read), sharing PMU counters doesn't increase the complexity. Helper functions event_pmu_[add|del|read]() are introduced to cover these cases. All these functions have O(1) time complexity.

Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Alexey Budankov <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Tejun Heo <[email protected]>
Quoted 3 lines

---
Changes in v12:
Fix new failures perf_event_tests. (kernel test robot)

Typo in subject. This is actually v12.

Thanks,
Song

author
#2 lore raw
Quoted 39 lines · Song Liu
Quoted 36 lines · Song Liu

This patch tries to enable PMU sharing. When multiple perf_events are counting the same metric, they can share the hardware PMU counter. We call these events as "compatible events".

The PMU sharing are limited to events within the same perf_event_context (ctx). When a event is installed or enabled, search the ctx for compatible events. This is implemented in perf_event_setup_dup(). One of these compatible events are picked as the master (stored in event->dup_master). Similarly, when the event is removed or disabled, perf_event_remove_dup() is used to clean up sharing.

If the master event is a cgroup event or a event in a group, it is possible that some slave events are ACTIVE, but the master event is not. To handle this scenario, we introduced PERF_EVENT_STATE_ENABLED. Also, since PMU drivers write into event->count, master event needs another variable (master_count) for the reading of this event.

On the critical paths (add, del read), sharing PMU counters doesn't increase the complexity. Helper functions event_pmu_[add|del|read]() are introduced to cover these cases. All these functions have O(1) time complexity.

Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Alexey Budankov <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Tejun Heo <[email protected]>
Quoted 5 lines
Quoted 3 lines

---
Changes in v12:
Fix new failures perf_event_tests. (kernel test robot)

Typo in subject. This is actually v12.

I was stupid... This version didn't work... Please discard it.

Song