perf tools: Unify perf.data mapping and events handling
[safe/jmp/linux-2.6] / 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
26 #include "util/parse-options.h"
27 #include "util/parse-events.h"
28
29 #include "util/data_map.h"
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 char             *dso_list_str, *comm_list_str, *sym_list_str,
37                         *col_width_list_str;
38 static struct strlist   *dso_list, *comm_list, *sym_list;
39
40 static int              force;
41
42 static int              full_paths;
43 static int              show_nr_samples;
44
45 static int              show_threads;
46 static struct perf_read_values  show_threads_values;
47
48 static char             default_pretty_printing_style[] = "normal";
49 static char             *pretty_printing_style = default_pretty_printing_style;
50
51 static int              exclude_other = 1;
52
53 static char             callchain_default_opt[] = "fractal,0.5";
54
55 static char             *cwd;
56 static int              cwdlen;
57
58 static struct rb_root   threads;
59 static struct thread    *last_match;
60
61 static struct perf_header *header;
62
63 static u64              sample_type;
64
65 static 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 }
82 static size_t
83 ipchain__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;
100                         ret += percent_color_fprintf(fp, "--%2.2f%%-- ", percent);
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
112 static struct symbol *rem_sq_bracket;
113 static struct callchain_list rem_hits;
114
115 static 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
127 static size_t
128 callchain__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;
135         u64 new_total;
136         u64 remaining;
137         size_t ret = 0;
138         int i;
139
140         if (callchain_param.mode == CHAIN_GRAPH_REL)
141                 new_total = self->children_hit;
142         else
143                 new_total = total_samples;
144
145         remaining = new_total;
146
147         node = rb_first(&self->rb_root);
148         while (node) {
149                 u64 cumul;
150
151                 child = rb_entry(node, struct callchain_node, rb_node);
152                 cumul = cumul_hits(child);
153                 remaining -= cumul;
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
158                  * level for the last child of this depth.
159                  * Except if we have remaining filtered hits. They will
160                  * supersede the last child
161                  */
162                 next = rb_next(node);
163                 if (!next && (callchain_param.mode != CHAIN_GRAPH_REL || !remaining))
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++,
177                                                       new_total,
178                                                       cumul);
179                 }
180                 ret += callchain__fprintf_graph(fp, child, new_total,
181                                                 depth + 1,
182                                                 new_depth_mask | (1 << depth));
183                 node = next;
184         }
185
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
199         return ret;
200 }
201
202 static size_t
203 callchain__fprintf_flat(FILE *fp, struct callchain_node *self,
204                         u64 total_samples)
205 {
206         struct callchain_list *chain;
207         size_t ret = 0;
208
209         if (!self)
210                 return 0;
211
212         ret += callchain__fprintf_flat(fp, self->parent, total_samples);
213
214
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",
222                                         (void *)(long)chain->ip);
223         }
224
225         return ret;
226 }
227
228 static size_t
229 hist_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;
242                 switch (callchain_param.mode) {
243                 case CHAIN_FLAT:
244                         ret += percent_color_fprintf(fp, "           %6.2f%%\n",
245                                                      percent);
246                         ret += callchain__fprintf_flat(fp, chain, total_samples);
247                         break;
248                 case CHAIN_GRAPH_ABS: /* Falldown */
249                 case CHAIN_GRAPH_REL:
250                         ret += callchain__fprintf_graph(fp, chain,
251                                                         total_samples, 1, 1);
252                 case CHAIN_NONE:
253                 default:
254                         break;
255                 }
256                 ret += fprintf(fp, "\n");
257                 rb_node = rb_next(rb_node);
258         }
259
260         return ret;
261 }
262
263 static size_t
264 hist_entry__fprintf(FILE *fp, struct hist_entry *self, u64 total_samples)
265 {
266         struct sort_entry *se;
267         size_t ret;
268
269         if (exclude_other && !self->parent)
270                 return 0;
271
272         if (total_samples)
273                 ret = percent_color_fprintf(fp,
274                                             field_sep ? "%.2f" : "   %6.2f%%",
275                                         (self->count * 100.0) / total_samples);
276         else
277                 ret = fprintf(fp, field_sep ? "%lld" : "%12lld ", self->count);
278
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         }
285
286         list_for_each_entry(se, &hist_entry__sort_list, list) {
287                 if (se->elide)
288                         continue;
289
290                 fprintf(fp, "%s", field_sep ?: "  ");
291                 ret += se->print(fp, self, se->width ? *se->width : 0);
292         }
293
294         ret += fprintf(fp, "\n");
295
296         if (callchain)
297                 hist_entry_callchain__fprintf(fp, self, total_samples);
298
299         return ret;
300 }
301
302 /*
303  *
304  */
305
306 static 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
318 static void thread__comm_adjust(struct thread *self)
319 {
320         char *comm = self->comm;
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         }
331 }
332
333 static 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);
341
342         return 0;
343 }
344
345
346 static struct symbol *
347 resolve_symbol(struct thread *thread, struct map **mapp, u64 *ipp)
348 {
349         struct map *map = mapp ? *mapp : NULL;
350         u64 ip = *ipp;
351
352         if (map)
353                 goto got_map;
354
355         if (!thread)
356                 return NULL;
357
358         map = thread__find_map(thread, ip);
359         if (map != NULL) {
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                  */
365                 if (!sort_dso.elide && !map->dso->slen_calculated)
366                         dso__calc_col_width(map->dso);
367
368                 if (mapp)
369                         *mapp = map;
370 got_map:
371                 ip = map->map_ip(map, ip);
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
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.
382                  */
383                 if ((long long)ip < 0)
384                         return kernel_maps__find_symbol(ip, mapp);
385         }
386         dump_printf(" ...... dso: %s\n",
387                     map ? map->dso->long_name : "<not found>");
388         dump_printf(" ...... map: %Lx -> %Lx\n", *ipp, ip);
389         *ipp  = ip;
390
391         return map ? map->dso->find_symbol(map->dso, ip) : NULL;
392 }
393
394 static int call__match(struct symbol *sym)
395 {
396         if (sym->name && !regexec(&parent_regex, sym->name, 0, NULL, 0))
397                 return 1;
398
399         return 0;
400 }
401
402 static struct symbol **resolve_callchain(struct thread *thread, struct map *map,
403                                          struct ip_callchain *chain,
404                                          struct symbol **parent)
405 {
406         u64 context = PERF_CONTEXT_MAX;
407         struct symbol **syms = NULL;
408         unsigned int i;
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];
420                 struct symbol *sym = NULL;
421
422                 if (ip >= PERF_CONTEXT_MAX) {
423                         context = ip;
424                         continue;
425                 }
426
427                 switch (context) {
428                 case PERF_CONTEXT_HV:
429                         break;
430                 case PERF_CONTEXT_KERNEL:
431                         sym = kernel_maps__find_symbol(ip, &map);
432                         break;
433                 default:
434                         sym = resolve_symbol(thread, &map, &ip);
435                         break;
436                 }
437
438                 if (sym) {
439                         if (sort__has_parent && !*parent && call__match(sym))
440                                 *parent = sym;
441                         if (!callchain)
442                                 break;
443                         syms[i] = sym;
444                 }
445         }
446
447         return syms;
448 }
449
450 /*
451  * collect histogram counts
452  */
453
454 static int
455 hist_entry__add(struct thread *thread, struct map *map,
456                 struct symbol *sym, u64 ip, struct ip_callchain *chain,
457                 char level, u64 count)
458 {
459         struct symbol **syms = NULL, *parent = NULL;
460         bool hit;
461         struct hist_entry *he;
462
463         if ((sort__has_parent || callchain) && chain)
464                 syms = resolve_callchain(thread, map, chain, &parent);
465
466         he = __hist_entry__add(thread, map, sym, parent,
467                                ip, count, level, &hit);
468         if (he == NULL)
469                 return -ENOMEM;
470
471         if (hit)
472                 he->count += count;
473
474         if (callchain) {
475                 if (!hit)
476                         callchain_init(&he->callchain);
477                 append_chain(&he->callchain, chain, syms);
478                 free(syms);
479         }
480
481         return 0;
482 }
483
484 static size_t output__fprintf(FILE *fp, u64 total_samples)
485 {
486         struct hist_entry *pos;
487         struct sort_entry *se;
488         struct rb_node *nd;
489         size_t ret = 0;
490         unsigned int width;
491         char *col_width = col_width_list_str;
492         int raw_printing_style;
493
494         raw_printing_style = !strcmp(pretty_printing_style, "raw");
495
496         init_rem_hits();
497
498         fprintf(fp, "# Samples: %Ld\n", (u64)total_samples);
499         fprintf(fp, "#\n");
500
501         fprintf(fp, "# Overhead");
502         if (show_nr_samples) {
503                 if (field_sep)
504                         fprintf(fp, "%cSamples", *field_sep);
505                 else
506                         fputs("  Samples  ", fp);
507         }
508         list_for_each_entry(se, &hist_entry__sort_list, list) {
509                 if (se->elide)
510                         continue;
511                 if (field_sep) {
512                         fprintf(fp, "%c%s", *field_sep, se->header);
513                         continue;
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);
528         }
529         fprintf(fp, "\n");
530
531         if (field_sep)
532                 goto print_entries;
533
534         fprintf(fp, "# ........");
535         if (show_nr_samples)
536                 fprintf(fp, " ..........");
537         list_for_each_entry(se, &hist_entry__sort_list, list) {
538                 unsigned int i;
539
540                 if (se->elide)
541                         continue;
542
543                 fprintf(fp, "  ");
544                 if (se->width)
545                         width = *se->width;
546                 else
547                         width = strlen(se->header);
548                 for (i = 0; i < width; i++)
549                         fprintf(fp, ".");
550         }
551         fprintf(fp, "\n");
552
553         fprintf(fp, "#\n");
554
555 print_entries:
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);
559         }
560
561         if (sort_order == default_sort_order &&
562                         parent_pattern == default_parent_pattern) {
563                 fprintf(fp, "#\n");
564                 fprintf(fp, "# (For a higher level overview, try: perf report --sort comm,dso)\n");
565                 fprintf(fp, "#\n");
566         }
567         fprintf(fp, "\n");
568
569         free(rem_sq_bracket);
570
571         if (show_threads)
572                 perf_read_values_display(fp, &show_threads_values,
573                                          raw_printing_style);
574
575         return ret;
576 }
577
578 static int validate_chain(struct ip_callchain *chain, event_t *event)
579 {
580         unsigned int chain_size;
581
582         chain_size = event->header.size;
583         chain_size -= (unsigned long)&event->ip.__more_data - (unsigned long)event;
584
585         if (chain->nr*sizeof(u64) > chain_size)
586                 return -1;
587
588         return 0;
589 }
590
591 static int
592 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
593 {
594         char level;
595         struct symbol *sym = NULL;
596         struct thread *thread;
597         u64 ip = event->ip.ip;
598         u64 period = 1;
599         struct map *map = NULL;
600         void *more_data = event->ip.__more_data;
601         struct ip_callchain *chain = NULL;
602         int cpumode;
603
604         thread = threads__findnew(event->ip.pid, &threads, &last_match);
605
606         if (sample_type & PERF_SAMPLE_PERIOD) {
607                 period = *(u64 *)more_data;
608                 more_data += sizeof(u64);
609         }
610
611         dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
612                 (void *)(offset + head),
613                 (void *)(long)(event->header.size),
614                 event->header.misc,
615                 event->ip.pid, event->ip.tid,
616                 (void *)(long)ip,
617                 (long long)period);
618
619         if (sample_type & PERF_SAMPLE_CALLCHAIN) {
620                 unsigned int i;
621
622                 chain = (void *)more_data;
623
624                 dump_printf("... chain: nr:%Lu\n", chain->nr);
625
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) {
632                         for (i = 0; i < chain->nr; i++)
633                                 dump_printf("..... %2d: %016Lx\n", i, chain->ips[i]);
634                 }
635         }
636
637         dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
638
639         if (thread == NULL) {
640                 eprintf("problem processing %d event, skipping it.\n",
641                         event->header.type);
642                 return -1;
643         }
644
645         if (comm_list && !strlist__has_entry(comm_list, thread->comm))
646                 return 0;
647
648         cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
649
650         if (cpumode == PERF_RECORD_MISC_KERNEL) {
651                 level = 'k';
652                 sym = kernel_maps__find_symbol(ip, &map);
653                 dump_printf(" ...... dso: %s\n",
654                             map ? map->dso->long_name : "<not found>");
655         } else if (cpumode == PERF_RECORD_MISC_USER) {
656                 level = '.';
657                 sym = resolve_symbol(thread, &map, &ip);
658
659         } else {
660                 level = 'H';
661                 dump_printf(" ...... dso: [hypervisor]\n");
662         }
663
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;
670
671         if (sym_list && sym && !strlist__has_entry(sym_list, sym->name))
672                 return 0;
673
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;
678         }
679
680         total += period;
681
682         return 0;
683 }
684
685 static int
686 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
687 {
688         struct thread *thread;
689         struct map *map = map__new(&event->mmap, cwd, cwdlen);
690
691         thread = threads__findnew(event->mmap.pid, &threads, &last_match);
692
693         dump_printf("%p [%p]: PERF_RECORD_MMAP %d/%d: [%p(%p) @ %p]: %s\n",
694                 (void *)(offset + head),
695                 (void *)(long)(event->header.size),
696                 event->mmap.pid,
697                 event->mmap.tid,
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) {
704                 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
705                 return 0;
706         }
707
708         thread__insert_map(thread, map);
709         total_mmap++;
710
711         return 0;
712 }
713
714 static int
715 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
716 {
717         struct thread *thread;
718
719         thread = threads__findnew(event->comm.pid, &threads, &last_match);
720
721         dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
722                 (void *)(offset + head),
723                 (void *)(long)(event->header.size),
724                 event->comm.comm, event->comm.pid);
725
726         if (thread == NULL ||
727             thread__set_comm_adjust(thread, event->comm.comm)) {
728                 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
729                 return -1;
730         }
731         total_comm++;
732
733         return 0;
734 }
735
736 static int
737 process_task_event(event_t *event, unsigned long offset, unsigned long head)
738 {
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);
744
745         dump_printf("%p [%p]: PERF_RECORD_%s: (%d:%d):(%d:%d)\n",
746                 (void *)(offset + head),
747                 (void *)(long)(event->header.size),
748                 event->header.type == PERF_RECORD_FORK ? "FORK" : "EXIT",
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
759         if (event->header.type == PERF_RECORD_EXIT)
760                 return 0;
761
762         if (!thread || !parent || thread__fork(thread, parent)) {
763                 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
764                 return -1;
765         }
766         total_fork++;
767
768         return 0;
769 }
770
771 static int
772 process_lost_event(event_t *event, unsigned long offset, unsigned long head)
773 {
774         dump_printf("%p [%p]: PERF_RECORD_LOST: id:%Ld: lost:%Ld\n",
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
785 static int
786 process_read_event(event_t *event, unsigned long offset, unsigned long head)
787 {
788         struct perf_event_attr *attr;
789
790         attr = perf_header__find_attr(event->read.id, header);
791
792         if (show_threads) {
793                 const char *name = attr ? __event_name(attr->type, attr->config)
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
802         dump_printf("%p [%p]: PERF_RECORD_READ: %d %d %s %Lu\n",
803                         (void *)(offset + head),
804                         (void *)(long)(event->header.size),
805                         event->read.pid,
806                         event->read.tid,
807                         attr ? __event_name(attr->type, attr->config)
808                              : "FAIL",
809                         event->read.value);
810
811         return 0;
812 }
813
814 static int sample_type_check(u64 type)
815 {
816         sample_type = type;
817
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");
823                         return -1;
824                 }
825                 if (callchain) {
826                         fprintf(stderr, "selected -g but no callchain data."
827                                         " Did you call perf record without"
828                                         " -g?\n");
829                         return -1;
830                 }
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");
836                                 return -1;
837                         }
838         }
839
840         return 0;
841 }
842
843 static 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 };
853
854
855 static int __cmd_report(void)
856 {
857         struct thread *idle;
858         int ret;
859
860         idle = register_idle_thread(&threads, &last_match);
861         thread__comm_adjust(idle);
862
863         if (show_threads)
864                 perf_read_values_init(&show_threads_values);
865
866         register_perf_file_handler(&file_handler);
867
868         ret = mmap_dispatch_perf_file(&header, input_name, force, full_paths,
869                                       &cwdlen, &cwd);
870         if (ret)
871                 return ret;
872
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);
878         dump_printf(" unknown events: %10ld\n", file_handler.total_unknown);
879
880         if (dump_trace)
881                 return 0;
882
883         if (verbose >= 3)
884                 threads__fprintf(stdout, &threads);
885
886         if (verbose >= 2)
887                 dsos__fprintf(stdout);
888
889         collapse__resort();
890         output__resort(total);
891         output__fprintf(stdout, total);
892
893         if (show_threads)
894                 perf_read_values_destroy(&show_threads_values);
895
896         return ret;
897 }
898
899 static int
900 parse_callchain_opt(const struct option *opt __used, const char *arg,
901                     int unset __used)
902 {
903         char *tok;
904         char *endptr;
905
906         callchain = 1;
907
908         if (!arg)
909                 return 0;
910
911         tok = strtok((char *)arg, ",");
912         if (!tok)
913                 return -1;
914
915         /* get the output mode */
916         if (!strncmp(tok, "graph", strlen(arg)))
917                 callchain_param.mode = CHAIN_GRAPH_ABS;
918
919         else if (!strncmp(tok, "flat", strlen(arg)))
920                 callchain_param.mode = CHAIN_FLAT;
921
922         else if (!strncmp(tok, "fractal", strlen(arg)))
923                 callchain_param.mode = CHAIN_GRAPH_REL;
924
925         else if (!strncmp(tok, "none", strlen(arg))) {
926                 callchain_param.mode = CHAIN_NONE;
927                 callchain = 0;
928
929                 return 0;
930         }
931
932         else
933                 return -1;
934
935         /* get the min percentage */
936         tok = strtok(NULL, ",");
937         if (!tok)
938                 goto setup;
939
940         callchain_param.min_percent = strtod(tok, &endptr);
941         if (tok == endptr)
942                 return -1;
943
944 setup:
945         if (register_callchain_param(&callchain_param) < 0) {
946                 fprintf(stderr, "Can't register callchain params\n");
947                 return -1;
948         }
949         return 0;
950 }
951
952 //static const char * const report_usage[] = {
953 const char * const report_usage[] = {
954         "perf report [<options>] <command>",
955         NULL
956 };
957
958 static const struct option options[] = {
959         OPT_STRING('i', "input", &input_name, "file",
960                     "input file name"),
961         OPT_BOOLEAN('v', "verbose", &verbose,
962                     "be more verbose (show symbol address, etc)"),
963         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
964                     "dump raw trace in ASCII"),
965         OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
966         OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
967         OPT_BOOLEAN('m', "modules", &modules,
968                     "load module symbols - WARNING: use only with -k and LIVE kernel"),
969         OPT_BOOLEAN('n', "show-nr-samples", &show_nr_samples,
970                     "Show a column with the number of samples"),
971         OPT_BOOLEAN('T', "threads", &show_threads,
972                     "Show per-thread event counters"),
973         OPT_STRING(0, "pretty", &pretty_printing_style, "key",
974                    "pretty printing style key: normal raw"),
975         OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
976                    "sort by key(s): pid, comm, dso, symbol, parent"),
977         OPT_BOOLEAN('P', "full-paths", &full_paths,
978                     "Don't shorten the pathnames taking into account the cwd"),
979         OPT_STRING('p', "parent", &parent_pattern, "regex",
980                    "regex filter to identify parent, see: '--sort parent'"),
981         OPT_BOOLEAN('x', "exclude-other", &exclude_other,
982                     "Only display entries with parent-match"),
983         OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
984                      "Display callchains using output_type and min percent threshold. "
985                      "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
986         OPT_STRING('d', "dsos", &dso_list_str, "dso[,dso...]",
987                    "only consider symbols in these dsos"),
988         OPT_STRING('C', "comms", &comm_list_str, "comm[,comm...]",
989                    "only consider symbols in these comms"),
990         OPT_STRING('S', "symbols", &sym_list_str, "symbol[,symbol...]",
991                    "only consider these symbols"),
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."),
998         OPT_END()
999 };
1000
1001 static 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
1016 static void setup_list(struct strlist **list, const char *list_str,
1017                        struct sort_entry *se, const char *list_name,
1018                        FILE *fp)
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                 }
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                 }
1032         }
1033 }
1034
1035 int cmd_report(int argc, const char **argv, const char *prefix __used)
1036 {
1037         symbol__init();
1038
1039         argc = parse_options(argc, argv, options, report_usage, 0);
1040
1041         setup_sorting();
1042
1043         if (parent_pattern != default_parent_pattern) {
1044                 sort_dimension__add("parent");
1045                 sort_parent.elide = 1;
1046         } else
1047                 exclude_other = 0;
1048
1049         /*
1050          * Any (unrecognized) arguments left?
1051          */
1052         if (argc)
1053                 usage_with_options(report_usage, options);
1054
1055         setup_pager();
1056
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);
1060
1061         if (field_sep && *field_sep == '.') {
1062                 fputs("'.' is the only non valid --field-separator argument\n",
1063                       stderr);
1064                 exit(129);
1065         }
1066
1067         return __cmd_report();
1068 }