[PATCH 27/34] perf cs-etm: Add packet-only timestamp decoder ops

From: James Clark

Date: Wed Nov 18 2026 - 11:32:22 EST


Add OpenCSD packet-processing callbacks that find the next timestamp
without fetching instructions or producing instruction ranges. Bound the
search with CS_ETM_TIMESTAMP_SEARCH_MAX packets.

Keep the timestamp search state in cs_etm_timestamp_decoder, separate
from the full packet decoder. Each source's callback context identifies
its protocol. The frontend continues to use the existing full decoder.

Signed-off-by: James Clark <james.clark@xxxxxxxxxx>
---
tools/perf/util/cs-etm-decoder/cs-etm-decoder.c | 79 +++++++++++++++++++++++++
1 file changed, 79 insertions(+)

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 4f8da538d645..685d8d75912f 100644
--- a/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c
+++ b/tools/perf/util/cs-etm-decoder/cs-etm-decoder.c
@@ -33,6 +33,14 @@
#define CS_PKT_MON 0
#endif

+/*
+ * Limit when searching forwards for a timestamp.
+ *
+ * Timestamps follow context packets (which appear on trace on) after only
+ * 1 or 2 atoms. Allow up to 1024 packets across all interleaved trace IDs.
+ */
+#define CS_ETM_TIMESTAMP_SEARCH_MAX 1024
+
struct cs_etm_decoder {
void *data;
void (*packet_printer)(const char *msg, void *data);
@@ -48,6 +56,19 @@ struct cs_etm_packet_decoder {
struct cs_etm_traceid_ctxt *traceid_ctxt;
};

+struct cs_etm_timestamp_source {
+ struct cs_etm_timestamp_decoder *decoder;
+ int protocol;
+};
+
+struct cs_etm_timestamp_decoder {
+ struct cs_etm_decoder base;
+ u64 timestamp; /* Raw trace timestamp. */
+ u64 timestamp_search_index;
+ u32 timestamp_packet_count;
+ struct cs_etm_timestamp_source sources[];
+};
+
static u32
cs_etm_decoder__mem_access(const void *context,
const ocsd_vaddr_t address,
@@ -512,6 +533,64 @@ static ocsd_datapath_resp_t cs_etm_decoder__gen_trace_elem_printer(
return resp;
}

+static ocsd_datapath_resp_t __maybe_unused
+cs_etm_decoder__timestamp(const void *context, const ocsd_datapath_op_t op,
+ const ocsd_trc_index_t indx, const void *packet)
+{
+ const struct cs_etm_timestamp_source *source = context;
+ struct cs_etm_timestamp_decoder *decoder = source->decoder;
+ bool timestamp = false;
+
+ if (op != OCSD_OP_DATA)
+ return OCSD_RESP_CONT;
+ if (indx < decoder->timestamp_search_index)
+ return OCSD_RESP_CONT;
+
+ decoder->timestamp_packet_count++;
+ switch (source->protocol) {
+ case CS_ETM_PROTO_ETMV3: {
+ const ocsd_etmv3_pkt *pkt = packet;
+
+ if (pkt->type == ETM3_PKT_TIMESTAMP) {
+ decoder->timestamp = pkt->timestamp;
+ timestamp = true;
+ }
+ break;
+ }
+ case CS_ETM_PROTO_PTM: {
+ const ocsd_ptm_pkt *pkt = packet;
+
+ if (pkt->type == PTM_PKT_TIMESTAMP) {
+ decoder->timestamp = pkt->timestamp;
+ timestamp = true;
+ }
+ break;
+ }
+ case CS_ETM_PROTO_ETMV4i:
+ case CS_ETM_PROTO_ETE: {
+ const ocsd_etmv4_i_pkt *pkt = packet;
+
+ if (pkt->type == ETM4_PKT_I_TIMESTAMP) {
+ decoder->timestamp = pkt->ts.timestamp;
+ timestamp = true;
+ }
+ break;
+ }
+ default:
+ return OCSD_RESP_FATAL_INVALID_PARAM;
+ }
+
+ /* Report where the timestamp was found */
+ if (timestamp)
+ decoder->timestamp_search_index = indx;
+
+ if (timestamp ||
+ decoder->timestamp_packet_count >= CS_ETM_TIMESTAMP_SEARCH_MAX)
+ return OCSD_RESP_WAIT;
+
+ return OCSD_RESP_CONT;
+}
+
static int
cs_etm_decoder__create_etm_decoder(struct cs_etm_decoder_params *d_params,
struct cs_etm_trace_params *t_params,

--
2.43.0