| 570 | int PerfEvents::_target_cpu; |
| 571 | |
| 572 | int PerfEvents::createForThread(int tid) { |
| 573 | if (tid >= _max_events) { |
| 574 | Log::warn("tid[%d] > pid_max[%d]. Restart profiler after changing pid_max", tid, _max_events); |
| 575 | return -1; |
| 576 | } |
| 577 | |
| 578 | // Mark _events[tid] early to prevent duplicates. Real fd will be put later. |
| 579 | if (!__sync_bool_compare_and_swap(&_events[tid]._fd, 0, -1)) { |
| 580 | // Lost race. The event is created either from PerfEvents::start() or from pthread hook. |
| 581 | return -1; |
| 582 | } |
| 583 | |
| 584 | PerfEventType* event_type = _event_type; |
| 585 | struct perf_event_attr attr = {0}; |
| 586 | attr.size = sizeof(attr); |
| 587 | attr.type = event_type->type; |
| 588 | |
| 589 | if (attr.type == PERF_TYPE_BREAKPOINT) { |
| 590 | attr.bp_type = event_type->config; |
| 591 | } else { |
| 592 | attr.config = event_type->config; |
| 593 | } |
| 594 | attr.config1 = event_type->config1; |
| 595 | attr.config2 = event_type->config2; |
| 596 | |
| 597 | // Hardware events may not always support zero skid |
| 598 | if (attr.type == PERF_TYPE_SOFTWARE) { |
| 599 | attr.precise_ip = 2; |
| 600 | } |
| 601 | |
| 602 | attr.sample_period = _interval; |
| 603 | attr.sample_type = PERF_SAMPLE_CALLCHAIN; |
| 604 | attr.disabled = 1; |
| 605 | attr.wakeup_events = 1; |
| 606 | |
| 607 | // flags for multiplexing support |
| 608 | attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING; |
| 609 | |
| 610 | if (_alluser) { |
| 611 | attr.exclude_kernel = 1; |
| 612 | } |
| 613 | |
| 614 | if (!_kernel_stack) { |
| 615 | attr.exclude_callchain_kernel = 1; |
| 616 | } |
| 617 | |
| 618 | if (_cstack >= CSTACK_FP) { |
| 619 | attr.exclude_callchain_user = 1; |
| 620 | } |
| 621 | |
| 622 | if (_record_cpu) { |
| 623 | attr.sample_type |= PERF_SAMPLE_CPU; |
| 624 | } |
| 625 | |
| 626 | int fd; |
| 627 | if (FdTransferClient::hasPeer()) { |
| 628 | fd = FdTransferClient::requestPerfFd(&tid, _target_cpu, &attr, PerfEventType::probe_func); |
| 629 | } else { |