2 * intel_pt.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>
36 #include "thread-stack.h"
38 #include "callchain.h"
46 #include "intel-pt-decoder/intel-pt-log.h"
47 #include "intel-pt-decoder/intel-pt-decoder.h"
48 #include "intel-pt-decoder/intel-pt-insn-decoder.h"
49 #include "intel-pt-decoder/intel-pt-pkt-decoder.h"
51 #define MAX_TIMESTAMP (~0ULL)
54 struct auxtrace auxtrace;
55 struct auxtrace_queues queues;
56 struct auxtrace_heap heap;
58 struct perf_session *session;
59 struct machine *machine;
60 struct perf_evsel *switch_evsel;
61 struct thread *unknown_thread;
62 bool timeless_decoding;
71 int have_sched_switch;
77 struct perf_tsc_conversion tc;
78 bool cap_user_time_zero;
80 struct itrace_synth_opts synth_opts;
82 bool sample_instructions;
83 u64 instructions_sample_type;
88 u64 branches_sample_type;
91 bool sample_transactions;
92 u64 transactions_sample_type;
95 bool synth_needs_swap;
104 unsigned max_non_turbo_ratio;
106 unsigned long num_events;
109 struct addr_filters filts;
113 INTEL_PT_SS_NOT_TRACING,
116 INTEL_PT_SS_EXPECTING_SWITCH_EVENT,
117 INTEL_PT_SS_EXPECTING_SWITCH_IP,
120 struct intel_pt_queue {
122 unsigned int queue_nr;
123 struct auxtrace_buffer *buffer;
125 const struct intel_pt_state *state;
126 struct ip_callchain *chain;
127 struct branch_stack *last_branch;
128 struct branch_stack *last_branch_rb;
129 size_t last_branch_pos;
130 union perf_event *event_buf;
133 bool step_through_buffers;
134 bool use_buffer_pid_tid;
139 struct thread *thread;
147 char insn[INTEL_PT_INSN_BUF_SZ];
150 static void intel_pt_dump(struct intel_pt *pt __maybe_unused,
151 unsigned char *buf, size_t len)
153 struct intel_pt_pkt packet;
156 char desc[INTEL_PT_PKT_DESC_MAX];
157 const char *color = PERF_COLOR_BLUE;
159 color_fprintf(stdout, color,
160 ". ... Intel Processor Trace data: size %zu bytes\n",
164 ret = intel_pt_get_packet(buf, len, &packet);
170 color_fprintf(stdout, color, " %08x: ", pos);
171 for (i = 0; i < pkt_len; i++)
172 color_fprintf(stdout, color, " %02x", buf[i]);
174 color_fprintf(stdout, color, " ");
176 ret = intel_pt_pkt_desc(&packet, desc,
177 INTEL_PT_PKT_DESC_MAX);
179 color_fprintf(stdout, color, " %s\n", desc);
181 color_fprintf(stdout, color, " Bad packet!\n");
189 static void intel_pt_dump_event(struct intel_pt *pt, unsigned char *buf,
193 intel_pt_dump(pt, buf, len);
196 static int intel_pt_do_fix_overlap(struct intel_pt *pt, struct auxtrace_buffer *a,
197 struct auxtrace_buffer *b)
201 start = intel_pt_find_overlap(a->data, a->size, b->data, b->size,
205 b->use_size = b->data + b->size - start;
210 static void intel_pt_use_buffer_pid_tid(struct intel_pt_queue *ptq,
211 struct auxtrace_queue *queue,
212 struct auxtrace_buffer *buffer)
214 if (queue->cpu == -1 && buffer->cpu != -1)
215 ptq->cpu = buffer->cpu;
217 ptq->pid = buffer->pid;
218 ptq->tid = buffer->tid;
220 intel_pt_log("queue %u cpu %d pid %d tid %d\n",
221 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
223 thread__zput(ptq->thread);
225 if (ptq->tid != -1) {
227 ptq->thread = machine__findnew_thread(ptq->pt->machine,
231 ptq->thread = machine__find_thread(ptq->pt->machine, -1,
236 /* This function assumes data is processed sequentially only */
237 static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data)
239 struct intel_pt_queue *ptq = data;
240 struct auxtrace_buffer *buffer = ptq->buffer, *old_buffer = buffer;
241 struct auxtrace_queue *queue;
248 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
250 buffer = auxtrace_buffer__next(queue, buffer);
253 auxtrace_buffer__drop_data(old_buffer);
258 ptq->buffer = buffer;
261 int fd = perf_data_file__fd(ptq->pt->session->file);
263 buffer->data = auxtrace_buffer__get_data(buffer, fd);
268 if (ptq->pt->snapshot_mode && !buffer->consecutive && old_buffer &&
269 intel_pt_do_fix_overlap(ptq->pt, old_buffer, buffer))
272 if (buffer->use_data) {
273 b->len = buffer->use_size;
274 b->buf = buffer->use_data;
276 b->len = buffer->size;
277 b->buf = buffer->data;
279 b->ref_timestamp = buffer->reference;
282 * If in snapshot mode and the buffer has no usable data, get next
283 * buffer and again check overlap against old_buffer.
285 if (ptq->pt->snapshot_mode && !b->len)
289 auxtrace_buffer__drop_data(old_buffer);
291 if (!old_buffer || ptq->pt->sampling_mode || (ptq->pt->snapshot_mode &&
292 !buffer->consecutive)) {
293 b->consecutive = false;
294 b->trace_nr = buffer->buffer_nr + 1;
296 b->consecutive = true;
299 if (ptq->use_buffer_pid_tid && (ptq->pid != buffer->pid ||
300 ptq->tid != buffer->tid))
301 intel_pt_use_buffer_pid_tid(ptq, queue, buffer);
303 if (ptq->step_through_buffers)
307 return intel_pt_get_trace(b, data);
312 struct intel_pt_cache_entry {
313 struct auxtrace_cache_entry entry;
316 enum intel_pt_insn_op op;
317 enum intel_pt_insn_branch branch;
320 char insn[INTEL_PT_INSN_BUF_SZ];
323 static int intel_pt_config_div(const char *var, const char *value, void *data)
328 if (!strcmp(var, "intel-pt.cache-divisor")) {
329 val = strtol(value, NULL, 0);
330 if (val > 0 && val <= INT_MAX)
337 static int intel_pt_cache_divisor(void)
344 perf_config(intel_pt_config_div, &d);
352 static unsigned int intel_pt_cache_size(struct dso *dso,
353 struct machine *machine)
357 size = dso__data_size(dso, machine);
358 size /= intel_pt_cache_divisor();
361 if (size > (1 << 21))
363 return 32 - __builtin_clz(size);
366 static struct auxtrace_cache *intel_pt_cache(struct dso *dso,
367 struct machine *machine)
369 struct auxtrace_cache *c;
372 if (dso->auxtrace_cache)
373 return dso->auxtrace_cache;
375 bits = intel_pt_cache_size(dso, machine);
377 /* Ignoring cache creation failure */
378 c = auxtrace_cache__new(bits, sizeof(struct intel_pt_cache_entry), 200);
380 dso->auxtrace_cache = c;
385 static int intel_pt_cache_add(struct dso *dso, struct machine *machine,
386 u64 offset, u64 insn_cnt, u64 byte_cnt,
387 struct intel_pt_insn *intel_pt_insn)
389 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
390 struct intel_pt_cache_entry *e;
396 e = auxtrace_cache__alloc_entry(c);
400 e->insn_cnt = insn_cnt;
401 e->byte_cnt = byte_cnt;
402 e->op = intel_pt_insn->op;
403 e->branch = intel_pt_insn->branch;
404 e->length = intel_pt_insn->length;
405 e->rel = intel_pt_insn->rel;
406 memcpy(e->insn, intel_pt_insn->buf, INTEL_PT_INSN_BUF_SZ);
408 err = auxtrace_cache__add(c, offset, &e->entry);
410 auxtrace_cache__free_entry(c, e);
415 static struct intel_pt_cache_entry *
416 intel_pt_cache_lookup(struct dso *dso, struct machine *machine, u64 offset)
418 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
423 return auxtrace_cache__lookup(dso->auxtrace_cache, offset);
426 static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
427 uint64_t *insn_cnt_ptr, uint64_t *ip,
428 uint64_t to_ip, uint64_t max_insn_cnt,
431 struct intel_pt_queue *ptq = data;
432 struct machine *machine = ptq->pt->machine;
433 struct thread *thread;
434 struct addr_location al;
435 unsigned char buf[INTEL_PT_INSN_BUF_SZ];
439 u64 offset, start_offset, start_ip;
443 intel_pt_insn->length = 0;
445 if (to_ip && *ip == to_ip)
448 if (*ip >= ptq->pt->kernel_start)
449 cpumode = PERF_RECORD_MISC_KERNEL;
451 cpumode = PERF_RECORD_MISC_USER;
453 thread = ptq->thread;
455 if (cpumode != PERF_RECORD_MISC_KERNEL)
457 thread = ptq->pt->unknown_thread;
461 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, *ip, &al);
462 if (!al.map || !al.map->dso)
465 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
466 dso__data_status_seen(al.map->dso,
467 DSO_DATA_STATUS_SEEN_ITRACE))
470 offset = al.map->map_ip(al.map, *ip);
472 if (!to_ip && one_map) {
473 struct intel_pt_cache_entry *e;
475 e = intel_pt_cache_lookup(al.map->dso, machine, offset);
477 (!max_insn_cnt || e->insn_cnt <= max_insn_cnt)) {
478 *insn_cnt_ptr = e->insn_cnt;
480 intel_pt_insn->op = e->op;
481 intel_pt_insn->branch = e->branch;
482 intel_pt_insn->length = e->length;
483 intel_pt_insn->rel = e->rel;
484 memcpy(intel_pt_insn->buf, e->insn,
485 INTEL_PT_INSN_BUF_SZ);
486 intel_pt_log_insn_no_data(intel_pt_insn, *ip);
491 start_offset = offset;
494 /* Load maps to ensure dso->is_64_bit has been updated */
497 x86_64 = al.map->dso->is_64_bit;
500 len = dso__data_read_offset(al.map->dso, machine,
502 INTEL_PT_INSN_BUF_SZ);
506 if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn))
509 intel_pt_log_insn(intel_pt_insn, *ip);
513 if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH)
516 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
519 *ip += intel_pt_insn->length;
521 if (to_ip && *ip == to_ip)
524 if (*ip >= al.map->end)
527 offset += intel_pt_insn->length;
532 *insn_cnt_ptr = insn_cnt;
538 * Didn't lookup in the 'to_ip' case, so do it now to prevent duplicate
542 struct intel_pt_cache_entry *e;
544 e = intel_pt_cache_lookup(al.map->dso, machine, start_offset);
549 /* Ignore cache errors */
550 intel_pt_cache_add(al.map->dso, machine, start_offset, insn_cnt,
551 *ip - start_ip, intel_pt_insn);
556 *insn_cnt_ptr = insn_cnt;
560 static bool intel_pt_match_pgd_ip(struct intel_pt *pt, uint64_t ip,
561 uint64_t offset, const char *filename)
563 struct addr_filter *filt;
564 bool have_filter = false;
565 bool hit_tracestop = false;
566 bool hit_filter = false;
568 list_for_each_entry(filt, &pt->filts.head, list) {
572 if ((filename && !filt->filename) ||
573 (!filename && filt->filename) ||
574 (filename && strcmp(filename, filt->filename)))
577 if (!(offset >= filt->addr && offset < filt->addr + filt->size))
580 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s hit filter: %s offset %#"PRIx64" size %#"PRIx64"\n",
581 ip, offset, filename ? filename : "[kernel]",
582 filt->start ? "filter" : "stop",
583 filt->addr, filt->size);
588 hit_tracestop = true;
591 if (!hit_tracestop && !hit_filter)
592 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s is not in a filter region\n",
593 ip, offset, filename ? filename : "[kernel]");
595 return hit_tracestop || (have_filter && !hit_filter);
598 static int __intel_pt_pgd_ip(uint64_t ip, void *data)
600 struct intel_pt_queue *ptq = data;
601 struct thread *thread;
602 struct addr_location al;
606 if (ip >= ptq->pt->kernel_start)
607 return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL);
609 cpumode = PERF_RECORD_MISC_USER;
611 thread = ptq->thread;
615 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, ip, &al);
616 if (!al.map || !al.map->dso)
619 offset = al.map->map_ip(al.map, ip);
621 return intel_pt_match_pgd_ip(ptq->pt, ip, offset,
622 al.map->dso->long_name);
625 static bool intel_pt_pgd_ip(uint64_t ip, void *data)
627 return __intel_pt_pgd_ip(ip, data) > 0;
630 static bool intel_pt_get_config(struct intel_pt *pt,
631 struct perf_event_attr *attr, u64 *config)
633 if (attr->type == pt->pmu_type) {
635 *config = attr->config;
642 static bool intel_pt_exclude_kernel(struct intel_pt *pt)
644 struct perf_evsel *evsel;
646 evlist__for_each_entry(pt->session->evlist, evsel) {
647 if (intel_pt_get_config(pt, &evsel->attr, NULL) &&
648 !evsel->attr.exclude_kernel)
654 static bool intel_pt_return_compression(struct intel_pt *pt)
656 struct perf_evsel *evsel;
659 if (!pt->noretcomp_bit)
662 evlist__for_each_entry(pt->session->evlist, evsel) {
663 if (intel_pt_get_config(pt, &evsel->attr, &config) &&
664 (config & pt->noretcomp_bit))
670 static bool intel_pt_branch_enable(struct intel_pt *pt)
672 struct perf_evsel *evsel;
675 evlist__for_each_entry(pt->session->evlist, evsel) {
676 if (intel_pt_get_config(pt, &evsel->attr, &config) &&
677 (config & 1) && !(config & 0x2000))
683 static unsigned int intel_pt_mtc_period(struct intel_pt *pt)
685 struct perf_evsel *evsel;
689 if (!pt->mtc_freq_bits)
692 for (shift = 0, config = pt->mtc_freq_bits; !(config & 1); shift++)
695 evlist__for_each_entry(pt->session->evlist, evsel) {
696 if (intel_pt_get_config(pt, &evsel->attr, &config))
697 return (config & pt->mtc_freq_bits) >> shift;
702 static bool intel_pt_timeless_decoding(struct intel_pt *pt)
704 struct perf_evsel *evsel;
705 bool timeless_decoding = true;
708 if (!pt->tsc_bit || !pt->cap_user_time_zero)
711 evlist__for_each_entry(pt->session->evlist, evsel) {
712 if (!(evsel->attr.sample_type & PERF_SAMPLE_TIME))
714 if (intel_pt_get_config(pt, &evsel->attr, &config)) {
715 if (config & pt->tsc_bit)
716 timeless_decoding = false;
721 return timeless_decoding;
724 static bool intel_pt_tracing_kernel(struct intel_pt *pt)
726 struct perf_evsel *evsel;
728 evlist__for_each_entry(pt->session->evlist, evsel) {
729 if (intel_pt_get_config(pt, &evsel->attr, NULL) &&
730 !evsel->attr.exclude_kernel)
736 static bool intel_pt_have_tsc(struct intel_pt *pt)
738 struct perf_evsel *evsel;
739 bool have_tsc = false;
745 evlist__for_each_entry(pt->session->evlist, evsel) {
746 if (intel_pt_get_config(pt, &evsel->attr, &config)) {
747 if (config & pt->tsc_bit)
756 static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns)
760 quot = ns / pt->tc.time_mult;
761 rem = ns % pt->tc.time_mult;
762 return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) /
766 static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt,
767 unsigned int queue_nr)
769 struct intel_pt_params params = { .get_trace = 0, };
770 struct intel_pt_queue *ptq;
772 ptq = zalloc(sizeof(struct intel_pt_queue));
776 if (pt->synth_opts.callchain) {
777 size_t sz = sizeof(struct ip_callchain);
779 sz += pt->synth_opts.callchain_sz * sizeof(u64);
780 ptq->chain = zalloc(sz);
785 if (pt->synth_opts.last_branch) {
786 size_t sz = sizeof(struct branch_stack);
788 sz += pt->synth_opts.last_branch_sz *
789 sizeof(struct branch_entry);
790 ptq->last_branch = zalloc(sz);
791 if (!ptq->last_branch)
793 ptq->last_branch_rb = zalloc(sz);
794 if (!ptq->last_branch_rb)
798 ptq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
803 ptq->queue_nr = queue_nr;
804 ptq->exclude_kernel = intel_pt_exclude_kernel(pt);
810 params.get_trace = intel_pt_get_trace;
811 params.walk_insn = intel_pt_walk_next_insn;
813 params.return_compression = intel_pt_return_compression(pt);
814 params.branch_enable = intel_pt_branch_enable(pt);
815 params.max_non_turbo_ratio = pt->max_non_turbo_ratio;
816 params.mtc_period = intel_pt_mtc_period(pt);
817 params.tsc_ctc_ratio_n = pt->tsc_ctc_ratio_n;
818 params.tsc_ctc_ratio_d = pt->tsc_ctc_ratio_d;
820 if (pt->filts.cnt > 0)
821 params.pgd_ip = intel_pt_pgd_ip;
823 if (pt->synth_opts.instructions) {
824 if (pt->synth_opts.period) {
825 switch (pt->synth_opts.period_type) {
826 case PERF_ITRACE_PERIOD_INSTRUCTIONS:
828 INTEL_PT_PERIOD_INSTRUCTIONS;
829 params.period = pt->synth_opts.period;
831 case PERF_ITRACE_PERIOD_TICKS:
832 params.period_type = INTEL_PT_PERIOD_TICKS;
833 params.period = pt->synth_opts.period;
835 case PERF_ITRACE_PERIOD_NANOSECS:
836 params.period_type = INTEL_PT_PERIOD_TICKS;
837 params.period = intel_pt_ns_to_ticks(pt,
838 pt->synth_opts.period);
845 if (!params.period) {
846 params.period_type = INTEL_PT_PERIOD_INSTRUCTIONS;
851 ptq->decoder = intel_pt_decoder_new(¶ms);
858 zfree(&ptq->event_buf);
859 zfree(&ptq->last_branch);
860 zfree(&ptq->last_branch_rb);
866 static void intel_pt_free_queue(void *priv)
868 struct intel_pt_queue *ptq = priv;
872 thread__zput(ptq->thread);
873 intel_pt_decoder_free(ptq->decoder);
874 zfree(&ptq->event_buf);
875 zfree(&ptq->last_branch);
876 zfree(&ptq->last_branch_rb);
881 static void intel_pt_set_pid_tid_cpu(struct intel_pt *pt,
882 struct auxtrace_queue *queue)
884 struct intel_pt_queue *ptq = queue->priv;
886 if (queue->tid == -1 || pt->have_sched_switch) {
887 ptq->tid = machine__get_current_tid(pt->machine, ptq->cpu);
888 thread__zput(ptq->thread);
891 if (!ptq->thread && ptq->tid != -1)
892 ptq->thread = machine__find_thread(pt->machine, -1, ptq->tid);
895 ptq->pid = ptq->thread->pid_;
896 if (queue->cpu == -1)
897 ptq->cpu = ptq->thread->cpu;
901 static void intel_pt_sample_flags(struct intel_pt_queue *ptq)
903 if (ptq->state->flags & INTEL_PT_ABORT_TX) {
904 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT;
905 } else if (ptq->state->flags & INTEL_PT_ASYNC) {
906 if (ptq->state->to_ip)
907 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
909 PERF_IP_FLAG_INTERRUPT;
911 ptq->flags = PERF_IP_FLAG_BRANCH |
912 PERF_IP_FLAG_TRACE_END;
915 if (ptq->state->from_ip)
916 ptq->flags = intel_pt_insn_type(ptq->state->insn_op);
918 ptq->flags = PERF_IP_FLAG_BRANCH |
919 PERF_IP_FLAG_TRACE_BEGIN;
920 if (ptq->state->flags & INTEL_PT_IN_TX)
921 ptq->flags |= PERF_IP_FLAG_IN_TX;
922 ptq->insn_len = ptq->state->insn_len;
923 memcpy(ptq->insn, ptq->state->insn, INTEL_PT_INSN_BUF_SZ);
927 static int intel_pt_setup_queue(struct intel_pt *pt,
928 struct auxtrace_queue *queue,
929 unsigned int queue_nr)
931 struct intel_pt_queue *ptq = queue->priv;
933 if (list_empty(&queue->head))
937 ptq = intel_pt_alloc_queue(pt, queue_nr);
942 if (queue->cpu != -1)
943 ptq->cpu = queue->cpu;
944 ptq->tid = queue->tid;
946 if (pt->sampling_mode) {
947 if (pt->timeless_decoding)
948 ptq->step_through_buffers = true;
949 if (pt->timeless_decoding || !pt->have_sched_switch)
950 ptq->use_buffer_pid_tid = true;
956 ptq->switch_state != INTEL_PT_SS_EXPECTING_SWITCH_EVENT)) {
957 const struct intel_pt_state *state;
960 if (pt->timeless_decoding)
963 intel_pt_log("queue %u getting timestamp\n", queue_nr);
964 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
965 queue_nr, ptq->cpu, ptq->pid, ptq->tid);
967 state = intel_pt_decode(ptq->decoder);
969 if (state->err == INTEL_PT_ERR_NODATA) {
970 intel_pt_log("queue %u has no timestamp\n",
976 if (state->timestamp)
980 ptq->timestamp = state->timestamp;
981 intel_pt_log("queue %u timestamp 0x%" PRIx64 "\n",
982 queue_nr, ptq->timestamp);
984 ptq->have_sample = true;
985 intel_pt_sample_flags(ptq);
986 ret = auxtrace_heap__add(&pt->heap, queue_nr, ptq->timestamp);
995 static int intel_pt_setup_queues(struct intel_pt *pt)
1000 for (i = 0; i < pt->queues.nr_queues; i++) {
1001 ret = intel_pt_setup_queue(pt, &pt->queues.queue_array[i], i);
1008 static inline void intel_pt_copy_last_branch_rb(struct intel_pt_queue *ptq)
1010 struct branch_stack *bs_src = ptq->last_branch_rb;
1011 struct branch_stack *bs_dst = ptq->last_branch;
1014 bs_dst->nr = bs_src->nr;
1019 nr = ptq->pt->synth_opts.last_branch_sz - ptq->last_branch_pos;
1020 memcpy(&bs_dst->entries[0],
1021 &bs_src->entries[ptq->last_branch_pos],
1022 sizeof(struct branch_entry) * nr);
1024 if (bs_src->nr >= ptq->pt->synth_opts.last_branch_sz) {
1025 memcpy(&bs_dst->entries[nr],
1026 &bs_src->entries[0],
1027 sizeof(struct branch_entry) * ptq->last_branch_pos);
1031 static inline void intel_pt_reset_last_branch_rb(struct intel_pt_queue *ptq)
1033 ptq->last_branch_pos = 0;
1034 ptq->last_branch_rb->nr = 0;
1037 static void intel_pt_update_last_branch_rb(struct intel_pt_queue *ptq)
1039 const struct intel_pt_state *state = ptq->state;
1040 struct branch_stack *bs = ptq->last_branch_rb;
1041 struct branch_entry *be;
1043 if (!ptq->last_branch_pos)
1044 ptq->last_branch_pos = ptq->pt->synth_opts.last_branch_sz;
1046 ptq->last_branch_pos -= 1;
1048 be = &bs->entries[ptq->last_branch_pos];
1049 be->from = state->from_ip;
1050 be->to = state->to_ip;
1051 be->flags.abort = !!(state->flags & INTEL_PT_ABORT_TX);
1052 be->flags.in_tx = !!(state->flags & INTEL_PT_IN_TX);
1053 /* No support for mispredict */
1054 be->flags.mispred = ptq->pt->mispred_all;
1056 if (bs->nr < ptq->pt->synth_opts.last_branch_sz)
1060 static inline bool intel_pt_skip_event(struct intel_pt *pt)
1062 return pt->synth_opts.initial_skip &&
1063 pt->num_events++ < pt->synth_opts.initial_skip;
1066 static void intel_pt_prep_b_sample(struct intel_pt *pt,
1067 struct intel_pt_queue *ptq,
1068 union perf_event *event,
1069 struct perf_sample *sample)
1071 event->sample.header.type = PERF_RECORD_SAMPLE;
1072 event->sample.header.misc = PERF_RECORD_MISC_USER;
1073 event->sample.header.size = sizeof(struct perf_event_header);
1075 if (!pt->timeless_decoding)
1076 sample->time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
1078 sample->cpumode = PERF_RECORD_MISC_USER;
1079 sample->ip = ptq->state->from_ip;
1080 sample->pid = ptq->pid;
1081 sample->tid = ptq->tid;
1082 sample->addr = ptq->state->to_ip;
1084 sample->cpu = ptq->cpu;
1085 sample->flags = ptq->flags;
1086 sample->insn_len = ptq->insn_len;
1087 memcpy(sample->insn, ptq->insn, INTEL_PT_INSN_BUF_SZ);
1090 static int intel_pt_inject_event(union perf_event *event,
1091 struct perf_sample *sample, u64 type,
1094 event->header.size = perf_event__sample_event_size(sample, type, 0);
1095 return perf_event__synthesize_sample(event, type, 0, sample, swapped);
1098 static inline int intel_pt_opt_inject(struct intel_pt *pt,
1099 union perf_event *event,
1100 struct perf_sample *sample, u64 type)
1102 if (!pt->synth_opts.inject)
1105 return intel_pt_inject_event(event, sample, type, pt->synth_needs_swap);
1108 static int intel_pt_deliver_synth_b_event(struct intel_pt *pt,
1109 union perf_event *event,
1110 struct perf_sample *sample, u64 type)
1114 ret = intel_pt_opt_inject(pt, event, sample, type);
1118 ret = perf_session__deliver_synth_event(pt->session, event, sample);
1120 pr_err("Intel PT: failed to deliver event, error %d\n", ret);
1125 static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq)
1127 struct intel_pt *pt = ptq->pt;
1128 union perf_event *event = ptq->event_buf;
1129 struct perf_sample sample = { .ip = 0, };
1130 struct dummy_branch_stack {
1132 struct branch_entry entries;
1135 if (pt->branches_filter && !(pt->branches_filter & ptq->flags))
1138 if (intel_pt_skip_event(pt))
1141 intel_pt_prep_b_sample(pt, ptq, event, &sample);
1143 sample.id = ptq->pt->branches_id;
1144 sample.stream_id = ptq->pt->branches_id;
1147 * perf report cannot handle events without a branch stack when using
1148 * SORT_MODE__BRANCH so make a dummy one.
1150 if (pt->synth_opts.last_branch && sort__mode == SORT_MODE__BRANCH) {
1151 dummy_bs = (struct dummy_branch_stack){
1158 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1161 return intel_pt_deliver_synth_b_event(pt, event, &sample,
1162 pt->branches_sample_type);
1165 static void intel_pt_prep_sample(struct intel_pt *pt,
1166 struct intel_pt_queue *ptq,
1167 union perf_event *event,
1168 struct perf_sample *sample)
1170 intel_pt_prep_b_sample(pt, ptq, event, sample);
1172 if (pt->synth_opts.callchain) {
1173 thread_stack__sample(ptq->thread, ptq->chain,
1174 pt->synth_opts.callchain_sz, sample->ip);
1175 sample->callchain = ptq->chain;
1178 if (pt->synth_opts.last_branch) {
1179 intel_pt_copy_last_branch_rb(ptq);
1180 sample->branch_stack = ptq->last_branch;
1184 static inline int intel_pt_deliver_synth_event(struct intel_pt *pt,
1185 struct intel_pt_queue *ptq,
1186 union perf_event *event,
1187 struct perf_sample *sample,
1192 ret = intel_pt_deliver_synth_b_event(pt, event, sample, type);
1194 if (pt->synth_opts.last_branch)
1195 intel_pt_reset_last_branch_rb(ptq);
1200 static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq)
1202 struct intel_pt *pt = ptq->pt;
1203 union perf_event *event = ptq->event_buf;
1204 struct perf_sample sample = { .ip = 0, };
1206 if (intel_pt_skip_event(pt))
1209 intel_pt_prep_sample(pt, ptq, event, &sample);
1211 sample.id = ptq->pt->instructions_id;
1212 sample.stream_id = ptq->pt->instructions_id;
1213 sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt;
1215 ptq->last_insn_cnt = ptq->state->tot_insn_cnt;
1217 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1218 pt->instructions_sample_type);
1221 static int intel_pt_synth_transaction_sample(struct intel_pt_queue *ptq)
1223 struct intel_pt *pt = ptq->pt;
1224 union perf_event *event = ptq->event_buf;
1225 struct perf_sample sample = { .ip = 0, };
1227 if (intel_pt_skip_event(pt))
1230 intel_pt_prep_sample(pt, ptq, event, &sample);
1232 sample.id = ptq->pt->transactions_id;
1233 sample.stream_id = ptq->pt->transactions_id;
1235 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1236 pt->transactions_sample_type);
1239 static int intel_pt_synth_error(struct intel_pt *pt, int code, int cpu,
1240 pid_t pid, pid_t tid, u64 ip)
1242 union perf_event event;
1243 char msg[MAX_AUXTRACE_ERROR_MSG];
1246 intel_pt__strerror(code, msg, MAX_AUXTRACE_ERROR_MSG);
1248 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
1249 code, cpu, pid, tid, ip, msg);
1251 err = perf_session__deliver_synth_event(pt->session, &event, NULL);
1253 pr_err("Intel Processor Trace: failed to deliver error event, error %d\n",
1259 static int intel_pt_next_tid(struct intel_pt *pt, struct intel_pt_queue *ptq)
1261 struct auxtrace_queue *queue;
1262 pid_t tid = ptq->next_tid;
1268 intel_pt_log("switch: cpu %d tid %d\n", ptq->cpu, tid);
1270 err = machine__set_current_tid(pt->machine, ptq->cpu, -1, tid);
1272 queue = &pt->queues.queue_array[ptq->queue_nr];
1273 intel_pt_set_pid_tid_cpu(pt, queue);
1280 static inline bool intel_pt_is_switch_ip(struct intel_pt_queue *ptq, u64 ip)
1282 struct intel_pt *pt = ptq->pt;
1284 return ip == pt->switch_ip &&
1285 (ptq->flags & PERF_IP_FLAG_BRANCH) &&
1286 !(ptq->flags & (PERF_IP_FLAG_CONDITIONAL | PERF_IP_FLAG_ASYNC |
1287 PERF_IP_FLAG_INTERRUPT | PERF_IP_FLAG_TX_ABORT));
1290 static int intel_pt_sample(struct intel_pt_queue *ptq)
1292 const struct intel_pt_state *state = ptq->state;
1293 struct intel_pt *pt = ptq->pt;
1296 if (!ptq->have_sample)
1299 ptq->have_sample = false;
1301 if (pt->sample_instructions && (state->type & INTEL_PT_INSTRUCTION)) {
1302 err = intel_pt_synth_instruction_sample(ptq);
1307 if (pt->sample_transactions && (state->type & INTEL_PT_TRANSACTION)) {
1308 err = intel_pt_synth_transaction_sample(ptq);
1313 if (!(state->type & INTEL_PT_BRANCH))
1316 if (pt->synth_opts.callchain || pt->synth_opts.thread_stack)
1317 thread_stack__event(ptq->thread, ptq->flags, state->from_ip,
1318 state->to_ip, ptq->insn_len,
1321 thread_stack__set_trace_nr(ptq->thread, state->trace_nr);
1323 if (pt->sample_branches) {
1324 err = intel_pt_synth_branch_sample(ptq);
1329 if (pt->synth_opts.last_branch)
1330 intel_pt_update_last_branch_rb(ptq);
1332 if (!pt->sync_switch)
1335 if (intel_pt_is_switch_ip(ptq, state->to_ip)) {
1336 switch (ptq->switch_state) {
1337 case INTEL_PT_SS_UNKNOWN:
1338 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
1339 err = intel_pt_next_tid(pt, ptq);
1342 ptq->switch_state = INTEL_PT_SS_TRACING;
1345 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_EVENT;
1348 } else if (!state->to_ip) {
1349 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
1350 } else if (ptq->switch_state == INTEL_PT_SS_NOT_TRACING) {
1351 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
1352 } else if (ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
1353 state->to_ip == pt->ptss_ip &&
1354 (ptq->flags & PERF_IP_FLAG_CALL)) {
1355 ptq->switch_state = INTEL_PT_SS_TRACING;
1361 static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)
1363 struct machine *machine = pt->machine;
1365 struct symbol *sym, *start;
1366 u64 ip, switch_ip = 0;
1372 map = machine__kernel_map(machine);
1379 start = dso__first_symbol(map->dso, MAP__FUNCTION);
1381 for (sym = start; sym; sym = dso__next_symbol(sym)) {
1382 if (sym->binding == STB_GLOBAL &&
1383 !strcmp(sym->name, "__switch_to")) {
1384 ip = map->unmap_ip(map, sym->start);
1385 if (ip >= map->start && ip < map->end) {
1392 if (!switch_ip || !ptss_ip)
1395 if (pt->have_sched_switch == 1)
1396 ptss = "perf_trace_sched_switch";
1398 ptss = "__perf_event_task_sched_out";
1400 for (sym = start; sym; sym = dso__next_symbol(sym)) {
1401 if (!strcmp(sym->name, ptss)) {
1402 ip = map->unmap_ip(map, sym->start);
1403 if (ip >= map->start && ip < map->end) {
1413 static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp)
1415 const struct intel_pt_state *state = ptq->state;
1416 struct intel_pt *pt = ptq->pt;
1419 if (!pt->kernel_start) {
1420 pt->kernel_start = machine__kernel_start(pt->machine);
1421 if (pt->per_cpu_mmaps &&
1422 (pt->have_sched_switch == 1 || pt->have_sched_switch == 3) &&
1423 !pt->timeless_decoding && intel_pt_tracing_kernel(pt) &&
1424 !pt->sampling_mode) {
1425 pt->switch_ip = intel_pt_switch_ip(pt, &pt->ptss_ip);
1426 if (pt->switch_ip) {
1427 intel_pt_log("switch_ip: %"PRIx64" ptss_ip: %"PRIx64"\n",
1428 pt->switch_ip, pt->ptss_ip);
1429 pt->sync_switch = true;
1434 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
1435 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
1437 err = intel_pt_sample(ptq);
1441 state = intel_pt_decode(ptq->decoder);
1443 if (state->err == INTEL_PT_ERR_NODATA)
1445 if (pt->sync_switch &&
1446 state->from_ip >= pt->kernel_start) {
1447 pt->sync_switch = false;
1448 intel_pt_next_tid(pt, ptq);
1450 if (pt->synth_opts.errors) {
1451 err = intel_pt_synth_error(pt, state->err,
1462 ptq->have_sample = true;
1463 intel_pt_sample_flags(ptq);
1465 /* Use estimated TSC upon return to user space */
1467 (state->from_ip >= pt->kernel_start || !state->from_ip) &&
1468 state->to_ip && state->to_ip < pt->kernel_start) {
1469 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
1470 state->timestamp, state->est_timestamp);
1471 ptq->timestamp = state->est_timestamp;
1472 /* Use estimated TSC in unknown switch state */
1473 } else if (pt->sync_switch &&
1474 ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
1475 intel_pt_is_switch_ip(ptq, state->to_ip) &&
1476 ptq->next_tid == -1) {
1477 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
1478 state->timestamp, state->est_timestamp);
1479 ptq->timestamp = state->est_timestamp;
1480 } else if (state->timestamp > ptq->timestamp) {
1481 ptq->timestamp = state->timestamp;
1484 if (!pt->timeless_decoding && ptq->timestamp >= *timestamp) {
1485 *timestamp = ptq->timestamp;
1492 static inline int intel_pt_update_queues(struct intel_pt *pt)
1494 if (pt->queues.new_data) {
1495 pt->queues.new_data = false;
1496 return intel_pt_setup_queues(pt);
1501 static int intel_pt_process_queues(struct intel_pt *pt, u64 timestamp)
1503 unsigned int queue_nr;
1508 struct auxtrace_queue *queue;
1509 struct intel_pt_queue *ptq;
1511 if (!pt->heap.heap_cnt)
1514 if (pt->heap.heap_array[0].ordinal >= timestamp)
1517 queue_nr = pt->heap.heap_array[0].queue_nr;
1518 queue = &pt->queues.queue_array[queue_nr];
1521 intel_pt_log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n",
1522 queue_nr, pt->heap.heap_array[0].ordinal,
1525 auxtrace_heap__pop(&pt->heap);
1527 if (pt->heap.heap_cnt) {
1528 ts = pt->heap.heap_array[0].ordinal + 1;
1535 intel_pt_set_pid_tid_cpu(pt, queue);
1537 ret = intel_pt_run_decoder(ptq, &ts);
1540 auxtrace_heap__add(&pt->heap, queue_nr, ts);
1545 ret = auxtrace_heap__add(&pt->heap, queue_nr, ts);
1549 ptq->on_heap = false;
1556 static int intel_pt_process_timeless_queues(struct intel_pt *pt, pid_t tid,
1559 struct auxtrace_queues *queues = &pt->queues;
1563 for (i = 0; i < queues->nr_queues; i++) {
1564 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
1565 struct intel_pt_queue *ptq = queue->priv;
1567 if (ptq && (tid == -1 || ptq->tid == tid)) {
1569 intel_pt_set_pid_tid_cpu(pt, queue);
1570 intel_pt_run_decoder(ptq, &ts);
1576 static int intel_pt_lost(struct intel_pt *pt, struct perf_sample *sample)
1578 return intel_pt_synth_error(pt, INTEL_PT_ERR_LOST, sample->cpu,
1579 sample->pid, sample->tid, 0);
1582 static struct intel_pt_queue *intel_pt_cpu_to_ptq(struct intel_pt *pt, int cpu)
1586 if (cpu < 0 || !pt->queues.nr_queues)
1589 if ((unsigned)cpu >= pt->queues.nr_queues)
1590 i = pt->queues.nr_queues - 1;
1594 if (pt->queues.queue_array[i].cpu == cpu)
1595 return pt->queues.queue_array[i].priv;
1597 for (j = 0; i > 0; j++) {
1598 if (pt->queues.queue_array[--i].cpu == cpu)
1599 return pt->queues.queue_array[i].priv;
1602 for (; j < pt->queues.nr_queues; j++) {
1603 if (pt->queues.queue_array[j].cpu == cpu)
1604 return pt->queues.queue_array[j].priv;
1610 static int intel_pt_sync_switch(struct intel_pt *pt, int cpu, pid_t tid,
1613 struct intel_pt_queue *ptq;
1616 if (!pt->sync_switch)
1619 ptq = intel_pt_cpu_to_ptq(pt, cpu);
1623 switch (ptq->switch_state) {
1624 case INTEL_PT_SS_NOT_TRACING:
1627 case INTEL_PT_SS_UNKNOWN:
1628 case INTEL_PT_SS_TRACING:
1629 ptq->next_tid = tid;
1630 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_IP;
1632 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
1633 if (!ptq->on_heap) {
1634 ptq->timestamp = perf_time_to_tsc(timestamp,
1636 err = auxtrace_heap__add(&pt->heap, ptq->queue_nr,
1640 ptq->on_heap = true;
1642 ptq->switch_state = INTEL_PT_SS_TRACING;
1644 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
1645 ptq->next_tid = tid;
1646 intel_pt_log("ERROR: cpu %d expecting switch ip\n", cpu);
1655 static int intel_pt_process_switch(struct intel_pt *pt,
1656 struct perf_sample *sample)
1658 struct perf_evsel *evsel;
1662 evsel = perf_evlist__id2evsel(pt->session->evlist, sample->id);
1663 if (evsel != pt->switch_evsel)
1666 tid = perf_evsel__intval(evsel, sample, "next_pid");
1669 intel_pt_log("sched_switch: cpu %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
1670 cpu, tid, sample->time, perf_time_to_tsc(sample->time,
1673 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
1677 return machine__set_current_tid(pt->machine, cpu, -1, tid);
1680 static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
1681 struct perf_sample *sample)
1683 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1689 if (pt->have_sched_switch == 3) {
1692 if (event->header.type != PERF_RECORD_SWITCH_CPU_WIDE) {
1693 pr_err("Expecting CPU-wide context switch event\n");
1696 pid = event->context_switch.next_prev_pid;
1697 tid = event->context_switch.next_prev_tid;
1706 pr_err("context_switch event has no tid\n");
1710 intel_pt_log("context_switch: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
1711 cpu, pid, tid, sample->time, perf_time_to_tsc(sample->time,
1714 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
1718 return machine__set_current_tid(pt->machine, cpu, pid, tid);
1721 static int intel_pt_process_itrace_start(struct intel_pt *pt,
1722 union perf_event *event,
1723 struct perf_sample *sample)
1725 if (!pt->per_cpu_mmaps)
1728 intel_pt_log("itrace_start: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
1729 sample->cpu, event->itrace_start.pid,
1730 event->itrace_start.tid, sample->time,
1731 perf_time_to_tsc(sample->time, &pt->tc));
1733 return machine__set_current_tid(pt->machine, sample->cpu,
1734 event->itrace_start.pid,
1735 event->itrace_start.tid);
1738 static int intel_pt_process_event(struct perf_session *session,
1739 union perf_event *event,
1740 struct perf_sample *sample,
1741 struct perf_tool *tool)
1743 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1751 if (!tool->ordered_events) {
1752 pr_err("Intel Processor Trace requires ordered events\n");
1756 if (sample->time && sample->time != (u64)-1)
1757 timestamp = perf_time_to_tsc(sample->time, &pt->tc);
1761 if (timestamp || pt->timeless_decoding) {
1762 err = intel_pt_update_queues(pt);
1767 if (pt->timeless_decoding) {
1768 if (event->header.type == PERF_RECORD_EXIT) {
1769 err = intel_pt_process_timeless_queues(pt,
1773 } else if (timestamp) {
1774 err = intel_pt_process_queues(pt, timestamp);
1779 if (event->header.type == PERF_RECORD_AUX &&
1780 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
1781 pt->synth_opts.errors) {
1782 err = intel_pt_lost(pt, sample);
1787 if (pt->switch_evsel && event->header.type == PERF_RECORD_SAMPLE)
1788 err = intel_pt_process_switch(pt, sample);
1789 else if (event->header.type == PERF_RECORD_ITRACE_START)
1790 err = intel_pt_process_itrace_start(pt, event, sample);
1791 else if (event->header.type == PERF_RECORD_SWITCH ||
1792 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
1793 err = intel_pt_context_switch(pt, event, sample);
1795 intel_pt_log("event %s (%u): cpu %d time %"PRIu64" tsc %#"PRIx64"\n",
1796 perf_event__name(event->header.type), event->header.type,
1797 sample->cpu, sample->time, timestamp);
1802 static int intel_pt_flush(struct perf_session *session, struct perf_tool *tool)
1804 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1811 if (!tool->ordered_events)
1814 ret = intel_pt_update_queues(pt);
1818 if (pt->timeless_decoding)
1819 return intel_pt_process_timeless_queues(pt, -1,
1822 return intel_pt_process_queues(pt, MAX_TIMESTAMP);
1825 static void intel_pt_free_events(struct perf_session *session)
1827 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1829 struct auxtrace_queues *queues = &pt->queues;
1832 for (i = 0; i < queues->nr_queues; i++) {
1833 intel_pt_free_queue(queues->queue_array[i].priv);
1834 queues->queue_array[i].priv = NULL;
1836 intel_pt_log_disable();
1837 auxtrace_queues__free(queues);
1840 static void intel_pt_free(struct perf_session *session)
1842 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1845 auxtrace_heap__free(&pt->heap);
1846 intel_pt_free_events(session);
1847 session->auxtrace = NULL;
1848 thread__put(pt->unknown_thread);
1849 addr_filters__exit(&pt->filts);
1854 static int intel_pt_process_auxtrace_event(struct perf_session *session,
1855 union perf_event *event,
1856 struct perf_tool *tool __maybe_unused)
1858 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1861 if (pt->sampling_mode)
1864 if (!pt->data_queued) {
1865 struct auxtrace_buffer *buffer;
1867 int fd = perf_data_file__fd(session->file);
1870 if (perf_data_file__is_pipe(session->file)) {
1873 data_offset = lseek(fd, 0, SEEK_CUR);
1874 if (data_offset == -1)
1878 err = auxtrace_queues__add_event(&pt->queues, session, event,
1879 data_offset, &buffer);
1883 /* Dump here now we have copied a piped trace out of the pipe */
1885 if (auxtrace_buffer__get_data(buffer, fd)) {
1886 intel_pt_dump_event(pt, buffer->data,
1888 auxtrace_buffer__put_data(buffer);
1896 struct intel_pt_synth {
1897 struct perf_tool dummy_tool;
1898 struct perf_session *session;
1901 static int intel_pt_event_synth(struct perf_tool *tool,
1902 union perf_event *event,
1903 struct perf_sample *sample __maybe_unused,
1904 struct machine *machine __maybe_unused)
1906 struct intel_pt_synth *intel_pt_synth =
1907 container_of(tool, struct intel_pt_synth, dummy_tool);
1909 return perf_session__deliver_synth_event(intel_pt_synth->session, event,
1913 static int intel_pt_synth_event(struct perf_session *session, const char *name,
1914 struct perf_event_attr *attr, u64 id)
1916 struct intel_pt_synth intel_pt_synth;
1919 pr_debug("Synthesizing '%s' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
1920 name, id, (u64)attr->sample_type);
1922 memset(&intel_pt_synth, 0, sizeof(struct intel_pt_synth));
1923 intel_pt_synth.session = session;
1925 err = perf_event__synthesize_attr(&intel_pt_synth.dummy_tool, attr, 1,
1926 &id, intel_pt_event_synth);
1928 pr_err("%s: failed to synthesize '%s' event type\n",
1934 static void intel_pt_set_event_name(struct perf_evlist *evlist, u64 id,
1937 struct perf_evsel *evsel;
1939 evlist__for_each_entry(evlist, evsel) {
1940 if (evsel->id && evsel->id[0] == id) {
1942 zfree(&evsel->name);
1943 evsel->name = strdup(name);
1949 static struct perf_evsel *intel_pt_evsel(struct intel_pt *pt,
1950 struct perf_evlist *evlist)
1952 struct perf_evsel *evsel;
1954 evlist__for_each_entry(evlist, evsel) {
1955 if (evsel->attr.type == pt->pmu_type && evsel->ids)
1962 static int intel_pt_synth_events(struct intel_pt *pt,
1963 struct perf_session *session)
1965 struct perf_evlist *evlist = session->evlist;
1966 struct perf_evsel *evsel = intel_pt_evsel(pt, evlist);
1967 struct perf_event_attr attr;
1972 pr_debug("There are no selected events with Intel Processor Trace data\n");
1976 memset(&attr, 0, sizeof(struct perf_event_attr));
1977 attr.size = sizeof(struct perf_event_attr);
1978 attr.type = PERF_TYPE_HARDWARE;
1979 attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
1980 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1982 if (pt->timeless_decoding)
1983 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1985 attr.sample_type |= PERF_SAMPLE_TIME;
1986 if (!pt->per_cpu_mmaps)
1987 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
1988 attr.exclude_user = evsel->attr.exclude_user;
1989 attr.exclude_kernel = evsel->attr.exclude_kernel;
1990 attr.exclude_hv = evsel->attr.exclude_hv;
1991 attr.exclude_host = evsel->attr.exclude_host;
1992 attr.exclude_guest = evsel->attr.exclude_guest;
1993 attr.sample_id_all = evsel->attr.sample_id_all;
1994 attr.read_format = evsel->attr.read_format;
1996 id = evsel->id[0] + 1000000000;
2000 if (pt->synth_opts.instructions) {
2001 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
2002 if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS)
2003 attr.sample_period =
2004 intel_pt_ns_to_ticks(pt, pt->synth_opts.period);
2006 attr.sample_period = pt->synth_opts.period;
2007 if (pt->synth_opts.callchain)
2008 attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
2009 if (pt->synth_opts.last_branch)
2010 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
2011 err = intel_pt_synth_event(session, "instructions", &attr, id);
2014 pt->sample_instructions = true;
2015 pt->instructions_sample_type = attr.sample_type;
2016 pt->instructions_id = id;
2020 if (pt->synth_opts.transactions) {
2021 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
2022 attr.sample_period = 1;
2023 if (pt->synth_opts.callchain)
2024 attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
2025 if (pt->synth_opts.last_branch)
2026 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
2027 err = intel_pt_synth_event(session, "transactions", &attr, id);
2030 pt->sample_transactions = true;
2031 pt->transactions_sample_type = attr.sample_type;
2032 pt->transactions_id = id;
2033 intel_pt_set_event_name(evlist, id, "transactions");
2037 if (pt->synth_opts.branches) {
2038 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
2039 attr.sample_period = 1;
2040 attr.sample_type |= PERF_SAMPLE_ADDR;
2041 attr.sample_type &= ~(u64)PERF_SAMPLE_CALLCHAIN;
2042 attr.sample_type &= ~(u64)PERF_SAMPLE_BRANCH_STACK;
2043 err = intel_pt_synth_event(session, "branches", &attr, id);
2046 pt->sample_branches = true;
2047 pt->branches_sample_type = attr.sample_type;
2048 pt->branches_id = id;
2051 pt->synth_needs_swap = evsel->needs_swap;
2056 static struct perf_evsel *intel_pt_find_sched_switch(struct perf_evlist *evlist)
2058 struct perf_evsel *evsel;
2060 evlist__for_each_entry_reverse(evlist, evsel) {
2061 const char *name = perf_evsel__name(evsel);
2063 if (!strcmp(name, "sched:sched_switch"))
2070 static bool intel_pt_find_switch(struct perf_evlist *evlist)
2072 struct perf_evsel *evsel;
2074 evlist__for_each_entry(evlist, evsel) {
2075 if (evsel->attr.context_switch)
2082 static int intel_pt_perf_config(const char *var, const char *value, void *data)
2084 struct intel_pt *pt = data;
2086 if (!strcmp(var, "intel-pt.mispred-all"))
2087 pt->mispred_all = perf_config_bool(var, value);
2092 static const char * const intel_pt_info_fmts[] = {
2093 [INTEL_PT_PMU_TYPE] = " PMU Type %"PRId64"\n",
2094 [INTEL_PT_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
2095 [INTEL_PT_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
2096 [INTEL_PT_TIME_ZERO] = " Time Zero %"PRIu64"\n",
2097 [INTEL_PT_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
2098 [INTEL_PT_TSC_BIT] = " TSC bit %#"PRIx64"\n",
2099 [INTEL_PT_NORETCOMP_BIT] = " NoRETComp bit %#"PRIx64"\n",
2100 [INTEL_PT_HAVE_SCHED_SWITCH] = " Have sched_switch %"PRId64"\n",
2101 [INTEL_PT_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
2102 [INTEL_PT_PER_CPU_MMAPS] = " Per-cpu maps %"PRId64"\n",
2103 [INTEL_PT_MTC_BIT] = " MTC bit %#"PRIx64"\n",
2104 [INTEL_PT_TSC_CTC_N] = " TSC:CTC numerator %"PRIu64"\n",
2105 [INTEL_PT_TSC_CTC_D] = " TSC:CTC denominator %"PRIu64"\n",
2106 [INTEL_PT_CYC_BIT] = " CYC bit %#"PRIx64"\n",
2107 [INTEL_PT_MAX_NONTURBO_RATIO] = " Max non-turbo ratio %"PRIu64"\n",
2108 [INTEL_PT_FILTER_STR_LEN] = " Filter string len. %"PRIu64"\n",
2111 static void intel_pt_print_info(u64 *arr, int start, int finish)
2118 for (i = start; i <= finish; i++)
2119 fprintf(stdout, intel_pt_info_fmts[i], arr[i]);
2122 static void intel_pt_print_info_str(const char *name, const char *str)
2127 fprintf(stdout, " %-20s%s\n", name, str ? str : "");
2130 static bool intel_pt_has(struct auxtrace_info_event *auxtrace_info, int pos)
2132 return auxtrace_info->header.size >=
2133 sizeof(struct auxtrace_info_event) + (sizeof(u64) * (pos + 1));
2136 int intel_pt_process_auxtrace_info(union perf_event *event,
2137 struct perf_session *session)
2139 struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
2140 size_t min_sz = sizeof(u64) * INTEL_PT_PER_CPU_MMAPS;
2141 struct intel_pt *pt;
2146 if (auxtrace_info->header.size < sizeof(struct auxtrace_info_event) +
2150 pt = zalloc(sizeof(struct intel_pt));
2154 addr_filters__init(&pt->filts);
2156 err = perf_config(intel_pt_perf_config, pt);
2160 err = auxtrace_queues__init(&pt->queues);
2164 intel_pt_log_set_name(INTEL_PT_PMU_NAME);
2166 pt->session = session;
2167 pt->machine = &session->machines.host; /* No kvm support */
2168 pt->auxtrace_type = auxtrace_info->type;
2169 pt->pmu_type = auxtrace_info->priv[INTEL_PT_PMU_TYPE];
2170 pt->tc.time_shift = auxtrace_info->priv[INTEL_PT_TIME_SHIFT];
2171 pt->tc.time_mult = auxtrace_info->priv[INTEL_PT_TIME_MULT];
2172 pt->tc.time_zero = auxtrace_info->priv[INTEL_PT_TIME_ZERO];
2173 pt->cap_user_time_zero = auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO];
2174 pt->tsc_bit = auxtrace_info->priv[INTEL_PT_TSC_BIT];
2175 pt->noretcomp_bit = auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT];
2176 pt->have_sched_switch = auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH];
2177 pt->snapshot_mode = auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE];
2178 pt->per_cpu_mmaps = auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS];
2179 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_PMU_TYPE,
2180 INTEL_PT_PER_CPU_MMAPS);
2182 if (intel_pt_has(auxtrace_info, INTEL_PT_CYC_BIT)) {
2183 pt->mtc_bit = auxtrace_info->priv[INTEL_PT_MTC_BIT];
2184 pt->mtc_freq_bits = auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS];
2185 pt->tsc_ctc_ratio_n = auxtrace_info->priv[INTEL_PT_TSC_CTC_N];
2186 pt->tsc_ctc_ratio_d = auxtrace_info->priv[INTEL_PT_TSC_CTC_D];
2187 pt->cyc_bit = auxtrace_info->priv[INTEL_PT_CYC_BIT];
2188 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_MTC_BIT,
2192 if (intel_pt_has(auxtrace_info, INTEL_PT_MAX_NONTURBO_RATIO)) {
2193 pt->max_non_turbo_ratio =
2194 auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO];
2195 intel_pt_print_info(&auxtrace_info->priv[0],
2196 INTEL_PT_MAX_NONTURBO_RATIO,
2197 INTEL_PT_MAX_NONTURBO_RATIO);
2200 info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
2201 info_end = (void *)info + auxtrace_info->header.size;
2203 if (intel_pt_has(auxtrace_info, INTEL_PT_FILTER_STR_LEN)) {
2206 len = auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN];
2207 intel_pt_print_info(&auxtrace_info->priv[0],
2208 INTEL_PT_FILTER_STR_LEN,
2209 INTEL_PT_FILTER_STR_LEN);
2211 const char *filter = (const char *)info;
2213 len = roundup(len + 1, 8);
2215 if ((void *)info > info_end) {
2216 pr_err("%s: bad filter string length\n", __func__);
2218 goto err_free_queues;
2220 pt->filter = memdup(filter, len);
2223 goto err_free_queues;
2225 if (session->header.needs_swap)
2226 mem_bswap_64(pt->filter, len);
2227 if (pt->filter[len - 1]) {
2228 pr_err("%s: filter string not null terminated\n", __func__);
2230 goto err_free_queues;
2232 err = addr_filters__parse_bare_filter(&pt->filts,
2235 goto err_free_queues;
2237 intel_pt_print_info_str("Filter string", pt->filter);
2240 pt->timeless_decoding = intel_pt_timeless_decoding(pt);
2241 pt->have_tsc = intel_pt_have_tsc(pt);
2242 pt->sampling_mode = false;
2243 pt->est_tsc = !pt->timeless_decoding;
2245 pt->unknown_thread = thread__new(999999999, 999999999);
2246 if (!pt->unknown_thread) {
2248 goto err_free_queues;
2252 * Since this thread will not be kept in any rbtree not in a
2253 * list, initialize its list node so that at thread__put() the
2254 * current thread lifetime assuption is kept and we don't segfault
2255 * at list_del_init().
2257 INIT_LIST_HEAD(&pt->unknown_thread->node);
2259 err = thread__set_comm(pt->unknown_thread, "unknown", 0);
2261 goto err_delete_thread;
2262 if (thread__init_map_groups(pt->unknown_thread, pt->machine)) {
2264 goto err_delete_thread;
2267 pt->auxtrace.process_event = intel_pt_process_event;
2268 pt->auxtrace.process_auxtrace_event = intel_pt_process_auxtrace_event;
2269 pt->auxtrace.flush_events = intel_pt_flush;
2270 pt->auxtrace.free_events = intel_pt_free_events;
2271 pt->auxtrace.free = intel_pt_free;
2272 session->auxtrace = &pt->auxtrace;
2277 if (pt->have_sched_switch == 1) {
2278 pt->switch_evsel = intel_pt_find_sched_switch(session->evlist);
2279 if (!pt->switch_evsel) {
2280 pr_err("%s: missing sched_switch event\n", __func__);
2282 goto err_delete_thread;
2284 } else if (pt->have_sched_switch == 2 &&
2285 !intel_pt_find_switch(session->evlist)) {
2286 pr_err("%s: missing context_switch attribute flag\n", __func__);
2288 goto err_delete_thread;
2291 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
2292 pt->synth_opts = *session->itrace_synth_opts;
2294 itrace_synth_opts__set_default(&pt->synth_opts);
2295 if (use_browser != -1) {
2296 pt->synth_opts.branches = false;
2297 pt->synth_opts.callchain = true;
2299 if (session->itrace_synth_opts)
2300 pt->synth_opts.thread_stack =
2301 session->itrace_synth_opts->thread_stack;
2304 if (pt->synth_opts.log)
2305 intel_pt_log_enable();
2307 /* Maximum non-turbo ratio is TSC freq / 100 MHz */
2308 if (pt->tc.time_mult) {
2309 u64 tsc_freq = intel_pt_ns_to_ticks(pt, 1000000000);
2311 if (!pt->max_non_turbo_ratio)
2312 pt->max_non_turbo_ratio =
2313 (tsc_freq + 50000000) / 100000000;
2314 intel_pt_log("TSC frequency %"PRIu64"\n", tsc_freq);
2315 intel_pt_log("Maximum non-turbo ratio %u\n",
2316 pt->max_non_turbo_ratio);
2319 if (pt->synth_opts.calls)
2320 pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
2321 PERF_IP_FLAG_TRACE_END;
2322 if (pt->synth_opts.returns)
2323 pt->branches_filter |= PERF_IP_FLAG_RETURN |
2324 PERF_IP_FLAG_TRACE_BEGIN;
2326 if (pt->synth_opts.callchain && !symbol_conf.use_callchain) {
2327 symbol_conf.use_callchain = true;
2328 if (callchain_register_param(&callchain_param) < 0) {
2329 symbol_conf.use_callchain = false;
2330 pt->synth_opts.callchain = false;
2334 err = intel_pt_synth_events(pt, session);
2336 goto err_delete_thread;
2338 err = auxtrace_queues__process_index(&pt->queues, session);
2340 goto err_delete_thread;
2342 if (pt->queues.populated)
2343 pt->data_queued = true;
2345 if (pt->timeless_decoding)
2346 pr_debug2("Intel PT decoding without timestamps\n");
2351 thread__zput(pt->unknown_thread);
2353 intel_pt_log_disable();
2354 auxtrace_queues__free(&pt->queues);
2355 session->auxtrace = NULL;
2357 addr_filters__exit(&pt->filts);