1 // SPDX-License-Identifier: GPL-2.0-only
3 * intel-bts.c: Intel Processor Trace support
4 * Copyright (c) 2013-2015, Intel Corporation.
8 #include <linux/kernel.h>
9 #include <linux/types.h>
10 #include <linux/bitops.h>
11 #include <linux/log2.h>
13 #include "../../util/cpumap.h"
14 #include "../../util/evsel.h"
15 #include "../../util/evlist.h"
16 #include "../../util/session.h"
17 #include "../../util/util.h"
18 #include "../../util/pmu.h"
19 #include "../../util/debug.h"
20 #include "../../util/tsc.h"
21 #include "../../util/auxtrace.h"
22 #include "../../util/intel-bts.h"
24 #define KiB(x) ((x) * 1024)
25 #define MiB(x) ((x) * 1024 * 1024)
26 #define KiB_MASK(x) (KiB(x) - 1)
27 #define MiB_MASK(x) (MiB(x) - 1)
29 struct intel_bts_snapshot_ref {
35 struct intel_bts_recording {
36 struct auxtrace_record itr;
37 struct perf_pmu *intel_bts_pmu;
38 struct perf_evlist *evlist;
42 struct intel_bts_snapshot_ref *snapshot_refs;
52 intel_bts_info_priv_size(struct auxtrace_record *itr __maybe_unused,
53 struct perf_evlist *evlist __maybe_unused)
55 return INTEL_BTS_AUXTRACE_PRIV_SIZE;
58 static int intel_bts_info_fill(struct auxtrace_record *itr,
59 struct perf_session *session,
60 struct auxtrace_info_event *auxtrace_info,
63 struct intel_bts_recording *btsr =
64 container_of(itr, struct intel_bts_recording, itr);
65 struct perf_pmu *intel_bts_pmu = btsr->intel_bts_pmu;
66 struct perf_event_mmap_page *pc;
67 struct perf_tsc_conversion tc = { .time_mult = 0, };
68 bool cap_user_time_zero = false;
71 if (priv_size != INTEL_BTS_AUXTRACE_PRIV_SIZE)
74 if (!session->evlist->nr_mmaps)
77 pc = session->evlist->mmap[0].base;
79 err = perf_read_tsc_conversion(pc, &tc);
81 if (err != -EOPNOTSUPP)
84 cap_user_time_zero = tc.time_mult != 0;
86 if (!cap_user_time_zero)
87 ui__warning("Intel BTS: TSC not available\n");
90 auxtrace_info->type = PERF_AUXTRACE_INTEL_BTS;
91 auxtrace_info->priv[INTEL_BTS_PMU_TYPE] = intel_bts_pmu->type;
92 auxtrace_info->priv[INTEL_BTS_TIME_SHIFT] = tc.time_shift;
93 auxtrace_info->priv[INTEL_BTS_TIME_MULT] = tc.time_mult;
94 auxtrace_info->priv[INTEL_BTS_TIME_ZERO] = tc.time_zero;
95 auxtrace_info->priv[INTEL_BTS_CAP_USER_TIME_ZERO] = cap_user_time_zero;
96 auxtrace_info->priv[INTEL_BTS_SNAPSHOT_MODE] = btsr->snapshot_mode;
101 static int intel_bts_recording_options(struct auxtrace_record *itr,
102 struct perf_evlist *evlist,
103 struct record_opts *opts)
105 struct intel_bts_recording *btsr =
106 container_of(itr, struct intel_bts_recording, itr);
107 struct perf_pmu *intel_bts_pmu = btsr->intel_bts_pmu;
108 struct perf_evsel *evsel, *intel_bts_evsel = NULL;
109 const struct cpu_map *cpus = evlist->cpus;
110 bool privileged = geteuid() == 0 || perf_event_paranoid() < 0;
112 btsr->evlist = evlist;
113 btsr->snapshot_mode = opts->auxtrace_snapshot_mode;
115 evlist__for_each_entry(evlist, evsel) {
116 if (evsel->attr.type == intel_bts_pmu->type) {
117 if (intel_bts_evsel) {
118 pr_err("There may be only one " INTEL_BTS_PMU_NAME " event\n");
121 evsel->attr.freq = 0;
122 evsel->attr.sample_period = 1;
123 intel_bts_evsel = evsel;
124 opts->full_auxtrace = true;
128 if (opts->auxtrace_snapshot_mode && !opts->full_auxtrace) {
129 pr_err("Snapshot mode (-S option) requires " INTEL_BTS_PMU_NAME " PMU event (-e " INTEL_BTS_PMU_NAME ")\n");
133 if (!opts->full_auxtrace)
136 if (opts->full_auxtrace && !cpu_map__empty(cpus)) {
137 pr_err(INTEL_BTS_PMU_NAME " does not support per-cpu recording\n");
141 /* Set default sizes for snapshot mode */
142 if (opts->auxtrace_snapshot_mode) {
143 if (!opts->auxtrace_snapshot_size && !opts->auxtrace_mmap_pages) {
145 opts->auxtrace_mmap_pages = MiB(4) / page_size;
147 opts->auxtrace_mmap_pages = KiB(128) / page_size;
148 if (opts->mmap_pages == UINT_MAX)
149 opts->mmap_pages = KiB(256) / page_size;
151 } else if (!opts->auxtrace_mmap_pages && !privileged &&
152 opts->mmap_pages == UINT_MAX) {
153 opts->mmap_pages = KiB(256) / page_size;
155 if (!opts->auxtrace_snapshot_size)
156 opts->auxtrace_snapshot_size =
157 opts->auxtrace_mmap_pages * (size_t)page_size;
158 if (!opts->auxtrace_mmap_pages) {
159 size_t sz = opts->auxtrace_snapshot_size;
161 sz = round_up(sz, page_size) / page_size;
162 opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
164 if (opts->auxtrace_snapshot_size >
165 opts->auxtrace_mmap_pages * (size_t)page_size) {
166 pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
167 opts->auxtrace_snapshot_size,
168 opts->auxtrace_mmap_pages * (size_t)page_size);
171 if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages) {
172 pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
175 pr_debug2("Intel BTS snapshot size: %zu\n",
176 opts->auxtrace_snapshot_size);
179 /* Set default sizes for full trace mode */
180 if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
182 opts->auxtrace_mmap_pages = MiB(4) / page_size;
184 opts->auxtrace_mmap_pages = KiB(128) / page_size;
185 if (opts->mmap_pages == UINT_MAX)
186 opts->mmap_pages = KiB(256) / page_size;
190 /* Validate auxtrace_mmap_pages */
191 if (opts->auxtrace_mmap_pages) {
192 size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
195 if (opts->auxtrace_snapshot_mode)
200 if (sz < min_sz || !is_power_of_2(sz)) {
201 pr_err("Invalid mmap size for Intel BTS: must be at least %zuKiB and a power of 2\n",
207 if (intel_bts_evsel) {
209 * To obtain the auxtrace buffer file descriptor, the auxtrace event
212 perf_evlist__to_front(evlist, intel_bts_evsel);
214 * In the case of per-cpu mmaps, we need the CPU on the
217 if (!cpu_map__empty(cpus))
218 perf_evsel__set_sample_bit(intel_bts_evsel, CPU);
221 /* Add dummy event to keep tracking */
222 if (opts->full_auxtrace) {
223 struct perf_evsel *tracking_evsel;
226 err = parse_events(evlist, "dummy:u", NULL);
230 tracking_evsel = perf_evlist__last(evlist);
232 perf_evlist__set_tracking_event(evlist, tracking_evsel);
234 tracking_evsel->attr.freq = 0;
235 tracking_evsel->attr.sample_period = 1;
241 static int intel_bts_parse_snapshot_options(struct auxtrace_record *itr,
242 struct record_opts *opts,
245 struct intel_bts_recording *btsr =
246 container_of(itr, struct intel_bts_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 btsr->snapshot_size = snapshot_size;
264 static u64 intel_bts_reference(struct auxtrace_record *itr __maybe_unused)
269 static int intel_bts_alloc_snapshot_refs(struct intel_bts_recording *btsr,
272 const size_t sz = sizeof(struct intel_bts_snapshot_ref);
273 int cnt = btsr->snapshot_ref_cnt, new_cnt = cnt * 2;
274 struct intel_bts_snapshot_ref *refs;
279 while (new_cnt <= idx)
282 refs = calloc(new_cnt, sz);
286 memcpy(refs, btsr->snapshot_refs, cnt * sz);
288 btsr->snapshot_refs = refs;
289 btsr->snapshot_ref_cnt = new_cnt;
294 static void intel_bts_free_snapshot_refs(struct intel_bts_recording *btsr)
298 for (i = 0; i < btsr->snapshot_ref_cnt; i++)
299 zfree(&btsr->snapshot_refs[i].ref_buf);
300 zfree(&btsr->snapshot_refs);
303 static void intel_bts_recording_free(struct auxtrace_record *itr)
305 struct intel_bts_recording *btsr =
306 container_of(itr, struct intel_bts_recording, itr);
308 intel_bts_free_snapshot_refs(btsr);
312 static int intel_bts_snapshot_start(struct auxtrace_record *itr)
314 struct intel_bts_recording *btsr =
315 container_of(itr, struct intel_bts_recording, itr);
316 struct perf_evsel *evsel;
318 evlist__for_each_entry(btsr->evlist, evsel) {
319 if (evsel->attr.type == btsr->intel_bts_pmu->type)
320 return perf_evsel__disable(evsel);
325 static int intel_bts_snapshot_finish(struct auxtrace_record *itr)
327 struct intel_bts_recording *btsr =
328 container_of(itr, struct intel_bts_recording, itr);
329 struct perf_evsel *evsel;
331 evlist__for_each_entry(btsr->evlist, evsel) {
332 if (evsel->attr.type == btsr->intel_bts_pmu->type)
333 return perf_evsel__enable(evsel);
338 static bool intel_bts_first_wrap(u64 *data, size_t buf_size)
347 for (i = a; i < b; i++) {
355 static int intel_bts_find_snapshot(struct auxtrace_record *itr, int idx,
356 struct auxtrace_mmap *mm, unsigned char *data,
359 struct intel_bts_recording *btsr =
360 container_of(itr, struct intel_bts_recording, itr);
364 pr_debug3("%s: mmap index %d old head %zu new head %zu\n",
365 __func__, idx, (size_t)*old, (size_t)*head);
367 if (idx >= btsr->snapshot_ref_cnt) {
368 err = intel_bts_alloc_snapshot_refs(btsr, idx);
373 wrapped = btsr->snapshot_refs[idx].wrapped;
374 if (!wrapped && intel_bts_first_wrap((u64 *)data, mm->len)) {
375 btsr->snapshot_refs[idx].wrapped = true;
380 * In full trace mode 'head' continually increases. However in snapshot
381 * mode 'head' is an offset within the buffer. Here 'old' and 'head'
382 * are adjusted to match the full trace case which expects that 'old' is
383 * always less than 'head'.
397 pr_debug3("%s: wrap-around %sdetected, adjusted old head %zu adjusted new head %zu\n",
398 __func__, wrapped ? "" : "not ", (size_t)*old, (size_t)*head);
403 pr_err("%s: failed, error %d\n", __func__, err);
407 static int intel_bts_read_finish(struct auxtrace_record *itr, int idx)
409 struct intel_bts_recording *btsr =
410 container_of(itr, struct intel_bts_recording, itr);
411 struct perf_evsel *evsel;
413 evlist__for_each_entry(btsr->evlist, evsel) {
414 if (evsel->attr.type == btsr->intel_bts_pmu->type)
415 return perf_evlist__enable_event_idx(btsr->evlist,
421 struct auxtrace_record *intel_bts_recording_init(int *err)
423 struct perf_pmu *intel_bts_pmu = perf_pmu__find(INTEL_BTS_PMU_NAME);
424 struct intel_bts_recording *btsr;
429 if (setenv("JITDUMP_USE_ARCH_TIMESTAMP", "1", 1)) {
434 btsr = zalloc(sizeof(struct intel_bts_recording));
440 btsr->intel_bts_pmu = intel_bts_pmu;
441 btsr->itr.recording_options = intel_bts_recording_options;
442 btsr->itr.info_priv_size = intel_bts_info_priv_size;
443 btsr->itr.info_fill = intel_bts_info_fill;
444 btsr->itr.free = intel_bts_recording_free;
445 btsr->itr.snapshot_start = intel_bts_snapshot_start;
446 btsr->itr.snapshot_finish = intel_bts_snapshot_finish;
447 btsr->itr.find_snapshot = intel_bts_find_snapshot;
448 btsr->itr.parse_snapshot_options = intel_bts_parse_snapshot_options;
449 btsr->itr.reference = intel_bts_reference;
450 btsr->itr.read_finish = intel_bts_read_finish;
451 btsr->itr.alignment = sizeof(struct branch);