]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - tools/perf/util/ui/browsers/annotate.c
perf ui: Reimplement ui__popup_menu using ui__browser
[thirdparty/kernel/linux.git] / tools / perf / util / ui / browsers / annotate.c
CommitLineData
211ef127
ACM
1#include "../browser.h"
2#include "../helpline.h"
3#include "../libslang.h"
78f7defe 4#include "../../annotate.h"
211ef127
ACM
5#include "../../hist.h"
6#include "../../sort.h"
7#include "../../symbol.h"
c97cf422 8#include <pthread.h>
cf958003 9#include <newt.h>
211ef127
ACM
10
11static 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
92221162
ACM
20struct annotate_browser {
21 struct ui_browser b;
22 struct rb_root entries;
f1e9214c 23 struct rb_node *curr_hot;
34958544 24 struct objdump_line *selection;
0361fc25
ACM
25 int nr_asm_entries;
26 int nr_entries;
27 bool hide_src_code;
92221162
ACM
28};
29
30struct objdump_line_rb_node {
31 struct rb_node rb_node;
32 double percent;
33 u32 idx;
0361fc25 34 int idx_asm;
92221162
ACM
35};
36
37static inline
38struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
39{
40 return (struct objdump_line_rb_node *)(self + 1);
41}
42
0361fc25
ACM
43static 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
211ef127
ACM
55static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
56{
34958544 57 struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
0361fc25 58 struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
211ef127
ACM
59 bool current_entry = ui_browser__is_current_entry(self, row);
60 int width = self->width;
61
62 if (ol->offset != -1) {
92221162 63 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
8f9bbc40 64 ui_browser__set_percent_color(self, olrb->percent, current_entry);
92221162 65 slsmg_printf(" %7.2f ", olrb->percent);
92221162 66 } else {
8f9bbc40 67 ui_browser__set_percent_color(self, 0, current_entry);
92221162
ACM
68 slsmg_write_nstring(" ", 9);
69 }
70
71 SLsmg_write_char(':');
72 slsmg_write_nstring(" ", 8);
c172f742
ACM
73
74 /* The scroll bar isn't being used */
75 if (!self->navkeypressed)
76 width += 1;
77
92221162
ACM
78 if (!*ol->line)
79 slsmg_write_nstring(" ", width - 18);
80 else
81 slsmg_write_nstring(ol->line, width - 18);
b99976e2
ACM
82
83 if (!current_entry)
84 ui_browser__set_color(self, HE_COLORSET_CODE);
34958544
ACM
85 else
86 ab->selection = ol;
92221162
ACM
87}
88
89static double objdump_line__calc_percent(struct objdump_line *self,
2f525d01 90 struct symbol *sym, int evidx)
92221162
ACM
91{
92 double percent = 0.0;
93
94 if (self->offset != -1) {
95 int len = sym->end - sym->start;
211ef127 96 unsigned int hits = 0;
78f7defe 97 struct annotation *notes = symbol__annotation(sym);
ce6f4fab 98 struct source_line *src_line = notes->src->lines;
2f525d01 99 struct sym_hist *h = annotation__histogram(notes, evidx);
92221162 100 s64 offset = self->offset;
ce6f4fab 101 struct objdump_line *next;
92221162 102
ce6f4fab 103 next = objdump__get_next_ip_line(&notes->src->source, self);
211ef127
ACM
104 while (offset < (s64)len &&
105 (next == NULL || offset < next->offset)) {
78f7defe
ACM
106 if (src_line) {
107 percent += src_line[offset].percent;
211ef127 108 } else
78f7defe 109 hits += h->addr[offset];
211ef127
ACM
110
111 ++offset;
112 }
78f7defe
ACM
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)
211ef127 118 percent = 100.0 * hits / h->sum;
211ef127
ACM
119 }
120
92221162
ACM
121 return percent;
122}
123
124static 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);
211ef127
ACM
141}
142
f1e9214c
ACM
143static 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
c97cf422
ACM
167static void annotate_browser__calc_percent(struct annotate_browser *browser,
168 int evidx)
f1e9214c 169{
34958544
ACM
170 struct map_symbol *ms = browser->b.priv;
171 struct symbol *sym = ms->sym;
c97cf422
ACM
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
0361fc25
ACM
193static 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
c97cf422 232static int annotate_browser__run(struct annotate_browser *self, int evidx,
34958544
ACM
233 int nr_events, void(*timer)(void *arg),
234 void *arg, int delay_secs)
c97cf422
ACM
235{
236 struct rb_node *nd = NULL;
34958544
ACM
237 struct map_symbol *ms = self->b.priv;
238 struct symbol *sym = ms->sym;
0361fc25
ACM
239 const char *help = "<-, ESC: exit, TAB/shift+TAB: cycle hottest lines, "
240 "H: Hottest, -> Line action, S -> Toggle source "
241 "code view";
b50e003d 242 int key;
f1e9214c 243
0361fc25 244 if (ui_browser__show(&self->b, sym->name, help) < 0)
f1e9214c 245 return -1;
c97cf422 246
c97cf422
ACM
247 annotate_browser__calc_percent(self, evidx);
248
249 if (self->curr_hot)
250 annotate_browser__set_top(self, self->curr_hot);
f1e9214c
ACM
251
252 nd = self->curr_hot;
c97cf422 253
f1e9214c 254 while (1) {
3af6e338 255 key = ui_browser__run(&self->b, delay_secs);
f1e9214c 256
81cce8de 257 if (delay_secs != 0) {
c97cf422
ACM
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
b50e003d 268 switch (key) {
cf958003 269 case K_TIMER:
81cce8de
ACM
270 if (timer != NULL)
271 timer(arg);
272
273 if (delay_secs != 0)
c97cf422
ACM
274 symbol__annotate_decay_histogram(sym, evidx);
275 continue;
cf958003 276 case K_TAB:
c97cf422
ACM
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;
f1e9214c 283 break;
cf958003 284 case K_UNTAB:
c97cf422
ACM
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;
f1e9214c 294 break;
0361fc25
ACM
295 case 'S':
296 if (annotate_browser__toggle_source(self))
297 ui_helpline__puts(help);
298 continue;
cf958003
ACM
299 case K_ENTER:
300 case K_RIGHT:
234a5375
ACM
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 {
34958544
ACM
310 char *s = strstr(self->selection->line, "callq ");
311 struct annotation *notes;
312 struct symbol *target;
313 u64 ip;
314
234a5375
ACM
315 if (s == NULL) {
316 ui_helpline__puts("Actions are only available for the 'callq' instruction.");
34958544 317 continue;
234a5375 318 }
34958544
ACM
319
320 s = strchr(s, ' ');
234a5375
ACM
321 if (s++ == NULL) {
322 ui_helpline__puts("Invallid callq instruction.");
34958544 323 continue;
234a5375 324 }
34958544
ACM
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);
234a5375
ACM
329 if (target == NULL) {
330 ui_helpline__puts("The called function was not found.");
34958544 331 continue;
234a5375 332 }
34958544
ACM
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 }
fe46e64c 349 continue;
cf958003
ACM
350 case K_LEFT:
351 case K_ESC:
ed7e5662
ACM
352 case 'q':
353 case CTRL('c'):
f1e9214c 354 goto out;
ed7e5662
ACM
355 default:
356 continue;
f1e9214c 357 }
c97cf422
ACM
358
359 if (nd != NULL)
360 annotate_browser__set_top(self, nd);
f1e9214c
ACM
361 }
362out:
59e8fe32 363 ui_browser__hide(&self->b);
b50e003d 364 return key;
f1e9214c
ACM
365}
366
34958544 367int hist_entry__tui_annotate(struct hist_entry *he, int evidx, int nr_events,
81cce8de 368 void(*timer)(void *arg), void *arg, int delay_secs)
78f7defe 369{
34958544 370 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx, nr_events,
81cce8de 371 timer, arg, delay_secs);
78f7defe
ACM
372}
373
c97cf422 374int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
34958544
ACM
375 int nr_events, void(*timer)(void *arg), void *arg,
376 int delay_secs)
211ef127 377{
211ef127 378 struct objdump_line *pos, *n;
db9a9cbc 379 struct annotation *notes;
34958544
ACM
380 struct map_symbol ms = {
381 .map = map,
382 .sym = sym,
383 };
92221162
ACM
384 struct annotate_browser browser = {
385 .b = {
92221162
ACM
386 .refresh = ui_browser__list_head_refresh,
387 .seek = ui_browser__list_head_seek,
388 .write = annotate_browser__write,
0361fc25 389 .filter = objdump_line__filter,
34958544 390 .priv = &ms,
c172f742 391 .use_navkeypressed = true,
92221162 392 },
211ef127
ACM
393 };
394 int ret;
395
78f7defe 396 if (sym == NULL)
211ef127
ACM
397 return -1;
398
78f7defe 399 if (map->dso->annotate_warned)
211ef127
ACM
400 return -1;
401
c97cf422 402 if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
1e6dd077 403 ui__error_window(ui_helpline__last_msg);
211ef127
ACM
404 return -1;
405 }
406
407 ui_helpline__push("Press <- or ESC to exit");
408
db9a9cbc
LM
409 notes = symbol__annotation(sym);
410
ce6f4fab 411 list_for_each_entry(pos, &notes->src->source, node) {
c97cf422 412 struct objdump_line_rb_node *rbpos;
211ef127 413 size_t line_len = strlen(pos->line);
c97cf422 414
92221162
ACM
415 if (browser.b.width < line_len)
416 browser.b.width = line_len;
417 rbpos = objdump_line__rb(pos);
0361fc25
ACM
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;
92221162
ACM
423 }
424
0361fc25 425 browser.b.nr_entries = browser.nr_entries;
db9a9cbc 426 browser.b.entries = &notes->src->source,
92221162 427 browser.b.width += 18; /* Percentage */
34958544
ACM
428 ret = annotate_browser__run(&browser, evidx, nr_events,
429 timer, arg, delay_secs);
ce6f4fab 430 list_for_each_entry_safe(pos, n, &notes->src->source, node) {
211ef127
ACM
431 list_del(&pos->node);
432 objdump_line__free(pos);
433 }
211ef127
ACM
434 return ret;
435}