aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/builtin-annotate.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/builtin-annotate.c')
-rw-r--r--tools/perf/builtin-annotate.c373
1 files changed, 97 insertions, 276 deletions
diff --git a/tools/perf/builtin-annotate.c b/tools/perf/builtin-annotate.c
index 1478dc64bf15..7b139e1e7e86 100644
--- a/tools/perf/builtin-annotate.c
+++ b/tools/perf/builtin-annotate.c
@@ -8,7 +8,6 @@
8#include "builtin.h" 8#include "builtin.h"
9 9
10#include "util/util.h" 10#include "util/util.h"
11
12#include "util/color.h" 11#include "util/color.h"
13#include <linux/list.h> 12#include <linux/list.h>
14#include "util/cache.h" 13#include "util/cache.h"
@@ -18,6 +17,9 @@
18#include "perf.h" 17#include "perf.h"
19#include "util/debug.h" 18#include "util/debug.h"
20 19
20#include "util/evlist.h"
21#include "util/evsel.h"
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"
@@ -28,7 +30,7 @@
28 30
29static char const *input_name = "perf.data"; 31static char const *input_name = "perf.data";
30 32
31static bool force; 33static bool force, use_tui, use_stdio;
32 34
33static bool full_paths; 35static bool full_paths;
34 36
@@ -36,9 +38,13 @@ static bool print_line;
36 38
37static const char *sym_hist_filter; 39static const char *sym_hist_filter;
38 40
39static int hists__add_entry(struct hists *self, struct addr_location *al) 41static int perf_evlist__add_sample(struct perf_evlist *evlist,
42 struct perf_sample *sample,
43 struct perf_evsel *evsel,
44 struct addr_location *al)
40{ 45{
41 struct hist_entry *he; 46 struct hist_entry *he;
47 int ret;
42 48
43 if (sym_hist_filter != NULL && 49 if (sym_hist_filter != NULL &&
44 (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) { 50 (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) {
@@ -51,25 +57,41 @@ static int hists__add_entry(struct hists *self, struct addr_location *al)
51 return 0; 57 return 0;
52 } 58 }
53 59
54 he = __hists__add_entry(self, al, NULL, 1); 60 he = __hists__add_entry(&evsel->hists, al, NULL, 1);
55 if (he == NULL) 61 if (he == NULL)
56 return -ENOMEM; 62 return -ENOMEM;
57 63
58 return hist_entry__inc_addr_samples(he, al->addr); 64 ret = 0;
65 if (he->ms.sym != NULL) {
66 struct annotation *notes = symbol__annotation(he->ms.sym);
67 if (notes->src == NULL &&
68 symbol__alloc_hist(he->ms.sym, evlist->nr_entries) < 0)
69 return -ENOMEM;
70
71 ret = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
72 }
73
74 evsel->hists.stats.total_period += sample->period;
75 hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
76 return ret;
59} 77}
60 78
61static int process_sample_event(event_t *event, struct perf_session *session) 79static int process_sample_event(union perf_event *event,
80 struct perf_sample *sample,
81 struct perf_evsel *evsel,
82 struct perf_session *session)
62{ 83{
63 struct addr_location al; 84 struct addr_location al;
64 struct sample_data data;
65 85
66 if (event__preprocess_sample(event, session, &al, &data, NULL) < 0) { 86 if (perf_event__preprocess_sample(event, session, &al, sample,
87 symbol__annotate_init) < 0) {
67 pr_warning("problem processing %d event, skipping it.\n", 88 pr_warning("problem processing %d event, skipping it.\n",
68 event->header.type); 89 event->header.type);
69 return -1; 90 return -1;
70 } 91 }
71 92
72 if (!al.filtered && hists__add_entry(&session->hists, &al)) { 93 if (!al.filtered &&
94 perf_evlist__add_sample(session->evlist, sample, evsel, &al)) {
73 pr_warning("problem incrementing symbol count, " 95 pr_warning("problem incrementing symbol count, "
74 "skipping event\n"); 96 "skipping event\n");
75 return -1; 97 return -1;
@@ -78,261 +100,26 @@ static int process_sample_event(event_t *event, struct perf_session *session)
78 return 0; 100 return 0;
79} 101}
80 102
81static int objdump_line__print(struct objdump_line *self, 103static int hist_entry__tty_annotate(struct hist_entry *he, int evidx)
82 struct list_head *head,
83 struct hist_entry *he, u64 len)
84{
85 struct symbol *sym = he->ms.sym;
86 static const char *prev_line;
87 static const char *prev_color;
88
89 if (self->offset != -1) {
90 const char *path = NULL;
91 unsigned int hits = 0;
92 double percent = 0.0;
93 const char *color;
94 struct sym_priv *priv = symbol__priv(sym);
95 struct sym_ext *sym_ext = priv->ext;
96 struct sym_hist *h = priv->hist;
97 s64 offset = self->offset;
98 struct objdump_line *next = objdump__get_next_ip_line(head, self);
99
100 while (offset < (s64)len &&
101 (next == NULL || offset < next->offset)) {
102 if (sym_ext) {
103 if (path == NULL)
104 path = sym_ext[offset].path;
105 percent += sym_ext[offset].percent;
106 } else
107 hits += h->ip[offset];
108
109 ++offset;
110 }
111
112 if (sym_ext == NULL && h->sum)
113 percent = 100.0 * hits / h->sum;
114
115 color = get_percent_color(percent);
116
117 /*
118 * Also color the filename and line if needed, with
119 * the same color than the percentage. Don't print it
120 * twice for close colored ip with the same filename:line
121 */
122 if (path) {
123 if (!prev_line || strcmp(prev_line, path)
124 || color != prev_color) {
125 color_fprintf(stdout, color, " %s", path);
126 prev_line = path;
127 prev_color = color;
128 }
129 }
130
131 color_fprintf(stdout, color, " %7.2f", percent);
132 printf(" : ");
133 color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", self->line);
134 } else {
135 if (!*self->line)
136 printf(" :\n");
137 else
138 printf(" : %s\n", self->line);
139 }
140
141 return 0;
142}
143
144static struct rb_root root_sym_ext;
145
146static void insert_source_line(struct sym_ext *sym_ext)
147{ 104{
148 struct sym_ext *iter; 105 return symbol__tty_annotate(he->ms.sym, he->ms.map, evidx,
149 struct rb_node **p = &root_sym_ext.rb_node; 106 print_line, full_paths, 0, 0);
150 struct rb_node *parent = NULL;
151
152 while (*p != NULL) {
153 parent = *p;
154 iter = rb_entry(parent, struct sym_ext, node);
155
156 if (sym_ext->percent > iter->percent)
157 p = &(*p)->rb_left;
158 else
159 p = &(*p)->rb_right;
160 }
161
162 rb_link_node(&sym_ext->node, parent, p);
163 rb_insert_color(&sym_ext->node, &root_sym_ext);
164} 107}
165 108
166static void free_source_line(struct hist_entry *he, int len) 109static void hists__find_annotations(struct hists *self, int evidx)
167{ 110{
168 struct sym_priv *priv = symbol__priv(he->ms.sym); 111 struct rb_node *nd = rb_first(&self->entries), *next;
169 struct sym_ext *sym_ext = priv->ext;
170 int i;
171
172 if (!sym_ext)
173 return;
174
175 for (i = 0; i < len; i++)
176 free(sym_ext[i].path);
177 free(sym_ext);
178
179 priv->ext = NULL;
180 root_sym_ext = RB_ROOT;
181}
182
183/* Get the filename:line for the colored entries */
184static void
185get_source_line(struct hist_entry *he, int len, const char *filename)
186{
187 struct symbol *sym = he->ms.sym;
188 u64 start;
189 int i;
190 char cmd[PATH_MAX * 2];
191 struct sym_ext *sym_ext;
192 struct sym_priv *priv = symbol__priv(sym);
193 struct sym_hist *h = priv->hist;
194
195 if (!h->sum)
196 return;
197
198 sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
199 if (!priv->ext)
200 return;
201
202 start = he->ms.map->unmap_ip(he->ms.map, sym->start);
203
204 for (i = 0; i < len; i++) {
205 char *path = NULL;
206 size_t line_len;
207 u64 offset;
208 FILE *fp;
209
210 sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
211 if (sym_ext[i].percent <= 0.5)
212 continue;
213
214 offset = start + i;
215 sprintf(cmd, "addr2line -e %s %016llx", filename, offset);
216 fp = popen(cmd, "r");
217 if (!fp)
218 continue;
219
220 if (getline(&path, &line_len, fp) < 0 || !line_len)
221 goto next;
222
223 sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
224 if (!sym_ext[i].path)
225 goto next;
226
227 strcpy(sym_ext[i].path, path);
228 insert_source_line(&sym_ext[i]);
229
230 next:
231 pclose(fp);
232 }
233}
234
235static void print_summary(const char *filename)
236{
237 struct sym_ext *sym_ext;
238 struct rb_node *node;
239
240 printf("\nSorted summary for file %s\n", filename);
241 printf("----------------------------------------------\n\n");
242
243 if (RB_EMPTY_ROOT(&root_sym_ext)) {
244 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
245 return;
246 }
247
248 node = rb_first(&root_sym_ext);
249 while (node) {
250 double percent;
251 const char *color;
252 char *path;
253
254 sym_ext = rb_entry(node, struct sym_ext, node);
255 percent = sym_ext->percent;
256 color = get_percent_color(percent);
257 path = sym_ext->path;
258
259 color_fprintf(stdout, color, " %7.2f %s", percent, path);
260 node = rb_next(node);
261 }
262}
263
264static void hist_entry__print_hits(struct hist_entry *self)
265{
266 struct symbol *sym = self->ms.sym;
267 struct sym_priv *priv = symbol__priv(sym);
268 struct sym_hist *h = priv->hist;
269 u64 len = sym->end - sym->start, offset;
270
271 for (offset = 0; offset < len; ++offset)
272 if (h->ip[offset] != 0)
273 printf("%*Lx: %Lu\n", BITS_PER_LONG / 2,
274 sym->start + offset, h->ip[offset]);
275 printf("%*s: %Lu\n", BITS_PER_LONG / 2, "h->sum", h->sum);
276}
277
278static int hist_entry__tty_annotate(struct hist_entry *he)
279{
280 struct map *map = he->ms.map;
281 struct dso *dso = map->dso;
282 struct symbol *sym = he->ms.sym;
283 const char *filename = dso->long_name, *d_filename;
284 u64 len;
285 LIST_HEAD(head);
286 struct objdump_line *pos, *n;
287
288 if (hist_entry__annotate(he, &head, 0) < 0)
289 return -1;
290
291 if (full_paths)
292 d_filename = filename;
293 else
294 d_filename = basename(filename);
295
296 len = sym->end - sym->start;
297
298 if (print_line) {
299 get_source_line(he, len, filename);
300 print_summary(filename);
301 }
302
303 printf("\n\n------------------------------------------------\n");
304 printf(" Percent | Source code & Disassembly of %s\n", d_filename);
305 printf("------------------------------------------------\n");
306
307 if (verbose)
308 hist_entry__print_hits(he);
309
310 list_for_each_entry_safe(pos, n, &head, node) {
311 objdump_line__print(pos, &head, he, len);
312 list_del(&pos->node);
313 objdump_line__free(pos);
314 }
315
316 if (print_line)
317 free_source_line(he, len);
318
319 return 0;
320}
321
322static void hists__find_annotations(struct hists *self)
323{
324 struct rb_node *first = rb_first(&self->entries), *nd = first;
325 int key = KEY_RIGHT; 112 int key = KEY_RIGHT;
326 113
327 while (nd) { 114 while (nd) {
328 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); 115 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
329 struct sym_priv *priv; 116 struct annotation *notes;
330 117
331 if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned) 118 if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
332 goto find_next; 119 goto find_next;
333 120
334 priv = symbol__priv(he->ms.sym); 121 notes = symbol__annotation(he->ms.sym);
335 if (priv->hist == NULL) { 122 if (notes->src == NULL) {
336find_next: 123find_next:
337 if (key == KEY_LEFT) 124 if (key == KEY_LEFT)
338 nd = rb_prev(nd); 125 nd = rb_prev(nd);
@@ -342,48 +129,51 @@ find_next:
342 } 129 }
343 130
344 if (use_browser > 0) { 131 if (use_browser > 0) {
345 key = hist_entry__tui_annotate(he); 132 key = hist_entry__tui_annotate(he, evidx);
346 if (is_exit_key(key))
347 break;
348 switch (key) { 133 switch (key) {
349 case KEY_RIGHT: 134 case KEY_RIGHT:
350 case '\t': 135 next = rb_next(nd);
351 nd = rb_next(nd);
352 break; 136 break;
353 case KEY_LEFT: 137 case KEY_LEFT:
354 if (nd == first) 138 next = rb_prev(nd);
355 continue;
356 nd = rb_prev(nd);
357 default:
358 break; 139 break;
140 default:
141 return;
359 } 142 }
143
144 if (next != NULL)
145 nd = next;
360 } else { 146 } else {
361 hist_entry__tty_annotate(he); 147 hist_entry__tty_annotate(he, evidx);
362 nd = rb_next(nd); 148 nd = rb_next(nd);
363 /* 149 /*
364 * Since we have a hist_entry per IP for the same 150 * Since we have a hist_entry per IP for the same
365 * symbol, free he->ms.sym->hist to signal we already 151 * symbol, free he->ms.sym->src to signal we already
366 * processed this symbol. 152 * processed this symbol.
367 */ 153 */
368 free(priv->hist); 154 free(notes->src);
369 priv->hist = NULL; 155 notes->src = NULL;
370 } 156 }
371 } 157 }
372} 158}
373 159
374static struct perf_event_ops event_ops = { 160static struct perf_event_ops event_ops = {
375 .sample = process_sample_event, 161 .sample = process_sample_event,
376 .mmap = event__process_mmap, 162 .mmap = perf_event__process_mmap,
377 .comm = event__process_comm, 163 .comm = perf_event__process_comm,
378 .fork = event__process_task, 164 .fork = perf_event__process_task,
165 .ordered_samples = true,
166 .ordering_requires_timestamps = true,
379}; 167};
380 168
381static int __cmd_annotate(void) 169static int __cmd_annotate(void)
382{ 170{
383 int ret; 171 int ret;
384 struct perf_session *session; 172 struct perf_session *session;
173 struct perf_evsel *pos;
174 u64 total_nr_samples;
385 175
386 session = perf_session__new(input_name, O_RDONLY, force, false); 176 session = perf_session__new(input_name, O_RDONLY, force, false, &event_ops);
387 if (session == NULL) 177 if (session == NULL)
388 return -ENOMEM; 178 return -ENOMEM;
389 179
@@ -402,12 +192,36 @@ static int __cmd_annotate(void)
402 if (verbose > 2) 192 if (verbose > 2)
403 perf_session__fprintf_dsos(session, stdout); 193 perf_session__fprintf_dsos(session, stdout);
404 194
405 hists__collapse_resort(&session->hists); 195 total_nr_samples = 0;
406 hists__output_resort(&session->hists); 196 list_for_each_entry(pos, &session->evlist->entries, node) {
407 hists__find_annotations(&session->hists); 197 struct hists *hists = &pos->hists;
408out_delete: 198 u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
409 perf_session__delete(session); 199
200 if (nr_samples > 0) {
201 total_nr_samples += nr_samples;
202 hists__collapse_resort(hists);
203 hists__output_resort(hists);
204 hists__find_annotations(hists, pos->idx);
205 }
206 }
410 207
208 if (total_nr_samples == 0) {
209 ui__warning("The %s file has no samples!\n", input_name);
210 goto out_delete;
211 }
212out_delete:
213 /*
214 * Speed up the exit process, for large files this can
215 * take quite a while.
216 *
217 * XXX Enable this when using valgrind or if we ever
218 * librarize this command.
219 *
220 * Also experiment with obstacks to see how much speed
221 * up we'll get here.
222 *
223 * perf_session__delete(session);
224 */
411 return ret; 225 return ret;
412} 226}
413 227
@@ -428,6 +242,8 @@ static const struct option options[] = {
428 "be more verbose (show symbol address, etc)"), 242 "be more verbose (show symbol address, etc)"),
429 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, 243 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
430 "dump raw trace in ASCII"), 244 "dump raw trace in ASCII"),
245 OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"),
246 OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"),
431 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, 247 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
432 "file", "vmlinux pathname"), 248 "file", "vmlinux pathname"),
433 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules, 249 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
@@ -443,9 +259,14 @@ int cmd_annotate(int argc, const char **argv, const char *prefix __used)
443{ 259{
444 argc = parse_options(argc, argv, options, annotate_usage, 0); 260 argc = parse_options(argc, argv, options, annotate_usage, 0);
445 261
446 setup_browser(); 262 if (use_stdio)
263 use_browser = 0;
264 else if (use_tui)
265 use_browser = 1;
266
267 setup_browser(true);
447 268
448 symbol_conf.priv_size = sizeof(struct sym_priv); 269 symbol_conf.priv_size = sizeof(struct annotation);
449 symbol_conf.try_vmlinux_path = true; 270 symbol_conf.try_vmlinux_path = true;
450 271
451 if (symbol__init() < 0) 272 if (symbol__init() < 0)