perf hist: Introduce hists class and move lots of methods to it
[pandora-kernel.git] / 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
18 #include "perf.h"
19 #include "util/debug.h"
20
21 #include "util/event.h"
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 #include "util/session.h"
28
29 static char             const *input_name = "perf.data";
30
31 static bool             force;
32
33 static bool             full_paths;
34
35 static bool             print_line;
36
37 struct sym_hist {
38         u64             sum;
39         u64             ip[0];
40 };
41
42 struct sym_ext {
43         struct rb_node  node;
44         double          percent;
45         char            *path;
46 };
47
48 struct sym_priv {
49         struct sym_hist *hist;
50         struct sym_ext  *ext;
51 };
52
53 static const char *sym_hist_filter;
54
55 static int sym__alloc_hist(struct symbol *self)
56 {
57         struct sym_priv *priv = symbol__priv(self);
58         const int size = (sizeof(*priv->hist) +
59                           (self->end - self->start) * sizeof(u64));
60
61         priv->hist = zalloc(size);
62         return priv->hist == NULL ? -1 : 0;
63 }
64
65 /*
66  * collect histogram counts
67  */
68 static int annotate__hist_hit(struct hist_entry *he, u64 ip)
69 {
70         unsigned int sym_size, offset;
71         struct symbol *sym = he->ms.sym;
72         struct sym_priv *priv;
73         struct sym_hist *h;
74
75         if (!sym || !he->ms.map)
76                 return 0;
77
78         priv = symbol__priv(sym);
79         if (priv->hist == NULL && sym__alloc_hist(sym) < 0)
80                 return -ENOMEM;
81
82         sym_size = sym->end - sym->start;
83         offset = ip - sym->start;
84
85         pr_debug3("%s: ip=%#Lx\n", __func__, he->ms.map->unmap_ip(he->ms.map, ip));
86
87         if (offset >= sym_size)
88                 return 0;
89
90         h = priv->hist;
91         h->sum++;
92         h->ip[offset]++;
93
94         pr_debug3("%#Lx %s: count++ [ip: %#Lx, %#Lx] => %Ld\n", he->ms.sym->start,
95                   he->ms.sym->name, ip, ip - he->ms.sym->start, h->ip[offset]);
96         return 0;
97 }
98
99 static int hists__add_entry(struct hists *self, struct addr_location *al)
100 {
101         struct hist_entry *he;
102
103         if (sym_hist_filter != NULL &&
104             (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) {
105                 /* We're only interested in a symbol named sym_hist_filter */
106                 if (al->sym != NULL) {
107                         rb_erase(&al->sym->rb_node,
108                                  &al->map->dso->symbols[al->map->type]);
109                         symbol__delete(al->sym);
110                 }
111                 return 0;
112         }
113
114         he = __hists__add_entry(self, al, NULL, 1);
115         if (he == NULL)
116                 return -ENOMEM;
117
118         return annotate__hist_hit(he, al->addr);
119 }
120
121 static int process_sample_event(event_t *event, struct perf_session *session)
122 {
123         struct addr_location al;
124
125         dump_printf("(IP, %d): %d: %#Lx\n", event->header.misc,
126                     event->ip.pid, event->ip.ip);
127
128         if (event__preprocess_sample(event, session, &al, NULL) < 0) {
129                 pr_warning("problem processing %d event, skipping it.\n",
130                            event->header.type);
131                 return -1;
132         }
133
134         if (!al.filtered && hists__add_entry(&session->hists, &al)) {
135                 pr_warning("problem incrementing symbol count, "
136                            "skipping event\n");
137                 return -1;
138         }
139
140         return 0;
141 }
142
143 struct objdump_line {
144         struct list_head node;
145         s64              offset;
146         char             *line;
147 };
148
149 static struct objdump_line *objdump_line__new(s64 offset, char *line)
150 {
151         struct objdump_line *self = malloc(sizeof(*self));
152
153         if (self != NULL) {
154                 self->offset = offset;
155                 self->line = line;
156         }
157
158         return self;
159 }
160
161 static void objdump_line__free(struct objdump_line *self)
162 {
163         free(self->line);
164         free(self);
165 }
166
167 static void objdump__add_line(struct list_head *head, struct objdump_line *line)
168 {
169         list_add_tail(&line->node, head);
170 }
171
172 static struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
173                                                       struct objdump_line *pos)
174 {
175         list_for_each_entry_continue(pos, head, node)
176                 if (pos->offset >= 0)
177                         return pos;
178
179         return NULL;
180 }
181
182 static int parse_line(FILE *file, struct hist_entry *he,
183                       struct list_head *head)
184 {
185         struct symbol *sym = he->ms.sym;
186         struct objdump_line *objdump_line;
187         char *line = NULL, *tmp, *tmp2;
188         size_t line_len;
189         s64 line_ip, offset = -1;
190         char *c;
191
192         if (getline(&line, &line_len, file) < 0)
193                 return -1;
194
195         if (!line)
196                 return -1;
197
198         c = strchr(line, '\n');
199         if (c)
200                 *c = 0;
201
202         line_ip = -1;
203
204         /*
205          * Strip leading spaces:
206          */
207         tmp = line;
208         while (*tmp) {
209                 if (*tmp != ' ')
210                         break;
211                 tmp++;
212         }
213
214         if (*tmp) {
215                 /*
216                  * Parse hexa addresses followed by ':'
217                  */
218                 line_ip = strtoull(tmp, &tmp2, 16);
219                 if (*tmp2 != ':')
220                         line_ip = -1;
221         }
222
223         if (line_ip != -1) {
224                 u64 start = map__rip_2objdump(he->ms.map, sym->start);
225                 offset = line_ip - start;
226         }
227
228         objdump_line = objdump_line__new(offset, line);
229         if (objdump_line == NULL) {
230                 free(line);
231                 return -1;
232         }
233         objdump__add_line(head, objdump_line);
234
235         return 0;
236 }
237
238 static int objdump_line__print(struct objdump_line *self,
239                                struct list_head *head,
240                                struct hist_entry *he, u64 len)
241 {
242         struct symbol *sym = he->ms.sym;
243         static const char *prev_line;
244         static const char *prev_color;
245
246         if (self->offset != -1) {
247                 const char *path = NULL;
248                 unsigned int hits = 0;
249                 double percent = 0.0;
250                 const char *color;
251                 struct sym_priv *priv = symbol__priv(sym);
252                 struct sym_ext *sym_ext = priv->ext;
253                 struct sym_hist *h = priv->hist;
254                 s64 offset = self->offset;
255                 struct objdump_line *next = objdump__get_next_ip_line(head, self);
256
257                 while (offset < (s64)len &&
258                        (next == NULL || offset < next->offset)) {
259                         if (sym_ext) {
260                                 if (path == NULL)
261                                         path = sym_ext[offset].path;
262                                 percent += sym_ext[offset].percent;
263                         } else
264                                 hits += h->ip[offset];
265
266                         ++offset;
267                 }
268
269                 if (sym_ext == NULL && h->sum)
270                         percent = 100.0 * hits / h->sum;
271
272                 color = get_percent_color(percent);
273
274                 /*
275                  * Also color the filename and line if needed, with
276                  * the same color than the percentage. Don't print it
277                  * twice for close colored ip with the same filename:line
278                  */
279                 if (path) {
280                         if (!prev_line || strcmp(prev_line, path)
281                                        || color != prev_color) {
282                                 color_fprintf(stdout, color, " %s", path);
283                                 prev_line = path;
284                                 prev_color = color;
285                         }
286                 }
287
288                 color_fprintf(stdout, color, " %7.2f", percent);
289                 printf(" :      ");
290                 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", self->line);
291         } else {
292                 if (!*self->line)
293                         printf("         :\n");
294                 else
295                         printf("         :      %s\n", self->line);
296         }
297
298         return 0;
299 }
300
301 static struct rb_root root_sym_ext;
302
303 static void insert_source_line(struct sym_ext *sym_ext)
304 {
305         struct sym_ext *iter;
306         struct rb_node **p = &root_sym_ext.rb_node;
307         struct rb_node *parent = NULL;
308
309         while (*p != NULL) {
310                 parent = *p;
311                 iter = rb_entry(parent, struct sym_ext, node);
312
313                 if (sym_ext->percent > iter->percent)
314                         p = &(*p)->rb_left;
315                 else
316                         p = &(*p)->rb_right;
317         }
318
319         rb_link_node(&sym_ext->node, parent, p);
320         rb_insert_color(&sym_ext->node, &root_sym_ext);
321 }
322
323 static void free_source_line(struct hist_entry *he, int len)
324 {
325         struct sym_priv *priv = symbol__priv(he->ms.sym);
326         struct sym_ext *sym_ext = priv->ext;
327         int i;
328
329         if (!sym_ext)
330                 return;
331
332         for (i = 0; i < len; i++)
333                 free(sym_ext[i].path);
334         free(sym_ext);
335
336         priv->ext = NULL;
337         root_sym_ext = RB_ROOT;
338 }
339
340 /* Get the filename:line for the colored entries */
341 static void
342 get_source_line(struct hist_entry *he, int len, const char *filename)
343 {
344         struct symbol *sym = he->ms.sym;
345         u64 start;
346         int i;
347         char cmd[PATH_MAX * 2];
348         struct sym_ext *sym_ext;
349         struct sym_priv *priv = symbol__priv(sym);
350         struct sym_hist *h = priv->hist;
351
352         if (!h->sum)
353                 return;
354
355         sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
356         if (!priv->ext)
357                 return;
358
359         start = he->ms.map->unmap_ip(he->ms.map, sym->start);
360
361         for (i = 0; i < len; i++) {
362                 char *path = NULL;
363                 size_t line_len;
364                 u64 offset;
365                 FILE *fp;
366
367                 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
368                 if (sym_ext[i].percent <= 0.5)
369                         continue;
370
371                 offset = start + i;
372                 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
373                 fp = popen(cmd, "r");
374                 if (!fp)
375                         continue;
376
377                 if (getline(&path, &line_len, fp) < 0 || !line_len)
378                         goto next;
379
380                 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
381                 if (!sym_ext[i].path)
382                         goto next;
383
384                 strcpy(sym_ext[i].path, path);
385                 insert_source_line(&sym_ext[i]);
386
387         next:
388                 pclose(fp);
389         }
390 }
391
392 static void print_summary(const char *filename)
393 {
394         struct sym_ext *sym_ext;
395         struct rb_node *node;
396
397         printf("\nSorted summary for file %s\n", filename);
398         printf("----------------------------------------------\n\n");
399
400         if (RB_EMPTY_ROOT(&root_sym_ext)) {
401                 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
402                 return;
403         }
404
405         node = rb_first(&root_sym_ext);
406         while (node) {
407                 double percent;
408                 const char *color;
409                 char *path;
410
411                 sym_ext = rb_entry(node, struct sym_ext, node);
412                 percent = sym_ext->percent;
413                 color = get_percent_color(percent);
414                 path = sym_ext->path;
415
416                 color_fprintf(stdout, color, " %7.2f %s", percent, path);
417                 node = rb_next(node);
418         }
419 }
420
421 static void hist_entry__print_hits(struct hist_entry *self)
422 {
423         struct symbol *sym = self->ms.sym;
424         struct sym_priv *priv = symbol__priv(sym);
425         struct sym_hist *h = priv->hist;
426         u64 len = sym->end - sym->start, offset;
427
428         for (offset = 0; offset < len; ++offset)
429                 if (h->ip[offset] != 0)
430                         printf("%*Lx: %Lu\n", BITS_PER_LONG / 2,
431                                sym->start + offset, h->ip[offset]);
432         printf("%*s: %Lu\n", BITS_PER_LONG / 2, "h->sum", h->sum);
433 }
434
435 static void annotate_sym(struct hist_entry *he)
436 {
437         struct map *map = he->ms.map;
438         struct dso *dso = map->dso;
439         struct symbol *sym = he->ms.sym;
440         const char *filename = dso->long_name, *d_filename;
441         u64 len;
442         char command[PATH_MAX*2];
443         FILE *file;
444         LIST_HEAD(head);
445         struct objdump_line *pos, *n;
446
447         if (!filename)
448                 return;
449
450         if (dso->origin == DSO__ORIG_KERNEL) {
451                 if (dso->annotate_warned)
452                         return;
453                 dso->annotate_warned = 1;
454                 pr_err("Can't annotate %s: No vmlinux file was found in the "
455                        "path:\n", sym->name);
456                 vmlinux_path__fprintf(stderr);
457                 return;
458         }
459
460         pr_debug("%s: filename=%s, sym=%s, start=%#Lx, end=%#Lx\n", __func__,
461                  filename, sym->name, map->unmap_ip(map, sym->start),
462                  map->unmap_ip(map, sym->end));
463
464         if (full_paths)
465                 d_filename = filename;
466         else
467                 d_filename = basename(filename);
468
469         len = sym->end - sym->start;
470
471         if (print_line) {
472                 get_source_line(he, len, filename);
473                 print_summary(filename);
474         }
475
476         printf("\n\n------------------------------------------------\n");
477         printf(" Percent |      Source code & Disassembly of %s\n", d_filename);
478         printf("------------------------------------------------\n");
479
480         if (verbose >= 2)
481                 printf("annotating [%p] %30s : [%p] %30s\n",
482                        dso, dso->long_name, sym, sym->name);
483
484         sprintf(command, "objdump --start-address=0x%016Lx --stop-address=0x%016Lx -dS %s|grep -v %s",
485                 map__rip_2objdump(map, sym->start),
486                 map__rip_2objdump(map, sym->end),
487                 filename, filename);
488
489         if (verbose >= 3)
490                 printf("doing: %s\n", command);
491
492         file = popen(command, "r");
493         if (!file)
494                 return;
495
496         while (!feof(file)) {
497                 if (parse_line(file, he, &head) < 0)
498                         break;
499         }
500
501         pclose(file);
502
503         if (verbose)
504                 hist_entry__print_hits(he);
505
506         list_for_each_entry_safe(pos, n, &head, node) {
507                 objdump_line__print(pos, &head, he, len);
508                 list_del(&pos->node);
509                 objdump_line__free(pos);
510         }
511
512         if (print_line)
513                 free_source_line(he, len);
514 }
515
516 static void hists__find_annotations(struct hists *self)
517 {
518         struct rb_node *nd;
519
520         for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
521                 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
522                 struct sym_priv *priv;
523
524                 if (he->ms.sym == NULL)
525                         continue;
526
527                 priv = symbol__priv(he->ms.sym);
528                 if (priv->hist == NULL)
529                         continue;
530
531                 annotate_sym(he);
532                 /*
533                  * Since we have a hist_entry per IP for the same symbol, free
534                  * he->ms.sym->hist to signal we already processed this symbol.
535                  */
536                 free(priv->hist);
537                 priv->hist = NULL;
538         }
539 }
540
541 static struct perf_event_ops event_ops = {
542         .sample = process_sample_event,
543         .mmap   = event__process_mmap,
544         .comm   = event__process_comm,
545         .fork   = event__process_task,
546 };
547
548 static int __cmd_annotate(void)
549 {
550         int ret;
551         struct perf_session *session;
552
553         session = perf_session__new(input_name, O_RDONLY, force, false);
554         if (session == NULL)
555                 return -ENOMEM;
556
557         ret = perf_session__process_events(session, &event_ops);
558         if (ret)
559                 goto out_delete;
560
561         if (dump_trace) {
562                 event__print_totals();
563                 goto out_delete;
564         }
565
566         if (verbose > 3)
567                 perf_session__fprintf(session, stdout);
568
569         if (verbose > 2)
570                 perf_session__fprintf_dsos(session, stdout);
571
572         hists__collapse_resort(&session->hists);
573         hists__output_resort(&session->hists);
574         hists__find_annotations(&session->hists);
575 out_delete:
576         perf_session__delete(session);
577
578         return ret;
579 }
580
581 static const char * const annotate_usage[] = {
582         "perf annotate [<options>] <command>",
583         NULL
584 };
585
586 static const struct option options[] = {
587         OPT_STRING('i', "input", &input_name, "file",
588                     "input file name"),
589         OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
590                    "only consider symbols in these dsos"),
591         OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
592                     "symbol to annotate"),
593         OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
594         OPT_INCR('v', "verbose", &verbose,
595                     "be more verbose (show symbol address, etc)"),
596         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
597                     "dump raw trace in ASCII"),
598         OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
599                    "file", "vmlinux pathname"),
600         OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
601                     "load module symbols - WARNING: use only with -k and LIVE kernel"),
602         OPT_BOOLEAN('l', "print-line", &print_line,
603                     "print matching source lines (may be slow)"),
604         OPT_BOOLEAN('P', "full-paths", &full_paths,
605                     "Don't shorten the displayed pathnames"),
606         OPT_END()
607 };
608
609 int cmd_annotate(int argc, const char **argv, const char *prefix __used)
610 {
611         argc = parse_options(argc, argv, options, annotate_usage, 0);
612
613         symbol_conf.priv_size = sizeof(struct sym_priv);
614         symbol_conf.try_vmlinux_path = true;
615
616         if (symbol__init() < 0)
617                 return -1;
618
619         setup_sorting(annotate_usage, options);
620
621         if (argc) {
622                 /*
623                  * Special case: if there's an argument left then assume tha
624                  * it's a symbol filter:
625                  */
626                 if (argc > 1)
627                         usage_with_options(annotate_usage, options);
628
629                 sym_hist_filter = argv[0];
630         }
631
632         setup_pager();
633
634         if (field_sep && *field_sep == '.') {
635                 pr_err("'.' is the only non valid --field-separator argument\n");
636                 return -1;
637         }
638
639         return __cmd_annotate();
640 }