1 // SPDX-License-Identifier: GPL-2.0-only
3 * intel-bts.c: Intel Processor Trace support
4 * Copyright (c) 2013-2015, Intel Corporation.
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/bitops.h>
14 #include <linux/log2.h>
15 #include <linux/zalloc.h>
25 #include "thread-stack.h"
29 #include "intel-pt-decoder/intel-pt-insn-decoder.h"
30 #include "intel-bts.h"
31 #include "util/synthetic-events.h"
33 #define MAX_TIMESTAMP (~0ULL)
35 #define INTEL_BTS_ERR_NOINSN 5
36 #define INTEL_BTS_ERR_LOST 9
38 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
39 #define le64_to_cpu bswap_64
45 struct auxtrace auxtrace;
46 struct auxtrace_queues queues;
47 struct auxtrace_heap heap;
49 struct perf_session *session;
50 struct machine *machine;
55 struct perf_tsc_conversion tc;
56 bool cap_user_time_zero;
57 struct itrace_synth_opts synth_opts;
60 u64 branches_sample_type;
62 size_t branches_event_size;
63 unsigned long num_events;
66 struct intel_bts_queue {
67 struct intel_bts *bts;
68 unsigned int queue_nr;
69 struct auxtrace_buffer *buffer;
76 struct intel_pt_insn intel_pt_insn;
86 static void intel_bts_dump(struct intel_bts *bts __maybe_unused,
87 unsigned char *buf, size_t len)
89 struct branch *branch;
90 size_t i, pos = 0, br_sz = sizeof(struct branch), sz;
91 const char *color = PERF_COLOR_BLUE;
93 color_fprintf(stdout, color,
94 ". ... Intel BTS data: size %zu bytes\n",
103 color_fprintf(stdout, color, " %08x: ", pos);
104 for (i = 0; i < sz; i++)
105 color_fprintf(stdout, color, " %02x", buf[i]);
106 for (; i < br_sz; i++)
107 color_fprintf(stdout, color, " ");
109 branch = (struct branch *)buf;
110 color_fprintf(stdout, color, " %"PRIx64" -> %"PRIx64" %s\n",
111 le64_to_cpu(branch->from),
112 le64_to_cpu(branch->to),
113 le64_to_cpu(branch->misc) & 0x10 ?
116 color_fprintf(stdout, color, " Bad record!\n");
124 static void intel_bts_dump_event(struct intel_bts *bts, unsigned char *buf,
128 intel_bts_dump(bts, buf, len);
131 static int intel_bts_lost(struct intel_bts *bts, struct perf_sample *sample)
133 union perf_event event;
136 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
137 INTEL_BTS_ERR_LOST, sample->cpu, sample->pid,
138 sample->tid, 0, "Lost trace data", sample->time);
140 err = perf_session__deliver_synth_event(bts->session, &event, NULL);
142 pr_err("Intel BTS: failed to deliver error event, error %d\n",
148 static struct intel_bts_queue *intel_bts_alloc_queue(struct intel_bts *bts,
149 unsigned int queue_nr)
151 struct intel_bts_queue *btsq;
153 btsq = zalloc(sizeof(struct intel_bts_queue));
158 btsq->queue_nr = queue_nr;
166 static int intel_bts_setup_queue(struct intel_bts *bts,
167 struct auxtrace_queue *queue,
168 unsigned int queue_nr)
170 struct intel_bts_queue *btsq = queue->priv;
172 if (list_empty(&queue->head))
176 btsq = intel_bts_alloc_queue(bts, queue_nr);
181 if (queue->cpu != -1)
182 btsq->cpu = queue->cpu;
183 btsq->tid = queue->tid;
186 if (bts->sampling_mode)
189 if (!btsq->on_heap && !btsq->buffer) {
192 btsq->buffer = auxtrace_buffer__next(queue, NULL);
196 ret = auxtrace_heap__add(&bts->heap, queue_nr,
197 btsq->buffer->reference);
200 btsq->on_heap = true;
206 static int intel_bts_setup_queues(struct intel_bts *bts)
211 for (i = 0; i < bts->queues.nr_queues; i++) {
212 ret = intel_bts_setup_queue(bts, &bts->queues.queue_array[i],
220 static inline int intel_bts_update_queues(struct intel_bts *bts)
222 if (bts->queues.new_data) {
223 bts->queues.new_data = false;
224 return intel_bts_setup_queues(bts);
229 static unsigned char *intel_bts_find_overlap(unsigned char *buf_a, size_t len_a,
230 unsigned char *buf_b, size_t len_b)
235 offs = len_a - len_b;
239 for (; offs < len_a; offs += sizeof(struct branch)) {
241 if (!memcmp(buf_a + offs, buf_b, len))
248 static int intel_bts_do_fix_overlap(struct auxtrace_queue *queue,
249 struct auxtrace_buffer *b)
251 struct auxtrace_buffer *a;
254 if (b->list.prev == &queue->head)
256 a = list_entry(b->list.prev, struct auxtrace_buffer, list);
257 start = intel_bts_find_overlap(a->data, a->size, b->data, b->size);
260 b->use_size = b->data + b->size - start;
265 static inline u8 intel_bts_cpumode(struct intel_bts *bts, uint64_t ip)
267 return machine__kernel_ip(bts->machine, ip) ?
268 PERF_RECORD_MISC_KERNEL :
269 PERF_RECORD_MISC_USER;
272 static int intel_bts_synth_branch_sample(struct intel_bts_queue *btsq,
273 struct branch *branch)
276 struct intel_bts *bts = btsq->bts;
277 union perf_event event;
278 struct perf_sample sample = { .ip = 0, };
280 if (bts->synth_opts.initial_skip &&
281 bts->num_events++ <= bts->synth_opts.initial_skip)
284 sample.ip = le64_to_cpu(branch->from);
285 sample.cpumode = intel_bts_cpumode(bts, sample.ip);
286 sample.pid = btsq->pid;
287 sample.tid = btsq->tid;
288 sample.addr = le64_to_cpu(branch->to);
289 sample.id = btsq->bts->branches_id;
290 sample.stream_id = btsq->bts->branches_id;
292 sample.cpu = btsq->cpu;
293 sample.flags = btsq->sample_flags;
294 sample.insn_len = btsq->intel_pt_insn.length;
295 memcpy(sample.insn, btsq->intel_pt_insn.buf, INTEL_PT_INSN_BUF_SZ);
297 event.sample.header.type = PERF_RECORD_SAMPLE;
298 event.sample.header.misc = sample.cpumode;
299 event.sample.header.size = sizeof(struct perf_event_header);
301 if (bts->synth_opts.inject) {
302 event.sample.header.size = bts->branches_event_size;
303 ret = perf_event__synthesize_sample(&event,
304 bts->branches_sample_type,
310 ret = perf_session__deliver_synth_event(bts->session, &event, &sample);
312 pr_err("Intel BTS: failed to deliver branch event, error %d\n",
318 static int intel_bts_get_next_insn(struct intel_bts_queue *btsq, u64 ip)
320 struct machine *machine = btsq->bts->machine;
321 struct thread *thread;
322 unsigned char buf[INTEL_PT_INSN_BUF_SZ];
327 thread = machine__find_thread(machine, -1, btsq->tid);
331 len = thread__memcpy(thread, machine, buf, ip, INTEL_PT_INSN_BUF_SZ, &x86_64);
335 if (intel_pt_get_insn(buf, len, x86_64, &btsq->intel_pt_insn))
344 static int intel_bts_synth_error(struct intel_bts *bts, int cpu, pid_t pid,
347 union perf_event event;
350 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
351 INTEL_BTS_ERR_NOINSN, cpu, pid, tid, ip,
352 "Failed to get instruction", 0);
354 err = perf_session__deliver_synth_event(bts->session, &event, NULL);
356 pr_err("Intel BTS: failed to deliver error event, error %d\n",
362 static int intel_bts_get_branch_type(struct intel_bts_queue *btsq,
363 struct branch *branch)
369 btsq->sample_flags = PERF_IP_FLAG_BRANCH |
370 PERF_IP_FLAG_TRACE_BEGIN;
372 btsq->sample_flags = 0;
373 btsq->intel_pt_insn.length = 0;
374 } else if (!branch->to) {
375 btsq->sample_flags = PERF_IP_FLAG_BRANCH |
376 PERF_IP_FLAG_TRACE_END;
377 btsq->intel_pt_insn.length = 0;
379 err = intel_bts_get_next_insn(btsq, branch->from);
381 btsq->sample_flags = 0;
382 btsq->intel_pt_insn.length = 0;
383 if (!btsq->bts->synth_opts.errors)
385 err = intel_bts_synth_error(btsq->bts, btsq->cpu,
386 btsq->pid, btsq->tid,
390 btsq->sample_flags = intel_pt_insn_type(btsq->intel_pt_insn.op);
391 /* Check for an async branch into the kernel */
392 if (!machine__kernel_ip(btsq->bts->machine, branch->from) &&
393 machine__kernel_ip(btsq->bts->machine, branch->to) &&
394 btsq->sample_flags != (PERF_IP_FLAG_BRANCH |
396 PERF_IP_FLAG_SYSCALLRET))
397 btsq->sample_flags = PERF_IP_FLAG_BRANCH |
400 PERF_IP_FLAG_INTERRUPT;
406 static int intel_bts_process_buffer(struct intel_bts_queue *btsq,
407 struct auxtrace_buffer *buffer,
408 struct thread *thread)
410 struct branch *branch;
411 size_t sz, bsz = sizeof(struct branch);
412 u32 filter = btsq->bts->branches_filter;
415 if (buffer->use_data) {
416 sz = buffer->use_size;
417 branch = buffer->use_data;
420 branch = buffer->data;
423 if (!btsq->bts->sample_branches)
426 for (; sz > bsz; branch += 1, sz -= bsz) {
427 if (!branch->from && !branch->to)
429 intel_bts_get_branch_type(btsq, branch);
430 if (btsq->bts->synth_opts.thread_stack)
431 thread_stack__event(thread, btsq->cpu, btsq->sample_flags,
432 le64_to_cpu(branch->from),
433 le64_to_cpu(branch->to),
434 btsq->intel_pt_insn.length,
435 buffer->buffer_nr + 1, true, 0, 0);
436 if (filter && !(filter & btsq->sample_flags))
438 err = intel_bts_synth_branch_sample(btsq, branch);
445 static int intel_bts_process_queue(struct intel_bts_queue *btsq, u64 *timestamp)
447 struct auxtrace_buffer *buffer = btsq->buffer, *old_buffer = buffer;
448 struct auxtrace_queue *queue;
449 struct thread *thread;
455 if (btsq->pid == -1) {
456 thread = machine__find_thread(btsq->bts->machine, -1,
459 btsq->pid = thread->pid_;
461 thread = machine__findnew_thread(btsq->bts->machine, btsq->pid,
465 queue = &btsq->bts->queues.queue_array[btsq->queue_nr];
468 buffer = auxtrace_buffer__next(queue, NULL);
471 if (!btsq->bts->sampling_mode)
477 /* Currently there is no support for split buffers */
478 if (buffer->consecutive) {
484 int fd = perf_data__fd(btsq->bts->session->data);
486 buffer->data = auxtrace_buffer__get_data(buffer, fd);
493 if (btsq->bts->snapshot_mode && !buffer->consecutive &&
494 intel_bts_do_fix_overlap(queue, buffer)) {
499 if (!btsq->bts->synth_opts.callchain &&
500 !btsq->bts->synth_opts.thread_stack && thread &&
501 (!old_buffer || btsq->bts->sampling_mode ||
502 (btsq->bts->snapshot_mode && !buffer->consecutive)))
503 thread_stack__set_trace_nr(thread, btsq->cpu, buffer->buffer_nr + 1);
505 err = intel_bts_process_buffer(btsq, buffer, thread);
507 auxtrace_buffer__drop_data(buffer);
509 btsq->buffer = auxtrace_buffer__next(queue, buffer);
512 *timestamp = btsq->buffer->reference;
514 if (!btsq->bts->sampling_mode)
522 static int intel_bts_flush_queue(struct intel_bts_queue *btsq)
528 ret = intel_bts_process_queue(btsq, &ts);
537 static int intel_bts_process_tid_exit(struct intel_bts *bts, pid_t tid)
539 struct auxtrace_queues *queues = &bts->queues;
542 for (i = 0; i < queues->nr_queues; i++) {
543 struct auxtrace_queue *queue = &bts->queues.queue_array[i];
544 struct intel_bts_queue *btsq = queue->priv;
546 if (btsq && btsq->tid == tid)
547 return intel_bts_flush_queue(btsq);
552 static int intel_bts_process_queues(struct intel_bts *bts, u64 timestamp)
555 unsigned int queue_nr;
556 struct auxtrace_queue *queue;
557 struct intel_bts_queue *btsq;
561 if (!bts->heap.heap_cnt)
564 if (bts->heap.heap_array[0].ordinal > timestamp)
567 queue_nr = bts->heap.heap_array[0].queue_nr;
568 queue = &bts->queues.queue_array[queue_nr];
571 auxtrace_heap__pop(&bts->heap);
573 ret = intel_bts_process_queue(btsq, &ts);
575 auxtrace_heap__add(&bts->heap, queue_nr, ts);
580 ret = auxtrace_heap__add(&bts->heap, queue_nr, ts);
584 btsq->on_heap = false;
591 static int intel_bts_process_event(struct perf_session *session,
592 union perf_event *event,
593 struct perf_sample *sample,
594 struct perf_tool *tool)
596 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
604 if (!tool->ordered_events) {
605 pr_err("Intel BTS requires ordered events\n");
609 if (sample->time && sample->time != (u64)-1)
610 timestamp = perf_time_to_tsc(sample->time, &bts->tc);
614 err = intel_bts_update_queues(bts);
618 err = intel_bts_process_queues(bts, timestamp);
621 if (event->header.type == PERF_RECORD_EXIT) {
622 err = intel_bts_process_tid_exit(bts, event->fork.tid);
627 if (event->header.type == PERF_RECORD_AUX &&
628 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
629 bts->synth_opts.errors)
630 err = intel_bts_lost(bts, sample);
635 static int intel_bts_process_auxtrace_event(struct perf_session *session,
636 union perf_event *event,
637 struct perf_tool *tool __maybe_unused)
639 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
642 if (bts->sampling_mode)
645 if (!bts->data_queued) {
646 struct auxtrace_buffer *buffer;
648 int fd = perf_data__fd(session->data);
651 if (perf_data__is_pipe(session->data)) {
654 data_offset = lseek(fd, 0, SEEK_CUR);
655 if (data_offset == -1)
659 err = auxtrace_queues__add_event(&bts->queues, session, event,
660 data_offset, &buffer);
664 /* Dump here now we have copied a piped trace out of the pipe */
666 if (auxtrace_buffer__get_data(buffer, fd)) {
667 intel_bts_dump_event(bts, buffer->data,
669 auxtrace_buffer__put_data(buffer);
677 static int intel_bts_flush(struct perf_session *session,
678 struct perf_tool *tool __maybe_unused)
680 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
684 if (dump_trace || bts->sampling_mode)
687 if (!tool->ordered_events)
690 ret = intel_bts_update_queues(bts);
694 return intel_bts_process_queues(bts, MAX_TIMESTAMP);
697 static void intel_bts_free_queue(void *priv)
699 struct intel_bts_queue *btsq = priv;
706 static void intel_bts_free_events(struct perf_session *session)
708 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
710 struct auxtrace_queues *queues = &bts->queues;
713 for (i = 0; i < queues->nr_queues; i++) {
714 intel_bts_free_queue(queues->queue_array[i].priv);
715 queues->queue_array[i].priv = NULL;
717 auxtrace_queues__free(queues);
720 static void intel_bts_free(struct perf_session *session)
722 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
725 auxtrace_heap__free(&bts->heap);
726 intel_bts_free_events(session);
727 session->auxtrace = NULL;
731 static bool intel_bts_evsel_is_auxtrace(struct perf_session *session,
734 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
737 return evsel->core.attr.type == bts->pmu_type;
740 struct intel_bts_synth {
741 struct perf_tool dummy_tool;
742 struct perf_session *session;
745 static int intel_bts_event_synth(struct perf_tool *tool,
746 union perf_event *event,
747 struct perf_sample *sample __maybe_unused,
748 struct machine *machine __maybe_unused)
750 struct intel_bts_synth *intel_bts_synth =
751 container_of(tool, struct intel_bts_synth, dummy_tool);
753 return perf_session__deliver_synth_event(intel_bts_synth->session,
757 static int intel_bts_synth_event(struct perf_session *session,
758 struct perf_event_attr *attr, u64 id)
760 struct intel_bts_synth intel_bts_synth;
762 memset(&intel_bts_synth, 0, sizeof(struct intel_bts_synth));
763 intel_bts_synth.session = session;
765 return perf_event__synthesize_attr(&intel_bts_synth.dummy_tool, attr, 1,
766 &id, intel_bts_event_synth);
769 static int intel_bts_synth_events(struct intel_bts *bts,
770 struct perf_session *session)
772 struct evlist *evlist = session->evlist;
774 struct perf_event_attr attr;
779 evlist__for_each_entry(evlist, evsel) {
780 if (evsel->core.attr.type == bts->pmu_type && evsel->core.ids) {
787 pr_debug("There are no selected events with Intel BTS data\n");
791 memset(&attr, 0, sizeof(struct perf_event_attr));
792 attr.size = sizeof(struct perf_event_attr);
793 attr.type = PERF_TYPE_HARDWARE;
794 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
795 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
797 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
798 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
799 attr.exclude_user = evsel->core.attr.exclude_user;
800 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
801 attr.exclude_hv = evsel->core.attr.exclude_hv;
802 attr.exclude_host = evsel->core.attr.exclude_host;
803 attr.exclude_guest = evsel->core.attr.exclude_guest;
804 attr.sample_id_all = evsel->core.attr.sample_id_all;
805 attr.read_format = evsel->core.attr.read_format;
807 id = evsel->core.id[0] + 1000000000;
811 if (bts->synth_opts.branches) {
812 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
813 attr.sample_period = 1;
814 attr.sample_type |= PERF_SAMPLE_ADDR;
815 pr_debug("Synthesizing 'branches' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
816 id, (u64)attr.sample_type);
817 err = intel_bts_synth_event(session, &attr, id);
819 pr_err("%s: failed to synthesize 'branches' event type\n",
823 bts->sample_branches = true;
824 bts->branches_sample_type = attr.sample_type;
825 bts->branches_id = id;
827 * We only use sample types from PERF_SAMPLE_MASK so we can use
828 * __evsel__sample_size() here.
830 bts->branches_event_size = sizeof(struct perf_record_sample) +
831 __evsel__sample_size(attr.sample_type);
837 static const char * const intel_bts_info_fmts[] = {
838 [INTEL_BTS_PMU_TYPE] = " PMU Type %"PRId64"\n",
839 [INTEL_BTS_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
840 [INTEL_BTS_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
841 [INTEL_BTS_TIME_ZERO] = " Time Zero %"PRIu64"\n",
842 [INTEL_BTS_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
843 [INTEL_BTS_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
846 static void intel_bts_print_info(__u64 *arr, int start, int finish)
853 for (i = start; i <= finish; i++)
854 fprintf(stdout, intel_bts_info_fmts[i], arr[i]);
857 int intel_bts_process_auxtrace_info(union perf_event *event,
858 struct perf_session *session)
860 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
861 size_t min_sz = sizeof(u64) * INTEL_BTS_SNAPSHOT_MODE;
862 struct intel_bts *bts;
865 if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) +
869 bts = zalloc(sizeof(struct intel_bts));
873 err = auxtrace_queues__init(&bts->queues);
877 bts->session = session;
878 bts->machine = &session->machines.host; /* No kvm support */
879 bts->auxtrace_type = auxtrace_info->type;
880 bts->pmu_type = auxtrace_info->priv[INTEL_BTS_PMU_TYPE];
881 bts->tc.time_shift = auxtrace_info->priv[INTEL_BTS_TIME_SHIFT];
882 bts->tc.time_mult = auxtrace_info->priv[INTEL_BTS_TIME_MULT];
883 bts->tc.time_zero = auxtrace_info->priv[INTEL_BTS_TIME_ZERO];
884 bts->cap_user_time_zero =
885 auxtrace_info->priv[INTEL_BTS_CAP_USER_TIME_ZERO];
886 bts->snapshot_mode = auxtrace_info->priv[INTEL_BTS_SNAPSHOT_MODE];
888 bts->sampling_mode = false;
890 bts->auxtrace.process_event = intel_bts_process_event;
891 bts->auxtrace.process_auxtrace_event = intel_bts_process_auxtrace_event;
892 bts->auxtrace.flush_events = intel_bts_flush;
893 bts->auxtrace.free_events = intel_bts_free_events;
894 bts->auxtrace.free = intel_bts_free;
895 bts->auxtrace.evsel_is_auxtrace = intel_bts_evsel_is_auxtrace;
896 session->auxtrace = &bts->auxtrace;
898 intel_bts_print_info(&auxtrace_info->priv[0], INTEL_BTS_PMU_TYPE,
899 INTEL_BTS_SNAPSHOT_MODE);
904 if (session->itrace_synth_opts->set) {
905 bts->synth_opts = *session->itrace_synth_opts;
907 itrace_synth_opts__set_default(&bts->synth_opts,
908 session->itrace_synth_opts->default_no_sample);
909 bts->synth_opts.thread_stack =
910 session->itrace_synth_opts->thread_stack;
913 if (bts->synth_opts.calls)
914 bts->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
915 PERF_IP_FLAG_TRACE_END;
916 if (bts->synth_opts.returns)
917 bts->branches_filter |= PERF_IP_FLAG_RETURN |
918 PERF_IP_FLAG_TRACE_BEGIN;
920 err = intel_bts_synth_events(bts, session);
922 goto err_free_queues;
924 err = auxtrace_queues__process_index(&bts->queues, session);
926 goto err_free_queues;
928 if (bts->queues.populated)
929 bts->data_queued = true;
934 auxtrace_queues__free(&bts->queues);
935 session->auxtrace = NULL;