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
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/bitops.h>
21 #include <linux/log2.h>
31 #include "thread-stack.h"
35 #include "intel-pt-decoder/intel-pt-insn-decoder.h"
36 #include "intel-bts.h"
38 #define MAX_TIMESTAMP (~0ULL)
40 #define INTEL_BTS_ERR_NOINSN 5
41 #define INTEL_BTS_ERR_LOST 9
43 #if __BYTE_ORDER == __BIG_ENDIAN
44 #define le64_to_cpu bswap_64
50 struct auxtrace auxtrace;
51 struct auxtrace_queues queues;
52 struct auxtrace_heap heap;
54 struct perf_session *session;
55 struct machine *machine;
60 struct perf_tsc_conversion tc;
61 bool cap_user_time_zero;
62 struct itrace_synth_opts synth_opts;
65 u64 branches_sample_type;
67 size_t branches_event_size;
68 bool synth_needs_swap;
69 unsigned long num_events;
72 struct intel_bts_queue {
73 struct intel_bts *bts;
74 unsigned int queue_nr;
75 struct auxtrace_buffer *buffer;
82 struct intel_pt_insn intel_pt_insn;
92 static void intel_bts_dump(struct intel_bts *bts __maybe_unused,
93 unsigned char *buf, size_t len)
95 struct branch *branch;
96 size_t i, pos = 0, br_sz = sizeof(struct branch), sz;
97 const char *color = PERF_COLOR_BLUE;
99 color_fprintf(stdout, color,
100 ". ... Intel BTS data: size %zu bytes\n",
109 color_fprintf(stdout, color, " %08x: ", pos);
110 for (i = 0; i < sz; i++)
111 color_fprintf(stdout, color, " %02x", buf[i]);
112 for (; i < br_sz; i++)
113 color_fprintf(stdout, color, " ");
115 branch = (struct branch *)buf;
116 color_fprintf(stdout, color, " %"PRIx64" -> %"PRIx64" %s\n",
117 le64_to_cpu(branch->from),
118 le64_to_cpu(branch->to),
119 le64_to_cpu(branch->misc) & 0x10 ?
122 color_fprintf(stdout, color, " Bad record!\n");
130 static void intel_bts_dump_event(struct intel_bts *bts, unsigned char *buf,
134 intel_bts_dump(bts, buf, len);
137 static int intel_bts_lost(struct intel_bts *bts, struct perf_sample *sample)
139 union perf_event event;
142 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
143 INTEL_BTS_ERR_LOST, sample->cpu, sample->pid,
144 sample->tid, 0, "Lost trace data");
146 err = perf_session__deliver_synth_event(bts->session, &event, NULL);
148 pr_err("Intel BTS: failed to deliver error event, error %d\n",
154 static struct intel_bts_queue *intel_bts_alloc_queue(struct intel_bts *bts,
155 unsigned int queue_nr)
157 struct intel_bts_queue *btsq;
159 btsq = zalloc(sizeof(struct intel_bts_queue));
164 btsq->queue_nr = queue_nr;
172 static int intel_bts_setup_queue(struct intel_bts *bts,
173 struct auxtrace_queue *queue,
174 unsigned int queue_nr)
176 struct intel_bts_queue *btsq = queue->priv;
178 if (list_empty(&queue->head))
182 btsq = intel_bts_alloc_queue(bts, queue_nr);
187 if (queue->cpu != -1)
188 btsq->cpu = queue->cpu;
189 btsq->tid = queue->tid;
192 if (bts->sampling_mode)
195 if (!btsq->on_heap && !btsq->buffer) {
198 btsq->buffer = auxtrace_buffer__next(queue, NULL);
202 ret = auxtrace_heap__add(&bts->heap, queue_nr,
203 btsq->buffer->reference);
206 btsq->on_heap = true;
212 static int intel_bts_setup_queues(struct intel_bts *bts)
217 for (i = 0; i < bts->queues.nr_queues; i++) {
218 ret = intel_bts_setup_queue(bts, &bts->queues.queue_array[i],
226 static inline int intel_bts_update_queues(struct intel_bts *bts)
228 if (bts->queues.new_data) {
229 bts->queues.new_data = false;
230 return intel_bts_setup_queues(bts);
235 static unsigned char *intel_bts_find_overlap(unsigned char *buf_a, size_t len_a,
236 unsigned char *buf_b, size_t len_b)
241 offs = len_a - len_b;
245 for (; offs < len_a; offs += sizeof(struct branch)) {
247 if (!memcmp(buf_a + offs, buf_b, len))
254 static int intel_bts_do_fix_overlap(struct auxtrace_queue *queue,
255 struct auxtrace_buffer *b)
257 struct auxtrace_buffer *a;
260 if (b->list.prev == &queue->head)
262 a = list_entry(b->list.prev, struct auxtrace_buffer, list);
263 start = intel_bts_find_overlap(a->data, a->size, b->data, b->size);
266 b->use_size = b->data + b->size - start;
271 static int intel_bts_synth_branch_sample(struct intel_bts_queue *btsq,
272 struct branch *branch)
275 struct intel_bts *bts = btsq->bts;
276 union perf_event event;
277 struct perf_sample sample = { .ip = 0, };
279 if (bts->synth_opts.initial_skip &&
280 bts->num_events++ <= bts->synth_opts.initial_skip)
283 event.sample.header.type = PERF_RECORD_SAMPLE;
284 event.sample.header.misc = PERF_RECORD_MISC_USER;
285 event.sample.header.size = sizeof(struct perf_event_header);
287 sample.cpumode = PERF_RECORD_MISC_USER;
288 sample.ip = le64_to_cpu(branch->from);
289 sample.pid = btsq->pid;
290 sample.tid = btsq->tid;
291 sample.addr = le64_to_cpu(branch->to);
292 sample.id = btsq->bts->branches_id;
293 sample.stream_id = btsq->bts->branches_id;
295 sample.cpu = btsq->cpu;
296 sample.flags = btsq->sample_flags;
297 sample.insn_len = btsq->intel_pt_insn.length;
299 if (bts->synth_opts.inject) {
300 event.sample.header.size = bts->branches_event_size;
301 ret = perf_event__synthesize_sample(&event,
302 bts->branches_sample_type,
304 bts->synth_needs_swap);
309 ret = perf_session__deliver_synth_event(bts->session, &event, &sample);
311 pr_err("Intel BTS: failed to deliver branch event, error %d\n",
317 static int intel_bts_get_next_insn(struct intel_bts_queue *btsq, u64 ip)
319 struct machine *machine = btsq->bts->machine;
320 struct thread *thread;
321 struct addr_location al;
322 unsigned char buf[INTEL_PT_INSN_BUF_SZ];
328 if (machine__kernel_ip(machine, ip))
329 cpumode = PERF_RECORD_MISC_KERNEL;
331 cpumode = PERF_RECORD_MISC_USER;
333 thread = machine__find_thread(machine, -1, btsq->tid);
337 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, ip, &al);
338 if (!al.map || !al.map->dso)
341 len = dso__data_read_addr(al.map->dso, al.map, machine, ip, buf,
342 INTEL_PT_INSN_BUF_SZ);
346 /* Load maps to ensure dso->is_64_bit has been updated */
349 x86_64 = al.map->dso->is_64_bit;
351 if (intel_pt_get_insn(buf, len, x86_64, &btsq->intel_pt_insn))
360 static int intel_bts_synth_error(struct intel_bts *bts, int cpu, pid_t pid,
363 union perf_event event;
366 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
367 INTEL_BTS_ERR_NOINSN, cpu, pid, tid, ip,
368 "Failed to get instruction");
370 err = perf_session__deliver_synth_event(bts->session, &event, NULL);
372 pr_err("Intel BTS: failed to deliver error event, error %d\n",
378 static int intel_bts_get_branch_type(struct intel_bts_queue *btsq,
379 struct branch *branch)
385 btsq->sample_flags = PERF_IP_FLAG_BRANCH |
386 PERF_IP_FLAG_TRACE_BEGIN;
388 btsq->sample_flags = 0;
389 btsq->intel_pt_insn.length = 0;
390 } else if (!branch->to) {
391 btsq->sample_flags = PERF_IP_FLAG_BRANCH |
392 PERF_IP_FLAG_TRACE_END;
393 btsq->intel_pt_insn.length = 0;
395 err = intel_bts_get_next_insn(btsq, branch->from);
397 btsq->sample_flags = 0;
398 btsq->intel_pt_insn.length = 0;
399 if (!btsq->bts->synth_opts.errors)
401 err = intel_bts_synth_error(btsq->bts, btsq->cpu,
402 btsq->pid, btsq->tid,
406 btsq->sample_flags = intel_pt_insn_type(btsq->intel_pt_insn.op);
407 /* Check for an async branch into the kernel */
408 if (!machine__kernel_ip(btsq->bts->machine, branch->from) &&
409 machine__kernel_ip(btsq->bts->machine, branch->to) &&
410 btsq->sample_flags != (PERF_IP_FLAG_BRANCH |
412 PERF_IP_FLAG_SYSCALLRET))
413 btsq->sample_flags = PERF_IP_FLAG_BRANCH |
416 PERF_IP_FLAG_INTERRUPT;
422 static int intel_bts_process_buffer(struct intel_bts_queue *btsq,
423 struct auxtrace_buffer *buffer,
424 struct thread *thread)
426 struct branch *branch;
427 size_t sz, bsz = sizeof(struct branch);
428 u32 filter = btsq->bts->branches_filter;
431 if (buffer->use_data) {
432 sz = buffer->use_size;
433 branch = buffer->use_data;
436 branch = buffer->data;
439 if (!btsq->bts->sample_branches)
442 for (; sz > bsz; branch += 1, sz -= bsz) {
443 if (!branch->from && !branch->to)
445 intel_bts_get_branch_type(btsq, branch);
446 if (btsq->bts->synth_opts.thread_stack)
447 thread_stack__event(thread, btsq->sample_flags,
448 le64_to_cpu(branch->from),
449 le64_to_cpu(branch->to),
450 btsq->intel_pt_insn.length,
451 buffer->buffer_nr + 1);
452 if (filter && !(filter & btsq->sample_flags))
454 err = intel_bts_synth_branch_sample(btsq, branch);
461 static int intel_bts_process_queue(struct intel_bts_queue *btsq, u64 *timestamp)
463 struct auxtrace_buffer *buffer = btsq->buffer, *old_buffer = buffer;
464 struct auxtrace_queue *queue;
465 struct thread *thread;
471 if (btsq->pid == -1) {
472 thread = machine__find_thread(btsq->bts->machine, -1,
475 btsq->pid = thread->pid_;
477 thread = machine__findnew_thread(btsq->bts->machine, btsq->pid,
481 queue = &btsq->bts->queues.queue_array[btsq->queue_nr];
484 buffer = auxtrace_buffer__next(queue, NULL);
487 if (!btsq->bts->sampling_mode)
493 /* Currently there is no support for split buffers */
494 if (buffer->consecutive) {
500 int fd = perf_data_file__fd(btsq->bts->session->file);
502 buffer->data = auxtrace_buffer__get_data(buffer, fd);
509 if (btsq->bts->snapshot_mode && !buffer->consecutive &&
510 intel_bts_do_fix_overlap(queue, buffer)) {
515 if (!btsq->bts->synth_opts.callchain &&
516 !btsq->bts->synth_opts.thread_stack && thread &&
517 (!old_buffer || btsq->bts->sampling_mode ||
518 (btsq->bts->snapshot_mode && !buffer->consecutive)))
519 thread_stack__set_trace_nr(thread, buffer->buffer_nr + 1);
521 err = intel_bts_process_buffer(btsq, buffer, thread);
523 auxtrace_buffer__drop_data(buffer);
525 btsq->buffer = auxtrace_buffer__next(queue, buffer);
528 *timestamp = btsq->buffer->reference;
530 if (!btsq->bts->sampling_mode)
538 static int intel_bts_flush_queue(struct intel_bts_queue *btsq)
544 ret = intel_bts_process_queue(btsq, &ts);
553 static int intel_bts_process_tid_exit(struct intel_bts *bts, pid_t tid)
555 struct auxtrace_queues *queues = &bts->queues;
558 for (i = 0; i < queues->nr_queues; i++) {
559 struct auxtrace_queue *queue = &bts->queues.queue_array[i];
560 struct intel_bts_queue *btsq = queue->priv;
562 if (btsq && btsq->tid == tid)
563 return intel_bts_flush_queue(btsq);
568 static int intel_bts_process_queues(struct intel_bts *bts, u64 timestamp)
571 unsigned int queue_nr;
572 struct auxtrace_queue *queue;
573 struct intel_bts_queue *btsq;
577 if (!bts->heap.heap_cnt)
580 if (bts->heap.heap_array[0].ordinal > timestamp)
583 queue_nr = bts->heap.heap_array[0].queue_nr;
584 queue = &bts->queues.queue_array[queue_nr];
587 auxtrace_heap__pop(&bts->heap);
589 ret = intel_bts_process_queue(btsq, &ts);
591 auxtrace_heap__add(&bts->heap, queue_nr, ts);
596 ret = auxtrace_heap__add(&bts->heap, queue_nr, ts);
600 btsq->on_heap = false;
607 static int intel_bts_process_event(struct perf_session *session,
608 union perf_event *event,
609 struct perf_sample *sample,
610 struct perf_tool *tool)
612 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
620 if (!tool->ordered_events) {
621 pr_err("Intel BTS requires ordered events\n");
625 if (sample->time && sample->time != (u64)-1)
626 timestamp = perf_time_to_tsc(sample->time, &bts->tc);
630 err = intel_bts_update_queues(bts);
634 err = intel_bts_process_queues(bts, timestamp);
637 if (event->header.type == PERF_RECORD_EXIT) {
638 err = intel_bts_process_tid_exit(bts, event->fork.tid);
643 if (event->header.type == PERF_RECORD_AUX &&
644 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
645 bts->synth_opts.errors)
646 err = intel_bts_lost(bts, sample);
651 static int intel_bts_process_auxtrace_event(struct perf_session *session,
652 union perf_event *event,
653 struct perf_tool *tool __maybe_unused)
655 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
658 if (bts->sampling_mode)
661 if (!bts->data_queued) {
662 struct auxtrace_buffer *buffer;
664 int fd = perf_data_file__fd(session->file);
667 if (perf_data_file__is_pipe(session->file)) {
670 data_offset = lseek(fd, 0, SEEK_CUR);
671 if (data_offset == -1)
675 err = auxtrace_queues__add_event(&bts->queues, session, event,
676 data_offset, &buffer);
680 /* Dump here now we have copied a piped trace out of the pipe */
682 if (auxtrace_buffer__get_data(buffer, fd)) {
683 intel_bts_dump_event(bts, buffer->data,
685 auxtrace_buffer__put_data(buffer);
693 static int intel_bts_flush(struct perf_session *session,
694 struct perf_tool *tool __maybe_unused)
696 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
700 if (dump_trace || bts->sampling_mode)
703 if (!tool->ordered_events)
706 ret = intel_bts_update_queues(bts);
710 return intel_bts_process_queues(bts, MAX_TIMESTAMP);
713 static void intel_bts_free_queue(void *priv)
715 struct intel_bts_queue *btsq = priv;
722 static void intel_bts_free_events(struct perf_session *session)
724 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
726 struct auxtrace_queues *queues = &bts->queues;
729 for (i = 0; i < queues->nr_queues; i++) {
730 intel_bts_free_queue(queues->queue_array[i].priv);
731 queues->queue_array[i].priv = NULL;
733 auxtrace_queues__free(queues);
736 static void intel_bts_free(struct perf_session *session)
738 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
741 auxtrace_heap__free(&bts->heap);
742 intel_bts_free_events(session);
743 session->auxtrace = NULL;
747 struct intel_bts_synth {
748 struct perf_tool dummy_tool;
749 struct perf_session *session;
752 static int intel_bts_event_synth(struct perf_tool *tool,
753 union perf_event *event,
754 struct perf_sample *sample __maybe_unused,
755 struct machine *machine __maybe_unused)
757 struct intel_bts_synth *intel_bts_synth =
758 container_of(tool, struct intel_bts_synth, dummy_tool);
760 return perf_session__deliver_synth_event(intel_bts_synth->session,
764 static int intel_bts_synth_event(struct perf_session *session,
765 struct perf_event_attr *attr, u64 id)
767 struct intel_bts_synth intel_bts_synth;
769 memset(&intel_bts_synth, 0, sizeof(struct intel_bts_synth));
770 intel_bts_synth.session = session;
772 return perf_event__synthesize_attr(&intel_bts_synth.dummy_tool, attr, 1,
773 &id, intel_bts_event_synth);
776 static int intel_bts_synth_events(struct intel_bts *bts,
777 struct perf_session *session)
779 struct perf_evlist *evlist = session->evlist;
780 struct perf_evsel *evsel;
781 struct perf_event_attr attr;
786 evlist__for_each_entry(evlist, evsel) {
787 if (evsel->attr.type == bts->pmu_type && evsel->ids) {
794 pr_debug("There are no selected events with Intel BTS data\n");
798 memset(&attr, 0, sizeof(struct perf_event_attr));
799 attr.size = sizeof(struct perf_event_attr);
800 attr.type = PERF_TYPE_HARDWARE;
801 attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
802 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
804 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
805 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
806 attr.exclude_user = evsel->attr.exclude_user;
807 attr.exclude_kernel = evsel->attr.exclude_kernel;
808 attr.exclude_hv = evsel->attr.exclude_hv;
809 attr.exclude_host = evsel->attr.exclude_host;
810 attr.exclude_guest = evsel->attr.exclude_guest;
811 attr.sample_id_all = evsel->attr.sample_id_all;
812 attr.read_format = evsel->attr.read_format;
814 id = evsel->id[0] + 1000000000;
818 if (bts->synth_opts.branches) {
819 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
820 attr.sample_period = 1;
821 attr.sample_type |= PERF_SAMPLE_ADDR;
822 pr_debug("Synthesizing 'branches' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
823 id, (u64)attr.sample_type);
824 err = intel_bts_synth_event(session, &attr, id);
826 pr_err("%s: failed to synthesize 'branches' event type\n",
830 bts->sample_branches = true;
831 bts->branches_sample_type = attr.sample_type;
832 bts->branches_id = id;
834 * We only use sample types from PERF_SAMPLE_MASK so we can use
835 * __perf_evsel__sample_size() here.
837 bts->branches_event_size = sizeof(struct sample_event) +
838 __perf_evsel__sample_size(attr.sample_type);
841 bts->synth_needs_swap = evsel->needs_swap;
846 static const char * const intel_bts_info_fmts[] = {
847 [INTEL_BTS_PMU_TYPE] = " PMU Type %"PRId64"\n",
848 [INTEL_BTS_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
849 [INTEL_BTS_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
850 [INTEL_BTS_TIME_ZERO] = " Time Zero %"PRIu64"\n",
851 [INTEL_BTS_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
852 [INTEL_BTS_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
855 static void intel_bts_print_info(u64 *arr, int start, int finish)
862 for (i = start; i <= finish; i++)
863 fprintf(stdout, intel_bts_info_fmts[i], arr[i]);
866 u64 intel_bts_auxtrace_info_priv[INTEL_BTS_AUXTRACE_PRIV_SIZE];
868 int intel_bts_process_auxtrace_info(union perf_event *event,
869 struct perf_session *session)
871 struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
872 size_t min_sz = sizeof(u64) * INTEL_BTS_SNAPSHOT_MODE;
873 struct intel_bts *bts;
876 if (auxtrace_info->header.size < sizeof(struct auxtrace_info_event) +
880 bts = zalloc(sizeof(struct intel_bts));
884 err = auxtrace_queues__init(&bts->queues);
888 bts->session = session;
889 bts->machine = &session->machines.host; /* No kvm support */
890 bts->auxtrace_type = auxtrace_info->type;
891 bts->pmu_type = auxtrace_info->priv[INTEL_BTS_PMU_TYPE];
892 bts->tc.time_shift = auxtrace_info->priv[INTEL_BTS_TIME_SHIFT];
893 bts->tc.time_mult = auxtrace_info->priv[INTEL_BTS_TIME_MULT];
894 bts->tc.time_zero = auxtrace_info->priv[INTEL_BTS_TIME_ZERO];
895 bts->cap_user_time_zero =
896 auxtrace_info->priv[INTEL_BTS_CAP_USER_TIME_ZERO];
897 bts->snapshot_mode = auxtrace_info->priv[INTEL_BTS_SNAPSHOT_MODE];
899 bts->sampling_mode = false;
901 bts->auxtrace.process_event = intel_bts_process_event;
902 bts->auxtrace.process_auxtrace_event = intel_bts_process_auxtrace_event;
903 bts->auxtrace.flush_events = intel_bts_flush;
904 bts->auxtrace.free_events = intel_bts_free_events;
905 bts->auxtrace.free = intel_bts_free;
906 session->auxtrace = &bts->auxtrace;
908 intel_bts_print_info(&auxtrace_info->priv[0], INTEL_BTS_PMU_TYPE,
909 INTEL_BTS_SNAPSHOT_MODE);
914 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
915 bts->synth_opts = *session->itrace_synth_opts;
917 itrace_synth_opts__set_default(&bts->synth_opts);
918 if (session->itrace_synth_opts)
919 bts->synth_opts.thread_stack =
920 session->itrace_synth_opts->thread_stack;
923 if (bts->synth_opts.calls)
924 bts->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
925 PERF_IP_FLAG_TRACE_END;
926 if (bts->synth_opts.returns)
927 bts->branches_filter |= PERF_IP_FLAG_RETURN |
928 PERF_IP_FLAG_TRACE_BEGIN;
930 err = intel_bts_synth_events(bts, session);
932 goto err_free_queues;
934 err = auxtrace_queues__process_index(&bts->queues, session);
936 goto err_free_queues;
938 if (bts->queues.populated)
939 bts->data_queued = true;
944 auxtrace_queues__free(&bts->queues);
945 session->auxtrace = NULL;