root/tools/perf/arch/x86/util/intel-bts.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. intel_bts_info_priv_size
  2. intel_bts_info_fill
  3. intel_bts_recording_options
  4. intel_bts_parse_snapshot_options
  5. intel_bts_reference
  6. intel_bts_alloc_snapshot_refs
  7. intel_bts_free_snapshot_refs
  8. intel_bts_recording_free
  9. intel_bts_snapshot_start
  10. intel_bts_snapshot_finish
  11. intel_bts_first_wrap
  12. intel_bts_find_snapshot
  13. intel_bts_read_finish
  14. intel_bts_recording_init

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

/* [<][>][^][v][top][bottom][index][help] */