Skip to contentSkip to conversation

perf cs-etm: Split Coresight decode by aux records

RFC PATCH Superseded James Clark 4 participants 8 messages Jun 1, 2021 – Jun 8, 2021

Subsystems (3)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 cover perf cs-etm: Split Coresight decode by aux records

Changes since v3:
* Include fix for snapshot mode
* Fix typo that was in the initial snapshot fix posted to the previous

    thread (&& -> &)

James Clark (1):
perf cs-etm: Split Coresight decode by aux records

 tools/perf/util/cs-etm.c | 159 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 156 insertions(+), 3 deletions(-)
Signature · 1 line
2.28.0
#1 lore raw

RFC PATCH v4 1/1 perf cs-etm: Split Coresight decode by aux records

Populate the auxtrace queues using AUX records rather than whole auxtrace buffers.

This is similar to the auxtrace_queues__process_index() -> auxtrace_queues__add_indexed_event() flow where perf_session__peek_event() is used to read AUXTRACE events out of random positions in the file based on the auxtrace index.

Now there is a second pass using perf_session__peek_events() which loops over all PERF_RECORD_AUX events, finds the corresponding auxtrace buffer, and adds a fragment of that buffer to the auxtrace queues, rather than the whole buffer.

This is a completely new implementation compared to v1 and v2 of the patchset where significant modifications were made to the decoding flow, including saving all of the AUX records, and changing the point where decoding started. These changes had effects on the outcome of decoding, but with this implementation, the output of decoding is identical to perf/core, except in the case where decoding failed due to the issue that this change addresses. No changes to decoding were made, apart from populating the auxtrace queues.

Because the decoder is already reset between each auxtrace buffer, populating the queues with fragments of whole buffers based on AUX records has the same affect as resetting the decoder on the AUX record bounds during decode. But this approach only requires a change to one part of the whole flow, rather than many parts.

There is still one TODO about how buffers are found. Currently there is a plain loop as a search, but this could be improved with a binary search or keeping a record of which buffers have already been added and starting the search from the last used place. AUX records and buffers will come in similar orders in the file so the latter would be simpler and probably be good enough. But I'm looking for feedback about the approach as a whole before ironing out all of the details.

Other issues that were fixed by the previous patchsets such as fixing the case where the first aux record comes before the first MMAP record can now be fixed as completely independent issues.

Testing
=======

Testing was done with the following script, to diff the decoding results between the patched and un-patched versions of perf:

	#!/bin/bash
	set -ex

	$1 script -i $3 $4 > split.script
	$2 script -i $3 $4 > default.script

	diff split.script default.script | head -n 20

And it was run like this, with various itrace options depending on the quantity of synthesised events:

	compare.sh ./perf-patched ./perf-default perf-per-cpu-2-threads.data --itrace=i100000ns

No changes in output were observed in the following scenarios:

* Simple per-cpu
perf record -e cs_etm/@tmc_etr0/u top

* Per-thread, single thread

	perf record -e cs_etm/@tmc_etr0/u --per-thread ./threads_C

* Per-thread multiple threads (but only one thread collected data):

	perf record -e cs_etm/@tmc_etr0/u --per-thread --pid 4596,4597

* Per-thread multiple threads (both threads collected data):

	perf record -e cs_etm/@tmc_etr0/u --per-thread --pid 4596,4597

* Per-cpu explicit threads:

	perf record -e cs_etm/@tmc_etr0/u --pid 853,854

* System-wide (per-cpu):
perf record -e cs_etm/@tmc_etr0/u -a

* No data collected (no aux buffers)
Can happen with any command when run for a short period

* Containing truncated records
Can happen with any command

* Containing aux records with 0 size
Can happen with any command

* Snapshot mode

	perf record -e cs_etm/@tmc_etr0/u -a --snapshot
---
 tools/perf/util/cs-etm.c | 159 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 156 insertions(+), 3 deletions(-)
tools/perf/util/cs-etm.c +156 -3
index 64536a6ed10a..053aad4b28cf 100644
@@ -2679,6 +2679,161 @@ static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset,
 	return metadata;
 }
 
+/**
+ * Puts a fragment of an auxtrace buffer into the auxtrace queues based
+ * on the bounds of aux_event, if it matches with the buffer that's at
+ * file_offset.
+ *
+ * Normally, whole auxtrace buffers would be added to the queue. But we
+ * want to reset the decoder for every PERF_RECORD_AUX event, and the decoder
+ * is reset across each buffer, so splitting the buffers up in advance has
+ * the same effect.
+ */
+static int cs_etm__queue_aux_fragment(struct perf_session *session,
+						off_t file_offset, size_t sz,
+						struct perf_record_aux *aux_event,
+						struct perf_sample *sample)
+{
+	int err;
+	char buf[PERF_SAMPLE_MAX_SIZE];
+	union perf_event *auxtrace_event_union;
+	struct perf_record_auxtrace *auxtrace_event;
+	union perf_event auxtrace_fragment;
+	bool matchesCpuPid;
+	__u64 aux_offset;
+	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
+						   struct cs_etm_auxtrace,
+						   auxtrace);
+
+	/*
+	 * There should be a PERF_RECORD_AUXTRACE event at the file_offset that we got
+	 * from looping through the auxtrace index.
+	 */
+	err = perf_session__peek_event(session, file_offset, buf,
+				       PERF_SAMPLE_MAX_SIZE, &auxtrace_event_union, NULL);
+	if (err)
+		return err;
+	auxtrace_event = &auxtrace_event_union->auxtrace;
+	if (auxtrace_event->header.type != PERF_RECORD_AUXTRACE)
+		return -EINVAL;
+
+	if (auxtrace_event->header.size < sizeof(struct perf_record_auxtrace) ||
+		auxtrace_event->header.size != sz) {
+		return -EINVAL;
+	}
+
+	/*
+	 * In per-thread mode, CPU is set to -1, but TID will be set instead.
+	 * See auxtrace_mmap_params__set_idx()
+	 */
+	if (auxtrace_event->cpu == (__u32) -1)
+		matchesCpuPid = auxtrace_event->tid == sample->tid;
+	else
+		matchesCpuPid = auxtrace_event->cpu == sample->cpu;
+
+	/*
+	 * In snapshot/overwrite mode, the head points to the end of the buffer so aux_offset needs
+	 * to have the size subtracted so it points to the beginning as in normal mode.
+	 */
+	if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE)
+		aux_offset = aux_event->aux_offset - aux_event->aux_size;
+	else
+		aux_offset = aux_event->aux_offset;
+
+	if (matchesCpuPid &&
+	    aux_offset >= auxtrace_event->offset &&
+	    aux_offset + aux_event->aux_size <= auxtrace_event->offset + auxtrace_event->size) {
+		/*
+		 * If this AUX event was inside this buffer somewhere, create a new auxtrace event
+		 * based on the sizes of the aux event, and queue that fragment.
+		 */
+		auxtrace_fragment.auxtrace = *auxtrace_event;
+		auxtrace_fragment.auxtrace.size = aux_event->aux_size;
+		auxtrace_fragment.auxtrace.offset = aux_offset;
+		file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size;
+		return auxtrace_queues__add_event(&etm->queues,
+					       session,
+					       &auxtrace_fragment,
+					       file_offset,
+					       NULL);
+	}
+
+	/* Wasn't inside this buffer, but there were no parse errors. 1 == 'not found' */
+	return 1;
+}
+
+static int cs_etm__queue_aux_records_cb(struct perf_session *session,
+			     union perf_event *event, u64 offset __maybe_unused,
+			     void *data __maybe_unused)
+{
+	struct perf_sample sample;
+	int ret;
+	struct auxtrace_index_entry *ent;
+	struct auxtrace_index *auxtrace_index;
+	struct evsel *evsel;
+	size_t i;
+	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
+						   struct cs_etm_auxtrace,
+						   auxtrace);
+
+	/* Don't care about any other events, we're only queuing buffers for AUX events */
+	if (event->header.type != PERF_RECORD_AUX)
+		return 0;
+
+	if (event->header.size < sizeof(struct perf_record_aux))
+		return -EINVAL;
+
+	/* Truncated Aux records can have 0 size and shouldn't result in anything being queued. */
+	if (!event->aux.aux_size)
+		return 0;
+
+	/*
+	 * Parse the sample, we need the sample_id_all data that comes after the event so that the
+	 * CPU or PID can be matched to an AUXTRACE buffer's CPU or PID.
+	 */
+	evsel = evlist__event2evsel(session->evlist, event);
+	evsel__parse_sample(evsel, event, &sample);
+
+	/*
+	 * Loop throuch the auxtrace index to find the buffer that matches up with this
+	 * aux event.
+	 * TODO: binary search?
+	 */
+	list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
+		for (i = 0; i < auxtrace_index->nr; i++) {
+			ent = &auxtrace_index->entries[i];
+			ret = cs_etm__queue_aux_fragment(session, ent->file_offset,
+							 ent->sz, &event->aux, &sample);
+			if (!ret) {
+				etm->data_queued = true;
+				return 0;
+			} else if (ret < 0) {
+				/*
+				 * Anything other than 1 is an error. Positive values are 'not
+				 * found' we just want to go onto the next one in that case.
+				 */
+				return ret;
+			}
+		}
+	}
+
+	/*
+	 * We would get here if there are no entries in the index (either no auxtrace buffers or
+	 * no index). Fail silently as there is the possibility of queueing them in
+	 * cs_etm__process_auxtrace_event() if etm->data_queued is still false;
+	 *
+	 * In that scenario, buffers will not be split by AUX records.
+	 */
+	return 0;
+}
+
+static int cs_etm__queue_aux_records(struct perf_session *session)
+{
+	return perf_session__peek_events(session, session->header.data_offset,
+					 session->header.data_size,
+					 cs_etm__queue_aux_records_cb, NULL);
+}
+
 int cs_etm__process_auxtrace_info(union perf_event *event,
 				  struct perf_session *session)
 {
@@ -2879,12 +3034,10 @@ int cs_etm__process_auxtrace_info(union perf_event *event,
 	if (err)
 		goto err_delete_thread;
 
-	err = auxtrace_queues__process_index(&etm->queues, session);
+	err = cs_etm__queue_aux_records(session);
 	if (err)
 		goto err_delete_thread;
 
-	etm->data_queued = etm->queues.populated;
-
 	return 0;
 
 err_delete_thread:
Signature · 1 line
2.28.0
#2 lore raw

Hi James,

Quoted 13 lines · James Clark

Populate the auxtrace queues using AUX records rather than whole auxtrace buffers.

This is similar to the auxtrace_queues__process_index() -> auxtrace_queues__add_indexed_event() flow where perf_session__peek_event() is used to read AUXTRACE events out of random positions in the file based on the auxtrace index.

Now there is a second pass using perf_session__peek_events() which loops over all PERF_RECORD_AUX events, finds the corresponding auxtrace buffer, and adds a fragment of that buffer to the auxtrace queues, rather than the whole buffer.

Quoted 8 lines

This is a completely new implementation compared to v1 and v2 of the patchset where significant modifications were made to the decoding flow, including saving all of the AUX records, and changing the point where decoding started. These changes had effects on the outcome of decoding, but with this implementation, the output of decoding is identical to perf/core, except in the case where decoding failed due to the issue that this change addresses. No changes to decoding were made, apart from populating the auxtrace queues.

IMHO, this patch is much better than previous versions! :)

When went through the whole patch, the most code actually is general enough and can consider to place into into util/auxtrace.c. If there have any objection, we can come back to add the code for only cs-etm specific.

Quoted 93 lines

Because the decoder is already reset between each auxtrace buffer, populating the queues with fragments of whole buffers based on AUX records has the same affect as resetting the decoder on the AUX record bounds during decode. But this approach only requires a change to one part of the whole flow, rather than many parts.

There is still one TODO about how buffers are found. Currently there is a plain loop as a search, but this could be improved with a binary search or keeping a record of which buffers have already been added and starting the search from the last used place. AUX records and buffers will come in similar orders in the file so the latter would be simpler and probably be good enough. But I'm looking for feedback about the approach as a whole before ironing out all of the details.

Other issues that were fixed by the previous patchsets such as fixing the case where the first aux record comes before the first MMAP record can now be fixed as completely independent issues.

Testing
=======

Testing was done with the following script, to diff the decoding results between the patched and un-patched versions of perf:

	#!/bin/bash
	set -ex

	$1 script -i $3 $4 > split.script
	$2 script -i $3 $4 > default.script

	diff split.script default.script | head -n 20

And it was run like this, with various itrace options depending on the quantity of synthesised events:

	compare.sh ./perf-patched ./perf-default perf-per-cpu-2-threads.data --itrace=i100000ns

No changes in output were observed in the following scenarios:

* Simple per-cpu
perf record -e cs_etm/@tmc_etr0/u top

* Per-thread, single thread

	perf record -e cs_etm/@tmc_etr0/u --per-thread ./threads_C

* Per-thread multiple threads (but only one thread collected data):

	perf record -e cs_etm/@tmc_etr0/u --per-thread --pid 4596,4597

* Per-thread multiple threads (both threads collected data):

	perf record -e cs_etm/@tmc_etr0/u --per-thread --pid 4596,4597

* Per-cpu explicit threads:

	perf record -e cs_etm/@tmc_etr0/u --pid 853,854

* System-wide (per-cpu):
perf record -e cs_etm/@tmc_etr0/u -a

* No data collected (no aux buffers)
Can happen with any command when run for a short period

* Containing truncated records
Can happen with any command

* Containing aux records with 0 size
Can happen with any command

* Snapshot mode

	perf record -e cs_etm/@tmc_etr0/u -a --snapshot
---
 tools/perf/util/cs-etm.c | 159 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 156 insertions(+), 3 deletions(-)
tools/perf/util/cs-etm.c +11 -0
index 64536a6ed10a..053aad4b28cf 100644
@@ -2679,6 +2679,161 @@ static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset,
 	return metadata;
 }
 
+/**
+ * Puts a fragment of an auxtrace buffer into the auxtrace queues based
+ * on the bounds of aux_event, if it matches with the buffer that's at
+ * file_offset.
+ *
+ * Normally, whole auxtrace buffers would be added to the queue. But we
+ * want to reset the decoder for every PERF_RECORD_AUX event, and the decoder
+ * is reset across each buffer, so splitting the buffers up in advance has
+ * the same effect.
+ */
+static int cs_etm__queue_aux_fragment(struct perf_session *session,

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2702

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+						off_t file_offset, size_t sz,
+						struct perf_record_aux *aux_event,
+						struct perf_sample *sample)
+{
+	int err;
+	char buf[PERF_SAMPLE_MAX_SIZE];
+	union perf_event *auxtrace_event_union;
+	struct perf_record_auxtrace *auxtrace_event;
+	union perf_event auxtrace_fragment;
+	bool matchesCpuPid;

It's better to avoid using camel naming.

Quoted 20 lines
tools/perf/util/cs-etm.c +20 -0
+	__u64 aux_offset;
+	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
+						   struct cs_etm_auxtrace,
+						   auxtrace);
+
+	/*
+	 * There should be a PERF_RECORD_AUXTRACE event at the file_offset that we got
+	 * from looping through the auxtrace index.
+	 */
+	err = perf_session__peek_event(session, file_offset, buf,
+				       PERF_SAMPLE_MAX_SIZE, &auxtrace_event_union, NULL);
+	if (err)
+		return err;
+	auxtrace_event = &auxtrace_event_union->auxtrace;
+	if (auxtrace_event->header.type != PERF_RECORD_AUXTRACE)
+		return -EINVAL;
+
+	if (auxtrace_event->header.size < sizeof(struct perf_record_auxtrace) ||
+		auxtrace_event->header.size != sz) {
+		return -EINVAL;

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2732

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+	}
+
+	/*
+	 * In per-thread mode, CPU is set to -1, but TID will be set instead.
+	 * See auxtrace_mmap_params__set_idx()
+	 */
+	if (auxtrace_event->cpu == (__u32) -1)
+		matchesCpuPid = auxtrace_event->tid == sample->tid;
+	else
+		matchesCpuPid = auxtrace_event->cpu == sample->cpu;

If "matchesCpuPid" is false, can directly bail out at here, so can avoid the below unnecessary calculations.

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2745

Quoted 13 lines
tools/perf/util/cs-etm.c +13 -0
+
+	/*
+	 * In snapshot/overwrite mode, the head points to the end of the buffer so aux_offset needs
+	 * to have the size subtracted so it points to the beginning as in normal mode.
+	 */
+	if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE)
+		aux_offset = aux_event->aux_offset - aux_event->aux_size;
+	else
+		aux_offset = aux_event->aux_offset;
+
+	if (matchesCpuPid &&
+	    aux_offset >= auxtrace_event->offset &&
+	    aux_offset + aux_event->aux_size <= auxtrace_event->offset + auxtrace_event->size) {

It's possible that an event PERF_RECORD_AUXTRACE saves AUX trace data for multiple events PERF_RECORD_AUX (if we consider watermark).

So here the logic makes sense for me for checking the buffer range and I don't find any issue.

Quoted 40 lines
tools/perf/util/cs-etm.c +40 -0
+		/*
+		 * If this AUX event was inside this buffer somewhere, create a new auxtrace event
+		 * based on the sizes of the aux event, and queue that fragment.
+		 */
+		auxtrace_fragment.auxtrace = *auxtrace_event;
+		auxtrace_fragment.auxtrace.size = aux_event->aux_size;
+		auxtrace_fragment.auxtrace.offset = aux_offset;
+		file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size;
+		return auxtrace_queues__add_event(&etm->queues,
+					       session,
+					       &auxtrace_fragment,
+					       file_offset,
+					       NULL);
+	}
+
+	/* Wasn't inside this buffer, but there were no parse errors. 1 == 'not found' */
+	return 1;
+}
+
+static int cs_etm__queue_aux_records_cb(struct perf_session *session,
+			     union perf_event *event, u64 offset __maybe_unused,
+			     void *data __maybe_unused)
+{
+	struct perf_sample sample;
+	int ret;
+	struct auxtrace_index_entry *ent;
+	struct auxtrace_index *auxtrace_index;
+	struct evsel *evsel;
+	size_t i;
+	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
+						   struct cs_etm_auxtrace,
+						   auxtrace);
+
+	/* Don't care about any other events, we're only queuing buffers for AUX events */
+	if (event->header.type != PERF_RECORD_AUX)
+		return 0;
+
+	if (event->header.size < sizeof(struct perf_record_aux))
+		return -EINVAL;
+

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2795

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+	/* Truncated Aux records can have 0 size and shouldn't result in anything being queued. */
+	if (!event->aux.aux_size)
+		return 0;
+
+	/*
+	 * Parse the sample, we need the sample_id_all data that comes after the event so that the
+	 * CPU or PID can be matched to an AUXTRACE buffer's CPU or PID.
+	 */
+	evsel = evlist__event2evsel(session->evlist, event);
+	evsel__parse_sample(evsel, event, &sample);

It's good to check the return errors from evsel__parse_sample().

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2800

Quoted 5 lines
tools/perf/util/cs-etm.c +5 -0
+
+	/*
+	 * Loop throuch the auxtrace index to find the buffer that matches up with this
+	 * aux event.
+	 * TODO: binary search?

For binary search, you might need to reorg the auxtrace_index and its entries, I personally think this is not the priority.

Quoted 8 lines
tools/perf/util/cs-etm.c +8 -0
+	 */
+	list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
+		for (i = 0; i < auxtrace_index->nr; i++) {
+			ent = &auxtrace_index->entries[i];
+			ret = cs_etm__queue_aux_fragment(session, ent->file_offset,
+							 ent->sz, &event->aux, &sample);
+			if (!ret) {
+				etm->data_queued = true;

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2818

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+				return 0;
+			} else if (ret < 0) {
+				/*
+				 * Anything other than 1 is an error. Positive values are 'not
+				 * found' we just want to go onto the next one in that case.
+				 */
+				return ret;
+			}
+		}
+	}

It's good to add a warning at here? If cannot find a matched AUXRECORD event and fails to queue the fragment, it's likely caused by the AUX ring buffer's overrun.

Quoted 22 lines
tools/perf/util/cs-etm.c +18 -0
+
+	/*
+	 * We would get here if there are no entries in the index (either no auxtrace buffers or
+	 * no index). Fail silently as there is the possibility of queueing them in
+	 * cs_etm__process_auxtrace_event() if etm->data_queued is still false;
+	 *
+	 * In that scenario, buffers will not be split by AUX records.
+	 */
+	return 0;
+}
+
+static int cs_etm__queue_aux_records(struct perf_session *session)
+{
+	return perf_session__peek_events(session, session->header.data_offset,
+					 session->header.data_size,
+					 cs_etm__queue_aux_records_cb, NULL);
+}
+
 int cs_etm__process_auxtrace_info(union perf_event *event,
 				  struct perf_session *session)
 {

tools/perf/util/cs-etm.c int cs_etm__process_auxtrace_info(union perf_event *event, · line 2886

Quoted 9 lines
tools/perf/util/cs-etm.c +1 -2
@@ -2879,12 +3034,10 @@ int cs_etm__process_auxtrace_info(union perf_event *event,
 	if (err)
 		goto err_delete_thread;
 
-	err = auxtrace_queues__process_index(&etm->queues, session);
+	err = cs_etm__queue_aux_records(session);
 	if (err)
 		goto err_delete_thread;
 
-	etm->data_queued = etm->queues.populated;

Seems to me it's no reason to remove this sentence.

"etm->queues.populated" will be set when call auxtrace_queues__add_event(), so we still can assign it to "etm->data_queued" at here.

Thanks,
Leo

Quoted 7 lines
tools/perf/util/cs-etm.c +0 -1
-
 	return 0;
 
 err_delete_thread:
Signature · 1 line
2.28.0
#3 lore raw

Hi James,

Leo has beat me to the punch on most of the things I noticed in this set, so this is just to complement what has already been pointed out.

Quoted 15 lines · Leo Yan

Hi James,

Quoted 12 lines · James Clark

Populate the auxtrace queues using AUX records rather than whole auxtrace buffers.

This is similar to the auxtrace_queues__process_index() -> auxtrace_queues__add_indexed_event() flow where perf_session__peek_event() is used to read AUXTRACE events out of random positions in the file based on the auxtrace index.

Now there is a second pass using perf_session__peek_events() which loops over all PERF_RECORD_AUX events, finds the corresponding auxtrace buffer, and adds a fragment of that buffer to the auxtrace queues, rather than the whole buffer.

The first loop is enacted by perf_session__peek_events() and processes all the PERF_RECORD_AUX events. The second loop correlates PERF_RECORD_AUX with PERF_RECORD_AUXTRACE.

The above paragraph seems to indicate the opposite.

Quoted 10 lines
Quoted 9 lines

This is a completely new implementation compared to v1 and v2 of the patchset where significant modifications were made to the decoding flow, including saving all of the AUX records, and changing the point where decoding started. These changes had effects on the outcome of decoding, but with this implementation, the output of decoding is identical to perf/core, except in the case where decoding failed due to the issue that this change addresses. No changes to decoding were made, apart from populating the auxtrace queues.

Quoted 6 lines

IMHO, this patch is much better than previous versions! :)

When went through the whole patch, the most code actually is general enough and can consider to place into into util/auxtrace.c. If there have any objection, we can come back to add the code for only cs-etm specific.

I would leave the idea of moving this to util/auxtrace.c alone for the time being.

Quoted 7 lines
Quoted 7 lines

Because the decoder is already reset between each auxtrace buffer, populating the queues with fragments of whole buffers based on AUX records has the same affect as resetting the decoder on the AUX record bounds during decode. But this approach only requires a change to one part of the whole flow, rather than many parts.

Quoted 7 lines
Quoted 7 lines

There is still one TODO about how buffers are found. Currently there is a plain loop as a search, but this could be improved with a binary search or keeping a record of which buffers have already been added and starting the search from the last used place. AUX records and buffers will come in similar orders in the file so the latter would be simpler and probably be good enough. But I'm looking for feedback about the approach as a whole before ironing out all of the details.

I think for the time being what you have here will work just fine.

Quoted 5 lines
Quoted 5 lines

Other issues that were fixed by the previous patchsets such as fixing the case where the first aux record comes before the first MMAP record can now be fixed as completely independent issues.

I assume you did not expect this patch to be merged and as such wrote as much as you could, which is fine. The next revision should have a changelog that will concentrate on why this patch is needed. I think the testing section (below) is useful and should be kept around.

Quoted 64 lines
Quoted 64 lines

Testing
=======

Testing was done with the following script, to diff the decoding results between the patched and un-patched versions of perf:

	#!/bin/bash
	set -ex

	$1 script -i $3 $4 > split.script
	$2 script -i $3 $4 > default.script

	diff split.script default.script | head -n 20

And it was run like this, with various itrace options depending on the quantity of synthesised events:

	compare.sh ./perf-patched ./perf-default perf-per-cpu-2-threads.data --itrace=i100000ns

No changes in output were observed in the following scenarios:

* Simple per-cpu
perf record -e cs_etm/@tmc_etr0/u top

* Per-thread, single thread

	perf record -e cs_etm/@tmc_etr0/u --per-thread ./threads_C

* Per-thread multiple threads (but only one thread collected data):

	perf record -e cs_etm/@tmc_etr0/u --per-thread --pid 4596,4597

* Per-thread multiple threads (both threads collected data):

	perf record -e cs_etm/@tmc_etr0/u --per-thread --pid 4596,4597

* Per-cpu explicit threads:

	perf record -e cs_etm/@tmc_etr0/u --pid 853,854

* System-wide (per-cpu):
perf record -e cs_etm/@tmc_etr0/u -a

* No data collected (no aux buffers)
Can happen with any command when run for a short period

* Containing truncated records
Can happen with any command

* Containing aux records with 0 size
Can happen with any command

* Snapshot mode

	perf record -e cs_etm/@tmc_etr0/u -a --snapshot
---
 tools/perf/util/cs-etm.c | 159 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 156 insertions(+), 3 deletions(-)
tools/perf/util/cs-etm.c +0 -0
index 64536a6ed10a..053aad4b28cf 100644
@@ -2679,6 +2679,161 @@ static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset,
 	return metadata;
 }
 
Quoted 14 lines
Quoted 4 lines
tools/perf/util/cs-etm.c +4 -0
+/**
+ * Puts a fragment of an auxtrace buffer into the auxtrace queues based
+ * on the bounds of aux_event, if it matches with the buffer that's at
+ * file_offset.

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2695

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+ *
+ * Normally, whole auxtrace buffers would be added to the queue. But we
+ * want to reset the decoder for every PERF_RECORD_AUX event, and the decoder
+ * is reset across each buffer, so splitting the buffers up in advance has
+ * the same effect.
+ */
+static int cs_etm__queue_aux_fragment(struct perf_session *session,
+						off_t file_offset, size_t sz,
+						struct perf_record_aux *aux_event,
+						struct perf_sample *sample)

The indentation doesn't work. The 'o' of off_t should be aligned with the 's' of struct on the first line.

Quoted 10 lines

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2702

Quoted 7 lines
tools/perf/util/cs-etm.c +7 -0
+{
+	int err;
+	char buf[PERF_SAMPLE_MAX_SIZE];
+	union perf_event *auxtrace_event_union;
+	struct perf_record_auxtrace *auxtrace_event;
+	union perf_event auxtrace_fragment;
+	bool matchesCpuPid;

It's better to avoid using camel naming.

Leo is correct - I'm surprised checkpatch hasn't complained bitterly.

Quoted 68 lines
Quoted 20 lines
tools/perf/util/cs-etm.c +20 -0
+	__u64 aux_offset;
+	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
+						   struct cs_etm_auxtrace,
+						   auxtrace);
+
+	/*
+	 * There should be a PERF_RECORD_AUXTRACE event at the file_offset that we got
+	 * from looping through the auxtrace index.
+	 */
+	err = perf_session__peek_event(session, file_offset, buf,
+				       PERF_SAMPLE_MAX_SIZE, &auxtrace_event_union, NULL);
+	if (err)
+		return err;
+	auxtrace_event = &auxtrace_event_union->auxtrace;
+	if (auxtrace_event->header.type != PERF_RECORD_AUXTRACE)
+		return -EINVAL;
+
+	if (auxtrace_event->header.size < sizeof(struct perf_record_auxtrace) ||
+		auxtrace_event->header.size != sz) {
+		return -EINVAL;

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2732

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+	}
+
+	/*
+	 * In per-thread mode, CPU is set to -1, but TID will be set instead.
+	 * See auxtrace_mmap_params__set_idx()
+	 */
+	if (auxtrace_event->cpu == (__u32) -1)
+		matchesCpuPid = auxtrace_event->tid == sample->tid;
+	else
+		matchesCpuPid = auxtrace_event->cpu == sample->cpu;

If "matchesCpuPid" is false, can directly bail out at here, so can avoid the below unnecessary calculations.

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2745

Quoted 13 lines
tools/perf/util/cs-etm.c +13 -0
+
+	/*
+	 * In snapshot/overwrite mode, the head points to the end of the buffer so aux_offset needs
+	 * to have the size subtracted so it points to the beginning as in normal mode.
+	 */
+	if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE)
+		aux_offset = aux_event->aux_offset - aux_event->aux_size;
+	else
+		aux_offset = aux_event->aux_offset;
+
+	if (matchesCpuPid &&
+	    aux_offset >= auxtrace_event->offset &&
+	    aux_offset + aux_event->aux_size <= auxtrace_event->offset + auxtrace_event->size) {

It's possible that an event PERF_RECORD_AUXTRACE saves AUX trace data for multiple events PERF_RECORD_AUX (if we consider watermark).

So here the logic makes sense for me for checking the buffer range and I don't find any issue.

Quoted 14 lines
tools/perf/util/cs-etm.c +14 -0
+		/*
+		 * If this AUX event was inside this buffer somewhere, create a new auxtrace event
+		 * based on the sizes of the aux event, and queue that fragment.
+		 */
+		auxtrace_fragment.auxtrace = *auxtrace_event;
+		auxtrace_fragment.auxtrace.size = aux_event->aux_size;
+		auxtrace_fragment.auxtrace.offset = aux_offset;
+		file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size;
+		return auxtrace_queues__add_event(&etm->queues,
+					       session,
+					       &auxtrace_fragment,
+					       file_offset,
+					       NULL);
+	}
Quoted 8 lines

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2767

Quoted 8 lines
tools/perf/util/cs-etm.c +8 -0
+
+	/* Wasn't inside this buffer, but there were no parse errors. 1 == 'not found' */
+	return 1;
+}
+
+static int cs_etm__queue_aux_records_cb(struct perf_session *session,
+			     union perf_event *event, u64 offset __maybe_unused,
+			     void *data __maybe_unused)

Same comment about the indentation.

Quoted 11 lines

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2778

Quoted 11 lines
tools/perf/util/cs-etm.c +11 -0
+{
+	struct perf_sample sample;
+	int ret;
+	struct auxtrace_index_entry *ent;
+	struct auxtrace_index *auxtrace_index;
+	struct evsel *evsel;
+	size_t i;
+	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
+						   struct cs_etm_auxtrace,
+						   auxtrace);
+
	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
						   struct cs_etm_auxtrace,
						   auxtrace);
	struct auxtrace_index *auxtrace_index;
	struct auxtrace_index_entry *ent;
	struct perf_sample sample;
	struct evsel *evsel;
	size_t i;
	int ret;
Quoted 20 lines
Quoted 7 lines
tools/perf/util/cs-etm.c +7 -0
+	/* Don't care about any other events, we're only queuing buffers for AUX events */
+	if (event->header.type != PERF_RECORD_AUX)
+		return 0;
+
+	if (event->header.size < sizeof(struct perf_record_aux))
+		return -EINVAL;
+

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2795

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+	/* Truncated Aux records can have 0 size and shouldn't result in anything being queued. */
+	if (!event->aux.aux_size)
+		return 0;
+
+	/*
+	 * Parse the sample, we need the sample_id_all data that comes after the event so that the
+	 * CPU or PID can be matched to an AUXTRACE buffer's CPU or PID.
+	 */
+	evsel = evlist__event2evsel(session->evlist, event);
+	evsel__parse_sample(evsel, event, &sample);

It's good to check the return errors from evsel__parse_sample().

Quoted 9 lines

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2800

Quoted 5 lines
tools/perf/util/cs-etm.c +5 -0
+
+	/*
+	 * Loop throuch the auxtrace index to find the buffer that matches up with this
+	 * aux event.
+	 * TODO: binary search?

For binary search, you might need to reorg the auxtrace_index and its entries, I personally think this is not the priority.

Here too I agree with Leo.

Quoted 23 lines
Quoted 8 lines
tools/perf/util/cs-etm.c +8 -0
+	 */
+	list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
+		for (i = 0; i < auxtrace_index->nr; i++) {
+			ent = &auxtrace_index->entries[i];
+			ret = cs_etm__queue_aux_fragment(session, ent->file_offset,
+							 ent->sz, &event->aux, &sample);
+			if (!ret) {
+				etm->data_queued = true;

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2818

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+				return 0;
+			} else if (ret < 0) {
+				/*
+				 * Anything other than 1 is an error. Positive values are 'not
+				 * found' we just want to go onto the next one in that case.
+				 */
+				return ret;
+			}
+		}
+	}

It's good to add a warning at here? If cannot find a matched AUXRECORD event and fails to queue the fragment, it's likely caused by the AUX ring buffer's overrun.

Quoted 16 lines
Quoted 6 lines
tools/perf/util/cs-etm.c +6 -0
+
+	/*
+	 * We would get here if there are no entries in the index (either no auxtrace buffers or
+	 * no index). Fail silently as there is the possibility of queueing them in
+	 * cs_etm__process_auxtrace_event() if etm->data_queued is still false;
+	 *

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2834

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+	 * In that scenario, buffers will not be split by AUX records.
+	 */
+	return 0;
+}
+
+static int cs_etm__queue_aux_records(struct perf_session *session)
+{
+	return perf_session__peek_events(session, session->header.data_offset,
+					 session->header.data_size,
+					 cs_etm__queue_aux_records_cb, NULL);

Perfect indentation. Here cs_etm__queue_aux_records() doesn't give us much. I would simply call perf_session__peek_events() from cs_etm__process_auxtrace_info().

Quoted 18 lines
Quoted 6 lines
tools/perf/util/cs-etm.c +2 -0
+}
+
 int cs_etm__process_auxtrace_info(union perf_event *event,
 				  struct perf_session *session)
 {

tools/perf/util/cs-etm.c int cs_etm__process_auxtrace_info(union perf_event *event, · line 2886

Quoted 9 lines
tools/perf/util/cs-etm.c +1 -2
@@ -2879,12 +3034,10 @@ int cs_etm__process_auxtrace_info(union perf_event *event,
 	if (err)
 		goto err_delete_thread;
 
-	err = auxtrace_queues__process_index(&etm->queues, session);
+	err = cs_etm__queue_aux_records(session);
 	if (err)
 		goto err_delete_thread;
 
-	etm->data_queued = etm->queues.populated;

Seems to me it's no reason to remove this sentence.

Quoted 7 lines

"etm->queues.populated" will be set when call auxtrace_queues__add_event(), so we still can assign it to "etm->data_queued" at here.

Thanks,
Leo

I'm happy with what is done in this patch.

Thanks,
Mathieu

Quoted 7 lines
Quoted 7 lines
tools/perf/util/cs-etm.c +0 -1
-
 	return 0;
 
 err_delete_thread:
Signature · 1 line
2.28.0
#4 lore raw
Quoted 22 lines · Mathieu Poirier

Hi James,

Leo has beat me to the punch on most of the things I noticed in this set, so this is just to complement what has already been pointed out.

Quoted 15 lines · Leo Yan

Hi James,

Quoted 12 lines · James Clark

Populate the auxtrace queues using AUX records rather than whole auxtrace buffers.

This is similar to the auxtrace_queues__process_index() -> auxtrace_queues__add_indexed_event() flow where perf_session__peek_event() is used to read AUXTRACE events out of random positions in the file based on the auxtrace index.

Now there is a second pass using perf_session__peek_events() which loops over all PERF_RECORD_AUX events, finds the corresponding auxtrace buffer, and adds a fragment of that buffer to the auxtrace queues, rather than the whole buffer.

Quoted 6 lines

The first loop is enacted by perf_session__peek_events() and processes all the PERF_RECORD_AUX events. The second loop correlates PERF_RECORD_AUX with PERF_RECORD_AUXTRACE.

The above paragraph seems to indicate the opposite.

Hi Mathieu,

Yes, you're right I will reword this. The rest of the comments I also agree with so I will fix them up and submit v5.

Quoted 43 lines
Quoted 10 lines
Quoted 9 lines

This is a completely new implementation compared to v1 and v2 of the patchset where significant modifications were made to the decoding flow, including saving all of the AUX records, and changing the point where decoding started. These changes had effects on the outcome of decoding, but with this implementation, the output of decoding is identical to perf/core, except in the case where decoding failed due to the issue that this change addresses. No changes to decoding were made, apart from populating the auxtrace queues.

Quoted 6 lines

IMHO, this patch is much better than previous versions! :)

When went through the whole patch, the most code actually is general enough and can consider to place into into util/auxtrace.c. If there have any objection, we can come back to add the code for only cs-etm specific.

I would leave the idea of moving this to util/auxtrace.c alone for the time being.

Quoted 7 lines
Quoted 7 lines

Because the decoder is already reset between each auxtrace buffer, populating the queues with fragments of whole buffers based on AUX records has the same affect as resetting the decoder on the AUX record bounds during decode. But this approach only requires a change to one part of the whole flow, rather than many parts.

Quoted 7 lines
Quoted 7 lines

There is still one TODO about how buffers are found. Currently there is a plain loop as a search, but this could be improved with a binary search or keeping a record of which buffers have already been added and starting the search from the last used place. AUX records and buffers will come in similar orders in the file so the latter would be simpler and probably be good enough. But I'm looking for feedback about the approach as a whole before ironing out all of the details.

I think for the time being what you have here will work just fine.

Quoted 5 lines
Quoted 5 lines

Other issues that were fixed by the previous patchsets such as fixing the case where the first aux record comes before the first MMAP record can now be fixed as completely independent issues.

Quoted 4 lines

I assume you did not expect this patch to be merged and as such wrote as much as you could, which is fine. The next revision should have a changelog that will concentrate on why this patch is needed. I think the testing section (below) is useful and should be kept around.

Do you think something like this is enough for the reasoning:

The reason that this change is needed is because AUX records are emitted any time tracing is disabled, for example when the process is scheduled out. Because ETM was disabled and enabled again, the decoder also needs to be reset to force the search for a sync packet. Otherwise there would be fatal decoding errors.

Thanks
James

Quoted 310 lines
Quoted 65 lines
Quoted 65 lines

Testing
=======

Testing was done with the following script, to diff the decoding results between the patched and un-patched versions of perf:

	#!/bin/bash
	set -ex

	$1 script -i $3 $4 > split.script
	$2 script -i $3 $4 > default.script

	diff split.script default.script | head -n 20

And it was run like this, with various itrace options depending on the quantity of synthesised events:

	compare.sh ./perf-patched ./perf-default perf-per-cpu-2-threads.data --itrace=i100000ns

No changes in output were observed in the following scenarios:

* Simple per-cpu
perf record -e cs_etm/@tmc_etr0/u top

* Per-thread, single thread

	perf record -e cs_etm/@tmc_etr0/u --per-thread ./threads_C

* Per-thread multiple threads (but only one thread collected data):

	perf record -e cs_etm/@tmc_etr0/u --per-thread --pid 4596,4597

* Per-thread multiple threads (both threads collected data):

	perf record -e cs_etm/@tmc_etr0/u --per-thread --pid 4596,4597

* Per-cpu explicit threads:

	perf record -e cs_etm/@tmc_etr0/u --pid 853,854

* System-wide (per-cpu):
perf record -e cs_etm/@tmc_etr0/u -a

* No data collected (no aux buffers)
Can happen with any command when run for a short period

* Containing truncated records
Can happen with any command

* Containing aux records with 0 size
Can happen with any command

* Snapshot mode

	perf record -e cs_etm/@tmc_etr0/u -a --snapshot
---
 tools/perf/util/cs-etm.c | 159 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 156 insertions(+), 3 deletions(-)
tools/perf/util/cs-etm.c +0 -0
index 64536a6ed10a..053aad4b28cf 100644
@@ -2679,6 +2679,161 @@ static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset,
 	return metadata;
 }
 
Quoted 14 lines
Quoted 4 lines
tools/perf/util/cs-etm.c +4 -0
+/**
+ * Puts a fragment of an auxtrace buffer into the auxtrace queues based
+ * on the bounds of aux_event, if it matches with the buffer that's at
+ * file_offset.

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2695

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+ *
+ * Normally, whole auxtrace buffers would be added to the queue. But we
+ * want to reset the decoder for every PERF_RECORD_AUX event, and the decoder
+ * is reset across each buffer, so splitting the buffers up in advance has
+ * the same effect.
+ */
+static int cs_etm__queue_aux_fragment(struct perf_session *session,
+						off_t file_offset, size_t sz,
+						struct perf_record_aux *aux_event,
+						struct perf_sample *sample)

The indentation doesn't work. The 'o' of off_t should be aligned with the 's' of struct on the first line.

Quoted 10 lines

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2702

Quoted 7 lines
tools/perf/util/cs-etm.c +7 -0
+{
+	int err;
+	char buf[PERF_SAMPLE_MAX_SIZE];
+	union perf_event *auxtrace_event_union;
+	struct perf_record_auxtrace *auxtrace_event;
+	union perf_event auxtrace_fragment;
+	bool matchesCpuPid;

It's better to avoid using camel naming.

Leo is correct - I'm surprised checkpatch hasn't complained bitterly.

Quoted 68 lines
Quoted 20 lines
tools/perf/util/cs-etm.c +20 -0
+	__u64 aux_offset;
+	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
+						   struct cs_etm_auxtrace,
+						   auxtrace);
+
+	/*
+	 * There should be a PERF_RECORD_AUXTRACE event at the file_offset that we got
+	 * from looping through the auxtrace index.
+	 */
+	err = perf_session__peek_event(session, file_offset, buf,
+				       PERF_SAMPLE_MAX_SIZE, &auxtrace_event_union, NULL);
+	if (err)
+		return err;
+	auxtrace_event = &auxtrace_event_union->auxtrace;
+	if (auxtrace_event->header.type != PERF_RECORD_AUXTRACE)
+		return -EINVAL;
+
+	if (auxtrace_event->header.size < sizeof(struct perf_record_auxtrace) ||
+		auxtrace_event->header.size != sz) {
+		return -EINVAL;

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2732

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+	}
+
+	/*
+	 * In per-thread mode, CPU is set to -1, but TID will be set instead.
+	 * See auxtrace_mmap_params__set_idx()
+	 */
+	if (auxtrace_event->cpu == (__u32) -1)
+		matchesCpuPid = auxtrace_event->tid == sample->tid;
+	else
+		matchesCpuPid = auxtrace_event->cpu == sample->cpu;

If "matchesCpuPid" is false, can directly bail out at here, so can avoid the below unnecessary calculations.

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2745

Quoted 13 lines
tools/perf/util/cs-etm.c +13 -0
+
+	/*
+	 * In snapshot/overwrite mode, the head points to the end of the buffer so aux_offset needs
+	 * to have the size subtracted so it points to the beginning as in normal mode.
+	 */
+	if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE)
+		aux_offset = aux_event->aux_offset - aux_event->aux_size;
+	else
+		aux_offset = aux_event->aux_offset;
+
+	if (matchesCpuPid &&
+	    aux_offset >= auxtrace_event->offset &&
+	    aux_offset + aux_event->aux_size <= auxtrace_event->offset + auxtrace_event->size) {

It's possible that an event PERF_RECORD_AUXTRACE saves AUX trace data for multiple events PERF_RECORD_AUX (if we consider watermark).

So here the logic makes sense for me for checking the buffer range and I don't find any issue.

Quoted 14 lines
tools/perf/util/cs-etm.c +14 -0
+		/*
+		 * If this AUX event was inside this buffer somewhere, create a new auxtrace event
+		 * based on the sizes of the aux event, and queue that fragment.
+		 */
+		auxtrace_fragment.auxtrace = *auxtrace_event;
+		auxtrace_fragment.auxtrace.size = aux_event->aux_size;
+		auxtrace_fragment.auxtrace.offset = aux_offset;
+		file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size;
+		return auxtrace_queues__add_event(&etm->queues,
+					       session,
+					       &auxtrace_fragment,
+					       file_offset,
+					       NULL);
+	}
Quoted 8 lines

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2767

Quoted 8 lines
tools/perf/util/cs-etm.c +8 -0
+
+	/* Wasn't inside this buffer, but there were no parse errors. 1 == 'not found' */
+	return 1;
+}
+
+static int cs_etm__queue_aux_records_cb(struct perf_session *session,
+			     union perf_event *event, u64 offset __maybe_unused,
+			     void *data __maybe_unused)

Same comment about the indentation.

Quoted 11 lines

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2778

Quoted 11 lines
tools/perf/util/cs-etm.c +11 -0
+{
+	struct perf_sample sample;
+	int ret;
+	struct auxtrace_index_entry *ent;
+	struct auxtrace_index *auxtrace_index;
+	struct evsel *evsel;
+	size_t i;
+	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
+						   struct cs_etm_auxtrace,
+						   auxtrace);
+
	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
						   struct cs_etm_auxtrace,
						   auxtrace);
	struct auxtrace_index *auxtrace_index;
	struct auxtrace_index_entry *ent;
	struct perf_sample sample;
	struct evsel *evsel;
	size_t i;
	int ret;
Quoted 20 lines
Quoted 7 lines
tools/perf/util/cs-etm.c +7 -0
+	/* Don't care about any other events, we're only queuing buffers for AUX events */
+	if (event->header.type != PERF_RECORD_AUX)
+		return 0;
+
+	if (event->header.size < sizeof(struct perf_record_aux))
+		return -EINVAL;
+

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2795

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+	/* Truncated Aux records can have 0 size and shouldn't result in anything being queued. */
+	if (!event->aux.aux_size)
+		return 0;
+
+	/*
+	 * Parse the sample, we need the sample_id_all data that comes after the event so that the
+	 * CPU or PID can be matched to an AUXTRACE buffer's CPU or PID.
+	 */
+	evsel = evlist__event2evsel(session->evlist, event);
+	evsel__parse_sample(evsel, event, &sample);

It's good to check the return errors from evsel__parse_sample().

Quoted 9 lines

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2800

Quoted 5 lines
tools/perf/util/cs-etm.c +5 -0
+
+	/*
+	 * Loop throuch the auxtrace index to find the buffer that matches up with this
+	 * aux event.
+	 * TODO: binary search?

For binary search, you might need to reorg the auxtrace_index and its entries, I personally think this is not the priority.

Here too I agree with Leo.

Quoted 23 lines
Quoted 8 lines
tools/perf/util/cs-etm.c +8 -0
+	 */
+	list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
+		for (i = 0; i < auxtrace_index->nr; i++) {
+			ent = &auxtrace_index->entries[i];
+			ret = cs_etm__queue_aux_fragment(session, ent->file_offset,
+							 ent->sz, &event->aux, &sample);
+			if (!ret) {
+				etm->data_queued = true;

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2818

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+				return 0;
+			} else if (ret < 0) {
+				/*
+				 * Anything other than 1 is an error. Positive values are 'not
+				 * found' we just want to go onto the next one in that case.
+				 */
+				return ret;
+			}
+		}
+	}

It's good to add a warning at here? If cannot find a matched AUXRECORD event and fails to queue the fragment, it's likely caused by the AUX ring buffer's overrun.

Quoted 16 lines
Quoted 6 lines
tools/perf/util/cs-etm.c +6 -0
+
+	/*
+	 * We would get here if there are no entries in the index (either no auxtrace buffers or
+	 * no index). Fail silently as there is the possibility of queueing them in
+	 * cs_etm__process_auxtrace_event() if etm->data_queued is still false;
+	 *

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2834

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+	 * In that scenario, buffers will not be split by AUX records.
+	 */
+	return 0;
+}
+
+static int cs_etm__queue_aux_records(struct perf_session *session)
+{
+	return perf_session__peek_events(session, session->header.data_offset,
+					 session->header.data_size,
+					 cs_etm__queue_aux_records_cb, NULL);

Perfect indentation. Here cs_etm__queue_aux_records() doesn't give us much. I would simply call perf_session__peek_events() from cs_etm__process_auxtrace_info().

Quoted 18 lines
Quoted 6 lines
tools/perf/util/cs-etm.c +2 -0
+}
+
 int cs_etm__process_auxtrace_info(union perf_event *event,
 				  struct perf_session *session)
 {

tools/perf/util/cs-etm.c int cs_etm__process_auxtrace_info(union perf_event *event, · line 2886

Quoted 9 lines
tools/perf/util/cs-etm.c +1 -2
@@ -2879,12 +3034,10 @@ int cs_etm__process_auxtrace_info(union perf_event *event,
 	if (err)
 		goto err_delete_thread;
 
-	err = auxtrace_queues__process_index(&etm->queues, session);
+	err = cs_etm__queue_aux_records(session);
 	if (err)
 		goto err_delete_thread;
 
-	etm->data_queued = etm->queues.populated;

Seems to me it's no reason to remove this sentence.

Quoted 7 lines

"etm->queues.populated" will be set when call auxtrace_queues__add_event(), so we still can assign it to "etm->data_queued" at here.

Thanks,
Leo

I'm happy with what is done in this patch.

Thanks,
Mathieu

Quoted 7 lines
Quoted 7 lines
tools/perf/util/cs-etm.c +0 -1
-
 	return 0;
 
 err_delete_thread:
Signature · 1 line
2.28.0
#5 lore raw
Quoted 85 lines · James Clark
Quoted 22 lines · Mathieu Poirier

Hi James,

Leo has beat me to the punch on most of the things I noticed in this set, so this is just to complement what has already been pointed out.

Quoted 15 lines · Leo Yan

Hi James,

Quoted 12 lines · James Clark

Populate the auxtrace queues using AUX records rather than whole auxtrace buffers.

This is similar to the auxtrace_queues__process_index() -> auxtrace_queues__add_indexed_event() flow where perf_session__peek_event() is used to read AUXTRACE events out of random positions in the file based on the auxtrace index.

Now there is a second pass using perf_session__peek_events() which loops over all PERF_RECORD_AUX events, finds the corresponding auxtrace buffer, and adds a fragment of that buffer to the auxtrace queues, rather than the whole buffer.

Quoted 6 lines

The first loop is enacted by perf_session__peek_events() and processes all the PERF_RECORD_AUX events. The second loop correlates PERF_RECORD_AUX with PERF_RECORD_AUXTRACE.

The above paragraph seems to indicate the opposite.

Hi Mathieu,

Yes, you're right I will reword this. The rest of the comments I also agree with so I will fix them up and submit v5.

Quoted 43 lines
Quoted 10 lines
Quoted 9 lines

This is a completely new implementation compared to v1 and v2 of the patchset where significant modifications were made to the decoding flow, including saving all of the AUX records, and changing the point where decoding started. These changes had effects on the outcome of decoding, but with this implementation, the output of decoding is identical to perf/core, except in the case where decoding failed due to the issue that this change addresses. No changes to decoding were made, apart from populating the auxtrace queues.

Quoted 6 lines

IMHO, this patch is much better than previous versions! :)

When went through the whole patch, the most code actually is general enough and can consider to place into into util/auxtrace.c. If there have any objection, we can come back to add the code for only cs-etm specific.

I would leave the idea of moving this to util/auxtrace.c alone for the time being.

Quoted 7 lines
Quoted 7 lines

Because the decoder is already reset between each auxtrace buffer, populating the queues with fragments of whole buffers based on AUX records has the same affect as resetting the decoder on the AUX record bounds during decode. But this approach only requires a change to one part of the whole flow, rather than many parts.

Quoted 7 lines
Quoted 7 lines

There is still one TODO about how buffers are found. Currently there is a plain loop as a search, but this could be improved with a binary search or keeping a record of which buffers have already been added and starting the search from the last used place. AUX records and buffers will come in similar orders in the file so the latter would be simpler and probably be good enough. But I'm looking for feedback about the approach as a whole before ironing out all of the details.

I think for the time being what you have here will work just fine.

Quoted 5 lines
Quoted 5 lines

Other issues that were fixed by the previous patchsets such as fixing the case where the first aux record comes before the first MMAP record can now be fixed as completely independent issues.

Quoted 4 lines

I assume you did not expect this patch to be merged and as such wrote as much as you could, which is fine. The next revision should have a changelog that will concentrate on why this patch is needed. I think the testing section (below) is useful and should be kept around.

Quoted 7 lines

Do you think something like this is enough for the reasoning:

The reason that this change is needed is because AUX records are emitted any time tracing is disabled, for example when the process is scheduled out. Because ETM was disabled and enabled again, the decoder also needs to be reset to force the search for a sync packet. Otherwise there would be fatal decoding errors.

Yes, that will work. It describes _why_ this patch is needed.

Quoted 314 lines

Thanks
James

Quoted 310 lines
Quoted 65 lines
Quoted 65 lines

Testing
=======

Testing was done with the following script, to diff the decoding results between the patched and un-patched versions of perf:

	#!/bin/bash
	set -ex

	$1 script -i $3 $4 > split.script
	$2 script -i $3 $4 > default.script

	diff split.script default.script | head -n 20

And it was run like this, with various itrace options depending on the quantity of synthesised events:

	compare.sh ./perf-patched ./perf-default perf-per-cpu-2-threads.data --itrace=i100000ns

No changes in output were observed in the following scenarios:

* Simple per-cpu
perf record -e cs_etm/@tmc_etr0/u top

* Per-thread, single thread

	perf record -e cs_etm/@tmc_etr0/u --per-thread ./threads_C

* Per-thread multiple threads (but only one thread collected data):

	perf record -e cs_etm/@tmc_etr0/u --per-thread --pid 4596,4597

* Per-thread multiple threads (both threads collected data):

	perf record -e cs_etm/@tmc_etr0/u --per-thread --pid 4596,4597

* Per-cpu explicit threads:

	perf record -e cs_etm/@tmc_etr0/u --pid 853,854

* System-wide (per-cpu):
perf record -e cs_etm/@tmc_etr0/u -a

* No data collected (no aux buffers)
Can happen with any command when run for a short period

* Containing truncated records
Can happen with any command

* Containing aux records with 0 size
Can happen with any command

* Snapshot mode

	perf record -e cs_etm/@tmc_etr0/u -a --snapshot
---
 tools/perf/util/cs-etm.c | 159 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 156 insertions(+), 3 deletions(-)
tools/perf/util/cs-etm.c +0 -0
index 64536a6ed10a..053aad4b28cf 100644
@@ -2679,6 +2679,161 @@ static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset,
 	return metadata;
 }
 
Quoted 14 lines
Quoted 4 lines
tools/perf/util/cs-etm.c +4 -0
+/**
+ * Puts a fragment of an auxtrace buffer into the auxtrace queues based
+ * on the bounds of aux_event, if it matches with the buffer that's at
+ * file_offset.

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2695

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+ *
+ * Normally, whole auxtrace buffers would be added to the queue. But we
+ * want to reset the decoder for every PERF_RECORD_AUX event, and the decoder
+ * is reset across each buffer, so splitting the buffers up in advance has
+ * the same effect.
+ */
+static int cs_etm__queue_aux_fragment(struct perf_session *session,
+						off_t file_offset, size_t sz,
+						struct perf_record_aux *aux_event,
+						struct perf_sample *sample)

The indentation doesn't work. The 'o' of off_t should be aligned with the 's' of struct on the first line.

Quoted 10 lines

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2702

Quoted 7 lines
tools/perf/util/cs-etm.c +7 -0
+{
+	int err;
+	char buf[PERF_SAMPLE_MAX_SIZE];
+	union perf_event *auxtrace_event_union;
+	struct perf_record_auxtrace *auxtrace_event;
+	union perf_event auxtrace_fragment;
+	bool matchesCpuPid;

It's better to avoid using camel naming.

Leo is correct - I'm surprised checkpatch hasn't complained bitterly.

Quoted 68 lines
Quoted 20 lines
tools/perf/util/cs-etm.c +20 -0
+	__u64 aux_offset;
+	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
+						   struct cs_etm_auxtrace,
+						   auxtrace);
+
+	/*
+	 * There should be a PERF_RECORD_AUXTRACE event at the file_offset that we got
+	 * from looping through the auxtrace index.
+	 */
+	err = perf_session__peek_event(session, file_offset, buf,
+				       PERF_SAMPLE_MAX_SIZE, &auxtrace_event_union, NULL);
+	if (err)
+		return err;
+	auxtrace_event = &auxtrace_event_union->auxtrace;
+	if (auxtrace_event->header.type != PERF_RECORD_AUXTRACE)
+		return -EINVAL;
+
+	if (auxtrace_event->header.size < sizeof(struct perf_record_auxtrace) ||
+		auxtrace_event->header.size != sz) {
+		return -EINVAL;

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2732

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+	}
+
+	/*
+	 * In per-thread mode, CPU is set to -1, but TID will be set instead.
+	 * See auxtrace_mmap_params__set_idx()
+	 */
+	if (auxtrace_event->cpu == (__u32) -1)
+		matchesCpuPid = auxtrace_event->tid == sample->tid;
+	else
+		matchesCpuPid = auxtrace_event->cpu == sample->cpu;

If "matchesCpuPid" is false, can directly bail out at here, so can avoid the below unnecessary calculations.

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2745

Quoted 13 lines
tools/perf/util/cs-etm.c +13 -0
+
+	/*
+	 * In snapshot/overwrite mode, the head points to the end of the buffer so aux_offset needs
+	 * to have the size subtracted so it points to the beginning as in normal mode.
+	 */
+	if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE)
+		aux_offset = aux_event->aux_offset - aux_event->aux_size;
+	else
+		aux_offset = aux_event->aux_offset;
+
+	if (matchesCpuPid &&
+	    aux_offset >= auxtrace_event->offset &&
+	    aux_offset + aux_event->aux_size <= auxtrace_event->offset + auxtrace_event->size) {

It's possible that an event PERF_RECORD_AUXTRACE saves AUX trace data for multiple events PERF_RECORD_AUX (if we consider watermark).

So here the logic makes sense for me for checking the buffer range and I don't find any issue.

Quoted 14 lines
tools/perf/util/cs-etm.c +14 -0
+		/*
+		 * If this AUX event was inside this buffer somewhere, create a new auxtrace event
+		 * based on the sizes of the aux event, and queue that fragment.
+		 */
+		auxtrace_fragment.auxtrace = *auxtrace_event;
+		auxtrace_fragment.auxtrace.size = aux_event->aux_size;
+		auxtrace_fragment.auxtrace.offset = aux_offset;
+		file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size;
+		return auxtrace_queues__add_event(&etm->queues,
+					       session,
+					       &auxtrace_fragment,
+					       file_offset,
+					       NULL);
+	}
Quoted 8 lines

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2767

Quoted 8 lines
tools/perf/util/cs-etm.c +8 -0
+
+	/* Wasn't inside this buffer, but there were no parse errors. 1 == 'not found' */
+	return 1;
+}
+
+static int cs_etm__queue_aux_records_cb(struct perf_session *session,
+			     union perf_event *event, u64 offset __maybe_unused,
+			     void *data __maybe_unused)

Same comment about the indentation.

Quoted 11 lines

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2778

Quoted 11 lines
tools/perf/util/cs-etm.c +11 -0
+{
+	struct perf_sample sample;
+	int ret;
+	struct auxtrace_index_entry *ent;
+	struct auxtrace_index *auxtrace_index;
+	struct evsel *evsel;
+	size_t i;
+	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
+						   struct cs_etm_auxtrace,
+						   auxtrace);
+
	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
						   struct cs_etm_auxtrace,
						   auxtrace);
	struct auxtrace_index *auxtrace_index;
	struct auxtrace_index_entry *ent;
	struct perf_sample sample;
	struct evsel *evsel;
	size_t i;
	int ret;
Quoted 20 lines
Quoted 7 lines
tools/perf/util/cs-etm.c +7 -0
+	/* Don't care about any other events, we're only queuing buffers for AUX events */
+	if (event->header.type != PERF_RECORD_AUX)
+		return 0;
+
+	if (event->header.size < sizeof(struct perf_record_aux))
+		return -EINVAL;
+

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2795

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+	/* Truncated Aux records can have 0 size and shouldn't result in anything being queued. */
+	if (!event->aux.aux_size)
+		return 0;
+
+	/*
+	 * Parse the sample, we need the sample_id_all data that comes after the event so that the
+	 * CPU or PID can be matched to an AUXTRACE buffer's CPU or PID.
+	 */
+	evsel = evlist__event2evsel(session->evlist, event);
+	evsel__parse_sample(evsel, event, &sample);

It's good to check the return errors from evsel__parse_sample().

Quoted 9 lines

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2800

Quoted 5 lines
tools/perf/util/cs-etm.c +5 -0
+
+	/*
+	 * Loop throuch the auxtrace index to find the buffer that matches up with this
+	 * aux event.
+	 * TODO: binary search?

For binary search, you might need to reorg the auxtrace_index and its entries, I personally think this is not the priority.

Here too I agree with Leo.

Quoted 23 lines
Quoted 8 lines
tools/perf/util/cs-etm.c +8 -0
+	 */
+	list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
+		for (i = 0; i < auxtrace_index->nr; i++) {
+			ent = &auxtrace_index->entries[i];
+			ret = cs_etm__queue_aux_fragment(session, ent->file_offset,
+							 ent->sz, &event->aux, &sample);
+			if (!ret) {
+				etm->data_queued = true;

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2818

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+				return 0;
+			} else if (ret < 0) {
+				/*
+				 * Anything other than 1 is an error. Positive values are 'not
+				 * found' we just want to go onto the next one in that case.
+				 */
+				return ret;
+			}
+		}
+	}

It's good to add a warning at here? If cannot find a matched AUXRECORD event and fails to queue the fragment, it's likely caused by the AUX ring buffer's overrun.

Quoted 16 lines
Quoted 6 lines
tools/perf/util/cs-etm.c +6 -0
+
+	/*
+	 * We would get here if there are no entries in the index (either no auxtrace buffers or
+	 * no index). Fail silently as there is the possibility of queueing them in
+	 * cs_etm__process_auxtrace_event() if etm->data_queued is still false;
+	 *

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2834

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+	 * In that scenario, buffers will not be split by AUX records.
+	 */
+	return 0;
+}
+
+static int cs_etm__queue_aux_records(struct perf_session *session)
+{
+	return perf_session__peek_events(session, session->header.data_offset,
+					 session->header.data_size,
+					 cs_etm__queue_aux_records_cb, NULL);

Perfect indentation. Here cs_etm__queue_aux_records() doesn't give us much. I would simply call perf_session__peek_events() from cs_etm__process_auxtrace_info().

Quoted 18 lines
Quoted 6 lines
tools/perf/util/cs-etm.c +2 -0
+}
+
 int cs_etm__process_auxtrace_info(union perf_event *event,
 				  struct perf_session *session)
 {

tools/perf/util/cs-etm.c int cs_etm__process_auxtrace_info(union perf_event *event, · line 2886

Quoted 9 lines
tools/perf/util/cs-etm.c +1 -2
@@ -2879,12 +3034,10 @@ int cs_etm__process_auxtrace_info(union perf_event *event,
 	if (err)
 		goto err_delete_thread;
 
-	err = auxtrace_queues__process_index(&etm->queues, session);
+	err = cs_etm__queue_aux_records(session);
 	if (err)
 		goto err_delete_thread;
 
-	etm->data_queued = etm->queues.populated;

Seems to me it's no reason to remove this sentence.

Quoted 7 lines

"etm->queues.populated" will be set when call auxtrace_queues__add_event(), so we still can assign it to "etm->data_queued" at here.

Thanks,
Leo

I'm happy with what is done in this patch.

Thanks,
Mathieu

Quoted 7 lines
Quoted 7 lines
tools/perf/util/cs-etm.c +0 -1
-
 	return 0;
 
 err_delete_thread:
Signature · 1 line
2.28.0
#6 lore raw
In reply to Leo Yan
Quoted 245 lines · Leo Yan

Hi James,

Quoted 13 lines · James Clark

Populate the auxtrace queues using AUX records rather than whole auxtrace buffers.

This is similar to the auxtrace_queues__process_index() -> auxtrace_queues__add_indexed_event() flow where perf_session__peek_event() is used to read AUXTRACE events out of random positions in the file based on the auxtrace index.

Now there is a second pass using perf_session__peek_events() which loops over all PERF_RECORD_AUX events, finds the corresponding auxtrace buffer, and adds a fragment of that buffer to the auxtrace queues, rather than the whole buffer.

Quoted 8 lines

This is a completely new implementation compared to v1 and v2 of the patchset where significant modifications were made to the decoding flow, including saving all of the AUX records, and changing the point where decoding started. These changes had effects on the outcome of decoding, but with this implementation, the output of decoding is identical to perf/core, except in the case where decoding failed due to the issue that this change addresses. No changes to decoding were made, apart from populating the auxtrace queues.

IMHO, this patch is much better than previous versions! :)

When went through the whole patch, the most code actually is general enough and can consider to place into into util/auxtrace.c. If there have any objection, we can come back to add the code for only cs-etm specific.

Quoted 93 lines

Because the decoder is already reset between each auxtrace buffer, populating the queues with fragments of whole buffers based on AUX records has the same affect as resetting the decoder on the AUX record bounds during decode. But this approach only requires a change to one part of the whole flow, rather than many parts.

There is still one TODO about how buffers are found. Currently there is a plain loop as a search, but this could be improved with a binary search or keeping a record of which buffers have already been added and starting the search from the last used place. AUX records and buffers will come in similar orders in the file so the latter would be simpler and probably be good enough. But I'm looking for feedback about the approach as a whole before ironing out all of the details.

Other issues that were fixed by the previous patchsets such as fixing the case where the first aux record comes before the first MMAP record can now be fixed as completely independent issues.

Testing
=======

Testing was done with the following script, to diff the decoding results between the patched and un-patched versions of perf:

	#!/bin/bash
	set -ex

	$1 script -i $3 $4 > split.script
	$2 script -i $3 $4 > default.script

	diff split.script default.script | head -n 20

And it was run like this, with various itrace options depending on the quantity of synthesised events:

	compare.sh ./perf-patched ./perf-default perf-per-cpu-2-threads.data --itrace=i100000ns

No changes in output were observed in the following scenarios:

* Simple per-cpu
perf record -e cs_etm/@tmc_etr0/u top

* Per-thread, single thread

	perf record -e cs_etm/@tmc_etr0/u --per-thread ./threads_C

* Per-thread multiple threads (but only one thread collected data):

	perf record -e cs_etm/@tmc_etr0/u --per-thread --pid 4596,4597

* Per-thread multiple threads (both threads collected data):

	perf record -e cs_etm/@tmc_etr0/u --per-thread --pid 4596,4597

* Per-cpu explicit threads:

	perf record -e cs_etm/@tmc_etr0/u --pid 853,854

* System-wide (per-cpu):
perf record -e cs_etm/@tmc_etr0/u -a

* No data collected (no aux buffers)
Can happen with any command when run for a short period

* Containing truncated records
Can happen with any command

* Containing aux records with 0 size
Can happen with any command

* Snapshot mode

	perf record -e cs_etm/@tmc_etr0/u -a --snapshot
---
 tools/perf/util/cs-etm.c | 159 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 156 insertions(+), 3 deletions(-)
tools/perf/util/cs-etm.c +11 -0
index 64536a6ed10a..053aad4b28cf 100644
@@ -2679,6 +2679,161 @@ static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset,
 	return metadata;
 }
 
+/**
+ * Puts a fragment of an auxtrace buffer into the auxtrace queues based
+ * on the bounds of aux_event, if it matches with the buffer that's at
+ * file_offset.
+ *
+ * Normally, whole auxtrace buffers would be added to the queue. But we
+ * want to reset the decoder for every PERF_RECORD_AUX event, and the decoder
+ * is reset across each buffer, so splitting the buffers up in advance has
+ * the same effect.
+ */
+static int cs_etm__queue_aux_fragment(struct perf_session *session,

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2702

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+						off_t file_offset, size_t sz,
+						struct perf_record_aux *aux_event,
+						struct perf_sample *sample)
+{
+	int err;
+	char buf[PERF_SAMPLE_MAX_SIZE];
+	union perf_event *auxtrace_event_union;
+	struct perf_record_auxtrace *auxtrace_event;
+	union perf_event auxtrace_fragment;
+	bool matchesCpuPid;

It's better to avoid using camel naming.

Quoted 20 lines
tools/perf/util/cs-etm.c +20 -0
+	__u64 aux_offset;
+	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
+						   struct cs_etm_auxtrace,
+						   auxtrace);
+
+	/*
+	 * There should be a PERF_RECORD_AUXTRACE event at the file_offset that we got
+	 * from looping through the auxtrace index.
+	 */
+	err = perf_session__peek_event(session, file_offset, buf,
+				       PERF_SAMPLE_MAX_SIZE, &auxtrace_event_union, NULL);
+	if (err)
+		return err;
+	auxtrace_event = &auxtrace_event_union->auxtrace;
+	if (auxtrace_event->header.type != PERF_RECORD_AUXTRACE)
+		return -EINVAL;
+
+	if (auxtrace_event->header.size < sizeof(struct perf_record_auxtrace) ||
+		auxtrace_event->header.size != sz) {
+		return -EINVAL;

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2732

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+	}
+
+	/*
+	 * In per-thread mode, CPU is set to -1, but TID will be set instead.
+	 * See auxtrace_mmap_params__set_idx()
+	 */
+	if (auxtrace_event->cpu == (__u32) -1)
+		matchesCpuPid = auxtrace_event->tid == sample->tid;
+	else
+		matchesCpuPid = auxtrace_event->cpu == sample->cpu;

If "matchesCpuPid" is false, can directly bail out at here, so can avoid the below unnecessary calculations.

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2745

Quoted 13 lines
tools/perf/util/cs-etm.c +13 -0
+
+	/*
+	 * In snapshot/overwrite mode, the head points to the end of the buffer so aux_offset needs
+	 * to have the size subtracted so it points to the beginning as in normal mode.
+	 */
+	if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE)
+		aux_offset = aux_event->aux_offset - aux_event->aux_size;
+	else
+		aux_offset = aux_event->aux_offset;
+
+	if (matchesCpuPid &&
+	    aux_offset >= auxtrace_event->offset &&
+	    aux_offset + aux_event->aux_size <= auxtrace_event->offset + auxtrace_event->size) {

It's possible that an event PERF_RECORD_AUXTRACE saves AUX trace data for multiple events PERF_RECORD_AUX (if we consider watermark).

So here the logic makes sense for me for checking the buffer range and I don't find any issue.

Quoted 40 lines
tools/perf/util/cs-etm.c +40 -0
+		/*
+		 * If this AUX event was inside this buffer somewhere, create a new auxtrace event
+		 * based on the sizes of the aux event, and queue that fragment.
+		 */
+		auxtrace_fragment.auxtrace = *auxtrace_event;
+		auxtrace_fragment.auxtrace.size = aux_event->aux_size;
+		auxtrace_fragment.auxtrace.offset = aux_offset;
+		file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size;
+		return auxtrace_queues__add_event(&etm->queues,
+					       session,
+					       &auxtrace_fragment,
+					       file_offset,
+					       NULL);
+	}
+
+	/* Wasn't inside this buffer, but there were no parse errors. 1 == 'not found' */
+	return 1;
+}
+
+static int cs_etm__queue_aux_records_cb(struct perf_session *session,
+			     union perf_event *event, u64 offset __maybe_unused,
+			     void *data __maybe_unused)
+{
+	struct perf_sample sample;
+	int ret;
+	struct auxtrace_index_entry *ent;
+	struct auxtrace_index *auxtrace_index;
+	struct evsel *evsel;
+	size_t i;
+	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
+						   struct cs_etm_auxtrace,
+						   auxtrace);
+
+	/* Don't care about any other events, we're only queuing buffers for AUX events */
+	if (event->header.type != PERF_RECORD_AUX)
+		return 0;
+
+	if (event->header.size < sizeof(struct perf_record_aux))
+		return -EINVAL;
+

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2795

Quoted 10 lines
tools/perf/util/cs-etm.c +10 -0
+	/* Truncated Aux records can have 0 size and shouldn't result in anything being queued. */
+	if (!event->aux.aux_size)
+		return 0;
+
+	/*
+	 * Parse the sample, we need the sample_id_all data that comes after the event so that the
+	 * CPU or PID can be matched to an AUXTRACE buffer's CPU or PID.
+	 */
+	evsel = evlist__event2evsel(session->evlist, event);
+	evsel__parse_sample(evsel, event, &sample);

It's good to check the return errors from evsel__parse_sample().

Quoted 8 lines

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2800

Quoted 5 lines
tools/perf/util/cs-etm.c +5 -0
+
+	/*
+	 * Loop throuch the auxtrace index to find the buffer that matches up with this
+	 * aux event.
+	 * TODO: binary search?

For binary search, you might need to reorg the auxtrace_index and its entries, I personally think this is not the priority.

I've also thought about this, and because in general there are so few AUXTRACE buffers compared to AUX records that I also think the loop is fine. And Mathieu agrees in his other message.

Quoted 15 lines
Quoted 15 lines
tools/perf/util/cs-etm.c +14 -0
+	 */
+	list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
+		for (i = 0; i < auxtrace_index->nr; i++) {
+			ent = &auxtrace_index->entries[i];
+			ret = cs_etm__queue_aux_fragment(session, ent->file_offset,
+							 ent->sz, &event->aux, &sample);
+			if (!ret) {
+				etm->data_queued = true;
+				return 0;
+			} else if (ret < 0) {
+				/*
+				 * Anything other than 1 is an error. Positive values are 'not
+				 * found' we just want to go onto the next one in that case.
+				 */
Quoted 9 lines

tools/perf/util/cs-etm.c static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset, · line 2818

Quoted 4 lines
tools/perf/util/cs-etm.c +4 -0
+				return ret;
+			}
+		}
+	}

It's good to add a warning at here? If cannot find a matched AUXRECORD event and fails to queue the fragment, it's likely caused by the AUX ring buffer's overrun.

I agree with all of your comments and I will make the changes for v5.

For this one I can add a warning if I move the check for no index entries earlier and keep that as the silent failure.

James

Quoted 48 lines
Quoted 22 lines
tools/perf/util/cs-etm.c +18 -0
+
+	/*
+	 * We would get here if there are no entries in the index (either no auxtrace buffers or
+	 * no index). Fail silently as there is the possibility of queueing them in
+	 * cs_etm__process_auxtrace_event() if etm->data_queued is still false;
+	 *
+	 * In that scenario, buffers will not be split by AUX records.
+	 */
+	return 0;
+}
+
+static int cs_etm__queue_aux_records(struct perf_session *session)
+{
+	return perf_session__peek_events(session, session->header.data_offset,
+					 session->header.data_size,
+					 cs_etm__queue_aux_records_cb, NULL);
+}
+
 int cs_etm__process_auxtrace_info(union perf_event *event,
 				  struct perf_session *session)
 {

tools/perf/util/cs-etm.c int cs_etm__process_auxtrace_info(union perf_event *event, · line 2886

Quoted 9 lines
tools/perf/util/cs-etm.c +1 -2
@@ -2879,12 +3034,10 @@ int cs_etm__process_auxtrace_info(union perf_event *event,
 	if (err)
 		goto err_delete_thread;
 
-	err = auxtrace_queues__process_index(&etm->queues, session);
+	err = cs_etm__queue_aux_records(session);
 	if (err)
 		goto err_delete_thread;
 
-	etm->data_queued = etm->queues.populated;

Seems to me it's no reason to remove this sentence.

"etm->queues.populated" will be set when call auxtrace_queues__add_event(), so we still can assign it to "etm->data_queued" at here.

Thanks,
Leo

Quoted 7 lines
tools/perf/util/cs-etm.c +0 -1
-
 	return 0;
 
 err_delete_thread:
Signature · 1 line
2.28.0
#7 lore raw
In reply to James Clark
Quoted 82 lines · James Clark

Populate the auxtrace queues using AUX records rather than whole auxtrace buffers.

This is similar to the auxtrace_queues__process_index() -> auxtrace_queues__add_indexed_event() flow where perf_session__peek_event() is used to read AUXTRACE events out of random positions in the file based on the auxtrace index.

Now there is a second pass using perf_session__peek_events() which loops over all PERF_RECORD_AUX events, finds the corresponding auxtrace buffer, and adds a fragment of that buffer to the auxtrace queues, rather than the whole buffer.

This is a completely new implementation compared to v1 and v2 of the patchset where significant modifications were made to the decoding flow, including saving all of the AUX records, and changing the point where decoding started. These changes had effects on the outcome of decoding, but with this implementation, the output of decoding is identical to perf/core, except in the case where decoding failed due to the issue that this change addresses. No changes to decoding were made, apart from populating the auxtrace queues.

Because the decoder is already reset between each auxtrace buffer, populating the queues with fragments of whole buffers based on AUX records has the same affect as resetting the decoder on the AUX record bounds during decode. But this approach only requires a change to one part of the whole flow, rather than many parts.

There is still one TODO about how buffers are found. Currently there is a plain loop as a search, but this could be improved with a binary search or keeping a record of which buffers have already been added and starting the search from the last used place. AUX records and buffers will come in similar orders in the file so the latter would be simpler and probably be good enough. But I'm looking for feedback about the approach as a whole before ironing out all of the details.

Other issues that were fixed by the previous patchsets such as fixing the case where the first aux record comes before the first MMAP record can now be fixed as completely independent issues.

Testing
=======

Testing was done with the following script, to diff the decoding results between the patched and un-patched versions of perf:

	#!/bin/bash
	set -ex

	$1 script -i $3 $4 > split.script
	$2 script -i $3 $4 > default.script

	diff split.script default.script | head -n 20

And it was run like this, with various itrace options depending on the quantity of synthesised events:

	compare.sh ./perf-patched ./perf-default perf-per-cpu-2-threads.data --itrace=i100000ns

No changes in output were observed in the following scenarios:

* Simple per-cpu
perf record -e cs_etm/@tmc_etr0/u top

* Per-thread, single thread

	perf record -e cs_etm/@tmc_etr0/u --per-thread ./threads_C

* Per-thread multiple threads (but only one thread collected data):

	perf record -e cs_etm/@tmc_etr0/u --per-thread --pid 4596,4597

* Per-thread multiple threads (both threads collected data):

	perf record -e cs_etm/@tmc_etr0/u --per-thread --pid 4596,4597

* Per-cpu explicit threads:

	perf record -e cs_etm/@tmc_etr0/u --pid 853,854

* System-wide (per-cpu):
perf record -e cs_etm/@tmc_etr0/u -a

* No data collected (no aux buffers)
Can happen with any command when run for a short period

Quoted 8 lines

* Containing truncated records
Can happen with any command

* Containing aux records with 0 size
Can happen with any command

* Snapshot mode

	perf record -e cs_etm/@tmc_etr0/u -a --snapshot

I have tested this patchset with traces recorded with ETM strobing and ETR polling patches. The traces have multiple records and have issues that this patch solves. Without this patch, there would be decoding errors reported, while with this patch, there are no decoding errors reported.
Configurations tested: system wide, cpu wide, per thread.
Actions tested: "perf inject -b"

Quoted 187 lines
---
  tools/perf/util/cs-etm.c | 159 ++++++++++++++++++++++++++++++++++++++-
  1 file changed, 156 insertions(+), 3 deletions(-)
tools/perf/util/cs-etm.c +156 -3
index 64536a6ed10a..053aad4b28cf 100644
@@ -2679,6 +2679,161 @@ static u64 *cs_etm__create_meta_blk(u64 *buff_in, int *buff_in_offset,
  	return metadata;
  }
 
+/**
+ * Puts a fragment of an auxtrace buffer into the auxtrace queues based
+ * on the bounds of aux_event, if it matches with the buffer that's at
+ * file_offset.
+ *
+ * Normally, whole auxtrace buffers would be added to the queue. But we
+ * want to reset the decoder for every PERF_RECORD_AUX event, and the decoder
+ * is reset across each buffer, so splitting the buffers up in advance has
+ * the same effect.
+ */
+static int cs_etm__queue_aux_fragment(struct perf_session *session,
+						off_t file_offset, size_t sz,
+						struct perf_record_aux *aux_event,
+						struct perf_sample *sample)
+{
+	int err;
+	char buf[PERF_SAMPLE_MAX_SIZE];
+	union perf_event *auxtrace_event_union;
+	struct perf_record_auxtrace *auxtrace_event;
+	union perf_event auxtrace_fragment;
+	bool matchesCpuPid;
+	__u64 aux_offset;
+	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
+						   struct cs_etm_auxtrace,
+						   auxtrace);
+
+	/*
+	 * There should be a PERF_RECORD_AUXTRACE event at the file_offset that we got
+	 * from looping through the auxtrace index.
+	 */
+	err = perf_session__peek_event(session, file_offset, buf,
+				       PERF_SAMPLE_MAX_SIZE, &auxtrace_event_union, NULL);
+	if (err)
+		return err;
+	auxtrace_event = &auxtrace_event_union->auxtrace;
+	if (auxtrace_event->header.type != PERF_RECORD_AUXTRACE)
+		return -EINVAL;
+
+	if (auxtrace_event->header.size < sizeof(struct perf_record_auxtrace) ||
+		auxtrace_event->header.size != sz) {
+		return -EINVAL;
+	}
+
+	/*
+	 * In per-thread mode, CPU is set to -1, but TID will be set instead.
+	 * See auxtrace_mmap_params__set_idx()
+	 */
+	if (auxtrace_event->cpu == (__u32) -1)
+		matchesCpuPid = auxtrace_event->tid == sample->tid;
+	else
+		matchesCpuPid = auxtrace_event->cpu == sample->cpu;
+
+	/*
+	 * In snapshot/overwrite mode, the head points to the end of the buffer so aux_offset needs
+	 * to have the size subtracted so it points to the beginning as in normal mode.
+	 */
+	if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE)
+		aux_offset = aux_event->aux_offset - aux_event->aux_size;
+	else
+		aux_offset = aux_event->aux_offset;
+
+	if (matchesCpuPid &&
+	    aux_offset >= auxtrace_event->offset &&
+	    aux_offset + aux_event->aux_size <= auxtrace_event->offset + auxtrace_event->size) {
+		/*
+		 * If this AUX event was inside this buffer somewhere, create a new auxtrace event
+		 * based on the sizes of the aux event, and queue that fragment.
+		 */
+		auxtrace_fragment.auxtrace = *auxtrace_event;
+		auxtrace_fragment.auxtrace.size = aux_event->aux_size;
+		auxtrace_fragment.auxtrace.offset = aux_offset;
+		file_offset += aux_offset - auxtrace_event->offset + auxtrace_event->header.size;
+		return auxtrace_queues__add_event(&etm->queues,
+					       session,
+					       &auxtrace_fragment,
+					       file_offset,
+					       NULL);
+	}
+
+	/* Wasn't inside this buffer, but there were no parse errors. 1 == 'not found' */
+	return 1;
+}
+
+static int cs_etm__queue_aux_records_cb(struct perf_session *session,
+			     union perf_event *event, u64 offset __maybe_unused,
+			     void *data __maybe_unused)
+{
+	struct perf_sample sample;
+	int ret;
+	struct auxtrace_index_entry *ent;
+	struct auxtrace_index *auxtrace_index;
+	struct evsel *evsel;
+	size_t i;
+	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
+						   struct cs_etm_auxtrace,
+						   auxtrace);
+
+	/* Don't care about any other events, we're only queuing buffers for AUX events */
+	if (event->header.type != PERF_RECORD_AUX)
+		return 0;
+
+	if (event->header.size < sizeof(struct perf_record_aux))
+		return -EINVAL;
+
+	/* Truncated Aux records can have 0 size and shouldn't result in anything being queued. */
+	if (!event->aux.aux_size)
+		return 0;
+
+	/*
+	 * Parse the sample, we need the sample_id_all data that comes after the event so that the
+	 * CPU or PID can be matched to an AUXTRACE buffer's CPU or PID.
+	 */
+	evsel = evlist__event2evsel(session->evlist, event);
+	evsel__parse_sample(evsel, event, &sample);
+
+	/*
+	 * Loop throuch the auxtrace index to find the buffer that matches up with this
+	 * aux event.
+	 * TODO: binary search?
+	 */
+	list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
+		for (i = 0; i < auxtrace_index->nr; i++) {
+			ent = &auxtrace_index->entries[i];
+			ret = cs_etm__queue_aux_fragment(session, ent->file_offset,
+							 ent->sz, &event->aux, &sample);
+			if (!ret) {
+				etm->data_queued = true;
+				return 0;
+			} else if (ret < 0) {
+				/*
+				 * Anything other than 1 is an error. Positive values are 'not
+				 * found' we just want to go onto the next one in that case.
+				 */
+				return ret;
+			}
+		}
+	}
+
+	/*
+	 * We would get here if there are no entries in the index (either no auxtrace buffers or
+	 * no index). Fail silently as there is the possibility of queueing them in
+	 * cs_etm__process_auxtrace_event() if etm->data_queued is still false;
+	 *
+	 * In that scenario, buffers will not be split by AUX records.
+	 */
+	return 0;
+}
+
+static int cs_etm__queue_aux_records(struct perf_session *session)
+{
+	return perf_session__peek_events(session, session->header.data_offset,
+					 session->header.data_size,
+					 cs_etm__queue_aux_records_cb, NULL);
+}
+
  int cs_etm__process_auxtrace_info(union perf_event *event,
  				  struct perf_session *session)
  {
@@ -2879,12 +3034,10 @@ int cs_etm__process_auxtrace_info(union perf_event *event,
  	if (err)
  		goto err_delete_thread;
 
-	err = auxtrace_queues__process_index(&etm->queues, session);
+	err = cs_etm__queue_aux_records(session);
  	if (err)
  		goto err_delete_thread;
 
-	etm->data_queued = etm->queues.populated;
-
  	return 0;
 
  err_delete_thread: