perf util: Support no index time percent slice
[linux-block.git] / tools / perf / builtin-report.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
bf9e1876
IM
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 */
16f762a2 9#include "builtin.h"
53cb8bc2 10
bf9e1876 11#include "util/util.h"
41840d21 12#include "util/config.h"
bf9e1876 13
78f7defe 14#include "util/annotate.h"
8fc0321f 15#include "util/color.h"
5da50258 16#include <linux/list.h>
43cbcd8a 17#include <linux/rbtree.h>
930f8b34 18#include <linux/err.h>
a2928c42 19#include "util/symbol.h"
f55c5552 20#include "util/callchain.h"
8d513270 21#include "util/values.h"
8fa66bdc 22
53cb8bc2 23#include "perf.h"
8f28827a 24#include "util/debug.h"
e248de33
ACM
25#include "util/evlist.h"
26#include "util/evsel.h"
7c6a1c65 27#include "util/header.h"
94c744b6 28#include "util/session.h"
45694aa7 29#include "util/tool.h"
53cb8bc2 30
4b6ab94e 31#include <subcmd/parse-options.h>
34b7b0f9 32#include <subcmd/exec-cmd.h>
53cb8bc2
IM
33#include "util/parse-events.h"
34
6baa0a5a 35#include "util/thread.h"
dd68ada2 36#include "util/sort.h"
3d1d07ec 37#include "util/hist.h"
f5fc1412 38#include "util/data.h"
68e94f4e 39#include "arch/common.h"
46690a80 40#include "util/time-utils.h"
520a2ebc 41#include "util/auxtrace.h"
58db1d6e 42#include "util/units.h"
2d78b189 43#include "util/branch.h"
520a2ebc 44
fc67297b 45#include <dlfcn.h>
a43783ae 46#include <errno.h>
fd20e811 47#include <inttypes.h>
1eae20c1 48#include <regex.h>
9607ad3a 49#include <signal.h>
5d67be97 50#include <linux/bitmap.h>
531d2410 51#include <linux/stringify.h>
7a8ef4c4
ACM
52#include <sys/types.h>
53#include <sys/stat.h>
54#include <unistd.h>
6439d7d1 55#include <linux/mman.h>
5d67be97 56
5b969bc7
JY
57#define PTIME_RANGE_MAX 10
58
28b21393 59struct report {
45694aa7 60 struct perf_tool tool;
d20deb64 61 struct perf_session *session;
2059fc7a 62 bool use_tui, use_gtk, use_stdio;
fa372aae
ACM
63 bool show_full_info;
64 bool show_threads;
65 bool inverted_callchain;
f4f7e28d 66 bool mem_mode;
a4a4d0a7 67 bool stats_mode;
930f8b34 68 bool tasks_mode;
6439d7d1 69 bool mmaps_mode;
5cfe2c82
JO
70 bool header;
71 bool header_only;
98df858e 72 bool nonany_branch_mode;
91e95617 73 int max_stack;
fa372aae
ACM
74 struct perf_read_values show_threads_values;
75 const char *pretty_printing_style;
fa372aae 76 const char *cpu_list;
b14ffaca 77 const char *symbol_filter_str;
46690a80 78 const char *time_str;
5b969bc7
JY
79 struct perf_time_interval ptime_range[PTIME_RANGE_MAX];
80 int range_num;
064f1981 81 float min_percent;
58c311da 82 u64 nr_entries;
94786b67 83 u64 queue_size;
21394d94 84 int socket_filter;
fa372aae 85 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
2d78b189 86 struct branch_type_stat brtype_stat;
d20deb64 87};
5d67be97 88
28b21393 89static int report__config(const char *var, const char *value, void *cb)
00c7e1f1 90{
94786b67
JO
91 struct report *rep = cb;
92
00c7e1f1
NK
93 if (!strcmp(var, "report.group")) {
94 symbol_conf.event_group = perf_config_bool(var, value);
95 return 0;
96 }
eec574e6 97 if (!strcmp(var, "report.percent-limit")) {
2665b452
NK
98 double pcnt = strtof(value, NULL);
99
100 rep->min_percent = pcnt;
101 callchain_param.min_percent = pcnt;
eec574e6
NK
102 return 0;
103 }
8d8e645c
NK
104 if (!strcmp(var, "report.children")) {
105 symbol_conf.cumulate_callchain = perf_config_bool(var, value);
106 return 0;
107 }
25ce4bb8
ACM
108 if (!strcmp(var, "report.queue-size"))
109 return perf_config_u64(&rep->queue_size, var, value);
110
fa1f4565
ACM
111 if (!strcmp(var, "report.sort_order")) {
112 default_sort_order = strdup(value);
113 return 0;
114 }
00c7e1f1 115
b8cbb349 116 return 0;
00c7e1f1
NK
117}
118
9d3c02d7
NK
119static int hist_iter__report_callback(struct hist_entry_iter *iter,
120 struct addr_location *al, bool single,
121 void *arg)
122{
123 int err = 0;
124 struct report *rep = arg;
125 struct hist_entry *he = iter->he;
126 struct perf_evsel *evsel = iter->evsel;
bab89f6a 127 struct perf_sample *sample = iter->sample;
9d3c02d7
NK
128 struct mem_info *mi;
129 struct branch_info *bi;
130
9d3c02d7
NK
131 if (!ui__has_annotation())
132 return 0;
133
bab89f6a 134 hist__account_cycles(sample->branch_stack, al, sample,
57849998
AK
135 rep->nonany_branch_mode);
136
9d3c02d7
NK
137 if (sort__mode == SORT_MODE__BRANCH) {
138 bi = he->branch_info;
bab89f6a 139 err = addr_map_symbol__inc_samples(&bi->from, sample, evsel->idx);
9d3c02d7
NK
140 if (err)
141 goto out;
142
bab89f6a 143 err = addr_map_symbol__inc_samples(&bi->to, sample, evsel->idx);
9d3c02d7
NK
144
145 } else if (rep->mem_mode) {
146 mi = he->mem_info;
bab89f6a 147 err = addr_map_symbol__inc_samples(&mi->daddr, sample, evsel->idx);
9d3c02d7
NK
148 if (err)
149 goto out;
150
bab89f6a 151 err = hist_entry__inc_addr_samples(he, sample, evsel->idx, al->addr);
9d3c02d7
NK
152
153 } else if (symbol_conf.cumulate_callchain) {
154 if (single)
bab89f6a 155 err = hist_entry__inc_addr_samples(he, sample, evsel->idx,
9d3c02d7
NK
156 al->addr);
157 } else {
bab89f6a 158 err = hist_entry__inc_addr_samples(he, sample, evsel->idx, al->addr);
9d3c02d7
NK
159 }
160
161out:
162 return err;
f4f7e28d
SE
163}
164
2d78b189
JY
165static int hist_iter__branch_callback(struct hist_entry_iter *iter,
166 struct addr_location *al __maybe_unused,
167 bool single __maybe_unused,
168 void *arg)
169{
170 struct hist_entry *he = iter->he;
171 struct report *rep = arg;
172 struct branch_info *bi;
40c39e30
JY
173 struct perf_sample *sample = iter->sample;
174 struct perf_evsel *evsel = iter->evsel;
175 int err;
176
177 if (!ui__has_annotation())
178 return 0;
179
180 hist__account_cycles(sample->branch_stack, al, sample,
181 rep->nonany_branch_mode);
2d78b189
JY
182
183 bi = he->branch_info;
40c39e30
JY
184 err = addr_map_symbol__inc_samples(&bi->from, sample, evsel->idx);
185 if (err)
186 goto out;
187
188 err = addr_map_symbol__inc_samples(&bi->to, sample, evsel->idx);
189
2d78b189
JY
190 branch_type_count(&rep->brtype_stat, &bi->flags,
191 bi->from.addr, bi->to.addr);
192
40c39e30
JY
193out:
194 return err;
2d78b189
JY
195}
196
45694aa7 197static int process_sample_event(struct perf_tool *tool,
d20deb64 198 union perf_event *event,
8115d60c 199 struct perf_sample *sample,
9e69c210 200 struct perf_evsel *evsel,
743eb868 201 struct machine *machine)
75051724 202{
28b21393 203 struct report *rep = container_of(tool, struct report, tool);
1ed091c4 204 struct addr_location al;
69bcb019 205 struct hist_entry_iter iter = {
063bd936
NK
206 .evsel = evsel,
207 .sample = sample,
b49a8fe5 208 .hide_unresolved = symbol_conf.hide_unresolved,
063bd936 209 .add_entry_cb = hist_iter__report_callback,
69bcb019 210 };
b91fc39f 211 int ret = 0;
180f95e2 212
5b969bc7
JY
213 if (perf_time__ranges_skip_sample(rep->ptime_range, rep->range_num,
214 sample->time)) {
46690a80 215 return 0;
5b969bc7 216 }
46690a80 217
bb3eb566 218 if (machine__resolve(machine, &al, sample) < 0) {
a4210141
NK
219 pr_debug("problem processing %d event, skipping it.\n",
220 event->header.type);
75051724
IM
221 return -1;
222 }
e7fb08b1 223
b49a8fe5 224 if (symbol_conf.hide_unresolved && al.sym == NULL)
b91fc39f 225 goto out_put;
7bec7a91 226
fa372aae 227 if (rep->cpu_list && !test_bit(sample->cpu, rep->cpu_bitmap))
b91fc39f 228 goto out_put;
5d67be97 229
f86225db
AH
230 if (sort__mode == SORT_MODE__BRANCH) {
231 /*
232 * A non-synthesized event might not have a branch stack if
233 * branch stacks have been synthesized (using itrace options).
234 */
235 if (!sample->branch_stack)
236 goto out_put;
2d78b189
JY
237
238 iter.add_entry_cb = hist_iter__branch_callback;
69bcb019 239 iter.ops = &hist_iter_branch;
f86225db 240 } else if (rep->mem_mode) {
69bcb019 241 iter.ops = &hist_iter_mem;
f86225db 242 } else if (symbol_conf.cumulate_callchain) {
7a13aa28 243 iter.ops = &hist_iter_cumulative;
f86225db 244 } else {
69bcb019 245 iter.ops = &hist_iter_normal;
f86225db 246 }
69bcb019
NK
247
248 if (al.map != NULL)
249 al.map->dso->hit = 1;
250
063bd936 251 ret = hist_entry_iter__add(&iter, &al, rep->max_stack, rep);
69bcb019
NK
252 if (ret < 0)
253 pr_debug("problem adding hist entry, skipping event\n");
b91fc39f
ACM
254out_put:
255 addr_location__put(&al);
27a0dcb7 256 return ret;
75051724 257}
3502973d 258
45694aa7 259static int process_read_event(struct perf_tool *tool,
d20deb64 260 union perf_event *event,
1d037ca1 261 struct perf_sample *sample __maybe_unused,
743eb868 262 struct perf_evsel *evsel,
1d037ca1 263 struct machine *machine __maybe_unused)
e9ea2fde 264{
28b21393 265 struct report *rep = container_of(tool, struct report, tool);
743eb868 266
fa372aae 267 if (rep->show_threads) {
7289f83c 268 const char *name = evsel ? perf_evsel__name(evsel) : "unknown";
89973506 269 int err = perf_read_values_add_value(&rep->show_threads_values,
8d513270 270 event->read.pid, event->read.tid,
9933183e 271 evsel->idx,
8d513270
BG
272 name,
273 event->read.value);
89973506
ACM
274
275 if (err)
276 return err;
8d513270
BG
277 }
278
e9ea2fde
PZ
279 return 0;
280}
281
300aa941 282/* For pipe mode, sample_type is not currently set */
28b21393 283static int report__setup_sample_type(struct report *rep)
d80d338d 284{
c824c433
ACM
285 struct perf_session *session = rep->session;
286 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
8ceb41d7 287 bool is_pipe = perf_data__is_pipe(session->data);
d20deb64 288
d062ac16
AH
289 if (session->itrace_synth_opts->callchain ||
290 (!is_pipe &&
291 perf_header__has_feat(&session->header, HEADER_AUXTRACE) &&
292 !session->itrace_synth_opts->set))
293 sample_type |= PERF_SAMPLE_CALLCHAIN;
294
c7eced63
AH
295 if (session->itrace_synth_opts->last_branch)
296 sample_type |= PERF_SAMPLE_BRANCH_STACK;
297
cc9784bd 298 if (!is_pipe && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
de7e6a7c 299 if (perf_hpp_list.parent) {
3780f488 300 ui__error("Selected --sort parent, but no "
00894ce9
ACM
301 "callchain data. Did you call "
302 "'perf record' without -g?\n");
d549c769 303 return -EINVAL;
91b4eaea 304 }
b49a821e
JY
305 if (symbol_conf.use_callchain &&
306 !symbol_conf.show_branchflag_count) {
307 ui__error("Selected -g or --branch-history.\n"
308 "But no callchain or branch data.\n"
309 "Did you call 'perf record' without -g or -b?\n");
016e92fb 310 return -1;
91b4eaea 311 }
1cc83815 312 } else if (!callchain_param.enabled &&
fa372aae 313 callchain_param.mode != CHAIN_NONE &&
b9a63b9b 314 !symbol_conf.use_callchain) {
d599db3f 315 symbol_conf.use_callchain = true;
16537f13 316 if (callchain_register_param(&callchain_param) < 0) {
3780f488 317 ui__error("Can't register callchain params.\n");
d549c769 318 return -EINVAL;
b1a88349 319 }
f5970550
PZ
320 }
321
793aaaab
NK
322 if (symbol_conf.cumulate_callchain) {
323 /* Silently ignore if callchain is missing */
324 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
325 symbol_conf.cumulate_callchain = false;
326 perf_hpp__cancel_cumulate();
327 }
328 }
329
55369fc1 330 if (sort__mode == SORT_MODE__BRANCH) {
cc9784bd 331 if (!is_pipe &&
7f3be652 332 !(sample_type & PERF_SAMPLE_BRANCH_STACK)) {
3780f488
NK
333 ui__error("Selected -b but no branch data. "
334 "Did you call perf record without -b?\n");
b50311dc
RAV
335 return -1;
336 }
337 }
338
0cdccac6
NK
339 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
340 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
eabad8c6 341 (sample_type & PERF_SAMPLE_STACK_USER)) {
0cdccac6 342 callchain_param.record_mode = CALLCHAIN_DWARF;
eabad8c6
ACM
343 dwarf_callchain_users = true;
344 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
aad2b21c 345 callchain_param.record_mode = CALLCHAIN_LBR;
0cdccac6
NK
346 else
347 callchain_param.record_mode = CALLCHAIN_FP;
348 }
98df858e
AK
349
350 /* ??? handle more cases than just ANY? */
351 if (!(perf_evlist__combined_branch_type(session->evlist) &
352 PERF_SAMPLE_BRANCH_ANY))
353 rep->nonany_branch_mode = true;
354
016e92fb
FW
355 return 0;
356}
6142f9ec 357
1d037ca1 358static void sig_handler(int sig __maybe_unused)
46656ac7
TZ
359{
360 session_done = 1;
361}
362
28b21393 363static size_t hists__fprintf_nr_sample_events(struct hists *hists, struct report *rep,
c82ee828
ACM
364 const char *evname, FILE *fp)
365{
366 size_t ret;
367 char unit;
c824c433
ACM
368 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
369 u64 nr_events = hists->stats.total_period;
370 struct perf_evsel *evsel = hists_to_evsel(hists);
717e263f
NK
371 char buf[512];
372 size_t size = sizeof(buf);
84734b06 373 int socked_id = hists->socket_filter;
717e263f 374
27fafab5
NK
375 if (quiet)
376 return 0;
377
f2148330
NK
378 if (symbol_conf.filter_relative) {
379 nr_samples = hists->stats.nr_non_filtered_samples;
380 nr_events = hists->stats.total_non_filtered_period;
381 }
382
759ff497 383 if (perf_evsel__is_group_event(evsel)) {
717e263f
NK
384 struct perf_evsel *pos;
385
386 perf_evsel__group_desc(evsel, buf, size);
387 evname = buf;
388
389 for_each_group_member(pos, evsel) {
4ea062ed
ACM
390 const struct hists *pos_hists = evsel__hists(pos);
391
f2148330 392 if (symbol_conf.filter_relative) {
4ea062ed
ACM
393 nr_samples += pos_hists->stats.nr_non_filtered_samples;
394 nr_events += pos_hists->stats.total_non_filtered_period;
f2148330 395 } else {
4ea062ed
ACM
396 nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
397 nr_events += pos_hists->stats.total_period;
f2148330 398 }
717e263f
NK
399 }
400 }
c82ee828 401
cc686280
AR
402 nr_samples = convert_unit(nr_samples, &unit);
403 ret = fprintf(fp, "# Samples: %lu%c", nr_samples, unit);
c82ee828 404 if (evname != NULL)
cc686280
AR
405 ret += fprintf(fp, " of event '%s'", evname);
406
9e207ddf
KL
407 if (symbol_conf.show_ref_callgraph &&
408 strstr(evname, "call-graph=no")) {
409 ret += fprintf(fp, ", show reference callgraph");
410 }
411
f4f7e28d
SE
412 if (rep->mem_mode) {
413 ret += fprintf(fp, "\n# Total weight : %" PRIu64, nr_events);
228f14f2 414 ret += fprintf(fp, "\n# Sort order : %s", sort_order ? : default_mem_sort_order);
f4f7e28d
SE
415 } else
416 ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events);
21394d94 417
84734b06
KL
418 if (socked_id > -1)
419 ret += fprintf(fp, "\n# Processor Socket: %d", socked_id);
21394d94 420
c82ee828
ACM
421 return ret + fprintf(fp, "\n#\n");
422}
423
7f0030b2 424static int perf_evlist__tty_browse_hists(struct perf_evlist *evlist,
28b21393 425 struct report *rep,
7f0030b2 426 const char *help)
d67f088e 427{
e248de33 428 struct perf_evsel *pos;
d67f088e 429
27fafab5
NK
430 if (!quiet) {
431 fprintf(stdout, "#\n# Total Lost Samples: %" PRIu64 "\n#\n",
432 evlist->stats.total_lost_samples);
433 }
434
e5cadb93 435 evlist__for_each_entry(evlist, pos) {
4ea062ed 436 struct hists *hists = evsel__hists(pos);
7289f83c 437 const char *evname = perf_evsel__name(pos);
d67f088e 438
fc24d7c2
NK
439 if (symbol_conf.event_group &&
440 !perf_evsel__is_group_leader(pos))
441 continue;
442
28b21393 443 hists__fprintf_nr_sample_events(hists, rep, evname, stdout);
27fafab5 444 hists__fprintf(hists, !quiet, 0, 0, rep->min_percent, stdout,
b49a821e
JY
445 symbol_conf.use_callchain ||
446 symbol_conf.show_branchflag_count);
d67f088e 447 fprintf(stdout, "\n\n");
d67f088e
ACM
448 }
449
8b53dbef 450 if (!quiet)
d67f088e
ACM
451 fprintf(stdout, "#\n# (%s)\n#\n", help);
452
021162cf
NK
453 if (rep->show_threads) {
454 bool style = !strcmp(rep->pretty_printing_style, "raw");
455 perf_read_values_display(stdout, &rep->show_threads_values,
456 style);
457 perf_read_values_destroy(&rep->show_threads_values);
d67f088e
ACM
458 }
459
2d78b189
JY
460 if (sort__mode == SORT_MODE__BRANCH)
461 branch_type_stat_display(stdout, &rep->brtype_stat);
462
d67f088e
ACM
463 return 0;
464}
465
fad2918e
ACM
466static void report__warn_kptr_restrict(const struct report *rep)
467{
a5e813c6 468 struct map *kernel_map = machine__kernel_map(&rep->session->machines.host);
f6fcc143 469 struct kmap *kernel_kmap = kernel_map ? map__kmap(kernel_map) : NULL;
fad2918e 470
9c39ed90
ACM
471 if (perf_evlist__exclude_kernel(rep->session->evlist))
472 return;
473
fad2918e
ACM
474 if (kernel_map == NULL ||
475 (kernel_map->dso->hit &&
476 (kernel_kmap->ref_reloc_sym == NULL ||
477 kernel_kmap->ref_reloc_sym->addr == 0))) {
478 const char *desc =
479 "As no suitable kallsyms nor vmlinux was found, kernel samples\n"
480 "can't be resolved.";
481
482 if (kernel_map) {
483 const struct dso *kdso = kernel_map->dso;
484 if (!RB_EMPTY_ROOT(&kdso->symbols[MAP__FUNCTION])) {
485 desc = "If some relocation was applied (e.g. "
486 "kexec) symbols may be misresolved.";
487 }
488 }
489
490 ui__warning(
491"Kernel address maps (/proc/{kallsyms,modules}) were restricted.\n\n"
492"Check /proc/sys/kernel/kptr_restrict before running 'perf record'.\n\n%s\n\n"
493"Samples in kernel modules can't be resolved as well.\n\n",
494 desc);
495 }
496}
497
8362951b
ACM
498static int report__gtk_browse_hists(struct report *rep, const char *help)
499{
500 int (*hist_browser)(struct perf_evlist *evlist, const char *help,
501 struct hist_browser_timer *timer, float min_pcnt);
502
503 hist_browser = dlsym(perf_gtk_handle, "perf_evlist__gtk_browse_hists");
504
505 if (hist_browser == NULL) {
506 ui__error("GTK browser not found!\n");
507 return -1;
508 }
509
510 return hist_browser(rep->session->evlist, help, NULL, rep->min_percent);
511}
512
513static int report__browse_hists(struct report *rep)
514{
515 int ret;
516 struct perf_session *session = rep->session;
517 struct perf_evlist *evlist = session->evlist;
34b7b0f9
NK
518 const char *help = perf_tip(system_path(TIPDIR));
519
520 if (help == NULL) {
521 /* fallback for people who don't install perf ;-) */
522 help = perf_tip(DOCDIR);
523 if (help == NULL)
524 help = "Cannot load tips.txt file, please install perf!";
525 }
8362951b
ACM
526
527 switch (use_browser) {
528 case 1:
529 ret = perf_evlist__tui_browse_hists(evlist, help, NULL,
530 rep->min_percent,
531 &session->header.env);
532 /*
533 * Usually "ret" is the last pressed key, and we only
534 * care if the key notifies us to switch data file.
535 */
536 if (ret != K_SWITCH_INPUT_DATA)
537 ret = 0;
538 break;
539 case 2:
540 ret = report__gtk_browse_hists(rep, help);
541 break;
542 default:
543 ret = perf_evlist__tty_browse_hists(evlist, rep, help);
544 break;
545 }
546
547 return ret;
548}
549
5b2ea6f2 550static int report__collapse_hists(struct report *rep)
f6d8b057
ACM
551{
552 struct ui_progress prog;
553 struct perf_evsel *pos;
5b2ea6f2 554 int ret = 0;
f6d8b057 555
58c311da 556 ui_progress__init(&prog, rep->nr_entries, "Merging related events...");
f6d8b057 557
e5cadb93 558 evlist__for_each_entry(rep->session->evlist, pos) {
4ea062ed 559 struct hists *hists = evsel__hists(pos);
f6d8b057
ACM
560
561 if (pos->idx == 0)
562 hists->symbol_filter_str = rep->symbol_filter_str;
563
21394d94
KL
564 hists->socket_filter = rep->socket_filter;
565
5b2ea6f2
NK
566 ret = hists__collapse_resort(hists, &prog);
567 if (ret < 0)
568 break;
f6d8b057
ACM
569
570 /* Non-group events are considered as leader */
571 if (symbol_conf.event_group &&
572 !perf_evsel__is_group_leader(pos)) {
4ea062ed 573 struct hists *leader_hists = evsel__hists(pos->leader);
f6d8b057
ACM
574
575 hists__match(leader_hists, hists);
576 hists__link(leader_hists, hists);
577 }
578 }
579
580 ui_progress__finish();
5b2ea6f2 581 return ret;
f6d8b057
ACM
582}
583
740b97f9
NK
584static void report__output_resort(struct report *rep)
585{
586 struct ui_progress prog;
587 struct perf_evsel *pos;
588
589 ui_progress__init(&prog, rep->nr_entries, "Sorting events for output...");
590
e5cadb93 591 evlist__for_each_entry(rep->session->evlist, pos)
452ce03b 592 perf_evsel__output_resort(pos, &prog);
740b97f9
NK
593
594 ui_progress__finish();
595}
596
a4a4d0a7
JO
597static void stats_setup(struct report *rep)
598{
599 memset(&rep->tool, 0, sizeof(rep->tool));
600 rep->tool.no_warn = true;
601}
602
603static int stats_print(struct report *rep)
604{
605 struct perf_session *session = rep->session;
606
607 perf_session__fprintf_nr_events(session, stdout);
608 return 0;
609}
610
930f8b34
JO
611static void tasks_setup(struct report *rep)
612{
613 memset(&rep->tool, 0, sizeof(rep->tool));
6439d7d1
ACM
614 if (rep->mmaps_mode) {
615 rep->tool.mmap = perf_event__process_mmap;
616 rep->tool.mmap2 = perf_event__process_mmap2;
617 }
930f8b34
JO
618 rep->tool.comm = perf_event__process_comm;
619 rep->tool.exit = perf_event__process_exit;
620 rep->tool.fork = perf_event__process_fork;
621 rep->tool.no_warn = true;
622}
623
624struct task {
625 struct thread *thread;
626 struct list_head list;
627 struct list_head children;
628};
629
630static struct task *tasks_list(struct task *task, struct machine *machine)
631{
632 struct thread *parent_thread, *thread = task->thread;
633 struct task *parent_task;
634
635 /* Already listed. */
636 if (!list_empty(&task->list))
637 return NULL;
638
639 /* Last one in the chain. */
640 if (thread->ppid == -1)
641 return task;
642
643 parent_thread = machine__find_thread(machine, -1, thread->ppid);
644 if (!parent_thread)
645 return ERR_PTR(-ENOENT);
646
647 parent_task = thread__priv(parent_thread);
648 list_add_tail(&task->list, &parent_task->children);
649 return tasks_list(parent_task, machine);
650}
651
6439d7d1
ACM
652static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp)
653{
654 size_t printed = 0;
655 struct rb_node *nd;
656
657 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
658 struct map *map = rb_entry(nd, struct map, rb_node);
659
660 printed += fprintf(fp, "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %" PRIu64 " %s\n",
661 indent, "", map->start, map->end,
662 map->prot & PROT_READ ? 'r' : '-',
663 map->prot & PROT_WRITE ? 'w' : '-',
664 map->prot & PROT_EXEC ? 'x' : '-',
665 map->flags & MAP_SHARED ? 's' : 'p',
666 map->pgoff,
667 map->ino, map->dso->name);
668 }
669
670 return printed;
671}
672
673static int map_groups__fprintf_task(struct map_groups *mg, int indent, FILE *fp)
674{
675 int printed = 0, i;
676 for (i = 0; i < MAP__NR_TYPES; ++i)
677 printed += maps__fprintf_task(&mg->maps[i], indent, fp);
678 return printed;
679}
680
930f8b34
JO
681static void task__print_level(struct task *task, FILE *fp, int level)
682{
683 struct thread *thread = task->thread;
684 struct task *child;
6439d7d1
ACM
685 int comm_indent = fprintf(fp, " %8d %8d %8d |%*s",
686 thread->pid_, thread->tid, thread->ppid,
687 level, "");
688
689 fprintf(fp, "%s\n", thread__comm_str(thread));
930f8b34 690
6439d7d1 691 map_groups__fprintf_task(thread->mg, comm_indent, fp);
930f8b34
JO
692
693 if (!list_empty(&task->children)) {
694 list_for_each_entry(child, &task->children, list)
695 task__print_level(child, fp, level + 1);
696 }
697}
698
699static int tasks_print(struct report *rep, FILE *fp)
700{
701 struct perf_session *session = rep->session;
702 struct machine *machine = &session->machines.host;
703 struct task *tasks, *task;
704 unsigned int nr = 0, itask = 0, i;
705 struct rb_node *nd;
706 LIST_HEAD(list);
707
708 /*
709 * No locking needed while accessing machine->threads,
710 * because --tasks is single threaded command.
711 */
712
713 /* Count all the threads. */
714 for (i = 0; i < THREADS__TABLE_SIZE; i++)
715 nr += machine->threads[i].nr;
716
717 tasks = malloc(sizeof(*tasks) * nr);
718 if (!tasks)
719 return -ENOMEM;
720
721 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
722 struct threads *threads = &machine->threads[i];
723
724 for (nd = rb_first(&threads->entries); nd; nd = rb_next(nd)) {
725 task = tasks + itask++;
726
727 task->thread = rb_entry(nd, struct thread, rb_node);
728 INIT_LIST_HEAD(&task->children);
729 INIT_LIST_HEAD(&task->list);
730 thread__set_priv(task->thread, task);
731 }
732 }
733
734 /*
735 * Iterate every task down to the unprocessed parent
736 * and link all in task children list. Task with no
737 * parent is added into 'list'.
738 */
739 for (itask = 0; itask < nr; itask++) {
740 task = tasks + itask;
741
742 if (!list_empty(&task->list))
743 continue;
744
745 task = tasks_list(task, machine);
746 if (IS_ERR(task)) {
747 pr_err("Error: failed to process tasks\n");
748 free(tasks);
749 return PTR_ERR(task);
750 }
751
752 if (task)
753 list_add_tail(&task->list, &list);
754 }
755
756 fprintf(fp, "# %8s %8s %8s %s\n", "pid", "tid", "ppid", "comm");
757
758 list_for_each_entry(task, &list, list)
759 task__print_level(task, fp, 0);
760
761 free(tasks);
762 return 0;
763}
764
28b21393 765static int __cmd_report(struct report *rep)
016e92fb 766{
f6d8b057 767 int ret;
993ac88d 768 struct perf_session *session = rep->session;
e248de33 769 struct perf_evsel *pos;
8ceb41d7 770 struct perf_data *data = session->data;
8fa66bdc 771
46656ac7
TZ
772 signal(SIGINT, sig_handler);
773
fa372aae
ACM
774 if (rep->cpu_list) {
775 ret = perf_session__cpu_bitmap(session, rep->cpu_list,
776 rep->cpu_bitmap);
25b1606b
NK
777 if (ret) {
778 ui__error("failed to set cpu bitmap\n");
d4ae0a6f 779 return ret;
25b1606b 780 }
644e0840 781 session->itrace_synth_opts->cpu_bitmap = rep->cpu_bitmap;
5d67be97
AB
782 }
783
89973506
ACM
784 if (rep->show_threads) {
785 ret = perf_read_values_init(&rep->show_threads_values);
786 if (ret)
787 return ret;
788 }
f5970550 789
28b21393 790 ret = report__setup_sample_type(rep);
25b1606b
NK
791 if (ret) {
792 /* report__setup_sample_type() already showed error message */
d4ae0a6f 793 return ret;
25b1606b 794 }
d549c769 795
a4a4d0a7
JO
796 if (rep->stats_mode)
797 stats_setup(rep);
798
930f8b34
JO
799 if (rep->tasks_mode)
800 tasks_setup(rep);
801
b7b61cbe 802 ret = perf_session__process_events(session);
25b1606b
NK
803 if (ret) {
804 ui__error("failed to process sample\n");
d4ae0a6f 805 return ret;
25b1606b 806 }
97b07b69 807
a4a4d0a7
JO
808 if (rep->stats_mode)
809 return stats_print(rep);
810
930f8b34
JO
811 if (rep->tasks_mode)
812 return tasks_print(rep, stdout);
813
fad2918e 814 report__warn_kptr_restrict(rep);
ec80fde7 815
e5cadb93 816 evlist__for_each_entry(session->evlist, pos)
590cd344
NK
817 rep->nr_entries += evsel__hists(pos)->nr_entries;
818
150e465a
NK
819 if (use_browser == 0) {
820 if (verbose > 3)
821 perf_session__fprintf(session, stdout);
9ac99545 822
150e465a
NK
823 if (verbose > 2)
824 perf_session__fprintf_dsos(session, stdout);
16f762a2 825
150e465a
NK
826 if (dump_trace) {
827 perf_session__fprintf_nr_events(session, stdout);
2a1731fb 828 perf_evlist__fprintf_nr_events(session->evlist, stdout);
150e465a
NK
829 return 0;
830 }
71ad0f5e
JO
831 }
832
5b2ea6f2
NK
833 ret = report__collapse_hists(rep);
834 if (ret) {
835 ui__error("failed to process hist entry\n");
836 return ret;
837 }
e248de33 838
33e940a2
ACM
839 if (session_done())
840 return 0;
841
740b97f9
NK
842 /*
843 * recalculate number of entries after collapsing since it
844 * might be changed during the collapse phase.
845 */
846 rep->nr_entries = 0;
e5cadb93 847 evlist__for_each_entry(session->evlist, pos)
740b97f9
NK
848 rep->nr_entries += evsel__hists(pos)->nr_entries;
849
58c311da 850 if (rep->nr_entries == 0) {
eae8ad80 851 ui__error("The %s file has no samples!\n", data->file.path);
d4ae0a6f 852 return 0;
cbbc79a5
EM
853 }
854
740b97f9 855 report__output_resort(rep);
6e1f601a 856
8362951b 857 return report__browse_hists(rep);
8fa66bdc
ACM
858}
859
4eb3e478 860static int
cff6bb46 861report_parse_callchain_opt(const struct option *opt, const char *arg, int unset)
4eb3e478 862{
1cc83815 863 struct callchain_param *callchain = opt->value;
c20ab37e 864
1cc83815 865 callchain->enabled = !unset;
b9a63b9b
ACM
866 /*
867 * --no-call-graph
868 */
869 if (unset) {
1cc83815
ACM
870 symbol_conf.use_callchain = false;
871 callchain->mode = CHAIN_NONE;
b9a63b9b
ACM
872 return 0;
873 }
874
cff6bb46 875 return parse_callchain_report_opt(arg);
4eb3e478
FW
876}
877
b21484f1
GP
878int
879report_parse_ignore_callees_opt(const struct option *opt __maybe_unused,
880 const char *arg, int unset __maybe_unused)
881{
882 if (arg) {
883 int err = regcomp(&ignore_callees_regex, arg, REG_EXTENDED);
884 if (err) {
885 char buf[BUFSIZ];
886 regerror(err, &ignore_callees_regex, buf, sizeof(buf));
887 pr_err("Invalid --ignore-callees regex: %s\n%s", arg, buf);
888 return -1;
889 }
890 have_ignore_callees = 1;
891 }
892
893 return 0;
894}
895
993ac88d 896static int
7e6a7998 897parse_branch_mode(const struct option *opt,
1d037ca1 898 const char *str __maybe_unused, int unset)
993ac88d 899{
55369fc1
NK
900 int *branch_mode = opt->value;
901
902 *branch_mode = !unset;
993ac88d
SE
903 return 0;
904}
905
064f1981
NK
906static int
907parse_percent_limit(const struct option *opt, const char *str,
908 int unset __maybe_unused)
909{
28b21393 910 struct report *rep = opt->value;
2665b452 911 double pcnt = strtof(str, NULL);
064f1981 912
2665b452
NK
913 rep->min_percent = pcnt;
914 callchain_param.min_percent = pcnt;
064f1981
NK
915 return 0;
916}
917
f2af0086 918#define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
76a26549
NK
919
920const char report_callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
921 CALLCHAIN_REPORT_HELP
922 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
21cf6284 923
b0ad8ea6 924int cmd_report(int argc, const char **argv)
d20deb64 925{
993ac88d 926 struct perf_session *session;
520a2ebc 927 struct itrace_synth_opts itrace_synth_opts = { .set = 0, };
efad1415 928 struct stat st;
993ac88d 929 bool has_br_stack = false;
55369fc1 930 int branch_mode = -1;
fa94c36c 931 bool branch_call_mode = false;
76a26549 932 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
d20deb64 933 const char * const report_usage[] = {
fb2baceb 934 "perf report [<options>]",
d20deb64
ACM
935 NULL
936 };
28b21393 937 struct report report = {
45694aa7 938 .tool = {
d20deb64
ACM
939 .sample = process_sample_event,
940 .mmap = perf_event__process_mmap,
5c5e854b 941 .mmap2 = perf_event__process_mmap2,
d20deb64 942 .comm = perf_event__process_comm,
f3b3614a 943 .namespaces = perf_event__process_namespaces,
f62d3f0f
ACM
944 .exit = perf_event__process_exit,
945 .fork = perf_event__process_fork,
d20deb64
ACM
946 .lost = perf_event__process_lost,
947 .read = process_read_event,
948 .attr = perf_event__process_attr,
d20deb64
ACM
949 .tracing_data = perf_event__process_tracing_data,
950 .build_id = perf_event__process_build_id,
520a2ebc
AH
951 .id_index = perf_event__process_id_index,
952 .auxtrace_info = perf_event__process_auxtrace_info,
953 .auxtrace = perf_event__process_auxtrace,
e9def1b2 954 .feature = perf_event__process_feature,
0a8cb85c 955 .ordered_events = true,
d20deb64
ACM
956 .ordering_requires_timestamps = true,
957 },
fe176085 958 .max_stack = PERF_MAX_STACK_DEPTH,
d20deb64 959 .pretty_printing_style = "normal",
21394d94 960 .socket_filter = -1,
d20deb64
ACM
961 };
962 const struct option options[] = {
70cb4e96 963 OPT_STRING('i', "input", &input_name, "file",
53cb8bc2 964 "input file name"),
c0555642 965 OPT_INCR('v', "verbose", &verbose,
815e777f 966 "be more verbose (show symbol address, etc)"),
27fafab5 967 OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any message"),
97b07b69
IM
968 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
969 "dump raw trace in ASCII"),
a4a4d0a7 970 OPT_BOOLEAN(0, "stats", &report.stats_mode, "Display event stats"),
930f8b34 971 OPT_BOOLEAN(0, "tasks", &report.tasks_mode, "Display recorded tasks"),
6439d7d1 972 OPT_BOOLEAN(0, "mmaps", &report.mmaps_mode, "Display recorded tasks memory maps"),
b32d133a
ACM
973 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
974 "file", "vmlinux pathname"),
b226a5a7
DA
975 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
976 "file", "kallsyms pathname"),
2059fc7a 977 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
b32d133a 978 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 979 "load module symbols - WARNING: use only with -k and LIVE kernel"),
d599db3f 980 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
e3d7e183 981 "Show a column with the number of samples"),
fa372aae 982 OPT_BOOLEAN('T', "threads", &report.show_threads,
8d513270 983 "Show per-thread event counters"),
fa372aae 984 OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
9f866697 985 "pretty printing style key: normal raw"),
fa372aae 986 OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
c31a9457 987 OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK2 interface"),
fa372aae
ACM
988 OPT_BOOLEAN(0, "stdio", &report.use_stdio,
989 "Use the stdio interface"),
5cfe2c82
JO
990 OPT_BOOLEAN(0, "header", &report.header, "Show data header."),
991 OPT_BOOLEAN(0, "header-only", &report.header_only,
992 "Show only data header."),
63299f05 993 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
a2ce067e
NK
994 "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
995 " Please refer the man page for the complete list."),
a7d945bc
NK
996 OPT_STRING('F', "fields", &field_order, "key[,keys...]",
997 "output field(s): overhead, period, sample plus all of sort keys"),
b272a59d 998 OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization,
a1645ce1 999 "Show sample percentage for different cpu modes"),
b272a59d
NK
1000 OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
1001 "Show sample percentage for different cpu modes", PARSE_OPT_HIDDEN),
b25bcf2f
IM
1002 OPT_STRING('p', "parent", &parent_pattern, "regex",
1003 "regex filter to identify parent, see: '--sort parent'"),
d599db3f 1004 OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
b8e6d829 1005 "Only display entries with parent-match"),
1cc83815 1006 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
f2af0086 1007 "print_type,threshold[,print_limit],order,sort_key[,branch],value",
21cf6284
NK
1008 report_callchain_help, &report_parse_callchain_opt,
1009 callchain_default_opt),
793aaaab
NK
1010 OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
1011 "Accumulate callchains of children and show total overhead as well"),
91e95617
WL
1012 OPT_INTEGER(0, "max-stack", &report.max_stack,
1013 "Set the maximum stack depth when parsing the callchain, "
1014 "anything beyond the specified depth will be ignored. "
4cb93446 1015 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
fa372aae
ACM
1016 OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
1017 "alias for inverted call graph"),
b21484f1
GP
1018 OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
1019 "ignore callees of these functions in call graphs",
1020 report_parse_ignore_callees_opt),
655000e7 1021 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
25903407 1022 "only consider symbols in these dsos"),
c8e66720 1023 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
cc8b88b1 1024 "only consider symbols in these comms"),
e03eaa40
DA
1025 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1026 "only consider symbols in these pids"),
1027 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1028 "only consider symbols in these tids"),
655000e7 1029 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
7bec7a91 1030 "only consider these symbols"),
b14ffaca
NK
1031 OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
1032 "only show symbols that (partially) match with this filter"),
655000e7 1033 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
52d422de
ACM
1034 "width[,width...]",
1035 "don't try to adjust column width, use these fixed values"),
0c8c2077 1036 OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
52d422de
ACM
1037 "separator for columns, no spaces will be added between "
1038 "columns '.' is reserved."),
b49a8fe5 1039 OPT_BOOLEAN('U', "hide-unresolved", &symbol_conf.hide_unresolved,
71289be7 1040 "Only display entries resolved to a symbol"),
a7066709
HK
1041 OPT_CALLBACK(0, "symfs", NULL, "directory",
1042 "Look for files with symbols relative to this directory",
1043 symbol__config_symfs),
c8e66720 1044 OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
fa372aae
ACM
1045 "list of cpus to profile"),
1046 OPT_BOOLEAN('I', "show-info", &report.show_full_info,
fbe96f29 1047 "Display extended information about perf.data file"),
64c6f0c7
ACM
1048 OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
1049 "Interleave source code with assembly code (default)"),
1050 OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
1051 "Display raw encoding of assembly instructions (default)"),
f69b64f7
AK
1052 OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
1053 "Specify disassembler style (e.g. -M intel for intel syntax)"),
3f2728bd
ACM
1054 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
1055 "Show a column with the sum of periods"),
01d14f16
NK
1056 OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
1057 "Show event group information together"),
55369fc1 1058 OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "",
fa94c36c
AK
1059 "use branch records for per branch histogram filling",
1060 parse_branch_mode),
1061 OPT_BOOLEAN(0, "branch-history", &branch_call_mode,
1062 "add last branch records to call history"),
7a4ec938
MB
1063 OPT_STRING(0, "objdump", &objdump_path, "path",
1064 "objdump binary to use for disassembly and annotations"),
328ccdac
NK
1065 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1066 "Disable symbol demangling"),
763122ad
AK
1067 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1068 "Enable kernel symbol demangling"),
f4f7e28d 1069 OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
064f1981
NK
1070 OPT_CALLBACK(0, "percent-limit", &report, "percent",
1071 "Don't show entries under that percent", parse_percent_limit),
f2148330 1072 OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
33db4568 1073 "how to display percentage of filtered entries", parse_filter_percentage),
520a2ebc
AH
1074 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1075 "Instruction Tracing options",
1076 itrace_parse_synth_opts),
a9710ba0
AK
1077 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1078 "Show full source file name path for source lines"),
9e207ddf
KL
1079 OPT_BOOLEAN(0, "show-ref-call-graph", &symbol_conf.show_ref_callgraph,
1080 "Show callgraph from reference event"),
21394d94
KL
1081 OPT_INTEGER(0, "socket-filter", &report.socket_filter,
1082 "only show processor socket that match with this filter"),
053a3989
NK
1083 OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
1084 "Show raw trace event output (do not use print fmt or plugins)"),
4251446d
NK
1085 OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
1086 "Show entries in a hierarchy"),
175b968b
ACM
1087 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
1088 "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
1089 stdio__config_color, "always"),
46690a80
DA
1090 OPT_STRING(0, "time", &report.time_str, "str",
1091 "Time span of interest (start,stop)"),
f3a60646
JY
1092 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
1093 "Show inline function"),
53cb8bc2 1094 OPT_END()
d20deb64 1095 };
8ceb41d7 1096 struct perf_data data = {
f5fc1412
JO
1097 .mode = PERF_DATA_MODE_READ,
1098 };
a635fc51
ACM
1099 int ret = hists__init();
1100
1101 if (ret < 0)
1102 return ret;
53cb8bc2 1103
ecc4c561
ACM
1104 ret = perf_config(report__config, &report);
1105 if (ret)
1106 return ret;
00c7e1f1 1107
655000e7 1108 argc = parse_options(argc, argv, options, report_usage, 0);
b3f38fc2
NK
1109 if (argc) {
1110 /*
1111 * Special case: if there's an argument left then assume that
1112 * it's a symbol filter:
1113 */
1114 if (argc > 1)
1115 usage_with_options(report_usage, options);
1116
1117 report.symbol_filter_str = argv[0];
1118 }
655000e7 1119
6439d7d1
ACM
1120 if (report.mmaps_mode)
1121 report.tasks_mode = true;
1122
27fafab5
NK
1123 if (quiet)
1124 perf_quiet_option();
1125
36c8bb56
LZ
1126 if (symbol_conf.vmlinux_name &&
1127 access(symbol_conf.vmlinux_name, R_OK)) {
1128 pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
1129 return -EINVAL;
1130 }
1131 if (symbol_conf.kallsyms_name &&
1132 access(symbol_conf.kallsyms_name, R_OK)) {
1133 pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
1134 return -EINVAL;
1135 }
1136
fa372aae 1137 if (report.inverted_callchain)
d797fdc5 1138 callchain_param.order = ORDER_CALLER;
792aeafa
NK
1139 if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
1140 callchain_param.order = ORDER_CALLER;
d797fdc5 1141
188bb5e2
AH
1142 if (itrace_synth_opts.callchain &&
1143 (int)itrace_synth_opts.callchain_sz > report.max_stack)
1144 report.max_stack = itrace_synth_opts.callchain_sz;
1145
70cb4e96 1146 if (!input_name || !strlen(input_name)) {
efad1415 1147 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
70cb4e96 1148 input_name = "-";
efad1415 1149 else
70cb4e96 1150 input_name = "perf.data";
efad1415 1151 }
ad0de097 1152
eae8ad80
JO
1153 data.file.path = input_name;
1154 data.force = symbol_conf.force;
f5fc1412 1155
ad0de097 1156repeat:
8ceb41d7 1157 session = perf_session__new(&data, false, &report.tool);
993ac88d 1158 if (session == NULL)
52e02834 1159 return -1;
993ac88d 1160
94786b67
JO
1161 if (report.queue_size) {
1162 ordered_events__set_alloc_size(&session->ordered_events,
1163 report.queue_size);
1164 }
1165
520a2ebc
AH
1166 session->itrace_synth_opts = &itrace_synth_opts;
1167
993ac88d
SE
1168 report.session = session;
1169
1170 has_br_stack = perf_header__has_feat(&session->header,
1171 HEADER_BRANCH_STACK);
efad1415 1172
fb9fab66
AH
1173 if (itrace_synth_opts.last_branch)
1174 has_br_stack = true;
1175
f9a7be7c
JY
1176 if (has_br_stack && branch_call_mode)
1177 symbol_conf.show_branchflag_count = true;
1178
2d78b189
JY
1179 memset(&report.brtype_stat, 0, sizeof(struct branch_type_stat));
1180
fa94c36c
AK
1181 /*
1182 * Branch mode is a tristate:
1183 * -1 means default, so decide based on the file having branch data.
1184 * 0/1 means the user chose a mode.
1185 */
1186 if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) &&
fefd2d96 1187 !branch_call_mode) {
55369fc1 1188 sort__mode = SORT_MODE__BRANCH;
793aaaab
NK
1189 symbol_conf.cumulate_callchain = false;
1190 }
fa94c36c 1191 if (branch_call_mode) {
09a6a1b0 1192 callchain_param.key = CCKEY_ADDRESS;
fa94c36c
AK
1193 callchain_param.branch_callstack = 1;
1194 symbol_conf.use_callchain = true;
1195 callchain_register_param(&callchain_param);
1196 if (sort_order == NULL)
1197 sort_order = "srcline,symbol,dso";
1198 }
993ac88d 1199
f4f7e28d 1200 if (report.mem_mode) {
55369fc1 1201 if (sort__mode == SORT_MODE__BRANCH) {
a4210141 1202 pr_err("branch and mem mode incompatible\n");
f4f7e28d
SE
1203 goto error;
1204 }
afab87b9 1205 sort__mode = SORT_MODE__MEMORY;
793aaaab 1206 symbol_conf.cumulate_callchain = false;
f4f7e28d 1207 }
a68c2c58 1208
4251446d
NK
1209 if (symbol_conf.report_hierarchy) {
1210 /* disable incompatible options */
4251446d
NK
1211 symbol_conf.cumulate_callchain = false;
1212
1213 if (field_order) {
1214 pr_err("Error: --hierarchy and --fields options cannot be used together\n");
1215 parse_options_usage(report_usage, options, "F", 1);
1216 parse_options_usage(NULL, options, "hierarchy", 0);
1217 goto error;
1218 }
1219
52225036 1220 perf_hpp_list.need_collapse = true;
4251446d
NK
1221 }
1222
712d36db
SS
1223 if (report.use_stdio)
1224 use_browser = 0;
1225 else if (report.use_tui)
1226 use_browser = 1;
1227 else if (report.use_gtk)
1228 use_browser = 2;
1229
b138f42e
NK
1230 /* Force tty output for header output and per-thread stat. */
1231 if (report.header || report.header_only || report.show_threads)
5cfe2c82 1232 use_browser = 0;
114f709e
DCC
1233 if (report.header || report.header_only)
1234 report.tool.show_feat_hdr = SHOW_FEAT_HEADER;
1235 if (report.show_full_info)
1236 report.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
930f8b34 1237 if (report.stats_mode || report.tasks_mode)
a4a4d0a7 1238 use_browser = 0;
930f8b34 1239 if (report.stats_mode && report.tasks_mode) {
6439d7d1 1240 pr_err("Error: --tasks and --mmaps can't be used together with --stats\n");
930f8b34
JO
1241 goto error;
1242 }
5cfe2c82 1243
4bceffbc
NK
1244 if (strcmp(input_name, "-") != 0)
1245 setup_browser(true);
22af969e 1246 else
4bceffbc 1247 use_browser = 0;
4bceffbc 1248
9887804d
JO
1249 if (setup_sorting(session->evlist) < 0) {
1250 if (sort_order)
1251 parse_options_usage(report_usage, options, "s", 1);
1252 if (field_order)
1253 parse_options_usage(sort_order ? NULL : report_usage,
1254 options, "F", 1);
1255 goto error;
1256 }
1257
27fafab5 1258 if ((report.header || report.header_only) && !quiet) {
5cfe2c82
JO
1259 perf_session__fprintf_info(session, stdout,
1260 report.show_full_info);
07a716ff
TS
1261 if (report.header_only) {
1262 ret = 0;
1263 goto error;
1264 }
930f8b34
JO
1265 } else if (use_browser == 0 && !quiet &&
1266 !report.stats_mode && !report.tasks_mode) {
5cfe2c82
JO
1267 fputs("# To display the perf.data header info, please use --header/--header-only options.\n#\n",
1268 stdout);
1269 }
1270
ef7b93a1 1271 /*
6692c262 1272 * Only in the TUI browser we are doing integrated annotation,
ef7b93a1
ACM
1273 * so don't allocate extra space that won't be used in the stdio
1274 * implementation.
1275 */
b9ce0c99 1276 if (ui__has_annotation()) {
b01141f4
ACM
1277 ret = symbol__annotation_init();
1278 if (ret < 0)
1279 goto error;
80d50cae
ACM
1280 /*
1281 * For searching by name on the "Browse map details".
1282 * providing it only in verbose mode not to bloat too
1283 * much struct symbol.
1284 */
bb963e16 1285 if (verbose > 0) {
80d50cae
ACM
1286 /*
1287 * XXX: Need to provide a less kludgy way to ask for
1288 * more space per symbol, the u32 is for the index on
1289 * the ui browser.
1290 * See symbol__browser_index.
1291 */
1292 symbol_conf.priv_size += sizeof(u32);
1293 symbol_conf.sort_by_name = true;
1294 }
1295 }
655000e7 1296
0a7e6d1b 1297 if (symbol__init(&session->header.env) < 0)
993ac88d 1298 goto error;
53cb8bc2 1299
5b969bc7
JY
1300 if (perf_time__parse_str(report.ptime_range, report.time_str) != 0) {
1301 if (session->evlist->first_sample_time == 0 &&
1302 session->evlist->last_sample_time == 0) {
eb0b419e
JY
1303 pr_err("HINT: no first/last sample time found in perf data.\n"
1304 "Please use latest perf binary to execute 'perf record'\n"
1305 "(if '--buildid-all' is enabled, please set '--timestamp-boundary').\n");
5b969bc7
JY
1306 return -EINVAL;
1307 }
1308
1309 report.range_num = perf_time__percent_parse_str(
1310 report.ptime_range, PTIME_RANGE_MAX,
1311 report.time_str,
1312 session->evlist->first_sample_time,
1313 session->evlist->last_sample_time);
1314
1315 if (report.range_num < 0) {
1316 pr_err("Invalid time string\n");
1317 return -EINVAL;
1318 }
1319 } else {
1320 report.range_num = 1;
46690a80
DA
1321 }
1322
08e71542 1323 sort__setup_elide(stdout);
25903407 1324
993ac88d 1325 ret = __cmd_report(&report);
ad0de097
FT
1326 if (ret == K_SWITCH_INPUT_DATA) {
1327 perf_session__delete(session);
1328 goto repeat;
1329 } else
1330 ret = 0;
1331
993ac88d
SE
1332error:
1333 perf_session__delete(session);
1334 return ret;
53cb8bc2 1335}