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;
71 struct intel_bts_queue {
72 struct intel_bts *bts;
73 unsigned int queue_nr;
74 struct auxtrace_buffer *buffer;
81 struct intel_pt_insn intel_pt_insn;
91 static void intel_bts_dump(struct intel_bts *bts __maybe_unused,
92 unsigned char *buf, size_t len)
94 struct branch *branch;
95 size_t i, pos = 0, br_sz = sizeof(struct branch), sz;
96 const char *color = PERF_COLOR_BLUE;
98 color_fprintf(stdout, color,
99 ". ... Intel BTS data: size %zu bytes\n",
108 color_fprintf(stdout, color, " %08x: ", pos);
109 for (i = 0; i < sz; i++)
110 color_fprintf(stdout, color, " %02x", buf[i]);
111 for (; i < br_sz; i++)
112 color_fprintf(stdout, color, " ");
114 branch = (struct branch *)buf;
115 color_fprintf(stdout, color, " %"PRIx64" -> %"PRIx64" %s\n",
116 le64_to_cpu(branch->from),
117 le64_to_cpu(branch->to),
118 le64_to_cpu(branch->misc) & 0x10 ?
121 color_fprintf(stdout, color, " Bad record!\n");
129 static void intel_bts_dump_event(struct intel_bts *bts, unsigned char *buf,
133 intel_bts_dump(bts, buf, len);
136 static int intel_bts_lost(struct intel_bts *bts, struct perf_sample *sample)
138 union perf_event event;
141 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
142 INTEL_BTS_ERR_LOST, sample->cpu, sample->pid,
143 sample->tid, 0, "Lost trace data");
145 err = perf_session__deliver_synth_event(bts->session, &event, NULL);
147 pr_err("Intel BTS: failed to deliver error event, error %d\n",
153 static struct intel_bts_queue *intel_bts_alloc_queue(struct intel_bts *bts,
154 unsigned int queue_nr)
156 struct intel_bts_queue *btsq;
158 btsq = zalloc(sizeof(struct intel_bts_queue));
163 btsq->queue_nr = queue_nr;
171 static int intel_bts_setup_queue(struct intel_bts *bts,
172 struct auxtrace_queue *queue,
173 unsigned int queue_nr)
175 struct intel_bts_queue *btsq = queue->priv;
177 if (list_empty(&queue->head))
181 btsq = intel_bts_alloc_queue(bts, queue_nr);
186 if (queue->cpu != -1)
187 btsq->cpu = queue->cpu;
188 btsq->tid = queue->tid;
191 if (bts->sampling_mode)
194 if (!btsq->on_heap && !btsq->buffer) {
197 btsq->buffer = auxtrace_buffer__next(queue, NULL);
201 ret = auxtrace_heap__add(&bts->heap, queue_nr,
202 btsq->buffer->reference);
205 btsq->on_heap = true;
211 static int intel_bts_setup_queues(struct intel_bts *bts)
216 for (i = 0; i < bts->queues.nr_queues; i++) {
217 ret = intel_bts_setup_queue(bts, &bts->queues.queue_array[i],
225 static inline int intel_bts_update_queues(struct intel_bts *bts)
227 if (bts->queues.new_data) {
228 bts->queues.new_data = false;
229 return intel_bts_setup_queues(bts);
234 static unsigned char *intel_bts_find_overlap(unsigned char *buf_a, size_t len_a,
235 unsigned char *buf_b, size_t len_b)
240 offs = len_a - len_b;
244 for (; offs < len_a; offs += sizeof(struct branch)) {
246 if (!memcmp(buf_a + offs, buf_b, len))
253 static int intel_bts_do_fix_overlap(struct auxtrace_queue *queue,
254 struct auxtrace_buffer *b)
256 struct auxtrace_buffer *a;
259 if (b->list.prev == &queue->head)
261 a = list_entry(b->list.prev, struct auxtrace_buffer, list);
262 start = intel_bts_find_overlap(a->data, a->size, b->data, b->size);
265 b->use_size = b->data + b->size - start;
270 static int intel_bts_synth_branch_sample(struct intel_bts_queue *btsq,
271 struct branch *branch)
274 struct intel_bts *bts = btsq->bts;
275 union perf_event event;
276 struct perf_sample sample = { .ip = 0, };
278 event.sample.header.type = PERF_RECORD_SAMPLE;
279 event.sample.header.misc = PERF_RECORD_MISC_USER;
280 event.sample.header.size = sizeof(struct perf_event_header);
282 sample.ip = le64_to_cpu(branch->from);
283 sample.pid = btsq->pid;
284 sample.tid = btsq->tid;
285 sample.addr = le64_to_cpu(branch->to);
286 sample.id = btsq->bts->branches_id;
287 sample.stream_id = btsq->bts->branches_id;
289 sample.cpu = btsq->cpu;
290 sample.flags = btsq->sample_flags;
291 sample.insn_len = btsq->intel_pt_insn.length;
293 if (bts->synth_opts.inject) {
294 event.sample.header.size = bts->branches_event_size;
295 ret = perf_event__synthesize_sample(&event,
296 bts->branches_sample_type,
298 bts->synth_needs_swap);
303 ret = perf_session__deliver_synth_event(bts->session, &event, &sample);
305 pr_err("Intel BTS: failed to deliver branch event, error %d\n",
311 static int intel_bts_get_next_insn(struct intel_bts_queue *btsq, u64 ip)
313 struct machine *machine = btsq->bts->machine;
314 struct thread *thread;
315 struct addr_location al;
316 unsigned char buf[1024];
323 bufsz = intel_pt_insn_max_size();
325 if (machine__kernel_ip(machine, ip))
326 cpumode = PERF_RECORD_MISC_KERNEL;
328 cpumode = PERF_RECORD_MISC_USER;
330 thread = machine__find_thread(machine, -1, btsq->tid);
334 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, ip, &al);
335 if (!al.map || !al.map->dso)
338 len = dso__data_read_addr(al.map->dso, al.map, machine, ip, buf, bufsz);
342 /* Load maps to ensure dso->is_64_bit has been updated */
343 map__load(al.map, machine->symbol_filter);
345 x86_64 = al.map->dso->is_64_bit;
347 if (intel_pt_get_insn(buf, len, x86_64, &btsq->intel_pt_insn))
356 static int intel_bts_synth_error(struct intel_bts *bts, int cpu, pid_t pid,
359 union perf_event event;
362 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
363 INTEL_BTS_ERR_NOINSN, cpu, pid, tid, ip,
364 "Failed to get instruction");
366 err = perf_session__deliver_synth_event(bts->session, &event, NULL);
368 pr_err("Intel BTS: failed to deliver error event, error %d\n",
374 static int intel_bts_get_branch_type(struct intel_bts_queue *btsq,
375 struct branch *branch)
381 btsq->sample_flags = PERF_IP_FLAG_BRANCH |
382 PERF_IP_FLAG_TRACE_BEGIN;
384 btsq->sample_flags = 0;
385 btsq->intel_pt_insn.length = 0;
386 } else if (!branch->to) {
387 btsq->sample_flags = PERF_IP_FLAG_BRANCH |
388 PERF_IP_FLAG_TRACE_END;
389 btsq->intel_pt_insn.length = 0;
391 err = intel_bts_get_next_insn(btsq, branch->from);
393 btsq->sample_flags = 0;
394 btsq->intel_pt_insn.length = 0;
395 if (!btsq->bts->synth_opts.errors)
397 err = intel_bts_synth_error(btsq->bts, btsq->cpu,
398 btsq->pid, btsq->tid,
402 btsq->sample_flags = intel_pt_insn_type(btsq->intel_pt_insn.op);
403 /* Check for an async branch into the kernel */
404 if (!machine__kernel_ip(btsq->bts->machine, branch->from) &&
405 machine__kernel_ip(btsq->bts->machine, branch->to) &&
406 btsq->sample_flags != (PERF_IP_FLAG_BRANCH |
408 PERF_IP_FLAG_SYSCALLRET))
409 btsq->sample_flags = PERF_IP_FLAG_BRANCH |
412 PERF_IP_FLAG_INTERRUPT;
418 static int intel_bts_process_buffer(struct intel_bts_queue *btsq,
419 struct auxtrace_buffer *buffer)
421 struct branch *branch;
422 size_t sz, bsz = sizeof(struct branch);
423 u32 filter = btsq->bts->branches_filter;
426 if (buffer->use_data) {
427 sz = buffer->use_size;
428 branch = buffer->use_data;
431 branch = buffer->data;
434 if (!btsq->bts->sample_branches)
437 for (; sz > bsz; branch += 1, sz -= bsz) {
438 if (!branch->from && !branch->to)
440 intel_bts_get_branch_type(btsq, branch);
441 if (filter && !(filter & btsq->sample_flags))
443 err = intel_bts_synth_branch_sample(btsq, branch);
450 static int intel_bts_process_queue(struct intel_bts_queue *btsq, u64 *timestamp)
452 struct auxtrace_buffer *buffer = btsq->buffer, *old_buffer = buffer;
453 struct auxtrace_queue *queue;
454 struct thread *thread;
460 if (btsq->pid == -1) {
461 thread = machine__find_thread(btsq->bts->machine, -1,
464 btsq->pid = thread->pid_;
466 thread = machine__findnew_thread(btsq->bts->machine, btsq->pid,
470 queue = &btsq->bts->queues.queue_array[btsq->queue_nr];
473 buffer = auxtrace_buffer__next(queue, NULL);
476 if (!btsq->bts->sampling_mode)
482 /* Currently there is no support for split buffers */
483 if (buffer->consecutive) {
489 int fd = perf_data_file__fd(btsq->bts->session->file);
491 buffer->data = auxtrace_buffer__get_data(buffer, fd);
498 if (btsq->bts->snapshot_mode && !buffer->consecutive &&
499 intel_bts_do_fix_overlap(queue, buffer)) {
504 if (!btsq->bts->synth_opts.callchain && thread &&
505 (!old_buffer || btsq->bts->sampling_mode ||
506 (btsq->bts->snapshot_mode && !buffer->consecutive)))
507 thread_stack__set_trace_nr(thread, buffer->buffer_nr + 1);
509 err = intel_bts_process_buffer(btsq, buffer);
511 auxtrace_buffer__drop_data(buffer);
513 btsq->buffer = auxtrace_buffer__next(queue, buffer);
516 *timestamp = btsq->buffer->reference;
518 if (!btsq->bts->sampling_mode)
526 static int intel_bts_flush_queue(struct intel_bts_queue *btsq)
532 ret = intel_bts_process_queue(btsq, &ts);
541 static int intel_bts_process_tid_exit(struct intel_bts *bts, pid_t tid)
543 struct auxtrace_queues *queues = &bts->queues;
546 for (i = 0; i < queues->nr_queues; i++) {
547 struct auxtrace_queue *queue = &bts->queues.queue_array[i];
548 struct intel_bts_queue *btsq = queue->priv;
550 if (btsq && btsq->tid == tid)
551 return intel_bts_flush_queue(btsq);
556 static int intel_bts_process_queues(struct intel_bts *bts, u64 timestamp)
559 unsigned int queue_nr;
560 struct auxtrace_queue *queue;
561 struct intel_bts_queue *btsq;
565 if (!bts->heap.heap_cnt)
568 if (bts->heap.heap_array[0].ordinal > timestamp)
571 queue_nr = bts->heap.heap_array[0].queue_nr;
572 queue = &bts->queues.queue_array[queue_nr];
575 auxtrace_heap__pop(&bts->heap);
577 ret = intel_bts_process_queue(btsq, &ts);
579 auxtrace_heap__add(&bts->heap, queue_nr, ts);
584 ret = auxtrace_heap__add(&bts->heap, queue_nr, ts);
588 btsq->on_heap = false;
595 static int intel_bts_process_event(struct perf_session *session,
596 union perf_event *event,
597 struct perf_sample *sample,
598 struct perf_tool *tool)
600 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
608 if (!tool->ordered_events) {
609 pr_err("Intel BTS requires ordered events\n");
613 if (sample->time && sample->time != (u64)-1)
614 timestamp = perf_time_to_tsc(sample->time, &bts->tc);
618 err = intel_bts_update_queues(bts);
622 err = intel_bts_process_queues(bts, timestamp);
625 if (event->header.type == PERF_RECORD_EXIT) {
626 err = intel_bts_process_tid_exit(bts, event->fork.tid);
631 if (event->header.type == PERF_RECORD_AUX &&
632 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
633 bts->synth_opts.errors)
634 err = intel_bts_lost(bts, sample);
639 static int intel_bts_process_auxtrace_event(struct perf_session *session,
640 union perf_event *event,
641 struct perf_tool *tool __maybe_unused)
643 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
646 if (bts->sampling_mode)
649 if (!bts->data_queued) {
650 struct auxtrace_buffer *buffer;
652 int fd = perf_data_file__fd(session->file);
655 if (perf_data_file__is_pipe(session->file)) {
658 data_offset = lseek(fd, 0, SEEK_CUR);
659 if (data_offset == -1)
663 err = auxtrace_queues__add_event(&bts->queues, session, event,
664 data_offset, &buffer);
668 /* Dump here now we have copied a piped trace out of the pipe */
670 if (auxtrace_buffer__get_data(buffer, fd)) {
671 intel_bts_dump_event(bts, buffer->data,
673 auxtrace_buffer__put_data(buffer);
681 static int intel_bts_flush(struct perf_session *session __maybe_unused,
682 struct perf_tool *tool __maybe_unused)
684 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
688 if (dump_trace || bts->sampling_mode)
691 if (!tool->ordered_events)
694 ret = intel_bts_update_queues(bts);
698 return intel_bts_process_queues(bts, MAX_TIMESTAMP);
701 static void intel_bts_free_queue(void *priv)
703 struct intel_bts_queue *btsq = priv;
710 static void intel_bts_free_events(struct perf_session *session)
712 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
714 struct auxtrace_queues *queues = &bts->queues;
717 for (i = 0; i < queues->nr_queues; i++) {
718 intel_bts_free_queue(queues->queue_array[i].priv);
719 queues->queue_array[i].priv = NULL;
721 auxtrace_queues__free(queues);
724 static void intel_bts_free(struct perf_session *session)
726 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
729 auxtrace_heap__free(&bts->heap);
730 intel_bts_free_events(session);
731 session->auxtrace = NULL;
735 struct intel_bts_synth {
736 struct perf_tool dummy_tool;
737 struct perf_session *session;
740 static int intel_bts_event_synth(struct perf_tool *tool,
741 union perf_event *event,
742 struct perf_sample *sample __maybe_unused,
743 struct machine *machine __maybe_unused)
745 struct intel_bts_synth *intel_bts_synth =
746 container_of(tool, struct intel_bts_synth, dummy_tool);
748 return perf_session__deliver_synth_event(intel_bts_synth->session,
752 static int intel_bts_synth_event(struct perf_session *session,
753 struct perf_event_attr *attr, u64 id)
755 struct intel_bts_synth intel_bts_synth;
757 memset(&intel_bts_synth, 0, sizeof(struct intel_bts_synth));
758 intel_bts_synth.session = session;
760 return perf_event__synthesize_attr(&intel_bts_synth.dummy_tool, attr, 1,
761 &id, intel_bts_event_synth);
764 static int intel_bts_synth_events(struct intel_bts *bts,
765 struct perf_session *session)
767 struct perf_evlist *evlist = session->evlist;
768 struct perf_evsel *evsel;
769 struct perf_event_attr attr;
774 evlist__for_each(evlist, evsel) {
775 if (evsel->attr.type == bts->pmu_type && evsel->ids) {
782 pr_debug("There are no selected events with Intel BTS data\n");
786 memset(&attr, 0, sizeof(struct perf_event_attr));
787 attr.size = sizeof(struct perf_event_attr);
788 attr.type = PERF_TYPE_HARDWARE;
789 attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
790 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
792 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
793 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
794 attr.exclude_user = evsel->attr.exclude_user;
795 attr.exclude_kernel = evsel->attr.exclude_kernel;
796 attr.exclude_hv = evsel->attr.exclude_hv;
797 attr.exclude_host = evsel->attr.exclude_host;
798 attr.exclude_guest = evsel->attr.exclude_guest;
799 attr.sample_id_all = evsel->attr.sample_id_all;
800 attr.read_format = evsel->attr.read_format;
802 id = evsel->id[0] + 1000000000;
806 if (bts->synth_opts.branches) {
807 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
808 attr.sample_period = 1;
809 attr.sample_type |= PERF_SAMPLE_ADDR;
810 pr_debug("Synthesizing 'branches' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
811 id, (u64)attr.sample_type);
812 err = intel_bts_synth_event(session, &attr, id);
814 pr_err("%s: failed to synthesize 'branches' event type\n",
818 bts->sample_branches = true;
819 bts->branches_sample_type = attr.sample_type;
820 bts->branches_id = id;
822 * We only use sample types from PERF_SAMPLE_MASK so we can use
823 * __perf_evsel__sample_size() here.
825 bts->branches_event_size = sizeof(struct sample_event) +
826 __perf_evsel__sample_size(attr.sample_type);
829 bts->synth_needs_swap = evsel->needs_swap;
834 static const char * const intel_bts_info_fmts[] = {
835 [INTEL_BTS_PMU_TYPE] = " PMU Type %"PRId64"\n",
836 [INTEL_BTS_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
837 [INTEL_BTS_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
838 [INTEL_BTS_TIME_ZERO] = " Time Zero %"PRIu64"\n",
839 [INTEL_BTS_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
840 [INTEL_BTS_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
843 static void intel_bts_print_info(u64 *arr, int start, int finish)
850 for (i = start; i <= finish; i++)
851 fprintf(stdout, intel_bts_info_fmts[i], arr[i]);
854 u64 intel_bts_auxtrace_info_priv[INTEL_BTS_AUXTRACE_PRIV_SIZE];
856 int intel_bts_process_auxtrace_info(union perf_event *event,
857 struct perf_session *session)
859 struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
860 size_t min_sz = sizeof(u64) * INTEL_BTS_SNAPSHOT_MODE;
861 struct intel_bts *bts;
864 if (auxtrace_info->header.size < sizeof(struct auxtrace_info_event) +
868 bts = zalloc(sizeof(struct intel_bts));
872 err = auxtrace_queues__init(&bts->queues);
876 bts->session = session;
877 bts->machine = &session->machines.host; /* No kvm support */
878 bts->auxtrace_type = auxtrace_info->type;
879 bts->pmu_type = auxtrace_info->priv[INTEL_BTS_PMU_TYPE];
880 bts->tc.time_shift = auxtrace_info->priv[INTEL_BTS_TIME_SHIFT];
881 bts->tc.time_mult = auxtrace_info->priv[INTEL_BTS_TIME_MULT];
882 bts->tc.time_zero = auxtrace_info->priv[INTEL_BTS_TIME_ZERO];
883 bts->cap_user_time_zero =
884 auxtrace_info->priv[INTEL_BTS_CAP_USER_TIME_ZERO];
885 bts->snapshot_mode = auxtrace_info->priv[INTEL_BTS_SNAPSHOT_MODE];
887 bts->sampling_mode = false;
889 bts->auxtrace.process_event = intel_bts_process_event;
890 bts->auxtrace.process_auxtrace_event = intel_bts_process_auxtrace_event;
891 bts->auxtrace.flush_events = intel_bts_flush;
892 bts->auxtrace.free_events = intel_bts_free_events;
893 bts->auxtrace.free = intel_bts_free;
894 session->auxtrace = &bts->auxtrace;
896 intel_bts_print_info(&auxtrace_info->priv[0], INTEL_BTS_PMU_TYPE,
897 INTEL_BTS_SNAPSHOT_MODE);
902 if (session->itrace_synth_opts && session->itrace_synth_opts->set)
903 bts->synth_opts = *session->itrace_synth_opts;
905 itrace_synth_opts__set_default(&bts->synth_opts);
907 if (bts->synth_opts.calls)
908 bts->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
909 PERF_IP_FLAG_TRACE_END;
910 if (bts->synth_opts.returns)
911 bts->branches_filter |= PERF_IP_FLAG_RETURN |
912 PERF_IP_FLAG_TRACE_BEGIN;
914 err = intel_bts_synth_events(bts, session);
916 goto err_free_queues;
918 err = auxtrace_queues__process_index(&bts->queues, session);
920 goto err_free_queues;
922 if (bts->queues.populated)
923 bts->data_queued = true;
928 auxtrace_queues__free(&bts->queues);
929 session->auxtrace = NULL;