perf bench: Add new subcommand 'bench' to perf.c
[safe/jmp/linux-2.6] / tools / perf / builtin-annotate.c
1 /*
2  * builtin-annotate.c
3  *
4  * Builtin annotate 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
19 #include "perf.h"
20 #include "util/debug.h"
21
22 #include "util/parse-options.h"
23 #include "util/parse-events.h"
24 #include "util/thread.h"
25 #include "util/sort.h"
26 #include "util/hist.h"
27
28 static char             const *input_name = "perf.data";
29
30 static int              force;
31 static int              input;
32
33 static int              full_paths;
34
35 static int              print_line;
36
37 static unsigned long    page_size;
38 static unsigned long    mmap_window = 32;
39
40 struct sym_hist {
41         u64             sum;
42         u64             ip[0];
43 };
44
45 struct sym_ext {
46         struct rb_node  node;
47         double          percent;
48         char            *path;
49 };
50
51 struct sym_priv {
52         struct sym_hist *hist;
53         struct sym_ext  *ext;
54 };
55
56 static const char *sym_hist_filter;
57
58 static int symbol_filter(struct map *map __used, struct symbol *sym)
59 {
60         if (sym_hist_filter == NULL ||
61             strcmp(sym->name, sym_hist_filter) == 0) {
62                 struct sym_priv *priv = symbol__priv(sym);
63                 const int size = (sizeof(*priv->hist) +
64                                   (sym->end - sym->start) * sizeof(u64));
65
66                 priv->hist = malloc(size);
67                 if (priv->hist)
68                         memset(priv->hist, 0, size);
69                 return 0;
70         }
71         /*
72          * FIXME: We should really filter it out, as we don't want to go thru symbols
73          * we're not interested, and if a DSO ends up with no symbols, delete it too,
74          * but right now the kernel loading routines in symbol.c bail out if no symbols
75          * are found, fix it later.
76          */
77         return 0;
78 }
79
80 /*
81  * collect histogram counts
82  */
83 static void hist_hit(struct hist_entry *he, u64 ip)
84 {
85         unsigned int sym_size, offset;
86         struct symbol *sym = he->sym;
87         struct sym_priv *priv;
88         struct sym_hist *h;
89
90         he->count++;
91
92         if (!sym || !he->map)
93                 return;
94
95         priv = symbol__priv(sym);
96         if (!priv->hist)
97                 return;
98
99         sym_size = sym->end - sym->start;
100         offset = ip - sym->start;
101
102         if (verbose)
103                 fprintf(stderr, "%s: ip=%Lx\n", __func__,
104                         he->map->unmap_ip(he->map, ip));
105
106         if (offset >= sym_size)
107                 return;
108
109         h = priv->hist;
110         h->sum++;
111         h->ip[offset]++;
112
113         if (verbose >= 3)
114                 printf("%p %s: count++ [ip: %p, %08Lx] => %Ld\n",
115                         (void *)(unsigned long)he->sym->start,
116                         he->sym->name,
117                         (void *)(unsigned long)ip, ip - he->sym->start,
118                         h->ip[offset]);
119 }
120
121 static int hist_entry__add(struct thread *thread, struct map *map,
122                            struct symbol *sym, u64 ip, u64 count, char level)
123 {
124         bool hit;
125         struct hist_entry *he = __hist_entry__add(thread, map, sym, NULL, ip,
126                                                   count, level, &hit);
127         if (he == NULL)
128                 return -ENOMEM;
129         hist_hit(he, ip);
130         return 0;
131 }
132
133 static int
134 process_sample_event(event_t *event, unsigned long offset, unsigned long head)
135 {
136         char level;
137         u64 ip = event->ip.ip;
138         struct map *map = NULL;
139         struct symbol *sym = NULL;
140         struct thread *thread = threads__findnew(event->ip.pid);
141
142         dump_printf("%p [%p]: PERF_EVENT (IP, %d): %d: %p\n",
143                 (void *)(offset + head),
144                 (void *)(long)(event->header.size),
145                 event->header.misc,
146                 event->ip.pid,
147                 (void *)(long)ip);
148
149         if (thread == NULL) {
150                 fprintf(stderr, "problem processing %d event, skipping it.\n",
151                         event->header.type);
152                 return -1;
153         }
154
155         dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);
156
157         if (event->header.misc & PERF_RECORD_MISC_KERNEL) {
158                 level = 'k';
159                 sym = kernel_maps__find_symbol(ip, &map);
160                 dump_printf(" ...... dso: %s\n",
161                             map ? map->dso->long_name : "<not found>");
162         } else if (event->header.misc & PERF_RECORD_MISC_USER) {
163                 level = '.';
164                 map = thread__find_map(thread, ip);
165                 if (map != NULL) {
166 got_map:
167                         ip = map->map_ip(map, ip);
168                         sym = map__find_symbol(map, ip, symbol_filter);
169                 } else {
170                         /*
171                          * If this is outside of all known maps,
172                          * and is a negative address, try to look it
173                          * up in the kernel dso, as it might be a
174                          * vsyscall or vdso (which executes in user-mode).
175                          *
176                          * XXX This is nasty, we should have a symbol list in
177                          * the "[vdso]" dso, but for now lets use the old
178                          * trick of looking in the whole kernel symbol list.
179                          */
180                         if ((long long)ip < 0) {
181                                 map = kernel_map;
182                                 goto got_map;
183                         }
184                 }
185                 dump_printf(" ...... dso: %s\n",
186                             map ? map->dso->long_name : "<not found>");
187         } else {
188                 level = 'H';
189                 dump_printf(" ...... dso: [hypervisor]\n");
190         }
191
192         if (hist_entry__add(thread, map, sym, ip, 1, level)) {
193                 fprintf(stderr, "problem incrementing symbol count, "
194                                 "skipping event\n");
195                 return -1;
196         }
197         total++;
198
199         return 0;
200 }
201
202 static int
203 process_mmap_event(event_t *event, unsigned long offset, unsigned long head)
204 {
205         struct map *map = map__new(&event->mmap, NULL, 0);
206         struct thread *thread = threads__findnew(event->mmap.pid);
207
208         dump_printf("%p [%p]: PERF_RECORD_MMAP %d: [%p(%p) @ %p]: %s\n",
209                 (void *)(offset + head),
210                 (void *)(long)(event->header.size),
211                 event->mmap.pid,
212                 (void *)(long)event->mmap.start,
213                 (void *)(long)event->mmap.len,
214                 (void *)(long)event->mmap.pgoff,
215                 event->mmap.filename);
216
217         if (thread == NULL || map == NULL) {
218                 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
219                 return 0;
220         }
221
222         thread__insert_map(thread, map);
223         total_mmap++;
224
225         return 0;
226 }
227
228 static int
229 process_comm_event(event_t *event, unsigned long offset, unsigned long head)
230 {
231         struct thread *thread = threads__findnew(event->comm.pid);
232
233         dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
234                 (void *)(offset + head),
235                 (void *)(long)(event->header.size),
236                 event->comm.comm, event->comm.pid);
237
238         if (thread == NULL ||
239             thread__set_comm(thread, event->comm.comm)) {
240                 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
241                 return -1;
242         }
243         total_comm++;
244
245         return 0;
246 }
247
248 static int
249 process_fork_event(event_t *event, unsigned long offset, unsigned long head)
250 {
251         struct thread *thread = threads__findnew(event->fork.pid);
252         struct thread *parent = threads__findnew(event->fork.ppid);
253
254         dump_printf("%p [%p]: PERF_RECORD_FORK: %d:%d\n",
255                 (void *)(offset + head),
256                 (void *)(long)(event->header.size),
257                 event->fork.pid, event->fork.ppid);
258
259         /*
260          * A thread clone will have the same PID for both
261          * parent and child.
262          */
263         if (thread == parent)
264                 return 0;
265
266         if (!thread || !parent || thread__fork(thread, parent)) {
267                 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
268                 return -1;
269         }
270         total_fork++;
271
272         return 0;
273 }
274
275 static int
276 process_event(event_t *event, unsigned long offset, unsigned long head)
277 {
278         switch (event->header.type) {
279         case PERF_RECORD_SAMPLE:
280                 return process_sample_event(event, offset, head);
281
282         case PERF_RECORD_MMAP:
283                 return process_mmap_event(event, offset, head);
284
285         case PERF_RECORD_COMM:
286                 return process_comm_event(event, offset, head);
287
288         case PERF_RECORD_FORK:
289                 return process_fork_event(event, offset, head);
290         /*
291          * We dont process them right now but they are fine:
292          */
293
294         case PERF_RECORD_THROTTLE:
295         case PERF_RECORD_UNTHROTTLE:
296                 return 0;
297
298         default:
299                 return -1;
300         }
301
302         return 0;
303 }
304
305 static int parse_line(FILE *file, struct hist_entry *he, u64 len)
306 {
307         struct symbol *sym = he->sym;
308         char *line = NULL, *tmp, *tmp2;
309         static const char *prev_line;
310         static const char *prev_color;
311         unsigned int offset;
312         size_t line_len;
313         u64 start;
314         s64 line_ip;
315         int ret;
316         char *c;
317
318         if (getline(&line, &line_len, file) < 0)
319                 return -1;
320         if (!line)
321                 return -1;
322
323         c = strchr(line, '\n');
324         if (c)
325                 *c = 0;
326
327         line_ip = -1;
328         offset = 0;
329         ret = -2;
330
331         /*
332          * Strip leading spaces:
333          */
334         tmp = line;
335         while (*tmp) {
336                 if (*tmp != ' ')
337                         break;
338                 tmp++;
339         }
340
341         if (*tmp) {
342                 /*
343                  * Parse hexa addresses followed by ':'
344                  */
345                 line_ip = strtoull(tmp, &tmp2, 16);
346                 if (*tmp2 != ':')
347                         line_ip = -1;
348         }
349
350         start = he->map->unmap_ip(he->map, sym->start);
351
352         if (line_ip != -1) {
353                 const char *path = NULL;
354                 unsigned int hits = 0;
355                 double percent = 0.0;
356                 const char *color;
357                 struct sym_priv *priv = symbol__priv(sym);
358                 struct sym_ext *sym_ext = priv->ext;
359                 struct sym_hist *h = priv->hist;
360
361                 offset = line_ip - start;
362                 if (offset < len)
363                         hits = h->ip[offset];
364
365                 if (offset < len && sym_ext) {
366                         path = sym_ext[offset].path;
367                         percent = sym_ext[offset].percent;
368                 } else if (h->sum)
369                         percent = 100.0 * hits / h->sum;
370
371                 color = get_percent_color(percent);
372
373                 /*
374                  * Also color the filename and line if needed, with
375                  * the same color than the percentage. Don't print it
376                  * twice for close colored ip with the same filename:line
377                  */
378                 if (path) {
379                         if (!prev_line || strcmp(prev_line, path)
380                                        || color != prev_color) {
381                                 color_fprintf(stdout, color, " %s", path);
382                                 prev_line = path;
383                                 prev_color = color;
384                         }
385                 }
386
387                 color_fprintf(stdout, color, " %7.2f", percent);
388                 printf(" :      ");
389                 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", line);
390         } else {
391                 if (!*line)
392                         printf("         :\n");
393                 else
394                         printf("         :      %s\n", line);
395         }
396
397         return 0;
398 }
399
400 static struct rb_root root_sym_ext;
401
402 static void insert_source_line(struct sym_ext *sym_ext)
403 {
404         struct sym_ext *iter;
405         struct rb_node **p = &root_sym_ext.rb_node;
406         struct rb_node *parent = NULL;
407
408         while (*p != NULL) {
409                 parent = *p;
410                 iter = rb_entry(parent, struct sym_ext, node);
411
412                 if (sym_ext->percent > iter->percent)
413                         p = &(*p)->rb_left;
414                 else
415                         p = &(*p)->rb_right;
416         }
417
418         rb_link_node(&sym_ext->node, parent, p);
419         rb_insert_color(&sym_ext->node, &root_sym_ext);
420 }
421
422 static void free_source_line(struct hist_entry *he, int len)
423 {
424         struct sym_priv *priv = symbol__priv(he->sym);
425         struct sym_ext *sym_ext = priv->ext;
426         int i;
427
428         if (!sym_ext)
429                 return;
430
431         for (i = 0; i < len; i++)
432                 free(sym_ext[i].path);
433         free(sym_ext);
434
435         priv->ext = NULL;
436         root_sym_ext = RB_ROOT;
437 }
438
439 /* Get the filename:line for the colored entries */
440 static void
441 get_source_line(struct hist_entry *he, int len, const char *filename)
442 {
443         struct symbol *sym = he->sym;
444         u64 start;
445         int i;
446         char cmd[PATH_MAX * 2];
447         struct sym_ext *sym_ext;
448         struct sym_priv *priv = symbol__priv(sym);
449         struct sym_hist *h = priv->hist;
450
451         if (!h->sum)
452                 return;
453
454         sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
455         if (!priv->ext)
456                 return;
457
458         start = he->map->unmap_ip(he->map, sym->start);
459
460         for (i = 0; i < len; i++) {
461                 char *path = NULL;
462                 size_t line_len;
463                 u64 offset;
464                 FILE *fp;
465
466                 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
467                 if (sym_ext[i].percent <= 0.5)
468                         continue;
469
470                 offset = start + i;
471                 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
472                 fp = popen(cmd, "r");
473                 if (!fp)
474                         continue;
475
476                 if (getline(&path, &line_len, fp) < 0 || !line_len)
477                         goto next;
478
479                 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
480                 if (!sym_ext[i].path)
481                         goto next;
482
483                 strcpy(sym_ext[i].path, path);
484                 insert_source_line(&sym_ext[i]);
485
486         next:
487                 pclose(fp);
488         }
489 }
490
491 static void print_summary(const char *filename)
492 {
493         struct sym_ext *sym_ext;
494         struct rb_node *node;
495
496         printf("\nSorted summary for file %s\n", filename);
497         printf("----------------------------------------------\n\n");
498
499         if (RB_EMPTY_ROOT(&root_sym_ext)) {
500                 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
501                 return;
502         }
503
504         node = rb_first(&root_sym_ext);
505         while (node) {
506                 double percent;
507                 const char *color;
508                 char *path;
509
510                 sym_ext = rb_entry(node, struct sym_ext, node);
511                 percent = sym_ext->percent;
512                 color = get_percent_color(percent);
513                 path = sym_ext->path;
514
515                 color_fprintf(stdout, color, " %7.2f %s", percent, path);
516                 node = rb_next(node);
517         }
518 }
519
520 static void annotate_sym(struct hist_entry *he)
521 {
522         struct map *map = he->map;
523         struct dso *dso = map->dso;
524         struct symbol *sym = he->sym;
525         const char *filename = dso->long_name, *d_filename;
526         u64 len;
527         char command[PATH_MAX*2];
528         FILE *file;
529
530         if (!filename)
531                 return;
532
533         if (verbose)
534                 fprintf(stderr, "%s: filename=%s, sym=%s, start=%Lx, end=%Lx\n",
535                         __func__, filename, sym->name,
536                         map->unmap_ip(map, sym->start),
537                         map->unmap_ip(map, sym->end));
538
539         if (full_paths)
540                 d_filename = filename;
541         else
542                 d_filename = basename(filename);
543
544         len = sym->end - sym->start;
545
546         if (print_line) {
547                 get_source_line(he, len, filename);
548                 print_summary(filename);
549         }
550
551         printf("\n\n------------------------------------------------\n");
552         printf(" Percent |      Source code & Disassembly of %s\n", d_filename);
553         printf("------------------------------------------------\n");
554
555         if (verbose >= 2)
556                 printf("annotating [%p] %30s : [%p] %30s\n",
557                        dso, dso->long_name, sym, sym->name);
558
559         sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
560                 map->unmap_ip(map, sym->start), map->unmap_ip(map, sym->end),
561                 filename, filename);
562
563         if (verbose >= 3)
564                 printf("doing: %s\n", command);
565
566         file = popen(command, "r");
567         if (!file)
568                 return;
569
570         while (!feof(file)) {
571                 if (parse_line(file, he, len) < 0)
572                         break;
573         }
574
575         pclose(file);
576         if (print_line)
577                 free_source_line(he, len);
578 }
579
580 static void find_annotations(void)
581 {
582         struct rb_node *nd;
583
584         for (nd = rb_first(&output_hists); nd; nd = rb_next(nd)) {
585                 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
586                 struct sym_priv *priv;
587
588                 if (he->sym == NULL)
589                         continue;
590
591                 priv = symbol__priv(he->sym);
592                 if (priv->hist == NULL)
593                         continue;
594
595                 annotate_sym(he);
596                 /*
597                  * Since we have a hist_entry per IP for the same symbol, free
598                  * he->sym->hist to signal we already processed this symbol.
599                  */
600                 free(priv->hist);
601                 priv->hist = NULL;
602         }
603 }
604
605 static int __cmd_annotate(void)
606 {
607         int ret, rc = EXIT_FAILURE;
608         unsigned long offset = 0;
609         unsigned long head = 0;
610         struct stat input_stat;
611         event_t *event;
612         uint32_t size;
613         char *buf;
614
615         register_idle_thread();
616
617         input = open(input_name, O_RDONLY);
618         if (input < 0) {
619                 perror("failed to open file");
620                 exit(-1);
621         }
622
623         ret = fstat(input, &input_stat);
624         if (ret < 0) {
625                 perror("failed to stat file");
626                 exit(-1);
627         }
628
629         if (!force && input_stat.st_uid && (input_stat.st_uid != geteuid())) {
630                 fprintf(stderr, "file: %s not owned by current user or root\n", input_name);
631                 exit(-1);
632         }
633
634         if (!input_stat.st_size) {
635                 fprintf(stderr, "zero-sized file, nothing to do!\n");
636                 exit(0);
637         }
638
639         if (load_kernel(symbol_filter) < 0) {
640                 perror("failed to load kernel symbols");
641                 return EXIT_FAILURE;
642         }
643
644 remap:
645         buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ,
646                            MAP_SHARED, input, offset);
647         if (buf == MAP_FAILED) {
648                 perror("failed to mmap file");
649                 exit(-1);
650         }
651
652 more:
653         event = (event_t *)(buf + head);
654
655         size = event->header.size;
656         if (!size)
657                 size = 8;
658
659         if (head + event->header.size >= page_size * mmap_window) {
660                 unsigned long shift = page_size * (head / page_size);
661                 int munmap_ret;
662
663                 munmap_ret = munmap(buf, page_size * mmap_window);
664                 assert(munmap_ret == 0);
665
666                 offset += shift;
667                 head -= shift;
668                 goto remap;
669         }
670
671         size = event->header.size;
672
673         dump_printf("%p [%p]: event: %d\n",
674                         (void *)(offset + head),
675                         (void *)(long)event->header.size,
676                         event->header.type);
677
678         if (!size || process_event(event, offset, head) < 0) {
679
680                 dump_printf("%p [%p]: skipping unknown header type: %d\n",
681                         (void *)(offset + head),
682                         (void *)(long)(event->header.size),
683                         event->header.type);
684
685                 total_unknown++;
686
687                 /*
688                  * assume we lost track of the stream, check alignment, and
689                  * increment a single u64 in the hope to catch on again 'soon'.
690                  */
691
692                 if (unlikely(head & 7))
693                         head &= ~7ULL;
694
695                 size = 8;
696         }
697
698         head += size;
699
700         if (offset + head < (unsigned long)input_stat.st_size)
701                 goto more;
702
703         rc = EXIT_SUCCESS;
704         close(input);
705
706         dump_printf("      IP events: %10ld\n", total);
707         dump_printf("    mmap events: %10ld\n", total_mmap);
708         dump_printf("    comm events: %10ld\n", total_comm);
709         dump_printf("    fork events: %10ld\n", total_fork);
710         dump_printf(" unknown events: %10ld\n", total_unknown);
711
712         if (dump_trace)
713                 return 0;
714
715         if (verbose > 3)
716                 threads__fprintf(stdout);
717
718         if (verbose > 2)
719                 dsos__fprintf(stdout);
720
721         collapse__resort();
722         output__resort(total);
723
724         find_annotations();
725
726         return rc;
727 }
728
729 static const char * const annotate_usage[] = {
730         "perf annotate [<options>] <command>",
731         NULL
732 };
733
734 static const struct option options[] = {
735         OPT_STRING('i', "input", &input_name, "file",
736                     "input file name"),
737         OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
738                     "symbol to annotate"),
739         OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
740         OPT_BOOLEAN('v', "verbose", &verbose,
741                     "be more verbose (show symbol address, etc)"),
742         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
743                     "dump raw trace in ASCII"),
744         OPT_STRING('k', "vmlinux", &vmlinux_name, "file", "vmlinux pathname"),
745         OPT_BOOLEAN('m', "modules", &modules,
746                     "load module symbols - WARNING: use only with -k and LIVE kernel"),
747         OPT_BOOLEAN('l', "print-line", &print_line,
748                     "print matching source lines (may be slow)"),
749         OPT_BOOLEAN('P', "full-paths", &full_paths,
750                     "Don't shorten the displayed pathnames"),
751         OPT_END()
752 };
753
754 static void setup_sorting(void)
755 {
756         char *tmp, *tok, *str = strdup(sort_order);
757
758         for (tok = strtok_r(str, ", ", &tmp);
759                         tok; tok = strtok_r(NULL, ", ", &tmp)) {
760                 if (sort_dimension__add(tok) < 0) {
761                         error("Unknown --sort key: `%s'", tok);
762                         usage_with_options(annotate_usage, options);
763                 }
764         }
765
766         free(str);
767 }
768
769 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
770 {
771         symbol__init(sizeof(struct sym_priv));
772
773         page_size = getpagesize();
774
775         argc = parse_options(argc, argv, options, annotate_usage, 0);
776
777         setup_sorting();
778
779         if (argc) {
780                 /*
781                  * Special case: if there's an argument left then assume tha
782                  * it's a symbol filter:
783                  */
784                 if (argc > 1)
785                         usage_with_options(annotate_usage, options);
786
787                 sym_hist_filter = argv[0];
788         }
789
790         setup_pager();
791
792         if (field_sep && *field_sep == '.') {
793                 fputs("'.' is the only non valid --field-separator argument\n",
794                                 stderr);
795                 exit(129);
796         }
797
798         return __cmd_annotate();
799 }