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
19 #include <linux/kernel.h>
20 #include <linux/types.h>
34 #include "thread-stack.h"
36 #include "callchain.h"
44 #include "intel-pt-decoder/intel-pt-log.h"
45 #include "intel-pt-decoder/intel-pt-decoder.h"
46 #include "intel-pt-decoder/intel-pt-insn-decoder.h"
47 #include "intel-pt-decoder/intel-pt-pkt-decoder.h"
49 #define MAX_TIMESTAMP (~0ULL)
52 struct auxtrace auxtrace;
53 struct auxtrace_queues queues;
54 struct auxtrace_heap heap;
56 struct perf_session *session;
57 struct machine *machine;
58 struct perf_evsel *switch_evsel;
59 struct thread *unknown_thread;
60 bool timeless_decoding;
69 int have_sched_switch;
75 struct perf_tsc_conversion tc;
76 bool cap_user_time_zero;
78 struct itrace_synth_opts synth_opts;
80 bool sample_instructions;
81 u64 instructions_sample_type;
82 u64 instructions_sample_period;
87 u64 branches_sample_type;
90 bool sample_transactions;
91 u64 transactions_sample_type;
94 bool synth_needs_swap;
103 unsigned max_non_turbo_ratio;
105 unsigned long num_events;
108 struct addr_filters filts;
112 INTEL_PT_SS_NOT_TRACING,
115 INTEL_PT_SS_EXPECTING_SWITCH_EVENT,
116 INTEL_PT_SS_EXPECTING_SWITCH_IP,
119 struct intel_pt_queue {
121 unsigned int queue_nr;
122 struct auxtrace_buffer *buffer;
124 const struct intel_pt_state *state;
125 struct ip_callchain *chain;
126 struct branch_stack *last_branch;
127 struct branch_stack *last_branch_rb;
128 size_t last_branch_pos;
129 union perf_event *event_buf;
132 bool step_through_buffers;
133 bool use_buffer_pid_tid;
138 struct thread *thread;
146 char insn[INTEL_PT_INSN_BUF_SZ];
149 static void intel_pt_dump(struct intel_pt *pt __maybe_unused,
150 unsigned char *buf, size_t len)
152 struct intel_pt_pkt packet;
155 char desc[INTEL_PT_PKT_DESC_MAX];
156 const char *color = PERF_COLOR_BLUE;
158 color_fprintf(stdout, color,
159 ". ... Intel Processor Trace data: size %zu bytes\n",
163 ret = intel_pt_get_packet(buf, len, &packet);
169 color_fprintf(stdout, color, " %08x: ", pos);
170 for (i = 0; i < pkt_len; i++)
171 color_fprintf(stdout, color, " %02x", buf[i]);
173 color_fprintf(stdout, color, " ");
175 ret = intel_pt_pkt_desc(&packet, desc,
176 INTEL_PT_PKT_DESC_MAX);
178 color_fprintf(stdout, color, " %s\n", desc);
180 color_fprintf(stdout, color, " Bad packet!\n");
188 static void intel_pt_dump_event(struct intel_pt *pt, unsigned char *buf,
192 intel_pt_dump(pt, buf, len);
195 static int intel_pt_do_fix_overlap(struct intel_pt *pt, struct auxtrace_buffer *a,
196 struct auxtrace_buffer *b)
200 start = intel_pt_find_overlap(a->data, a->size, b->data, b->size,
204 b->use_size = b->data + b->size - start;
209 static void intel_pt_use_buffer_pid_tid(struct intel_pt_queue *ptq,
210 struct auxtrace_queue *queue,
211 struct auxtrace_buffer *buffer)
213 if (queue->cpu == -1 && buffer->cpu != -1)
214 ptq->cpu = buffer->cpu;
216 ptq->pid = buffer->pid;
217 ptq->tid = buffer->tid;
219 intel_pt_log("queue %u cpu %d pid %d tid %d\n",
220 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
222 thread__zput(ptq->thread);
224 if (ptq->tid != -1) {
226 ptq->thread = machine__findnew_thread(ptq->pt->machine,
230 ptq->thread = machine__find_thread(ptq->pt->machine, -1,
235 /* This function assumes data is processed sequentially only */
236 static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data)
238 struct intel_pt_queue *ptq = data;
239 struct auxtrace_buffer *buffer = ptq->buffer, *old_buffer = buffer;
240 struct auxtrace_queue *queue;
247 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
249 buffer = auxtrace_buffer__next(queue, buffer);
252 auxtrace_buffer__drop_data(old_buffer);
257 ptq->buffer = buffer;
260 int fd = perf_data_file__fd(ptq->pt->session->file);
262 buffer->data = auxtrace_buffer__get_data(buffer, fd);
267 if (ptq->pt->snapshot_mode && !buffer->consecutive && old_buffer &&
268 intel_pt_do_fix_overlap(ptq->pt, old_buffer, buffer))
271 if (buffer->use_data) {
272 b->len = buffer->use_size;
273 b->buf = buffer->use_data;
275 b->len = buffer->size;
276 b->buf = buffer->data;
278 b->ref_timestamp = buffer->reference;
281 * If in snapshot mode and the buffer has no usable data, get next
282 * buffer and again check overlap against old_buffer.
284 if (ptq->pt->snapshot_mode && !b->len)
288 auxtrace_buffer__drop_data(old_buffer);
290 if (!old_buffer || ptq->pt->sampling_mode || (ptq->pt->snapshot_mode &&
291 !buffer->consecutive)) {
292 b->consecutive = false;
293 b->trace_nr = buffer->buffer_nr + 1;
295 b->consecutive = true;
298 if (ptq->use_buffer_pid_tid && (ptq->pid != buffer->pid ||
299 ptq->tid != buffer->tid))
300 intel_pt_use_buffer_pid_tid(ptq, queue, buffer);
302 if (ptq->step_through_buffers)
306 return intel_pt_get_trace(b, data);
311 struct intel_pt_cache_entry {
312 struct auxtrace_cache_entry entry;
315 enum intel_pt_insn_op op;
316 enum intel_pt_insn_branch branch;
319 char insn[INTEL_PT_INSN_BUF_SZ];
322 static int intel_pt_config_div(const char *var, const char *value, void *data)
327 if (!strcmp(var, "intel-pt.cache-divisor")) {
328 val = strtol(value, NULL, 0);
329 if (val > 0 && val <= INT_MAX)
336 static int intel_pt_cache_divisor(void)
343 perf_config(intel_pt_config_div, &d);
351 static unsigned int intel_pt_cache_size(struct dso *dso,
352 struct machine *machine)
356 size = dso__data_size(dso, machine);
357 size /= intel_pt_cache_divisor();
360 if (size > (1 << 21))
362 return 32 - __builtin_clz(size);
365 static struct auxtrace_cache *intel_pt_cache(struct dso *dso,
366 struct machine *machine)
368 struct auxtrace_cache *c;
371 if (dso->auxtrace_cache)
372 return dso->auxtrace_cache;
374 bits = intel_pt_cache_size(dso, machine);
376 /* Ignoring cache creation failure */
377 c = auxtrace_cache__new(bits, sizeof(struct intel_pt_cache_entry), 200);
379 dso->auxtrace_cache = c;
384 static int intel_pt_cache_add(struct dso *dso, struct machine *machine,
385 u64 offset, u64 insn_cnt, u64 byte_cnt,
386 struct intel_pt_insn *intel_pt_insn)
388 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
389 struct intel_pt_cache_entry *e;
395 e = auxtrace_cache__alloc_entry(c);
399 e->insn_cnt = insn_cnt;
400 e->byte_cnt = byte_cnt;
401 e->op = intel_pt_insn->op;
402 e->branch = intel_pt_insn->branch;
403 e->length = intel_pt_insn->length;
404 e->rel = intel_pt_insn->rel;
405 memcpy(e->insn, intel_pt_insn->buf, INTEL_PT_INSN_BUF_SZ);
407 err = auxtrace_cache__add(c, offset, &e->entry);
409 auxtrace_cache__free_entry(c, e);
414 static struct intel_pt_cache_entry *
415 intel_pt_cache_lookup(struct dso *dso, struct machine *machine, u64 offset)
417 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
422 return auxtrace_cache__lookup(dso->auxtrace_cache, offset);
425 static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
426 uint64_t *insn_cnt_ptr, uint64_t *ip,
427 uint64_t to_ip, uint64_t max_insn_cnt,
430 struct intel_pt_queue *ptq = data;
431 struct machine *machine = ptq->pt->machine;
432 struct thread *thread;
433 struct addr_location al;
434 unsigned char buf[INTEL_PT_INSN_BUF_SZ];
438 u64 offset, start_offset, start_ip;
442 intel_pt_insn->length = 0;
444 if (to_ip && *ip == to_ip)
447 if (*ip >= ptq->pt->kernel_start)
448 cpumode = PERF_RECORD_MISC_KERNEL;
450 cpumode = PERF_RECORD_MISC_USER;
452 thread = ptq->thread;
454 if (cpumode != PERF_RECORD_MISC_KERNEL)
456 thread = ptq->pt->unknown_thread;
460 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, *ip, &al);
461 if (!al.map || !al.map->dso)
464 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
465 dso__data_status_seen(al.map->dso,
466 DSO_DATA_STATUS_SEEN_ITRACE))
469 offset = al.map->map_ip(al.map, *ip);
471 if (!to_ip && one_map) {
472 struct intel_pt_cache_entry *e;
474 e = intel_pt_cache_lookup(al.map->dso, machine, offset);
476 (!max_insn_cnt || e->insn_cnt <= max_insn_cnt)) {
477 *insn_cnt_ptr = e->insn_cnt;
479 intel_pt_insn->op = e->op;
480 intel_pt_insn->branch = e->branch;
481 intel_pt_insn->length = e->length;
482 intel_pt_insn->rel = e->rel;
483 memcpy(intel_pt_insn->buf, e->insn,
484 INTEL_PT_INSN_BUF_SZ);
485 intel_pt_log_insn_no_data(intel_pt_insn, *ip);
490 start_offset = offset;
493 /* Load maps to ensure dso->is_64_bit has been updated */
496 x86_64 = al.map->dso->is_64_bit;
499 len = dso__data_read_offset(al.map->dso, machine,
501 INTEL_PT_INSN_BUF_SZ);
505 if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn))
508 intel_pt_log_insn(intel_pt_insn, *ip);
512 if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH)
515 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
518 *ip += intel_pt_insn->length;
520 if (to_ip && *ip == to_ip)
523 if (*ip >= al.map->end)
526 offset += intel_pt_insn->length;
531 *insn_cnt_ptr = insn_cnt;
537 * Didn't lookup in the 'to_ip' case, so do it now to prevent duplicate
541 struct intel_pt_cache_entry *e;
543 e = intel_pt_cache_lookup(al.map->dso, machine, start_offset);
548 /* Ignore cache errors */
549 intel_pt_cache_add(al.map->dso, machine, start_offset, insn_cnt,
550 *ip - start_ip, intel_pt_insn);
555 *insn_cnt_ptr = insn_cnt;
559 static bool intel_pt_match_pgd_ip(struct intel_pt *pt, uint64_t ip,
560 uint64_t offset, const char *filename)
562 struct addr_filter *filt;
563 bool have_filter = false;
564 bool hit_tracestop = false;
565 bool hit_filter = false;
567 list_for_each_entry(filt, &pt->filts.head, list) {
571 if ((filename && !filt->filename) ||
572 (!filename && filt->filename) ||
573 (filename && strcmp(filename, filt->filename)))
576 if (!(offset >= filt->addr && offset < filt->addr + filt->size))
579 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s hit filter: %s offset %#"PRIx64" size %#"PRIx64"\n",
580 ip, offset, filename ? filename : "[kernel]",
581 filt->start ? "filter" : "stop",
582 filt->addr, filt->size);
587 hit_tracestop = true;
590 if (!hit_tracestop && !hit_filter)
591 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s is not in a filter region\n",
592 ip, offset, filename ? filename : "[kernel]");
594 return hit_tracestop || (have_filter && !hit_filter);
597 static int __intel_pt_pgd_ip(uint64_t ip, void *data)
599 struct intel_pt_queue *ptq = data;
600 struct thread *thread;
601 struct addr_location al;
605 if (ip >= ptq->pt->kernel_start)
606 return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL);
608 cpumode = PERF_RECORD_MISC_USER;
610 thread = ptq->thread;
614 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, ip, &al);
615 if (!al.map || !al.map->dso)
618 offset = al.map->map_ip(al.map, ip);
620 return intel_pt_match_pgd_ip(ptq->pt, ip, offset,
621 al.map->dso->long_name);
624 static bool intel_pt_pgd_ip(uint64_t ip, void *data)
626 return __intel_pt_pgd_ip(ip, data) > 0;
629 static bool intel_pt_get_config(struct intel_pt *pt,
630 struct perf_event_attr *attr, u64 *config)
632 if (attr->type == pt->pmu_type) {
634 *config = attr->config;
641 static bool intel_pt_exclude_kernel(struct intel_pt *pt)
643 struct perf_evsel *evsel;
645 evlist__for_each_entry(pt->session->evlist, evsel) {
646 if (intel_pt_get_config(pt, &evsel->attr, NULL) &&
647 !evsel->attr.exclude_kernel)
653 static bool intel_pt_return_compression(struct intel_pt *pt)
655 struct perf_evsel *evsel;
658 if (!pt->noretcomp_bit)
661 evlist__for_each_entry(pt->session->evlist, evsel) {
662 if (intel_pt_get_config(pt, &evsel->attr, &config) &&
663 (config & pt->noretcomp_bit))
669 static unsigned int intel_pt_mtc_period(struct intel_pt *pt)
671 struct perf_evsel *evsel;
675 if (!pt->mtc_freq_bits)
678 for (shift = 0, config = pt->mtc_freq_bits; !(config & 1); shift++)
681 evlist__for_each_entry(pt->session->evlist, evsel) {
682 if (intel_pt_get_config(pt, &evsel->attr, &config))
683 return (config & pt->mtc_freq_bits) >> shift;
688 static bool intel_pt_timeless_decoding(struct intel_pt *pt)
690 struct perf_evsel *evsel;
691 bool timeless_decoding = true;
694 if (!pt->tsc_bit || !pt->cap_user_time_zero)
697 evlist__for_each_entry(pt->session->evlist, evsel) {
698 if (!(evsel->attr.sample_type & PERF_SAMPLE_TIME))
700 if (intel_pt_get_config(pt, &evsel->attr, &config)) {
701 if (config & pt->tsc_bit)
702 timeless_decoding = false;
707 return timeless_decoding;
710 static bool intel_pt_tracing_kernel(struct intel_pt *pt)
712 struct perf_evsel *evsel;
714 evlist__for_each_entry(pt->session->evlist, evsel) {
715 if (intel_pt_get_config(pt, &evsel->attr, NULL) &&
716 !evsel->attr.exclude_kernel)
722 static bool intel_pt_have_tsc(struct intel_pt *pt)
724 struct perf_evsel *evsel;
725 bool have_tsc = false;
731 evlist__for_each_entry(pt->session->evlist, evsel) {
732 if (intel_pt_get_config(pt, &evsel->attr, &config)) {
733 if (config & pt->tsc_bit)
742 static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns)
746 quot = ns / pt->tc.time_mult;
747 rem = ns % pt->tc.time_mult;
748 return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) /
752 static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt,
753 unsigned int queue_nr)
755 struct intel_pt_params params = { .get_trace = 0, };
756 struct intel_pt_queue *ptq;
758 ptq = zalloc(sizeof(struct intel_pt_queue));
762 if (pt->synth_opts.callchain) {
763 size_t sz = sizeof(struct ip_callchain);
765 sz += pt->synth_opts.callchain_sz * sizeof(u64);
766 ptq->chain = zalloc(sz);
771 if (pt->synth_opts.last_branch) {
772 size_t sz = sizeof(struct branch_stack);
774 sz += pt->synth_opts.last_branch_sz *
775 sizeof(struct branch_entry);
776 ptq->last_branch = zalloc(sz);
777 if (!ptq->last_branch)
779 ptq->last_branch_rb = zalloc(sz);
780 if (!ptq->last_branch_rb)
784 ptq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
789 ptq->queue_nr = queue_nr;
790 ptq->exclude_kernel = intel_pt_exclude_kernel(pt);
796 params.get_trace = intel_pt_get_trace;
797 params.walk_insn = intel_pt_walk_next_insn;
799 params.return_compression = intel_pt_return_compression(pt);
800 params.max_non_turbo_ratio = pt->max_non_turbo_ratio;
801 params.mtc_period = intel_pt_mtc_period(pt);
802 params.tsc_ctc_ratio_n = pt->tsc_ctc_ratio_n;
803 params.tsc_ctc_ratio_d = pt->tsc_ctc_ratio_d;
805 if (pt->filts.cnt > 0)
806 params.pgd_ip = intel_pt_pgd_ip;
808 if (pt->synth_opts.instructions) {
809 if (pt->synth_opts.period) {
810 switch (pt->synth_opts.period_type) {
811 case PERF_ITRACE_PERIOD_INSTRUCTIONS:
813 INTEL_PT_PERIOD_INSTRUCTIONS;
814 params.period = pt->synth_opts.period;
816 case PERF_ITRACE_PERIOD_TICKS:
817 params.period_type = INTEL_PT_PERIOD_TICKS;
818 params.period = pt->synth_opts.period;
820 case PERF_ITRACE_PERIOD_NANOSECS:
821 params.period_type = INTEL_PT_PERIOD_TICKS;
822 params.period = intel_pt_ns_to_ticks(pt,
823 pt->synth_opts.period);
830 if (!params.period) {
831 params.period_type = INTEL_PT_PERIOD_INSTRUCTIONS;
836 ptq->decoder = intel_pt_decoder_new(¶ms);
843 zfree(&ptq->event_buf);
844 zfree(&ptq->last_branch);
845 zfree(&ptq->last_branch_rb);
851 static void intel_pt_free_queue(void *priv)
853 struct intel_pt_queue *ptq = priv;
857 thread__zput(ptq->thread);
858 intel_pt_decoder_free(ptq->decoder);
859 zfree(&ptq->event_buf);
860 zfree(&ptq->last_branch);
861 zfree(&ptq->last_branch_rb);
866 static void intel_pt_set_pid_tid_cpu(struct intel_pt *pt,
867 struct auxtrace_queue *queue)
869 struct intel_pt_queue *ptq = queue->priv;
871 if (queue->tid == -1 || pt->have_sched_switch) {
872 ptq->tid = machine__get_current_tid(pt->machine, ptq->cpu);
873 thread__zput(ptq->thread);
876 if (!ptq->thread && ptq->tid != -1)
877 ptq->thread = machine__find_thread(pt->machine, -1, ptq->tid);
880 ptq->pid = ptq->thread->pid_;
881 if (queue->cpu == -1)
882 ptq->cpu = ptq->thread->cpu;
886 static void intel_pt_sample_flags(struct intel_pt_queue *ptq)
888 if (ptq->state->flags & INTEL_PT_ABORT_TX) {
889 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT;
890 } else if (ptq->state->flags & INTEL_PT_ASYNC) {
891 if (ptq->state->to_ip)
892 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
894 PERF_IP_FLAG_INTERRUPT;
896 ptq->flags = PERF_IP_FLAG_BRANCH |
897 PERF_IP_FLAG_TRACE_END;
900 if (ptq->state->from_ip)
901 ptq->flags = intel_pt_insn_type(ptq->state->insn_op);
903 ptq->flags = PERF_IP_FLAG_BRANCH |
904 PERF_IP_FLAG_TRACE_BEGIN;
905 if (ptq->state->flags & INTEL_PT_IN_TX)
906 ptq->flags |= PERF_IP_FLAG_IN_TX;
907 ptq->insn_len = ptq->state->insn_len;
908 memcpy(ptq->insn, ptq->state->insn, INTEL_PT_INSN_BUF_SZ);
912 static int intel_pt_setup_queue(struct intel_pt *pt,
913 struct auxtrace_queue *queue,
914 unsigned int queue_nr)
916 struct intel_pt_queue *ptq = queue->priv;
918 if (list_empty(&queue->head))
922 ptq = intel_pt_alloc_queue(pt, queue_nr);
927 if (queue->cpu != -1)
928 ptq->cpu = queue->cpu;
929 ptq->tid = queue->tid;
931 if (pt->sampling_mode) {
932 if (pt->timeless_decoding)
933 ptq->step_through_buffers = true;
934 if (pt->timeless_decoding || !pt->have_sched_switch)
935 ptq->use_buffer_pid_tid = true;
941 ptq->switch_state != INTEL_PT_SS_EXPECTING_SWITCH_EVENT)) {
942 const struct intel_pt_state *state;
945 if (pt->timeless_decoding)
948 intel_pt_log("queue %u getting timestamp\n", queue_nr);
949 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
950 queue_nr, ptq->cpu, ptq->pid, ptq->tid);
952 state = intel_pt_decode(ptq->decoder);
954 if (state->err == INTEL_PT_ERR_NODATA) {
955 intel_pt_log("queue %u has no timestamp\n",
961 if (state->timestamp)
965 ptq->timestamp = state->timestamp;
966 intel_pt_log("queue %u timestamp 0x%" PRIx64 "\n",
967 queue_nr, ptq->timestamp);
969 ptq->have_sample = true;
970 intel_pt_sample_flags(ptq);
971 ret = auxtrace_heap__add(&pt->heap, queue_nr, ptq->timestamp);
980 static int intel_pt_setup_queues(struct intel_pt *pt)
985 for (i = 0; i < pt->queues.nr_queues; i++) {
986 ret = intel_pt_setup_queue(pt, &pt->queues.queue_array[i], i);
993 static inline void intel_pt_copy_last_branch_rb(struct intel_pt_queue *ptq)
995 struct branch_stack *bs_src = ptq->last_branch_rb;
996 struct branch_stack *bs_dst = ptq->last_branch;
999 bs_dst->nr = bs_src->nr;
1004 nr = ptq->pt->synth_opts.last_branch_sz - ptq->last_branch_pos;
1005 memcpy(&bs_dst->entries[0],
1006 &bs_src->entries[ptq->last_branch_pos],
1007 sizeof(struct branch_entry) * nr);
1009 if (bs_src->nr >= ptq->pt->synth_opts.last_branch_sz) {
1010 memcpy(&bs_dst->entries[nr],
1011 &bs_src->entries[0],
1012 sizeof(struct branch_entry) * ptq->last_branch_pos);
1016 static inline void intel_pt_reset_last_branch_rb(struct intel_pt_queue *ptq)
1018 ptq->last_branch_pos = 0;
1019 ptq->last_branch_rb->nr = 0;
1022 static void intel_pt_update_last_branch_rb(struct intel_pt_queue *ptq)
1024 const struct intel_pt_state *state = ptq->state;
1025 struct branch_stack *bs = ptq->last_branch_rb;
1026 struct branch_entry *be;
1028 if (!ptq->last_branch_pos)
1029 ptq->last_branch_pos = ptq->pt->synth_opts.last_branch_sz;
1031 ptq->last_branch_pos -= 1;
1033 be = &bs->entries[ptq->last_branch_pos];
1034 be->from = state->from_ip;
1035 be->to = state->to_ip;
1036 be->flags.abort = !!(state->flags & INTEL_PT_ABORT_TX);
1037 be->flags.in_tx = !!(state->flags & INTEL_PT_IN_TX);
1038 /* No support for mispredict */
1039 be->flags.mispred = ptq->pt->mispred_all;
1041 if (bs->nr < ptq->pt->synth_opts.last_branch_sz)
1045 static int intel_pt_inject_event(union perf_event *event,
1046 struct perf_sample *sample, u64 type,
1049 event->header.size = perf_event__sample_event_size(sample, type, 0);
1050 return perf_event__synthesize_sample(event, type, 0, sample, swapped);
1053 static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq)
1056 struct intel_pt *pt = ptq->pt;
1057 union perf_event *event = ptq->event_buf;
1058 struct perf_sample sample = { .ip = 0, };
1059 struct dummy_branch_stack {
1061 struct branch_entry entries;
1064 if (pt->branches_filter && !(pt->branches_filter & ptq->flags))
1067 if (pt->synth_opts.initial_skip &&
1068 pt->num_events++ < pt->synth_opts.initial_skip)
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;
1083 sample.id = ptq->pt->branches_id;
1084 sample.stream_id = ptq->pt->branches_id;
1086 sample.cpu = ptq->cpu;
1087 sample.flags = ptq->flags;
1088 sample.insn_len = ptq->insn_len;
1089 memcpy(sample.insn, ptq->insn, INTEL_PT_INSN_BUF_SZ);
1092 * perf report cannot handle events without a branch stack when using
1093 * SORT_MODE__BRANCH so make a dummy one.
1095 if (pt->synth_opts.last_branch && sort__mode == SORT_MODE__BRANCH) {
1096 dummy_bs = (struct dummy_branch_stack){
1103 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1106 if (pt->synth_opts.inject) {
1107 ret = intel_pt_inject_event(event, &sample,
1108 pt->branches_sample_type,
1109 pt->synth_needs_swap);
1114 ret = perf_session__deliver_synth_event(pt->session, event, &sample);
1116 pr_err("Intel Processor Trace: failed to deliver branch event, error %d\n",
1122 static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq)
1125 struct intel_pt *pt = ptq->pt;
1126 union perf_event *event = ptq->event_buf;
1127 struct perf_sample sample = { .ip = 0, };
1129 if (pt->synth_opts.initial_skip &&
1130 pt->num_events++ < pt->synth_opts.initial_skip)
1133 event->sample.header.type = PERF_RECORD_SAMPLE;
1134 event->sample.header.misc = PERF_RECORD_MISC_USER;
1135 event->sample.header.size = sizeof(struct perf_event_header);
1137 if (!pt->timeless_decoding)
1138 sample.time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
1140 sample.cpumode = PERF_RECORD_MISC_USER;
1141 sample.ip = ptq->state->from_ip;
1142 sample.pid = ptq->pid;
1143 sample.tid = ptq->tid;
1144 sample.addr = ptq->state->to_ip;
1145 sample.id = ptq->pt->instructions_id;
1146 sample.stream_id = ptq->pt->instructions_id;
1147 sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt;
1148 sample.cpu = ptq->cpu;
1149 sample.flags = ptq->flags;
1150 sample.insn_len = ptq->insn_len;
1151 memcpy(sample.insn, ptq->insn, INTEL_PT_INSN_BUF_SZ);
1153 ptq->last_insn_cnt = ptq->state->tot_insn_cnt;
1155 if (pt->synth_opts.callchain) {
1156 thread_stack__sample(ptq->thread, ptq->chain,
1157 pt->synth_opts.callchain_sz, sample.ip);
1158 sample.callchain = ptq->chain;
1161 if (pt->synth_opts.last_branch) {
1162 intel_pt_copy_last_branch_rb(ptq);
1163 sample.branch_stack = ptq->last_branch;
1166 if (pt->synth_opts.inject) {
1167 ret = intel_pt_inject_event(event, &sample,
1168 pt->instructions_sample_type,
1169 pt->synth_needs_swap);
1174 ret = perf_session__deliver_synth_event(pt->session, event, &sample);
1176 pr_err("Intel Processor Trace: failed to deliver instruction event, error %d\n",
1179 if (pt->synth_opts.last_branch)
1180 intel_pt_reset_last_branch_rb(ptq);
1185 static int intel_pt_synth_transaction_sample(struct intel_pt_queue *ptq)
1188 struct intel_pt *pt = ptq->pt;
1189 union perf_event *event = ptq->event_buf;
1190 struct perf_sample sample = { .ip = 0, };
1192 if (pt->synth_opts.initial_skip &&
1193 pt->num_events++ < pt->synth_opts.initial_skip)
1196 event->sample.header.type = PERF_RECORD_SAMPLE;
1197 event->sample.header.misc = PERF_RECORD_MISC_USER;
1198 event->sample.header.size = sizeof(struct perf_event_header);
1200 if (!pt->timeless_decoding)
1201 sample.time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
1203 sample.cpumode = PERF_RECORD_MISC_USER;
1204 sample.ip = ptq->state->from_ip;
1205 sample.pid = ptq->pid;
1206 sample.tid = ptq->tid;
1207 sample.addr = ptq->state->to_ip;
1208 sample.id = ptq->pt->transactions_id;
1209 sample.stream_id = ptq->pt->transactions_id;
1211 sample.cpu = ptq->cpu;
1212 sample.flags = ptq->flags;
1213 sample.insn_len = ptq->insn_len;
1214 memcpy(sample.insn, ptq->insn, INTEL_PT_INSN_BUF_SZ);
1216 if (pt->synth_opts.callchain) {
1217 thread_stack__sample(ptq->thread, ptq->chain,
1218 pt->synth_opts.callchain_sz, sample.ip);
1219 sample.callchain = ptq->chain;
1222 if (pt->synth_opts.last_branch) {
1223 intel_pt_copy_last_branch_rb(ptq);
1224 sample.branch_stack = ptq->last_branch;
1227 if (pt->synth_opts.inject) {
1228 ret = intel_pt_inject_event(event, &sample,
1229 pt->transactions_sample_type,
1230 pt->synth_needs_swap);
1235 ret = perf_session__deliver_synth_event(pt->session, event, &sample);
1237 pr_err("Intel Processor Trace: failed to deliver transaction event, error %d\n",
1240 if (pt->synth_opts.last_branch)
1241 intel_pt_reset_last_branch_rb(ptq);
1246 static int intel_pt_synth_error(struct intel_pt *pt, int code, int cpu,
1247 pid_t pid, pid_t tid, u64 ip)
1249 union perf_event event;
1250 char msg[MAX_AUXTRACE_ERROR_MSG];
1253 intel_pt__strerror(code, msg, MAX_AUXTRACE_ERROR_MSG);
1255 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
1256 code, cpu, pid, tid, ip, msg);
1258 err = perf_session__deliver_synth_event(pt->session, &event, NULL);
1260 pr_err("Intel Processor Trace: failed to deliver error event, error %d\n",
1266 static int intel_pt_next_tid(struct intel_pt *pt, struct intel_pt_queue *ptq)
1268 struct auxtrace_queue *queue;
1269 pid_t tid = ptq->next_tid;
1275 intel_pt_log("switch: cpu %d tid %d\n", ptq->cpu, tid);
1277 err = machine__set_current_tid(pt->machine, ptq->cpu, -1, tid);
1279 queue = &pt->queues.queue_array[ptq->queue_nr];
1280 intel_pt_set_pid_tid_cpu(pt, queue);
1287 static inline bool intel_pt_is_switch_ip(struct intel_pt_queue *ptq, u64 ip)
1289 struct intel_pt *pt = ptq->pt;
1291 return ip == pt->switch_ip &&
1292 (ptq->flags & PERF_IP_FLAG_BRANCH) &&
1293 !(ptq->flags & (PERF_IP_FLAG_CONDITIONAL | PERF_IP_FLAG_ASYNC |
1294 PERF_IP_FLAG_INTERRUPT | PERF_IP_FLAG_TX_ABORT));
1297 static int intel_pt_sample(struct intel_pt_queue *ptq)
1299 const struct intel_pt_state *state = ptq->state;
1300 struct intel_pt *pt = ptq->pt;
1303 if (!ptq->have_sample)
1306 ptq->have_sample = false;
1308 if (pt->sample_instructions &&
1309 (state->type & INTEL_PT_INSTRUCTION) &&
1310 (!pt->synth_opts.initial_skip ||
1311 pt->num_events++ >= pt->synth_opts.initial_skip)) {
1312 err = intel_pt_synth_instruction_sample(ptq);
1317 if (pt->sample_transactions &&
1318 (state->type & INTEL_PT_TRANSACTION) &&
1319 (!pt->synth_opts.initial_skip ||
1320 pt->num_events++ >= pt->synth_opts.initial_skip)) {
1321 err = intel_pt_synth_transaction_sample(ptq);
1326 if (!(state->type & INTEL_PT_BRANCH))
1329 if (pt->synth_opts.callchain || pt->synth_opts.thread_stack)
1330 thread_stack__event(ptq->thread, ptq->flags, state->from_ip,
1331 state->to_ip, ptq->insn_len,
1334 thread_stack__set_trace_nr(ptq->thread, state->trace_nr);
1336 if (pt->sample_branches) {
1337 err = intel_pt_synth_branch_sample(ptq);
1342 if (pt->synth_opts.last_branch)
1343 intel_pt_update_last_branch_rb(ptq);
1345 if (!pt->sync_switch)
1348 if (intel_pt_is_switch_ip(ptq, state->to_ip)) {
1349 switch (ptq->switch_state) {
1350 case INTEL_PT_SS_UNKNOWN:
1351 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
1352 err = intel_pt_next_tid(pt, ptq);
1355 ptq->switch_state = INTEL_PT_SS_TRACING;
1358 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_EVENT;
1361 } else if (!state->to_ip) {
1362 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
1363 } else if (ptq->switch_state == INTEL_PT_SS_NOT_TRACING) {
1364 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
1365 } else if (ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
1366 state->to_ip == pt->ptss_ip &&
1367 (ptq->flags & PERF_IP_FLAG_CALL)) {
1368 ptq->switch_state = INTEL_PT_SS_TRACING;
1374 static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)
1376 struct machine *machine = pt->machine;
1378 struct symbol *sym, *start;
1379 u64 ip, switch_ip = 0;
1385 map = machine__kernel_map(machine);
1392 start = dso__first_symbol(map->dso, MAP__FUNCTION);
1394 for (sym = start; sym; sym = dso__next_symbol(sym)) {
1395 if (sym->binding == STB_GLOBAL &&
1396 !strcmp(sym->name, "__switch_to")) {
1397 ip = map->unmap_ip(map, sym->start);
1398 if (ip >= map->start && ip < map->end) {
1405 if (!switch_ip || !ptss_ip)
1408 if (pt->have_sched_switch == 1)
1409 ptss = "perf_trace_sched_switch";
1411 ptss = "__perf_event_task_sched_out";
1413 for (sym = start; sym; sym = dso__next_symbol(sym)) {
1414 if (!strcmp(sym->name, ptss)) {
1415 ip = map->unmap_ip(map, sym->start);
1416 if (ip >= map->start && ip < map->end) {
1426 static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp)
1428 const struct intel_pt_state *state = ptq->state;
1429 struct intel_pt *pt = ptq->pt;
1432 if (!pt->kernel_start) {
1433 pt->kernel_start = machine__kernel_start(pt->machine);
1434 if (pt->per_cpu_mmaps &&
1435 (pt->have_sched_switch == 1 || pt->have_sched_switch == 3) &&
1436 !pt->timeless_decoding && intel_pt_tracing_kernel(pt) &&
1437 !pt->sampling_mode) {
1438 pt->switch_ip = intel_pt_switch_ip(pt, &pt->ptss_ip);
1439 if (pt->switch_ip) {
1440 intel_pt_log("switch_ip: %"PRIx64" ptss_ip: %"PRIx64"\n",
1441 pt->switch_ip, pt->ptss_ip);
1442 pt->sync_switch = true;
1447 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
1448 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
1450 err = intel_pt_sample(ptq);
1454 state = intel_pt_decode(ptq->decoder);
1456 if (state->err == INTEL_PT_ERR_NODATA)
1458 if (pt->sync_switch &&
1459 state->from_ip >= pt->kernel_start) {
1460 pt->sync_switch = false;
1461 intel_pt_next_tid(pt, ptq);
1463 if (pt->synth_opts.errors) {
1464 err = intel_pt_synth_error(pt, state->err,
1475 ptq->have_sample = true;
1476 intel_pt_sample_flags(ptq);
1478 /* Use estimated TSC upon return to user space */
1480 (state->from_ip >= pt->kernel_start || !state->from_ip) &&
1481 state->to_ip && state->to_ip < pt->kernel_start) {
1482 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
1483 state->timestamp, state->est_timestamp);
1484 ptq->timestamp = state->est_timestamp;
1485 /* Use estimated TSC in unknown switch state */
1486 } else if (pt->sync_switch &&
1487 ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
1488 intel_pt_is_switch_ip(ptq, state->to_ip) &&
1489 ptq->next_tid == -1) {
1490 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
1491 state->timestamp, state->est_timestamp);
1492 ptq->timestamp = state->est_timestamp;
1493 } else if (state->timestamp > ptq->timestamp) {
1494 ptq->timestamp = state->timestamp;
1497 if (!pt->timeless_decoding && ptq->timestamp >= *timestamp) {
1498 *timestamp = ptq->timestamp;
1505 static inline int intel_pt_update_queues(struct intel_pt *pt)
1507 if (pt->queues.new_data) {
1508 pt->queues.new_data = false;
1509 return intel_pt_setup_queues(pt);
1514 static int intel_pt_process_queues(struct intel_pt *pt, u64 timestamp)
1516 unsigned int queue_nr;
1521 struct auxtrace_queue *queue;
1522 struct intel_pt_queue *ptq;
1524 if (!pt->heap.heap_cnt)
1527 if (pt->heap.heap_array[0].ordinal >= timestamp)
1530 queue_nr = pt->heap.heap_array[0].queue_nr;
1531 queue = &pt->queues.queue_array[queue_nr];
1534 intel_pt_log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n",
1535 queue_nr, pt->heap.heap_array[0].ordinal,
1538 auxtrace_heap__pop(&pt->heap);
1540 if (pt->heap.heap_cnt) {
1541 ts = pt->heap.heap_array[0].ordinal + 1;
1548 intel_pt_set_pid_tid_cpu(pt, queue);
1550 ret = intel_pt_run_decoder(ptq, &ts);
1553 auxtrace_heap__add(&pt->heap, queue_nr, ts);
1558 ret = auxtrace_heap__add(&pt->heap, queue_nr, ts);
1562 ptq->on_heap = false;
1569 static int intel_pt_process_timeless_queues(struct intel_pt *pt, pid_t tid,
1572 struct auxtrace_queues *queues = &pt->queues;
1576 for (i = 0; i < queues->nr_queues; i++) {
1577 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
1578 struct intel_pt_queue *ptq = queue->priv;
1580 if (ptq && (tid == -1 || ptq->tid == tid)) {
1582 intel_pt_set_pid_tid_cpu(pt, queue);
1583 intel_pt_run_decoder(ptq, &ts);
1589 static int intel_pt_lost(struct intel_pt *pt, struct perf_sample *sample)
1591 return intel_pt_synth_error(pt, INTEL_PT_ERR_LOST, sample->cpu,
1592 sample->pid, sample->tid, 0);
1595 static struct intel_pt_queue *intel_pt_cpu_to_ptq(struct intel_pt *pt, int cpu)
1599 if (cpu < 0 || !pt->queues.nr_queues)
1602 if ((unsigned)cpu >= pt->queues.nr_queues)
1603 i = pt->queues.nr_queues - 1;
1607 if (pt->queues.queue_array[i].cpu == cpu)
1608 return pt->queues.queue_array[i].priv;
1610 for (j = 0; i > 0; j++) {
1611 if (pt->queues.queue_array[--i].cpu == cpu)
1612 return pt->queues.queue_array[i].priv;
1615 for (; j < pt->queues.nr_queues; j++) {
1616 if (pt->queues.queue_array[j].cpu == cpu)
1617 return pt->queues.queue_array[j].priv;
1623 static int intel_pt_sync_switch(struct intel_pt *pt, int cpu, pid_t tid,
1626 struct intel_pt_queue *ptq;
1629 if (!pt->sync_switch)
1632 ptq = intel_pt_cpu_to_ptq(pt, cpu);
1636 switch (ptq->switch_state) {
1637 case INTEL_PT_SS_NOT_TRACING:
1640 case INTEL_PT_SS_UNKNOWN:
1641 case INTEL_PT_SS_TRACING:
1642 ptq->next_tid = tid;
1643 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_IP;
1645 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
1646 if (!ptq->on_heap) {
1647 ptq->timestamp = perf_time_to_tsc(timestamp,
1649 err = auxtrace_heap__add(&pt->heap, ptq->queue_nr,
1653 ptq->on_heap = true;
1655 ptq->switch_state = INTEL_PT_SS_TRACING;
1657 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
1658 ptq->next_tid = tid;
1659 intel_pt_log("ERROR: cpu %d expecting switch ip\n", cpu);
1668 static int intel_pt_process_switch(struct intel_pt *pt,
1669 struct perf_sample *sample)
1671 struct perf_evsel *evsel;
1675 evsel = perf_evlist__id2evsel(pt->session->evlist, sample->id);
1676 if (evsel != pt->switch_evsel)
1679 tid = perf_evsel__intval(evsel, sample, "next_pid");
1682 intel_pt_log("sched_switch: cpu %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
1683 cpu, tid, sample->time, perf_time_to_tsc(sample->time,
1686 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
1690 return machine__set_current_tid(pt->machine, cpu, -1, tid);
1693 static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
1694 struct perf_sample *sample)
1696 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1702 if (pt->have_sched_switch == 3) {
1705 if (event->header.type != PERF_RECORD_SWITCH_CPU_WIDE) {
1706 pr_err("Expecting CPU-wide context switch event\n");
1709 pid = event->context_switch.next_prev_pid;
1710 tid = event->context_switch.next_prev_tid;
1719 pr_err("context_switch event has no tid\n");
1723 intel_pt_log("context_switch: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
1724 cpu, pid, tid, sample->time, perf_time_to_tsc(sample->time,
1727 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
1731 return machine__set_current_tid(pt->machine, cpu, pid, tid);
1734 static int intel_pt_process_itrace_start(struct intel_pt *pt,
1735 union perf_event *event,
1736 struct perf_sample *sample)
1738 if (!pt->per_cpu_mmaps)
1741 intel_pt_log("itrace_start: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
1742 sample->cpu, event->itrace_start.pid,
1743 event->itrace_start.tid, sample->time,
1744 perf_time_to_tsc(sample->time, &pt->tc));
1746 return machine__set_current_tid(pt->machine, sample->cpu,
1747 event->itrace_start.pid,
1748 event->itrace_start.tid);
1751 static int intel_pt_process_event(struct perf_session *session,
1752 union perf_event *event,
1753 struct perf_sample *sample,
1754 struct perf_tool *tool)
1756 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1764 if (!tool->ordered_events) {
1765 pr_err("Intel Processor Trace requires ordered events\n");
1769 if (sample->time && sample->time != (u64)-1)
1770 timestamp = perf_time_to_tsc(sample->time, &pt->tc);
1774 if (timestamp || pt->timeless_decoding) {
1775 err = intel_pt_update_queues(pt);
1780 if (pt->timeless_decoding) {
1781 if (event->header.type == PERF_RECORD_EXIT) {
1782 err = intel_pt_process_timeless_queues(pt,
1786 } else if (timestamp) {
1787 err = intel_pt_process_queues(pt, timestamp);
1792 if (event->header.type == PERF_RECORD_AUX &&
1793 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
1794 pt->synth_opts.errors) {
1795 err = intel_pt_lost(pt, sample);
1800 if (pt->switch_evsel && event->header.type == PERF_RECORD_SAMPLE)
1801 err = intel_pt_process_switch(pt, sample);
1802 else if (event->header.type == PERF_RECORD_ITRACE_START)
1803 err = intel_pt_process_itrace_start(pt, event, sample);
1804 else if (event->header.type == PERF_RECORD_SWITCH ||
1805 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
1806 err = intel_pt_context_switch(pt, event, sample);
1808 intel_pt_log("event %s (%u): cpu %d time %"PRIu64" tsc %#"PRIx64"\n",
1809 perf_event__name(event->header.type), event->header.type,
1810 sample->cpu, sample->time, timestamp);
1815 static int intel_pt_flush(struct perf_session *session, struct perf_tool *tool)
1817 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1824 if (!tool->ordered_events)
1827 ret = intel_pt_update_queues(pt);
1831 if (pt->timeless_decoding)
1832 return intel_pt_process_timeless_queues(pt, -1,
1835 return intel_pt_process_queues(pt, MAX_TIMESTAMP);
1838 static void intel_pt_free_events(struct perf_session *session)
1840 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1842 struct auxtrace_queues *queues = &pt->queues;
1845 for (i = 0; i < queues->nr_queues; i++) {
1846 intel_pt_free_queue(queues->queue_array[i].priv);
1847 queues->queue_array[i].priv = NULL;
1849 intel_pt_log_disable();
1850 auxtrace_queues__free(queues);
1853 static void intel_pt_free(struct perf_session *session)
1855 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1858 auxtrace_heap__free(&pt->heap);
1859 intel_pt_free_events(session);
1860 session->auxtrace = NULL;
1861 thread__put(pt->unknown_thread);
1862 addr_filters__exit(&pt->filts);
1867 static int intel_pt_process_auxtrace_event(struct perf_session *session,
1868 union perf_event *event,
1869 struct perf_tool *tool __maybe_unused)
1871 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1874 if (pt->sampling_mode)
1877 if (!pt->data_queued) {
1878 struct auxtrace_buffer *buffer;
1880 int fd = perf_data_file__fd(session->file);
1883 if (perf_data_file__is_pipe(session->file)) {
1886 data_offset = lseek(fd, 0, SEEK_CUR);
1887 if (data_offset == -1)
1891 err = auxtrace_queues__add_event(&pt->queues, session, event,
1892 data_offset, &buffer);
1896 /* Dump here now we have copied a piped trace out of the pipe */
1898 if (auxtrace_buffer__get_data(buffer, fd)) {
1899 intel_pt_dump_event(pt, buffer->data,
1901 auxtrace_buffer__put_data(buffer);
1909 struct intel_pt_synth {
1910 struct perf_tool dummy_tool;
1911 struct perf_session *session;
1914 static int intel_pt_event_synth(struct perf_tool *tool,
1915 union perf_event *event,
1916 struct perf_sample *sample __maybe_unused,
1917 struct machine *machine __maybe_unused)
1919 struct intel_pt_synth *intel_pt_synth =
1920 container_of(tool, struct intel_pt_synth, dummy_tool);
1922 return perf_session__deliver_synth_event(intel_pt_synth->session, event,
1926 static int intel_pt_synth_event(struct perf_session *session,
1927 struct perf_event_attr *attr, u64 id)
1929 struct intel_pt_synth intel_pt_synth;
1931 memset(&intel_pt_synth, 0, sizeof(struct intel_pt_synth));
1932 intel_pt_synth.session = session;
1934 return perf_event__synthesize_attr(&intel_pt_synth.dummy_tool, attr, 1,
1935 &id, intel_pt_event_synth);
1938 static int intel_pt_synth_events(struct intel_pt *pt,
1939 struct perf_session *session)
1941 struct perf_evlist *evlist = session->evlist;
1942 struct perf_evsel *evsel;
1943 struct perf_event_attr attr;
1948 evlist__for_each_entry(evlist, evsel) {
1949 if (evsel->attr.type == pt->pmu_type && evsel->ids) {
1956 pr_debug("There are no selected events with Intel Processor Trace data\n");
1960 memset(&attr, 0, sizeof(struct perf_event_attr));
1961 attr.size = sizeof(struct perf_event_attr);
1962 attr.type = PERF_TYPE_HARDWARE;
1963 attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
1964 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1966 if (pt->timeless_decoding)
1967 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1969 attr.sample_type |= PERF_SAMPLE_TIME;
1970 if (!pt->per_cpu_mmaps)
1971 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
1972 attr.exclude_user = evsel->attr.exclude_user;
1973 attr.exclude_kernel = evsel->attr.exclude_kernel;
1974 attr.exclude_hv = evsel->attr.exclude_hv;
1975 attr.exclude_host = evsel->attr.exclude_host;
1976 attr.exclude_guest = evsel->attr.exclude_guest;
1977 attr.sample_id_all = evsel->attr.sample_id_all;
1978 attr.read_format = evsel->attr.read_format;
1980 id = evsel->id[0] + 1000000000;
1984 if (pt->synth_opts.instructions) {
1985 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
1986 if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS)
1987 attr.sample_period =
1988 intel_pt_ns_to_ticks(pt, pt->synth_opts.period);
1990 attr.sample_period = pt->synth_opts.period;
1991 pt->instructions_sample_period = attr.sample_period;
1992 if (pt->synth_opts.callchain)
1993 attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
1994 if (pt->synth_opts.last_branch)
1995 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
1996 pr_debug("Synthesizing 'instructions' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
1997 id, (u64)attr.sample_type);
1998 err = intel_pt_synth_event(session, &attr, id);
2000 pr_err("%s: failed to synthesize 'instructions' event type\n",
2004 pt->sample_instructions = true;
2005 pt->instructions_sample_type = attr.sample_type;
2006 pt->instructions_id = id;
2010 if (pt->synth_opts.transactions) {
2011 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
2012 attr.sample_period = 1;
2013 if (pt->synth_opts.callchain)
2014 attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
2015 if (pt->synth_opts.last_branch)
2016 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
2017 pr_debug("Synthesizing 'transactions' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
2018 id, (u64)attr.sample_type);
2019 err = intel_pt_synth_event(session, &attr, id);
2021 pr_err("%s: failed to synthesize 'transactions' event type\n",
2025 pt->sample_transactions = true;
2026 pt->transactions_id = id;
2028 evlist__for_each_entry(evlist, evsel) {
2029 if (evsel->id && evsel->id[0] == pt->transactions_id) {
2031 zfree(&evsel->name);
2032 evsel->name = strdup("transactions");
2038 if (pt->synth_opts.branches) {
2039 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
2040 attr.sample_period = 1;
2041 attr.sample_type |= PERF_SAMPLE_ADDR;
2042 attr.sample_type &= ~(u64)PERF_SAMPLE_CALLCHAIN;
2043 attr.sample_type &= ~(u64)PERF_SAMPLE_BRANCH_STACK;
2044 pr_debug("Synthesizing 'branches' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
2045 id, (u64)attr.sample_type);
2046 err = intel_pt_synth_event(session, &attr, id);
2048 pr_err("%s: failed to synthesize 'branches' event type\n",
2052 pt->sample_branches = true;
2053 pt->branches_sample_type = attr.sample_type;
2054 pt->branches_id = id;
2057 pt->synth_needs_swap = evsel->needs_swap;
2062 static struct perf_evsel *intel_pt_find_sched_switch(struct perf_evlist *evlist)
2064 struct perf_evsel *evsel;
2066 evlist__for_each_entry_reverse(evlist, evsel) {
2067 const char *name = perf_evsel__name(evsel);
2069 if (!strcmp(name, "sched:sched_switch"))
2076 static bool intel_pt_find_switch(struct perf_evlist *evlist)
2078 struct perf_evsel *evsel;
2080 evlist__for_each_entry(evlist, evsel) {
2081 if (evsel->attr.context_switch)
2088 static int intel_pt_perf_config(const char *var, const char *value, void *data)
2090 struct intel_pt *pt = data;
2092 if (!strcmp(var, "intel-pt.mispred-all"))
2093 pt->mispred_all = perf_config_bool(var, value);
2098 static const char * const intel_pt_info_fmts[] = {
2099 [INTEL_PT_PMU_TYPE] = " PMU Type %"PRId64"\n",
2100 [INTEL_PT_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
2101 [INTEL_PT_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
2102 [INTEL_PT_TIME_ZERO] = " Time Zero %"PRIu64"\n",
2103 [INTEL_PT_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
2104 [INTEL_PT_TSC_BIT] = " TSC bit %#"PRIx64"\n",
2105 [INTEL_PT_NORETCOMP_BIT] = " NoRETComp bit %#"PRIx64"\n",
2106 [INTEL_PT_HAVE_SCHED_SWITCH] = " Have sched_switch %"PRId64"\n",
2107 [INTEL_PT_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
2108 [INTEL_PT_PER_CPU_MMAPS] = " Per-cpu maps %"PRId64"\n",
2109 [INTEL_PT_MTC_BIT] = " MTC bit %#"PRIx64"\n",
2110 [INTEL_PT_TSC_CTC_N] = " TSC:CTC numerator %"PRIu64"\n",
2111 [INTEL_PT_TSC_CTC_D] = " TSC:CTC denominator %"PRIu64"\n",
2112 [INTEL_PT_CYC_BIT] = " CYC bit %#"PRIx64"\n",
2113 [INTEL_PT_MAX_NONTURBO_RATIO] = " Max non-turbo ratio %"PRIu64"\n",
2114 [INTEL_PT_FILTER_STR_LEN] = " Filter string len. %"PRIu64"\n",
2117 static void intel_pt_print_info(u64 *arr, int start, int finish)
2124 for (i = start; i <= finish; i++)
2125 fprintf(stdout, intel_pt_info_fmts[i], arr[i]);
2128 static void intel_pt_print_info_str(const char *name, const char *str)
2133 fprintf(stdout, " %-20s%s\n", name, str ? str : "");
2136 static bool intel_pt_has(struct auxtrace_info_event *auxtrace_info, int pos)
2138 return auxtrace_info->header.size >=
2139 sizeof(struct auxtrace_info_event) + (sizeof(u64) * (pos + 1));
2142 int intel_pt_process_auxtrace_info(union perf_event *event,
2143 struct perf_session *session)
2145 struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
2146 size_t min_sz = sizeof(u64) * INTEL_PT_PER_CPU_MMAPS;
2147 struct intel_pt *pt;
2152 if (auxtrace_info->header.size < sizeof(struct auxtrace_info_event) +
2156 pt = zalloc(sizeof(struct intel_pt));
2160 addr_filters__init(&pt->filts);
2162 perf_config(intel_pt_perf_config, pt);
2164 err = auxtrace_queues__init(&pt->queues);
2168 intel_pt_log_set_name(INTEL_PT_PMU_NAME);
2170 pt->session = session;
2171 pt->machine = &session->machines.host; /* No kvm support */
2172 pt->auxtrace_type = auxtrace_info->type;
2173 pt->pmu_type = auxtrace_info->priv[INTEL_PT_PMU_TYPE];
2174 pt->tc.time_shift = auxtrace_info->priv[INTEL_PT_TIME_SHIFT];
2175 pt->tc.time_mult = auxtrace_info->priv[INTEL_PT_TIME_MULT];
2176 pt->tc.time_zero = auxtrace_info->priv[INTEL_PT_TIME_ZERO];
2177 pt->cap_user_time_zero = auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO];
2178 pt->tsc_bit = auxtrace_info->priv[INTEL_PT_TSC_BIT];
2179 pt->noretcomp_bit = auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT];
2180 pt->have_sched_switch = auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH];
2181 pt->snapshot_mode = auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE];
2182 pt->per_cpu_mmaps = auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS];
2183 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_PMU_TYPE,
2184 INTEL_PT_PER_CPU_MMAPS);
2186 if (intel_pt_has(auxtrace_info, INTEL_PT_CYC_BIT)) {
2187 pt->mtc_bit = auxtrace_info->priv[INTEL_PT_MTC_BIT];
2188 pt->mtc_freq_bits = auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS];
2189 pt->tsc_ctc_ratio_n = auxtrace_info->priv[INTEL_PT_TSC_CTC_N];
2190 pt->tsc_ctc_ratio_d = auxtrace_info->priv[INTEL_PT_TSC_CTC_D];
2191 pt->cyc_bit = auxtrace_info->priv[INTEL_PT_CYC_BIT];
2192 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_MTC_BIT,
2196 if (intel_pt_has(auxtrace_info, INTEL_PT_MAX_NONTURBO_RATIO)) {
2197 pt->max_non_turbo_ratio =
2198 auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO];
2199 intel_pt_print_info(&auxtrace_info->priv[0],
2200 INTEL_PT_MAX_NONTURBO_RATIO,
2201 INTEL_PT_MAX_NONTURBO_RATIO);
2204 info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
2205 info_end = (void *)info + auxtrace_info->header.size;
2207 if (intel_pt_has(auxtrace_info, INTEL_PT_FILTER_STR_LEN)) {
2210 len = auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN];
2211 intel_pt_print_info(&auxtrace_info->priv[0],
2212 INTEL_PT_FILTER_STR_LEN,
2213 INTEL_PT_FILTER_STR_LEN);
2215 const char *filter = (const char *)info;
2217 len = roundup(len + 1, 8);
2219 if ((void *)info > info_end) {
2220 pr_err("%s: bad filter string length\n", __func__);
2222 goto err_free_queues;
2224 pt->filter = memdup(filter, len);
2227 goto err_free_queues;
2229 if (session->header.needs_swap)
2230 mem_bswap_64(pt->filter, len);
2231 if (pt->filter[len - 1]) {
2232 pr_err("%s: filter string not null terminated\n", __func__);
2234 goto err_free_queues;
2236 err = addr_filters__parse_bare_filter(&pt->filts,
2239 goto err_free_queues;
2241 intel_pt_print_info_str("Filter string", pt->filter);
2244 pt->timeless_decoding = intel_pt_timeless_decoding(pt);
2245 pt->have_tsc = intel_pt_have_tsc(pt);
2246 pt->sampling_mode = false;
2247 pt->est_tsc = !pt->timeless_decoding;
2249 pt->unknown_thread = thread__new(999999999, 999999999);
2250 if (!pt->unknown_thread) {
2252 goto err_free_queues;
2256 * Since this thread will not be kept in any rbtree not in a
2257 * list, initialize its list node so that at thread__put() the
2258 * current thread lifetime assuption is kept and we don't segfault
2259 * at list_del_init().
2261 INIT_LIST_HEAD(&pt->unknown_thread->node);
2263 err = thread__set_comm(pt->unknown_thread, "unknown", 0);
2265 goto err_delete_thread;
2266 if (thread__init_map_groups(pt->unknown_thread, pt->machine)) {
2268 goto err_delete_thread;
2271 pt->auxtrace.process_event = intel_pt_process_event;
2272 pt->auxtrace.process_auxtrace_event = intel_pt_process_auxtrace_event;
2273 pt->auxtrace.flush_events = intel_pt_flush;
2274 pt->auxtrace.free_events = intel_pt_free_events;
2275 pt->auxtrace.free = intel_pt_free;
2276 session->auxtrace = &pt->auxtrace;
2281 if (pt->have_sched_switch == 1) {
2282 pt->switch_evsel = intel_pt_find_sched_switch(session->evlist);
2283 if (!pt->switch_evsel) {
2284 pr_err("%s: missing sched_switch event\n", __func__);
2286 goto err_delete_thread;
2288 } else if (pt->have_sched_switch == 2 &&
2289 !intel_pt_find_switch(session->evlist)) {
2290 pr_err("%s: missing context_switch attribute flag\n", __func__);
2292 goto err_delete_thread;
2295 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
2296 pt->synth_opts = *session->itrace_synth_opts;
2298 itrace_synth_opts__set_default(&pt->synth_opts);
2299 if (use_browser != -1) {
2300 pt->synth_opts.branches = false;
2301 pt->synth_opts.callchain = true;
2303 if (session->itrace_synth_opts)
2304 pt->synth_opts.thread_stack =
2305 session->itrace_synth_opts->thread_stack;
2308 if (pt->synth_opts.log)
2309 intel_pt_log_enable();
2311 /* Maximum non-turbo ratio is TSC freq / 100 MHz */
2312 if (pt->tc.time_mult) {
2313 u64 tsc_freq = intel_pt_ns_to_ticks(pt, 1000000000);
2315 if (!pt->max_non_turbo_ratio)
2316 pt->max_non_turbo_ratio =
2317 (tsc_freq + 50000000) / 100000000;
2318 intel_pt_log("TSC frequency %"PRIu64"\n", tsc_freq);
2319 intel_pt_log("Maximum non-turbo ratio %u\n",
2320 pt->max_non_turbo_ratio);
2323 if (pt->synth_opts.calls)
2324 pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
2325 PERF_IP_FLAG_TRACE_END;
2326 if (pt->synth_opts.returns)
2327 pt->branches_filter |= PERF_IP_FLAG_RETURN |
2328 PERF_IP_FLAG_TRACE_BEGIN;
2330 if (pt->synth_opts.callchain && !symbol_conf.use_callchain) {
2331 symbol_conf.use_callchain = true;
2332 if (callchain_register_param(&callchain_param) < 0) {
2333 symbol_conf.use_callchain = false;
2334 pt->synth_opts.callchain = false;
2338 err = intel_pt_synth_events(pt, session);
2340 goto err_delete_thread;
2342 err = auxtrace_queues__process_index(&pt->queues, session);
2344 goto err_delete_thread;
2346 if (pt->queues.populated)
2347 pt->data_queued = true;
2349 if (pt->timeless_decoding)
2350 pr_debug2("Intel PT decoding without timestamps\n");
2355 thread__zput(pt->unknown_thread);
2357 intel_pt_log_disable();
2358 auxtrace_queues__free(&pt->queues);
2359 session->auxtrace = NULL;
2361 addr_filters__exit(&pt->filts);