History

perf cs-etm: Handle PERF_RECORD_AUX_OUTPUT_HW_ID packet

When using dynamically assigned CoreSight trace IDs the drivers can output the ID / CPU association as a PERF_RECORD_AUX_OUTPUT_HW_ID packet. Update cs-etm decoder to handle this packet by setting the CPU/Trace ID mapping.

Mike Leach committed 3 years ago

Commit b6521ea1 parent e5fa5b43 files · +250 −18Browse files

Unified · Split

3 files
  1. tools/include/linux/coresight-pmu.h +15 −0
  2. tools/perf/util/cs-etm-decoder/cs-etm-decoder.c +7 −0
  3. tools/perf/util/cs-etm.c +228 −18
tools/include/linux/coresight-pmu.h+15−0
@@ -31,6 +31,9 @@ · 30 unmodified lines
3131 */
3232 #define CORESIGHT_TRACE_ID_UNUSED_FLAG BIT(31)
3333
34+/* Value to set for unused trace ID values */
35+#define CORESIGHT_TRACE_ID_UNUSED_VAL 0x7F
36+
3437 /*
3538 * Below are the definition of bit offsets for perf option, and works as
3639 * arbitrary values for all ETM versions.
@@ -55,4 +58,16 @@ · 18 unmodified lines
5558 #define ETM4_CFG_BIT_RETSTK 12
5659 #define ETM4_CFG_BIT_VMID_OPT 15
5760
61+/*
62+ * Interpretation of the PERF_RECORD_AUX_OUTPUT_HW_ID payload.
63+ * Used to associate a CPU with the CoreSight Trace ID.
64+ * [07:00] - Trace ID - uses 8 bits to make value easy to read in file.
65+ * [59:08] - Unused (SBZ)
66+ * [63:60] - Version
67+ */
68+#define CS_AUX_HW_ID_TRACE_ID_MASK GENMASK_ULL(7, 0)
69+#define CS_AUX_HW_ID_VERSION_MASK GENMASK_ULL(63, 60)
70+
71+#define CS_AUX_HW_ID_CURR_VERSION 0
72+
5873 #endif
tools/perf/util/cs-etm-decoder/cs-etm-decoder.c+7−0
@@ -668,6 +668,7 @@ cs_etm_decoder__create_etm_decoder(struct cs_etm_decoder_params *d_params, · 667 unmodified lines
668668 switch (t_params->protocol) {
669669 case CS_ETM_PROTO_ETMV3:
670670 case CS_ETM_PROTO_PTM:
671+ csid = (t_params->etmv3.reg_idr & CORESIGHT_TRACE_ID_VAL_MASK);
671672 cs_etm_decoder__gen_etmv3_config(t_params, &config_etmv3);
672673 decoder->decoder_name = (t_params->protocol == CS_ETM_PROTO_ETMV3) ?
673674 OCSD_BUILTIN_DCD_ETMV3 :
@@ -675,11 +676,13 @@ cs_etm_decoder__create_etm_decoder(struct cs_etm_decoder_params *d_params, · 1 unmodified line
675676 trace_config = &config_etmv3;
676677 break;
677678 case CS_ETM_PROTO_ETMV4i:
679+ csid = (t_params->etmv4.reg_traceidr & CORESIGHT_TRACE_ID_VAL_MASK);
678680 cs_etm_decoder__gen_etmv4_config(t_params, &trace_config_etmv4);
679681 decoder->decoder_name = OCSD_BUILTIN_DCD_ETMV4I;
680682 trace_config = &trace_config_etmv4;
681683 break;
682684 case CS_ETM_PROTO_ETE:
685+ csid = (t_params->ete.reg_traceidr & CORESIGHT_TRACE_ID_VAL_MASK);
683686 cs_etm_decoder__gen_ete_config(t_params, &trace_config_ete);
684687 decoder->decoder_name = OCSD_BUILTIN_DCD_ETE;
685688 trace_config = &trace_config_ete;
@@ -688,6 +691,10 @@ cs_etm_decoder__create_etm_decoder(struct cs_etm_decoder_params *d_params, · 2 unmodified lines
688691 return -1;
689692 }
690693
694+ /* if the CPU has no trace ID associated, no decoder needed */
695+ if (csid == CORESIGHT_TRACE_ID_UNUSED_VAL)
696+ return 0;
697+
691698 if (d_params->operation == CS_ETM_OPERATION_DECODE) {
692699 if (ocsd_dt_create_decoder(decoder->dcd_tree,
693700 decoder->decoder_name,
tools/perf/util/cs-etm.c+228−18
@@ -220,6 +220,143 @@ static int cs_etm__map_trace_id(u8 trace_chan_id, u64 *cpu_metadata) · 219 unmodified lines
220220 return 0;
221221 }
222222
223+static int cs_etm__metadata_get_trace_id(u8 *trace_chan_id, u64 *cpu_metadata)
224+{
225+ u64 cs_etm_magic = cpu_metadata[CS_ETM_MAGIC];
226+
227+ switch (cs_etm_magic) {
228+ case __perf_cs_etmv3_magic:
229+ *trace_chan_id = (u8)(cpu_metadata[CS_ETM_ETMTRACEIDR] &
230+ CORESIGHT_TRACE_ID_VAL_MASK);
231+ break;
232+ case __perf_cs_etmv4_magic:
233+ case __perf_cs_ete_magic:
234+ *trace_chan_id = (u8)(cpu_metadata[CS_ETMV4_TRCTRACEIDR] &
235+ CORESIGHT_TRACE_ID_VAL_MASK);
236+ break;
237+ default:
238+ return -EINVAL;
239+ }
240+ return 0;
241+}
242+
243+/*
244+ * update metadata trace ID from the value found in the AUX_HW_INFO packet.
245+ * This will also clear the CORESIGHT_TRACE_ID_UNUSED_FLAG flag if present.
246+ */
247+static int cs_etm__metadata_set_trace_id(u8 trace_chan_id, u64 *cpu_metadata)
248+{
249+ u64 cs_etm_magic = cpu_metadata[CS_ETM_MAGIC];
250+
251+ switch (cs_etm_magic) {
252+ case __perf_cs_etmv3_magic:
253+ cpu_metadata[CS_ETM_ETMTRACEIDR] = trace_chan_id;
254+ break;
255+ case __perf_cs_etmv4_magic:
256+ case __perf_cs_ete_magic:
257+ cpu_metadata[CS_ETMV4_TRCTRACEIDR] = trace_chan_id;
258+ break;
259+
260+ default:
261+ return -EINVAL;
262+ }
263+ return 0;
264+}
265+
266+/*
267+ * FIELD_GET (linux/bitfield.h) not available outside kernel code,
268+ * and the header contains too many dependencies to just copy over,
269+ * so roll our own based on the original
270+ */
271+#define __bf_shf(x) (__builtin_ffsll(x) - 1)
272+#define FIELD_GET(_mask, _reg) \
273+ ({ \
274+ (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
275+ })
276+
277+/*
278+ * Handle the PERF_RECORD_AUX_OUTPUT_HW_ID event.
279+ *
280+ * The payload associates the Trace ID and the CPU.
281+ * The routine is tolerant of seeing multiple packets with the same association,
282+ * but a CPU / Trace ID association changing during a session is an error.
283+ */
284+static int cs_etm__process_aux_output_hw_id(struct perf_session *session,
285+ union perf_event *event)
286+{
287+ struct cs_etm_auxtrace *etm;
288+ struct perf_sample sample;
289+ struct int_node *inode;
290+ struct evsel *evsel;
291+ u64 *cpu_data;
292+ u64 hw_id;
293+ int cpu, version, err;
294+ u8 trace_chan_id, curr_chan_id;
295+
296+ /* extract and parse the HW ID */
297+ hw_id = event->aux_output_hw_id.hw_id;
298+ version = FIELD_GET(CS_AUX_HW_ID_VERSION_MASK, hw_id);
299+ trace_chan_id = FIELD_GET(CS_AUX_HW_ID_TRACE_ID_MASK, hw_id);
300+
301+ /* check that we can handle this version */
302+ if (version > CS_AUX_HW_ID_CURR_VERSION)
303+ return -EINVAL;
304+
305+ /* get access to the etm metadata */
306+ etm = container_of(session->auxtrace, struct cs_etm_auxtrace, auxtrace);
307+ if (!etm || !etm->metadata)
308+ return -EINVAL;
309+
310+ /* parse the sample to get the CPU */
311+ evsel = evlist__event2evsel(session->evlist, event);
312+ if (!evsel)
313+ return -EINVAL;
314+ err = evsel__parse_sample(evsel, event, &sample);
315+ if (err)
316+ return err;
317+ cpu = sample.cpu;
318+ if (cpu == -1) {
319+ /* no CPU in the sample - possibly recorded with an old version of perf */
320+ pr_err("CS_ETM: no CPU AUX_OUTPUT_HW_ID sample. Use compatible perf to record.");
321+ return -EINVAL;
322+ }
323+
324+ /* See if the ID is mapped to a CPU, and it matches the current CPU */
325+ inode = intlist__find(traceid_list, trace_chan_id);
326+ if (inode) {
327+ cpu_data = inode->priv;
328+ if ((int)cpu_data[CS_ETM_CPU] != cpu) {
329+ pr_err("CS_ETM: map mismatch between HW_ID packet CPU and Trace ID\n");
330+ return -EINVAL;
331+ }
332+
333+ /* check that the mapped ID matches */
334+ err = cs_etm__metadata_get_trace_id(&curr_chan_id, cpu_data);
335+ if (err)
336+ return err;
337+ if (curr_chan_id != trace_chan_id) {
338+ pr_err("CS_ETM: mismatch between CPU trace ID and HW_ID packet ID\n");
339+ return -EINVAL;
340+ }
341+
342+ /* mapped and matched - return OK */
343+ return 0;
344+ }
345+
346+ /* not one we've seen before - lets map it */
347+ cpu_data = etm->metadata[cpu];
348+ err = cs_etm__map_trace_id(trace_chan_id, cpu_data);
349+ if (err)
350+ return err;
351+
352+ /*
353+ * if we are picking up the association from the packet, need to plug
354+ * the correct trace ID into the metadata for setting up decoders later.
355+ */
356+ err = cs_etm__metadata_set_trace_id(trace_chan_id, cpu_data);
357+ return err;
358+}
359+
223360 void cs_etm__etmq_set_traceid_queue_timestamp(struct cs_etm_queue *etmq,
224361 u8 trace_chan_id)
225362 {
@@ -2668,11 +2805,16 @@ static int cs_etm__queue_aux_fragment(struct perf_session *session, off_t file_o · 2442 unmodified lines
26682805 }
26692806
26702807 /*
2671− * In per-thread mode, CPU is set to -1, but TID will be set instead. See
2672− * auxtrace_mmap_params__set_idx(). Return 'not found' if neither CPU nor TID match.
2808+ * In per-thread mode, auxtrace CPU is set to -1, but TID will be set instead. See
2809+ * auxtrace_mmap_params__set_idx(). However, the sample AUX event will contain a
2810+ * CPU as we set this always for the AUX_OUTPUT_HW_ID event.
2811+ * So now compare only TIDs if auxtrace CPU is -1, and CPUs if auxtrace CPU is not -1.
2812+ * Return 'not found' if mismatch.
26732813 */
2674− if ((auxtrace_event->cpu == (__u32) -1 && auxtrace_event->tid != sample->tid) ||
2675− auxtrace_event->cpu != sample->cpu)
2814+ if (auxtrace_event->cpu == (__u32) -1) {
2815+ if (auxtrace_event->tid != sample->tid)
2816+ return 1;
2817+ } else if (auxtrace_event->cpu != sample->cpu)
26762818 return 1;
26772819
26782820 if (aux_event->flags & PERF_AUX_FLAG_OVERWRITE) {
@@ -2721,6 +2863,17 @@ static int cs_etm__queue_aux_fragment(struct perf_session *session, off_t file_o · 42 unmodified lines
27212863 return 1;
27222864 }
27232865
2866+static int cs_etm__process_aux_hw_id_cb(struct perf_session *session, union perf_event *event,
2867+ u64 offset __maybe_unused, void *data __maybe_unused)
2868+{
2869+ /* look to handle PERF_RECORD_AUX_OUTPUT_HW_ID early to ensure decoders can be set up */
2870+ if (event->header.type == PERF_RECORD_AUX_OUTPUT_HW_ID) {
2871+ (*(int *)data)++; /* increment found count */
2872+ return cs_etm__process_aux_output_hw_id(session, event);
2873+ }
2874+ return 0;
2875+}
2876+
27242877 static int cs_etm__queue_aux_records_cb(struct perf_session *session, union perf_event *event,
27252878 u64 offset __maybe_unused, void *data __maybe_unused)
27262879 {
@@ -2839,13 +2992,13 @@ static int cs_etm__map_trace_ids_metadata(int num_cpu, u64 **metadata) · 112 unmodified lines
28392992 cs_etm_magic = metadata[i][CS_ETM_MAGIC];
28402993 switch (cs_etm_magic) {
28412994 case __perf_cs_etmv3_magic:
2842− trace_chan_id = (u8)((metadata[i][CS_ETM_ETMTRACEIDR]) &
2843− CORESIGHT_TRACE_ID_VAL_MASK);
2995+ metadata[i][CS_ETM_ETMTRACEIDR] &= CORESIGHT_TRACE_ID_VAL_MASK;
2996+ trace_chan_id = (u8)(metadata[i][CS_ETM_ETMTRACEIDR]);
28442997 break;
28452998 case __perf_cs_etmv4_magic:
28462999 case __perf_cs_ete_magic:
2847− trace_chan_id = (u8)((metadata[i][CS_ETMV4_TRCTRACEIDR]) &
2848− CORESIGHT_TRACE_ID_VAL_MASK);
3000+ metadata[i][CS_ETMV4_TRCTRACEIDR] &= CORESIGHT_TRACE_ID_VAL_MASK;
3001+ trace_chan_id = (u8)(metadata[i][CS_ETMV4_TRCTRACEIDR]);
28493002 break;
28503003 default:
28513004 /* unknown magic number */
@@ -2858,6 +3011,35 @@ static int cs_etm__map_trace_ids_metadata(int num_cpu, u64 **metadata) · 6 unmodified lines
28583011 return 0;
28593012 }
28603013
3014+/*
3015+ * If we found AUX_HW_ID packets, then set any metadata marked as unused to the
3016+ * unused value to reduce the number of unneeded decoders created.
3017+ */
3018+static int cs_etm__clear_unused_trace_ids_metadata(int num_cpu, u64 **metadata)
3019+{
3020+ u64 cs_etm_magic;
3021+ int i;
3022+
3023+ for (i = 0; i < num_cpu; i++) {
3024+ cs_etm_magic = metadata[i][CS_ETM_MAGIC];
3025+ switch (cs_etm_magic) {
3026+ case __perf_cs_etmv3_magic:
3027+ if (metadata[i][CS_ETM_ETMTRACEIDR] & CORESIGHT_TRACE_ID_UNUSED_FLAG)
3028+ metadata[i][CS_ETM_ETMTRACEIDR] = CORESIGHT_TRACE_ID_UNUSED_VAL;
3029+ break;
3030+ case __perf_cs_etmv4_magic:
3031+ case __perf_cs_ete_magic:
3032+ if (metadata[i][CS_ETMV4_TRCTRACEIDR] & CORESIGHT_TRACE_ID_UNUSED_FLAG)
3033+ metadata[i][CS_ETMV4_TRCTRACEIDR] = CORESIGHT_TRACE_ID_UNUSED_VAL;
3034+ break;
3035+ default:
3036+ /* unknown magic number */
3037+ return -EINVAL;
3038+ }
3039+ }
3040+ return 0;
3041+}
3042+
28613043 int cs_etm__process_auxtrace_info_full(union perf_event *event,
28623044 struct perf_session *session)
28633045 {
@@ -2869,6 +3051,7 @@ int cs_etm__process_auxtrace_info_full(union perf_event *event, · 5 unmodified lines
28693051 int priv_size = 0;
28703052 int num_cpu;
28713053 int err = 0;
3054+ int aux_hw_id_found;
28723055 int i, j;
28733056 u64 *ptr = NULL;
28743057 u64 **metadata = NULL;
@@ -3017,8 +3200,43 @@ int cs_etm__process_auxtrace_info_full(union perf_event *event, · 142 unmodified lines
30173200 if (err)
30183201 goto err_delete_thread;
30193202
3020− /* before aux records are queued, need to map metadata to trace IDs */
3021− err = cs_etm__map_trace_ids_metadata(num_cpu, metadata);
3203+ /*
3204+ * Map Trace ID values to CPU metadata.
3205+ *
3206+ * Trace metadata will always contain Trace ID values from the legacy algorithm. If the
3207+ * files has been recorded by a "new" perf updated to handle AUX_HW_ID then the metadata
3208+ * ID value will also have the CORESIGHT_TRACE_ID_UNUSED_FLAG set.
3209+ *
3210+ * The updated kernel drivers that use AUX_HW_ID to sent Trace IDs will attempt to use
3211+ * the same IDs as the old algorithm as far as is possible, unless there are clashes
3212+ * in which case a different value will be used. This means an older perf may still
3213+ * be able to record and read files generate on a newer system.
3214+ *
3215+ * For a perf able to interpret AUX_HW_ID packets we first check for the presence of
3216+ * those packets. If they are there then the values will be mapped and plugged into
3217+ * the metadata. We then set any remaining metadata values with the used flag to a
3218+ * value CORESIGHT_TRACE_ID_UNUSED_VAL - which indicates no decoder is required.
3219+ *
3220+ * If no AUX_HW_ID packets are present - which means a file recorded on an old kernel
3221+ * then we map Trace ID values to CPU directly from the metadata - clearing any unused
3222+ * flags if present.
3223+ */
3224+
3225+ /* first scan for AUX_OUTPUT_HW_ID records to map trace ID values to CPU metadata */
3226+ aux_hw_id_found = 0;
3227+ err = perf_session__peek_events(session, session->header.data_offset,
3228+ session->header.data_size,
3229+ cs_etm__process_aux_hw_id_cb, &aux_hw_id_found);
3230+ if (err)
3231+ goto err_delete_thread;
3232+
3233+ /* if HW ID found then clear any unused metadata ID values */
3234+ if (aux_hw_id_found)
3235+ err = cs_etm__clear_unused_trace_ids_metadata(num_cpu, metadata);
3236+ /* otherwise, this is a file with metadata values only, map from metadata */
3237+ else
3238+ err = cs_etm__map_trace_ids_metadata(num_cpu, metadata);
3239+
30223240 if (err)
30233241 goto err_delete_thread;
30243242
@@ -3027,14 +3245,6 @@ int cs_etm__process_auxtrace_info_full(union perf_event *event, · 2 unmodified lines
30273245 goto err_delete_thread;
30283246
30293247 etm->data_queued = etm->queues.populated;
3030− /*
3031− * Print warning in pipe mode, see cs_etm__process_auxtrace_event() and
3032− * cs_etm__queue_aux_fragment() for details relating to limitations.
3033− */
3034− if (!etm->data_queued)
3035− pr_warning("CS ETM warning: Coresight decode and TRBE support requires random file access.\n"
3036− "Continuing with best effort decoding in piped mode.\n\n");
3037−
30383248 return 0;
30393249
30403250 err_delete_thread:

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t