Skip to contentSkip to conversation

perf auxtrace: Support multiple AUX events

PATCH Superseded Leo Yan 2 participants 8 messages Aug 6, 2024 perf events

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Leo Yan

author
#0 lore raw

v3 cover perf auxtrace: Support multiple AUX events

This series is to support multiple events with the *same* type in Perf AUX trace. As the events are same type, the trace data shares the same format and can be decoded by the same decoder.

This patch series is dependent on auxtrace refactoring patch series:
https://lore.kernel.org/lkml/[email protected]/T/#mae62825a677381797a13252c1d071caf18d957d7

Note, a more complex case - different types of AUX events, (e.g. Arm CoreSight event and Arm SPE events are enabled simultaneously) - is still not supported.

Patch 01 is to change the perf core layer in the kernel to allow multiple AUX events to output to single FD, so that '--per-thread' mode can be supported.

Patch 02 validates the AUX events' CPU map. It presumes the AUX events do not overlap for CPU maps, otherwise, it returns failure.

Patches 03, 04, 05 is to support multiple AUX events for buffer mapped index.

As the first enabled instance, the patch series has been tested with multiple Arm SPE events (e.g. arm_spe_0, arm_spe_1, etc). And verified this patch series on Intel-PT for no regression.

Changes from v2:
- Added patch 01 for support per-thread mode (Adrian).
- Added patch 02 for verifying CPU maps without overlapping (Adrian).
- Reworked patches to fix the regression on Intel-PT (Ian).

Changes from v1:
- Added comment in patch 01 for iterating AUX events (Adrian)
- Added patch 02 for removing unused field 'pmu' (Adrian)

Leo Yan (5):
perf/core: Allow multiple AUX PMU events with the same module perf auxtrace: Introduce auxtrace_record__validate_events() perf auxtrace: Refactor evlist__enable_event_idx()
perf auxtrace: Bails out after finding the event for the map index perf auxtrace: Iterate all AUX events when finish reading

 kernel/events/core.c        |   3 +-
 tools/perf/builtin-record.c |   4 ++
 tools/perf/util/auxtrace.c  | 110 ++++++++++++++++++++++++++++++++----
 tools/perf/util/auxtrace.h  |   7 +++
 4 files changed, 113 insertions(+), 11 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/5 perf/core: Allow multiple AUX PMU events with the same module

This commit changes the condition from checking the same PMU instance to checking the same PMU driver module. This allows support for multiple PMU events with the same driver module.

As a result, more than one AUX event (e.g. arm_spe_0 and arm_spe_1) can record trace into the AUX ring buffer.

---
 kernel/events/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/events/core.c +2 -1
index aa3450bdc227..fdb8918e62a0 100644
@@ -12344,9 +12344,10 @@ perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
 
 	/*
 	 * If both events generate aux data, they must be on the same PMU
+	 * module but can be with different PMU instances.
 	 */
 	if (has_aux(event) && has_aux(output_event) &&
-	    event->pmu != output_event->pmu)
+	    event->pmu->module != output_event->pmu->module)
 		goto out;
 
 	/*
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v3 1/5] perf/core: Allow multiple AUX PMU events with the same module

Quoted 17 lines · Leo Yan

This commit changes the condition from checking the same PMU instance to checking the same PMU driver module. This allows support for multiple PMU events with the same driver module.

As a result, more than one AUX event (e.g. arm_spe_0 and arm_spe_1) can record trace into the AUX ring buffer.

---
 kernel/events/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/events/core.c +0 -0
index aa3450bdc227..fdb8918e62a0 100644

kernel/events/core.c perf_event_set_output(struct perf_event *event, struct perf_event *output_event) · line 12350

Quoted 8 lines
kernel/events/core.c +2 -1
@@ -12344,9 +12344,10 @@ perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
 
 	/*
 	 * If both events generate aux data, they must be on the same PMU
+	 * module but can be with different PMU instances.
 	 */
 	if (has_aux(event) && has_aux(output_event) &&
-	    event->pmu != output_event->pmu)
+	    event->pmu->module != output_event->pmu->module)

A very quick look at: arch/x86/events/intel/pt.c seems to suggest that doesn't set pmu->module. Which seems to suggest the above won't work for us.

author
#3 lore raw

Re: [PATCH v3 1/5] perf/core: Allow multiple AUX PMU events with the same module

Quoted 19 lines · Peter Zijlstra
Quoted 18 lines · Leo Yan

This commit changes the condition from checking the same PMU instance to checking the same PMU driver module. This allows support for multiple PMU events with the same driver module.

As a result, more than one AUX event (e.g. arm_spe_0 and arm_spe_1) can record trace into the AUX ring buffer.

---
 kernel/events/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/events/core.c +0 -0
index aa3450bdc227..fdb8918e62a0 100644
@@ -12344,9 +12344,10 @@ perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
 
Quoted 11 lines

kernel/events/core.c perf_event_set_output(struct perf_event *event, struct perf_event *output_event) · line 12350

Quoted 7 lines
kernel/events/core.c +2 -1
      /*
       * If both events generate aux data, they must be on the same PMU
+      * module but can be with different PMU instances.
       */
      if (has_aux(event) && has_aux(output_event) &&
-         event->pmu != output_event->pmu)
+         event->pmu->module != output_event->pmu->module)

A very quick look at: arch/x86/events/intel/pt.c seems to suggest that doesn't set pmu->module. Which seems to suggest the above won't work for us.

Seems Intel PT and BTS drivers both do not set the pmu->module.

Either we assign the pmu->module for both drivers, or we can update this patch like below:

        if (has_aux(event) && has_aux(output_event)) {
            /*
             * No idea the module for the PMU is pmu->module is NULL,
             * do not allow tracing for these two AUX event.
             */
            if (!event->pmu->module || !output_event->pmu->module)
                goto out;

            /*
             * Don't allow generate AUX trace data if PMUs don't come from
             * the same module.
             */
            if (event->pmu->module != output_event->pmu->module)
                goto out;
        }

How about you think? Thanks for review!

Leo

author
#4 lore raw

PATCH v3 2/5 perf auxtrace: Introduce auxtrace_record__validate_events()

In reply to Leo Yan

A prerequisite for multiple AUX events is that the AUX events cannot overlap CPU maps. The reason is that every CPU has only one AUX trace buffer and maps it to an unique buffer index for CPU and system tracing mode.

To prevent the case of CPU maps overlapping occurring within multiple AUX events, the auxtrace_record__validate_events() function is introduced. It iterates through all AUX events and returns failure if it detects CPU maps overlapping.

---
 tools/perf/builtin-record.c |  4 +++
 tools/perf/util/auxtrace.c  | 64 +++++++++++++++++++++++++++++++++++++
 tools/perf/util/auxtrace.h  |  7 ++++
 3 files changed, 75 insertions(+)
tools/perf/builtin-record.c +4 -0
index 72345d1e54b0..dffb56052219 100644
@@ -853,6 +853,10 @@ static int record__auxtrace_init(struct record *rec)
 
 	auxtrace_regroup_aux_output(rec->evlist);
 
+	err = auxtrace_validate_events(rec->evlist);
+	if (err)
+		return err;
+
 	return auxtrace_parse_filters(rec->evlist);
 }
 
tools/perf/util/auxtrace.c +64 -0

70 lines not on this page · raw message

tools/perf/util/auxtrace.h +7 -0
index f13c2de17653..9fd5095894de 100644
@@ -634,6 +634,7 @@ void addr_filters__exit(struct addr_filters *filts);
 int addr_filters__parse_bare_filter(struct addr_filters *filts,
 				    const char *filter);
 int auxtrace_parse_filters(struct evlist *evlist);
+int auxtrace_validate_events(struct evlist *evlist);
 
 int auxtrace__process_event(struct perf_session *session, union perf_event *event,
 			    struct perf_sample *sample, struct perf_tool *tool);
@@ -873,6 +874,12 @@ int auxtrace_parse_filters(struct evlist *evlist __maybe_unused)
 	return 0;
 }
 
+static inline
+int auxtrace_validate_events(struct evlist *evlist __maybe_unused)
+{
+	return 0;
+}
+
 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
 			struct auxtrace_mmap_params *mp,
 			void *userpg, int fd);
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v3 3/5 perf auxtrace: Refactor evlist__enable_event_idx()

In reply to Leo Yan

This commit splits the evlist__enable_event_idx() function into two steps. The first step uses a new function evlist__find_cpu_map_idx() to find the CPU map index, based on the found CPU map index or a thread map index, it continues to call evlist__enable_event_idx() for enabling the corresponding event.

---
 tools/perf/util/auxtrace.c | 42 +++++++++++++++++++++++++++++---------
 1 file changed, 32 insertions(+), 10 deletions(-)
tools/perf/util/auxtrace.c +32 -10
index d5015d27b6dd..174d486056e0 100644
@@ -651,20 +651,30 @@ int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
 	return -EINVAL;
 }
 
-static int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
+static int evlist__find_cpu_map_idx(struct evlist *evlist, struct evsel *evsel,
+				    int idx)
 {
 	bool per_cpu_mmaps = !perf_cpu_map__has_any_cpu(evlist->core.user_requested_cpus);
+	struct perf_cpu evlist_cpu;
+	int cpu_map_idx;
 
-	if (per_cpu_mmaps) {
-		struct perf_cpu evlist_cpu = perf_cpu_map__cpu(evlist->core.all_cpus, idx);
-		int cpu_map_idx = perf_cpu_map__idx(evsel->core.cpus, evlist_cpu);
+	if (!per_cpu_mmaps)
+		return -EINVAL;
 
-		if (cpu_map_idx == -1)
-			return -EINVAL;
-		return perf_evsel__enable_cpu(&evsel->core, cpu_map_idx);
-	}
+	evlist_cpu = perf_cpu_map__cpu(evlist->core.all_cpus, idx);
+	cpu_map_idx = perf_cpu_map__idx(evsel->core.cpus, evlist_cpu);
+	if (cpu_map_idx == -1)
+		return -ENOENT;
+
+	return cpu_map_idx;
+}
 
-	return perf_evsel__enable_thread(&evsel->core, idx);
+static int evlist__enable_event_idx(struct evsel *evsel, int cpu_mode, int idx)
+{
+	if (cpu_mode)
+		return perf_evsel__enable_cpu(&evsel->core, idx);
+	else
+		return perf_evsel__enable_thread(&evsel->core, idx);
 }
 
 int auxtrace_record__read_finish(struct auxtrace_record *itr, int idx)
@@ -675,10 +685,22 @@ int auxtrace_record__read_finish(struct auxtrace_record *itr, int idx)
 		return -EINVAL;
 
 	evlist__for_each_entry(itr->evlist, evsel) {
+		int cpu_map_idx;
+
 		if (evsel__is_aux_event(evsel)) {
 			if (evsel->disabled)
 				return 0;
-			return evlist__enable_event_idx(itr->evlist, evsel, idx);
+
+			cpu_map_idx = evlist__find_cpu_map_idx(itr->evlist,
+							       evsel, idx);
+			/* No map is found in per CPU mmap */
+			if (cpu_map_idx == -ENOENT)
+				return cpu_map_idx;
+
+			if (cpu_map_idx >= 0)
+				return evlist__enable_event_idx(evsel, 1, cpu_map_idx);
+			else
+				return evlist__enable_event_idx(evsel, 0, idx);
 		}
 	}
 	return -EINVAL;
Signature · 1 line
2.34.1
author
#6 lore raw

PATCH v3 4/5 perf auxtrace: Bails out after finding the event for the map index

In reply to Leo Yan

After finding the corresponding event for the passed buffer index, it is safe to say the found event has been used. Then, the tool can check the event status and bails out if it has been disabled.

---
 tools/perf/util/auxtrace.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/perf/util/auxtrace.c +3 -3
index 174d486056e0..7c998f95d2ff 100644
@@ -688,15 +688,15 @@ int auxtrace_record__read_finish(struct auxtrace_record *itr, int idx)
 		int cpu_map_idx;
 
 		if (evsel__is_aux_event(evsel)) {
-			if (evsel->disabled)
-				return 0;
-
 			cpu_map_idx = evlist__find_cpu_map_idx(itr->evlist,
 							       evsel, idx);
 			/* No map is found in per CPU mmap */
 			if (cpu_map_idx == -ENOENT)
 				return cpu_map_idx;
 
+			if (evsel->disabled)
+				return 0;
+
 			if (cpu_map_idx >= 0)
 				return evlist__enable_event_idx(evsel, 1, cpu_map_idx);
 			else
Signature · 1 line
2.34.1
author
#7 lore raw

PATCH v3 5/5 perf auxtrace: Iterate all AUX events when finish reading

In reply to Leo Yan

When finished to read AUX trace data from mmaped buffer, based on the AUX buffer index the core layer needs to search the corresponding PMU event and re-enable it to continue tracing.

However, current code only searches the first AUX event. It misses to search other enabled AUX events, thus, it returns failure if the buffer index does not belong to the first AUX event.

This patch changes to continue searching every enabled AUX events for covering the mmaped buffer indexes.

---
 tools/perf/util/auxtrace.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/perf/util/auxtrace.c +6 -2
index 7c998f95d2ff..926eb3fe6015 100644
@@ -690,9 +690,13 @@ int auxtrace_record__read_finish(struct auxtrace_record *itr, int idx)
 		if (evsel__is_aux_event(evsel)) {
 			cpu_map_idx = evlist__find_cpu_map_idx(itr->evlist,
 							       evsel, idx);
-			/* No map is found in per CPU mmap */
+			/*
+			 * No map is found in per CPU mmap. Multiple AUX events
+			 * might be opened in a session, continue to check if
+			 * the next AUX event can cover the mmaped buffer 'idx'.
+			 */
 			if (cpu_map_idx == -ENOENT)
-				return cpu_map_idx;
+				continue;
 
 			if (evsel->disabled)
 				return 0;
Signature · 1 line
2.34.1