[PATCH 24/34] perf cs-etm: Share the thread ctxt between frontend and backend
From: James Clark
Date: Wed Nov 18 2026 - 11:30:49 EST
The decoder stops at every context packet and resumes only after the
frontend has processed it, so now we only need to keep track of a single
thread context.
The thread gets updated when the frontend processes a context packet.
Signed-off-by: James Clark <james.clark@xxxxxxxxxx>
---
tools/perf/util/cs-etm-decoder/cs-etm-decoder.c | 4 --
tools/perf/util/cs-etm.c | 73 +++++++------------------
tools/perf/util/cs-etm.h | 10 +---
3 files changed, 22 insertions(+), 65 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 7a1d50ca5949..06c57326ac80 100644
--- a/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c
+++ b/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c
@@ -468,10 +468,6 @@ cs_etm_decoder__set_tid(struct cs_etm_queue *etmq,
break;
}
- if (cs_etm__etmq_update_decode_context(etmq, trace_chan_id,
- elem->context.exception_level, tid))
- return OCSD_RESP_FATAL_SYS_ERR;
-
ret = cs_etm_decoder__buffer_packet(etmq, tidc, elem,
trace_chan_id, CS_ETM_CONTEXT);
if (ret != OCSD_RESP_CONT && ret != OCSD_RESP_WAIT)
diff --git a/tools/perf/util/cs-etm.c b/tools/perf/util/cs-etm.c
index e2e84561c0c9..97ed12d03555 100644
--- a/tools/perf/util/cs-etm.c
+++ b/tools/perf/util/cs-etm.c
@@ -604,11 +604,9 @@ static int cs_etm__init_traceid_ctxt(struct cs_etm_queue *etmq,
queue = &etmq->etm->queues.queue_array[etmq->queue_nr];
tidc->trace_chan_id = trace_chan_id;
tidc->decode_el = ocsd_EL_unknown;
- tidc->frontend_thread = machine__findnew_thread(&etm->session->machines.host, -1,
+ tidc->thread = machine__findnew_thread(&etm->session->machines.host, -1,
queue->tid);
- tidc->decode_thread = machine__findnew_thread(&etm->session->machines.host, -1,
- queue->tid);
- if (!tidc->frontend_thread || !tidc->decode_thread)
+ if (!tidc->thread)
goto out;
tidc->packet = zalloc(sizeof(struct cs_etm_packet));
@@ -652,8 +650,7 @@ static int cs_etm__init_traceid_ctxt(struct cs_etm_queue *etmq,
zfree(&tidc->prev_packet);
zfree(&tidc->packet);
out:
- thread__zput(tidc->frontend_thread);
- thread__zput(tidc->decode_thread);
+ thread__zput(tidc->thread);
return rc;
}
@@ -908,8 +905,7 @@ static void cs_etm__free_traceid_ctxts(struct cs_etm_queue *etmq)
/* Free this decode context from the array */
tidc = etmq->traceid_ctxts[idx];
- thread__zput(tidc->frontend_thread);
- thread__zput(tidc->decode_thread);
+ thread__zput(tidc->thread);
zfree(&tidc->event_buf);
zfree(&tidc->callchain);
zfree(&tidc->last_branch);
@@ -1168,7 +1164,7 @@ static u32 cs_etm__frontend_mem_access(struct cs_etm_queue *etmq,
u64 address, size_t size, u8 *buffer)
{
return __cs_etm__mem_access(etmq, address, size, buffer, 0, packet->el,
- tidc->frontend_thread);
+ tidc->thread);
}
static u32 cs_etm__decoder_mem_access(struct cs_etm_queue *etmq, u8 trace_chan_id,
@@ -1183,7 +1179,7 @@ static u32 cs_etm__decoder_mem_access(struct cs_etm_queue *etmq, u8 trace_chan_i
return __cs_etm__mem_access(etmq, address, size, buffer,
mem_space, tidc->decode_el,
- tidc->decode_thread);
+ tidc->thread);
}
static struct cs_etm_queue *cs_etm__alloc_queue(void)
@@ -1472,30 +1468,6 @@ static int cs_etm__etmq_update_thread(struct cs_etm_queue *etmq,
return 0;
}
-/*
- * Set the thread and EL of the decode context which is ahead in time of the
- * frontend context.
- */
-int cs_etm__etmq_update_decode_context(struct cs_etm_queue *etmq,
- u8 trace_chan_id,
- ocsd_ex_level el, pid_t tid)
-{
- struct cs_etm_traceid_ctxt *tidc;
- int ret;
-
- tidc = cs_etm__etmq_get_traceid_ctxt(etmq, trace_chan_id);
- if (!tidc)
- return -EINVAL;
-
- ret = cs_etm__etmq_update_thread(etmq, el, tid,
- &tidc->decode_thread);
- if (ret)
- return ret;
-
- tidc->decode_el = el;
- return 0;
-}
-
bool cs_etm__has_timestamps(struct cs_etm_queue *etmq)
{
return etmq->etm->has_timestamps;
@@ -1590,13 +1562,13 @@ static void cs_etm__add_stack_event(struct cs_etm_queue *etmq,
size = cs_etm__instr_size(etmq, tidc, tidc->prev_packet, from);
/* Enable callchain so thread stack entry can be allocated */
- thread_stack__event(tidc->frontend_thread, tidc->prev_packet->cpu,
+ thread_stack__event(tidc->thread, tidc->prev_packet->cpu,
tidc->prev_packet->flags, from, to, size,
etmq->buffer->buffer_nr + 1,
etmq->etm->use_callchain,
tidc->br_stack_sz, 0);
} else {
- thread_stack__set_trace_nr(tidc->frontend_thread,
+ thread_stack__set_trace_nr(tidc->thread,
tidc->prev_packet->cpu,
etmq->buffer->buffer_nr + 1);
}
@@ -1607,14 +1579,14 @@ static void cs_etm__sample_branch_stack(struct cs_etm_auxtrace *etm,
struct perf_sample *sample)
{
if (etm->synth_opts.last_branch) {
- thread_stack__br_sample(tidc->frontend_thread, tidc->packet->cpu,
+ thread_stack__br_sample(tidc->thread, tidc->packet->cpu,
tidc->last_branch, tidc->br_stack_sz);
sample->branch_stack = tidc->last_branch;
}
if (etm->synth_opts.callchain) {
if (tidc->kernel_start)
- thread_stack__sample(tidc->frontend_thread,
+ thread_stack__sample(tidc->thread,
tidc->packet->cpu,
tidc->callchain,
etm->synth_opts.callchain_sz + 1,
@@ -1652,8 +1624,8 @@ static int cs_etm__synth_instruction_sample(struct cs_etm_queue *etmq,
sample.time = cs_etm__resolve_sample_time(etmq, tidc);
sample.ip = addr;
- sample.pid = thread__pid(tidc->frontend_thread);
- sample.tid = thread__tid(tidc->frontend_thread);
+ sample.pid = thread__pid(tidc->thread);
+ sample.tid = thread__tid(tidc->thread);
sample.id = etmq->etm->instructions_id;
sample.stream_id = etmq->etm->instructions_id;
sample.period = period;
@@ -1748,8 +1720,8 @@ static int cs_etm__synth_branch_sample(struct cs_etm_queue *etmq,
sample.time = cs_etm__resolve_sample_time(etmq, tidc);
sample.ip = ip;
- sample.pid = thread__pid(tidc->frontend_thread);
- sample.tid = thread__tid(tidc->frontend_thread);
+ sample.pid = thread__pid(tidc->thread);
+ sample.tid = thread__tid(tidc->thread);
sample.addr = cs_etm__first_executed_instr(tidc->packet);
sample.id = etmq->etm->branches_id;
sample.stream_id = etmq->etm->branches_id;
@@ -1994,16 +1966,17 @@ static int cs_etm__context(struct cs_etm_queue *etmq,
}
tidc->kernel_start = machine__kernel_start(machine);
+ tidc->decode_el = el;
ret = cs_etm__etmq_update_thread(etmq, el, tidc->packet->tid,
- &tidc->frontend_thread);
+ &tidc->thread);
if (ret)
goto err;
return 0;
err:
- thread__zput(tidc->frontend_thread);
+ thread__zput(tidc->thread);
tidc->kernel_start = 0;
return ret;
}
@@ -2052,7 +2025,7 @@ static int cs_etm__flush(struct cs_etm_queue *etmq,
/* Reset last branches after flush the trace */
if (etm->use_thread_stack)
- thread_stack__flush(tidc->frontend_thread);
+ thread_stack__flush(tidc->thread);
return err;
}
@@ -2164,15 +2137,11 @@ static int cs_etm__get_data_block(struct cs_etm_queue *etmq)
tidc = cs_etm__etmq_get_traceid_ctxt(etmq, inode->i);
if (!tidc)
return -ENOMEM;
- if (thread__tid(tidc->decode_thread) == etmq->buffer->tid)
+ if (thread__tid(tidc->thread) == etmq->buffer->tid)
continue;
- thread__zput(tidc->frontend_thread);
- thread__zput(tidc->decode_thread);
- tidc->frontend_thread = machine__findnew_thread(
- &etm->session->machines.host, -1,
- etmq->buffer->tid);
- tidc->decode_thread = machine__findnew_thread(
+ thread__zput(tidc->thread);
+ tidc->thread = machine__findnew_thread(
&etm->session->machines.host, -1,
etmq->buffer->tid);
}
diff --git a/tools/perf/util/cs-etm.h b/tools/perf/util/cs-etm.h
index 115def9c6abb..c41a7b5dd62f 100644
--- a/tools/perf/util/cs-etm.h
+++ b/tools/perf/util/cs-etm.h
@@ -260,21 +260,13 @@ struct cs_etm_traceid_ctxt {
u32 packet_count;
u64 cs_timestamp; /* Timestamp, in ns when has_virtual_ts == true */
- struct thread *decode_thread;
ocsd_ex_level decode_el;
- /*
- * Samples use the EL saved in their source packet. A branch sample
- * cannot change thread, so the thread is kept in the frontend context.
- */
- struct thread *frontend_thread;
+ struct thread *thread;
};
int cs_etm__get_cpu(struct cs_etm_queue *etmq, u8 trace_chan_id, int *cpu);
enum cs_etm_pid_fmt cs_etm__get_pid_fmt(struct cs_etm_queue *etmq);
-int cs_etm__etmq_update_decode_context(struct cs_etm_queue *etmq,
- u8 trace_chan_id, ocsd_ex_level el,
- pid_t tid);
bool cs_etm__has_timestamps(struct cs_etm_queue *etmq);
void cs_etm__etmq_set_traceid_ctxt_ready(struct cs_etm_queue *etmq,
u8 trace_chan_id);
--
2.43.0