| 1 | |
| 2 | |
| 3 | * intel-bts.c: Intel Processor Trace support
|
| 4 | * Copyright (c) 2013-2015, Intel Corporation.
|
| 5 |
|
| 6 |
|
| 7 | #include <endian.h>
|
| 8 | #include <errno.h>
|
| 9 | #include <byteswap.h>
|
| 10 | #include <inttypes.h>
|
| 11 | #include <linux/kernel.h>
|
| 12 | #include <linux/types.h>
|
| 13 | #include <linux/bitops.h>
|
| 14 | #include <linux/log2.h>
|
| 15 | #include <linux/zalloc.h>
|
| 16 |
|
| 17 | #include "color.h"
|
| 18 | #include "evsel.h"
|
| 19 | #include "evlist.h"
|
| 20 | #include "machine.h"
|
| 21 | #include "symbol.h"
|
| 22 | #include "session.h"
|
| 23 | #include "tool.h"
|
| 24 | #include "thread.h"
|
| 25 | #include "thread-stack.h"
|
| 26 | #include "debug.h"
|
| 27 | #include "tsc.h"
|
| 28 | #include "auxtrace.h"
|
| 29 | #include "intel-pt-decoder/intel-pt-insn-decoder.h"
|
| 30 | #include "intel-bts.h"
|
| 31 | #include "util/synthetic-events.h"
|
| 32 |
|
| 33 | #define MAX_TIMESTAMP (~0ULL)
|
| 34 |
|
| 35 | #define INTEL_BTS_ERR_NOINSN 5
|
| 36 | #define INTEL_BTS_ERR_LOST 9
|
| 37 |
|
| 38 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
| 39 | #define le64_to_cpu bswap_64
|
| 40 | #else
|
| 41 | #define le64_to_cpu
|
| 42 | #endif
|
| 43 |
|
| 44 | struct intel_bts {
|
| 45 | struct auxtrace auxtrace;
|
| 46 | struct auxtrace_queues queues;
|
| 47 | struct auxtrace_heap heap;
|
| 48 | u32 auxtrace_type;
|
| 49 | struct perf_session *session;
|
| 50 | struct machine *machine;
|
| 51 | bool sampling_mode;
|
| 52 | bool snapshot_mode;
|
| 53 | bool data_queued;
|
| 54 | u32 pmu_type;
|
| 55 | struct perf_tsc_conversion tc;
|
| 56 | bool cap_user_time_zero;
|
| 57 | struct itrace_synth_opts synth_opts;
|
| 58 | bool sample_branches;
|
| 59 | u32 branches_filter;
|
| 60 | u64 branches_sample_type;
|
| 61 | u64 branches_id;
|
| 62 | size_t branches_event_size;
|
| 63 | unsigned long num_events;
|
| 64 | };
|
| 65 |
|
| 66 | struct intel_bts_queue {
|
| 67 | struct intel_bts *bts;
|
| 68 | unsigned int queue_nr;
|
| 69 | struct auxtrace_buffer *buffer;
|
| 70 | bool on_heap;
|
| 71 | bool done;
|
| 72 | pid_t pid;
|
| 73 | pid_t tid;
|
| 74 | int cpu;
|
| 75 | u64 time;
|
| 76 | struct intel_pt_insn intel_pt_insn;
|
| 77 | u32 sample_flags;
|
| 78 | };
|
| 79 |
|
| 80 | struct branch {
|
| 81 | u64 from;
|
| 82 | u64 to;
|
| 83 | u64 misc;
|
| 84 | };
|
| 85 |
|
| 86 | static void intel_bts_dump(struct intel_bts *bts __maybe_unused,
|
| 87 | unsigned char *buf, size_t len)
|
| 88 | {
|
| 89 | struct branch *branch;
|
| 90 | size_t i, pos = 0, br_sz = sizeof(struct branch), sz;
|
| 91 | const char *color = PERF_COLOR_BLUE;
|
| 92 |
|
| 93 | color_fprintf(stdout, color,
|
| 94 | ". ... Intel BTS data: size %zu bytes\n",
|
| 95 | len);
|
| 96 |
|
| 97 | while (len) {
|
| 98 | if (len >= br_sz)
|
| 99 | sz = br_sz;
|
| 100 | else
|
| 101 | sz = len;
|
| 102 | printf(".");
|
| 103 | color_fprintf(stdout, color, " %08zx: ", pos);
|
| 104 | for (i = 0; i < sz; i++)
|
| 105 | color_fprintf(stdout, color, " %02x", buf[i]);
|
| 106 | for (; i < br_sz; i++)
|
| 107 | color_fprintf(stdout, color, " ");
|
| 108 | if (len >= br_sz) {
|
| 109 | branch = (struct branch *)buf;
|
| 110 | color_fprintf(stdout, color, " %"PRIx64" -> %"PRIx64" %s\n",
|
| 111 | le64_to_cpu(branch->from),
|
| 112 | le64_to_cpu(branch->to),
|
| 113 | le64_to_cpu(branch->misc) & 0x10 ?
|
| 114 | "pred" : "miss");
|
| 115 | } else {
|
| 116 | color_fprintf(stdout, color, " Bad record!\n");
|
| 117 | }
|
| 118 | pos += sz;
|
| 119 | buf += sz;
|
| 120 | len -= sz;
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | static void intel_bts_dump_event(struct intel_bts *bts, unsigned char *buf,
|
| 125 | size_t len)
|
| 126 | {
|
| 127 | printf(".\n");
|
| 128 | intel_bts_dump(bts, buf, len);
|
| 129 | }
|
| 130 |
|
| 131 | static int intel_bts_lost(struct intel_bts *bts, struct perf_sample *sample)
|
| 132 | {
|
| 133 | union perf_event event;
|
| 134 | int err;
|
| 135 |
|
| 136 | auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
|
| 137 | INTEL_BTS_ERR_LOST, sample->cpu, sample->pid,
|
| 138 | sample->tid, 0, "Lost trace data", sample->time);
|
| 139 |
|
| 140 | err = perf_session__deliver_synth_event(bts->session, &event, NULL);
|
| 141 | if (err)
|
| 142 | pr_err("Intel BTS: failed to deliver error event, error %d\n",
|
| 143 | err);
|
| 144 |
|
| 145 | return err;
|
| 146 | }
|
| 147 |
|
| 148 | static struct intel_bts_queue *intel_bts_alloc_queue(struct intel_bts *bts,
|
| 149 | unsigned int queue_nr)
|
| 150 | {
|
| 151 | struct intel_bts_queue *btsq;
|
| 152 |
|
| 153 | btsq = zalloc(sizeof(struct intel_bts_queue));
|
| 154 | if (!btsq)
|
| 155 | return NULL;
|
| 156 |
|
| 157 | btsq->bts = bts;
|
| 158 | btsq->queue_nr = queue_nr;
|
| 159 | btsq->pid = -1;
|
| 160 | btsq->tid = -1;
|
| 161 | btsq->cpu = -1;
|
| 162 |
|
| 163 | return btsq;
|
| 164 | }
|
| 165 |
|
| 166 | static int intel_bts_setup_queue(struct intel_bts *bts,
|
| 167 | struct auxtrace_queue *queue,
|
| 168 | unsigned int queue_nr)
|
| 169 | {
|
| 170 | struct intel_bts_queue *btsq = queue->priv;
|
| 171 |
|
| 172 | if (list_empty(&queue->head))
|
| 173 | return 0;
|
| 174 |
|
| 175 | if (!btsq) {
|
| 176 | btsq = intel_bts_alloc_queue(bts, queue_nr);
|
| 177 | if (!btsq)
|
| 178 | return -ENOMEM;
|
| 179 | queue->priv = btsq;
|
| 180 |
|
| 181 | if (queue->cpu != -1)
|
| 182 | btsq->cpu = queue->cpu;
|
| 183 | btsq->tid = queue->tid;
|
| 184 | }
|
| 185 |
|
| 186 | if (bts->sampling_mode)
|
| 187 | return 0;
|
| 188 |
|
| 189 | if (!btsq->on_heap && !btsq->buffer) {
|
| 190 | int ret;
|
| 191 |
|
| 192 | btsq->buffer = auxtrace_buffer__next(queue, NULL);
|
| 193 | if (!btsq->buffer)
|
| 194 | return 0;
|
| 195 |
|
| 196 | ret = auxtrace_heap__add(&bts->heap, queue_nr,
|
| 197 | btsq->buffer->reference);
|
| 198 | if (ret)
|
| 199 | return ret;
|
| 200 | btsq->on_heap = true;
|
| 201 | }
|
| 202 |
|
| 203 | return 0;
|
| 204 | }
|
| 205 |
|
| 206 | static int intel_bts_setup_queues(struct intel_bts *bts)
|
| 207 | {
|
| 208 | unsigned int i;
|
| 209 | int ret;
|
| 210 |
|
| 211 | for (i = 0; i < bts->queues.nr_queues; i++) {
|
| 212 | ret = intel_bts_setup_queue(bts, &bts->queues.queue_array[i],
|
| 213 | i);
|
| 214 | if (ret)
|
| 215 | return ret;
|
| 216 | }
|
| 217 | return 0;
|
| 218 | }
|
| 219 |
|
| 220 | static inline int intel_bts_update_queues(struct intel_bts *bts)
|
| 221 | {
|
| 222 | if (bts->queues.new_data) {
|
| 223 | bts->queues.new_data = false;
|
| 224 | return intel_bts_setup_queues(bts);
|
| 225 | }
|
| 226 | return 0;
|
| 227 | }
|
| 228 |
|
| 229 | static unsigned char *intel_bts_find_overlap(unsigned char *buf_a, size_t len_a,
|
| 230 | unsigned char *buf_b, size_t len_b)
|
| 231 | {
|
| 232 | size_t offs, len;
|
| 233 |
|
| 234 | if (len_a > len_b)
|
| 235 | offs = len_a - len_b;
|
| 236 | else
|
| 237 | offs = 0;
|
| 238 |
|
| 239 | for (; offs < len_a; offs += sizeof(struct branch)) {
|
| 240 | len = len_a - offs;
|
| 241 | if (!memcmp(buf_a + offs, buf_b, len))
|
| 242 | return buf_b + len;
|
| 243 | }
|
| 244 |
|
| 245 | return buf_b;
|
| 246 | }
|
| 247 |
|
| 248 | static int intel_bts_do_fix_overlap(struct auxtrace_queue *queue,
|
| 249 | struct auxtrace_buffer *b)
|
| 250 | {
|
| 251 | struct auxtrace_buffer *a;
|
| 252 | void *start;
|
| 253 |
|
| 254 | if (b->list.prev == &queue->head)
|
| 255 | return 0;
|
| 256 | a = list_entry(b->list.prev, struct auxtrace_buffer, list);
|
| 257 | start = intel_bts_find_overlap(a->data, a->size, b->data, b->size);
|
| 258 | if (!start)
|
| 259 | return -EINVAL;
|
| 260 | b->use_size = b->data + b->size - start;
|
| 261 | b->use_data = start;
|
| 262 | return 0;
|
| 263 | }
|
| 264 |
|
| 265 | static inline u8 intel_bts_cpumode(struct intel_bts *bts, uint64_t ip)
|
| 266 | {
|
| 267 | return machine__kernel_ip(bts->machine, ip) ?
|
| 268 | PERF_RECORD_MISC_KERNEL :
|
| 269 | PERF_RECORD_MISC_USER;
|
| 270 | }
|
| 271 |
|
| 272 | static int intel_bts_synth_branch_sample(struct intel_bts_queue *btsq,
|
| 273 | struct branch *branch)
|
| 274 | {
|
| 275 | int ret;
|
| 276 | struct intel_bts *bts = btsq->bts;
|
| 277 | union perf_event event;
|
| 278 | struct perf_sample sample;
|
| 279 |
|
| 280 | if (bts->synth_opts.initial_skip &&
|
| 281 | bts->num_events++ <= bts->synth_opts.initial_skip)
|
| 282 | return 0;
|
| 283 |
|
| 284 | perf_sample__init(&sample, true);
|
| 285 | sample.ip = le64_to_cpu(branch->from);
|
| 286 | sample.cpumode = intel_bts_cpumode(bts, sample.ip);
|
| 287 | sample.pid = btsq->pid;
|
| 288 | sample.tid = btsq->tid;
|
| 289 | sample.addr = le64_to_cpu(branch->to);
|
| 290 | sample.id = btsq->bts->branches_id;
|
| 291 | sample.stream_id = btsq->bts->branches_id;
|
| 292 | sample.period = 1;
|
| 293 | sample.cpu = btsq->cpu;
|
| 294 | sample.flags = btsq->sample_flags;
|
| 295 | sample.insn_len = btsq->intel_pt_insn.length;
|
| 296 | memcpy(sample.insn, btsq->intel_pt_insn.buf, INTEL_PT_INSN_BUF_SZ);
|
| 297 |
|
| 298 | event.sample.header.type = PERF_RECORD_SAMPLE;
|
| 299 | event.sample.header.misc = sample.cpumode;
|
| 300 | event.sample.header.size = sizeof(struct perf_event_header);
|
| 301 |
|
| 302 | if (bts->synth_opts.inject) {
|
| 303 | event.sample.header.size = bts->branches_event_size;
|
| 304 | ret = perf_event__synthesize_sample(&event,
|
| 305 | bts->branches_sample_type,
|
| 306 | 0, 0,
|
| 307 | &sample);
|
| 308 | if (ret)
|
| 309 | return ret;
|
| 310 | }
|
| 311 |
|
| 312 | ret = perf_session__deliver_synth_event(bts->session, &event, &sample);
|
| 313 | if (ret)
|
| 314 | pr_err("Intel BTS: failed to deliver branch event, error %d\n",
|
| 315 | ret);
|
| 316 |
|
| 317 | perf_sample__exit(&sample);
|
| 318 | return ret;
|
| 319 | }
|
| 320 |
|
| 321 | static int intel_bts_get_next_insn(struct intel_bts_queue *btsq, u64 ip)
|
| 322 | {
|
| 323 | struct machine *machine = btsq->bts->machine;
|
| 324 | struct thread *thread;
|
| 325 | unsigned char buf[INTEL_PT_INSN_BUF_SZ];
|
| 326 | ssize_t len;
|
| 327 | bool x86_64;
|
| 328 | int err = -1;
|
| 329 |
|
| 330 | thread = machine__find_thread(machine, -1, btsq->tid);
|
| 331 | if (!thread)
|
| 332 | return -1;
|
| 333 |
|
| 334 | len = thread__memcpy(thread, machine, buf, ip, INTEL_PT_INSN_BUF_SZ, &x86_64);
|
| 335 | if (len <= 0)
|
| 336 | goto out_put;
|
| 337 |
|
| 338 | if (intel_pt_get_insn(buf, len, x86_64, &btsq->intel_pt_insn))
|
| 339 | goto out_put;
|
| 340 |
|
| 341 | err = 0;
|
| 342 | out_put:
|
| 343 | thread__put(thread);
|
| 344 | return err;
|
| 345 | }
|
| 346 |
|
| 347 | static int intel_bts_synth_error(struct intel_bts *bts, int cpu, pid_t pid,
|
| 348 | pid_t tid, u64 ip)
|
| 349 | {
|
| 350 | union perf_event event;
|
| 351 | int err;
|
| 352 |
|
| 353 | auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
|
| 354 | INTEL_BTS_ERR_NOINSN, cpu, pid, tid, ip,
|
| 355 | "Failed to get instruction", 0);
|
| 356 |
|
| 357 | err = perf_session__deliver_synth_event(bts->session, &event, NULL);
|
| 358 | if (err)
|
| 359 | pr_err("Intel BTS: failed to deliver error event, error %d\n",
|
| 360 | err);
|
| 361 |
|
| 362 | return err;
|
| 363 | }
|
| 364 |
|
| 365 | static int intel_bts_get_branch_type(struct intel_bts_queue *btsq,
|
| 366 | struct branch *branch)
|
| 367 | {
|
| 368 | int err;
|
| 369 |
|
| 370 | if (!branch->from) {
|
| 371 | if (branch->to)
|
| 372 | btsq->sample_flags = PERF_IP_FLAG_BRANCH |
|
| 373 | PERF_IP_FLAG_TRACE_BEGIN;
|
| 374 | else
|
| 375 | btsq->sample_flags = 0;
|
| 376 | btsq->intel_pt_insn.length = 0;
|
| 377 | } else if (!branch->to) {
|
| 378 | btsq->sample_flags = PERF_IP_FLAG_BRANCH |
|
| 379 | PERF_IP_FLAG_TRACE_END;
|
| 380 | btsq->intel_pt_insn.length = 0;
|
| 381 | } else {
|
| 382 | err = intel_bts_get_next_insn(btsq, branch->from);
|
| 383 | if (err) {
|
| 384 | btsq->sample_flags = 0;
|
| 385 | btsq->intel_pt_insn.length = 0;
|
| 386 | if (!btsq->bts->synth_opts.errors)
|
| 387 | return 0;
|
| 388 | err = intel_bts_synth_error(btsq->bts, btsq->cpu,
|
| 389 | btsq->pid, btsq->tid,
|
| 390 | branch->from);
|
| 391 | return err;
|
| 392 | }
|
| 393 | btsq->sample_flags = intel_pt_insn_type(btsq->intel_pt_insn.op);
|
| 394 |
|
| 395 | if (!machine__kernel_ip(btsq->bts->machine, branch->from) &&
|
| 396 | machine__kernel_ip(btsq->bts->machine, branch->to) &&
|
| 397 | btsq->sample_flags != (PERF_IP_FLAG_BRANCH |
|
| 398 | PERF_IP_FLAG_CALL |
|
| 399 | PERF_IP_FLAG_SYSCALLRET))
|
| 400 | btsq->sample_flags = PERF_IP_FLAG_BRANCH |
|
| 401 | PERF_IP_FLAG_CALL |
|
| 402 | PERF_IP_FLAG_ASYNC |
|
| 403 | PERF_IP_FLAG_INTERRUPT;
|
| 404 | }
|
| 405 |
|
| 406 | return 0;
|
| 407 | }
|
| 408 |
|
| 409 | static int intel_bts_process_buffer(struct intel_bts_queue *btsq,
|
| 410 | struct auxtrace_buffer *buffer,
|
| 411 | struct thread *thread)
|
| 412 | {
|
| 413 | struct branch *branch;
|
| 414 | size_t sz, bsz = sizeof(struct branch);
|
| 415 | u32 filter = btsq->bts->branches_filter;
|
| 416 | int err = 0;
|
| 417 |
|
| 418 | if (buffer->use_data) {
|
| 419 | sz = buffer->use_size;
|
| 420 | branch = buffer->use_data;
|
| 421 | } else {
|
| 422 | sz = buffer->size;
|
| 423 | branch = buffer->data;
|
| 424 | }
|
| 425 |
|
| 426 | if (!btsq->bts->sample_branches)
|
| 427 | return 0;
|
| 428 |
|
| 429 | for (; sz > bsz; branch += 1, sz -= bsz) {
|
| 430 | if (!branch->from && !branch->to)
|
| 431 | continue;
|
| 432 | intel_bts_get_branch_type(btsq, branch);
|
| 433 | if (btsq->bts->synth_opts.thread_stack)
|
| 434 | thread_stack__event(thread, btsq->cpu, btsq->sample_flags,
|
| 435 | le64_to_cpu(branch->from),
|
| 436 | le64_to_cpu(branch->to),
|
| 437 | btsq->intel_pt_insn.length,
|
| 438 | buffer->buffer_nr + 1, true, 0, 0);
|
| 439 | if (filter && !(filter & btsq->sample_flags))
|
| 440 | continue;
|
| 441 | err = intel_bts_synth_branch_sample(btsq, branch);
|
| 442 | if (err)
|
| 443 | break;
|
| 444 | }
|
| 445 | return err;
|
| 446 | }
|
| 447 |
|
| 448 | static int intel_bts_process_queue(struct intel_bts_queue *btsq, u64 *timestamp)
|
| 449 | {
|
| 450 | struct auxtrace_buffer *buffer = btsq->buffer, *old_buffer = buffer;
|
| 451 | struct auxtrace_queue *queue;
|
| 452 | struct thread *thread;
|
| 453 | int err;
|
| 454 |
|
| 455 | if (btsq->done)
|
| 456 | return 1;
|
| 457 |
|
| 458 | if (btsq->pid == -1) {
|
| 459 | thread = machine__find_thread(btsq->bts->machine, -1,
|
| 460 | btsq->tid);
|
| 461 | if (thread)
|
| 462 | btsq->pid = thread__pid(thread);
|
| 463 | } else {
|
| 464 | thread = machine__findnew_thread(btsq->bts->machine, btsq->pid,
|
| 465 | btsq->tid);
|
| 466 | }
|
| 467 |
|
| 468 | queue = &btsq->bts->queues.queue_array[btsq->queue_nr];
|
| 469 |
|
| 470 | if (!buffer)
|
| 471 | buffer = auxtrace_buffer__next(queue, NULL);
|
| 472 |
|
| 473 | if (!buffer) {
|
| 474 | if (!btsq->bts->sampling_mode)
|
| 475 | btsq->done = 1;
|
| 476 | err = 1;
|
| 477 | goto out_put;
|
| 478 | }
|
| 479 |
|
| 480 |
|
| 481 | if (buffer->consecutive) {
|
| 482 | err = -EINVAL;
|
| 483 | goto out_put;
|
| 484 | }
|
| 485 |
|
| 486 | if (!buffer->data) {
|
| 487 | int fd = perf_data__fd(btsq->bts->session->data);
|
| 488 |
|
| 489 | buffer->data = auxtrace_buffer__get_data(buffer, fd);
|
| 490 | if (!buffer->data) {
|
| 491 | err = -ENOMEM;
|
| 492 | goto out_put;
|
| 493 | }
|
| 494 | }
|
| 495 |
|
| 496 | if (btsq->bts->snapshot_mode && !buffer->consecutive &&
|
| 497 | intel_bts_do_fix_overlap(queue, buffer)) {
|
| 498 | err = -ENOMEM;
|
| 499 | goto out_put;
|
| 500 | }
|
| 501 |
|
| 502 | if (!btsq->bts->synth_opts.callchain &&
|
| 503 | !btsq->bts->synth_opts.thread_stack && thread &&
|
| 504 | (!old_buffer || btsq->bts->sampling_mode ||
|
| 505 | (btsq->bts->snapshot_mode && !buffer->consecutive)))
|
| 506 | thread_stack__set_trace_nr(thread, btsq->cpu, buffer->buffer_nr + 1);
|
| 507 |
|
| 508 | err = intel_bts_process_buffer(btsq, buffer, thread);
|
| 509 |
|
| 510 | auxtrace_buffer__drop_data(buffer);
|
| 511 |
|
| 512 | btsq->buffer = auxtrace_buffer__next(queue, buffer);
|
| 513 | if (btsq->buffer) {
|
| 514 | if (timestamp)
|
| 515 | *timestamp = btsq->buffer->reference;
|
| 516 | } else {
|
| 517 | if (!btsq->bts->sampling_mode)
|
| 518 | btsq->done = 1;
|
| 519 | }
|
| 520 | out_put:
|
| 521 | thread__put(thread);
|
| 522 | return err;
|
| 523 | }
|
| 524 |
|
| 525 | static int intel_bts_flush_queue(struct intel_bts_queue *btsq)
|
| 526 | {
|
| 527 | u64 ts = 0;
|
| 528 | int ret;
|
| 529 |
|
| 530 | while (1) {
|
| 531 | ret = intel_bts_process_queue(btsq, &ts);
|
| 532 | if (ret < 0)
|
| 533 | return ret;
|
| 534 | if (ret)
|
| 535 | break;
|
| 536 | }
|
| 537 | return 0;
|
| 538 | }
|
| 539 |
|
| 540 | static int intel_bts_process_tid_exit(struct intel_bts *bts, pid_t tid)
|
| 541 | {
|
| 542 | struct auxtrace_queues *queues = &bts->queues;
|
| 543 | unsigned int i;
|
| 544 |
|
| 545 | for (i = 0; i < queues->nr_queues; i++) {
|
| 546 | struct auxtrace_queue *queue = &bts->queues.queue_array[i];
|
| 547 | struct intel_bts_queue *btsq = queue->priv;
|
| 548 |
|
| 549 | if (btsq && btsq->tid == tid)
|
| 550 | return intel_bts_flush_queue(btsq);
|
| 551 | }
|
| 552 | return 0;
|
| 553 | }
|
| 554 |
|
| 555 | static int intel_bts_process_queues(struct intel_bts *bts, u64 timestamp)
|
| 556 | {
|
| 557 | while (1) {
|
| 558 | unsigned int queue_nr;
|
| 559 | struct auxtrace_queue *queue;
|
| 560 | struct intel_bts_queue *btsq;
|
| 561 | u64 ts = 0;
|
| 562 | int ret;
|
| 563 |
|
| 564 | if (!bts->heap.heap_cnt)
|
| 565 | return 0;
|
| 566 |
|
| 567 | if (bts->heap.heap_array[0].ordinal > timestamp)
|
| 568 | return 0;
|
| 569 |
|
| 570 | queue_nr = bts->heap.heap_array[0].queue_nr;
|
| 571 | queue = &bts->queues.queue_array[queue_nr];
|
| 572 | btsq = queue->priv;
|
| 573 |
|
| 574 | auxtrace_heap__pop(&bts->heap);
|
| 575 |
|
| 576 | ret = intel_bts_process_queue(btsq, &ts);
|
| 577 | if (ret < 0) {
|
| 578 | auxtrace_heap__add(&bts->heap, queue_nr, ts);
|
| 579 | return ret;
|
| 580 | }
|
| 581 |
|
| 582 | if (!ret) {
|
| 583 | ret = auxtrace_heap__add(&bts->heap, queue_nr, ts);
|
| 584 | if (ret < 0)
|
| 585 | return ret;
|
| 586 | } else {
|
| 587 | btsq->on_heap = false;
|
| 588 | }
|
| 589 | }
|
| 590 |
|
| 591 | return 0;
|
| 592 | }
|
| 593 |
|
| 594 | static int intel_bts_process_event(struct perf_session *session,
|
| 595 | union perf_event *event,
|
| 596 | struct perf_sample *sample,
|
| 597 | const struct perf_tool *tool)
|
| 598 | {
|
| 599 | struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
|
| 600 | auxtrace);
|
| 601 | u64 timestamp;
|
| 602 | int err;
|
| 603 |
|
| 604 | if (dump_trace)
|
| 605 | return 0;
|
| 606 |
|
| 607 | if (!tool->ordered_events) {
|
| 608 | pr_err("Intel BTS requires ordered events\n");
|
| 609 | return -EINVAL;
|
| 610 | }
|
| 611 |
|
| 612 | if (sample->time && sample->time != (u64)-1)
|
| 613 | timestamp = perf_time_to_tsc(sample->time, &bts->tc);
|
| 614 | else
|
| 615 | timestamp = 0;
|
| 616 |
|
| 617 | err = intel_bts_update_queues(bts);
|
| 618 | if (err)
|
| 619 | return err;
|
| 620 |
|
| 621 | err = intel_bts_process_queues(bts, timestamp);
|
| 622 | if (err)
|
| 623 | return err;
|
| 624 | if (event->header.type == PERF_RECORD_EXIT) {
|
| 625 | err = intel_bts_process_tid_exit(bts, event->fork.tid);
|
| 626 | if (err)
|
| 627 | return err;
|
| 628 | }
|
| 629 |
|
| 630 | if (event->header.type == PERF_RECORD_AUX &&
|
| 631 | (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
|
| 632 | bts->synth_opts.errors)
|
| 633 | err = intel_bts_lost(bts, sample);
|
| 634 |
|
| 635 | return err;
|
| 636 | }
|
| 637 |
|
| 638 | static int intel_bts_process_auxtrace_event(struct perf_session *session,
|
| 639 | union perf_event *event,
|
| 640 | const struct perf_tool *tool __maybe_unused)
|
| 641 | {
|
| 642 | struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
|
| 643 | auxtrace);
|
| 644 |
|
| 645 | if (bts->sampling_mode)
|
| 646 | return 0;
|
| 647 |
|
| 648 | if (!bts->data_queued) {
|
| 649 | struct auxtrace_buffer *buffer;
|
| 650 | off_t data_offset;
|
| 651 | int fd = perf_data__fd(session->data);
|
| 652 | int err;
|
| 653 |
|
| 654 | if (perf_data__is_pipe(session->data)) {
|
| 655 | data_offset = 0;
|
| 656 | } else {
|
| 657 | data_offset = lseek(fd, 0, SEEK_CUR);
|
| 658 | if (data_offset == -1)
|
| 659 | return -errno;
|
| 660 | }
|
| 661 |
|
| 662 | err = auxtrace_queues__add_event(&bts->queues, session, event,
|
| 663 | data_offset, &buffer);
|
| 664 | if (err)
|
| 665 | return err;
|
| 666 |
|
| 667 |
|
| 668 | if (dump_trace) {
|
| 669 | if (auxtrace_buffer__get_data(buffer, fd)) {
|
| 670 | intel_bts_dump_event(bts, buffer->data,
|
| 671 | buffer->size);
|
| 672 | auxtrace_buffer__put_data(buffer);
|
| 673 | }
|
| 674 | }
|
| 675 | }
|
| 676 |
|
| 677 | return 0;
|
| 678 | }
|
| 679 |
|
| 680 | static int intel_bts_flush(struct perf_session *session,
|
| 681 | const struct perf_tool *tool __maybe_unused)
|
| 682 | {
|
| 683 | struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
|
| 684 | auxtrace);
|
| 685 | int ret;
|
| 686 |
|
| 687 | if (dump_trace || bts->sampling_mode)
|
| 688 | return 0;
|
| 689 |
|
| 690 | if (!tool->ordered_events)
|
| 691 | return -EINVAL;
|
| 692 |
|
| 693 | ret = intel_bts_update_queues(bts);
|
| 694 | if (ret < 0)
|
| 695 | return ret;
|
| 696 |
|
| 697 | return intel_bts_process_queues(bts, MAX_TIMESTAMP);
|
| 698 | }
|
| 699 |
|
| 700 | static void intel_bts_free_queue(void *priv)
|
| 701 | {
|
| 702 | struct intel_bts_queue *btsq = priv;
|
| 703 |
|
| 704 | if (!btsq)
|
| 705 | return;
|
| 706 | free(btsq);
|
| 707 | }
|
| 708 |
|
| 709 | static void intel_bts_free_events(struct perf_session *session)
|
| 710 | {
|
| 711 | struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
|
| 712 | auxtrace);
|
| 713 | struct auxtrace_queues *queues = &bts->queues;
|
| 714 | unsigned int i;
|
| 715 |
|
| 716 | for (i = 0; i < queues->nr_queues; i++) {
|
| 717 | intel_bts_free_queue(queues->queue_array[i].priv);
|
| 718 | queues->queue_array[i].priv = NULL;
|
| 719 | }
|
| 720 | auxtrace_queues__free(queues);
|
| 721 | }
|
| 722 |
|
| 723 | static void intel_bts_free(struct perf_session *session)
|
| 724 | {
|
| 725 | struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
|
| 726 | auxtrace);
|
| 727 |
|
| 728 | auxtrace_heap__free(&bts->heap);
|
| 729 | intel_bts_free_events(session);
|
| 730 | session->auxtrace = NULL;
|
| 731 | free(bts);
|
| 732 | }
|
| 733 |
|
| 734 | static bool intel_bts_evsel_is_auxtrace(struct perf_session *session,
|
| 735 | struct evsel *evsel)
|
| 736 | {
|
| 737 | struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts,
|
| 738 | auxtrace);
|
| 739 |
|
| 740 | return evsel->core.attr.type == bts->pmu_type;
|
| 741 | }
|
| 742 |
|
| 743 | static int intel_bts_synth_events(struct intel_bts *bts,
|
| 744 | struct perf_session *session)
|
| 745 | {
|
| 746 | struct evlist *evlist = session->evlist;
|
| 747 | struct evsel *evsel;
|
| 748 | struct perf_event_attr attr;
|
| 749 | bool found = false;
|
| 750 | u64 id;
|
| 751 | int err;
|
| 752 |
|
| 753 | evlist__for_each_entry(evlist, evsel) {
|
| 754 | if (evsel->core.attr.type == bts->pmu_type && evsel->core.ids) {
|
| 755 | found = true;
|
| 756 | break;
|
| 757 | }
|
| 758 | }
|
| 759 |
|
| 760 | if (!found) {
|
| 761 | pr_debug("There are no selected events with Intel BTS data\n");
|
| 762 | return 0;
|
| 763 | }
|
| 764 |
|
| 765 | memset(&attr, 0, sizeof(struct perf_event_attr));
|
| 766 | attr.size = sizeof(struct perf_event_attr);
|
| 767 | attr.type = PERF_TYPE_HARDWARE;
|
| 768 | attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
|
| 769 | attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
|
| 770 | PERF_SAMPLE_PERIOD;
|
| 771 | attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
|
| 772 | attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
|
| 773 | attr.exclude_user = evsel->core.attr.exclude_user;
|
| 774 | attr.exclude_kernel = evsel->core.attr.exclude_kernel;
|
| 775 | attr.exclude_hv = evsel->core.attr.exclude_hv;
|
| 776 | attr.exclude_host = evsel->core.attr.exclude_host;
|
| 777 | attr.exclude_guest = evsel->core.attr.exclude_guest;
|
| 778 | attr.sample_id_all = evsel->core.attr.sample_id_all;
|
| 779 | attr.read_format = evsel->core.attr.read_format;
|
| 780 |
|
| 781 | id = auxtrace_synth_id_range_start(evsel);
|
| 782 |
|
| 783 | if (bts->synth_opts.branches) {
|
| 784 | attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
|
| 785 | attr.sample_period = 1;
|
| 786 | attr.sample_type |= PERF_SAMPLE_ADDR;
|
| 787 | pr_debug("Synthesizing 'branches' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
|
| 788 | id, (u64)attr.sample_type);
|
| 789 | err = perf_session__deliver_synth_attr_event(session, &attr, id);
|
| 790 | if (err) {
|
| 791 | pr_err("%s: failed to synthesize 'branches' event type\n",
|
| 792 | __func__);
|
| 793 | return err;
|
| 794 | }
|
| 795 | bts->sample_branches = true;
|
| 796 | bts->branches_sample_type = attr.sample_type;
|
| 797 | bts->branches_id = id;
|
| 798 | |
| 799 | * We only use sample types from PERF_SAMPLE_MASK so we can use
|
| 800 | * __evsel__sample_size() here.
|
| 801 |
|
| 802 | bts->branches_event_size = sizeof(struct perf_record_sample) +
|
| 803 | __evsel__sample_size(attr.sample_type);
|
| 804 | }
|
| 805 |
|
| 806 | return 0;
|
| 807 | }
|
| 808 |
|
| 809 | static const char * const intel_bts_info_fmts[] = {
|
| 810 | [INTEL_BTS_PMU_TYPE] = " PMU Type %"PRId64"\n",
|
| 811 | [INTEL_BTS_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
|
| 812 | [INTEL_BTS_TIME_MULT] = " Time Multiplier %"PRIu64"\n",
|
| 813 | [INTEL_BTS_TIME_ZERO] = " Time Zero %"PRIu64"\n",
|
| 814 | [INTEL_BTS_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
|
| 815 | [INTEL_BTS_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
|
| 816 | };
|
| 817 |
|
| 818 | static void intel_bts_print_info(__u64 *arr, int start, int finish)
|
| 819 | {
|
| 820 | int i;
|
| 821 |
|
| 822 | if (!dump_trace)
|
| 823 | return;
|
| 824 |
|
| 825 | for (i = start; i <= finish; i++)
|
| 826 | fprintf(stdout, intel_bts_info_fmts[i], arr[i]);
|
| 827 | }
|
| 828 |
|
| 829 | int intel_bts_process_auxtrace_info(union perf_event *event,
|
| 830 | struct perf_session *session)
|
| 831 | {
|
| 832 | struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
|
| 833 | size_t min_sz = sizeof(u64) * (INTEL_BTS_SNAPSHOT_MODE + 1);
|
| 834 | struct intel_bts *bts;
|
| 835 | int err;
|
| 836 |
|
| 837 | if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) +
|
| 838 | min_sz)
|
| 839 | return -EINVAL;
|
| 840 |
|
| 841 | bts = zalloc(sizeof(struct intel_bts));
|
| 842 | if (!bts)
|
| 843 | return -ENOMEM;
|
| 844 |
|
| 845 | err = auxtrace_queues__init(&bts->queues);
|
| 846 | if (err)
|
| 847 | goto err_free;
|
| 848 |
|
| 849 | bts->session = session;
|
| 850 | bts->machine = &session->machines.host;
|
| 851 | bts->auxtrace_type = auxtrace_info->type;
|
| 852 | bts->pmu_type = auxtrace_info->priv[INTEL_BTS_PMU_TYPE];
|
| 853 | bts->tc.time_shift = auxtrace_info->priv[INTEL_BTS_TIME_SHIFT];
|
| 854 | bts->tc.time_mult = auxtrace_info->priv[INTEL_BTS_TIME_MULT];
|
| 855 | bts->tc.time_zero = auxtrace_info->priv[INTEL_BTS_TIME_ZERO];
|
| 856 | bts->cap_user_time_zero =
|
| 857 | auxtrace_info->priv[INTEL_BTS_CAP_USER_TIME_ZERO];
|
| 858 | bts->snapshot_mode = auxtrace_info->priv[INTEL_BTS_SNAPSHOT_MODE];
|
| 859 |
|
| 860 | bts->sampling_mode = false;
|
| 861 |
|
| 862 | bts->auxtrace.process_event = intel_bts_process_event;
|
| 863 | bts->auxtrace.process_auxtrace_event = intel_bts_process_auxtrace_event;
|
| 864 | bts->auxtrace.flush_events = intel_bts_flush;
|
| 865 | bts->auxtrace.free_events = intel_bts_free_events;
|
| 866 | bts->auxtrace.free = intel_bts_free;
|
| 867 | bts->auxtrace.evsel_is_auxtrace = intel_bts_evsel_is_auxtrace;
|
| 868 | session->auxtrace = &bts->auxtrace;
|
| 869 |
|
| 870 | intel_bts_print_info(&auxtrace_info->priv[0], INTEL_BTS_PMU_TYPE,
|
| 871 | INTEL_BTS_SNAPSHOT_MODE);
|
| 872 |
|
| 873 | if (dump_trace)
|
| 874 | return 0;
|
| 875 |
|
| 876 | if (session->itrace_synth_opts->set) {
|
| 877 | bts->synth_opts = *session->itrace_synth_opts;
|
| 878 | } else {
|
| 879 | itrace_synth_opts__set_default(&bts->synth_opts,
|
| 880 | session->itrace_synth_opts->default_no_sample);
|
| 881 | bts->synth_opts.thread_stack =
|
| 882 | session->itrace_synth_opts->thread_stack;
|
| 883 | }
|
| 884 |
|
| 885 | if (bts->synth_opts.calls)
|
| 886 | bts->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
|
| 887 | PERF_IP_FLAG_TRACE_END;
|
| 888 | if (bts->synth_opts.returns)
|
| 889 | bts->branches_filter |= PERF_IP_FLAG_RETURN |
|
| 890 | PERF_IP_FLAG_TRACE_BEGIN;
|
| 891 |
|
| 892 | err = intel_bts_synth_events(bts, session);
|
| 893 | if (err)
|
| 894 | goto err_free_queues;
|
| 895 |
|
| 896 | err = auxtrace_queues__process_index(&bts->queues, session);
|
| 897 | if (err)
|
| 898 | goto err_free_queues;
|
| 899 |
|
| 900 | if (bts->queues.populated)
|
| 901 | bts->data_queued = true;
|
| 902 |
|
| 903 | return 0;
|
| 904 |
|
| 905 | err_free_queues:
|
| 906 | auxtrace_queues__free(&bts->queues);
|
| 907 | session->auxtrace = NULL;
|
| 908 | err_free:
|
| 909 | free(bts);
|
| 910 | return err;
|
| 911 | }
|