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