perf ui browser: Return the current color when setting a new one
[deliverable/linux.git] / tools / perf / util / ui / browsers / annotate.c
CommitLineData
ae55795e 1#include "../../util.h"
211ef127
ACM
2#include "../browser.h"
3#include "../helpline.h"
4#include "../libslang.h"
ae55795e
ACM
5#include "../ui.h"
6#include "../util.h"
78f7defe 7#include "../../annotate.h"
211ef127
ACM
8#include "../../hist.h"
9#include "../../sort.h"
10#include "../../symbol.h"
c97cf422 11#include <pthread.h>
cf958003 12#include <newt.h>
211ef127 13
92221162
ACM
14struct annotate_browser {
15 struct ui_browser b;
16 struct rb_root entries;
f1e9214c 17 struct rb_node *curr_hot;
34958544 18 struct objdump_line *selection;
0361fc25
ACM
19 int nr_asm_entries;
20 int nr_entries;
21 bool hide_src_code;
92221162
ACM
22};
23
24struct objdump_line_rb_node {
25 struct rb_node rb_node;
26 double percent;
27 u32 idx;
0361fc25 28 int idx_asm;
92221162
ACM
29};
30
31static inline
32struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
33{
34 return (struct objdump_line_rb_node *)(self + 1);
35}
36
0361fc25
ACM
37static bool objdump_line__filter(struct ui_browser *browser, void *entry)
38{
39 struct annotate_browser *ab = container_of(browser, struct annotate_browser, b);
40
41 if (ab->hide_src_code) {
42 struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
43 return ol->offset == -1;
44 }
45
46 return false;
47}
48
211ef127
ACM
49static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
50{
34958544 51 struct annotate_browser *ab = container_of(self, struct annotate_browser, b);
0361fc25 52 struct objdump_line *ol = list_entry(entry, struct objdump_line, node);
211ef127
ACM
53 bool current_entry = ui_browser__is_current_entry(self, row);
54 int width = self->width;
55
56 if (ol->offset != -1) {
92221162 57 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
8f9bbc40 58 ui_browser__set_percent_color(self, olrb->percent, current_entry);
92221162 59 slsmg_printf(" %7.2f ", olrb->percent);
92221162 60 } else {
8f9bbc40 61 ui_browser__set_percent_color(self, 0, current_entry);
92221162
ACM
62 slsmg_write_nstring(" ", 9);
63 }
64
65 SLsmg_write_char(':');
66 slsmg_write_nstring(" ", 8);
c172f742
ACM
67
68 /* The scroll bar isn't being used */
69 if (!self->navkeypressed)
70 width += 1;
71
58e817d9
NK
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);
76
92221162
ACM
77 if (!*ol->line)
78 slsmg_write_nstring(" ", width - 18);
79 else
80 slsmg_write_nstring(ol->line, width - 18);
b99976e2 81
58e817d9 82 if (current_entry)
34958544 83 ab->selection = ol;
92221162
ACM
84}
85
86static double objdump_line__calc_percent(struct objdump_line *self,
2f525d01 87 struct symbol *sym, int evidx)
92221162
ACM
88{
89 double percent = 0.0;
90
91 if (self->offset != -1) {
92 int len = sym->end - sym->start;
211ef127 93 unsigned int hits = 0;
78f7defe 94 struct annotation *notes = symbol__annotation(sym);
ce6f4fab 95 struct source_line *src_line = notes->src->lines;
2f525d01 96 struct sym_hist *h = annotation__histogram(notes, evidx);
92221162 97 s64 offset = self->offset;
ce6f4fab 98 struct objdump_line *next;
92221162 99
ce6f4fab 100 next = objdump__get_next_ip_line(&notes->src->source, self);
211ef127
ACM
101 while (offset < (s64)len &&
102 (next == NULL || offset < next->offset)) {
78f7defe
ACM
103 if (src_line) {
104 percent += src_line[offset].percent;
211ef127 105 } else
78f7defe 106 hits += h->addr[offset];
211ef127
ACM
107
108 ++offset;
109 }
78f7defe
ACM
110 /*
111 * If the percentage wasn't already calculated in
112 * symbol__get_source_line, do it now:
113 */
114 if (src_line == NULL && h->sum)
211ef127 115 percent = 100.0 * hits / h->sum;
211ef127
ACM
116 }
117
92221162
ACM
118 return percent;
119}
120
121static void objdump__insert_line(struct rb_root *self,
122 struct objdump_line_rb_node *line)
123{
124 struct rb_node **p = &self->rb_node;
125 struct rb_node *parent = NULL;
126 struct objdump_line_rb_node *l;
127
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;
135 }
136 rb_link_node(&line->rb_node, parent, p);
137 rb_insert_color(&line->rb_node, self);
211ef127
ACM
138}
139
f1e9214c
ACM
140static void annotate_browser__set_top(struct annotate_browser *self,
141 struct rb_node *nd)
142{
143 struct objdump_line_rb_node *rbpos;
144 struct objdump_line *pos;
145 unsigned back;
146
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;
152
153 while (self->b.top_idx != 0 && back != 0) {
154 pos = list_entry(pos->node.prev, struct objdump_line, node);
155
156 --self->b.top_idx;
157 --back;
158 }
159
160 self->b.top = pos;
161 self->curr_hot = nd;
162}
163
c97cf422
ACM
164static void annotate_browser__calc_percent(struct annotate_browser *browser,
165 int evidx)
f1e9214c 166{
34958544
ACM
167 struct map_symbol *ms = browser->b.priv;
168 struct symbol *sym = ms->sym;
c97cf422
ACM
169 struct annotation *notes = symbol__annotation(sym);
170 struct objdump_line *pos;
171
172 browser->entries = RB_ROOT;
173
174 pthread_mutex_lock(&notes->lock);
175
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;
182 }
183 objdump__insert_line(&browser->entries, rbpos);
184 }
185 pthread_mutex_unlock(&notes->lock);
186
187 browser->curr_hot = rb_last(&browser->entries);
188}
189
0361fc25
ACM
190static bool annotate_browser__toggle_source(struct annotate_browser *browser)
191{
192 struct objdump_line *ol;
193 struct objdump_line_rb_node *olrb;
194 off_t offset = browser->b.index - browser->b.top_idx;
195
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);
199
200 if (browser->hide_src_code) {
201 if (olrb->idx_asm < offset)
202 offset = olrb->idx;
203
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;
214 }
215
216 if (olrb->idx_asm < offset)
217 offset = olrb->idx_asm;
218
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;
224 }
225
226 return true;
227}
228
c97cf422 229static int annotate_browser__run(struct annotate_browser *self, int evidx,
d04b35f8 230 void(*timer)(void *arg),
34958544 231 void *arg, int delay_secs)
c97cf422
ACM
232{
233 struct rb_node *nd = NULL;
34958544
ACM
234 struct map_symbol *ms = self->b.priv;
235 struct symbol *sym = ms->sym;
824ac0e9
NK
236 const char *help = "<-/ESC: Exit, TAB/shift+TAB: Cycle hot lines, "
237 "H: Go to hottest line, ->/ENTER: Line action, "
238 "S: Toggle source code view";
b50e003d 239 int key;
f1e9214c 240
0361fc25 241 if (ui_browser__show(&self->b, sym->name, help) < 0)
f1e9214c 242 return -1;
c97cf422 243
c97cf422
ACM
244 annotate_browser__calc_percent(self, evidx);
245
246 if (self->curr_hot)
247 annotate_browser__set_top(self, self->curr_hot);
f1e9214c
ACM
248
249 nd = self->curr_hot;
c97cf422 250
f1e9214c 251 while (1) {
3af6e338 252 key = ui_browser__run(&self->b, delay_secs);
f1e9214c 253
81cce8de 254 if (delay_secs != 0) {
c97cf422
ACM
255 annotate_browser__calc_percent(self, evidx);
256 /*
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).
260 */
261 if (nd != NULL && RB_EMPTY_NODE(nd))
262 nd = NULL;
263 }
264
b50e003d 265 switch (key) {
cf958003 266 case K_TIMER:
81cce8de
ACM
267 if (timer != NULL)
268 timer(arg);
269
270 if (delay_secs != 0)
c97cf422
ACM
271 symbol__annotate_decay_histogram(sym, evidx);
272 continue;
cf958003 273 case K_TAB:
c97cf422
ACM
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;
f1e9214c 280 break;
cf958003 281 case K_UNTAB:
c97cf422
ACM
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':
ef7c5372 290 case 'h':
c97cf422 291 nd = self->curr_hot;
f1e9214c 292 break;
0361fc25 293 case 'S':
ef7c5372 294 case 's':
0361fc25
ACM
295 if (annotate_browser__toggle_source(self))
296 ui_helpline__puts(help);
297 continue;
cf958003
ACM
298 case K_ENTER:
299 case K_RIGHT:
234a5375
ACM
300 if (self->selection == NULL) {
301 ui_helpline__puts("Huh? No selection. Report to linux-kernel@vger.kernel.org");
302 continue;
303 }
304
305 if (self->selection->offset == -1) {
306 ui_helpline__puts("Actions are only available for assembly lines.");
307 continue;
308 } else {
34958544
ACM
309 char *s = strstr(self->selection->line, "callq ");
310 struct annotation *notes;
311 struct symbol *target;
312 u64 ip;
313
234a5375
ACM
314 if (s == NULL) {
315 ui_helpline__puts("Actions are only available for the 'callq' instruction.");
34958544 316 continue;
234a5375 317 }
34958544
ACM
318
319 s = strchr(s, ' ');
234a5375
ACM
320 if (s++ == NULL) {
321 ui_helpline__puts("Invallid callq instruction.");
34958544 322 continue;
234a5375 323 }
34958544
ACM
324
325 ip = strtoull(s, NULL, 16);
326 ip = ms->map->map_ip(ms->map, ip);
327 target = map__find_symbol(ms->map, ip, NULL);
234a5375
ACM
328 if (target == NULL) {
329 ui_helpline__puts("The called function was not found.");
34958544 330 continue;
234a5375 331 }
34958544
ACM
332
333 notes = symbol__annotation(target);
334 pthread_mutex_lock(&notes->lock);
335
d04b35f8 336 if (notes->src == NULL && symbol__alloc_hist(target) < 0) {
34958544
ACM
337 pthread_mutex_unlock(&notes->lock);
338 ui__warning("Not enough memory for annotating '%s' symbol!\n",
339 target->name);
340 continue;
341 }
342
343 pthread_mutex_unlock(&notes->lock);
d04b35f8 344 symbol__tui_annotate(target, ms->map, evidx,
34958544 345 timer, arg, delay_secs);
142cfbd0 346 ui_browser__show_title(&self->b, sym->name);
34958544 347 }
fe46e64c 348 continue;
cf958003
ACM
349 case K_LEFT:
350 case K_ESC:
ed7e5662
ACM
351 case 'q':
352 case CTRL('c'):
f1e9214c 353 goto out;
ed7e5662
ACM
354 default:
355 continue;
f1e9214c 356 }
c97cf422
ACM
357
358 if (nd != NULL)
359 annotate_browser__set_top(self, nd);
f1e9214c
ACM
360 }
361out:
59e8fe32 362 ui_browser__hide(&self->b);
b50e003d 363 return key;
f1e9214c
ACM
364}
365
d04b35f8 366int hist_entry__tui_annotate(struct hist_entry *he, int evidx,
81cce8de 367 void(*timer)(void *arg), void *arg, int delay_secs)
78f7defe 368{
d04b35f8 369 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx,
81cce8de 370 timer, arg, delay_secs);
78f7defe
ACM
371}
372
c97cf422 373int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx,
d04b35f8 374 void(*timer)(void *arg), void *arg,
34958544 375 int delay_secs)
211ef127 376{
211ef127 377 struct objdump_line *pos, *n;
db9a9cbc 378 struct annotation *notes;
34958544
ACM
379 struct map_symbol ms = {
380 .map = map,
381 .sym = sym,
382 };
92221162
ACM
383 struct annotate_browser browser = {
384 .b = {
92221162
ACM
385 .refresh = ui_browser__list_head_refresh,
386 .seek = ui_browser__list_head_seek,
387 .write = annotate_browser__write,
0361fc25 388 .filter = objdump_line__filter,
34958544 389 .priv = &ms,
c172f742 390 .use_navkeypressed = true,
92221162 391 },
211ef127
ACM
392 };
393 int ret;
394
78f7defe 395 if (sym == NULL)
211ef127
ACM
396 return -1;
397
78f7defe 398 if (map->dso->annotate_warned)
211ef127
ACM
399 return -1;
400
c97cf422 401 if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) {
ae55795e 402 ui__error("%s", ui_helpline__last_msg);
211ef127
ACM
403 return -1;
404 }
405
406 ui_helpline__push("Press <- or ESC to exit");
407
db9a9cbc
LM
408 notes = symbol__annotation(sym);
409
ce6f4fab 410 list_for_each_entry(pos, &notes->src->source, node) {
c97cf422 411 struct objdump_line_rb_node *rbpos;
211ef127 412 size_t line_len = strlen(pos->line);
c97cf422 413
92221162
ACM
414 if (browser.b.width < line_len)
415 browser.b.width = line_len;
416 rbpos = objdump_line__rb(pos);
0361fc25
ACM
417 rbpos->idx = browser.nr_entries++;
418 if (pos->offset != -1)
419 rbpos->idx_asm = browser.nr_asm_entries++;
420 else
421 rbpos->idx_asm = -1;
92221162
ACM
422 }
423
0361fc25 424 browser.b.nr_entries = browser.nr_entries;
db9a9cbc 425 browser.b.entries = &notes->src->source,
92221162 426 browser.b.width += 18; /* Percentage */
d04b35f8 427 ret = annotate_browser__run(&browser, evidx, timer, arg, delay_secs);
ce6f4fab 428 list_for_each_entry_safe(pos, n, &notes->src->source, node) {
211ef127
ACM
429 list_del(&pos->node);
430 objdump_line__free(pos);
431 }
211ef127
ACM
432 return ret;
433}
This page took 0.1029 seconds and 5 git commands to generate.