2 * intel-bts.c: Intel Processor Trace support
3 * Copyright (c) 2013-2015, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 #include <linux/kernel.h>
21 #include <linux/types.h>
22 #include <linux/bitops.h>
23 #include <linux/log2.h>
35 #include "thread-stack.h"
39 #include "intel-pt-decoder/intel-pt-insn-decoder.h"
40 #include "intel-bts.h"
42 #define MAX_TIMESTAMP (~0ULL)
44 #define INTEL_BTS_ERR_NOINSN 5
45 #define INTEL_BTS_ERR_LOST 9
47 #if __BYTE_ORDER == __BIG_ENDIAN
48 #define le64_to_cpu bswap_64
54 struct auxtrace auxtrace;
55 struct auxtrace_queues queues;
56 struct auxtrace_heap heap;
58 struct perf_session *session;
59 struct machine *machine;
64 struct perf_tsc_conversion tc;
65 bool cap_user_time_zero;
66 struct itrace_synth_opts synth_opts;
69 u64 branches_sample_type;
71 size_t branches_event_size;
72 unsigned long num_events;
75 struct intel_bts_queue {
76 struct intel_bts *bts;
77 unsigned int queue_nr;
78 struct auxtrace_buffer *buffer;
85 struct intel_pt_insn intel_pt_insn;
95 static void intel_bts_dump(struct intel_bts *bts __maybe_unused,
96 unsigned char *buf, size_t len)
98 struct branch *branch;
99 size_t i, pos = 0, br_sz = sizeof(struct branch), sz;
100 const char *color = PERF_COLOR_BLUE;
102 color_fprintf(stdout, color,
103 ". ... Intel BTS data: size %zu bytes\n",
112 color_fprintf(stdout, color, " %08x: ", pos);
113 for (i = 0; i < sz; i++)
114 color_fprintf(stdout, color, " %02x", buf[i]);
115 for (; i < br_sz; i++)
116 color_fprintf(stdout, color, " ");
118 branch = (struct branch *)buf;
119 color_fprintf(stdout, color, " %"PRIx64" -> %"PRIx64" %s\n",
120 le64_to_cpu(branch->from),
121 le64_to_cpu(branch->to),
122 le64_to_cpu(branch->misc) & 0x10 ?
125 color_fprintf(stdout, color, " Bad record!\n");
133 static void intel_bts_dump_event(struct intel_bts *bts, unsigned char *buf,
137 intel_bts_dump(bts, buf, len);
140 static int intel_bts_lost(struct intel_bts *bts, struct perf_sample *sample)
142 union perf_event event;
145 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
146 INTEL_BTS_ERR_LOST, sample->cpu, sample->pid,
147 sample->tid, 0, "Lost trace data", sample->time);
149 err = perf_session__deliver_synth_event(bts->session, &event, NULL);
151 pr_err("Intel BTS: failed to deliver error event, error %d\n",
157 static struct intel_bts_queue *intel_bts_alloc_queue(struct intel_bts *bts,
158 unsigned int queue_nr)
160 struct intel_bts_queue *btsq;
162 btsq = zalloc(sizeof(struct intel_bts_queue));
167 btsq->queue_nr = queue_nr;
175 static int intel_bts_setup_queue(struct intel_bts *bts,
176 struct auxtrace_queue *queue,
177 unsigned int queue_nr)
179 struct intel_bts_queue *btsq = queue->priv;
181 if (list_empty(&queue->head))
185 btsq = intel_bts_alloc_queue(bts, queue_nr);
190 if (queue->cpu != -1)
191 btsq->cpu = queue->cpu;
192 btsq->tid = queue->tid;
195 if (bts->sampling_mode)
198 if (!btsq->on_heap && !btsq->buffer) {
201 btsq->buffer = auxtrace_buffer__next(queue, NULL);
205 ret = auxtrace_heap__add(&bts->heap, queue_nr,
206 btsq->buffer->reference);
209 btsq->on_heap = true;
215 static int intel_bts_setup_queues(struct intel_bts *bts)
220 for (i = 0; i < bts->queues.nr_queues; i++) {
221 ret = intel_bts_setup_queue(bts, &bts->queues.queue_array[i],
229 static inline int intel_bts_update_queues(struct intel_bts *bts)
231 if (bts->queues.new_data) {
232 bts->queues.new_data = false;
233 return intel_bts_setup_queues(bts);
238 static unsigned char *intel_bts_find_overlap(unsigned char *buf_a, size_t len_a,
239 unsigned char *buf_b, size_t len_b)
244 offs = len_a - len_b;
248 for (; offs < len_a; offs += sizeof(struct branch)) {
250 if (!memcmp(buf_a + offs, buf_b, len))
257 static int intel_bts_do_fix_overlap(struct auxtrace_queue *queue,
258 struct auxtrace_buffer *b)
260 struct auxtrace_buffer *a;
263 if (b->list.prev == &queue->head)
265 a = list_entry(b->list.prev, struct auxtrace_buffer, list);
266 start = intel_bts_find_overlap(a->data, a->size, b->data, b->size);
269 b->use_size = b->data + b->size - start;
274 static inline u8 intel_bts_cpumode(struct intel_bts *bts, uint64_t ip)
276 return machine__kernel_ip(bts->machine, ip) ?
277 PERF_RECORD_MISC_KERNEL :
278 PERF_RECORD_MISC_USER;
281 static int intel_bts_synth_branch_sample(struct intel_bts_queue *btsq,
282 struct branch *branch)
285 struct intel_bts *bts = btsq->bts;
286 union perf_event event;
287 struct perf_sample sample = { .ip = 0, };
289 if (bts->synth_opts.initial_skip &&
290 bts->num_events++ <= bts->synth_opts.initial_skip)
293 sample.ip = le64_to_cpu(branch->from);
294 sample.cpumode = intel_bts_cpumode(bts, sample.ip);
295 sample.pid = btsq->pid;
296 sample.tid = btsq->tid;
297 sample.addr = le64_to_cpu(branch->to);
298 sample.id = btsq->bts->branches_id;
299 sample.stream_id = btsq->bts->branches_id;
301 sample.cpu = btsq->cpu;
302 sample.flags = btsq->sample_flags;
303 sample.insn_len = btsq->intel_pt_insn.length;
304 memcpy(sample.insn, btsq->intel_pt_insn.buf, INTEL_PT_INSN_BUF_SZ);
306 event.sample.header.type = PERF_RECORD_SAMPLE;
307 event.sample.header.misc = sample.cpumode;
308 event.sample.header.size = sizeof(struct perf_event_header);
310 if (bts->synth_opts.inject) {
311 event.sample.header.size = bts->branches_event_size;
312 ret = perf_event__synthesize_sample(&event,
313 bts->branches_sample_type,
319 ret = perf_session__deliver_synth_event(bts->session, &event, &sample);
321 pr_err("Intel BTS: failed to deliver branch event, error %d\n",
327 static int intel_bts_get_next_insn(struct intel_bts_queue *btsq, u64 ip)
329 struct machine *machine = btsq->bts->machine;
330 struct thread *thread;
331 unsigned char buf[INTEL_PT_INSN_BUF_SZ];
336 thread = machine__find_thread(machine, -1, btsq->tid);
340 len = thread__memcpy(thread, machine, buf, ip, INTEL_PT_INSN_BUF_SZ, &x86_64);
344 if (intel_pt_get_insn(buf, len, x86_64, &btsq->intel_pt_insn))
353 static int intel_bts_synth_error(struct intel_bts *bts, int cpu, pid_t pid,
356 union perf_event event;
359 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
360 INTEL_BTS_ERR_NOINSN, cpu, pid, tid, ip,
361 "Failed to get instruction", 0);
363 err = perf_session__deliver_synth_event(bts->session, &event, NULL);
365 pr_err("Intel BTS: failed to deliver error event, error %d\n",
371 static int intel_bts_get_branch_type(struct intel_bts_queue *btsq,
372 struct branch *branch)
378 btsq->sample_flags = PERF_IP_FLAG_BRANCH |
379 PERF_IP_FLAG_TRACE_BEGIN;
381 btsq->sample_flags = 0;
382 btsq->intel_pt_insn.length = 0;
383 } else if (!branch->to) {
384 btsq->sample_flags = PERF_IP_FLAG_BRANCH |
385 PERF_IP_FLAG_TRACE_END;
386 btsq->intel_pt_insn.length = 0;
388 err = intel_bts_get_next_insn(btsq, branch->from);
390 btsq->sample_flags = 0;
391 btsq->intel_pt_insn.length = 0;
392 if (!btsq->bts->synth_opts.errors)
394 err = intel_bts_synth_error(btsq->bts, btsq->cpu,
395 btsq->pid, btsq->tid,
399 btsq->sample_flags = intel_pt_insn_type(btsq->intel_pt_insn.op);
400 /* Check for an async branch into the kernel */
401 if (!machine__kernel_ip(btsq->bts->machine, branch->from) &&
402 machine__kernel_ip(btsq->bts->machine, branch->to) &&
403 btsq->sample_flags != (PERF_IP_FLAG_BRANCH |
405 PERF_IP_FLAG_SYSCALLRET))
406 btsq->sample_flags = PERF_IP_FLAG_BRANCH |
409 PERF_IP_FLAG_INTERRUPT;
415 static int intel_bts_process_buffer(struct intel_bts_queue *btsq,
416 struct auxtrace_buffer *buffer,
417 struct thread *thread)
419 struct branch *branch;
420 size_t sz, bsz = sizeof(struct branch);
421 u32 filter = btsq->bts->branches_filter;
424 if (buffer->use_data) {
425 sz = buffer->use_size;
426 branch = buffer->use_data;
429 branch = buffer->data;
432 if (!btsq->bts->sample_branches)
435 for (; sz > bsz; branch += 1, sz -= bsz) {
436 if (!branch->from && !branch->to)
438 intel_bts_get_branch_type(btsq, branch);
439 if (btsq->bts->synth_opts.thread_stack)
440 thread_stack__event(thread, btsq->cpu, btsq->sample_flags,
441 le64_to_cpu(branch->from),
442 le64_to_cpu(branch->to),
443 btsq->intel_pt_insn.length,
444 buffer->buffer_nr + 1);
445 if (filter && !(filter & btsq->sample_flags))
447 err = intel_bts_synth_branch_sample(btsq, branch);
454 static int intel_bts_process_queue(struct intel_bts_queue *btsq, u64 *timestamp)
456 struct auxtrace_buffer *buffer = btsq->buffer, *old_buffer = buffer;
457 struct auxtrace_queue *queue;
458 struct thread *thread;
464 if (btsq->pid == -1) {
465 thread = machine__find_thread(btsq->bts->machine, -1,
468 btsq->pid = thread->pid_;
470 thread = machine__findnew_thread(btsq->bts->machine, btsq->pid,
474 queue = &btsq->bts->queues.queue_array[btsq->queue_nr];
477 buffer = auxtrace_buffer__next(queue, NULL);
480 if (!btsq->bts->sampling_mode)
486 /* Currently there is no support for split buffers */
487 if (buffer->consecutive) {
493 int fd = perf_data__fd(btsq->bts->session->data);
495 buffer->data = auxtrace_buffer__get_data(buffer, fd);
502 if (btsq->bts->snapshot_mode && !buffer->consecutive &&
503 intel_bts_do_fix_overlap(queue, buffer)) {
508 if (!btsq->bts->synth_opts.callchain &&
509 !btsq->bts->synth_opts.thread_stack && thread &&
510 (!old_buffer || btsq->bts->sampling_mode ||
511 (btsq->bts->snapshot_mode && !buffer->consecutive)))
512 thread_stack__set_trace_nr(thread, btsq->cpu, buffer->buffer_nr + 1);
514 err = intel_bts_process_buffer(btsq, buffer, thread);
516 auxtrace_buffer__drop_data(buffer);
518 btsq->buffer = auxtrace_buffer__next(queue, buffer);
521 *timestamp = btsq->buffer->reference;
523 if (!btsq->bts->sampling_mode)
531 static int intel_bts_flush_queue(struct intel_bts_queue *btsq)
537 ret = intel_bts_process_queue(btsq, &ts);
546 static int intel_bts_process_tid_exit(struct intel_bts *bts, pid_t tid)
548 struct auxtrace_queues *queues = &bts->queues;
551 for (i = 0; i < queues->nr_queues; i++) {
552 struct auxtrace_queue *queue = &bts->queues.queue_array[i];
553 struct intel_bts_queue *btsq = queue->priv;
555 if (btsq && btsq->tid == tid)
556 return intel_bts_flush_queue(btsq);
561 static int intel_bts_process_queues(struct intel_bts *bts, u64 timestamp)
564 unsigned int queue_nr;
565 struct auxtrace_queue *queue;
566 struct intel_bts_queue *btsq;
570 if (!bts->heap.heap_cnt)
573 if (bts->heap.heap_array[0].ordinal > timestamp)
576 queue_nr = bts->heap.heap_array[0].queue_nr;
577 queue = &bts->queues.queue_array[queue_nr];
580 auxtrace_heap__pop(&bts->heap);
582 ret = intel_bts_process_queue(btsq, &ts);
584 auxtrace_heap__add(&bts->heap, queue_nr, ts);
589 ret = auxtrace_heap__add(&bts->heap, queue_nr, ts);
593 btsq->on_heap = false;
600 static int intel_bts_process_event(struct perf_session *session,
601 union perf_event *event,
602 struct perf_sample *sample,
603 struct perf_tool *tool)
605 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
613 if (!tool->ordered_events) {
614 pr_err("Intel BTS requires ordered events\n");
618 if (sample->time && sample->time != (u64)-1)
619 timestamp = perf_time_to_tsc(sample->time, &bts->tc);
623 err = intel_bts_update_queues(bts);
627 err = intel_bts_process_queues(bts, timestamp);
630 if (event->header.type == PERF_RECORD_EXIT) {
631 err = intel_bts_process_tid_exit(bts, event->fork.tid);
636 if (event->header.type == PERF_RECORD_AUX &&
637 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
638 bts->synth_opts.errors)
639 err = intel_bts_lost(bts, sample);
644 static int intel_bts_process_auxtrace_event(struct perf_session *session,
645 union perf_event *event,
646 struct perf_tool *tool __maybe_unused)
648 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
651 if (bts->sampling_mode)
654 if (!bts->data_queued) {
655 struct auxtrace_buffer *buffer;
657 int fd = perf_data__fd(session->data);
660 if (perf_data__is_pipe(session->data)) {
663 data_offset = lseek(fd, 0, SEEK_CUR);
664 if (data_offset == -1)
668 err = auxtrace_queues__add_event(&bts->queues, session, event,
669 data_offset, &buffer);
673 /* Dump here now we have copied a piped trace out of the pipe */
675 if (auxtrace_buffer__get_data(buffer, fd)) {
676 intel_bts_dump_event(bts, buffer->data,
678 auxtrace_buffer__put_data(buffer);
686 static int intel_bts_flush(struct perf_session *session,
687 struct perf_tool *tool __maybe_unused)
689 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
693 if (dump_trace || bts->sampling_mode)
696 if (!tool->ordered_events)
699 ret = intel_bts_update_queues(bts);
703 return intel_bts_process_queues(bts, MAX_TIMESTAMP);
706 static void intel_bts_free_queue(void *priv)
708 struct intel_bts_queue *btsq = priv;
715 static void intel_bts_free_events(struct perf_session *session)
717 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
719 struct auxtrace_queues *queues = &bts->queues;
722 for (i = 0; i < queues->nr_queues; i++) {
723 intel_bts_free_queue(queues->queue_array[i].priv);
724 queues->queue_array[i].priv = NULL;
726 auxtrace_queues__free(queues);
729 static void intel_bts_free(struct perf_session *session)
731 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
734 auxtrace_heap__free(&bts->heap);
735 intel_bts_free_events(session);
736 session->auxtrace = NULL;
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 perf_evlist *evlist = session->evlist;
773 struct perf_evsel *evsel;
774 struct perf_event_attr attr;
779 evlist__for_each_entry(evlist, evsel) {
780 if (evsel->attr.type == bts->pmu_type && evsel->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->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->attr.exclude_user;
800 attr.exclude_kernel = evsel->attr.exclude_kernel;
801 attr.exclude_hv = evsel->attr.exclude_hv;
802 attr.exclude_host = evsel->attr.exclude_host;
803 attr.exclude_guest = evsel->attr.exclude_guest;
804 attr.sample_id_all = evsel->attr.sample_id_all;
805 attr.read_format = evsel->attr.read_format;
807 id = evsel->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 * __perf_evsel__sample_size() here.
830 bts->branches_event_size = sizeof(struct sample_event) +
831 __perf_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 auxtrace_info_event *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 auxtrace_info_event) +
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 session->auxtrace = &bts->auxtrace;
897 intel_bts_print_info(&auxtrace_info->priv[0], INTEL_BTS_PMU_TYPE,
898 INTEL_BTS_SNAPSHOT_MODE);
903 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
904 bts->synth_opts = *session->itrace_synth_opts;
906 itrace_synth_opts__set_default(&bts->synth_opts,
907 session->itrace_synth_opts->default_no_sample);
908 if (session->itrace_synth_opts)
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;