aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf
diff options
context:
space:
mode:
authorIngo Molnar <mingo@elte.hu>2011-02-07 02:45:48 -0500
committerIngo Molnar <mingo@elte.hu>2011-02-07 02:45:48 -0500
commit075de90c46562de1435db16c2129ec4ff92e5bd2 (patch)
tree3b6f91ddc2773069a707538d607b2b4ea680c118 /tools/perf
parentc7f9a6f377fa64e5a74f8c128d4349765c28fab1 (diff)
parent36532461a0f60bb36c5470a0326f7394f19db23c (diff)
Merge branch 'perf/core' of git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux-2.6 into perf/core
Diffstat (limited to 'tools/perf')
-rw-r--r--tools/perf/Makefile2
-rw-r--r--tools/perf/builtin-annotate.c276
-rw-r--r--tools/perf/builtin-report.c18
-rw-r--r--tools/perf/builtin-top.c184
-rw-r--r--tools/perf/util/annotate.c550
-rw-r--r--tools/perf/util/annotate.h94
-rw-r--r--tools/perf/util/hist.c219
-rw-r--r--tools/perf/util/hist.h43
-rw-r--r--tools/perf/util/top.c5
-rw-r--r--tools/perf/util/top.h12
-rw-r--r--tools/perf/util/ui/browsers/annotate.c45
-rw-r--r--tools/perf/util/ui/browsers/hists.c9
12 files changed, 769 insertions, 688 deletions
diff --git a/tools/perf/Makefile b/tools/perf/Makefile
index 4c9499cb4398..be3eb1dc9a5a 100644
--- a/tools/perf/Makefile
+++ b/tools/perf/Makefile
@@ -401,6 +401,7 @@ LIB_H += util/include/dwarf-regs.h
401LIB_H += util/include/asm/dwarf2.h 401LIB_H += util/include/asm/dwarf2.h
402LIB_H += util/include/asm/cpufeature.h 402LIB_H += util/include/asm/cpufeature.h
403LIB_H += perf.h 403LIB_H += perf.h
404LIB_H += util/annotate.h
404LIB_H += util/cache.h 405LIB_H += util/cache.h
405LIB_H += util/callchain.h 406LIB_H += util/callchain.h
406LIB_H += util/build-id.h 407LIB_H += util/build-id.h
@@ -444,6 +445,7 @@ LIB_H += $(ARCH_INCLUDE)
444 445
445LIB_OBJS += $(OUTPUT)util/abspath.o 446LIB_OBJS += $(OUTPUT)util/abspath.o
446LIB_OBJS += $(OUTPUT)util/alias.o 447LIB_OBJS += $(OUTPUT)util/alias.o
448LIB_OBJS += $(OUTPUT)util/annotate.o
447LIB_OBJS += $(OUTPUT)util/build-id.o 449LIB_OBJS += $(OUTPUT)util/build-id.o
448LIB_OBJS += $(OUTPUT)util/config.o 450LIB_OBJS += $(OUTPUT)util/config.o
449LIB_OBJS += $(OUTPUT)util/ctype.o 451LIB_OBJS += $(OUTPUT)util/ctype.o
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index cd9dec46c19f..ea6a1165956f 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -9,6 +9,7 @@
9 9
10#include "util/util.h" 10#include "util/util.h"
11 11
12#include "util/util.h"
12#include "util/color.h" 13#include "util/color.h"
13#include <linux/list.h> 14#include <linux/list.h>
14#include "util/cache.h" 15#include "util/cache.h"
@@ -18,6 +19,7 @@
18#include "perf.h" 19#include "perf.h"
19#include "util/debug.h" 20#include "util/debug.h"
20 21
22#include "util/annotate.h"
21#include "util/event.h" 23#include "util/event.h"
22#include "util/parse-options.h" 24#include "util/parse-options.h"
23#include "util/parse-events.h" 25#include "util/parse-events.h"
@@ -55,7 +57,18 @@ static int hists__add_entry(struct hists *self, struct addr_location *al)
55 if (he == NULL) 57 if (he == NULL)
56 return -ENOMEM; 58 return -ENOMEM;
57 59
58 return hist_entry__inc_addr_samples(he, al->addr); 60 if (he->ms.sym != NULL) {
61 /*
62 * All aggregated on the first sym_hist.
63 */
64 struct annotation *notes = symbol__annotation(he->ms.sym);
65 if (notes->histograms == NULL && symbol__alloc_hist(he->ms.sym, 1) < 0)
66 return -ENOMEM;
67
68 return hist_entry__inc_addr_samples(he, 0, al->addr);
69 }
70
71 return 0;
59} 72}
60 73
61static int process_sample_event(union perf_event *event, 74static int process_sample_event(union perf_event *event,
@@ -79,245 +92,10 @@ static int process_sample_event(union perf_event *event,
79 return 0; 92 return 0;
80} 93}
81 94
82static int objdump_line__print(struct objdump_line *self, 95static int hist_entry__tty_annotate(struct hist_entry *he, int evidx)
83 struct list_head *head,
84 struct hist_entry *he, u64 len)
85{
86 struct symbol *sym = he->ms.sym;
87 static const char *prev_line;
88 static const char *prev_color;
89
90 if (self->offset != -1) {
91 const char *path = NULL;
92 unsigned int hits = 0;
93 double percent = 0.0;
94 const char *color;
95 struct sym_priv *priv = symbol__priv(sym);
96 struct sym_ext *sym_ext = priv->ext;
97 struct sym_hist *h = priv->hist;
98 s64 offset = self->offset;
99 struct objdump_line *next = objdump__get_next_ip_line(head, self);
100
101 while (offset < (s64)len &&
102 (next == NULL || offset < next->offset)) {
103 if (sym_ext) {
104 if (path == NULL)
105 path = sym_ext[offset].path;
106 percent += sym_ext[offset].percent;
107 } else
108 hits += h->ip[offset];
109
110 ++offset;
111 }
112
113 if (sym_ext == NULL && h->sum)
114 percent = 100.0 * hits / h->sum;
115
116 color = get_percent_color(percent);
117
118 /*
119 * Also color the filename and line if needed, with
120 * the same color than the percentage. Don't print it
121 * twice for close colored ip with the same filename:line
122 */
123 if (path) {
124 if (!prev_line || strcmp(prev_line, path)
125 || color != prev_color) {
126 color_fprintf(stdout, color, " %s", path);
127 prev_line = path;
128 prev_color = color;
129 }
130 }
131
132 color_fprintf(stdout, color, " %7.2f", percent);
133 printf(" : ");
134 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", self->line);
135 } else {
136 if (!*self->line)
137 printf(" :\n");
138 else
139 printf(" : %s\n", self->line);
140 }
141
142 return 0;
143}
144
145static struct rb_root root_sym_ext;
146
147static void insert_source_line(struct sym_ext *sym_ext)
148{
149 struct sym_ext *iter;
150 struct rb_node **p = &root_sym_ext.rb_node;
151 struct rb_node *parent = NULL;
152
153 while (*p != NULL) {
154 parent = *p;
155 iter = rb_entry(parent, struct sym_ext, node);
156
157 if (sym_ext->percent > iter->percent)
158 p = &(*p)->rb_left;
159 else
160 p = &(*p)->rb_right;
161 }
162
163 rb_link_node(&sym_ext->node, parent, p);
164 rb_insert_color(&sym_ext->node, &root_sym_ext);
165}
166
167static void free_source_line(struct hist_entry *he, int len)
168{
169 struct sym_priv *priv = symbol__priv(he->ms.sym);
170 struct sym_ext *sym_ext = priv->ext;
171 int i;
172
173 if (!sym_ext)
174 return;
175
176 for (i = 0; i < len; i++)
177 free(sym_ext[i].path);
178 free(sym_ext);
179
180 priv->ext = NULL;
181 root_sym_ext = RB_ROOT;
182}
183
184/* Get the filename:line for the colored entries */
185static void
186get_source_line(struct hist_entry *he, int len, const char *filename)
187{
188 struct symbol *sym = he->ms.sym;
189 u64 start;
190 int i;
191 char cmd[PATH_MAX * 2];
192 struct sym_ext *sym_ext;
193 struct sym_priv *priv = symbol__priv(sym);
194 struct sym_hist *h = priv->hist;
195
196 if (!h->sum)
197 return;
198
199 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
200 if (!priv->ext)
201 return;
202
203 start = he->ms.map->unmap_ip(he->ms.map, sym->start);
204
205 for (i = 0; i < len; i++) {
206 char *path = NULL;
207 size_t line_len;
208 u64 offset;
209 FILE *fp;
210
211 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
212 if (sym_ext[i].percent <= 0.5)
213 continue;
214
215 offset = start + i;
216 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
217 fp = popen(cmd, "r");
218 if (!fp)
219 continue;
220
221 if (getline(&path, &line_len, fp) < 0 || !line_len)
222 goto next;
223
224 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
225 if (!sym_ext[i].path)
226 goto next;
227
228 strcpy(sym_ext[i].path, path);
229 insert_source_line(&sym_ext[i]);
230
231 next:
232 pclose(fp);
233 }
234}
235
236static void print_summary(const char *filename)
237{ 96{
238 struct sym_ext *sym_ext; 97 return symbol__tty_annotate(he->ms.sym, he->ms.map, evidx,
239 struct rb_node *node; 98 print_line, full_paths, 0, 0);
240
241 printf("\nSorted summary for file %s\n", filename);
242 printf("----------------------------------------------\n\n");
243
244 if (RB_EMPTY_ROOT(&root_sym_ext)) {
245 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
246 return;
247 }
248
249 node = rb_first(&root_sym_ext);
250 while (node) {
251 double percent;
252 const char *color;
253 char *path;
254
255 sym_ext = rb_entry(node, struct sym_ext, node);
256 percent = sym_ext->percent;
257 color = get_percent_color(percent);
258 path = sym_ext->path;
259
260 color_fprintf(stdout, color, " %7.2f %s", percent, path);
261 node = rb_next(node);
262 }
263}
264
265static void hist_entry__print_hits(struct hist_entry *self)
266{
267 struct symbol *sym = self->ms.sym;
268 struct sym_priv *priv = symbol__priv(sym);
269 struct sym_hist *h = priv->hist;
270 u64 len = sym->end - sym->start, offset;
271
272 for (offset = 0; offset < len; ++offset)
273 if (h->ip[offset] != 0)
274 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
275 sym->start + offset, h->ip[offset]);
276 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
277}
278
279static int hist_entry__tty_annotate(struct hist_entry *he)
280{
281 struct map *map = he->ms.map;
282 struct dso *dso = map->dso;
283 struct symbol *sym = he->ms.sym;
284 const char *filename = dso->long_name, *d_filename;
285 u64 len;
286 LIST_HEAD(head);
287 struct objdump_line *pos, *n;
288
289 if (hist_entry__annotate(he, &head, 0) < 0)
290 return -1;
291
292 if (full_paths)
293 d_filename = filename;
294 else
295 d_filename = basename(filename);
296
297 len = sym->end - sym->start;
298
299 if (print_line) {
300 get_source_line(he, len, filename);
301 print_summary(filename);
302 }
303
304 printf("\n\n------------------------------------------------\n");
305 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
306 printf("------------------------------------------------\n");
307
308 if (verbose)
309 hist_entry__print_hits(he);
310
311 list_for_each_entry_safe(pos, n, &head, node) {
312 objdump_line__print(pos, &head, he, len);
313 list_del(&pos->node);
314 objdump_line__free(pos);
315 }
316
317 if (print_line)
318 free_source_line(he, len);
319
320 return 0;
321} 99}
322 100
323static void hists__find_annotations(struct hists *self) 101static void hists__find_annotations(struct hists *self)
@@ -327,13 +105,13 @@ static void hists__find_annotations(struct hists *self)
327 105
328 while (nd) { 106 while (nd) {
329 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); 107 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
330 struct sym_priv *priv; 108 struct annotation *notes;
331 109
332 if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned) 110 if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
333 goto find_next; 111 goto find_next;
334 112
335 priv = symbol__priv(he->ms.sym); 113 notes = symbol__annotation(he->ms.sym);
336 if (priv->hist == NULL) { 114 if (notes->histograms == NULL) {
337find_next: 115find_next:
338 if (key == KEY_LEFT) 116 if (key == KEY_LEFT)
339 nd = rb_prev(nd); 117 nd = rb_prev(nd);
@@ -343,7 +121,8 @@ find_next:
343 } 121 }
344 122
345 if (use_browser > 0) { 123 if (use_browser > 0) {
346 key = hist_entry__tui_annotate(he); 124 /* For now all is aggregated on the first */
125 key = hist_entry__tui_annotate(he, 0);
347 switch (key) { 126 switch (key) {
348 case KEY_RIGHT: 127 case KEY_RIGHT:
349 next = rb_next(nd); 128 next = rb_next(nd);
@@ -358,15 +137,16 @@ find_next:
358 if (next != NULL) 137 if (next != NULL)
359 nd = next; 138 nd = next;
360 } else { 139 } else {
361 hist_entry__tty_annotate(he); 140 /* For now all is aggregated on the first */
141 hist_entry__tty_annotate(he, 0);
362 nd = rb_next(nd); 142 nd = rb_next(nd);
363 /* 143 /*
364 * Since we have a hist_entry per IP for the same 144 * Since we have a hist_entry per IP for the same
365 * symbol, free he->ms.sym->hist to signal we already 145 * symbol, free he->ms.sym->histogram to signal we already
366 * processed this symbol. 146 * processed this symbol.
367 */ 147 */
368 free(priv->hist); 148 free(notes->histograms);
369 priv->hist = NULL; 149 notes->histograms = NULL;
370 } 150 }
371 } 151 }
372} 152}
@@ -454,7 +234,7 @@ int cmd_annotate(int argc, const char **argv, const char *prefix __used)
454 234
455 setup_browser(true); 235 setup_browser(true);
456 236
457 symbol_conf.priv_size = sizeof(struct sym_priv); 237 symbol_conf.priv_size = sizeof(struct annotation);
458 symbol_conf.try_vmlinux_path = true; 238 symbol_conf.try_vmlinux_path = true;
459 239
460 if (symbol__init() < 0) 240 if (symbol__init() < 0)
diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 080937c3a656..de06bf55efff 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -9,6 +9,7 @@
9 9
10#include "util/util.h" 10#include "util/util.h"
11 11
12#include "util/annotate.h"
12#include "util/color.h" 13#include "util/color.h"
13#include <linux/list.h> 14#include <linux/list.h>
14#include "util/cache.h" 15#include "util/cache.h"
@@ -117,8 +118,17 @@ static int perf_session__add_hist_entry(struct perf_session *session,
117 * so we don't allocated the extra space needed because the stdio 118 * so we don't allocated the extra space needed because the stdio
118 * code will not use it. 119 * code will not use it.
119 */ 120 */
120 if (use_browser > 0) 121 if (al->sym != NULL && use_browser > 0) {
121 err = hist_entry__inc_addr_samples(he, al->addr); 122 /*
123 * All aggregated on the first sym_hist.
124 */
125 struct annotation *notes = symbol__annotation(he->ms.sym);
126 if (notes->histograms == NULL &&
127 symbol__alloc_hist(he->ms.sym, 1) < 0)
128 err = -ENOMEM;
129 else
130 err = hist_entry__inc_addr_samples(he, 0, al->addr);
131 }
122 132
123 return err; 133 return err;
124} 134}
@@ -348,7 +358,7 @@ static int __cmd_report(void)
348 } 358 }
349 359
350 if (use_browser > 0) 360 if (use_browser > 0)
351 hists__tui_browse_tree(&session->hists_tree, help); 361 hists__tui_browse_tree(&session->hists_tree, help, 0);
352 else 362 else
353 hists__tty_browse_tree(&session->hists_tree, help); 363 hists__tty_browse_tree(&session->hists_tree, help);
354 364
@@ -508,7 +518,7 @@ int cmd_report(int argc, const char **argv, const char *prefix __used)
508 * implementation. 518 * implementation.
509 */ 519 */
510 if (use_browser > 0) { 520 if (use_browser > 0) {
511 symbol_conf.priv_size = sizeof(struct sym_priv); 521 symbol_conf.priv_size = sizeof(struct annotation);
512 /* 522 /*
513 * For searching by name on the "Browse map details". 523 * For searching by name on the "Browse map details".
514 * providing it only in verbose mode not to bloat too 524 * providing it only in verbose mode not to bloat too
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 104de9ab314c..716118a3b3e4 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -20,6 +20,7 @@
20 20
21#include "perf.h" 21#include "perf.h"
22 22
23#include "util/annotate.h"
23#include "util/cache.h" 24#include "util/cache.h"
24#include "util/color.h" 25#include "util/color.h"
25#include "util/evlist.h" 26#include "util/evlist.h"
@@ -140,10 +141,7 @@ static int parse_source(struct sym_entry *syme)
140 struct symbol *sym; 141 struct symbol *sym;
141 struct sym_entry_source *source; 142 struct sym_entry_source *source;
142 struct map *map; 143 struct map *map;
143 FILE *file; 144 int err = -1;
144 char command[PATH_MAX*2];
145 const char *path;
146 u64 len;
147 145
148 if (!syme) 146 if (!syme)
149 return -1; 147 return -1;
@@ -162,197 +160,80 @@ static int parse_source(struct sym_entry *syme)
162 if (syme->src == NULL) 160 if (syme->src == NULL)
163 return -1; 161 return -1;
164 pthread_mutex_init(&syme->src->lock, NULL); 162 pthread_mutex_init(&syme->src->lock, NULL);
163 INIT_LIST_HEAD(&syme->src->head);
165 } 164 }
166 165
167 source = syme->src; 166 source = syme->src;
168 167
169 if (source->lines) { 168 if (symbol__annotation(sym)->histograms != NULL) {
170 pthread_mutex_lock(&source->lock); 169 pthread_mutex_lock(&source->lock);
171 goto out_assign; 170 goto out_assign;
172 } 171 }
173 path = map->dso->long_name;
174
175 len = sym->end - sym->start;
176
177 sprintf(command,
178 "objdump --start-address=%#0*" PRIx64 " --stop-address=%#0*" PRIx64 " -dS %s",
179 BITS_PER_LONG / 4, map__rip_2objdump(map, sym->start),
180 BITS_PER_LONG / 4, map__rip_2objdump(map, sym->end), path);
181
182 file = popen(command, "r");
183 if (!file)
184 return -1;
185 172
186 pthread_mutex_lock(&source->lock); 173 pthread_mutex_lock(&source->lock);
187 source->lines_tail = &source->lines;
188 while (!feof(file)) {
189 struct source_line *src;
190 size_t dummy = 0;
191 char *c, *sep;
192
193 src = malloc(sizeof(struct source_line));
194 assert(src != NULL);
195 memset(src, 0, sizeof(struct source_line));
196
197 if (getline(&src->line, &dummy, file) < 0)
198 break;
199 if (!src->line)
200 break;
201
202 c = strchr(src->line, '\n');
203 if (c)
204 *c = 0;
205 174
206 src->next = NULL; 175 if (symbol__alloc_hist(sym, top.evlist->nr_entries) < 0) {
207 *source->lines_tail = src; 176 pr_err("Not enough memory for annotating '%s' symbol!\n",
208 source->lines_tail = &src->next; 177 sym->name);
209 178 goto out_unlock;
210 src->eip = strtoull(src->line, &sep, 16);
211 if (*sep == ':')
212 src->eip = map__objdump_2ip(map, src->eip);
213 else /* this line has no ip info (e.g. source line) */
214 src->eip = 0;
215 } 179 }
216 pclose(file); 180
181 err = symbol__annotate(sym, syme->map, &source->head, 0);
182 if (err == 0) {
217out_assign: 183out_assign:
218 sym_filter_entry = syme; 184 sym_filter_entry = syme;
185 }
186out_unlock:
219 pthread_mutex_unlock(&source->lock); 187 pthread_mutex_unlock(&source->lock);
220 return 0; 188 return err;
221} 189}
222 190
223static void __zero_source_counters(struct sym_entry *syme) 191static void __zero_source_counters(struct sym_entry *syme)
224{ 192{
225 int i; 193 struct symbol *sym = sym_entry__symbol(syme);
226 struct source_line *line; 194 symbol__annotate_zero_histograms(sym);
227
228 line = syme->src->lines;
229 while (line) {
230 for (i = 0; i < top.evlist->nr_entries; i++)
231 line->count[i] = 0;
232 line = line->next;
233 }
234} 195}
235 196
236static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip) 197static void record_precise_ip(struct sym_entry *syme, int counter, u64 ip)
237{ 198{
238 struct source_line *line;
239
240 if (syme != sym_filter_entry) 199 if (syme != sym_filter_entry)
241 return; 200 return;
242 201
243 if (pthread_mutex_trylock(&syme->src->lock)) 202 if (pthread_mutex_trylock(&syme->src->lock))
244 return; 203 return;
245 204
246 if (syme->src == NULL || syme->src->source == NULL) 205 ip = syme->map->map_ip(syme->map, ip);
247 goto out_unlock; 206 symbol__inc_addr_samples(sym_entry__symbol(syme), syme->map, counter, ip);
248 207
249 for (line = syme->src->lines; line; line = line->next) {
250 /* skip lines without IP info */
251 if (line->eip == 0)
252 continue;
253 if (line->eip == ip) {
254 line->count[counter]++;
255 break;
256 }
257 if (line->eip > ip)
258 break;
259 }
260out_unlock:
261 pthread_mutex_unlock(&syme->src->lock); 208 pthread_mutex_unlock(&syme->src->lock);
262} 209}
263 210
264#define PATTERN_LEN (BITS_PER_LONG / 4 + 2)
265
266static void lookup_sym_source(struct sym_entry *syme)
267{
268 struct symbol *symbol = sym_entry__symbol(syme);
269 struct source_line *line;
270 char pattern[PATTERN_LEN + 1];
271
272 sprintf(pattern, "%0*" PRIx64 " <", BITS_PER_LONG / 4,
273 map__rip_2objdump(syme->map, symbol->start));
274
275 pthread_mutex_lock(&syme->src->lock);
276 for (line = syme->src->lines; line; line = line->next) {
277 if (memcmp(line->line, pattern, PATTERN_LEN) == 0) {
278 syme->src->source = line;
279 break;
280 }
281 }
282 pthread_mutex_unlock(&syme->src->lock);
283}
284
285static void show_lines(struct source_line *queue, int count, int total)
286{
287 int i;
288 struct source_line *line;
289
290 line = queue;
291 for (i = 0; i < count; i++) {
292 float pcnt = 100.0*(float)line->count[top.sym_counter]/(float)total;
293
294 printf("%8li %4.1f%%\t%s\n", line->count[top.sym_counter], pcnt, line->line);
295 line = line->next;
296 }
297}
298
299#define TRACE_COUNT 3
300
301static void show_details(struct sym_entry *syme) 211static void show_details(struct sym_entry *syme)
302{ 212{
303 struct symbol *symbol; 213 struct symbol *symbol;
304 struct source_line *line; 214 int more;
305 struct source_line *line_queue = NULL;
306 int displayed = 0;
307 int line_queue_count = 0, total = 0, more = 0;
308 215
309 if (!syme) 216 if (!syme)
310 return; 217 return;
311 218
312 if (!syme->src->source) 219 symbol = sym_entry__symbol(syme);
313 lookup_sym_source(syme); 220 if (!syme->src || symbol__annotation(symbol)->histograms == NULL)
314
315 if (!syme->src->source)
316 return; 221 return;
317 222
318 symbol = sym_entry__symbol(syme);
319 printf("Showing %s for %s\n", event_name(top.sym_evsel), symbol->name); 223 printf("Showing %s for %s\n", event_name(top.sym_evsel), symbol->name);
320 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter); 224 printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
321 225
322 pthread_mutex_lock(&syme->src->lock); 226 pthread_mutex_lock(&syme->src->lock);
323 line = syme->src->source; 227 more = symbol__annotate_printf(symbol, syme->map, &syme->src->head,
324 while (line) { 228 top.sym_evsel->idx, 0, sym_pcnt_filter,
325 total += line->count[top.sym_counter]; 229 top.print_entries);
326 line = line->next; 230 if (top.zero)
327 } 231 symbol__annotate_zero_histogram(symbol, top.sym_evsel->idx);
328 232 else
329 line = syme->src->source; 233 symbol__annotate_decay_histogram(symbol, &syme->src->head,
330 while (line) { 234 top.sym_evsel->idx);
331 float pcnt = 0.0;
332
333 if (!line_queue_count)
334 line_queue = line;
335 line_queue_count++;
336
337 if (line->count[top.sym_counter])
338 pcnt = 100.0 * line->count[top.sym_counter] / (float)total;
339 if (pcnt >= (float)sym_pcnt_filter) {
340 if (displayed <= top.print_entries)
341 show_lines(line_queue, line_queue_count, total);
342 else more++;
343 displayed += line_queue_count;
344 line_queue_count = 0;
345 line_queue = NULL;
346 } else if (line_queue_count > TRACE_COUNT) {
347 line_queue = line_queue->next;
348 line_queue_count--;
349 }
350
351 line->count[top.sym_counter] = top.zero ? 0 : line->count[top.sym_counter] * 7 / 8;
352 line = line->next;
353 }
354 pthread_mutex_unlock(&syme->src->lock); 235 pthread_mutex_unlock(&syme->src->lock);
355 if (more) 236 if (more != 0)
356 printf("%d lines not displayed, maybe increase display entries [e]\n", more); 237 printf("%d lines not displayed, maybe increase display entries [e]\n", more);
357} 238}
358 239
@@ -787,9 +668,6 @@ static int symbol_filter(struct map *map, struct symbol *sym)
787 } 668 }
788 } 669 }
789 670
790 if (!syme->skip)
791 syme->name_len = strlen(sym->name);
792
793 return 0; 671 return 0;
794} 672}
795 673
@@ -1175,7 +1053,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
1175 1053
1176 top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node); 1054 top.sym_evsel = list_entry(top.evlist->entries.next, struct perf_evsel, node);
1177 1055
1178 symbol_conf.priv_size = (sizeof(struct sym_entry) + 1056 symbol_conf.priv_size = (sizeof(struct sym_entry) + sizeof(struct annotation) +
1179 (top.evlist->nr_entries + 1) * sizeof(unsigned long)); 1057 (top.evlist->nr_entries + 1) * sizeof(unsigned long));
1180 1058
1181 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL); 1059 symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
new file mode 100644
index 000000000000..297337649c21
--- /dev/null
+++ b/tools/perf/util/annotate.c
@@ -0,0 +1,550 @@
1/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-annotate.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9
10#include "util.h"
11#include "build-id.h"
12#include "color.h"
13#include "cache.h"
14#include "symbol.h"
15#include "debug.h"
16#include "annotate.h"
17
18int symbol__alloc_hist(struct symbol *sym, int nevents)
19{
20 struct annotation *notes = symbol__annotation(sym);
21
22 notes->sizeof_sym_hist = (sizeof(*notes->histograms) +
23 (sym->end - sym->start) * sizeof(u64));
24 notes->histograms = calloc(nevents, notes->sizeof_sym_hist);
25 notes->nr_histograms = nevents;
26 return notes->histograms == NULL ? -1 : 0;
27}
28
29void symbol__annotate_zero_histograms(struct symbol *sym)
30{
31 struct annotation *notes = symbol__annotation(sym);
32
33 if (notes->histograms != NULL)
34 memset(notes->histograms, 0,
35 notes->nr_histograms * notes->sizeof_sym_hist);
36}
37
38int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
39 int evidx, u64 addr)
40{
41 unsigned offset;
42 struct annotation *notes;
43 struct sym_hist *h;
44
45 notes = symbol__annotation(sym);
46 if (notes->histograms == NULL)
47 return -ENOMEM;
48
49 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
50
51 if (addr >= sym->end)
52 return 0;
53
54 offset = addr - sym->start;
55 h = annotation__histogram(notes, evidx);
56 h->sum++;
57 h->addr[offset]++;
58
59 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
60 ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
61 addr, addr - sym->start, evidx, h->addr[offset]);
62 return 0;
63}
64
65static struct objdump_line *objdump_line__new(s64 offset, char *line, size_t privsize)
66{
67 struct objdump_line *self = malloc(sizeof(*self) + privsize);
68
69 if (self != NULL) {
70 self->offset = offset;
71 self->line = line;
72 }
73
74 return self;
75}
76
77void objdump_line__free(struct objdump_line *self)
78{
79 free(self->line);
80 free(self);
81}
82
83static void objdump__add_line(struct list_head *head, struct objdump_line *line)
84{
85 list_add_tail(&line->node, head);
86}
87
88struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
89 struct objdump_line *pos)
90{
91 list_for_each_entry_continue(pos, head, node)
92 if (pos->offset >= 0)
93 return pos;
94
95 return NULL;
96}
97
98static int objdump_line__print(struct objdump_line *oline,
99 struct list_head *head, struct symbol *sym,
100 int evidx, u64 len, int min_pcnt,
101 int printed, int max_lines)
102{
103 static const char *prev_line;
104 static const char *prev_color;
105
106 if (oline->offset != -1) {
107 const char *path = NULL;
108 unsigned int hits = 0;
109 double percent = 0.0;
110 const char *color;
111 struct annotation *notes = symbol__annotation(sym);
112 struct source_line *src_line = notes->src_line;
113 struct sym_hist *h = annotation__histogram(notes, evidx);
114 s64 offset = oline->offset;
115 struct objdump_line *next = objdump__get_next_ip_line(head, oline);
116
117 while (offset < (s64)len &&
118 (next == NULL || offset < next->offset)) {
119 if (src_line) {
120 if (path == NULL)
121 path = src_line[offset].path;
122 percent += src_line[offset].percent;
123 } else
124 hits += h->addr[offset];
125
126 ++offset;
127 }
128
129 if (src_line == NULL && h->sum)
130 percent = 100.0 * hits / h->sum;
131
132 if (percent < min_pcnt)
133 return -1;
134
135 if (printed >= max_lines)
136 return 1;
137
138 color = get_percent_color(percent);
139
140 /*
141 * Also color the filename and line if needed, with
142 * the same color than the percentage. Don't print it
143 * twice for close colored addr with the same filename:line
144 */
145 if (path) {
146 if (!prev_line || strcmp(prev_line, path)
147 || color != prev_color) {
148 color_fprintf(stdout, color, " %s", path);
149 prev_line = path;
150 prev_color = color;
151 }
152 }
153
154 color_fprintf(stdout, color, " %7.2f", percent);
155 printf(" : ");
156 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", oline->line);
157 } else if (printed >= max_lines)
158 return 1;
159 else {
160 if (!*oline->line)
161 printf(" :\n");
162 else
163 printf(" : %s\n", oline->line);
164 }
165
166 return 0;
167}
168
169static int symbol__parse_objdump_line(struct symbol *sym, struct map *map, FILE *file,
170 struct list_head *head, size_t privsize)
171{
172 struct objdump_line *objdump_line;
173 char *line = NULL, *tmp, *tmp2, *c;
174 size_t line_len;
175 s64 line_ip, offset = -1;
176
177 if (getline(&line, &line_len, file) < 0)
178 return -1;
179
180 if (!line)
181 return -1;
182
183 while (line_len != 0 && isspace(line[line_len - 1]))
184 line[--line_len] = '\0';
185
186 c = strchr(line, '\n');
187 if (c)
188 *c = 0;
189
190 line_ip = -1;
191
192 /*
193 * Strip leading spaces:
194 */
195 tmp = line;
196 while (*tmp) {
197 if (*tmp != ' ')
198 break;
199 tmp++;
200 }
201
202 if (*tmp) {
203 /*
204 * Parse hexa addresses followed by ':'
205 */
206 line_ip = strtoull(tmp, &tmp2, 16);
207 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
208 line_ip = -1;
209 }
210
211 if (line_ip != -1) {
212 u64 start = map__rip_2objdump(map, sym->start),
213 end = map__rip_2objdump(map, sym->end);
214
215 offset = line_ip - start;
216 if (offset < 0 || (u64)line_ip > end)
217 offset = -1;
218 }
219
220 objdump_line = objdump_line__new(offset, line, privsize);
221 if (objdump_line == NULL) {
222 free(line);
223 return -1;
224 }
225 objdump__add_line(head, objdump_line);
226
227 return 0;
228}
229
230int symbol__annotate(struct symbol *sym, struct map *map,
231 struct list_head *head, size_t privsize)
232{
233 struct dso *dso = map->dso;
234 char *filename = dso__build_id_filename(dso, NULL, 0);
235 bool free_filename = true;
236 char command[PATH_MAX * 2];
237 FILE *file;
238 int err = 0;
239 u64 len;
240 char symfs_filename[PATH_MAX];
241
242 if (filename) {
243 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
244 symbol_conf.symfs, filename);
245 }
246
247 if (filename == NULL) {
248 if (dso->has_build_id) {
249 pr_err("Can't annotate %s: not enough memory\n",
250 sym->name);
251 return -ENOMEM;
252 }
253 goto fallback;
254 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
255 strstr(command, "[kernel.kallsyms]") ||
256 access(symfs_filename, R_OK)) {
257 free(filename);
258fallback:
259 /*
260 * If we don't have build-ids or the build-id file isn't in the
261 * cache, or is just a kallsyms file, well, lets hope that this
262 * DSO is the same as when 'perf record' ran.
263 */
264 filename = dso->long_name;
265 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
266 symbol_conf.symfs, filename);
267 free_filename = false;
268 }
269
270 if (dso->origin == DSO__ORIG_KERNEL) {
271 if (dso->annotate_warned)
272 goto out_free_filename;
273 err = -ENOENT;
274 dso->annotate_warned = 1;
275 pr_err("Can't annotate %s: No vmlinux file was found in the "
276 "path\n", sym->name);
277 goto out_free_filename;
278 }
279
280 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
281 filename, sym->name, map->unmap_ip(map, sym->start),
282 map->unmap_ip(map, sym->end));
283
284 len = sym->end - sym->start;
285
286 pr_debug("annotating [%p] %30s : [%p] %30s\n",
287 dso, dso->long_name, sym, sym->name);
288
289 snprintf(command, sizeof(command),
290 "objdump --start-address=0x%016" PRIx64
291 " --stop-address=0x%016" PRIx64 " -dS -C %s|grep -v %s|expand",
292 map__rip_2objdump(map, sym->start),
293 map__rip_2objdump(map, sym->end),
294 symfs_filename, filename);
295
296 pr_debug("Executing: %s\n", command);
297
298 file = popen(command, "r");
299 if (!file)
300 goto out_free_filename;
301
302 while (!feof(file))
303 if (symbol__parse_objdump_line(sym, map, file, head, privsize) < 0)
304 break;
305
306 pclose(file);
307out_free_filename:
308 if (free_filename)
309 free(filename);
310 return err;
311}
312
313static void insert_source_line(struct rb_root *root, struct source_line *src_line)
314{
315 struct source_line *iter;
316 struct rb_node **p = &root->rb_node;
317 struct rb_node *parent = NULL;
318
319 while (*p != NULL) {
320 parent = *p;
321 iter = rb_entry(parent, struct source_line, node);
322
323 if (src_line->percent > iter->percent)
324 p = &(*p)->rb_left;
325 else
326 p = &(*p)->rb_right;
327 }
328
329 rb_link_node(&src_line->node, parent, p);
330 rb_insert_color(&src_line->node, root);
331}
332
333static void symbol__free_source_line(struct symbol *sym, int len)
334{
335 struct annotation *notes = symbol__annotation(sym);
336 struct source_line *src_line = notes->src_line;
337 int i;
338
339 for (i = 0; i < len; i++)
340 free(src_line[i].path);
341
342 free(src_line);
343 notes->src_line = NULL;
344}
345
346/* Get the filename:line for the colored entries */
347static int symbol__get_source_line(struct symbol *sym, struct map *map,
348 int evidx, struct rb_root *root, int len,
349 const char *filename)
350{
351 u64 start;
352 int i;
353 char cmd[PATH_MAX * 2];
354 struct source_line *src_line;
355 struct annotation *notes = symbol__annotation(sym);
356 struct sym_hist *h = annotation__histogram(notes, evidx);
357
358 if (!h->sum)
359 return 0;
360
361 src_line = notes->src_line = calloc(len, sizeof(struct source_line));
362 if (!notes->src_line)
363 return -1;
364
365 start = map->unmap_ip(map, sym->start);
366
367 for (i = 0; i < len; i++) {
368 char *path = NULL;
369 size_t line_len;
370 u64 offset;
371 FILE *fp;
372
373 src_line[i].percent = 100.0 * h->addr[i] / h->sum;
374 if (src_line[i].percent <= 0.5)
375 continue;
376
377 offset = start + i;
378 sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
379 fp = popen(cmd, "r");
380 if (!fp)
381 continue;
382
383 if (getline(&path, &line_len, fp) < 0 || !line_len)
384 goto next;
385
386 src_line[i].path = malloc(sizeof(char) * line_len + 1);
387 if (!src_line[i].path)
388 goto next;
389
390 strcpy(src_line[i].path, path);
391 insert_source_line(root, &src_line[i]);
392
393 next:
394 pclose(fp);
395 }
396
397 return 0;
398}
399
400static void print_summary(struct rb_root *root, const char *filename)
401{
402 struct source_line *src_line;
403 struct rb_node *node;
404
405 printf("\nSorted summary for file %s\n", filename);
406 printf("----------------------------------------------\n\n");
407
408 if (RB_EMPTY_ROOT(root)) {
409 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
410 return;
411 }
412
413 node = rb_first(root);
414 while (node) {
415 double percent;
416 const char *color;
417 char *path;
418
419 src_line = rb_entry(node, struct source_line, node);
420 percent = src_line->percent;
421 color = get_percent_color(percent);
422 path = src_line->path;
423
424 color_fprintf(stdout, color, " %7.2f %s", percent, path);
425 node = rb_next(node);
426 }
427}
428
429static void symbol__annotate_hits(struct symbol *sym, int evidx)
430{
431 struct annotation *notes = symbol__annotation(sym);
432 struct sym_hist *h = annotation__histogram(notes, evidx);
433 u64 len = sym->end - sym->start, offset;
434
435 for (offset = 0; offset < len; ++offset)
436 if (h->addr[offset] != 0)
437 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
438 sym->start + offset, h->addr[offset]);
439 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
440}
441
442int symbol__annotate_printf(struct symbol *sym, struct map *map,
443 struct list_head *head, int evidx, bool full_paths,
444 int min_pcnt, int max_lines)
445{
446 struct dso *dso = map->dso;
447 const char *filename = dso->long_name, *d_filename;
448 struct objdump_line *pos;
449 int printed = 2;
450 int more = 0;
451 u64 len;
452
453 if (full_paths)
454 d_filename = filename;
455 else
456 d_filename = basename(filename);
457
458 len = sym->end - sym->start;
459
460 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
461 printf("------------------------------------------------\n");
462
463 if (verbose)
464 symbol__annotate_hits(sym, evidx);
465
466 list_for_each_entry(pos, head, node) {
467 switch (objdump_line__print(pos, head, sym, evidx, len, min_pcnt,
468 printed, max_lines)) {
469 case 0:
470 ++printed;
471 break;
472 case 1:
473 /* filtered by max_lines */
474 ++more;
475 break;
476 case -1:
477 default:
478 /* filtered by min_pcnt */
479 break;
480 }
481 }
482
483 return more;
484}
485
486void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
487{
488 struct annotation *notes = symbol__annotation(sym);
489 struct sym_hist *h = annotation__histogram(notes, evidx);
490
491 memset(h, 0, notes->sizeof_sym_hist);
492}
493
494void symbol__annotate_decay_histogram(struct symbol *sym,
495 struct list_head *head, int evidx)
496{
497 struct annotation *notes = symbol__annotation(sym);
498 struct sym_hist *h = annotation__histogram(notes, evidx);
499 struct objdump_line *pos;
500
501 h->sum = 0;
502
503 list_for_each_entry(pos, head, node) {
504 if (pos->offset != -1) {
505 h->addr[pos->offset] = h->addr[pos->offset] * 7 / 8;
506 h->sum += h->addr[pos->offset];
507 }
508 }
509}
510
511void objdump_line_list__purge(struct list_head *head)
512{
513 struct objdump_line *pos, *n;
514
515 list_for_each_entry_safe(pos, n, head, node) {
516 list_del(&pos->node);
517 objdump_line__free(pos);
518 }
519}
520
521int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
522 bool print_lines, bool full_paths, int min_pcnt,
523 int max_lines)
524{
525 struct dso *dso = map->dso;
526 const char *filename = dso->long_name;
527 struct rb_root source_line = RB_ROOT;
528 LIST_HEAD(head);
529 u64 len;
530
531 if (symbol__annotate(sym, map, &head, 0) < 0)
532 return -1;
533
534 len = sym->end - sym->start;
535
536 if (print_lines) {
537 symbol__get_source_line(sym, map, evidx, &source_line,
538 len, filename);
539 print_summary(&source_line, filename);
540 }
541
542 symbol__annotate_printf(sym, map, &head, evidx, full_paths,
543 min_pcnt, max_lines);
544 if (print_lines)
545 symbol__free_source_line(sym, len);
546
547 objdump_line_list__purge(&head);
548
549 return 0;
550}
diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h
new file mode 100644
index 000000000000..b1253aadf340
--- /dev/null
+++ b/tools/perf/util/annotate.h
@@ -0,0 +1,94 @@
1#ifndef __PERF_ANNOTATE_H
2#define __PERF_ANNOTATE_H
3
4#include <stdbool.h>
5#include "types.h"
6#include "symbol.h"
7#include <linux/list.h>
8#include <linux/rbtree.h>
9
10struct objdump_line {
11 struct list_head node;
12 s64 offset;
13 char *line;
14};
15
16void objdump_line__free(struct objdump_line *self);
17struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
18 struct objdump_line *pos);
19
20struct sym_hist {
21 u64 sum;
22 u64 addr[0];
23};
24
25struct source_line {
26 struct rb_node node;
27 double percent;
28 char *path;
29};
30
31/** struct annotation - symbols with hits have this attached as in sannotation
32 *
33 * @histogram: Array of addr hit histograms per event being monitored
34 * @src_line: If 'print_lines' is specified, per source code line percentages
35 *
36 * src_line is allocated, percentages calculated and all sorted by percentage
37 * when the annotation is about to be presented, so the percentages are for
38 * one of the entries in the histogram array, i.e. for the event/counter being
39 * presented. It is deallocated right after symbol__{tui,tty,etc}_annotate
40 * returns.
41 */
42struct annotation {
43 struct source_line *src_line;
44 struct sym_hist *histograms;
45 int nr_histograms;
46 int sizeof_sym_hist;
47};
48
49struct sannotation {
50 struct annotation annotation;
51 struct symbol symbol;
52};
53
54static inline struct sym_hist *annotation__histogram(struct annotation *notes, int idx)
55{
56 return ((void *)notes->histograms) + (notes->sizeof_sym_hist * idx);
57}
58
59static inline struct annotation *symbol__annotation(struct symbol *sym)
60{
61 struct sannotation *a = container_of(sym, struct sannotation, symbol);
62 return &a->annotation;
63}
64
65int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
66 int evidx, u64 addr);
67int symbol__alloc_hist(struct symbol *sym, int nevents);
68void symbol__annotate_zero_histograms(struct symbol *sym);
69
70int symbol__annotate(struct symbol *sym, struct map *map,
71 struct list_head *head, size_t privsize);
72int symbol__annotate_printf(struct symbol *sym, struct map *map,
73 struct list_head *head, int evidx, bool full_paths,
74 int min_pcnt, int max_lines);
75void symbol__annotate_zero_histogram(struct symbol *sym, int evidx);
76void symbol__annotate_decay_histogram(struct symbol *sym,
77 struct list_head *head, int evidx);
78void objdump_line_list__purge(struct list_head *head);
79
80int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
81 bool print_lines, bool full_paths, int min_pcnt,
82 int max_lines);
83
84#ifdef NO_NEWT_SUPPORT
85static inline int symbol__tui_annotate(symbol *sym __used,
86 struct map *map __used, int evidx __used)
87{
88 return 0;
89}
90#else
91int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx);
92#endif
93
94#endif /* __PERF_ANNOTATE_H */
diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
index 95887804dc8e..bac5ab684967 100644
--- a/tools/perf/util/hist.c
+++ b/tools/perf/util/hist.c
@@ -1,3 +1,4 @@
1#include "annotate.h"
1#include "util.h" 2#include "util.h"
2#include "build-id.h" 3#include "build-id.h"
3#include "hist.h" 4#include "hist.h"
@@ -949,225 +950,15 @@ void hists__filter_by_thread(struct hists *self, const struct thread *thread)
949 } 950 }
950} 951}
951 952
952static int symbol__alloc_hist(struct symbol *self) 953int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
953{ 954{
954 struct sym_priv *priv = symbol__priv(self); 955 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
955 const int size = (sizeof(*priv->hist) +
956 (self->end - self->start) * sizeof(u64));
957
958 priv->hist = zalloc(size);
959 return priv->hist == NULL ? -1 : 0;
960}
961
962int hist_entry__inc_addr_samples(struct hist_entry *self, u64 ip)
963{
964 unsigned int sym_size, offset;
965 struct symbol *sym = self->ms.sym;
966 struct sym_priv *priv;
967 struct sym_hist *h;
968
969 if (!sym || !self->ms.map)
970 return 0;
971
972 priv = symbol__priv(sym);
973 if (priv->hist == NULL && symbol__alloc_hist(sym) < 0)
974 return -ENOMEM;
975
976 sym_size = sym->end - sym->start;
977 offset = ip - sym->start;
978
979 pr_debug3("%s: ip=%#" PRIx64 "\n", __func__, self->ms.map->unmap_ip(self->ms.map, ip));
980
981 if (offset >= sym_size)
982 return 0;
983
984 h = priv->hist;
985 h->sum++;
986 h->ip[offset]++;
987
988 pr_debug3("%#" PRIx64 " %s: period++ [ip: %#" PRIx64 ", %#" PRIx64
989 "] => %" PRIu64 "\n", self->ms.sym->start, self->ms.sym->name,
990 ip, ip - self->ms.sym->start, h->ip[offset]);
991 return 0;
992}
993
994static struct objdump_line *objdump_line__new(s64 offset, char *line, size_t privsize)
995{
996 struct objdump_line *self = malloc(sizeof(*self) + privsize);
997
998 if (self != NULL) {
999 self->offset = offset;
1000 self->line = line;
1001 }
1002
1003 return self;
1004}
1005
1006void objdump_line__free(struct objdump_line *self)
1007{
1008 free(self->line);
1009 free(self);
1010}
1011
1012static void objdump__add_line(struct list_head *head, struct objdump_line *line)
1013{
1014 list_add_tail(&line->node, head);
1015}
1016
1017struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
1018 struct objdump_line *pos)
1019{
1020 list_for_each_entry_continue(pos, head, node)
1021 if (pos->offset >= 0)
1022 return pos;
1023
1024 return NULL;
1025} 956}
1026 957
1027static int hist_entry__parse_objdump_line(struct hist_entry *self, FILE *file, 958int hist_entry__annotate(struct hist_entry *he, struct list_head *head,
1028 struct list_head *head, size_t privsize)
1029{
1030 struct symbol *sym = self->ms.sym;
1031 struct objdump_line *objdump_line;
1032 char *line = NULL, *tmp, *tmp2, *c;
1033 size_t line_len;
1034 s64 line_ip, offset = -1;
1035
1036 if (getline(&line, &line_len, file) < 0)
1037 return -1;
1038
1039 if (!line)
1040 return -1;
1041
1042 while (line_len != 0 && isspace(line[line_len - 1]))
1043 line[--line_len] = '\0';
1044
1045 c = strchr(line, '\n');
1046 if (c)
1047 *c = 0;
1048
1049 line_ip = -1;
1050
1051 /*
1052 * Strip leading spaces:
1053 */
1054 tmp = line;
1055 while (*tmp) {
1056 if (*tmp != ' ')
1057 break;
1058 tmp++;
1059 }
1060
1061 if (*tmp) {
1062 /*
1063 * Parse hexa addresses followed by ':'
1064 */
1065 line_ip = strtoull(tmp, &tmp2, 16);
1066 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
1067 line_ip = -1;
1068 }
1069
1070 if (line_ip != -1) {
1071 u64 start = map__rip_2objdump(self->ms.map, sym->start),
1072 end = map__rip_2objdump(self->ms.map, sym->end);
1073
1074 offset = line_ip - start;
1075 if (offset < 0 || (u64)line_ip > end)
1076 offset = -1;
1077 }
1078
1079 objdump_line = objdump_line__new(offset, line, privsize);
1080 if (objdump_line == NULL) {
1081 free(line);
1082 return -1;
1083 }
1084 objdump__add_line(head, objdump_line);
1085
1086 return 0;
1087}
1088
1089int hist_entry__annotate(struct hist_entry *self, struct list_head *head,
1090 size_t privsize) 959 size_t privsize)
1091{ 960{
1092 struct symbol *sym = self->ms.sym; 961 return symbol__annotate(he->ms.sym, he->ms.map, head, privsize);
1093 struct map *map = self->ms.map;
1094 struct dso *dso = map->dso;
1095 char *filename = dso__build_id_filename(dso, NULL, 0);
1096 bool free_filename = true;
1097 char command[PATH_MAX * 2];
1098 FILE *file;
1099 int err = 0;
1100 u64 len;
1101 char symfs_filename[PATH_MAX];
1102
1103 if (filename) {
1104 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
1105 symbol_conf.symfs, filename);
1106 }
1107
1108 if (filename == NULL) {
1109 if (dso->has_build_id) {
1110 pr_err("Can't annotate %s: not enough memory\n",
1111 sym->name);
1112 return -ENOMEM;
1113 }
1114 goto fallback;
1115 } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
1116 strstr(command, "[kernel.kallsyms]") ||
1117 access(symfs_filename, R_OK)) {
1118 free(filename);
1119fallback:
1120 /*
1121 * If we don't have build-ids or the build-id file isn't in the
1122 * cache, or is just a kallsyms file, well, lets hope that this
1123 * DSO is the same as when 'perf record' ran.
1124 */
1125 filename = dso->long_name;
1126 snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
1127 symbol_conf.symfs, filename);
1128 free_filename = false;
1129 }
1130
1131 if (dso->origin == DSO__ORIG_KERNEL) {
1132 if (dso->annotate_warned)
1133 goto out_free_filename;
1134 err = -ENOENT;
1135 dso->annotate_warned = 1;
1136 pr_err("Can't annotate %s: No vmlinux file was found in the "
1137 "path\n", sym->name);
1138 goto out_free_filename;
1139 }
1140
1141 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
1142 filename, sym->name, map->unmap_ip(map, sym->start),
1143 map->unmap_ip(map, sym->end));
1144
1145 len = sym->end - sym->start;
1146
1147 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1148 dso, dso->long_name, sym, sym->name);
1149
1150 snprintf(command, sizeof(command),
1151 "objdump --start-address=0x%016" PRIx64 " --stop-address=0x%016" PRIx64 " -dS -C %s|grep -v %s|expand",
1152 map__rip_2objdump(map, sym->start),
1153 map__rip_2objdump(map, sym->end),
1154 symfs_filename, filename);
1155
1156 pr_debug("Executing: %s\n", command);
1157
1158 file = popen(command, "r");
1159 if (!file)
1160 goto out_free_filename;
1161
1162 while (!feof(file))
1163 if (hist_entry__parse_objdump_line(self, file, head, privsize) < 0)
1164 break;
1165
1166 pclose(file);
1167out_free_filename:
1168 if (free_filename)
1169 free(filename);
1170 return err;
1171} 962}
1172 963
1173void hists__inc_nr_events(struct hists *self, u32 type) 964void hists__inc_nr_events(struct hists *self, u32 type)
diff --git a/tools/perf/util/hist.h b/tools/perf/util/hist.h
index 889559b86492..2c6cdae6a764 100644
--- a/tools/perf/util/hist.h
+++ b/tools/perf/util/hist.h
@@ -9,33 +9,6 @@ extern struct callchain_param callchain_param;
9struct hist_entry; 9struct hist_entry;
10struct addr_location; 10struct addr_location;
11struct symbol; 11struct symbol;
12struct rb_root;
13
14struct objdump_line {
15 struct list_head node;
16 s64 offset;
17 char *line;
18};
19
20void objdump_line__free(struct objdump_line *self);
21struct objdump_line *objdump__get_next_ip_line(struct list_head *head,
22 struct objdump_line *pos);
23
24struct sym_hist {
25 u64 sum;
26 u64 ip[0];
27};
28
29struct sym_ext {
30 struct rb_node node;
31 double percent;
32 char *path;
33};
34
35struct sym_priv {
36 struct sym_hist *hist;
37 struct sym_ext *ext;
38};
39 12
40/* 13/*
41 * The kernel collects the number of events it couldn't send in a stretch and 14 * The kernel collects the number of events it couldn't send in a stretch and
@@ -104,7 +77,7 @@ size_t hists__fprintf_nr_events(struct hists *self, FILE *fp);
104size_t hists__fprintf(struct hists *self, struct hists *pair, 77size_t hists__fprintf(struct hists *self, struct hists *pair,
105 bool show_displacement, FILE *fp); 78 bool show_displacement, FILE *fp);
106 79
107int hist_entry__inc_addr_samples(struct hist_entry *self, u64 ip); 80int hist_entry__inc_addr_samples(struct hist_entry *self, int evidx, u64 addr);
108int hist_entry__annotate(struct hist_entry *self, struct list_head *head, 81int hist_entry__annotate(struct hist_entry *self, struct list_head *head,
109 size_t privsize); 82 size_t privsize);
110 83
@@ -118,18 +91,20 @@ bool hists__new_col_len(struct hists *self, enum hist_column col, u16 len);
118#ifdef NO_NEWT_SUPPORT 91#ifdef NO_NEWT_SUPPORT
119static inline int hists__browse(struct hists *self __used, 92static inline int hists__browse(struct hists *self __used,
120 const char *helpline __used, 93 const char *helpline __used,
121 const char *ev_name __used) 94 const char *ev_name __used, int evidx __used)
122{ 95{
123 return 0; 96 return 0;
124} 97}
125 98
126static inline int hists__tui_browse_tree(struct rb_root *self __used, 99static inline int hists__tui_browse_tree(struct rb_root *self __used,
127 const char *help __used) 100 const char *help __used,
101 int evidx __used)
128{ 102{
129 return 0; 103 return 0;
130} 104}
131 105
132static inline int hist_entry__tui_annotate(struct hist_entry *self __used) 106static inline int hist_entry__tui_annotate(struct hist_entry *self __used,
107 int evidx __used)
133{ 108{
134 return 0; 109 return 0;
135} 110}
@@ -138,13 +113,13 @@ static inline int hist_entry__tui_annotate(struct hist_entry *self __used)
138#else 113#else
139#include <newt.h> 114#include <newt.h>
140int hists__browse(struct hists *self, const char *helpline, 115int hists__browse(struct hists *self, const char *helpline,
141 const char *ev_name); 116 const char *ev_name, int evidx);
142int hist_entry__tui_annotate(struct hist_entry *self); 117int hist_entry__tui_annotate(struct hist_entry *self, int evidx);
143 118
144#define KEY_LEFT NEWT_KEY_LEFT 119#define KEY_LEFT NEWT_KEY_LEFT
145#define KEY_RIGHT NEWT_KEY_RIGHT 120#define KEY_RIGHT NEWT_KEY_RIGHT
146 121
147int hists__tui_browse_tree(struct rb_root *self, const char *help); 122int hists__tui_browse_tree(struct rb_root *self, const char *help, int evidx);
148#endif 123#endif
149 124
150unsigned int hists__sort_list_width(struct hists *self); 125unsigned int hists__sort_list_width(struct hists *self);
diff --git a/tools/perf/util/top.c b/tools/perf/util/top.c
index 1d2e2652cd68..70a9c13f4ad5 100644
--- a/tools/perf/util/top.c
+++ b/tools/perf/util/top.c
@@ -200,6 +200,7 @@ void perf_top__find_widths(struct perf_top *top, struct rb_root *root,
200 200
201 for (nd = rb_first(root); nd; nd = rb_next(nd)) { 201 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
202 struct sym_entry *syme = rb_entry(nd, struct sym_entry, rb_node); 202 struct sym_entry *syme = rb_entry(nd, struct sym_entry, rb_node);
203 struct symbol *sym = sym_entry__symbol(syme);
203 204
204 if (++printed > top->print_entries || 205 if (++printed > top->print_entries ||
205 (int)syme->snap_count < top->count_filter) 206 (int)syme->snap_count < top->count_filter)
@@ -211,7 +212,7 @@ void perf_top__find_widths(struct perf_top *top, struct rb_root *root,
211 if (syme->map->dso->short_name_len > *dso_short_width) 212 if (syme->map->dso->short_name_len > *dso_short_width)
212 *dso_short_width = syme->map->dso->short_name_len; 213 *dso_short_width = syme->map->dso->short_name_len;
213 214
214 if (syme->name_len > *sym_width) 215 if (sym->namelen > *sym_width)
215 *sym_width = syme->name_len; 216 *sym_width = sym->namelen;
216 } 217 }
217} 218}
diff --git a/tools/perf/util/top.h b/tools/perf/util/top.h
index 611370fa7df8..fe44afb69985 100644
--- a/tools/perf/util/top.h
+++ b/tools/perf/util/top.h
@@ -11,17 +11,8 @@
11struct perf_evlist; 11struct perf_evlist;
12struct perf_evsel; 12struct perf_evsel;
13 13
14struct source_line {
15 u64 eip;
16 unsigned long count[MAX_COUNTERS]; /* FIXME */
17 char *line;
18 struct source_line *next;
19};
20
21struct sym_entry_source { 14struct sym_entry_source {
22 struct source_line *source; 15 struct list_head head;
23 struct source_line *lines;
24 struct source_line **lines_tail;
25 pthread_mutex_t lock; 16 pthread_mutex_t lock;
26}; 17};
27 18
@@ -31,7 +22,6 @@ struct sym_entry {
31 unsigned long snap_count; 22 unsigned long snap_count;
32 double weight; 23 double weight;
33 int skip; 24 int skip;
34 u16 name_len;
35 u8 origin; 25 u8 origin;
36 struct map *map; 26 struct map *map;
37 struct sym_entry_source *src; 27 struct sym_entry_source *src;
diff --git a/tools/perf/util/ui/browsers/annotate.c b/tools/perf/util/ui/browsers/annotate.c
index 82b78f99251b..8d8a16895af7 100644
--- a/tools/perf/util/ui/browsers/annotate.c
+++ b/tools/perf/util/ui/browsers/annotate.c
@@ -1,9 +1,11 @@
1#include "../browser.h" 1#include "../browser.h"
2#include "../helpline.h" 2#include "../helpline.h"
3#include "../libslang.h" 3#include "../libslang.h"
4#include "../../annotate.h"
4#include "../../hist.h" 5#include "../../hist.h"
5#include "../../sort.h" 6#include "../../sort.h"
6#include "../../symbol.h" 7#include "../../symbol.h"
8#include "../../annotate.h"
7 9
8static void ui__error_window(const char *fmt, ...) 10static void ui__error_window(const char *fmt, ...)
9{ 11{
@@ -59,31 +61,33 @@ static void annotate_browser__write(struct ui_browser *self, void *entry, int ro
59 61
60static double objdump_line__calc_percent(struct objdump_line *self, 62static double objdump_line__calc_percent(struct objdump_line *self,
61 struct list_head *head, 63 struct list_head *head,
62 struct symbol *sym) 64 struct symbol *sym, int evidx)
63{ 65{
64 double percent = 0.0; 66 double percent = 0.0;
65 67
66 if (self->offset != -1) { 68 if (self->offset != -1) {
67 int len = sym->end - sym->start; 69 int len = sym->end - sym->start;
68 unsigned int hits = 0; 70 unsigned int hits = 0;
69 struct sym_priv *priv = symbol__priv(sym); 71 struct annotation *notes = symbol__annotation(sym);
70 struct sym_ext *sym_ext = priv->ext; 72 struct source_line *src_line = notes->src_line;
71 struct sym_hist *h = priv->hist; 73 struct sym_hist *h = annotation__histogram(notes, evidx);
72 s64 offset = self->offset; 74 s64 offset = self->offset;
73 struct objdump_line *next = objdump__get_next_ip_line(head, self); 75 struct objdump_line *next = objdump__get_next_ip_line(head, self);
74 76
75
76 while (offset < (s64)len && 77 while (offset < (s64)len &&
77 (next == NULL || offset < next->offset)) { 78 (next == NULL || offset < next->offset)) {
78 if (sym_ext) { 79 if (src_line) {
79 percent += sym_ext[offset].percent; 80 percent += src_line[offset].percent;
80 } else 81 } else
81 hits += h->ip[offset]; 82 hits += h->addr[offset];
82 83
83 ++offset; 84 ++offset;
84 } 85 }
85 86 /*
86 if (sym_ext == NULL && h->sum) 87 * If the percentage wasn't already calculated in
88 * symbol__get_source_line, do it now:
89 */
90 if (src_line == NULL && h->sum)
87 percent = 100.0 * hits / h->sum; 91 percent = 100.0 * hits / h->sum;
88 } 92 }
89 93
@@ -136,10 +140,10 @@ static void annotate_browser__set_top(struct annotate_browser *self,
136static int annotate_browser__run(struct annotate_browser *self) 140static int annotate_browser__run(struct annotate_browser *self)
137{ 141{
138 struct rb_node *nd; 142 struct rb_node *nd;
139 struct hist_entry *he = self->b.priv; 143 struct symbol *sym = self->b.priv;
140 int key; 144 int key;
141 145
142 if (ui_browser__show(&self->b, he->ms.sym->name, 146 if (ui_browser__show(&self->b, sym->name,
143 "<-, -> or ESC: exit, TAB/shift+TAB: cycle thru samples") < 0) 147 "<-, -> or ESC: exit, TAB/shift+TAB: cycle thru samples") < 0)
144 return -1; 148 return -1;
145 /* 149 /*
@@ -179,7 +183,12 @@ out:
179 return key; 183 return key;
180} 184}
181 185
182int hist_entry__tui_annotate(struct hist_entry *self) 186int hist_entry__tui_annotate(struct hist_entry *he, int evidx)
187{
188 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx);
189}
190
191int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx)
183{ 192{
184 struct objdump_line *pos, *n; 193 struct objdump_line *pos, *n;
185 struct objdump_line_rb_node *rbpos; 194 struct objdump_line_rb_node *rbpos;
@@ -190,18 +199,18 @@ int hist_entry__tui_annotate(struct hist_entry *self)
190 .refresh = ui_browser__list_head_refresh, 199 .refresh = ui_browser__list_head_refresh,
191 .seek = ui_browser__list_head_seek, 200 .seek = ui_browser__list_head_seek,
192 .write = annotate_browser__write, 201 .write = annotate_browser__write,
193 .priv = self, 202 .priv = sym,
194 }, 203 },
195 }; 204 };
196 int ret; 205 int ret;
197 206
198 if (self->ms.sym == NULL) 207 if (sym == NULL)
199 return -1; 208 return -1;
200 209
201 if (self->ms.map->dso->annotate_warned) 210 if (map->dso->annotate_warned)
202 return -1; 211 return -1;
203 212
204 if (hist_entry__annotate(self, &head, sizeof(*rbpos)) < 0) { 213 if (symbol__annotate(sym, map, &head, sizeof(*rbpos)) < 0) {
205 ui__error_window(ui_helpline__last_msg); 214 ui__error_window(ui_helpline__last_msg);
206 return -1; 215 return -1;
207 } 216 }
@@ -214,7 +223,7 @@ int hist_entry__tui_annotate(struct hist_entry *self)
214 browser.b.width = line_len; 223 browser.b.width = line_len;
215 rbpos = objdump_line__rb(pos); 224 rbpos = objdump_line__rb(pos);
216 rbpos->idx = browser.b.nr_entries++; 225 rbpos->idx = browser.b.nr_entries++;
217 rbpos->percent = objdump_line__calc_percent(pos, &head, self->ms.sym); 226 rbpos->percent = objdump_line__calc_percent(pos, &head, sym, evidx);
218 if (rbpos->percent < 0.01) 227 if (rbpos->percent < 0.01)
219 continue; 228 continue;
220 objdump__insert_line(&browser.entries, rbpos); 229 objdump__insert_line(&browser.entries, rbpos);
diff --git a/tools/perf/util/ui/browsers/hists.c b/tools/perf/util/ui/browsers/hists.c
index 86428239fa65..294b49538522 100644
--- a/tools/perf/util/ui/browsers/hists.c
+++ b/tools/perf/util/ui/browsers/hists.c
@@ -797,7 +797,8 @@ static int hists__browser_title(struct hists *self, char *bf, size_t size,
797 return printed; 797 return printed;
798} 798}
799 799
800int hists__browse(struct hists *self, const char *helpline, const char *ev_name) 800int hists__browse(struct hists *self, const char *helpline,
801 const char *ev_name, int evidx)
801{ 802{
802 struct hist_browser *browser = hist_browser__new(self); 803 struct hist_browser *browser = hist_browser__new(self);
803 struct pstack *fstack; 804 struct pstack *fstack;
@@ -935,7 +936,7 @@ do_annotate:
935 if (he == NULL) 936 if (he == NULL)
936 continue; 937 continue;
937 938
938 hist_entry__tui_annotate(he); 939 hist_entry__tui_annotate(he, evidx);
939 } else if (choice == browse_map) 940 } else if (choice == browse_map)
940 map__browse(browser->selection->map); 941 map__browse(browser->selection->map);
941 else if (choice == zoom_dso) { 942 else if (choice == zoom_dso) {
@@ -984,7 +985,7 @@ out:
984 return key; 985 return key;
985} 986}
986 987
987int hists__tui_browse_tree(struct rb_root *self, const char *help) 988int hists__tui_browse_tree(struct rb_root *self, const char *help, int evidx)
988{ 989{
989 struct rb_node *first = rb_first(self), *nd = first, *next; 990 struct rb_node *first = rb_first(self), *nd = first, *next;
990 int key = 0; 991 int key = 0;
@@ -993,7 +994,7 @@ int hists__tui_browse_tree(struct rb_root *self, const char *help)
993 struct hists *hists = rb_entry(nd, struct hists, rb_node); 994 struct hists *hists = rb_entry(nd, struct hists, rb_node);
994 const char *ev_name = __event_name(hists->type, hists->config); 995 const char *ev_name = __event_name(hists->type, hists->config);
995 996
996 key = hists__browse(hists, help, ev_name); 997 key = hists__browse(hists, help, ev_name, evidx);
997 switch (key) { 998 switch (key) {
998 case NEWT_KEY_TAB: 999 case NEWT_KEY_TAB:
999 next = rb_next(nd); 1000 next = rb_next(nd);