Merge branch 'staging-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[pandora-kernel.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 "../../annotate.h"
9 #include <pthread.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 };
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 objdump_line *ol = rb_entry(entry, struct objdump_line, node);
41         bool current_entry = ui_browser__is_current_entry(self, row);
42         int width = self->width;
43
44         if (ol->offset != -1) {
45                 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
46                 ui_browser__set_percent_color(self, olrb->percent, current_entry);
47                 slsmg_printf(" %7.2f ", olrb->percent);
48         } else {
49                 ui_browser__set_percent_color(self, 0, current_entry);
50                 slsmg_write_nstring(" ", 9);
51         }
52
53         SLsmg_write_char(':');
54         slsmg_write_nstring(" ", 8);
55         if (!*ol->line)
56                 slsmg_write_nstring(" ", width - 18);
57         else
58                 slsmg_write_nstring(ol->line, width - 18);
59
60         if (!current_entry)
61                 ui_browser__set_color(self, HE_COLORSET_CODE);
62 }
63
64 static double objdump_line__calc_percent(struct objdump_line *self,
65                                          struct symbol *sym, int evidx)
66 {
67         double percent = 0.0;
68
69         if (self->offset != -1) {
70                 int len = sym->end - sym->start;
71                 unsigned int hits = 0;
72                 struct annotation *notes = symbol__annotation(sym);
73                 struct source_line *src_line = notes->src->lines;
74                 struct sym_hist *h = annotation__histogram(notes, evidx);
75                 s64 offset = self->offset;
76                 struct objdump_line *next;
77
78                 next = objdump__get_next_ip_line(&notes->src->source, self);
79                 while (offset < (s64)len &&
80                        (next == NULL || offset < next->offset)) {
81                         if (src_line) {
82                                 percent += src_line[offset].percent;
83                         } else
84                                 hits += h->addr[offset];
85
86                         ++offset;
87                 }
88                 /*
89                  * If the percentage wasn't already calculated in
90                  * symbol__get_source_line, do it now:
91                  */
92                 if (src_line == NULL && h->sum)
93                         percent = 100.0 * hits / h->sum;
94         }
95
96         return percent;
97 }
98
99 static void objdump__insert_line(struct rb_root *self,
100                                  struct objdump_line_rb_node *line)
101 {
102         struct rb_node **p = &self->rb_node;
103         struct rb_node *parent = NULL;
104         struct objdump_line_rb_node *l;
105
106         while (*p != NULL) {
107                 parent = *p;
108                 l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
109                 if (line->percent < l->percent)
110                         p = &(*p)->rb_left;
111                 else
112                         p = &(*p)->rb_right;
113         }
114         rb_link_node(&line->rb_node, parent, p);
115         rb_insert_color(&line->rb_node, self);
116 }
117
118 static void annotate_browser__set_top(struct annotate_browser *self,
119                                       struct rb_node *nd)
120 {
121         struct objdump_line_rb_node *rbpos;
122         struct objdump_line *pos;
123         unsigned back;
124
125         ui_browser__refresh_dimensions(&self->b);
126         back = self->b.height / 2;
127         rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
128         pos = ((struct objdump_line *)rbpos) - 1;
129         self->b.top_idx = self->b.index = rbpos->idx;
130
131         while (self->b.top_idx != 0 && back != 0) {
132                 pos = list_entry(pos->node.prev, struct objdump_line, node);
133
134                 --self->b.top_idx;
135                 --back;
136         }
137
138         self->b.top = pos;
139         self->curr_hot = nd;
140 }
141
142 static void annotate_browser__calc_percent(struct annotate_browser *browser,
143                                            int evidx)
144 {
145         struct symbol *sym = browser->b.priv;
146         struct annotation *notes = symbol__annotation(sym);
147         struct objdump_line *pos;
148
149         browser->entries = RB_ROOT;
150
151         pthread_mutex_lock(&notes->lock);
152
153         list_for_each_entry(pos, &notes->src->source, node) {
154                 struct objdump_line_rb_node *rbpos = objdump_line__rb(pos);
155                 rbpos->percent = objdump_line__calc_percent(pos, sym, evidx);
156                 if (rbpos->percent < 0.01) {
157                         RB_CLEAR_NODE(&rbpos->rb_node);
158                         continue;
159                 }
160                 objdump__insert_line(&browser->entries, rbpos);
161         }
162         pthread_mutex_unlock(&notes->lock);
163
164         browser->curr_hot = rb_last(&browser->entries);
165 }
166
167 static int annotate_browser__run(struct annotate_browser *self, int evidx,
168                                  int refresh)
169 {
170         struct rb_node *nd = NULL;
171         struct symbol *sym = self->b.priv;
172         /*
173          * RIGHT To allow builtin-annotate to cycle thru multiple symbols by
174          * examining the exit key for this function.
175          */
176         int exit_keys[] = { 'H', NEWT_KEY_TAB, NEWT_KEY_UNTAB,
177                             NEWT_KEY_RIGHT, 0 };
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") < 0)
183                 return -1;
184
185         ui_browser__add_exit_keys(&self->b, exit_keys);
186         annotate_browser__calc_percent(self, evidx);
187
188         if (self->curr_hot)
189                 annotate_browser__set_top(self, self->curr_hot);
190
191         nd = self->curr_hot;
192
193         if (refresh != 0)
194                 newtFormSetTimer(self->b.form, refresh);
195
196         while (1) {
197                 key = ui_browser__run(&self->b);
198
199                 if (refresh != 0) {
200                         annotate_browser__calc_percent(self, evidx);
201                         /*
202                          * Current line focus got out of the list of most active
203                          * lines, NULL it so that if TAB|UNTAB is pressed, we
204                          * move to curr_hot (current hottest line).
205                          */
206                         if (nd != NULL && RB_EMPTY_NODE(nd))
207                                 nd = NULL;
208                 }
209
210                 switch (key) {
211                 case -1:
212                         /*
213                          * FIXME we need to check if it was
214                          * es.reason == NEWT_EXIT_TIMER
215                          */
216                         if (refresh != 0)
217                                 symbol__annotate_decay_histogram(sym, evidx);
218                         continue;
219                 case NEWT_KEY_TAB:
220                         if (nd != NULL) {
221                                 nd = rb_prev(nd);
222                                 if (nd == NULL)
223                                         nd = rb_last(&self->entries);
224                         } else
225                                 nd = self->curr_hot;
226                         break;
227                 case NEWT_KEY_UNTAB:
228                         if (nd != NULL)
229                                 nd = rb_next(nd);
230                                 if (nd == NULL)
231                                         nd = rb_first(&self->entries);
232                         else
233                                 nd = self->curr_hot;
234                         break;
235                 case 'H':
236                         nd = self->curr_hot;
237                         break;
238                 default:
239                         goto out;
240                 }
241
242                 if (nd != NULL)
243                         annotate_browser__set_top(self, nd);
244         }
245 out:
246         ui_browser__hide(&self->b);
247         return key;
248 }
249
250 int hist_entry__tui_annotate(struct hist_entry *he, int evidx)
251 {
252         return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx, 0);
253 }
254
255 int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
256                          int refresh)
257 {
258         struct objdump_line *pos, *n;
259         struct annotation *notes;
260         struct annotate_browser browser = {
261                 .b = {
262                         .refresh = ui_browser__list_head_refresh,
263                         .seek    = ui_browser__list_head_seek,
264                         .write   = annotate_browser__write,
265                         .priv    = sym,
266                 },
267         };
268         int ret;
269
270         if (sym == NULL)
271                 return -1;
272
273         if (map->dso->annotate_warned)
274                 return -1;
275
276         if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
277                 ui__error_window(ui_helpline__last_msg);
278                 return -1;
279         }
280
281         ui_helpline__push("Press <- or ESC to exit");
282
283         notes = symbol__annotation(sym);
284
285         list_for_each_entry(pos, &notes->src->source, node) {
286                 struct objdump_line_rb_node *rbpos;
287                 size_t line_len = strlen(pos->line);
288
289                 if (browser.b.width < line_len)
290                         browser.b.width = line_len;
291                 rbpos = objdump_line__rb(pos);
292                 rbpos->idx = browser.b.nr_entries++;
293         }
294
295         browser.b.entries = &notes->src->source,
296         browser.b.width += 18; /* Percentage */
297         ret = annotate_browser__run(&browser, evidx, refresh);
298         list_for_each_entry_safe(pos, n, &notes->src->source, node) {
299                 list_del(&pos->node);
300                 objdump_line__free(pos);
301         }
302         return ret;
303 }