xref: /openbmc/linux/tools/perf/arch/x86/util/intel-bts.c (revision 6355592e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * intel-bts.c: Intel Processor Trace support
4  * Copyright (c) 2013-2015, Intel Corporation.
5  */
6 
7 #include <errno.h>
8 #include <linux/kernel.h>
9 #include <linux/types.h>
10 #include <linux/bitops.h>
11 #include <linux/log2.h>
12 #include <linux/zalloc.h>
13 
14 #include "../../util/cpumap.h"
15 #include "../../util/event.h"
16 #include "../../util/evsel.h"
17 #include "../../util/evlist.h"
18 #include "../../util/session.h"
19 #include "../../util/pmu.h"
20 #include "../../util/debug.h"
21 #include "../../util/record.h"
22 #include "../../util/tsc.h"
23 #include "../../util/auxtrace.h"
24 #include "../../util/intel-bts.h"
25 #include "../../util/util.h"
26 
27 #define KiB(x) ((x) * 1024)
28 #define MiB(x) ((x) * 1024 * 1024)
29 #define KiB_MASK(x) (KiB(x) - 1)
30 #define MiB_MASK(x) (MiB(x) - 1)
31 
32 struct intel_bts_snapshot_ref {
33 	void	*ref_buf;
34 	size_t	ref_offset;
35 	bool	wrapped;
36 };
37 
38 struct intel_bts_recording {
39 	struct auxtrace_record		itr;
40 	struct perf_pmu			*intel_bts_pmu;
41 	struct evlist		*evlist;
42 	bool				snapshot_mode;
43 	size_t				snapshot_size;
44 	int				snapshot_ref_cnt;
45 	struct intel_bts_snapshot_ref	*snapshot_refs;
46 };
47 
48 struct branch {
49 	u64 from;
50 	u64 to;
51 	u64 misc;
52 };
53 
54 static size_t
55 intel_bts_info_priv_size(struct auxtrace_record *itr __maybe_unused,
56 			 struct evlist *evlist __maybe_unused)
57 {
58 	return INTEL_BTS_AUXTRACE_PRIV_SIZE;
59 }
60 
61 static int intel_bts_info_fill(struct auxtrace_record *itr,
62 			       struct perf_session *session,
63 			       struct perf_record_auxtrace_info *auxtrace_info,
64 			       size_t priv_size)
65 {
66 	struct intel_bts_recording *btsr =
67 			container_of(itr, struct intel_bts_recording, itr);
68 	struct perf_pmu *intel_bts_pmu = btsr->intel_bts_pmu;
69 	struct perf_event_mmap_page *pc;
70 	struct perf_tsc_conversion tc = { .time_mult = 0, };
71 	bool cap_user_time_zero = false;
72 	int err;
73 
74 	if (priv_size != INTEL_BTS_AUXTRACE_PRIV_SIZE)
75 		return -EINVAL;
76 
77 	if (!session->evlist->nr_mmaps)
78 		return -EINVAL;
79 
80 	pc = session->evlist->mmap[0].base;
81 	if (pc) {
82 		err = perf_read_tsc_conversion(pc, &tc);
83 		if (err) {
84 			if (err != -EOPNOTSUPP)
85 				return err;
86 		} else {
87 			cap_user_time_zero = tc.time_mult != 0;
88 		}
89 		if (!cap_user_time_zero)
90 			ui__warning("Intel BTS: TSC not available\n");
91 	}
92 
93 	auxtrace_info->type = PERF_AUXTRACE_INTEL_BTS;
94 	auxtrace_info->priv[INTEL_BTS_PMU_TYPE] = intel_bts_pmu->type;
95 	auxtrace_info->priv[INTEL_BTS_TIME_SHIFT] = tc.time_shift;
96 	auxtrace_info->priv[INTEL_BTS_TIME_MULT] = tc.time_mult;
97 	auxtrace_info->priv[INTEL_BTS_TIME_ZERO] = tc.time_zero;
98 	auxtrace_info->priv[INTEL_BTS_CAP_USER_TIME_ZERO] = cap_user_time_zero;
99 	auxtrace_info->priv[INTEL_BTS_SNAPSHOT_MODE] = btsr->snapshot_mode;
100 
101 	return 0;
102 }
103 
104 static int intel_bts_recording_options(struct auxtrace_record *itr,
105 				       struct evlist *evlist,
106 				       struct record_opts *opts)
107 {
108 	struct intel_bts_recording *btsr =
109 			container_of(itr, struct intel_bts_recording, itr);
110 	struct perf_pmu *intel_bts_pmu = btsr->intel_bts_pmu;
111 	struct evsel *evsel, *intel_bts_evsel = NULL;
112 	const struct perf_cpu_map *cpus = evlist->core.cpus;
113 	bool privileged = perf_event_paranoid_check(-1);
114 
115 	btsr->evlist = evlist;
116 	btsr->snapshot_mode = opts->auxtrace_snapshot_mode;
117 
118 	evlist__for_each_entry(evlist, evsel) {
119 		if (evsel->core.attr.type == intel_bts_pmu->type) {
120 			if (intel_bts_evsel) {
121 				pr_err("There may be only one " INTEL_BTS_PMU_NAME " event\n");
122 				return -EINVAL;
123 			}
124 			evsel->core.attr.freq = 0;
125 			evsel->core.attr.sample_period = 1;
126 			intel_bts_evsel = evsel;
127 			opts->full_auxtrace = true;
128 		}
129 	}
130 
131 	if (opts->auxtrace_snapshot_mode && !opts->full_auxtrace) {
132 		pr_err("Snapshot mode (-S option) requires " INTEL_BTS_PMU_NAME " PMU event (-e " INTEL_BTS_PMU_NAME ")\n");
133 		return -EINVAL;
134 	}
135 
136 	if (!opts->full_auxtrace)
137 		return 0;
138 
139 	if (opts->full_auxtrace && !perf_cpu_map__empty(cpus)) {
140 		pr_err(INTEL_BTS_PMU_NAME " does not support per-cpu recording\n");
141 		return -EINVAL;
142 	}
143 
144 	/* Set default sizes for snapshot mode */
145 	if (opts->auxtrace_snapshot_mode) {
146 		if (!opts->auxtrace_snapshot_size && !opts->auxtrace_mmap_pages) {
147 			if (privileged) {
148 				opts->auxtrace_mmap_pages = MiB(4) / page_size;
149 			} else {
150 				opts->auxtrace_mmap_pages = KiB(128) / page_size;
151 				if (opts->mmap_pages == UINT_MAX)
152 					opts->mmap_pages = KiB(256) / page_size;
153 			}
154 		} else if (!opts->auxtrace_mmap_pages && !privileged &&
155 			   opts->mmap_pages == UINT_MAX) {
156 			opts->mmap_pages = KiB(256) / page_size;
157 		}
158 		if (!opts->auxtrace_snapshot_size)
159 			opts->auxtrace_snapshot_size =
160 				opts->auxtrace_mmap_pages * (size_t)page_size;
161 		if (!opts->auxtrace_mmap_pages) {
162 			size_t sz = opts->auxtrace_snapshot_size;
163 
164 			sz = round_up(sz, page_size) / page_size;
165 			opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
166 		}
167 		if (opts->auxtrace_snapshot_size >
168 				opts->auxtrace_mmap_pages * (size_t)page_size) {
169 			pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
170 			       opts->auxtrace_snapshot_size,
171 			       opts->auxtrace_mmap_pages * (size_t)page_size);
172 			return -EINVAL;
173 		}
174 		if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages) {
175 			pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
176 			return -EINVAL;
177 		}
178 		pr_debug2("Intel BTS snapshot size: %zu\n",
179 			  opts->auxtrace_snapshot_size);
180 	}
181 
182 	/* Set default sizes for full trace mode */
183 	if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
184 		if (privileged) {
185 			opts->auxtrace_mmap_pages = MiB(4) / page_size;
186 		} else {
187 			opts->auxtrace_mmap_pages = KiB(128) / page_size;
188 			if (opts->mmap_pages == UINT_MAX)
189 				opts->mmap_pages = KiB(256) / page_size;
190 		}
191 	}
192 
193 	/* Validate auxtrace_mmap_pages */
194 	if (opts->auxtrace_mmap_pages) {
195 		size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
196 		size_t min_sz;
197 
198 		if (opts->auxtrace_snapshot_mode)
199 			min_sz = KiB(4);
200 		else
201 			min_sz = KiB(8);
202 
203 		if (sz < min_sz || !is_power_of_2(sz)) {
204 			pr_err("Invalid mmap size for Intel BTS: must be at least %zuKiB and a power of 2\n",
205 			       min_sz / 1024);
206 			return -EINVAL;
207 		}
208 	}
209 
210 	if (intel_bts_evsel) {
211 		/*
212 		 * To obtain the auxtrace buffer file descriptor, the auxtrace event
213 		 * must come first.
214 		 */
215 		perf_evlist__to_front(evlist, intel_bts_evsel);
216 		/*
217 		 * In the case of per-cpu mmaps, we need the CPU on the
218 		 * AUX event.
219 		 */
220 		if (!perf_cpu_map__empty(cpus))
221 			perf_evsel__set_sample_bit(intel_bts_evsel, CPU);
222 	}
223 
224 	/* Add dummy event to keep tracking */
225 	if (opts->full_auxtrace) {
226 		struct evsel *tracking_evsel;
227 		int err;
228 
229 		err = parse_events(evlist, "dummy:u", NULL);
230 		if (err)
231 			return err;
232 
233 		tracking_evsel = perf_evlist__last(evlist);
234 
235 		perf_evlist__set_tracking_event(evlist, tracking_evsel);
236 
237 		tracking_evsel->core.attr.freq = 0;
238 		tracking_evsel->core.attr.sample_period = 1;
239 	}
240 
241 	return 0;
242 }
243 
244 static int intel_bts_parse_snapshot_options(struct auxtrace_record *itr,
245 					    struct record_opts *opts,
246 					    const char *str)
247 {
248 	struct intel_bts_recording *btsr =
249 			container_of(itr, struct intel_bts_recording, itr);
250 	unsigned long long snapshot_size = 0;
251 	char *endptr;
252 
253 	if (str) {
254 		snapshot_size = strtoull(str, &endptr, 0);
255 		if (*endptr || snapshot_size > SIZE_MAX)
256 			return -1;
257 	}
258 
259 	opts->auxtrace_snapshot_mode = true;
260 	opts->auxtrace_snapshot_size = snapshot_size;
261 
262 	btsr->snapshot_size = snapshot_size;
263 
264 	return 0;
265 }
266 
267 static u64 intel_bts_reference(struct auxtrace_record *itr __maybe_unused)
268 {
269 	return rdtsc();
270 }
271 
272 static int intel_bts_alloc_snapshot_refs(struct intel_bts_recording *btsr,
273 					 int idx)
274 {
275 	const size_t sz = sizeof(struct intel_bts_snapshot_ref);
276 	int cnt = btsr->snapshot_ref_cnt, new_cnt = cnt * 2;
277 	struct intel_bts_snapshot_ref *refs;
278 
279 	if (!new_cnt)
280 		new_cnt = 16;
281 
282 	while (new_cnt <= idx)
283 		new_cnt *= 2;
284 
285 	refs = calloc(new_cnt, sz);
286 	if (!refs)
287 		return -ENOMEM;
288 
289 	memcpy(refs, btsr->snapshot_refs, cnt * sz);
290 
291 	btsr->snapshot_refs = refs;
292 	btsr->snapshot_ref_cnt = new_cnt;
293 
294 	return 0;
295 }
296 
297 static void intel_bts_free_snapshot_refs(struct intel_bts_recording *btsr)
298 {
299 	int i;
300 
301 	for (i = 0; i < btsr->snapshot_ref_cnt; i++)
302 		zfree(&btsr->snapshot_refs[i].ref_buf);
303 	zfree(&btsr->snapshot_refs);
304 }
305 
306 static void intel_bts_recording_free(struct auxtrace_record *itr)
307 {
308 	struct intel_bts_recording *btsr =
309 			container_of(itr, struct intel_bts_recording, itr);
310 
311 	intel_bts_free_snapshot_refs(btsr);
312 	free(btsr);
313 }
314 
315 static int intel_bts_snapshot_start(struct auxtrace_record *itr)
316 {
317 	struct intel_bts_recording *btsr =
318 			container_of(itr, struct intel_bts_recording, itr);
319 	struct evsel *evsel;
320 
321 	evlist__for_each_entry(btsr->evlist, evsel) {
322 		if (evsel->core.attr.type == btsr->intel_bts_pmu->type)
323 			return evsel__disable(evsel);
324 	}
325 	return -EINVAL;
326 }
327 
328 static int intel_bts_snapshot_finish(struct auxtrace_record *itr)
329 {
330 	struct intel_bts_recording *btsr =
331 			container_of(itr, struct intel_bts_recording, itr);
332 	struct evsel *evsel;
333 
334 	evlist__for_each_entry(btsr->evlist, evsel) {
335 		if (evsel->core.attr.type == btsr->intel_bts_pmu->type)
336 			return evsel__enable(evsel);
337 	}
338 	return -EINVAL;
339 }
340 
341 static bool intel_bts_first_wrap(u64 *data, size_t buf_size)
342 {
343 	int i, a, b;
344 
345 	b = buf_size >> 3;
346 	a = b - 512;
347 	if (a < 0)
348 		a = 0;
349 
350 	for (i = a; i < b; i++) {
351 		if (data[i])
352 			return true;
353 	}
354 
355 	return false;
356 }
357 
358 static int intel_bts_find_snapshot(struct auxtrace_record *itr, int idx,
359 				   struct auxtrace_mmap *mm, unsigned char *data,
360 				   u64 *head, u64 *old)
361 {
362 	struct intel_bts_recording *btsr =
363 			container_of(itr, struct intel_bts_recording, itr);
364 	bool wrapped;
365 	int err;
366 
367 	pr_debug3("%s: mmap index %d old head %zu new head %zu\n",
368 		  __func__, idx, (size_t)*old, (size_t)*head);
369 
370 	if (idx >= btsr->snapshot_ref_cnt) {
371 		err = intel_bts_alloc_snapshot_refs(btsr, idx);
372 		if (err)
373 			goto out_err;
374 	}
375 
376 	wrapped = btsr->snapshot_refs[idx].wrapped;
377 	if (!wrapped && intel_bts_first_wrap((u64 *)data, mm->len)) {
378 		btsr->snapshot_refs[idx].wrapped = true;
379 		wrapped = true;
380 	}
381 
382 	/*
383 	 * In full trace mode 'head' continually increases.  However in snapshot
384 	 * mode 'head' is an offset within the buffer.  Here 'old' and 'head'
385 	 * are adjusted to match the full trace case which expects that 'old' is
386 	 * always less than 'head'.
387 	 */
388 	if (wrapped) {
389 		*old = *head;
390 		*head += mm->len;
391 	} else {
392 		if (mm->mask)
393 			*old &= mm->mask;
394 		else
395 			*old %= mm->len;
396 		if (*old > *head)
397 			*head += mm->len;
398 	}
399 
400 	pr_debug3("%s: wrap-around %sdetected, adjusted old head %zu adjusted new head %zu\n",
401 		  __func__, wrapped ? "" : "not ", (size_t)*old, (size_t)*head);
402 
403 	return 0;
404 
405 out_err:
406 	pr_err("%s: failed, error %d\n", __func__, err);
407 	return err;
408 }
409 
410 static int intel_bts_read_finish(struct auxtrace_record *itr, int idx)
411 {
412 	struct intel_bts_recording *btsr =
413 			container_of(itr, struct intel_bts_recording, itr);
414 	struct evsel *evsel;
415 
416 	evlist__for_each_entry(btsr->evlist, evsel) {
417 		if (evsel->core.attr.type == btsr->intel_bts_pmu->type)
418 			return perf_evlist__enable_event_idx(btsr->evlist,
419 							     evsel, idx);
420 	}
421 	return -EINVAL;
422 }
423 
424 struct auxtrace_record *intel_bts_recording_init(int *err)
425 {
426 	struct perf_pmu *intel_bts_pmu = perf_pmu__find(INTEL_BTS_PMU_NAME);
427 	struct intel_bts_recording *btsr;
428 
429 	if (!intel_bts_pmu)
430 		return NULL;
431 
432 	if (setenv("JITDUMP_USE_ARCH_TIMESTAMP", "1", 1)) {
433 		*err = -errno;
434 		return NULL;
435 	}
436 
437 	btsr = zalloc(sizeof(struct intel_bts_recording));
438 	if (!btsr) {
439 		*err = -ENOMEM;
440 		return NULL;
441 	}
442 
443 	btsr->intel_bts_pmu = intel_bts_pmu;
444 	btsr->itr.recording_options = intel_bts_recording_options;
445 	btsr->itr.info_priv_size = intel_bts_info_priv_size;
446 	btsr->itr.info_fill = intel_bts_info_fill;
447 	btsr->itr.free = intel_bts_recording_free;
448 	btsr->itr.snapshot_start = intel_bts_snapshot_start;
449 	btsr->itr.snapshot_finish = intel_bts_snapshot_finish;
450 	btsr->itr.find_snapshot = intel_bts_find_snapshot;
451 	btsr->itr.parse_snapshot_options = intel_bts_parse_snapshot_options;
452 	btsr->itr.reference = intel_bts_reference;
453 	btsr->itr.read_finish = intel_bts_read_finish;
454 	btsr->itr.alignment = sizeof(struct branch);
455 	return &btsr->itr;
456 }
457