1 // SPDX-License-Identifier: GPL-2.0 2 #include <errno.h> 3 #include <inttypes.h> 4 #include <linux/kernel.h> 5 #include <traceevent/event-parse.h> 6 #include <api/fs/fs.h> 7 8 #include <byteswap.h> 9 #include <unistd.h> 10 #include <sys/types.h> 11 #include <sys/mman.h> 12 13 #include "evlist.h" 14 #include "evsel.h" 15 #include "memswap.h" 16 #include "session.h" 17 #include "tool.h" 18 #include "sort.h" 19 #include "util.h" 20 #include "cpumap.h" 21 #include "perf_regs.h" 22 #include "asm/bug.h" 23 #include "auxtrace.h" 24 #include "thread.h" 25 #include "thread-stack.h" 26 #include "stat.h" 27 28 static int perf_session__deliver_event(struct perf_session *session, 29 union perf_event *event, 30 struct perf_tool *tool, 31 u64 file_offset); 32 33 static int perf_session__open(struct perf_session *session) 34 { 35 struct perf_data *data = session->data; 36 37 if (perf_session__read_header(session) < 0) { 38 pr_err("incompatible file format (rerun with -v to learn more)\n"); 39 return -1; 40 } 41 42 if (perf_data__is_pipe(data)) 43 return 0; 44 45 if (perf_header__has_feat(&session->header, HEADER_STAT)) 46 return 0; 47 48 if (!perf_evlist__valid_sample_type(session->evlist)) { 49 pr_err("non matching sample_type\n"); 50 return -1; 51 } 52 53 if (!perf_evlist__valid_sample_id_all(session->evlist)) { 54 pr_err("non matching sample_id_all\n"); 55 return -1; 56 } 57 58 if (!perf_evlist__valid_read_format(session->evlist)) { 59 pr_err("non matching read_format\n"); 60 return -1; 61 } 62 63 return 0; 64 } 65 66 void perf_session__set_id_hdr_size(struct perf_session *session) 67 { 68 u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist); 69 70 machines__set_id_hdr_size(&session->machines, id_hdr_size); 71 } 72 73 int perf_session__create_kernel_maps(struct perf_session *session) 74 { 75 int ret = machine__create_kernel_maps(&session->machines.host); 76 77 if (ret >= 0) 78 ret = machines__create_guest_kernel_maps(&session->machines); 79 return ret; 80 } 81 82 static void perf_session__destroy_kernel_maps(struct perf_session *session) 83 { 84 machines__destroy_kernel_maps(&session->machines); 85 } 86 87 static bool perf_session__has_comm_exec(struct perf_session *session) 88 { 89 struct perf_evsel *evsel; 90 91 evlist__for_each_entry(session->evlist, evsel) { 92 if (evsel->attr.comm_exec) 93 return true; 94 } 95 96 return false; 97 } 98 99 static void perf_session__set_comm_exec(struct perf_session *session) 100 { 101 bool comm_exec = perf_session__has_comm_exec(session); 102 103 machines__set_comm_exec(&session->machines, comm_exec); 104 } 105 106 static int ordered_events__deliver_event(struct ordered_events *oe, 107 struct ordered_event *event) 108 { 109 struct perf_session *session = container_of(oe, struct perf_session, 110 ordered_events); 111 112 return perf_session__deliver_event(session, event->event, 113 session->tool, event->file_offset); 114 } 115 116 struct perf_session *perf_session__new(struct perf_data *data, 117 bool repipe, struct perf_tool *tool) 118 { 119 struct perf_session *session = zalloc(sizeof(*session)); 120 121 if (!session) 122 goto out; 123 124 session->repipe = repipe; 125 session->tool = tool; 126 INIT_LIST_HEAD(&session->auxtrace_index); 127 machines__init(&session->machines); 128 ordered_events__init(&session->ordered_events, ordered_events__deliver_event); 129 130 if (data) { 131 if (perf_data__open(data)) 132 goto out_delete; 133 134 session->data = data; 135 136 if (perf_data__is_read(data)) { 137 if (perf_session__open(session) < 0) 138 goto out_close; 139 140 /* 141 * set session attributes that are present in perf.data 142 * but not in pipe-mode. 143 */ 144 if (!data->is_pipe) { 145 perf_session__set_id_hdr_size(session); 146 perf_session__set_comm_exec(session); 147 } 148 } 149 } else { 150 session->machines.host.env = &perf_env; 151 } 152 153 if (!data || perf_data__is_write(data)) { 154 /* 155 * In O_RDONLY mode this will be performed when reading the 156 * kernel MMAP event, in perf_event__process_mmap(). 157 */ 158 if (perf_session__create_kernel_maps(session) < 0) 159 pr_warning("Cannot read kernel map\n"); 160 } 161 162 /* 163 * In pipe-mode, evlist is empty until PERF_RECORD_HEADER_ATTR is 164 * processed, so perf_evlist__sample_id_all is not meaningful here. 165 */ 166 if ((!data || !data->is_pipe) && tool && tool->ordering_requires_timestamps && 167 tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) { 168 dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n"); 169 tool->ordered_events = false; 170 } 171 172 return session; 173 174 out_close: 175 perf_data__close(data); 176 out_delete: 177 perf_session__delete(session); 178 out: 179 return NULL; 180 } 181 182 static void perf_session__delete_threads(struct perf_session *session) 183 { 184 machine__delete_threads(&session->machines.host); 185 } 186 187 void perf_session__delete(struct perf_session *session) 188 { 189 if (session == NULL) 190 return; 191 auxtrace__free(session); 192 auxtrace_index__free(&session->auxtrace_index); 193 perf_session__destroy_kernel_maps(session); 194 perf_session__delete_threads(session); 195 perf_env__exit(&session->header.env); 196 machines__exit(&session->machines); 197 if (session->data) 198 perf_data__close(session->data); 199 free(session); 200 } 201 202 static int process_event_synth_tracing_data_stub(struct perf_session *session 203 __maybe_unused, 204 union perf_event *event 205 __maybe_unused) 206 { 207 dump_printf(": unhandled!\n"); 208 return 0; 209 } 210 211 static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused, 212 union perf_event *event __maybe_unused, 213 struct perf_evlist **pevlist 214 __maybe_unused) 215 { 216 dump_printf(": unhandled!\n"); 217 return 0; 218 } 219 220 static int process_event_synth_event_update_stub(struct perf_tool *tool __maybe_unused, 221 union perf_event *event __maybe_unused, 222 struct perf_evlist **pevlist 223 __maybe_unused) 224 { 225 if (dump_trace) 226 perf_event__fprintf_event_update(event, stdout); 227 228 dump_printf(": unhandled!\n"); 229 return 0; 230 } 231 232 static int process_event_sample_stub(struct perf_tool *tool __maybe_unused, 233 union perf_event *event __maybe_unused, 234 struct perf_sample *sample __maybe_unused, 235 struct perf_evsel *evsel __maybe_unused, 236 struct machine *machine __maybe_unused) 237 { 238 dump_printf(": unhandled!\n"); 239 return 0; 240 } 241 242 static int process_event_stub(struct perf_tool *tool __maybe_unused, 243 union perf_event *event __maybe_unused, 244 struct perf_sample *sample __maybe_unused, 245 struct machine *machine __maybe_unused) 246 { 247 dump_printf(": unhandled!\n"); 248 return 0; 249 } 250 251 static int process_finished_round_stub(struct perf_tool *tool __maybe_unused, 252 union perf_event *event __maybe_unused, 253 struct ordered_events *oe __maybe_unused) 254 { 255 dump_printf(": unhandled!\n"); 256 return 0; 257 } 258 259 static int process_finished_round(struct perf_tool *tool, 260 union perf_event *event, 261 struct ordered_events *oe); 262 263 static int skipn(int fd, off_t n) 264 { 265 char buf[4096]; 266 ssize_t ret; 267 268 while (n > 0) { 269 ret = read(fd, buf, min(n, (off_t)sizeof(buf))); 270 if (ret <= 0) 271 return ret; 272 n -= ret; 273 } 274 275 return 0; 276 } 277 278 static s64 process_event_auxtrace_stub(struct perf_session *session __maybe_unused, 279 union perf_event *event) 280 { 281 dump_printf(": unhandled!\n"); 282 if (perf_data__is_pipe(session->data)) 283 skipn(perf_data__fd(session->data), event->auxtrace.size); 284 return event->auxtrace.size; 285 } 286 287 static int process_event_op2_stub(struct perf_session *session __maybe_unused, 288 union perf_event *event __maybe_unused) 289 { 290 dump_printf(": unhandled!\n"); 291 return 0; 292 } 293 294 295 static 296 int process_event_thread_map_stub(struct perf_session *session __maybe_unused, 297 union perf_event *event __maybe_unused) 298 { 299 if (dump_trace) 300 perf_event__fprintf_thread_map(event, stdout); 301 302 dump_printf(": unhandled!\n"); 303 return 0; 304 } 305 306 static 307 int process_event_cpu_map_stub(struct perf_session *session __maybe_unused, 308 union perf_event *event __maybe_unused) 309 { 310 if (dump_trace) 311 perf_event__fprintf_cpu_map(event, stdout); 312 313 dump_printf(": unhandled!\n"); 314 return 0; 315 } 316 317 static 318 int process_event_stat_config_stub(struct perf_session *session __maybe_unused, 319 union perf_event *event __maybe_unused) 320 { 321 if (dump_trace) 322 perf_event__fprintf_stat_config(event, stdout); 323 324 dump_printf(": unhandled!\n"); 325 return 0; 326 } 327 328 static int process_stat_stub(struct perf_session *perf_session __maybe_unused, 329 union perf_event *event) 330 { 331 if (dump_trace) 332 perf_event__fprintf_stat(event, stdout); 333 334 dump_printf(": unhandled!\n"); 335 return 0; 336 } 337 338 static int process_stat_round_stub(struct perf_session *perf_session __maybe_unused, 339 union perf_event *event) 340 { 341 if (dump_trace) 342 perf_event__fprintf_stat_round(event, stdout); 343 344 dump_printf(": unhandled!\n"); 345 return 0; 346 } 347 348 void perf_tool__fill_defaults(struct perf_tool *tool) 349 { 350 if (tool->sample == NULL) 351 tool->sample = process_event_sample_stub; 352 if (tool->mmap == NULL) 353 tool->mmap = process_event_stub; 354 if (tool->mmap2 == NULL) 355 tool->mmap2 = process_event_stub; 356 if (tool->comm == NULL) 357 tool->comm = process_event_stub; 358 if (tool->namespaces == NULL) 359 tool->namespaces = process_event_stub; 360 if (tool->fork == NULL) 361 tool->fork = process_event_stub; 362 if (tool->exit == NULL) 363 tool->exit = process_event_stub; 364 if (tool->lost == NULL) 365 tool->lost = perf_event__process_lost; 366 if (tool->lost_samples == NULL) 367 tool->lost_samples = perf_event__process_lost_samples; 368 if (tool->aux == NULL) 369 tool->aux = perf_event__process_aux; 370 if (tool->itrace_start == NULL) 371 tool->itrace_start = perf_event__process_itrace_start; 372 if (tool->context_switch == NULL) 373 tool->context_switch = perf_event__process_switch; 374 if (tool->read == NULL) 375 tool->read = process_event_sample_stub; 376 if (tool->throttle == NULL) 377 tool->throttle = process_event_stub; 378 if (tool->unthrottle == NULL) 379 tool->unthrottle = process_event_stub; 380 if (tool->attr == NULL) 381 tool->attr = process_event_synth_attr_stub; 382 if (tool->event_update == NULL) 383 tool->event_update = process_event_synth_event_update_stub; 384 if (tool->tracing_data == NULL) 385 tool->tracing_data = process_event_synth_tracing_data_stub; 386 if (tool->build_id == NULL) 387 tool->build_id = process_event_op2_stub; 388 if (tool->finished_round == NULL) { 389 if (tool->ordered_events) 390 tool->finished_round = process_finished_round; 391 else 392 tool->finished_round = process_finished_round_stub; 393 } 394 if (tool->id_index == NULL) 395 tool->id_index = process_event_op2_stub; 396 if (tool->auxtrace_info == NULL) 397 tool->auxtrace_info = process_event_op2_stub; 398 if (tool->auxtrace == NULL) 399 tool->auxtrace = process_event_auxtrace_stub; 400 if (tool->auxtrace_error == NULL) 401 tool->auxtrace_error = process_event_op2_stub; 402 if (tool->thread_map == NULL) 403 tool->thread_map = process_event_thread_map_stub; 404 if (tool->cpu_map == NULL) 405 tool->cpu_map = process_event_cpu_map_stub; 406 if (tool->stat_config == NULL) 407 tool->stat_config = process_event_stat_config_stub; 408 if (tool->stat == NULL) 409 tool->stat = process_stat_stub; 410 if (tool->stat_round == NULL) 411 tool->stat_round = process_stat_round_stub; 412 if (tool->time_conv == NULL) 413 tool->time_conv = process_event_op2_stub; 414 if (tool->feature == NULL) 415 tool->feature = process_event_op2_stub; 416 } 417 418 static void swap_sample_id_all(union perf_event *event, void *data) 419 { 420 void *end = (void *) event + event->header.size; 421 int size = end - data; 422 423 BUG_ON(size % sizeof(u64)); 424 mem_bswap_64(data, size); 425 } 426 427 static void perf_event__all64_swap(union perf_event *event, 428 bool sample_id_all __maybe_unused) 429 { 430 struct perf_event_header *hdr = &event->header; 431 mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr)); 432 } 433 434 static void perf_event__comm_swap(union perf_event *event, bool sample_id_all) 435 { 436 event->comm.pid = bswap_32(event->comm.pid); 437 event->comm.tid = bswap_32(event->comm.tid); 438 439 if (sample_id_all) { 440 void *data = &event->comm.comm; 441 442 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); 443 swap_sample_id_all(event, data); 444 } 445 } 446 447 static void perf_event__mmap_swap(union perf_event *event, 448 bool sample_id_all) 449 { 450 event->mmap.pid = bswap_32(event->mmap.pid); 451 event->mmap.tid = bswap_32(event->mmap.tid); 452 event->mmap.start = bswap_64(event->mmap.start); 453 event->mmap.len = bswap_64(event->mmap.len); 454 event->mmap.pgoff = bswap_64(event->mmap.pgoff); 455 456 if (sample_id_all) { 457 void *data = &event->mmap.filename; 458 459 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); 460 swap_sample_id_all(event, data); 461 } 462 } 463 464 static void perf_event__mmap2_swap(union perf_event *event, 465 bool sample_id_all) 466 { 467 event->mmap2.pid = bswap_32(event->mmap2.pid); 468 event->mmap2.tid = bswap_32(event->mmap2.tid); 469 event->mmap2.start = bswap_64(event->mmap2.start); 470 event->mmap2.len = bswap_64(event->mmap2.len); 471 event->mmap2.pgoff = bswap_64(event->mmap2.pgoff); 472 event->mmap2.maj = bswap_32(event->mmap2.maj); 473 event->mmap2.min = bswap_32(event->mmap2.min); 474 event->mmap2.ino = bswap_64(event->mmap2.ino); 475 476 if (sample_id_all) { 477 void *data = &event->mmap2.filename; 478 479 data += PERF_ALIGN(strlen(data) + 1, sizeof(u64)); 480 swap_sample_id_all(event, data); 481 } 482 } 483 static void perf_event__task_swap(union perf_event *event, bool sample_id_all) 484 { 485 event->fork.pid = bswap_32(event->fork.pid); 486 event->fork.tid = bswap_32(event->fork.tid); 487 event->fork.ppid = bswap_32(event->fork.ppid); 488 event->fork.ptid = bswap_32(event->fork.ptid); 489 event->fork.time = bswap_64(event->fork.time); 490 491 if (sample_id_all) 492 swap_sample_id_all(event, &event->fork + 1); 493 } 494 495 static void perf_event__read_swap(union perf_event *event, bool sample_id_all) 496 { 497 event->read.pid = bswap_32(event->read.pid); 498 event->read.tid = bswap_32(event->read.tid); 499 event->read.value = bswap_64(event->read.value); 500 event->read.time_enabled = bswap_64(event->read.time_enabled); 501 event->read.time_running = bswap_64(event->read.time_running); 502 event->read.id = bswap_64(event->read.id); 503 504 if (sample_id_all) 505 swap_sample_id_all(event, &event->read + 1); 506 } 507 508 static void perf_event__aux_swap(union perf_event *event, bool sample_id_all) 509 { 510 event->aux.aux_offset = bswap_64(event->aux.aux_offset); 511 event->aux.aux_size = bswap_64(event->aux.aux_size); 512 event->aux.flags = bswap_64(event->aux.flags); 513 514 if (sample_id_all) 515 swap_sample_id_all(event, &event->aux + 1); 516 } 517 518 static void perf_event__itrace_start_swap(union perf_event *event, 519 bool sample_id_all) 520 { 521 event->itrace_start.pid = bswap_32(event->itrace_start.pid); 522 event->itrace_start.tid = bswap_32(event->itrace_start.tid); 523 524 if (sample_id_all) 525 swap_sample_id_all(event, &event->itrace_start + 1); 526 } 527 528 static void perf_event__switch_swap(union perf_event *event, bool sample_id_all) 529 { 530 if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) { 531 event->context_switch.next_prev_pid = 532 bswap_32(event->context_switch.next_prev_pid); 533 event->context_switch.next_prev_tid = 534 bswap_32(event->context_switch.next_prev_tid); 535 } 536 537 if (sample_id_all) 538 swap_sample_id_all(event, &event->context_switch + 1); 539 } 540 541 static void perf_event__throttle_swap(union perf_event *event, 542 bool sample_id_all) 543 { 544 event->throttle.time = bswap_64(event->throttle.time); 545 event->throttle.id = bswap_64(event->throttle.id); 546 event->throttle.stream_id = bswap_64(event->throttle.stream_id); 547 548 if (sample_id_all) 549 swap_sample_id_all(event, &event->throttle + 1); 550 } 551 552 static u8 revbyte(u8 b) 553 { 554 int rev = (b >> 4) | ((b & 0xf) << 4); 555 rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2); 556 rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1); 557 return (u8) rev; 558 } 559 560 /* 561 * XXX this is hack in attempt to carry flags bitfield 562 * through endian village. ABI says: 563 * 564 * Bit-fields are allocated from right to left (least to most significant) 565 * on little-endian implementations and from left to right (most to least 566 * significant) on big-endian implementations. 567 * 568 * The above seems to be byte specific, so we need to reverse each 569 * byte of the bitfield. 'Internet' also says this might be implementation 570 * specific and we probably need proper fix and carry perf_event_attr 571 * bitfield flags in separate data file FEAT_ section. Thought this seems 572 * to work for now. 573 */ 574 static void swap_bitfield(u8 *p, unsigned len) 575 { 576 unsigned i; 577 578 for (i = 0; i < len; i++) { 579 *p = revbyte(*p); 580 p++; 581 } 582 } 583 584 /* exported for swapping attributes in file header */ 585 void perf_event__attr_swap(struct perf_event_attr *attr) 586 { 587 attr->type = bswap_32(attr->type); 588 attr->size = bswap_32(attr->size); 589 590 #define bswap_safe(f, n) \ 591 (attr->size > (offsetof(struct perf_event_attr, f) + \ 592 sizeof(attr->f) * (n))) 593 #define bswap_field(f, sz) \ 594 do { \ 595 if (bswap_safe(f, 0)) \ 596 attr->f = bswap_##sz(attr->f); \ 597 } while(0) 598 #define bswap_field_16(f) bswap_field(f, 16) 599 #define bswap_field_32(f) bswap_field(f, 32) 600 #define bswap_field_64(f) bswap_field(f, 64) 601 602 bswap_field_64(config); 603 bswap_field_64(sample_period); 604 bswap_field_64(sample_type); 605 bswap_field_64(read_format); 606 bswap_field_32(wakeup_events); 607 bswap_field_32(bp_type); 608 bswap_field_64(bp_addr); 609 bswap_field_64(bp_len); 610 bswap_field_64(branch_sample_type); 611 bswap_field_64(sample_regs_user); 612 bswap_field_32(sample_stack_user); 613 bswap_field_32(aux_watermark); 614 bswap_field_16(sample_max_stack); 615 616 /* 617 * After read_format are bitfields. Check read_format because 618 * we are unable to use offsetof on bitfield. 619 */ 620 if (bswap_safe(read_format, 1)) 621 swap_bitfield((u8 *) (&attr->read_format + 1), 622 sizeof(u64)); 623 #undef bswap_field_64 624 #undef bswap_field_32 625 #undef bswap_field 626 #undef bswap_safe 627 } 628 629 static void perf_event__hdr_attr_swap(union perf_event *event, 630 bool sample_id_all __maybe_unused) 631 { 632 size_t size; 633 634 perf_event__attr_swap(&event->attr.attr); 635 636 size = event->header.size; 637 size -= (void *)&event->attr.id - (void *)event; 638 mem_bswap_64(event->attr.id, size); 639 } 640 641 static void perf_event__event_update_swap(union perf_event *event, 642 bool sample_id_all __maybe_unused) 643 { 644 event->event_update.type = bswap_64(event->event_update.type); 645 event->event_update.id = bswap_64(event->event_update.id); 646 } 647 648 static void perf_event__event_type_swap(union perf_event *event, 649 bool sample_id_all __maybe_unused) 650 { 651 event->event_type.event_type.event_id = 652 bswap_64(event->event_type.event_type.event_id); 653 } 654 655 static void perf_event__tracing_data_swap(union perf_event *event, 656 bool sample_id_all __maybe_unused) 657 { 658 event->tracing_data.size = bswap_32(event->tracing_data.size); 659 } 660 661 static void perf_event__auxtrace_info_swap(union perf_event *event, 662 bool sample_id_all __maybe_unused) 663 { 664 size_t size; 665 666 event->auxtrace_info.type = bswap_32(event->auxtrace_info.type); 667 668 size = event->header.size; 669 size -= (void *)&event->auxtrace_info.priv - (void *)event; 670 mem_bswap_64(event->auxtrace_info.priv, size); 671 } 672 673 static void perf_event__auxtrace_swap(union perf_event *event, 674 bool sample_id_all __maybe_unused) 675 { 676 event->auxtrace.size = bswap_64(event->auxtrace.size); 677 event->auxtrace.offset = bswap_64(event->auxtrace.offset); 678 event->auxtrace.reference = bswap_64(event->auxtrace.reference); 679 event->auxtrace.idx = bswap_32(event->auxtrace.idx); 680 event->auxtrace.tid = bswap_32(event->auxtrace.tid); 681 event->auxtrace.cpu = bswap_32(event->auxtrace.cpu); 682 } 683 684 static void perf_event__auxtrace_error_swap(union perf_event *event, 685 bool sample_id_all __maybe_unused) 686 { 687 event->auxtrace_error.type = bswap_32(event->auxtrace_error.type); 688 event->auxtrace_error.code = bswap_32(event->auxtrace_error.code); 689 event->auxtrace_error.cpu = bswap_32(event->auxtrace_error.cpu); 690 event->auxtrace_error.pid = bswap_32(event->auxtrace_error.pid); 691 event->auxtrace_error.tid = bswap_32(event->auxtrace_error.tid); 692 event->auxtrace_error.ip = bswap_64(event->auxtrace_error.ip); 693 } 694 695 static void perf_event__thread_map_swap(union perf_event *event, 696 bool sample_id_all __maybe_unused) 697 { 698 unsigned i; 699 700 event->thread_map.nr = bswap_64(event->thread_map.nr); 701 702 for (i = 0; i < event->thread_map.nr; i++) 703 event->thread_map.entries[i].pid = bswap_64(event->thread_map.entries[i].pid); 704 } 705 706 static void perf_event__cpu_map_swap(union perf_event *event, 707 bool sample_id_all __maybe_unused) 708 { 709 struct cpu_map_data *data = &event->cpu_map.data; 710 struct cpu_map_entries *cpus; 711 struct cpu_map_mask *mask; 712 unsigned i; 713 714 data->type = bswap_64(data->type); 715 716 switch (data->type) { 717 case PERF_CPU_MAP__CPUS: 718 cpus = (struct cpu_map_entries *)data->data; 719 720 cpus->nr = bswap_16(cpus->nr); 721 722 for (i = 0; i < cpus->nr; i++) 723 cpus->cpu[i] = bswap_16(cpus->cpu[i]); 724 break; 725 case PERF_CPU_MAP__MASK: 726 mask = (struct cpu_map_mask *) data->data; 727 728 mask->nr = bswap_16(mask->nr); 729 mask->long_size = bswap_16(mask->long_size); 730 731 switch (mask->long_size) { 732 case 4: mem_bswap_32(&mask->mask, mask->nr); break; 733 case 8: mem_bswap_64(&mask->mask, mask->nr); break; 734 default: 735 pr_err("cpu_map swap: unsupported long size\n"); 736 } 737 default: 738 break; 739 } 740 } 741 742 static void perf_event__stat_config_swap(union perf_event *event, 743 bool sample_id_all __maybe_unused) 744 { 745 u64 size; 746 747 size = event->stat_config.nr * sizeof(event->stat_config.data[0]); 748 size += 1; /* nr item itself */ 749 mem_bswap_64(&event->stat_config.nr, size); 750 } 751 752 static void perf_event__stat_swap(union perf_event *event, 753 bool sample_id_all __maybe_unused) 754 { 755 event->stat.id = bswap_64(event->stat.id); 756 event->stat.thread = bswap_32(event->stat.thread); 757 event->stat.cpu = bswap_32(event->stat.cpu); 758 event->stat.val = bswap_64(event->stat.val); 759 event->stat.ena = bswap_64(event->stat.ena); 760 event->stat.run = bswap_64(event->stat.run); 761 } 762 763 static void perf_event__stat_round_swap(union perf_event *event, 764 bool sample_id_all __maybe_unused) 765 { 766 event->stat_round.type = bswap_64(event->stat_round.type); 767 event->stat_round.time = bswap_64(event->stat_round.time); 768 } 769 770 typedef void (*perf_event__swap_op)(union perf_event *event, 771 bool sample_id_all); 772 773 static perf_event__swap_op perf_event__swap_ops[] = { 774 [PERF_RECORD_MMAP] = perf_event__mmap_swap, 775 [PERF_RECORD_MMAP2] = perf_event__mmap2_swap, 776 [PERF_RECORD_COMM] = perf_event__comm_swap, 777 [PERF_RECORD_FORK] = perf_event__task_swap, 778 [PERF_RECORD_EXIT] = perf_event__task_swap, 779 [PERF_RECORD_LOST] = perf_event__all64_swap, 780 [PERF_RECORD_READ] = perf_event__read_swap, 781 [PERF_RECORD_THROTTLE] = perf_event__throttle_swap, 782 [PERF_RECORD_UNTHROTTLE] = perf_event__throttle_swap, 783 [PERF_RECORD_SAMPLE] = perf_event__all64_swap, 784 [PERF_RECORD_AUX] = perf_event__aux_swap, 785 [PERF_RECORD_ITRACE_START] = perf_event__itrace_start_swap, 786 [PERF_RECORD_LOST_SAMPLES] = perf_event__all64_swap, 787 [PERF_RECORD_SWITCH] = perf_event__switch_swap, 788 [PERF_RECORD_SWITCH_CPU_WIDE] = perf_event__switch_swap, 789 [PERF_RECORD_HEADER_ATTR] = perf_event__hdr_attr_swap, 790 [PERF_RECORD_HEADER_EVENT_TYPE] = perf_event__event_type_swap, 791 [PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap, 792 [PERF_RECORD_HEADER_BUILD_ID] = NULL, 793 [PERF_RECORD_ID_INDEX] = perf_event__all64_swap, 794 [PERF_RECORD_AUXTRACE_INFO] = perf_event__auxtrace_info_swap, 795 [PERF_RECORD_AUXTRACE] = perf_event__auxtrace_swap, 796 [PERF_RECORD_AUXTRACE_ERROR] = perf_event__auxtrace_error_swap, 797 [PERF_RECORD_THREAD_MAP] = perf_event__thread_map_swap, 798 [PERF_RECORD_CPU_MAP] = perf_event__cpu_map_swap, 799 [PERF_RECORD_STAT_CONFIG] = perf_event__stat_config_swap, 800 [PERF_RECORD_STAT] = perf_event__stat_swap, 801 [PERF_RECORD_STAT_ROUND] = perf_event__stat_round_swap, 802 [PERF_RECORD_EVENT_UPDATE] = perf_event__event_update_swap, 803 [PERF_RECORD_TIME_CONV] = perf_event__all64_swap, 804 [PERF_RECORD_HEADER_MAX] = NULL, 805 }; 806 807 /* 808 * When perf record finishes a pass on every buffers, it records this pseudo 809 * event. 810 * We record the max timestamp t found in the pass n. 811 * Assuming these timestamps are monotonic across cpus, we know that if 812 * a buffer still has events with timestamps below t, they will be all 813 * available and then read in the pass n + 1. 814 * Hence when we start to read the pass n + 2, we can safely flush every 815 * events with timestamps below t. 816 * 817 * ============ PASS n ================= 818 * CPU 0 | CPU 1 819 * | 820 * cnt1 timestamps | cnt2 timestamps 821 * 1 | 2 822 * 2 | 3 823 * - | 4 <--- max recorded 824 * 825 * ============ PASS n + 1 ============== 826 * CPU 0 | CPU 1 827 * | 828 * cnt1 timestamps | cnt2 timestamps 829 * 3 | 5 830 * 4 | 6 831 * 5 | 7 <---- max recorded 832 * 833 * Flush every events below timestamp 4 834 * 835 * ============ PASS n + 2 ============== 836 * CPU 0 | CPU 1 837 * | 838 * cnt1 timestamps | cnt2 timestamps 839 * 6 | 8 840 * 7 | 9 841 * - | 10 842 * 843 * Flush every events below timestamp 7 844 * etc... 845 */ 846 static int process_finished_round(struct perf_tool *tool __maybe_unused, 847 union perf_event *event __maybe_unused, 848 struct ordered_events *oe) 849 { 850 if (dump_trace) 851 fprintf(stdout, "\n"); 852 return ordered_events__flush(oe, OE_FLUSH__ROUND); 853 } 854 855 int perf_session__queue_event(struct perf_session *s, union perf_event *event, 856 u64 timestamp, u64 file_offset) 857 { 858 return ordered_events__queue(&s->ordered_events, event, timestamp, file_offset); 859 } 860 861 static void callchain__lbr_callstack_printf(struct perf_sample *sample) 862 { 863 struct ip_callchain *callchain = sample->callchain; 864 struct branch_stack *lbr_stack = sample->branch_stack; 865 u64 kernel_callchain_nr = callchain->nr; 866 unsigned int i; 867 868 for (i = 0; i < kernel_callchain_nr; i++) { 869 if (callchain->ips[i] == PERF_CONTEXT_USER) 870 break; 871 } 872 873 if ((i != kernel_callchain_nr) && lbr_stack->nr) { 874 u64 total_nr; 875 /* 876 * LBR callstack can only get user call chain, 877 * i is kernel call chain number, 878 * 1 is PERF_CONTEXT_USER. 879 * 880 * The user call chain is stored in LBR registers. 881 * LBR are pair registers. The caller is stored 882 * in "from" register, while the callee is stored 883 * in "to" register. 884 * For example, there is a call stack 885 * "A"->"B"->"C"->"D". 886 * The LBR registers will recorde like 887 * "C"->"D", "B"->"C", "A"->"B". 888 * So only the first "to" register and all "from" 889 * registers are needed to construct the whole stack. 890 */ 891 total_nr = i + 1 + lbr_stack->nr + 1; 892 kernel_callchain_nr = i + 1; 893 894 printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr); 895 896 for (i = 0; i < kernel_callchain_nr; i++) 897 printf("..... %2d: %016" PRIx64 "\n", 898 i, callchain->ips[i]); 899 900 printf("..... %2d: %016" PRIx64 "\n", 901 (int)(kernel_callchain_nr), lbr_stack->entries[0].to); 902 for (i = 0; i < lbr_stack->nr; i++) 903 printf("..... %2d: %016" PRIx64 "\n", 904 (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from); 905 } 906 } 907 908 static void callchain__printf(struct perf_evsel *evsel, 909 struct perf_sample *sample) 910 { 911 unsigned int i; 912 struct ip_callchain *callchain = sample->callchain; 913 914 if (perf_evsel__has_branch_callstack(evsel)) 915 callchain__lbr_callstack_printf(sample); 916 917 printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr); 918 919 for (i = 0; i < callchain->nr; i++) 920 printf("..... %2d: %016" PRIx64 "\n", 921 i, callchain->ips[i]); 922 } 923 924 static void branch_stack__printf(struct perf_sample *sample) 925 { 926 uint64_t i; 927 928 printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr); 929 930 for (i = 0; i < sample->branch_stack->nr; i++) { 931 struct branch_entry *e = &sample->branch_stack->entries[i]; 932 933 printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x\n", 934 i, e->from, e->to, 935 (unsigned short)e->flags.cycles, 936 e->flags.mispred ? "M" : " ", 937 e->flags.predicted ? "P" : " ", 938 e->flags.abort ? "A" : " ", 939 e->flags.in_tx ? "T" : " ", 940 (unsigned)e->flags.reserved); 941 } 942 } 943 944 static void regs_dump__printf(u64 mask, u64 *regs) 945 { 946 unsigned rid, i = 0; 947 948 for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) { 949 u64 val = regs[i++]; 950 951 printf(".... %-5s 0x%" PRIx64 "\n", 952 perf_reg_name(rid), val); 953 } 954 } 955 956 static const char *regs_abi[] = { 957 [PERF_SAMPLE_REGS_ABI_NONE] = "none", 958 [PERF_SAMPLE_REGS_ABI_32] = "32-bit", 959 [PERF_SAMPLE_REGS_ABI_64] = "64-bit", 960 }; 961 962 static inline const char *regs_dump_abi(struct regs_dump *d) 963 { 964 if (d->abi > PERF_SAMPLE_REGS_ABI_64) 965 return "unknown"; 966 967 return regs_abi[d->abi]; 968 } 969 970 static void regs__printf(const char *type, struct regs_dump *regs) 971 { 972 u64 mask = regs->mask; 973 974 printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n", 975 type, 976 mask, 977 regs_dump_abi(regs)); 978 979 regs_dump__printf(mask, regs->regs); 980 } 981 982 static void regs_user__printf(struct perf_sample *sample) 983 { 984 struct regs_dump *user_regs = &sample->user_regs; 985 986 if (user_regs->regs) 987 regs__printf("user", user_regs); 988 } 989 990 static void regs_intr__printf(struct perf_sample *sample) 991 { 992 struct regs_dump *intr_regs = &sample->intr_regs; 993 994 if (intr_regs->regs) 995 regs__printf("intr", intr_regs); 996 } 997 998 static void stack_user__printf(struct stack_dump *dump) 999 { 1000 printf("... ustack: size %" PRIu64 ", offset 0x%x\n", 1001 dump->size, dump->offset); 1002 } 1003 1004 static void perf_evlist__print_tstamp(struct perf_evlist *evlist, 1005 union perf_event *event, 1006 struct perf_sample *sample) 1007 { 1008 u64 sample_type = __perf_evlist__combined_sample_type(evlist); 1009 1010 if (event->header.type != PERF_RECORD_SAMPLE && 1011 !perf_evlist__sample_id_all(evlist)) { 1012 fputs("-1 -1 ", stdout); 1013 return; 1014 } 1015 1016 if ((sample_type & PERF_SAMPLE_CPU)) 1017 printf("%u ", sample->cpu); 1018 1019 if (sample_type & PERF_SAMPLE_TIME) 1020 printf("%" PRIu64 " ", sample->time); 1021 } 1022 1023 static void sample_read__printf(struct perf_sample *sample, u64 read_format) 1024 { 1025 printf("... sample_read:\n"); 1026 1027 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 1028 printf("...... time enabled %016" PRIx64 "\n", 1029 sample->read.time_enabled); 1030 1031 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 1032 printf("...... time running %016" PRIx64 "\n", 1033 sample->read.time_running); 1034 1035 if (read_format & PERF_FORMAT_GROUP) { 1036 u64 i; 1037 1038 printf(".... group nr %" PRIu64 "\n", sample->read.group.nr); 1039 1040 for (i = 0; i < sample->read.group.nr; i++) { 1041 struct sample_read_value *value; 1042 1043 value = &sample->read.group.values[i]; 1044 printf("..... id %016" PRIx64 1045 ", value %016" PRIx64 "\n", 1046 value->id, value->value); 1047 } 1048 } else 1049 printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n", 1050 sample->read.one.id, sample->read.one.value); 1051 } 1052 1053 static void dump_event(struct perf_evlist *evlist, union perf_event *event, 1054 u64 file_offset, struct perf_sample *sample) 1055 { 1056 if (!dump_trace) 1057 return; 1058 1059 printf("\n%#" PRIx64 " [%#x]: event: %d\n", 1060 file_offset, event->header.size, event->header.type); 1061 1062 trace_event(event); 1063 1064 if (sample) 1065 perf_evlist__print_tstamp(evlist, event, sample); 1066 1067 printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset, 1068 event->header.size, perf_event__name(event->header.type)); 1069 } 1070 1071 static void dump_sample(struct perf_evsel *evsel, union perf_event *event, 1072 struct perf_sample *sample) 1073 { 1074 u64 sample_type; 1075 1076 if (!dump_trace) 1077 return; 1078 1079 printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n", 1080 event->header.misc, sample->pid, sample->tid, sample->ip, 1081 sample->period, sample->addr); 1082 1083 sample_type = evsel->attr.sample_type; 1084 1085 if (evsel__has_callchain(evsel)) 1086 callchain__printf(evsel, sample); 1087 1088 if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !perf_evsel__has_branch_callstack(evsel)) 1089 branch_stack__printf(sample); 1090 1091 if (sample_type & PERF_SAMPLE_REGS_USER) 1092 regs_user__printf(sample); 1093 1094 if (sample_type & PERF_SAMPLE_REGS_INTR) 1095 regs_intr__printf(sample); 1096 1097 if (sample_type & PERF_SAMPLE_STACK_USER) 1098 stack_user__printf(&sample->user_stack); 1099 1100 if (sample_type & PERF_SAMPLE_WEIGHT) 1101 printf("... weight: %" PRIu64 "\n", sample->weight); 1102 1103 if (sample_type & PERF_SAMPLE_DATA_SRC) 1104 printf(" . data_src: 0x%"PRIx64"\n", sample->data_src); 1105 1106 if (sample_type & PERF_SAMPLE_PHYS_ADDR) 1107 printf(" .. phys_addr: 0x%"PRIx64"\n", sample->phys_addr); 1108 1109 if (sample_type & PERF_SAMPLE_TRANSACTION) 1110 printf("... transaction: %" PRIx64 "\n", sample->transaction); 1111 1112 if (sample_type & PERF_SAMPLE_READ) 1113 sample_read__printf(sample, evsel->attr.read_format); 1114 } 1115 1116 static void dump_read(struct perf_evsel *evsel, union perf_event *event) 1117 { 1118 struct read_event *read_event = &event->read; 1119 u64 read_format; 1120 1121 if (!dump_trace) 1122 return; 1123 1124 printf(": %d %d %s %" PRIu64 "\n", event->read.pid, event->read.tid, 1125 evsel ? perf_evsel__name(evsel) : "FAIL", 1126 event->read.value); 1127 1128 read_format = evsel->attr.read_format; 1129 1130 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 1131 printf("... time enabled : %" PRIu64 "\n", read_event->time_enabled); 1132 1133 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 1134 printf("... time running : %" PRIu64 "\n", read_event->time_running); 1135 1136 if (read_format & PERF_FORMAT_ID) 1137 printf("... id : %" PRIu64 "\n", read_event->id); 1138 } 1139 1140 static struct machine *machines__find_for_cpumode(struct machines *machines, 1141 union perf_event *event, 1142 struct perf_sample *sample) 1143 { 1144 struct machine *machine; 1145 1146 if (perf_guest && 1147 ((sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL) || 1148 (sample->cpumode == PERF_RECORD_MISC_GUEST_USER))) { 1149 u32 pid; 1150 1151 if (event->header.type == PERF_RECORD_MMAP 1152 || event->header.type == PERF_RECORD_MMAP2) 1153 pid = event->mmap.pid; 1154 else 1155 pid = sample->pid; 1156 1157 machine = machines__find(machines, pid); 1158 if (!machine) 1159 machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID); 1160 return machine; 1161 } 1162 1163 return &machines->host; 1164 } 1165 1166 static int deliver_sample_value(struct perf_evlist *evlist, 1167 struct perf_tool *tool, 1168 union perf_event *event, 1169 struct perf_sample *sample, 1170 struct sample_read_value *v, 1171 struct machine *machine) 1172 { 1173 struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id); 1174 1175 if (sid) { 1176 sample->id = v->id; 1177 sample->period = v->value - sid->period; 1178 sid->period = v->value; 1179 } 1180 1181 if (!sid || sid->evsel == NULL) { 1182 ++evlist->stats.nr_unknown_id; 1183 return 0; 1184 } 1185 1186 return tool->sample(tool, event, sample, sid->evsel, machine); 1187 } 1188 1189 static int deliver_sample_group(struct perf_evlist *evlist, 1190 struct perf_tool *tool, 1191 union perf_event *event, 1192 struct perf_sample *sample, 1193 struct machine *machine) 1194 { 1195 int ret = -EINVAL; 1196 u64 i; 1197 1198 for (i = 0; i < sample->read.group.nr; i++) { 1199 ret = deliver_sample_value(evlist, tool, event, sample, 1200 &sample->read.group.values[i], 1201 machine); 1202 if (ret) 1203 break; 1204 } 1205 1206 return ret; 1207 } 1208 1209 static int 1210 perf_evlist__deliver_sample(struct perf_evlist *evlist, 1211 struct perf_tool *tool, 1212 union perf_event *event, 1213 struct perf_sample *sample, 1214 struct perf_evsel *evsel, 1215 struct machine *machine) 1216 { 1217 /* We know evsel != NULL. */ 1218 u64 sample_type = evsel->attr.sample_type; 1219 u64 read_format = evsel->attr.read_format; 1220 1221 /* Standard sample delivery. */ 1222 if (!(sample_type & PERF_SAMPLE_READ)) 1223 return tool->sample(tool, event, sample, evsel, machine); 1224 1225 /* For PERF_SAMPLE_READ we have either single or group mode. */ 1226 if (read_format & PERF_FORMAT_GROUP) 1227 return deliver_sample_group(evlist, tool, event, sample, 1228 machine); 1229 else 1230 return deliver_sample_value(evlist, tool, event, sample, 1231 &sample->read.one, machine); 1232 } 1233 1234 static int machines__deliver_event(struct machines *machines, 1235 struct perf_evlist *evlist, 1236 union perf_event *event, 1237 struct perf_sample *sample, 1238 struct perf_tool *tool, u64 file_offset) 1239 { 1240 struct perf_evsel *evsel; 1241 struct machine *machine; 1242 1243 dump_event(evlist, event, file_offset, sample); 1244 1245 evsel = perf_evlist__id2evsel(evlist, sample->id); 1246 1247 machine = machines__find_for_cpumode(machines, event, sample); 1248 1249 switch (event->header.type) { 1250 case PERF_RECORD_SAMPLE: 1251 if (evsel == NULL) { 1252 ++evlist->stats.nr_unknown_id; 1253 return 0; 1254 } 1255 dump_sample(evsel, event, sample); 1256 if (machine == NULL) { 1257 ++evlist->stats.nr_unprocessable_samples; 1258 return 0; 1259 } 1260 return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine); 1261 case PERF_RECORD_MMAP: 1262 return tool->mmap(tool, event, sample, machine); 1263 case PERF_RECORD_MMAP2: 1264 if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT) 1265 ++evlist->stats.nr_proc_map_timeout; 1266 return tool->mmap2(tool, event, sample, machine); 1267 case PERF_RECORD_COMM: 1268 return tool->comm(tool, event, sample, machine); 1269 case PERF_RECORD_NAMESPACES: 1270 return tool->namespaces(tool, event, sample, machine); 1271 case PERF_RECORD_FORK: 1272 return tool->fork(tool, event, sample, machine); 1273 case PERF_RECORD_EXIT: 1274 return tool->exit(tool, event, sample, machine); 1275 case PERF_RECORD_LOST: 1276 if (tool->lost == perf_event__process_lost) 1277 evlist->stats.total_lost += event->lost.lost; 1278 return tool->lost(tool, event, sample, machine); 1279 case PERF_RECORD_LOST_SAMPLES: 1280 if (tool->lost_samples == perf_event__process_lost_samples) 1281 evlist->stats.total_lost_samples += event->lost_samples.lost; 1282 return tool->lost_samples(tool, event, sample, machine); 1283 case PERF_RECORD_READ: 1284 dump_read(evsel, event); 1285 return tool->read(tool, event, sample, evsel, machine); 1286 case PERF_RECORD_THROTTLE: 1287 return tool->throttle(tool, event, sample, machine); 1288 case PERF_RECORD_UNTHROTTLE: 1289 return tool->unthrottle(tool, event, sample, machine); 1290 case PERF_RECORD_AUX: 1291 if (tool->aux == perf_event__process_aux) { 1292 if (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) 1293 evlist->stats.total_aux_lost += 1; 1294 if (event->aux.flags & PERF_AUX_FLAG_PARTIAL) 1295 evlist->stats.total_aux_partial += 1; 1296 } 1297 return tool->aux(tool, event, sample, machine); 1298 case PERF_RECORD_ITRACE_START: 1299 return tool->itrace_start(tool, event, sample, machine); 1300 case PERF_RECORD_SWITCH: 1301 case PERF_RECORD_SWITCH_CPU_WIDE: 1302 return tool->context_switch(tool, event, sample, machine); 1303 default: 1304 ++evlist->stats.nr_unknown_events; 1305 return -1; 1306 } 1307 } 1308 1309 static int perf_session__deliver_event(struct perf_session *session, 1310 union perf_event *event, 1311 struct perf_tool *tool, 1312 u64 file_offset) 1313 { 1314 struct perf_sample sample; 1315 int ret; 1316 1317 ret = perf_evlist__parse_sample(session->evlist, event, &sample); 1318 if (ret) { 1319 pr_err("Can't parse sample, err = %d\n", ret); 1320 return ret; 1321 } 1322 1323 ret = auxtrace__process_event(session, event, &sample, tool); 1324 if (ret < 0) 1325 return ret; 1326 if (ret > 0) 1327 return 0; 1328 1329 return machines__deliver_event(&session->machines, session->evlist, 1330 event, &sample, tool, file_offset); 1331 } 1332 1333 static s64 perf_session__process_user_event(struct perf_session *session, 1334 union perf_event *event, 1335 u64 file_offset) 1336 { 1337 struct ordered_events *oe = &session->ordered_events; 1338 struct perf_tool *tool = session->tool; 1339 struct perf_sample sample = { .time = 0, }; 1340 int fd = perf_data__fd(session->data); 1341 int err; 1342 1343 dump_event(session->evlist, event, file_offset, &sample); 1344 1345 /* These events are processed right away */ 1346 switch (event->header.type) { 1347 case PERF_RECORD_HEADER_ATTR: 1348 err = tool->attr(tool, event, &session->evlist); 1349 if (err == 0) { 1350 perf_session__set_id_hdr_size(session); 1351 perf_session__set_comm_exec(session); 1352 } 1353 return err; 1354 case PERF_RECORD_EVENT_UPDATE: 1355 return tool->event_update(tool, event, &session->evlist); 1356 case PERF_RECORD_HEADER_EVENT_TYPE: 1357 /* 1358 * Depreceated, but we need to handle it for sake 1359 * of old data files create in pipe mode. 1360 */ 1361 return 0; 1362 case PERF_RECORD_HEADER_TRACING_DATA: 1363 /* setup for reading amidst mmap */ 1364 lseek(fd, file_offset, SEEK_SET); 1365 return tool->tracing_data(session, event); 1366 case PERF_RECORD_HEADER_BUILD_ID: 1367 return tool->build_id(session, event); 1368 case PERF_RECORD_FINISHED_ROUND: 1369 return tool->finished_round(tool, event, oe); 1370 case PERF_RECORD_ID_INDEX: 1371 return tool->id_index(session, event); 1372 case PERF_RECORD_AUXTRACE_INFO: 1373 return tool->auxtrace_info(session, event); 1374 case PERF_RECORD_AUXTRACE: 1375 /* setup for reading amidst mmap */ 1376 lseek(fd, file_offset + event->header.size, SEEK_SET); 1377 return tool->auxtrace(session, event); 1378 case PERF_RECORD_AUXTRACE_ERROR: 1379 perf_session__auxtrace_error_inc(session, event); 1380 return tool->auxtrace_error(session, event); 1381 case PERF_RECORD_THREAD_MAP: 1382 return tool->thread_map(session, event); 1383 case PERF_RECORD_CPU_MAP: 1384 return tool->cpu_map(session, event); 1385 case PERF_RECORD_STAT_CONFIG: 1386 return tool->stat_config(session, event); 1387 case PERF_RECORD_STAT: 1388 return tool->stat(session, event); 1389 case PERF_RECORD_STAT_ROUND: 1390 return tool->stat_round(session, event); 1391 case PERF_RECORD_TIME_CONV: 1392 session->time_conv = event->time_conv; 1393 return tool->time_conv(session, event); 1394 case PERF_RECORD_HEADER_FEATURE: 1395 return tool->feature(session, event); 1396 default: 1397 return -EINVAL; 1398 } 1399 } 1400 1401 int perf_session__deliver_synth_event(struct perf_session *session, 1402 union perf_event *event, 1403 struct perf_sample *sample) 1404 { 1405 struct perf_evlist *evlist = session->evlist; 1406 struct perf_tool *tool = session->tool; 1407 1408 events_stats__inc(&evlist->stats, event->header.type); 1409 1410 if (event->header.type >= PERF_RECORD_USER_TYPE_START) 1411 return perf_session__process_user_event(session, event, 0); 1412 1413 return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0); 1414 } 1415 1416 static void event_swap(union perf_event *event, bool sample_id_all) 1417 { 1418 perf_event__swap_op swap; 1419 1420 swap = perf_event__swap_ops[event->header.type]; 1421 if (swap) 1422 swap(event, sample_id_all); 1423 } 1424 1425 int perf_session__peek_event(struct perf_session *session, off_t file_offset, 1426 void *buf, size_t buf_sz, 1427 union perf_event **event_ptr, 1428 struct perf_sample *sample) 1429 { 1430 union perf_event *event; 1431 size_t hdr_sz, rest; 1432 int fd; 1433 1434 if (session->one_mmap && !session->header.needs_swap) { 1435 event = file_offset - session->one_mmap_offset + 1436 session->one_mmap_addr; 1437 goto out_parse_sample; 1438 } 1439 1440 if (perf_data__is_pipe(session->data)) 1441 return -1; 1442 1443 fd = perf_data__fd(session->data); 1444 hdr_sz = sizeof(struct perf_event_header); 1445 1446 if (buf_sz < hdr_sz) 1447 return -1; 1448 1449 if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 || 1450 readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz) 1451 return -1; 1452 1453 event = (union perf_event *)buf; 1454 1455 if (session->header.needs_swap) 1456 perf_event_header__bswap(&event->header); 1457 1458 if (event->header.size < hdr_sz || event->header.size > buf_sz) 1459 return -1; 1460 1461 rest = event->header.size - hdr_sz; 1462 1463 if (readn(fd, buf, rest) != (ssize_t)rest) 1464 return -1; 1465 1466 if (session->header.needs_swap) 1467 event_swap(event, perf_evlist__sample_id_all(session->evlist)); 1468 1469 out_parse_sample: 1470 1471 if (sample && event->header.type < PERF_RECORD_USER_TYPE_START && 1472 perf_evlist__parse_sample(session->evlist, event, sample)) 1473 return -1; 1474 1475 *event_ptr = event; 1476 1477 return 0; 1478 } 1479 1480 static s64 perf_session__process_event(struct perf_session *session, 1481 union perf_event *event, u64 file_offset) 1482 { 1483 struct perf_evlist *evlist = session->evlist; 1484 struct perf_tool *tool = session->tool; 1485 int ret; 1486 1487 if (session->header.needs_swap) 1488 event_swap(event, perf_evlist__sample_id_all(evlist)); 1489 1490 if (event->header.type >= PERF_RECORD_HEADER_MAX) 1491 return -EINVAL; 1492 1493 events_stats__inc(&evlist->stats, event->header.type); 1494 1495 if (event->header.type >= PERF_RECORD_USER_TYPE_START) 1496 return perf_session__process_user_event(session, event, file_offset); 1497 1498 if (tool->ordered_events) { 1499 u64 timestamp = -1ULL; 1500 1501 ret = perf_evlist__parse_sample_timestamp(evlist, event, ×tamp); 1502 if (ret && ret != -1) 1503 return ret; 1504 1505 ret = perf_session__queue_event(session, event, timestamp, file_offset); 1506 if (ret != -ETIME) 1507 return ret; 1508 } 1509 1510 return perf_session__deliver_event(session, event, tool, file_offset); 1511 } 1512 1513 void perf_event_header__bswap(struct perf_event_header *hdr) 1514 { 1515 hdr->type = bswap_32(hdr->type); 1516 hdr->misc = bswap_16(hdr->misc); 1517 hdr->size = bswap_16(hdr->size); 1518 } 1519 1520 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid) 1521 { 1522 return machine__findnew_thread(&session->machines.host, -1, pid); 1523 } 1524 1525 int perf_session__register_idle_thread(struct perf_session *session) 1526 { 1527 struct thread *thread; 1528 int err = 0; 1529 1530 thread = machine__findnew_thread(&session->machines.host, 0, 0); 1531 if (thread == NULL || thread__set_comm(thread, "swapper", 0)) { 1532 pr_err("problem inserting idle task.\n"); 1533 err = -1; 1534 } 1535 1536 if (thread == NULL || thread__set_namespaces(thread, 0, NULL)) { 1537 pr_err("problem inserting idle task.\n"); 1538 err = -1; 1539 } 1540 1541 /* machine__findnew_thread() got the thread, so put it */ 1542 thread__put(thread); 1543 return err; 1544 } 1545 1546 static void 1547 perf_session__warn_order(const struct perf_session *session) 1548 { 1549 const struct ordered_events *oe = &session->ordered_events; 1550 struct perf_evsel *evsel; 1551 bool should_warn = true; 1552 1553 evlist__for_each_entry(session->evlist, evsel) { 1554 if (evsel->attr.write_backward) 1555 should_warn = false; 1556 } 1557 1558 if (!should_warn) 1559 return; 1560 if (oe->nr_unordered_events != 0) 1561 ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events); 1562 } 1563 1564 static void perf_session__warn_about_errors(const struct perf_session *session) 1565 { 1566 const struct events_stats *stats = &session->evlist->stats; 1567 1568 if (session->tool->lost == perf_event__process_lost && 1569 stats->nr_events[PERF_RECORD_LOST] != 0) { 1570 ui__warning("Processed %d events and lost %d chunks!\n\n" 1571 "Check IO/CPU overload!\n\n", 1572 stats->nr_events[0], 1573 stats->nr_events[PERF_RECORD_LOST]); 1574 } 1575 1576 if (session->tool->lost_samples == perf_event__process_lost_samples) { 1577 double drop_rate; 1578 1579 drop_rate = (double)stats->total_lost_samples / 1580 (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples); 1581 if (drop_rate > 0.05) { 1582 ui__warning("Processed %" PRIu64 " samples and lost %3.2f%%!\n\n", 1583 stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples, 1584 drop_rate * 100.0); 1585 } 1586 } 1587 1588 if (session->tool->aux == perf_event__process_aux && 1589 stats->total_aux_lost != 0) { 1590 ui__warning("AUX data lost %" PRIu64 " times out of %u!\n\n", 1591 stats->total_aux_lost, 1592 stats->nr_events[PERF_RECORD_AUX]); 1593 } 1594 1595 if (session->tool->aux == perf_event__process_aux && 1596 stats->total_aux_partial != 0) { 1597 bool vmm_exclusive = false; 1598 1599 (void)sysfs__read_bool("module/kvm_intel/parameters/vmm_exclusive", 1600 &vmm_exclusive); 1601 1602 ui__warning("AUX data had gaps in it %" PRIu64 " times out of %u!\n\n" 1603 "Are you running a KVM guest in the background?%s\n\n", 1604 stats->total_aux_partial, 1605 stats->nr_events[PERF_RECORD_AUX], 1606 vmm_exclusive ? 1607 "\nReloading kvm_intel module with vmm_exclusive=0\n" 1608 "will reduce the gaps to only guest's timeslices." : 1609 ""); 1610 } 1611 1612 if (stats->nr_unknown_events != 0) { 1613 ui__warning("Found %u unknown events!\n\n" 1614 "Is this an older tool processing a perf.data " 1615 "file generated by a more recent tool?\n\n" 1616 "If that is not the case, consider " 1617 "reporting to linux-kernel@vger.kernel.org.\n\n", 1618 stats->nr_unknown_events); 1619 } 1620 1621 if (stats->nr_unknown_id != 0) { 1622 ui__warning("%u samples with id not present in the header\n", 1623 stats->nr_unknown_id); 1624 } 1625 1626 if (stats->nr_invalid_chains != 0) { 1627 ui__warning("Found invalid callchains!\n\n" 1628 "%u out of %u events were discarded for this reason.\n\n" 1629 "Consider reporting to linux-kernel@vger.kernel.org.\n\n", 1630 stats->nr_invalid_chains, 1631 stats->nr_events[PERF_RECORD_SAMPLE]); 1632 } 1633 1634 if (stats->nr_unprocessable_samples != 0) { 1635 ui__warning("%u unprocessable samples recorded.\n" 1636 "Do you have a KVM guest running and not using 'perf kvm'?\n", 1637 stats->nr_unprocessable_samples); 1638 } 1639 1640 perf_session__warn_order(session); 1641 1642 events_stats__auxtrace_error_warn(stats); 1643 1644 if (stats->nr_proc_map_timeout != 0) { 1645 ui__warning("%d map information files for pre-existing threads were\n" 1646 "not processed, if there are samples for addresses they\n" 1647 "will not be resolved, you may find out which are these\n" 1648 "threads by running with -v and redirecting the output\n" 1649 "to a file.\n" 1650 "The time limit to process proc map is too short?\n" 1651 "Increase it by --proc-map-timeout\n", 1652 stats->nr_proc_map_timeout); 1653 } 1654 } 1655 1656 static int perf_session__flush_thread_stack(struct thread *thread, 1657 void *p __maybe_unused) 1658 { 1659 return thread_stack__flush(thread); 1660 } 1661 1662 static int perf_session__flush_thread_stacks(struct perf_session *session) 1663 { 1664 return machines__for_each_thread(&session->machines, 1665 perf_session__flush_thread_stack, 1666 NULL); 1667 } 1668 1669 volatile int session_done; 1670 1671 static int __perf_session__process_pipe_events(struct perf_session *session) 1672 { 1673 struct ordered_events *oe = &session->ordered_events; 1674 struct perf_tool *tool = session->tool; 1675 int fd = perf_data__fd(session->data); 1676 union perf_event *event; 1677 uint32_t size, cur_size = 0; 1678 void *buf = NULL; 1679 s64 skip = 0; 1680 u64 head; 1681 ssize_t err; 1682 void *p; 1683 1684 perf_tool__fill_defaults(tool); 1685 1686 head = 0; 1687 cur_size = sizeof(union perf_event); 1688 1689 buf = malloc(cur_size); 1690 if (!buf) 1691 return -errno; 1692 ordered_events__set_copy_on_queue(oe, true); 1693 more: 1694 event = buf; 1695 err = readn(fd, event, sizeof(struct perf_event_header)); 1696 if (err <= 0) { 1697 if (err == 0) 1698 goto done; 1699 1700 pr_err("failed to read event header\n"); 1701 goto out_err; 1702 } 1703 1704 if (session->header.needs_swap) 1705 perf_event_header__bswap(&event->header); 1706 1707 size = event->header.size; 1708 if (size < sizeof(struct perf_event_header)) { 1709 pr_err("bad event header size\n"); 1710 goto out_err; 1711 } 1712 1713 if (size > cur_size) { 1714 void *new = realloc(buf, size); 1715 if (!new) { 1716 pr_err("failed to allocate memory to read event\n"); 1717 goto out_err; 1718 } 1719 buf = new; 1720 cur_size = size; 1721 event = buf; 1722 } 1723 p = event; 1724 p += sizeof(struct perf_event_header); 1725 1726 if (size - sizeof(struct perf_event_header)) { 1727 err = readn(fd, p, size - sizeof(struct perf_event_header)); 1728 if (err <= 0) { 1729 if (err == 0) { 1730 pr_err("unexpected end of event stream\n"); 1731 goto done; 1732 } 1733 1734 pr_err("failed to read event data\n"); 1735 goto out_err; 1736 } 1737 } 1738 1739 if ((skip = perf_session__process_event(session, event, head)) < 0) { 1740 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", 1741 head, event->header.size, event->header.type); 1742 err = -EINVAL; 1743 goto out_err; 1744 } 1745 1746 head += size; 1747 1748 if (skip > 0) 1749 head += skip; 1750 1751 if (!session_done()) 1752 goto more; 1753 done: 1754 /* do the final flush for ordered samples */ 1755 err = ordered_events__flush(oe, OE_FLUSH__FINAL); 1756 if (err) 1757 goto out_err; 1758 err = auxtrace__flush_events(session, tool); 1759 if (err) 1760 goto out_err; 1761 err = perf_session__flush_thread_stacks(session); 1762 out_err: 1763 free(buf); 1764 if (!tool->no_warn) 1765 perf_session__warn_about_errors(session); 1766 ordered_events__free(&session->ordered_events); 1767 auxtrace__free_events(session); 1768 return err; 1769 } 1770 1771 static union perf_event * 1772 fetch_mmaped_event(struct perf_session *session, 1773 u64 head, size_t mmap_size, char *buf) 1774 { 1775 union perf_event *event; 1776 1777 /* 1778 * Ensure we have enough space remaining to read 1779 * the size of the event in the headers. 1780 */ 1781 if (head + sizeof(event->header) > mmap_size) 1782 return NULL; 1783 1784 event = (union perf_event *)(buf + head); 1785 1786 if (session->header.needs_swap) 1787 perf_event_header__bswap(&event->header); 1788 1789 if (head + event->header.size > mmap_size) { 1790 /* We're not fetching the event so swap back again */ 1791 if (session->header.needs_swap) 1792 perf_event_header__bswap(&event->header); 1793 return NULL; 1794 } 1795 1796 return event; 1797 } 1798 1799 /* 1800 * On 64bit we can mmap the data file in one go. No need for tiny mmap 1801 * slices. On 32bit we use 32MB. 1802 */ 1803 #if BITS_PER_LONG == 64 1804 #define MMAP_SIZE ULLONG_MAX 1805 #define NUM_MMAPS 1 1806 #else 1807 #define MMAP_SIZE (32 * 1024 * 1024ULL) 1808 #define NUM_MMAPS 128 1809 #endif 1810 1811 static int __perf_session__process_events(struct perf_session *session, 1812 u64 data_offset, u64 data_size, 1813 u64 file_size) 1814 { 1815 struct ordered_events *oe = &session->ordered_events; 1816 struct perf_tool *tool = session->tool; 1817 int fd = perf_data__fd(session->data); 1818 u64 head, page_offset, file_offset, file_pos, size; 1819 int err, mmap_prot, mmap_flags, map_idx = 0; 1820 size_t mmap_size; 1821 char *buf, *mmaps[NUM_MMAPS]; 1822 union perf_event *event; 1823 struct ui_progress prog; 1824 s64 skip; 1825 1826 perf_tool__fill_defaults(tool); 1827 1828 page_offset = page_size * (data_offset / page_size); 1829 file_offset = page_offset; 1830 head = data_offset - page_offset; 1831 1832 if (data_size == 0) 1833 goto out; 1834 1835 if (data_offset + data_size < file_size) 1836 file_size = data_offset + data_size; 1837 1838 ui_progress__init_size(&prog, file_size, "Processing events..."); 1839 1840 mmap_size = MMAP_SIZE; 1841 if (mmap_size > file_size) { 1842 mmap_size = file_size; 1843 session->one_mmap = true; 1844 } 1845 1846 memset(mmaps, 0, sizeof(mmaps)); 1847 1848 mmap_prot = PROT_READ; 1849 mmap_flags = MAP_SHARED; 1850 1851 if (session->header.needs_swap) { 1852 mmap_prot |= PROT_WRITE; 1853 mmap_flags = MAP_PRIVATE; 1854 } 1855 remap: 1856 buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd, 1857 file_offset); 1858 if (buf == MAP_FAILED) { 1859 pr_err("failed to mmap file\n"); 1860 err = -errno; 1861 goto out_err; 1862 } 1863 mmaps[map_idx] = buf; 1864 map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1); 1865 file_pos = file_offset + head; 1866 if (session->one_mmap) { 1867 session->one_mmap_addr = buf; 1868 session->one_mmap_offset = file_offset; 1869 } 1870 1871 more: 1872 event = fetch_mmaped_event(session, head, mmap_size, buf); 1873 if (!event) { 1874 if (mmaps[map_idx]) { 1875 munmap(mmaps[map_idx], mmap_size); 1876 mmaps[map_idx] = NULL; 1877 } 1878 1879 page_offset = page_size * (head / page_size); 1880 file_offset += page_offset; 1881 head -= page_offset; 1882 goto remap; 1883 } 1884 1885 size = event->header.size; 1886 1887 if (size < sizeof(struct perf_event_header) || 1888 (skip = perf_session__process_event(session, event, file_pos)) < 0) { 1889 pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n", 1890 file_offset + head, event->header.size, 1891 event->header.type); 1892 err = -EINVAL; 1893 goto out_err; 1894 } 1895 1896 if (skip) 1897 size += skip; 1898 1899 head += size; 1900 file_pos += size; 1901 1902 ui_progress__update(&prog, size); 1903 1904 if (session_done()) 1905 goto out; 1906 1907 if (file_pos < file_size) 1908 goto more; 1909 1910 out: 1911 /* do the final flush for ordered samples */ 1912 err = ordered_events__flush(oe, OE_FLUSH__FINAL); 1913 if (err) 1914 goto out_err; 1915 err = auxtrace__flush_events(session, tool); 1916 if (err) 1917 goto out_err; 1918 err = perf_session__flush_thread_stacks(session); 1919 out_err: 1920 ui_progress__finish(); 1921 if (!tool->no_warn) 1922 perf_session__warn_about_errors(session); 1923 /* 1924 * We may switching perf.data output, make ordered_events 1925 * reusable. 1926 */ 1927 ordered_events__reinit(&session->ordered_events); 1928 auxtrace__free_events(session); 1929 session->one_mmap = false; 1930 return err; 1931 } 1932 1933 int perf_session__process_events(struct perf_session *session) 1934 { 1935 u64 size = perf_data__size(session->data); 1936 int err; 1937 1938 if (perf_session__register_idle_thread(session) < 0) 1939 return -ENOMEM; 1940 1941 if (!perf_data__is_pipe(session->data)) 1942 err = __perf_session__process_events(session, 1943 session->header.data_offset, 1944 session->header.data_size, size); 1945 else 1946 err = __perf_session__process_pipe_events(session); 1947 1948 return err; 1949 } 1950 1951 bool perf_session__has_traces(struct perf_session *session, const char *msg) 1952 { 1953 struct perf_evsel *evsel; 1954 1955 evlist__for_each_entry(session->evlist, evsel) { 1956 if (evsel->attr.type == PERF_TYPE_TRACEPOINT) 1957 return true; 1958 } 1959 1960 pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg); 1961 return false; 1962 } 1963 1964 int map__set_kallsyms_ref_reloc_sym(struct map *map, const char *symbol_name, u64 addr) 1965 { 1966 char *bracket; 1967 struct ref_reloc_sym *ref; 1968 struct kmap *kmap; 1969 1970 ref = zalloc(sizeof(struct ref_reloc_sym)); 1971 if (ref == NULL) 1972 return -ENOMEM; 1973 1974 ref->name = strdup(symbol_name); 1975 if (ref->name == NULL) { 1976 free(ref); 1977 return -ENOMEM; 1978 } 1979 1980 bracket = strchr(ref->name, ']'); 1981 if (bracket) 1982 *bracket = '\0'; 1983 1984 ref->addr = addr; 1985 1986 kmap = map__kmap(map); 1987 if (kmap) 1988 kmap->ref_reloc_sym = ref; 1989 1990 return 0; 1991 } 1992 1993 size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp) 1994 { 1995 return machines__fprintf_dsos(&session->machines, fp); 1996 } 1997 1998 size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp, 1999 bool (skip)(struct dso *dso, int parm), int parm) 2000 { 2001 return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm); 2002 } 2003 2004 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp) 2005 { 2006 size_t ret; 2007 const char *msg = ""; 2008 2009 if (perf_header__has_feat(&session->header, HEADER_AUXTRACE)) 2010 msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)"; 2011 2012 ret = fprintf(fp, "\nAggregated stats:%s\n", msg); 2013 2014 ret += events_stats__fprintf(&session->evlist->stats, fp); 2015 return ret; 2016 } 2017 2018 size_t perf_session__fprintf(struct perf_session *session, FILE *fp) 2019 { 2020 /* 2021 * FIXME: Here we have to actually print all the machines in this 2022 * session, not just the host... 2023 */ 2024 return machine__fprintf(&session->machines.host, fp); 2025 } 2026 2027 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session, 2028 unsigned int type) 2029 { 2030 struct perf_evsel *pos; 2031 2032 evlist__for_each_entry(session->evlist, pos) { 2033 if (pos->attr.type == type) 2034 return pos; 2035 } 2036 return NULL; 2037 } 2038 2039 int perf_session__cpu_bitmap(struct perf_session *session, 2040 const char *cpu_list, unsigned long *cpu_bitmap) 2041 { 2042 int i, err = -1; 2043 struct cpu_map *map; 2044 2045 for (i = 0; i < PERF_TYPE_MAX; ++i) { 2046 struct perf_evsel *evsel; 2047 2048 evsel = perf_session__find_first_evtype(session, i); 2049 if (!evsel) 2050 continue; 2051 2052 if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) { 2053 pr_err("File does not contain CPU events. " 2054 "Remove -C option to proceed.\n"); 2055 return -1; 2056 } 2057 } 2058 2059 map = cpu_map__new(cpu_list); 2060 if (map == NULL) { 2061 pr_err("Invalid cpu_list\n"); 2062 return -1; 2063 } 2064 2065 for (i = 0; i < map->nr; i++) { 2066 int cpu = map->map[i]; 2067 2068 if (cpu >= MAX_NR_CPUS) { 2069 pr_err("Requested CPU %d too large. " 2070 "Consider raising MAX_NR_CPUS\n", cpu); 2071 goto out_delete_map; 2072 } 2073 2074 set_bit(cpu, cpu_bitmap); 2075 } 2076 2077 err = 0; 2078 2079 out_delete_map: 2080 cpu_map__put(map); 2081 return err; 2082 } 2083 2084 void perf_session__fprintf_info(struct perf_session *session, FILE *fp, 2085 bool full) 2086 { 2087 if (session == NULL || fp == NULL) 2088 return; 2089 2090 fprintf(fp, "# ========\n"); 2091 perf_header__fprintf_info(session, fp, full); 2092 fprintf(fp, "# ========\n#\n"); 2093 } 2094 2095 2096 int __perf_session__set_tracepoints_handlers(struct perf_session *session, 2097 const struct perf_evsel_str_handler *assocs, 2098 size_t nr_assocs) 2099 { 2100 struct perf_evsel *evsel; 2101 size_t i; 2102 int err; 2103 2104 for (i = 0; i < nr_assocs; i++) { 2105 /* 2106 * Adding a handler for an event not in the session, 2107 * just ignore it. 2108 */ 2109 evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name); 2110 if (evsel == NULL) 2111 continue; 2112 2113 err = -EEXIST; 2114 if (evsel->handler != NULL) 2115 goto out; 2116 evsel->handler = assocs[i].handler; 2117 } 2118 2119 err = 0; 2120 out: 2121 return err; 2122 } 2123 2124 int perf_event__process_id_index(struct perf_session *session, 2125 union perf_event *event) 2126 { 2127 struct perf_evlist *evlist = session->evlist; 2128 struct id_index_event *ie = &event->id_index; 2129 size_t i, nr, max_nr; 2130 2131 max_nr = (ie->header.size - sizeof(struct id_index_event)) / 2132 sizeof(struct id_index_entry); 2133 nr = ie->nr; 2134 if (nr > max_nr) 2135 return -EINVAL; 2136 2137 if (dump_trace) 2138 fprintf(stdout, " nr: %zu\n", nr); 2139 2140 for (i = 0; i < nr; i++) { 2141 struct id_index_entry *e = &ie->entries[i]; 2142 struct perf_sample_id *sid; 2143 2144 if (dump_trace) { 2145 fprintf(stdout, " ... id: %"PRIu64, e->id); 2146 fprintf(stdout, " idx: %"PRIu64, e->idx); 2147 fprintf(stdout, " cpu: %"PRId64, e->cpu); 2148 fprintf(stdout, " tid: %"PRId64"\n", e->tid); 2149 } 2150 2151 sid = perf_evlist__id2sid(evlist, e->id); 2152 if (!sid) 2153 return -ENOENT; 2154 sid->idx = e->idx; 2155 sid->cpu = e->cpu; 2156 sid->tid = e->tid; 2157 } 2158 return 0; 2159 } 2160 2161 int perf_event__synthesize_id_index(struct perf_tool *tool, 2162 perf_event__handler_t process, 2163 struct perf_evlist *evlist, 2164 struct machine *machine) 2165 { 2166 union perf_event *ev; 2167 struct perf_evsel *evsel; 2168 size_t nr = 0, i = 0, sz, max_nr, n; 2169 int err; 2170 2171 pr_debug2("Synthesizing id index\n"); 2172 2173 max_nr = (UINT16_MAX - sizeof(struct id_index_event)) / 2174 sizeof(struct id_index_entry); 2175 2176 evlist__for_each_entry(evlist, evsel) 2177 nr += evsel->ids; 2178 2179 n = nr > max_nr ? max_nr : nr; 2180 sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry); 2181 ev = zalloc(sz); 2182 if (!ev) 2183 return -ENOMEM; 2184 2185 ev->id_index.header.type = PERF_RECORD_ID_INDEX; 2186 ev->id_index.header.size = sz; 2187 ev->id_index.nr = n; 2188 2189 evlist__for_each_entry(evlist, evsel) { 2190 u32 j; 2191 2192 for (j = 0; j < evsel->ids; j++) { 2193 struct id_index_entry *e; 2194 struct perf_sample_id *sid; 2195 2196 if (i >= n) { 2197 err = process(tool, ev, NULL, machine); 2198 if (err) 2199 goto out_err; 2200 nr -= n; 2201 i = 0; 2202 } 2203 2204 e = &ev->id_index.entries[i++]; 2205 2206 e->id = evsel->id[j]; 2207 2208 sid = perf_evlist__id2sid(evlist, e->id); 2209 if (!sid) { 2210 free(ev); 2211 return -ENOENT; 2212 } 2213 2214 e->idx = sid->idx; 2215 e->cpu = sid->cpu; 2216 e->tid = sid->tid; 2217 } 2218 } 2219 2220 sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry); 2221 ev->id_index.header.size = sz; 2222 ev->id_index.nr = nr; 2223 2224 err = process(tool, ev, NULL, machine); 2225 out_err: 2226 free(ev); 2227 2228 return err; 2229 } 2230