perf ui browser: Return the exit key in all browsers
[deliverable/linux.git] / tools / perf / util / ui / browsers / annotate.c
CommitLineData
211ef127
ACM
1#include "../browser.h"
2#include "../helpline.h"
3#include "../libslang.h"
4#include "../../hist.h"
5#include "../../sort.h"
6#include "../../symbol.h"
7
8static void ui__error_window(const char *fmt, ...)
9{
10 va_list ap;
11
12 va_start(ap, fmt);
13 newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap);
14 va_end(ap);
15}
16
92221162
ACM
17struct annotate_browser {
18 struct ui_browser b;
19 struct rb_root entries;
f1e9214c 20 struct rb_node *curr_hot;
92221162
ACM
21};
22
23struct objdump_line_rb_node {
24 struct rb_node rb_node;
25 double percent;
26 u32 idx;
27};
28
29static inline
30struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
31{
32 return (struct objdump_line_rb_node *)(self + 1);
33}
34
211ef127
ACM
35static void annotate_browser__write(struct ui_browser *self, void *entry, int row)
36{
37 struct objdump_line *ol = rb_entry(entry, struct objdump_line, node);
38 bool current_entry = ui_browser__is_current_entry(self, row);
39 int width = self->width;
40
41 if (ol->offset != -1) {
92221162 42 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
8f9bbc40 43 ui_browser__set_percent_color(self, olrb->percent, current_entry);
92221162
ACM
44 slsmg_printf(" %7.2f ", olrb->percent);
45 if (!current_entry)
8f9bbc40 46 ui_browser__set_color(self, HE_COLORSET_CODE);
92221162 47 } else {
8f9bbc40 48 ui_browser__set_percent_color(self, 0, current_entry);
92221162
ACM
49 slsmg_write_nstring(" ", 9);
50 }
51
52 SLsmg_write_char(':');
53 slsmg_write_nstring(" ", 8);
54 if (!*ol->line)
55 slsmg_write_nstring(" ", width - 18);
56 else
57 slsmg_write_nstring(ol->line, width - 18);
58}
59
60static double objdump_line__calc_percent(struct objdump_line *self,
61 struct list_head *head,
62 struct symbol *sym)
63{
64 double percent = 0.0;
65
66 if (self->offset != -1) {
67 int len = sym->end - sym->start;
211ef127 68 unsigned int hits = 0;
211ef127
ACM
69 struct sym_priv *priv = symbol__priv(sym);
70 struct sym_ext *sym_ext = priv->ext;
71 struct sym_hist *h = priv->hist;
92221162
ACM
72 s64 offset = self->offset;
73 struct objdump_line *next = objdump__get_next_ip_line(head, self);
74
211ef127
ACM
75
76 while (offset < (s64)len &&
77 (next == NULL || offset < next->offset)) {
78 if (sym_ext) {
79 percent += sym_ext[offset].percent;
80 } else
81 hits += h->ip[offset];
82
83 ++offset;
84 }
85
86 if (sym_ext == NULL && h->sum)
87 percent = 100.0 * hits / h->sum;
211ef127
ACM
88 }
89
92221162
ACM
90 return percent;
91}
92
93static void objdump__insert_line(struct rb_root *self,
94 struct objdump_line_rb_node *line)
95{
96 struct rb_node **p = &self->rb_node;
97 struct rb_node *parent = NULL;
98 struct objdump_line_rb_node *l;
99
100 while (*p != NULL) {
101 parent = *p;
102 l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
103 if (line->percent < l->percent)
104 p = &(*p)->rb_left;
105 else
106 p = &(*p)->rb_right;
107 }
108 rb_link_node(&line->rb_node, parent, p);
109 rb_insert_color(&line->rb_node, self);
211ef127
ACM
110}
111
f1e9214c
ACM
112static void annotate_browser__set_top(struct annotate_browser *self,
113 struct rb_node *nd)
114{
115 struct objdump_line_rb_node *rbpos;
116 struct objdump_line *pos;
117 unsigned back;
118
119 ui_browser__refresh_dimensions(&self->b);
120 back = self->b.height / 2;
121 rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
122 pos = ((struct objdump_line *)rbpos) - 1;
123 self->b.top_idx = self->b.index = rbpos->idx;
124
125 while (self->b.top_idx != 0 && back != 0) {
126 pos = list_entry(pos->node.prev, struct objdump_line, node);
127
128 --self->b.top_idx;
129 --back;
130 }
131
132 self->b.top = pos;
133 self->curr_hot = nd;
134}
135
b50e003d 136static int annotate_browser__run(struct annotate_browser *self)
f1e9214c
ACM
137{
138 struct rb_node *nd;
139 struct hist_entry *he = self->b.priv;
b50e003d 140 int key;
f1e9214c 141
59e8fe32 142 if (ui_browser__show(&self->b, he->ms.sym->name,
b50e003d 143 "<-, -> or ESC: exit, TAB/shift+TAB: cycle thru samples") < 0)
f1e9214c
ACM
144 return -1;
145
f1e9214c 146 newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT);
e9184621 147 newtFormAddHotKey(self->b.form, NEWT_KEY_RIGHT);
f1e9214c
ACM
148
149 nd = self->curr_hot;
150 if (nd) {
151 newtFormAddHotKey(self->b.form, NEWT_KEY_TAB);
152 newtFormAddHotKey(self->b.form, NEWT_KEY_UNTAB);
153 }
154
155 while (1) {
b50e003d 156 key = ui_browser__run(&self->b);
f1e9214c 157
b50e003d 158 switch (key) {
f1e9214c
ACM
159 case NEWT_KEY_TAB:
160 nd = rb_prev(nd);
161 if (nd == NULL)
162 nd = rb_last(&self->entries);
163 annotate_browser__set_top(self, nd);
164 break;
165 case NEWT_KEY_UNTAB:
166 nd = rb_next(nd);
167 if (nd == NULL)
168 nd = rb_first(&self->entries);
169 annotate_browser__set_top(self, nd);
170 break;
171 default:
172 goto out;
173 }
174 }
175out:
59e8fe32 176 ui_browser__hide(&self->b);
b50e003d 177 return key;
f1e9214c
ACM
178}
179
211ef127
ACM
180int hist_entry__tui_annotate(struct hist_entry *self)
181{
211ef127 182 struct objdump_line *pos, *n;
92221162 183 struct objdump_line_rb_node *rbpos;
211ef127 184 LIST_HEAD(head);
92221162
ACM
185 struct annotate_browser browser = {
186 .b = {
187 .entries = &head,
188 .refresh = ui_browser__list_head_refresh,
189 .seek = ui_browser__list_head_seek,
190 .write = annotate_browser__write,
191 .priv = self,
192 },
211ef127
ACM
193 };
194 int ret;
195
196 if (self->ms.sym == NULL)
197 return -1;
198
199 if (self->ms.map->dso->annotate_warned)
200 return -1;
201
92221162 202 if (hist_entry__annotate(self, &head, sizeof(*rbpos)) < 0) {
1e6dd077 203 ui__error_window(ui_helpline__last_msg);
211ef127
ACM
204 return -1;
205 }
206
207 ui_helpline__push("Press <- or ESC to exit");
208
209 list_for_each_entry(pos, &head, node) {
210 size_t line_len = strlen(pos->line);
92221162
ACM
211 if (browser.b.width < line_len)
212 browser.b.width = line_len;
213 rbpos = objdump_line__rb(pos);
214 rbpos->idx = browser.b.nr_entries++;
215 rbpos->percent = objdump_line__calc_percent(pos, &head, self->ms.sym);
216 if (rbpos->percent < 0.01)
217 continue;
218 objdump__insert_line(&browser.entries, rbpos);
219 }
220
221 /*
222 * Position the browser at the hottest line.
223 */
f1e9214c
ACM
224 browser.curr_hot = rb_last(&browser.entries);
225 if (browser.curr_hot)
226 annotate_browser__set_top(&browser, browser.curr_hot);
211ef127 227
92221162 228 browser.b.width += 18; /* Percentage */
b50e003d 229 ret = annotate_browser__run(&browser);
211ef127
ACM
230 list_for_each_entry_safe(pos, n, &head, node) {
231 list_del(&pos->node);
232 objdump_line__free(pos);
233 }
211ef127
ACM
234 return ret;
235}
This page took 0.037544 seconds and 5 git commands to generate.