]>
Commit | Line | Data |
---|---|---|
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 | ||
8fc0321f | 12 | #include "util/color.h" |
5da50258 | 13 | #include <linux/list.h> |
a930d2c0 | 14 | #include "util/cache.h" |
43cbcd8a | 15 | #include <linux/rbtree.h> |
a2928c42 | 16 | #include "util/symbol.h" |
a0055ae2 | 17 | #include "util/string.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" |
7c6a1c65 | 24 | #include "util/header.h" |
94c744b6 | 25 | #include "util/session.h" |
53cb8bc2 IM |
26 | |
27 | #include "util/parse-options.h" | |
28 | #include "util/parse-events.h" | |
29 | ||
6baa0a5a | 30 | #include "util/thread.h" |
dd68ada2 | 31 | #include "util/sort.h" |
3d1d07ec | 32 | #include "util/hist.h" |
6baa0a5a | 33 | |
23ac9cbe | 34 | static char const *input_name = "perf.data"; |
bd74137e | 35 | |
fa6963b2 | 36 | static int force; |
97b07b69 | 37 | |
8d513270 BG |
38 | static int show_threads; |
39 | static struct perf_read_values show_threads_values; | |
40 | ||
9f866697 BG |
41 | static char default_pretty_printing_style[] = "normal"; |
42 | static char *pretty_printing_style = default_pretty_printing_style; | |
43 | ||
805d127d FW |
44 | static char callchain_default_opt[] = "fractal,0.5"; |
45 | ||
4e4f06e4 ACM |
46 | static int perf_session__add_hist_entry(struct perf_session *self, |
47 | struct addr_location *al, | |
48 | struct ip_callchain *chain, u64 count) | |
8fa66bdc | 49 | { |
9735abf1 ACM |
50 | struct symbol **syms = NULL, *parent = NULL; |
51 | bool hit; | |
e7fb08b1 | 52 | struct hist_entry *he; |
e7fb08b1 | 53 | |
d599db3f | 54 | if ((sort__has_parent || symbol_conf.use_callchain) && chain) |
a328626b ACM |
55 | syms = perf_session__resolve_callchain(self, al->thread, |
56 | chain, &parent); | |
4e4f06e4 | 57 | he = __perf_session__add_hist_entry(self, al, parent, count, &hit); |
9735abf1 ACM |
58 | if (he == NULL) |
59 | return -ENOMEM; | |
e7fb08b1 | 60 | |
9735abf1 ACM |
61 | if (hit) |
62 | he->count += count; | |
e7fb08b1 | 63 | |
d599db3f | 64 | if (symbol_conf.use_callchain) { |
9735abf1 ACM |
65 | if (!hit) |
66 | callchain_init(&he->callchain); | |
4424961a FW |
67 | append_chain(&he->callchain, chain, syms); |
68 | free(syms); | |
f55c5552 | 69 | } |
e7fb08b1 PZ |
70 | |
71 | return 0; | |
8fa66bdc ACM |
72 | } |
73 | ||
2a0a50fe | 74 | static int validate_chain(struct ip_callchain *chain, event_t *event) |
7522060c IM |
75 | { |
76 | unsigned int chain_size; | |
77 | ||
7522060c IM |
78 | chain_size = event->header.size; |
79 | chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event; | |
80 | ||
9cffa8d5 | 81 | if (chain->nr*sizeof(u64) > chain_size) |
7522060c IM |
82 | return -1; |
83 | ||
84 | return 0; | |
85 | } | |
86 | ||
b3165f41 | 87 | static int process_sample_event(event_t *event, struct perf_session *session) |
75051724 | 88 | { |
c410a338 | 89 | struct sample_data data = { .period = 1, }; |
1ed091c4 | 90 | struct addr_location al; |
180f95e2 | 91 | |
c019879b | 92 | event__parse_sample(event, session->sample_type, &data); |
ea1900e5 | 93 | |
62daacb5 | 94 | dump_printf("(IP, %d): %d/%d: %p period: %Ld\n", |
75051724 | 95 | event->header.misc, |
180f95e2 OH |
96 | data.pid, data.tid, |
97 | (void *)(long)data.ip, | |
98 | (long long)data.period); | |
75051724 | 99 | |
c019879b | 100 | if (session->sample_type & PERF_SAMPLE_CALLCHAIN) { |
f37a291c | 101 | unsigned int i; |
3efa1cc9 | 102 | |
180f95e2 | 103 | dump_printf("... chain: nr:%Lu\n", data.callchain->nr); |
3efa1cc9 | 104 | |
180f95e2 | 105 | if (validate_chain(data.callchain, event) < 0) { |
6beba7ad ACM |
106 | pr_debug("call-chain problem with event, " |
107 | "skipping it.\n"); | |
7522060c IM |
108 | return 0; |
109 | } | |
110 | ||
111 | if (dump_trace) { | |
180f95e2 OH |
112 | for (i = 0; i < data.callchain->nr; i++) |
113 | dump_printf("..... %2d: %016Lx\n", | |
114 | i, data.callchain->ips[i]); | |
3efa1cc9 IM |
115 | } |
116 | } | |
117 | ||
c410a338 ACM |
118 | if (event__preprocess_sample(event, session, &al, NULL) < 0) { |
119 | fprintf(stderr, "problem processing %d event, skipping it.\n", | |
75051724 IM |
120 | event->header.type); |
121 | return -1; | |
122 | } | |
e7fb08b1 | 123 | |
c410a338 | 124 | if (al.filtered) |
ec218fc4 | 125 | return 0; |
7bec7a91 | 126 | |
4e4f06e4 | 127 | if (perf_session__add_hist_entry(session, &al, data.callchain, data.period)) { |
6beba7ad | 128 | pr_debug("problem incrementing symbol count, skipping event\n"); |
ec218fc4 | 129 | return -1; |
8fa66bdc | 130 | } |
ec218fc4 | 131 | |
f823e441 | 132 | session->events_stats.total += data.period; |
75051724 IM |
133 | return 0; |
134 | } | |
3502973d | 135 | |
d8f66248 | 136 | static int process_read_event(event_t *event, struct perf_session *session __used) |
e9ea2fde | 137 | { |
cdd6c482 | 138 | struct perf_event_attr *attr; |
0d3a5c88 | 139 | |
94c744b6 | 140 | attr = perf_header__find_attr(event->read.id, &session->header); |
8f18aec5 | 141 | |
8d513270 | 142 | if (show_threads) { |
83a0944f | 143 | const char *name = attr ? __event_name(attr->type, attr->config) |
8d513270 BG |
144 | : "unknown"; |
145 | perf_read_values_add_value(&show_threads_values, | |
146 | event->read.pid, event->read.tid, | |
147 | event->read.id, | |
148 | name, | |
149 | event->read.value); | |
150 | } | |
151 | ||
62daacb5 ACM |
152 | dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid, |
153 | attr ? __event_name(attr->type, attr->config) : "FAIL", | |
154 | event->read.value); | |
e9ea2fde PZ |
155 | |
156 | return 0; | |
157 | } | |
158 | ||
c019879b | 159 | static int sample_type_check(struct perf_session *session) |
d80d338d | 160 | { |
c019879b | 161 | if (!(session->sample_type & PERF_SAMPLE_CALLCHAIN)) { |
91b4eaea FW |
162 | if (sort__has_parent) { |
163 | fprintf(stderr, "selected --sort parent, but no" | |
164 | " callchain data. Did you call" | |
165 | " perf record without -g?\n"); | |
016e92fb | 166 | return -1; |
91b4eaea | 167 | } |
d599db3f | 168 | if (symbol_conf.use_callchain) { |
6ede59c4 | 169 | fprintf(stderr, "selected -g but no callchain data." |
91b4eaea FW |
170 | " Did you call perf record without" |
171 | " -g?\n"); | |
016e92fb | 172 | return -1; |
91b4eaea | 173 | } |
d599db3f ACM |
174 | } else if (callchain_param.mode != CHAIN_NONE && !symbol_conf.use_callchain) { |
175 | symbol_conf.use_callchain = true; | |
b1a88349 FW |
176 | if (register_callchain_param(&callchain_param) < 0) { |
177 | fprintf(stderr, "Can't register callchain" | |
178 | " params\n"); | |
016e92fb | 179 | return -1; |
b1a88349 | 180 | } |
f5970550 PZ |
181 | } |
182 | ||
016e92fb FW |
183 | return 0; |
184 | } | |
6142f9ec | 185 | |
301a0b02 | 186 | static struct perf_event_ops event_ops = { |
016e92fb | 187 | .process_sample_event = process_sample_event, |
62daacb5 | 188 | .process_mmap_event = event__process_mmap, |
125c4fad | 189 | .process_comm_event = event__process_comm, |
62daacb5 ACM |
190 | .process_exit_event = event__process_task, |
191 | .process_fork_event = event__process_task, | |
192 | .process_lost_event = event__process_lost, | |
016e92fb FW |
193 | .process_read_event = process_read_event, |
194 | .sample_type_check = sample_type_check, | |
195 | }; | |
6142f9ec | 196 | |
6142f9ec | 197 | |
016e92fb FW |
198 | static int __cmd_report(void) |
199 | { | |
016e92fb | 200 | int ret; |
d8f66248 | 201 | struct perf_session *session; |
8fa66bdc | 202 | |
75be6cf4 | 203 | session = perf_session__new(input_name, O_RDONLY, force); |
94c744b6 ACM |
204 | if (session == NULL) |
205 | return -ENOMEM; | |
206 | ||
016e92fb FW |
207 | if (show_threads) |
208 | perf_read_values_init(&show_threads_values); | |
f5970550 | 209 | |
ec913369 | 210 | ret = perf_session__process_events(session, &event_ops); |
016e92fb | 211 | if (ret) |
94c744b6 | 212 | goto out_delete; |
97b07b69 | 213 | |
62daacb5 ACM |
214 | if (dump_trace) { |
215 | event__print_totals(); | |
94c744b6 | 216 | goto out_delete; |
62daacb5 | 217 | } |
97b07b69 | 218 | |
da21d1b5 | 219 | if (verbose > 3) |
b3165f41 | 220 | perf_session__fprintf(session, stdout); |
9ac99545 | 221 | |
da21d1b5 | 222 | if (verbose > 2) |
16f762a2 | 223 | dsos__fprintf(stdout); |
16f762a2 | 224 | |
4e4f06e4 | 225 | perf_session__collapse_resort(session); |
f823e441 | 226 | perf_session__output_resort(session, session->events_stats.total); |
3e6055ab | 227 | fprintf(stdout, "# Samples: %ld\n#\n", session->events_stats.total); |
c351c281 | 228 | perf_session__fprintf_hists(session, NULL, false, stdout); |
3e6055ab ACM |
229 | if (sort_order == default_sort_order && |
230 | parent_pattern == default_parent_pattern) | |
231 | fprintf(stdout, "#\n# (For a higher level overview, try: perf report --sort comm,dso)\n#\n"); | |
232 | ||
d599db3f ACM |
233 | if (show_threads) { |
234 | bool raw_printing_style = !strcmp(pretty_printing_style, "raw"); | |
235 | perf_read_values_display(stdout, &show_threads_values, | |
236 | raw_printing_style); | |
8d513270 | 237 | perf_read_values_destroy(&show_threads_values); |
d599db3f | 238 | } |
94c744b6 ACM |
239 | out_delete: |
240 | perf_session__delete(session); | |
016e92fb | 241 | return ret; |
8fa66bdc ACM |
242 | } |
243 | ||
4eb3e478 FW |
244 | static int |
245 | parse_callchain_opt(const struct option *opt __used, const char *arg, | |
246 | int unset __used) | |
247 | { | |
c20ab37e FW |
248 | char *tok; |
249 | char *endptr; | |
250 | ||
d599db3f | 251 | symbol_conf.use_callchain = true; |
4eb3e478 FW |
252 | |
253 | if (!arg) | |
254 | return 0; | |
255 | ||
c20ab37e FW |
256 | tok = strtok((char *)arg, ","); |
257 | if (!tok) | |
258 | return -1; | |
259 | ||
260 | /* get the output mode */ | |
261 | if (!strncmp(tok, "graph", strlen(arg))) | |
805d127d | 262 | callchain_param.mode = CHAIN_GRAPH_ABS; |
4eb3e478 | 263 | |
c20ab37e | 264 | else if (!strncmp(tok, "flat", strlen(arg))) |
805d127d FW |
265 | callchain_param.mode = CHAIN_FLAT; |
266 | ||
267 | else if (!strncmp(tok, "fractal", strlen(arg))) | |
268 | callchain_param.mode = CHAIN_GRAPH_REL; | |
269 | ||
b1a88349 FW |
270 | else if (!strncmp(tok, "none", strlen(arg))) { |
271 | callchain_param.mode = CHAIN_NONE; | |
d599db3f | 272 | symbol_conf.use_callchain = true; |
b1a88349 FW |
273 | |
274 | return 0; | |
275 | } | |
276 | ||
4eb3e478 FW |
277 | else |
278 | return -1; | |
279 | ||
c20ab37e FW |
280 | /* get the min percentage */ |
281 | tok = strtok(NULL, ","); | |
282 | if (!tok) | |
805d127d | 283 | goto setup; |
c20ab37e | 284 | |
805d127d | 285 | callchain_param.min_percent = strtod(tok, &endptr); |
c20ab37e FW |
286 | if (tok == endptr) |
287 | return -1; | |
288 | ||
805d127d FW |
289 | setup: |
290 | if (register_callchain_param(&callchain_param) < 0) { | |
291 | fprintf(stderr, "Can't register callchain params\n"); | |
292 | return -1; | |
293 | } | |
4eb3e478 FW |
294 | return 0; |
295 | } | |
296 | ||
dd68ada2 JK |
297 | //static const char * const report_usage[] = { |
298 | const char * const report_usage[] = { | |
53cb8bc2 IM |
299 | "perf report [<options>] <command>", |
300 | NULL | |
301 | }; | |
302 | ||
303 | static const struct option options[] = { | |
304 | OPT_STRING('i', "input", &input_name, "file", | |
305 | "input file name"), | |
815e777f ACM |
306 | OPT_BOOLEAN('v', "verbose", &verbose, |
307 | "be more verbose (show symbol address, etc)"), | |
97b07b69 IM |
308 | OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, |
309 | "dump raw trace in ASCII"), | |
b32d133a ACM |
310 | OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, |
311 | "file", "vmlinux pathname"), | |
fa6963b2 | 312 | OPT_BOOLEAN('f', "force", &force, "don't complain, do it"), |
b32d133a | 313 | OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, |
42976487 | 314 | "load module symbols - WARNING: use only with -k and LIVE kernel"), |
d599db3f | 315 | OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples, |
e3d7e183 | 316 | "Show a column with the number of samples"), |
8d513270 BG |
317 | OPT_BOOLEAN('T', "threads", &show_threads, |
318 | "Show per-thread event counters"), | |
9f866697 BG |
319 | OPT_STRING(0, "pretty", &pretty_printing_style, "key", |
320 | "pretty printing style key: normal raw"), | |
63299f05 | 321 | OPT_STRING('s', "sort", &sort_order, "key[,key2...]", |
b25bcf2f | 322 | "sort by key(s): pid, comm, dso, symbol, parent"), |
ec913369 | 323 | OPT_BOOLEAN('P', "full-paths", &event_ops.full_paths, |
b78c07d4 | 324 | "Don't shorten the pathnames taking into account the cwd"), |
b25bcf2f IM |
325 | OPT_STRING('p', "parent", &parent_pattern, "regex", |
326 | "regex filter to identify parent, see: '--sort parent'"), | |
d599db3f | 327 | OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other, |
b8e6d829 | 328 | "Only display entries with parent-match"), |
1483b19f | 329 | OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent", |
c20ab37e | 330 | "Display callchains using output_type and min percent threshold. " |
1483b19f | 331 | "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt), |
655000e7 | 332 | OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]", |
25903407 | 333 | "only consider symbols in these dsos"), |
655000e7 | 334 | OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]", |
cc8b88b1 | 335 | "only consider symbols in these comms"), |
655000e7 | 336 | OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]", |
7bec7a91 | 337 | "only consider these symbols"), |
655000e7 | 338 | OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str, |
52d422de ACM |
339 | "width[,width...]", |
340 | "don't try to adjust column width, use these fixed values"), | |
c410a338 | 341 | OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator", |
52d422de ACM |
342 | "separator for columns, no spaces will be added between " |
343 | "columns '.' is reserved."), | |
53cb8bc2 IM |
344 | OPT_END() |
345 | }; | |
346 | ||
f37a291c | 347 | int cmd_report(int argc, const char **argv, const char *prefix __used) |
53cb8bc2 | 348 | { |
655000e7 ACM |
349 | argc = parse_options(argc, argv, options, report_usage, 0); |
350 | ||
351 | setup_pager(); | |
352 | ||
75be6cf4 | 353 | if (symbol__init() < 0) |
b32d133a | 354 | return -1; |
53cb8bc2 | 355 | |
c8829c7a | 356 | setup_sorting(report_usage, options); |
1aa16738 | 357 | |
021191b3 | 358 | if (parent_pattern != default_parent_pattern) { |
b8e6d829 | 359 | sort_dimension__add("parent"); |
021191b3 ACM |
360 | sort_parent.elide = 1; |
361 | } else | |
d599db3f | 362 | symbol_conf.exclude_other = false; |
b8e6d829 | 363 | |
edc52dea IM |
364 | /* |
365 | * Any (unrecognized) arguments left? | |
366 | */ | |
367 | if (argc) | |
368 | usage_with_options(report_usage, options); | |
369 | ||
655000e7 ACM |
370 | sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout); |
371 | sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout); | |
372 | sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout); | |
25903407 | 373 | |
53cb8bc2 IM |
374 | return __cmd_report(); |
375 | } |