Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[~shefty/rdma-dev.git] / tools / perf / util / ui / browsers / annotate.c
1 #include "../browser.h"
2 #include "../helpline.h"
3 #include "../libslang.h"
4 #include "../../annotate.h"
5 #include "../../hist.h"
6 #include "../../sort.h"
7 #include "../../symbol.h"
8 #include <pthread.h>
9 #include <newt.h>
10
11 static void ui__error_window(const char *fmt, ...)
12 {
13         va_list ap;
14
15         va_start(ap, fmt);
16         newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
17         va_end(ap);
18 }
19
20 struct annotate_browser {
21         struct ui_browser b;
22         struct rb_root    entries;
23         struct rb_node    *curr_hot;
24         struct objdump_line *selection;
25         int                 nr_asm_entries;
26         int                 nr_entries;
27         bool                hide_src_code;
28 };
29
30 struct objdump_line_rb_node {
31         struct rb_node  rb_node;
32         double          percent;
33         u32             idx;
34         int             idx_asm;
35 };
36
37 static inline
38 struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
39 {
40         return (struct objdump_line_rb_node *)(self + 1);
41 }
42
43 static bool objdump_line__filter(struct ui_browser *browser, void *entry)
44 {
45         struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
46
47         if (ab->hide_src_code) {
48                 struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
49                 return ol->offset == -1;
50         }
51
52         return false;
53 }
54
55 static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
56 {
57         struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
58         struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
59         bool current_entry = ui_browser__is_current_entry(self, row);
60         int width = self->width;
61
62         if (ol->offset != -1) {
63                 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
64                 ui_browser__set_percent_color(self, olrb->percent, current_entry);
65                 slsmg_printf(" %7.2f ", olrb->percent);
66         } else {
67                 ui_browser__set_percent_color(self, 0, current_entry);
68                 slsmg_write_nstring(" ", 9);
69         }
70
71         SLsmg_write_char(':');
72         slsmg_write_nstring(" ", 8);
73
74         /* The scroll bar isn't being used */
75         if (!self->navkeypressed)
76                 width += 1;
77
78         if (!*ol->line)
79                 slsmg_write_nstring(" ", width - 18);
80         else
81                 slsmg_write_nstring(ol->line, width - 18);
82
83         if (!current_entry)
84                 ui_browser__set_color(self, HE_COLORSET_CODE);
85         else
86                 ab->selection = ol;
87 }
88
89 static double objdump_line__calc_percent(struct objdump_line *self,
90                                          struct symbol *sym, int evidx)
91 {
92         double percent = 0.0;
93
94         if (self->offset != -1) {
95                 int len = sym->end - sym->start;
96                 unsigned int hits = 0;
97                 struct annotation *notes = symbol__annotation(sym);
98                 struct source_line *src_line = notes->src->lines;
99                 struct sym_hist *h = annotation__histogram(notes, evidx);
100                 s64 offset = self->offset;
101                 struct objdump_line *next;
102
103                 next = objdump__get_next_ip_line(&notes->src->source, self);
104                 while (offset < (s64)len &&
105                        (next == NULL || offset < next->offset)) {
106                         if (src_line) {
107                                 percent += src_line[offset].percent;
108                         } else
109                                 hits += h->addr[offset];
110
111                         ++offset;
112                 }
113                 /*
114                  * If the percentage wasn't already calculated in
115                  * symbol__get_source_line, do it now:
116                  */
117                 if (src_line == NULL && h->sum)
118                         percent = 100.0 * hits / h->sum;
119         }
120
121         return percent;
122 }
123
124 static void objdump__insert_line(struct rb_root *self,
125                                  struct objdump_line_rb_node *line)
126 {
127         struct rb_node **p = &self->rb_node;
128         struct rb_node *parent = NULL;
129         struct objdump_line_rb_node *l;
130
131         while (*p != NULL) {
132                 parent = *p;
133                 l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
134                 if (line->percent < l->percent)
135                         p = &(*p)->rb_left;
136                 else
137                         p = &(*p)->rb_right;
138         }
139         rb_link_node(&line->rb_node, parent, p);
140         rb_insert_color(&line->rb_node, self);
141 }
142
143 static void annotate_browser__set_top(struct annotate_browser *self,
144                                       struct rb_node *nd)
145 {
146         struct objdump_line_rb_node *rbpos;
147         struct objdump_line *pos;
148         unsigned back;
149
150         ui_browser__refresh_dimensions(&self->b);
151         back = self->b.height / 2;
152         rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
153         pos = ((struct objdump_line *)rbpos) - 1;
154         self->b.top_idx = self->b.index = rbpos->idx;
155
156         while (self->b.top_idx != 0 && back != 0) {
157                 pos = list_entry(pos->node.prev, struct objdump_line, node);
158
159                 --self->b.top_idx;
160                 --back;
161         }
162
163         self->b.top = pos;
164         self->curr_hot = nd;
165 }
166
167 static void annotate_browser__calc_percent(struct annotate_browser *browser,
168                                            int evidx)
169 {
170         struct map_symbol *ms = browser->b.priv;
171         struct symbol *sym = ms->sym;
172         struct annotation *notes = symbol__annotation(sym);
173         struct objdump_line *pos;
174
175         browser->entries = RB_ROOT;
176
177         pthread_mutex_lock(&notes->lock);
178
179         list_for_each_entry(pos, &notes->src->source, node) {
180                 struct objdump_line_rb_node *rbpos = objdump_line__rb(pos);
181                 rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
182                 if (rbpos->percent < 0.01) {
183                         RB_CLEAR_NODE(&rbpos->rb_node);
184                         continue;
185                 }
186                 objdump__insert_line(&browser->entries, rbpos);
187         }
188         pthread_mutex_unlock(&notes->lock);
189
190         browser->curr_hot = rb_last(&browser->entries);
191 }
192
193 static bool annotate_browser__toggle_source(struct annotate_browser *browser)
194 {
195         struct objdump_line *ol;
196         struct objdump_line_rb_node *olrb;
197         off_t offset = browser->b.index - browser->b.top_idx;
198
199         browser->b.seek(&browser->b, offset, SEEK_CUR);
200         ol = list_entry(browser->b.top, struct objdump_line, node);
201         olrb = objdump_line__rb(ol);
202
203         if (browser->hide_src_code) {
204                 if (olrb->idx_asm < offset)
205                         offset = olrb->idx;
206
207                 browser->b.nr_entries = browser->nr_entries;
208                 browser->hide_src_code = false;
209                 browser->b.seek(&browser->b, -offset, SEEK_CUR);
210                 browser->b.top_idx = olrb->idx - offset;
211                 browser->b.index = olrb->idx;
212         } else {
213                 if (olrb->idx_asm < 0) {
214                         ui_helpline__puts("Only available for assembly lines.");
215                         browser->b.seek(&browser->b, -offset, SEEK_CUR);
216                         return false;
217                 }
218
219                 if (olrb->idx_asm < offset)
220                         offset = olrb->idx_asm;
221
222                 browser->b.nr_entries = browser->nr_asm_entries;
223                 browser->hide_src_code = true;
224                 browser->b.seek(&browser->b, -offset, SEEK_CUR);
225                 browser->b.top_idx = olrb->idx_asm - offset;
226                 browser->b.index = olrb->idx_asm;
227         }
228
229         return true;
230 }
231
232 static int annotate_browser__run(struct annotate_browser *self, int evidx,
233                                  int nr_events, void(*timer)(void *arg),
234                                  void *arg, int delay_secs)
235 {
236         struct rb_node *nd = NULL;
237         struct map_symbol *ms = self->b.priv;
238         struct symbol *sym = ms->sym;
239         const char *help = "<-, ESC: exit, TAB/shift+TAB: cycle hottest lines, "
240                            "H: Hottest, -> Line action, S -> Toggle source "
241                            "code view";
242         int key;
243
244         if (ui_browser__show(&self->b, sym->name, help) < 0)
245                 return -1;
246
247         annotate_browser__calc_percent(self, evidx);
248
249         if (self->curr_hot)
250                 annotate_browser__set_top(self, self->curr_hot);
251
252         nd = self->curr_hot;
253
254         while (1) {
255                 key = ui_browser__run(&self->b, delay_secs);
256
257                 if (delay_secs != 0) {
258                         annotate_browser__calc_percent(self, evidx);
259                         /*
260                          * Current line focus got out of the list of most active
261                          * lines, NULL it so that if TAB|UNTAB is pressed, we
262                          * move to curr_hot (current hottest line).
263                          */
264                         if (nd != NULL && RB_EMPTY_NODE(nd))
265                                 nd = NULL;
266                 }
267
268                 switch (key) {
269                 case K_TIMER:
270                         if (timer != NULL)
271                                 timer(arg);
272
273                         if (delay_secs != 0)
274                                 symbol__annotate_decay_histogram(sym, evidx);
275                         continue;
276                 case K_TAB:
277                         if (nd != NULL) {
278                                 nd = rb_prev(nd);
279                                 if (nd == NULL)
280                                         nd = rb_last(&self->entries);
281                         } else
282                                 nd = self->curr_hot;
283                         break;
284                 case K_UNTAB:
285                         if (nd != NULL)
286                                 nd = rb_next(nd);
287                                 if (nd == NULL)
288                                         nd = rb_first(&self->entries);
289                         else
290                                 nd = self->curr_hot;
291                         break;
292                 case 'H':
293                         nd = self->curr_hot;
294                         break;
295                 case 'S':
296                         if (annotate_browser__toggle_source(self))
297                                 ui_helpline__puts(help);
298                         continue;
299                 case K_ENTER:
300                 case K_RIGHT:
301                         if (self->selection == NULL) {
302                                 ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
303                                 continue;
304                         }
305
306                         if (self->selection->offset == -1) {
307                                 ui_helpline__puts("Actions are only available for assembly lines.");
308                                 continue;
309                         } else {
310                                 char *s = strstr(self->selection->line, "callq ");
311                                 struct annotation *notes;
312                                 struct symbol *target;
313                                 u64 ip;
314
315                                 if (s == NULL) {
316                                         ui_helpline__puts("Actions are only available for the 'callq' instruction.");
317                                         continue;
318                                 }
319
320                                 s = strchr(s, ' ');
321                                 if (s++ == NULL) {
322                                         ui_helpline__puts("Invallid callq instruction.");
323                                         continue;
324                                 }
325
326                                 ip = strtoull(s, NULL, 16);
327                                 ip = ms->map->map_ip(ms->map, ip);
328                                 target = map__find_symbol(ms->map, ip, NULL);
329                                 if (target == NULL) {
330                                         ui_helpline__puts("The called function was not found.");
331                                         continue;
332                                 }
333
334                                 notes = symbol__annotation(target);
335                                 pthread_mutex_lock(&notes->lock);
336
337                                 if (notes->src == NULL &&
338                                     symbol__alloc_hist(target, nr_events) < 0) {
339                                         pthread_mutex_unlock(&notes->lock);
340                                         ui__warning("Not enough memory for annotating '%s' symbol!\n",
341                                                     target->name);
342                                         continue;
343                                 }
344
345                                 pthread_mutex_unlock(&notes->lock);
346                                 symbol__tui_annotate(target, ms->map, evidx, nr_events,
347                                                      timer, arg, delay_secs);
348                         }
349                         continue;
350                 case K_LEFT:
351                 case K_ESC:
352                 case 'q':
353                 case CTRL('c'):
354                         goto out;
355                 default:
356                         continue;
357                 }
358
359                 if (nd != NULL)
360                         annotate_browser__set_top(self, nd);
361         }
362 out:
363         ui_browser__hide(&self->b);
364         return key;
365 }
366
367 int hist_entry__tui_annotate(struct hist_entry *he, int evidx, int nr_events,
368                              void(*timer)(void *arg), void *arg, int delay_secs)
369 {
370         return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx, nr_events,
371                                     timer, arg, delay_secs);
372 }
373
374 int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
375                          int nr_events, void(*timer)(void *arg), void *arg,
376                          int delay_secs)
377 {
378         struct objdump_line *pos, *n;
379         struct annotation *notes;
380         struct map_symbol ms = {
381                 .map = map,
382                 .sym = sym,
383         };
384         struct annotate_browser browser = {
385                 .b = {
386                         .refresh = ui_browser__list_head_refresh,
387                         .seek    = ui_browser__list_head_seek,
388                         .write   = annotate_browser__write,
389                         .filter  = objdump_line__filter,
390                         .priv    = &ms,
391                         .use_navkeypressed = true,
392                 },
393         };
394         int ret;
395
396         if (sym == NULL)
397                 return -1;
398
399         if (map->dso->annotate_warned)
400                 return -1;
401
402         if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
403                 ui__error_window(ui_helpline__last_msg);
404                 return -1;
405         }
406
407         ui_helpline__push("Press <- or ESC to exit");
408
409         notes = symbol__annotation(sym);
410
411         list_for_each_entry(pos, &notes->src->source, node) {
412                 struct objdump_line_rb_node *rbpos;
413                 size_t line_len = strlen(pos->line);
414
415                 if (browser.b.width < line_len)
416                         browser.b.width = line_len;
417                 rbpos = objdump_line__rb(pos);
418                 rbpos->idx = browser.nr_entries++;
419                 if (pos->offset != -1)
420                         rbpos->idx_asm = browser.nr_asm_entries++;
421                 else
422                         rbpos->idx_asm = -1;
423         }
424
425         browser.b.nr_entries = browser.nr_entries;
426         browser.b.entries = &notes->src->source,
427         browser.b.width += 18; /* Percentage */
428         ret = annotate_browser__run(&browser, evidx, nr_events,
429                                     timer, arg, delay_secs);
430         list_for_each_entry_safe(pos, n, &notes->src->source, node) {
431                 list_del(&pos->node);
432                 objdump_line__free(pos);
433         }
434         return ret;
435 }