Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/tools/perf/arch/x86/util/intel-pt.c
26289 views
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
* intel_pt.c: Intel Processor Trace support
4
* Copyright (c) 2013-2015, Intel Corporation.
5
*/
6
7
#include <errno.h>
8
#include <stdbool.h>
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>
14
#include <linux/err.h>
15
#include <cpuid.h>
16
17
#include "../../../util/session.h"
18
#include "../../../util/event.h"
19
#include "../../../util/evlist.h"
20
#include "../../../util/evsel.h"
21
#include "../../../util/evsel_config.h"
22
#include "../../../util/config.h"
23
#include "../../../util/cpumap.h"
24
#include "../../../util/mmap.h"
25
#include <subcmd/parse-options.h>
26
#include "../../../util/parse-events.h"
27
#include "../../../util/pmus.h"
28
#include "../../../util/debug.h"
29
#include "../../../util/auxtrace.h"
30
#include "../../../util/perf_api_probe.h"
31
#include "../../../util/record.h"
32
#include "../../../util/target.h"
33
#include "../../../util/tsc.h"
34
#include <internal/lib.h> // page_size
35
#include "../../../util/intel-pt.h"
36
#include <api/fs/fs.h>
37
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)
42
43
#define INTEL_PT_PSB_PERIOD_NEAR 256
44
45
struct intel_pt_snapshot_ref {
46
void *ref_buf;
47
size_t ref_offset;
48
bool wrapped;
49
};
50
51
struct intel_pt_recording {
52
struct auxtrace_record itr;
53
struct perf_pmu *intel_pt_pmu;
54
int have_sched_switch;
55
struct evlist *evlist;
56
bool all_switch_events;
57
bool snapshot_mode;
58
bool snapshot_init_done;
59
size_t snapshot_size;
60
size_t snapshot_ref_buf_size;
61
int snapshot_ref_cnt;
62
struct intel_pt_snapshot_ref *snapshot_refs;
63
size_t priv_size;
64
};
65
66
static int intel_pt_parse_terms_with_default(const struct perf_pmu *pmu,
67
const char *str,
68
u64 *config)
69
{
70
struct parse_events_terms terms;
71
struct perf_event_attr attr = { .size = 0, };
72
int err;
73
74
parse_events_terms__init(&terms);
75
err = parse_events_terms(&terms, str, /*input=*/ NULL);
76
if (err)
77
goto out_free;
78
79
attr.config = *config;
80
err = perf_pmu__config_terms(pmu, &attr, &terms, /*zero=*/true, /*apply_hardcoded=*/false,
81
/*err=*/NULL);
82
if (err)
83
goto out_free;
84
85
*config = attr.config;
86
out_free:
87
parse_events_terms__exit(&terms);
88
return err;
89
}
90
91
static int intel_pt_parse_terms(const struct perf_pmu *pmu, const char *str, u64 *config)
92
{
93
*config = 0;
94
return intel_pt_parse_terms_with_default(pmu, str, config);
95
}
96
97
static u64 intel_pt_masked_bits(u64 mask, u64 bits)
98
{
99
const u64 top_bit = 1ULL << 63;
100
u64 res = 0;
101
int i;
102
103
for (i = 0; i < 64; i++) {
104
if (mask & top_bit) {
105
res <<= 1;
106
if (bits & top_bit)
107
res |= 1;
108
}
109
mask <<= 1;
110
bits <<= 1;
111
}
112
113
return res;
114
}
115
116
static int intel_pt_read_config(struct perf_pmu *intel_pt_pmu, const char *str,
117
struct evlist *evlist, u64 *res)
118
{
119
struct evsel *evsel;
120
u64 mask;
121
122
*res = 0;
123
124
mask = perf_pmu__format_bits(intel_pt_pmu, str);
125
if (!mask)
126
return -EINVAL;
127
128
evlist__for_each_entry(evlist, evsel) {
129
if (evsel->core.attr.type == intel_pt_pmu->type) {
130
*res = intel_pt_masked_bits(mask, evsel->core.attr.config);
131
return 0;
132
}
133
}
134
135
return -EINVAL;
136
}
137
138
static size_t intel_pt_psb_period(struct perf_pmu *intel_pt_pmu,
139
struct evlist *evlist)
140
{
141
u64 val;
142
int err, topa_multiple_entries;
143
size_t psb_period;
144
145
if (perf_pmu__scan_file(intel_pt_pmu, "caps/topa_multiple_entries",
146
"%d", &topa_multiple_entries) != 1)
147
topa_multiple_entries = 0;
148
149
/*
150
* Use caps/topa_multiple_entries to indicate early hardware that had
151
* extra frequent PSBs.
152
*/
153
if (!topa_multiple_entries) {
154
psb_period = 256;
155
goto out;
156
}
157
158
err = intel_pt_read_config(intel_pt_pmu, "psb_period", evlist, &val);
159
if (err)
160
val = 0;
161
162
psb_period = 1 << (val + 11);
163
out:
164
pr_debug2("%s psb_period %zu\n", intel_pt_pmu->name, psb_period);
165
return psb_period;
166
}
167
168
static int intel_pt_pick_bit(int bits, int target)
169
{
170
int pos, pick = -1;
171
172
for (pos = 0; bits; bits >>= 1, pos++) {
173
if (bits & 1) {
174
if (pos <= target || pick < 0)
175
pick = pos;
176
if (pos >= target)
177
break;
178
}
179
}
180
181
return pick;
182
}
183
184
static u64 intel_pt_default_config(const struct perf_pmu *intel_pt_pmu)
185
{
186
char buf[256];
187
int mtc, mtc_periods = 0, mtc_period;
188
int psb_cyc, psb_periods, psb_period;
189
int pos = 0;
190
u64 config;
191
char c;
192
int dirfd;
193
194
dirfd = perf_pmu__event_source_devices_fd();
195
196
pos += scnprintf(buf + pos, sizeof(buf) - pos, "tsc");
197
198
if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "caps/mtc", "%d",
199
&mtc) != 1)
200
mtc = 1;
201
202
if (mtc) {
203
if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "caps/mtc_periods", "%x",
204
&mtc_periods) != 1)
205
mtc_periods = 0;
206
if (mtc_periods) {
207
mtc_period = intel_pt_pick_bit(mtc_periods, 3);
208
pos += scnprintf(buf + pos, sizeof(buf) - pos,
209
",mtc,mtc_period=%d", mtc_period);
210
}
211
}
212
213
if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "caps/psb_cyc", "%d",
214
&psb_cyc) != 1)
215
psb_cyc = 1;
216
217
if (psb_cyc && mtc_periods) {
218
if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "caps/psb_periods", "%x",
219
&psb_periods) != 1)
220
psb_periods = 0;
221
if (psb_periods) {
222
psb_period = intel_pt_pick_bit(psb_periods, 3);
223
pos += scnprintf(buf + pos, sizeof(buf) - pos,
224
",psb_period=%d", psb_period);
225
}
226
}
227
228
if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "format/pt", "%c", &c) == 1 &&
229
perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "format/branch", "%c", &c) == 1)
230
pos += scnprintf(buf + pos, sizeof(buf) - pos, ",pt,branch");
231
232
pr_debug2("%s default config: %s\n", intel_pt_pmu->name, buf);
233
234
intel_pt_parse_terms(intel_pt_pmu, buf, &config);
235
236
close(dirfd);
237
return config;
238
}
239
240
static int intel_pt_parse_snapshot_options(struct auxtrace_record *itr,
241
struct record_opts *opts,
242
const char *str)
243
{
244
struct intel_pt_recording *ptr =
245
container_of(itr, struct intel_pt_recording, itr);
246
unsigned long long snapshot_size = 0;
247
char *endptr;
248
249
if (str) {
250
snapshot_size = strtoull(str, &endptr, 0);
251
if (*endptr || snapshot_size > SIZE_MAX)
252
return -1;
253
}
254
255
opts->auxtrace_snapshot_mode = true;
256
opts->auxtrace_snapshot_size = snapshot_size;
257
258
ptr->snapshot_size = snapshot_size;
259
260
return 0;
261
}
262
263
void intel_pt_pmu_default_config(const struct perf_pmu *intel_pt_pmu,
264
struct perf_event_attr *attr)
265
{
266
static u64 config;
267
static bool initialized;
268
269
if (!initialized) {
270
config = intel_pt_default_config(intel_pt_pmu);
271
initialized = true;
272
}
273
attr->config = config;
274
}
275
276
static const char *intel_pt_find_filter(struct evlist *evlist,
277
struct perf_pmu *intel_pt_pmu)
278
{
279
struct evsel *evsel;
280
281
evlist__for_each_entry(evlist, evsel) {
282
if (evsel->core.attr.type == intel_pt_pmu->type)
283
return evsel->filter;
284
}
285
286
return NULL;
287
}
288
289
static size_t intel_pt_filter_bytes(const char *filter)
290
{
291
size_t len = filter ? strlen(filter) : 0;
292
293
return len ? roundup(len + 1, 8) : 0;
294
}
295
296
static size_t
297
intel_pt_info_priv_size(struct auxtrace_record *itr, struct evlist *evlist)
298
{
299
struct intel_pt_recording *ptr =
300
container_of(itr, struct intel_pt_recording, itr);
301
const char *filter = intel_pt_find_filter(evlist, ptr->intel_pt_pmu);
302
303
ptr->priv_size = (INTEL_PT_AUXTRACE_PRIV_MAX * sizeof(u64)) +
304
intel_pt_filter_bytes(filter);
305
ptr->priv_size += sizeof(u64); /* Cap Event Trace */
306
307
return ptr->priv_size;
308
}
309
310
static void intel_pt_tsc_ctc_ratio(u32 *n, u32 *d)
311
{
312
unsigned int eax = 0, ebx = 0, ecx = 0, edx = 0;
313
314
__get_cpuid(0x15, &eax, &ebx, &ecx, &edx);
315
*n = ebx;
316
*d = eax;
317
}
318
319
static int intel_pt_info_fill(struct auxtrace_record *itr,
320
struct perf_session *session,
321
struct perf_record_auxtrace_info *auxtrace_info,
322
size_t priv_size)
323
{
324
struct intel_pt_recording *ptr =
325
container_of(itr, struct intel_pt_recording, itr);
326
struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;
327
struct perf_event_mmap_page *pc;
328
struct perf_tsc_conversion tc = { .time_mult = 0, };
329
bool cap_user_time_zero = false, per_cpu_mmaps;
330
u64 tsc_bit, mtc_bit, mtc_freq_bits, cyc_bit, noretcomp_bit;
331
u32 tsc_ctc_ratio_n, tsc_ctc_ratio_d;
332
unsigned long max_non_turbo_ratio;
333
size_t filter_str_len;
334
const char *filter;
335
int event_trace;
336
__u64 *info;
337
int err;
338
339
if (priv_size != ptr->priv_size)
340
return -EINVAL;
341
342
intel_pt_parse_terms(intel_pt_pmu, "tsc", &tsc_bit);
343
intel_pt_parse_terms(intel_pt_pmu, "noretcomp", &noretcomp_bit);
344
intel_pt_parse_terms(intel_pt_pmu, "mtc", &mtc_bit);
345
mtc_freq_bits = perf_pmu__format_bits(intel_pt_pmu, "mtc_period");
346
intel_pt_parse_terms(intel_pt_pmu, "cyc", &cyc_bit);
347
348
intel_pt_tsc_ctc_ratio(&tsc_ctc_ratio_n, &tsc_ctc_ratio_d);
349
350
if (perf_pmu__scan_file(intel_pt_pmu, "max_nonturbo_ratio",
351
"%lu", &max_non_turbo_ratio) != 1)
352
max_non_turbo_ratio = 0;
353
if (perf_pmu__scan_file(intel_pt_pmu, "caps/event_trace",
354
"%d", &event_trace) != 1)
355
event_trace = 0;
356
357
filter = intel_pt_find_filter(session->evlist, ptr->intel_pt_pmu);
358
filter_str_len = filter ? strlen(filter) : 0;
359
360
if (!session->evlist->core.nr_mmaps)
361
return -EINVAL;
362
363
pc = session->evlist->mmap[0].core.base;
364
if (pc) {
365
err = perf_read_tsc_conversion(pc, &tc);
366
if (err) {
367
if (err != -EOPNOTSUPP)
368
return err;
369
} else {
370
cap_user_time_zero = tc.time_mult != 0;
371
}
372
if (!cap_user_time_zero)
373
ui__warning("Intel Processor Trace: TSC not available\n");
374
}
375
376
per_cpu_mmaps = !perf_cpu_map__is_any_cpu_or_is_empty(session->evlist->core.user_requested_cpus);
377
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;
396
397
info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
398
399
if (filter_str_len) {
400
size_t len = intel_pt_filter_bytes(filter);
401
402
strncpy((char *)info, filter, len);
403
info += len >> 3;
404
}
405
406
*info++ = event_trace;
407
408
return 0;
409
}
410
411
#ifdef HAVE_LIBTRACEEVENT
412
static int intel_pt_track_switches(struct evlist *evlist)
413
{
414
const char *sched_switch = "sched:sched_switch";
415
struct evsel *evsel;
416
int err;
417
418
if (!evlist__can_select_event(evlist, sched_switch))
419
return -EPERM;
420
421
evsel = evlist__add_sched_switch(evlist, true);
422
if (IS_ERR(evsel)) {
423
err = PTR_ERR(evsel);
424
pr_debug2("%s: failed to create %s, error = %d\n",
425
__func__, sched_switch, err);
426
return err;
427
}
428
429
evsel->immediate = true;
430
431
return 0;
432
}
433
#endif
434
435
static bool intel_pt_exclude_guest(void)
436
{
437
int pt_mode;
438
439
if (sysfs__read_int("module/kvm_intel/parameters/pt_mode", &pt_mode))
440
pt_mode = 0;
441
442
return pt_mode == 1;
443
}
444
445
static void intel_pt_valid_str(char *str, size_t len, u64 valid)
446
{
447
unsigned int val, last = 0, state = 1;
448
int p = 0;
449
450
str[0] = '\0';
451
452
for (val = 0; val <= 64; val++, valid >>= 1) {
453
if (valid & 1) {
454
last = val;
455
switch (state) {
456
case 0:
457
p += scnprintf(str + p, len - p, ",");
458
/* Fall through */
459
case 1:
460
p += scnprintf(str + p, len - p, "%u", val);
461
state = 2;
462
break;
463
case 2:
464
state = 3;
465
break;
466
case 3:
467
state = 4;
468
break;
469
default:
470
break;
471
}
472
} else {
473
switch (state) {
474
case 3:
475
p += scnprintf(str + p, len - p, ",%u", last);
476
state = 0;
477
break;
478
case 4:
479
p += scnprintf(str + p, len - p, "-%u", last);
480
state = 0;
481
break;
482
default:
483
break;
484
}
485
if (state != 1)
486
state = 0;
487
}
488
}
489
}
490
491
static int intel_pt_val_config_term(struct perf_pmu *intel_pt_pmu, int dirfd,
492
const char *caps, const char *name,
493
const char *supported, u64 config)
494
{
495
char valid_str[256];
496
unsigned int shift;
497
unsigned long long valid;
498
u64 bits;
499
int ok;
500
501
if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, caps, "%llx", &valid) != 1)
502
valid = 0;
503
504
if (supported &&
505
perf_pmu__scan_file_at(intel_pt_pmu, dirfd, supported, "%d", &ok) == 1 && !ok)
506
valid = 0;
507
508
valid |= 1;
509
510
bits = perf_pmu__format_bits(intel_pt_pmu, name);
511
512
config &= bits;
513
514
for (shift = 0; bits && !(bits & 1); shift++)
515
bits >>= 1;
516
517
config >>= shift;
518
519
if (config > 63)
520
goto out_err;
521
522
if (valid & (1 << config))
523
return 0;
524
out_err:
525
intel_pt_valid_str(valid_str, sizeof(valid_str), valid);
526
pr_err("Invalid %s for %s. Valid values are: %s\n",
527
name, INTEL_PT_PMU_NAME, valid_str);
528
return -EINVAL;
529
}
530
531
static int intel_pt_validate_config(struct perf_pmu *intel_pt_pmu,
532
struct evsel *evsel)
533
{
534
int err, dirfd;
535
char c;
536
537
if (!evsel)
538
return 0;
539
540
dirfd = perf_pmu__event_source_devices_fd();
541
if (dirfd < 0)
542
return dirfd;
543
544
/*
545
* If supported, force pass-through config term (pt=1) even if user
546
* sets pt=0, which avoids senseless kernel errors.
547
*/
548
if (perf_pmu__scan_file_at(intel_pt_pmu, dirfd, "format/pt", "%c", &c) == 1 &&
549
!(evsel->core.attr.config & 1)) {
550
pr_warning("pt=0 doesn't make sense, forcing pt=1\n");
551
evsel->core.attr.config |= 1;
552
}
553
554
err = intel_pt_val_config_term(intel_pt_pmu, dirfd, "caps/cycle_thresholds",
555
"cyc_thresh", "caps/psb_cyc",
556
evsel->core.attr.config);
557
if (err)
558
goto out;
559
560
err = intel_pt_val_config_term(intel_pt_pmu, dirfd, "caps/mtc_periods",
561
"mtc_period", "caps/mtc",
562
evsel->core.attr.config);
563
if (err)
564
goto out;
565
566
err = intel_pt_val_config_term(intel_pt_pmu, dirfd, "caps/psb_periods",
567
"psb_period", "caps/psb_cyc",
568
evsel->core.attr.config);
569
570
out:
571
close(dirfd);
572
return err;
573
}
574
575
static void intel_pt_min_max_sample_sz(struct evlist *evlist,
576
size_t *min_sz, size_t *max_sz)
577
{
578
struct evsel *evsel;
579
580
evlist__for_each_entry(evlist, evsel) {
581
size_t sz = evsel->core.attr.aux_sample_size;
582
583
if (!sz)
584
continue;
585
if (min_sz && (sz < *min_sz || !*min_sz))
586
*min_sz = sz;
587
if (max_sz && sz > *max_sz)
588
*max_sz = sz;
589
}
590
}
591
592
/*
593
* Currently, there is not enough information to disambiguate different PEBS
594
* events, so only allow one.
595
*/
596
static bool intel_pt_too_many_aux_output(struct evlist *evlist)
597
{
598
struct evsel *evsel;
599
int aux_output_cnt = 0;
600
601
evlist__for_each_entry(evlist, evsel)
602
aux_output_cnt += !!evsel->core.attr.aux_output;
603
604
if (aux_output_cnt > 1) {
605
pr_err(INTEL_PT_PMU_NAME " supports at most one event with aux-output\n");
606
return true;
607
}
608
609
return false;
610
}
611
612
static int intel_pt_recording_options(struct auxtrace_record *itr,
613
struct evlist *evlist,
614
struct record_opts *opts)
615
{
616
struct intel_pt_recording *ptr =
617
container_of(itr, struct intel_pt_recording, itr);
618
struct perf_pmu *intel_pt_pmu = ptr->intel_pt_pmu;
619
bool have_timing_info, need_immediate = false;
620
struct evsel *evsel, *intel_pt_evsel = NULL;
621
const struct perf_cpu_map *cpus = evlist->core.user_requested_cpus;
622
bool privileged = perf_event_paranoid_check(-1);
623
u64 tsc_bit;
624
int err;
625
626
ptr->evlist = evlist;
627
ptr->snapshot_mode = opts->auxtrace_snapshot_mode;
628
629
evlist__for_each_entry(evlist, evsel) {
630
if (evsel->core.attr.type == intel_pt_pmu->type) {
631
if (intel_pt_evsel) {
632
pr_err("There may be only one " INTEL_PT_PMU_NAME " event\n");
633
return -EINVAL;
634
}
635
evsel->core.attr.freq = 0;
636
evsel->core.attr.sample_period = 1;
637
evsel->core.attr.exclude_guest = intel_pt_exclude_guest();
638
evsel->no_aux_samples = true;
639
evsel->needs_auxtrace_mmap = true;
640
intel_pt_evsel = evsel;
641
opts->full_auxtrace = true;
642
}
643
}
644
645
if (opts->auxtrace_snapshot_mode && !opts->full_auxtrace) {
646
pr_err("Snapshot mode (-S option) requires " INTEL_PT_PMU_NAME " PMU event (-e " INTEL_PT_PMU_NAME ")\n");
647
return -EINVAL;
648
}
649
650
if (opts->auxtrace_snapshot_mode && opts->auxtrace_sample_mode) {
651
pr_err("Snapshot mode (" INTEL_PT_PMU_NAME " PMU) and sample trace cannot be used together\n");
652
return -EINVAL;
653
}
654
655
if (opts->use_clockid) {
656
pr_err("Cannot use clockid (-k option) with " INTEL_PT_PMU_NAME "\n");
657
return -EINVAL;
658
}
659
660
if (intel_pt_too_many_aux_output(evlist))
661
return -EINVAL;
662
663
if (!opts->full_auxtrace)
664
return 0;
665
666
if (opts->auxtrace_sample_mode)
667
evsel__set_config_if_unset(intel_pt_pmu, intel_pt_evsel,
668
"psb_period", 0);
669
670
err = intel_pt_validate_config(intel_pt_pmu, intel_pt_evsel);
671
if (err)
672
return err;
673
674
/* Set default sizes for snapshot mode */
675
if (opts->auxtrace_snapshot_mode) {
676
size_t psb_period = intel_pt_psb_period(intel_pt_pmu, evlist);
677
678
if (!opts->auxtrace_snapshot_size && !opts->auxtrace_mmap_pages) {
679
if (privileged) {
680
opts->auxtrace_mmap_pages = MiB(4) / page_size;
681
} else {
682
opts->auxtrace_mmap_pages = KiB(128) / page_size;
683
if (opts->mmap_pages == UINT_MAX)
684
opts->mmap_pages = KiB(256) / page_size;
685
}
686
} else if (!opts->auxtrace_mmap_pages && !privileged &&
687
opts->mmap_pages == UINT_MAX) {
688
opts->mmap_pages = KiB(256) / page_size;
689
}
690
if (!opts->auxtrace_snapshot_size)
691
opts->auxtrace_snapshot_size =
692
opts->auxtrace_mmap_pages * (size_t)page_size;
693
if (!opts->auxtrace_mmap_pages) {
694
size_t sz = opts->auxtrace_snapshot_size;
695
696
sz = round_up(sz, page_size) / page_size;
697
opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
698
}
699
if (opts->auxtrace_snapshot_size >
700
opts->auxtrace_mmap_pages * (size_t)page_size) {
701
pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
702
opts->auxtrace_snapshot_size,
703
opts->auxtrace_mmap_pages * (size_t)page_size);
704
return -EINVAL;
705
}
706
if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages) {
707
pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
708
return -EINVAL;
709
}
710
pr_debug2("Intel PT snapshot size: %zu\n",
711
opts->auxtrace_snapshot_size);
712
if (psb_period &&
713
opts->auxtrace_snapshot_size <= psb_period +
714
INTEL_PT_PSB_PERIOD_NEAR)
715
ui__warning("Intel PT snapshot size (%zu) may be too small for PSB period (%zu)\n",
716
opts->auxtrace_snapshot_size, psb_period);
717
}
718
719
/* Set default sizes for sample mode */
720
if (opts->auxtrace_sample_mode) {
721
size_t psb_period = intel_pt_psb_period(intel_pt_pmu, evlist);
722
size_t min_sz = 0, max_sz = 0;
723
724
intel_pt_min_max_sample_sz(evlist, &min_sz, &max_sz);
725
if (!opts->auxtrace_mmap_pages && !privileged &&
726
opts->mmap_pages == UINT_MAX)
727
opts->mmap_pages = KiB(256) / page_size;
728
if (!opts->auxtrace_mmap_pages) {
729
size_t sz = round_up(max_sz, page_size) / page_size;
730
731
opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
732
}
733
if (max_sz > opts->auxtrace_mmap_pages * (size_t)page_size) {
734
pr_err("Sample size %zu must not be greater than AUX area tracing mmap size %zu\n",
735
max_sz,
736
opts->auxtrace_mmap_pages * (size_t)page_size);
737
return -EINVAL;
738
}
739
pr_debug2("Intel PT min. sample size: %zu max. sample size: %zu\n",
740
min_sz, max_sz);
741
if (psb_period &&
742
min_sz <= psb_period + INTEL_PT_PSB_PERIOD_NEAR)
743
ui__warning("Intel PT sample size (%zu) may be too small for PSB period (%zu)\n",
744
min_sz, psb_period);
745
}
746
747
/* Set default sizes for full trace mode */
748
if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
749
if (privileged) {
750
opts->auxtrace_mmap_pages = MiB(4) / page_size;
751
} else {
752
opts->auxtrace_mmap_pages = KiB(128) / page_size;
753
if (opts->mmap_pages == UINT_MAX)
754
opts->mmap_pages = KiB(256) / page_size;
755
}
756
}
757
758
/* Validate auxtrace_mmap_pages */
759
if (opts->auxtrace_mmap_pages) {
760
size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
761
size_t min_sz;
762
763
if (opts->auxtrace_snapshot_mode || opts->auxtrace_sample_mode)
764
min_sz = KiB(4);
765
else
766
min_sz = KiB(8);
767
768
if (sz < min_sz || !is_power_of_2(sz)) {
769
pr_err("Invalid mmap size for Intel Processor Trace: must be at least %zuKiB and a power of 2\n",
770
min_sz / 1024);
771
return -EINVAL;
772
}
773
}
774
775
if (!opts->auxtrace_snapshot_mode && !opts->auxtrace_sample_mode) {
776
size_t aw = opts->auxtrace_mmap_pages * (size_t)page_size / 4;
777
u32 aux_watermark = aw > UINT_MAX ? UINT_MAX : aw;
778
779
intel_pt_evsel->core.attr.aux_watermark = aux_watermark;
780
}
781
782
intel_pt_parse_terms(intel_pt_pmu, "tsc", &tsc_bit);
783
784
if (opts->full_auxtrace && (intel_pt_evsel->core.attr.config & tsc_bit))
785
have_timing_info = true;
786
else
787
have_timing_info = false;
788
789
/*
790
* Per-cpu recording needs sched_switch events to distinguish different
791
* threads.
792
*/
793
if (have_timing_info && !perf_cpu_map__is_any_cpu_or_is_empty(cpus) &&
794
!record_opts__no_switch_events(opts)) {
795
if (perf_can_record_switch_events()) {
796
bool cpu_wide = !target__none(&opts->target) &&
797
!target__has_task(&opts->target);
798
799
if (ptr->all_switch_events && !cpu_wide && perf_can_record_cpu_wide()) {
800
struct evsel *switch_evsel;
801
802
switch_evsel = evlist__add_dummy_on_all_cpus(evlist);
803
if (!switch_evsel)
804
return -ENOMEM;
805
806
switch_evsel->core.attr.context_switch = 1;
807
switch_evsel->immediate = true;
808
809
evsel__set_sample_bit(switch_evsel, TID);
810
evsel__set_sample_bit(switch_evsel, TIME);
811
evsel__set_sample_bit(switch_evsel, CPU);
812
evsel__reset_sample_bit(switch_evsel, BRANCH_STACK);
813
814
opts->record_switch_events = false;
815
ptr->have_sched_switch = 3;
816
} else {
817
opts->record_switch_events = true;
818
need_immediate = true;
819
if (cpu_wide)
820
ptr->have_sched_switch = 3;
821
else
822
ptr->have_sched_switch = 2;
823
}
824
} else {
825
#ifdef HAVE_LIBTRACEEVENT
826
err = intel_pt_track_switches(evlist);
827
if (err == -EPERM)
828
pr_debug2("Unable to select sched:sched_switch\n");
829
else if (err)
830
return err;
831
else
832
ptr->have_sched_switch = 1;
833
#endif
834
}
835
}
836
837
if (have_timing_info && !intel_pt_evsel->core.attr.exclude_kernel &&
838
perf_can_record_text_poke_events() && perf_can_record_cpu_wide())
839
opts->text_poke = true;
840
841
if (intel_pt_evsel) {
842
/*
843
* To obtain the auxtrace buffer file descriptor, the auxtrace
844
* event must come first.
845
*/
846
evlist__to_front(evlist, intel_pt_evsel);
847
/*
848
* In the case of per-cpu mmaps, we need the CPU on the
849
* AUX event.
850
*/
851
if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus))
852
evsel__set_sample_bit(intel_pt_evsel, CPU);
853
}
854
855
/* Add dummy event to keep tracking */
856
if (opts->full_auxtrace) {
857
bool need_system_wide_tracking;
858
struct evsel *tracking_evsel;
859
860
/*
861
* User space tasks can migrate between CPUs, so when tracing
862
* selected CPUs, sideband for all CPUs is still needed.
863
*/
864
need_system_wide_tracking = opts->target.cpu_list &&
865
!intel_pt_evsel->core.attr.exclude_user;
866
867
tracking_evsel = evlist__add_aux_dummy(evlist, need_system_wide_tracking);
868
if (!tracking_evsel)
869
return -ENOMEM;
870
871
evlist__set_tracking_event(evlist, tracking_evsel);
872
873
if (need_immediate)
874
tracking_evsel->immediate = true;
875
876
/* In per-cpu case, always need the time of mmap events etc */
877
if (!perf_cpu_map__is_any_cpu_or_is_empty(cpus)) {
878
evsel__set_sample_bit(tracking_evsel, TIME);
879
/* And the CPU for switch events */
880
evsel__set_sample_bit(tracking_evsel, CPU);
881
}
882
evsel__reset_sample_bit(tracking_evsel, BRANCH_STACK);
883
}
884
885
/*
886
* Warn the user when we do not have enough information to decode i.e.
887
* per-cpu with no sched_switch (except workload-only).
888
*/
889
if (!ptr->have_sched_switch && !perf_cpu_map__is_any_cpu_or_is_empty(cpus) &&
890
!target__none(&opts->target) &&
891
!intel_pt_evsel->core.attr.exclude_user)
892
ui__warning("Intel Processor Trace decoding will not be possible except for kernel tracing!\n");
893
894
return 0;
895
}
896
897
static int intel_pt_snapshot_start(struct auxtrace_record *itr)
898
{
899
struct intel_pt_recording *ptr =
900
container_of(itr, struct intel_pt_recording, itr);
901
struct evsel *evsel;
902
903
evlist__for_each_entry(ptr->evlist, evsel) {
904
if (evsel->core.attr.type == ptr->intel_pt_pmu->type)
905
return evsel__disable(evsel);
906
}
907
return -EINVAL;
908
}
909
910
static int intel_pt_snapshot_finish(struct auxtrace_record *itr)
911
{
912
struct intel_pt_recording *ptr =
913
container_of(itr, struct intel_pt_recording, itr);
914
struct evsel *evsel;
915
916
evlist__for_each_entry(ptr->evlist, evsel) {
917
if (evsel->core.attr.type == ptr->intel_pt_pmu->type)
918
return evsel__enable(evsel);
919
}
920
return -EINVAL;
921
}
922
923
static int intel_pt_alloc_snapshot_refs(struct intel_pt_recording *ptr, int idx)
924
{
925
const size_t sz = sizeof(struct intel_pt_snapshot_ref);
926
int cnt = ptr->snapshot_ref_cnt, new_cnt = cnt * 2;
927
struct intel_pt_snapshot_ref *refs;
928
929
if (!new_cnt)
930
new_cnt = 16;
931
932
while (new_cnt <= idx)
933
new_cnt *= 2;
934
935
refs = calloc(new_cnt, sz);
936
if (!refs)
937
return -ENOMEM;
938
939
memcpy(refs, ptr->snapshot_refs, cnt * sz);
940
941
ptr->snapshot_refs = refs;
942
ptr->snapshot_ref_cnt = new_cnt;
943
944
return 0;
945
}
946
947
static void intel_pt_free_snapshot_refs(struct intel_pt_recording *ptr)
948
{
949
int i;
950
951
for (i = 0; i < ptr->snapshot_ref_cnt; i++)
952
zfree(&ptr->snapshot_refs[i].ref_buf);
953
zfree(&ptr->snapshot_refs);
954
}
955
956
static void intel_pt_recording_free(struct auxtrace_record *itr)
957
{
958
struct intel_pt_recording *ptr =
959
container_of(itr, struct intel_pt_recording, itr);
960
961
intel_pt_free_snapshot_refs(ptr);
962
free(ptr);
963
}
964
965
static int intel_pt_alloc_snapshot_ref(struct intel_pt_recording *ptr, int idx,
966
size_t snapshot_buf_size)
967
{
968
size_t ref_buf_size = ptr->snapshot_ref_buf_size;
969
void *ref_buf;
970
971
ref_buf = zalloc(ref_buf_size);
972
if (!ref_buf)
973
return -ENOMEM;
974
975
ptr->snapshot_refs[idx].ref_buf = ref_buf;
976
ptr->snapshot_refs[idx].ref_offset = snapshot_buf_size - ref_buf_size;
977
978
return 0;
979
}
980
981
static size_t intel_pt_snapshot_ref_buf_size(struct intel_pt_recording *ptr,
982
size_t snapshot_buf_size)
983
{
984
const size_t max_size = 256 * 1024;
985
size_t buf_size = 0, psb_period;
986
987
if (ptr->snapshot_size <= 64 * 1024)
988
return 0;
989
990
psb_period = intel_pt_psb_period(ptr->intel_pt_pmu, ptr->evlist);
991
if (psb_period)
992
buf_size = psb_period * 2;
993
994
if (!buf_size || buf_size > max_size)
995
buf_size = max_size;
996
997
if (buf_size >= snapshot_buf_size)
998
return 0;
999
1000
if (buf_size >= ptr->snapshot_size / 2)
1001
return 0;
1002
1003
return buf_size;
1004
}
1005
1006
static int intel_pt_snapshot_init(struct intel_pt_recording *ptr,
1007
size_t snapshot_buf_size)
1008
{
1009
if (ptr->snapshot_init_done)
1010
return 0;
1011
1012
ptr->snapshot_init_done = true;
1013
1014
ptr->snapshot_ref_buf_size = intel_pt_snapshot_ref_buf_size(ptr,
1015
snapshot_buf_size);
1016
1017
return 0;
1018
}
1019
1020
/**
1021
* intel_pt_compare_buffers - compare bytes in a buffer to a circular buffer.
1022
* @buf1: first buffer
1023
* @compare_size: number of bytes to compare
1024
* @buf2: second buffer (a circular buffer)
1025
* @offs2: offset in second buffer
1026
* @buf2_size: size of second buffer
1027
*
1028
* The comparison allows for the possibility that the bytes to compare in the
1029
* circular buffer are not contiguous. It is assumed that @compare_size <=
1030
* @buf2_size. This function returns %false if the bytes are identical, %true
1031
* otherwise.
1032
*/
1033
static bool intel_pt_compare_buffers(void *buf1, size_t compare_size,
1034
void *buf2, size_t offs2, size_t buf2_size)
1035
{
1036
size_t end2 = offs2 + compare_size, part_size;
1037
1038
if (end2 <= buf2_size)
1039
return memcmp(buf1, buf2 + offs2, compare_size);
1040
1041
part_size = end2 - buf2_size;
1042
if (memcmp(buf1, buf2 + offs2, part_size))
1043
return true;
1044
1045
compare_size -= part_size;
1046
1047
return memcmp(buf1 + part_size, buf2, compare_size);
1048
}
1049
1050
static bool intel_pt_compare_ref(void *ref_buf, size_t ref_offset,
1051
size_t ref_size, size_t buf_size,
1052
void *data, size_t head)
1053
{
1054
size_t ref_end = ref_offset + ref_size;
1055
1056
if (ref_end > buf_size) {
1057
if (head > ref_offset || head < ref_end - buf_size)
1058
return true;
1059
} else if (head > ref_offset && head < ref_end) {
1060
return true;
1061
}
1062
1063
return intel_pt_compare_buffers(ref_buf, ref_size, data, ref_offset,
1064
buf_size);
1065
}
1066
1067
static void intel_pt_copy_ref(void *ref_buf, size_t ref_size, size_t buf_size,
1068
void *data, size_t head)
1069
{
1070
if (head >= ref_size) {
1071
memcpy(ref_buf, data + head - ref_size, ref_size);
1072
} else {
1073
memcpy(ref_buf, data, head);
1074
ref_size -= head;
1075
memcpy(ref_buf + head, data + buf_size - ref_size, ref_size);
1076
}
1077
}
1078
1079
static bool intel_pt_wrapped(struct intel_pt_recording *ptr, int idx,
1080
struct auxtrace_mmap *mm, unsigned char *data,
1081
u64 head)
1082
{
1083
struct intel_pt_snapshot_ref *ref = &ptr->snapshot_refs[idx];
1084
bool wrapped;
1085
1086
wrapped = intel_pt_compare_ref(ref->ref_buf, ref->ref_offset,
1087
ptr->snapshot_ref_buf_size, mm->len,
1088
data, head);
1089
1090
intel_pt_copy_ref(ref->ref_buf, ptr->snapshot_ref_buf_size, mm->len,
1091
data, head);
1092
1093
return wrapped;
1094
}
1095
1096
static bool intel_pt_first_wrap(u64 *data, size_t buf_size)
1097
{
1098
int i, a, b;
1099
1100
b = buf_size >> 3;
1101
a = b - 512;
1102
if (a < 0)
1103
a = 0;
1104
1105
for (i = a; i < b; i++) {
1106
if (data[i])
1107
return true;
1108
}
1109
1110
return false;
1111
}
1112
1113
static int intel_pt_find_snapshot(struct auxtrace_record *itr, int idx,
1114
struct auxtrace_mmap *mm, unsigned char *data,
1115
u64 *head, u64 *old)
1116
{
1117
struct intel_pt_recording *ptr =
1118
container_of(itr, struct intel_pt_recording, itr);
1119
bool wrapped;
1120
int err;
1121
1122
pr_debug3("%s: mmap index %d old head %zu new head %zu\n",
1123
__func__, idx, (size_t)*old, (size_t)*head);
1124
1125
err = intel_pt_snapshot_init(ptr, mm->len);
1126
if (err)
1127
goto out_err;
1128
1129
if (idx >= ptr->snapshot_ref_cnt) {
1130
err = intel_pt_alloc_snapshot_refs(ptr, idx);
1131
if (err)
1132
goto out_err;
1133
}
1134
1135
if (ptr->snapshot_ref_buf_size) {
1136
if (!ptr->snapshot_refs[idx].ref_buf) {
1137
err = intel_pt_alloc_snapshot_ref(ptr, idx, mm->len);
1138
if (err)
1139
goto out_err;
1140
}
1141
wrapped = intel_pt_wrapped(ptr, idx, mm, data, *head);
1142
} else {
1143
wrapped = ptr->snapshot_refs[idx].wrapped;
1144
if (!wrapped && intel_pt_first_wrap((u64 *)data, mm->len)) {
1145
ptr->snapshot_refs[idx].wrapped = true;
1146
wrapped = true;
1147
}
1148
}
1149
1150
/*
1151
* In full trace mode 'head' continually increases. However in snapshot
1152
* mode 'head' is an offset within the buffer. Here 'old' and 'head'
1153
* are adjusted to match the full trace case which expects that 'old' is
1154
* always less than 'head'.
1155
*/
1156
if (wrapped) {
1157
*old = *head;
1158
*head += mm->len;
1159
} else {
1160
if (mm->mask)
1161
*old &= mm->mask;
1162
else
1163
*old %= mm->len;
1164
if (*old > *head)
1165
*head += mm->len;
1166
}
1167
1168
pr_debug3("%s: wrap-around %sdetected, adjusted old head %zu adjusted new head %zu\n",
1169
__func__, wrapped ? "" : "not ", (size_t)*old, (size_t)*head);
1170
1171
return 0;
1172
1173
out_err:
1174
pr_err("%s: failed, error %d\n", __func__, err);
1175
return err;
1176
}
1177
1178
static u64 intel_pt_reference(struct auxtrace_record *itr __maybe_unused)
1179
{
1180
return rdtsc();
1181
}
1182
1183
static int intel_pt_perf_config(const char *var, const char *value, void *data)
1184
{
1185
struct intel_pt_recording *ptr = data;
1186
1187
if (!strcmp(var, "intel-pt.all-switch-events"))
1188
ptr->all_switch_events = perf_config_bool(var, value);
1189
1190
return 0;
1191
}
1192
1193
struct auxtrace_record *intel_pt_recording_init(int *err)
1194
{
1195
struct perf_pmu *intel_pt_pmu = perf_pmus__find(INTEL_PT_PMU_NAME);
1196
struct intel_pt_recording *ptr;
1197
1198
if (!intel_pt_pmu)
1199
return NULL;
1200
1201
if (setenv("JITDUMP_USE_ARCH_TIMESTAMP", "1", 1)) {
1202
*err = -errno;
1203
return NULL;
1204
}
1205
1206
ptr = zalloc(sizeof(struct intel_pt_recording));
1207
if (!ptr) {
1208
*err = -ENOMEM;
1209
return NULL;
1210
}
1211
1212
perf_config(intel_pt_perf_config, ptr);
1213
1214
ptr->intel_pt_pmu = intel_pt_pmu;
1215
ptr->itr.recording_options = intel_pt_recording_options;
1216
ptr->itr.info_priv_size = intel_pt_info_priv_size;
1217
ptr->itr.info_fill = intel_pt_info_fill;
1218
ptr->itr.free = intel_pt_recording_free;
1219
ptr->itr.snapshot_start = intel_pt_snapshot_start;
1220
ptr->itr.snapshot_finish = intel_pt_snapshot_finish;
1221
ptr->itr.find_snapshot = intel_pt_find_snapshot;
1222
ptr->itr.parse_snapshot_options = intel_pt_parse_snapshot_options;
1223
ptr->itr.reference = intel_pt_reference;
1224
ptr->itr.read_finish = auxtrace_record__read_finish;
1225
/*
1226
* Decoding starts at a PSB packet. Minimum PSB period is 2K so 4K
1227
* should give at least 1 PSB per sample.
1228
*/
1229
ptr->itr.default_aux_sample_size = 4096;
1230
return &ptr->itr;
1231
}
1232
1233