perf session: Reduce the number of parms to perf_session__process_events
[linux-block.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,
9735abf1
ACM
417 struct ip_callchain *chain,
418 struct symbol **parent)
4424961a 419{
1ed091c4 420 u8 cpumode = PERF_RECORD_MISC_USER;
029e5b16 421 struct symbol **syms = NULL;
f37a291c 422 unsigned int i;
4424961a
FW
423
424 if (callchain) {
425 syms = calloc(chain->nr, sizeof(*syms));
426 if (!syms) {
427 fprintf(stderr, "Can't allocate memory for symbols\n");
428 exit(-1);
429 }
430 }
431
432 for (i = 0; i < chain->nr; i++) {
433 u64 ip = chain->ips[i];
1ed091c4 434 struct addr_location al;
4424961a
FW
435
436 if (ip >= PERF_CONTEXT_MAX) {
1ed091c4
ACM
437 switch (ip) {
438 case PERF_CONTEXT_HV:
439 cpumode = PERF_RECORD_MISC_HYPERVISOR; break;
440 case PERF_CONTEXT_KERNEL:
441 cpumode = PERF_RECORD_MISC_KERNEL; break;
442 case PERF_CONTEXT_USER:
443 cpumode = PERF_RECORD_MISC_USER; break;
444 default:
445 break;
446 }
4424961a
FW
447 continue;
448 }
449
1ed091c4
ACM
450 thread__find_addr_location(thread, cpumode, MAP__FUNCTION,
451 ip, &al, NULL);
452 if (al.sym != NULL) {
453 if (sort__has_parent && !*parent &&
454 call__match(al.sym))
455 *parent = al.sym;
4424961a
FW
456 if (!callchain)
457 break;
1ed091c4 458 syms[i] = al.sym;
4424961a
FW
459 }
460 }
461
462 return syms;
463}
464
1aa16738
PZ
465/*
466 * collect histogram counts
467 */
468
1ed091c4
ACM
469static int hist_entry__add(struct addr_location *al,
470 struct ip_callchain *chain, u64 count)
8fa66bdc 471{
9735abf1
ACM
472 struct symbol **syms = NULL, *parent = NULL;
473 bool hit;
e7fb08b1 474 struct hist_entry *he;
e7fb08b1 475
4424961a 476 if ((sort__has_parent || callchain) && chain)
1ed091c4 477 syms = resolve_callchain(al->thread, chain, &parent);
e7fb08b1 478
1ed091c4 479 he = __hist_entry__add(al, parent, count, &hit);
9735abf1
ACM
480 if (he == NULL)
481 return -ENOMEM;
e7fb08b1 482
9735abf1
ACM
483 if (hit)
484 he->count += count;
e7fb08b1 485
f55c5552 486 if (callchain) {
9735abf1
ACM
487 if (!hit)
488 callchain_init(&he->callchain);
4424961a
FW
489 append_chain(&he->callchain, chain, syms);
490 free(syms);
f55c5552 491 }
e7fb08b1
PZ
492
493 return 0;
8fa66bdc
ACM
494}
495
9cffa8d5 496static size_t output__fprintf(FILE *fp, u64 total_samples)
3a4b8cc7 497{
e7fb08b1 498 struct hist_entry *pos;
2d65537e 499 struct sort_entry *se;
3a4b8cc7
ACM
500 struct rb_node *nd;
501 size_t ret = 0;
52d422de
ACM
502 unsigned int width;
503 char *col_width = col_width_list_str;
9f866697
BG
504 int raw_printing_style;
505
506 raw_printing_style = !strcmp(pretty_printing_style, "raw");
3a4b8cc7 507
25446036
FW
508 init_rem_hits();
509
021191b3 510 fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
ca8cdeef
PZ
511 fprintf(fp, "#\n");
512
513 fprintf(fp, "# Overhead");
e3d7e183
ACM
514 if (show_nr_samples) {
515 if (field_sep)
516 fprintf(fp, "%cSamples", *field_sep);
517 else
518 fputs(" Samples ", fp);
519 }
b8e6d829 520 list_for_each_entry(se, &hist_entry__sort_list, list) {
021191b3 521 if (se->elide)
b8e6d829 522 continue;
52d422de
ACM
523 if (field_sep) {
524 fprintf(fp, "%c%s", *field_sep, se->header);
b8e6d829 525 continue;
52d422de
ACM
526 }
527 width = strlen(se->header);
528 if (se->width) {
529 if (col_width_list_str) {
530 if (col_width) {
531 *se->width = atoi(col_width);
532 col_width = strchr(col_width, ',');
533 if (col_width)
534 ++col_width;
535 }
536 }
537 width = *se->width = max(*se->width, width);
538 }
539 fprintf(fp, " %*s", width, se->header);
b8e6d829 540 }
ca8cdeef
PZ
541 fprintf(fp, "\n");
542
52d422de
ACM
543 if (field_sep)
544 goto print_entries;
545
ca8cdeef 546 fprintf(fp, "# ........");
e3d7e183
ACM
547 if (show_nr_samples)
548 fprintf(fp, " ..........");
2d65537e 549 list_for_each_entry(se, &hist_entry__sort_list, list) {
f37a291c 550 unsigned int i;
ca8cdeef 551
021191b3 552 if (se->elide)
b8e6d829
IM
553 continue;
554
4593bba8 555 fprintf(fp, " ");
52d422de
ACM
556 if (se->width)
557 width = *se->width;
558 else
559 width = strlen(se->header);
560 for (i = 0; i < width; i++)
ca8cdeef 561 fprintf(fp, ".");
2d65537e 562 }
ca8cdeef
PZ
563 fprintf(fp, "\n");
564
565 fprintf(fp, "#\n");
2d65537e 566
52d422de 567print_entries:
e7fb08b1
PZ
568 for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
569 pos = rb_entry(nd, struct hist_entry, rb_node);
570 ret += hist_entry__fprintf(fp, pos, total_samples);
3a4b8cc7
ACM
571 }
572
b8e6d829
IM
573 if (sort_order == default_sort_order &&
574 parent_pattern == default_parent_pattern) {
bd74137e 575 fprintf(fp, "#\n");
114cfab2 576 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
bd74137e
IM
577 fprintf(fp, "#\n");
578 }
71dd8945 579 fprintf(fp, "\n");
bd74137e 580
25446036
FW
581 free(rem_sq_bracket);
582
8d513270 583 if (show_threads)
9f866697
BG
584 perf_read_values_display(fp, &show_threads_values,
585 raw_printing_style);
8d513270 586
3a4b8cc7
ACM
587 return ret;
588}
589
2a0a50fe 590static int validate_chain(struct ip_callchain *chain, event_t *event)
7522060c
IM
591{
592 unsigned int chain_size;
593
7522060c
IM
594 chain_size = event->header.size;
595 chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
596
9cffa8d5 597 if (chain->nr*sizeof(u64) > chain_size)
7522060c
IM
598 return -1;
599
600 return 0;
601}
602
d8f66248 603static int process_sample_event(event_t *event, struct perf_session *session __used)
75051724 604{
180f95e2 605 struct sample_data data;
d8db1b57 606 int cpumode;
1ed091c4 607 struct addr_location al;
180f95e2 608 struct thread *thread;
6baa0a5a 609
180f95e2
OH
610 memset(&data, 0, sizeof(data));
611 data.period = 1;
612
613 event__parse_sample(event, sample_type, &data);
ea1900e5 614
62daacb5 615 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
75051724 616 event->header.misc,
180f95e2
OH
617 data.pid, data.tid,
618 (void *)(long)data.ip,
619 (long long)data.period);
75051724 620
e6e18ec7 621 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
f37a291c 622 unsigned int i;
3efa1cc9 623
180f95e2 624 dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
3efa1cc9 625
180f95e2 626 if (validate_chain(data.callchain, event) < 0) {
6beba7ad
ACM
627 pr_debug("call-chain problem with event, "
628 "skipping it.\n");
7522060c
IM
629 return 0;
630 }
631
632 if (dump_trace) {
180f95e2
OH
633 for (i = 0; i < data.callchain->nr; i++)
634 dump_printf("..... %2d: %016Lx\n",
635 i, data.callchain->ips[i]);
3efa1cc9
IM
636 }
637 }
638
180f95e2 639 thread = threads__findnew(data.pid);
75051724 640 if (thread == NULL) {
6beba7ad 641 pr_debug("problem processing %d event, skipping it.\n",
75051724
IM
642 event->header.type);
643 return -1;
644 }
e7fb08b1 645
f39cdf25
JL
646 dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
647
cc8b88b1
ACM
648 if (comm_list && !strlist__has_entry(comm_list, thread->comm))
649 return 0;
650
cdd6c482 651 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
d8db1b57 652
1ed091c4 653 thread__find_addr_location(thread, cpumode,
180f95e2 654 MAP__FUNCTION, data.ip, &al, NULL);
1ed091c4
ACM
655 /*
656 * We have to do this here as we may have a dso with no symbol hit that
657 * has a name longer than the ones with symbols sampled.
658 */
659 if (al.map && !sort_dso.elide && !al.map->dso->slen_calculated)
660 dso__calc_col_width(al.map->dso);
8fa66bdc 661
ec218fc4 662 if (dso_list &&
1ed091c4
ACM
663 (!al.map || !al.map->dso ||
664 !(strlist__has_entry(dso_list, al.map->dso->short_name) ||
665 (al.map->dso->short_name != al.map->dso->long_name &&
666 strlist__has_entry(dso_list, al.map->dso->long_name)))))
ec218fc4 667 return 0;
25903407 668
1ed091c4 669 if (sym_list && al.sym && !strlist__has_entry(sym_list, al.sym->name))
ec218fc4 670 return 0;
7bec7a91 671
180f95e2 672 if (hist_entry__add(&al, data.callchain, data.period)) {
6beba7ad 673 pr_debug("problem incrementing symbol count, skipping event\n");
ec218fc4 674 return -1;
8fa66bdc 675 }
ec218fc4 676
180f95e2 677 event__stats.total += data.period;
8fa66bdc 678
75051724
IM
679 return 0;
680}
3502973d 681
d8f66248 682static int process_comm_event(event_t *event, struct perf_session *session __used)
75051724 683{
d5b889f2 684 struct thread *thread = threads__findnew(event->comm.pid);
75051724 685
62daacb5 686 dump_printf(": %s:%d\n", event->comm.comm, event->comm.pid);
75051724
IM
687
688 if (thread == NULL ||
4273b005 689 thread__set_comm_adjust(thread, event->comm.comm)) {
cdd6c482 690 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
75051724 691 return -1;
8fa66bdc 692 }
9d91a6f7
PZ
693
694 return 0;
695}
696
d8f66248 697static int process_read_event(event_t *event, struct perf_session *session __used)
e9ea2fde 698{
cdd6c482 699 struct perf_event_attr *attr;
0d3a5c88 700
94c744b6 701 attr = perf_header__find_attr(event->read.id, &session->header);
8f18aec5 702
8d513270 703 if (show_threads) {
83a0944f 704 const char *name = attr ? __event_name(attr->type, attr->config)
8d513270
BG
705 : "unknown";
706 perf_read_values_add_value(&show_threads_values,
707 event->read.pid, event->read.tid,
708 event->read.id,
709 name,
710 event->read.value);
711 }
712
62daacb5
ACM
713 dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
714 attr ? __event_name(attr->type, attr->config) : "FAIL",
715 event->read.value);
e9ea2fde
PZ
716
717 return 0;
718}
719
016e92fb 720static int sample_type_check(u64 type)
d80d338d 721{
016e92fb 722 sample_type = type;
e6e18ec7 723
91b4eaea
FW
724 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
725 if (sort__has_parent) {
726 fprintf(stderr, "selected --sort parent, but no"
727 " callchain data. Did you call"
728 " perf record without -g?\n");
016e92fb 729 return -1;
91b4eaea
FW
730 }
731 if (callchain) {
6ede59c4 732 fprintf(stderr, "selected -g but no callchain data."
91b4eaea
FW
733 " Did you call perf record without"
734 " -g?\n");
016e92fb 735 return -1;
91b4eaea 736 }
b1a88349
FW
737 } else if (callchain_param.mode != CHAIN_NONE && !callchain) {
738 callchain = 1;
739 if (register_callchain_param(&callchain_param) < 0) {
740 fprintf(stderr, "Can't register callchain"
741 " params\n");
016e92fb 742 return -1;
b1a88349 743 }
f5970550
PZ
744 }
745
016e92fb
FW
746 return 0;
747}
6142f9ec 748
301a0b02 749static struct perf_event_ops event_ops = {
016e92fb 750 .process_sample_event = process_sample_event,
62daacb5 751 .process_mmap_event = event__process_mmap,
016e92fb 752 .process_comm_event = process_comm_event,
62daacb5
ACM
753 .process_exit_event = event__process_task,
754 .process_fork_event = event__process_task,
755 .process_lost_event = event__process_lost,
016e92fb
FW
756 .process_read_event = process_read_event,
757 .sample_type_check = sample_type_check,
758};
6142f9ec 759
6142f9ec 760
016e92fb
FW
761static int __cmd_report(void)
762{
016e92fb 763 int ret;
d8f66248 764 struct perf_session *session;
8fa66bdc 765
94c744b6
ACM
766 session = perf_session__new(input_name, O_RDONLY, force);
767 if (session == NULL)
768 return -ENOMEM;
769
016e92fb
FW
770 if (show_threads)
771 perf_read_values_init(&show_threads_values);
f5970550 772
ec913369 773 ret = perf_session__process_events(session, &event_ops);
016e92fb 774 if (ret)
94c744b6 775 goto out_delete;
97b07b69 776
62daacb5
ACM
777 if (dump_trace) {
778 event__print_totals();
94c744b6 779 goto out_delete;
62daacb5 780 }
97b07b69 781
da21d1b5 782 if (verbose > 3)
d5b889f2 783 threads__fprintf(stdout);
9ac99545 784
da21d1b5 785 if (verbose > 2)
16f762a2 786 dsos__fprintf(stdout);
16f762a2 787
8229289b 788 collapse__resort();
62daacb5
ACM
789 output__resort(event__stats.total);
790 output__fprintf(stdout, event__stats.total);
8fa66bdc 791
8d513270
BG
792 if (show_threads)
793 perf_read_values_destroy(&show_threads_values);
94c744b6
ACM
794out_delete:
795 perf_session__delete(session);
016e92fb 796 return ret;
8fa66bdc
ACM
797}
798
4eb3e478
FW
799static int
800parse_callchain_opt(const struct option *opt __used, const char *arg,
801 int unset __used)
802{
c20ab37e
FW
803 char *tok;
804 char *endptr;
805
4eb3e478
FW
806 callchain = 1;
807
808 if (!arg)
809 return 0;
810
c20ab37e
FW
811 tok = strtok((char *)arg, ",");
812 if (!tok)
813 return -1;
814
815 /* get the output mode */
816 if (!strncmp(tok, "graph", strlen(arg)))
805d127d 817 callchain_param.mode = CHAIN_GRAPH_ABS;
4eb3e478 818
c20ab37e 819 else if (!strncmp(tok, "flat", strlen(arg)))
805d127d
FW
820 callchain_param.mode = CHAIN_FLAT;
821
822 else if (!strncmp(tok, "fractal", strlen(arg)))
823 callchain_param.mode = CHAIN_GRAPH_REL;
824
b1a88349
FW
825 else if (!strncmp(tok, "none", strlen(arg))) {
826 callchain_param.mode = CHAIN_NONE;
827 callchain = 0;
828
829 return 0;
830 }
831
4eb3e478
FW
832 else
833 return -1;
834
c20ab37e
FW
835 /* get the min percentage */
836 tok = strtok(NULL, ",");
837 if (!tok)
805d127d 838 goto setup;
c20ab37e 839
805d127d 840 callchain_param.min_percent = strtod(tok, &endptr);
c20ab37e
FW
841 if (tok == endptr)
842 return -1;
843
805d127d
FW
844setup:
845 if (register_callchain_param(&callchain_param) < 0) {
846 fprintf(stderr, "Can't register callchain params\n");
847 return -1;
848 }
4eb3e478
FW
849 return 0;
850}
851
dd68ada2
JK
852//static const char * const report_usage[] = {
853const char * const report_usage[] = {
53cb8bc2
IM
854 "perf report [<options>] <command>",
855 NULL
856};
857
858static const struct option options[] = {
859 OPT_STRING('i', "input", &input_name, "file",
860 "input file name"),
815e777f
ACM
861 OPT_BOOLEAN('v', "verbose", &verbose,
862 "be more verbose (show symbol address, etc)"),
97b07b69
IM
863 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
864 "dump raw trace in ASCII"),
b32d133a
ACM
865 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
866 "file", "vmlinux pathname"),
fa6963b2 867 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
b32d133a 868 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 869 "load module symbols - WARNING: use only with -k and LIVE kernel"),
e3d7e183
ACM
870 OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
871 "Show a column with the number of samples"),
8d513270
BG
872 OPT_BOOLEAN('T', "threads", &show_threads,
873 "Show per-thread event counters"),
9f866697
BG
874 OPT_STRING(0, "pretty", &pretty_printing_style, "key",
875 "pretty printing style key: normal raw"),
63299f05 876 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
b25bcf2f 877 "sort by key(s): pid, comm, dso, symbol, parent"),
ec913369 878 OPT_BOOLEAN('P', "full-paths", &event_ops.full_paths,
b78c07d4 879 "Don't shorten the pathnames taking into account the cwd"),
b25bcf2f
IM
880 OPT_STRING('p', "parent", &parent_pattern, "regex",
881 "regex filter to identify parent, see: '--sort parent'"),
b8e6d829
IM
882 OPT_BOOLEAN('x', "exclude-other", &exclude_other,
883 "Only display entries with parent-match"),
1483b19f 884 OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
c20ab37e 885 "Display callchains using output_type and min percent threshold. "
1483b19f 886 "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
25903407
ACM
887 OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
888 "only consider symbols in these dsos"),
cc8b88b1
ACM
889 OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
890 "only consider symbols in these comms"),
7bec7a91
ACM
891 OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
892 "only consider these symbols"),
52d422de
ACM
893 OPT_STRING('w', "column-widths", &col_width_list_str,
894 "width[,width...]",
895 "don't try to adjust column width, use these fixed values"),
896 OPT_STRING('t', "field-separator", &field_sep, "separator",
897 "separator for columns, no spaces will be added between "
898 "columns '.' is reserved."),
53cb8bc2
IM
899 OPT_END()
900};
901
5352f35d
IM
902static void setup_sorting(void)
903{
904 char *tmp, *tok, *str = strdup(sort_order);
905
906 for (tok = strtok_r(str, ", ", &tmp);
907 tok; tok = strtok_r(NULL, ", ", &tmp)) {
908 if (sort_dimension__add(tok) < 0) {
909 error("Unknown --sort key: `%s'", tok);
910 usage_with_options(report_usage, options);
911 }
912 }
913
914 free(str);
915}
916
cc8b88b1 917static void setup_list(struct strlist **list, const char *list_str,
021191b3
ACM
918 struct sort_entry *se, const char *list_name,
919 FILE *fp)
cc8b88b1
ACM
920{
921 if (list_str) {
922 *list = strlist__new(true, list_str);
923 if (!*list) {
924 fprintf(stderr, "problems parsing %s list\n",
925 list_name);
926 exit(129);
927 }
021191b3
ACM
928 if (strlist__nr_entries(*list) == 1) {
929 fprintf(fp, "# %s: %s\n", list_name,
930 strlist__entry(*list, 0)->s);
931 se->elide = true;
932 }
cc8b88b1
ACM
933 }
934}
935
f37a291c 936int cmd_report(int argc, const char **argv, const char *prefix __used)
53cb8bc2 937{
b32d133a
ACM
938 if (symbol__init(&symbol_conf) < 0)
939 return -1;
53cb8bc2 940
edc52dea 941 argc = parse_options(argc, argv, options, report_usage, 0);
53cb8bc2 942
1aa16738
PZ
943 setup_sorting();
944
021191b3 945 if (parent_pattern != default_parent_pattern) {
b8e6d829 946 sort_dimension__add("parent");
021191b3
ACM
947 sort_parent.elide = 1;
948 } else
b8e6d829
IM
949 exclude_other = 0;
950
edc52dea
IM
951 /*
952 * Any (unrecognized) arguments left?
953 */
954 if (argc)
955 usage_with_options(report_usage, options);
956
a930d2c0
IM
957 setup_pager();
958
021191b3
ACM
959 setup_list(&dso_list, dso_list_str, &sort_dso, "dso", stdout);
960 setup_list(&comm_list, comm_list_str, &sort_comm, "comm", stdout);
961 setup_list(&sym_list, sym_list_str, &sort_sym, "symbol", stdout);
25903407 962
52d422de
ACM
963 if (field_sep && *field_sep == '.') {
964 fputs("'.' is the only non valid --field-separator argument\n",
965 stderr);
966 exit(129);
967 }
968
53cb8bc2
IM
969 return __cmd_report();
970}