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