Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "James Clark" <james.clark@linaro.org>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 17/34] perf cs-etm: Decode in order of Perf samples
Date: Wed, 18 Nov 2026 17:10:15 +0000	[thread overview]
Message-ID: <sashiko-outbox-221583@kernel.org> (raw)
In-Reply-To: <20261118-james-coresight-jit-v1-17-dad4d90747f6@linaro.org>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] perf cs-etm: trace decoding fails when PERF_SAMPLE_TIME is
  absent
- [Medium] perf cs-etm: premature next-buffer load in cs_etm__pop_heap()
  before flush
--

commit 11ab0fc2c585444dee0fab0f3ef2be954d8f75a6
Author: James Clark <james.clark@linaro.org>

perf cs-etm: Decode in order of Perf samples

This commit replaces the separate timestamped, timeless, and per-thread
CoreSight ETM decode paths with a unified heap-ordered decode driven by
Perf event timestamps.

> diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
> index b746d1215e0a0..5c98c7960f823 100644
> --- a/tools/perf/util/cs-etm.c
> +++ b/tools/perf/util/cs-etm.c
[ ... ]
> @@ -960,21 +952,19 @@ static int cs_etm__flush_events(struct perf_session *session,
>  	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
>  						   struct cs_etm_auxtrace,
>  						   auxtrace);
> +	int ret;
> +
>  	if (dump_trace || auxtrace__dont_decode(session))
>  		return 0;
>  
>  	if (!tool->ordered_events)
>  		return -EINVAL;
>  
> -	if (!etm->has_timestamps) {
> -		/*
> -		 * Pass tid = -1 to process all queues. But likely they will have
> -		 * already been processed on PERF_RECORD_EXIT anyway.
> -		 */
> -		return cs_etm__process_timeless_queues(etm, -1);
> -	}
> +	ret = cs_etm__pop_heap(etm, MAX_TIMESTAMP);
> +	if (ret)
> +		return ret;
>  
> -	return cs_etm__process_timestamped_queues(etm);
> +	return cs_etm__flush_queues(etm);
>  }
[ ... ]
> @@ -1321,75 +1311,77 @@ static int cs_etm__setup_queue(struct cs_etm_auxtrace *etm,
[ ... ]
> +static int cs_etm__push_queue(struct cs_etm_queue *etmq, u64 time)
> +{
> +	int ret;
> +
> +	if (!etmq || etmq->on_heap)
> +		return 0;
> +
> +	while (!etmq->on_heap) {
>  		ret = cs_etm__get_data_block(etmq);
>  		if (ret <= 0)
> -			goto out;
> +			return ret;
> +		if (!etmq->etm->has_virtual_ts && etmq->buffer->time > time)
> +			return 0;
>  
> -		/*
> -		 * Run decoder on the trace block.  The decoder will stop when
> -		 * encountering a CS timestamp, a full packet queue or the end of
> -		 * trace for that block.
> -		 */
> -		ret = cs_etm__decode_data_block(etmq);
> +		ret = cs_etm__add_to_heap(etmq);
>  		if (ret)
> -			goto out;
> +			return ret;
> +	}
>  
> -		/*
> -		 * Function cs_etm_decoder__do_{hard|soft}_timestamp() does all
> -		 * the timestamp calculation for us.
> -		 */
> -		cs_timestamp = cs_etm__etmq_get_sort_timestamp(etmq, &trace_chan_id);
> +	return 0;
> +}
[ ... ]
> @@ -2895,11 +2743,21 @@ static int cs_etm__process_timestamped_queues(struct cs_etm_auxtrace *etm)
>  		queue = &etm->queues.queue_array[queue_nr];
>  		etmq = queue->priv;
>  
> +		/* Don't go further than the trace timestamp or collection time */
> +		if (etm->has_virtual_ts) {
> +			if (etm->heap.heap_array[0].ordinal >= timestamp)
> +				break;
> +		} else {
> +			if (etmq->buffer->time >= timestamp)
> +				break;
> +		}
> +
>  		/*
>  		 * Remove the top entry from the heap since we are about
>  		 * to process it.
>  		 */
>  		auxtrace_heap__pop(&etm->heap);
> +		etmq->on_heap = false;
>  
>  		tidq  = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
>  		if (!tidq) {
> @@ -2913,7 +2771,7 @@ static int cs_etm__process_timestamped_queues(struct cs_etm_auxtrace *etm)
>  		}
>  
>  		/*
> -		 * Packets associated with this timestamp are already in
> +		 * Packets associated with this timestamp, if any, are already in
>  		 * the etmq's traceID queue, so process them.
>  		 */
>  		ret = cs_etm__process_traceid_queue(etmq, tidq);
> @@ -2925,7 +2783,7 @@ static int cs_etm__process_timestamped_queues(struct cs_etm_auxtrace *etm)
>  		 * move on to the next timestamp, fetching a new auxtrace_buffer
>  		 * if need be.
>  		 */
> -refetch:
> +get_data_block:
>  		ret = cs_etm__get_data_block(etmq);
>  		if (ret < 0)
>  			goto out;
> @@ -2937,37 +2795,28 @@ static int cs_etm__process_timestamped_queues(struct cs_etm_auxtrace *etm)
>  		if (!ret)
>  			continue;
>  
> -		ret = cs_etm__decode_data_block(etmq);
> -		if (ret)
> -			goto out;
> -
> -		cs_timestamp = cs_etm__etmq_get_sort_timestamp(etmq, &trace_chan_id);
> -
> -		if (!cs_timestamp) {
> -			/*
> -			 * Function cs_etm__decode_data_block() returns when
> -			 * there is no more traces to decode in the current
> -			 * auxtrace_buffer OR when a timestamp has been
> -			 * encountered on any of the traceID queues.  Since we
> -			 * did not get a timestamp, there is no more traces to
> -			 * process in this auxtrace_buffer.  As such empty and
> -			 * flush all traceID queues.
> -			 */
> -			cs_etm__clear_all_traceid_queues(etmq);
> -
> -			/* Fetch another auxtrace_buffer for this etmq */
> -			goto refetch;
> -		}
> -
>  		/*
> -		 * Add to the min heap the timestamp for packets that have
> -		 * just been decoded.  They will be processed and synthesized
> -		 * during the next call to cs_etm__process_traceid_queue() for
> -		 * this queue/traceID.
> +		 * Add the next timestamp or decoded packets to the min heap.
> +		 * Packets will be processed and synthesized during the next call
> +		 * to cs_etm__process_traceid_queue() for this queue/traceID.
>  		 */
> -		cs_queue_nr = TO_CS_QUEUE_NR(queue_nr, trace_chan_id);
> -		ret = auxtrace_heap__add(&etm->heap, cs_queue_nr, cs_timestamp);
> +		ret = cs_etm__add_to_heap(etmq);
> +		if (ret)
> +			goto out;
> +		if (!etmq->on_heap)
> +			goto get_data_block;
>  	}

[Severity: Medium]
Does loading the next auxtrace_buffer via cs_etm__get_data_block() here
corrupt the state needed to flush the previous buffer's final packet and
cause premature decoding of future AUX buffers?

When the last CS_ETM_RANGE packet of an auxtrace_buffer is processed,
cs_etm__sample() swaps it into tidq->prev_packet with any unsampled
period_instructions remaining.  Unlike the removed timeless decoder loops,
which called cs_etm__end_block() at the end of each buffer before fetching
the next one, cs_etm__pop_heap() immediately calls cs_etm__get_data_block()
when etmq->buf_len reaches 0.  When the next buffer is loaded,
cs_etm__get_trace() replaces etmq->buffer, wipes all thread branch stacks
via cs_etm__flush_all_stack(), and in per-thread mode overwrites
tidq->frontend_thread and tidq->decode_thread with the new buffer's TID.
When the new buffer's initial CS_ETM_DISCONTINUITY packet later calls
cs_etm__flush(), the previous buffer's final instruction and branch samples
are synthesized with the new buffer's timestamp, thread, and cleared call
stack.

In addition, when !etm->has_virtual_ts, both cs_etm__push_queue() and this
loop call cs_etm__get_data_block() before checking whether the next buffer's
time is beyond the current Perf event timestamp, and cs_etm__pop_heap() does
not check etmq->buffer->time > timestamp at all before calling
cs_etm__add_to_heap(), decoding the start of the next AUX buffer before
intervening MMAP and COMM records have been processed (a later patch in this
series, commit "perf cs-etm: Find timestamps by searching ahead", adds the
timestamp check via cs_etm__insert_heap_queue(), but still calls
cs_etm__get_data_block() before that check and before flushing the previous
buffer).

[ ... ]
> @@ -3049,6 +2898,8 @@ static int cs_etm__process_event(struct perf_session *session,
>  	struct cs_etm_auxtrace *etm = container_of(session->auxtrace,
>  						   struct cs_etm_auxtrace,
>  						   auxtrace);
> +	bool has_sample_time = sample->time && sample->time != (u64)-1;
> +	int ret = 0;
>  
>  	if (dump_trace || auxtrace__dont_decode(session))
>  		return 0;
> @@ -3059,34 +2910,71 @@ static int cs_etm__process_event(struct perf_session *session,
>  	}
>  
>  	switch (event->header.type) {
> -	case PERF_RECORD_EXIT:
> +	case PERF_RECORD_ITRACE_START:
> +		ret = cs_etm__process_itrace_start(etm, event);
> +		if (ret)
> +			return ret;
> +
>  		/*
> -		 * Don't need to wait for cs_etm__flush_events() in per-thread mode to
> -		 * start the decode because we know there will be no more trace from
> -		 * this thread. All this does is emit samples earlier than waiting for
> -		 * the flush in other modes, but with timestamps it makes sense to wait
> -		 * for flush so that events from different threads are interleaved
> -		 * properly.
> +		 * Start decoding for this CPU on trace start. Timestamps in the
> +		 * trace will prevent decoding from going further ahead than
> +		 * Perf events.
>  		 */
> -		if (etm->per_thread_decoding && !etm->has_timestamps)
> -			return cs_etm__process_timeless_queues(etm,
> -							       event->fork.tid);
> +		if (has_sample_time && etm->has_virtual_ts) {
> +			ret = cs_etm__push_heap(etm, sample);
> +			if (ret)
> +				return ret;
> +		}
>  		break;
[ ... ]
> +	case PERF_RECORD_AUX:
> +		/*
> +		 * Start decoding after AUX to support !etm->has_virtual_ts
> +		 * because mmaps for that buffer must be available at this
> +		 * point. Push all queues so that they are ordered between each
> +		 * other, rather than waiting for their own AUX records which
> +		 * would be too late.
> +		 */
> +		if (etm->has_timestamps && !etm->has_virtual_ts)
> +			ret = cs_etm__push_heap_all(etm);
> +		else if (has_sample_time)
> +			ret = cs_etm__push_heap(etm, sample);
> +
> +		if (ret)
> +			return ret;
> +		break;
>  
>  	default:
>  		break;
>  	}
>  
> +	if (has_sample_time)
> +		return cs_etm__pop_heap(etm, sample->time);
> +
>  	return 0;
>  }

[Severity: Medium]
Does this skip all trace decoding when analyzing a perf.data file recorded
without PERF_SAMPLE_TIME (such as per-thread recordings created by older
perf binaries without -T)?

When PERF_SAMPLE_TIME is not set, __evsel__parse_sample() sets sample->time
to (u64)-1 (which equals MAX_TIMESTAMP), so has_sample_time is false for
every event and cs_etm__queue_aux_fragment() sets buffer->time to (u64)-1.
As a result, queues are never pushed during cs_etm__process_event() when
etm->has_virtual_ts or !etm->has_timestamps, and cs_etm__flush_events()
calls cs_etm__pop_heap(etm, MAX_TIMESTAMP) without pushing them first.
Even when etm->has_timestamps && !etm->has_virtual_ts pushes the queues on
PERF_RECORD_AUX, cs_etm__pop_heap(etm, MAX_TIMESTAMP) immediately hits
etmq->buffer->time >= timestamp (~0ULL >= ~0ULL) and breaks out without
decoding any trace data.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-james-coresight-jit-v1-0-dad4d90747f6@linaro.org?part=17

  reply	other threads:[~2026-11-18 17:10 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 16:29 [PATCH 00/34] perf cs-etm: JIT decoding support James Clark
2026-11-18 16:29 ` [PATCH 01/34] perf jit: Support JITDUMP_USE_ARCH_TIMESTAMP on arm64 James Clark
2026-11-18 16:40   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 02/34] perf jit: Add name of clock in error message James Clark
2026-11-18 16:32   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 03/34] perf jit: Add address ranges to DWARF compilation units James Clark
2026-11-18 16:34   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 04/34] perf libdw: Don't look up the module by ELF address James Clark
2026-11-18 16:36   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 05/34] perf cs-etm: Use the exception level to tell kernel from user James Clark
2026-11-18 16:47   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 06/34] perf cs-etm: Don't read past the end of the map James Clark
2026-11-18 17:11   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 07/34] perf build-id: Cache the vDSO in build ID mmap mode James Clark
2026-11-18 17:03   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 08/34] perf cs-etm: arm-spe: Use arch timestamp in HW tracers James Clark
2026-11-18 16:42   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 09/34] perf cs-etm: arm-spe: Report error if default clock isn't used James Clark
2026-11-18 16:35   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 10/34] perf cs-etm: Remove old pipe mode support James Clark
2026-11-18 16:44   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 11/34] perf cs-etm: Track threads in timeless mode James Clark
2026-11-18 16:35   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 12/34] perf cs-etm: Track sched out events James Clark
2026-11-18 16:40   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 13/34] perf cs-etm: Handle PERF_RECORD_SWITCH events James Clark
2026-11-18 16:36   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 14/34] perf cs-etm: Always enable Perf event timestamps James Clark
2026-11-18 16:40   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 15/34] perf cs-etm: Use time from Perf samples James Clark
2026-11-18 16:51   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 16/34] perf cs-etm: Don't buffer non-timestamped packets James Clark
2026-11-18 16:43   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 17/34] perf cs-etm: Decode in order of Perf samples James Clark
2026-11-18 17:10   ` sashiko-bot [this message]
2026-11-18 16:29 ` [PATCH 18/34] perf cs-etm: Don't special case per-thread mode James Clark
2026-11-18 16:40   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 19/34] perf cs-etm: Decode one packet at a time and don't estimate timestamps James Clark
2026-11-18 16:39   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 20/34] perf cs-etm: Remove the decoded packet ring buffer James Clark
2026-11-18 16:46   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 21/34] perf cs-etm: Fold packet state into the trace ID queue James Clark
2026-11-18 16:40   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 22/34] perf cs-etm: Rename trace ID queues to trace ID contexts James Clark
2026-11-18 16:40   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 23/34] perf cs-etm: Rename tidq variables to tidc James Clark
2026-11-18 16:49   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 24/34] perf cs-etm: Share the thread ctxt between frontend and backend James Clark
2026-11-18 16:47   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 25/34] perf cs-etm: Consume each OpenCSD packet immediately James Clark
2026-11-18 16:44   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 26/34] perf cs-etm: Split base and packet decoder state James Clark
2026-11-18 16:42   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 27/34] perf cs-etm: Add packet-only timestamp decoder ops James Clark
2026-11-18 16:42   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 28/34] perf cs-etm: Create timestamp decoders James Clark
2026-11-18 16:48   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 29/34] perf cs-etm: Find timestamps by searching ahead James Clark
2026-11-18 17:01   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 30/34] perf cs-etm: Invalidate OpenCSD's cache on MMAPs James Clark
2026-11-18 16:45   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 31/34] perf test cs-etm: Add a Java JIT source-line test James Clark
2026-11-18 17:11   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 32/34] perf test cs-etm: Test trace is ordered with context switch records James Clark
2026-11-18 16:49   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 33/34] perf test cs-etm: Test decoding of kernel modules James Clark
2026-11-18 16:57   ` sashiko-bot
2026-11-18 16:29 ` [PATCH 34/34] perf test cs-etm: Add an HHVM JIT call stack test James Clark
2026-11-18 17:01   ` sashiko-bot
2026-11-18 16:33 ` [PATCH 00/34] perf cs-etm: JIT decoding support James Clark

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-221583@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=james.clark@linaro.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox