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