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