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 "../../perf.h"
17 #include "../../util/session.h"
18 #include "../../util/event.h"
19 #include "../../util/evlist.h"
20 #include "../../util/evsel.h"
21 #include "../../util/cpumap.h"
22 #include <subcmd/parse-options.h>
23 #include "../../util/parse-events.h"
24 #include "../../util/pmu.h"
25 #include "../../util/debug.h"
26 #include "../../util/auxtrace.h"
27 #include "../../util/tsc.h"
28 #include "../../util/intel-pt.h"
30 #define KiB(x) ((x) * 1024)
31 #define MiB(x) ((x) * 1024 * 1024)
32 #define KiB_MASK(x) (KiB(x) - 1)
33 #define MiB_MASK(x) (MiB(x) - 1)
35 #define INTEL_PT_PSB_PERIOD_NEAR 256
37 struct intel_pt_snapshot_ref {
43 struct intel_pt_recording {
44 struct auxtrace_record itr;
45 struct perf_pmu *intel_pt_pmu;
46 int have_sched_switch;
47 struct evlist *evlist;
49 bool snapshot_init_done;
51 size_t snapshot_ref_buf_size;
53 struct intel_pt_snapshot_ref *snapshot_refs;
57 static int intel_pt_parse_terms_with_default(struct list_head *formats,
61 struct list_head *terms;
62 struct perf_event_attr attr = { .size = 0, };
65 terms = malloc(sizeof(struct list_head));
69 INIT_LIST_HEAD(terms);
71 err = parse_events_terms(terms, str);
75 attr.config = *config;
76 err = perf_pmu__config_terms(formats, &attr, terms, true, NULL);
80 *config = attr.config;
82 parse_events_terms__delete(terms);
86 static int intel_pt_parse_terms(struct list_head *formats, const char *str,
90 return intel_pt_parse_terms_with_default(formats, str, config);
93 static u64 intel_pt_masked_bits(u64 mask, u64 bits)
95 const u64 top_bit = 1ULL << 63;
99 for (i = 0; i < 64; i++) {
100 if (mask & top_bit) {
112 static int intel_pt_read_config(struct perf_pmu *intel_pt_pmu, const char *str,
113 struct evlist *evlist, u64 *res)
120 mask = perf_pmu__format_bits(&intel_pt_pmu->format, str);
124 evlist__for_each_entry(evlist, evsel) {
125 if (evsel->core.attr.type == intel_pt_pmu->type) {
126 *res = intel_pt_masked_bits(mask, evsel->core.attr.config);
134 static size_t intel_pt_psb_period(struct perf_pmu *intel_pt_pmu,
135 struct evlist *evlist)
138 int err, topa_multiple_entries;
141 if (perf_pmu__scan_file(intel_pt_pmu, "caps/topa_multiple_entries",
142 "%d", &topa_multiple_entries) != 1)
143 topa_multiple_entries = 0;
146 * Use caps/topa_multiple_entries to indicate early hardware that had
147 * extra frequent PSBs.
149 if (!topa_multiple_entries) {
154 err = intel_pt_read_config(intel_pt_pmu, "psb_period", evlist, &val);
158 psb_period = 1 << (val + 11);
160 pr_debug2("%s psb_period %zu\n", intel_pt_pmu->name, psb_period);
164 static int intel_pt_pick_bit(int bits, int target)
168 for (pos = 0; bits; bits >>= 1, pos++) {
170 if (pos <= target || pick < 0)
180 static u64 intel_pt_default_config(struct perf_pmu *intel_pt_pmu)
183 int mtc, mtc_periods = 0, mtc_period;
184 int psb_cyc, psb_periods, psb_period;
189 pos += scnprintf(buf + pos, sizeof(buf) - pos, "tsc");
191 if (perf_pmu__scan_file(intel_pt_pmu, "caps/mtc", "%d",
196 if (perf_pmu__scan_file(intel_pt_pmu, "caps/mtc_periods", "%x",
200 mtc_period = intel_pt_pick_bit(mtc_periods, 3);
201 pos += scnprintf(buf + pos, sizeof(buf) - pos,
202 ",mtc,mtc_period=%d", mtc_period);
206 if (perf_pmu__scan_file(intel_pt_pmu, "caps/psb_cyc", "%d",
210 if (psb_cyc && mtc_periods) {
211 if (perf_pmu__scan_file(intel_pt_pmu, "caps/psb_periods", "%x",
215 psb_period = intel_pt_pick_bit(psb_periods, 3);
216 pos += scnprintf(buf + pos, sizeof(buf) - pos,
217 ",psb_period=%d", psb_period);
221 if (perf_pmu__scan_file(intel_pt_pmu, "format/pt", "%c", &c) == 1 &&
222 perf_pmu__scan_file(intel_pt_pmu, "format/branch", "%c", &c) == 1)
223 pos += scnprintf(buf + pos, sizeof(buf) - pos, ",pt,branch");
225 pr_debug2("%s default config: %s\n", intel_pt_pmu->name, buf);
227 intel_pt_parse_terms(&intel_pt_pmu->format, buf, &config);
232 static int intel_pt_parse_snapshot_options(struct auxtrace_record *itr,
233 struct record_opts *opts,
236 struct intel_pt_recording *ptr =
237 container_of(itr, struct intel_pt_recording, itr);
238 unsigned long long snapshot_size = 0;
242 snapshot_size = strtoull(str, &endptr, 0);
243 if (*endptr || snapshot_size > SIZE_MAX)
247 opts->auxtrace_snapshot_mode = true;
248 opts->auxtrace_snapshot_size = snapshot_size;
250 ptr->snapshot_size = snapshot_size;
255 struct perf_event_attr *
256 intel_pt_pmu_default_config(struct perf_pmu *intel_pt_pmu)
258 struct perf_event_attr *attr;
260 attr = zalloc(sizeof(struct perf_event_attr));
264 attr->config = intel_pt_default_config(intel_pt_pmu);
266 intel_pt_pmu->selectable = true;
271 static const char *intel_pt_find_filter(struct evlist *evlist,
272 struct perf_pmu *intel_pt_pmu)
276 evlist__for_each_entry(evlist, evsel) {
277 if (evsel->core.attr.type == intel_pt_pmu->type)
278 return evsel->filter;
284 static size_t intel_pt_filter_bytes(const char *filter)
286 size_t len = filter ? strlen(filter) : 0;
288 return len ? roundup(len + 1, 8) : 0;
292 intel_pt_info_priv_size(struct auxtrace_record *itr, struct evlist *evlist)
294 struct intel_pt_recording *ptr =
295 container_of(itr, struct intel_pt_recording, itr);
296 const char *filter = intel_pt_find_filter(evlist, ptr->intel_pt_pmu);
298 ptr->priv_size = (INTEL_PT_AUXTRACE_PRIV_MAX * sizeof(u64)) +
299 intel_pt_filter_bytes(filter);
301 return ptr->priv_size;
304 static void intel_pt_tsc_ctc_ratio(u32 *n, u32 *d)
306 unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
308 __get_cpuid(0x15, &eax, &ebx, &ecx, &edx);
313 static int intel_pt_info_fill(struct auxtrace_record *itr,
314 struct perf_session *session,
315 struct auxtrace_info_event *auxtrace_info,
318 struct intel_pt_recording *ptr =
319 container_of(itr, struct intel_pt_recording, itr);
320 struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;
321 struct perf_event_mmap_page *pc;
322 struct perf_tsc_conversion tc = { .time_mult = 0, };
323 bool cap_user_time_zero = false, per_cpu_mmaps;
324 u64 tsc_bit, mtc_bit, mtc_freq_bits, cyc_bit, noretcomp_bit;
325 u32 tsc_ctc_ratio_n, tsc_ctc_ratio_d;
326 unsigned long max_non_turbo_ratio;
327 size_t filter_str_len;
332 if (priv_size != ptr->priv_size)
335 intel_pt_parse_terms(&intel_pt_pmu->format, "tsc", &tsc_bit);
336 intel_pt_parse_terms(&intel_pt_pmu->format, "noretcomp",
338 intel_pt_parse_terms(&intel_pt_pmu->format, "mtc", &mtc_bit);
339 mtc_freq_bits = perf_pmu__format_bits(&intel_pt_pmu->format,
341 intel_pt_parse_terms(&intel_pt_pmu->format, "cyc", &cyc_bit);
343 intel_pt_tsc_ctc_ratio(&tsc_ctc_ratio_n, &tsc_ctc_ratio_d);
345 if (perf_pmu__scan_file(intel_pt_pmu, "max_nonturbo_ratio",
346 "%lu", &max_non_turbo_ratio) != 1)
347 max_non_turbo_ratio = 0;
349 filter = intel_pt_find_filter(session->evlist, ptr->intel_pt_pmu);
350 filter_str_len = filter ? strlen(filter) : 0;
352 if (!session->evlist->nr_mmaps)
355 pc = session->evlist->mmap[0].base;
357 err = perf_read_tsc_conversion(pc, &tc);
359 if (err != -EOPNOTSUPP)
362 cap_user_time_zero = tc.time_mult != 0;
364 if (!cap_user_time_zero)
365 ui__warning("Intel Processor Trace: TSC not available\n");
368 per_cpu_mmaps = !cpu_map__empty(session->evlist->core.cpus);
370 auxtrace_info->type = PERF_AUXTRACE_INTEL_PT;
371 auxtrace_info->priv[INTEL_PT_PMU_TYPE] = intel_pt_pmu->type;
372 auxtrace_info->priv[INTEL_PT_TIME_SHIFT] = tc.time_shift;
373 auxtrace_info->priv[INTEL_PT_TIME_MULT] = tc.time_mult;
374 auxtrace_info->priv[INTEL_PT_TIME_ZERO] = tc.time_zero;
375 auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO] = cap_user_time_zero;
376 auxtrace_info->priv[INTEL_PT_TSC_BIT] = tsc_bit;
377 auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT] = noretcomp_bit;
378 auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH] = ptr->have_sched_switch;
379 auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE] = ptr->snapshot_mode;
380 auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS] = per_cpu_mmaps;
381 auxtrace_info->priv[INTEL_PT_MTC_BIT] = mtc_bit;
382 auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS] = mtc_freq_bits;
383 auxtrace_info->priv[INTEL_PT_TSC_CTC_N] = tsc_ctc_ratio_n;
384 auxtrace_info->priv[INTEL_PT_TSC_CTC_D] = tsc_ctc_ratio_d;
385 auxtrace_info->priv[INTEL_PT_CYC_BIT] = cyc_bit;
386 auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO] = max_non_turbo_ratio;
387 auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] = filter_str_len;
389 info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
391 if (filter_str_len) {
392 size_t len = intel_pt_filter_bytes(filter);
394 strncpy((char *)info, filter, len);
401 static int intel_pt_track_switches(struct evlist *evlist)
403 const char *sched_switch = "sched:sched_switch";
407 if (!perf_evlist__can_select_event(evlist, sched_switch))
410 err = parse_events(evlist, sched_switch, NULL);
412 pr_debug2("%s: failed to parse %s, error %d\n",
413 __func__, sched_switch, err);
417 evsel = perf_evlist__last(evlist);
419 perf_evsel__set_sample_bit(evsel, CPU);
420 perf_evsel__set_sample_bit(evsel, TIME);
422 evsel->system_wide = true;
423 evsel->no_aux_samples = true;
424 evsel->immediate = true;
429 static void intel_pt_valid_str(char *str, size_t len, u64 valid)
431 unsigned int val, last = 0, state = 1;
436 for (val = 0; val <= 64; val++, valid >>= 1) {
441 p += scnprintf(str + p, len - p, ",");
444 p += scnprintf(str + p, len - p, "%u", val);
459 p += scnprintf(str + p, len - p, ",%u", last);
463 p += scnprintf(str + p, len - p, "-%u", last);
475 static int intel_pt_val_config_term(struct perf_pmu *intel_pt_pmu,
476 const char *caps, const char *name,
477 const char *supported, u64 config)
481 unsigned long long valid;
485 if (perf_pmu__scan_file(intel_pt_pmu, caps, "%llx", &valid) != 1)
489 perf_pmu__scan_file(intel_pt_pmu, supported, "%d", &ok) == 1 && !ok)
494 bits = perf_pmu__format_bits(&intel_pt_pmu->format, name);
498 for (shift = 0; bits && !(bits & 1); shift++)
506 if (valid & (1 << config))
509 intel_pt_valid_str(valid_str, sizeof(valid_str), valid);
510 pr_err("Invalid %s for %s. Valid values are: %s\n",
511 name, INTEL_PT_PMU_NAME, valid_str);
515 static int intel_pt_validate_config(struct perf_pmu *intel_pt_pmu,
525 * If supported, force pass-through config term (pt=1) even if user
526 * sets pt=0, which avoids senseless kernel errors.
528 if (perf_pmu__scan_file(intel_pt_pmu, "format/pt", "%c", &c) == 1 &&
529 !(evsel->core.attr.config & 1)) {
530 pr_warning("pt=0 doesn't make sense, forcing pt=1\n");
531 evsel->core.attr.config |= 1;
534 err = intel_pt_val_config_term(intel_pt_pmu, "caps/cycle_thresholds",
535 "cyc_thresh", "caps/psb_cyc",
536 evsel->core.attr.config);
540 err = intel_pt_val_config_term(intel_pt_pmu, "caps/mtc_periods",
541 "mtc_period", "caps/mtc",
542 evsel->core.attr.config);
546 return intel_pt_val_config_term(intel_pt_pmu, "caps/psb_periods",
547 "psb_period", "caps/psb_cyc",
548 evsel->core.attr.config);
552 * Currently, there is not enough information to disambiguate different PEBS
553 * events, so only allow one.
555 static bool intel_pt_too_many_aux_output(struct evlist *evlist)
558 int aux_output_cnt = 0;
560 evlist__for_each_entry(evlist, evsel)
561 aux_output_cnt += !!evsel->core.attr.aux_output;
563 if (aux_output_cnt > 1) {
564 pr_err(INTEL_PT_PMU_NAME " supports at most one event with aux-output\n");
571 static int intel_pt_recording_options(struct auxtrace_record *itr,
572 struct evlist *evlist,
573 struct record_opts *opts)
575 struct intel_pt_recording *ptr =
576 container_of(itr, struct intel_pt_recording, itr);
577 struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;
578 bool have_timing_info, need_immediate = false;
579 struct evsel *evsel, *intel_pt_evsel = NULL;
580 const struct perf_cpu_map *cpus = evlist->core.cpus;
581 bool privileged = geteuid() == 0 || perf_event_paranoid() < 0;
585 ptr->evlist = evlist;
586 ptr->snapshot_mode = opts->auxtrace_snapshot_mode;
588 evlist__for_each_entry(evlist, evsel) {
589 if (evsel->core.attr.type == intel_pt_pmu->type) {
590 if (intel_pt_evsel) {
591 pr_err("There may be only one " INTEL_PT_PMU_NAME " event\n");
594 evsel->core.attr.freq = 0;
595 evsel->core.attr.sample_period = 1;
596 intel_pt_evsel = evsel;
597 opts->full_auxtrace = true;
601 if (opts->auxtrace_snapshot_mode && !opts->full_auxtrace) {
602 pr_err("Snapshot mode (-S option) requires " INTEL_PT_PMU_NAME " PMU event (-e " INTEL_PT_PMU_NAME ")\n");
606 if (opts->use_clockid) {
607 pr_err("Cannot use clockid (-k option) with " INTEL_PT_PMU_NAME "\n");
611 if (intel_pt_too_many_aux_output(evlist))
614 if (!opts->full_auxtrace)
617 err = intel_pt_validate_config(intel_pt_pmu, intel_pt_evsel);
621 /* Set default sizes for snapshot mode */
622 if (opts->auxtrace_snapshot_mode) {
623 size_t psb_period = intel_pt_psb_period(intel_pt_pmu, evlist);
625 if (!opts->auxtrace_snapshot_size && !opts->auxtrace_mmap_pages) {
627 opts->auxtrace_mmap_pages = MiB(4) / page_size;
629 opts->auxtrace_mmap_pages = KiB(128) / page_size;
630 if (opts->mmap_pages == UINT_MAX)
631 opts->mmap_pages = KiB(256) / page_size;
633 } else if (!opts->auxtrace_mmap_pages && !privileged &&
634 opts->mmap_pages == UINT_MAX) {
635 opts->mmap_pages = KiB(256) / page_size;
637 if (!opts->auxtrace_snapshot_size)
638 opts->auxtrace_snapshot_size =
639 opts->auxtrace_mmap_pages * (size_t)page_size;
640 if (!opts->auxtrace_mmap_pages) {
641 size_t sz = opts->auxtrace_snapshot_size;
643 sz = round_up(sz, page_size) / page_size;
644 opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
646 if (opts->auxtrace_snapshot_size >
647 opts->auxtrace_mmap_pages * (size_t)page_size) {
648 pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
649 opts->auxtrace_snapshot_size,
650 opts->auxtrace_mmap_pages * (size_t)page_size);
653 if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages) {
654 pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
657 pr_debug2("Intel PT snapshot size: %zu\n",
658 opts->auxtrace_snapshot_size);
660 opts->auxtrace_snapshot_size <= psb_period +
661 INTEL_PT_PSB_PERIOD_NEAR)
662 ui__warning("Intel PT snapshot size (%zu) may be too small for PSB period (%zu)\n",
663 opts->auxtrace_snapshot_size, psb_period);
666 /* Set default sizes for full trace mode */
667 if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
669 opts->auxtrace_mmap_pages = MiB(4) / page_size;
671 opts->auxtrace_mmap_pages = KiB(128) / page_size;
672 if (opts->mmap_pages == UINT_MAX)
673 opts->mmap_pages = KiB(256) / page_size;
677 /* Validate auxtrace_mmap_pages */
678 if (opts->auxtrace_mmap_pages) {
679 size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
682 if (opts->auxtrace_snapshot_mode)
687 if (sz < min_sz || !is_power_of_2(sz)) {
688 pr_err("Invalid mmap size for Intel Processor Trace: must be at least %zuKiB and a power of 2\n",
694 intel_pt_parse_terms(&intel_pt_pmu->format, "tsc", &tsc_bit);
696 if (opts->full_auxtrace && (intel_pt_evsel->core.attr.config & tsc_bit))
697 have_timing_info = true;
699 have_timing_info = false;
702 * Per-cpu recording needs sched_switch events to distinguish different
705 if (have_timing_info && !cpu_map__empty(cpus)) {
706 if (perf_can_record_switch_events()) {
707 bool cpu_wide = !target__none(&opts->target) &&
708 !target__has_task(&opts->target);
710 if (!cpu_wide && perf_can_record_cpu_wide()) {
711 struct evsel *switch_evsel;
713 err = parse_events(evlist, "dummy:u", NULL);
717 switch_evsel = perf_evlist__last(evlist);
719 switch_evsel->core.attr.freq = 0;
720 switch_evsel->core.attr.sample_period = 1;
721 switch_evsel->core.attr.context_switch = 1;
723 switch_evsel->system_wide = true;
724 switch_evsel->no_aux_samples = true;
725 switch_evsel->immediate = true;
727 perf_evsel__set_sample_bit(switch_evsel, TID);
728 perf_evsel__set_sample_bit(switch_evsel, TIME);
729 perf_evsel__set_sample_bit(switch_evsel, CPU);
730 perf_evsel__reset_sample_bit(switch_evsel, BRANCH_STACK);
732 opts->record_switch_events = false;
733 ptr->have_sched_switch = 3;
735 opts->record_switch_events = true;
736 need_immediate = true;
738 ptr->have_sched_switch = 3;
740 ptr->have_sched_switch = 2;
743 err = intel_pt_track_switches(evlist);
745 pr_debug2("Unable to select sched:sched_switch\n");
749 ptr->have_sched_switch = 1;
753 if (intel_pt_evsel) {
755 * To obtain the auxtrace buffer file descriptor, the auxtrace
756 * event must come first.
758 perf_evlist__to_front(evlist, intel_pt_evsel);
760 * In the case of per-cpu mmaps, we need the CPU on the
763 if (!cpu_map__empty(cpus))
764 perf_evsel__set_sample_bit(intel_pt_evsel, CPU);
767 /* Add dummy event to keep tracking */
768 if (opts->full_auxtrace) {
769 struct evsel *tracking_evsel;
771 err = parse_events(evlist, "dummy:u", NULL);
775 tracking_evsel = perf_evlist__last(evlist);
777 perf_evlist__set_tracking_event(evlist, tracking_evsel);
779 tracking_evsel->core.attr.freq = 0;
780 tracking_evsel->core.attr.sample_period = 1;
782 tracking_evsel->no_aux_samples = true;
784 tracking_evsel->immediate = true;
786 /* In per-cpu case, always need the time of mmap events etc */
787 if (!cpu_map__empty(cpus)) {
788 perf_evsel__set_sample_bit(tracking_evsel, TIME);
789 /* And the CPU for switch events */
790 perf_evsel__set_sample_bit(tracking_evsel, CPU);
792 perf_evsel__reset_sample_bit(tracking_evsel, BRANCH_STACK);
796 * Warn the user when we do not have enough information to decode i.e.
797 * per-cpu with no sched_switch (except workload-only).
799 if (!ptr->have_sched_switch && !cpu_map__empty(cpus) &&
800 !target__none(&opts->target))
801 ui__warning("Intel Processor Trace decoding will not be possible except for kernel tracing!\n");
806 static int intel_pt_snapshot_start(struct auxtrace_record *itr)
808 struct intel_pt_recording *ptr =
809 container_of(itr, struct intel_pt_recording, itr);
812 evlist__for_each_entry(ptr->evlist, evsel) {
813 if (evsel->core.attr.type == ptr->intel_pt_pmu->type)
814 return evsel__disable(evsel);
819 static int intel_pt_snapshot_finish(struct auxtrace_record *itr)
821 struct intel_pt_recording *ptr =
822 container_of(itr, struct intel_pt_recording, itr);
825 evlist__for_each_entry(ptr->evlist, evsel) {
826 if (evsel->core.attr.type == ptr->intel_pt_pmu->type)
827 return evsel__enable(evsel);
832 static int intel_pt_alloc_snapshot_refs(struct intel_pt_recording *ptr, int idx)
834 const size_t sz = sizeof(struct intel_pt_snapshot_ref);
835 int cnt = ptr->snapshot_ref_cnt, new_cnt = cnt * 2;
836 struct intel_pt_snapshot_ref *refs;
841 while (new_cnt <= idx)
844 refs = calloc(new_cnt, sz);
848 memcpy(refs, ptr->snapshot_refs, cnt * sz);
850 ptr->snapshot_refs = refs;
851 ptr->snapshot_ref_cnt = new_cnt;
856 static void intel_pt_free_snapshot_refs(struct intel_pt_recording *ptr)
860 for (i = 0; i < ptr->snapshot_ref_cnt; i++)
861 zfree(&ptr->snapshot_refs[i].ref_buf);
862 zfree(&ptr->snapshot_refs);
865 static void intel_pt_recording_free(struct auxtrace_record *itr)
867 struct intel_pt_recording *ptr =
868 container_of(itr, struct intel_pt_recording, itr);
870 intel_pt_free_snapshot_refs(ptr);
874 static int intel_pt_alloc_snapshot_ref(struct intel_pt_recording *ptr, int idx,
875 size_t snapshot_buf_size)
877 size_t ref_buf_size = ptr->snapshot_ref_buf_size;
880 ref_buf = zalloc(ref_buf_size);
884 ptr->snapshot_refs[idx].ref_buf = ref_buf;
885 ptr->snapshot_refs[idx].ref_offset = snapshot_buf_size - ref_buf_size;
890 static size_t intel_pt_snapshot_ref_buf_size(struct intel_pt_recording *ptr,
891 size_t snapshot_buf_size)
893 const size_t max_size = 256 * 1024;
894 size_t buf_size = 0, psb_period;
896 if (ptr->snapshot_size <= 64 * 1024)
899 psb_period = intel_pt_psb_period(ptr->intel_pt_pmu, ptr->evlist);
901 buf_size = psb_period * 2;
903 if (!buf_size || buf_size > max_size)
906 if (buf_size >= snapshot_buf_size)
909 if (buf_size >= ptr->snapshot_size / 2)
915 static int intel_pt_snapshot_init(struct intel_pt_recording *ptr,
916 size_t snapshot_buf_size)
918 if (ptr->snapshot_init_done)
921 ptr->snapshot_init_done = true;
923 ptr->snapshot_ref_buf_size = intel_pt_snapshot_ref_buf_size(ptr,
930 * intel_pt_compare_buffers - compare bytes in a buffer to a circular buffer.
931 * @buf1: first buffer
932 * @compare_size: number of bytes to compare
933 * @buf2: second buffer (a circular buffer)
934 * @offs2: offset in second buffer
935 * @buf2_size: size of second buffer
937 * The comparison allows for the possibility that the bytes to compare in the
938 * circular buffer are not contiguous. It is assumed that @compare_size <=
939 * @buf2_size. This function returns %false if the bytes are identical, %true
942 static bool intel_pt_compare_buffers(void *buf1, size_t compare_size,
943 void *buf2, size_t offs2, size_t buf2_size)
945 size_t end2 = offs2 + compare_size, part_size;
947 if (end2 <= buf2_size)
948 return memcmp(buf1, buf2 + offs2, compare_size);
950 part_size = end2 - buf2_size;
951 if (memcmp(buf1, buf2 + offs2, part_size))
954 compare_size -= part_size;
956 return memcmp(buf1 + part_size, buf2, compare_size);
959 static bool intel_pt_compare_ref(void *ref_buf, size_t ref_offset,
960 size_t ref_size, size_t buf_size,
961 void *data, size_t head)
963 size_t ref_end = ref_offset + ref_size;
965 if (ref_end > buf_size) {
966 if (head > ref_offset || head < ref_end - buf_size)
968 } else if (head > ref_offset && head < ref_end) {
972 return intel_pt_compare_buffers(ref_buf, ref_size, data, ref_offset,
976 static void intel_pt_copy_ref(void *ref_buf, size_t ref_size, size_t buf_size,
977 void *data, size_t head)
979 if (head >= ref_size) {
980 memcpy(ref_buf, data + head - ref_size, ref_size);
982 memcpy(ref_buf, data, head);
984 memcpy(ref_buf + head, data + buf_size - ref_size, ref_size);
988 static bool intel_pt_wrapped(struct intel_pt_recording *ptr, int idx,
989 struct auxtrace_mmap *mm, unsigned char *data,
992 struct intel_pt_snapshot_ref *ref = &ptr->snapshot_refs[idx];
995 wrapped = intel_pt_compare_ref(ref->ref_buf, ref->ref_offset,
996 ptr->snapshot_ref_buf_size, mm->len,
999 intel_pt_copy_ref(ref->ref_buf, ptr->snapshot_ref_buf_size, mm->len,
1005 static bool intel_pt_first_wrap(u64 *data, size_t buf_size)
1014 for (i = a; i < b; i++) {
1022 static int intel_pt_find_snapshot(struct auxtrace_record *itr, int idx,
1023 struct auxtrace_mmap *mm, unsigned char *data,
1024 u64 *head, u64 *old)
1026 struct intel_pt_recording *ptr =
1027 container_of(itr, struct intel_pt_recording, itr);
1031 pr_debug3("%s: mmap index %d old head %zu new head %zu\n",
1032 __func__, idx, (size_t)*old, (size_t)*head);
1034 err = intel_pt_snapshot_init(ptr, mm->len);
1038 if (idx >= ptr->snapshot_ref_cnt) {
1039 err = intel_pt_alloc_snapshot_refs(ptr, idx);
1044 if (ptr->snapshot_ref_buf_size) {
1045 if (!ptr->snapshot_refs[idx].ref_buf) {
1046 err = intel_pt_alloc_snapshot_ref(ptr, idx, mm->len);
1050 wrapped = intel_pt_wrapped(ptr, idx, mm, data, *head);
1052 wrapped = ptr->snapshot_refs[idx].wrapped;
1053 if (!wrapped && intel_pt_first_wrap((u64 *)data, mm->len)) {
1054 ptr->snapshot_refs[idx].wrapped = true;
1060 * In full trace mode 'head' continually increases. However in snapshot
1061 * mode 'head' is an offset within the buffer. Here 'old' and 'head'
1062 * are adjusted to match the full trace case which expects that 'old' is
1063 * always less than 'head'.
1077 pr_debug3("%s: wrap-around %sdetected, adjusted old head %zu adjusted new head %zu\n",
1078 __func__, wrapped ? "" : "not ", (size_t)*old, (size_t)*head);
1083 pr_err("%s: failed, error %d\n", __func__, err);
1087 static u64 intel_pt_reference(struct auxtrace_record *itr __maybe_unused)
1092 static int intel_pt_read_finish(struct auxtrace_record *itr, int idx)
1094 struct intel_pt_recording *ptr =
1095 container_of(itr, struct intel_pt_recording, itr);
1096 struct evsel *evsel;
1098 evlist__for_each_entry(ptr->evlist, evsel) {
1099 if (evsel->core.attr.type == ptr->intel_pt_pmu->type)
1100 return perf_evlist__enable_event_idx(ptr->evlist, evsel,
1106 struct auxtrace_record *intel_pt_recording_init(int *err)
1108 struct perf_pmu *intel_pt_pmu = perf_pmu__find(INTEL_PT_PMU_NAME);
1109 struct intel_pt_recording *ptr;
1114 if (setenv("JITDUMP_USE_ARCH_TIMESTAMP", "1", 1)) {
1119 ptr = zalloc(sizeof(struct intel_pt_recording));
1125 ptr->intel_pt_pmu = intel_pt_pmu;
1126 ptr->itr.recording_options = intel_pt_recording_options;
1127 ptr->itr.info_priv_size = intel_pt_info_priv_size;
1128 ptr->itr.info_fill = intel_pt_info_fill;
1129 ptr->itr.free = intel_pt_recording_free;
1130 ptr->itr.snapshot_start = intel_pt_snapshot_start;
1131 ptr->itr.snapshot_finish = intel_pt_snapshot_finish;
1132 ptr->itr.find_snapshot = intel_pt_find_snapshot;
1133 ptr->itr.parse_snapshot_options = intel_pt_parse_snapshot_options;
1134 ptr->itr.reference = intel_pt_reference;
1135 ptr->itr.read_finish = intel_pt_read_finish;