blob: d3c0b04e2e22b22714d11eb73a27bb351c0a07a6 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * builtin-report.c
4 *
5 * Builtin report command: Analyze the perf.data input file,
6 * look up and read DSOs and symbol information and display
7 * a histogram of results, along various sorting keys.
8 */
9#include "builtin.h"
10
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000011#include "util/config.h"
12
13#include "util/annotate.h"
14#include "util/color.h"
David Brazdil0f672f62019-12-10 10:32:29 +000015#include "util/dso.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016#include <linux/list.h>
17#include <linux/rbtree.h>
18#include <linux/err.h>
David Brazdil0f672f62019-12-10 10:32:29 +000019#include <linux/zalloc.h>
20#include "util/map.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000021#include "util/symbol.h"
David Brazdil0f672f62019-12-10 10:32:29 +000022#include "util/map_symbol.h"
23#include "util/mem-events.h"
24#include "util/branch.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000025#include "util/callchain.h"
26#include "util/values.h"
27
28#include "perf.h"
29#include "util/debug.h"
30#include "util/evlist.h"
31#include "util/evsel.h"
David Brazdil0f672f62019-12-10 10:32:29 +000032#include "util/evswitch.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000033#include "util/header.h"
34#include "util/session.h"
David Brazdil0f672f62019-12-10 10:32:29 +000035#include "util/srcline.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036#include "util/tool.h"
37
38#include <subcmd/parse-options.h>
39#include <subcmd/exec-cmd.h>
40#include "util/parse-events.h"
41
42#include "util/thread.h"
43#include "util/sort.h"
44#include "util/hist.h"
45#include "util/data.h"
46#include "arch/common.h"
47#include "util/time-utils.h"
48#include "util/auxtrace.h"
49#include "util/units.h"
50#include "util/branch.h"
David Brazdil0f672f62019-12-10 10:32:29 +000051#include "util/util.h" // perf_tip()
52#include "ui/ui.h"
53#include "ui/progress.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000054
55#include <dlfcn.h>
56#include <errno.h>
57#include <inttypes.h>
58#include <regex.h>
David Brazdil0f672f62019-12-10 10:32:29 +000059#include <linux/ctype.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000060#include <signal.h>
61#include <linux/bitmap.h>
David Brazdil0f672f62019-12-10 10:32:29 +000062#include <linux/string.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063#include <linux/stringify.h>
David Brazdil0f672f62019-12-10 10:32:29 +000064#include <linux/time64.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065#include <sys/types.h>
66#include <sys/stat.h>
67#include <unistd.h>
68#include <linux/mman.h>
69
70struct report {
71 struct perf_tool tool;
72 struct perf_session *session;
David Brazdil0f672f62019-12-10 10:32:29 +000073 struct evswitch evswitch;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000074 bool use_tui, use_gtk, use_stdio;
75 bool show_full_info;
76 bool show_threads;
77 bool inverted_callchain;
78 bool mem_mode;
79 bool stats_mode;
80 bool tasks_mode;
81 bool mmaps_mode;
82 bool header;
83 bool header_only;
84 bool nonany_branch_mode;
85 bool group_set;
86 int max_stack;
87 struct perf_read_values show_threads_values;
88 struct annotation_options annotation_opts;
89 const char *pretty_printing_style;
90 const char *cpu_list;
91 const char *symbol_filter_str;
92 const char *time_str;
93 struct perf_time_interval *ptime_range;
94 int range_size;
95 int range_num;
96 float min_percent;
97 u64 nr_entries;
98 u64 queue_size;
99 int socket_filter;
100 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
101 struct branch_type_stat brtype_stat;
David Brazdil0f672f62019-12-10 10:32:29 +0000102 bool symbol_ipc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000103};
104
105static int report__config(const char *var, const char *value, void *cb)
106{
107 struct report *rep = cb;
108
109 if (!strcmp(var, "report.group")) {
110 symbol_conf.event_group = perf_config_bool(var, value);
111 return 0;
112 }
113 if (!strcmp(var, "report.percent-limit")) {
114 double pcnt = strtof(value, NULL);
115
116 rep->min_percent = pcnt;
117 callchain_param.min_percent = pcnt;
118 return 0;
119 }
120 if (!strcmp(var, "report.children")) {
121 symbol_conf.cumulate_callchain = perf_config_bool(var, value);
122 return 0;
123 }
124 if (!strcmp(var, "report.queue-size"))
125 return perf_config_u64(&rep->queue_size, var, value);
126
127 if (!strcmp(var, "report.sort_order")) {
128 default_sort_order = strdup(value);
129 return 0;
130 }
131
132 return 0;
133}
134
135static int hist_iter__report_callback(struct hist_entry_iter *iter,
136 struct addr_location *al, bool single,
137 void *arg)
138{
139 int err = 0;
140 struct report *rep = arg;
141 struct hist_entry *he = iter->he;
David Brazdil0f672f62019-12-10 10:32:29 +0000142 struct evsel *evsel = iter->evsel;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000143 struct perf_sample *sample = iter->sample;
144 struct mem_info *mi;
145 struct branch_info *bi;
146
David Brazdil0f672f62019-12-10 10:32:29 +0000147 if (!ui__has_annotation() && !rep->symbol_ipc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000148 return 0;
149
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000150 if (sort__mode == SORT_MODE__BRANCH) {
151 bi = he->branch_info;
152 err = addr_map_symbol__inc_samples(&bi->from, sample, evsel);
153 if (err)
154 goto out;
155
156 err = addr_map_symbol__inc_samples(&bi->to, sample, evsel);
157
158 } else if (rep->mem_mode) {
159 mi = he->mem_info;
160 err = addr_map_symbol__inc_samples(&mi->daddr, sample, evsel);
161 if (err)
162 goto out;
163
164 err = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
165
166 } else if (symbol_conf.cumulate_callchain) {
167 if (single)
168 err = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
169 } else {
170 err = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
171 }
172
173out:
174 return err;
175}
176
177static int hist_iter__branch_callback(struct hist_entry_iter *iter,
178 struct addr_location *al __maybe_unused,
179 bool single __maybe_unused,
180 void *arg)
181{
182 struct hist_entry *he = iter->he;
183 struct report *rep = arg;
Olivier Deprez0e641232021-09-23 10:07:05 +0200184 struct branch_info *bi = he->branch_info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000185 struct perf_sample *sample = iter->sample;
David Brazdil0f672f62019-12-10 10:32:29 +0000186 struct evsel *evsel = iter->evsel;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000187 int err;
188
Olivier Deprez0e641232021-09-23 10:07:05 +0200189 branch_type_count(&rep->brtype_stat, &bi->flags,
190 bi->from.addr, bi->to.addr);
191
David Brazdil0f672f62019-12-10 10:32:29 +0000192 if (!ui__has_annotation() && !rep->symbol_ipc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000193 return 0;
194
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000195 err = addr_map_symbol__inc_samples(&bi->from, sample, evsel);
196 if (err)
197 goto out;
198
199 err = addr_map_symbol__inc_samples(&bi->to, sample, evsel);
200
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000201out:
202 return err;
203}
204
205static void setup_forced_leader(struct report *report,
David Brazdil0f672f62019-12-10 10:32:29 +0000206 struct evlist *evlist)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000207{
208 if (report->group_set)
209 perf_evlist__force_leader(evlist);
210}
211
David Brazdil0f672f62019-12-10 10:32:29 +0000212static int process_feature_event(struct perf_session *session,
213 union perf_event *event)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000214{
David Brazdil0f672f62019-12-10 10:32:29 +0000215 struct report *rep = container_of(session->tool, struct report, tool);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000216
217 if (event->feat.feat_id < HEADER_LAST_FEATURE)
David Brazdil0f672f62019-12-10 10:32:29 +0000218 return perf_event__process_feature(session, event);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000219
220 if (event->feat.feat_id != HEADER_LAST_FEATURE) {
David Brazdil0f672f62019-12-10 10:32:29 +0000221 pr_err("failed: wrong feature ID: %" PRI_lu64 "\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000222 event->feat.feat_id);
223 return -1;
224 }
225
226 /*
227 * (feat_id = HEADER_LAST_FEATURE) is the end marker which
228 * means all features are received, now we can force the
229 * group if needed.
230 */
231 setup_forced_leader(rep, session->evlist);
232 return 0;
233}
234
235static int process_sample_event(struct perf_tool *tool,
236 union perf_event *event,
237 struct perf_sample *sample,
David Brazdil0f672f62019-12-10 10:32:29 +0000238 struct evsel *evsel,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000239 struct machine *machine)
240{
241 struct report *rep = container_of(tool, struct report, tool);
242 struct addr_location al;
243 struct hist_entry_iter iter = {
244 .evsel = evsel,
245 .sample = sample,
246 .hide_unresolved = symbol_conf.hide_unresolved,
247 .add_entry_cb = hist_iter__report_callback,
248 };
249 int ret = 0;
250
251 if (perf_time__ranges_skip_sample(rep->ptime_range, rep->range_num,
252 sample->time)) {
253 return 0;
254 }
255
David Brazdil0f672f62019-12-10 10:32:29 +0000256 if (evswitch__discard(&rep->evswitch, evsel))
257 return 0;
258
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000259 if (machine__resolve(machine, &al, sample) < 0) {
260 pr_debug("problem processing %d event, skipping it.\n",
261 event->header.type);
262 return -1;
263 }
264
265 if (symbol_conf.hide_unresolved && al.sym == NULL)
266 goto out_put;
267
268 if (rep->cpu_list && !test_bit(sample->cpu, rep->cpu_bitmap))
269 goto out_put;
270
271 if (sort__mode == SORT_MODE__BRANCH) {
272 /*
273 * A non-synthesized event might not have a branch stack if
274 * branch stacks have been synthesized (using itrace options).
275 */
276 if (!sample->branch_stack)
277 goto out_put;
278
279 iter.add_entry_cb = hist_iter__branch_callback;
280 iter.ops = &hist_iter_branch;
281 } else if (rep->mem_mode) {
282 iter.ops = &hist_iter_mem;
283 } else if (symbol_conf.cumulate_callchain) {
284 iter.ops = &hist_iter_cumulative;
285 } else {
286 iter.ops = &hist_iter_normal;
287 }
288
289 if (al.map != NULL)
290 al.map->dso->hit = 1;
291
David Brazdil0f672f62019-12-10 10:32:29 +0000292 if (ui__has_annotation() || rep->symbol_ipc) {
293 hist__account_cycles(sample->branch_stack, &al, sample,
294 rep->nonany_branch_mode);
295 }
296
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000297 ret = hist_entry_iter__add(&iter, &al, rep->max_stack, rep);
298 if (ret < 0)
299 pr_debug("problem adding hist entry, skipping event\n");
300out_put:
301 addr_location__put(&al);
302 return ret;
303}
304
305static int process_read_event(struct perf_tool *tool,
306 union perf_event *event,
307 struct perf_sample *sample __maybe_unused,
David Brazdil0f672f62019-12-10 10:32:29 +0000308 struct evsel *evsel,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000309 struct machine *machine __maybe_unused)
310{
311 struct report *rep = container_of(tool, struct report, tool);
312
313 if (rep->show_threads) {
David Brazdil0f672f62019-12-10 10:32:29 +0000314 const char *name = perf_evsel__name(evsel);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000315 int err = perf_read_values_add_value(&rep->show_threads_values,
316 event->read.pid, event->read.tid,
317 evsel->idx,
318 name,
319 event->read.value);
320
321 if (err)
322 return err;
323 }
324
325 return 0;
326}
327
328/* For pipe mode, sample_type is not currently set */
329static int report__setup_sample_type(struct report *rep)
330{
331 struct perf_session *session = rep->session;
332 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
333 bool is_pipe = perf_data__is_pipe(session->data);
334
335 if (session->itrace_synth_opts->callchain ||
336 (!is_pipe &&
337 perf_header__has_feat(&session->header, HEADER_AUXTRACE) &&
338 !session->itrace_synth_opts->set))
339 sample_type |= PERF_SAMPLE_CALLCHAIN;
340
341 if (session->itrace_synth_opts->last_branch)
342 sample_type |= PERF_SAMPLE_BRANCH_STACK;
343
344 if (!is_pipe && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
345 if (perf_hpp_list.parent) {
346 ui__error("Selected --sort parent, but no "
347 "callchain data. Did you call "
348 "'perf record' without -g?\n");
349 return -EINVAL;
350 }
351 if (symbol_conf.use_callchain &&
352 !symbol_conf.show_branchflag_count) {
353 ui__error("Selected -g or --branch-history.\n"
354 "But no callchain or branch data.\n"
355 "Did you call 'perf record' without -g or -b?\n");
356 return -1;
357 }
358 } else if (!callchain_param.enabled &&
359 callchain_param.mode != CHAIN_NONE &&
360 !symbol_conf.use_callchain) {
361 symbol_conf.use_callchain = true;
362 if (callchain_register_param(&callchain_param) < 0) {
363 ui__error("Can't register callchain params.\n");
364 return -EINVAL;
365 }
366 }
367
368 if (symbol_conf.cumulate_callchain) {
369 /* Silently ignore if callchain is missing */
370 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
371 symbol_conf.cumulate_callchain = false;
372 perf_hpp__cancel_cumulate();
373 }
374 }
375
376 if (sort__mode == SORT_MODE__BRANCH) {
377 if (!is_pipe &&
378 !(sample_type & PERF_SAMPLE_BRANCH_STACK)) {
379 ui__error("Selected -b but no branch data. "
380 "Did you call perf record without -b?\n");
381 return -1;
382 }
383 }
384
385 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
386 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
387 (sample_type & PERF_SAMPLE_STACK_USER)) {
388 callchain_param.record_mode = CALLCHAIN_DWARF;
389 dwarf_callchain_users = true;
390 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
391 callchain_param.record_mode = CALLCHAIN_LBR;
392 else
393 callchain_param.record_mode = CALLCHAIN_FP;
394 }
395
396 /* ??? handle more cases than just ANY? */
397 if (!(perf_evlist__combined_branch_type(session->evlist) &
398 PERF_SAMPLE_BRANCH_ANY))
399 rep->nonany_branch_mode = true;
400
Olivier Deprez0e641232021-09-23 10:07:05 +0200401#if !defined(HAVE_LIBUNWIND_SUPPORT) && !defined(HAVE_DWARF_SUPPORT)
402 if (dwarf_callchain_users) {
403 ui__warning("Please install libunwind or libdw "
404 "development packages during the perf build.\n");
405 }
406#endif
407
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000408 return 0;
409}
410
411static void sig_handler(int sig __maybe_unused)
412{
413 session_done = 1;
414}
415
416static size_t hists__fprintf_nr_sample_events(struct hists *hists, struct report *rep,
417 const char *evname, FILE *fp)
418{
419 size_t ret;
420 char unit;
421 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
422 u64 nr_events = hists->stats.total_period;
David Brazdil0f672f62019-12-10 10:32:29 +0000423 struct evsel *evsel = hists_to_evsel(hists);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000424 char buf[512];
425 size_t size = sizeof(buf);
426 int socked_id = hists->socket_filter;
427
428 if (quiet)
429 return 0;
430
431 if (symbol_conf.filter_relative) {
432 nr_samples = hists->stats.nr_non_filtered_samples;
433 nr_events = hists->stats.total_non_filtered_period;
434 }
435
436 if (perf_evsel__is_group_event(evsel)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000437 struct evsel *pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000438
439 perf_evsel__group_desc(evsel, buf, size);
440 evname = buf;
441
442 for_each_group_member(pos, evsel) {
443 const struct hists *pos_hists = evsel__hists(pos);
444
445 if (symbol_conf.filter_relative) {
446 nr_samples += pos_hists->stats.nr_non_filtered_samples;
447 nr_events += pos_hists->stats.total_non_filtered_period;
448 } else {
449 nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
450 nr_events += pos_hists->stats.total_period;
451 }
452 }
453 }
454
455 nr_samples = convert_unit(nr_samples, &unit);
456 ret = fprintf(fp, "# Samples: %lu%c", nr_samples, unit);
457 if (evname != NULL) {
458 ret += fprintf(fp, " of event%s '%s'",
David Brazdil0f672f62019-12-10 10:32:29 +0000459 evsel->core.nr_members > 1 ? "s" : "", evname);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000460 }
461
462 if (rep->time_str)
463 ret += fprintf(fp, " (time slices: %s)", rep->time_str);
464
Olivier Deprez0e641232021-09-23 10:07:05 +0200465 if (symbol_conf.show_ref_callgraph && evname && strstr(evname, "call-graph=no")) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000466 ret += fprintf(fp, ", show reference callgraph");
467 }
468
469 if (rep->mem_mode) {
470 ret += fprintf(fp, "\n# Total weight : %" PRIu64, nr_events);
471 ret += fprintf(fp, "\n# Sort order : %s", sort_order ? : default_mem_sort_order);
472 } else
473 ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events);
474
475 if (socked_id > -1)
476 ret += fprintf(fp, "\n# Processor Socket: %d", socked_id);
477
478 return ret + fprintf(fp, "\n#\n");
479}
480
David Brazdil0f672f62019-12-10 10:32:29 +0000481static int perf_evlist__tty_browse_hists(struct evlist *evlist,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000482 struct report *rep,
483 const char *help)
484{
David Brazdil0f672f62019-12-10 10:32:29 +0000485 struct evsel *pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000486
487 if (!quiet) {
488 fprintf(stdout, "#\n# Total Lost Samples: %" PRIu64 "\n#\n",
489 evlist->stats.total_lost_samples);
490 }
491
492 evlist__for_each_entry(evlist, pos) {
493 struct hists *hists = evsel__hists(pos);
494 const char *evname = perf_evsel__name(pos);
495
496 if (symbol_conf.event_group &&
497 !perf_evsel__is_group_leader(pos))
498 continue;
499
500 hists__fprintf_nr_sample_events(hists, rep, evname, stdout);
501 hists__fprintf(hists, !quiet, 0, 0, rep->min_percent, stdout,
502 !(symbol_conf.use_callchain ||
503 symbol_conf.show_branchflag_count));
504 fprintf(stdout, "\n\n");
505 }
506
507 if (!quiet)
508 fprintf(stdout, "#\n# (%s)\n#\n", help);
509
510 if (rep->show_threads) {
511 bool style = !strcmp(rep->pretty_printing_style, "raw");
512 perf_read_values_display(stdout, &rep->show_threads_values,
513 style);
514 perf_read_values_destroy(&rep->show_threads_values);
515 }
516
517 if (sort__mode == SORT_MODE__BRANCH)
518 branch_type_stat_display(stdout, &rep->brtype_stat);
519
520 return 0;
521}
522
523static void report__warn_kptr_restrict(const struct report *rep)
524{
525 struct map *kernel_map = machine__kernel_map(&rep->session->machines.host);
526 struct kmap *kernel_kmap = kernel_map ? map__kmap(kernel_map) : NULL;
527
528 if (perf_evlist__exclude_kernel(rep->session->evlist))
529 return;
530
531 if (kernel_map == NULL ||
532 (kernel_map->dso->hit &&
533 (kernel_kmap->ref_reloc_sym == NULL ||
534 kernel_kmap->ref_reloc_sym->addr == 0))) {
535 const char *desc =
536 "As no suitable kallsyms nor vmlinux was found, kernel samples\n"
537 "can't be resolved.";
538
539 if (kernel_map && map__has_symbols(kernel_map)) {
540 desc = "If some relocation was applied (e.g. "
541 "kexec) symbols may be misresolved.";
542 }
543
544 ui__warning(
545"Kernel address maps (/proc/{kallsyms,modules}) were restricted.\n\n"
546"Check /proc/sys/kernel/kptr_restrict before running 'perf record'.\n\n%s\n\n"
547"Samples in kernel modules can't be resolved as well.\n\n",
548 desc);
549 }
550}
551
552static int report__gtk_browse_hists(struct report *rep, const char *help)
553{
David Brazdil0f672f62019-12-10 10:32:29 +0000554 int (*hist_browser)(struct evlist *evlist, const char *help,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000555 struct hist_browser_timer *timer, float min_pcnt);
556
557 hist_browser = dlsym(perf_gtk_handle, "perf_evlist__gtk_browse_hists");
558
559 if (hist_browser == NULL) {
560 ui__error("GTK browser not found!\n");
561 return -1;
562 }
563
564 return hist_browser(rep->session->evlist, help, NULL, rep->min_percent);
565}
566
567static int report__browse_hists(struct report *rep)
568{
569 int ret;
570 struct perf_session *session = rep->session;
David Brazdil0f672f62019-12-10 10:32:29 +0000571 struct evlist *evlist = session->evlist;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000572 const char *help = perf_tip(system_path(TIPDIR));
573
574 if (help == NULL) {
575 /* fallback for people who don't install perf ;-) */
576 help = perf_tip(DOCDIR);
577 if (help == NULL)
578 help = "Cannot load tips.txt file, please install perf!";
579 }
580
581 switch (use_browser) {
582 case 1:
583 ret = perf_evlist__tui_browse_hists(evlist, help, NULL,
584 rep->min_percent,
585 &session->header.env,
586 true, &rep->annotation_opts);
587 /*
588 * Usually "ret" is the last pressed key, and we only
589 * care if the key notifies us to switch data file.
590 */
591 if (ret != K_SWITCH_INPUT_DATA)
592 ret = 0;
593 break;
594 case 2:
595 ret = report__gtk_browse_hists(rep, help);
596 break;
597 default:
598 ret = perf_evlist__tty_browse_hists(evlist, rep, help);
599 break;
600 }
601
602 return ret;
603}
604
605static int report__collapse_hists(struct report *rep)
606{
607 struct ui_progress prog;
David Brazdil0f672f62019-12-10 10:32:29 +0000608 struct evsel *pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000609 int ret = 0;
610
611 ui_progress__init(&prog, rep->nr_entries, "Merging related events...");
612
613 evlist__for_each_entry(rep->session->evlist, pos) {
614 struct hists *hists = evsel__hists(pos);
615
616 if (pos->idx == 0)
617 hists->symbol_filter_str = rep->symbol_filter_str;
618
619 hists->socket_filter = rep->socket_filter;
620
621 ret = hists__collapse_resort(hists, &prog);
622 if (ret < 0)
623 break;
624
625 /* Non-group events are considered as leader */
626 if (symbol_conf.event_group &&
627 !perf_evsel__is_group_leader(pos)) {
628 struct hists *leader_hists = evsel__hists(pos->leader);
629
630 hists__match(leader_hists, hists);
631 hists__link(leader_hists, hists);
632 }
633 }
634
635 ui_progress__finish();
636 return ret;
637}
638
David Brazdil0f672f62019-12-10 10:32:29 +0000639static int hists__resort_cb(struct hist_entry *he, void *arg)
640{
641 struct report *rep = arg;
642 struct symbol *sym = he->ms.sym;
643
644 if (rep->symbol_ipc && sym && !sym->annotate2) {
645 struct evsel *evsel = hists_to_evsel(he->hists);
646
647 symbol__annotate2(sym, he->ms.map, evsel,
648 &annotation__default_options, NULL);
649 }
650
651 return 0;
652}
653
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000654static void report__output_resort(struct report *rep)
655{
656 struct ui_progress prog;
David Brazdil0f672f62019-12-10 10:32:29 +0000657 struct evsel *pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000658
659 ui_progress__init(&prog, rep->nr_entries, "Sorting events for output...");
660
David Brazdil0f672f62019-12-10 10:32:29 +0000661 evlist__for_each_entry(rep->session->evlist, pos) {
662 perf_evsel__output_resort_cb(pos, &prog,
663 hists__resort_cb, rep);
664 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000665
666 ui_progress__finish();
667}
668
669static void stats_setup(struct report *rep)
670{
671 memset(&rep->tool, 0, sizeof(rep->tool));
672 rep->tool.no_warn = true;
673}
674
675static int stats_print(struct report *rep)
676{
677 struct perf_session *session = rep->session;
678
679 perf_session__fprintf_nr_events(session, stdout);
680 return 0;
681}
682
683static void tasks_setup(struct report *rep)
684{
685 memset(&rep->tool, 0, sizeof(rep->tool));
686 rep->tool.ordered_events = true;
687 if (rep->mmaps_mode) {
688 rep->tool.mmap = perf_event__process_mmap;
689 rep->tool.mmap2 = perf_event__process_mmap2;
690 }
691 rep->tool.comm = perf_event__process_comm;
692 rep->tool.exit = perf_event__process_exit;
693 rep->tool.fork = perf_event__process_fork;
694 rep->tool.no_warn = true;
695}
696
697struct task {
698 struct thread *thread;
699 struct list_head list;
700 struct list_head children;
701};
702
703static struct task *tasks_list(struct task *task, struct machine *machine)
704{
705 struct thread *parent_thread, *thread = task->thread;
706 struct task *parent_task;
707
708 /* Already listed. */
709 if (!list_empty(&task->list))
710 return NULL;
711
712 /* Last one in the chain. */
713 if (thread->ppid == -1)
714 return task;
715
716 parent_thread = machine__find_thread(machine, -1, thread->ppid);
717 if (!parent_thread)
718 return ERR_PTR(-ENOENT);
719
720 parent_task = thread__priv(parent_thread);
721 list_add_tail(&task->list, &parent_task->children);
722 return tasks_list(parent_task, machine);
723}
724
725static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp)
726{
727 size_t printed = 0;
728 struct rb_node *nd;
729
730 for (nd = rb_first(&maps->entries); nd; nd = rb_next(nd)) {
731 struct map *map = rb_entry(nd, struct map, rb_node);
732
733 printed += fprintf(fp, "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %" PRIu64 " %s\n",
734 indent, "", map->start, map->end,
735 map->prot & PROT_READ ? 'r' : '-',
736 map->prot & PROT_WRITE ? 'w' : '-',
737 map->prot & PROT_EXEC ? 'x' : '-',
738 map->flags & MAP_SHARED ? 's' : 'p',
739 map->pgoff,
740 map->ino, map->dso->name);
741 }
742
743 return printed;
744}
745
746static int map_groups__fprintf_task(struct map_groups *mg, int indent, FILE *fp)
747{
748 return maps__fprintf_task(&mg->maps, indent, fp);
749}
750
751static void task__print_level(struct task *task, FILE *fp, int level)
752{
753 struct thread *thread = task->thread;
754 struct task *child;
755 int comm_indent = fprintf(fp, " %8d %8d %8d |%*s",
756 thread->pid_, thread->tid, thread->ppid,
757 level, "");
758
759 fprintf(fp, "%s\n", thread__comm_str(thread));
760
761 map_groups__fprintf_task(thread->mg, comm_indent, fp);
762
763 if (!list_empty(&task->children)) {
764 list_for_each_entry(child, &task->children, list)
765 task__print_level(child, fp, level + 1);
766 }
767}
768
769static int tasks_print(struct report *rep, FILE *fp)
770{
771 struct perf_session *session = rep->session;
772 struct machine *machine = &session->machines.host;
773 struct task *tasks, *task;
774 unsigned int nr = 0, itask = 0, i;
775 struct rb_node *nd;
776 LIST_HEAD(list);
777
778 /*
779 * No locking needed while accessing machine->threads,
780 * because --tasks is single threaded command.
781 */
782
783 /* Count all the threads. */
784 for (i = 0; i < THREADS__TABLE_SIZE; i++)
785 nr += machine->threads[i].nr;
786
787 tasks = malloc(sizeof(*tasks) * nr);
788 if (!tasks)
789 return -ENOMEM;
790
791 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
792 struct threads *threads = &machine->threads[i];
793
David Brazdil0f672f62019-12-10 10:32:29 +0000794 for (nd = rb_first_cached(&threads->entries); nd;
795 nd = rb_next(nd)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000796 task = tasks + itask++;
797
798 task->thread = rb_entry(nd, struct thread, rb_node);
799 INIT_LIST_HEAD(&task->children);
800 INIT_LIST_HEAD(&task->list);
801 thread__set_priv(task->thread, task);
802 }
803 }
804
805 /*
806 * Iterate every task down to the unprocessed parent
807 * and link all in task children list. Task with no
808 * parent is added into 'list'.
809 */
810 for (itask = 0; itask < nr; itask++) {
811 task = tasks + itask;
812
813 if (!list_empty(&task->list))
814 continue;
815
816 task = tasks_list(task, machine);
817 if (IS_ERR(task)) {
818 pr_err("Error: failed to process tasks\n");
819 free(tasks);
820 return PTR_ERR(task);
821 }
822
823 if (task)
824 list_add_tail(&task->list, &list);
825 }
826
827 fprintf(fp, "# %8s %8s %8s %s\n", "pid", "tid", "ppid", "comm");
828
829 list_for_each_entry(task, &list, list)
830 task__print_level(task, fp, 0);
831
832 free(tasks);
833 return 0;
834}
835
836static int __cmd_report(struct report *rep)
837{
838 int ret;
839 struct perf_session *session = rep->session;
David Brazdil0f672f62019-12-10 10:32:29 +0000840 struct evsel *pos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000841 struct perf_data *data = session->data;
842
843 signal(SIGINT, sig_handler);
844
845 if (rep->cpu_list) {
846 ret = perf_session__cpu_bitmap(session, rep->cpu_list,
847 rep->cpu_bitmap);
848 if (ret) {
849 ui__error("failed to set cpu bitmap\n");
850 return ret;
851 }
852 session->itrace_synth_opts->cpu_bitmap = rep->cpu_bitmap;
853 }
854
855 if (rep->show_threads) {
856 ret = perf_read_values_init(&rep->show_threads_values);
857 if (ret)
858 return ret;
859 }
860
861 ret = report__setup_sample_type(rep);
862 if (ret) {
863 /* report__setup_sample_type() already showed error message */
864 return ret;
865 }
866
867 if (rep->stats_mode)
868 stats_setup(rep);
869
870 if (rep->tasks_mode)
871 tasks_setup(rep);
872
873 ret = perf_session__process_events(session);
874 if (ret) {
875 ui__error("failed to process sample\n");
876 return ret;
877 }
878
879 if (rep->stats_mode)
880 return stats_print(rep);
881
882 if (rep->tasks_mode)
883 return tasks_print(rep, stdout);
884
885 report__warn_kptr_restrict(rep);
886
887 evlist__for_each_entry(session->evlist, pos)
888 rep->nr_entries += evsel__hists(pos)->nr_entries;
889
890 if (use_browser == 0) {
891 if (verbose > 3)
892 perf_session__fprintf(session, stdout);
893
894 if (verbose > 2)
895 perf_session__fprintf_dsos(session, stdout);
896
897 if (dump_trace) {
898 perf_session__fprintf_nr_events(session, stdout);
899 perf_evlist__fprintf_nr_events(session->evlist, stdout);
900 return 0;
901 }
902 }
903
904 ret = report__collapse_hists(rep);
905 if (ret) {
906 ui__error("failed to process hist entry\n");
907 return ret;
908 }
909
910 if (session_done())
911 return 0;
912
913 /*
914 * recalculate number of entries after collapsing since it
915 * might be changed during the collapse phase.
916 */
917 rep->nr_entries = 0;
918 evlist__for_each_entry(session->evlist, pos)
919 rep->nr_entries += evsel__hists(pos)->nr_entries;
920
921 if (rep->nr_entries == 0) {
David Brazdil0f672f62019-12-10 10:32:29 +0000922 ui__error("The %s data has no samples!\n", data->path);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000923 return 0;
924 }
925
926 report__output_resort(rep);
927
928 return report__browse_hists(rep);
929}
930
931static int
932report_parse_callchain_opt(const struct option *opt, const char *arg, int unset)
933{
934 struct callchain_param *callchain = opt->value;
935
936 callchain->enabled = !unset;
937 /*
938 * --no-call-graph
939 */
940 if (unset) {
941 symbol_conf.use_callchain = false;
942 callchain->mode = CHAIN_NONE;
943 return 0;
944 }
945
946 return parse_callchain_report_opt(arg);
947}
948
David Brazdil0f672f62019-12-10 10:32:29 +0000949static int
950parse_time_quantum(const struct option *opt, const char *arg,
951 int unset __maybe_unused)
952{
953 unsigned long *time_q = opt->value;
954 char *end;
955
956 *time_q = strtoul(arg, &end, 0);
957 if (end == arg)
958 goto parse_err;
959 if (*time_q == 0) {
960 pr_err("time quantum cannot be 0");
961 return -1;
962 }
963 end = skip_spaces(end);
964 if (*end == 0)
965 return 0;
966 if (!strcmp(end, "s")) {
967 *time_q *= NSEC_PER_SEC;
968 return 0;
969 }
970 if (!strcmp(end, "ms")) {
971 *time_q *= NSEC_PER_MSEC;
972 return 0;
973 }
974 if (!strcmp(end, "us")) {
975 *time_q *= NSEC_PER_USEC;
976 return 0;
977 }
978 if (!strcmp(end, "ns"))
979 return 0;
980parse_err:
981 pr_err("Cannot parse time quantum `%s'\n", arg);
982 return -1;
983}
984
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000985int
986report_parse_ignore_callees_opt(const struct option *opt __maybe_unused,
987 const char *arg, int unset __maybe_unused)
988{
989 if (arg) {
990 int err = regcomp(&ignore_callees_regex, arg, REG_EXTENDED);
991 if (err) {
992 char buf[BUFSIZ];
993 regerror(err, &ignore_callees_regex, buf, sizeof(buf));
994 pr_err("Invalid --ignore-callees regex: %s\n%s", arg, buf);
995 return -1;
996 }
997 have_ignore_callees = 1;
998 }
999
1000 return 0;
1001}
1002
1003static int
1004parse_branch_mode(const struct option *opt,
1005 const char *str __maybe_unused, int unset)
1006{
1007 int *branch_mode = opt->value;
1008
1009 *branch_mode = !unset;
1010 return 0;
1011}
1012
1013static int
1014parse_percent_limit(const struct option *opt, const char *str,
1015 int unset __maybe_unused)
1016{
1017 struct report *rep = opt->value;
1018 double pcnt = strtof(str, NULL);
1019
1020 rep->min_percent = pcnt;
1021 callchain_param.min_percent = pcnt;
1022 return 0;
1023}
1024
1025int cmd_report(int argc, const char **argv)
1026{
1027 struct perf_session *session;
1028 struct itrace_synth_opts itrace_synth_opts = { .set = 0, };
1029 struct stat st;
1030 bool has_br_stack = false;
1031 int branch_mode = -1;
Olivier Deprez0e641232021-09-23 10:07:05 +02001032 int last_key = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001033 bool branch_call_mode = false;
1034#define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
David Brazdil0f672f62019-12-10 10:32:29 +00001035 static const char report_callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
1036 CALLCHAIN_REPORT_HELP
1037 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001038 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
1039 const char * const report_usage[] = {
1040 "perf report [<options>]",
1041 NULL
1042 };
1043 struct report report = {
1044 .tool = {
1045 .sample = process_sample_event,
1046 .mmap = perf_event__process_mmap,
1047 .mmap2 = perf_event__process_mmap2,
1048 .comm = perf_event__process_comm,
1049 .namespaces = perf_event__process_namespaces,
1050 .exit = perf_event__process_exit,
1051 .fork = perf_event__process_fork,
1052 .lost = perf_event__process_lost,
1053 .read = process_read_event,
1054 .attr = perf_event__process_attr,
1055 .tracing_data = perf_event__process_tracing_data,
1056 .build_id = perf_event__process_build_id,
1057 .id_index = perf_event__process_id_index,
1058 .auxtrace_info = perf_event__process_auxtrace_info,
1059 .auxtrace = perf_event__process_auxtrace,
1060 .event_update = perf_event__process_event_update,
1061 .feature = process_feature_event,
1062 .ordered_events = true,
1063 .ordering_requires_timestamps = true,
1064 },
1065 .max_stack = PERF_MAX_STACK_DEPTH,
1066 .pretty_printing_style = "normal",
1067 .socket_filter = -1,
1068 .annotation_opts = annotation__default_options,
1069 };
1070 const struct option options[] = {
1071 OPT_STRING('i', "input", &input_name, "file",
1072 "input file name"),
1073 OPT_INCR('v', "verbose", &verbose,
1074 "be more verbose (show symbol address, etc)"),
1075 OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any message"),
1076 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1077 "dump raw trace in ASCII"),
1078 OPT_BOOLEAN(0, "stats", &report.stats_mode, "Display event stats"),
1079 OPT_BOOLEAN(0, "tasks", &report.tasks_mode, "Display recorded tasks"),
1080 OPT_BOOLEAN(0, "mmaps", &report.mmaps_mode, "Display recorded tasks memory maps"),
1081 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1082 "file", "vmlinux pathname"),
1083 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
1084 "don't load vmlinux even if found"),
1085 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1086 "file", "kallsyms pathname"),
1087 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
1088 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
1089 "load module symbols - WARNING: use only with -k and LIVE kernel"),
1090 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
1091 "Show a column with the number of samples"),
1092 OPT_BOOLEAN('T', "threads", &report.show_threads,
1093 "Show per-thread event counters"),
1094 OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
1095 "pretty printing style key: normal raw"),
1096 OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
1097 OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK2 interface"),
1098 OPT_BOOLEAN(0, "stdio", &report.use_stdio,
1099 "Use the stdio interface"),
1100 OPT_BOOLEAN(0, "header", &report.header, "Show data header."),
1101 OPT_BOOLEAN(0, "header-only", &report.header_only,
1102 "Show only data header."),
1103 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
David Brazdil0f672f62019-12-10 10:32:29 +00001104 sort_help("sort by key(s):")),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001105 OPT_STRING('F', "fields", &field_order, "key[,keys...]",
David Brazdil0f672f62019-12-10 10:32:29 +00001106 sort_help("output field(s): overhead period sample ")),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001107 OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization,
1108 "Show sample percentage for different cpu modes"),
1109 OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
1110 "Show sample percentage for different cpu modes", PARSE_OPT_HIDDEN),
1111 OPT_STRING('p', "parent", &parent_pattern, "regex",
1112 "regex filter to identify parent, see: '--sort parent'"),
1113 OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
1114 "Only display entries with parent-match"),
1115 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
1116 "print_type,threshold[,print_limit],order,sort_key[,branch],value",
1117 report_callchain_help, &report_parse_callchain_opt,
1118 callchain_default_opt),
1119 OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
1120 "Accumulate callchains of children and show total overhead as well"),
1121 OPT_INTEGER(0, "max-stack", &report.max_stack,
1122 "Set the maximum stack depth when parsing the callchain, "
1123 "anything beyond the specified depth will be ignored. "
1124 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
1125 OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
1126 "alias for inverted call graph"),
1127 OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
1128 "ignore callees of these functions in call graphs",
1129 report_parse_ignore_callees_opt),
1130 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
1131 "only consider symbols in these dsos"),
1132 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1133 "only consider symbols in these comms"),
1134 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1135 "only consider symbols in these pids"),
1136 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1137 "only consider symbols in these tids"),
1138 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1139 "only consider these symbols"),
1140 OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
1141 "only show symbols that (partially) match with this filter"),
1142 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
1143 "width[,width...]",
1144 "don't try to adjust column width, use these fixed values"),
1145 OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
1146 "separator for columns, no spaces will be added between "
1147 "columns '.' is reserved."),
1148 OPT_BOOLEAN('U', "hide-unresolved", &symbol_conf.hide_unresolved,
1149 "Only display entries resolved to a symbol"),
1150 OPT_CALLBACK(0, "symfs", NULL, "directory",
1151 "Look for files with symbols relative to this directory",
1152 symbol__config_symfs),
1153 OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
1154 "list of cpus to profile"),
1155 OPT_BOOLEAN('I', "show-info", &report.show_full_info,
1156 "Display extended information about perf.data file"),
1157 OPT_BOOLEAN(0, "source", &report.annotation_opts.annotate_src,
1158 "Interleave source code with assembly code (default)"),
1159 OPT_BOOLEAN(0, "asm-raw", &report.annotation_opts.show_asm_raw,
1160 "Display raw encoding of assembly instructions (default)"),
1161 OPT_STRING('M', "disassembler-style", &report.annotation_opts.disassembler_style, "disassembler style",
1162 "Specify disassembler style (e.g. -M intel for intel syntax)"),
1163 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
1164 "Show a column with the sum of periods"),
1165 OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group, &report.group_set,
1166 "Show event group information together"),
1167 OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "",
1168 "use branch records for per branch histogram filling",
1169 parse_branch_mode),
1170 OPT_BOOLEAN(0, "branch-history", &branch_call_mode,
1171 "add last branch records to call history"),
1172 OPT_STRING(0, "objdump", &report.annotation_opts.objdump_path, "path",
1173 "objdump binary to use for disassembly and annotations"),
1174 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1175 "Disable symbol demangling"),
1176 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1177 "Enable kernel symbol demangling"),
1178 OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
David Brazdil0f672f62019-12-10 10:32:29 +00001179 OPT_INTEGER(0, "samples", &symbol_conf.res_sample,
1180 "Number of samples to save per histogram entry for individual browsing"),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001181 OPT_CALLBACK(0, "percent-limit", &report, "percent",
1182 "Don't show entries under that percent", parse_percent_limit),
1183 OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
1184 "how to display percentage of filtered entries", parse_filter_percentage),
1185 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
David Brazdil0f672f62019-12-10 10:32:29 +00001186 "Instruction Tracing options\n" ITRACE_HELP,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001187 itrace_parse_synth_opts),
1188 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1189 "Show full source file name path for source lines"),
1190 OPT_BOOLEAN(0, "show-ref-call-graph", &symbol_conf.show_ref_callgraph,
1191 "Show callgraph from reference event"),
1192 OPT_INTEGER(0, "socket-filter", &report.socket_filter,
1193 "only show processor socket that match with this filter"),
1194 OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
1195 "Show raw trace event output (do not use print fmt or plugins)"),
1196 OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
1197 "Show entries in a hierarchy"),
1198 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
1199 "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
1200 stdio__config_color, "always"),
1201 OPT_STRING(0, "time", &report.time_str, "str",
1202 "Time span of interest (start,stop)"),
1203 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
1204 "Show inline function"),
1205 OPT_CALLBACK(0, "percent-type", &report.annotation_opts, "local-period",
1206 "Set percent type local/global-period/hits",
1207 annotate_parse_percent_type),
David Brazdil0f672f62019-12-10 10:32:29 +00001208 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs, "Show times in nanosecs"),
1209 OPT_CALLBACK(0, "time-quantum", &symbol_conf.time_quantum, "time (ms|us|ns|s)",
1210 "Set time quantum for time sort key (default 100ms)",
1211 parse_time_quantum),
1212 OPTS_EVSWITCH(&report.evswitch),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001213 OPT_END()
1214 };
1215 struct perf_data data = {
1216 .mode = PERF_DATA_MODE_READ,
1217 };
1218 int ret = hists__init();
David Brazdil0f672f62019-12-10 10:32:29 +00001219 char sort_tmp[128];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001220
1221 if (ret < 0)
1222 return ret;
1223
1224 ret = perf_config(report__config, &report);
1225 if (ret)
1226 return ret;
1227
1228 argc = parse_options(argc, argv, options, report_usage, 0);
1229 if (argc) {
1230 /*
1231 * Special case: if there's an argument left then assume that
1232 * it's a symbol filter:
1233 */
1234 if (argc > 1)
1235 usage_with_options(report_usage, options);
1236
1237 report.symbol_filter_str = argv[0];
1238 }
1239
1240 if (report.mmaps_mode)
1241 report.tasks_mode = true;
1242
1243 if (quiet)
1244 perf_quiet_option();
1245
1246 if (symbol_conf.vmlinux_name &&
1247 access(symbol_conf.vmlinux_name, R_OK)) {
1248 pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
1249 return -EINVAL;
1250 }
1251 if (symbol_conf.kallsyms_name &&
1252 access(symbol_conf.kallsyms_name, R_OK)) {
1253 pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
1254 return -EINVAL;
1255 }
1256
1257 if (report.inverted_callchain)
1258 callchain_param.order = ORDER_CALLER;
1259 if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
1260 callchain_param.order = ORDER_CALLER;
1261
1262 if (itrace_synth_opts.callchain &&
1263 (int)itrace_synth_opts.callchain_sz > report.max_stack)
1264 report.max_stack = itrace_synth_opts.callchain_sz;
1265
1266 if (!input_name || !strlen(input_name)) {
1267 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
1268 input_name = "-";
1269 else
1270 input_name = "perf.data";
1271 }
1272
David Brazdil0f672f62019-12-10 10:32:29 +00001273 data.path = input_name;
1274 data.force = symbol_conf.force;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001275
1276repeat:
1277 session = perf_session__new(&data, false, &report.tool);
David Brazdil0f672f62019-12-10 10:32:29 +00001278 if (IS_ERR(session))
1279 return PTR_ERR(session);
1280
1281 ret = evswitch__init(&report.evswitch, session->evlist, stderr);
1282 if (ret)
1283 return ret;
1284
1285 if (zstd_init(&(session->zstd_data), 0) < 0)
1286 pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001287
1288 if (report.queue_size) {
1289 ordered_events__set_alloc_size(&session->ordered_events,
1290 report.queue_size);
1291 }
1292
1293 session->itrace_synth_opts = &itrace_synth_opts;
1294
1295 report.session = session;
1296
1297 has_br_stack = perf_header__has_feat(&session->header,
1298 HEADER_BRANCH_STACK);
David Brazdil0f672f62019-12-10 10:32:29 +00001299 if (perf_evlist__combined_sample_type(session->evlist) & PERF_SAMPLE_STACK_USER)
1300 has_br_stack = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001301
1302 setup_forced_leader(&report, session->evlist);
1303
1304 if (itrace_synth_opts.last_branch)
1305 has_br_stack = true;
1306
1307 if (has_br_stack && branch_call_mode)
1308 symbol_conf.show_branchflag_count = true;
1309
1310 memset(&report.brtype_stat, 0, sizeof(struct branch_type_stat));
1311
1312 /*
1313 * Branch mode is a tristate:
1314 * -1 means default, so decide based on the file having branch data.
1315 * 0/1 means the user chose a mode.
1316 */
1317 if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) &&
1318 !branch_call_mode) {
1319 sort__mode = SORT_MODE__BRANCH;
1320 symbol_conf.cumulate_callchain = false;
1321 }
1322 if (branch_call_mode) {
1323 callchain_param.key = CCKEY_ADDRESS;
1324 callchain_param.branch_callstack = 1;
1325 symbol_conf.use_callchain = true;
1326 callchain_register_param(&callchain_param);
1327 if (sort_order == NULL)
1328 sort_order = "srcline,symbol,dso";
1329 }
1330
1331 if (report.mem_mode) {
1332 if (sort__mode == SORT_MODE__BRANCH) {
1333 pr_err("branch and mem mode incompatible\n");
1334 goto error;
1335 }
1336 sort__mode = SORT_MODE__MEMORY;
1337 symbol_conf.cumulate_callchain = false;
1338 }
1339
1340 if (symbol_conf.report_hierarchy) {
1341 /* disable incompatible options */
1342 symbol_conf.cumulate_callchain = false;
1343
1344 if (field_order) {
1345 pr_err("Error: --hierarchy and --fields options cannot be used together\n");
1346 parse_options_usage(report_usage, options, "F", 1);
1347 parse_options_usage(NULL, options, "hierarchy", 0);
1348 goto error;
1349 }
1350
1351 perf_hpp_list.need_collapse = true;
1352 }
1353
1354 if (report.use_stdio)
1355 use_browser = 0;
1356 else if (report.use_tui)
1357 use_browser = 1;
1358 else if (report.use_gtk)
1359 use_browser = 2;
1360
1361 /* Force tty output for header output and per-thread stat. */
1362 if (report.header || report.header_only || report.show_threads)
1363 use_browser = 0;
1364 if (report.header || report.header_only)
1365 report.tool.show_feat_hdr = SHOW_FEAT_HEADER;
1366 if (report.show_full_info)
1367 report.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
1368 if (report.stats_mode || report.tasks_mode)
1369 use_browser = 0;
1370 if (report.stats_mode && report.tasks_mode) {
1371 pr_err("Error: --tasks and --mmaps can't be used together with --stats\n");
1372 goto error;
1373 }
1374
1375 if (strcmp(input_name, "-") != 0)
1376 setup_browser(true);
1377 else
1378 use_browser = 0;
1379
David Brazdil0f672f62019-12-10 10:32:29 +00001380 if (sort_order && strstr(sort_order, "ipc")) {
1381 parse_options_usage(report_usage, options, "s", 1);
1382 goto error;
1383 }
1384
1385 if (sort_order && strstr(sort_order, "symbol")) {
1386 if (sort__mode == SORT_MODE__BRANCH) {
1387 snprintf(sort_tmp, sizeof(sort_tmp), "%s,%s",
1388 sort_order, "ipc_lbr");
1389 report.symbol_ipc = true;
1390 } else {
1391 snprintf(sort_tmp, sizeof(sort_tmp), "%s,%s",
1392 sort_order, "ipc_null");
1393 }
1394
1395 sort_order = sort_tmp;
1396 }
1397
Olivier Deprez0e641232021-09-23 10:07:05 +02001398 if ((last_key != K_SWITCH_INPUT_DATA) &&
1399 (setup_sorting(session->evlist) < 0)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001400 if (sort_order)
1401 parse_options_usage(report_usage, options, "s", 1);
1402 if (field_order)
1403 parse_options_usage(sort_order ? NULL : report_usage,
1404 options, "F", 1);
1405 goto error;
1406 }
1407
1408 if ((report.header || report.header_only) && !quiet) {
1409 perf_session__fprintf_info(session, stdout,
1410 report.show_full_info);
1411 if (report.header_only) {
1412 ret = 0;
1413 goto error;
1414 }
1415 } else if (use_browser == 0 && !quiet &&
1416 !report.stats_mode && !report.tasks_mode) {
1417 fputs("# To display the perf.data header info, please use --header/--header-only options.\n#\n",
1418 stdout);
1419 }
1420
1421 /*
1422 * Only in the TUI browser we are doing integrated annotation,
1423 * so don't allocate extra space that won't be used in the stdio
1424 * implementation.
1425 */
David Brazdil0f672f62019-12-10 10:32:29 +00001426 if (ui__has_annotation() || report.symbol_ipc) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001427 ret = symbol__annotation_init();
1428 if (ret < 0)
1429 goto error;
1430 /*
1431 * For searching by name on the "Browse map details".
1432 * providing it only in verbose mode not to bloat too
1433 * much struct symbol.
1434 */
1435 if (verbose > 0) {
1436 /*
1437 * XXX: Need to provide a less kludgy way to ask for
1438 * more space per symbol, the u32 is for the index on
1439 * the ui browser.
1440 * See symbol__browser_index.
1441 */
1442 symbol_conf.priv_size += sizeof(u32);
1443 symbol_conf.sort_by_name = true;
1444 }
1445 annotation_config__init();
1446 }
1447
1448 if (symbol__init(&session->header.env) < 0)
1449 goto error;
1450
David Brazdil0f672f62019-12-10 10:32:29 +00001451 if (report.time_str) {
1452 ret = perf_time__parse_for_ranges(report.time_str, session,
1453 &report.ptime_range,
1454 &report.range_size,
1455 &report.range_num);
1456 if (ret < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001457 goto error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001458
David Brazdil0f672f62019-12-10 10:32:29 +00001459 itrace_synth_opts__set_time_range(&itrace_synth_opts,
1460 report.ptime_range,
1461 report.range_num);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001462 }
1463
1464 if (session->tevent.pevent &&
1465 tep_set_function_resolver(session->tevent.pevent,
1466 machine__resolve_kernel_addr,
1467 &session->machines.host) < 0) {
1468 pr_err("%s: failed to set libtraceevent function resolver\n",
1469 __func__);
1470 return -1;
1471 }
1472
1473 sort__setup_elide(stdout);
1474
1475 ret = __cmd_report(&report);
1476 if (ret == K_SWITCH_INPUT_DATA) {
1477 perf_session__delete(session);
Olivier Deprez0e641232021-09-23 10:07:05 +02001478 last_key = K_SWITCH_INPUT_DATA;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001479 goto repeat;
1480 } else
1481 ret = 0;
1482
1483error:
David Brazdil0f672f62019-12-10 10:32:29 +00001484 if (report.ptime_range) {
1485 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
1486 zfree(&report.ptime_range);
1487 }
1488 zstd_fini(&(session->zstd_data));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001489 perf_session__delete(session);
1490 return ret;
1491}