1 // SPDX-License-Identifier: GPL-2.0-only
3 * intel_pt.c: Intel Processor Trace support
4 * Copyright (c) 2013-2015, Intel Corporation.
9 #include <linux/kernel.h>
10 #include <linux/types.h>
11 #include <linux/bitops.h>
12 #include <linux/log2.h>
13 #include <linux/zalloc.h>
16 #include "../../../util/session.h"
17 #include "../../../util/event.h"
18 #include "../../../util/evlist.h"
19 #include "../../../util/evsel.h"
20 #include "../../../util/evsel_config.h"
21 #include "../../../util/cpumap.h"
22 #include "../../../util/mmap.h"
23 #include <subcmd/parse-options.h>
24 #include "../../../util/parse-events.h"
25 #include "../../../util/pmu.h"
26 #include "../../../util/debug.h"
27 #include "../../../util/auxtrace.h"
28 #include "../../../util/perf_api_probe.h"
29 #include "../../../util/record.h"
30 #include "../../../util/target.h"
31 #include "../../../util/tsc.h"
32 #include <internal/lib.h> // page_size
33 #include "../../../util/intel-pt.h"
35 #define KiB(x) ((x) * 1024)
36 #define MiB(x) ((x) * 1024 * 1024)
37 #define KiB_MASK(x) (KiB(x) - 1)
38 #define MiB_MASK(x) (MiB(x) - 1)
40 #define INTEL_PT_PSB_PERIOD_NEAR 256
42 struct intel_pt_snapshot_ref
{
48 struct intel_pt_recording
{
49 struct auxtrace_record itr
;
50 struct perf_pmu
*intel_pt_pmu
;
51 int have_sched_switch
;
52 struct evlist
*evlist
;
54 bool snapshot_init_done
;
56 size_t snapshot_ref_buf_size
;
58 struct intel_pt_snapshot_ref
*snapshot_refs
;
62 static int intel_pt_parse_terms_with_default(const char *pmu_name
,
63 struct list_head
*formats
,
67 struct list_head
*terms
;
68 struct perf_event_attr attr
= { .size
= 0, };
71 terms
= malloc(sizeof(struct list_head
));
75 INIT_LIST_HEAD(terms
);
77 err
= parse_events_terms(terms
, str
);
81 attr
.config
= *config
;
82 err
= perf_pmu__config_terms(pmu_name
, formats
, &attr
, terms
, true,
87 *config
= attr
.config
;
89 parse_events_terms__delete(terms
);
93 static int intel_pt_parse_terms(const char *pmu_name
, struct list_head
*formats
,
94 const char *str
, u64
*config
)
97 return intel_pt_parse_terms_with_default(pmu_name
, formats
, str
,
101 static u64
intel_pt_masked_bits(u64 mask
, u64 bits
)
103 const u64 top_bit
= 1ULL << 63;
107 for (i
= 0; i
< 64; i
++) {
108 if (mask
& top_bit
) {
120 static int intel_pt_read_config(struct perf_pmu
*intel_pt_pmu
, const char *str
,
121 struct evlist
*evlist
, u64
*res
)
128 mask
= perf_pmu__format_bits(&intel_pt_pmu
->format
, str
);
132 evlist__for_each_entry(evlist
, evsel
) {
133 if (evsel
->core
.attr
.type
== intel_pt_pmu
->type
) {
134 *res
= intel_pt_masked_bits(mask
, evsel
->core
.attr
.config
);
142 static size_t intel_pt_psb_period(struct perf_pmu
*intel_pt_pmu
,
143 struct evlist
*evlist
)
146 int err
, topa_multiple_entries
;
149 if (perf_pmu__scan_file(intel_pt_pmu
, "caps/topa_multiple_entries",
150 "%d", &topa_multiple_entries
) != 1)
151 topa_multiple_entries
= 0;
154 * Use caps/topa_multiple_entries to indicate early hardware that had
155 * extra frequent PSBs.
157 if (!topa_multiple_entries
) {
162 err
= intel_pt_read_config(intel_pt_pmu
, "psb_period", evlist
, &val
);
166 psb_period
= 1 << (val
+ 11);
168 pr_debug2("%s psb_period %zu\n", intel_pt_pmu
->name
, psb_period
);
172 static int intel_pt_pick_bit(int bits
, int target
)
176 for (pos
= 0; bits
; bits
>>= 1, pos
++) {
178 if (pos
<= target
|| pick
< 0)
188 static u64
intel_pt_default_config(struct perf_pmu
*intel_pt_pmu
)
191 int mtc
, mtc_periods
= 0, mtc_period
;
192 int psb_cyc
, psb_periods
, psb_period
;
197 pos
+= scnprintf(buf
+ pos
, sizeof(buf
) - pos
, "tsc");
199 if (perf_pmu__scan_file(intel_pt_pmu
, "caps/mtc", "%d",
204 if (perf_pmu__scan_file(intel_pt_pmu
, "caps/mtc_periods", "%x",
208 mtc_period
= intel_pt_pick_bit(mtc_periods
, 3);
209 pos
+= scnprintf(buf
+ pos
, sizeof(buf
) - pos
,
210 ",mtc,mtc_period=%d", mtc_period
);
214 if (perf_pmu__scan_file(intel_pt_pmu
, "caps/psb_cyc", "%d",
218 if (psb_cyc
&& mtc_periods
) {
219 if (perf_pmu__scan_file(intel_pt_pmu
, "caps/psb_periods", "%x",
223 psb_period
= intel_pt_pick_bit(psb_periods
, 3);
224 pos
+= scnprintf(buf
+ pos
, sizeof(buf
) - pos
,
225 ",psb_period=%d", psb_period
);
229 if (perf_pmu__scan_file(intel_pt_pmu
, "format/pt", "%c", &c
) == 1 &&
230 perf_pmu__scan_file(intel_pt_pmu
, "format/branch", "%c", &c
) == 1)
231 pos
+= scnprintf(buf
+ pos
, sizeof(buf
) - pos
, ",pt,branch");
233 pr_debug2("%s default config: %s\n", intel_pt_pmu
->name
, buf
);
235 intel_pt_parse_terms(intel_pt_pmu
->name
, &intel_pt_pmu
->format
, buf
,
241 static int intel_pt_parse_snapshot_options(struct auxtrace_record
*itr
,
242 struct record_opts
*opts
,
245 struct intel_pt_recording
*ptr
=
246 container_of(itr
, struct intel_pt_recording
, itr
);
247 unsigned long long snapshot_size
= 0;
251 snapshot_size
= strtoull(str
, &endptr
, 0);
252 if (*endptr
|| snapshot_size
> SIZE_MAX
)
256 opts
->auxtrace_snapshot_mode
= true;
257 opts
->auxtrace_snapshot_size
= snapshot_size
;
259 ptr
->snapshot_size
= snapshot_size
;
264 struct perf_event_attr
*
265 intel_pt_pmu_default_config(struct perf_pmu
*intel_pt_pmu
)
267 struct perf_event_attr
*attr
;
269 attr
= zalloc(sizeof(struct perf_event_attr
));
273 attr
->config
= intel_pt_default_config(intel_pt_pmu
);
275 intel_pt_pmu
->selectable
= true;
280 static const char *intel_pt_find_filter(struct evlist
*evlist
,
281 struct perf_pmu
*intel_pt_pmu
)
285 evlist__for_each_entry(evlist
, evsel
) {
286 if (evsel
->core
.attr
.type
== intel_pt_pmu
->type
)
287 return evsel
->filter
;
293 static size_t intel_pt_filter_bytes(const char *filter
)
295 size_t len
= filter
? strlen(filter
) : 0;
297 return len
? roundup(len
+ 1, 8) : 0;
301 intel_pt_info_priv_size(struct auxtrace_record
*itr
, struct evlist
*evlist
)
303 struct intel_pt_recording
*ptr
=
304 container_of(itr
, struct intel_pt_recording
, itr
);
305 const char *filter
= intel_pt_find_filter(evlist
, ptr
->intel_pt_pmu
);
307 ptr
->priv_size
= (INTEL_PT_AUXTRACE_PRIV_MAX
* sizeof(u64
)) +
308 intel_pt_filter_bytes(filter
);
310 return ptr
->priv_size
;
313 static void intel_pt_tsc_ctc_ratio(u32
*n
, u32
*d
)
315 unsigned int eax
= 0, ebx
= 0, ecx
= 0, edx
= 0;
317 __get_cpuid(0x15, &eax
, &ebx
, &ecx
, &edx
);
322 static int intel_pt_info_fill(struct auxtrace_record
*itr
,
323 struct perf_session
*session
,
324 struct perf_record_auxtrace_info
*auxtrace_info
,
327 struct intel_pt_recording
*ptr
=
328 container_of(itr
, struct intel_pt_recording
, itr
);
329 struct perf_pmu
*intel_pt_pmu
= ptr
->intel_pt_pmu
;
330 struct perf_event_mmap_page
*pc
;
331 struct perf_tsc_conversion tc
= { .time_mult
= 0, };
332 bool cap_user_time_zero
= false, per_cpu_mmaps
;
333 u64 tsc_bit
, mtc_bit
, mtc_freq_bits
, cyc_bit
, noretcomp_bit
;
334 u32 tsc_ctc_ratio_n
, tsc_ctc_ratio_d
;
335 unsigned long max_non_turbo_ratio
;
336 size_t filter_str_len
;
341 if (priv_size
!= ptr
->priv_size
)
344 intel_pt_parse_terms(intel_pt_pmu
->name
, &intel_pt_pmu
->format
,
346 intel_pt_parse_terms(intel_pt_pmu
->name
, &intel_pt_pmu
->format
,
347 "noretcomp", &noretcomp_bit
);
348 intel_pt_parse_terms(intel_pt_pmu
->name
, &intel_pt_pmu
->format
,
350 mtc_freq_bits
= perf_pmu__format_bits(&intel_pt_pmu
->format
,
352 intel_pt_parse_terms(intel_pt_pmu
->name
, &intel_pt_pmu
->format
,
355 intel_pt_tsc_ctc_ratio(&tsc_ctc_ratio_n
, &tsc_ctc_ratio_d
);
357 if (perf_pmu__scan_file(intel_pt_pmu
, "max_nonturbo_ratio",
358 "%lu", &max_non_turbo_ratio
) != 1)
359 max_non_turbo_ratio
= 0;
361 filter
= intel_pt_find_filter(session
->evlist
, ptr
->intel_pt_pmu
);
362 filter_str_len
= filter
? strlen(filter
) : 0;
364 if (!session
->evlist
->core
.nr_mmaps
)
367 pc
= session
->evlist
->mmap
[0].core
.base
;
369 err
= perf_read_tsc_conversion(pc
, &tc
);
371 if (err
!= -EOPNOTSUPP
)
374 cap_user_time_zero
= tc
.time_mult
!= 0;
376 if (!cap_user_time_zero
)
377 ui__warning("Intel Processor Trace: TSC not available\n");
380 per_cpu_mmaps
= !perf_cpu_map__empty(session
->evlist
->core
.cpus
);
382 auxtrace_info
->type
= PERF_AUXTRACE_INTEL_PT
;
383 auxtrace_info
->priv
[INTEL_PT_PMU_TYPE
] = intel_pt_pmu
->type
;
384 auxtrace_info
->priv
[INTEL_PT_TIME_SHIFT
] = tc
.time_shift
;
385 auxtrace_info
->priv
[INTEL_PT_TIME_MULT
] = tc
.time_mult
;
386 auxtrace_info
->priv
[INTEL_PT_TIME_ZERO
] = tc
.time_zero
;
387 auxtrace_info
->priv
[INTEL_PT_CAP_USER_TIME_ZERO
] = cap_user_time_zero
;
388 auxtrace_info
->priv
[INTEL_PT_TSC_BIT
] = tsc_bit
;
389 auxtrace_info
->priv
[INTEL_PT_NORETCOMP_BIT
] = noretcomp_bit
;
390 auxtrace_info
->priv
[INTEL_PT_HAVE_SCHED_SWITCH
] = ptr
->have_sched_switch
;
391 auxtrace_info
->priv
[INTEL_PT_SNAPSHOT_MODE
] = ptr
->snapshot_mode
;
392 auxtrace_info
->priv
[INTEL_PT_PER_CPU_MMAPS
] = per_cpu_mmaps
;
393 auxtrace_info
->priv
[INTEL_PT_MTC_BIT
] = mtc_bit
;
394 auxtrace_info
->priv
[INTEL_PT_MTC_FREQ_BITS
] = mtc_freq_bits
;
395 auxtrace_info
->priv
[INTEL_PT_TSC_CTC_N
] = tsc_ctc_ratio_n
;
396 auxtrace_info
->priv
[INTEL_PT_TSC_CTC_D
] = tsc_ctc_ratio_d
;
397 auxtrace_info
->priv
[INTEL_PT_CYC_BIT
] = cyc_bit
;
398 auxtrace_info
->priv
[INTEL_PT_MAX_NONTURBO_RATIO
] = max_non_turbo_ratio
;
399 auxtrace_info
->priv
[INTEL_PT_FILTER_STR_LEN
] = filter_str_len
;
401 info
= &auxtrace_info
->priv
[INTEL_PT_FILTER_STR_LEN
] + 1;
403 if (filter_str_len
) {
404 size_t len
= intel_pt_filter_bytes(filter
);
406 strncpy((char *)info
, filter
, len
);
413 static int intel_pt_track_switches(struct evlist
*evlist
)
415 const char *sched_switch
= "sched:sched_switch";
419 if (!evlist__can_select_event(evlist
, sched_switch
))
422 err
= parse_events(evlist
, sched_switch
, NULL
);
424 pr_debug2("%s: failed to parse %s, error %d\n",
425 __func__
, sched_switch
, err
);
429 evsel
= evlist__last(evlist
);
431 evsel__set_sample_bit(evsel
, CPU
);
432 evsel__set_sample_bit(evsel
, TIME
);
434 evsel
->core
.system_wide
= true;
435 evsel
->no_aux_samples
= true;
436 evsel
->immediate
= true;
441 static void intel_pt_valid_str(char *str
, size_t len
, u64 valid
)
443 unsigned int val
, last
= 0, state
= 1;
448 for (val
= 0; val
<= 64; val
++, valid
>>= 1) {
453 p
+= scnprintf(str
+ p
, len
- p
, ",");
456 p
+= scnprintf(str
+ p
, len
- p
, "%u", val
);
471 p
+= scnprintf(str
+ p
, len
- p
, ",%u", last
);
475 p
+= scnprintf(str
+ p
, len
- p
, "-%u", last
);
487 static int intel_pt_val_config_term(struct perf_pmu
*intel_pt_pmu
,
488 const char *caps
, const char *name
,
489 const char *supported
, u64 config
)
493 unsigned long long valid
;
497 if (perf_pmu__scan_file(intel_pt_pmu
, caps
, "%llx", &valid
) != 1)
501 perf_pmu__scan_file(intel_pt_pmu
, supported
, "%d", &ok
) == 1 && !ok
)
506 bits
= perf_pmu__format_bits(&intel_pt_pmu
->format
, name
);
510 for (shift
= 0; bits
&& !(bits
& 1); shift
++)
518 if (valid
& (1 << config
))
521 intel_pt_valid_str(valid_str
, sizeof(valid_str
), valid
);
522 pr_err("Invalid %s for %s. Valid values are: %s\n",
523 name
, INTEL_PT_PMU_NAME
, valid_str
);
527 static int intel_pt_validate_config(struct perf_pmu
*intel_pt_pmu
,
537 * If supported, force pass-through config term (pt=1) even if user
538 * sets pt=0, which avoids senseless kernel errors.
540 if (perf_pmu__scan_file(intel_pt_pmu
, "format/pt", "%c", &c
) == 1 &&
541 !(evsel
->core
.attr
.config
& 1)) {
542 pr_warning("pt=0 doesn't make sense, forcing pt=1\n");
543 evsel
->core
.attr
.config
|= 1;
546 err
= intel_pt_val_config_term(intel_pt_pmu
, "caps/cycle_thresholds",
547 "cyc_thresh", "caps/psb_cyc",
548 evsel
->core
.attr
.config
);
552 err
= intel_pt_val_config_term(intel_pt_pmu
, "caps/mtc_periods",
553 "mtc_period", "caps/mtc",
554 evsel
->core
.attr
.config
);
558 return intel_pt_val_config_term(intel_pt_pmu
, "caps/psb_periods",
559 "psb_period", "caps/psb_cyc",
560 evsel
->core
.attr
.config
);
563 static void intel_pt_config_sample_mode(struct perf_pmu
*intel_pt_pmu
,
566 u64 user_bits
= 0, bits
;
567 struct evsel_config_term
*term
= evsel__get_config_term(evsel
, CFG_CHG
);
570 user_bits
= term
->val
.cfg_chg
;
572 bits
= perf_pmu__format_bits(&intel_pt_pmu
->format
, "psb_period");
574 /* Did user change psb_period */
575 if (bits
& user_bits
)
578 /* Set psb_period to 0 */
579 evsel
->core
.attr
.config
&= ~bits
;
582 static void intel_pt_min_max_sample_sz(struct evlist
*evlist
,
583 size_t *min_sz
, size_t *max_sz
)
587 evlist__for_each_entry(evlist
, evsel
) {
588 size_t sz
= evsel
->core
.attr
.aux_sample_size
;
592 if (min_sz
&& (sz
< *min_sz
|| !*min_sz
))
594 if (max_sz
&& sz
> *max_sz
)
600 * Currently, there is not enough information to disambiguate different PEBS
601 * events, so only allow one.
603 static bool intel_pt_too_many_aux_output(struct evlist
*evlist
)
606 int aux_output_cnt
= 0;
608 evlist__for_each_entry(evlist
, evsel
)
609 aux_output_cnt
+= !!evsel
->core
.attr
.aux_output
;
611 if (aux_output_cnt
> 1) {
612 pr_err(INTEL_PT_PMU_NAME
" supports at most one event with aux-output\n");
619 static int intel_pt_recording_options(struct auxtrace_record
*itr
,
620 struct evlist
*evlist
,
621 struct record_opts
*opts
)
623 struct intel_pt_recording
*ptr
=
624 container_of(itr
, struct intel_pt_recording
, itr
);
625 struct perf_pmu
*intel_pt_pmu
= ptr
->intel_pt_pmu
;
626 bool have_timing_info
, need_immediate
= false;
627 struct evsel
*evsel
, *intel_pt_evsel
= NULL
;
628 const struct perf_cpu_map
*cpus
= evlist
->core
.cpus
;
629 bool privileged
= perf_event_paranoid_check(-1);
633 ptr
->evlist
= evlist
;
634 ptr
->snapshot_mode
= opts
->auxtrace_snapshot_mode
;
636 evlist__for_each_entry(evlist
, evsel
) {
637 if (evsel
->core
.attr
.type
== intel_pt_pmu
->type
) {
638 if (intel_pt_evsel
) {
639 pr_err("There may be only one " INTEL_PT_PMU_NAME
" event\n");
642 evsel
->core
.attr
.freq
= 0;
643 evsel
->core
.attr
.sample_period
= 1;
644 evsel
->no_aux_samples
= true;
645 intel_pt_evsel
= evsel
;
646 opts
->full_auxtrace
= true;
650 if (opts
->auxtrace_snapshot_mode
&& !opts
->full_auxtrace
) {
651 pr_err("Snapshot mode (-S option) requires " INTEL_PT_PMU_NAME
" PMU event (-e " INTEL_PT_PMU_NAME
")\n");
655 if (opts
->auxtrace_snapshot_mode
&& opts
->auxtrace_sample_mode
) {
656 pr_err("Snapshot mode (" INTEL_PT_PMU_NAME
" PMU) and sample trace cannot be used together\n");
660 if (opts
->use_clockid
) {
661 pr_err("Cannot use clockid (-k option) with " INTEL_PT_PMU_NAME
"\n");
665 if (intel_pt_too_many_aux_output(evlist
))
668 if (!opts
->full_auxtrace
)
671 if (opts
->auxtrace_sample_mode
)
672 intel_pt_config_sample_mode(intel_pt_pmu
, intel_pt_evsel
);
674 err
= intel_pt_validate_config(intel_pt_pmu
, intel_pt_evsel
);
678 /* Set default sizes for snapshot mode */
679 if (opts
->auxtrace_snapshot_mode
) {
680 size_t psb_period
= intel_pt_psb_period(intel_pt_pmu
, evlist
);
682 if (!opts
->auxtrace_snapshot_size
&& !opts
->auxtrace_mmap_pages
) {
684 opts
->auxtrace_mmap_pages
= MiB(4) / page_size
;
686 opts
->auxtrace_mmap_pages
= KiB(128) / page_size
;
687 if (opts
->mmap_pages
== UINT_MAX
)
688 opts
->mmap_pages
= KiB(256) / page_size
;
690 } else if (!opts
->auxtrace_mmap_pages
&& !privileged
&&
691 opts
->mmap_pages
== UINT_MAX
) {
692 opts
->mmap_pages
= KiB(256) / page_size
;
694 if (!opts
->auxtrace_snapshot_size
)
695 opts
->auxtrace_snapshot_size
=
696 opts
->auxtrace_mmap_pages
* (size_t)page_size
;
697 if (!opts
->auxtrace_mmap_pages
) {
698 size_t sz
= opts
->auxtrace_snapshot_size
;
700 sz
= round_up(sz
, page_size
) / page_size
;
701 opts
->auxtrace_mmap_pages
= roundup_pow_of_two(sz
);
703 if (opts
->auxtrace_snapshot_size
>
704 opts
->auxtrace_mmap_pages
* (size_t)page_size
) {
705 pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
706 opts
->auxtrace_snapshot_size
,
707 opts
->auxtrace_mmap_pages
* (size_t)page_size
);
710 if (!opts
->auxtrace_snapshot_size
|| !opts
->auxtrace_mmap_pages
) {
711 pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
714 pr_debug2("Intel PT snapshot size: %zu\n",
715 opts
->auxtrace_snapshot_size
);
717 opts
->auxtrace_snapshot_size
<= psb_period
+
718 INTEL_PT_PSB_PERIOD_NEAR
)
719 ui__warning("Intel PT snapshot size (%zu) may be too small for PSB period (%zu)\n",
720 opts
->auxtrace_snapshot_size
, psb_period
);
723 /* Set default sizes for sample mode */
724 if (opts
->auxtrace_sample_mode
) {
725 size_t psb_period
= intel_pt_psb_period(intel_pt_pmu
, evlist
);
726 size_t min_sz
= 0, max_sz
= 0;
728 intel_pt_min_max_sample_sz(evlist
, &min_sz
, &max_sz
);
729 if (!opts
->auxtrace_mmap_pages
&& !privileged
&&
730 opts
->mmap_pages
== UINT_MAX
)
731 opts
->mmap_pages
= KiB(256) / page_size
;
732 if (!opts
->auxtrace_mmap_pages
) {
733 size_t sz
= round_up(max_sz
, page_size
) / page_size
;
735 opts
->auxtrace_mmap_pages
= roundup_pow_of_two(sz
);
737 if (max_sz
> opts
->auxtrace_mmap_pages
* (size_t)page_size
) {
738 pr_err("Sample size %zu must not be greater than AUX area tracing mmap size %zu\n",
740 opts
->auxtrace_mmap_pages
* (size_t)page_size
);
743 pr_debug2("Intel PT min. sample size: %zu max. sample size: %zu\n",
746 min_sz
<= psb_period
+ INTEL_PT_PSB_PERIOD_NEAR
)
747 ui__warning("Intel PT sample size (%zu) may be too small for PSB period (%zu)\n",
751 /* Set default sizes for full trace mode */
752 if (opts
->full_auxtrace
&& !opts
->auxtrace_mmap_pages
) {
754 opts
->auxtrace_mmap_pages
= MiB(4) / page_size
;
756 opts
->auxtrace_mmap_pages
= KiB(128) / page_size
;
757 if (opts
->mmap_pages
== UINT_MAX
)
758 opts
->mmap_pages
= KiB(256) / page_size
;
762 /* Validate auxtrace_mmap_pages */
763 if (opts
->auxtrace_mmap_pages
) {
764 size_t sz
= opts
->auxtrace_mmap_pages
* (size_t)page_size
;
767 if (opts
->auxtrace_snapshot_mode
|| opts
->auxtrace_sample_mode
)
772 if (sz
< min_sz
|| !is_power_of_2(sz
)) {
773 pr_err("Invalid mmap size for Intel Processor Trace: must be at least %zuKiB and a power of 2\n",
779 intel_pt_parse_terms(intel_pt_pmu
->name
, &intel_pt_pmu
->format
,
782 if (opts
->full_auxtrace
&& (intel_pt_evsel
->core
.attr
.config
& tsc_bit
))
783 have_timing_info
= true;
785 have_timing_info
= false;
788 * Per-cpu recording needs sched_switch events to distinguish different
791 if (have_timing_info
&& !perf_cpu_map__empty(cpus
) &&
792 !record_opts__no_switch_events(opts
)) {
793 if (perf_can_record_switch_events()) {
794 bool cpu_wide
= !target__none(&opts
->target
) &&
795 !target__has_task(&opts
->target
);
797 if (!cpu_wide
&& perf_can_record_cpu_wide()) {
798 struct evsel
*switch_evsel
;
800 err
= parse_events(evlist
, "dummy:u", NULL
);
804 switch_evsel
= evlist__last(evlist
);
806 switch_evsel
->core
.attr
.freq
= 0;
807 switch_evsel
->core
.attr
.sample_period
= 1;
808 switch_evsel
->core
.attr
.context_switch
= 1;
810 switch_evsel
->core
.system_wide
= true;
811 switch_evsel
->no_aux_samples
= true;
812 switch_evsel
->immediate
= true;
814 evsel__set_sample_bit(switch_evsel
, TID
);
815 evsel__set_sample_bit(switch_evsel
, TIME
);
816 evsel__set_sample_bit(switch_evsel
, CPU
);
817 evsel__reset_sample_bit(switch_evsel
, BRANCH_STACK
);
819 opts
->record_switch_events
= false;
820 ptr
->have_sched_switch
= 3;
822 opts
->record_switch_events
= true;
823 need_immediate
= true;
825 ptr
->have_sched_switch
= 3;
827 ptr
->have_sched_switch
= 2;
830 err
= intel_pt_track_switches(evlist
);
832 pr_debug2("Unable to select sched:sched_switch\n");
836 ptr
->have_sched_switch
= 1;
840 if (have_timing_info
&& !intel_pt_evsel
->core
.attr
.exclude_kernel
&&
841 perf_can_record_text_poke_events() && perf_can_record_cpu_wide())
842 opts
->text_poke
= true;
844 if (intel_pt_evsel
) {
846 * To obtain the auxtrace buffer file descriptor, the auxtrace
847 * event must come first.
849 evlist__to_front(evlist
, intel_pt_evsel
);
851 * In the case of per-cpu mmaps, we need the CPU on the
854 if (!perf_cpu_map__empty(cpus
))
855 evsel__set_sample_bit(intel_pt_evsel
, CPU
);
858 /* Add dummy event to keep tracking */
859 if (opts
->full_auxtrace
) {
860 struct evsel
*tracking_evsel
;
862 err
= parse_events(evlist
, "dummy:u", NULL
);
866 tracking_evsel
= evlist__last(evlist
);
868 evlist__set_tracking_event(evlist
, tracking_evsel
);
870 tracking_evsel
->core
.attr
.freq
= 0;
871 tracking_evsel
->core
.attr
.sample_period
= 1;
873 tracking_evsel
->no_aux_samples
= true;
875 tracking_evsel
->immediate
= true;
877 /* In per-cpu case, always need the time of mmap events etc */
878 if (!perf_cpu_map__empty(cpus
)) {
879 evsel__set_sample_bit(tracking_evsel
, TIME
);
880 /* And the CPU for switch events */
881 evsel__set_sample_bit(tracking_evsel
, CPU
);
883 evsel__reset_sample_bit(tracking_evsel
, BRANCH_STACK
);
887 * Warn the user when we do not have enough information to decode i.e.
888 * per-cpu with no sched_switch (except workload-only).
890 if (!ptr
->have_sched_switch
&& !perf_cpu_map__empty(cpus
) &&
891 !target__none(&opts
->target
) &&
892 !intel_pt_evsel
->core
.attr
.exclude_user
)
893 ui__warning("Intel Processor Trace decoding will not be possible except for kernel tracing!\n");
898 static int intel_pt_snapshot_start(struct auxtrace_record
*itr
)
900 struct intel_pt_recording
*ptr
=
901 container_of(itr
, struct intel_pt_recording
, itr
);
904 evlist__for_each_entry(ptr
->evlist
, evsel
) {
905 if (evsel
->core
.attr
.type
== ptr
->intel_pt_pmu
->type
)
906 return evsel__disable(evsel
);
911 static int intel_pt_snapshot_finish(struct auxtrace_record
*itr
)
913 struct intel_pt_recording
*ptr
=
914 container_of(itr
, struct intel_pt_recording
, itr
);
917 evlist__for_each_entry(ptr
->evlist
, evsel
) {
918 if (evsel
->core
.attr
.type
== ptr
->intel_pt_pmu
->type
)
919 return evsel__enable(evsel
);
924 static int intel_pt_alloc_snapshot_refs(struct intel_pt_recording
*ptr
, int idx
)
926 const size_t sz
= sizeof(struct intel_pt_snapshot_ref
);
927 int cnt
= ptr
->snapshot_ref_cnt
, new_cnt
= cnt
* 2;
928 struct intel_pt_snapshot_ref
*refs
;
933 while (new_cnt
<= idx
)
936 refs
= calloc(new_cnt
, sz
);
940 memcpy(refs
, ptr
->snapshot_refs
, cnt
* sz
);
942 ptr
->snapshot_refs
= refs
;
943 ptr
->snapshot_ref_cnt
= new_cnt
;
948 static void intel_pt_free_snapshot_refs(struct intel_pt_recording
*ptr
)
952 for (i
= 0; i
< ptr
->snapshot_ref_cnt
; i
++)
953 zfree(&ptr
->snapshot_refs
[i
].ref_buf
);
954 zfree(&ptr
->snapshot_refs
);
957 static void intel_pt_recording_free(struct auxtrace_record
*itr
)
959 struct intel_pt_recording
*ptr
=
960 container_of(itr
, struct intel_pt_recording
, itr
);
962 intel_pt_free_snapshot_refs(ptr
);
966 static int intel_pt_alloc_snapshot_ref(struct intel_pt_recording
*ptr
, int idx
,
967 size_t snapshot_buf_size
)
969 size_t ref_buf_size
= ptr
->snapshot_ref_buf_size
;
972 ref_buf
= zalloc(ref_buf_size
);
976 ptr
->snapshot_refs
[idx
].ref_buf
= ref_buf
;
977 ptr
->snapshot_refs
[idx
].ref_offset
= snapshot_buf_size
- ref_buf_size
;
982 static size_t intel_pt_snapshot_ref_buf_size(struct intel_pt_recording
*ptr
,
983 size_t snapshot_buf_size
)
985 const size_t max_size
= 256 * 1024;
986 size_t buf_size
= 0, psb_period
;
988 if (ptr
->snapshot_size
<= 64 * 1024)
991 psb_period
= intel_pt_psb_period(ptr
->intel_pt_pmu
, ptr
->evlist
);
993 buf_size
= psb_period
* 2;
995 if (!buf_size
|| buf_size
> max_size
)
998 if (buf_size
>= snapshot_buf_size
)
1001 if (buf_size
>= ptr
->snapshot_size
/ 2)
1007 static int intel_pt_snapshot_init(struct intel_pt_recording
*ptr
,
1008 size_t snapshot_buf_size
)
1010 if (ptr
->snapshot_init_done
)
1013 ptr
->snapshot_init_done
= true;
1015 ptr
->snapshot_ref_buf_size
= intel_pt_snapshot_ref_buf_size(ptr
,
1022 * intel_pt_compare_buffers - compare bytes in a buffer to a circular buffer.
1023 * @buf1: first buffer
1024 * @compare_size: number of bytes to compare
1025 * @buf2: second buffer (a circular buffer)
1026 * @offs2: offset in second buffer
1027 * @buf2_size: size of second buffer
1029 * The comparison allows for the possibility that the bytes to compare in the
1030 * circular buffer are not contiguous. It is assumed that @compare_size <=
1031 * @buf2_size. This function returns %false if the bytes are identical, %true
1034 static bool intel_pt_compare_buffers(void *buf1
, size_t compare_size
,
1035 void *buf2
, size_t offs2
, size_t buf2_size
)
1037 size_t end2
= offs2
+ compare_size
, part_size
;
1039 if (end2
<= buf2_size
)
1040 return memcmp(buf1
, buf2
+ offs2
, compare_size
);
1042 part_size
= end2
- buf2_size
;
1043 if (memcmp(buf1
, buf2
+ offs2
, part_size
))
1046 compare_size
-= part_size
;
1048 return memcmp(buf1
+ part_size
, buf2
, compare_size
);
1051 static bool intel_pt_compare_ref(void *ref_buf
, size_t ref_offset
,
1052 size_t ref_size
, size_t buf_size
,
1053 void *data
, size_t head
)
1055 size_t ref_end
= ref_offset
+ ref_size
;
1057 if (ref_end
> buf_size
) {
1058 if (head
> ref_offset
|| head
< ref_end
- buf_size
)
1060 } else if (head
> ref_offset
&& head
< ref_end
) {
1064 return intel_pt_compare_buffers(ref_buf
, ref_size
, data
, ref_offset
,
1068 static void intel_pt_copy_ref(void *ref_buf
, size_t ref_size
, size_t buf_size
,
1069 void *data
, size_t head
)
1071 if (head
>= ref_size
) {
1072 memcpy(ref_buf
, data
+ head
- ref_size
, ref_size
);
1074 memcpy(ref_buf
, data
, head
);
1076 memcpy(ref_buf
+ head
, data
+ buf_size
- ref_size
, ref_size
);
1080 static bool intel_pt_wrapped(struct intel_pt_recording
*ptr
, int idx
,
1081 struct auxtrace_mmap
*mm
, unsigned char *data
,
1084 struct intel_pt_snapshot_ref
*ref
= &ptr
->snapshot_refs
[idx
];
1087 wrapped
= intel_pt_compare_ref(ref
->ref_buf
, ref
->ref_offset
,
1088 ptr
->snapshot_ref_buf_size
, mm
->len
,
1091 intel_pt_copy_ref(ref
->ref_buf
, ptr
->snapshot_ref_buf_size
, mm
->len
,
1097 static bool intel_pt_first_wrap(u64
*data
, size_t buf_size
)
1106 for (i
= a
; i
< b
; i
++) {
1114 static int intel_pt_find_snapshot(struct auxtrace_record
*itr
, int idx
,
1115 struct auxtrace_mmap
*mm
, unsigned char *data
,
1116 u64
*head
, u64
*old
)
1118 struct intel_pt_recording
*ptr
=
1119 container_of(itr
, struct intel_pt_recording
, itr
);
1123 pr_debug3("%s: mmap index %d old head %zu new head %zu\n",
1124 __func__
, idx
, (size_t)*old
, (size_t)*head
);
1126 err
= intel_pt_snapshot_init(ptr
, mm
->len
);
1130 if (idx
>= ptr
->snapshot_ref_cnt
) {
1131 err
= intel_pt_alloc_snapshot_refs(ptr
, idx
);
1136 if (ptr
->snapshot_ref_buf_size
) {
1137 if (!ptr
->snapshot_refs
[idx
].ref_buf
) {
1138 err
= intel_pt_alloc_snapshot_ref(ptr
, idx
, mm
->len
);
1142 wrapped
= intel_pt_wrapped(ptr
, idx
, mm
, data
, *head
);
1144 wrapped
= ptr
->snapshot_refs
[idx
].wrapped
;
1145 if (!wrapped
&& intel_pt_first_wrap((u64
*)data
, mm
->len
)) {
1146 ptr
->snapshot_refs
[idx
].wrapped
= true;
1152 * In full trace mode 'head' continually increases. However in snapshot
1153 * mode 'head' is an offset within the buffer. Here 'old' and 'head'
1154 * are adjusted to match the full trace case which expects that 'old' is
1155 * always less than 'head'.
1169 pr_debug3("%s: wrap-around %sdetected, adjusted old head %zu adjusted new head %zu\n",
1170 __func__
, wrapped
? "" : "not ", (size_t)*old
, (size_t)*head
);
1175 pr_err("%s: failed, error %d\n", __func__
, err
);
1179 static u64
intel_pt_reference(struct auxtrace_record
*itr __maybe_unused
)
1184 struct auxtrace_record
*intel_pt_recording_init(int *err
)
1186 struct perf_pmu
*intel_pt_pmu
= perf_pmu__find(INTEL_PT_PMU_NAME
);
1187 struct intel_pt_recording
*ptr
;
1192 if (setenv("JITDUMP_USE_ARCH_TIMESTAMP", "1", 1)) {
1197 ptr
= zalloc(sizeof(struct intel_pt_recording
));
1203 ptr
->intel_pt_pmu
= intel_pt_pmu
;
1204 ptr
->itr
.pmu
= intel_pt_pmu
;
1205 ptr
->itr
.recording_options
= intel_pt_recording_options
;
1206 ptr
->itr
.info_priv_size
= intel_pt_info_priv_size
;
1207 ptr
->itr
.info_fill
= intel_pt_info_fill
;
1208 ptr
->itr
.free
= intel_pt_recording_free
;
1209 ptr
->itr
.snapshot_start
= intel_pt_snapshot_start
;
1210 ptr
->itr
.snapshot_finish
= intel_pt_snapshot_finish
;
1211 ptr
->itr
.find_snapshot
= intel_pt_find_snapshot
;
1212 ptr
->itr
.parse_snapshot_options
= intel_pt_parse_snapshot_options
;
1213 ptr
->itr
.reference
= intel_pt_reference
;
1214 ptr
->itr
.read_finish
= auxtrace_record__read_finish
;
1216 * Decoding starts at a PSB packet. Minimum PSB period is 2K so 4K
1217 * should give at least 1 PSB per sample.
1219 ptr
->itr
.default_aux_sample_size
= 4096;