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