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;
51 struct perf_tsc_conversion tc;
61 u8 sample_remote_access;
63 u8 sample_instructions;
64 u64 instructions_sample_period;
79 unsigned long num_events;
80 u8 use_ctx_pkt_for_pid;
83 struct arm_spe_queue {
85 unsigned int queue_nr;
86 struct auxtrace_buffer *buffer;
87 struct auxtrace_buffer *old_buffer;
88 union perf_event *event_buf;
94 struct arm_spe_decoder *decoder;
97 struct thread *thread;
98 u64 period_instructions;
101 static void arm_spe_dump(struct arm_spe *spe __maybe_unused,
102 unsigned char *buf, size_t len)
104 struct arm_spe_pkt packet;
107 char desc[ARM_SPE_PKT_DESC_MAX];
108 const char *color = PERF_COLOR_BLUE;
110 color_fprintf(stdout, color,
111 ". ... ARM SPE data: size %#zx bytes\n",
115 ret = arm_spe_get_packet(buf, len, &packet);
121 color_fprintf(stdout, color, " %08x: ", pos);
122 for (i = 0; i < pkt_len; i++)
123 color_fprintf(stdout, color, " %02x", buf[i]);
125 color_fprintf(stdout, color, " ");
127 ret = arm_spe_pkt_desc(&packet, desc,
128 ARM_SPE_PKT_DESC_MAX);
130 color_fprintf(stdout, color, " %s\n", desc);
132 color_fprintf(stdout, color, " Bad packet!\n");
140 static void arm_spe_dump_event(struct arm_spe *spe, unsigned char *buf,
144 arm_spe_dump(spe, buf, len);
147 static int arm_spe_get_trace(struct arm_spe_buffer *b, void *data)
149 struct arm_spe_queue *speq = data;
150 struct auxtrace_buffer *buffer = speq->buffer;
151 struct auxtrace_buffer *old_buffer = speq->old_buffer;
152 struct auxtrace_queue *queue;
154 queue = &speq->spe->queues.queue_array[speq->queue_nr];
156 buffer = auxtrace_buffer__next(queue, buffer);
157 /* If no more data, drop the previous auxtrace_buffer and return */
160 auxtrace_buffer__drop_data(old_buffer);
165 speq->buffer = buffer;
167 /* If the aux_buffer doesn't have data associated, try to load it */
169 /* get the file desc associated with the perf data file */
170 int fd = perf_data__fd(speq->spe->session->data);
172 buffer->data = auxtrace_buffer__get_data(buffer, fd);
177 b->len = buffer->size;
178 b->buf = buffer->data;
182 auxtrace_buffer__drop_data(old_buffer);
183 speq->old_buffer = buffer;
185 auxtrace_buffer__drop_data(buffer);
186 return arm_spe_get_trace(b, data);
192 static struct arm_spe_queue *arm_spe__alloc_queue(struct arm_spe *spe,
193 unsigned int queue_nr)
195 struct arm_spe_params params = { .get_trace = 0, };
196 struct arm_spe_queue *speq;
198 speq = zalloc(sizeof(*speq));
202 speq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
203 if (!speq->event_buf)
207 speq->queue_nr = queue_nr;
211 speq->period_instructions = 0;
214 params.get_trace = arm_spe_get_trace;
217 /* create new decoder */
218 speq->decoder = arm_spe_decoder_new(¶ms);
225 zfree(&speq->event_buf);
231 static inline u8 arm_spe_cpumode(struct arm_spe *spe, u64 ip)
233 return ip >= spe->kernel_start ?
234 PERF_RECORD_MISC_KERNEL :
235 PERF_RECORD_MISC_USER;
238 static void arm_spe_set_pid_tid_cpu(struct arm_spe *spe,
239 struct auxtrace_queue *queue)
241 struct arm_spe_queue *speq = queue->priv;
244 tid = machine__get_current_tid(spe->machine, speq->cpu);
247 thread__zput(speq->thread);
249 speq->tid = queue->tid;
251 if ((!speq->thread) && (speq->tid != -1)) {
252 speq->thread = machine__find_thread(spe->machine, -1,
257 speq->pid = thread__pid(speq->thread);
258 if (queue->cpu == -1)
259 speq->cpu = thread__cpu(speq->thread);
263 static int arm_spe_set_tid(struct arm_spe_queue *speq, pid_t tid)
265 struct arm_spe *spe = speq->spe;
266 int err = machine__set_current_tid(spe->machine, speq->cpu, -1, tid);
271 arm_spe_set_pid_tid_cpu(spe, &spe->queues.queue_array[speq->queue_nr]);
276 static struct simd_flags arm_spe__synth_simd_flags(const struct arm_spe_record *record)
278 struct simd_flags simd_flags = {};
280 if ((record->op & ARM_SPE_OP_LDST) && (record->op & ARM_SPE_OP_SVE_LDST))
281 simd_flags.arch |= SIMD_OP_FLAGS_ARCH_SVE;
283 if ((record->op & ARM_SPE_OP_OTHER) && (record->op & ARM_SPE_OP_SVE_OTHER))
284 simd_flags.arch |= SIMD_OP_FLAGS_ARCH_SVE;
286 if (record->type & ARM_SPE_SVE_PARTIAL_PRED)
287 simd_flags.pred |= SIMD_OP_FLAGS_PRED_PARTIAL;
289 if (record->type & ARM_SPE_SVE_EMPTY_PRED)
290 simd_flags.pred |= SIMD_OP_FLAGS_PRED_EMPTY;
295 static void arm_spe_prep_sample(struct arm_spe *spe,
296 struct arm_spe_queue *speq,
297 union perf_event *event,
298 struct perf_sample *sample)
300 struct arm_spe_record *record = &speq->decoder->record;
302 if (!spe->timeless_decoding)
303 sample->time = tsc_to_perf_time(record->timestamp, &spe->tc);
305 sample->ip = record->from_ip;
306 sample->cpumode = arm_spe_cpumode(spe, sample->ip);
307 sample->pid = speq->pid;
308 sample->tid = speq->tid;
310 sample->cpu = speq->cpu;
311 sample->simd_flags = arm_spe__synth_simd_flags(record);
313 event->sample.header.type = PERF_RECORD_SAMPLE;
314 event->sample.header.misc = sample->cpumode;
315 event->sample.header.size = sizeof(struct perf_event_header);
318 static int arm_spe__inject_event(union perf_event *event, struct perf_sample *sample, u64 type)
320 event->header.size = perf_event__sample_event_size(sample, type, 0);
321 return perf_event__synthesize_sample(event, type, 0, sample);
325 arm_spe_deliver_synth_event(struct arm_spe *spe,
326 struct arm_spe_queue *speq __maybe_unused,
327 union perf_event *event,
328 struct perf_sample *sample)
332 if (spe->synth_opts.inject) {
333 ret = arm_spe__inject_event(event, sample, spe->sample_type);
338 ret = perf_session__deliver_synth_event(spe->session, event, sample);
340 pr_err("ARM SPE: failed to deliver event, error %d\n", ret);
345 static int arm_spe__synth_mem_sample(struct arm_spe_queue *speq,
346 u64 spe_events_id, u64 data_src)
348 struct arm_spe *spe = speq->spe;
349 struct arm_spe_record *record = &speq->decoder->record;
350 union perf_event *event = speq->event_buf;
351 struct perf_sample sample = { .ip = 0, };
353 arm_spe_prep_sample(spe, speq, event, &sample);
355 sample.id = spe_events_id;
356 sample.stream_id = spe_events_id;
357 sample.addr = record->virt_addr;
358 sample.phys_addr = record->phys_addr;
359 sample.data_src = data_src;
360 sample.weight = record->latency;
362 return arm_spe_deliver_synth_event(spe, speq, event, &sample);
365 static int arm_spe__synth_branch_sample(struct arm_spe_queue *speq,
368 struct arm_spe *spe = speq->spe;
369 struct arm_spe_record *record = &speq->decoder->record;
370 union perf_event *event = speq->event_buf;
371 struct perf_sample sample = { .ip = 0, };
373 arm_spe_prep_sample(spe, speq, event, &sample);
375 sample.id = spe_events_id;
376 sample.stream_id = spe_events_id;
377 sample.addr = record->to_ip;
378 sample.weight = record->latency;
380 return arm_spe_deliver_synth_event(spe, speq, event, &sample);
383 static int arm_spe__synth_instruction_sample(struct arm_spe_queue *speq,
384 u64 spe_events_id, u64 data_src)
386 struct arm_spe *spe = speq->spe;
387 struct arm_spe_record *record = &speq->decoder->record;
388 union perf_event *event = speq->event_buf;
389 struct perf_sample sample = { .ip = 0, };
392 * Handles perf instruction sampling period.
394 speq->period_instructions++;
395 if (speq->period_instructions < spe->instructions_sample_period)
397 speq->period_instructions = 0;
399 arm_spe_prep_sample(spe, speq, event, &sample);
401 sample.id = spe_events_id;
402 sample.stream_id = spe_events_id;
403 sample.addr = record->virt_addr;
404 sample.phys_addr = record->phys_addr;
405 sample.data_src = data_src;
406 sample.period = spe->instructions_sample_period;
407 sample.weight = record->latency;
409 return arm_spe_deliver_synth_event(spe, speq, event, &sample);
412 static const struct midr_range neoverse_spe[] = {
413 MIDR_ALL_VERSIONS(MIDR_NEOVERSE_N1),
414 MIDR_ALL_VERSIONS(MIDR_NEOVERSE_N2),
415 MIDR_ALL_VERSIONS(MIDR_NEOVERSE_V1),
419 static void arm_spe__synth_data_source_neoverse(const struct arm_spe_record *record,
420 union perf_mem_data_src *data_src)
423 * Even though four levels of cache hierarchy are possible, no known
424 * production Neoverse systems currently include more than three levels
425 * so for the time being we assume three exist. If a production system
426 * is built with four the this function would have to be changed to
427 * detect the number of levels for reporting.
431 * We have no data on the hit level or data source for stores in the
432 * Neoverse SPE records.
434 if (record->op & ARM_SPE_OP_ST) {
435 data_src->mem_lvl = PERF_MEM_LVL_NA;
436 data_src->mem_lvl_num = PERF_MEM_LVLNUM_NA;
437 data_src->mem_snoop = PERF_MEM_SNOOP_NA;
441 switch (record->source) {
443 data_src->mem_lvl = PERF_MEM_LVL_L1 | PERF_MEM_LVL_HIT;
444 data_src->mem_lvl_num = PERF_MEM_LVLNUM_L1;
445 data_src->mem_snoop = PERF_MEM_SNOOP_NONE;
448 data_src->mem_lvl = PERF_MEM_LVL_L2 | PERF_MEM_LVL_HIT;
449 data_src->mem_lvl_num = PERF_MEM_LVLNUM_L2;
450 data_src->mem_snoop = PERF_MEM_SNOOP_NONE;
452 case ARM_SPE_NV_PEER_CORE:
453 data_src->mem_lvl = PERF_MEM_LVL_L2 | PERF_MEM_LVL_HIT;
454 data_src->mem_lvl_num = PERF_MEM_LVLNUM_L2;
455 data_src->mem_snoopx = PERF_MEM_SNOOPX_PEER;
458 * We don't know if this is L1, L2 but we do know it was a cache-2-cache
459 * transfer, so set SNOOPX_PEER
461 case ARM_SPE_NV_LOCAL_CLUSTER:
462 case ARM_SPE_NV_PEER_CLUSTER:
463 data_src->mem_lvl = PERF_MEM_LVL_L3 | PERF_MEM_LVL_HIT;
464 data_src->mem_lvl_num = PERF_MEM_LVLNUM_L3;
465 data_src->mem_snoopx = PERF_MEM_SNOOPX_PEER;
468 * System cache is assumed to be L3
470 case ARM_SPE_NV_SYS_CACHE:
471 data_src->mem_lvl = PERF_MEM_LVL_L3 | PERF_MEM_LVL_HIT;
472 data_src->mem_lvl_num = PERF_MEM_LVLNUM_L3;
473 data_src->mem_snoop = PERF_MEM_SNOOP_HIT;
476 * We don't know what level it hit in, except it came from the other
479 case ARM_SPE_NV_REMOTE:
480 data_src->mem_lvl = PERF_MEM_LVL_REM_CCE1;
481 data_src->mem_lvl_num = PERF_MEM_LVLNUM_ANY_CACHE;
482 data_src->mem_remote = PERF_MEM_REMOTE_REMOTE;
483 data_src->mem_snoopx = PERF_MEM_SNOOPX_PEER;
485 case ARM_SPE_NV_DRAM:
486 data_src->mem_lvl = PERF_MEM_LVL_LOC_RAM | PERF_MEM_LVL_HIT;
487 data_src->mem_lvl_num = PERF_MEM_LVLNUM_RAM;
488 data_src->mem_snoop = PERF_MEM_SNOOP_NONE;
495 static void arm_spe__synth_data_source_generic(const struct arm_spe_record *record,
496 union perf_mem_data_src *data_src)
498 if (record->type & (ARM_SPE_LLC_ACCESS | ARM_SPE_LLC_MISS)) {
499 data_src->mem_lvl = PERF_MEM_LVL_L3;
501 if (record->type & ARM_SPE_LLC_MISS)
502 data_src->mem_lvl |= PERF_MEM_LVL_MISS;
504 data_src->mem_lvl |= PERF_MEM_LVL_HIT;
505 } else if (record->type & (ARM_SPE_L1D_ACCESS | ARM_SPE_L1D_MISS)) {
506 data_src->mem_lvl = PERF_MEM_LVL_L1;
508 if (record->type & ARM_SPE_L1D_MISS)
509 data_src->mem_lvl |= PERF_MEM_LVL_MISS;
511 data_src->mem_lvl |= PERF_MEM_LVL_HIT;
514 if (record->type & ARM_SPE_REMOTE_ACCESS)
515 data_src->mem_lvl |= PERF_MEM_LVL_REM_CCE1;
518 static u64 arm_spe__synth_data_source(const struct arm_spe_record *record, u64 midr)
520 union perf_mem_data_src data_src = { .mem_op = PERF_MEM_OP_NA };
521 bool is_neoverse = is_midr_in_range_list(midr, neoverse_spe);
523 if (record->op & ARM_SPE_OP_LD)
524 data_src.mem_op = PERF_MEM_OP_LOAD;
525 else if (record->op & ARM_SPE_OP_ST)
526 data_src.mem_op = PERF_MEM_OP_STORE;
531 arm_spe__synth_data_source_neoverse(record, &data_src);
533 arm_spe__synth_data_source_generic(record, &data_src);
535 if (record->type & (ARM_SPE_TLB_ACCESS | ARM_SPE_TLB_MISS)) {
536 data_src.mem_dtlb = PERF_MEM_TLB_WK;
538 if (record->type & ARM_SPE_TLB_MISS)
539 data_src.mem_dtlb |= PERF_MEM_TLB_MISS;
541 data_src.mem_dtlb |= PERF_MEM_TLB_HIT;
547 static int arm_spe_sample(struct arm_spe_queue *speq)
549 const struct arm_spe_record *record = &speq->decoder->record;
550 struct arm_spe *spe = speq->spe;
554 data_src = arm_spe__synth_data_source(record, spe->midr);
556 if (spe->sample_flc) {
557 if (record->type & ARM_SPE_L1D_MISS) {
558 err = arm_spe__synth_mem_sample(speq, spe->l1d_miss_id,
564 if (record->type & ARM_SPE_L1D_ACCESS) {
565 err = arm_spe__synth_mem_sample(speq, spe->l1d_access_id,
572 if (spe->sample_llc) {
573 if (record->type & ARM_SPE_LLC_MISS) {
574 err = arm_spe__synth_mem_sample(speq, spe->llc_miss_id,
580 if (record->type & ARM_SPE_LLC_ACCESS) {
581 err = arm_spe__synth_mem_sample(speq, spe->llc_access_id,
588 if (spe->sample_tlb) {
589 if (record->type & ARM_SPE_TLB_MISS) {
590 err = arm_spe__synth_mem_sample(speq, spe->tlb_miss_id,
596 if (record->type & ARM_SPE_TLB_ACCESS) {
597 err = arm_spe__synth_mem_sample(speq, spe->tlb_access_id,
604 if (spe->sample_branch && (record->type & ARM_SPE_BRANCH_MISS)) {
605 err = arm_spe__synth_branch_sample(speq, spe->branch_miss_id);
610 if (spe->sample_remote_access &&
611 (record->type & ARM_SPE_REMOTE_ACCESS)) {
612 err = arm_spe__synth_mem_sample(speq, spe->remote_access_id,
619 * When data_src is zero it means the record is not a memory operation,
620 * skip to synthesize memory sample for this case.
622 if (spe->sample_memory && data_src) {
623 err = arm_spe__synth_mem_sample(speq, spe->memory_id, data_src);
628 if (spe->sample_instructions) {
629 err = arm_spe__synth_instruction_sample(speq, spe->instructions_id, data_src);
637 static int arm_spe_run_decoder(struct arm_spe_queue *speq, u64 *timestamp)
639 struct arm_spe *spe = speq->spe;
640 struct arm_spe_record *record;
643 if (!spe->kernel_start)
644 spe->kernel_start = machine__kernel_start(spe->machine);
648 * The usual logic is firstly to decode the packets, and then
649 * based the record to synthesize sample; but here the flow is
650 * reversed: it calls arm_spe_sample() for synthesizing samples
651 * prior to arm_spe_decode().
653 * Two reasons for this code logic:
654 * 1. Firstly, when setup queue in arm_spe__setup_queue(), it
655 * has decoded trace data and generated a record, but the record
656 * is left to generate sample until run to here, so it's correct
657 * to synthesize sample for the left record.
658 * 2. After decoding trace data, it needs to compare the record
659 * timestamp with the coming perf event, if the record timestamp
660 * is later than the perf event, it needs bail out and pushs the
661 * record into auxtrace heap, thus the record can be deferred to
662 * synthesize sample until run to here at the next time; so this
663 * can correlate samples between Arm SPE trace data and other
664 * perf events with correct time ordering.
668 * Update pid/tid info.
670 record = &speq->decoder->record;
671 if (!spe->timeless_decoding && record->context_id != (u64)-1) {
672 ret = arm_spe_set_tid(speq, record->context_id);
676 spe->use_ctx_pkt_for_pid = true;
679 ret = arm_spe_sample(speq);
683 ret = arm_spe_decode(speq->decoder);
685 pr_debug("No data or all data has been processed.\n");
690 * Error is detected when decode SPE trace data, continue to
691 * the next trace data and find out more records.
696 record = &speq->decoder->record;
698 /* Update timestamp for the last record */
699 if (record->timestamp > speq->timestamp)
700 speq->timestamp = record->timestamp;
703 * If the timestamp of the queue is later than timestamp of the
704 * coming perf event, bail out so can allow the perf event to
705 * be processed ahead.
707 if (!spe->timeless_decoding && speq->timestamp >= *timestamp) {
708 *timestamp = speq->timestamp;
716 static int arm_spe__setup_queue(struct arm_spe *spe,
717 struct auxtrace_queue *queue,
718 unsigned int queue_nr)
720 struct arm_spe_queue *speq = queue->priv;
721 struct arm_spe_record *record;
723 if (list_empty(&queue->head) || speq)
726 speq = arm_spe__alloc_queue(spe, queue_nr);
733 if (queue->cpu != -1)
734 speq->cpu = queue->cpu;
736 if (!speq->on_heap) {
739 if (spe->timeless_decoding)
743 ret = arm_spe_decode(speq->decoder);
751 record = &speq->decoder->record;
753 speq->timestamp = record->timestamp;
754 ret = auxtrace_heap__add(&spe->heap, queue_nr, speq->timestamp);
757 speq->on_heap = true;
763 static int arm_spe__setup_queues(struct arm_spe *spe)
768 for (i = 0; i < spe->queues.nr_queues; i++) {
769 ret = arm_spe__setup_queue(spe, &spe->queues.queue_array[i], i);
777 static int arm_spe__update_queues(struct arm_spe *spe)
779 if (spe->queues.new_data) {
780 spe->queues.new_data = false;
781 return arm_spe__setup_queues(spe);
787 static bool arm_spe__is_timeless_decoding(struct arm_spe *spe)
790 struct evlist *evlist = spe->session->evlist;
791 bool timeless_decoding = true;
794 * Circle through the list of event and complain if we find one
795 * with the time bit set.
797 evlist__for_each_entry(evlist, evsel) {
798 if ((evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
799 timeless_decoding = false;
802 return timeless_decoding;
805 static int arm_spe_process_queues(struct arm_spe *spe, u64 timestamp)
807 unsigned int queue_nr;
812 struct auxtrace_queue *queue;
813 struct arm_spe_queue *speq;
815 if (!spe->heap.heap_cnt)
818 if (spe->heap.heap_array[0].ordinal >= timestamp)
821 queue_nr = spe->heap.heap_array[0].queue_nr;
822 queue = &spe->queues.queue_array[queue_nr];
825 auxtrace_heap__pop(&spe->heap);
827 if (spe->heap.heap_cnt) {
828 ts = spe->heap.heap_array[0].ordinal + 1;
836 * A previous context-switch event has set pid/tid in the machine's context, so
837 * here we need to update the pid/tid in the thread and SPE queue.
839 if (!spe->use_ctx_pkt_for_pid)
840 arm_spe_set_pid_tid_cpu(spe, queue);
842 ret = arm_spe_run_decoder(speq, &ts);
844 auxtrace_heap__add(&spe->heap, queue_nr, ts);
849 ret = auxtrace_heap__add(&spe->heap, queue_nr, ts);
853 speq->on_heap = false;
860 static int arm_spe_process_timeless_queues(struct arm_spe *spe, pid_t tid,
863 struct auxtrace_queues *queues = &spe->queues;
867 for (i = 0; i < queues->nr_queues; i++) {
868 struct auxtrace_queue *queue = &spe->queues.queue_array[i];
869 struct arm_spe_queue *speq = queue->priv;
871 if (speq && (tid == -1 || speq->tid == tid)) {
873 arm_spe_set_pid_tid_cpu(spe, queue);
874 arm_spe_run_decoder(speq, &ts);
880 static int arm_spe_context_switch(struct arm_spe *spe, union perf_event *event,
881 struct perf_sample *sample)
886 if (!(event->header.misc & PERF_RECORD_MISC_SWITCH_OUT))
889 pid = event->context_switch.next_prev_pid;
890 tid = event->context_switch.next_prev_tid;
894 pr_warning("context_switch event has no tid\n");
896 return machine__set_current_tid(spe->machine, cpu, pid, tid);
899 static int arm_spe_process_event(struct perf_session *session,
900 union perf_event *event,
901 struct perf_sample *sample,
902 struct perf_tool *tool)
906 struct arm_spe *spe = container_of(session->auxtrace,
907 struct arm_spe, auxtrace);
912 if (!tool->ordered_events) {
913 pr_err("SPE trace requires ordered events\n");
917 if (sample->time && (sample->time != (u64) -1))
918 timestamp = perf_time_to_tsc(sample->time, &spe->tc);
922 if (timestamp || spe->timeless_decoding) {
923 err = arm_spe__update_queues(spe);
928 if (spe->timeless_decoding) {
929 if (event->header.type == PERF_RECORD_EXIT) {
930 err = arm_spe_process_timeless_queues(spe,
934 } else if (timestamp) {
935 err = arm_spe_process_queues(spe, timestamp);
939 if (!spe->use_ctx_pkt_for_pid &&
940 (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE ||
941 event->header.type == PERF_RECORD_SWITCH))
942 err = arm_spe_context_switch(spe, event, sample);
948 static int arm_spe_process_auxtrace_event(struct perf_session *session,
949 union perf_event *event,
950 struct perf_tool *tool __maybe_unused)
952 struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
955 if (!spe->data_queued) {
956 struct auxtrace_buffer *buffer;
958 int fd = perf_data__fd(session->data);
961 if (perf_data__is_pipe(session->data)) {
964 data_offset = lseek(fd, 0, SEEK_CUR);
965 if (data_offset == -1)
969 err = auxtrace_queues__add_event(&spe->queues, session, event,
970 data_offset, &buffer);
974 /* Dump here now we have copied a piped trace out of the pipe */
976 if (auxtrace_buffer__get_data(buffer, fd)) {
977 arm_spe_dump_event(spe, buffer->data,
979 auxtrace_buffer__put_data(buffer);
987 static int arm_spe_flush(struct perf_session *session __maybe_unused,
988 struct perf_tool *tool __maybe_unused)
990 struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
997 if (!tool->ordered_events)
1000 ret = arm_spe__update_queues(spe);
1004 if (spe->timeless_decoding)
1005 return arm_spe_process_timeless_queues(spe, -1,
1008 ret = arm_spe_process_queues(spe, MAX_TIMESTAMP);
1012 if (!spe->use_ctx_pkt_for_pid)
1013 ui__warning("Arm SPE CONTEXT packets not found in the traces.\n"
1014 "Matching of TIDs to SPE events could be inaccurate.\n");
1019 static void arm_spe_free_queue(void *priv)
1021 struct arm_spe_queue *speq = priv;
1025 thread__zput(speq->thread);
1026 arm_spe_decoder_free(speq->decoder);
1027 zfree(&speq->event_buf);
1031 static void arm_spe_free_events(struct perf_session *session)
1033 struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
1035 struct auxtrace_queues *queues = &spe->queues;
1038 for (i = 0; i < queues->nr_queues; i++) {
1039 arm_spe_free_queue(queues->queue_array[i].priv);
1040 queues->queue_array[i].priv = NULL;
1042 auxtrace_queues__free(queues);
1045 static void arm_spe_free(struct perf_session *session)
1047 struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
1050 auxtrace_heap__free(&spe->heap);
1051 arm_spe_free_events(session);
1052 session->auxtrace = NULL;
1056 static bool arm_spe_evsel_is_auxtrace(struct perf_session *session,
1057 struct evsel *evsel)
1059 struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe, auxtrace);
1061 return evsel->core.attr.type == spe->pmu_type;
1064 static const char * const arm_spe_info_fmts[] = {
1065 [ARM_SPE_PMU_TYPE] = " PMU Type %"PRId64"\n",
1068 static void arm_spe_print_info(__u64 *arr)
1073 fprintf(stdout, arm_spe_info_fmts[ARM_SPE_PMU_TYPE], arr[ARM_SPE_PMU_TYPE]);
1076 struct arm_spe_synth {
1077 struct perf_tool dummy_tool;
1078 struct perf_session *session;
1081 static int arm_spe_event_synth(struct perf_tool *tool,
1082 union perf_event *event,
1083 struct perf_sample *sample __maybe_unused,
1084 struct machine *machine __maybe_unused)
1086 struct arm_spe_synth *arm_spe_synth =
1087 container_of(tool, struct arm_spe_synth, dummy_tool);
1089 return perf_session__deliver_synth_event(arm_spe_synth->session,
1093 static int arm_spe_synth_event(struct perf_session *session,
1094 struct perf_event_attr *attr, u64 id)
1096 struct arm_spe_synth arm_spe_synth;
1098 memset(&arm_spe_synth, 0, sizeof(struct arm_spe_synth));
1099 arm_spe_synth.session = session;
1101 return perf_event__synthesize_attr(&arm_spe_synth.dummy_tool, attr, 1,
1102 &id, arm_spe_event_synth);
1105 static void arm_spe_set_event_name(struct evlist *evlist, u64 id,
1108 struct evsel *evsel;
1110 evlist__for_each_entry(evlist, evsel) {
1111 if (evsel->core.id && evsel->core.id[0] == id) {
1113 zfree(&evsel->name);
1114 evsel->name = strdup(name);
1121 arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
1123 struct evlist *evlist = session->evlist;
1124 struct evsel *evsel;
1125 struct perf_event_attr attr;
1130 evlist__for_each_entry(evlist, evsel) {
1131 if (evsel->core.attr.type == spe->pmu_type) {
1138 pr_debug("No selected events with SPE trace data\n");
1142 memset(&attr, 0, sizeof(struct perf_event_attr));
1143 attr.size = sizeof(struct perf_event_attr);
1144 attr.type = PERF_TYPE_HARDWARE;
1145 attr.sample_type = evsel->core.attr.sample_type &
1146 (PERF_SAMPLE_MASK | PERF_SAMPLE_PHYS_ADDR);
1147 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1148 PERF_SAMPLE_PERIOD | PERF_SAMPLE_DATA_SRC |
1149 PERF_SAMPLE_WEIGHT | PERF_SAMPLE_ADDR;
1150 if (spe->timeless_decoding)
1151 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1153 attr.sample_type |= PERF_SAMPLE_TIME;
1155 spe->sample_type = attr.sample_type;
1157 attr.exclude_user = evsel->core.attr.exclude_user;
1158 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
1159 attr.exclude_hv = evsel->core.attr.exclude_hv;
1160 attr.exclude_host = evsel->core.attr.exclude_host;
1161 attr.exclude_guest = evsel->core.attr.exclude_guest;
1162 attr.sample_id_all = evsel->core.attr.sample_id_all;
1163 attr.read_format = evsel->core.attr.read_format;
1165 /* create new id val to be a fixed offset from evsel id */
1166 id = evsel->core.id[0] + 1000000000;
1171 if (spe->synth_opts.flc) {
1172 spe->sample_flc = true;
1174 /* Level 1 data cache miss */
1175 err = arm_spe_synth_event(session, &attr, id);
1178 spe->l1d_miss_id = id;
1179 arm_spe_set_event_name(evlist, id, "l1d-miss");
1182 /* Level 1 data cache access */
1183 err = arm_spe_synth_event(session, &attr, id);
1186 spe->l1d_access_id = id;
1187 arm_spe_set_event_name(evlist, id, "l1d-access");
1191 if (spe->synth_opts.llc) {
1192 spe->sample_llc = true;
1194 /* Last level cache miss */
1195 err = arm_spe_synth_event(session, &attr, id);
1198 spe->llc_miss_id = id;
1199 arm_spe_set_event_name(evlist, id, "llc-miss");
1202 /* Last level cache access */
1203 err = arm_spe_synth_event(session, &attr, id);
1206 spe->llc_access_id = id;
1207 arm_spe_set_event_name(evlist, id, "llc-access");
1211 if (spe->synth_opts.tlb) {
1212 spe->sample_tlb = true;
1215 err = arm_spe_synth_event(session, &attr, id);
1218 spe->tlb_miss_id = id;
1219 arm_spe_set_event_name(evlist, id, "tlb-miss");
1223 err = arm_spe_synth_event(session, &attr, id);
1226 spe->tlb_access_id = id;
1227 arm_spe_set_event_name(evlist, id, "tlb-access");
1231 if (spe->synth_opts.branches) {
1232 spe->sample_branch = true;
1235 err = arm_spe_synth_event(session, &attr, id);
1238 spe->branch_miss_id = id;
1239 arm_spe_set_event_name(evlist, id, "branch-miss");
1243 if (spe->synth_opts.remote_access) {
1244 spe->sample_remote_access = true;
1247 err = arm_spe_synth_event(session, &attr, id);
1250 spe->remote_access_id = id;
1251 arm_spe_set_event_name(evlist, id, "remote-access");
1255 if (spe->synth_opts.mem) {
1256 spe->sample_memory = true;
1258 err = arm_spe_synth_event(session, &attr, id);
1261 spe->memory_id = id;
1262 arm_spe_set_event_name(evlist, id, "memory");
1266 if (spe->synth_opts.instructions) {
1267 if (spe->synth_opts.period_type != PERF_ITRACE_PERIOD_INSTRUCTIONS) {
1268 pr_warning("Only instruction-based sampling period is currently supported by Arm SPE.\n");
1269 goto synth_instructions_out;
1271 if (spe->synth_opts.period > 1)
1272 pr_warning("Arm SPE has a hardware-based sample period.\n"
1273 "Additional instruction events will be discarded by --itrace\n");
1275 spe->sample_instructions = true;
1276 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
1277 attr.sample_period = spe->synth_opts.period;
1278 spe->instructions_sample_period = attr.sample_period;
1279 err = arm_spe_synth_event(session, &attr, id);
1282 spe->instructions_id = id;
1283 arm_spe_set_event_name(evlist, id, "instructions");
1285 synth_instructions_out:
1290 int arm_spe_process_auxtrace_info(union perf_event *event,
1291 struct perf_session *session)
1293 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
1294 size_t min_sz = sizeof(u64) * ARM_SPE_AUXTRACE_PRIV_MAX;
1295 struct perf_record_time_conv *tc = &session->time_conv;
1296 const char *cpuid = perf_env__cpuid(session->evlist->env);
1297 u64 midr = strtol(cpuid, NULL, 16);
1298 struct arm_spe *spe;
1301 if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) +
1305 spe = zalloc(sizeof(struct arm_spe));
1309 err = auxtrace_queues__init(&spe->queues);
1313 spe->session = session;
1314 spe->machine = &session->machines.host; /* No kvm support */
1315 spe->auxtrace_type = auxtrace_info->type;
1316 spe->pmu_type = auxtrace_info->priv[ARM_SPE_PMU_TYPE];
1319 spe->timeless_decoding = arm_spe__is_timeless_decoding(spe);
1322 * The synthesized event PERF_RECORD_TIME_CONV has been handled ahead
1323 * and the parameters for hardware clock are stored in the session
1324 * context. Passes these parameters to the struct perf_tsc_conversion
1325 * in "spe->tc", which is used for later conversion between clock
1326 * counter and timestamp.
1328 * For backward compatibility, copies the fields starting from
1329 * "time_cycles" only if they are contained in the event.
1331 spe->tc.time_shift = tc->time_shift;
1332 spe->tc.time_mult = tc->time_mult;
1333 spe->tc.time_zero = tc->time_zero;
1335 if (event_contains(*tc, time_cycles)) {
1336 spe->tc.time_cycles = tc->time_cycles;
1337 spe->tc.time_mask = tc->time_mask;
1338 spe->tc.cap_user_time_zero = tc->cap_user_time_zero;
1339 spe->tc.cap_user_time_short = tc->cap_user_time_short;
1342 spe->auxtrace.process_event = arm_spe_process_event;
1343 spe->auxtrace.process_auxtrace_event = arm_spe_process_auxtrace_event;
1344 spe->auxtrace.flush_events = arm_spe_flush;
1345 spe->auxtrace.free_events = arm_spe_free_events;
1346 spe->auxtrace.free = arm_spe_free;
1347 spe->auxtrace.evsel_is_auxtrace = arm_spe_evsel_is_auxtrace;
1348 session->auxtrace = &spe->auxtrace;
1350 arm_spe_print_info(&auxtrace_info->priv[0]);
1355 if (session->itrace_synth_opts && session->itrace_synth_opts->set)
1356 spe->synth_opts = *session->itrace_synth_opts;
1358 itrace_synth_opts__set_default(&spe->synth_opts, false);
1360 err = arm_spe_synth_events(spe, session);
1362 goto err_free_queues;
1364 err = auxtrace_queues__process_index(&spe->queues, session);
1366 goto err_free_queues;
1368 if (spe->queues.populated)
1369 spe->data_queued = true;
1374 auxtrace_queues__free(&spe->queues);
1375 session->auxtrace = NULL;