]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - tools/perf/builtin-report.c
perf tools: No need for three rb_trees for sorting hist entries
[mirror_ubuntu-zesty-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
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 34static char const *input_name = "perf.data";
bd74137e 35
52d422de
ACM
36static char *dso_list_str, *comm_list_str, *sym_list_str,
37 *col_width_list_str;
7bec7a91 38static struct strlist *dso_list, *comm_list, *sym_list;
bd74137e 39
fa6963b2 40static int force;
8fa66bdc 41
e3d7e183 42static int show_nr_samples;
97b07b69 43
8d513270
BG
44static int show_threads;
45static struct perf_read_values show_threads_values;
46
9f866697
BG
47static char default_pretty_printing_style[] = "normal";
48static char *pretty_printing_style = default_pretty_printing_style;
49
b8e6d829 50static int exclude_other = 1;
be903885 51
805d127d
FW
52static char callchain_default_opt[] = "fractal,0.5";
53
e6e18ec7
PZ
54static u64 sample_type;
55
b32d133a
ACM
56struct symbol_conf symbol_conf;
57
a4fb581b
FW
58
59static size_t
60callchain__fprintf_left_margin(FILE *fp, int left_margin)
61{
62 int i;
63 int ret;
64
65 ret = fprintf(fp, " ");
66
67 for (i = 0; i < left_margin; i++)
68 ret += fprintf(fp, " ");
69
70 return ret;
71}
72
73static size_t ipchain__fprintf_graph_line(FILE *fp, int depth, int depth_mask,
74 int left_margin)
4eb3e478
FW
75{
76 int i;
77 size_t ret = 0;
78
a4fb581b 79 ret += callchain__fprintf_left_margin(fp, left_margin);
4eb3e478
FW
80
81 for (i = 0; i < depth; i++)
82 if (depth_mask & (1 << i))
83 ret += fprintf(fp, "| ");
84 else
85 ret += fprintf(fp, " ");
86
87 ret += fprintf(fp, "\n");
88
89 return ret;
90}
f55c5552 91static size_t
4eb3e478
FW
92ipchain__fprintf_graph(FILE *fp, struct callchain_list *chain, int depth,
93 int depth_mask, int count, u64 total_samples,
a4fb581b 94 int hits, int left_margin)
4eb3e478
FW
95{
96 int i;
97 size_t ret = 0;
98
a4fb581b 99 ret += callchain__fprintf_left_margin(fp, left_margin);
4eb3e478
FW
100 for (i = 0; i < depth; i++) {
101 if (depth_mask & (1 << i))
102 ret += fprintf(fp, "|");
103 else
104 ret += fprintf(fp, " ");
105 if (!count && i == depth - 1) {
106 double percent;
107
108 percent = hits * 100.0 / total_samples;
24b57c69 109 ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
4eb3e478
FW
110 } else
111 ret += fprintf(fp, "%s", " ");
112 }
113 if (chain->sym)
114 ret += fprintf(fp, "%s\n", chain->sym->name);
115 else
116 ret += fprintf(fp, "%p\n", (void *)(long)chain->ip);
117
118 return ret;
119}
120
25446036
FW
121static struct symbol *rem_sq_bracket;
122static struct callchain_list rem_hits;
123
124static void init_rem_hits(void)
125{
126 rem_sq_bracket = malloc(sizeof(*rem_sq_bracket) + 6);
127 if (!rem_sq_bracket) {
128 fprintf(stderr, "Not enough memory to display remaining hits\n");
129 return;
130 }
131
132 strcpy(rem_sq_bracket->name, "[...]");
133 rem_hits.sym = rem_sq_bracket;
134}
135
4eb3e478 136static size_t
af0a6fa4 137__callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
a4fb581b
FW
138 u64 total_samples, int depth, int depth_mask,
139 int left_margin)
4eb3e478
FW
140{
141 struct rb_node *node, *next;
142 struct callchain_node *child;
143 struct callchain_list *chain;
144 int new_depth_mask = depth_mask;
805d127d 145 u64 new_total;
25446036 146 u64 remaining;
4eb3e478
FW
147 size_t ret = 0;
148 int i;
149
805d127d 150 if (callchain_param.mode == CHAIN_GRAPH_REL)
1953287b 151 new_total = self->children_hit;
805d127d
FW
152 else
153 new_total = total_samples;
154
25446036
FW
155 remaining = new_total;
156
4eb3e478
FW
157 node = rb_first(&self->rb_root);
158 while (node) {
25446036
FW
159 u64 cumul;
160
4eb3e478 161 child = rb_entry(node, struct callchain_node, rb_node);
25446036
FW
162 cumul = cumul_hits(child);
163 remaining -= cumul;
4eb3e478
FW
164
165 /*
166 * The depth mask manages the output of pipes that show
167 * the depth. We don't want to keep the pipes of the current
25446036
FW
168 * level for the last child of this depth.
169 * Except if we have remaining filtered hits. They will
170 * supersede the last child
4eb3e478
FW
171 */
172 next = rb_next(node);
25446036 173 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
4eb3e478
FW
174 new_depth_mask &= ~(1 << (depth - 1));
175
176 /*
177 * But we keep the older depth mask for the line seperator
178 * to keep the level link until we reach the last child
179 */
a4fb581b
FW
180 ret += ipchain__fprintf_graph_line(fp, depth, depth_mask,
181 left_margin);
4eb3e478
FW
182 i = 0;
183 list_for_each_entry(chain, &child->val, list) {
184 if (chain->ip >= PERF_CONTEXT_MAX)
185 continue;
186 ret += ipchain__fprintf_graph(fp, chain, depth,
187 new_depth_mask, i++,
805d127d 188 new_total,
a4fb581b
FW
189 cumul,
190 left_margin);
4eb3e478 191 }
af0a6fa4
FW
192 ret += __callchain__fprintf_graph(fp, child, new_total,
193 depth + 1,
a4fb581b
FW
194 new_depth_mask | (1 << depth),
195 left_margin);
4eb3e478
FW
196 node = next;
197 }
198
25446036
FW
199 if (callchain_param.mode == CHAIN_GRAPH_REL &&
200 remaining && remaining != new_total) {
201
202 if (!rem_sq_bracket)
203 return ret;
204
205 new_depth_mask &= ~(1 << (depth - 1));
206
207 ret += ipchain__fprintf_graph(fp, &rem_hits, depth,
208 new_depth_mask, 0, new_total,
a4fb581b 209 remaining, left_margin);
25446036
FW
210 }
211
4eb3e478
FW
212 return ret;
213}
214
a4fb581b 215
af0a6fa4
FW
216static size_t
217callchain__fprintf_graph(FILE *fp, struct callchain_node *self,
a4fb581b 218 u64 total_samples, int left_margin)
af0a6fa4
FW
219{
220 struct callchain_list *chain;
a4fb581b 221 bool printed = false;
af0a6fa4
FW
222 int i = 0;
223 int ret = 0;
224
225 list_for_each_entry(chain, &self->val, list) {
226 if (chain->ip >= PERF_CONTEXT_MAX)
227 continue;
228
a4fb581b 229 if (!i++ && sort__first_dimension == SORT_SYM)
af0a6fa4
FW
230 continue;
231
a4fb581b
FW
232 if (!printed) {
233 ret += callchain__fprintf_left_margin(fp, left_margin);
234 ret += fprintf(fp, "|\n");
235 ret += callchain__fprintf_left_margin(fp, left_margin);
236 ret += fprintf(fp, "---");
237
238 left_margin += 3;
239 printed = true;
240 } else
241 ret += callchain__fprintf_left_margin(fp, left_margin);
242
af0a6fa4 243 if (chain->sym)
a4fb581b 244 ret += fprintf(fp, " %s\n", chain->sym->name);
af0a6fa4 245 else
a4fb581b 246 ret += fprintf(fp, " %p\n", (void *)(long)chain->ip);
af0a6fa4
FW
247 }
248
a4fb581b 249 ret += __callchain__fprintf_graph(fp, self, total_samples, 1, 1, left_margin);
af0a6fa4
FW
250
251 return ret;
252}
253
4eb3e478
FW
254static size_t
255callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
256 u64 total_samples)
f55c5552
FW
257{
258 struct callchain_list *chain;
259 size_t ret = 0;
260
261 if (!self)
262 return 0;
263
4eb3e478 264 ret += callchain__fprintf_flat(fp, self->parent, total_samples);
f55c5552
FW
265
266
4424961a
FW
267 list_for_each_entry(chain, &self->val, list) {
268 if (chain->ip >= PERF_CONTEXT_MAX)
269 continue;
270 if (chain->sym)
271 ret += fprintf(fp, " %s\n", chain->sym->name);
272 else
273 ret += fprintf(fp, " %p\n",
f37a291c 274 (void *)(long)chain->ip);
4424961a 275 }
f55c5552
FW
276
277 return ret;
278}
279
280static size_t
281hist_entry_callchain__fprintf(FILE *fp, struct hist_entry *self,
a4fb581b 282 u64 total_samples, int left_margin)
f55c5552
FW
283{
284 struct rb_node *rb_node;
285 struct callchain_node *chain;
286 size_t ret = 0;
287
288 rb_node = rb_first(&self->sorted_chain);
289 while (rb_node) {
290 double percent;
291
292 chain = rb_entry(rb_node, struct callchain_node, rb_node);
293 percent = chain->hit * 100.0 / total_samples;
805d127d
FW
294 switch (callchain_param.mode) {
295 case CHAIN_FLAT:
24b57c69
FW
296 ret += percent_color_fprintf(fp, " %6.2f%%\n",
297 percent);
4eb3e478 298 ret += callchain__fprintf_flat(fp, chain, total_samples);
805d127d
FW
299 break;
300 case CHAIN_GRAPH_ABS: /* Falldown */
301 case CHAIN_GRAPH_REL:
a4fb581b
FW
302 ret += callchain__fprintf_graph(fp, chain, total_samples,
303 left_margin);
83a0944f 304 case CHAIN_NONE:
805d127d
FW
305 default:
306 break;
4eb3e478 307 }
f55c5552
FW
308 ret += fprintf(fp, "\n");
309 rb_node = rb_next(rb_node);
310 }
311
312 return ret;
313}
314
1aa16738 315static size_t
9cffa8d5 316hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
1aa16738
PZ
317{
318 struct sort_entry *se;
319 size_t ret;
320
b8e6d829
IM
321 if (exclude_other && !self->parent)
322 return 0;
323
1e11fd82 324 if (total_samples)
52d422de
ACM
325 ret = percent_color_fprintf(fp,
326 field_sep ? "%.2f" : " %6.2f%%",
327 (self->count * 100.0) / total_samples);
1e11fd82 328 else
52d422de 329 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
1aa16738 330
e3d7e183
ACM
331 if (show_nr_samples) {
332 if (field_sep)
333 fprintf(fp, "%c%lld", *field_sep, self->count);
334 else
335 fprintf(fp, "%11lld", self->count);
336 }
1aa16738 337
71dd8945 338 list_for_each_entry(se, &hist_entry__sort_list, list) {
021191b3 339 if (se->elide)
b8e6d829
IM
340 continue;
341
52d422de
ACM
342 fprintf(fp, "%s", field_sep ?: " ");
343 ret += se->print(fp, self, se->width ? *se->width : 0);
71dd8945 344 }
1aa16738
PZ
345
346 ret += fprintf(fp, "\n");
347
a4fb581b
FW
348 if (callchain) {
349 int left_margin = 0;
350
351 if (sort__first_dimension == SORT_COMM) {
352 se = list_first_entry(&hist_entry__sort_list, typeof(*se),
353 list);
354 left_margin = se->width ? *se->width : 0;
355 left_margin -= thread__comm_len(self->thread);
356 }
357
358 hist_entry_callchain__fprintf(fp, self, total_samples,
359 left_margin);
360 }
f55c5552 361
1aa16738
PZ
362 return ret;
363}
364
6e7d6fdc
PZ
365/*
366 *
367 */
368
52d422de
ACM
369static void dso__calc_col_width(struct dso *self)
370{
371 if (!col_width_list_str && !field_sep &&
372 (!dso_list || strlist__has_entry(dso_list, self->name))) {
373 unsigned int slen = strlen(self->name);
374 if (slen > dsos__col_width)
375 dsos__col_width = slen;
376 }
377
378 self->slen_calculated = 1;
379}
380
5b447a6a 381static void thread__comm_adjust(struct thread *self)
4273b005 382{
5b447a6a 383 char *comm = self->comm;
4273b005
FW
384
385 if (!col_width_list_str && !field_sep &&
386 (!comm_list || strlist__has_entry(comm_list, comm))) {
387 unsigned int slen = strlen(comm);
388
389 if (slen > comms__col_width) {
390 comms__col_width = slen;
391 threads__col_width = slen + 6;
392 }
393 }
5b447a6a
FW
394}
395
396static int thread__set_comm_adjust(struct thread *self, const char *comm)
397{
398 int ret = thread__set_comm(self, comm);
399
400 if (ret)
401 return ret;
402
403 thread__comm_adjust(self);
4273b005
FW
404
405 return 0;
406}
407
2a0a50fe 408static int call__match(struct symbol *sym)
6e7d6fdc 409{
b25bcf2f 410 if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
2a0a50fe 411 return 1;
6e7d6fdc 412
2a0a50fe 413 return 0;
6e7d6fdc
PZ
414}
415
50e5095a 416static struct symbol **resolve_callchain(struct thread *thread,
4aa65636 417 struct perf_session *session,
9735abf1
ACM
418 struct ip_callchain *chain,
419 struct symbol **parent)
4424961a 420{
1ed091c4 421 u8 cpumode = PERF_RECORD_MISC_USER;
029e5b16 422 struct symbol **syms = NULL;
f37a291c 423 unsigned int i;
4424961a
FW
424
425 if (callchain) {
426 syms = calloc(chain->nr, sizeof(*syms));
427 if (!syms) {
428 fprintf(stderr, "Can't allocate memory for symbols\n");
429 exit(-1);
430 }
431 }
432
433 for (i = 0; i < chain->nr; i++) {
434 u64 ip = chain->ips[i];
1ed091c4 435 struct addr_location al;
4424961a
FW
436
437 if (ip >= PERF_CONTEXT_MAX) {
1ed091c4
ACM
438 switch (ip) {
439 case PERF_CONTEXT_HV:
440 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
441 case PERF_CONTEXT_KERNEL:
442 cpumode = PERF_RECORD_MISC_KERNEL; break;
443 case PERF_CONTEXT_USER:
444 cpumode = PERF_RECORD_MISC_USER; break;
445 default:
446 break;
447 }
4424961a
FW
448 continue;
449 }
450
4aa65636
ACM
451 thread__find_addr_location(thread, session, cpumode,
452 MAP__FUNCTION, ip, &al, NULL);
1ed091c4
ACM
453 if (al.sym != NULL) {
454 if (sort__has_parent && !*parent &&
455 call__match(al.sym))
456 *parent = al.sym;
4424961a
FW
457 if (!callchain)
458 break;
1ed091c4 459 syms[i] = al.sym;
4424961a
FW
460 }
461 }
462
463 return syms;
464}
465
1aa16738
PZ
466/*
467 * collect histogram counts
468 */
469
1ed091c4 470static int hist_entry__add(struct addr_location *al,
4aa65636 471 struct perf_session *session,
1ed091c4 472 struct ip_callchain *chain, u64 count)
8fa66bdc 473{
9735abf1
ACM
474 struct symbol **syms = NULL, *parent = NULL;
475 bool hit;
e7fb08b1 476 struct hist_entry *he;
e7fb08b1 477
4424961a 478 if ((sort__has_parent || callchain) && chain)
4aa65636 479 syms = resolve_callchain(al->thread, session, chain, &parent);
e7fb08b1 480
1ed091c4 481 he = __hist_entry__add(al, parent, count, &hit);
9735abf1
ACM
482 if (he == NULL)
483 return -ENOMEM;
e7fb08b1 484
9735abf1
ACM
485 if (hit)
486 he->count += count;
e7fb08b1 487
f55c5552 488 if (callchain) {
9735abf1
ACM
489 if (!hit)
490 callchain_init(&he->callchain);
4424961a
FW
491 append_chain(&he->callchain, chain, syms);
492 free(syms);
f55c5552 493 }
e7fb08b1
PZ
494
495 return 0;
8fa66bdc
ACM
496}
497
9cffa8d5 498static size_t output__fprintf(FILE *fp, u64 total_samples)
3a4b8cc7 499{
e7fb08b1 500 struct hist_entry *pos;
2d65537e 501 struct sort_entry *se;
3a4b8cc7
ACM
502 struct rb_node *nd;
503 size_t ret = 0;
52d422de
ACM
504 unsigned int width;
505 char *col_width = col_width_list_str;
9f866697
BG
506 int raw_printing_style;
507
508 raw_printing_style = !strcmp(pretty_printing_style, "raw");
3a4b8cc7 509
25446036
FW
510 init_rem_hits();
511
021191b3 512 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
ca8cdeef
PZ
513 fprintf(fp, "#\n");
514
515 fprintf(fp, "# Overhead");
e3d7e183
ACM
516 if (show_nr_samples) {
517 if (field_sep)
518 fprintf(fp, "%cSamples", *field_sep);
519 else
520 fputs(" Samples ", fp);
521 }
b8e6d829 522 list_for_each_entry(se, &hist_entry__sort_list, list) {
021191b3 523 if (se->elide)
b8e6d829 524 continue;
52d422de
ACM
525 if (field_sep) {
526 fprintf(fp, "%c%s", *field_sep, se->header);
b8e6d829 527 continue;
52d422de
ACM
528 }
529 width = strlen(se->header);
530 if (se->width) {
531 if (col_width_list_str) {
532 if (col_width) {
533 *se->width = atoi(col_width);
534 col_width = strchr(col_width, ',');
535 if (col_width)
536 ++col_width;
537 }
538 }
539 width = *se->width = max(*se->width, width);
540 }
541 fprintf(fp, " %*s", width, se->header);
b8e6d829 542 }
ca8cdeef
PZ
543 fprintf(fp, "\n");
544
52d422de
ACM
545 if (field_sep)
546 goto print_entries;
547
ca8cdeef 548 fprintf(fp, "# ........");
e3d7e183
ACM
549 if (show_nr_samples)
550 fprintf(fp, " ..........");
2d65537e 551 list_for_each_entry(se, &hist_entry__sort_list, list) {
f37a291c 552 unsigned int i;
ca8cdeef 553
021191b3 554 if (se->elide)
b8e6d829
IM
555 continue;
556
4593bba8 557 fprintf(fp, " ");
52d422de
ACM
558 if (se->width)
559 width = *se->width;
560 else
561 width = strlen(se->header);
562 for (i = 0; i < width; i++)
ca8cdeef 563 fprintf(fp, ".");
2d65537e 564 }
ca8cdeef
PZ
565 fprintf(fp, "\n");
566
567 fprintf(fp, "#\n");
2d65537e 568
52d422de 569print_entries:
b9bf0892 570 for (nd = rb_first(&hist); nd; nd = rb_next(nd)) {
e7fb08b1
PZ
571 pos = rb_entry(nd, struct hist_entry, rb_node);
572 ret += hist_entry__fprintf(fp, pos, total_samples);
3a4b8cc7
ACM
573 }
574
b8e6d829
IM
575 if (sort_order == default_sort_order &&
576 parent_pattern == default_parent_pattern) {
bd74137e 577 fprintf(fp, "#\n");
114cfab2 578 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
bd74137e
IM
579 fprintf(fp, "#\n");
580 }
71dd8945 581 fprintf(fp, "\n");
bd74137e 582
25446036
FW
583 free(rem_sq_bracket);
584
8d513270 585 if (show_threads)
9f866697
BG
586 perf_read_values_display(fp, &show_threads_values,
587 raw_printing_style);
8d513270 588
3a4b8cc7
ACM
589 return ret;
590}
591
2a0a50fe 592static int validate_chain(struct ip_callchain *chain, event_t *event)
7522060c
IM
593{
594 unsigned int chain_size;
595
7522060c
IM
596 chain_size = event->header.size;
597 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
598
9cffa8d5 599 if (chain->nr*sizeof(u64) > chain_size)
7522060c
IM
600 return -1;
601
602 return 0;
603}
604
b3165f41 605static int process_sample_event(event_t *event, struct perf_session *session)
75051724 606{
180f95e2 607 struct sample_data data;
d8db1b57 608 int cpumode;
1ed091c4 609 struct addr_location al;
180f95e2 610 struct thread *thread;
6baa0a5a 611
180f95e2
OH
612 memset(&data, 0, sizeof(data));
613 data.period = 1;
614
615 event__parse_sample(event, sample_type, &data);
ea1900e5 616
62daacb5 617 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
75051724 618 event->header.misc,
180f95e2
OH
619 data.pid, data.tid,
620 (void *)(long)data.ip,
621 (long long)data.period);
75051724 622
e6e18ec7 623 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
f37a291c 624 unsigned int i;
3efa1cc9 625
180f95e2 626 dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
3efa1cc9 627
180f95e2 628 if (validate_chain(data.callchain, event) < 0) {
6beba7ad
ACM
629 pr_debug("call-chain problem with event, "
630 "skipping it.\n");
7522060c
IM
631 return 0;
632 }
633
634 if (dump_trace) {
180f95e2
OH
635 for (i = 0; i < data.callchain->nr; i++)
636 dump_printf("..... %2d: %016Lx\n",
637 i, data.callchain->ips[i]);
3efa1cc9
IM
638 }
639 }
640
b3165f41 641 thread = perf_session__findnew(session, data.pid);
75051724 642 if (thread == NULL) {
6beba7ad 643 pr_debug("problem processing %d event, skipping it.\n",
75051724
IM
644 event->header.type);
645 return -1;
646 }
e7fb08b1 647
f39cdf25
JL
648 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
649
cc8b88b1
ACM
650 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
651 return 0;
652
cdd6c482 653 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
d8db1b57 654
4aa65636 655 thread__find_addr_location(thread, session, cpumode,
180f95e2 656 MAP__FUNCTION, data.ip, &al, NULL);
1ed091c4
ACM
657 /*
658 * We have to do this here as we may have a dso with no symbol hit that
659 * has a name longer than the ones with symbols sampled.
660 */
661 if (al.map && !sort_dso.elide && !al.map->dso->slen_calculated)
662 dso__calc_col_width(al.map->dso);
8fa66bdc 663
ec218fc4 664 if (dso_list &&
1ed091c4
ACM
665 (!al.map || !al.map->dso ||
666 !(strlist__has_entry(dso_list, al.map->dso->short_name) ||
667 (al.map->dso->short_name != al.map->dso->long_name &&
668 strlist__has_entry(dso_list, al.map->dso->long_name)))))
ec218fc4 669 return 0;
25903407 670
1ed091c4 671 if (sym_list && al.sym && !strlist__has_entry(sym_list, al.sym->name))
ec218fc4 672 return 0;
7bec7a91 673
4aa65636 674 if (hist_entry__add(&al, session, data.callchain, data.period)) {
6beba7ad 675 pr_debug("problem incrementing symbol count, skipping event\n");
ec218fc4 676 return -1;
8fa66bdc 677 }
ec218fc4 678
180f95e2 679 event__stats.total += data.period;
8fa66bdc 680
75051724
IM
681 return 0;
682}
3502973d 683
b3165f41 684static int process_comm_event(event_t *event, struct perf_session *session)
75051724 685{
b3165f41 686 struct thread *thread = perf_session__findnew(session, event->comm.pid);
75051724 687
62daacb5 688 dump_printf(": %s:%d\n", event->comm.comm, event->comm.pid);
75051724
IM
689
690 if (thread == NULL ||
4273b005 691 thread__set_comm_adjust(thread, event->comm.comm)) {
cdd6c482 692 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
75051724 693 return -1;
8fa66bdc 694 }
9d91a6f7
PZ
695
696 return 0;
697}
698
d8f66248 699static int process_read_event(event_t *event, struct perf_session *session __used)
e9ea2fde 700{
cdd6c482 701 struct perf_event_attr *attr;
0d3a5c88 702
94c744b6 703 attr = perf_header__find_attr(event->read.id, &session->header);
8f18aec5 704
8d513270 705 if (show_threads) {
83a0944f 706 const char *name = attr ? __event_name(attr->type, attr->config)
8d513270
BG
707 : "unknown";
708 perf_read_values_add_value(&show_threads_values,
709 event->read.pid, event->read.tid,
710 event->read.id,
711 name,
712 event->read.value);
713 }
714
62daacb5
ACM
715 dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
716 attr ? __event_name(attr->type, attr->config) : "FAIL",
717 event->read.value);
e9ea2fde
PZ
718
719 return 0;
720}
721
016e92fb 722static int sample_type_check(u64 type)
d80d338d 723{
016e92fb 724 sample_type = type;
e6e18ec7 725
91b4eaea
FW
726 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
727 if (sort__has_parent) {
728 fprintf(stderr, "selected --sort parent, but no"
729 " callchain data. Did you call"
730 " perf record without -g?\n");
016e92fb 731 return -1;
91b4eaea
FW
732 }
733 if (callchain) {
6ede59c4 734 fprintf(stderr, "selected -g but no callchain data."
91b4eaea
FW
735 " Did you call perf record without"
736 " -g?\n");
016e92fb 737 return -1;
91b4eaea 738 }
b1a88349
FW
739 } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
740 callchain = 1;
741 if (register_callchain_param(&callchain_param) < 0) {
742 fprintf(stderr, "Can't register callchain"
743 " params\n");
016e92fb 744 return -1;
b1a88349 745 }
f5970550
PZ
746 }
747
016e92fb
FW
748 return 0;
749}
6142f9ec 750
301a0b02 751static struct perf_event_ops event_ops = {
016e92fb 752 .process_sample_event = process_sample_event,
62daacb5 753 .process_mmap_event = event__process_mmap,
016e92fb 754 .process_comm_event = process_comm_event,
62daacb5
ACM
755 .process_exit_event = event__process_task,
756 .process_fork_event = event__process_task,
757 .process_lost_event = event__process_lost,
016e92fb
FW
758 .process_read_event = process_read_event,
759 .sample_type_check = sample_type_check,
760};
6142f9ec 761
6142f9ec 762
016e92fb
FW
763static int __cmd_report(void)
764{
016e92fb 765 int ret;
d8f66248 766 struct perf_session *session;
8fa66bdc 767
4aa65636 768 session = perf_session__new(input_name, O_RDONLY, force, &symbol_conf);
94c744b6
ACM
769 if (session == NULL)
770 return -ENOMEM;
771
016e92fb
FW
772 if (show_threads)
773 perf_read_values_init(&show_threads_values);
f5970550 774
ec913369 775 ret = perf_session__process_events(session, &event_ops);
016e92fb 776 if (ret)
94c744b6 777 goto out_delete;
97b07b69 778
62daacb5
ACM
779 if (dump_trace) {
780 event__print_totals();
94c744b6 781 goto out_delete;
62daacb5 782 }
97b07b69 783
da21d1b5 784 if (verbose > 3)
b3165f41 785 perf_session__fprintf(session, stdout);
9ac99545 786
da21d1b5 787 if (verbose > 2)
16f762a2 788 dsos__fprintf(stdout);
16f762a2 789
8229289b 790 collapse__resort();
62daacb5
ACM
791 output__resort(event__stats.total);
792 output__fprintf(stdout, event__stats.total);
8fa66bdc 793
8d513270
BG
794 if (show_threads)
795 perf_read_values_destroy(&show_threads_values);
94c744b6
ACM
796out_delete:
797 perf_session__delete(session);
016e92fb 798 return ret;
8fa66bdc
ACM
799}
800
4eb3e478
FW
801static int
802parse_callchain_opt(const struct option *opt __used, const char *arg,
803 int unset __used)
804{
c20ab37e
FW
805 char *tok;
806 char *endptr;
807
4eb3e478
FW
808 callchain = 1;
809
810 if (!arg)
811 return 0;
812
c20ab37e
FW
813 tok = strtok((char *)arg, ",");
814 if (!tok)
815 return -1;
816
817 /* get the output mode */
818 if (!strncmp(tok, "graph", strlen(arg)))
805d127d 819 callchain_param.mode = CHAIN_GRAPH_ABS;
4eb3e478 820
c20ab37e 821 else if (!strncmp(tok, "flat", strlen(arg)))
805d127d
FW
822 callchain_param.mode = CHAIN_FLAT;
823
824 else if (!strncmp(tok, "fractal", strlen(arg)))
825 callchain_param.mode = CHAIN_GRAPH_REL;
826
b1a88349
FW
827 else if (!strncmp(tok, "none", strlen(arg))) {
828 callchain_param.mode = CHAIN_NONE;
829 callchain = 0;
830
831 return 0;
832 }
833
4eb3e478
FW
834 else
835 return -1;
836
c20ab37e
FW
837 /* get the min percentage */
838 tok = strtok(NULL, ",");
839 if (!tok)
805d127d 840 goto setup;
c20ab37e 841
805d127d 842 callchain_param.min_percent = strtod(tok, &endptr);
c20ab37e
FW
843 if (tok == endptr)
844 return -1;
845
805d127d
FW
846setup:
847 if (register_callchain_param(&callchain_param) < 0) {
848 fprintf(stderr, "Can't register callchain params\n");
849 return -1;
850 }
4eb3e478
FW
851 return 0;
852}
853
dd68ada2
JK
854//static const char * const report_usage[] = {
855const char * const report_usage[] = {
53cb8bc2
IM
856 "perf report [<options>] <command>",
857 NULL
858};
859
860static const struct option options[] = {
861 OPT_STRING('i', "input", &input_name, "file",
862 "input file name"),
815e777f
ACM
863 OPT_BOOLEAN('v', "verbose", &verbose,
864 "be more verbose (show symbol address, etc)"),
97b07b69
IM
865 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
866 "dump raw trace in ASCII"),
b32d133a
ACM
867 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
868 "file", "vmlinux pathname"),
fa6963b2 869 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
b32d133a 870 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 871 "load module symbols - WARNING: use only with -k and LIVE kernel"),
e3d7e183
ACM
872 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
873 "Show a column with the number of samples"),
8d513270
BG
874 OPT_BOOLEAN('T', "threads", &show_threads,
875 "Show per-thread event counters"),
9f866697
BG
876 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
877 "pretty printing style key: normal raw"),
63299f05 878 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
b25bcf2f 879 "sort by key(s): pid, comm, dso, symbol, parent"),
ec913369 880 OPT_BOOLEAN('P', "full-paths", &event_ops.full_paths,
b78c07d4 881 "Don't shorten the pathnames taking into account the cwd"),
b25bcf2f
IM
882 OPT_STRING('p', "parent", &parent_pattern, "regex",
883 "regex filter to identify parent, see: '--sort parent'"),
b8e6d829
IM
884 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
885 "Only display entries with parent-match"),
1483b19f 886 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
c20ab37e 887 "Display callchains using output_type and min percent threshold. "
1483b19f 888 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
25903407
ACM
889 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
890 "only consider symbols in these dsos"),
cc8b88b1
ACM
891 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
892 "only consider symbols in these comms"),
7bec7a91
ACM
893 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
894 "only consider these symbols"),
52d422de
ACM
895 OPT_STRING('w', "column-widths", &col_width_list_str,
896 "width[,width...]",
897 "don't try to adjust column width, use these fixed values"),
898 OPT_STRING('t', "field-separator", &field_sep, "separator",
899 "separator for columns, no spaces will be added between "
900 "columns '.' is reserved."),
53cb8bc2
IM
901 OPT_END()
902};
903
5352f35d
IM
904static void setup_sorting(void)
905{
906 char *tmp, *tok, *str = strdup(sort_order);
907
908 for (tok = strtok_r(str, ", ", &tmp);
909 tok; tok = strtok_r(NULL, ", ", &tmp)) {
910 if (sort_dimension__add(tok) < 0) {
911 error("Unknown --sort key: `%s'", tok);
912 usage_with_options(report_usage, options);
913 }
914 }
915
916 free(str);
917}
918
cc8b88b1 919static void setup_list(struct strlist **list, const char *list_str,
021191b3
ACM
920 struct sort_entry *se, const char *list_name,
921 FILE *fp)
cc8b88b1
ACM
922{
923 if (list_str) {
924 *list = strlist__new(true, list_str);
925 if (!*list) {
926 fprintf(stderr, "problems parsing %s list\n",
927 list_name);
928 exit(129);
929 }
021191b3
ACM
930 if (strlist__nr_entries(*list) == 1) {
931 fprintf(fp, "# %s: %s\n", list_name,
932 strlist__entry(*list, 0)->s);
933 se->elide = true;
934 }
cc8b88b1
ACM
935 }
936}
937
f37a291c 938int cmd_report(int argc, const char **argv, const char *prefix __used)
53cb8bc2 939{
b32d133a
ACM
940 if (symbol__init(&symbol_conf) < 0)
941 return -1;
53cb8bc2 942
edc52dea 943 argc = parse_options(argc, argv, options, report_usage, 0);
53cb8bc2 944
1aa16738
PZ
945 setup_sorting();
946
021191b3 947 if (parent_pattern != default_parent_pattern) {
b8e6d829 948 sort_dimension__add("parent");
021191b3
ACM
949 sort_parent.elide = 1;
950 } else
b8e6d829
IM
951 exclude_other = 0;
952
edc52dea
IM
953 /*
954 * Any (unrecognized) arguments left?
955 */
956 if (argc)
957 usage_with_options(report_usage, options);
958
a930d2c0
IM
959 setup_pager();
960
021191b3
ACM
961 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
962 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
963 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
25903407 964
52d422de
ACM
965 if (field_sep && *field_sep == '.') {
966 fputs("'.' is the only non valid --field-separator argument\n",
967 stderr);
968 exit(129);
969 }
970
53cb8bc2
IM
971 return __cmd_report();
972}