pref tools: Add missing map.h includes
[linux-2.6-block.git] / tools / perf / util / intel-bts.c
CommitLineData
d0170af7
AH
1/*
2 * intel-bts.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
16#include <endian.h>
a43783ae 17#include <errno.h>
d0170af7 18#include <byteswap.h>
fd20e811 19#include <inttypes.h>
d0170af7
AH
20#include <linux/kernel.h>
21#include <linux/types.h>
22#include <linux/bitops.h>
23#include <linux/log2.h>
24
25#include "cpumap.h"
26#include "color.h"
27#include "evsel.h"
28#include "evlist.h"
29#include "machine.h"
1101f69a 30#include "map.h"
d0170af7
AH
31#include "session.h"
32#include "util.h"
33#include "thread.h"
34#include "thread-stack.h"
35#include "debug.h"
36#include "tsc.h"
37#include "auxtrace.h"
38#include "intel-pt-decoder/intel-pt-insn-decoder.h"
39#include "intel-bts.h"
40
41#define MAX_TIMESTAMP (~0ULL)
42
43#define INTEL_BTS_ERR_NOINSN 5
44#define INTEL_BTS_ERR_LOST 9
45
46#if __BYTE_ORDER == __BIG_ENDIAN
47#define le64_to_cpu bswap_64
48#else
49#define le64_to_cpu
50#endif
51
52struct intel_bts {
53 struct auxtrace auxtrace;
54 struct auxtrace_queues queues;
55 struct auxtrace_heap heap;
56 u32 auxtrace_type;
57 struct perf_session *session;
58 struct machine *machine;
59 bool sampling_mode;
60 bool snapshot_mode;
61 bool data_queued;
62 u32 pmu_type;
63 struct perf_tsc_conversion tc;
64 bool cap_user_time_zero;
65 struct itrace_synth_opts synth_opts;
66 bool sample_branches;
67 u32 branches_filter;
68 u64 branches_sample_type;
69 u64 branches_id;
70 size_t branches_event_size;
d1706b39 71 unsigned long num_events;
d0170af7
AH
72};
73
74struct intel_bts_queue {
75 struct intel_bts *bts;
76 unsigned int queue_nr;
77 struct auxtrace_buffer *buffer;
78 bool on_heap;
79 bool done;
80 pid_t pid;
81 pid_t tid;
82 int cpu;
83 u64 time;
84 struct intel_pt_insn intel_pt_insn;
85 u32 sample_flags;
86};
87
88struct branch {
89 u64 from;
90 u64 to;
91 u64 misc;
92};
93
94static void intel_bts_dump(struct intel_bts *bts __maybe_unused,
95 unsigned char *buf, size_t len)
96{
97 struct branch *branch;
98 size_t i, pos = 0, br_sz = sizeof(struct branch), sz;
99 const char *color = PERF_COLOR_BLUE;
100
101 color_fprintf(stdout, color,
102 ". ... Intel BTS data: size %zu bytes\n",
103 len);
104
105 while (len) {
106 if (len >= br_sz)
107 sz = br_sz;
108 else
109 sz = len;
110 printf(".");
111 color_fprintf(stdout, color, " %08x: ", pos);
112 for (i = 0; i < sz; i++)
113 color_fprintf(stdout, color, " %02x", buf[i]);
114 for (; i < br_sz; i++)
115 color_fprintf(stdout, color, " ");
116 if (len >= br_sz) {
117 branch = (struct branch *)buf;
118 color_fprintf(stdout, color, " %"PRIx64" -> %"PRIx64" %s\n",
119 le64_to_cpu(branch->from),
120 le64_to_cpu(branch->to),
121 le64_to_cpu(branch->misc) & 0x10 ?
122 "pred" : "miss");
123 } else {
124 color_fprintf(stdout, color, " Bad record!\n");
125 }
126 pos += sz;
127 buf += sz;
128 len -= sz;
129 }
130}
131
132static void intel_bts_dump_event(struct intel_bts *bts, unsigned char *buf,
133 size_t len)
134{
135 printf(".\n");
136 intel_bts_dump(bts, buf, len);
137}
138
139static int intel_bts_lost(struct intel_bts *bts, struct perf_sample *sample)
140{
141 union perf_event event;
142 int err;
143
144 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
145 INTEL_BTS_ERR_LOST, sample->cpu, sample->pid,
146 sample->tid, 0, "Lost trace data");
147
148 err = perf_session__deliver_synth_event(bts->session, &event, NULL);
149 if (err)
150 pr_err("Intel BTS: failed to deliver error event, error %d\n",
151 err);
152
153 return err;
154}
155
156static struct intel_bts_queue *intel_bts_alloc_queue(struct intel_bts *bts,
157 unsigned int queue_nr)
158{
159 struct intel_bts_queue *btsq;
160
161 btsq = zalloc(sizeof(struct intel_bts_queue));
162 if (!btsq)
163 return NULL;
164
165 btsq->bts = bts;
166 btsq->queue_nr = queue_nr;
167 btsq->pid = -1;
168 btsq->tid = -1;
169 btsq->cpu = -1;
170
171 return btsq;
172}
173
174static int intel_bts_setup_queue(struct intel_bts *bts,
175 struct auxtrace_queue *queue,
176 unsigned int queue_nr)
177{
178 struct intel_bts_queue *btsq = queue->priv;
179
180 if (list_empty(&queue->head))
181 return 0;
182
183 if (!btsq) {
184 btsq = intel_bts_alloc_queue(bts, queue_nr);
185 if (!btsq)
186 return -ENOMEM;
187 queue->priv = btsq;
188
189 if (queue->cpu != -1)
190 btsq->cpu = queue->cpu;
191 btsq->tid = queue->tid;
192 }
193
194 if (bts->sampling_mode)
195 return 0;
196
197 if (!btsq->on_heap && !btsq->buffer) {
198 int ret;
199
200 btsq->buffer = auxtrace_buffer__next(queue, NULL);
201 if (!btsq->buffer)
202 return 0;
203
204 ret = auxtrace_heap__add(&bts->heap, queue_nr,
205 btsq->buffer->reference);
206 if (ret)
207 return ret;
208 btsq->on_heap = true;
209 }
210
211 return 0;
212}
213
214static int intel_bts_setup_queues(struct intel_bts *bts)
215{
216 unsigned int i;
217 int ret;
218
219 for (i = 0; i < bts->queues.nr_queues; i++) {
220 ret = intel_bts_setup_queue(bts, &bts->queues.queue_array[i],
221 i);
222 if (ret)
223 return ret;
224 }
225 return 0;
226}
227
228static inline int intel_bts_update_queues(struct intel_bts *bts)
229{
230 if (bts->queues.new_data) {
231 bts->queues.new_data = false;
232 return intel_bts_setup_queues(bts);
233 }
234 return 0;
235}
236
237static unsigned char *intel_bts_find_overlap(unsigned char *buf_a, size_t len_a,
238 unsigned char *buf_b, size_t len_b)
239{
240 size_t offs, len;
241
242 if (len_a > len_b)
243 offs = len_a - len_b;
244 else
245 offs = 0;
246
247 for (; offs < len_a; offs += sizeof(struct branch)) {
248 len = len_a - offs;
249 if (!memcmp(buf_a + offs, buf_b, len))
250 return buf_b + len;
251 }
252
253 return buf_b;
254}
255
256static int intel_bts_do_fix_overlap(struct auxtrace_queue *queue,
257 struct auxtrace_buffer *b)
258{
259 struct auxtrace_buffer *a;
260 void *start;
261
262 if (b->list.prev == &queue->head)
263 return 0;
264 a = list_entry(b->list.prev, struct auxtrace_buffer, list);
265 start = intel_bts_find_overlap(a->data, a->size, b->data, b->size);
266 if (!start)
267 return -EINVAL;
268 b->use_size = b->data + b->size - start;
269 b->use_data = start;
270 return 0;
271}
272
5d4f0eda
AH
273static inline u8 intel_bts_cpumode(struct intel_bts *bts, uint64_t ip)
274{
275 return machine__kernel_ip(bts->machine, ip) ?
276 PERF_RECORD_MISC_KERNEL :
277 PERF_RECORD_MISC_USER;
278}
279
d0170af7
AH
280static int intel_bts_synth_branch_sample(struct intel_bts_queue *btsq,
281 struct branch *branch)
282{
283 int ret;
284 struct intel_bts *bts = btsq->bts;
285 union perf_event event;
286 struct perf_sample sample = { .ip = 0, };
287
d1706b39
AK
288 if (bts->synth_opts.initial_skip &&
289 bts->num_events++ <= bts->synth_opts.initial_skip)
290 return 0;
291
d0170af7 292 sample.ip = le64_to_cpu(branch->from);
5d4f0eda 293 sample.cpumode = intel_bts_cpumode(bts, sample.ip);
d0170af7
AH
294 sample.pid = btsq->pid;
295 sample.tid = btsq->tid;
296 sample.addr = le64_to_cpu(branch->to);
297 sample.id = btsq->bts->branches_id;
298 sample.stream_id = btsq->bts->branches_id;
299 sample.period = 1;
300 sample.cpu = btsq->cpu;
301 sample.flags = btsq->sample_flags;
302 sample.insn_len = btsq->intel_pt_insn.length;
faaa8768 303 memcpy(sample.insn, btsq->intel_pt_insn.buf, INTEL_PT_INSN_BUF_SZ);
d0170af7 304
5d4f0eda
AH
305 event.sample.header.type = PERF_RECORD_SAMPLE;
306 event.sample.header.misc = sample.cpumode;
307 event.sample.header.size = sizeof(struct perf_event_header);
308
d0170af7
AH
309 if (bts->synth_opts.inject) {
310 event.sample.header.size = bts->branches_event_size;
311 ret = perf_event__synthesize_sample(&event,
312 bts->branches_sample_type,
936f1f30 313 0, &sample);
d0170af7
AH
314 if (ret)
315 return ret;
316 }
317
318 ret = perf_session__deliver_synth_event(bts->session, &event, &sample);
319 if (ret)
320 pr_err("Intel BTS: failed to deliver branch event, error %d\n",
321 ret);
322
323 return ret;
324}
325
326static int intel_bts_get_next_insn(struct intel_bts_queue *btsq, u64 ip)
327{
328 struct machine *machine = btsq->bts->machine;
329 struct thread *thread;
330 struct addr_location al;
32f98aab 331 unsigned char buf[INTEL_PT_INSN_BUF_SZ];
d0170af7
AH
332 ssize_t len;
333 int x86_64;
334 uint8_t cpumode;
335 int err = -1;
336
d0170af7
AH
337 if (machine__kernel_ip(machine, ip))
338 cpumode = PERF_RECORD_MISC_KERNEL;
339 else
340 cpumode = PERF_RECORD_MISC_USER;
341
342 thread = machine__find_thread(machine, -1, btsq->tid);
343 if (!thread)
344 return -1;
345
71a84b5a 346 if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso)
d0170af7
AH
347 goto out_put;
348
32f98aab
AH
349 len = dso__data_read_addr(al.map->dso, al.map, machine, ip, buf,
350 INTEL_PT_INSN_BUF_SZ);
d0170af7
AH
351 if (len <= 0)
352 goto out_put;
353
354 /* Load maps to ensure dso->is_64_bit has been updated */
be39db9f 355 map__load(al.map);
d0170af7
AH
356
357 x86_64 = al.map->dso->is_64_bit;
358
359 if (intel_pt_get_insn(buf, len, x86_64, &btsq->intel_pt_insn))
360 goto out_put;
361
362 err = 0;
363out_put:
364 thread__put(thread);
365 return err;
366}
367
368static int intel_bts_synth_error(struct intel_bts *bts, int cpu, pid_t pid,
369 pid_t tid, u64 ip)
370{
371 union perf_event event;
372 int err;
373
374 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
375 INTEL_BTS_ERR_NOINSN, cpu, pid, tid, ip,
376 "Failed to get instruction");
377
378 err = perf_session__deliver_synth_event(bts->session, &event, NULL);
379 if (err)
380 pr_err("Intel BTS: failed to deliver error event, error %d\n",
381 err);
382
383 return err;
384}
385
386static int intel_bts_get_branch_type(struct intel_bts_queue *btsq,
387 struct branch *branch)
388{
389 int err;
390
391 if (!branch->from) {
392 if (branch->to)
393 btsq->sample_flags = PERF_IP_FLAG_BRANCH |
394 PERF_IP_FLAG_TRACE_BEGIN;
395 else
396 btsq->sample_flags = 0;
397 btsq->intel_pt_insn.length = 0;
398 } else if (!branch->to) {
399 btsq->sample_flags = PERF_IP_FLAG_BRANCH |
400 PERF_IP_FLAG_TRACE_END;
401 btsq->intel_pt_insn.length = 0;
402 } else {
403 err = intel_bts_get_next_insn(btsq, branch->from);
404 if (err) {
405 btsq->sample_flags = 0;
406 btsq->intel_pt_insn.length = 0;
407 if (!btsq->bts->synth_opts.errors)
408 return 0;
409 err = intel_bts_synth_error(btsq->bts, btsq->cpu,
410 btsq->pid, btsq->tid,
411 branch->from);
412 return err;
413 }
414 btsq->sample_flags = intel_pt_insn_type(btsq->intel_pt_insn.op);
415 /* Check for an async branch into the kernel */
416 if (!machine__kernel_ip(btsq->bts->machine, branch->from) &&
417 machine__kernel_ip(btsq->bts->machine, branch->to) &&
418 btsq->sample_flags != (PERF_IP_FLAG_BRANCH |
419 PERF_IP_FLAG_CALL |
420 PERF_IP_FLAG_SYSCALLRET))
421 btsq->sample_flags = PERF_IP_FLAG_BRANCH |
422 PERF_IP_FLAG_CALL |
423 PERF_IP_FLAG_ASYNC |
424 PERF_IP_FLAG_INTERRUPT;
425 }
426
427 return 0;
428}
429
430static int intel_bts_process_buffer(struct intel_bts_queue *btsq,
50f73637
AH
431 struct auxtrace_buffer *buffer,
432 struct thread *thread)
d0170af7
AH
433{
434 struct branch *branch;
435 size_t sz, bsz = sizeof(struct branch);
436 u32 filter = btsq->bts->branches_filter;
437 int err = 0;
438
439 if (buffer->use_data) {
440 sz = buffer->use_size;
441 branch = buffer->use_data;
442 } else {
443 sz = buffer->size;
444 branch = buffer->data;
445 }
446
447 if (!btsq->bts->sample_branches)
448 return 0;
449
450 for (; sz > bsz; branch += 1, sz -= bsz) {
451 if (!branch->from && !branch->to)
452 continue;
453 intel_bts_get_branch_type(btsq, branch);
50f73637 454 if (btsq->bts->synth_opts.thread_stack)
256d92bc 455 thread_stack__event(thread, btsq->cpu, btsq->sample_flags,
50f73637
AH
456 le64_to_cpu(branch->from),
457 le64_to_cpu(branch->to),
458 btsq->intel_pt_insn.length,
459 buffer->buffer_nr + 1);
d0170af7
AH
460 if (filter && !(filter & btsq->sample_flags))
461 continue;
462 err = intel_bts_synth_branch_sample(btsq, branch);
463 if (err)
464 break;
465 }
466 return err;
467}
468
469static int intel_bts_process_queue(struct intel_bts_queue *btsq, u64 *timestamp)
470{
471 struct auxtrace_buffer *buffer = btsq->buffer, *old_buffer = buffer;
472 struct auxtrace_queue *queue;
473 struct thread *thread;
474 int err;
475
476 if (btsq->done)
477 return 1;
478
479 if (btsq->pid == -1) {
480 thread = machine__find_thread(btsq->bts->machine, -1,
481 btsq->tid);
482 if (thread)
483 btsq->pid = thread->pid_;
484 } else {
485 thread = machine__findnew_thread(btsq->bts->machine, btsq->pid,
486 btsq->tid);
487 }
488
489 queue = &btsq->bts->queues.queue_array[btsq->queue_nr];
490
491 if (!buffer)
492 buffer = auxtrace_buffer__next(queue, NULL);
493
494 if (!buffer) {
495 if (!btsq->bts->sampling_mode)
496 btsq->done = 1;
497 err = 1;
498 goto out_put;
499 }
500
501 /* Currently there is no support for split buffers */
502 if (buffer->consecutive) {
503 err = -EINVAL;
504 goto out_put;
505 }
506
507 if (!buffer->data) {
8ceb41d7 508 int fd = perf_data__fd(btsq->bts->session->data);
d0170af7
AH
509
510 buffer->data = auxtrace_buffer__get_data(buffer, fd);
511 if (!buffer->data) {
512 err = -ENOMEM;
513 goto out_put;
514 }
515 }
516
517 if (btsq->bts->snapshot_mode && !buffer->consecutive &&
518 intel_bts_do_fix_overlap(queue, buffer)) {
519 err = -ENOMEM;
520 goto out_put;
521 }
522
50f73637
AH
523 if (!btsq->bts->synth_opts.callchain &&
524 !btsq->bts->synth_opts.thread_stack && thread &&
d0170af7
AH
525 (!old_buffer || btsq->bts->sampling_mode ||
526 (btsq->bts->snapshot_mode && !buffer->consecutive)))
256d92bc 527 thread_stack__set_trace_nr(thread, btsq->cpu, buffer->buffer_nr + 1);
d0170af7 528
50f73637 529 err = intel_bts_process_buffer(btsq, buffer, thread);
d0170af7
AH
530
531 auxtrace_buffer__drop_data(buffer);
532
533 btsq->buffer = auxtrace_buffer__next(queue, buffer);
534 if (btsq->buffer) {
535 if (timestamp)
536 *timestamp = btsq->buffer->reference;
537 } else {
538 if (!btsq->bts->sampling_mode)
539 btsq->done = 1;
540 }
541out_put:
542 thread__put(thread);
543 return err;
544}
545
546static int intel_bts_flush_queue(struct intel_bts_queue *btsq)
547{
548 u64 ts = 0;
549 int ret;
550
551 while (1) {
552 ret = intel_bts_process_queue(btsq, &ts);
553 if (ret < 0)
554 return ret;
555 if (ret)
556 break;
557 }
558 return 0;
559}
560
561static int intel_bts_process_tid_exit(struct intel_bts *bts, pid_t tid)
562{
563 struct auxtrace_queues *queues = &bts->queues;
564 unsigned int i;
565
566 for (i = 0; i < queues->nr_queues; i++) {
567 struct auxtrace_queue *queue = &bts->queues.queue_array[i];
568 struct intel_bts_queue *btsq = queue->priv;
569
570 if (btsq && btsq->tid == tid)
571 return intel_bts_flush_queue(btsq);
572 }
573 return 0;
574}
575
576static int intel_bts_process_queues(struct intel_bts *bts, u64 timestamp)
577{
578 while (1) {
579 unsigned int queue_nr;
580 struct auxtrace_queue *queue;
581 struct intel_bts_queue *btsq;
582 u64 ts = 0;
583 int ret;
584
585 if (!bts->heap.heap_cnt)
586 return 0;
587
588 if (bts->heap.heap_array[0].ordinal > timestamp)
589 return 0;
590
591 queue_nr = bts->heap.heap_array[0].queue_nr;
592 queue = &bts->queues.queue_array[queue_nr];
593 btsq = queue->priv;
594
595 auxtrace_heap__pop(&bts->heap);
596
597 ret = intel_bts_process_queue(btsq, &ts);
598 if (ret < 0) {
599 auxtrace_heap__add(&bts->heap, queue_nr, ts);
600 return ret;
601 }
602
603 if (!ret) {
604 ret = auxtrace_heap__add(&bts->heap, queue_nr, ts);
605 if (ret < 0)
606 return ret;
607 } else {
608 btsq->on_heap = false;
609 }
610 }
611
612 return 0;
613}
614
615static int intel_bts_process_event(struct perf_session *session,
616 union perf_event *event,
617 struct perf_sample *sample,
618 struct perf_tool *tool)
619{
620 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
621 auxtrace);
622 u64 timestamp;
623 int err;
624
625 if (dump_trace)
626 return 0;
627
628 if (!tool->ordered_events) {
629 pr_err("Intel BTS requires ordered events\n");
630 return -EINVAL;
631 }
632
633 if (sample->time && sample->time != (u64)-1)
634 timestamp = perf_time_to_tsc(sample->time, &bts->tc);
635 else
636 timestamp = 0;
637
638 err = intel_bts_update_queues(bts);
639 if (err)
640 return err;
641
642 err = intel_bts_process_queues(bts, timestamp);
643 if (err)
644 return err;
645 if (event->header.type == PERF_RECORD_EXIT) {
53ff6bc3 646 err = intel_bts_process_tid_exit(bts, event->fork.tid);
d0170af7
AH
647 if (err)
648 return err;
649 }
650
651 if (event->header.type == PERF_RECORD_AUX &&
652 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
653 bts->synth_opts.errors)
654 err = intel_bts_lost(bts, sample);
655
656 return err;
657}
658
659static int intel_bts_process_auxtrace_event(struct perf_session *session,
660 union perf_event *event,
661 struct perf_tool *tool __maybe_unused)
662{
663 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
664 auxtrace);
665
666 if (bts->sampling_mode)
667 return 0;
668
669 if (!bts->data_queued) {
670 struct auxtrace_buffer *buffer;
671 off_t data_offset;
8ceb41d7 672 int fd = perf_data__fd(session->data);
d0170af7
AH
673 int err;
674
8ceb41d7 675 if (perf_data__is_pipe(session->data)) {
d0170af7
AH
676 data_offset = 0;
677 } else {
678 data_offset = lseek(fd, 0, SEEK_CUR);
679 if (data_offset == -1)
680 return -errno;
681 }
682
683 err = auxtrace_queues__add_event(&bts->queues, session, event,
684 data_offset, &buffer);
685 if (err)
686 return err;
687
688 /* Dump here now we have copied a piped trace out of the pipe */
689 if (dump_trace) {
690 if (auxtrace_buffer__get_data(buffer, fd)) {
691 intel_bts_dump_event(bts, buffer->data,
692 buffer->size);
693 auxtrace_buffer__put_data(buffer);
694 }
695 }
696 }
697
698 return 0;
699}
700
b8f8eb84 701static int intel_bts_flush(struct perf_session *session,
d0170af7
AH
702 struct perf_tool *tool __maybe_unused)
703{
704 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
705 auxtrace);
706 int ret;
707
708 if (dump_trace || bts->sampling_mode)
709 return 0;
710
711 if (!tool->ordered_events)
712 return -EINVAL;
713
714 ret = intel_bts_update_queues(bts);
715 if (ret < 0)
716 return ret;
717
718 return intel_bts_process_queues(bts, MAX_TIMESTAMP);
719}
720
721static void intel_bts_free_queue(void *priv)
722{
723 struct intel_bts_queue *btsq = priv;
724
725 if (!btsq)
726 return;
727 free(btsq);
728}
729
730static void intel_bts_free_events(struct perf_session *session)
731{
732 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
733 auxtrace);
734 struct auxtrace_queues *queues = &bts->queues;
735 unsigned int i;
736
737 for (i = 0; i < queues->nr_queues; i++) {
738 intel_bts_free_queue(queues->queue_array[i].priv);
739 queues->queue_array[i].priv = NULL;
740 }
741 auxtrace_queues__free(queues);
742}
743
744static void intel_bts_free(struct perf_session *session)
745{
746 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
747 auxtrace);
748
749 auxtrace_heap__free(&bts->heap);
750 intel_bts_free_events(session);
751 session->auxtrace = NULL;
752 free(bts);
753}
754
755struct intel_bts_synth {
756 struct perf_tool dummy_tool;
757 struct perf_session *session;
758};
759
760static int intel_bts_event_synth(struct perf_tool *tool,
761 union perf_event *event,
762 struct perf_sample *sample __maybe_unused,
763 struct machine *machine __maybe_unused)
764{
765 struct intel_bts_synth *intel_bts_synth =
766 container_of(tool, struct intel_bts_synth, dummy_tool);
767
768 return perf_session__deliver_synth_event(intel_bts_synth->session,
769 event, NULL);
770}
771
772static int intel_bts_synth_event(struct perf_session *session,
773 struct perf_event_attr *attr, u64 id)
774{
775 struct intel_bts_synth intel_bts_synth;
776
777 memset(&intel_bts_synth, 0, sizeof(struct intel_bts_synth));
778 intel_bts_synth.session = session;
779
780 return perf_event__synthesize_attr(&intel_bts_synth.dummy_tool, attr, 1,
781 &id, intel_bts_event_synth);
782}
783
784static int intel_bts_synth_events(struct intel_bts *bts,
785 struct perf_session *session)
786{
787 struct perf_evlist *evlist = session->evlist;
788 struct perf_evsel *evsel;
789 struct perf_event_attr attr;
790 bool found = false;
791 u64 id;
792 int err;
793
e5cadb93 794 evlist__for_each_entry(evlist, evsel) {
d0170af7
AH
795 if (evsel->attr.type == bts->pmu_type && evsel->ids) {
796 found = true;
797 break;
798 }
799 }
800
801 if (!found) {
802 pr_debug("There are no selected events with Intel BTS data\n");
803 return 0;
804 }
805
806 memset(&attr, 0, sizeof(struct perf_event_attr));
807 attr.size = sizeof(struct perf_event_attr);
808 attr.type = PERF_TYPE_HARDWARE;
809 attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
810 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
811 PERF_SAMPLE_PERIOD;
812 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
813 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
814 attr.exclude_user = evsel->attr.exclude_user;
815 attr.exclude_kernel = evsel->attr.exclude_kernel;
816 attr.exclude_hv = evsel->attr.exclude_hv;
817 attr.exclude_host = evsel->attr.exclude_host;
818 attr.exclude_guest = evsel->attr.exclude_guest;
819 attr.sample_id_all = evsel->attr.sample_id_all;
820 attr.read_format = evsel->attr.read_format;
821
822 id = evsel->id[0] + 1000000000;
823 if (!id)
824 id = 1;
825
826 if (bts->synth_opts.branches) {
827 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
828 attr.sample_period = 1;
829 attr.sample_type |= PERF_SAMPLE_ADDR;
830 pr_debug("Synthesizing 'branches' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
831 id, (u64)attr.sample_type);
832 err = intel_bts_synth_event(session, &attr, id);
833 if (err) {
834 pr_err("%s: failed to synthesize 'branches' event type\n",
835 __func__);
836 return err;
837 }
838 bts->sample_branches = true;
839 bts->branches_sample_type = attr.sample_type;
840 bts->branches_id = id;
841 /*
842 * We only use sample types from PERF_SAMPLE_MASK so we can use
843 * __perf_evsel__sample_size() here.
844 */
845 bts->branches_event_size = sizeof(struct sample_event) +
846 __perf_evsel__sample_size(attr.sample_type);
847 }
848
d0170af7
AH
849 return 0;
850}
851
852static const char * const intel_bts_info_fmts[] = {
853 [INTEL_BTS_PMU_TYPE] = " PMU Type %"PRId64"\n",
854 [INTEL_BTS_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
855 [INTEL_BTS_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
856 [INTEL_BTS_TIME_ZERO] = " Time Zero %"PRIu64"\n",
857 [INTEL_BTS_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
858 [INTEL_BTS_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
859};
860
861static void intel_bts_print_info(u64 *arr, int start, int finish)
862{
863 int i;
864
865 if (!dump_trace)
866 return;
867
868 for (i = start; i <= finish; i++)
869 fprintf(stdout, intel_bts_info_fmts[i], arr[i]);
870}
871
d0170af7
AH
872int intel_bts_process_auxtrace_info(union perf_event *event,
873 struct perf_session *session)
874{
875 struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
876 size_t min_sz = sizeof(u64) * INTEL_BTS_SNAPSHOT_MODE;
877 struct intel_bts *bts;
878 int err;
879
880 if (auxtrace_info->header.size < sizeof(struct auxtrace_info_event) +
881 min_sz)
882 return -EINVAL;
883
884 bts = zalloc(sizeof(struct intel_bts));
885 if (!bts)
886 return -ENOMEM;
887
888 err = auxtrace_queues__init(&bts->queues);
889 if (err)
890 goto err_free;
891
892 bts->session = session;
893 bts->machine = &session->machines.host; /* No kvm support */
894 bts->auxtrace_type = auxtrace_info->type;
895 bts->pmu_type = auxtrace_info->priv[INTEL_BTS_PMU_TYPE];
896 bts->tc.time_shift = auxtrace_info->priv[INTEL_BTS_TIME_SHIFT];
897 bts->tc.time_mult = auxtrace_info->priv[INTEL_BTS_TIME_MULT];
898 bts->tc.time_zero = auxtrace_info->priv[INTEL_BTS_TIME_ZERO];
899 bts->cap_user_time_zero =
900 auxtrace_info->priv[INTEL_BTS_CAP_USER_TIME_ZERO];
901 bts->snapshot_mode = auxtrace_info->priv[INTEL_BTS_SNAPSHOT_MODE];
902
903 bts->sampling_mode = false;
904
905 bts->auxtrace.process_event = intel_bts_process_event;
906 bts->auxtrace.process_auxtrace_event = intel_bts_process_auxtrace_event;
907 bts->auxtrace.flush_events = intel_bts_flush;
908 bts->auxtrace.free_events = intel_bts_free_events;
909 bts->auxtrace.free = intel_bts_free;
910 session->auxtrace = &bts->auxtrace;
911
912 intel_bts_print_info(&auxtrace_info->priv[0], INTEL_BTS_PMU_TYPE,
913 INTEL_BTS_SNAPSHOT_MODE);
914
915 if (dump_trace)
916 return 0;
917
50f73637 918 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
d0170af7 919 bts->synth_opts = *session->itrace_synth_opts;
50f73637 920 } else {
4eb06815
AK
921 itrace_synth_opts__set_default(&bts->synth_opts,
922 session->itrace_synth_opts->default_no_sample);
50f73637
AH
923 if (session->itrace_synth_opts)
924 bts->synth_opts.thread_stack =
925 session->itrace_synth_opts->thread_stack;
926 }
d0170af7
AH
927
928 if (bts->synth_opts.calls)
929 bts->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
930 PERF_IP_FLAG_TRACE_END;
931 if (bts->synth_opts.returns)
932 bts->branches_filter |= PERF_IP_FLAG_RETURN |
933 PERF_IP_FLAG_TRACE_BEGIN;
934
935 err = intel_bts_synth_events(bts, session);
936 if (err)
937 goto err_free_queues;
938
939 err = auxtrace_queues__process_index(&bts->queues, session);
940 if (err)
941 goto err_free_queues;
942
943 if (bts->queues.populated)
944 bts->data_queued = true;
945
946 return 0;
947
948err_free_queues:
949 auxtrace_queues__free(&bts->queues);
950 session->auxtrace = NULL;
951err_free:
952 free(bts);
953 return err;
954}