1 // SPDX-License-Identifier: GPL-2.0
3 * Arm Statistical Profiling Extensions (SPE) support
4 * Copyright (c) 2017-2018, Arm Ltd.
11 #include <linux/bitops.h>
12 #include <linux/kernel.h>
13 #include <linux/log2.h>
14 #include <linux/types.h>
15 #include <linux/zalloc.h>
28 #include "thread-stack.h"
31 #include "util/synthetic-events.h"
34 #include "arm-spe-decoder/arm-spe-decoder.h"
35 #include "arm-spe-decoder/arm-spe-pkt-decoder.h"
37 #include "../../arch/arm64/include/asm/cputype.h"
38 #define MAX_TIMESTAMP (~0ULL)
41 struct auxtrace auxtrace;
42 struct auxtrace_queues queues;
43 struct auxtrace_heap heap;
44 struct itrace_synth_opts synth_opts;
46 struct perf_session *session;
47 struct machine *machine;
50 struct perf_tsc_conversion tc;
60 u8 sample_remote_access;
62 u8 sample_instructions;
63 u64 instructions_sample_period;
78 unsigned long num_events;
79 u8 use_ctx_pkt_for_pid;
87 struct arm_spe_queue {
89 unsigned int queue_nr;
90 struct auxtrace_buffer *buffer;
91 struct auxtrace_buffer *old_buffer;
92 union perf_event *event_buf;
98 struct arm_spe_decoder *decoder;
101 struct thread *thread;
102 u64 period_instructions;
106 static void arm_spe_dump(struct arm_spe *spe __maybe_unused,
107 unsigned char *buf, size_t len)
109 struct arm_spe_pkt packet;
112 char desc[ARM_SPE_PKT_DESC_MAX];
113 const char *color = PERF_COLOR_BLUE;
115 color_fprintf(stdout, color,
116 ". ... ARM SPE data: size %#zx bytes\n",
120 ret = arm_spe_get_packet(buf, len, &packet);
126 color_fprintf(stdout, color, " %08zx: ", pos);
127 for (i = 0; i < pkt_len; i++)
128 color_fprintf(stdout, color, " %02x", buf[i]);
130 color_fprintf(stdout, color, " ");
132 ret = arm_spe_pkt_desc(&packet, desc,
133 ARM_SPE_PKT_DESC_MAX);
135 color_fprintf(stdout, color, " %s\n", desc);
137 color_fprintf(stdout, color, " Bad packet!\n");
145 static void arm_spe_dump_event(struct arm_spe *spe, unsigned char *buf,
149 arm_spe_dump(spe, buf, len);
152 static int arm_spe_get_trace(struct arm_spe_buffer *b, void *data)
154 struct arm_spe_queue *speq = data;
155 struct auxtrace_buffer *buffer = speq->buffer;
156 struct auxtrace_buffer *old_buffer = speq->old_buffer;
157 struct auxtrace_queue *queue;
159 queue = &speq->spe->queues.queue_array[speq->queue_nr];
161 buffer = auxtrace_buffer__next(queue, buffer);
162 /* If no more data, drop the previous auxtrace_buffer and return */
165 auxtrace_buffer__drop_data(old_buffer);
170 speq->buffer = buffer;
172 /* If the aux_buffer doesn't have data associated, try to load it */
174 /* get the file desc associated with the perf data file */
175 int fd = perf_data__fd(speq->spe->session->data);
177 buffer->data = auxtrace_buffer__get_data(buffer, fd);
182 b->len = buffer->size;
183 b->buf = buffer->data;
187 auxtrace_buffer__drop_data(old_buffer);
188 speq->old_buffer = buffer;
190 auxtrace_buffer__drop_data(buffer);
191 return arm_spe_get_trace(b, data);
197 static struct arm_spe_queue *arm_spe__alloc_queue(struct arm_spe *spe,
198 unsigned int queue_nr)
200 struct arm_spe_params params = { .get_trace = 0, };
201 struct arm_spe_queue *speq;
203 speq = zalloc(sizeof(*speq));
207 speq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
208 if (!speq->event_buf)
212 speq->queue_nr = queue_nr;
216 speq->period_instructions = 0;
219 params.get_trace = arm_spe_get_trace;
222 /* create new decoder */
223 speq->decoder = arm_spe_decoder_new(¶ms);
230 zfree(&speq->event_buf);
236 static inline u8 arm_spe_cpumode(struct arm_spe *spe, u64 ip)
238 return ip >= spe->kernel_start ?
239 PERF_RECORD_MISC_KERNEL :
240 PERF_RECORD_MISC_USER;
243 static void arm_spe_set_pid_tid_cpu(struct arm_spe *spe,
244 struct auxtrace_queue *queue)
246 struct arm_spe_queue *speq = queue->priv;
249 tid = machine__get_current_tid(spe->machine, speq->cpu);
252 thread__zput(speq->thread);
254 speq->tid = queue->tid;
256 if ((!speq->thread) && (speq->tid != -1)) {
257 speq->thread = machine__find_thread(spe->machine, -1,
262 speq->pid = thread__pid(speq->thread);
263 if (queue->cpu == -1)
264 speq->cpu = thread__cpu(speq->thread);
268 static int arm_spe_set_tid(struct arm_spe_queue *speq, pid_t tid)
270 struct arm_spe *spe = speq->spe;
271 int err = machine__set_current_tid(spe->machine, speq->cpu, -1, tid);
276 arm_spe_set_pid_tid_cpu(spe, &spe->queues.queue_array[speq->queue_nr]);
281 static u64 *arm_spe__get_metadata_by_cpu(struct arm_spe *spe, u64 cpu)
288 for (i = 0; i < spe->metadata_nr_cpu; i++)
289 if (spe->metadata[i][ARM_SPE_CPU] == cpu)
290 return spe->metadata[i];
295 static struct simd_flags arm_spe__synth_simd_flags(const struct arm_spe_record *record)
297 struct simd_flags simd_flags = {};
299 if ((record->op & ARM_SPE_OP_LDST) && (record->op & ARM_SPE_OP_SVE_LDST))
300 simd_flags.arch |= SIMD_OP_FLAGS_ARCH_SVE;
302 if ((record->op & ARM_SPE_OP_OTHER) && (record->op & ARM_SPE_OP_SVE_OTHER))
303 simd_flags.arch |= SIMD_OP_FLAGS_ARCH_SVE;
305 if (record->type & ARM_SPE_SVE_PARTIAL_PRED)
306 simd_flags.pred |= SIMD_OP_FLAGS_PRED_PARTIAL;
308 if (record->type & ARM_SPE_SVE_EMPTY_PRED)
309 simd_flags.pred |= SIMD_OP_FLAGS_PRED_EMPTY;
314 static void arm_spe_prep_sample(struct arm_spe *spe,
315 struct arm_spe_queue *speq,
316 union perf_event *event,
317 struct perf_sample *sample)
319 struct arm_spe_record *record = &speq->decoder->record;
321 if (!spe->timeless_decoding)
322 sample->time = tsc_to_perf_time(record->timestamp, &spe->tc);
324 sample->ip = record->from_ip;
325 sample->cpumode = arm_spe_cpumode(spe, sample->ip);
326 sample->pid = speq->pid;
327 sample->tid = speq->tid;
329 sample->cpu = speq->cpu;
330 sample->simd_flags = arm_spe__synth_simd_flags(record);
332 event->sample.header.type = PERF_RECORD_SAMPLE;
333 event->sample.header.misc = sample->cpumode;
334 event->sample.header.size = sizeof(struct perf_event_header);
337 static int arm_spe__inject_event(union perf_event *event, struct perf_sample *sample, u64 type)
339 event->header.size = perf_event__sample_event_size(sample, type, 0);
340 return perf_event__synthesize_sample(event, type, 0, sample);
344 arm_spe_deliver_synth_event(struct arm_spe *spe,
345 struct arm_spe_queue *speq __maybe_unused,
346 union perf_event *event,
347 struct perf_sample *sample)
351 if (spe->synth_opts.inject) {
352 ret = arm_spe__inject_event(event, sample, spe->sample_type);
357 ret = perf_session__deliver_synth_event(spe->session, event, sample);
359 pr_err("ARM SPE: failed to deliver event, error %d\n", ret);
364 static int arm_spe__synth_mem_sample(struct arm_spe_queue *speq,
365 u64 spe_events_id, u64 data_src)
367 struct arm_spe *spe = speq->spe;
368 struct arm_spe_record *record = &speq->decoder->record;
369 union perf_event *event = speq->event_buf;
370 struct perf_sample sample = { .ip = 0, };
372 arm_spe_prep_sample(spe, speq, event, &sample);
374 sample.id = spe_events_id;
375 sample.stream_id = spe_events_id;
376 sample.addr = record->virt_addr;
377 sample.phys_addr = record->phys_addr;
378 sample.data_src = data_src;
379 sample.weight = record->latency;
381 return arm_spe_deliver_synth_event(spe, speq, event, &sample);
384 static int arm_spe__synth_branch_sample(struct arm_spe_queue *speq,
387 struct arm_spe *spe = speq->spe;
388 struct arm_spe_record *record = &speq->decoder->record;
389 union perf_event *event = speq->event_buf;
390 struct perf_sample sample = { .ip = 0, };
392 arm_spe_prep_sample(spe, speq, event, &sample);
394 sample.id = spe_events_id;
395 sample.stream_id = spe_events_id;
396 sample.addr = record->to_ip;
397 sample.weight = record->latency;
398 sample.flags = speq->flags;
400 return arm_spe_deliver_synth_event(spe, speq, event, &sample);
403 static int arm_spe__synth_instruction_sample(struct arm_spe_queue *speq,
404 u64 spe_events_id, u64 data_src)
406 struct arm_spe *spe = speq->spe;
407 struct arm_spe_record *record = &speq->decoder->record;
408 union perf_event *event = speq->event_buf;
409 struct perf_sample sample = { .ip = 0, };
412 * Handles perf instruction sampling period.
414 speq->period_instructions++;
415 if (speq->period_instructions < spe->instructions_sample_period)
417 speq->period_instructions = 0;
419 arm_spe_prep_sample(spe, speq, event, &sample);
421 sample.id = spe_events_id;
422 sample.stream_id = spe_events_id;
423 sample.addr = record->to_ip;
424 sample.phys_addr = record->phys_addr;
425 sample.data_src = data_src;
426 sample.period = spe->instructions_sample_period;
427 sample.weight = record->latency;
428 sample.flags = speq->flags;
430 return arm_spe_deliver_synth_event(spe, speq, event, &sample);
433 static const struct midr_range common_ds_encoding_cpus[] = {
434 MIDR_ALL_VERSIONS(MIDR_CORTEX_A720),
435 MIDR_ALL_VERSIONS(MIDR_CORTEX_A725),
436 MIDR_ALL_VERSIONS(MIDR_CORTEX_X1C),
437 MIDR_ALL_VERSIONS(MIDR_CORTEX_X3),
438 MIDR_ALL_VERSIONS(MIDR_CORTEX_X925),
439 MIDR_ALL_VERSIONS(MIDR_NEOVERSE_N1),
440 MIDR_ALL_VERSIONS(MIDR_NEOVERSE_N2),
441 MIDR_ALL_VERSIONS(MIDR_NEOVERSE_V1),
442 MIDR_ALL_VERSIONS(MIDR_NEOVERSE_V2),
446 static void arm_spe__sample_flags(struct arm_spe_queue *speq)
448 const struct arm_spe_record *record = &speq->decoder->record;
451 if (record->op & ARM_SPE_OP_BRANCH_ERET) {
452 speq->flags = PERF_IP_FLAG_BRANCH;
454 if (record->type & ARM_SPE_BRANCH_MISS)
455 speq->flags |= PERF_IP_FLAG_BRANCH_MISS;
459 static void arm_spe__synth_data_source_common(const struct arm_spe_record *record,
460 union perf_mem_data_src *data_src)
463 * Even though four levels of cache hierarchy are possible, no known
464 * production Neoverse systems currently include more than three levels
465 * so for the time being we assume three exist. If a production system
466 * is built with four the this function would have to be changed to
467 * detect the number of levels for reporting.
471 * We have no data on the hit level or data source for stores in the
472 * Neoverse SPE records.
474 if (record->op & ARM_SPE_OP_ST) {
475 data_src->mem_lvl = PERF_MEM_LVL_NA;
476 data_src->mem_lvl_num = PERF_MEM_LVLNUM_NA;
477 data_src->mem_snoop = PERF_MEM_SNOOP_NA;
481 switch (record->source) {
482 case ARM_SPE_COMMON_DS_L1D:
483 data_src->mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT;
484 data_src->mem_lvl_num = PERF_MEM_LVLNUM_L1;
485 data_src->mem_snoop = PERF_MEM_SNOOP_NONE;
487 case ARM_SPE_COMMON_DS_L2:
488 data_src->mem_lvl = PERF_MEM_LVL_L2 | PERF_MEM_LVL_HIT;
489 data_src->mem_lvl_num = PERF_MEM_LVLNUM_L2;
490 data_src->mem_snoop = PERF_MEM_SNOOP_NONE;
492 case ARM_SPE_COMMON_DS_PEER_CORE:
493 data_src->mem_lvl = PERF_MEM_LVL_L2 | PERF_MEM_LVL_HIT;
494 data_src->mem_lvl_num = PERF_MEM_LVLNUM_L2;
495 data_src->mem_snoopx = PERF_MEM_SNOOPX_PEER;
498 * We don't know if this is L1, L2 but we do know it was a cache-2-cache
499 * transfer, so set SNOOPX_PEER
501 case ARM_SPE_COMMON_DS_LOCAL_CLUSTER:
502 case ARM_SPE_COMMON_DS_PEER_CLUSTER:
503 data_src->mem_lvl = PERF_MEM_LVL_L3 | PERF_MEM_LVL_HIT;
504 data_src->mem_lvl_num = PERF_MEM_LVLNUM_L3;
505 data_src->mem_snoopx = PERF_MEM_SNOOPX_PEER;
508 * System cache is assumed to be L3
510 case ARM_SPE_COMMON_DS_SYS_CACHE:
511 data_src->mem_lvl = PERF_MEM_LVL_L3 | PERF_MEM_LVL_HIT;
512 data_src->mem_lvl_num = PERF_MEM_LVLNUM_L3;
513 data_src->mem_snoop = PERF_MEM_SNOOP_HIT;
516 * We don't know what level it hit in, except it came from the other
519 case ARM_SPE_COMMON_DS_REMOTE:
520 data_src->mem_lvl = PERF_MEM_LVL_REM_CCE1;
521 data_src->mem_lvl_num = PERF_MEM_LVLNUM_ANY_CACHE;
522 data_src->mem_remote = PERF_MEM_REMOTE_REMOTE;
523 data_src->mem_snoopx = PERF_MEM_SNOOPX_PEER;
525 case ARM_SPE_COMMON_DS_DRAM:
526 data_src->mem_lvl = PERF_MEM_LVL_LOC_RAM | PERF_MEM_LVL_HIT;
527 data_src->mem_lvl_num = PERF_MEM_LVLNUM_RAM;
528 data_src->mem_snoop = PERF_MEM_SNOOP_NONE;
535 static void arm_spe__synth_memory_level(const struct arm_spe_record *record,
536 union perf_mem_data_src *data_src)
538 if (record->type & (ARM_SPE_LLC_ACCESS | ARM_SPE_LLC_MISS)) {
539 data_src->mem_lvl = PERF_MEM_LVL_L3;
541 if (record->type & ARM_SPE_LLC_MISS)
542 data_src->mem_lvl |= PERF_MEM_LVL_MISS;
544 data_src->mem_lvl |= PERF_MEM_LVL_HIT;
545 } else if (record->type & (ARM_SPE_L1D_ACCESS | ARM_SPE_L1D_MISS)) {
546 data_src->mem_lvl = PERF_MEM_LVL_L1;
548 if (record->type & ARM_SPE_L1D_MISS)
549 data_src->mem_lvl |= PERF_MEM_LVL_MISS;
551 data_src->mem_lvl |= PERF_MEM_LVL_HIT;
554 if (record->type & ARM_SPE_REMOTE_ACCESS)
555 data_src->mem_lvl |= PERF_MEM_LVL_REM_CCE1;
558 static bool arm_spe__is_common_ds_encoding(struct arm_spe_queue *speq)
560 struct arm_spe *spe = speq->spe;
562 u64 *metadata = NULL;
565 /* Metadata version 1 assumes all CPUs are the same (old behavior) */
566 if (spe->metadata_ver == 1) {
569 pr_warning_once("Old SPE metadata, re-record to improve decode accuracy\n");
570 cpuid = perf_env__cpuid(spe->session->evlist->env);
571 midr = strtol(cpuid, NULL, 16);
573 /* CPU ID is -1 for per-thread mode */
576 * On the heterogeneous system, due to CPU ID is -1,
577 * cannot confirm the data source packet is supported.
579 if (!spe->is_homogeneous)
582 /* In homogeneous system, simply use CPU0's metadata */
584 metadata = spe->metadata[0];
586 metadata = arm_spe__get_metadata_by_cpu(spe, speq->cpu);
592 midr = metadata[ARM_SPE_CPU_MIDR];
595 is_in_cpu_list = is_midr_in_range_list(midr, common_ds_encoding_cpus);
602 static u64 arm_spe__synth_data_source(struct arm_spe_queue *speq,
603 const struct arm_spe_record *record)
605 union perf_mem_data_src data_src = { .mem_op = PERF_MEM_OP_NA };
606 bool is_common = arm_spe__is_common_ds_encoding(speq);
608 if (record->op & ARM_SPE_OP_LD)
609 data_src.mem_op = PERF_MEM_OP_LOAD;
610 else if (record->op & ARM_SPE_OP_ST)
611 data_src.mem_op = PERF_MEM_OP_STORE;
616 arm_spe__synth_data_source_common(record, &data_src);
618 arm_spe__synth_memory_level(record, &data_src);
620 if (record->type & (ARM_SPE_TLB_ACCESS | ARM_SPE_TLB_MISS)) {
621 data_src.mem_dtlb = PERF_MEM_TLB_WK;
623 if (record->type & ARM_SPE_TLB_MISS)
624 data_src.mem_dtlb |= PERF_MEM_TLB_MISS;
626 data_src.mem_dtlb |= PERF_MEM_TLB_HIT;
632 static int arm_spe_sample(struct arm_spe_queue *speq)
634 const struct arm_spe_record *record = &speq->decoder->record;
635 struct arm_spe *spe = speq->spe;
639 arm_spe__sample_flags(speq);
640 data_src = arm_spe__synth_data_source(speq, record);
642 if (spe->sample_flc) {
643 if (record->type & ARM_SPE_L1D_MISS) {
644 err = arm_spe__synth_mem_sample(speq, spe->l1d_miss_id,
650 if (record->type & ARM_SPE_L1D_ACCESS) {
651 err = arm_spe__synth_mem_sample(speq, spe->l1d_access_id,
658 if (spe->sample_llc) {
659 if (record->type & ARM_SPE_LLC_MISS) {
660 err = arm_spe__synth_mem_sample(speq, spe->llc_miss_id,
666 if (record->type & ARM_SPE_LLC_ACCESS) {
667 err = arm_spe__synth_mem_sample(speq, spe->llc_access_id,
674 if (spe->sample_tlb) {
675 if (record->type & ARM_SPE_TLB_MISS) {
676 err = arm_spe__synth_mem_sample(speq, spe->tlb_miss_id,
682 if (record->type & ARM_SPE_TLB_ACCESS) {
683 err = arm_spe__synth_mem_sample(speq, spe->tlb_access_id,
690 if (spe->sample_branch && (record->op & ARM_SPE_OP_BRANCH_ERET)) {
691 err = arm_spe__synth_branch_sample(speq, spe->branch_id);
696 if (spe->sample_remote_access &&
697 (record->type & ARM_SPE_REMOTE_ACCESS)) {
698 err = arm_spe__synth_mem_sample(speq, spe->remote_access_id,
705 * When data_src is zero it means the record is not a memory operation,
706 * skip to synthesize memory sample for this case.
708 if (spe->sample_memory && data_src) {
709 err = arm_spe__synth_mem_sample(speq, spe->memory_id, data_src);
714 if (spe->sample_instructions) {
715 err = arm_spe__synth_instruction_sample(speq, spe->instructions_id, data_src);
723 static int arm_spe_run_decoder(struct arm_spe_queue *speq, u64 *timestamp)
725 struct arm_spe *spe = speq->spe;
726 struct arm_spe_record *record;
729 if (!spe->kernel_start)
730 spe->kernel_start = machine__kernel_start(spe->machine);
734 * The usual logic is firstly to decode the packets, and then
735 * based the record to synthesize sample; but here the flow is
736 * reversed: it calls arm_spe_sample() for synthesizing samples
737 * prior to arm_spe_decode().
739 * Two reasons for this code logic:
740 * 1. Firstly, when setup queue in arm_spe__setup_queue(), it
741 * has decoded trace data and generated a record, but the record
742 * is left to generate sample until run to here, so it's correct
743 * to synthesize sample for the left record.
744 * 2. After decoding trace data, it needs to compare the record
745 * timestamp with the coming perf event, if the record timestamp
746 * is later than the perf event, it needs bail out and pushs the
747 * record into auxtrace heap, thus the record can be deferred to
748 * synthesize sample until run to here at the next time; so this
749 * can correlate samples between Arm SPE trace data and other
750 * perf events with correct time ordering.
754 * Update pid/tid info.
756 record = &speq->decoder->record;
757 if (!spe->timeless_decoding && record->context_id != (u64)-1) {
758 ret = arm_spe_set_tid(speq, record->context_id);
762 spe->use_ctx_pkt_for_pid = true;
765 ret = arm_spe_sample(speq);
769 ret = arm_spe_decode(speq->decoder);
771 pr_debug("No data or all data has been processed.\n");
776 * Error is detected when decode SPE trace data, continue to
777 * the next trace data and find out more records.
782 record = &speq->decoder->record;
784 /* Update timestamp for the last record */
785 if (record->timestamp > speq->timestamp)
786 speq->timestamp = record->timestamp;
789 * If the timestamp of the queue is later than timestamp of the
790 * coming perf event, bail out so can allow the perf event to
791 * be processed ahead.
793 if (!spe->timeless_decoding && speq->timestamp >= *timestamp) {
794 *timestamp = speq->timestamp;
802 static int arm_spe__setup_queue(struct arm_spe *spe,
803 struct auxtrace_queue *queue,
804 unsigned int queue_nr)
806 struct arm_spe_queue *speq = queue->priv;
807 struct arm_spe_record *record;
809 if (list_empty(&queue->head) || speq)
812 speq = arm_spe__alloc_queue(spe, queue_nr);
819 if (queue->cpu != -1)
820 speq->cpu = queue->cpu;
822 if (!speq->on_heap) {
825 if (spe->timeless_decoding)
829 ret = arm_spe_decode(speq->decoder);
837 record = &speq->decoder->record;
839 speq->timestamp = record->timestamp;
840 ret = auxtrace_heap__add(&spe->heap, queue_nr, speq->timestamp);
843 speq->on_heap = true;
849 static int arm_spe__setup_queues(struct arm_spe *spe)
854 for (i = 0; i < spe->queues.nr_queues; i++) {
855 ret = arm_spe__setup_queue(spe, &spe->queues.queue_array[i], i);
863 static int arm_spe__update_queues(struct arm_spe *spe)
865 if (spe->queues.new_data) {
866 spe->queues.new_data = false;
867 return arm_spe__setup_queues(spe);
873 static bool arm_spe__is_timeless_decoding(struct arm_spe *spe)
876 struct evlist *evlist = spe->session->evlist;
877 bool timeless_decoding = true;
880 * Circle through the list of event and complain if we find one
881 * with the time bit set.
883 evlist__for_each_entry(evlist, evsel) {
884 if ((evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
885 timeless_decoding = false;
888 return timeless_decoding;
891 static int arm_spe_process_queues(struct arm_spe *spe, u64 timestamp)
893 unsigned int queue_nr;
898 struct auxtrace_queue *queue;
899 struct arm_spe_queue *speq;
901 if (!spe->heap.heap_cnt)
904 if (spe->heap.heap_array[0].ordinal >= timestamp)
907 queue_nr = spe->heap.heap_array[0].queue_nr;
908 queue = &spe->queues.queue_array[queue_nr];
911 auxtrace_heap__pop(&spe->heap);
913 if (spe->heap.heap_cnt) {
914 ts = spe->heap.heap_array[0].ordinal + 1;
922 * A previous context-switch event has set pid/tid in the machine's context, so
923 * here we need to update the pid/tid in the thread and SPE queue.
925 if (!spe->use_ctx_pkt_for_pid)
926 arm_spe_set_pid_tid_cpu(spe, queue);
928 ret = arm_spe_run_decoder(speq, &ts);
930 auxtrace_heap__add(&spe->heap, queue_nr, ts);
935 ret = auxtrace_heap__add(&spe->heap, queue_nr, ts);
939 speq->on_heap = false;
946 static int arm_spe_process_timeless_queues(struct arm_spe *spe, pid_t tid,
949 struct auxtrace_queues *queues = &spe->queues;
953 for (i = 0; i < queues->nr_queues; i++) {
954 struct auxtrace_queue *queue = &spe->queues.queue_array[i];
955 struct arm_spe_queue *speq = queue->priv;
957 if (speq && (tid == -1 || speq->tid == tid)) {
959 arm_spe_set_pid_tid_cpu(spe, queue);
960 arm_spe_run_decoder(speq, &ts);
966 static int arm_spe_context_switch(struct arm_spe *spe, union perf_event *event,
967 struct perf_sample *sample)
972 if (!(event->header.misc & PERF_RECORD_MISC_SWITCH_OUT))
975 pid = event->context_switch.next_prev_pid;
976 tid = event->context_switch.next_prev_tid;
980 pr_warning("context_switch event has no tid\n");
982 return machine__set_current_tid(spe->machine, cpu, pid, tid);
985 static int arm_spe_process_event(struct perf_session *session,
986 union perf_event *event,
987 struct perf_sample *sample,
988 const struct perf_tool *tool)
992 struct arm_spe *spe = container_of(session->auxtrace,
993 struct arm_spe, auxtrace);
998 if (!tool->ordered_events) {
999 pr_err("SPE trace requires ordered events\n");
1003 if (sample->time && (sample->time != (u64) -1))
1004 timestamp = perf_time_to_tsc(sample->time, &spe->tc);
1008 if (timestamp || spe->timeless_decoding) {
1009 err = arm_spe__update_queues(spe);
1014 if (spe->timeless_decoding) {
1015 if (event->header.type == PERF_RECORD_EXIT) {
1016 err = arm_spe_process_timeless_queues(spe,
1020 } else if (timestamp) {
1021 err = arm_spe_process_queues(spe, timestamp);
1025 if (!spe->use_ctx_pkt_for_pid &&
1026 (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE ||
1027 event->header.type == PERF_RECORD_SWITCH))
1028 err = arm_spe_context_switch(spe, event, sample);
1034 static int arm_spe_process_auxtrace_event(struct perf_session *session,
1035 union perf_event *event,
1036 const struct perf_tool *tool __maybe_unused)
1038 struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
1041 if (!spe->data_queued) {
1042 struct auxtrace_buffer *buffer;
1044 int fd = perf_data__fd(session->data);
1047 if (perf_data__is_pipe(session->data)) {
1050 data_offset = lseek(fd, 0, SEEK_CUR);
1051 if (data_offset == -1)
1055 err = auxtrace_queues__add_event(&spe->queues, session, event,
1056 data_offset, &buffer);
1060 /* Dump here now we have copied a piped trace out of the pipe */
1062 if (auxtrace_buffer__get_data(buffer, fd)) {
1063 arm_spe_dump_event(spe, buffer->data,
1065 auxtrace_buffer__put_data(buffer);
1073 static int arm_spe_flush(struct perf_session *session __maybe_unused,
1074 const struct perf_tool *tool __maybe_unused)
1076 struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
1083 if (!tool->ordered_events)
1086 ret = arm_spe__update_queues(spe);
1090 if (spe->timeless_decoding)
1091 return arm_spe_process_timeless_queues(spe, -1,
1094 ret = arm_spe_process_queues(spe, MAX_TIMESTAMP);
1098 if (!spe->use_ctx_pkt_for_pid)
1099 ui__warning("Arm SPE CONTEXT packets not found in the traces.\n"
1100 "Matching of TIDs to SPE events could be inaccurate.\n");
1105 static u64 *arm_spe__alloc_per_cpu_metadata(u64 *buf, int per_cpu_size)
1109 metadata = zalloc(per_cpu_size);
1113 memcpy(metadata, buf, per_cpu_size);
1117 static void arm_spe__free_metadata(u64 **metadata, int nr_cpu)
1121 for (i = 0; i < nr_cpu; i++)
1122 zfree(&metadata[i]);
1126 static u64 **arm_spe__alloc_metadata(struct perf_record_auxtrace_info *info,
1127 u64 *ver, int *nr_cpu)
1129 u64 *ptr = (u64 *)info->priv;
1131 u64 **metadata = NULL;
1132 int hdr_sz, per_cpu_sz, i;
1134 metadata_size = info->header.size -
1135 sizeof(struct perf_record_auxtrace_info);
1137 /* Metadata version 1 */
1138 if (metadata_size == ARM_SPE_AUXTRACE_V1_PRIV_SIZE) {
1141 /* No per CPU metadata */
1145 *ver = ptr[ARM_SPE_HEADER_VERSION];
1146 hdr_sz = ptr[ARM_SPE_HEADER_SIZE];
1147 *nr_cpu = ptr[ARM_SPE_CPUS_NUM];
1149 metadata = calloc(*nr_cpu, sizeof(*metadata));
1153 /* Locate the start address of per CPU metadata */
1155 per_cpu_sz = (metadata_size - (hdr_sz * sizeof(u64))) / (*nr_cpu);
1157 for (i = 0; i < *nr_cpu; i++) {
1158 metadata[i] = arm_spe__alloc_per_cpu_metadata(ptr, per_cpu_sz);
1160 goto err_per_cpu_metadata;
1162 ptr += per_cpu_sz / sizeof(u64);
1167 err_per_cpu_metadata:
1168 arm_spe__free_metadata(metadata, *nr_cpu);
1172 static void arm_spe_free_queue(void *priv)
1174 struct arm_spe_queue *speq = priv;
1178 thread__zput(speq->thread);
1179 arm_spe_decoder_free(speq->decoder);
1180 zfree(&speq->event_buf);
1184 static void arm_spe_free_events(struct perf_session *session)
1186 struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
1188 struct auxtrace_queues *queues = &spe->queues;
1191 for (i = 0; i < queues->nr_queues; i++) {
1192 arm_spe_free_queue(queues->queue_array[i].priv);
1193 queues->queue_array[i].priv = NULL;
1195 auxtrace_queues__free(queues);
1198 static void arm_spe_free(struct perf_session *session)
1200 struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
1203 auxtrace_heap__free(&spe->heap);
1204 arm_spe_free_events(session);
1205 session->auxtrace = NULL;
1206 arm_spe__free_metadata(spe->metadata, spe->metadata_nr_cpu);
1210 static bool arm_spe_evsel_is_auxtrace(struct perf_session *session,
1211 struct evsel *evsel)
1213 struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe, auxtrace);
1215 return evsel->core.attr.type == spe->pmu_type;
1218 static const char * const metadata_hdr_v1_fmts[] = {
1219 [ARM_SPE_PMU_TYPE] = " PMU Type :%"PRId64"\n",
1220 [ARM_SPE_PER_CPU_MMAPS] = " Per CPU mmaps :%"PRId64"\n",
1223 static const char * const metadata_hdr_fmts[] = {
1224 [ARM_SPE_HEADER_VERSION] = " Header version :%"PRId64"\n",
1225 [ARM_SPE_HEADER_SIZE] = " Header size :%"PRId64"\n",
1226 [ARM_SPE_PMU_TYPE_V2] = " PMU type v2 :%"PRId64"\n",
1227 [ARM_SPE_CPUS_NUM] = " CPU number :%"PRId64"\n",
1230 static const char * const metadata_per_cpu_fmts[] = {
1231 [ARM_SPE_MAGIC] = " Magic :0x%"PRIx64"\n",
1232 [ARM_SPE_CPU] = " CPU # :%"PRId64"\n",
1233 [ARM_SPE_CPU_NR_PARAMS] = " Num of params :%"PRId64"\n",
1234 [ARM_SPE_CPU_MIDR] = " MIDR :0x%"PRIx64"\n",
1235 [ARM_SPE_CPU_PMU_TYPE] = " PMU Type :%"PRId64"\n",
1236 [ARM_SPE_CAP_MIN_IVAL] = " Min Interval :%"PRId64"\n",
1239 static void arm_spe_print_info(struct arm_spe *spe, __u64 *arr)
1241 unsigned int i, cpu, hdr_size, cpu_num, cpu_size;
1242 const char * const *hdr_fmts;
1247 if (spe->metadata_ver == 1) {
1249 hdr_size = ARM_SPE_AUXTRACE_V1_PRIV_MAX;
1250 hdr_fmts = metadata_hdr_v1_fmts;
1252 cpu_num = arr[ARM_SPE_CPUS_NUM];
1253 hdr_size = arr[ARM_SPE_HEADER_SIZE];
1254 hdr_fmts = metadata_hdr_fmts;
1257 for (i = 0; i < hdr_size; i++)
1258 fprintf(stdout, hdr_fmts[i], arr[i]);
1261 for (cpu = 0; cpu < cpu_num; cpu++) {
1263 * The parameters from ARM_SPE_MAGIC to ARM_SPE_CPU_NR_PARAMS
1264 * are fixed. The sequential parameter size is decided by the
1265 * field 'ARM_SPE_CPU_NR_PARAMS'.
1267 cpu_size = (ARM_SPE_CPU_NR_PARAMS + 1) + arr[ARM_SPE_CPU_NR_PARAMS];
1268 for (i = 0; i < cpu_size; i++)
1269 fprintf(stdout, metadata_per_cpu_fmts[i], arr[i]);
1274 static void arm_spe_set_event_name(struct evlist *evlist, u64 id,
1277 struct evsel *evsel;
1279 evlist__for_each_entry(evlist, evsel) {
1280 if (evsel->core.id && evsel->core.id[0] == id) {
1282 zfree(&evsel->name);
1283 evsel->name = strdup(name);
1290 arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
1292 struct evlist *evlist = session->evlist;
1293 struct evsel *evsel;
1294 struct perf_event_attr attr;
1299 evlist__for_each_entry(evlist, evsel) {
1300 if (evsel->core.attr.type == spe->pmu_type) {
1307 pr_debug("No selected events with SPE trace data\n");
1311 memset(&attr, 0, sizeof(struct perf_event_attr));
1312 attr.size = sizeof(struct perf_event_attr);
1313 attr.type = PERF_TYPE_HARDWARE;
1314 attr.sample_type = evsel->core.attr.sample_type &
1315 (PERF_SAMPLE_MASK | PERF_SAMPLE_PHYS_ADDR);
1316 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1317 PERF_SAMPLE_PERIOD | PERF_SAMPLE_DATA_SRC |
1318 PERF_SAMPLE_WEIGHT | PERF_SAMPLE_ADDR;
1319 if (spe->timeless_decoding)
1320 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1322 attr.sample_type |= PERF_SAMPLE_TIME;
1324 spe->sample_type = attr.sample_type;
1326 attr.exclude_user = evsel->core.attr.exclude_user;
1327 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
1328 attr.exclude_hv = evsel->core.attr.exclude_hv;
1329 attr.exclude_host = evsel->core.attr.exclude_host;
1330 attr.exclude_guest = evsel->core.attr.exclude_guest;
1331 attr.sample_id_all = evsel->core.attr.sample_id_all;
1332 attr.read_format = evsel->core.attr.read_format;
1334 /* create new id val to be a fixed offset from evsel id */
1335 id = evsel->core.id[0] + 1000000000;
1340 if (spe->synth_opts.flc) {
1341 spe->sample_flc = true;
1343 /* Level 1 data cache miss */
1344 err = perf_session__deliver_synth_attr_event(session, &attr, id);
1347 spe->l1d_miss_id = id;
1348 arm_spe_set_event_name(evlist, id, "l1d-miss");
1351 /* Level 1 data cache access */
1352 err = perf_session__deliver_synth_attr_event(session, &attr, id);
1355 spe->l1d_access_id = id;
1356 arm_spe_set_event_name(evlist, id, "l1d-access");
1360 if (spe->synth_opts.llc) {
1361 spe->sample_llc = true;
1363 /* Last level cache miss */
1364 err = perf_session__deliver_synth_attr_event(session, &attr, id);
1367 spe->llc_miss_id = id;
1368 arm_spe_set_event_name(evlist, id, "llc-miss");
1371 /* Last level cache access */
1372 err = perf_session__deliver_synth_attr_event(session, &attr, id);
1375 spe->llc_access_id = id;
1376 arm_spe_set_event_name(evlist, id, "llc-access");
1380 if (spe->synth_opts.tlb) {
1381 spe->sample_tlb = true;
1384 err = perf_session__deliver_synth_attr_event(session, &attr, id);
1387 spe->tlb_miss_id = id;
1388 arm_spe_set_event_name(evlist, id, "tlb-miss");
1392 err = perf_session__deliver_synth_attr_event(session, &attr, id);
1395 spe->tlb_access_id = id;
1396 arm_spe_set_event_name(evlist, id, "tlb-access");
1400 if (spe->synth_opts.branches) {
1401 spe->sample_branch = true;
1404 err = perf_session__deliver_synth_attr_event(session, &attr, id);
1407 spe->branch_id = id;
1408 arm_spe_set_event_name(evlist, id, "branch");
1412 if (spe->synth_opts.remote_access) {
1413 spe->sample_remote_access = true;
1416 err = perf_session__deliver_synth_attr_event(session, &attr, id);
1419 spe->remote_access_id = id;
1420 arm_spe_set_event_name(evlist, id, "remote-access");
1424 if (spe->synth_opts.mem) {
1425 spe->sample_memory = true;
1427 err = perf_session__deliver_synth_attr_event(session, &attr, id);
1430 spe->memory_id = id;
1431 arm_spe_set_event_name(evlist, id, "memory");
1435 if (spe->synth_opts.instructions) {
1436 if (spe->synth_opts.period_type != PERF_ITRACE_PERIOD_INSTRUCTIONS) {
1437 pr_warning("Only instruction-based sampling period is currently supported by Arm SPE.\n");
1438 goto synth_instructions_out;
1440 if (spe->synth_opts.period > 1)
1441 pr_warning("Arm SPE has a hardware-based sample period.\n"
1442 "Additional instruction events will be discarded by --itrace\n");
1444 spe->sample_instructions = true;
1445 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
1446 attr.sample_period = spe->synth_opts.period;
1447 spe->instructions_sample_period = attr.sample_period;
1448 err = perf_session__deliver_synth_attr_event(session, &attr, id);
1451 spe->instructions_id = id;
1452 arm_spe_set_event_name(evlist, id, "instructions");
1454 synth_instructions_out:
1459 static bool arm_spe__is_homogeneous(u64 **metadata, int nr_cpu)
1467 for (i = 0; i < nr_cpu; i++) {
1472 midr = metadata[i][ARM_SPE_CPU_MIDR];
1476 if (midr != metadata[i][ARM_SPE_CPU_MIDR])
1483 int arm_spe_process_auxtrace_info(union perf_event *event,
1484 struct perf_session *session)
1486 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
1487 size_t min_sz = ARM_SPE_AUXTRACE_V1_PRIV_SIZE;
1488 struct perf_record_time_conv *tc = &session->time_conv;
1489 struct arm_spe *spe;
1490 u64 **metadata = NULL;
1494 if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) +
1498 metadata = arm_spe__alloc_metadata(auxtrace_info, &metadata_ver,
1500 if (!metadata && metadata_ver != 1) {
1501 pr_err("Failed to parse Arm SPE metadata.\n");
1505 spe = zalloc(sizeof(struct arm_spe));
1508 goto err_free_metadata;
1511 err = auxtrace_queues__init(&spe->queues);
1515 spe->session = session;
1516 spe->machine = &session->machines.host; /* No kvm support */
1517 spe->auxtrace_type = auxtrace_info->type;
1518 if (metadata_ver == 1)
1519 spe->pmu_type = auxtrace_info->priv[ARM_SPE_PMU_TYPE];
1521 spe->pmu_type = auxtrace_info->priv[ARM_SPE_PMU_TYPE_V2];
1522 spe->metadata = metadata;
1523 spe->metadata_ver = metadata_ver;
1524 spe->metadata_nr_cpu = nr_cpu;
1525 spe->is_homogeneous = arm_spe__is_homogeneous(metadata, nr_cpu);
1527 spe->timeless_decoding = arm_spe__is_timeless_decoding(spe);
1530 * The synthesized event PERF_RECORD_TIME_CONV has been handled ahead
1531 * and the parameters for hardware clock are stored in the session
1532 * context. Passes these parameters to the struct perf_tsc_conversion
1533 * in "spe->tc", which is used for later conversion between clock
1534 * counter and timestamp.
1536 * For backward compatibility, copies the fields starting from
1537 * "time_cycles" only if they are contained in the event.
1539 spe->tc.time_shift = tc->time_shift;
1540 spe->tc.time_mult = tc->time_mult;
1541 spe->tc.time_zero = tc->time_zero;
1543 if (event_contains(*tc, time_cycles)) {
1544 spe->tc.time_cycles = tc->time_cycles;
1545 spe->tc.time_mask = tc->time_mask;
1546 spe->tc.cap_user_time_zero = tc->cap_user_time_zero;
1547 spe->tc.cap_user_time_short = tc->cap_user_time_short;
1550 spe->auxtrace.process_event = arm_spe_process_event;
1551 spe->auxtrace.process_auxtrace_event = arm_spe_process_auxtrace_event;
1552 spe->auxtrace.flush_events = arm_spe_flush;
1553 spe->auxtrace.free_events = arm_spe_free_events;
1554 spe->auxtrace.free = arm_spe_free;
1555 spe->auxtrace.evsel_is_auxtrace = arm_spe_evsel_is_auxtrace;
1556 session->auxtrace = &spe->auxtrace;
1558 arm_spe_print_info(spe, &auxtrace_info->priv[0]);
1563 if (session->itrace_synth_opts && session->itrace_synth_opts->set)
1564 spe->synth_opts = *session->itrace_synth_opts;
1566 itrace_synth_opts__set_default(&spe->synth_opts, false);
1568 err = arm_spe_synth_events(spe, session);
1570 goto err_free_queues;
1572 err = auxtrace_queues__process_index(&spe->queues, session);
1574 goto err_free_queues;
1576 if (spe->queues.populated)
1577 spe->data_queued = true;
1582 auxtrace_queues__free(&spe->queues);
1583 session->auxtrace = NULL;
1587 arm_spe__free_metadata(metadata, nr_cpu);