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