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