0001
0002
0003
0004
0005
0006
0007 #include <errno.h>
0008 #include <linux/kernel.h>
0009 #include <linux/types.h>
0010 #include <linux/bitops.h>
0011 #include <linux/log2.h>
0012 #include <linux/zalloc.h>
0013
0014 #include "../../../util/cpumap.h"
0015 #include "../../../util/event.h"
0016 #include "../../../util/evsel.h"
0017 #include "../../../util/evlist.h"
0018 #include "../../../util/mmap.h"
0019 #include "../../../util/session.h"
0020 #include "../../../util/pmu.h"
0021 #include "../../../util/debug.h"
0022 #include "../../../util/record.h"
0023 #include "../../../util/tsc.h"
0024 #include "../../../util/auxtrace.h"
0025 #include "../../../util/intel-bts.h"
0026 #include <internal/lib.h> // page_size
0027
0028 #define KiB(x) ((x) * 1024)
0029 #define MiB(x) ((x) * 1024 * 1024)
0030 #define KiB_MASK(x) (KiB(x) - 1)
0031 #define MiB_MASK(x) (MiB(x) - 1)
0032
0033 struct intel_bts_snapshot_ref {
0034 void *ref_buf;
0035 size_t ref_offset;
0036 bool wrapped;
0037 };
0038
0039 struct intel_bts_recording {
0040 struct auxtrace_record itr;
0041 struct perf_pmu *intel_bts_pmu;
0042 struct evlist *evlist;
0043 bool snapshot_mode;
0044 size_t snapshot_size;
0045 int snapshot_ref_cnt;
0046 struct intel_bts_snapshot_ref *snapshot_refs;
0047 };
0048
0049 struct branch {
0050 u64 from;
0051 u64 to;
0052 u64 misc;
0053 };
0054
0055 static size_t
0056 intel_bts_info_priv_size(struct auxtrace_record *itr __maybe_unused,
0057 struct evlist *evlist __maybe_unused)
0058 {
0059 return INTEL_BTS_AUXTRACE_PRIV_SIZE;
0060 }
0061
0062 static int intel_bts_info_fill(struct auxtrace_record *itr,
0063 struct perf_session *session,
0064 struct perf_record_auxtrace_info *auxtrace_info,
0065 size_t priv_size)
0066 {
0067 struct intel_bts_recording *btsr =
0068 container_of(itr, struct intel_bts_recording, itr);
0069 struct perf_pmu *intel_bts_pmu = btsr->intel_bts_pmu;
0070 struct perf_event_mmap_page *pc;
0071 struct perf_tsc_conversion tc = { .time_mult = 0, };
0072 bool cap_user_time_zero = false;
0073 int err;
0074
0075 if (priv_size != INTEL_BTS_AUXTRACE_PRIV_SIZE)
0076 return -EINVAL;
0077
0078 if (!session->evlist->core.nr_mmaps)
0079 return -EINVAL;
0080
0081 pc = session->evlist->mmap[0].core.base;
0082 if (pc) {
0083 err = perf_read_tsc_conversion(pc, &tc);
0084 if (err) {
0085 if (err != -EOPNOTSUPP)
0086 return err;
0087 } else {
0088 cap_user_time_zero = tc.time_mult != 0;
0089 }
0090 if (!cap_user_time_zero)
0091 ui__warning("Intel BTS: TSC not available\n");
0092 }
0093
0094 auxtrace_info->type = PERF_AUXTRACE_INTEL_BTS;
0095 auxtrace_info->priv[INTEL_BTS_PMU_TYPE] = intel_bts_pmu->type;
0096 auxtrace_info->priv[INTEL_BTS_TIME_SHIFT] = tc.time_shift;
0097 auxtrace_info->priv[INTEL_BTS_TIME_MULT] = tc.time_mult;
0098 auxtrace_info->priv[INTEL_BTS_TIME_ZERO] = tc.time_zero;
0099 auxtrace_info->priv[INTEL_BTS_CAP_USER_TIME_ZERO] = cap_user_time_zero;
0100 auxtrace_info->priv[INTEL_BTS_SNAPSHOT_MODE] = btsr->snapshot_mode;
0101
0102 return 0;
0103 }
0104
0105 static int intel_bts_recording_options(struct auxtrace_record *itr,
0106 struct evlist *evlist,
0107 struct record_opts *opts)
0108 {
0109 struct intel_bts_recording *btsr =
0110 container_of(itr, struct intel_bts_recording, itr);
0111 struct perf_pmu *intel_bts_pmu = btsr->intel_bts_pmu;
0112 struct evsel *evsel, *intel_bts_evsel = NULL;
0113 const struct perf_cpu_map *cpus = evlist->core.user_requested_cpus;
0114 bool privileged = perf_event_paranoid_check(-1);
0115
0116 if (opts->auxtrace_sample_mode) {
0117 pr_err("Intel BTS does not support AUX area sampling\n");
0118 return -EINVAL;
0119 }
0120
0121 btsr->evlist = evlist;
0122 btsr->snapshot_mode = opts->auxtrace_snapshot_mode;
0123
0124 evlist__for_each_entry(evlist, evsel) {
0125 if (evsel->core.attr.type == intel_bts_pmu->type) {
0126 if (intel_bts_evsel) {
0127 pr_err("There may be only one " INTEL_BTS_PMU_NAME " event\n");
0128 return -EINVAL;
0129 }
0130 evsel->core.attr.freq = 0;
0131 evsel->core.attr.sample_period = 1;
0132 evsel->needs_auxtrace_mmap = true;
0133 intel_bts_evsel = evsel;
0134 opts->full_auxtrace = true;
0135 }
0136 }
0137
0138 if (opts->auxtrace_snapshot_mode && !opts->full_auxtrace) {
0139 pr_err("Snapshot mode (-S option) requires " INTEL_BTS_PMU_NAME " PMU event (-e " INTEL_BTS_PMU_NAME ")\n");
0140 return -EINVAL;
0141 }
0142
0143 if (!opts->full_auxtrace)
0144 return 0;
0145
0146 if (opts->full_auxtrace && !perf_cpu_map__empty(cpus)) {
0147 pr_err(INTEL_BTS_PMU_NAME " does not support per-cpu recording\n");
0148 return -EINVAL;
0149 }
0150
0151
0152 if (opts->auxtrace_snapshot_mode) {
0153 if (!opts->auxtrace_snapshot_size && !opts->auxtrace_mmap_pages) {
0154 if (privileged) {
0155 opts->auxtrace_mmap_pages = MiB(4) / page_size;
0156 } else {
0157 opts->auxtrace_mmap_pages = KiB(128) / page_size;
0158 if (opts->mmap_pages == UINT_MAX)
0159 opts->mmap_pages = KiB(256) / page_size;
0160 }
0161 } else if (!opts->auxtrace_mmap_pages && !privileged &&
0162 opts->mmap_pages == UINT_MAX) {
0163 opts->mmap_pages = KiB(256) / page_size;
0164 }
0165 if (!opts->auxtrace_snapshot_size)
0166 opts->auxtrace_snapshot_size =
0167 opts->auxtrace_mmap_pages * (size_t)page_size;
0168 if (!opts->auxtrace_mmap_pages) {
0169 size_t sz = opts->auxtrace_snapshot_size;
0170
0171 sz = round_up(sz, page_size) / page_size;
0172 opts->auxtrace_mmap_pages = roundup_pow_of_two(sz);
0173 }
0174 if (opts->auxtrace_snapshot_size >
0175 opts->auxtrace_mmap_pages * (size_t)page_size) {
0176 pr_err("Snapshot size %zu must not be greater than AUX area tracing mmap size %zu\n",
0177 opts->auxtrace_snapshot_size,
0178 opts->auxtrace_mmap_pages * (size_t)page_size);
0179 return -EINVAL;
0180 }
0181 if (!opts->auxtrace_snapshot_size || !opts->auxtrace_mmap_pages) {
0182 pr_err("Failed to calculate default snapshot size and/or AUX area tracing mmap pages\n");
0183 return -EINVAL;
0184 }
0185 pr_debug2("Intel BTS snapshot size: %zu\n",
0186 opts->auxtrace_snapshot_size);
0187 }
0188
0189
0190 if (opts->full_auxtrace && !opts->auxtrace_mmap_pages) {
0191 if (privileged) {
0192 opts->auxtrace_mmap_pages = MiB(4) / page_size;
0193 } else {
0194 opts->auxtrace_mmap_pages = KiB(128) / page_size;
0195 if (opts->mmap_pages == UINT_MAX)
0196 opts->mmap_pages = KiB(256) / page_size;
0197 }
0198 }
0199
0200
0201 if (opts->auxtrace_mmap_pages) {
0202 size_t sz = opts->auxtrace_mmap_pages * (size_t)page_size;
0203 size_t min_sz;
0204
0205 if (opts->auxtrace_snapshot_mode)
0206 min_sz = KiB(4);
0207 else
0208 min_sz = KiB(8);
0209
0210 if (sz < min_sz || !is_power_of_2(sz)) {
0211 pr_err("Invalid mmap size for Intel BTS: must be at least %zuKiB and a power of 2\n",
0212 min_sz / 1024);
0213 return -EINVAL;
0214 }
0215 }
0216
0217 if (intel_bts_evsel) {
0218
0219
0220
0221
0222 evlist__to_front(evlist, intel_bts_evsel);
0223
0224
0225
0226
0227 if (!perf_cpu_map__empty(cpus))
0228 evsel__set_sample_bit(intel_bts_evsel, CPU);
0229 }
0230
0231
0232 if (opts->full_auxtrace) {
0233 struct evsel *tracking_evsel;
0234 int err;
0235
0236 err = parse_event(evlist, "dummy:u");
0237 if (err)
0238 return err;
0239
0240 tracking_evsel = evlist__last(evlist);
0241
0242 evlist__set_tracking_event(evlist, tracking_evsel);
0243
0244 tracking_evsel->core.attr.freq = 0;
0245 tracking_evsel->core.attr.sample_period = 1;
0246 }
0247
0248 return 0;
0249 }
0250
0251 static int intel_bts_parse_snapshot_options(struct auxtrace_record *itr,
0252 struct record_opts *opts,
0253 const char *str)
0254 {
0255 struct intel_bts_recording *btsr =
0256 container_of(itr, struct intel_bts_recording, itr);
0257 unsigned long long snapshot_size = 0;
0258 char *endptr;
0259
0260 if (str) {
0261 snapshot_size = strtoull(str, &endptr, 0);
0262 if (*endptr || snapshot_size > SIZE_MAX)
0263 return -1;
0264 }
0265
0266 opts->auxtrace_snapshot_mode = true;
0267 opts->auxtrace_snapshot_size = snapshot_size;
0268
0269 btsr->snapshot_size = snapshot_size;
0270
0271 return 0;
0272 }
0273
0274 static u64 intel_bts_reference(struct auxtrace_record *itr __maybe_unused)
0275 {
0276 return rdtsc();
0277 }
0278
0279 static int intel_bts_alloc_snapshot_refs(struct intel_bts_recording *btsr,
0280 int idx)
0281 {
0282 const size_t sz = sizeof(struct intel_bts_snapshot_ref);
0283 int cnt = btsr->snapshot_ref_cnt, new_cnt = cnt * 2;
0284 struct intel_bts_snapshot_ref *refs;
0285
0286 if (!new_cnt)
0287 new_cnt = 16;
0288
0289 while (new_cnt <= idx)
0290 new_cnt *= 2;
0291
0292 refs = calloc(new_cnt, sz);
0293 if (!refs)
0294 return -ENOMEM;
0295
0296 memcpy(refs, btsr->snapshot_refs, cnt * sz);
0297
0298 btsr->snapshot_refs = refs;
0299 btsr->snapshot_ref_cnt = new_cnt;
0300
0301 return 0;
0302 }
0303
0304 static void intel_bts_free_snapshot_refs(struct intel_bts_recording *btsr)
0305 {
0306 int i;
0307
0308 for (i = 0; i < btsr->snapshot_ref_cnt; i++)
0309 zfree(&btsr->snapshot_refs[i].ref_buf);
0310 zfree(&btsr->snapshot_refs);
0311 }
0312
0313 static void intel_bts_recording_free(struct auxtrace_record *itr)
0314 {
0315 struct intel_bts_recording *btsr =
0316 container_of(itr, struct intel_bts_recording, itr);
0317
0318 intel_bts_free_snapshot_refs(btsr);
0319 free(btsr);
0320 }
0321
0322 static int intel_bts_snapshot_start(struct auxtrace_record *itr)
0323 {
0324 struct intel_bts_recording *btsr =
0325 container_of(itr, struct intel_bts_recording, itr);
0326 struct evsel *evsel;
0327
0328 evlist__for_each_entry(btsr->evlist, evsel) {
0329 if (evsel->core.attr.type == btsr->intel_bts_pmu->type)
0330 return evsel__disable(evsel);
0331 }
0332 return -EINVAL;
0333 }
0334
0335 static int intel_bts_snapshot_finish(struct auxtrace_record *itr)
0336 {
0337 struct intel_bts_recording *btsr =
0338 container_of(itr, struct intel_bts_recording, itr);
0339 struct evsel *evsel;
0340
0341 evlist__for_each_entry(btsr->evlist, evsel) {
0342 if (evsel->core.attr.type == btsr->intel_bts_pmu->type)
0343 return evsel__enable(evsel);
0344 }
0345 return -EINVAL;
0346 }
0347
0348 static bool intel_bts_first_wrap(u64 *data, size_t buf_size)
0349 {
0350 int i, a, b;
0351
0352 b = buf_size >> 3;
0353 a = b - 512;
0354 if (a < 0)
0355 a = 0;
0356
0357 for (i = a; i < b; i++) {
0358 if (data[i])
0359 return true;
0360 }
0361
0362 return false;
0363 }
0364
0365 static int intel_bts_find_snapshot(struct auxtrace_record *itr, int idx,
0366 struct auxtrace_mmap *mm, unsigned char *data,
0367 u64 *head, u64 *old)
0368 {
0369 struct intel_bts_recording *btsr =
0370 container_of(itr, struct intel_bts_recording, itr);
0371 bool wrapped;
0372 int err;
0373
0374 pr_debug3("%s: mmap index %d old head %zu new head %zu\n",
0375 __func__, idx, (size_t)*old, (size_t)*head);
0376
0377 if (idx >= btsr->snapshot_ref_cnt) {
0378 err = intel_bts_alloc_snapshot_refs(btsr, idx);
0379 if (err)
0380 goto out_err;
0381 }
0382
0383 wrapped = btsr->snapshot_refs[idx].wrapped;
0384 if (!wrapped && intel_bts_first_wrap((u64 *)data, mm->len)) {
0385 btsr->snapshot_refs[idx].wrapped = true;
0386 wrapped = true;
0387 }
0388
0389
0390
0391
0392
0393
0394
0395 if (wrapped) {
0396 *old = *head;
0397 *head += mm->len;
0398 } else {
0399 if (mm->mask)
0400 *old &= mm->mask;
0401 else
0402 *old %= mm->len;
0403 if (*old > *head)
0404 *head += mm->len;
0405 }
0406
0407 pr_debug3("%s: wrap-around %sdetected, adjusted old head %zu adjusted new head %zu\n",
0408 __func__, wrapped ? "" : "not ", (size_t)*old, (size_t)*head);
0409
0410 return 0;
0411
0412 out_err:
0413 pr_err("%s: failed, error %d\n", __func__, err);
0414 return err;
0415 }
0416
0417 struct auxtrace_record *intel_bts_recording_init(int *err)
0418 {
0419 struct perf_pmu *intel_bts_pmu = perf_pmu__find(INTEL_BTS_PMU_NAME);
0420 struct intel_bts_recording *btsr;
0421
0422 if (!intel_bts_pmu)
0423 return NULL;
0424
0425 if (setenv("JITDUMP_USE_ARCH_TIMESTAMP", "1", 1)) {
0426 *err = -errno;
0427 return NULL;
0428 }
0429
0430 btsr = zalloc(sizeof(struct intel_bts_recording));
0431 if (!btsr) {
0432 *err = -ENOMEM;
0433 return NULL;
0434 }
0435
0436 btsr->intel_bts_pmu = intel_bts_pmu;
0437 btsr->itr.pmu = intel_bts_pmu;
0438 btsr->itr.recording_options = intel_bts_recording_options;
0439 btsr->itr.info_priv_size = intel_bts_info_priv_size;
0440 btsr->itr.info_fill = intel_bts_info_fill;
0441 btsr->itr.free = intel_bts_recording_free;
0442 btsr->itr.snapshot_start = intel_bts_snapshot_start;
0443 btsr->itr.snapshot_finish = intel_bts_snapshot_finish;
0444 btsr->itr.find_snapshot = intel_bts_find_snapshot;
0445 btsr->itr.parse_snapshot_options = intel_bts_parse_snapshot_options;
0446 btsr->itr.reference = intel_bts_reference;
0447 btsr->itr.read_finish = auxtrace_record__read_finish;
0448 btsr->itr.alignment = sizeof(struct branch);
0449 return &btsr->itr;
0450 }