1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * builtin-report.c 4 * 5 * Builtin report command: Analyze the perf.data input file, 6 * look up and read DSOs and symbol information and display 7 * a histogram of results, along various sorting keys. 8 */ 9 #include "builtin.h" 10 11 #include "util/config.h" 12 13 #include "util/annotate.h" 14 #include "util/color.h" 15 #include "util/dso.h" 16 #include <linux/list.h> 17 #include <linux/rbtree.h> 18 #include <linux/err.h> 19 #include <linux/zalloc.h> 20 #include "util/map.h" 21 #include "util/symbol.h" 22 #include "util/map_symbol.h" 23 #include "util/mem-events.h" 24 #include "util/branch.h" 25 #include "util/callchain.h" 26 #include "util/values.h" 27 28 #include "perf.h" 29 #include "util/debug.h" 30 #include "util/event.h" 31 #include "util/evlist.h" 32 #include "util/evsel.h" 33 #include "util/evswitch.h" 34 #include "util/header.h" 35 #include "util/mem-info.h" 36 #include "util/session.h" 37 #include "util/srcline.h" 38 #include "util/tool.h" 39 40 #include <subcmd/parse-options.h> 41 #include <subcmd/exec-cmd.h> 42 #include "util/parse-events.h" 43 44 #include "util/thread.h" 45 #include "util/sort.h" 46 #include "util/hist.h" 47 #include "util/data.h" 48 #include "arch/common.h" 49 #include "util/time-utils.h" 50 #include "util/auxtrace.h" 51 #include "util/units.h" 52 #include "util/unwind.h" 53 #include "util/util.h" // perf_tip() 54 #include "ui/ui.h" 55 #include "ui/progress.h" 56 #include "util/block-info.h" 57 58 #include <dlfcn.h> 59 #include <errno.h> 60 #include <inttypes.h> 61 #include <regex.h> 62 #include <linux/ctype.h> 63 #include <signal.h> 64 #include <linux/bitmap.h> 65 #include <linux/list_sort.h> 66 #include <linux/string.h> 67 #include <linux/stringify.h> 68 #include <linux/time64.h> 69 #include <sys/types.h> 70 #include <sys/stat.h> 71 #include <unistd.h> 72 #include <linux/mman.h> 73 74 #ifdef HAVE_LIBTRACEEVENT 75 #include <event-parse.h> 76 #endif 77 78 struct report { 79 struct perf_tool tool; 80 struct perf_session *session; 81 struct evswitch evswitch; 82 #ifdef HAVE_SLANG_SUPPORT 83 bool use_tui; 84 #endif 85 #ifdef HAVE_GTK2_SUPPORT 86 bool use_gtk; 87 #endif 88 bool use_stdio; 89 bool show_full_info; 90 bool show_threads; 91 bool inverted_callchain; 92 bool mem_mode; 93 bool stats_mode; 94 bool tasks_mode; 95 bool mmaps_mode; 96 bool header; 97 bool header_only; 98 bool nonany_branch_mode; 99 bool group_set; 100 bool stitch_lbr; 101 bool disable_order; 102 bool skip_empty; 103 bool data_type; 104 int max_stack; 105 struct perf_read_values show_threads_values; 106 const char *pretty_printing_style; 107 const char *cpu_list; 108 const char *symbol_filter_str; 109 const char *time_str; 110 struct perf_time_interval *ptime_range; 111 int range_size; 112 int range_num; 113 float min_percent; 114 u64 nr_entries; 115 u64 queue_size; 116 u64 total_cycles; 117 u64 total_samples; 118 u64 singlethreaded_samples; 119 int socket_filter; 120 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS); 121 struct branch_type_stat brtype_stat; 122 bool symbol_ipc; 123 bool total_cycles_mode; 124 struct block_report *block_reports; 125 int nr_block_reports; 126 }; 127 128 static int report__config(const char *var, const char *value, void *cb) 129 { 130 struct report *rep = cb; 131 132 if (!strcmp(var, "report.group")) { 133 symbol_conf.event_group = perf_config_bool(var, value); 134 return 0; 135 } 136 if (!strcmp(var, "report.percent-limit")) { 137 double pcnt = strtof(value, NULL); 138 139 rep->min_percent = pcnt; 140 callchain_param.min_percent = pcnt; 141 return 0; 142 } 143 if (!strcmp(var, "report.children")) { 144 symbol_conf.cumulate_callchain = perf_config_bool(var, value); 145 return 0; 146 } 147 if (!strcmp(var, "report.queue-size")) 148 return perf_config_u64(&rep->queue_size, var, value); 149 150 if (!strcmp(var, "report.sort_order")) { 151 default_sort_order = strdup(value); 152 if (!default_sort_order) { 153 pr_err("Not enough memory for report.sort_order\n"); 154 return -1; 155 } 156 return 0; 157 } 158 159 if (!strcmp(var, "report.skip-empty")) { 160 rep->skip_empty = perf_config_bool(var, value); 161 return 0; 162 } 163 164 pr_debug("%s variable unknown, ignoring...", var); 165 return 0; 166 } 167 168 static int hist_iter__report_callback(struct hist_entry_iter *iter, 169 struct addr_location *al, bool single, 170 void *arg) 171 { 172 int err = 0; 173 struct report *rep = arg; 174 struct hist_entry *he = iter->he; 175 struct perf_sample *sample = iter->sample; 176 struct mem_info *mi; 177 struct branch_info *bi; 178 179 if (!ui__has_annotation() && !rep->symbol_ipc) 180 return 0; 181 182 if (sort__mode == SORT_MODE__BRANCH) { 183 bi = he->branch_info; 184 err = addr_map_symbol__inc_samples(&bi->from, sample); 185 if (err) 186 goto out; 187 188 err = addr_map_symbol__inc_samples(&bi->to, sample); 189 190 } else if (rep->mem_mode) { 191 mi = he->mem_info; 192 err = addr_map_symbol__inc_samples(mem_info__daddr(mi), sample); 193 if (err) 194 goto out; 195 196 err = hist_entry__inc_addr_samples(he, sample, al->addr); 197 198 } else if (symbol_conf.cumulate_callchain) { 199 if (single) 200 err = hist_entry__inc_addr_samples(he, sample, al->addr); 201 } else { 202 err = hist_entry__inc_addr_samples(he, sample, al->addr); 203 } 204 205 out: 206 return err; 207 } 208 209 static int hist_iter__branch_callback(struct hist_entry_iter *iter, 210 struct addr_location *al __maybe_unused, 211 bool single __maybe_unused, 212 void *arg) 213 { 214 struct hist_entry *he = iter->he; 215 struct report *rep = arg; 216 struct branch_info *bi = he->branch_info; 217 struct perf_sample *sample = iter->sample; 218 int err; 219 220 branch_type_count(&rep->brtype_stat, &bi->flags, 221 bi->from.addr, bi->to.addr); 222 223 if (!ui__has_annotation() && !rep->symbol_ipc) 224 return 0; 225 226 err = addr_map_symbol__inc_samples(&bi->from, sample); 227 if (err) 228 goto out; 229 230 err = addr_map_symbol__inc_samples(&bi->to, sample); 231 232 out: 233 return err; 234 } 235 236 static void setup_forced_leader(struct report *report, 237 struct evlist *evlist) 238 { 239 if (report->group_set) 240 evlist__force_leader(evlist); 241 } 242 243 static int process_feature_event(const struct perf_tool *tool, 244 struct perf_session *session, 245 union perf_event *event) 246 { 247 struct report *rep = container_of(tool, struct report, tool); 248 int ret = perf_event__process_feature(tool, session, event); 249 250 if (ret == 0 && event->header.size == sizeof(struct perf_record_header_feature) && 251 (int)event->feat.feat_id >= session->header.last_feat) { 252 /* 253 * (feat_id = HEADER_LAST_FEATURE) is the end marker which means 254 * all features are received. 255 */ 256 if (rep->header_only) 257 session_done = 1; 258 259 setup_forced_leader(rep, session->evlist); 260 } 261 return ret; 262 } 263 264 static int process_sample_event(const struct perf_tool *tool, 265 union perf_event *event, 266 struct perf_sample *sample, 267 struct machine *machine) 268 { 269 struct report *rep = container_of(tool, struct report, tool); 270 struct addr_location al; 271 struct hist_entry_iter iter = { 272 .sample = sample, 273 .hide_unresolved = symbol_conf.hide_unresolved, 274 .add_entry_cb = hist_iter__report_callback, 275 }; 276 int ret = 0; 277 278 if (perf_time__ranges_skip_sample(rep->ptime_range, rep->range_num, 279 sample->time)) { 280 return 0; 281 } 282 283 if (evswitch__discard(&rep->evswitch, sample->evsel)) 284 return 0; 285 286 addr_location__init(&al); 287 if (machine__resolve(machine, &al, sample) < 0) { 288 pr_debug("problem processing %s (%u) event at offset %#" PRIx64 ", skipping it.\n", 289 perf_event__name(event->header.type), event->header.type, 290 sample->file_offset); 291 ret = -1; 292 goto out_put; 293 } 294 295 if (rep->stitch_lbr) 296 thread__set_lbr_stitch_enable(al.thread, true); 297 298 if (symbol_conf.hide_unresolved && al.sym == NULL) 299 goto out_put; 300 301 if (rep->cpu_list && (sample->cpu >= MAX_NR_CPUS || 302 !test_bit(sample->cpu, rep->cpu_bitmap))) 303 goto out_put; 304 305 if (sort__mode == SORT_MODE__BRANCH) { 306 /* 307 * A non-synthesized event might not have a branch stack if 308 * branch stacks have been synthesized (using itrace options). 309 */ 310 if (!sample->branch_stack) 311 goto out_put; 312 313 iter.add_entry_cb = hist_iter__branch_callback; 314 iter.ops = &hist_iter_branch; 315 } else if (rep->mem_mode) { 316 iter.ops = &hist_iter_mem; 317 } else if (symbol_conf.cumulate_callchain) { 318 iter.ops = &hist_iter_cumulative; 319 } else { 320 iter.ops = &hist_iter_normal; 321 } 322 323 if (al.map != NULL) 324 dso__set_hit(map__dso(al.map)); 325 326 if (ui__has_annotation() || rep->symbol_ipc || rep->total_cycles_mode) { 327 hist__account_cycles(sample->branch_stack, &al, sample, 328 rep->nonany_branch_mode, 329 &rep->total_cycles); 330 } 331 332 rep->total_samples++; 333 if (al.parallelism == 1) 334 rep->singlethreaded_samples++; 335 336 ret = hist_entry_iter__add(&iter, &al, rep->max_stack, rep); 337 if (ret < 0) 338 pr_debug("problem adding hist entry at offset %#" PRIx64 ", skipping event\n", 339 sample->file_offset); 340 out_put: 341 addr_location__exit(&al); 342 return ret; 343 } 344 345 static int process_read_event(const struct perf_tool *tool, 346 union perf_event *event, 347 struct perf_sample *sample __maybe_unused, 348 struct machine *machine __maybe_unused) 349 { 350 struct report *rep = container_of(tool, struct report, tool); 351 352 if (rep->show_threads) { 353 int err = perf_read_values_add_value(&rep->show_threads_values, 354 event->read.pid, event->read.tid, 355 sample->evsel, 356 event->read.value); 357 358 if (err) 359 return err; 360 } 361 362 return 0; 363 } 364 365 /* For pipe mode, sample_type is not currently set */ 366 static int report__setup_sample_type(struct report *rep) 367 { 368 struct perf_session *session = rep->session; 369 u64 sample_type = evlist__combined_sample_type(session->evlist); 370 bool is_pipe = perf_data__is_pipe(session->data); 371 struct evsel *evsel; 372 373 if (session->itrace_synth_opts->callchain || 374 session->itrace_synth_opts->add_callchain || 375 (!is_pipe && 376 perf_header__has_feat(&session->header, HEADER_AUXTRACE) && 377 !session->itrace_synth_opts->set)) 378 sample_type |= PERF_SAMPLE_CALLCHAIN; 379 380 if (session->itrace_synth_opts->last_branch || 381 session->itrace_synth_opts->add_last_branch) 382 sample_type |= PERF_SAMPLE_BRANCH_STACK; 383 384 if (!is_pipe && !(sample_type & PERF_SAMPLE_CALLCHAIN)) { 385 if (perf_hpp_list.parent) { 386 ui__error("Selected --sort parent, but no " 387 "callchain data. Did you call " 388 "'perf record' without -g?\n"); 389 return -EINVAL; 390 } 391 if (symbol_conf.use_callchain && 392 !symbol_conf.show_branchflag_count) { 393 ui__error("Selected -g or --branch-history.\n" 394 "But no callchain or branch data.\n" 395 "Did you call 'perf record' without -g or -b?\n"); 396 return -1; 397 } 398 } else if (!callchain_param.enabled && 399 callchain_param.mode != CHAIN_NONE && 400 !symbol_conf.use_callchain) { 401 symbol_conf.use_callchain = true; 402 if (callchain_register_param(&callchain_param) < 0) { 403 ui__error("Can't register callchain params.\n"); 404 return -EINVAL; 405 } 406 } 407 408 if (symbol_conf.cumulate_callchain) { 409 /* Silently ignore if callchain is missing */ 410 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) { 411 symbol_conf.cumulate_callchain = false; 412 perf_hpp__cancel_cumulate(session->evlist); 413 } 414 } 415 416 if (sort__mode == SORT_MODE__BRANCH) { 417 if (!is_pipe && 418 !(sample_type & PERF_SAMPLE_BRANCH_STACK)) { 419 ui__error("Selected -b but no branch data. " 420 "Did you call perf record without -b?\n"); 421 return -1; 422 } 423 } 424 425 if (sort__mode == SORT_MODE__MEMORY) { 426 /* 427 * FIXUP: prior to kernel 5.18, Arm SPE missed to set 428 * PERF_SAMPLE_DATA_SRC bit in sample type. For backward 429 * compatibility, set the bit if it's an old perf data file. 430 */ 431 evlist__for_each_entry(session->evlist, evsel) { 432 if (strstr(evsel__name(evsel), "arm_spe") && 433 !(sample_type & PERF_SAMPLE_DATA_SRC)) { 434 evsel->core.attr.sample_type |= PERF_SAMPLE_DATA_SRC; 435 sample_type |= PERF_SAMPLE_DATA_SRC; 436 } 437 } 438 439 if (!is_pipe && !(sample_type & PERF_SAMPLE_DATA_SRC)) { 440 ui__error("Selected --mem-mode but no mem data. " 441 "Did you call perf record without -d?\n"); 442 return -1; 443 } 444 } 445 446 callchain_param_setup(sample_type, perf_session__e_machine(session, /*e_flags=*/NULL)); 447 448 if (rep->stitch_lbr && (callchain_param.record_mode != CALLCHAIN_LBR)) { 449 ui__warning("Can't find LBR callchain. Switch off --stitch-lbr.\n" 450 "Please apply --call-graph lbr when recording.\n"); 451 rep->stitch_lbr = false; 452 } 453 454 /* ??? handle more cases than just ANY? */ 455 if (!(evlist__combined_branch_type(session->evlist) & PERF_SAMPLE_BRANCH_ANY)) 456 rep->nonany_branch_mode = true; 457 458 #if !defined(HAVE_LIBUNWIND_SUPPORT) && !defined(HAVE_LIBDW_SUPPORT) 459 if (dwarf_callchain_users) { 460 ui__warning("Please install libunwind or libdw " 461 "development packages during the perf build.\n"); 462 } 463 #endif 464 465 return 0; 466 } 467 468 static void sig_handler(int sig __maybe_unused) 469 { 470 session_done = 1; 471 } 472 473 static size_t hists__fprintf_nr_sample_events(struct hists *hists, struct report *rep, 474 const char *evname, FILE *fp) 475 { 476 size_t ret; 477 char unit; 478 unsigned long nr_samples = hists->stats.nr_samples; 479 u64 nr_events = hists->stats.total_period; 480 struct evsel *evsel = hists_to_evsel(hists); 481 char buf[512]; 482 size_t size = sizeof(buf); 483 int socked_id = hists->socket_filter; 484 485 if (quiet) 486 return 0; 487 488 if (symbol_conf.filter_relative) { 489 nr_samples = hists->stats.nr_non_filtered_samples; 490 nr_events = hists->stats.total_non_filtered_period; 491 } 492 493 if (evsel__is_group_event(evsel)) { 494 struct evsel *pos; 495 496 evsel__group_desc(evsel, buf, size); 497 evname = buf; 498 499 for_each_group_member(pos, evsel) { 500 const struct hists *pos_hists = evsel__hists(pos); 501 502 if (symbol_conf.filter_relative) { 503 nr_samples += pos_hists->stats.nr_non_filtered_samples; 504 nr_events += pos_hists->stats.total_non_filtered_period; 505 } else { 506 nr_samples += pos_hists->stats.nr_samples; 507 nr_events += pos_hists->stats.total_period; 508 } 509 } 510 } 511 512 nr_samples = convert_unit(nr_samples, &unit); 513 ret = fprintf(fp, "# Samples: %lu%c", nr_samples, unit); 514 if (evname != NULL) { 515 ret += fprintf(fp, " of event%s '%s'", 516 evsel->core.nr_members > 1 ? "s" : "", evname); 517 } 518 519 if (rep->time_str) 520 ret += fprintf(fp, " (time slices: %s)", rep->time_str); 521 522 if (symbol_conf.show_ref_callgraph && evname && strstr(evname, "call-graph=no")) { 523 ret += fprintf(fp, ", show reference callgraph"); 524 } 525 526 if (rep->mem_mode) { 527 ret += fprintf(fp, "\n# Total weight : %" PRIu64, nr_events); 528 if (sort_order || !field_order) { 529 ret += fprintf(fp, "\n# Sort order : %s", 530 sort_order ? : default_mem_sort_order); 531 } 532 } else 533 ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events); 534 535 if (socked_id > -1) 536 ret += fprintf(fp, "\n# Processor Socket: %d", socked_id); 537 538 return ret + fprintf(fp, "\n#\n"); 539 } 540 541 static int evlist__tui_block_hists_browse(struct evlist *evlist, struct report *rep) 542 { 543 struct evsel *pos; 544 int i = 0, ret; 545 546 evlist__for_each_entry(evlist, pos) { 547 ret = report__browse_block_hists(&rep->block_reports[i++].hist, 548 rep->min_percent, pos, 549 perf_session__env(rep->session)); 550 if (ret != 0) 551 return ret; 552 } 553 554 return 0; 555 } 556 557 static int evlist__tty_browse_hists(struct evlist *evlist, struct report *rep, const char *help) 558 { 559 struct evsel *pos; 560 int i = 0; 561 562 if (!quiet) { 563 fprintf(stdout, "#\n# Total Lost Samples: %" PRIu64 "\n#\n", 564 evlist->stats.total_lost_samples); 565 } 566 567 evlist__for_each_entry(evlist, pos) { 568 struct hists *hists = evsel__hists(pos); 569 const char *evname = evsel__name(pos); 570 571 i++; 572 if (symbol_conf.event_group && !evsel__is_group_leader(pos)) 573 continue; 574 575 if (rep->skip_empty && !hists->stats.nr_samples) 576 continue; 577 578 hists__fprintf_nr_sample_events(hists, rep, evname, stdout); 579 580 if (rep->total_cycles_mode) { 581 char *buf; 582 583 if (!annotation_br_cntr_abbr_list(&buf, pos, true)) { 584 fprintf(stdout, "%s", buf); 585 fprintf(stdout, "#\n"); 586 free(buf); 587 } 588 report__browse_block_hists(&rep->block_reports[i - 1].hist, 589 rep->min_percent, pos, NULL); 590 continue; 591 } 592 593 hists__fprintf(hists, !quiet, 0, 0, rep->min_percent, stdout, 594 !(symbol_conf.use_callchain || 595 symbol_conf.show_branchflag_count)); 596 fprintf(stdout, "\n\n"); 597 } 598 599 if (!quiet) 600 fprintf(stdout, "#\n# (%s)\n#\n", help); 601 602 if (rep->show_threads) { 603 bool style = !strcmp(rep->pretty_printing_style, "raw"); 604 perf_read_values_display(stdout, &rep->show_threads_values, 605 style); 606 perf_read_values_destroy(&rep->show_threads_values); 607 } 608 609 if (sort__mode == SORT_MODE__BRANCH) 610 branch_type_stat_display(stdout, &rep->brtype_stat); 611 612 return 0; 613 } 614 615 static void report__warn_kptr_restrict(const struct report *rep) 616 { 617 struct map *kernel_map = machine__kernel_map(&rep->session->machines.host); 618 struct kmap *kernel_kmap = kernel_map ? map__kmap(kernel_map) : NULL; 619 620 if (evlist__exclude_kernel(rep->session->evlist)) 621 return; 622 623 if (kernel_map == NULL || 624 (dso__hit(map__dso(kernel_map)) && 625 (kernel_kmap->ref_reloc_sym == NULL || 626 kernel_kmap->ref_reloc_sym->addr == 0))) { 627 const char *desc = 628 "As no suitable kallsyms nor vmlinux was found, kernel samples\n" 629 "can't be resolved."; 630 631 if (kernel_map && map__has_symbols(kernel_map)) { 632 desc = "If some relocation was applied (e.g. " 633 "kexec) symbols may be misresolved."; 634 } 635 636 ui__warning( 637 "Kernel address maps (/proc/{kallsyms,modules}) were restricted.\n\n" 638 "Check /proc/sys/kernel/kptr_restrict before running 'perf record'.\n\n%s\n\n" 639 "Samples in kernel modules can't be resolved as well.\n\n", 640 desc); 641 } 642 } 643 644 static int report__gtk_browse_hists(struct report *rep, const char *help) 645 { 646 int (*hist_browser)(struct evlist *evlist, const char *help, 647 struct hist_browser_timer *timer, float min_pcnt); 648 649 hist_browser = dlsym(perf_gtk_handle, "evlist__gtk_browse_hists"); 650 651 if (hist_browser == NULL) { 652 ui__error("GTK browser not found!\n"); 653 return -1; 654 } 655 656 return hist_browser(rep->session->evlist, help, NULL, rep->min_percent); 657 } 658 659 static int report__browse_hists(struct report *rep) 660 { 661 int ret; 662 struct perf_session *session = rep->session; 663 struct evlist *evlist = session->evlist; 664 char *help = NULL, *path = NULL; 665 666 path = system_path(TIPDIR); 667 if (perf_tip(&help, path) || help == NULL) { 668 /* fallback for people who don't install perf ;-) */ 669 free(path); 670 path = system_path(DOCDIR); 671 if (perf_tip(&help, path) || help == NULL) 672 help = strdup("Cannot load tips.txt file, please install perf!"); 673 } 674 free(path); 675 676 switch (use_browser) { 677 case 1: 678 if (rep->total_cycles_mode) { 679 ret = evlist__tui_block_hists_browse(evlist, rep); 680 break; 681 } 682 683 ret = evlist__tui_browse_hists(evlist, help, NULL, rep->min_percent, 684 perf_session__env(session), true); 685 /* 686 * Usually "ret" is the last pressed key, and we only 687 * care if the key notifies us to switch data file. 688 */ 689 if (ret != K_SWITCH_INPUT_DATA && ret != K_RELOAD) 690 ret = 0; 691 break; 692 case 2: 693 ret = report__gtk_browse_hists(rep, help); 694 break; 695 default: 696 ret = evlist__tty_browse_hists(evlist, rep, help); 697 break; 698 } 699 free(help); 700 return ret; 701 } 702 703 static int report__collapse_hists(struct report *rep) 704 { 705 struct perf_session *session = rep->session; 706 struct evlist *evlist = session->evlist; 707 struct ui_progress prog; 708 struct evsel *pos; 709 int ret = 0; 710 711 /* 712 * The pipe data needs to setup hierarchy hpp formats now, because it 713 * cannot know about evsels in the data before reading the data. The 714 * normal file data saves the event (attribute) info in the header 715 * section, but pipe does not have the luxury. 716 */ 717 if (perf_data__is_pipe(session->data)) { 718 if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0) { 719 ui__error("Failed to setup hierarchy output formats\n"); 720 return -1; 721 } 722 } 723 724 ui_progress__init(&prog, rep->nr_entries, "Merging related events..."); 725 726 evlist__for_each_entry(rep->session->evlist, pos) { 727 struct hists *hists = evsel__hists(pos); 728 729 if (pos->core.idx == 0) 730 hists->symbol_filter_str = rep->symbol_filter_str; 731 732 hists->socket_filter = rep->socket_filter; 733 734 ret = hists__collapse_resort(hists, &prog); 735 if (ret < 0) 736 break; 737 738 /* Non-group events are considered as leader */ 739 if (symbol_conf.event_group && !evsel__is_group_leader(pos)) { 740 struct hists *leader_hists = evsel__hists(evsel__leader(pos)); 741 742 hists__match(leader_hists, hists); 743 hists__link(leader_hists, hists); 744 } 745 } 746 747 ui_progress__finish(); 748 return ret; 749 } 750 751 static int hists__resort_cb(struct hist_entry *he, void *arg) 752 { 753 struct report *rep = arg; 754 struct symbol *sym = he->ms.sym; 755 756 if (rep->symbol_ipc && sym && !symbol__is_annotate2(sym)) { 757 struct evsel *evsel = hists_to_evsel(he->hists); 758 759 symbol__annotate2(&he->ms, evsel, NULL); 760 } 761 762 return 0; 763 } 764 765 static void report__output_resort(struct report *rep) 766 { 767 struct ui_progress prog; 768 struct evsel *pos; 769 770 ui_progress__init(&prog, rep->nr_entries, "Sorting events for output..."); 771 772 evlist__for_each_entry(rep->session->evlist, pos) { 773 evsel__output_resort_cb(pos, &prog, hists__resort_cb, rep); 774 } 775 776 ui_progress__finish(); 777 } 778 779 static int count_sample_event(const struct perf_tool *tool __maybe_unused, 780 union perf_event *event __maybe_unused, 781 struct perf_sample *sample, 782 struct machine *machine __maybe_unused) 783 { 784 struct hists *hists = evsel__hists(sample->evsel); 785 786 hists__inc_nr_events(hists); 787 return 0; 788 } 789 790 static int count_lost_samples_event(const struct perf_tool *tool, 791 union perf_event *event, 792 struct perf_sample *sample, 793 struct machine *machine __maybe_unused) 794 { 795 struct report *rep = container_of(tool, struct report, tool); 796 struct evsel *evsel = sample->evsel; 797 798 if (!evsel) 799 evsel = evlist__id2evsel(rep->session->evlist, sample->id); 800 801 if (evsel) { 802 struct hists *hists = evsel__hists(evsel); 803 u32 count = event->lost_samples.lost; 804 805 if (event->header.misc & PERF_RECORD_MISC_LOST_SAMPLES_BPF) 806 hists__inc_nr_dropped_samples(hists, count); 807 else 808 hists__inc_nr_lost_samples(hists, count); 809 } 810 return 0; 811 } 812 813 static int process_attr(const struct perf_tool *tool __maybe_unused, 814 union perf_event *event, 815 struct evlist **pevlist); 816 817 static void stats_setup(struct report *rep) 818 { 819 perf_tool__init(&rep->tool, /*ordered_events=*/false); 820 rep->tool.attr = process_attr; 821 rep->tool.sample = count_sample_event; 822 rep->tool.lost_samples = count_lost_samples_event; 823 rep->tool.event_update = perf_event__process_event_update; 824 rep->tool.no_warn = true; 825 } 826 827 static int stats_print(struct report *rep) 828 { 829 struct perf_session *session = rep->session; 830 831 perf_session__fprintf_nr_events(session, stdout); 832 evlist__fprintf_nr_events(session->evlist, stdout); 833 return 0; 834 } 835 836 static void tasks_setup(struct report *rep) 837 { 838 perf_tool__init(&rep->tool, /*ordered_events=*/true); 839 if (rep->mmaps_mode) { 840 rep->tool.mmap = perf_event__process_mmap; 841 rep->tool.mmap2 = perf_event__process_mmap2; 842 } 843 rep->tool.attr = process_attr; 844 rep->tool.comm = perf_event__process_comm; 845 rep->tool.exit = perf_event__process_exit; 846 rep->tool.fork = perf_event__process_fork; 847 rep->tool.no_warn = true; 848 } 849 850 struct maps__fprintf_task_args { 851 int indent; 852 FILE *fp; 853 size_t printed; 854 }; 855 856 static int maps__fprintf_task_cb(struct map *map, void *data) 857 { 858 struct maps__fprintf_task_args *args = data; 859 const struct dso *dso = map__dso(map); 860 u32 prot = map__prot(map); 861 const struct dso_id *dso_id = dso__id_const(dso); 862 int ret; 863 char buf[SBUILD_ID_SIZE]; 864 865 if (dso_id->mmap2_valid) 866 snprintf(buf, sizeof(buf), "%" PRIu64, dso_id->ino); 867 else 868 build_id__snprintf(&dso_id->build_id, buf, sizeof(buf)); 869 870 ret = fprintf(args->fp, 871 "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %s %s\n", 872 args->indent, "", map__start(map), map__end(map), 873 prot & PROT_READ ? 'r' : '-', 874 prot & PROT_WRITE ? 'w' : '-', 875 prot & PROT_EXEC ? 'x' : '-', 876 map__flags(map) ? 's' : 'p', 877 map__pgoff(map), 878 buf, dso__name(dso)); 879 880 if (ret < 0) 881 return ret; 882 883 args->printed += ret; 884 return 0; 885 } 886 887 static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp) 888 { 889 struct maps__fprintf_task_args args = { 890 .indent = indent, 891 .fp = fp, 892 .printed = 0, 893 }; 894 895 maps__for_each_map(maps, maps__fprintf_task_cb, &args); 896 897 return args.printed; 898 } 899 900 static int thread_level(struct machine *machine, const struct thread *thread) 901 { 902 struct thread *parent_thread; 903 int res; 904 905 if (thread__tid(thread) <= 0) 906 return 0; 907 908 if (thread__ppid(thread) <= 0) 909 return 1; 910 911 parent_thread = machine__find_thread(machine, -1, thread__ppid(thread)); 912 if (!parent_thread) { 913 pr_err("Missing parent thread of %d\n", thread__tid(thread)); 914 return 0; 915 } 916 res = 1 + thread_level(machine, parent_thread); 917 thread__put(parent_thread); 918 return res; 919 } 920 921 static void task__print_level(struct machine *machine, struct thread *thread, FILE *fp) 922 { 923 int level = thread_level(machine, thread); 924 int comm_indent = fprintf(fp, " %8d %8d %8d |%*s", 925 thread__pid(thread), thread__tid(thread), 926 thread__ppid(thread), level, ""); 927 928 fprintf(fp, "%s\n", thread__comm_str(thread)); 929 930 maps__fprintf_task(thread__maps(thread), comm_indent, fp); 931 } 932 933 /* 934 * Sort two thread list nodes such that they form a tree. The first node is the 935 * root of the tree, its children are ordered numerically after it. If a child 936 * has children itself then they appear immediately after their parent. For 937 * example, the 4 threads in the order they'd appear in the list: 938 * - init with a TID 1 and a parent of 0 939 * - systemd with a TID 3000 and a parent of init/1 940 * - systemd child thread with TID 4000, the parent is 3000 941 * - NetworkManager is a child of init with a TID of 3500. 942 */ 943 static int task_list_cmp(void *priv, const struct list_head *la, const struct list_head *lb) 944 { 945 struct machine *machine = priv; 946 struct thread_list *task_a = list_entry(la, struct thread_list, list); 947 struct thread_list *task_b = list_entry(lb, struct thread_list, list); 948 struct thread *a = task_a->thread; 949 struct thread *b = task_b->thread; 950 int level_a, level_b, res; 951 952 /* Same thread? */ 953 if (thread__tid(a) == thread__tid(b)) 954 return 0; 955 956 /* Compare a and b to root. */ 957 if (thread__tid(a) == 0) 958 return -1; 959 960 if (thread__tid(b) == 0) 961 return 1; 962 963 /* If parents match sort by tid. */ 964 if (thread__ppid(a) == thread__ppid(b)) 965 return thread__tid(a) < thread__tid(b) ? -1 : 1; 966 967 /* 968 * Find a and b such that if they are a child of each other a and b's 969 * tid's match, otherwise a and b have a common parent and distinct 970 * tid's to sort by. First make the depths of the threads match. 971 */ 972 level_a = thread_level(machine, a); 973 level_b = thread_level(machine, b); 974 a = thread__get(a); 975 b = thread__get(b); 976 for (int i = level_a; i > level_b; i--) { 977 struct thread *parent = machine__find_thread(machine, -1, thread__ppid(a)); 978 979 thread__put(a); 980 if (!parent) { 981 pr_err("Missing parent thread of %d\n", thread__tid(a)); 982 thread__put(b); 983 return -1; 984 } 985 a = parent; 986 } 987 for (int i = level_b; i > level_a; i--) { 988 struct thread *parent = machine__find_thread(machine, -1, thread__ppid(b)); 989 990 thread__put(b); 991 if (!parent) { 992 pr_err("Missing parent thread of %d\n", thread__tid(b)); 993 thread__put(a); 994 return 1; 995 } 996 b = parent; 997 } 998 /* Search up to a common parent. */ 999 while (thread__ppid(a) != thread__ppid(b)) { 1000 struct thread *parent; 1001 1002 parent = machine__find_thread(machine, -1, thread__ppid(a)); 1003 thread__put(a); 1004 if (!parent) 1005 pr_err("Missing parent thread of %d\n", thread__tid(a)); 1006 a = parent; 1007 parent = machine__find_thread(machine, -1, thread__ppid(b)); 1008 thread__put(b); 1009 if (!parent) 1010 pr_err("Missing parent thread of %d\n", thread__tid(b)); 1011 b = parent; 1012 if (!a || !b) { 1013 /* Handle missing parent (unexpected) with some sanity. */ 1014 thread__put(a); 1015 thread__put(b); 1016 return !a && !b ? 0 : (!a ? -1 : 1); 1017 } 1018 } 1019 if (thread__tid(a) == thread__tid(b)) { 1020 /* a is a child of b or vice-versa, deeper levels appear later. */ 1021 res = level_a < level_b ? -1 : (level_a > level_b ? 1 : 0); 1022 } else { 1023 /* Sort by tid now the parent is the same. */ 1024 res = thread__tid(a) < thread__tid(b) ? -1 : 1; 1025 } 1026 thread__put(a); 1027 thread__put(b); 1028 return res; 1029 } 1030 1031 static int tasks_print(struct report *rep, FILE *fp) 1032 { 1033 struct machine *machine = &rep->session->machines.host; 1034 LIST_HEAD(tasks); 1035 int ret; 1036 1037 ret = machine__thread_list(machine, &tasks); 1038 if (!ret) { 1039 struct thread_list *task; 1040 1041 list_sort(machine, &tasks, task_list_cmp); 1042 1043 fprintf(fp, "# %8s %8s %8s %s\n", "pid", "tid", "ppid", "comm"); 1044 1045 list_for_each_entry(task, &tasks, list) 1046 task__print_level(machine, task->thread, fp); 1047 } 1048 thread_list__delete(&tasks); 1049 return ret; 1050 } 1051 1052 static int __cmd_report(struct report *rep) 1053 { 1054 int ret; 1055 struct perf_session *session = rep->session; 1056 struct evsel *pos; 1057 struct perf_data *data = session->data; 1058 1059 signal(SIGINT, sig_handler); 1060 1061 if (rep->cpu_list) { 1062 ret = perf_session__cpu_bitmap(session, rep->cpu_list, 1063 rep->cpu_bitmap); 1064 if (ret) { 1065 ui__error("failed to set cpu bitmap\n"); 1066 return ret; 1067 } 1068 session->itrace_synth_opts->cpu_bitmap = rep->cpu_bitmap; 1069 } 1070 1071 if (rep->show_threads) { 1072 ret = perf_read_values_init(&rep->show_threads_values); 1073 if (ret) 1074 return ret; 1075 } 1076 1077 ret = report__setup_sample_type(rep); 1078 if (ret) { 1079 /* report__setup_sample_type() already showed error message */ 1080 return ret; 1081 } 1082 1083 if (rep->stats_mode) 1084 stats_setup(rep); 1085 1086 if (rep->tasks_mode) 1087 tasks_setup(rep); 1088 1089 ret = perf_session__process_events(session); 1090 if (ret) { 1091 ui__error("failed to process sample\n"); 1092 return ret; 1093 } 1094 1095 /* Don't show Latency column for non-parallel profiles by default. */ 1096 if (!symbol_conf.prefer_latency && rep->total_samples && 1097 rep->singlethreaded_samples * 100 / rep->total_samples >= 99) 1098 perf_hpp__cancel_latency(session->evlist); 1099 1100 evlist__check_mem_load_aux(session->evlist); 1101 1102 if (rep->stats_mode) 1103 return stats_print(rep); 1104 1105 if (rep->tasks_mode) 1106 return tasks_print(rep, stdout); 1107 1108 report__warn_kptr_restrict(rep); 1109 1110 evlist__for_each_entry(session->evlist, pos) 1111 rep->nr_entries += evsel__hists(pos)->nr_entries; 1112 1113 if (use_browser == 0) { 1114 if (verbose > 3) 1115 perf_session__fprintf(session, stdout); 1116 1117 if (verbose > 2) 1118 perf_session__fprintf_dsos(session, stdout); 1119 1120 if (dump_trace) { 1121 stats_print(rep); 1122 return 0; 1123 } 1124 } 1125 1126 ret = report__collapse_hists(rep); 1127 if (ret) { 1128 ui__error("failed to process hist entry\n"); 1129 return ret; 1130 } 1131 1132 if (session_done()) 1133 return 0; 1134 1135 /* 1136 * recalculate number of entries after collapsing since it 1137 * might be changed during the collapse phase. 1138 */ 1139 rep->nr_entries = 0; 1140 evlist__for_each_entry(session->evlist, pos) 1141 rep->nr_entries += evsel__hists(pos)->nr_entries; 1142 1143 if (rep->nr_entries == 0) { 1144 ui__error("The %s data has no samples!\n", data->path); 1145 return 0; 1146 } 1147 1148 report__output_resort(rep); 1149 1150 if (rep->total_cycles_mode) { 1151 int nr_hpps = 4; 1152 int block_hpps[PERF_HPP_REPORT__BLOCK_MAX_INDEX] = { 1153 PERF_HPP_REPORT__BLOCK_TOTAL_CYCLES_PCT, 1154 PERF_HPP_REPORT__BLOCK_LBR_CYCLES, 1155 PERF_HPP_REPORT__BLOCK_CYCLES_PCT, 1156 PERF_HPP_REPORT__BLOCK_AVG_CYCLES, 1157 }; 1158 1159 if (session->evlist->nr_br_cntr > 0) 1160 block_hpps[nr_hpps++] = PERF_HPP_REPORT__BLOCK_BRANCH_COUNTER; 1161 1162 block_hpps[nr_hpps++] = PERF_HPP_REPORT__BLOCK_RANGE; 1163 block_hpps[nr_hpps++] = PERF_HPP_REPORT__BLOCK_DSO; 1164 1165 rep->block_reports = block_info__create_report(session->evlist, 1166 rep->total_cycles, 1167 block_hpps, nr_hpps, 1168 &rep->nr_block_reports); 1169 if (!rep->block_reports) 1170 return -1; 1171 } 1172 1173 return report__browse_hists(rep); 1174 } 1175 1176 static int 1177 report_parse_callchain_opt(const struct option *opt, const char *arg, int unset) 1178 { 1179 struct callchain_param *callchain = opt->value; 1180 1181 callchain->enabled = !unset; 1182 /* 1183 * --no-call-graph 1184 */ 1185 if (unset) { 1186 symbol_conf.use_callchain = false; 1187 callchain->mode = CHAIN_NONE; 1188 return 0; 1189 } 1190 1191 return parse_callchain_report_opt(arg); 1192 } 1193 1194 static int 1195 parse_time_quantum(const struct option *opt, const char *arg, 1196 int unset __maybe_unused) 1197 { 1198 unsigned long *time_q = opt->value; 1199 char *end; 1200 1201 *time_q = strtoul(arg, &end, 0); 1202 if (end == arg) 1203 goto parse_err; 1204 if (*time_q == 0) { 1205 pr_err("time quantum cannot be 0"); 1206 return -1; 1207 } 1208 end = skip_spaces(end); 1209 if (*end == 0) 1210 return 0; 1211 if (!strcmp(end, "s")) { 1212 *time_q *= NSEC_PER_SEC; 1213 return 0; 1214 } 1215 if (!strcmp(end, "ms")) { 1216 *time_q *= NSEC_PER_MSEC; 1217 return 0; 1218 } 1219 if (!strcmp(end, "us")) { 1220 *time_q *= NSEC_PER_USEC; 1221 return 0; 1222 } 1223 if (!strcmp(end, "ns")) 1224 return 0; 1225 parse_err: 1226 pr_err("Cannot parse time quantum `%s'\n", arg); 1227 return -1; 1228 } 1229 1230 int 1231 report_parse_ignore_callees_opt(const struct option *opt __maybe_unused, 1232 const char *arg, int unset __maybe_unused) 1233 { 1234 if (arg) { 1235 int err = regcomp(&ignore_callees_regex, arg, REG_EXTENDED); 1236 if (err) { 1237 char buf[BUFSIZ]; 1238 regerror(err, &ignore_callees_regex, buf, sizeof(buf)); 1239 pr_err("Invalid --ignore-callees regex: %s\n%s", arg, buf); 1240 return -1; 1241 } 1242 have_ignore_callees = 1; 1243 } 1244 1245 return 0; 1246 } 1247 1248 static int 1249 parse_branch_mode(const struct option *opt, 1250 const char *str __maybe_unused, int unset) 1251 { 1252 int *branch_mode = opt->value; 1253 1254 *branch_mode = !unset; 1255 return 0; 1256 } 1257 1258 static int 1259 parse_percent_limit(const struct option *opt, const char *str, 1260 int unset __maybe_unused) 1261 { 1262 struct report *rep = opt->value; 1263 double pcnt = strtof(str, NULL); 1264 1265 rep->min_percent = pcnt; 1266 callchain_param.min_percent = pcnt; 1267 return 0; 1268 } 1269 1270 static int 1271 report_parse_addr2line_config(const struct option *opt __maybe_unused, 1272 const char *arg, int unset __maybe_unused) 1273 { 1274 return addr2line_configure("addr2line.style", arg, NULL); 1275 } 1276 1277 static int process_attr(const struct perf_tool *tool __maybe_unused, 1278 union perf_event *event, 1279 struct evlist **pevlist) 1280 { 1281 struct perf_session *session; 1282 u64 sample_type; 1283 int err; 1284 1285 err = perf_event__process_attr(tool, event, pevlist); 1286 if (err) 1287 return err; 1288 1289 /* 1290 * Check if we need to enable callchains based 1291 * on events sample_type. 1292 */ 1293 sample_type = evlist__combined_sample_type(*pevlist); 1294 session = (*pevlist)->session; 1295 callchain_param_setup(sample_type, perf_session__e_machine(session, /*e_flags=*/NULL)); 1296 return 0; 1297 } 1298 1299 #define CALLCHAIN_BRANCH_SORT_ORDER \ 1300 "srcline,symbol,dso,callchain_branch_predicted," \ 1301 "callchain_branch_abort,callchain_branch_cycles" 1302 1303 int cmd_report(int argc, const char **argv) 1304 { 1305 struct perf_session *session; 1306 struct itrace_synth_opts itrace_synth_opts = { .set = 0, }; 1307 struct stat st; 1308 bool has_br_stack = false; 1309 int branch_mode = -1; 1310 int last_key = 0; 1311 bool branch_call_mode = false; 1312 #define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent" 1313 static const char report_callchain_help[] = "Display call graph (stack chain/backtrace):\n\n" 1314 CALLCHAIN_REPORT_HELP 1315 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT; 1316 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT; 1317 const char * const report_usage[] = { 1318 "perf report [<options>]", 1319 NULL 1320 }; 1321 struct report report = { 1322 .max_stack = PERF_MAX_STACK_DEPTH, 1323 .pretty_printing_style = "normal", 1324 .socket_filter = -1, 1325 .skip_empty = true, 1326 }; 1327 char *sort_order_help = sort_help("sort by key(s):", SORT_MODE__NORMAL); 1328 char *field_order_help = sort_help("output field(s):", SORT_MODE__NORMAL); 1329 const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL; 1330 const struct option options[] = { 1331 OPT_STRING('i', "input", &input_name, "file", 1332 "input file name"), 1333 OPT_INCR('v', "verbose", &verbose, 1334 "be more verbose (show symbol address, etc)"), 1335 OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any warnings or messages"), 1336 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 1337 "dump raw trace in ASCII"), 1338 OPT_BOOLEAN(0, "stats", &report.stats_mode, "Display event stats"), 1339 OPT_BOOLEAN(0, "tasks", &report.tasks_mode, "Display recorded tasks"), 1340 OPT_BOOLEAN(0, "mmaps", &report.mmaps_mode, "Display recorded tasks memory maps"), 1341 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, 1342 "file", "vmlinux pathname"), 1343 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux, 1344 "don't load vmlinux even if found"), 1345 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, 1346 "file", "kallsyms pathname"), 1347 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"), 1348 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, 1349 "load module symbols - WARNING: use only with -k and LIVE kernel"), 1350 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples, 1351 "Show a column with the number of samples"), 1352 OPT_BOOLEAN('T', "threads", &report.show_threads, 1353 "Show per-thread event counters"), 1354 OPT_STRING(0, "pretty", &report.pretty_printing_style, "key", 1355 "pretty printing style key: normal raw"), 1356 #ifdef HAVE_SLANG_SUPPORT 1357 OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"), 1358 #endif 1359 #ifdef HAVE_GTK2_SUPPORT 1360 OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK2 interface"), 1361 #endif 1362 OPT_BOOLEAN(0, "stdio", &report.use_stdio, 1363 "Use the stdio interface"), 1364 OPT_BOOLEAN(0, "header", &report.header, "Show data header."), 1365 OPT_BOOLEAN(0, "header-only", &report.header_only, 1366 "Show only data header."), 1367 OPT_STRING('s', "sort", &sort_order, "key[,key2...]", 1368 sort_order_help), 1369 OPT_STRING('F', "fields", &field_order, "key[,keys...]", 1370 field_order_help), 1371 OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization, 1372 "Show sample percentage for different cpu modes"), 1373 OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization, 1374 "Show sample percentage for different cpu modes", PARSE_OPT_HIDDEN), 1375 OPT_STRING('p', "parent", &parent_pattern, "regex", 1376 "regex filter to identify parent, see: '--sort parent'"), 1377 OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other, 1378 "Only display entries with parent-match"), 1379 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param, 1380 "print_type,threshold[,print_limit],order,sort_key[,branch],value", 1381 report_callchain_help, &report_parse_callchain_opt, 1382 callchain_default_opt), 1383 OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain, 1384 "Accumulate callchains of children and show total overhead as well. " 1385 "Enabled by default, use --no-children to disable."), 1386 OPT_INTEGER(0, "max-stack", &report.max_stack, 1387 "Set the maximum stack depth when parsing the callchain, " 1388 "anything beyond the specified depth will be ignored. " 1389 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)), 1390 OPT_BOOLEAN('G', "inverted", &report.inverted_callchain, 1391 "alias for inverted call graph"), 1392 OPT_CALLBACK(0, "ignore-callees", NULL, "regex", 1393 "ignore callees of these functions in call graphs", 1394 report_parse_ignore_callees_opt), 1395 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", 1396 "only consider symbols in these dsos"), 1397 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", 1398 "only consider symbols in these comms"), 1399 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]", 1400 "only consider symbols in these pids"), 1401 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]", 1402 "only consider symbols in these tids"), 1403 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", 1404 "only consider these symbols"), 1405 OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter", 1406 "only show symbols that (partially) match with this filter"), 1407 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str, 1408 "width[,width...]", 1409 "don't try to adjust column width, use these fixed values"), 1410 OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator", 1411 "separator for columns, no spaces will be added between " 1412 "columns '.' is reserved."), 1413 OPT_BOOLEAN('U', "hide-unresolved", &symbol_conf.hide_unresolved, 1414 "Only display entries resolved to a symbol"), 1415 OPT_CALLBACK(0, "symfs", NULL, "directory[,layout]", SYMFS_HELP, 1416 symbol__config_symfs), 1417 OPT_STRING('C', "cpu", &report.cpu_list, "cpu", 1418 "list of cpus to profile"), 1419 OPT_STRING(0, "parallelism", &symbol_conf.parallelism_list_str, "parallelism", 1420 "only consider these parallelism levels (cpu set format)"), 1421 OPT_BOOLEAN('I', "show-info", &report.show_full_info, 1422 "Display extended information about perf.data file"), 1423 OPT_BOOLEAN(0, "source", &annotate_opts.annotate_src, 1424 "Interleave source code with assembly code (default)"), 1425 OPT_BOOLEAN(0, "asm-raw", &annotate_opts.show_asm_raw, 1426 "Display raw encoding of assembly instructions (default)"), 1427 OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style", 1428 "Specify disassembler style (e.g. -M intel for intel syntax)"), 1429 OPT_STRING(0, "prefix", &annotate_opts.prefix, "prefix", 1430 "Add prefix to source file path names in programs (with --prefix-strip)"), 1431 OPT_STRING(0, "prefix-strip", &annotate_opts.prefix_strip, "N", 1432 "Strip first N entries of source file path name in programs (with --prefix)"), 1433 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period, 1434 "Show a column with the sum of periods"), 1435 OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group, &report.group_set, 1436 "Show event group information together"), 1437 OPT_INTEGER(0, "group-sort-idx", &symbol_conf.group_sort_idx, 1438 "Sort the output by the event at the index n in group. " 1439 "If n is invalid, sort by the first event. " 1440 "WARNING: should be used on grouped events."), 1441 OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "", 1442 "use branch records for per branch histogram filling", 1443 parse_branch_mode), 1444 OPT_BOOLEAN(0, "branch-history", &branch_call_mode, 1445 "add last branch records to call history"), 1446 OPT_STRING(0, "objdump", &objdump_path, "path", 1447 "objdump binary to use for disassembly and annotations"), 1448 OPT_STRING(0, "addr2line", &addr2line_path, "path", 1449 "addr2line binary to use for line numbers"), 1450 OPT_CALLBACK(0, "addr2line-style", NULL, "addr2line style", 1451 "addr2line styles (libdw,llvm,libbfd,addr2line)", 1452 report_parse_addr2line_config), 1453 OPT_CALLBACK(0, "unwind-style", NULL, "unwind style", 1454 "unwind styles (libdw,libunwind)", 1455 unwind__option), 1456 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle, 1457 "Symbol demangling. Enabled by default, use --no-demangle to disable."), 1458 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel, 1459 "Enable kernel symbol demangling"), 1460 OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"), 1461 OPT_INTEGER(0, "samples", &symbol_conf.res_sample, 1462 "Number of samples to save per histogram entry for individual browsing"), 1463 OPT_CALLBACK(0, "percent-limit", &report, "percent", 1464 "Don't show entries under that percent", parse_percent_limit), 1465 OPT_CALLBACK(0, "percentage", NULL, "relative|absolute", 1466 "how to display percentage of filtered entries", parse_filter_percentage), 1467 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts", 1468 "Instruction Tracing options\n" ITRACE_HELP, 1469 itrace_parse_synth_opts), 1470 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename, 1471 "Show full source file name path for source lines"), 1472 OPT_BOOLEAN(0, "show-ref-call-graph", &symbol_conf.show_ref_callgraph, 1473 "Show callgraph from reference event"), 1474 OPT_BOOLEAN(0, "stitch-lbr", &report.stitch_lbr, 1475 "Enable LBR callgraph stitching approach"), 1476 OPT_INTEGER(0, "socket-filter", &report.socket_filter, 1477 "only show processor socket that match with this filter"), 1478 OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace, 1479 "Show raw trace event output (do not use print fmt or plugins)"), 1480 OPT_BOOLEAN('H', "hierarchy", &symbol_conf.report_hierarchy, 1481 "Show entries in a hierarchy"), 1482 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode", 1483 "'always' (default), 'never' or 'auto' only applicable to --stdio mode", 1484 stdio__config_color, "always"), 1485 OPT_STRING(0, "time", &report.time_str, "str", 1486 "Time span of interest (start,stop)"), 1487 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name, 1488 "Show inline function"), 1489 OPT_CALLBACK(0, "percent-type", &annotate_opts, "local-period", 1490 "Set percent type local/global-period/hits", 1491 annotate_parse_percent_type), 1492 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs, "Show times in nanosecs"), 1493 OPT_CALLBACK(0, "time-quantum", &symbol_conf.time_quantum, "time (ms|us|ns|s)", 1494 "Set time quantum for time sort key (default 100ms)", 1495 parse_time_quantum), 1496 OPTS_EVSWITCH(&report.evswitch), 1497 OPT_BOOLEAN(0, "total-cycles", &report.total_cycles_mode, 1498 "Sort all blocks by 'Sampled Cycles%'"), 1499 OPT_BOOLEAN(0, "disable-order", &report.disable_order, 1500 "Disable raw trace ordering"), 1501 OPT_BOOLEAN(0, "skip-empty", &report.skip_empty, 1502 "Do not display empty (or dummy) events in the output"), 1503 OPT_BOOLEAN(0, "latency", &symbol_conf.prefer_latency, 1504 "Show latency-centric profile rather than the default\n" 1505 "\t\t\t CPU-consumption-centric profile\n" 1506 "\t\t\t (requires perf record --latency flag)."), 1507 OPT_END() 1508 }; 1509 struct perf_data data = { 1510 .mode = PERF_DATA_MODE_READ, 1511 }; 1512 int ret = hists__init(); 1513 char sort_tmp[128]; 1514 bool ordered_events = true; 1515 1516 if (ret < 0) 1517 goto exit; 1518 1519 /* 1520 * tasks_mode require access to exited threads to list those that are in 1521 * the data file. Off-cpu events are synthesized after other events and 1522 * reference exited threads. 1523 */ 1524 symbol_conf.keep_exited_threads = true; 1525 1526 annotation_options__init(); 1527 1528 ret = perf_config(report__config, &report); 1529 if (ret) 1530 goto exit; 1531 1532 argc = parse_options(argc, argv, options, report_usage, 0); 1533 if (argc) { 1534 /* 1535 * Special case: if there's an argument left then assume that 1536 * it's a symbol filter: 1537 */ 1538 if (argc > 1) 1539 usage_with_options(report_usage, options); 1540 1541 report.symbol_filter_str = argv[0]; 1542 } 1543 1544 if (disassembler_style) { 1545 annotate_opts.disassembler_style = strdup(disassembler_style); 1546 if (!annotate_opts.disassembler_style) 1547 return -ENOMEM; 1548 } 1549 if (objdump_path) { 1550 annotate_opts.objdump_path = strdup(objdump_path); 1551 if (!annotate_opts.objdump_path) 1552 return -ENOMEM; 1553 } 1554 if (addr2line_path) { 1555 symbol_conf.addr2line_path = strdup(addr2line_path); 1556 if (!symbol_conf.addr2line_path) 1557 return -ENOMEM; 1558 } 1559 1560 if (annotate_check_args() < 0) { 1561 ret = -EINVAL; 1562 goto exit; 1563 } 1564 1565 if (report.mmaps_mode) 1566 report.tasks_mode = true; 1567 1568 if (dump_trace && report.disable_order) 1569 ordered_events = false; 1570 1571 if (quiet) 1572 perf_quiet_option(); 1573 1574 ret = symbol__validate_sym_arguments(); 1575 if (ret) 1576 goto exit; 1577 1578 if (report.inverted_callchain) 1579 callchain_param.order = ORDER_CALLER; 1580 if (symbol_conf.cumulate_callchain && !callchain_param.order_set) 1581 callchain_param.order = ORDER_CALLER; 1582 1583 if ((itrace_synth_opts.callchain || itrace_synth_opts.add_callchain) && 1584 (int)itrace_synth_opts.callchain_sz > report.max_stack) 1585 report.max_stack = itrace_synth_opts.callchain_sz; 1586 1587 if (!input_name || !strlen(input_name)) { 1588 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode)) 1589 input_name = "-"; 1590 else 1591 input_name = "perf.data"; 1592 } 1593 1594 repeat: 1595 data.path = input_name; 1596 data.force = symbol_conf.force; 1597 1598 symbol_conf.skip_empty = report.skip_empty; 1599 1600 perf_tool__init(&report.tool, ordered_events); 1601 report.tool.sample = process_sample_event; 1602 report.tool.mmap = perf_event__process_mmap; 1603 report.tool.mmap2 = perf_event__process_mmap2; 1604 report.tool.comm = perf_event__process_comm; 1605 report.tool.namespaces = perf_event__process_namespaces; 1606 report.tool.cgroup = perf_event__process_cgroup; 1607 report.tool.exit = perf_event__process_exit; 1608 report.tool.fork = perf_event__process_fork; 1609 report.tool.context_switch = perf_event__process_switch; 1610 report.tool.lost = perf_event__process_lost; 1611 report.tool.read = process_read_event; 1612 report.tool.attr = process_attr; 1613 #ifdef HAVE_LIBTRACEEVENT 1614 report.tool.tracing_data = perf_event__process_tracing_data; 1615 #endif 1616 report.tool.build_id = perf_event__process_build_id; 1617 report.tool.id_index = perf_event__process_id_index; 1618 report.tool.auxtrace_info = perf_event__process_auxtrace_info; 1619 report.tool.auxtrace = perf_event__process_auxtrace; 1620 report.tool.event_update = perf_event__process_event_update; 1621 report.tool.feature = process_feature_event; 1622 report.tool.ordering_requires_timestamps = true; 1623 report.tool.merge_deferred_callchains = !dump_trace; 1624 1625 session = perf_session__new(&data, &report.tool); 1626 if (IS_ERR(session)) { 1627 ret = PTR_ERR(session); 1628 goto exit; 1629 } 1630 1631 ret = evswitch__init(&report.evswitch, session->evlist, stderr); 1632 if (ret) 1633 goto exit; 1634 1635 if (zstd_init(&(session->zstd_data), 0) < 0) 1636 pr_warning("Decompression initialization failed. Reported data may be incomplete.\n"); 1637 1638 if (report.queue_size) { 1639 ordered_events__set_alloc_size(&session->ordered_events, 1640 report.queue_size); 1641 } 1642 1643 session->itrace_synth_opts = &itrace_synth_opts; 1644 1645 report.session = session; 1646 1647 has_br_stack = perf_header__has_feat(&session->header, 1648 HEADER_BRANCH_STACK); 1649 if (evlist__combined_sample_type(session->evlist) & PERF_SAMPLE_STACK_USER) 1650 has_br_stack = false; 1651 1652 setup_forced_leader(&report, session->evlist); 1653 1654 if (symbol_conf.group_sort_idx && evlist__nr_groups(session->evlist) == 0) { 1655 parse_options_usage(NULL, options, "group-sort-idx", 0); 1656 ret = -EINVAL; 1657 goto error; 1658 } 1659 1660 if (itrace_synth_opts.last_branch || itrace_synth_opts.add_last_branch) 1661 has_br_stack = true; 1662 1663 if (has_br_stack && branch_call_mode) 1664 symbol_conf.show_branchflag_count = true; 1665 1666 memset(&report.brtype_stat, 0, sizeof(struct branch_type_stat)); 1667 1668 /* 1669 * Branch mode is a tristate: 1670 * -1 means default, so decide based on the file having branch data. 1671 * 0/1 means the user chose a mode. 1672 */ 1673 if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) && 1674 !branch_call_mode) { 1675 sort__mode = SORT_MODE__BRANCH; 1676 symbol_conf.cumulate_callchain = false; 1677 } 1678 if (branch_call_mode) { 1679 callchain_param.key = CCKEY_ADDRESS; 1680 callchain_param.branch_callstack = true; 1681 symbol_conf.use_callchain = true; 1682 callchain_register_param(&callchain_param); 1683 if (sort_order == NULL) 1684 sort_order = CALLCHAIN_BRANCH_SORT_ORDER; 1685 } 1686 1687 if (report.mem_mode) { 1688 if (sort__mode == SORT_MODE__BRANCH) { 1689 pr_err("branch and mem mode incompatible\n"); 1690 goto error; 1691 } 1692 sort__mode = SORT_MODE__MEMORY; 1693 symbol_conf.cumulate_callchain = false; 1694 } 1695 1696 if (symbol_conf.report_hierarchy) { 1697 /* 1698 * The hist entries in hierarchy are added during the collpase 1699 * phase. Let's enable it even if no sort keys require it. 1700 */ 1701 perf_hpp_list.need_collapse = true; 1702 } 1703 1704 if (report.use_stdio) 1705 use_browser = 0; 1706 #ifdef HAVE_SLANG_SUPPORT 1707 else if (report.use_tui) 1708 use_browser = 1; 1709 #endif 1710 #ifdef HAVE_GTK2_SUPPORT 1711 else if (report.use_gtk) 1712 use_browser = 2; 1713 #endif 1714 1715 /* Force tty output for header output and per-thread stat. */ 1716 if (report.header || report.header_only || report.show_threads) 1717 use_browser = 0; 1718 if (report.header || report.header_only) 1719 report.tool.show_feat_hdr = SHOW_FEAT_HEADER; 1720 if (report.show_full_info) 1721 report.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO; 1722 if (report.stats_mode || report.tasks_mode) 1723 use_browser = 0; 1724 if (report.stats_mode && report.tasks_mode) { 1725 pr_err("Error: --tasks and --mmaps can't be used together with --stats\n"); 1726 goto error; 1727 } 1728 1729 if (report.total_cycles_mode) { 1730 if (sort__mode != SORT_MODE__BRANCH) 1731 report.total_cycles_mode = false; 1732 else 1733 sort_order = NULL; 1734 } 1735 1736 if ((sort_order && strstr(sort_order, "type")) || 1737 (field_order && strstr(field_order, "type"))) { 1738 report.data_type = true; 1739 annotate_opts.annotate_src = false; 1740 1741 /* disable incompatible options */ 1742 symbol_conf.cumulate_callchain = false; 1743 1744 #ifndef HAVE_LIBDW_SUPPORT 1745 pr_err("Error: Data type profiling is disabled due to missing DWARF support\n"); 1746 goto error; 1747 #endif 1748 } 1749 1750 if (strcmp(input_name, "-") != 0) 1751 setup_browser(true); 1752 else 1753 use_browser = 0; 1754 1755 if (report.data_type && use_browser == 1) { 1756 symbol_conf.annotate_data_member = true; 1757 symbol_conf.annotate_data_sample = true; 1758 } 1759 1760 symbol_conf.enable_latency = true; 1761 if (report.disable_order || !perf_session__has_switch_events(session)) { 1762 if (symbol_conf.parallelism_list_str || 1763 symbol_conf.prefer_latency || 1764 (sort_order && (strstr(sort_order, "latency") || 1765 strstr(sort_order, "parallelism"))) || 1766 (field_order && (strstr(field_order, "latency") || 1767 strstr(field_order, "parallelism")))) { 1768 if (report.disable_order) 1769 ui__error("Use of latency profile or parallelism is incompatible with --disable-order.\n"); 1770 else 1771 ui__error("Use of latency profile or parallelism requires --latency flag during record.\n"); 1772 return -1; 1773 } 1774 /* 1775 * If user did not ask for anything related to 1776 * latency/parallelism explicitly, just don't show it. 1777 */ 1778 symbol_conf.enable_latency = false; 1779 } 1780 1781 if (last_key != K_SWITCH_INPUT_DATA) { 1782 if (sort_order && strstr(sort_order, "ipc")) { 1783 parse_options_usage(report_usage, options, "s", 1); 1784 goto error; 1785 } 1786 1787 if (sort_order && strstr(sort_order, "symbol")) { 1788 if (sort__mode == SORT_MODE__BRANCH) { 1789 snprintf(sort_tmp, sizeof(sort_tmp), "%s,%s", 1790 sort_order, "ipc_lbr"); 1791 report.symbol_ipc = true; 1792 } else { 1793 snprintf(sort_tmp, sizeof(sort_tmp), "%s,%s", 1794 sort_order, "ipc_null"); 1795 } 1796 1797 sort_order = sort_tmp; 1798 } 1799 } 1800 1801 if ((last_key != K_SWITCH_INPUT_DATA && last_key != K_RELOAD) && 1802 (setup_sorting(session->evlist, perf_session__env(session)) < 0)) { 1803 if (sort_order) 1804 parse_options_usage(report_usage, options, "s", 1); 1805 if (field_order) 1806 parse_options_usage(sort_order ? NULL : report_usage, 1807 options, "F", 1); 1808 goto error; 1809 } 1810 1811 if ((report.header || report.header_only) && !quiet) { 1812 perf_session__fprintf_info(session, stdout, 1813 report.show_full_info); 1814 if (report.header_only) { 1815 if (data.is_pipe) { 1816 /* 1817 * we need to process first few records 1818 * which contains PERF_RECORD_HEADER_FEATURE. 1819 */ 1820 perf_session__process_events(session); 1821 } 1822 ret = 0; 1823 goto error; 1824 } 1825 } else if (use_browser == 0 && !quiet && 1826 !report.stats_mode && !report.tasks_mode) { 1827 fputs("# To display the perf.data header info, please use --header/--header-only options.\n#\n", 1828 stdout); 1829 } 1830 1831 /* 1832 * Only in the TUI browser we are doing integrated annotation, 1833 * so don't allocate extra space that won't be used in the stdio 1834 * implementation. 1835 */ 1836 if (ui__has_annotation() || report.symbol_ipc || report.data_type || 1837 report.total_cycles_mode) { 1838 ret = symbol__annotation_init(); 1839 if (ret < 0) 1840 goto error; 1841 /* 1842 * For searching by name on the "Browse map details". 1843 * providing it only in verbose mode not to bloat too 1844 * much struct symbol. 1845 */ 1846 if (verbose > 0) { 1847 /* 1848 * XXX: Need to provide a less kludgy way to ask for 1849 * more space per symbol, the u32 is for the index on 1850 * the ui browser. 1851 * See symbol__browser_index. 1852 */ 1853 symbol_conf.priv_size += sizeof(u32); 1854 } 1855 annotation_config__init(); 1856 } 1857 1858 if (symbol__init(perf_session__env(session)) < 0) 1859 goto error; 1860 1861 if (report.time_str) { 1862 ret = perf_time__parse_for_ranges(report.time_str, session, 1863 &report.ptime_range, 1864 &report.range_size, 1865 &report.range_num); 1866 if (ret < 0) 1867 goto error; 1868 1869 itrace_synth_opts__set_time_range(&itrace_synth_opts, 1870 report.ptime_range, 1871 report.range_num); 1872 } 1873 1874 #ifdef HAVE_LIBTRACEEVENT 1875 if (session->tevent.pevent && 1876 tep_set_function_resolver(session->tevent.pevent, 1877 machine__resolve_kernel_addr, 1878 &session->machines.host) < 0) { 1879 pr_err("%s: failed to set libtraceevent function resolver\n", 1880 __func__); 1881 return -1; 1882 } 1883 #endif 1884 sort__setup_elide(stdout); 1885 1886 ret = __cmd_report(&report); 1887 if (ret == K_SWITCH_INPUT_DATA || ret == K_RELOAD) { 1888 perf_session__delete(session); 1889 last_key = K_SWITCH_INPUT_DATA; 1890 /* 1891 * To support switching between data with and without callchains. 1892 * report__setup_sample_type() will update it properly. 1893 */ 1894 symbol_conf.use_callchain = false; 1895 goto repeat; 1896 } else 1897 ret = 0; 1898 1899 if (!use_browser && (verbose > 2 || debug_kmaps)) 1900 perf_session__dump_kmaps(session); 1901 error: 1902 if (report.ptime_range) { 1903 itrace_synth_opts__clear_time_range(&itrace_synth_opts); 1904 zfree(&report.ptime_range); 1905 } 1906 1907 if (report.block_reports) { 1908 block_info__free_report(report.block_reports, 1909 report.nr_block_reports); 1910 report.block_reports = NULL; 1911 } 1912 1913 zstd_fini(&(session->zstd_data)); 1914 perf_session__delete(session); 1915 exit: 1916 annotation_options__exit(); 1917 free(sort_order_help); 1918 free(field_order_help); 1919 return ret; 1920 } 1921