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