perf report: Introduce special handling for pipe input
[deliverable/linux.git] / tools / perf / builtin-report.c
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 */
8 #include "builtin.h"
9
10 #include "util/util.h"
11
12 #include "util/color.h"
13 #include <linux/list.h>
14 #include "util/cache.h"
15 #include <linux/rbtree.h>
16 #include "util/symbol.h"
17 #include "util/callchain.h"
18 #include "util/strlist.h"
19 #include "util/values.h"
20
21 #include "perf.h"
22 #include "util/debug.h"
23 #include "util/header.h"
24 #include "util/session.h"
25
26 #include "util/parse-options.h"
27 #include "util/parse-events.h"
28
29 #include "util/thread.h"
30 #include "util/sort.h"
31 #include "util/hist.h"
32
33 static char const *input_name = "perf.data";
34
35 static bool force;
36 static bool hide_unresolved;
37 static bool dont_use_callchains;
38
39 static bool show_threads;
40 static struct perf_read_values show_threads_values;
41
42 static char default_pretty_printing_style[] = "normal";
43 static char *pretty_printing_style = default_pretty_printing_style;
44
45 static char callchain_default_opt[] = "fractal,0.5";
46
47 static struct event_stat_id *get_stats(struct perf_session *self,
48 u64 event_stream, u32 type, u64 config)
49 {
50 struct rb_node **p = &self->stats_by_id.rb_node;
51 struct rb_node *parent = NULL;
52 struct event_stat_id *iter, *new;
53
54 while (*p != NULL) {
55 parent = *p;
56 iter = rb_entry(parent, struct event_stat_id, rb_node);
57 if (iter->config == config)
58 return iter;
59
60
61 if (config > iter->config)
62 p = &(*p)->rb_right;
63 else
64 p = &(*p)->rb_left;
65 }
66
67 new = malloc(sizeof(struct event_stat_id));
68 if (new == NULL)
69 return NULL;
70 memset(new, 0, sizeof(struct event_stat_id));
71 new->event_stream = event_stream;
72 new->config = config;
73 new->type = type;
74 rb_link_node(&new->rb_node, parent, p);
75 rb_insert_color(&new->rb_node, &self->stats_by_id);
76 return new;
77 }
78
79 static int perf_session__add_hist_entry(struct perf_session *self,
80 struct addr_location *al,
81 struct sample_data *data)
82 {
83 struct map_symbol *syms = NULL;
84 struct symbol *parent = NULL;
85 bool hit;
86 int err;
87 struct hist_entry *he;
88 struct event_stat_id *stats;
89 struct perf_event_attr *attr;
90
91 if ((sort__has_parent || symbol_conf.use_callchain) && data->callchain) {
92 syms = perf_session__resolve_callchain(self, al->thread,
93 data->callchain, &parent);
94 if (syms == NULL)
95 return -ENOMEM;
96 }
97
98 attr = perf_header__find_attr(data->id, &self->header);
99 if (attr)
100 stats = get_stats(self, data->id, attr->type, attr->config);
101 else
102 stats = get_stats(self, data->id, 0, 0);
103 if (stats == NULL)
104 return -ENOMEM;
105 he = __perf_session__add_hist_entry(&stats->hists, al, parent,
106 data->period, &hit);
107 if (he == NULL)
108 return -ENOMEM;
109
110 if (hit)
111 he->count += data->period;
112
113 if (symbol_conf.use_callchain) {
114 if (!hit)
115 callchain_init(he->callchain);
116 err = append_chain(he->callchain, data->callchain, syms);
117 free(syms);
118
119 if (err)
120 return err;
121 }
122
123 return 0;
124 }
125
126 static int validate_chain(struct ip_callchain *chain, event_t *event)
127 {
128 unsigned int chain_size;
129
130 chain_size = event->header.size;
131 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
132
133 if (chain->nr*sizeof(u64) > chain_size)
134 return -1;
135
136 return 0;
137 }
138
139 static int add_event_total(struct perf_session *session,
140 struct sample_data *data,
141 struct perf_event_attr *attr)
142 {
143 struct event_stat_id *stats;
144
145 if (attr)
146 stats = get_stats(session, data->id, attr->type, attr->config);
147 else
148 stats = get_stats(session, data->id, 0, 0);
149
150 if (!stats)
151 return -ENOMEM;
152
153 stats->stats.total += data->period;
154 session->events_stats.total += data->period;
155 return 0;
156 }
157
158 static int process_sample_event(event_t *event, struct perf_session *session)
159 {
160 struct sample_data data = { .period = 1, };
161 struct addr_location al;
162 struct perf_event_attr *attr;
163
164 event__parse_sample(event, session->sample_type, &data);
165
166 dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
167 data.pid, data.tid, data.ip, data.period);
168
169 if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
170 unsigned int i;
171
172 dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
173
174 if (validate_chain(data.callchain, event) < 0) {
175 pr_debug("call-chain problem with event, "
176 "skipping it.\n");
177 return 0;
178 }
179
180 if (dump_trace) {
181 for (i = 0; i < data.callchain->nr; i++)
182 dump_printf("..... %2d: %016Lx\n",
183 i, data.callchain->ips[i]);
184 }
185 }
186
187 if (event__preprocess_sample(event, session, &al, NULL) < 0) {
188 fprintf(stderr, "problem processing %d event, skipping it.\n",
189 event->header.type);
190 return -1;
191 }
192
193 if (al.filtered || (hide_unresolved && al.sym == NULL))
194 return 0;
195
196 if (perf_session__add_hist_entry(session, &al, &data)) {
197 pr_debug("problem incrementing symbol count, skipping event\n");
198 return -1;
199 }
200
201 attr = perf_header__find_attr(data.id, &session->header);
202
203 if (add_event_total(session, &data, attr)) {
204 pr_debug("problem adding event count\n");
205 return -1;
206 }
207
208 return 0;
209 }
210
211 static int process_read_event(event_t *event, struct perf_session *session __used)
212 {
213 struct perf_event_attr *attr;
214
215 attr = perf_header__find_attr(event->read.id, &session->header);
216
217 if (show_threads) {
218 const char *name = attr ? __event_name(attr->type, attr->config)
219 : "unknown";
220 perf_read_values_add_value(&show_threads_values,
221 event->read.pid, event->read.tid,
222 event->read.id,
223 name,
224 event->read.value);
225 }
226
227 dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
228 attr ? __event_name(attr->type, attr->config) : "FAIL",
229 event->read.value);
230
231 return 0;
232 }
233
234 static int perf_session__setup_sample_type(struct perf_session *self)
235 {
236 if (!(self->sample_type & PERF_SAMPLE_CALLCHAIN)) {
237 if (sort__has_parent) {
238 fprintf(stderr, "selected --sort parent, but no"
239 " callchain data. Did you call"
240 " perf record without -g?\n");
241 return -EINVAL;
242 }
243 if (symbol_conf.use_callchain) {
244 fprintf(stderr, "selected -g but no callchain data."
245 " Did you call perf record without"
246 " -g?\n");
247 return -1;
248 }
249 } else if (!dont_use_callchains && callchain_param.mode != CHAIN_NONE &&
250 !symbol_conf.use_callchain) {
251 symbol_conf.use_callchain = true;
252 if (register_callchain_param(&callchain_param) < 0) {
253 fprintf(stderr, "Can't register callchain"
254 " params\n");
255 return -EINVAL;
256 }
257 }
258
259 return 0;
260 }
261
262 static struct perf_event_ops event_ops = {
263 .sample = process_sample_event,
264 .mmap = event__process_mmap,
265 .comm = event__process_comm,
266 .exit = event__process_task,
267 .fork = event__process_task,
268 .lost = event__process_lost,
269 .read = process_read_event,
270 };
271
272 extern volatile int session_done;
273
274 static void sig_handler(int sig __attribute__((__unused__)))
275 {
276 session_done = 1;
277 }
278
279 static int __cmd_report(void)
280 {
281 int ret = -EINVAL;
282 struct perf_session *session;
283 struct rb_node *next;
284 const char *help = "For a higher level overview, try: perf report --sort comm,dso";
285
286 signal(SIGINT, sig_handler);
287
288 session = perf_session__new(input_name, O_RDONLY, force);
289 if (session == NULL)
290 return -ENOMEM;
291
292 if (show_threads)
293 perf_read_values_init(&show_threads_values);
294
295 ret = perf_session__setup_sample_type(session);
296 if (ret)
297 goto out_delete;
298
299 ret = perf_session__process_events(session, &event_ops);
300 if (ret)
301 goto out_delete;
302
303 if (dump_trace) {
304 event__print_totals();
305 goto out_delete;
306 }
307
308 if (verbose > 3)
309 perf_session__fprintf(session, stdout);
310
311 if (verbose > 2)
312 dsos__fprintf(stdout);
313
314 next = rb_first(&session->stats_by_id);
315 while (next) {
316 struct event_stat_id *stats;
317 u64 nr_hists;
318
319 stats = rb_entry(next, struct event_stat_id, rb_node);
320 perf_session__collapse_resort(&stats->hists);
321 nr_hists = perf_session__output_resort(&stats->hists,
322 stats->stats.total);
323 if (use_browser)
324 perf_session__browse_hists(&stats->hists, nr_hists,
325 stats->stats.total, help,
326 input_name);
327 else {
328 if (rb_first(&session->stats_by_id) ==
329 rb_last(&session->stats_by_id))
330 fprintf(stdout, "# Samples: %Ld\n#\n",
331 stats->stats.total);
332 else
333 fprintf(stdout, "# Samples: %Ld %s\n#\n",
334 stats->stats.total,
335 __event_name(stats->type, stats->config));
336
337 perf_session__fprintf_hists(&stats->hists, NULL, false, stdout,
338 stats->stats.total);
339 fprintf(stdout, "\n\n");
340 }
341
342 next = rb_next(&stats->rb_node);
343 }
344
345 if (!use_browser && sort_order == default_sort_order &&
346 parent_pattern == default_parent_pattern) {
347 fprintf(stdout, "#\n# (%s)\n#\n", help);
348
349 if (show_threads) {
350 bool style = !strcmp(pretty_printing_style, "raw");
351 perf_read_values_display(stdout, &show_threads_values,
352 style);
353 perf_read_values_destroy(&show_threads_values);
354 }
355 }
356 out_delete:
357 perf_session__delete(session);
358 return ret;
359 }
360
361 static int
362 parse_callchain_opt(const struct option *opt __used, const char *arg,
363 int unset)
364 {
365 char *tok;
366 char *endptr;
367
368 /*
369 * --no-call-graph
370 */
371 if (unset) {
372 dont_use_callchains = true;
373 return 0;
374 }
375
376 symbol_conf.use_callchain = true;
377
378 if (!arg)
379 return 0;
380
381 tok = strtok((char *)arg, ",");
382 if (!tok)
383 return -1;
384
385 /* get the output mode */
386 if (!strncmp(tok, "graph", strlen(arg)))
387 callchain_param.mode = CHAIN_GRAPH_ABS;
388
389 else if (!strncmp(tok, "flat", strlen(arg)))
390 callchain_param.mode = CHAIN_FLAT;
391
392 else if (!strncmp(tok, "fractal", strlen(arg)))
393 callchain_param.mode = CHAIN_GRAPH_REL;
394
395 else if (!strncmp(tok, "none", strlen(arg))) {
396 callchain_param.mode = CHAIN_NONE;
397 symbol_conf.use_callchain = false;
398
399 return 0;
400 }
401
402 else
403 return -1;
404
405 /* get the min percentage */
406 tok = strtok(NULL, ",");
407 if (!tok)
408 goto setup;
409
410 callchain_param.min_percent = strtod(tok, &endptr);
411 if (tok == endptr)
412 return -1;
413
414 setup:
415 if (register_callchain_param(&callchain_param) < 0) {
416 fprintf(stderr, "Can't register callchain params\n");
417 return -1;
418 }
419 return 0;
420 }
421
422 static const char * const report_usage[] = {
423 "perf report [<options>] <command>",
424 NULL
425 };
426
427 static const struct option options[] = {
428 OPT_STRING('i', "input", &input_name, "file",
429 "input file name"),
430 OPT_INCR('v', "verbose", &verbose,
431 "be more verbose (show symbol address, etc)"),
432 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
433 "dump raw trace in ASCII"),
434 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
435 "file", "vmlinux pathname"),
436 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
437 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
438 "load module symbols - WARNING: use only with -k and LIVE kernel"),
439 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
440 "Show a column with the number of samples"),
441 OPT_BOOLEAN('T', "threads", &show_threads,
442 "Show per-thread event counters"),
443 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
444 "pretty printing style key: normal raw"),
445 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
446 "sort by key(s): pid, comm, dso, symbol, parent"),
447 OPT_BOOLEAN('P', "full-paths", &symbol_conf.full_paths,
448 "Don't shorten the pathnames taking into account the cwd"),
449 OPT_STRING('p', "parent", &parent_pattern, "regex",
450 "regex filter to identify parent, see: '--sort parent'"),
451 OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
452 "Only display entries with parent-match"),
453 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
454 "Display callchains using output_type and min percent threshold. "
455 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
456 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
457 "only consider symbols in these dsos"),
458 OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
459 "only consider symbols in these comms"),
460 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
461 "only consider these symbols"),
462 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
463 "width[,width...]",
464 "don't try to adjust column width, use these fixed values"),
465 OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
466 "separator for columns, no spaces will be added between "
467 "columns '.' is reserved."),
468 OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved,
469 "Only display entries resolved to a symbol"),
470 OPT_END()
471 };
472
473 int cmd_report(int argc, const char **argv, const char *prefix __used)
474 {
475 argc = parse_options(argc, argv, options, report_usage, 0);
476
477 if (strcmp(input_name, "-") != 0)
478 setup_browser();
479
480 if (symbol__init() < 0)
481 return -1;
482
483 setup_sorting(report_usage, options);
484
485 if (parent_pattern != default_parent_pattern) {
486 if (sort_dimension__add("parent") < 0)
487 return -1;
488 sort_parent.elide = 1;
489 } else
490 symbol_conf.exclude_other = false;
491
492 /*
493 * Any (unrecognized) arguments left?
494 */
495 if (argc)
496 usage_with_options(report_usage, options);
497
498 sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
499 sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
500 sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
501
502 return __cmd_report();
503 }
This page took 0.04096 seconds and 6 git commands to generate.