2 * intel-bts.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
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/bitops.h>
19 #include <linux/log2.h>
21 #include "../../util/cpumap.h"
22 #include "../../util/evsel.h"
23 #include "../../util/evlist.h"
24 #include "../../util/session.h"
25 #include "../../util/util.h"
26 #include "../../util/pmu.h"
27 #include "../../util/debug.h"
28 #include "../../util/tsc.h"
29 #include "../../util/auxtrace.h"
30 #include "../../util/intel-bts.h"
32 #define KiB(x) ((x) * 1024)
33 #define MiB(x) ((x) * 1024 * 1024)
34 #define KiB_MASK(x) (KiB(x) - 1)
35 #define MiB_MASK(x) (MiB(x) - 1)
37 #define INTEL_BTS_DFLT_SAMPLE_SIZE KiB(4)
39 #define INTEL_BTS_MAX_SAMPLE_SIZE KiB(60)
41 struct intel_bts_snapshot_ref
{
47 struct intel_bts_recording
{
48 struct auxtrace_record itr
;
49 struct perf_pmu
*intel_bts_pmu
;
50 struct perf_evlist
*evlist
;
54 struct intel_bts_snapshot_ref
*snapshot_refs
;
64 intel_bts_info_priv_size(struct auxtrace_record
*itr __maybe_unused
,
65 struct perf_evlist
*evlist __maybe_unused
)
67 return INTEL_BTS_AUXTRACE_PRIV_SIZE
;
70 static int intel_bts_info_fill(struct auxtrace_record
*itr
,
71 struct perf_session
*session
,
72 struct auxtrace_info_event
*auxtrace_info
,
75 struct intel_bts_recording
*btsr
=
76 container_of(itr
, struct intel_bts_recording
, itr
);
77 struct perf_pmu
*intel_bts_pmu
= btsr
->intel_bts_pmu
;
78 struct perf_event_mmap_page
*pc
;
79 struct perf_tsc_conversion tc
= { .time_mult
= 0, };
80 bool cap_user_time_zero
= false;
83 if (priv_size
!= INTEL_BTS_AUXTRACE_PRIV_SIZE
)
86 if (!session
->evlist
->nr_mmaps
)
89 pc
= session
->evlist
->mmap
[0].base
;
91 err
= perf_read_tsc_conversion(pc
, &tc
);
93 if (err
!= -EOPNOTSUPP
)
96 cap_user_time_zero
= tc
.time_mult
!= 0;
98 if (!cap_user_time_zero
)
99 ui__warning("Intel BTS: TSC not available\n");
102 auxtrace_info
->type
= PERF_AUXTRACE_INTEL_BTS
;
103 auxtrace_info
->priv
[INTEL_BTS_PMU_TYPE
] = intel_bts_pmu
->type
;
104 auxtrace_info
->priv
[INTEL_BTS_TIME_SHIFT
] = tc
.time_shift
;
105 auxtrace_info
->priv
[INTEL_BTS_TIME_MULT
] = tc
.time_mult
;
106 auxtrace_info
->priv
[INTEL_BTS_TIME_ZERO
] = tc
.time_zero
;
107 auxtrace_info
->priv
[INTEL_BTS_CAP_USER_TIME_ZERO
] = cap_user_time_zero
;
108 auxtrace_info
->priv
[INTEL_BTS_SNAPSHOT_MODE
] = btsr
->snapshot_mode
;
113 static int intel_bts_recording_options(struct auxtrace_record
*itr
,
114 struct perf_evlist
*evlist
,
115 struct record_opts
*opts
)
117 struct intel_bts_recording
*btsr
=
118 container_of(itr
, struct intel_bts_recording
, itr
);
119 struct perf_pmu
*intel_bts_pmu
= btsr
->intel_bts_pmu
;
120 struct perf_evsel
*evsel
, *intel_bts_evsel
= NULL
;
121 const struct cpu_map
*cpus
= evlist
->cpus
;
122 bool privileged
= geteuid() == 0 || perf_event_paranoid() < 0;
124 btsr
->evlist
= evlist
;
125 btsr
->snapshot_mode
= opts
->auxtrace_snapshot_mode
;
127 evlist__for_each(evlist
, evsel
) {
128 if (evsel
->attr
.type
== intel_bts_pmu
->type
) {
129 if (intel_bts_evsel
) {
130 pr_err("There may be only one " INTEL_BTS_PMU_NAME
" event\n");
133 evsel
->attr
.freq
= 0;
134 evsel
->attr
.sample_period
= 1;
135 intel_bts_evsel
= evsel
;
136 opts
->full_auxtrace
= true;
140 if (opts
->auxtrace_snapshot_mode
&& !opts
->full_auxtrace
) {
141 pr_err("Snapshot mode (-S option) requires " INTEL_BTS_PMU_NAME
" PMU event (-e " INTEL_BTS_PMU_NAME
")\n");
145 if (!opts
->full_auxtrace
)
148 if (opts
->full_auxtrace
&& !cpu_map__empty(cpus
)) {
149 pr_err(INTEL_BTS_PMU_NAME
" does not support per-cpu recording\n");
153 /* Set default sizes for snapshot mode */
154 if (opts
->auxtrace_snapshot_mode
) {
155 if (!opts
->auxtrace_snapshot_size
&& !opts
->auxtrace_mmap_pages
) {
157 opts
->auxtrace_mmap_pages
= MiB(4) / page_size
;
159 opts
->auxtrace_mmap_pages
= KiB(128) / page_size
;
160 if (opts
->mmap_pages
== UINT_MAX
)
161 opts
->mmap_pages
= KiB(256) / page_size
;
163 } else if (!opts
->auxtrace_mmap_pages
&& !privileged
&&
164 opts
->mmap_pages
== UINT_MAX
) {
165 opts
->mmap_pages
= KiB(256) / page_size
;
167 if (!opts
->auxtrace_snapshot_size
)
168 opts
->auxtrace_snapshot_size
=
169 opts
->auxtrace_mmap_pages
* (size_t)page_size
;
170 if (!opts
->auxtrace_mmap_pages
) {
171 size_t sz
= opts
->auxtrace_snapshot_size
;
173 sz
= round_up(sz
, page_size
) / page_size
;
174 opts
->auxtrace_mmap_pages
= roundup_pow_of_two(sz
);
176 if (opts
->auxtrace_snapshot_size
>
177 opts
->auxtrace_mmap_pages
* (size_t)page_size
) {
178 pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
179 opts
->auxtrace_snapshot_size
,
180 opts
->auxtrace_mmap_pages
* (size_t)page_size
);
183 if (!opts
->auxtrace_snapshot_size
|| !opts
->auxtrace_mmap_pages
) {
184 pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
187 pr_debug2("Intel BTS snapshot size: %zu\n",
188 opts
->auxtrace_snapshot_size
);
191 /* Set default sizes for full trace mode */
192 if (opts
->full_auxtrace
&& !opts
->auxtrace_mmap_pages
) {
194 opts
->auxtrace_mmap_pages
= MiB(4) / page_size
;
196 opts
->auxtrace_mmap_pages
= KiB(128) / page_size
;
197 if (opts
->mmap_pages
== UINT_MAX
)
198 opts
->mmap_pages
= KiB(256) / page_size
;
202 /* Validate auxtrace_mmap_pages */
203 if (opts
->auxtrace_mmap_pages
) {
204 size_t sz
= opts
->auxtrace_mmap_pages
* (size_t)page_size
;
207 if (opts
->auxtrace_snapshot_mode
)
212 if (sz
< min_sz
|| !is_power_of_2(sz
)) {
213 pr_err("Invalid mmap size for Intel BTS: must be at least %zuKiB and a power of 2\n",
219 if (intel_bts_evsel
) {
221 * To obtain the auxtrace buffer file descriptor, the auxtrace event
224 perf_evlist__to_front(evlist
, intel_bts_evsel
);
226 * In the case of per-cpu mmaps, we need the CPU on the
229 if (!cpu_map__empty(cpus
))
230 perf_evsel__set_sample_bit(intel_bts_evsel
, CPU
);
233 /* Add dummy event to keep tracking */
234 if (opts
->full_auxtrace
) {
235 struct perf_evsel
*tracking_evsel
;
238 err
= parse_events(evlist
, "dummy:u", NULL
);
242 tracking_evsel
= perf_evlist__last(evlist
);
244 perf_evlist__set_tracking_event(evlist
, tracking_evsel
);
246 tracking_evsel
->attr
.freq
= 0;
247 tracking_evsel
->attr
.sample_period
= 1;
253 static int intel_bts_parse_snapshot_options(struct auxtrace_record
*itr
,
254 struct record_opts
*opts
,
257 struct intel_bts_recording
*btsr
=
258 container_of(itr
, struct intel_bts_recording
, itr
);
259 unsigned long long snapshot_size
= 0;
263 snapshot_size
= strtoull(str
, &endptr
, 0);
264 if (*endptr
|| snapshot_size
> SIZE_MAX
)
268 opts
->auxtrace_snapshot_mode
= true;
269 opts
->auxtrace_snapshot_size
= snapshot_size
;
271 btsr
->snapshot_size
= snapshot_size
;
276 static u64
intel_bts_reference(struct auxtrace_record
*itr __maybe_unused
)
281 static int intel_bts_alloc_snapshot_refs(struct intel_bts_recording
*btsr
,
284 const size_t sz
= sizeof(struct intel_bts_snapshot_ref
);
285 int cnt
= btsr
->snapshot_ref_cnt
, new_cnt
= cnt
* 2;
286 struct intel_bts_snapshot_ref
*refs
;
291 while (new_cnt
<= idx
)
294 refs
= calloc(new_cnt
, sz
);
298 memcpy(refs
, btsr
->snapshot_refs
, cnt
* sz
);
300 btsr
->snapshot_refs
= refs
;
301 btsr
->snapshot_ref_cnt
= new_cnt
;
306 static void intel_bts_free_snapshot_refs(struct intel_bts_recording
*btsr
)
310 for (i
= 0; i
< btsr
->snapshot_ref_cnt
; i
++)
311 zfree(&btsr
->snapshot_refs
[i
].ref_buf
);
312 zfree(&btsr
->snapshot_refs
);
315 static void intel_bts_recording_free(struct auxtrace_record
*itr
)
317 struct intel_bts_recording
*btsr
=
318 container_of(itr
, struct intel_bts_recording
, itr
);
320 intel_bts_free_snapshot_refs(btsr
);
324 static int intel_bts_snapshot_start(struct auxtrace_record
*itr
)
326 struct intel_bts_recording
*btsr
=
327 container_of(itr
, struct intel_bts_recording
, itr
);
328 struct perf_evsel
*evsel
;
330 evlist__for_each(btsr
->evlist
, evsel
) {
331 if (evsel
->attr
.type
== btsr
->intel_bts_pmu
->type
)
332 return perf_evsel__disable(evsel
);
337 static int intel_bts_snapshot_finish(struct auxtrace_record
*itr
)
339 struct intel_bts_recording
*btsr
=
340 container_of(itr
, struct intel_bts_recording
, itr
);
341 struct perf_evsel
*evsel
;
343 evlist__for_each(btsr
->evlist
, evsel
) {
344 if (evsel
->attr
.type
== btsr
->intel_bts_pmu
->type
)
345 return perf_evsel__enable(evsel
);
350 static bool intel_bts_first_wrap(u64
*data
, size_t buf_size
)
359 for (i
= a
; i
< b
; i
++) {
367 static int intel_bts_find_snapshot(struct auxtrace_record
*itr
, int idx
,
368 struct auxtrace_mmap
*mm
, unsigned char *data
,
371 struct intel_bts_recording
*btsr
=
372 container_of(itr
, struct intel_bts_recording
, itr
);
376 pr_debug3("%s: mmap index %d old head %zu new head %zu\n",
377 __func__
, idx
, (size_t)*old
, (size_t)*head
);
379 if (idx
>= btsr
->snapshot_ref_cnt
) {
380 err
= intel_bts_alloc_snapshot_refs(btsr
, idx
);
385 wrapped
= btsr
->snapshot_refs
[idx
].wrapped
;
386 if (!wrapped
&& intel_bts_first_wrap((u64
*)data
, mm
->len
)) {
387 btsr
->snapshot_refs
[idx
].wrapped
= true;
392 * In full trace mode 'head' continually increases. However in snapshot
393 * mode 'head' is an offset within the buffer. Here 'old' and 'head'
394 * are adjusted to match the full trace case which expects that 'old' is
395 * always less than 'head'.
409 pr_debug3("%s: wrap-around %sdetected, adjusted old head %zu adjusted new head %zu\n",
410 __func__
, wrapped
? "" : "not ", (size_t)*old
, (size_t)*head
);
415 pr_err("%s: failed, error %d\n", __func__
, err
);
419 static int intel_bts_read_finish(struct auxtrace_record
*itr
, int idx
)
421 struct intel_bts_recording
*btsr
=
422 container_of(itr
, struct intel_bts_recording
, itr
);
423 struct perf_evsel
*evsel
;
425 evlist__for_each(btsr
->evlist
, evsel
) {
426 if (evsel
->attr
.type
== btsr
->intel_bts_pmu
->type
)
427 return perf_evlist__enable_event_idx(btsr
->evlist
,
433 struct auxtrace_record
*intel_bts_recording_init(int *err
)
435 struct perf_pmu
*intel_bts_pmu
= perf_pmu__find(INTEL_BTS_PMU_NAME
);
436 struct intel_bts_recording
*btsr
;
441 btsr
= zalloc(sizeof(struct intel_bts_recording
));
447 btsr
->intel_bts_pmu
= intel_bts_pmu
;
448 btsr
->itr
.recording_options
= intel_bts_recording_options
;
449 btsr
->itr
.info_priv_size
= intel_bts_info_priv_size
;
450 btsr
->itr
.info_fill
= intel_bts_info_fill
;
451 btsr
->itr
.free
= intel_bts_recording_free
;
452 btsr
->itr
.snapshot_start
= intel_bts_snapshot_start
;
453 btsr
->itr
.snapshot_finish
= intel_bts_snapshot_finish
;
454 btsr
->itr
.find_snapshot
= intel_bts_find_snapshot
;
455 btsr
->itr
.parse_snapshot_options
= intel_bts_parse_snapshot_options
;
456 btsr
->itr
.reference
= intel_bts_reference
;
457 btsr
->itr
.read_finish
= intel_bts_read_finish
;
458 btsr
->itr
.alignment
= sizeof(struct branch
);