]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - tools/perf/builtin-report.c
perf: Better fit max unprivileged mlock pages for tools needs
[mirror_ubuntu-jammy-kernel.git] / tools / perf / builtin-report.c
CommitLineData
bf9e1876
IM
1/*
2 * builtin-report.c
3 *
4 * Builtin report command: Analyze the perf.data input file,
5 * look up and read DSOs and symbol information and display
6 * a histogram of results, along various sorting keys.
7 */
16f762a2 8#include "builtin.h"
53cb8bc2 9
bf9e1876
IM
10#include "util/util.h"
11
78f7defe 12#include "util/annotate.h"
8fc0321f 13#include "util/color.h"
5da50258 14#include <linux/list.h>
a930d2c0 15#include "util/cache.h"
43cbcd8a 16#include <linux/rbtree.h>
a2928c42 17#include "util/symbol.h"
f55c5552 18#include "util/callchain.h"
25903407 19#include "util/strlist.h"
8d513270 20#include "util/values.h"
8fa66bdc 21
53cb8bc2 22#include "perf.h"
8f28827a 23#include "util/debug.h"
e248de33
ACM
24#include "util/evlist.h"
25#include "util/evsel.h"
7c6a1c65 26#include "util/header.h"
94c744b6 27#include "util/session.h"
53cb8bc2
IM
28
29#include "util/parse-options.h"
30#include "util/parse-events.h"
31
6baa0a5a 32#include "util/thread.h"
dd68ada2 33#include "util/sort.h"
3d1d07ec 34#include "util/hist.h"
6baa0a5a 35
23ac9cbe 36static char const *input_name = "perf.data";
bd74137e 37
8b9e74eb 38static bool force, use_tui, use_stdio;
71289be7 39static bool hide_unresolved;
b9a63b9b 40static bool dont_use_callchains;
97b07b69 41
c0555642 42static bool show_threads;
8d513270
BG
43static struct perf_read_values show_threads_values;
44
edb7c60e
ACM
45static const char default_pretty_printing_style[] = "normal";
46static const char *pretty_printing_style = default_pretty_printing_style;
9f866697 47
805d127d 48static char callchain_default_opt[] = "fractal,0.5";
0849327d 49static symbol_filter_t annotate_init;
805d127d 50
8d50e5b4 51static int perf_session__add_hist_entry(struct perf_session *session,
4e4f06e4 52 struct addr_location *al,
8d50e5b4 53 struct perf_sample *sample)
8fa66bdc 54{
b3c9ac08 55 struct symbol *parent = NULL;
1b3a0e95 56 int err = 0;
e7fb08b1 57 struct hist_entry *he;
e248de33 58 struct perf_evsel *evsel;
e7fb08b1 59
8d50e5b4
ACM
60 if ((sort__has_parent || symbol_conf.use_callchain) && sample->callchain) {
61 err = perf_session__resolve_callchain(session, al->thread,
62 sample->callchain, &parent);
1b3a0e95
FW
63 if (err)
64 return err;
ad5b217b 65 }
cbbc79a5 66
e248de33
ACM
67 evsel = perf_evlist__id2evsel(session->evlist, sample->id);
68 if (evsel == NULL) {
69 /*
70 * FIXME: Propagate this back, but at least we're in a builtin,
71 * where exit() is allowed. ;-)
72 */
a91e5431
ACM
73 ui__warning("Invalid %s file, contains samples with id %" PRIu64 " not in "
74 "its header!\n", input_name, sample->id);
e248de33
ACM
75 exit_browser(0);
76 exit(1);
77 }
1b3a0e95 78
e248de33 79 he = __hists__add_entry(&evsel->hists, al, parent, sample->period);
9735abf1 80 if (he == NULL)
1b3a0e95
FW
81 return -ENOMEM;
82
ef7b93a1 83 if (symbol_conf.use_callchain) {
8d50e5b4
ACM
84 err = callchain_append(he->callchain, &session->callchain_cursor,
85 sample->period);
ef7b93a1 86 if (err)
1b3a0e95 87 return err;
ef7b93a1
ACM
88 }
89 /*
90 * Only in the newt browser we are doing integrated annotation,
91 * so we don't allocated the extra space needed because the stdio
92 * code will not use it.
93 */
2f525d01 94 if (al->sym != NULL && use_browser > 0) {
2f525d01 95 struct annotation *notes = symbol__annotation(he->ms.sym);
e248de33
ACM
96
97 assert(evsel != NULL);
98
99 err = -ENOMEM;
ce6f4fab 100 if (notes->src == NULL &&
e248de33
ACM
101 symbol__alloc_hist(he->ms.sym, session->evlist->nr_entries) < 0)
102 goto out;
103
104 err = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
2f525d01 105 }
1b3a0e95 106
e248de33
ACM
107 evsel->hists.stats.total_period += sample->period;
108 hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
109out:
39d1e1b1 110 return err;
8fa66bdc
ACM
111}
112
cbbc79a5 113
8115d60c
ACM
114static int process_sample_event(union perf_event *event,
115 struct perf_sample *sample,
640c03ce 116 struct perf_session *session)
75051724 117{
1ed091c4 118 struct addr_location al;
180f95e2 119
ce6f4fab 120 if (perf_event__preprocess_sample(event, session, &al, sample,
0849327d 121 annotate_init) < 0) {
c410a338 122 fprintf(stderr, "problem processing %d event, skipping it.\n",
75051724
IM
123 event->header.type);
124 return -1;
125 }
e7fb08b1 126
71289be7 127 if (al.filtered || (hide_unresolved && al.sym == NULL))
ec218fc4 128 return 0;
7bec7a91 129
640c03ce 130 if (perf_session__add_hist_entry(session, &al, sample)) {
c82ee828 131 pr_debug("problem incrementing symbol period, skipping event\n");
ec218fc4 132 return -1;
8fa66bdc 133 }
ec218fc4 134
75051724
IM
135 return 0;
136}
3502973d 137
8115d60c
ACM
138static int process_read_event(union perf_event *event,
139 struct perf_sample *sample __used,
e248de33 140 struct perf_session *session)
e9ea2fde 141{
e248de33
ACM
142 struct perf_evsel *evsel = perf_evlist__id2evsel(session->evlist,
143 event->read.id);
8d513270 144 if (show_threads) {
e248de33 145 const char *name = evsel ? event_name(evsel) : "unknown";
8d513270
BG
146 perf_read_values_add_value(&show_threads_values,
147 event->read.pid, event->read.tid,
148 event->read.id,
149 name,
150 event->read.value);
151 }
152
9486aa38 153 dump_printf(": %d %d %s %" PRIu64 "\n", event->read.pid, event->read.tid,
e248de33 154 evsel ? event_name(evsel) : "FAIL",
62daacb5 155 event->read.value);
e9ea2fde
PZ
156
157 return 0;
158}
159
d549c769 160static int perf_session__setup_sample_type(struct perf_session *self)
d80d338d 161{
d549c769 162 if (!(self->sample_type & PERF_SAMPLE_CALLCHAIN)) {
91b4eaea
FW
163 if (sort__has_parent) {
164 fprintf(stderr, "selected --sort parent, but no"
165 " callchain data. Did you call"
166 " perf record without -g?\n");
d549c769 167 return -EINVAL;
91b4eaea 168 }
d599db3f 169 if (symbol_conf.use_callchain) {
6ede59c4 170 fprintf(stderr, "selected -g but no callchain data."
91b4eaea
FW
171 " Did you call perf record without"
172 " -g?\n");
016e92fb 173 return -1;
91b4eaea 174 }
b9a63b9b
ACM
175 } else if (!dont_use_callchains && callchain_param.mode != CHAIN_NONE &&
176 !symbol_conf.use_callchain) {
d599db3f 177 symbol_conf.use_callchain = true;
16537f13 178 if (callchain_register_param(&callchain_param) < 0) {
b1a88349
FW
179 fprintf(stderr, "Can't register callchain"
180 " params\n");
d549c769 181 return -EINVAL;
b1a88349 182 }
f5970550
PZ
183 }
184
016e92fb
FW
185 return 0;
186}
6142f9ec 187
301a0b02 188static struct perf_event_ops event_ops = {
8115d60c
ACM
189 .sample = process_sample_event,
190 .mmap = perf_event__process_mmap,
191 .comm = perf_event__process_comm,
192 .exit = perf_event__process_task,
193 .fork = perf_event__process_task,
194 .lost = perf_event__process_lost,
195 .read = process_read_event,
196 .attr = perf_event__process_attr,
197 .event_type = perf_event__process_event_type,
198 .tracing_data = perf_event__process_tracing_data,
199 .build_id = perf_event__process_build_id,
eac23d1c
IM
200 .ordered_samples = true,
201 .ordering_requires_timestamps = true,
016e92fb 202};
6142f9ec 203
46656ac7
TZ
204extern volatile int session_done;
205
c82ee828 206static void sig_handler(int sig __used)
46656ac7
TZ
207{
208 session_done = 1;
209}
210
c82ee828
ACM
211static size_t hists__fprintf_nr_sample_events(struct hists *self,
212 const char *evname, FILE *fp)
213{
214 size_t ret;
215 char unit;
216 unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE];
217
218 nr_events = convert_unit(nr_events, &unit);
219 ret = fprintf(fp, "# Events: %lu%c", nr_events, unit);
220 if (evname != NULL)
221 ret += fprintf(fp, " %s", evname);
222 return ret + fprintf(fp, "\n#\n");
223}
224
7f0030b2
ACM
225static int perf_evlist__tty_browse_hists(struct perf_evlist *evlist,
226 const char *help)
d67f088e 227{
e248de33 228 struct perf_evsel *pos;
d67f088e 229
e248de33
ACM
230 list_for_each_entry(pos, &evlist->entries, node) {
231 struct hists *hists = &pos->hists;
d67f088e
ACM
232 const char *evname = NULL;
233
234 if (rb_first(&hists->entries) != rb_last(&hists->entries))
e248de33 235 evname = event_name(pos);
d67f088e
ACM
236
237 hists__fprintf_nr_sample_events(hists, evname, stdout);
238 hists__fprintf(hists, NULL, false, stdout);
239 fprintf(stdout, "\n\n");
d67f088e
ACM
240 }
241
242 if (sort_order == default_sort_order &&
243 parent_pattern == default_parent_pattern) {
244 fprintf(stdout, "#\n# (%s)\n#\n", help);
245
246 if (show_threads) {
247 bool style = !strcmp(pretty_printing_style, "raw");
248 perf_read_values_display(stdout, &show_threads_values,
249 style);
250 perf_read_values_destroy(&show_threads_values);
251 }
252 }
253
254 return 0;
255}
256
016e92fb
FW
257static int __cmd_report(void)
258{
d549c769 259 int ret = -EINVAL;
e248de33 260 u64 nr_samples;
d8f66248 261 struct perf_session *session;
e248de33 262 struct perf_evsel *pos;
f9224c5c 263 const char *help = "For a higher level overview, try: perf report --sort comm,dso";
8fa66bdc 264
46656ac7
TZ
265 signal(SIGINT, sig_handler);
266
21ef97f0 267 session = perf_session__new(input_name, O_RDONLY, force, false, &event_ops);
94c744b6
ACM
268 if (session == NULL)
269 return -ENOMEM;
270
016e92fb
FW
271 if (show_threads)
272 perf_read_values_init(&show_threads_values);
f5970550 273
d549c769
ACM
274 ret = perf_session__setup_sample_type(session);
275 if (ret)
276 goto out_delete;
277
ec913369 278 ret = perf_session__process_events(session, &event_ops);
016e92fb 279 if (ret)
94c744b6 280 goto out_delete;
97b07b69 281
62daacb5 282 if (dump_trace) {
c8446b9b 283 perf_session__fprintf_nr_events(session, stdout);
94c744b6 284 goto out_delete;
62daacb5 285 }
97b07b69 286
da21d1b5 287 if (verbose > 3)
b3165f41 288 perf_session__fprintf(session, stdout);
9ac99545 289
da21d1b5 290 if (verbose > 2)
cbf69680 291 perf_session__fprintf_dsos(session, stdout);
16f762a2 292
e248de33
ACM
293 nr_samples = 0;
294 list_for_each_entry(pos, &session->evlist->entries, node) {
295 struct hists *hists = &pos->hists;
cbbc79a5 296
1c02c4d2 297 hists__collapse_resort(hists);
fefb0b94 298 hists__output_resort(hists);
e248de33
ACM
299 nr_samples += hists->stats.nr_events[PERF_RECORD_SAMPLE];
300 }
301
302 if (nr_samples == 0) {
303 ui__warning("The %s file has no samples!\n", input_name);
304 goto out_delete;
cbbc79a5
EM
305 }
306
d67f088e 307 if (use_browser > 0)
7f0030b2 308 perf_evlist__tui_browse_hists(session->evlist, help);
d67f088e 309 else
7f0030b2 310 perf_evlist__tty_browse_hists(session->evlist, help);
3e6055ab 311
94c744b6 312out_delete:
71e7cf3a
ACM
313 /*
314 * Speed up the exit process, for large files this can
315 * take quite a while.
316 *
317 * XXX Enable this when using valgrind or if we ever
318 * librarize this command.
319 *
320 * Also experiment with obstacks to see how much speed
321 * up we'll get here.
322 *
323 * perf_session__delete(session);
324 */
016e92fb 325 return ret;
8fa66bdc
ACM
326}
327
4eb3e478
FW
328static int
329parse_callchain_opt(const struct option *opt __used, const char *arg,
b9a63b9b 330 int unset)
4eb3e478 331{
232a5c94 332 char *tok, *tok2;
c20ab37e
FW
333 char *endptr;
334
b9a63b9b
ACM
335 /*
336 * --no-call-graph
337 */
338 if (unset) {
339 dont_use_callchains = true;
340 return 0;
341 }
342
d599db3f 343 symbol_conf.use_callchain = true;
4eb3e478
FW
344
345 if (!arg)
346 return 0;
347
c20ab37e
FW
348 tok = strtok((char *)arg, ",");
349 if (!tok)
350 return -1;
351
352 /* get the output mode */
353 if (!strncmp(tok, "graph", strlen(arg)))
805d127d 354 callchain_param.mode = CHAIN_GRAPH_ABS;
4eb3e478 355
c20ab37e 356 else if (!strncmp(tok, "flat", strlen(arg)))
805d127d
FW
357 callchain_param.mode = CHAIN_FLAT;
358
359 else if (!strncmp(tok, "fractal", strlen(arg)))
360 callchain_param.mode = CHAIN_GRAPH_REL;
361
b1a88349
FW
362 else if (!strncmp(tok, "none", strlen(arg))) {
363 callchain_param.mode = CHAIN_NONE;
b7ae11b3 364 symbol_conf.use_callchain = false;
b1a88349
FW
365
366 return 0;
367 }
368
4eb3e478
FW
369 else
370 return -1;
371
c20ab37e
FW
372 /* get the min percentage */
373 tok = strtok(NULL, ",");
374 if (!tok)
805d127d 375 goto setup;
c20ab37e 376
232a5c94 377 tok2 = strtok(NULL, ",");
805d127d 378 callchain_param.min_percent = strtod(tok, &endptr);
c20ab37e
FW
379 if (tok == endptr)
380 return -1;
381
232a5c94
ACM
382 if (tok2)
383 callchain_param.print_limit = strtod(tok2, &endptr);
805d127d 384setup:
16537f13 385 if (callchain_register_param(&callchain_param) < 0) {
805d127d
FW
386 fprintf(stderr, "Can't register callchain params\n");
387 return -1;
388 }
4eb3e478
FW
389 return 0;
390}
391
0422a4fc 392static const char * const report_usage[] = {
53cb8bc2
IM
393 "perf report [<options>] <command>",
394 NULL
395};
396
397static const struct option options[] = {
398 OPT_STRING('i', "input", &input_name, "file",
399 "input file name"),
c0555642 400 OPT_INCR('v', "verbose", &verbose,
815e777f 401 "be more verbose (show symbol address, etc)"),
97b07b69
IM
402 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
403 "dump raw trace in ASCII"),
b32d133a
ACM
404 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
405 "file", "vmlinux pathname"),
b226a5a7
DA
406 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
407 "file", "kallsyms pathname"),
fa6963b2 408 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
b32d133a 409 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 410 "load module symbols - WARNING: use only with -k and LIVE kernel"),
d599db3f 411 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
e3d7e183 412 "Show a column with the number of samples"),
8d513270
BG
413 OPT_BOOLEAN('T', "threads", &show_threads,
414 "Show per-thread event counters"),
9f866697
BG
415 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
416 "pretty printing style key: normal raw"),
8b9e74eb
ACM
417 OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"),
418 OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"),
63299f05 419 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
b25bcf2f 420 "sort by key(s): pid, comm, dso, symbol, parent"),
a1645ce1
ZY
421 OPT_BOOLEAN(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
422 "Show sample percentage for different cpu modes"),
b25bcf2f
IM
423 OPT_STRING('p', "parent", &parent_pattern, "regex",
424 "regex filter to identify parent, see: '--sort parent'"),
d599db3f 425 OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
b8e6d829 426 "Only display entries with parent-match"),
1483b19f 427 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
e157eb83 428 "Display callchains using output_type (graph, flat, fractal, or none) and min percent threshold. "
1483b19f 429 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
655000e7 430 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
25903407 431 "only consider symbols in these dsos"),
655000e7 432 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
cc8b88b1 433 "only consider symbols in these comms"),
655000e7 434 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
7bec7a91 435 "only consider these symbols"),
655000e7 436 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
52d422de
ACM
437 "width[,width...]",
438 "don't try to adjust column width, use these fixed values"),
c410a338 439 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
52d422de
ACM
440 "separator for columns, no spaces will be added between "
441 "columns '.' is reserved."),
71289be7
ACM
442 OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved,
443 "Only display entries resolved to a symbol"),
ec5761ea
DA
444 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
445 "Look for files with symbols relative to this directory"),
53cb8bc2
IM
446 OPT_END()
447};
448
f37a291c 449int cmd_report(int argc, const char **argv, const char *prefix __used)
53cb8bc2 450{
655000e7
ACM
451 argc = parse_options(argc, argv, options, report_usage, 0);
452
8b9e74eb
ACM
453 if (use_stdio)
454 use_browser = 0;
455 else if (use_tui)
456 use_browser = 1;
457
46656ac7 458 if (strcmp(input_name, "-") != 0)
229ade9b 459 setup_browser(true);
8b9e74eb
ACM
460 else
461 use_browser = 0;
ef7b93a1
ACM
462 /*
463 * Only in the newt browser we are doing integrated annotation,
464 * so don't allocate extra space that won't be used in the stdio
465 * implementation.
466 */
80d50cae 467 if (use_browser > 0) {
78f7defe 468 symbol_conf.priv_size = sizeof(struct annotation);
0849327d 469 annotate_init = symbol__annotate_init;
80d50cae
ACM
470 /*
471 * For searching by name on the "Browse map details".
472 * providing it only in verbose mode not to bloat too
473 * much struct symbol.
474 */
475 if (verbose) {
476 /*
477 * XXX: Need to provide a less kludgy way to ask for
478 * more space per symbol, the u32 is for the index on
479 * the ui browser.
480 * See symbol__browser_index.
481 */
482 symbol_conf.priv_size += sizeof(u32);
483 symbol_conf.sort_by_name = true;
484 }
485 }
655000e7 486
75be6cf4 487 if (symbol__init() < 0)
b32d133a 488 return -1;
53cb8bc2 489
c8829c7a 490 setup_sorting(report_usage, options);
1aa16738 491
021191b3 492 if (parent_pattern != default_parent_pattern) {
2aefa4f7
ACM
493 if (sort_dimension__add("parent") < 0)
494 return -1;
021191b3
ACM
495 sort_parent.elide = 1;
496 } else
d599db3f 497 symbol_conf.exclude_other = false;
b8e6d829 498
edc52dea
IM
499 /*
500 * Any (unrecognized) arguments left?
501 */
502 if (argc)
503 usage_with_options(report_usage, options);
504
655000e7
ACM
505 sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
506 sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
507 sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
25903407 508
53cb8bc2
IM
509 return __cmd_report();
510}