[PATCH 20/34] perf cs-etm: Remove the decoded packet ring buffer

From: James Clark

Date: Wed Nov 18 2026 - 11:31:11 EST


Full decoding now stops after every output packet, so the frontend
drains the packet ring before the decoder can produce another packet.

Write directly into the frontend's current packet instead. Remove the
ring storage but retain the packet count as the pending-packet
indicator, which will be removed later.

Signed-off-by: James Clark <james.clark@xxxxxxxxxx>
---
tools/perf/util/cs-etm-decoder/cs-etm-decoder.c | 64 ++++++++++---------------
tools/perf/util/cs-etm.c | 35 ++++----------
tools/perf/util/cs-etm.h | 7 +--
3 files changed, 36 insertions(+), 70 deletions(-)

diff --git a/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c b/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c
index d7b596aeaa41..e2eb606f2feb 100644
--- a/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c
+++ b/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c
@@ -96,22 +96,8 @@ int cs_etm_decoder__get_packet(struct cs_etm_packet_queue *packet_queue,
/* Nothing to do, might as well just return */
if (packet_queue->packet_count == 0)
return 0;
- /*
- * The queueing process in function cs_etm_decoder__buffer_packet()
- * increments the tail *before* using it. This is somewhat counter
- * intuitive but it has the advantage of centralizing tail management
- * at a single location. Because of that we need to follow the same
- * heuristic with the head, i.e we increment it before using its
- * value. Otherwise the first element of the packet queue is not
- * used.
- */
- packet_queue->head = (packet_queue->head + 1) &
- (CS_ETM_PACKET_MAX_BUFFER - 1);
-
- *packet = packet_queue->packet_buffer[packet_queue->head];

packet_queue->packet_count--;
-
return 1;
}

@@ -312,36 +298,36 @@ cs_etm_decoder__buffer_packet(struct cs_etm_queue *etmq,
const u8 trace_chan_id,
enum cs_etm_sample_type sample_type)
{
- u32 et = 0;
+ struct cs_etm_packet *packet;
int cpu;

- if (packet_queue->packet_count >= CS_ETM_PACKET_MAX_BUFFER - 1)
+ if (packet_queue->packet_count)
return OCSD_RESP_FATAL_SYS_ERR;

if (cs_etm__get_cpu(etmq, trace_chan_id, &cpu) < 0)
return OCSD_RESP_FATAL_SYS_ERR;

- et = packet_queue->tail;
- et = (et + 1) & (CS_ETM_PACKET_MAX_BUFFER - 1);
- packet_queue->tail = et;
- packet_queue->packet_count++;
+ packet = cs_etm__etmq_get_packet(etmq, trace_chan_id);
+ if (!packet)
+ return OCSD_RESP_FATAL_SYS_ERR;

- packet_queue->packet_buffer[et].sample_type = sample_type;
- packet_queue->packet_buffer[et].isa = cs_etm_decoder__get_isa(elem);
- packet_queue->packet_buffer[et].cpu = cpu;
- packet_queue->packet_buffer[et].start_addr = CS_ETM_INVAL_ADDR;
- packet_queue->packet_buffer[et].end_addr = CS_ETM_INVAL_ADDR;
- packet_queue->packet_buffer[et].instr_count = 0;
- packet_queue->packet_buffer[et].last_instr_taken_branch = false;
- packet_queue->packet_buffer[et].last_instr_size = 0;
- packet_queue->packet_buffer[et].last_instr_type = 0;
- packet_queue->packet_buffer[et].last_instr_subtype = 0;
- packet_queue->packet_buffer[et].last_instr_cond = 0;
- packet_queue->packet_buffer[et].flags = 0;
- packet_queue->packet_buffer[et].exception_number = UINT32_MAX;
- packet_queue->packet_buffer[et].trace_chan_id = trace_chan_id;
- packet_queue->packet_buffer[et].el = ocsd_EL_unknown;
- packet_queue->packet_buffer[et].tid = -1;
+ packet_queue->packet_count++;
+ packet->sample_type = sample_type;
+ packet->isa = cs_etm_decoder__get_isa(elem);
+ packet->cpu = cpu;
+ packet->start_addr = CS_ETM_INVAL_ADDR;
+ packet->end_addr = CS_ETM_INVAL_ADDR;
+ packet->instr_count = 0;
+ packet->last_instr_taken_branch = false;
+ packet->last_instr_size = 0;
+ packet->last_instr_type = 0;
+ packet->last_instr_subtype = 0;
+ packet->last_instr_cond = 0;
+ packet->flags = 0;
+ packet->exception_number = UINT32_MAX;
+ packet->trace_chan_id = trace_chan_id;
+ packet->el = ocsd_EL_unknown;
+ packet->tid = -1;

cs_etm__etmq_set_traceid_queue_ready(etmq, trace_chan_id);
return OCSD_RESP_WAIT;
@@ -361,7 +347,7 @@ cs_etm_decoder__buffer_range(struct cs_etm_queue *etmq,
if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT)
return ret;

- packet = &packet_queue->packet_buffer[packet_queue->tail];
+ packet = cs_etm__etmq_get_packet(etmq, trace_chan_id);

packet->start_addr = elem->st_addr;
packet->end_addr = elem->en_addr;
@@ -407,7 +393,7 @@ cs_etm_decoder__buffer_exception(struct cs_etm_queue *etmq,
if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT)
return ret;

- packet = &queue->packet_buffer[queue->tail];
+ packet = cs_etm__etmq_get_packet(etmq, trace_chan_id);
packet->exception_number = elem->exception_number;
if (elem->context.el_valid)
packet->el = elem->context.exception_level;
@@ -491,7 +477,7 @@ cs_etm_decoder__set_tid(struct cs_etm_queue *etmq,
if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT)
return ret;

- packet = &packet_queue->packet_buffer[packet_queue->tail];
+ packet = cs_etm__etmq_get_packet(etmq, trace_chan_id);
packet->tid = tid;
if (elem->context.el_valid)
packet->el = elem->context.exception_level;
diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
index f33e410e85bf..0175b2facaac 100644
--- a/tools/perf/util/cs-etm.c
+++ b/tools/perf/util/cs-etm.c
@@ -615,30 +615,6 @@ static u64 cs_etm__etmq_get_sort_timestamp(struct cs_etm_queue *etmq,
return packet_queue->cs_timestamp;
}

-static void cs_etm__clear_packet_queue(struct cs_etm_packet_queue *queue)
-{
- int i;
-
- queue->head = 0;
- queue->tail = 0;
- queue->packet_count = 0;
- for (i = 0; i < CS_ETM_PACKET_MAX_BUFFER; i++) {
- queue->packet_buffer[i].isa = CS_ETM_ISA_UNKNOWN;
- queue->packet_buffer[i].start_addr = CS_ETM_INVAL_ADDR;
- queue->packet_buffer[i].end_addr = CS_ETM_INVAL_ADDR;
- queue->packet_buffer[i].instr_count = 0;
- queue->packet_buffer[i].last_instr_taken_branch = false;
- queue->packet_buffer[i].last_instr_size = 0;
- queue->packet_buffer[i].last_instr_type = 0;
- queue->packet_buffer[i].last_instr_subtype = 0;
- queue->packet_buffer[i].last_instr_cond = 0;
- queue->packet_buffer[i].flags = 0;
- queue->packet_buffer[i].exception_number = UINT32_MAX;
- queue->packet_buffer[i].trace_chan_id = UINT8_MAX;
- queue->packet_buffer[i].cpu = INT_MIN;
- }
-}
-
static int cs_etm__init_traceid_queue(struct cs_etm_queue *etmq,
struct cs_etm_traceid_queue *tidq,
u8 trace_chan_id)
@@ -647,8 +623,6 @@ static int cs_etm__init_traceid_queue(struct cs_etm_queue *etmq,
struct auxtrace_queue *queue;
struct cs_etm_auxtrace *etm = etmq->etm;

- cs_etm__clear_packet_queue(&tidq->packet_queue);
-
queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
tidq->trace_chan_id = trace_chan_id;
tidq->decode_el = ocsd_EL_unknown;
@@ -772,6 +746,15 @@ static struct cs_etm_traceid_queue
return NULL;
}

+struct cs_etm_packet *cs_etm__etmq_get_packet(struct cs_etm_queue *etmq,
+ u8 trace_chan_id)
+{
+ struct cs_etm_traceid_queue *tidq;
+
+ tidq = cs_etm__etmq_get_traceid_queue(etmq, trace_chan_id);
+ return tidq ? tidq->packet : NULL;
+}
+
struct cs_etm_packet_queue
*cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id)
{
diff --git a/tools/perf/util/cs-etm.h b/tools/perf/util/cs-etm.h
index e404753820dc..c757a1fb465d 100644
--- a/tools/perf/util/cs-etm.h
+++ b/tools/perf/util/cs-etm.h
@@ -193,14 +193,9 @@ struct cs_etm_packet {
pid_t tid;
};

-#define CS_ETM_PACKET_MAX_BUFFER 1024
-
struct cs_etm_packet_queue {
u32 packet_count;
- u32 head;
- u32 tail;
u64 cs_timestamp; /* Timestamp, in ns when has_virtual_ts == true */
- struct cs_etm_packet packet_buffer[CS_ETM_PACKET_MAX_BUFFER];
};

#define KiB(x) ((x) * 1024)
@@ -260,6 +255,8 @@ int cs_etm__etmq_update_decode_context(struct cs_etm_queue *etmq,
bool cs_etm__has_timestamps(struct cs_etm_queue *etmq);
void cs_etm__etmq_set_traceid_queue_ready(struct cs_etm_queue *etmq,
u8 trace_chan_id);
+struct cs_etm_packet *cs_etm__etmq_get_packet(struct cs_etm_queue *etmq,
+ u8 trace_chan_id);
struct cs_etm_packet_queue
*cs_etm__etmq_get_packet_queue(struct cs_etm_queue *etmq, u8 trace_chan_id);
int cs_etm__process_auxtrace_info_full(union perf_event *event __maybe_unused,

--
2.43.0