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"
30 #include "util/synthetic-events.h"
33 #include "arm-spe-decoder/arm-spe-decoder.h"
34 #include "arm-spe-decoder/arm-spe-pkt-decoder.h"
36 #define MAX_TIMESTAMP (~0ULL)
39 struct auxtrace auxtrace;
40 struct auxtrace_queues queues;
41 struct auxtrace_heap heap;
42 struct itrace_synth_opts synth_opts;
44 struct perf_session *session;
45 struct machine *machine;
56 u8 sample_remote_access;
69 unsigned long num_events;
72 struct arm_spe_queue {
74 unsigned int queue_nr;
75 struct auxtrace_buffer *buffer;
76 struct auxtrace_buffer *old_buffer;
77 union perf_event *event_buf;
83 struct arm_spe_decoder *decoder;
86 struct thread *thread;
89 static void arm_spe_dump(struct arm_spe *spe __maybe_unused,
90 unsigned char *buf, size_t len)
92 struct arm_spe_pkt packet;
95 char desc[ARM_SPE_PKT_DESC_MAX];
96 const char *color = PERF_COLOR_BLUE;
98 color_fprintf(stdout, color,
99 ". ... ARM SPE data: size %zu bytes\n",
103 ret = arm_spe_get_packet(buf, len, &packet);
109 color_fprintf(stdout, color, " %08x: ", pos);
110 for (i = 0; i < pkt_len; i++)
111 color_fprintf(stdout, color, " %02x", buf[i]);
113 color_fprintf(stdout, color, " ");
115 ret = arm_spe_pkt_desc(&packet, desc,
116 ARM_SPE_PKT_DESC_MAX);
118 color_fprintf(stdout, color, " %s\n", desc);
120 color_fprintf(stdout, color, " Bad packet!\n");
128 static void arm_spe_dump_event(struct arm_spe *spe, unsigned char *buf,
132 arm_spe_dump(spe, buf, len);
135 static int arm_spe_get_trace(struct arm_spe_buffer *b, void *data)
137 struct arm_spe_queue *speq = data;
138 struct auxtrace_buffer *buffer = speq->buffer;
139 struct auxtrace_buffer *old_buffer = speq->old_buffer;
140 struct auxtrace_queue *queue;
142 queue = &speq->spe->queues.queue_array[speq->queue_nr];
144 buffer = auxtrace_buffer__next(queue, buffer);
145 /* If no more data, drop the previous auxtrace_buffer and return */
148 auxtrace_buffer__drop_data(old_buffer);
153 speq->buffer = buffer;
155 /* If the aux_buffer doesn't have data associated, try to load it */
157 /* get the file desc associated with the perf data file */
158 int fd = perf_data__fd(speq->spe->session->data);
160 buffer->data = auxtrace_buffer__get_data(buffer, fd);
165 b->len = buffer->size;
166 b->buf = buffer->data;
170 auxtrace_buffer__drop_data(old_buffer);
171 speq->old_buffer = buffer;
173 auxtrace_buffer__drop_data(buffer);
174 return arm_spe_get_trace(b, data);
180 static struct arm_spe_queue *arm_spe__alloc_queue(struct arm_spe *spe,
181 unsigned int queue_nr)
183 struct arm_spe_params params = { .get_trace = 0, };
184 struct arm_spe_queue *speq;
186 speq = zalloc(sizeof(*speq));
190 speq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
191 if (!speq->event_buf)
195 speq->queue_nr = queue_nr;
201 params.get_trace = arm_spe_get_trace;
204 /* create new decoder */
205 speq->decoder = arm_spe_decoder_new(¶ms);
212 zfree(&speq->event_buf);
218 static inline u8 arm_spe_cpumode(struct arm_spe *spe, u64 ip)
220 return ip >= spe->kernel_start ?
221 PERF_RECORD_MISC_KERNEL :
222 PERF_RECORD_MISC_USER;
225 static void arm_spe_prep_sample(struct arm_spe *spe,
226 struct arm_spe_queue *speq,
227 union perf_event *event,
228 struct perf_sample *sample)
230 struct arm_spe_record *record = &speq->decoder->record;
232 if (!spe->timeless_decoding)
233 sample->time = speq->timestamp;
235 sample->ip = record->from_ip;
236 sample->cpumode = arm_spe_cpumode(spe, sample->ip);
237 sample->pid = speq->pid;
238 sample->tid = speq->tid;
239 sample->addr = record->to_ip;
241 sample->cpu = speq->cpu;
243 event->sample.header.type = PERF_RECORD_SAMPLE;
244 event->sample.header.misc = sample->cpumode;
245 event->sample.header.size = sizeof(struct perf_event_header);
248 static int arm_spe__inject_event(union perf_event *event, struct perf_sample *sample, u64 type)
250 event->header.size = perf_event__sample_event_size(sample, type, 0);
251 return perf_event__synthesize_sample(event, type, 0, sample);
255 arm_spe_deliver_synth_event(struct arm_spe *spe,
256 struct arm_spe_queue *speq __maybe_unused,
257 union perf_event *event,
258 struct perf_sample *sample)
262 if (spe->synth_opts.inject) {
263 ret = arm_spe__inject_event(event, sample, spe->sample_type);
268 ret = perf_session__deliver_synth_event(spe->session, event, sample);
270 pr_err("ARM SPE: failed to deliver event, error %d\n", ret);
276 arm_spe_synth_spe_events_sample(struct arm_spe_queue *speq,
279 struct arm_spe *spe = speq->spe;
280 union perf_event *event = speq->event_buf;
281 struct perf_sample sample = { .ip = 0, };
283 arm_spe_prep_sample(spe, speq, event, &sample);
285 sample.id = spe_events_id;
286 sample.stream_id = spe_events_id;
288 return arm_spe_deliver_synth_event(spe, speq, event, &sample);
291 static int arm_spe_sample(struct arm_spe_queue *speq)
293 const struct arm_spe_record *record = &speq->decoder->record;
294 struct arm_spe *spe = speq->spe;
297 if (spe->sample_flc) {
298 if (record->type & ARM_SPE_L1D_MISS) {
299 err = arm_spe_synth_spe_events_sample(
300 speq, spe->l1d_miss_id);
305 if (record->type & ARM_SPE_L1D_ACCESS) {
306 err = arm_spe_synth_spe_events_sample(
307 speq, spe->l1d_access_id);
313 if (spe->sample_llc) {
314 if (record->type & ARM_SPE_LLC_MISS) {
315 err = arm_spe_synth_spe_events_sample(
316 speq, spe->llc_miss_id);
321 if (record->type & ARM_SPE_LLC_ACCESS) {
322 err = arm_spe_synth_spe_events_sample(
323 speq, spe->llc_access_id);
329 if (spe->sample_tlb) {
330 if (record->type & ARM_SPE_TLB_MISS) {
331 err = arm_spe_synth_spe_events_sample(
332 speq, spe->tlb_miss_id);
337 if (record->type & ARM_SPE_TLB_ACCESS) {
338 err = arm_spe_synth_spe_events_sample(
339 speq, spe->tlb_access_id);
345 if (spe->sample_branch && (record->type & ARM_SPE_BRANCH_MISS)) {
346 err = arm_spe_synth_spe_events_sample(speq,
347 spe->branch_miss_id);
352 if (spe->sample_remote_access &&
353 (record->type & ARM_SPE_REMOTE_ACCESS)) {
354 err = arm_spe_synth_spe_events_sample(speq,
355 spe->remote_access_id);
363 static int arm_spe_run_decoder(struct arm_spe_queue *speq, u64 *timestamp)
365 struct arm_spe *spe = speq->spe;
368 if (!spe->kernel_start)
369 spe->kernel_start = machine__kernel_start(spe->machine);
372 ret = arm_spe_decode(speq->decoder);
374 pr_debug("No data or all data has been processed.\n");
379 * Error is detected when decode SPE trace data, continue to
380 * the next trace data and find out more records.
385 ret = arm_spe_sample(speq);
389 if (!spe->timeless_decoding && speq->timestamp >= *timestamp) {
390 *timestamp = speq->timestamp;
398 static int arm_spe__setup_queue(struct arm_spe *spe,
399 struct auxtrace_queue *queue,
400 unsigned int queue_nr)
402 struct arm_spe_queue *speq = queue->priv;
403 struct arm_spe_record *record;
405 if (list_empty(&queue->head) || speq)
408 speq = arm_spe__alloc_queue(spe, queue_nr);
415 if (queue->cpu != -1)
416 speq->cpu = queue->cpu;
418 if (!speq->on_heap) {
421 if (spe->timeless_decoding)
425 ret = arm_spe_decode(speq->decoder);
433 record = &speq->decoder->record;
435 speq->timestamp = record->timestamp;
436 ret = auxtrace_heap__add(&spe->heap, queue_nr, speq->timestamp);
439 speq->on_heap = true;
445 static int arm_spe__setup_queues(struct arm_spe *spe)
450 for (i = 0; i < spe->queues.nr_queues; i++) {
451 ret = arm_spe__setup_queue(spe, &spe->queues.queue_array[i], i);
459 static int arm_spe__update_queues(struct arm_spe *spe)
461 if (spe->queues.new_data) {
462 spe->queues.new_data = false;
463 return arm_spe__setup_queues(spe);
469 static bool arm_spe__is_timeless_decoding(struct arm_spe *spe)
472 struct evlist *evlist = spe->session->evlist;
473 bool timeless_decoding = true;
476 * Circle through the list of event and complain if we find one
477 * with the time bit set.
479 evlist__for_each_entry(evlist, evsel) {
480 if ((evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
481 timeless_decoding = false;
484 return timeless_decoding;
487 static void arm_spe_set_pid_tid_cpu(struct arm_spe *spe,
488 struct auxtrace_queue *queue)
490 struct arm_spe_queue *speq = queue->priv;
493 tid = machine__get_current_tid(spe->machine, speq->cpu);
496 thread__zput(speq->thread);
498 speq->tid = queue->tid;
500 if ((!speq->thread) && (speq->tid != -1)) {
501 speq->thread = machine__find_thread(spe->machine, -1,
506 speq->pid = speq->thread->pid_;
507 if (queue->cpu == -1)
508 speq->cpu = speq->thread->cpu;
512 static int arm_spe_process_queues(struct arm_spe *spe, u64 timestamp)
514 unsigned int queue_nr;
519 struct auxtrace_queue *queue;
520 struct arm_spe_queue *speq;
522 if (!spe->heap.heap_cnt)
525 if (spe->heap.heap_array[0].ordinal >= timestamp)
528 queue_nr = spe->heap.heap_array[0].queue_nr;
529 queue = &spe->queues.queue_array[queue_nr];
532 auxtrace_heap__pop(&spe->heap);
534 if (spe->heap.heap_cnt) {
535 ts = spe->heap.heap_array[0].ordinal + 1;
542 arm_spe_set_pid_tid_cpu(spe, queue);
544 ret = arm_spe_run_decoder(speq, &ts);
546 auxtrace_heap__add(&spe->heap, queue_nr, ts);
551 ret = auxtrace_heap__add(&spe->heap, queue_nr, ts);
555 speq->on_heap = false;
562 static int arm_spe_process_timeless_queues(struct arm_spe *spe, pid_t tid,
565 struct auxtrace_queues *queues = &spe->queues;
569 for (i = 0; i < queues->nr_queues; i++) {
570 struct auxtrace_queue *queue = &spe->queues.queue_array[i];
571 struct arm_spe_queue *speq = queue->priv;
573 if (speq && (tid == -1 || speq->tid == tid)) {
575 arm_spe_set_pid_tid_cpu(spe, queue);
576 arm_spe_run_decoder(speq, &ts);
582 static int arm_spe_process_event(struct perf_session *session,
583 union perf_event *event,
584 struct perf_sample *sample,
585 struct perf_tool *tool)
589 struct arm_spe *spe = container_of(session->auxtrace,
590 struct arm_spe, auxtrace);
595 if (!tool->ordered_events) {
596 pr_err("SPE trace requires ordered events\n");
600 if (sample->time && (sample->time != (u64) -1))
601 timestamp = sample->time;
605 if (timestamp || spe->timeless_decoding) {
606 err = arm_spe__update_queues(spe);
611 if (spe->timeless_decoding) {
612 if (event->header.type == PERF_RECORD_EXIT) {
613 err = arm_spe_process_timeless_queues(spe,
617 } else if (timestamp) {
618 if (event->header.type == PERF_RECORD_EXIT) {
619 err = arm_spe_process_queues(spe, timestamp);
628 static int arm_spe_process_auxtrace_event(struct perf_session *session,
629 union perf_event *event,
630 struct perf_tool *tool __maybe_unused)
632 struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
635 if (!spe->data_queued) {
636 struct auxtrace_buffer *buffer;
638 int fd = perf_data__fd(session->data);
641 if (perf_data__is_pipe(session->data)) {
644 data_offset = lseek(fd, 0, SEEK_CUR);
645 if (data_offset == -1)
649 err = auxtrace_queues__add_event(&spe->queues, session, event,
650 data_offset, &buffer);
654 /* Dump here now we have copied a piped trace out of the pipe */
656 if (auxtrace_buffer__get_data(buffer, fd)) {
657 arm_spe_dump_event(spe, buffer->data,
659 auxtrace_buffer__put_data(buffer);
667 static int arm_spe_flush(struct perf_session *session __maybe_unused,
668 struct perf_tool *tool __maybe_unused)
670 struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
677 if (!tool->ordered_events)
680 ret = arm_spe__update_queues(spe);
684 if (spe->timeless_decoding)
685 return arm_spe_process_timeless_queues(spe, -1,
688 return arm_spe_process_queues(spe, MAX_TIMESTAMP);
691 static void arm_spe_free_queue(void *priv)
693 struct arm_spe_queue *speq = priv;
697 thread__zput(speq->thread);
698 arm_spe_decoder_free(speq->decoder);
699 zfree(&speq->event_buf);
703 static void arm_spe_free_events(struct perf_session *session)
705 struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
707 struct auxtrace_queues *queues = &spe->queues;
710 for (i = 0; i < queues->nr_queues; i++) {
711 arm_spe_free_queue(queues->queue_array[i].priv);
712 queues->queue_array[i].priv = NULL;
714 auxtrace_queues__free(queues);
717 static void arm_spe_free(struct perf_session *session)
719 struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe,
722 auxtrace_heap__free(&spe->heap);
723 arm_spe_free_events(session);
724 session->auxtrace = NULL;
728 static bool arm_spe_evsel_is_auxtrace(struct perf_session *session,
731 struct arm_spe *spe = container_of(session->auxtrace, struct arm_spe, auxtrace);
733 return evsel->core.attr.type == spe->pmu_type;
736 static const char * const arm_spe_info_fmts[] = {
737 [ARM_SPE_PMU_TYPE] = " PMU Type %"PRId64"\n",
740 static void arm_spe_print_info(__u64 *arr)
745 fprintf(stdout, arm_spe_info_fmts[ARM_SPE_PMU_TYPE], arr[ARM_SPE_PMU_TYPE]);
748 struct arm_spe_synth {
749 struct perf_tool dummy_tool;
750 struct perf_session *session;
753 static int arm_spe_event_synth(struct perf_tool *tool,
754 union perf_event *event,
755 struct perf_sample *sample __maybe_unused,
756 struct machine *machine __maybe_unused)
758 struct arm_spe_synth *arm_spe_synth =
759 container_of(tool, struct arm_spe_synth, dummy_tool);
761 return perf_session__deliver_synth_event(arm_spe_synth->session,
765 static int arm_spe_synth_event(struct perf_session *session,
766 struct perf_event_attr *attr, u64 id)
768 struct arm_spe_synth arm_spe_synth;
770 memset(&arm_spe_synth, 0, sizeof(struct arm_spe_synth));
771 arm_spe_synth.session = session;
773 return perf_event__synthesize_attr(&arm_spe_synth.dummy_tool, attr, 1,
774 &id, arm_spe_event_synth);
777 static void arm_spe_set_event_name(struct evlist *evlist, u64 id,
782 evlist__for_each_entry(evlist, evsel) {
783 if (evsel->core.id && evsel->core.id[0] == id) {
786 evsel->name = strdup(name);
793 arm_spe_synth_events(struct arm_spe *spe, struct perf_session *session)
795 struct evlist *evlist = session->evlist;
797 struct perf_event_attr attr;
802 evlist__for_each_entry(evlist, evsel) {
803 if (evsel->core.attr.type == spe->pmu_type) {
810 pr_debug("No selected events with SPE trace data\n");
814 memset(&attr, 0, sizeof(struct perf_event_attr));
815 attr.size = sizeof(struct perf_event_attr);
816 attr.type = PERF_TYPE_HARDWARE;
817 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
818 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
820 if (spe->timeless_decoding)
821 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
823 attr.sample_type |= PERF_SAMPLE_TIME;
825 spe->sample_type = attr.sample_type;
827 attr.exclude_user = evsel->core.attr.exclude_user;
828 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
829 attr.exclude_hv = evsel->core.attr.exclude_hv;
830 attr.exclude_host = evsel->core.attr.exclude_host;
831 attr.exclude_guest = evsel->core.attr.exclude_guest;
832 attr.sample_id_all = evsel->core.attr.sample_id_all;
833 attr.read_format = evsel->core.attr.read_format;
835 /* create new id val to be a fixed offset from evsel id */
836 id = evsel->core.id[0] + 1000000000;
841 if (spe->synth_opts.flc) {
842 spe->sample_flc = true;
844 /* Level 1 data cache miss */
845 err = arm_spe_synth_event(session, &attr, id);
848 spe->l1d_miss_id = id;
849 arm_spe_set_event_name(evlist, id, "l1d-miss");
852 /* Level 1 data cache access */
853 err = arm_spe_synth_event(session, &attr, id);
856 spe->l1d_access_id = id;
857 arm_spe_set_event_name(evlist, id, "l1d-access");
861 if (spe->synth_opts.llc) {
862 spe->sample_llc = true;
864 /* Last level cache miss */
865 err = arm_spe_synth_event(session, &attr, id);
868 spe->llc_miss_id = id;
869 arm_spe_set_event_name(evlist, id, "llc-miss");
872 /* Last level cache access */
873 err = arm_spe_synth_event(session, &attr, id);
876 spe->llc_access_id = id;
877 arm_spe_set_event_name(evlist, id, "llc-access");
881 if (spe->synth_opts.tlb) {
882 spe->sample_tlb = true;
885 err = arm_spe_synth_event(session, &attr, id);
888 spe->tlb_miss_id = id;
889 arm_spe_set_event_name(evlist, id, "tlb-miss");
893 err = arm_spe_synth_event(session, &attr, id);
896 spe->tlb_access_id = id;
897 arm_spe_set_event_name(evlist, id, "tlb-access");
901 if (spe->synth_opts.branches) {
902 spe->sample_branch = true;
905 err = arm_spe_synth_event(session, &attr, id);
908 spe->branch_miss_id = id;
909 arm_spe_set_event_name(evlist, id, "branch-miss");
913 if (spe->synth_opts.remote_access) {
914 spe->sample_remote_access = true;
917 err = arm_spe_synth_event(session, &attr, id);
920 spe->remote_access_id = id;
921 arm_spe_set_event_name(evlist, id, "remote-access");
928 int arm_spe_process_auxtrace_info(union perf_event *event,
929 struct perf_session *session)
931 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
932 size_t min_sz = sizeof(u64) * ARM_SPE_AUXTRACE_PRIV_MAX;
936 if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) +
940 spe = zalloc(sizeof(struct arm_spe));
944 err = auxtrace_queues__init(&spe->queues);
948 spe->session = session;
949 spe->machine = &session->machines.host; /* No kvm support */
950 spe->auxtrace_type = auxtrace_info->type;
951 spe->pmu_type = auxtrace_info->priv[ARM_SPE_PMU_TYPE];
953 spe->timeless_decoding = arm_spe__is_timeless_decoding(spe);
954 spe->auxtrace.process_event = arm_spe_process_event;
955 spe->auxtrace.process_auxtrace_event = arm_spe_process_auxtrace_event;
956 spe->auxtrace.flush_events = arm_spe_flush;
957 spe->auxtrace.free_events = arm_spe_free_events;
958 spe->auxtrace.free = arm_spe_free;
959 spe->auxtrace.evsel_is_auxtrace = arm_spe_evsel_is_auxtrace;
960 session->auxtrace = &spe->auxtrace;
962 arm_spe_print_info(&auxtrace_info->priv[0]);
967 if (session->itrace_synth_opts && session->itrace_synth_opts->set)
968 spe->synth_opts = *session->itrace_synth_opts;
970 itrace_synth_opts__set_default(&spe->synth_opts, false);
972 err = arm_spe_synth_events(spe, session);
974 goto err_free_queues;
976 err = auxtrace_queues__process_index(&spe->queues, session);
978 goto err_free_queues;
980 if (spe->queues.populated)
981 spe->data_queued = true;
986 auxtrace_queues__free(&spe->queues);
987 session->auxtrace = NULL;