2 * intel_pt.c: Intel Processor Trace support
3 * Copyright (c) 2013-2015, Intel Corporation.
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.
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
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/bitops.h>
21 #include <linux/log2.h>
24 #include "../../perf.h"
25 #include "../../util/session.h"
26 #include "../../util/event.h"
27 #include "../../util/evlist.h"
28 #include "../../util/evsel.h"
29 #include "../../util/cpumap.h"
30 #include <subcmd/parse-options.h>
31 #include "../../util/parse-events.h"
32 #include "../../util/pmu.h"
33 #include "../../util/debug.h"
34 #include "../../util/auxtrace.h"
35 #include "../../util/tsc.h"
36 #include "../../util/intel-pt.h"
38 #define KiB(x) ((x) * 1024)
39 #define MiB(x) ((x) * 1024 * 1024)
40 #define KiB_MASK(x) (KiB(x) - 1)
41 #define MiB_MASK(x) (MiB(x) - 1)
43 #define INTEL_PT_PSB_PERIOD_NEAR 256
45 struct intel_pt_snapshot_ref {
51 struct intel_pt_recording {
52 struct auxtrace_record itr;
53 struct perf_pmu *intel_pt_pmu;
54 int have_sched_switch;
55 struct perf_evlist *evlist;
57 bool snapshot_init_done;
59 size_t snapshot_ref_buf_size;
61 struct intel_pt_snapshot_ref *snapshot_refs;
65 static int intel_pt_parse_terms_with_default(struct list_head *formats,
69 struct list_head *terms;
70 struct perf_event_attr attr = { .size = 0, };
73 terms = malloc(sizeof(struct list_head));
77 INIT_LIST_HEAD(terms);
79 err = parse_events_terms(terms, str);
83 attr.config = *config;
84 err = perf_pmu__config_terms(formats, &attr, terms, true, NULL);
88 *config = attr.config;
90 parse_events_terms__delete(terms);
94 static int intel_pt_parse_terms(struct list_head *formats, const char *str,
98 return intel_pt_parse_terms_with_default(formats, str, config);
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 perf_evlist *evlist, u64 *res)
123 struct perf_evsel *evsel;
128 mask = perf_pmu__format_bits(&intel_pt_pmu->format, str);
132 evlist__for_each_entry(evlist, evsel) {
133 if (evsel->attr.type == intel_pt_pmu->type) {
134 *res = intel_pt_masked_bits(mask, evsel->attr.config);
142 static size_t intel_pt_psb_period(struct perf_pmu *intel_pt_pmu,
143 struct perf_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->format, buf, &config);
240 static int intel_pt_parse_snapshot_options(struct auxtrace_record *itr,
241 struct record_opts *opts,
244 struct intel_pt_recording *ptr =
245 container_of(itr, struct intel_pt_recording, itr);
246 unsigned long long snapshot_size = 0;
250 snapshot_size = strtoull(str, &endptr, 0);
251 if (*endptr || snapshot_size > SIZE_MAX)
255 opts->auxtrace_snapshot_mode = true;
256 opts->auxtrace_snapshot_size = snapshot_size;
258 ptr->snapshot_size = snapshot_size;
263 struct perf_event_attr *
264 intel_pt_pmu_default_config(struct perf_pmu *intel_pt_pmu)
266 struct perf_event_attr *attr;
268 attr = zalloc(sizeof(struct perf_event_attr));
272 attr->config = intel_pt_default_config(intel_pt_pmu);
274 intel_pt_pmu->selectable = true;
279 static const char *intel_pt_find_filter(struct perf_evlist *evlist,
280 struct perf_pmu *intel_pt_pmu)
282 struct perf_evsel *evsel;
284 evlist__for_each_entry(evlist, evsel) {
285 if (evsel->attr.type == intel_pt_pmu->type)
286 return evsel->filter;
292 static size_t intel_pt_filter_bytes(const char *filter)
294 size_t len = filter ? strlen(filter) : 0;
296 return len ? roundup(len + 1, 8) : 0;
300 intel_pt_info_priv_size(struct auxtrace_record *itr, struct perf_evlist *evlist)
302 struct intel_pt_recording *ptr =
303 container_of(itr, struct intel_pt_recording, itr);
304 const char *filter = intel_pt_find_filter(evlist, ptr->intel_pt_pmu);
306 ptr->priv_size = (INTEL_PT_AUXTRACE_PRIV_MAX * sizeof(u64)) +
307 intel_pt_filter_bytes(filter);
309 return ptr->priv_size;
312 static void intel_pt_tsc_ctc_ratio(u32 *n, u32 *d)
314 unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
316 __get_cpuid(0x15, &eax, &ebx, &ecx, &edx);
321 static int intel_pt_info_fill(struct auxtrace_record *itr,
322 struct perf_session *session,
323 struct auxtrace_info_event *auxtrace_info,
326 struct intel_pt_recording *ptr =
327 container_of(itr, struct intel_pt_recording, itr);
328 struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;
329 struct perf_event_mmap_page *pc;
330 struct perf_tsc_conversion tc = { .time_mult = 0, };
331 bool cap_user_time_zero = false, per_cpu_mmaps;
332 u64 tsc_bit, mtc_bit, mtc_freq_bits, cyc_bit, noretcomp_bit;
333 u32 tsc_ctc_ratio_n, tsc_ctc_ratio_d;
334 unsigned long max_non_turbo_ratio;
335 size_t filter_str_len;
340 if (priv_size != ptr->priv_size)
343 intel_pt_parse_terms(&intel_pt_pmu->format, "tsc", &tsc_bit);
344 intel_pt_parse_terms(&intel_pt_pmu->format, "noretcomp",
346 intel_pt_parse_terms(&intel_pt_pmu->format, "mtc", &mtc_bit);
347 mtc_freq_bits = perf_pmu__format_bits(&intel_pt_pmu->format,
349 intel_pt_parse_terms(&intel_pt_pmu->format, "cyc", &cyc_bit);
351 intel_pt_tsc_ctc_ratio(&tsc_ctc_ratio_n, &tsc_ctc_ratio_d);
353 if (perf_pmu__scan_file(intel_pt_pmu, "max_nonturbo_ratio",
354 "%lu", &max_non_turbo_ratio) != 1)
355 max_non_turbo_ratio = 0;
357 filter = intel_pt_find_filter(session->evlist, ptr->intel_pt_pmu);
358 filter_str_len = filter ? strlen(filter) : 0;
360 if (!session->evlist->nr_mmaps)
363 pc = session->evlist->mmap[0].base;
365 err = perf_read_tsc_conversion(pc, &tc);
367 if (err != -EOPNOTSUPP)
370 cap_user_time_zero = tc.time_mult != 0;
372 if (!cap_user_time_zero)
373 ui__warning("Intel Processor Trace: TSC not available\n");
376 per_cpu_mmaps = !cpu_map__empty(session->evlist->cpus);
378 auxtrace_info->type = PERF_AUXTRACE_INTEL_PT;
379 auxtrace_info->priv[INTEL_PT_PMU_TYPE] = intel_pt_pmu->type;
380 auxtrace_info->priv[INTEL_PT_TIME_SHIFT] = tc.time_shift;
381 auxtrace_info->priv[INTEL_PT_TIME_MULT] = tc.time_mult;
382 auxtrace_info->priv[INTEL_PT_TIME_ZERO] = tc.time_zero;
383 auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO] = cap_user_time_zero;
384 auxtrace_info->priv[INTEL_PT_TSC_BIT] = tsc_bit;
385 auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT] = noretcomp_bit;
386 auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH] = ptr->have_sched_switch;
387 auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE] = ptr->snapshot_mode;
388 auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS] = per_cpu_mmaps;
389 auxtrace_info->priv[INTEL_PT_MTC_BIT] = mtc_bit;
390 auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS] = mtc_freq_bits;
391 auxtrace_info->priv[INTEL_PT_TSC_CTC_N] = tsc_ctc_ratio_n;
392 auxtrace_info->priv[INTEL_PT_TSC_CTC_D] = tsc_ctc_ratio_d;
393 auxtrace_info->priv[INTEL_PT_CYC_BIT] = cyc_bit;
394 auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO] = max_non_turbo_ratio;
395 auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] = filter_str_len;
397 info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
399 if (filter_str_len) {
400 size_t len = intel_pt_filter_bytes(filter);
402 strncpy((char *)info, filter, len);
409 static int intel_pt_track_switches(struct perf_evlist *evlist)
411 const char *sched_switch = "sched:sched_switch";
412 struct perf_evsel *evsel;
415 if (!perf_evlist__can_select_event(evlist, sched_switch))
418 err = parse_events(evlist, sched_switch, NULL);
420 pr_debug2("%s: failed to parse %s, error %d\n",
421 __func__, sched_switch, err);
425 evsel = perf_evlist__last(evlist);
427 perf_evsel__set_sample_bit(evsel, CPU);
428 perf_evsel__set_sample_bit(evsel, TIME);
430 evsel->system_wide = true;
431 evsel->no_aux_samples = true;
432 evsel->immediate = true;
437 static void intel_pt_valid_str(char *str, size_t len, u64 valid)
439 unsigned int val, last = 0, state = 1;
444 for (val = 0; val <= 64; val++, valid >>= 1) {
449 p += scnprintf(str + p, len - p, ",");
452 p += scnprintf(str + p, len - p, "%u", val);
467 p += scnprintf(str + p, len - p, ",%u", last);
471 p += scnprintf(str + p, len - p, "-%u", last);
483 static int intel_pt_val_config_term(struct perf_pmu *intel_pt_pmu,
484 const char *caps, const char *name,
485 const char *supported, u64 config)
489 unsigned long long valid;
493 if (perf_pmu__scan_file(intel_pt_pmu, caps, "%llx", &valid) != 1)
497 perf_pmu__scan_file(intel_pt_pmu, supported, "%d", &ok) == 1 && !ok)
502 bits = perf_pmu__format_bits(&intel_pt_pmu->format, name);
506 for (shift = 0; bits && !(bits & 1); shift++)
514 if (valid & (1 << config))
517 intel_pt_valid_str(valid_str, sizeof(valid_str), valid);
518 pr_err("Invalid %s for %s. Valid values are: %s\n",
519 name, INTEL_PT_PMU_NAME, valid_str);
523 static int intel_pt_validate_config(struct perf_pmu *intel_pt_pmu,
524 struct perf_evsel *evsel)
533 * If supported, force pass-through config term (pt=1) even if user
534 * sets pt=0, which avoids senseless kernel errors.
536 if (perf_pmu__scan_file(intel_pt_pmu, "format/pt", "%c", &c) == 1 &&
537 !(evsel->attr.config & 1)) {
538 pr_warning("pt=0 doesn't make sense, forcing pt=1\n");
539 evsel->attr.config |= 1;
542 err = intel_pt_val_config_term(intel_pt_pmu, "caps/cycle_thresholds",
543 "cyc_thresh", "caps/psb_cyc",
548 err = intel_pt_val_config_term(intel_pt_pmu, "caps/mtc_periods",
549 "mtc_period", "caps/mtc",
554 return intel_pt_val_config_term(intel_pt_pmu, "caps/psb_periods",
555 "psb_period", "caps/psb_cyc",
559 static int intel_pt_recording_options(struct auxtrace_record *itr,
560 struct perf_evlist *evlist,
561 struct record_opts *opts)
563 struct intel_pt_recording *ptr =
564 container_of(itr, struct intel_pt_recording, itr);
565 struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;
566 bool have_timing_info, need_immediate = false;
567 struct perf_evsel *evsel, *intel_pt_evsel = NULL;
568 const struct cpu_map *cpus = evlist->cpus;
569 bool privileged = geteuid() == 0 || perf_event_paranoid() < 0;
573 ptr->evlist = evlist;
574 ptr->snapshot_mode = opts->auxtrace_snapshot_mode;
576 evlist__for_each_entry(evlist, evsel) {
577 if (evsel->attr.type == intel_pt_pmu->type) {
578 if (intel_pt_evsel) {
579 pr_err("There may be only one " INTEL_PT_PMU_NAME " event\n");
582 evsel->attr.freq = 0;
583 evsel->attr.sample_period = 1;
584 intel_pt_evsel = evsel;
585 opts->full_auxtrace = true;
589 if (opts->auxtrace_snapshot_mode && !opts->full_auxtrace) {
590 pr_err("Snapshot mode (-S option) requires " INTEL_PT_PMU_NAME " PMU event (-e " INTEL_PT_PMU_NAME ")\n");
594 if (opts->use_clockid) {
595 pr_err("Cannot use clockid (-k option) with " INTEL_PT_PMU_NAME "\n");
599 if (!opts->full_auxtrace)
602 err = intel_pt_validate_config(intel_pt_pmu, intel_pt_evsel);
606 /* Set default sizes for snapshot mode */
607 if (opts->auxtrace_snapshot_mode) {
608 size_t psb_period = intel_pt_psb_period(intel_pt_pmu, evlist);
610 if (!opts->auxtrace_snapshot_size && !opts->auxtrace_mmap_pages) {
612 opts->auxtrace_mmap_pages = MiB(4) / page_size;
614 opts->auxtrace_mmap_pages = KiB(128) / page_size;
615 if (opts->mmap_pages == UINT_MAX)
616 opts->mmap_pages = KiB(256) / page_size;
618 } else if (!opts->auxtrace_mmap_pages && !privileged &&
619 opts->mmap_pages == UINT_MAX) {
620 opts->mmap_pages = KiB(256) / page_size;
622 if (!opts->auxtrace_snapshot_size)
623 opts->auxtrace_snapshot_size =
624 opts->auxtrace_mmap_pages * (size_t)page_size;
625 if (!opts->auxtrace_mmap_pages) {
626 size_t sz = opts->auxtrace_snapshot_size;
628 sz = round_up(sz, page_size) / page_size;
629 opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
631 if (opts->auxtrace_snapshot_size >
632 opts->auxtrace_mmap_pages * (size_t)page_size) {
633 pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
634 opts->auxtrace_snapshot_size,
635 opts->auxtrace_mmap_pages * (size_t)page_size);
638 if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages) {
639 pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
642 pr_debug2("Intel PT snapshot size: %zu\n",
643 opts->auxtrace_snapshot_size);
645 opts->auxtrace_snapshot_size <= psb_period +
646 INTEL_PT_PSB_PERIOD_NEAR)
647 ui__warning("Intel PT snapshot size (%zu) may be too small for PSB period (%zu)\n",
648 opts->auxtrace_snapshot_size, psb_period);
651 /* Set default sizes for full trace mode */
652 if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
654 opts->auxtrace_mmap_pages = MiB(4) / page_size;
656 opts->auxtrace_mmap_pages = KiB(128) / page_size;
657 if (opts->mmap_pages == UINT_MAX)
658 opts->mmap_pages = KiB(256) / page_size;
662 /* Validate auxtrace_mmap_pages */
663 if (opts->auxtrace_mmap_pages) {
664 size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
667 if (opts->auxtrace_snapshot_mode)
672 if (sz < min_sz || !is_power_of_2(sz)) {
673 pr_err("Invalid mmap size for Intel Processor Trace: must be at least %zuKiB and a power of 2\n",
679 intel_pt_parse_terms(&intel_pt_pmu->format, "tsc", &tsc_bit);
681 if (opts->full_auxtrace && (intel_pt_evsel->attr.config & tsc_bit))
682 have_timing_info = true;
684 have_timing_info = false;
687 * Per-cpu recording needs sched_switch events to distinguish different
690 if (have_timing_info && !cpu_map__empty(cpus)) {
691 if (perf_can_record_switch_events()) {
692 bool cpu_wide = !target__none(&opts->target) &&
693 !target__has_task(&opts->target);
695 if (!cpu_wide && perf_can_record_cpu_wide()) {
696 struct perf_evsel *switch_evsel;
698 err = parse_events(evlist, "dummy:u", NULL);
702 switch_evsel = perf_evlist__last(evlist);
704 switch_evsel->attr.freq = 0;
705 switch_evsel->attr.sample_period = 1;
706 switch_evsel->attr.context_switch = 1;
708 switch_evsel->system_wide = true;
709 switch_evsel->no_aux_samples = true;
710 switch_evsel->immediate = true;
712 perf_evsel__set_sample_bit(switch_evsel, TID);
713 perf_evsel__set_sample_bit(switch_evsel, TIME);
714 perf_evsel__set_sample_bit(switch_evsel, CPU);
715 perf_evsel__reset_sample_bit(switch_evsel, BRANCH_STACK);
717 opts->record_switch_events = false;
718 ptr->have_sched_switch = 3;
720 opts->record_switch_events = true;
721 need_immediate = true;
723 ptr->have_sched_switch = 3;
725 ptr->have_sched_switch = 2;
728 err = intel_pt_track_switches(evlist);
730 pr_debug2("Unable to select sched:sched_switch\n");
734 ptr->have_sched_switch = 1;
738 if (intel_pt_evsel) {
740 * To obtain the auxtrace buffer file descriptor, the auxtrace
741 * event must come first.
743 perf_evlist__to_front(evlist, intel_pt_evsel);
745 * In the case of per-cpu mmaps, we need the CPU on the
748 if (!cpu_map__empty(cpus))
749 perf_evsel__set_sample_bit(intel_pt_evsel, CPU);
752 /* Add dummy event to keep tracking */
753 if (opts->full_auxtrace) {
754 struct perf_evsel *tracking_evsel;
756 err = parse_events(evlist, "dummy:u", NULL);
760 tracking_evsel = perf_evlist__last(evlist);
762 perf_evlist__set_tracking_event(evlist, tracking_evsel);
764 tracking_evsel->attr.freq = 0;
765 tracking_evsel->attr.sample_period = 1;
767 tracking_evsel->no_aux_samples = true;
769 tracking_evsel->immediate = true;
771 /* In per-cpu case, always need the time of mmap events etc */
772 if (!cpu_map__empty(cpus)) {
773 perf_evsel__set_sample_bit(tracking_evsel, TIME);
774 /* And the CPU for switch events */
775 perf_evsel__set_sample_bit(tracking_evsel, CPU);
777 perf_evsel__reset_sample_bit(tracking_evsel, BRANCH_STACK);
781 * Warn the user when we do not have enough information to decode i.e.
782 * per-cpu with no sched_switch (except workload-only).
784 if (!ptr->have_sched_switch && !cpu_map__empty(cpus) &&
785 !target__none(&opts->target))
786 ui__warning("Intel Processor Trace decoding will not be possible except for kernel tracing!\n");
791 static int intel_pt_snapshot_start(struct auxtrace_record *itr)
793 struct intel_pt_recording *ptr =
794 container_of(itr, struct intel_pt_recording, itr);
795 struct perf_evsel *evsel;
797 evlist__for_each_entry(ptr->evlist, evsel) {
798 if (evsel->attr.type == ptr->intel_pt_pmu->type)
799 return perf_evsel__disable(evsel);
804 static int intel_pt_snapshot_finish(struct auxtrace_record *itr)
806 struct intel_pt_recording *ptr =
807 container_of(itr, struct intel_pt_recording, itr);
808 struct perf_evsel *evsel;
810 evlist__for_each_entry(ptr->evlist, evsel) {
811 if (evsel->attr.type == ptr->intel_pt_pmu->type)
812 return perf_evsel__enable(evsel);
817 static int intel_pt_alloc_snapshot_refs(struct intel_pt_recording *ptr, int idx)
819 const size_t sz = sizeof(struct intel_pt_snapshot_ref);
820 int cnt = ptr->snapshot_ref_cnt, new_cnt = cnt * 2;
821 struct intel_pt_snapshot_ref *refs;
826 while (new_cnt <= idx)
829 refs = calloc(new_cnt, sz);
833 memcpy(refs, ptr->snapshot_refs, cnt * sz);
835 ptr->snapshot_refs = refs;
836 ptr->snapshot_ref_cnt = new_cnt;
841 static void intel_pt_free_snapshot_refs(struct intel_pt_recording *ptr)
845 for (i = 0; i < ptr->snapshot_ref_cnt; i++)
846 zfree(&ptr->snapshot_refs[i].ref_buf);
847 zfree(&ptr->snapshot_refs);
850 static void intel_pt_recording_free(struct auxtrace_record *itr)
852 struct intel_pt_recording *ptr =
853 container_of(itr, struct intel_pt_recording, itr);
855 intel_pt_free_snapshot_refs(ptr);
859 static int intel_pt_alloc_snapshot_ref(struct intel_pt_recording *ptr, int idx,
860 size_t snapshot_buf_size)
862 size_t ref_buf_size = ptr->snapshot_ref_buf_size;
865 ref_buf = zalloc(ref_buf_size);
869 ptr->snapshot_refs[idx].ref_buf = ref_buf;
870 ptr->snapshot_refs[idx].ref_offset = snapshot_buf_size - ref_buf_size;
875 static size_t intel_pt_snapshot_ref_buf_size(struct intel_pt_recording *ptr,
876 size_t snapshot_buf_size)
878 const size_t max_size = 256 * 1024;
879 size_t buf_size = 0, psb_period;
881 if (ptr->snapshot_size <= 64 * 1024)
884 psb_period = intel_pt_psb_period(ptr->intel_pt_pmu, ptr->evlist);
886 buf_size = psb_period * 2;
888 if (!buf_size || buf_size > max_size)
891 if (buf_size >= snapshot_buf_size)
894 if (buf_size >= ptr->snapshot_size / 2)
900 static int intel_pt_snapshot_init(struct intel_pt_recording *ptr,
901 size_t snapshot_buf_size)
903 if (ptr->snapshot_init_done)
906 ptr->snapshot_init_done = true;
908 ptr->snapshot_ref_buf_size = intel_pt_snapshot_ref_buf_size(ptr,
915 * intel_pt_compare_buffers - compare bytes in a buffer to a circular buffer.
916 * @buf1: first buffer
917 * @compare_size: number of bytes to compare
918 * @buf2: second buffer (a circular buffer)
919 * @offs2: offset in second buffer
920 * @buf2_size: size of second buffer
922 * The comparison allows for the possibility that the bytes to compare in the
923 * circular buffer are not contiguous. It is assumed that @compare_size <=
924 * @buf2_size. This function returns %false if the bytes are identical, %true
927 static bool intel_pt_compare_buffers(void *buf1, size_t compare_size,
928 void *buf2, size_t offs2, size_t buf2_size)
930 size_t end2 = offs2 + compare_size, part_size;
932 if (end2 <= buf2_size)
933 return memcmp(buf1, buf2 + offs2, compare_size);
935 part_size = end2 - buf2_size;
936 if (memcmp(buf1, buf2 + offs2, part_size))
939 compare_size -= part_size;
941 return memcmp(buf1 + part_size, buf2, compare_size);
944 static bool intel_pt_compare_ref(void *ref_buf, size_t ref_offset,
945 size_t ref_size, size_t buf_size,
946 void *data, size_t head)
948 size_t ref_end = ref_offset + ref_size;
950 if (ref_end > buf_size) {
951 if (head > ref_offset || head < ref_end - buf_size)
953 } else if (head > ref_offset && head < ref_end) {
957 return intel_pt_compare_buffers(ref_buf, ref_size, data, ref_offset,
961 static void intel_pt_copy_ref(void *ref_buf, size_t ref_size, size_t buf_size,
962 void *data, size_t head)
964 if (head >= ref_size) {
965 memcpy(ref_buf, data + head - ref_size, ref_size);
967 memcpy(ref_buf, data, head);
969 memcpy(ref_buf + head, data + buf_size - ref_size, ref_size);
973 static bool intel_pt_wrapped(struct intel_pt_recording *ptr, int idx,
974 struct auxtrace_mmap *mm, unsigned char *data,
977 struct intel_pt_snapshot_ref *ref = &ptr->snapshot_refs[idx];
980 wrapped = intel_pt_compare_ref(ref->ref_buf, ref->ref_offset,
981 ptr->snapshot_ref_buf_size, mm->len,
984 intel_pt_copy_ref(ref->ref_buf, ptr->snapshot_ref_buf_size, mm->len,
990 static bool intel_pt_first_wrap(u64 *data, size_t buf_size)
999 for (i = a; i < b; i++) {
1007 static int intel_pt_find_snapshot(struct auxtrace_record *itr, int idx,
1008 struct auxtrace_mmap *mm, unsigned char *data,
1009 u64 *head, u64 *old)
1011 struct intel_pt_recording *ptr =
1012 container_of(itr, struct intel_pt_recording, itr);
1016 pr_debug3("%s: mmap index %d old head %zu new head %zu\n",
1017 __func__, idx, (size_t)*old, (size_t)*head);
1019 err = intel_pt_snapshot_init(ptr, mm->len);
1023 if (idx >= ptr->snapshot_ref_cnt) {
1024 err = intel_pt_alloc_snapshot_refs(ptr, idx);
1029 if (ptr->snapshot_ref_buf_size) {
1030 if (!ptr->snapshot_refs[idx].ref_buf) {
1031 err = intel_pt_alloc_snapshot_ref(ptr, idx, mm->len);
1035 wrapped = intel_pt_wrapped(ptr, idx, mm, data, *head);
1037 wrapped = ptr->snapshot_refs[idx].wrapped;
1038 if (!wrapped && intel_pt_first_wrap((u64 *)data, mm->len)) {
1039 ptr->snapshot_refs[idx].wrapped = true;
1045 * In full trace mode 'head' continually increases. However in snapshot
1046 * mode 'head' is an offset within the buffer. Here 'old' and 'head'
1047 * are adjusted to match the full trace case which expects that 'old' is
1048 * always less than 'head'.
1062 pr_debug3("%s: wrap-around %sdetected, adjusted old head %zu adjusted new head %zu\n",
1063 __func__, wrapped ? "" : "not ", (size_t)*old, (size_t)*head);
1068 pr_err("%s: failed, error %d\n", __func__, err);
1072 static u64 intel_pt_reference(struct auxtrace_record *itr __maybe_unused)
1077 static int intel_pt_read_finish(struct auxtrace_record *itr, int idx)
1079 struct intel_pt_recording *ptr =
1080 container_of(itr, struct intel_pt_recording, itr);
1081 struct perf_evsel *evsel;
1083 evlist__for_each_entry(ptr->evlist, evsel) {
1084 if (evsel->attr.type == ptr->intel_pt_pmu->type)
1085 return perf_evlist__enable_event_idx(ptr->evlist, evsel,
1091 struct auxtrace_record *intel_pt_recording_init(int *err)
1093 struct perf_pmu *intel_pt_pmu = perf_pmu__find(INTEL_PT_PMU_NAME);
1094 struct intel_pt_recording *ptr;
1099 if (setenv("JITDUMP_USE_ARCH_TIMESTAMP", "1", 1)) {
1104 ptr = zalloc(sizeof(struct intel_pt_recording));
1110 ptr->intel_pt_pmu = intel_pt_pmu;
1111 ptr->itr.recording_options = intel_pt_recording_options;
1112 ptr->itr.info_priv_size = intel_pt_info_priv_size;
1113 ptr->itr.info_fill = intel_pt_info_fill;
1114 ptr->itr.free = intel_pt_recording_free;
1115 ptr->itr.snapshot_start = intel_pt_snapshot_start;
1116 ptr->itr.snapshot_finish = intel_pt_snapshot_finish;
1117 ptr->itr.find_snapshot = intel_pt_find_snapshot;
1118 ptr->itr.parse_snapshot_options = intel_pt_parse_snapshot_options;
1119 ptr->itr.reference = intel_pt_reference;
1120 ptr->itr.read_finish = intel_pt_read_finish;