blob: 4a7746249999b1ce806ec10d6f7decede6f1c72e [file] [log] [blame]
Adrian Hunter90e457f2015-07-17 19:33:41 +03001/*
2 * intel_pt.c: Intel Processor Trace support
3 * Copyright (c) 2013-2015, Intel Corporation.
4 *
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.
8 *
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
12 * more details.
13 *
14 */
15
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030016#include <inttypes.h>
Adrian Hunter90e457f2015-07-17 19:33:41 +030017#include <stdio.h>
18#include <stdbool.h>
19#include <errno.h>
20#include <linux/kernel.h>
21#include <linux/types.h>
22
23#include "../perf.h"
24#include "session.h"
25#include "machine.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030026#include "memswap.h"
Adrian Hunterf14445e2015-09-25 16:15:45 +030027#include "sort.h"
Adrian Hunter90e457f2015-07-17 19:33:41 +030028#include "tool.h"
29#include "event.h"
30#include "evlist.h"
31#include "evsel.h"
32#include "map.h"
33#include "color.h"
34#include "util.h"
35#include "thread.h"
36#include "thread-stack.h"
37#include "symbol.h"
38#include "callchain.h"
39#include "dso.h"
40#include "debug.h"
41#include "auxtrace.h"
42#include "tsc.h"
43#include "intel-pt.h"
Taeung Song41840d22016-06-23 17:55:17 +090044#include "config.h"
Adrian Hunter90e457f2015-07-17 19:33:41 +030045
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"
50
51#define MAX_TIMESTAMP (~0ULL)
52
53struct intel_pt {
54 struct auxtrace auxtrace;
55 struct auxtrace_queues queues;
56 struct auxtrace_heap heap;
57 u32 auxtrace_type;
58 struct perf_session *session;
59 struct machine *machine;
60 struct perf_evsel *switch_evsel;
61 struct thread *unknown_thread;
62 bool timeless_decoding;
63 bool sampling_mode;
64 bool snapshot_mode;
65 bool per_cpu_mmaps;
66 bool have_tsc;
67 bool data_queued;
68 bool est_tsc;
69 bool sync_switch;
Adrian Hunterba11ba62015-09-25 16:15:56 +030070 bool mispred_all;
Adrian Hunter90e457f2015-07-17 19:33:41 +030071 int have_sched_switch;
72 u32 pmu_type;
73 u64 kernel_start;
74 u64 switch_ip;
75 u64 ptss_ip;
76
77 struct perf_tsc_conversion tc;
78 bool cap_user_time_zero;
79
80 struct itrace_synth_opts synth_opts;
81
82 bool sample_instructions;
83 u64 instructions_sample_type;
Adrian Hunter90e457f2015-07-17 19:33:41 +030084 u64 instructions_id;
85
86 bool sample_branches;
87 u32 branches_filter;
88 u64 branches_sample_type;
89 u64 branches_id;
90
91 bool sample_transactions;
92 u64 transactions_sample_type;
93 u64 transactions_id;
94
Adrian Hunter37973072017-06-30 11:36:45 +030095 bool sample_ptwrites;
96 u64 ptwrites_sample_type;
97 u64 ptwrites_id;
98
99 bool sample_pwr_events;
100 u64 pwr_events_sample_type;
101 u64 mwait_id;
102 u64 pwre_id;
103 u64 exstop_id;
104 u64 pwrx_id;
105 u64 cbr_id;
106
Adrian Hunter90e457f2015-07-17 19:33:41 +0300107 u64 tsc_bit;
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300108 u64 mtc_bit;
109 u64 mtc_freq_bits;
110 u32 tsc_ctc_ratio_n;
111 u32 tsc_ctc_ratio_d;
112 u64 cyc_bit;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300113 u64 noretcomp_bit;
114 unsigned max_non_turbo_ratio;
Adrian Hunter37973072017-06-30 11:36:45 +0300115 unsigned cbr2khz;
Andi Kleend1706b32016-03-28 10:45:38 -0700116
117 unsigned long num_events;
Adrian Hunter2b9e32c2016-09-23 17:38:46 +0300118
119 char *filter;
Adrian Hunter2acee102016-09-23 17:38:48 +0300120 struct addr_filters filts;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300121};
122
123enum switch_state {
124 INTEL_PT_SS_NOT_TRACING,
125 INTEL_PT_SS_UNKNOWN,
126 INTEL_PT_SS_TRACING,
127 INTEL_PT_SS_EXPECTING_SWITCH_EVENT,
128 INTEL_PT_SS_EXPECTING_SWITCH_IP,
129};
130
131struct intel_pt_queue {
132 struct intel_pt *pt;
133 unsigned int queue_nr;
134 struct auxtrace_buffer *buffer;
135 void *decoder;
136 const struct intel_pt_state *state;
137 struct ip_callchain *chain;
Adrian Hunterf14445e2015-09-25 16:15:45 +0300138 struct branch_stack *last_branch;
139 struct branch_stack *last_branch_rb;
140 size_t last_branch_pos;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300141 union perf_event *event_buf;
142 bool on_heap;
143 bool stop;
144 bool step_through_buffers;
145 bool use_buffer_pid_tid;
146 pid_t pid, tid;
147 int cpu;
148 int switch_state;
149 pid_t next_tid;
150 struct thread *thread;
151 bool exclude_kernel;
152 bool have_sample;
153 u64 time;
154 u64 timestamp;
155 u32 flags;
156 u16 insn_len;
Adrian Hunter2a21d032015-07-17 19:33:48 +0300157 u64 last_insn_cnt;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300158 char insn[INTEL_PT_INSN_BUF_SZ];
Adrian Hunter90e457f2015-07-17 19:33:41 +0300159};
160
161static void intel_pt_dump(struct intel_pt *pt __maybe_unused,
162 unsigned char *buf, size_t len)
163{
164 struct intel_pt_pkt packet;
165 size_t pos = 0;
166 int ret, pkt_len, i;
167 char desc[INTEL_PT_PKT_DESC_MAX];
168 const char *color = PERF_COLOR_BLUE;
169
170 color_fprintf(stdout, color,
171 ". ... Intel Processor Trace data: size %zu bytes\n",
172 len);
173
174 while (len) {
175 ret = intel_pt_get_packet(buf, len, &packet);
176 if (ret > 0)
177 pkt_len = ret;
178 else
179 pkt_len = 1;
180 printf(".");
181 color_fprintf(stdout, color, " %08x: ", pos);
182 for (i = 0; i < pkt_len; i++)
183 color_fprintf(stdout, color, " %02x", buf[i]);
184 for (; i < 16; i++)
185 color_fprintf(stdout, color, " ");
186 if (ret > 0) {
187 ret = intel_pt_pkt_desc(&packet, desc,
188 INTEL_PT_PKT_DESC_MAX);
189 if (ret > 0)
190 color_fprintf(stdout, color, " %s\n", desc);
191 } else {
192 color_fprintf(stdout, color, " Bad packet!\n");
193 }
194 pos += pkt_len;
195 buf += pkt_len;
196 len -= pkt_len;
197 }
198}
199
200static void intel_pt_dump_event(struct intel_pt *pt, unsigned char *buf,
201 size_t len)
202{
203 printf(".\n");
204 intel_pt_dump(pt, buf, len);
205}
206
207static int intel_pt_do_fix_overlap(struct intel_pt *pt, struct auxtrace_buffer *a,
208 struct auxtrace_buffer *b)
209{
Adrian Hunter117db4b2018-03-07 16:02:21 +0200210 bool consecutive = false;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300211 void *start;
212
213 start = intel_pt_find_overlap(a->data, a->size, b->data, b->size,
Adrian Hunter117db4b2018-03-07 16:02:21 +0200214 pt->have_tsc, &consecutive);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300215 if (!start)
216 return -EINVAL;
217 b->use_size = b->data + b->size - start;
218 b->use_data = start;
Adrian Hunter117db4b2018-03-07 16:02:21 +0200219 if (b->use_size && consecutive)
220 b->consecutive = true;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300221 return 0;
222}
223
224static void intel_pt_use_buffer_pid_tid(struct intel_pt_queue *ptq,
225 struct auxtrace_queue *queue,
226 struct auxtrace_buffer *buffer)
227{
228 if (queue->cpu == -1 && buffer->cpu != -1)
229 ptq->cpu = buffer->cpu;
230
231 ptq->pid = buffer->pid;
232 ptq->tid = buffer->tid;
233
234 intel_pt_log("queue %u cpu %d pid %d tid %d\n",
235 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
236
237 thread__zput(ptq->thread);
238
239 if (ptq->tid != -1) {
240 if (ptq->pid != -1)
241 ptq->thread = machine__findnew_thread(ptq->pt->machine,
242 ptq->pid,
243 ptq->tid);
244 else
245 ptq->thread = machine__find_thread(ptq->pt->machine, -1,
246 ptq->tid);
247 }
248}
249
250/* This function assumes data is processed sequentially only */
251static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data)
252{
253 struct intel_pt_queue *ptq = data;
254 struct auxtrace_buffer *buffer = ptq->buffer, *old_buffer = buffer;
255 struct auxtrace_queue *queue;
256
257 if (ptq->stop) {
258 b->len = 0;
259 return 0;
260 }
261
262 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
Adrian Hunter810c3982016-09-23 17:38:41 +0300263next:
Adrian Hunter90e457f2015-07-17 19:33:41 +0300264 buffer = auxtrace_buffer__next(queue, buffer);
265 if (!buffer) {
266 if (old_buffer)
267 auxtrace_buffer__drop_data(old_buffer);
268 b->len = 0;
269 return 0;
270 }
271
272 ptq->buffer = buffer;
273
274 if (!buffer->data) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100275 int fd = perf_data__fd(ptq->pt->session->data);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300276
277 buffer->data = auxtrace_buffer__get_data(buffer, fd);
278 if (!buffer->data)
279 return -ENOMEM;
280 }
281
282 if (ptq->pt->snapshot_mode && !buffer->consecutive && old_buffer &&
283 intel_pt_do_fix_overlap(ptq->pt, old_buffer, buffer))
284 return -ENOMEM;
285
Adrian Hunter90e457f2015-07-17 19:33:41 +0300286 if (buffer->use_data) {
287 b->len = buffer->use_size;
288 b->buf = buffer->use_data;
289 } else {
290 b->len = buffer->size;
291 b->buf = buffer->data;
292 }
293 b->ref_timestamp = buffer->reference;
294
Adrian Hunter810c3982016-09-23 17:38:41 +0300295 /*
296 * If in snapshot mode and the buffer has no usable data, get next
297 * buffer and again check overlap against old_buffer.
298 */
299 if (ptq->pt->snapshot_mode && !b->len)
300 goto next;
301
302 if (old_buffer)
303 auxtrace_buffer__drop_data(old_buffer);
304
Adrian Hunter90e457f2015-07-17 19:33:41 +0300305 if (!old_buffer || ptq->pt->sampling_mode || (ptq->pt->snapshot_mode &&
306 !buffer->consecutive)) {
307 b->consecutive = false;
308 b->trace_nr = buffer->buffer_nr + 1;
309 } else {
310 b->consecutive = true;
311 }
312
313 if (ptq->use_buffer_pid_tid && (ptq->pid != buffer->pid ||
314 ptq->tid != buffer->tid))
315 intel_pt_use_buffer_pid_tid(ptq, queue, buffer);
316
317 if (ptq->step_through_buffers)
318 ptq->stop = true;
319
320 if (!b->len)
321 return intel_pt_get_trace(b, data);
322
323 return 0;
324}
325
326struct intel_pt_cache_entry {
327 struct auxtrace_cache_entry entry;
328 u64 insn_cnt;
329 u64 byte_cnt;
330 enum intel_pt_insn_op op;
331 enum intel_pt_insn_branch branch;
332 int length;
333 int32_t rel;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300334 char insn[INTEL_PT_INSN_BUF_SZ];
Adrian Hunter90e457f2015-07-17 19:33:41 +0300335};
336
337static int intel_pt_config_div(const char *var, const char *value, void *data)
338{
339 int *d = data;
340 long val;
341
342 if (!strcmp(var, "intel-pt.cache-divisor")) {
343 val = strtol(value, NULL, 0);
344 if (val > 0 && val <= INT_MAX)
345 *d = val;
346 }
347
348 return 0;
349}
350
351static int intel_pt_cache_divisor(void)
352{
353 static int d;
354
355 if (d)
356 return d;
357
358 perf_config(intel_pt_config_div, &d);
359
360 if (!d)
361 d = 64;
362
363 return d;
364}
365
366static unsigned int intel_pt_cache_size(struct dso *dso,
367 struct machine *machine)
368{
369 off_t size;
370
371 size = dso__data_size(dso, machine);
372 size /= intel_pt_cache_divisor();
373 if (size < 1000)
374 return 10;
375 if (size > (1 << 21))
376 return 21;
377 return 32 - __builtin_clz(size);
378}
379
380static struct auxtrace_cache *intel_pt_cache(struct dso *dso,
381 struct machine *machine)
382{
383 struct auxtrace_cache *c;
384 unsigned int bits;
385
386 if (dso->auxtrace_cache)
387 return dso->auxtrace_cache;
388
389 bits = intel_pt_cache_size(dso, machine);
390
391 /* Ignoring cache creation failure */
392 c = auxtrace_cache__new(bits, sizeof(struct intel_pt_cache_entry), 200);
393
394 dso->auxtrace_cache = c;
395
396 return c;
397}
398
399static int intel_pt_cache_add(struct dso *dso, struct machine *machine,
400 u64 offset, u64 insn_cnt, u64 byte_cnt,
401 struct intel_pt_insn *intel_pt_insn)
402{
403 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
404 struct intel_pt_cache_entry *e;
405 int err;
406
407 if (!c)
408 return -ENOMEM;
409
410 e = auxtrace_cache__alloc_entry(c);
411 if (!e)
412 return -ENOMEM;
413
414 e->insn_cnt = insn_cnt;
415 e->byte_cnt = byte_cnt;
416 e->op = intel_pt_insn->op;
417 e->branch = intel_pt_insn->branch;
418 e->length = intel_pt_insn->length;
419 e->rel = intel_pt_insn->rel;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300420 memcpy(e->insn, intel_pt_insn->buf, INTEL_PT_INSN_BUF_SZ);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300421
422 err = auxtrace_cache__add(c, offset, &e->entry);
423 if (err)
424 auxtrace_cache__free_entry(c, e);
425
426 return err;
427}
428
429static struct intel_pt_cache_entry *
430intel_pt_cache_lookup(struct dso *dso, struct machine *machine, u64 offset)
431{
432 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
433
434 if (!c)
435 return NULL;
436
437 return auxtrace_cache__lookup(dso->auxtrace_cache, offset);
438}
439
440static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
441 uint64_t *insn_cnt_ptr, uint64_t *ip,
442 uint64_t to_ip, uint64_t max_insn_cnt,
443 void *data)
444{
445 struct intel_pt_queue *ptq = data;
446 struct machine *machine = ptq->pt->machine;
447 struct thread *thread;
448 struct addr_location al;
Adrian Hunter32f98aa2016-10-07 16:42:25 +0300449 unsigned char buf[INTEL_PT_INSN_BUF_SZ];
Adrian Hunter90e457f2015-07-17 19:33:41 +0300450 ssize_t len;
451 int x86_64;
452 u8 cpumode;
453 u64 offset, start_offset, start_ip;
454 u64 insn_cnt = 0;
455 bool one_map = true;
456
Andi Kleenfaaa8762016-10-07 16:42:26 +0300457 intel_pt_insn->length = 0;
458
Adrian Hunter90e457f2015-07-17 19:33:41 +0300459 if (to_ip && *ip == to_ip)
460 goto out_no_cache;
461
Adrian Hunter90e457f2015-07-17 19:33:41 +0300462 if (*ip >= ptq->pt->kernel_start)
463 cpumode = PERF_RECORD_MISC_KERNEL;
464 else
465 cpumode = PERF_RECORD_MISC_USER;
466
467 thread = ptq->thread;
468 if (!thread) {
469 if (cpumode != PERF_RECORD_MISC_KERNEL)
470 return -EINVAL;
471 thread = ptq->pt->unknown_thread;
472 }
473
474 while (1) {
475 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, *ip, &al);
476 if (!al.map || !al.map->dso)
477 return -EINVAL;
478
479 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
480 dso__data_status_seen(al.map->dso,
481 DSO_DATA_STATUS_SEEN_ITRACE))
482 return -ENOENT;
483
484 offset = al.map->map_ip(al.map, *ip);
485
486 if (!to_ip && one_map) {
487 struct intel_pt_cache_entry *e;
488
489 e = intel_pt_cache_lookup(al.map->dso, machine, offset);
490 if (e &&
491 (!max_insn_cnt || e->insn_cnt <= max_insn_cnt)) {
492 *insn_cnt_ptr = e->insn_cnt;
493 *ip += e->byte_cnt;
494 intel_pt_insn->op = e->op;
495 intel_pt_insn->branch = e->branch;
496 intel_pt_insn->length = e->length;
497 intel_pt_insn->rel = e->rel;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300498 memcpy(intel_pt_insn->buf, e->insn,
499 INTEL_PT_INSN_BUF_SZ);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300500 intel_pt_log_insn_no_data(intel_pt_insn, *ip);
501 return 0;
502 }
503 }
504
505 start_offset = offset;
506 start_ip = *ip;
507
508 /* Load maps to ensure dso->is_64_bit has been updated */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300509 map__load(al.map);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300510
511 x86_64 = al.map->dso->is_64_bit;
512
513 while (1) {
514 len = dso__data_read_offset(al.map->dso, machine,
Adrian Hunter32f98aa2016-10-07 16:42:25 +0300515 offset, buf,
516 INTEL_PT_INSN_BUF_SZ);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300517 if (len <= 0)
518 return -EINVAL;
519
520 if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn))
521 return -EINVAL;
522
523 intel_pt_log_insn(intel_pt_insn, *ip);
524
525 insn_cnt += 1;
526
527 if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH)
528 goto out;
529
530 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
531 goto out_no_cache;
532
533 *ip += intel_pt_insn->length;
534
535 if (to_ip && *ip == to_ip)
536 goto out_no_cache;
537
538 if (*ip >= al.map->end)
539 break;
540
541 offset += intel_pt_insn->length;
542 }
543 one_map = false;
544 }
545out:
546 *insn_cnt_ptr = insn_cnt;
547
548 if (!one_map)
549 goto out_no_cache;
550
551 /*
552 * Didn't lookup in the 'to_ip' case, so do it now to prevent duplicate
553 * entries.
554 */
555 if (to_ip) {
556 struct intel_pt_cache_entry *e;
557
558 e = intel_pt_cache_lookup(al.map->dso, machine, start_offset);
559 if (e)
560 return 0;
561 }
562
563 /* Ignore cache errors */
564 intel_pt_cache_add(al.map->dso, machine, start_offset, insn_cnt,
565 *ip - start_ip, intel_pt_insn);
566
567 return 0;
568
569out_no_cache:
570 *insn_cnt_ptr = insn_cnt;
571 return 0;
572}
573
Adrian Hunter2acee102016-09-23 17:38:48 +0300574static bool intel_pt_match_pgd_ip(struct intel_pt *pt, uint64_t ip,
575 uint64_t offset, const char *filename)
576{
577 struct addr_filter *filt;
578 bool have_filter = false;
579 bool hit_tracestop = false;
580 bool hit_filter = false;
581
582 list_for_each_entry(filt, &pt->filts.head, list) {
583 if (filt->start)
584 have_filter = true;
585
586 if ((filename && !filt->filename) ||
587 (!filename && filt->filename) ||
588 (filename && strcmp(filename, filt->filename)))
589 continue;
590
591 if (!(offset >= filt->addr && offset < filt->addr + filt->size))
592 continue;
593
594 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s hit filter: %s offset %#"PRIx64" size %#"PRIx64"\n",
595 ip, offset, filename ? filename : "[kernel]",
596 filt->start ? "filter" : "stop",
597 filt->addr, filt->size);
598
599 if (filt->start)
600 hit_filter = true;
601 else
602 hit_tracestop = true;
603 }
604
605 if (!hit_tracestop && !hit_filter)
606 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s is not in a filter region\n",
607 ip, offset, filename ? filename : "[kernel]");
608
609 return hit_tracestop || (have_filter && !hit_filter);
610}
611
612static int __intel_pt_pgd_ip(uint64_t ip, void *data)
613{
614 struct intel_pt_queue *ptq = data;
615 struct thread *thread;
616 struct addr_location al;
617 u8 cpumode;
618 u64 offset;
619
620 if (ip >= ptq->pt->kernel_start)
621 return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL);
622
623 cpumode = PERF_RECORD_MISC_USER;
624
625 thread = ptq->thread;
626 if (!thread)
627 return -EINVAL;
628
629 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, ip, &al);
630 if (!al.map || !al.map->dso)
631 return -EINVAL;
632
633 offset = al.map->map_ip(al.map, ip);
634
635 return intel_pt_match_pgd_ip(ptq->pt, ip, offset,
636 al.map->dso->long_name);
637}
638
639static bool intel_pt_pgd_ip(uint64_t ip, void *data)
640{
641 return __intel_pt_pgd_ip(ip, data) > 0;
642}
643
Adrian Hunter90e457f2015-07-17 19:33:41 +0300644static bool intel_pt_get_config(struct intel_pt *pt,
645 struct perf_event_attr *attr, u64 *config)
646{
647 if (attr->type == pt->pmu_type) {
648 if (config)
649 *config = attr->config;
650 return true;
651 }
652
653 return false;
654}
655
656static bool intel_pt_exclude_kernel(struct intel_pt *pt)
657{
658 struct perf_evsel *evsel;
659
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300660 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300661 if (intel_pt_get_config(pt, &evsel->attr, NULL) &&
662 !evsel->attr.exclude_kernel)
663 return false;
664 }
665 return true;
666}
667
668static bool intel_pt_return_compression(struct intel_pt *pt)
669{
670 struct perf_evsel *evsel;
671 u64 config;
672
673 if (!pt->noretcomp_bit)
674 return true;
675
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300676 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300677 if (intel_pt_get_config(pt, &evsel->attr, &config) &&
678 (config & pt->noretcomp_bit))
679 return false;
680 }
681 return true;
682}
683
Adrian Hunter83959812017-05-26 11:17:11 +0300684static bool intel_pt_branch_enable(struct intel_pt *pt)
685{
686 struct perf_evsel *evsel;
687 u64 config;
688
689 evlist__for_each_entry(pt->session->evlist, evsel) {
690 if (intel_pt_get_config(pt, &evsel->attr, &config) &&
691 (config & 1) && !(config & 0x2000))
692 return false;
693 }
694 return true;
695}
696
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300697static unsigned int intel_pt_mtc_period(struct intel_pt *pt)
698{
699 struct perf_evsel *evsel;
700 unsigned int shift;
701 u64 config;
702
703 if (!pt->mtc_freq_bits)
704 return 0;
705
706 for (shift = 0, config = pt->mtc_freq_bits; !(config & 1); shift++)
707 config >>= 1;
708
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300709 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300710 if (intel_pt_get_config(pt, &evsel->attr, &config))
711 return (config & pt->mtc_freq_bits) >> shift;
712 }
713 return 0;
714}
715
Adrian Hunter90e457f2015-07-17 19:33:41 +0300716static bool intel_pt_timeless_decoding(struct intel_pt *pt)
717{
718 struct perf_evsel *evsel;
719 bool timeless_decoding = true;
720 u64 config;
721
722 if (!pt->tsc_bit || !pt->cap_user_time_zero)
723 return true;
724
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300725 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300726 if (!(evsel->attr.sample_type & PERF_SAMPLE_TIME))
727 return true;
728 if (intel_pt_get_config(pt, &evsel->attr, &config)) {
729 if (config & pt->tsc_bit)
730 timeless_decoding = false;
731 else
732 return true;
733 }
734 }
735 return timeless_decoding;
736}
737
738static bool intel_pt_tracing_kernel(struct intel_pt *pt)
739{
740 struct perf_evsel *evsel;
741
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300742 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300743 if (intel_pt_get_config(pt, &evsel->attr, NULL) &&
744 !evsel->attr.exclude_kernel)
745 return true;
746 }
747 return false;
748}
749
750static bool intel_pt_have_tsc(struct intel_pt *pt)
751{
752 struct perf_evsel *evsel;
753 bool have_tsc = false;
754 u64 config;
755
756 if (!pt->tsc_bit)
757 return false;
758
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300759 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300760 if (intel_pt_get_config(pt, &evsel->attr, &config)) {
761 if (config & pt->tsc_bit)
762 have_tsc = true;
763 else
764 return false;
765 }
766 }
767 return have_tsc;
768}
769
770static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns)
771{
772 u64 quot, rem;
773
774 quot = ns / pt->tc.time_mult;
775 rem = ns % pt->tc.time_mult;
776 return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) /
777 pt->tc.time_mult;
778}
779
780static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt,
781 unsigned int queue_nr)
782{
783 struct intel_pt_params params = { .get_trace = 0, };
784 struct intel_pt_queue *ptq;
785
786 ptq = zalloc(sizeof(struct intel_pt_queue));
787 if (!ptq)
788 return NULL;
789
790 if (pt->synth_opts.callchain) {
791 size_t sz = sizeof(struct ip_callchain);
792
793 sz += pt->synth_opts.callchain_sz * sizeof(u64);
794 ptq->chain = zalloc(sz);
795 if (!ptq->chain)
796 goto out_free;
797 }
798
Adrian Hunterf14445e2015-09-25 16:15:45 +0300799 if (pt->synth_opts.last_branch) {
800 size_t sz = sizeof(struct branch_stack);
801
802 sz += pt->synth_opts.last_branch_sz *
803 sizeof(struct branch_entry);
804 ptq->last_branch = zalloc(sz);
805 if (!ptq->last_branch)
806 goto out_free;
807 ptq->last_branch_rb = zalloc(sz);
808 if (!ptq->last_branch_rb)
809 goto out_free;
810 }
811
Adrian Hunter90e457f2015-07-17 19:33:41 +0300812 ptq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
813 if (!ptq->event_buf)
814 goto out_free;
815
816 ptq->pt = pt;
817 ptq->queue_nr = queue_nr;
818 ptq->exclude_kernel = intel_pt_exclude_kernel(pt);
819 ptq->pid = -1;
820 ptq->tid = -1;
821 ptq->cpu = -1;
822 ptq->next_tid = -1;
823
824 params.get_trace = intel_pt_get_trace;
825 params.walk_insn = intel_pt_walk_next_insn;
826 params.data = ptq;
827 params.return_compression = intel_pt_return_compression(pt);
Adrian Hunter83959812017-05-26 11:17:11 +0300828 params.branch_enable = intel_pt_branch_enable(pt);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300829 params.max_non_turbo_ratio = pt->max_non_turbo_ratio;
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300830 params.mtc_period = intel_pt_mtc_period(pt);
831 params.tsc_ctc_ratio_n = pt->tsc_ctc_ratio_n;
832 params.tsc_ctc_ratio_d = pt->tsc_ctc_ratio_d;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300833
Adrian Hunter2acee102016-09-23 17:38:48 +0300834 if (pt->filts.cnt > 0)
835 params.pgd_ip = intel_pt_pgd_ip;
836
Adrian Hunter90e457f2015-07-17 19:33:41 +0300837 if (pt->synth_opts.instructions) {
838 if (pt->synth_opts.period) {
839 switch (pt->synth_opts.period_type) {
840 case PERF_ITRACE_PERIOD_INSTRUCTIONS:
841 params.period_type =
842 INTEL_PT_PERIOD_INSTRUCTIONS;
843 params.period = pt->synth_opts.period;
844 break;
845 case PERF_ITRACE_PERIOD_TICKS:
846 params.period_type = INTEL_PT_PERIOD_TICKS;
847 params.period = pt->synth_opts.period;
848 break;
849 case PERF_ITRACE_PERIOD_NANOSECS:
850 params.period_type = INTEL_PT_PERIOD_TICKS;
851 params.period = intel_pt_ns_to_ticks(pt,
852 pt->synth_opts.period);
853 break;
854 default:
855 break;
856 }
857 }
858
859 if (!params.period) {
860 params.period_type = INTEL_PT_PERIOD_INSTRUCTIONS;
Adrian Huntere1791342015-09-25 16:15:32 +0300861 params.period = 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300862 }
863 }
864
865 ptq->decoder = intel_pt_decoder_new(&params);
866 if (!ptq->decoder)
867 goto out_free;
868
869 return ptq;
870
871out_free:
872 zfree(&ptq->event_buf);
Adrian Hunterf14445e2015-09-25 16:15:45 +0300873 zfree(&ptq->last_branch);
874 zfree(&ptq->last_branch_rb);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300875 zfree(&ptq->chain);
876 free(ptq);
877 return NULL;
878}
879
880static void intel_pt_free_queue(void *priv)
881{
882 struct intel_pt_queue *ptq = priv;
883
884 if (!ptq)
885 return;
886 thread__zput(ptq->thread);
887 intel_pt_decoder_free(ptq->decoder);
888 zfree(&ptq->event_buf);
Adrian Hunterf14445e2015-09-25 16:15:45 +0300889 zfree(&ptq->last_branch);
890 zfree(&ptq->last_branch_rb);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300891 zfree(&ptq->chain);
892 free(ptq);
893}
894
895static void intel_pt_set_pid_tid_cpu(struct intel_pt *pt,
896 struct auxtrace_queue *queue)
897{
898 struct intel_pt_queue *ptq = queue->priv;
899
900 if (queue->tid == -1 || pt->have_sched_switch) {
901 ptq->tid = machine__get_current_tid(pt->machine, ptq->cpu);
902 thread__zput(ptq->thread);
903 }
904
905 if (!ptq->thread && ptq->tid != -1)
906 ptq->thread = machine__find_thread(pt->machine, -1, ptq->tid);
907
908 if (ptq->thread) {
909 ptq->pid = ptq->thread->pid_;
910 if (queue->cpu == -1)
911 ptq->cpu = ptq->thread->cpu;
912 }
913}
914
915static void intel_pt_sample_flags(struct intel_pt_queue *ptq)
916{
917 if (ptq->state->flags & INTEL_PT_ABORT_TX) {
918 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT;
919 } else if (ptq->state->flags & INTEL_PT_ASYNC) {
920 if (ptq->state->to_ip)
921 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
922 PERF_IP_FLAG_ASYNC |
923 PERF_IP_FLAG_INTERRUPT;
924 else
925 ptq->flags = PERF_IP_FLAG_BRANCH |
926 PERF_IP_FLAG_TRACE_END;
927 ptq->insn_len = 0;
928 } else {
929 if (ptq->state->from_ip)
930 ptq->flags = intel_pt_insn_type(ptq->state->insn_op);
931 else
932 ptq->flags = PERF_IP_FLAG_BRANCH |
933 PERF_IP_FLAG_TRACE_BEGIN;
934 if (ptq->state->flags & INTEL_PT_IN_TX)
935 ptq->flags |= PERF_IP_FLAG_IN_TX;
936 ptq->insn_len = ptq->state->insn_len;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300937 memcpy(ptq->insn, ptq->state->insn, INTEL_PT_INSN_BUF_SZ);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300938 }
939}
940
941static int intel_pt_setup_queue(struct intel_pt *pt,
942 struct auxtrace_queue *queue,
943 unsigned int queue_nr)
944{
945 struct intel_pt_queue *ptq = queue->priv;
946
947 if (list_empty(&queue->head))
948 return 0;
949
950 if (!ptq) {
951 ptq = intel_pt_alloc_queue(pt, queue_nr);
952 if (!ptq)
953 return -ENOMEM;
954 queue->priv = ptq;
955
956 if (queue->cpu != -1)
957 ptq->cpu = queue->cpu;
958 ptq->tid = queue->tid;
959
960 if (pt->sampling_mode) {
961 if (pt->timeless_decoding)
962 ptq->step_through_buffers = true;
963 if (pt->timeless_decoding || !pt->have_sched_switch)
964 ptq->use_buffer_pid_tid = true;
965 }
966 }
967
968 if (!ptq->on_heap &&
969 (!pt->sync_switch ||
970 ptq->switch_state != INTEL_PT_SS_EXPECTING_SWITCH_EVENT)) {
971 const struct intel_pt_state *state;
972 int ret;
973
974 if (pt->timeless_decoding)
975 return 0;
976
977 intel_pt_log("queue %u getting timestamp\n", queue_nr);
978 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
979 queue_nr, ptq->cpu, ptq->pid, ptq->tid);
980 while (1) {
981 state = intel_pt_decode(ptq->decoder);
982 if (state->err) {
983 if (state->err == INTEL_PT_ERR_NODATA) {
984 intel_pt_log("queue %u has no timestamp\n",
985 queue_nr);
986 return 0;
987 }
988 continue;
989 }
990 if (state->timestamp)
991 break;
992 }
993
994 ptq->timestamp = state->timestamp;
995 intel_pt_log("queue %u timestamp 0x%" PRIx64 "\n",
996 queue_nr, ptq->timestamp);
997 ptq->state = state;
998 ptq->have_sample = true;
999 intel_pt_sample_flags(ptq);
1000 ret = auxtrace_heap__add(&pt->heap, queue_nr, ptq->timestamp);
1001 if (ret)
1002 return ret;
1003 ptq->on_heap = true;
1004 }
1005
1006 return 0;
1007}
1008
1009static int intel_pt_setup_queues(struct intel_pt *pt)
1010{
1011 unsigned int i;
1012 int ret;
1013
1014 for (i = 0; i < pt->queues.nr_queues; i++) {
1015 ret = intel_pt_setup_queue(pt, &pt->queues.queue_array[i], i);
1016 if (ret)
1017 return ret;
1018 }
1019 return 0;
1020}
1021
Adrian Hunterf14445e2015-09-25 16:15:45 +03001022static inline void intel_pt_copy_last_branch_rb(struct intel_pt_queue *ptq)
1023{
1024 struct branch_stack *bs_src = ptq->last_branch_rb;
1025 struct branch_stack *bs_dst = ptq->last_branch;
1026 size_t nr = 0;
1027
1028 bs_dst->nr = bs_src->nr;
1029
1030 if (!bs_src->nr)
1031 return;
1032
1033 nr = ptq->pt->synth_opts.last_branch_sz - ptq->last_branch_pos;
1034 memcpy(&bs_dst->entries[0],
1035 &bs_src->entries[ptq->last_branch_pos],
1036 sizeof(struct branch_entry) * nr);
1037
1038 if (bs_src->nr >= ptq->pt->synth_opts.last_branch_sz) {
1039 memcpy(&bs_dst->entries[nr],
1040 &bs_src->entries[0],
1041 sizeof(struct branch_entry) * ptq->last_branch_pos);
1042 }
1043}
1044
1045static inline void intel_pt_reset_last_branch_rb(struct intel_pt_queue *ptq)
1046{
1047 ptq->last_branch_pos = 0;
1048 ptq->last_branch_rb->nr = 0;
1049}
1050
1051static void intel_pt_update_last_branch_rb(struct intel_pt_queue *ptq)
1052{
1053 const struct intel_pt_state *state = ptq->state;
1054 struct branch_stack *bs = ptq->last_branch_rb;
1055 struct branch_entry *be;
1056
1057 if (!ptq->last_branch_pos)
1058 ptq->last_branch_pos = ptq->pt->synth_opts.last_branch_sz;
1059
1060 ptq->last_branch_pos -= 1;
1061
1062 be = &bs->entries[ptq->last_branch_pos];
1063 be->from = state->from_ip;
1064 be->to = state->to_ip;
1065 be->flags.abort = !!(state->flags & INTEL_PT_ABORT_TX);
1066 be->flags.in_tx = !!(state->flags & INTEL_PT_IN_TX);
1067 /* No support for mispredict */
Adrian Hunterba11ba62015-09-25 16:15:56 +03001068 be->flags.mispred = ptq->pt->mispred_all;
Adrian Hunterf14445e2015-09-25 16:15:45 +03001069
1070 if (bs->nr < ptq->pt->synth_opts.last_branch_sz)
1071 bs->nr += 1;
1072}
1073
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001074static inline bool intel_pt_skip_event(struct intel_pt *pt)
1075{
1076 return pt->synth_opts.initial_skip &&
1077 pt->num_events++ < pt->synth_opts.initial_skip;
1078}
1079
1080static void intel_pt_prep_b_sample(struct intel_pt *pt,
1081 struct intel_pt_queue *ptq,
1082 union perf_event *event,
1083 struct perf_sample *sample)
1084{
1085 event->sample.header.type = PERF_RECORD_SAMPLE;
1086 event->sample.header.misc = PERF_RECORD_MISC_USER;
1087 event->sample.header.size = sizeof(struct perf_event_header);
1088
1089 if (!pt->timeless_decoding)
1090 sample->time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
1091
1092 sample->cpumode = PERF_RECORD_MISC_USER;
1093 sample->ip = ptq->state->from_ip;
1094 sample->pid = ptq->pid;
1095 sample->tid = ptq->tid;
1096 sample->addr = ptq->state->to_ip;
1097 sample->period = 1;
1098 sample->cpu = ptq->cpu;
1099 sample->flags = ptq->flags;
1100 sample->insn_len = ptq->insn_len;
1101 memcpy(sample->insn, ptq->insn, INTEL_PT_INSN_BUF_SZ);
1102}
1103
Adrian Hunter90e457f2015-07-17 19:33:41 +03001104static int intel_pt_inject_event(union perf_event *event,
Adrian Huntera10eb532018-01-16 15:14:50 +02001105 struct perf_sample *sample, u64 type)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001106{
1107 event->header.size = perf_event__sample_event_size(sample, type, 0);
Adrian Hunter936f1f32018-01-16 15:14:52 +02001108 return perf_event__synthesize_sample(event, type, 0, sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001109}
1110
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001111static inline int intel_pt_opt_inject(struct intel_pt *pt,
1112 union perf_event *event,
1113 struct perf_sample *sample, u64 type)
1114{
1115 if (!pt->synth_opts.inject)
1116 return 0;
1117
Adrian Huntera10eb532018-01-16 15:14:50 +02001118 return intel_pt_inject_event(event, sample, type);
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001119}
1120
1121static int intel_pt_deliver_synth_b_event(struct intel_pt *pt,
1122 union perf_event *event,
1123 struct perf_sample *sample, u64 type)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001124{
1125 int ret;
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001126
1127 ret = intel_pt_opt_inject(pt, event, sample, type);
1128 if (ret)
1129 return ret;
1130
1131 ret = perf_session__deliver_synth_event(pt->session, event, sample);
1132 if (ret)
1133 pr_err("Intel PT: failed to deliver event, error %d\n", ret);
1134
1135 return ret;
1136}
1137
1138static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq)
1139{
Adrian Hunter90e457f2015-07-17 19:33:41 +03001140 struct intel_pt *pt = ptq->pt;
1141 union perf_event *event = ptq->event_buf;
1142 struct perf_sample sample = { .ip = 0, };
Adrian Hunterf14445e2015-09-25 16:15:45 +03001143 struct dummy_branch_stack {
1144 u64 nr;
1145 struct branch_entry entries;
1146 } dummy_bs;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001147
Adrian Hunter385e3302015-09-25 16:15:44 +03001148 if (pt->branches_filter && !(pt->branches_filter & ptq->flags))
1149 return 0;
1150
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001151 if (intel_pt_skip_event(pt))
Andi Kleend1706b32016-03-28 10:45:38 -07001152 return 0;
1153
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001154 intel_pt_prep_b_sample(pt, ptq, event, &sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001155
Adrian Hunter90e457f2015-07-17 19:33:41 +03001156 sample.id = ptq->pt->branches_id;
1157 sample.stream_id = ptq->pt->branches_id;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001158
Adrian Hunterf14445e2015-09-25 16:15:45 +03001159 /*
1160 * perf report cannot handle events without a branch stack when using
1161 * SORT_MODE__BRANCH so make a dummy one.
1162 */
1163 if (pt->synth_opts.last_branch && sort__mode == SORT_MODE__BRANCH) {
1164 dummy_bs = (struct dummy_branch_stack){
1165 .nr = 1,
1166 .entries = {
1167 .from = sample.ip,
1168 .to = sample.addr,
1169 },
1170 };
1171 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1172 }
1173
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001174 return intel_pt_deliver_synth_b_event(pt, event, &sample,
1175 pt->branches_sample_type);
1176}
1177
1178static void intel_pt_prep_sample(struct intel_pt *pt,
1179 struct intel_pt_queue *ptq,
1180 union perf_event *event,
1181 struct perf_sample *sample)
1182{
1183 intel_pt_prep_b_sample(pt, ptq, event, sample);
1184
1185 if (pt->synth_opts.callchain) {
1186 thread_stack__sample(ptq->thread, ptq->chain,
1187 pt->synth_opts.callchain_sz, sample->ip);
1188 sample->callchain = ptq->chain;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001189 }
1190
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001191 if (pt->synth_opts.last_branch) {
1192 intel_pt_copy_last_branch_rb(ptq);
1193 sample->branch_stack = ptq->last_branch;
1194 }
1195}
1196
1197static inline int intel_pt_deliver_synth_event(struct intel_pt *pt,
1198 struct intel_pt_queue *ptq,
1199 union perf_event *event,
1200 struct perf_sample *sample,
1201 u64 type)
1202{
1203 int ret;
1204
1205 ret = intel_pt_deliver_synth_b_event(pt, event, sample, type);
1206
1207 if (pt->synth_opts.last_branch)
1208 intel_pt_reset_last_branch_rb(ptq);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001209
1210 return ret;
1211}
1212
1213static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq)
1214{
Adrian Hunter90e457f2015-07-17 19:33:41 +03001215 struct intel_pt *pt = ptq->pt;
1216 union perf_event *event = ptq->event_buf;
1217 struct perf_sample sample = { .ip = 0, };
1218
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001219 if (intel_pt_skip_event(pt))
Andi Kleend1706b32016-03-28 10:45:38 -07001220 return 0;
1221
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001222 intel_pt_prep_sample(pt, ptq, event, &sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001223
Adrian Hunter90e457f2015-07-17 19:33:41 +03001224 sample.id = ptq->pt->instructions_id;
1225 sample.stream_id = ptq->pt->instructions_id;
Adrian Hunter2a21d032015-07-17 19:33:48 +03001226 sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001227
Adrian Hunter2a21d032015-07-17 19:33:48 +03001228 ptq->last_insn_cnt = ptq->state->tot_insn_cnt;
1229
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001230 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1231 pt->instructions_sample_type);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001232}
1233
1234static int intel_pt_synth_transaction_sample(struct intel_pt_queue *ptq)
1235{
Adrian Hunter90e457f2015-07-17 19:33:41 +03001236 struct intel_pt *pt = ptq->pt;
1237 union perf_event *event = ptq->event_buf;
1238 struct perf_sample sample = { .ip = 0, };
1239
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001240 if (intel_pt_skip_event(pt))
Andi Kleend1706b32016-03-28 10:45:38 -07001241 return 0;
1242
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001243 intel_pt_prep_sample(pt, ptq, event, &sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001244
Adrian Hunter90e457f2015-07-17 19:33:41 +03001245 sample.id = ptq->pt->transactions_id;
1246 sample.stream_id = ptq->pt->transactions_id;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001247
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001248 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1249 pt->transactions_sample_type);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001250}
1251
Adrian Hunter37973072017-06-30 11:36:45 +03001252static void intel_pt_prep_p_sample(struct intel_pt *pt,
1253 struct intel_pt_queue *ptq,
1254 union perf_event *event,
1255 struct perf_sample *sample)
1256{
1257 intel_pt_prep_sample(pt, ptq, event, sample);
1258
1259 /*
1260 * Zero IP is used to mean "trace start" but that is not the case for
1261 * power or PTWRITE events with no IP, so clear the flags.
1262 */
1263 if (!sample->ip)
1264 sample->flags = 0;
1265}
1266
1267static int intel_pt_synth_ptwrite_sample(struct intel_pt_queue *ptq)
1268{
1269 struct intel_pt *pt = ptq->pt;
1270 union perf_event *event = ptq->event_buf;
1271 struct perf_sample sample = { .ip = 0, };
1272 struct perf_synth_intel_ptwrite raw;
1273
1274 if (intel_pt_skip_event(pt))
1275 return 0;
1276
1277 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1278
1279 sample.id = ptq->pt->ptwrites_id;
1280 sample.stream_id = ptq->pt->ptwrites_id;
1281
1282 raw.flags = 0;
1283 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
1284 raw.payload = cpu_to_le64(ptq->state->ptw_payload);
1285
1286 sample.raw_size = perf_synth__raw_size(raw);
1287 sample.raw_data = perf_synth__raw_data(&raw);
1288
1289 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1290 pt->ptwrites_sample_type);
1291}
1292
1293static int intel_pt_synth_cbr_sample(struct intel_pt_queue *ptq)
1294{
1295 struct intel_pt *pt = ptq->pt;
1296 union perf_event *event = ptq->event_buf;
1297 struct perf_sample sample = { .ip = 0, };
1298 struct perf_synth_intel_cbr raw;
1299 u32 flags;
1300
1301 if (intel_pt_skip_event(pt))
1302 return 0;
1303
1304 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1305
1306 sample.id = ptq->pt->cbr_id;
1307 sample.stream_id = ptq->pt->cbr_id;
1308
1309 flags = (u16)ptq->state->cbr_payload | (pt->max_non_turbo_ratio << 16);
1310 raw.flags = cpu_to_le32(flags);
1311 raw.freq = cpu_to_le32(raw.cbr * pt->cbr2khz);
1312 raw.reserved3 = 0;
1313
1314 sample.raw_size = perf_synth__raw_size(raw);
1315 sample.raw_data = perf_synth__raw_data(&raw);
1316
1317 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1318 pt->pwr_events_sample_type);
1319}
1320
1321static int intel_pt_synth_mwait_sample(struct intel_pt_queue *ptq)
1322{
1323 struct intel_pt *pt = ptq->pt;
1324 union perf_event *event = ptq->event_buf;
1325 struct perf_sample sample = { .ip = 0, };
1326 struct perf_synth_intel_mwait raw;
1327
1328 if (intel_pt_skip_event(pt))
1329 return 0;
1330
1331 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1332
1333 sample.id = ptq->pt->mwait_id;
1334 sample.stream_id = ptq->pt->mwait_id;
1335
1336 raw.reserved = 0;
1337 raw.payload = cpu_to_le64(ptq->state->mwait_payload);
1338
1339 sample.raw_size = perf_synth__raw_size(raw);
1340 sample.raw_data = perf_synth__raw_data(&raw);
1341
1342 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1343 pt->pwr_events_sample_type);
1344}
1345
1346static int intel_pt_synth_pwre_sample(struct intel_pt_queue *ptq)
1347{
1348 struct intel_pt *pt = ptq->pt;
1349 union perf_event *event = ptq->event_buf;
1350 struct perf_sample sample = { .ip = 0, };
1351 struct perf_synth_intel_pwre raw;
1352
1353 if (intel_pt_skip_event(pt))
1354 return 0;
1355
1356 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1357
1358 sample.id = ptq->pt->pwre_id;
1359 sample.stream_id = ptq->pt->pwre_id;
1360
1361 raw.reserved = 0;
1362 raw.payload = cpu_to_le64(ptq->state->pwre_payload);
1363
1364 sample.raw_size = perf_synth__raw_size(raw);
1365 sample.raw_data = perf_synth__raw_data(&raw);
1366
1367 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1368 pt->pwr_events_sample_type);
1369}
1370
1371static int intel_pt_synth_exstop_sample(struct intel_pt_queue *ptq)
1372{
1373 struct intel_pt *pt = ptq->pt;
1374 union perf_event *event = ptq->event_buf;
1375 struct perf_sample sample = { .ip = 0, };
1376 struct perf_synth_intel_exstop raw;
1377
1378 if (intel_pt_skip_event(pt))
1379 return 0;
1380
1381 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1382
1383 sample.id = ptq->pt->exstop_id;
1384 sample.stream_id = ptq->pt->exstop_id;
1385
1386 raw.flags = 0;
1387 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
1388
1389 sample.raw_size = perf_synth__raw_size(raw);
1390 sample.raw_data = perf_synth__raw_data(&raw);
1391
1392 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1393 pt->pwr_events_sample_type);
1394}
1395
1396static int intel_pt_synth_pwrx_sample(struct intel_pt_queue *ptq)
1397{
1398 struct intel_pt *pt = ptq->pt;
1399 union perf_event *event = ptq->event_buf;
1400 struct perf_sample sample = { .ip = 0, };
1401 struct perf_synth_intel_pwrx raw;
1402
1403 if (intel_pt_skip_event(pt))
1404 return 0;
1405
1406 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1407
1408 sample.id = ptq->pt->pwrx_id;
1409 sample.stream_id = ptq->pt->pwrx_id;
1410
1411 raw.reserved = 0;
1412 raw.payload = cpu_to_le64(ptq->state->pwrx_payload);
1413
1414 sample.raw_size = perf_synth__raw_size(raw);
1415 sample.raw_data = perf_synth__raw_data(&raw);
1416
1417 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1418 pt->pwr_events_sample_type);
1419}
1420
Adrian Hunter90e457f2015-07-17 19:33:41 +03001421static int intel_pt_synth_error(struct intel_pt *pt, int code, int cpu,
1422 pid_t pid, pid_t tid, u64 ip)
1423{
1424 union perf_event event;
1425 char msg[MAX_AUXTRACE_ERROR_MSG];
1426 int err;
1427
1428 intel_pt__strerror(code, msg, MAX_AUXTRACE_ERROR_MSG);
1429
1430 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
1431 code, cpu, pid, tid, ip, msg);
1432
1433 err = perf_session__deliver_synth_event(pt->session, &event, NULL);
1434 if (err)
1435 pr_err("Intel Processor Trace: failed to deliver error event, error %d\n",
1436 err);
1437
1438 return err;
1439}
1440
1441static int intel_pt_next_tid(struct intel_pt *pt, struct intel_pt_queue *ptq)
1442{
1443 struct auxtrace_queue *queue;
1444 pid_t tid = ptq->next_tid;
1445 int err;
1446
1447 if (tid == -1)
1448 return 0;
1449
1450 intel_pt_log("switch: cpu %d tid %d\n", ptq->cpu, tid);
1451
1452 err = machine__set_current_tid(pt->machine, ptq->cpu, -1, tid);
1453
1454 queue = &pt->queues.queue_array[ptq->queue_nr];
1455 intel_pt_set_pid_tid_cpu(pt, queue);
1456
1457 ptq->next_tid = -1;
1458
1459 return err;
1460}
1461
1462static inline bool intel_pt_is_switch_ip(struct intel_pt_queue *ptq, u64 ip)
1463{
1464 struct intel_pt *pt = ptq->pt;
1465
1466 return ip == pt->switch_ip &&
1467 (ptq->flags & PERF_IP_FLAG_BRANCH) &&
1468 !(ptq->flags & (PERF_IP_FLAG_CONDITIONAL | PERF_IP_FLAG_ASYNC |
1469 PERF_IP_FLAG_INTERRUPT | PERF_IP_FLAG_TX_ABORT));
1470}
1471
Adrian Hunter37973072017-06-30 11:36:45 +03001472#define INTEL_PT_PWR_EVT (INTEL_PT_MWAIT_OP | INTEL_PT_PWR_ENTRY | \
1473 INTEL_PT_EX_STOP | INTEL_PT_PWR_EXIT | \
1474 INTEL_PT_CBR_CHG)
1475
Adrian Hunter90e457f2015-07-17 19:33:41 +03001476static int intel_pt_sample(struct intel_pt_queue *ptq)
1477{
1478 const struct intel_pt_state *state = ptq->state;
1479 struct intel_pt *pt = ptq->pt;
1480 int err;
1481
1482 if (!ptq->have_sample)
1483 return 0;
1484
1485 ptq->have_sample = false;
1486
Adrian Hunter37973072017-06-30 11:36:45 +03001487 if (pt->sample_pwr_events && (state->type & INTEL_PT_PWR_EVT)) {
1488 if (state->type & INTEL_PT_CBR_CHG) {
1489 err = intel_pt_synth_cbr_sample(ptq);
1490 if (err)
1491 return err;
1492 }
1493 if (state->type & INTEL_PT_MWAIT_OP) {
1494 err = intel_pt_synth_mwait_sample(ptq);
1495 if (err)
1496 return err;
1497 }
1498 if (state->type & INTEL_PT_PWR_ENTRY) {
1499 err = intel_pt_synth_pwre_sample(ptq);
1500 if (err)
1501 return err;
1502 }
1503 if (state->type & INTEL_PT_EX_STOP) {
1504 err = intel_pt_synth_exstop_sample(ptq);
1505 if (err)
1506 return err;
1507 }
1508 if (state->type & INTEL_PT_PWR_EXIT) {
1509 err = intel_pt_synth_pwrx_sample(ptq);
1510 if (err)
1511 return err;
1512 }
1513 }
1514
Adrian Hunter406a1802017-05-26 11:17:29 +03001515 if (pt->sample_instructions && (state->type & INTEL_PT_INSTRUCTION)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03001516 err = intel_pt_synth_instruction_sample(ptq);
1517 if (err)
1518 return err;
1519 }
1520
Adrian Hunter406a1802017-05-26 11:17:29 +03001521 if (pt->sample_transactions && (state->type & INTEL_PT_TRANSACTION)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03001522 err = intel_pt_synth_transaction_sample(ptq);
1523 if (err)
1524 return err;
1525 }
1526
Adrian Hunter37973072017-06-30 11:36:45 +03001527 if (pt->sample_ptwrites && (state->type & INTEL_PT_PTW)) {
1528 err = intel_pt_synth_ptwrite_sample(ptq);
1529 if (err)
1530 return err;
1531 }
1532
Adrian Hunter90e457f2015-07-17 19:33:41 +03001533 if (!(state->type & INTEL_PT_BRANCH))
1534 return 0;
1535
Adrian Hunter50f736372016-06-23 16:40:57 +03001536 if (pt->synth_opts.callchain || pt->synth_opts.thread_stack)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001537 thread_stack__event(ptq->thread, ptq->flags, state->from_ip,
1538 state->to_ip, ptq->insn_len,
1539 state->trace_nr);
1540 else
1541 thread_stack__set_trace_nr(ptq->thread, state->trace_nr);
1542
1543 if (pt->sample_branches) {
1544 err = intel_pt_synth_branch_sample(ptq);
1545 if (err)
1546 return err;
1547 }
1548
Adrian Hunterf14445e2015-09-25 16:15:45 +03001549 if (pt->synth_opts.last_branch)
1550 intel_pt_update_last_branch_rb(ptq);
1551
Adrian Hunter90e457f2015-07-17 19:33:41 +03001552 if (!pt->sync_switch)
1553 return 0;
1554
1555 if (intel_pt_is_switch_ip(ptq, state->to_ip)) {
1556 switch (ptq->switch_state) {
1557 case INTEL_PT_SS_UNKNOWN:
1558 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
1559 err = intel_pt_next_tid(pt, ptq);
1560 if (err)
1561 return err;
1562 ptq->switch_state = INTEL_PT_SS_TRACING;
1563 break;
1564 default:
1565 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_EVENT;
1566 return 1;
1567 }
1568 } else if (!state->to_ip) {
1569 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
1570 } else if (ptq->switch_state == INTEL_PT_SS_NOT_TRACING) {
1571 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
1572 } else if (ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
1573 state->to_ip == pt->ptss_ip &&
1574 (ptq->flags & PERF_IP_FLAG_CALL)) {
1575 ptq->switch_state = INTEL_PT_SS_TRACING;
1576 }
1577
1578 return 0;
1579}
1580
Adrian Hunter86c27862015-08-13 12:40:57 +03001581static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001582{
Adrian Hunter86c27862015-08-13 12:40:57 +03001583 struct machine *machine = pt->machine;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001584 struct map *map;
1585 struct symbol *sym, *start;
1586 u64 ip, switch_ip = 0;
Adrian Hunter86c27862015-08-13 12:40:57 +03001587 const char *ptss;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001588
1589 if (ptss_ip)
1590 *ptss_ip = 0;
1591
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001592 map = machine__kernel_map(machine);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001593 if (!map)
1594 return 0;
1595
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001596 if (map__load(map))
Adrian Hunter90e457f2015-07-17 19:33:41 +03001597 return 0;
1598
1599 start = dso__first_symbol(map->dso, MAP__FUNCTION);
1600
1601 for (sym = start; sym; sym = dso__next_symbol(sym)) {
1602 if (sym->binding == STB_GLOBAL &&
1603 !strcmp(sym->name, "__switch_to")) {
1604 ip = map->unmap_ip(map, sym->start);
1605 if (ip >= map->start && ip < map->end) {
1606 switch_ip = ip;
1607 break;
1608 }
1609 }
1610 }
1611
1612 if (!switch_ip || !ptss_ip)
1613 return 0;
1614
Adrian Hunter86c27862015-08-13 12:40:57 +03001615 if (pt->have_sched_switch == 1)
1616 ptss = "perf_trace_sched_switch";
1617 else
1618 ptss = "__perf_event_task_sched_out";
1619
Adrian Hunter90e457f2015-07-17 19:33:41 +03001620 for (sym = start; sym; sym = dso__next_symbol(sym)) {
Adrian Hunter86c27862015-08-13 12:40:57 +03001621 if (!strcmp(sym->name, ptss)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03001622 ip = map->unmap_ip(map, sym->start);
1623 if (ip >= map->start && ip < map->end) {
1624 *ptss_ip = ip;
1625 break;
1626 }
1627 }
1628 }
1629
1630 return switch_ip;
1631}
1632
1633static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp)
1634{
1635 const struct intel_pt_state *state = ptq->state;
1636 struct intel_pt *pt = ptq->pt;
1637 int err;
1638
1639 if (!pt->kernel_start) {
1640 pt->kernel_start = machine__kernel_start(pt->machine);
Adrian Hunter86c27862015-08-13 12:40:57 +03001641 if (pt->per_cpu_mmaps &&
1642 (pt->have_sched_switch == 1 || pt->have_sched_switch == 3) &&
Adrian Hunter90e457f2015-07-17 19:33:41 +03001643 !pt->timeless_decoding && intel_pt_tracing_kernel(pt) &&
1644 !pt->sampling_mode) {
Adrian Hunter86c27862015-08-13 12:40:57 +03001645 pt->switch_ip = intel_pt_switch_ip(pt, &pt->ptss_ip);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001646 if (pt->switch_ip) {
1647 intel_pt_log("switch_ip: %"PRIx64" ptss_ip: %"PRIx64"\n",
1648 pt->switch_ip, pt->ptss_ip);
1649 pt->sync_switch = true;
1650 }
1651 }
1652 }
1653
1654 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
1655 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
1656 while (1) {
1657 err = intel_pt_sample(ptq);
1658 if (err)
1659 return err;
1660
1661 state = intel_pt_decode(ptq->decoder);
1662 if (state->err) {
1663 if (state->err == INTEL_PT_ERR_NODATA)
1664 return 1;
1665 if (pt->sync_switch &&
1666 state->from_ip >= pt->kernel_start) {
1667 pt->sync_switch = false;
1668 intel_pt_next_tid(pt, ptq);
1669 }
1670 if (pt->synth_opts.errors) {
1671 err = intel_pt_synth_error(pt, state->err,
1672 ptq->cpu, ptq->pid,
1673 ptq->tid,
1674 state->from_ip);
1675 if (err)
1676 return err;
1677 }
1678 continue;
1679 }
1680
1681 ptq->state = state;
1682 ptq->have_sample = true;
1683 intel_pt_sample_flags(ptq);
1684
1685 /* Use estimated TSC upon return to user space */
1686 if (pt->est_tsc &&
1687 (state->from_ip >= pt->kernel_start || !state->from_ip) &&
1688 state->to_ip && state->to_ip < pt->kernel_start) {
1689 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
1690 state->timestamp, state->est_timestamp);
1691 ptq->timestamp = state->est_timestamp;
1692 /* Use estimated TSC in unknown switch state */
1693 } else if (pt->sync_switch &&
1694 ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
1695 intel_pt_is_switch_ip(ptq, state->to_ip) &&
1696 ptq->next_tid == -1) {
1697 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
1698 state->timestamp, state->est_timestamp);
1699 ptq->timestamp = state->est_timestamp;
1700 } else if (state->timestamp > ptq->timestamp) {
1701 ptq->timestamp = state->timestamp;
1702 }
1703
1704 if (!pt->timeless_decoding && ptq->timestamp >= *timestamp) {
1705 *timestamp = ptq->timestamp;
1706 return 0;
1707 }
1708 }
1709 return 0;
1710}
1711
1712static inline int intel_pt_update_queues(struct intel_pt *pt)
1713{
1714 if (pt->queues.new_data) {
1715 pt->queues.new_data = false;
1716 return intel_pt_setup_queues(pt);
1717 }
1718 return 0;
1719}
1720
1721static int intel_pt_process_queues(struct intel_pt *pt, u64 timestamp)
1722{
1723 unsigned int queue_nr;
1724 u64 ts;
1725 int ret;
1726
1727 while (1) {
1728 struct auxtrace_queue *queue;
1729 struct intel_pt_queue *ptq;
1730
1731 if (!pt->heap.heap_cnt)
1732 return 0;
1733
1734 if (pt->heap.heap_array[0].ordinal >= timestamp)
1735 return 0;
1736
1737 queue_nr = pt->heap.heap_array[0].queue_nr;
1738 queue = &pt->queues.queue_array[queue_nr];
1739 ptq = queue->priv;
1740
1741 intel_pt_log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n",
1742 queue_nr, pt->heap.heap_array[0].ordinal,
1743 timestamp);
1744
1745 auxtrace_heap__pop(&pt->heap);
1746
1747 if (pt->heap.heap_cnt) {
1748 ts = pt->heap.heap_array[0].ordinal + 1;
1749 if (ts > timestamp)
1750 ts = timestamp;
1751 } else {
1752 ts = timestamp;
1753 }
1754
1755 intel_pt_set_pid_tid_cpu(pt, queue);
1756
1757 ret = intel_pt_run_decoder(ptq, &ts);
1758
1759 if (ret < 0) {
1760 auxtrace_heap__add(&pt->heap, queue_nr, ts);
1761 return ret;
1762 }
1763
1764 if (!ret) {
1765 ret = auxtrace_heap__add(&pt->heap, queue_nr, ts);
1766 if (ret < 0)
1767 return ret;
1768 } else {
1769 ptq->on_heap = false;
1770 }
1771 }
1772
1773 return 0;
1774}
1775
1776static int intel_pt_process_timeless_queues(struct intel_pt *pt, pid_t tid,
1777 u64 time_)
1778{
1779 struct auxtrace_queues *queues = &pt->queues;
1780 unsigned int i;
1781 u64 ts = 0;
1782
1783 for (i = 0; i < queues->nr_queues; i++) {
1784 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
1785 struct intel_pt_queue *ptq = queue->priv;
1786
1787 if (ptq && (tid == -1 || ptq->tid == tid)) {
1788 ptq->time = time_;
1789 intel_pt_set_pid_tid_cpu(pt, queue);
1790 intel_pt_run_decoder(ptq, &ts);
1791 }
1792 }
1793 return 0;
1794}
1795
1796static int intel_pt_lost(struct intel_pt *pt, struct perf_sample *sample)
1797{
1798 return intel_pt_synth_error(pt, INTEL_PT_ERR_LOST, sample->cpu,
1799 sample->pid, sample->tid, 0);
1800}
1801
1802static struct intel_pt_queue *intel_pt_cpu_to_ptq(struct intel_pt *pt, int cpu)
1803{
1804 unsigned i, j;
1805
1806 if (cpu < 0 || !pt->queues.nr_queues)
1807 return NULL;
1808
1809 if ((unsigned)cpu >= pt->queues.nr_queues)
1810 i = pt->queues.nr_queues - 1;
1811 else
1812 i = cpu;
1813
1814 if (pt->queues.queue_array[i].cpu == cpu)
1815 return pt->queues.queue_array[i].priv;
1816
1817 for (j = 0; i > 0; j++) {
1818 if (pt->queues.queue_array[--i].cpu == cpu)
1819 return pt->queues.queue_array[i].priv;
1820 }
1821
1822 for (; j < pt->queues.nr_queues; j++) {
1823 if (pt->queues.queue_array[j].cpu == cpu)
1824 return pt->queues.queue_array[j].priv;
1825 }
1826
1827 return NULL;
1828}
1829
Adrian Hunter86c27862015-08-13 12:40:57 +03001830static int intel_pt_sync_switch(struct intel_pt *pt, int cpu, pid_t tid,
1831 u64 timestamp)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001832{
1833 struct intel_pt_queue *ptq;
Adrian Hunter86c27862015-08-13 12:40:57 +03001834 int err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001835
1836 if (!pt->sync_switch)
Adrian Hunter86c27862015-08-13 12:40:57 +03001837 return 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001838
1839 ptq = intel_pt_cpu_to_ptq(pt, cpu);
1840 if (!ptq)
Adrian Hunter86c27862015-08-13 12:40:57 +03001841 return 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001842
1843 switch (ptq->switch_state) {
1844 case INTEL_PT_SS_NOT_TRACING:
1845 ptq->next_tid = -1;
1846 break;
1847 case INTEL_PT_SS_UNKNOWN:
1848 case INTEL_PT_SS_TRACING:
1849 ptq->next_tid = tid;
1850 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_IP;
1851 return 0;
1852 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
1853 if (!ptq->on_heap) {
Adrian Hunter86c27862015-08-13 12:40:57 +03001854 ptq->timestamp = perf_time_to_tsc(timestamp,
Adrian Hunter90e457f2015-07-17 19:33:41 +03001855 &pt->tc);
1856 err = auxtrace_heap__add(&pt->heap, ptq->queue_nr,
1857 ptq->timestamp);
1858 if (err)
1859 return err;
1860 ptq->on_heap = true;
1861 }
1862 ptq->switch_state = INTEL_PT_SS_TRACING;
1863 break;
1864 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
1865 ptq->next_tid = tid;
1866 intel_pt_log("ERROR: cpu %d expecting switch ip\n", cpu);
1867 break;
1868 default:
1869 break;
1870 }
Adrian Hunter86c27862015-08-13 12:40:57 +03001871
1872 return 1;
1873}
1874
1875static int intel_pt_process_switch(struct intel_pt *pt,
1876 struct perf_sample *sample)
1877{
1878 struct perf_evsel *evsel;
1879 pid_t tid;
1880 int cpu, ret;
1881
1882 evsel = perf_evlist__id2evsel(pt->session->evlist, sample->id);
1883 if (evsel != pt->switch_evsel)
1884 return 0;
1885
1886 tid = perf_evsel__intval(evsel, sample, "next_pid");
1887 cpu = sample->cpu;
1888
1889 intel_pt_log("sched_switch: cpu %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
1890 cpu, tid, sample->time, perf_time_to_tsc(sample->time,
1891 &pt->tc));
1892
1893 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
1894 if (ret <= 0)
1895 return ret;
1896
Adrian Hunter90e457f2015-07-17 19:33:41 +03001897 return machine__set_current_tid(pt->machine, cpu, -1, tid);
1898}
1899
Adrian Hunter86c27862015-08-13 12:40:57 +03001900static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
1901 struct perf_sample *sample)
1902{
1903 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1904 pid_t pid, tid;
1905 int cpu, ret;
1906
1907 cpu = sample->cpu;
1908
1909 if (pt->have_sched_switch == 3) {
1910 if (!out)
1911 return 0;
1912 if (event->header.type != PERF_RECORD_SWITCH_CPU_WIDE) {
1913 pr_err("Expecting CPU-wide context switch event\n");
1914 return -EINVAL;
1915 }
1916 pid = event->context_switch.next_prev_pid;
1917 tid = event->context_switch.next_prev_tid;
1918 } else {
1919 if (out)
1920 return 0;
1921 pid = sample->pid;
1922 tid = sample->tid;
1923 }
1924
1925 if (tid == -1) {
1926 pr_err("context_switch event has no tid\n");
1927 return -EINVAL;
1928 }
1929
1930 intel_pt_log("context_switch: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
1931 cpu, pid, tid, sample->time, perf_time_to_tsc(sample->time,
1932 &pt->tc));
1933
1934 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
1935 if (ret <= 0)
1936 return ret;
1937
1938 return machine__set_current_tid(pt->machine, cpu, pid, tid);
1939}
1940
Adrian Hunter90e457f2015-07-17 19:33:41 +03001941static int intel_pt_process_itrace_start(struct intel_pt *pt,
1942 union perf_event *event,
1943 struct perf_sample *sample)
1944{
1945 if (!pt->per_cpu_mmaps)
1946 return 0;
1947
1948 intel_pt_log("itrace_start: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
1949 sample->cpu, event->itrace_start.pid,
1950 event->itrace_start.tid, sample->time,
1951 perf_time_to_tsc(sample->time, &pt->tc));
1952
1953 return machine__set_current_tid(pt->machine, sample->cpu,
1954 event->itrace_start.pid,
1955 event->itrace_start.tid);
1956}
1957
1958static int intel_pt_process_event(struct perf_session *session,
1959 union perf_event *event,
1960 struct perf_sample *sample,
1961 struct perf_tool *tool)
1962{
1963 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1964 auxtrace);
1965 u64 timestamp;
1966 int err = 0;
1967
1968 if (dump_trace)
1969 return 0;
1970
1971 if (!tool->ordered_events) {
1972 pr_err("Intel Processor Trace requires ordered events\n");
1973 return -EINVAL;
1974 }
1975
Adrian Hunter81cd60c2015-08-20 11:51:32 +03001976 if (sample->time && sample->time != (u64)-1)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001977 timestamp = perf_time_to_tsc(sample->time, &pt->tc);
1978 else
1979 timestamp = 0;
1980
1981 if (timestamp || pt->timeless_decoding) {
1982 err = intel_pt_update_queues(pt);
1983 if (err)
1984 return err;
1985 }
1986
1987 if (pt->timeless_decoding) {
1988 if (event->header.type == PERF_RECORD_EXIT) {
1989 err = intel_pt_process_timeless_queues(pt,
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001990 event->fork.tid,
Adrian Hunter90e457f2015-07-17 19:33:41 +03001991 sample->time);
1992 }
1993 } else if (timestamp) {
1994 err = intel_pt_process_queues(pt, timestamp);
1995 }
1996 if (err)
1997 return err;
1998
1999 if (event->header.type == PERF_RECORD_AUX &&
2000 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
2001 pt->synth_opts.errors) {
2002 err = intel_pt_lost(pt, sample);
2003 if (err)
2004 return err;
2005 }
2006
2007 if (pt->switch_evsel && event->header.type == PERF_RECORD_SAMPLE)
2008 err = intel_pt_process_switch(pt, sample);
2009 else if (event->header.type == PERF_RECORD_ITRACE_START)
2010 err = intel_pt_process_itrace_start(pt, event, sample);
Adrian Hunter86c27862015-08-13 12:40:57 +03002011 else if (event->header.type == PERF_RECORD_SWITCH ||
2012 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
2013 err = intel_pt_context_switch(pt, event, sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002014
2015 intel_pt_log("event %s (%u): cpu %d time %"PRIu64" tsc %#"PRIx64"\n",
2016 perf_event__name(event->header.type), event->header.type,
2017 sample->cpu, sample->time, timestamp);
2018
2019 return err;
2020}
2021
2022static int intel_pt_flush(struct perf_session *session, struct perf_tool *tool)
2023{
2024 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2025 auxtrace);
2026 int ret;
2027
2028 if (dump_trace)
2029 return 0;
2030
2031 if (!tool->ordered_events)
2032 return -EINVAL;
2033
2034 ret = intel_pt_update_queues(pt);
2035 if (ret < 0)
2036 return ret;
2037
2038 if (pt->timeless_decoding)
2039 return intel_pt_process_timeless_queues(pt, -1,
2040 MAX_TIMESTAMP - 1);
2041
2042 return intel_pt_process_queues(pt, MAX_TIMESTAMP);
2043}
2044
2045static void intel_pt_free_events(struct perf_session *session)
2046{
2047 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2048 auxtrace);
2049 struct auxtrace_queues *queues = &pt->queues;
2050 unsigned int i;
2051
2052 for (i = 0; i < queues->nr_queues; i++) {
2053 intel_pt_free_queue(queues->queue_array[i].priv);
2054 queues->queue_array[i].priv = NULL;
2055 }
2056 intel_pt_log_disable();
2057 auxtrace_queues__free(queues);
2058}
2059
2060static void intel_pt_free(struct perf_session *session)
2061{
2062 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2063 auxtrace);
2064
2065 auxtrace_heap__free(&pt->heap);
2066 intel_pt_free_events(session);
2067 session->auxtrace = NULL;
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -03002068 thread__put(pt->unknown_thread);
Adrian Hunter2acee102016-09-23 17:38:48 +03002069 addr_filters__exit(&pt->filts);
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03002070 zfree(&pt->filter);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002071 free(pt);
2072}
2073
2074static int intel_pt_process_auxtrace_event(struct perf_session *session,
2075 union perf_event *event,
2076 struct perf_tool *tool __maybe_unused)
2077{
2078 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2079 auxtrace);
2080
2081 if (pt->sampling_mode)
2082 return 0;
2083
2084 if (!pt->data_queued) {
2085 struct auxtrace_buffer *buffer;
2086 off_t data_offset;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002087 int fd = perf_data__fd(session->data);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002088 int err;
2089
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002090 if (perf_data__is_pipe(session->data)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03002091 data_offset = 0;
2092 } else {
2093 data_offset = lseek(fd, 0, SEEK_CUR);
2094 if (data_offset == -1)
2095 return -errno;
2096 }
2097
2098 err = auxtrace_queues__add_event(&pt->queues, session, event,
2099 data_offset, &buffer);
2100 if (err)
2101 return err;
2102
2103 /* Dump here now we have copied a piped trace out of the pipe */
2104 if (dump_trace) {
2105 if (auxtrace_buffer__get_data(buffer, fd)) {
2106 intel_pt_dump_event(pt, buffer->data,
2107 buffer->size);
2108 auxtrace_buffer__put_data(buffer);
2109 }
2110 }
2111 }
2112
2113 return 0;
2114}
2115
2116struct intel_pt_synth {
2117 struct perf_tool dummy_tool;
2118 struct perf_session *session;
2119};
2120
2121static int intel_pt_event_synth(struct perf_tool *tool,
2122 union perf_event *event,
2123 struct perf_sample *sample __maybe_unused,
2124 struct machine *machine __maybe_unused)
2125{
2126 struct intel_pt_synth *intel_pt_synth =
2127 container_of(tool, struct intel_pt_synth, dummy_tool);
2128
2129 return perf_session__deliver_synth_event(intel_pt_synth->session, event,
2130 NULL);
2131}
2132
Adrian Hunter63a22cd2017-05-26 11:17:31 +03002133static int intel_pt_synth_event(struct perf_session *session, const char *name,
Adrian Hunter90e457f2015-07-17 19:33:41 +03002134 struct perf_event_attr *attr, u64 id)
2135{
2136 struct intel_pt_synth intel_pt_synth;
Adrian Hunter63a22cd2017-05-26 11:17:31 +03002137 int err;
2138
2139 pr_debug("Synthesizing '%s' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
2140 name, id, (u64)attr->sample_type);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002141
2142 memset(&intel_pt_synth, 0, sizeof(struct intel_pt_synth));
2143 intel_pt_synth.session = session;
2144
Adrian Hunter63a22cd2017-05-26 11:17:31 +03002145 err = perf_event__synthesize_attr(&intel_pt_synth.dummy_tool, attr, 1,
2146 &id, intel_pt_event_synth);
2147 if (err)
2148 pr_err("%s: failed to synthesize '%s' event type\n",
2149 __func__, name);
2150
2151 return err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002152}
2153
Adrian Hunterbbac88e2017-05-26 11:17:32 +03002154static void intel_pt_set_event_name(struct perf_evlist *evlist, u64 id,
2155 const char *name)
2156{
2157 struct perf_evsel *evsel;
2158
2159 evlist__for_each_entry(evlist, evsel) {
2160 if (evsel->id && evsel->id[0] == id) {
2161 if (evsel->name)
2162 zfree(&evsel->name);
2163 evsel->name = strdup(name);
2164 break;
2165 }
2166 }
2167}
2168
Adrian Hunter85a564d2017-05-26 11:17:30 +03002169static struct perf_evsel *intel_pt_evsel(struct intel_pt *pt,
2170 struct perf_evlist *evlist)
2171{
2172 struct perf_evsel *evsel;
2173
2174 evlist__for_each_entry(evlist, evsel) {
2175 if (evsel->attr.type == pt->pmu_type && evsel->ids)
2176 return evsel;
2177 }
2178
2179 return NULL;
2180}
2181
Adrian Hunter90e457f2015-07-17 19:33:41 +03002182static int intel_pt_synth_events(struct intel_pt *pt,
2183 struct perf_session *session)
2184{
2185 struct perf_evlist *evlist = session->evlist;
Adrian Hunter85a564d2017-05-26 11:17:30 +03002186 struct perf_evsel *evsel = intel_pt_evsel(pt, evlist);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002187 struct perf_event_attr attr;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002188 u64 id;
2189 int err;
2190
Adrian Hunter85a564d2017-05-26 11:17:30 +03002191 if (!evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03002192 pr_debug("There are no selected events with Intel Processor Trace data\n");
2193 return 0;
2194 }
2195
2196 memset(&attr, 0, sizeof(struct perf_event_attr));
2197 attr.size = sizeof(struct perf_event_attr);
2198 attr.type = PERF_TYPE_HARDWARE;
2199 attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
2200 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
2201 PERF_SAMPLE_PERIOD;
2202 if (pt->timeless_decoding)
2203 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
2204 else
2205 attr.sample_type |= PERF_SAMPLE_TIME;
2206 if (!pt->per_cpu_mmaps)
2207 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
2208 attr.exclude_user = evsel->attr.exclude_user;
2209 attr.exclude_kernel = evsel->attr.exclude_kernel;
2210 attr.exclude_hv = evsel->attr.exclude_hv;
2211 attr.exclude_host = evsel->attr.exclude_host;
2212 attr.exclude_guest = evsel->attr.exclude_guest;
2213 attr.sample_id_all = evsel->attr.sample_id_all;
2214 attr.read_format = evsel->attr.read_format;
2215
2216 id = evsel->id[0] + 1000000000;
2217 if (!id)
2218 id = 1;
2219
Adrian Hunter4a9fd4e2017-05-26 11:17:33 +03002220 if (pt->synth_opts.branches) {
2221 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
2222 attr.sample_period = 1;
2223 attr.sample_type |= PERF_SAMPLE_ADDR;
2224 err = intel_pt_synth_event(session, "branches", &attr, id);
2225 if (err)
2226 return err;
2227 pt->sample_branches = true;
2228 pt->branches_sample_type = attr.sample_type;
2229 pt->branches_id = id;
2230 id += 1;
2231 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
2232 }
2233
2234 if (pt->synth_opts.callchain)
2235 attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
2236 if (pt->synth_opts.last_branch)
2237 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
2238
Adrian Hunter90e457f2015-07-17 19:33:41 +03002239 if (pt->synth_opts.instructions) {
2240 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
2241 if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS)
2242 attr.sample_period =
2243 intel_pt_ns_to_ticks(pt, pt->synth_opts.period);
2244 else
2245 attr.sample_period = pt->synth_opts.period;
Adrian Hunter63a22cd2017-05-26 11:17:31 +03002246 err = intel_pt_synth_event(session, "instructions", &attr, id);
2247 if (err)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002248 return err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002249 pt->sample_instructions = true;
2250 pt->instructions_sample_type = attr.sample_type;
2251 pt->instructions_id = id;
2252 id += 1;
2253 }
2254
Adrian Hunter4a9fd4e2017-05-26 11:17:33 +03002255 attr.sample_type &= ~(u64)PERF_SAMPLE_PERIOD;
2256 attr.sample_period = 1;
2257
Adrian Hunter90e457f2015-07-17 19:33:41 +03002258 if (pt->synth_opts.transactions) {
2259 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
Adrian Hunter63a22cd2017-05-26 11:17:31 +03002260 err = intel_pt_synth_event(session, "transactions", &attr, id);
2261 if (err)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002262 return err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002263 pt->sample_transactions = true;
Adrian Hunter21160742017-05-26 11:17:18 +03002264 pt->transactions_sample_type = attr.sample_type;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002265 pt->transactions_id = id;
Adrian Hunterbbac88e2017-05-26 11:17:32 +03002266 intel_pt_set_event_name(evlist, id, "transactions");
Adrian Hunter90e457f2015-07-17 19:33:41 +03002267 id += 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002268 }
2269
Adrian Hunter37973072017-06-30 11:36:45 +03002270 attr.type = PERF_TYPE_SYNTH;
2271 attr.sample_type |= PERF_SAMPLE_RAW;
2272
2273 if (pt->synth_opts.ptwrites) {
2274 attr.config = PERF_SYNTH_INTEL_PTWRITE;
2275 err = intel_pt_synth_event(session, "ptwrite", &attr, id);
2276 if (err)
2277 return err;
2278 pt->sample_ptwrites = true;
2279 pt->ptwrites_sample_type = attr.sample_type;
2280 pt->ptwrites_id = id;
2281 intel_pt_set_event_name(evlist, id, "ptwrite");
2282 id += 1;
2283 }
2284
2285 if (pt->synth_opts.pwr_events) {
2286 pt->sample_pwr_events = true;
2287 pt->pwr_events_sample_type = attr.sample_type;
2288
2289 attr.config = PERF_SYNTH_INTEL_CBR;
2290 err = intel_pt_synth_event(session, "cbr", &attr, id);
2291 if (err)
2292 return err;
2293 pt->cbr_id = id;
2294 intel_pt_set_event_name(evlist, id, "cbr");
2295 id += 1;
2296 }
2297
2298 if (pt->synth_opts.pwr_events && (evsel->attr.config & 0x10)) {
2299 attr.config = PERF_SYNTH_INTEL_MWAIT;
2300 err = intel_pt_synth_event(session, "mwait", &attr, id);
2301 if (err)
2302 return err;
2303 pt->mwait_id = id;
2304 intel_pt_set_event_name(evlist, id, "mwait");
2305 id += 1;
2306
2307 attr.config = PERF_SYNTH_INTEL_PWRE;
2308 err = intel_pt_synth_event(session, "pwre", &attr, id);
2309 if (err)
2310 return err;
2311 pt->pwre_id = id;
2312 intel_pt_set_event_name(evlist, id, "pwre");
2313 id += 1;
2314
2315 attr.config = PERF_SYNTH_INTEL_EXSTOP;
2316 err = intel_pt_synth_event(session, "exstop", &attr, id);
2317 if (err)
2318 return err;
2319 pt->exstop_id = id;
2320 intel_pt_set_event_name(evlist, id, "exstop");
2321 id += 1;
2322
2323 attr.config = PERF_SYNTH_INTEL_PWRX;
2324 err = intel_pt_synth_event(session, "pwrx", &attr, id);
2325 if (err)
2326 return err;
2327 pt->pwrx_id = id;
2328 intel_pt_set_event_name(evlist, id, "pwrx");
2329 id += 1;
2330 }
2331
Adrian Hunter90e457f2015-07-17 19:33:41 +03002332 return 0;
2333}
2334
2335static struct perf_evsel *intel_pt_find_sched_switch(struct perf_evlist *evlist)
2336{
2337 struct perf_evsel *evsel;
2338
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002339 evlist__for_each_entry_reverse(evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03002340 const char *name = perf_evsel__name(evsel);
2341
2342 if (!strcmp(name, "sched:sched_switch"))
2343 return evsel;
2344 }
2345
2346 return NULL;
2347}
2348
Adrian Hunter86c27862015-08-13 12:40:57 +03002349static bool intel_pt_find_switch(struct perf_evlist *evlist)
2350{
2351 struct perf_evsel *evsel;
2352
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002353 evlist__for_each_entry(evlist, evsel) {
Adrian Hunter86c27862015-08-13 12:40:57 +03002354 if (evsel->attr.context_switch)
2355 return true;
2356 }
2357
2358 return false;
2359}
2360
Adrian Hunterba11ba62015-09-25 16:15:56 +03002361static int intel_pt_perf_config(const char *var, const char *value, void *data)
2362{
2363 struct intel_pt *pt = data;
2364
2365 if (!strcmp(var, "intel-pt.mispred-all"))
2366 pt->mispred_all = perf_config_bool(var, value);
2367
2368 return 0;
2369}
2370
Adrian Hunter90e457f2015-07-17 19:33:41 +03002371static const char * const intel_pt_info_fmts[] = {
Adrian Hunter11fa7cb2015-07-17 19:33:54 +03002372 [INTEL_PT_PMU_TYPE] = " PMU Type %"PRId64"\n",
2373 [INTEL_PT_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
2374 [INTEL_PT_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
2375 [INTEL_PT_TIME_ZERO] = " Time Zero %"PRIu64"\n",
2376 [INTEL_PT_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
2377 [INTEL_PT_TSC_BIT] = " TSC bit %#"PRIx64"\n",
2378 [INTEL_PT_NORETCOMP_BIT] = " NoRETComp bit %#"PRIx64"\n",
2379 [INTEL_PT_HAVE_SCHED_SWITCH] = " Have sched_switch %"PRId64"\n",
2380 [INTEL_PT_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
2381 [INTEL_PT_PER_CPU_MMAPS] = " Per-cpu maps %"PRId64"\n",
2382 [INTEL_PT_MTC_BIT] = " MTC bit %#"PRIx64"\n",
2383 [INTEL_PT_TSC_CTC_N] = " TSC:CTC numerator %"PRIu64"\n",
2384 [INTEL_PT_TSC_CTC_D] = " TSC:CTC denominator %"PRIu64"\n",
2385 [INTEL_PT_CYC_BIT] = " CYC bit %#"PRIx64"\n",
Adrian Hunterfa8025c2016-09-23 17:38:42 +03002386 [INTEL_PT_MAX_NONTURBO_RATIO] = " Max non-turbo ratio %"PRIu64"\n",
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03002387 [INTEL_PT_FILTER_STR_LEN] = " Filter string len. %"PRIu64"\n",
Adrian Hunter90e457f2015-07-17 19:33:41 +03002388};
2389
2390static void intel_pt_print_info(u64 *arr, int start, int finish)
2391{
2392 int i;
2393
2394 if (!dump_trace)
2395 return;
2396
2397 for (i = start; i <= finish; i++)
2398 fprintf(stdout, intel_pt_info_fmts[i], arr[i]);
2399}
2400
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03002401static void intel_pt_print_info_str(const char *name, const char *str)
2402{
2403 if (!dump_trace)
2404 return;
2405
2406 fprintf(stdout, " %-20s%s\n", name, str ? str : "");
2407}
2408
Adrian Hunter40b746a2016-09-23 17:38:44 +03002409static bool intel_pt_has(struct auxtrace_info_event *auxtrace_info, int pos)
2410{
2411 return auxtrace_info->header.size >=
2412 sizeof(struct auxtrace_info_event) + (sizeof(u64) * (pos + 1));
2413}
2414
Adrian Hunter90e457f2015-07-17 19:33:41 +03002415int intel_pt_process_auxtrace_info(union perf_event *event,
2416 struct perf_session *session)
2417{
2418 struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
2419 size_t min_sz = sizeof(u64) * INTEL_PT_PER_CPU_MMAPS;
2420 struct intel_pt *pt;
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03002421 void *info_end;
2422 u64 *info;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002423 int err;
2424
2425 if (auxtrace_info->header.size < sizeof(struct auxtrace_info_event) +
2426 min_sz)
2427 return -EINVAL;
2428
2429 pt = zalloc(sizeof(struct intel_pt));
2430 if (!pt)
2431 return -ENOMEM;
2432
Adrian Hunter2acee102016-09-23 17:38:48 +03002433 addr_filters__init(&pt->filts);
2434
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002435 err = perf_config(intel_pt_perf_config, pt);
2436 if (err)
2437 goto err_free;
Adrian Hunterba11ba62015-09-25 16:15:56 +03002438
Adrian Hunter90e457f2015-07-17 19:33:41 +03002439 err = auxtrace_queues__init(&pt->queues);
2440 if (err)
2441 goto err_free;
2442
2443 intel_pt_log_set_name(INTEL_PT_PMU_NAME);
2444
2445 pt->session = session;
2446 pt->machine = &session->machines.host; /* No kvm support */
2447 pt->auxtrace_type = auxtrace_info->type;
2448 pt->pmu_type = auxtrace_info->priv[INTEL_PT_PMU_TYPE];
2449 pt->tc.time_shift = auxtrace_info->priv[INTEL_PT_TIME_SHIFT];
2450 pt->tc.time_mult = auxtrace_info->priv[INTEL_PT_TIME_MULT];
2451 pt->tc.time_zero = auxtrace_info->priv[INTEL_PT_TIME_ZERO];
2452 pt->cap_user_time_zero = auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO];
2453 pt->tsc_bit = auxtrace_info->priv[INTEL_PT_TSC_BIT];
2454 pt->noretcomp_bit = auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT];
2455 pt->have_sched_switch = auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH];
2456 pt->snapshot_mode = auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE];
2457 pt->per_cpu_mmaps = auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS];
2458 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_PMU_TYPE,
2459 INTEL_PT_PER_CPU_MMAPS);
2460
Adrian Hunter40b746a2016-09-23 17:38:44 +03002461 if (intel_pt_has(auxtrace_info, INTEL_PT_CYC_BIT)) {
Adrian Hunter11fa7cb2015-07-17 19:33:54 +03002462 pt->mtc_bit = auxtrace_info->priv[INTEL_PT_MTC_BIT];
2463 pt->mtc_freq_bits = auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS];
2464 pt->tsc_ctc_ratio_n = auxtrace_info->priv[INTEL_PT_TSC_CTC_N];
2465 pt->tsc_ctc_ratio_d = auxtrace_info->priv[INTEL_PT_TSC_CTC_D];
2466 pt->cyc_bit = auxtrace_info->priv[INTEL_PT_CYC_BIT];
2467 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_MTC_BIT,
2468 INTEL_PT_CYC_BIT);
2469 }
2470
Adrian Hunter40b746a2016-09-23 17:38:44 +03002471 if (intel_pt_has(auxtrace_info, INTEL_PT_MAX_NONTURBO_RATIO)) {
Adrian Hunterfa8025c2016-09-23 17:38:42 +03002472 pt->max_non_turbo_ratio =
2473 auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO];
2474 intel_pt_print_info(&auxtrace_info->priv[0],
2475 INTEL_PT_MAX_NONTURBO_RATIO,
2476 INTEL_PT_MAX_NONTURBO_RATIO);
2477 }
2478
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03002479 info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
2480 info_end = (void *)info + auxtrace_info->header.size;
2481
2482 if (intel_pt_has(auxtrace_info, INTEL_PT_FILTER_STR_LEN)) {
2483 size_t len;
2484
2485 len = auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN];
2486 intel_pt_print_info(&auxtrace_info->priv[0],
2487 INTEL_PT_FILTER_STR_LEN,
2488 INTEL_PT_FILTER_STR_LEN);
2489 if (len) {
2490 const char *filter = (const char *)info;
2491
2492 len = roundup(len + 1, 8);
2493 info += len >> 3;
2494 if ((void *)info > info_end) {
2495 pr_err("%s: bad filter string length\n", __func__);
2496 err = -EINVAL;
2497 goto err_free_queues;
2498 }
2499 pt->filter = memdup(filter, len);
2500 if (!pt->filter) {
2501 err = -ENOMEM;
2502 goto err_free_queues;
2503 }
2504 if (session->header.needs_swap)
2505 mem_bswap_64(pt->filter, len);
2506 if (pt->filter[len - 1]) {
2507 pr_err("%s: filter string not null terminated\n", __func__);
2508 err = -EINVAL;
2509 goto err_free_queues;
2510 }
Adrian Hunter2acee102016-09-23 17:38:48 +03002511 err = addr_filters__parse_bare_filter(&pt->filts,
2512 filter);
2513 if (err)
2514 goto err_free_queues;
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03002515 }
2516 intel_pt_print_info_str("Filter string", pt->filter);
2517 }
2518
Adrian Hunter90e457f2015-07-17 19:33:41 +03002519 pt->timeless_decoding = intel_pt_timeless_decoding(pt);
2520 pt->have_tsc = intel_pt_have_tsc(pt);
2521 pt->sampling_mode = false;
2522 pt->est_tsc = !pt->timeless_decoding;
2523
2524 pt->unknown_thread = thread__new(999999999, 999999999);
2525 if (!pt->unknown_thread) {
2526 err = -ENOMEM;
2527 goto err_free_queues;
2528 }
Adrian Hunter3a4acda2016-02-01 03:21:04 +00002529
2530 /*
2531 * Since this thread will not be kept in any rbtree not in a
2532 * list, initialize its list node so that at thread__put() the
2533 * current thread lifetime assuption is kept and we don't segfault
2534 * at list_del_init().
2535 */
2536 INIT_LIST_HEAD(&pt->unknown_thread->node);
2537
Adrian Hunter90e457f2015-07-17 19:33:41 +03002538 err = thread__set_comm(pt->unknown_thread, "unknown", 0);
2539 if (err)
2540 goto err_delete_thread;
2541 if (thread__init_map_groups(pt->unknown_thread, pt->machine)) {
2542 err = -ENOMEM;
2543 goto err_delete_thread;
2544 }
2545
2546 pt->auxtrace.process_event = intel_pt_process_event;
2547 pt->auxtrace.process_auxtrace_event = intel_pt_process_auxtrace_event;
2548 pt->auxtrace.flush_events = intel_pt_flush;
2549 pt->auxtrace.free_events = intel_pt_free_events;
2550 pt->auxtrace.free = intel_pt_free;
2551 session->auxtrace = &pt->auxtrace;
2552
2553 if (dump_trace)
2554 return 0;
2555
2556 if (pt->have_sched_switch == 1) {
2557 pt->switch_evsel = intel_pt_find_sched_switch(session->evlist);
2558 if (!pt->switch_evsel) {
2559 pr_err("%s: missing sched_switch event\n", __func__);
Adrian Hunter4d34e102016-09-23 17:38:43 +03002560 err = -EINVAL;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002561 goto err_delete_thread;
2562 }
Adrian Hunter86c27862015-08-13 12:40:57 +03002563 } else if (pt->have_sched_switch == 2 &&
2564 !intel_pt_find_switch(session->evlist)) {
2565 pr_err("%s: missing context_switch attribute flag\n", __func__);
Adrian Hunter4d34e102016-09-23 17:38:43 +03002566 err = -EINVAL;
Adrian Hunter86c27862015-08-13 12:40:57 +03002567 goto err_delete_thread;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002568 }
2569
2570 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
2571 pt->synth_opts = *session->itrace_synth_opts;
2572 } else {
2573 itrace_synth_opts__set_default(&pt->synth_opts);
2574 if (use_browser != -1) {
2575 pt->synth_opts.branches = false;
2576 pt->synth_opts.callchain = true;
2577 }
Adrian Hunter50f736372016-06-23 16:40:57 +03002578 if (session->itrace_synth_opts)
2579 pt->synth_opts.thread_stack =
2580 session->itrace_synth_opts->thread_stack;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002581 }
2582
2583 if (pt->synth_opts.log)
2584 intel_pt_log_enable();
2585
2586 /* Maximum non-turbo ratio is TSC freq / 100 MHz */
2587 if (pt->tc.time_mult) {
2588 u64 tsc_freq = intel_pt_ns_to_ticks(pt, 1000000000);
2589
Adrian Hunterfa8025c2016-09-23 17:38:42 +03002590 if (!pt->max_non_turbo_ratio)
2591 pt->max_non_turbo_ratio =
2592 (tsc_freq + 50000000) / 100000000;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002593 intel_pt_log("TSC frequency %"PRIu64"\n", tsc_freq);
2594 intel_pt_log("Maximum non-turbo ratio %u\n",
2595 pt->max_non_turbo_ratio);
Adrian Hunter37973072017-06-30 11:36:45 +03002596 pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002597 }
2598
2599 if (pt->synth_opts.calls)
2600 pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
2601 PERF_IP_FLAG_TRACE_END;
2602 if (pt->synth_opts.returns)
2603 pt->branches_filter |= PERF_IP_FLAG_RETURN |
2604 PERF_IP_FLAG_TRACE_BEGIN;
2605
2606 if (pt->synth_opts.callchain && !symbol_conf.use_callchain) {
2607 symbol_conf.use_callchain = true;
2608 if (callchain_register_param(&callchain_param) < 0) {
2609 symbol_conf.use_callchain = false;
2610 pt->synth_opts.callchain = false;
2611 }
2612 }
2613
2614 err = intel_pt_synth_events(pt, session);
2615 if (err)
2616 goto err_delete_thread;
2617
2618 err = auxtrace_queues__process_index(&pt->queues, session);
2619 if (err)
2620 goto err_delete_thread;
2621
2622 if (pt->queues.populated)
2623 pt->data_queued = true;
2624
2625 if (pt->timeless_decoding)
2626 pr_debug2("Intel PT decoding without timestamps\n");
2627
2628 return 0;
2629
2630err_delete_thread:
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -03002631 thread__zput(pt->unknown_thread);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002632err_free_queues:
2633 intel_pt_log_disable();
2634 auxtrace_queues__free(&pt->queues);
2635 session->auxtrace = NULL;
2636err_free:
Adrian Hunter2acee102016-09-23 17:38:48 +03002637 addr_filters__exit(&pt->filts);
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03002638 zfree(&pt->filter);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002639 free(pt);
2640 return err;
2641}