Skip to contentSkip to conversation

perf cs-etm: Set time on synthesised samples to preserve ordering

PATCH Superseded James Clark 3 participants 6 messages Apr 16, 2021 – Apr 19, 2021

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover perf cs-etm: Set time on synthesised samples to preserve ordering

Changes since v1:
* Improved variable name from etm_timestamp -> cs_timestamp
* Fixed ordering of Signed-off-by

James Clark (2):
perf cs-etm: Refactor timestamp variable names
perf cs-etm: Set time on synthesised samples to preserve ordering

 .../perf/util/cs-etm-decoder/cs-etm-decoder.c | 18 +++----
 tools/perf/util/cs-etm.c                      | 52 ++++++++++---------
 tools/perf/util/cs-etm.h                      |  4 +-
 3 files changed, 39 insertions(+), 35 deletions(-)
Signature · 1 line
2.28.0
#1 lore raw

PATCH v2 1/2 perf cs-etm: Refactor timestamp variable names

Remove ambiguity in variable names relating to timestamps. A later commit will save the sample kernel timestamp in one of the etm structs, so name all elements appropriately to avoid confusion.

This is also removes some ambiguity arising from the fact that the --timestamp argument to perf record refers to sample kernel timestamps, and the /timestamp/ event modifier refers to CS timestamps, so the term is overloaded.

---
 .../perf/util/cs-etm-decoder/cs-etm-decoder.c | 18 ++++----
 tools/perf/util/cs-etm.c                      | 42 +++++++++----------
 tools/perf/util/cs-etm.h                      |  4 +-
 3 files changed, 31 insertions(+), 33 deletions(-)
tools/perf/util/cs-etm-decoder/cs-etm-decoder.c +9 -9
index 059bcec3f651..b01d363b9301 100644
@@ -276,13 +276,13 @@ cs_etm_decoder__do_soft_timestamp(struct cs_etm_queue *etmq,
 				  const uint8_t trace_chan_id)
 {
 	/* No timestamp packet has been received, nothing to do */
-	if (!packet_queue->timestamp)
+	if (!packet_queue->cs_timestamp)
 		return OCSD_RESP_CONT;
 
-	packet_queue->timestamp = packet_queue->next_timestamp;
+	packet_queue->cs_timestamp = packet_queue->next_cs_timestamp;
 
 	/* Estimate the timestamp for the next range packet */
-	packet_queue->next_timestamp += packet_queue->instr_count;
+	packet_queue->next_cs_timestamp += packet_queue->instr_count;
 	packet_queue->instr_count = 0;
 
 	/* Tell the front end which traceid_queue needs attention */
@@ -308,8 +308,8 @@ cs_etm_decoder__do_hard_timestamp(struct cs_etm_queue *etmq,
 	 * Function do_soft_timestamp() will report the value to the front end,
 	 * hence asking the decoder to keep decoding rather than stopping.
 	 */
-	if (packet_queue->timestamp) {
-		packet_queue->next_timestamp = elem->timestamp;
+	if (packet_queue->cs_timestamp) {
+		packet_queue->next_cs_timestamp = elem->timestamp;
 		return OCSD_RESP_CONT;
 	}
 
@@ -320,8 +320,8 @@ cs_etm_decoder__do_hard_timestamp(struct cs_etm_queue *etmq,
 	 * which instructions started by subtracting the number of instructions
 	 * executed to the timestamp.
 	 */
-	packet_queue->timestamp = elem->timestamp - packet_queue->instr_count;
-	packet_queue->next_timestamp = elem->timestamp;
+	packet_queue->cs_timestamp = elem->timestamp - packet_queue->instr_count;
+	packet_queue->next_cs_timestamp = elem->timestamp;
 	packet_queue->instr_count = 0;
 
 	/* Tell the front end which traceid_queue needs attention */
@@ -334,8 +334,8 @@ cs_etm_decoder__do_hard_timestamp(struct cs_etm_queue *etmq,
 static void
 cs_etm_decoder__reset_timestamp(struct cs_etm_packet_queue *packet_queue)
 {
-	packet_queue->timestamp = 0;
-	packet_queue->next_timestamp = 0;
+	packet_queue->cs_timestamp = 0;
+	packet_queue->next_cs_timestamp = 0;
 	packet_queue->instr_count = 0;
 }
 
tools/perf/util/cs-etm.c +20 -22

136 lines not on this page · raw message

tools/perf/util/cs-etm.h +2 -2
index 36428918411e..d65c7b19407d 100644
@@ -171,8 +171,8 @@ struct cs_etm_packet_queue {
 	u32 head;
 	u32 tail;
 	u32 instr_count;
-	u64 timestamp;
-	u64 next_timestamp;
+	u64 cs_timestamp;
+	u64 next_cs_timestamp;
 	struct cs_etm_packet packet_buffer[CS_ETM_PACKET_MAX_BUFFER];
 };
 
Signature · 1 line
2.28.0
#2 lore raw

PATCH v2 2/2 perf cs-etm: Set time on synthesised samples to preserve ordering

In reply to James Clark

The following attribute is set when synthesising samples in timed decoding mode:

    attr.sample_type |= PERF_SAMPLE_TIME;

This results in new samples that appear to have timestamps but because we don't assign any timestamps to the samples, when the resulting inject file is opened again, the synthesised samples will be on the wrong side of the MMAP or COMM events.

For example this results in the samples being associated with the perf binary, rather than the target of the record:

perf record -e cs_etm/@tmc_etr0/u top
perf inject -i perf.data -o perf.inject --itrace=i100il perf report -i perf.inject

Where 'Command' == perf should show as 'top':

    # Overhead  Command  Source Shared Object  Source Symbol           Target Symbol           Basic Block Cycles
    # ........  .......  ....................  ......................  ......................  ..................
    #
        31.08%  perf     [unknown]             [.] 0x000000000040c3f8  [.] 0x000000000040c3e8  -

If the perf.data file is opened directly with perf, without the inject step, then this already works correctly because the events are synthesised after the COMM and MMAP events and no second sorting happens. Re-sorting only happens when opening the perf.inject file for the second time so timestamps are needed.

Using the timestamp from the AUX record mirrors the current behaviour when opening directly with perf, because the events are generated on the call to cs_etm__process_queues().

---
 tools/perf/util/cs-etm.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
tools/perf/util/cs-etm.c +8 -2
index 533f6f2f0685..e5c1a1b22a2a 100644
@@ -54,6 +54,7 @@ struct cs_etm_auxtrace {
 	u8 sample_instructions;
 
 	int num_cpu;
+	u64 latest_kernel_timestamp;
 	u32 auxtrace_type;
 	u64 branches_sample_type;
 	u64 branches_id;
@@ -1192,6 +1193,8 @@ static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
 	event->sample.header.misc = cs_etm__cpu_mode(etmq, addr);
 	event->sample.header.size = sizeof(struct perf_event_header);
 
+	if (!etm->timeless_decoding)
+		sample.time = etm->latest_kernel_timestamp;
 	sample.ip = addr;
 	sample.pid = tidq->pid;
 	sample.tid = tidq->tid;
@@ -1248,6 +1251,8 @@ static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
 	event->sample.header.misc = cs_etm__cpu_mode(etmq, ip);
 	event->sample.header.size = sizeof(struct perf_event_header);
 
+	if (!etm->timeless_decoding)
+		sample.time = etm->latest_kernel_timestamp;
 	sample.ip = ip;
 	sample.pid = tidq->pid;
 	sample.tid = tidq->tid;
@@ -2412,9 +2417,10 @@ static int cs_etm__process_event(struct perf_session *session,
 	else if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
 		return cs_etm__process_switch_cpu_wide(etm, event);
 
-	if (!etm->timeless_decoding &&
-	    event->header.type == PERF_RECORD_AUX)
+	if (!etm->timeless_decoding && event->header.type == PERF_RECORD_AUX) {
+		etm->latest_kernel_timestamp = sample_kernel_timestamp;
 		return cs_etm__process_queues(etm);
+	}
 
 	return 0;
 }
Signature · 1 line
2.28.0
#3 lore raw
In reply to James Clark

Hi James,

Quoted 4 lines · James Clark

Changes since v1:
* Improved variable name from etm_timestamp -> cs_timestamp
* Fixed ordering of Signed-off-by

You forgot to add the RB and AB you received. Since Arnaldo is responsible for the perf tools subsystem, please send another revision.

Thanks,
Mathieu

Quoted 12 lines

James Clark (2):
perf cs-etm: Refactor timestamp variable names
perf cs-etm: Set time on synthesised samples to preserve ordering

 .../perf/util/cs-etm-decoder/cs-etm-decoder.c | 18 +++----
 tools/perf/util/cs-etm.c                      | 52 ++++++++++---------
 tools/perf/util/cs-etm.h                      |  4 +-
 3 files changed, 39 insertions(+), 35 deletions(-)
Signature · 1 line
2.28.0
#4 lore raw

Em Fri, Apr 16, 2021 at 09:07:09AM -0600, Mathieu Poirier escreveu:

Quoted 10 lines

Hi James,

Quoted 4 lines · James Clark

Changes since v1:
* Improved variable name from etm_timestamp -> cs_timestamp
* Fixed ordering of Signed-off-by

You forgot to add the RB and AB you received. Since Arnaldo is responsible for the perf tools subsystem, please send another revision.

Yep, please collect Reported-by and Acked-by as you go sending new versions of a patchset, the last one I don't have a problem collecting myself, but if you have to resend, please collect the feedback tags.

- Arnaldo

Quoted 15 lines

Thanks,
Mathieu

Quoted 12 lines

James Clark (2):
perf cs-etm: Refactor timestamp variable names
perf cs-etm: Set time on synthesised samples to preserve ordering

 .../perf/util/cs-etm-decoder/cs-etm-decoder.c | 18 +++----
 tools/perf/util/cs-etm.c                      | 52 ++++++++++---------
 tools/perf/util/cs-etm.h                      |  4 +-
 3 files changed, 39 insertions(+), 35 deletions(-)
Signature · 1 line
2.28.0
Signature · 1 line
- Arnaldo
#5 lore raw
Quoted 14 lines · Arnaldo Carvalho de Melo

Em Fri, Apr 16, 2021 at 09:07:09AM -0600, Mathieu Poirier escreveu:

Quoted 10 lines

Hi James,

Quoted 4 lines · James Clark

Changes since v1:
* Improved variable name from etm_timestamp -> cs_timestamp
* Fixed ordering of Signed-off-by

You forgot to add the RB and AB you received. Since Arnaldo is responsible for the perf tools subsystem, please send another revision.

Quoted 6 lines

Yep, please collect Reported-by and Acked-by as you go sending new versions of a patchset, the last one I don't have a problem collecting myself, but if you have to resend, please collect the feedback tags.

- Arnaldo

Ok thanks, I will keep that in mind for the future. I wasn't sure if they still applied or not as it was a new version.

Thanks
James

Quoted 16 lines
Quoted 15 lines

Thanks,
Mathieu

Quoted 12 lines

James Clark (2):
perf cs-etm: Refactor timestamp variable names
perf cs-etm: Set time on synthesised samples to preserve ordering

 .../perf/util/cs-etm-decoder/cs-etm-decoder.c | 18 +++----
 tools/perf/util/cs-etm.c                      | 52 ++++++++++---------
 tools/perf/util/cs-etm.h                      |  4 +-
 3 files changed, 39 insertions(+), 35 deletions(-)
Signature · 1 line
2.28.0