]> Git Repo - linux.git/blame - tools/perf/builtin-report.c
perf util: Allocate time slices buffer according to number of comma
[linux.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
7425664b
JY
407 if (rep->time_str)
408 ret += fprintf(fp, " (time slices: %s)", rep->time_str);
409
9e207ddf
KL
410 if (symbol_conf.show_ref_callgraph &&
411 strstr(evname, "call-graph=no")) {
412 ret += fprintf(fp, ", show reference callgraph");
413 }
414
f4f7e28d
SE
415 if (rep->mem_mode) {
416 ret += fprintf(fp, "\n# Total weight : %" PRIu64, nr_events);
228f14f2 417 ret += fprintf(fp, "\n# Sort order : %s", sort_order ? : default_mem_sort_order);
f4f7e28d
SE
418 } else
419 ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events);
21394d94 420
84734b06
KL
421 if (socked_id > -1)
422 ret += fprintf(fp, "\n# Processor Socket: %d", socked_id);
21394d94 423
c82ee828
ACM
424 return ret + fprintf(fp, "\n#\n");
425}
426
7f0030b2 427static int perf_evlist__tty_browse_hists(struct perf_evlist *evlist,
28b21393 428 struct report *rep,
7f0030b2 429 const char *help)
d67f088e 430{
e248de33 431 struct perf_evsel *pos;
d67f088e 432
27fafab5
NK
433 if (!quiet) {
434 fprintf(stdout, "#\n# Total Lost Samples: %" PRIu64 "\n#\n",
435 evlist->stats.total_lost_samples);
436 }
437
e5cadb93 438 evlist__for_each_entry(evlist, pos) {
4ea062ed 439 struct hists *hists = evsel__hists(pos);
7289f83c 440 const char *evname = perf_evsel__name(pos);
d67f088e 441
fc24d7c2
NK
442 if (symbol_conf.event_group &&
443 !perf_evsel__is_group_leader(pos))
444 continue;
445
28b21393 446 hists__fprintf_nr_sample_events(hists, rep, evname, stdout);
27fafab5 447 hists__fprintf(hists, !quiet, 0, 0, rep->min_percent, stdout,
b49a821e
JY
448 symbol_conf.use_callchain ||
449 symbol_conf.show_branchflag_count);
d67f088e 450 fprintf(stdout, "\n\n");
d67f088e
ACM
451 }
452
8b53dbef 453 if (!quiet)
d67f088e
ACM
454 fprintf(stdout, "#\n# (%s)\n#\n", help);
455
021162cf
NK
456 if (rep->show_threads) {
457 bool style = !strcmp(rep->pretty_printing_style, "raw");
458 perf_read_values_display(stdout, &rep->show_threads_values,
459 style);
460 perf_read_values_destroy(&rep->show_threads_values);
d67f088e
ACM
461 }
462
2d78b189
JY
463 if (sort__mode == SORT_MODE__BRANCH)
464 branch_type_stat_display(stdout, &rep->brtype_stat);
465
d67f088e
ACM
466 return 0;
467}
468
fad2918e
ACM
469static void report__warn_kptr_restrict(const struct report *rep)
470{
a5e813c6 471 struct map *kernel_map = machine__kernel_map(&rep->session->machines.host);
f6fcc143 472 struct kmap *kernel_kmap = kernel_map ? map__kmap(kernel_map) : NULL;
fad2918e 473
9c39ed90
ACM
474 if (perf_evlist__exclude_kernel(rep->session->evlist))
475 return;
476
fad2918e
ACM
477 if (kernel_map == NULL ||
478 (kernel_map->dso->hit &&
479 (kernel_kmap->ref_reloc_sym == NULL ||
480 kernel_kmap->ref_reloc_sym->addr == 0))) {
481 const char *desc =
482 "As no suitable kallsyms nor vmlinux was found, kernel samples\n"
483 "can't be resolved.";
484
485 if (kernel_map) {
486 const struct dso *kdso = kernel_map->dso;
487 if (!RB_EMPTY_ROOT(&kdso->symbols[MAP__FUNCTION])) {
488 desc = "If some relocation was applied (e.g. "
489 "kexec) symbols may be misresolved.";
490 }
491 }
492
493 ui__warning(
494"Kernel address maps (/proc/{kallsyms,modules}) were restricted.\n\n"
495"Check /proc/sys/kernel/kptr_restrict before running 'perf record'.\n\n%s\n\n"
496"Samples in kernel modules can't be resolved as well.\n\n",
497 desc);
498 }
499}
500
8362951b
ACM
501static int report__gtk_browse_hists(struct report *rep, const char *help)
502{
503 int (*hist_browser)(struct perf_evlist *evlist, const char *help,
504 struct hist_browser_timer *timer, float min_pcnt);
505
506 hist_browser = dlsym(perf_gtk_handle, "perf_evlist__gtk_browse_hists");
507
508 if (hist_browser == NULL) {
509 ui__error("GTK browser not found!\n");
510 return -1;
511 }
512
513 return hist_browser(rep->session->evlist, help, NULL, rep->min_percent);
514}
515
516static int report__browse_hists(struct report *rep)
517{
518 int ret;
519 struct perf_session *session = rep->session;
520 struct perf_evlist *evlist = session->evlist;
34b7b0f9
NK
521 const char *help = perf_tip(system_path(TIPDIR));
522
523 if (help == NULL) {
524 /* fallback for people who don't install perf ;-) */
525 help = perf_tip(DOCDIR);
526 if (help == NULL)
527 help = "Cannot load tips.txt file, please install perf!";
528 }
8362951b
ACM
529
530 switch (use_browser) {
531 case 1:
532 ret = perf_evlist__tui_browse_hists(evlist, help, NULL,
533 rep->min_percent,
534 &session->header.env);
535 /*
536 * Usually "ret" is the last pressed key, and we only
537 * care if the key notifies us to switch data file.
538 */
539 if (ret != K_SWITCH_INPUT_DATA)
540 ret = 0;
541 break;
542 case 2:
543 ret = report__gtk_browse_hists(rep, help);
544 break;
545 default:
546 ret = perf_evlist__tty_browse_hists(evlist, rep, help);
547 break;
548 }
549
550 return ret;
551}
552
5b2ea6f2 553static int report__collapse_hists(struct report *rep)
f6d8b057
ACM
554{
555 struct ui_progress prog;
556 struct perf_evsel *pos;
5b2ea6f2 557 int ret = 0;
f6d8b057 558
58c311da 559 ui_progress__init(&prog, rep->nr_entries, "Merging related events...");
f6d8b057 560
e5cadb93 561 evlist__for_each_entry(rep->session->evlist, pos) {
4ea062ed 562 struct hists *hists = evsel__hists(pos);
f6d8b057
ACM
563
564 if (pos->idx == 0)
565 hists->symbol_filter_str = rep->symbol_filter_str;
566
21394d94
KL
567 hists->socket_filter = rep->socket_filter;
568
5b2ea6f2
NK
569 ret = hists__collapse_resort(hists, &prog);
570 if (ret < 0)
571 break;
f6d8b057
ACM
572
573 /* Non-group events are considered as leader */
574 if (symbol_conf.event_group &&
575 !perf_evsel__is_group_leader(pos)) {
4ea062ed 576 struct hists *leader_hists = evsel__hists(pos->leader);
f6d8b057
ACM
577
578 hists__match(leader_hists, hists);
579 hists__link(leader_hists, hists);
580 }
581 }
582
583 ui_progress__finish();
5b2ea6f2 584 return ret;
f6d8b057
ACM
585}
586
740b97f9
NK
587static void report__output_resort(struct report *rep)
588{
589 struct ui_progress prog;
590 struct perf_evsel *pos;
591
592 ui_progress__init(&prog, rep->nr_entries, "Sorting events for output...");
593
e5cadb93 594 evlist__for_each_entry(rep->session->evlist, pos)
452ce03b 595 perf_evsel__output_resort(pos, &prog);
740b97f9
NK
596
597 ui_progress__finish();
598}
599
a4a4d0a7
JO
600static void stats_setup(struct report *rep)
601{
602 memset(&rep->tool, 0, sizeof(rep->tool));
603 rep->tool.no_warn = true;
604}
605
606static int stats_print(struct report *rep)
607{
608 struct perf_session *session = rep->session;
609
610 perf_session__fprintf_nr_events(session, stdout);
611 return 0;
612}
613
930f8b34
JO
614static void tasks_setup(struct report *rep)
615{
616 memset(&rep->tool, 0, sizeof(rep->tool));
6439d7d1
ACM
617 if (rep->mmaps_mode) {
618 rep->tool.mmap = perf_event__process_mmap;
619 rep->tool.mmap2 = perf_event__process_mmap2;
620 }
930f8b34
JO
621 rep->tool.comm = perf_event__process_comm;
622 rep->tool.exit = perf_event__process_exit;
623 rep->tool.fork = perf_event__process_fork;
624 rep->tool.no_warn = true;
625}
626
627struct task {
628 struct thread *thread;
629 struct list_head list;
630 struct list_head children;
631};
632
633static struct task *tasks_list(struct task *task, struct machine *machine)
634{
635 struct thread *parent_thread, *thread = task->thread;
636 struct task *parent_task;
637
638 /* Already listed. */
639 if (!list_empty(&task->list))
640 return NULL;
641
642 /* Last one in the chain. */
643 if (thread->ppid == -1)
644 return task;
645
646 parent_thread = machine__find_thread(machine, -1, thread->ppid);
647 if (!parent_thread)
648 return ERR_PTR(-ENOENT);
649
650 parent_task = thread__priv(parent_thread);
651 list_add_tail(&task->list, &parent_task->children);
652 return tasks_list(parent_task, machine);
653}
654
6439d7d1
ACM
655static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp)
656{
657 size_t printed = 0;
658 struct rb_node *nd;
659
660 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
661 struct map *map = rb_entry(nd, struct map, rb_node);
662
663 printed += fprintf(fp, "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %" PRIu64 " %s\n",
664 indent, "", map->start, map->end,
665 map->prot & PROT_READ ? 'r' : '-',
666 map->prot & PROT_WRITE ? 'w' : '-',
667 map->prot & PROT_EXEC ? 'x' : '-',
668 map->flags & MAP_SHARED ? 's' : 'p',
669 map->pgoff,
670 map->ino, map->dso->name);
671 }
672
673 return printed;
674}
675
676static int map_groups__fprintf_task(struct map_groups *mg, int indent, FILE *fp)
677{
678 int printed = 0, i;
679 for (i = 0; i < MAP__NR_TYPES; ++i)
680 printed += maps__fprintf_task(&mg->maps[i], indent, fp);
681 return printed;
682}
683
930f8b34
JO
684static void task__print_level(struct task *task, FILE *fp, int level)
685{
686 struct thread *thread = task->thread;
687 struct task *child;
6439d7d1
ACM
688 int comm_indent = fprintf(fp, " %8d %8d %8d |%*s",
689 thread->pid_, thread->tid, thread->ppid,
690 level, "");
691
692 fprintf(fp, "%s\n", thread__comm_str(thread));
930f8b34 693
6439d7d1 694 map_groups__fprintf_task(thread->mg, comm_indent, fp);
930f8b34
JO
695
696 if (!list_empty(&task->children)) {
697 list_for_each_entry(child, &task->children, list)
698 task__print_level(child, fp, level + 1);
699 }
700}
701
702static int tasks_print(struct report *rep, FILE *fp)
703{
704 struct perf_session *session = rep->session;
705 struct machine *machine = &session->machines.host;
706 struct task *tasks, *task;
707 unsigned int nr = 0, itask = 0, i;
708 struct rb_node *nd;
709 LIST_HEAD(list);
710
711 /*
712 * No locking needed while accessing machine->threads,
713 * because --tasks is single threaded command.
714 */
715
716 /* Count all the threads. */
717 for (i = 0; i < THREADS__TABLE_SIZE; i++)
718 nr += machine->threads[i].nr;
719
720 tasks = malloc(sizeof(*tasks) * nr);
721 if (!tasks)
722 return -ENOMEM;
723
724 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
725 struct threads *threads = &machine->threads[i];
726
727 for (nd = rb_first(&threads->entries); nd; nd = rb_next(nd)) {
728 task = tasks + itask++;
729
730 task->thread = rb_entry(nd, struct thread, rb_node);
731 INIT_LIST_HEAD(&task->children);
732 INIT_LIST_HEAD(&task->list);
733 thread__set_priv(task->thread, task);
734 }
735 }
736
737 /*
738 * Iterate every task down to the unprocessed parent
739 * and link all in task children list. Task with no
740 * parent is added into 'list'.
741 */
742 for (itask = 0; itask < nr; itask++) {
743 task = tasks + itask;
744
745 if (!list_empty(&task->list))
746 continue;
747
748 task = tasks_list(task, machine);
749 if (IS_ERR(task)) {
750 pr_err("Error: failed to process tasks\n");
751 free(tasks);
752 return PTR_ERR(task);
753 }
754
755 if (task)
756 list_add_tail(&task->list, &list);
757 }
758
759 fprintf(fp, "# %8s %8s %8s %s\n", "pid", "tid", "ppid", "comm");
760
761 list_for_each_entry(task, &list, list)
762 task__print_level(task, fp, 0);
763
764 free(tasks);
765 return 0;
766}
767
28b21393 768static int __cmd_report(struct report *rep)
016e92fb 769{
f6d8b057 770 int ret;
993ac88d 771 struct perf_session *session = rep->session;
e248de33 772 struct perf_evsel *pos;
8ceb41d7 773 struct perf_data *data = session->data;
8fa66bdc 774
46656ac7
TZ
775 signal(SIGINT, sig_handler);
776
fa372aae
ACM
777 if (rep->cpu_list) {
778 ret = perf_session__cpu_bitmap(session, rep->cpu_list,
779 rep->cpu_bitmap);
25b1606b
NK
780 if (ret) {
781 ui__error("failed to set cpu bitmap\n");
d4ae0a6f 782 return ret;
25b1606b 783 }
644e0840 784 session->itrace_synth_opts->cpu_bitmap = rep->cpu_bitmap;
5d67be97
AB
785 }
786
89973506
ACM
787 if (rep->show_threads) {
788 ret = perf_read_values_init(&rep->show_threads_values);
789 if (ret)
790 return ret;
791 }
f5970550 792
28b21393 793 ret = report__setup_sample_type(rep);
25b1606b
NK
794 if (ret) {
795 /* report__setup_sample_type() already showed error message */
d4ae0a6f 796 return ret;
25b1606b 797 }
d549c769 798
a4a4d0a7
JO
799 if (rep->stats_mode)
800 stats_setup(rep);
801
930f8b34
JO
802 if (rep->tasks_mode)
803 tasks_setup(rep);
804
b7b61cbe 805 ret = perf_session__process_events(session);
25b1606b
NK
806 if (ret) {
807 ui__error("failed to process sample\n");
d4ae0a6f 808 return ret;
25b1606b 809 }
97b07b69 810
a4a4d0a7
JO
811 if (rep->stats_mode)
812 return stats_print(rep);
813
930f8b34
JO
814 if (rep->tasks_mode)
815 return tasks_print(rep, stdout);
816
fad2918e 817 report__warn_kptr_restrict(rep);
ec80fde7 818
e5cadb93 819 evlist__for_each_entry(session->evlist, pos)
590cd344
NK
820 rep->nr_entries += evsel__hists(pos)->nr_entries;
821
150e465a
NK
822 if (use_browser == 0) {
823 if (verbose > 3)
824 perf_session__fprintf(session, stdout);
9ac99545 825
150e465a
NK
826 if (verbose > 2)
827 perf_session__fprintf_dsos(session, stdout);
16f762a2 828
150e465a
NK
829 if (dump_trace) {
830 perf_session__fprintf_nr_events(session, stdout);
2a1731fb 831 perf_evlist__fprintf_nr_events(session->evlist, stdout);
150e465a
NK
832 return 0;
833 }
71ad0f5e
JO
834 }
835
5b2ea6f2
NK
836 ret = report__collapse_hists(rep);
837 if (ret) {
838 ui__error("failed to process hist entry\n");
839 return ret;
840 }
e248de33 841
33e940a2
ACM
842 if (session_done())
843 return 0;
844
740b97f9
NK
845 /*
846 * recalculate number of entries after collapsing since it
847 * might be changed during the collapse phase.
848 */
849 rep->nr_entries = 0;
e5cadb93 850 evlist__for_each_entry(session->evlist, pos)
740b97f9
NK
851 rep->nr_entries += evsel__hists(pos)->nr_entries;
852
58c311da 853 if (rep->nr_entries == 0) {
eae8ad80 854 ui__error("The %s file has no samples!\n", data->file.path);
d4ae0a6f 855 return 0;
cbbc79a5
EM
856 }
857
740b97f9 858 report__output_resort(rep);
6e1f601a 859
8362951b 860 return report__browse_hists(rep);
8fa66bdc
ACM
861}
862
4eb3e478 863static int
cff6bb46 864report_parse_callchain_opt(const struct option *opt, const char *arg, int unset)
4eb3e478 865{
1cc83815 866 struct callchain_param *callchain = opt->value;
c20ab37e 867
1cc83815 868 callchain->enabled = !unset;
b9a63b9b
ACM
869 /*
870 * --no-call-graph
871 */
872 if (unset) {
1cc83815
ACM
873 symbol_conf.use_callchain = false;
874 callchain->mode = CHAIN_NONE;
b9a63b9b
ACM
875 return 0;
876 }
877
cff6bb46 878 return parse_callchain_report_opt(arg);
4eb3e478
FW
879}
880
b21484f1
GP
881int
882report_parse_ignore_callees_opt(const struct option *opt __maybe_unused,
883 const char *arg, int unset __maybe_unused)
884{
885 if (arg) {
886 int err = regcomp(&ignore_callees_regex, arg, REG_EXTENDED);
887 if (err) {
888 char buf[BUFSIZ];
889 regerror(err, &ignore_callees_regex, buf, sizeof(buf));
890 pr_err("Invalid --ignore-callees regex: %s\n%s", arg, buf);
891 return -1;
892 }
893 have_ignore_callees = 1;
894 }
895
896 return 0;
897}
898
993ac88d 899static int
7e6a7998 900parse_branch_mode(const struct option *opt,
1d037ca1 901 const char *str __maybe_unused, int unset)
993ac88d 902{
55369fc1
NK
903 int *branch_mode = opt->value;
904
905 *branch_mode = !unset;
993ac88d
SE
906 return 0;
907}
908
064f1981
NK
909static int
910parse_percent_limit(const struct option *opt, const char *str,
911 int unset __maybe_unused)
912{
28b21393 913 struct report *rep = opt->value;
2665b452 914 double pcnt = strtof(str, NULL);
064f1981 915
2665b452
NK
916 rep->min_percent = pcnt;
917 callchain_param.min_percent = pcnt;
064f1981
NK
918 return 0;
919}
920
f2af0086 921#define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
76a26549
NK
922
923const char report_callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
924 CALLCHAIN_REPORT_HELP
925 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
21cf6284 926
b0ad8ea6 927int cmd_report(int argc, const char **argv)
d20deb64 928{
993ac88d 929 struct perf_session *session;
520a2ebc 930 struct itrace_synth_opts itrace_synth_opts = { .set = 0, };
efad1415 931 struct stat st;
993ac88d 932 bool has_br_stack = false;
55369fc1 933 int branch_mode = -1;
fa94c36c 934 bool branch_call_mode = false;
76a26549 935 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
d20deb64 936 const char * const report_usage[] = {
fb2baceb 937 "perf report [<options>]",
d20deb64
ACM
938 NULL
939 };
28b21393 940 struct report report = {
45694aa7 941 .tool = {
d20deb64
ACM
942 .sample = process_sample_event,
943 .mmap = perf_event__process_mmap,
5c5e854b 944 .mmap2 = perf_event__process_mmap2,
d20deb64 945 .comm = perf_event__process_comm,
f3b3614a 946 .namespaces = perf_event__process_namespaces,
f62d3f0f
ACM
947 .exit = perf_event__process_exit,
948 .fork = perf_event__process_fork,
d20deb64
ACM
949 .lost = perf_event__process_lost,
950 .read = process_read_event,
951 .attr = perf_event__process_attr,
d20deb64
ACM
952 .tracing_data = perf_event__process_tracing_data,
953 .build_id = perf_event__process_build_id,
520a2ebc
AH
954 .id_index = perf_event__process_id_index,
955 .auxtrace_info = perf_event__process_auxtrace_info,
956 .auxtrace = perf_event__process_auxtrace,
e9def1b2 957 .feature = perf_event__process_feature,
0a8cb85c 958 .ordered_events = true,
d20deb64
ACM
959 .ordering_requires_timestamps = true,
960 },
fe176085 961 .max_stack = PERF_MAX_STACK_DEPTH,
d20deb64 962 .pretty_printing_style = "normal",
21394d94 963 .socket_filter = -1,
d20deb64
ACM
964 };
965 const struct option options[] = {
70cb4e96 966 OPT_STRING('i', "input", &input_name, "file",
53cb8bc2 967 "input file name"),
c0555642 968 OPT_INCR('v', "verbose", &verbose,
815e777f 969 "be more verbose (show symbol address, etc)"),
27fafab5 970 OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any message"),
97b07b69
IM
971 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
972 "dump raw trace in ASCII"),
a4a4d0a7 973 OPT_BOOLEAN(0, "stats", &report.stats_mode, "Display event stats"),
930f8b34 974 OPT_BOOLEAN(0, "tasks", &report.tasks_mode, "Display recorded tasks"),
6439d7d1 975 OPT_BOOLEAN(0, "mmaps", &report.mmaps_mode, "Display recorded tasks memory maps"),
b32d133a
ACM
976 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
977 "file", "vmlinux pathname"),
b226a5a7
DA
978 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
979 "file", "kallsyms pathname"),
2059fc7a 980 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
b32d133a 981 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 982 "load module symbols - WARNING: use only with -k and LIVE kernel"),
d599db3f 983 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
e3d7e183 984 "Show a column with the number of samples"),
fa372aae 985 OPT_BOOLEAN('T', "threads", &report.show_threads,
8d513270 986 "Show per-thread event counters"),
fa372aae 987 OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
9f866697 988 "pretty printing style key: normal raw"),
fa372aae 989 OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
c31a9457 990 OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK2 interface"),
fa372aae
ACM
991 OPT_BOOLEAN(0, "stdio", &report.use_stdio,
992 "Use the stdio interface"),
5cfe2c82
JO
993 OPT_BOOLEAN(0, "header", &report.header, "Show data header."),
994 OPT_BOOLEAN(0, "header-only", &report.header_only,
995 "Show only data header."),
63299f05 996 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
a2ce067e
NK
997 "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
998 " Please refer the man page for the complete list."),
a7d945bc
NK
999 OPT_STRING('F', "fields", &field_order, "key[,keys...]",
1000 "output field(s): overhead, period, sample plus all of sort keys"),
b272a59d 1001 OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization,
a1645ce1 1002 "Show sample percentage for different cpu modes"),
b272a59d
NK
1003 OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
1004 "Show sample percentage for different cpu modes", PARSE_OPT_HIDDEN),
b25bcf2f
IM
1005 OPT_STRING('p', "parent", &parent_pattern, "regex",
1006 "regex filter to identify parent, see: '--sort parent'"),
d599db3f 1007 OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
b8e6d829 1008 "Only display entries with parent-match"),
1cc83815 1009 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
f2af0086 1010 "print_type,threshold[,print_limit],order,sort_key[,branch],value",
21cf6284
NK
1011 report_callchain_help, &report_parse_callchain_opt,
1012 callchain_default_opt),
793aaaab
NK
1013 OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
1014 "Accumulate callchains of children and show total overhead as well"),
91e95617
WL
1015 OPT_INTEGER(0, "max-stack", &report.max_stack,
1016 "Set the maximum stack depth when parsing the callchain, "
1017 "anything beyond the specified depth will be ignored. "
4cb93446 1018 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
fa372aae
ACM
1019 OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
1020 "alias for inverted call graph"),
b21484f1
GP
1021 OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
1022 "ignore callees of these functions in call graphs",
1023 report_parse_ignore_callees_opt),
655000e7 1024 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
25903407 1025 "only consider symbols in these dsos"),
c8e66720 1026 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
cc8b88b1 1027 "only consider symbols in these comms"),
e03eaa40
DA
1028 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1029 "only consider symbols in these pids"),
1030 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1031 "only consider symbols in these tids"),
655000e7 1032 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
7bec7a91 1033 "only consider these symbols"),
b14ffaca
NK
1034 OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
1035 "only show symbols that (partially) match with this filter"),
655000e7 1036 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
52d422de
ACM
1037 "width[,width...]",
1038 "don't try to adjust column width, use these fixed values"),
0c8c2077 1039 OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
52d422de
ACM
1040 "separator for columns, no spaces will be added between "
1041 "columns '.' is reserved."),
b49a8fe5 1042 OPT_BOOLEAN('U', "hide-unresolved", &symbol_conf.hide_unresolved,
71289be7 1043 "Only display entries resolved to a symbol"),
a7066709
HK
1044 OPT_CALLBACK(0, "symfs", NULL, "directory",
1045 "Look for files with symbols relative to this directory",
1046 symbol__config_symfs),
c8e66720 1047 OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
fa372aae
ACM
1048 "list of cpus to profile"),
1049 OPT_BOOLEAN('I', "show-info", &report.show_full_info,
fbe96f29 1050 "Display extended information about perf.data file"),
64c6f0c7
ACM
1051 OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
1052 "Interleave source code with assembly code (default)"),
1053 OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
1054 "Display raw encoding of assembly instructions (default)"),
f69b64f7
AK
1055 OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
1056 "Specify disassembler style (e.g. -M intel for intel syntax)"),
3f2728bd
ACM
1057 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
1058 "Show a column with the sum of periods"),
01d14f16
NK
1059 OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
1060 "Show event group information together"),
55369fc1 1061 OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "",
fa94c36c
AK
1062 "use branch records for per branch histogram filling",
1063 parse_branch_mode),
1064 OPT_BOOLEAN(0, "branch-history", &branch_call_mode,
1065 "add last branch records to call history"),
7a4ec938
MB
1066 OPT_STRING(0, "objdump", &objdump_path, "path",
1067 "objdump binary to use for disassembly and annotations"),
328ccdac
NK
1068 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1069 "Disable symbol demangling"),
763122ad
AK
1070 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1071 "Enable kernel symbol demangling"),
f4f7e28d 1072 OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
064f1981
NK
1073 OPT_CALLBACK(0, "percent-limit", &report, "percent",
1074 "Don't show entries under that percent", parse_percent_limit),
f2148330 1075 OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
33db4568 1076 "how to display percentage of filtered entries", parse_filter_percentage),
520a2ebc
AH
1077 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1078 "Instruction Tracing options",
1079 itrace_parse_synth_opts),
a9710ba0
AK
1080 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1081 "Show full source file name path for source lines"),
9e207ddf
KL
1082 OPT_BOOLEAN(0, "show-ref-call-graph", &symbol_conf.show_ref_callgraph,
1083 "Show callgraph from reference event"),
21394d94
KL
1084 OPT_INTEGER(0, "socket-filter", &report.socket_filter,
1085 "only show processor socket that match with this filter"),
053a3989
NK
1086 OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
1087 "Show raw trace event output (do not use print fmt or plugins)"),
4251446d
NK
1088 OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
1089 "Show entries in a hierarchy"),
175b968b
ACM
1090 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
1091 "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
1092 stdio__config_color, "always"),
46690a80
DA
1093 OPT_STRING(0, "time", &report.time_str, "str",
1094 "Time span of interest (start,stop)"),
f3a60646
JY
1095 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
1096 "Show inline function"),
53cb8bc2 1097 OPT_END()
d20deb64 1098 };
8ceb41d7 1099 struct perf_data data = {
f5fc1412
JO
1100 .mode = PERF_DATA_MODE_READ,
1101 };
a635fc51
ACM
1102 int ret = hists__init();
1103
1104 if (ret < 0)
1105 return ret;
53cb8bc2 1106
ecc4c561
ACM
1107 ret = perf_config(report__config, &report);
1108 if (ret)
1109 return ret;
00c7e1f1 1110
655000e7 1111 argc = parse_options(argc, argv, options, report_usage, 0);
b3f38fc2
NK
1112 if (argc) {
1113 /*
1114 * Special case: if there's an argument left then assume that
1115 * it's a symbol filter:
1116 */
1117 if (argc > 1)
1118 usage_with_options(report_usage, options);
1119
1120 report.symbol_filter_str = argv[0];
1121 }
655000e7 1122
6439d7d1
ACM
1123 if (report.mmaps_mode)
1124 report.tasks_mode = true;
1125
27fafab5
NK
1126 if (quiet)
1127 perf_quiet_option();
1128
36c8bb56
LZ
1129 if (symbol_conf.vmlinux_name &&
1130 access(symbol_conf.vmlinux_name, R_OK)) {
1131 pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
1132 return -EINVAL;
1133 }
1134 if (symbol_conf.kallsyms_name &&
1135 access(symbol_conf.kallsyms_name, R_OK)) {
1136 pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
1137 return -EINVAL;
1138 }
1139
fa372aae 1140 if (report.inverted_callchain)
d797fdc5 1141 callchain_param.order = ORDER_CALLER;
792aeafa
NK
1142 if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
1143 callchain_param.order = ORDER_CALLER;
d797fdc5 1144
188bb5e2
AH
1145 if (itrace_synth_opts.callchain &&
1146 (int)itrace_synth_opts.callchain_sz > report.max_stack)
1147 report.max_stack = itrace_synth_opts.callchain_sz;
1148
70cb4e96 1149 if (!input_name || !strlen(input_name)) {
efad1415 1150 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
70cb4e96 1151 input_name = "-";
efad1415 1152 else
70cb4e96 1153 input_name = "perf.data";
efad1415 1154 }
ad0de097 1155
eae8ad80
JO
1156 data.file.path = input_name;
1157 data.force = symbol_conf.force;
f5fc1412 1158
ad0de097 1159repeat:
8ceb41d7 1160 session = perf_session__new(&data, false, &report.tool);
993ac88d 1161 if (session == NULL)
52e02834 1162 return -1;
993ac88d 1163
94786b67
JO
1164 if (report.queue_size) {
1165 ordered_events__set_alloc_size(&session->ordered_events,
1166 report.queue_size);
1167 }
1168
520a2ebc
AH
1169 session->itrace_synth_opts = &itrace_synth_opts;
1170
993ac88d
SE
1171 report.session = session;
1172
1173 has_br_stack = perf_header__has_feat(&session->header,
1174 HEADER_BRANCH_STACK);
efad1415 1175
fb9fab66
AH
1176 if (itrace_synth_opts.last_branch)
1177 has_br_stack = true;
1178
f9a7be7c
JY
1179 if (has_br_stack && branch_call_mode)
1180 symbol_conf.show_branchflag_count = true;
1181
2d78b189
JY
1182 memset(&report.brtype_stat, 0, sizeof(struct branch_type_stat));
1183
fa94c36c
AK
1184 /*
1185 * Branch mode is a tristate:
1186 * -1 means default, so decide based on the file having branch data.
1187 * 0/1 means the user chose a mode.
1188 */
1189 if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) &&
fefd2d96 1190 !branch_call_mode) {
55369fc1 1191 sort__mode = SORT_MODE__BRANCH;
793aaaab
NK
1192 symbol_conf.cumulate_callchain = false;
1193 }
fa94c36c 1194 if (branch_call_mode) {
09a6a1b0 1195 callchain_param.key = CCKEY_ADDRESS;
fa94c36c
AK
1196 callchain_param.branch_callstack = 1;
1197 symbol_conf.use_callchain = true;
1198 callchain_register_param(&callchain_param);
1199 if (sort_order == NULL)
1200 sort_order = "srcline,symbol,dso";
1201 }
993ac88d 1202
f4f7e28d 1203 if (report.mem_mode) {
55369fc1 1204 if (sort__mode == SORT_MODE__BRANCH) {
a4210141 1205 pr_err("branch and mem mode incompatible\n");
f4f7e28d
SE
1206 goto error;
1207 }
afab87b9 1208 sort__mode = SORT_MODE__MEMORY;
793aaaab 1209 symbol_conf.cumulate_callchain = false;
f4f7e28d 1210 }
a68c2c58 1211
4251446d
NK
1212 if (symbol_conf.report_hierarchy) {
1213 /* disable incompatible options */
4251446d
NK
1214 symbol_conf.cumulate_callchain = false;
1215
1216 if (field_order) {
1217 pr_err("Error: --hierarchy and --fields options cannot be used together\n");
1218 parse_options_usage(report_usage, options, "F", 1);
1219 parse_options_usage(NULL, options, "hierarchy", 0);
1220 goto error;
1221 }
1222
52225036 1223 perf_hpp_list.need_collapse = true;
4251446d
NK
1224 }
1225
712d36db
SS
1226 if (report.use_stdio)
1227 use_browser = 0;
1228 else if (report.use_tui)
1229 use_browser = 1;
1230 else if (report.use_gtk)
1231 use_browser = 2;
1232
b138f42e
NK
1233 /* Force tty output for header output and per-thread stat. */
1234 if (report.header || report.header_only || report.show_threads)
5cfe2c82 1235 use_browser = 0;
114f709e
DCC
1236 if (report.header || report.header_only)
1237 report.tool.show_feat_hdr = SHOW_FEAT_HEADER;
1238 if (report.show_full_info)
1239 report.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
930f8b34 1240 if (report.stats_mode || report.tasks_mode)
a4a4d0a7 1241 use_browser = 0;
930f8b34 1242 if (report.stats_mode && report.tasks_mode) {
6439d7d1 1243 pr_err("Error: --tasks and --mmaps can't be used together with --stats\n");
930f8b34
JO
1244 goto error;
1245 }
5cfe2c82 1246
4bceffbc
NK
1247 if (strcmp(input_name, "-") != 0)
1248 setup_browser(true);
22af969e 1249 else
4bceffbc 1250 use_browser = 0;
4bceffbc 1251
9887804d
JO
1252 if (setup_sorting(session->evlist) < 0) {
1253 if (sort_order)
1254 parse_options_usage(report_usage, options, "s", 1);
1255 if (field_order)
1256 parse_options_usage(sort_order ? NULL : report_usage,
1257 options, "F", 1);
1258 goto error;
1259 }
1260
27fafab5 1261 if ((report.header || report.header_only) && !quiet) {
5cfe2c82
JO
1262 perf_session__fprintf_info(session, stdout,
1263 report.show_full_info);
07a716ff
TS
1264 if (report.header_only) {
1265 ret = 0;
1266 goto error;
1267 }
930f8b34
JO
1268 } else if (use_browser == 0 && !quiet &&
1269 !report.stats_mode && !report.tasks_mode) {
5cfe2c82
JO
1270 fputs("# To display the perf.data header info, please use --header/--header-only options.\n#\n",
1271 stdout);
1272 }
1273
ef7b93a1 1274 /*
6692c262 1275 * Only in the TUI browser we are doing integrated annotation,
ef7b93a1
ACM
1276 * so don't allocate extra space that won't be used in the stdio
1277 * implementation.
1278 */
b9ce0c99 1279 if (ui__has_annotation()) {
b01141f4
ACM
1280 ret = symbol__annotation_init();
1281 if (ret < 0)
1282 goto error;
80d50cae
ACM
1283 /*
1284 * For searching by name on the "Browse map details".
1285 * providing it only in verbose mode not to bloat too
1286 * much struct symbol.
1287 */
bb963e16 1288 if (verbose > 0) {
80d50cae
ACM
1289 /*
1290 * XXX: Need to provide a less kludgy way to ask for
1291 * more space per symbol, the u32 is for the index on
1292 * the ui browser.
1293 * See symbol__browser_index.
1294 */
1295 symbol_conf.priv_size += sizeof(u32);
1296 symbol_conf.sort_by_name = true;
1297 }
1298 }
655000e7 1299
0a7e6d1b 1300 if (symbol__init(&session->header.env) < 0)
993ac88d 1301 goto error;
53cb8bc2 1302
5b969bc7
JY
1303 if (perf_time__parse_str(report.ptime_range, report.time_str) != 0) {
1304 if (session->evlist->first_sample_time == 0 &&
1305 session->evlist->last_sample_time == 0) {
eb0b419e
JY
1306 pr_err("HINT: no first/last sample time found in perf data.\n"
1307 "Please use latest perf binary to execute 'perf record'\n"
1308 "(if '--buildid-all' is enabled, please set '--timestamp-boundary').\n");
5b969bc7
JY
1309 return -EINVAL;
1310 }
1311
1312 report.range_num = perf_time__percent_parse_str(
1313 report.ptime_range, PTIME_RANGE_MAX,
1314 report.time_str,
1315 session->evlist->first_sample_time,
1316 session->evlist->last_sample_time);
1317
1318 if (report.range_num < 0) {
1319 pr_err("Invalid time string\n");
1320 return -EINVAL;
1321 }
1322 } else {
1323 report.range_num = 1;
46690a80
DA
1324 }
1325
08e71542 1326 sort__setup_elide(stdout);
25903407 1327
993ac88d 1328 ret = __cmd_report(&report);
ad0de097
FT
1329 if (ret == K_SWITCH_INPUT_DATA) {
1330 perf_session__delete(session);
1331 goto repeat;
1332 } else
1333 ret = 0;
1334
993ac88d
SE
1335error:
1336 perf_session__delete(session);
1337 return ret;
53cb8bc2 1338}
This page took 0.680778 seconds and 4 git commands to generate.