1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
|
// SPDX-License-Identifier: GPL-2.0
/*
* VPA DTL PMU support
*/
#include <linux/string.h>
#include <errno.h>
#include <inttypes.h>
#include "color.h"
#include "evlist.h"
#include "session.h"
#include "auxtrace.h"
#include "data.h"
#include "machine.h"
#include "debug.h"
#include "powerpc-vpadtl.h"
#include "sample.h"
#include "tool.h"
/*
* Structure to save the auxtrace queue
*/
struct powerpc_vpadtl {
struct auxtrace auxtrace;
struct auxtrace_queues queues;
struct auxtrace_heap heap;
u32 auxtrace_type;
struct perf_session *session;
struct machine *machine;
u32 pmu_type;
u64 sample_id;
};
struct boottb_freq {
u64 boot_tb;
u64 tb_freq;
u64 timebase;
u64 padded[3];
};
struct powerpc_vpadtl_queue {
struct powerpc_vpadtl *vpa;
unsigned int queue_nr;
struct auxtrace_buffer *buffer;
struct thread *thread;
bool on_heap;
struct powerpc_vpadtl_entry *dtl;
u64 timestamp;
unsigned long pkt_len;
unsigned long buf_len;
u64 boot_tb;
u64 tb_freq;
unsigned int tb_buffer;
unsigned int size;
bool done;
pid_t pid;
pid_t tid;
int cpu;
};
const char *dispatch_reasons[11] = {
"external_interrupt",
"firmware_internal_event",
"H_PROD",
"decrementer_interrupt",
"system_reset",
"firmware_internal_event",
"conferred_cycles",
"time_slice",
"virtual_memory_page_fault",
"expropriated_adjunct",
"priv_doorbell"};
const char *preempt_reasons[10] = {
"unused",
"firmware_internal_event",
"H_CEDE",
"H_CONFER",
"time_slice",
"migration_hibernation_page_fault",
"virtual_memory_page_fault",
"H_CONFER_ADJUNCT",
"hcall_adjunct",
"HDEC_adjunct"};
#define dtl_entry_size sizeof(struct powerpc_vpadtl_entry)
/*
* Function to dump the dispatch trace data when perf report
* is invoked with -D
*/
static void powerpc_vpadtl_dump(struct powerpc_vpadtl *vpa __maybe_unused,
unsigned char *buf, size_t len)
{
struct powerpc_vpadtl_entry *dtl;
int pkt_len, pos = 0;
const char *color = PERF_COLOR_BLUE;
color_fprintf(stdout, color,
". ... VPA DTL PMU data: size %zu bytes, entries is %zu\n",
len, len/dtl_entry_size);
if (len % dtl_entry_size)
len = len - (len % dtl_entry_size);
while (len) {
pkt_len = dtl_entry_size;
printf(".");
color_fprintf(stdout, color, " %08x: ", pos);
dtl = (struct powerpc_vpadtl_entry *)buf;
if (dtl->timebase != 0) {
printf("dispatch_reason:%s, preempt_reason:%s, "
"enqueue_to_dispatch_time:%d, ready_to_enqueue_time:%d, "
"waiting_to_ready_time:%d\n",
dispatch_reasons[dtl->dispatch_reason],
preempt_reasons[dtl->preempt_reason],
be32_to_cpu(dtl->enqueue_to_dispatch_time),
be32_to_cpu(dtl->ready_to_enqueue_time),
be32_to_cpu(dtl->waiting_to_ready_time));
} else {
struct boottb_freq *boot_tb = (struct boottb_freq *)buf;
printf("boot_tb: %" PRIu64 ", tb_freq: %" PRIu64 "\n",
boot_tb->boot_tb, boot_tb->tb_freq);
}
pos += pkt_len;
buf += pkt_len;
len -= pkt_len;
}
}
static unsigned long long powerpc_vpadtl_timestamp(struct powerpc_vpadtl_queue *vpaq)
{
struct powerpc_vpadtl_entry *record = vpaq->dtl;
unsigned long long timestamp = 0;
unsigned long long boot_tb;
unsigned long long diff;
double result, div;
double boot_freq;
/*
* Formula used to get timestamp that can be co-related with
* other perf events:
* ((timbase from DTL entry - boot time) / frequency) * 1000000000
*/
if (record->timebase) {
boot_tb = vpaq->boot_tb;
boot_freq = vpaq->tb_freq;
diff = be64_to_cpu(record->timebase) - boot_tb;
div = diff / boot_freq;
result = div;
result = result * 1000000000;
timestamp = result;
}
return timestamp;
}
static struct powerpc_vpadtl *session_to_vpa(struct perf_session *session)
{
return container_of(session->auxtrace, struct powerpc_vpadtl, auxtrace);
}
static void powerpc_vpadtl_dump_event(struct powerpc_vpadtl *vpa, unsigned char *buf,
size_t len)
{
printf(".\n");
powerpc_vpadtl_dump(vpa, buf, len);
}
/*
* Generate perf sample for each entry in the dispatch trace log.
* - sample ip is picked from srr0 field of powerpc_vpadtl_entry
* - sample cpu is logical cpu.
* - cpumode is set to PERF_RECORD_MISC_KERNEL
* - Additionally save the details in raw_data of sample. This
* is to print the relevant fields in perf_sample__fprintf_synth()
* when called from builtin-script
*/
static int powerpc_vpadtl_sample(struct powerpc_vpadtl_entry *record,
struct powerpc_vpadtl *vpa, u64 save, int cpu)
{
struct perf_sample sample;
union perf_event event;
sample.ip = be64_to_cpu(record->srr0);
sample.period = 1;
sample.cpu = cpu;
sample.id = vpa->sample_id;
sample.callchain = NULL;
sample.branch_stack = NULL;
memset(&event, 0, sizeof(event));
sample.cpumode = PERF_RECORD_MISC_KERNEL;
sample.time = save;
sample.raw_data = record;
sample.raw_size = sizeof(record);
event.sample.header.type = PERF_RECORD_SAMPLE;
event.sample.header.misc = sample.cpumode;
event.sample.header.size = sizeof(struct perf_event_header);
if (perf_session__deliver_synth_event(vpa->session, &event, &sample)) {
pr_debug("Failed to create sample for dtl entry\n");
return -1;
}
return 0;
}
static int powerpc_vpadtl_get_buffer(struct powerpc_vpadtl_queue *vpaq)
{
struct auxtrace_buffer *buffer = vpaq->buffer;
struct auxtrace_queues *queues = &vpaq->vpa->queues;
struct auxtrace_queue *queue;
queue = &queues->queue_array[vpaq->queue_nr];
buffer = auxtrace_buffer__next(queue, buffer);
if (!buffer)
return 0;
vpaq->buffer = buffer;
vpaq->size = buffer->size;
/* If the aux_buffer doesn't have data associated, try to load it */
if (!buffer->data) {
/* get the file desc associated with the perf data file */
int fd = perf_data__fd(vpaq->vpa->session->data);
buffer->data = auxtrace_buffer__get_data(buffer, fd);
if (!buffer->data)
return -ENOMEM;
}
vpaq->buf_len = buffer->size;
if (buffer->size % dtl_entry_size)
vpaq->buf_len = buffer->size - (buffer->size % dtl_entry_size);
if (vpaq->tb_buffer != buffer->buffer_nr) {
vpaq->pkt_len = 0;
vpaq->tb_buffer = 0;
}
return 1;
}
/*
* The first entry in the queue for VPA DTL PMU has the boot timebase,
* frequency details which are needed to get timestamp which is required to
* correlate with other events. Save the boot_tb and tb_freq as part of
* powerpc_vpadtl_queue. The very next entry is the actual trace data to
* be returned.
*/
static int powerpc_vpadtl_decode(struct powerpc_vpadtl_queue *vpaq)
{
int ret;
char *buf;
struct boottb_freq *boottb;
ret = powerpc_vpadtl_get_buffer(vpaq);
if (ret <= 0)
return ret;
boottb = (struct boottb_freq *)vpaq->buffer->data;
if (boottb->timebase == 0) {
vpaq->boot_tb = boottb->boot_tb;
vpaq->tb_freq = boottb->tb_freq;
vpaq->pkt_len += dtl_entry_size;
}
buf = vpaq->buffer->data;
buf += vpaq->pkt_len;
vpaq->dtl = (struct powerpc_vpadtl_entry *)buf;
vpaq->tb_buffer = vpaq->buffer->buffer_nr;
vpaq->buffer = NULL;
vpaq->buf_len = 0;
return 1;
}
static int powerpc_vpadtl_decode_all(struct powerpc_vpadtl_queue *vpaq)
{
int ret;
unsigned char *buf;
if (!vpaq->buf_len || vpaq->pkt_len == vpaq->size) {
ret = powerpc_vpadtl_get_buffer(vpaq);
if (ret <= 0)
return ret;
}
if (vpaq->buffer) {
buf = vpaq->buffer->data;
buf += vpaq->pkt_len;
vpaq->dtl = (struct powerpc_vpadtl_entry *)buf;
if ((long long)be64_to_cpu(vpaq->dtl->timebase) <= 0) {
if (vpaq->pkt_len != dtl_entry_size && vpaq->buf_len) {
vpaq->pkt_len += dtl_entry_size;
vpaq->buf_len -= dtl_entry_size;
}
return -1;
}
vpaq->pkt_len += dtl_entry_size;
vpaq->buf_len -= dtl_entry_size;
} else {
return 0;
}
return 1;
}
static int powerpc_vpadtl_run_decoder(struct powerpc_vpadtl_queue *vpaq, u64 *timestamp)
{
struct powerpc_vpadtl *vpa = vpaq->vpa;
struct powerpc_vpadtl_entry *record;
int ret;
unsigned long long vpaq_timestamp;
while (1) {
ret = powerpc_vpadtl_decode_all(vpaq);
if (!ret) {
pr_debug("All data in the queue has been processed.\n");
return 1;
}
/*
* Error is detected when decoding VPA PMU trace. Continue to
* the next trace data and find out more dtl entries.
*/
if (ret < 0)
continue;
record = vpaq->dtl;
vpaq_timestamp = powerpc_vpadtl_timestamp(vpaq);
/* Update timestamp for the last record */
if (vpaq_timestamp > vpaq->timestamp)
vpaq->timestamp = vpaq_timestamp;
/*
* If the timestamp of the queue is later than timestamp of the
* coming perf event, bail out so can allow the perf event to
* be processed ahead.
*/
if (vpaq->timestamp >= *timestamp) {
*timestamp = vpaq->timestamp;
vpaq->pkt_len -= dtl_entry_size;
vpaq->buf_len += dtl_entry_size;
return 0;
}
ret = powerpc_vpadtl_sample(record, vpa, vpaq_timestamp, vpaq->cpu);
if (ret)
continue;
}
return 0;
}
/*
* For each of the PERF_RECORD_XX record, compare the timestamp
* of perf record with timestamp of top element in the auxtrace heap.
* Process the auxtrace queue if the timestamp of element from heap is
* lower than timestamp from entry in perf record.
*
* Update the timestamp of the auxtrace heap with the timestamp
* of last processed entry from the auxtrace buffer.
*/
static int powerpc_vpadtl_process_queues(struct powerpc_vpadtl *vpa, u64 timestamp)
{
unsigned int queue_nr;
u64 ts;
int ret;
while (1) {
struct auxtrace_queue *queue;
struct powerpc_vpadtl_queue *vpaq;
if (!vpa->heap.heap_cnt)
return 0;
if (vpa->heap.heap_array[0].ordinal >= timestamp)
return 0;
queue_nr = vpa->heap.heap_array[0].queue_nr;
queue = &vpa->queues.queue_array[queue_nr];
vpaq = queue->priv;
auxtrace_heap__pop(&vpa->heap);
if (vpa->heap.heap_cnt) {
ts = vpa->heap.heap_array[0].ordinal + 1;
if (ts > timestamp)
ts = timestamp;
} else {
ts = timestamp;
}
ret = powerpc_vpadtl_run_decoder(vpaq, &ts);
if (ret < 0) {
auxtrace_heap__add(&vpa->heap, queue_nr, ts);
return ret;
}
if (!ret) {
ret = auxtrace_heap__add(&vpa->heap, queue_nr, ts);
if (ret < 0)
return ret;
} else {
vpaq->on_heap = false;
}
}
return 0;
}
static struct powerpc_vpadtl_queue *powerpc_vpadtl__alloc_queue(struct powerpc_vpadtl *vpa,
unsigned int queue_nr)
{
struct powerpc_vpadtl_queue *vpaq;
vpaq = zalloc(sizeof(*vpaq));
if (!vpaq)
return NULL;
vpaq->vpa = vpa;
vpaq->queue_nr = queue_nr;
return vpaq;
}
/*
* When the Dispatch Trace Log data is collected along with other events
* like sched tracepoint events, it needs to be correlated and present
* interleaved along with these events. Perf events can be collected
* parallely across the CPUs.
*
* An auxtrace_queue is created for each CPU. Data within each queue is in
* increasing order of timestamp. Allocate and setup auxtrace queues here.
* All auxtrace queues is maintained in auxtrace heap in the increasing order
* of timestamp. So always the lowest timestamp (entries to be processed first)
* is on top of the heap.
*
* To add to auxtrace heap, fetch the timestamp from first DTL entry
* for each of the queue.
*/
static int powerpc_vpadtl__setup_queue(struct powerpc_vpadtl *vpa,
struct auxtrace_queue *queue,
unsigned int queue_nr)
{
struct powerpc_vpadtl_queue *vpaq = queue->priv;
if (list_empty(&queue->head) || vpaq)
return 0;
vpaq = powerpc_vpadtl__alloc_queue(vpa, queue_nr);
if (!vpaq)
return -ENOMEM;
queue->priv = vpaq;
if (queue->cpu != -1)
vpaq->cpu = queue->cpu;
if (!vpaq->on_heap) {
int ret;
retry:
ret = powerpc_vpadtl_decode(vpaq);
if (!ret)
return 0;
if (ret < 0)
goto retry;
vpaq->timestamp = powerpc_vpadtl_timestamp(vpaq);
ret = auxtrace_heap__add(&vpa->heap, queue_nr, vpaq->timestamp);
if (ret)
return ret;
vpaq->on_heap = true;
}
return 0;
}
static int powerpc_vpadtl__setup_queues(struct powerpc_vpadtl *vpa)
{
unsigned int i;
int ret;
for (i = 0; i < vpa->queues.nr_queues; i++) {
ret = powerpc_vpadtl__setup_queue(vpa, &vpa->queues.queue_array[i], i);
if (ret)
return ret;
}
return 0;
}
static int powerpc_vpadtl__update_queues(struct powerpc_vpadtl *vpa)
{
if (vpa->queues.new_data) {
vpa->queues.new_data = false;
return powerpc_vpadtl__setup_queues(vpa);
}
return 0;
}
static int powerpc_vpadtl_process_event(struct perf_session *session,
union perf_event *event __maybe_unused,
struct perf_sample *sample,
const struct perf_tool *tool)
{
struct powerpc_vpadtl *vpa = session_to_vpa(session);
int err = 0;
if (dump_trace)
return 0;
if (!tool->ordered_events) {
pr_err("VPA requires ordered events\n");
return -EINVAL;
}
if (sample->time) {
err = powerpc_vpadtl__update_queues(vpa);
if (err)
return err;
err = powerpc_vpadtl_process_queues(vpa, sample->time);
}
return err;
}
/*
* Process PERF_RECORD_AUXTRACE records
*/
static int powerpc_vpadtl_process_auxtrace_event(struct perf_session *session,
union perf_event *event,
const struct perf_tool *tool __maybe_unused)
{
struct powerpc_vpadtl *vpa = session_to_vpa(session);
struct auxtrace_buffer *buffer;
int fd = perf_data__fd(session->data);
off_t data_offset;
int err;
if (!dump_trace)
return 0;
if (perf_data__is_pipe(session->data)) {
data_offset = 0;
} else {
data_offset = lseek(fd, 0, SEEK_CUR);
if (data_offset == -1)
return -errno;
}
err = auxtrace_queues__add_event(&vpa->queues, session, event,
data_offset, &buffer);
if (err)
return err;
/* Dump here now we have copied a piped trace out of the pipe */
if (auxtrace_buffer__get_data(buffer, fd)) {
powerpc_vpadtl_dump_event(vpa, buffer->data, buffer->size);
auxtrace_buffer__put_data(buffer);
}
return 0;
}
static int powerpc_vpadtl_flush(struct perf_session *session __maybe_unused,
const struct perf_tool *tool __maybe_unused)
{
return 0;
}
static void powerpc_vpadtl_free_events(struct perf_session *session)
{
struct powerpc_vpadtl *vpa = session_to_vpa(session);
struct auxtrace_queues *queues = &vpa->queues;
for (unsigned int i = 0; i < queues->nr_queues; i++)
zfree(&queues->queue_array[i].priv);
auxtrace_queues__free(queues);
}
static void powerpc_vpadtl_free(struct perf_session *session)
{
struct powerpc_vpadtl *vpa = session_to_vpa(session);
auxtrace_heap__free(&vpa->heap);
powerpc_vpadtl_free_events(session);
session->auxtrace = NULL;
free(vpa);
}
static const char * const powerpc_vpadtl_info_fmts[] = {
[POWERPC_VPADTL_TYPE] = " PMU Type %"PRId64"\n",
};
static void powerpc_vpadtl_print_info(__u64 *arr)
{
if (!dump_trace)
return;
fprintf(stdout, powerpc_vpadtl_info_fmts[POWERPC_VPADTL_TYPE], arr[POWERPC_VPADTL_TYPE]);
}
static void set_event_name(struct evlist *evlist, u64 id,
const char *name)
{
struct evsel *evsel;
evlist__for_each_entry(evlist, evsel) {
if (evsel->core.id && evsel->core.id[0] == id) {
if (evsel->name)
zfree(&evsel->name);
evsel->name = strdup(name);
break;
}
}
}
static int
powerpc_vpadtl_synth_events(struct powerpc_vpadtl *vpa, struct perf_session *session)
{
struct evlist *evlist = session->evlist;
struct evsel *evsel;
struct perf_event_attr attr;
bool found = false;
u64 id;
int err;
evlist__for_each_entry(evlist, evsel) {
if (strstarts(evsel->name, "vpa_dtl")) {
found = true;
break;
}
}
if (!found) {
pr_debug("No selected events with VPA trace data\n");
return 0;
}
memset(&attr, 0, sizeof(struct perf_event_attr));
attr.size = sizeof(struct perf_event_attr);
attr.sample_type = evsel->core.attr.sample_type;
attr.sample_id_all = evsel->core.attr.sample_id_all;
attr.type = PERF_TYPE_SYNTH;
attr.config = PERF_SYNTH_POWERPC_VPA_DTL;
/* create new id val to be a fixed offset from evsel id */
id = auxtrace_synth_id_range_start(evsel);
err = perf_session__deliver_synth_attr_event(session, &attr, id);
if (err)
return err;
vpa->sample_id = id;
set_event_name(evlist, id, "vpa-dtl");
return 0;
}
/*
* Process the PERF_RECORD_AUXTRACE_INFO records and setup
* the infrastructure to process auxtrace events. PERF_RECORD_AUXTRACE_INFO
* is processed first since it is of type perf_user_event_type.
* Initialise the aux buffer queues using auxtrace_queues__init().
* auxtrace_queue is created for each CPU.
*/
int powerpc_vpadtl_process_auxtrace_info(union perf_event *event,
struct perf_session *session)
{
struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
size_t min_sz = sizeof(u64) * POWERPC_VPADTL_TYPE;
struct powerpc_vpadtl *vpa;
int err;
if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) +
min_sz)
return -EINVAL;
vpa = zalloc(sizeof(struct powerpc_vpadtl));
if (!vpa)
return -ENOMEM;
err = auxtrace_queues__init(&vpa->queues);
if (err)
goto err_free;
vpa->session = session;
vpa->machine = &session->machines.host; /* No kvm support */
vpa->auxtrace_type = auxtrace_info->type;
vpa->pmu_type = auxtrace_info->priv[POWERPC_VPADTL_TYPE];
vpa->auxtrace.process_event = powerpc_vpadtl_process_event;
vpa->auxtrace.process_auxtrace_event = powerpc_vpadtl_process_auxtrace_event;
vpa->auxtrace.flush_events = powerpc_vpadtl_flush;
vpa->auxtrace.free_events = powerpc_vpadtl_free_events;
vpa->auxtrace.free = powerpc_vpadtl_free;
session->auxtrace = &vpa->auxtrace;
powerpc_vpadtl_print_info(&auxtrace_info->priv[0]);
if (dump_trace)
return 0;
err = powerpc_vpadtl_synth_events(vpa, session);
if (err)
goto err_free_queues;
err = auxtrace_queues__process_index(&vpa->queues, session);
if (err)
goto err_free_queues;
return 0;
err_free_queues:
auxtrace_queues__free(&vpa->queues);
session->auxtrace = NULL;
err_free:
free(vpa);
return err;
}
|