blob: 82b78f99251bb2b764165cf8066a85f1e6e4b97d [file] [log] [blame]
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -03001#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
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030017struct annotate_browser {
18 struct ui_browser b;
19 struct rb_root entries;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -030020 struct rb_node *curr_hot;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030021};
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
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030035static 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) {
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030042 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030043 ui_browser__set_percent_color(self, olrb->percent, current_entry);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030044 slsmg_printf(" %7.2f ", olrb->percent);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030045 if (!current_entry)
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030046 ui_browser__set_color(self, HE_COLORSET_CODE);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030047 } else {
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -030048 ui_browser__set_percent_color(self, 0, current_entry);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -030049 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
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -030060static 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;
68 unsigned int hits = 0;
69 struct sym_priv *priv = symbol__priv(sym);
70 struct sym_ext *sym_ext = priv->ext;
71 struct sym_hist *h = priv->hist;
72 s64 offset = self->offset;
73 struct objdump_line *next = objdump__get_next_ip_line(head, self);
74
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;
88 }
89
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);
110}
111
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300112static 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
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300136static int annotate_browser__run(struct annotate_browser *self)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300137{
138 struct rb_node *nd;
139 struct hist_entry *he = self->b.priv;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300140 int key;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300141
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300142 if (ui_browser__show(&self->b, he->ms.sym->name,
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300143 "<-, -> or ESC: exit, TAB/shift+TAB: cycle thru samples") < 0)
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300144 return -1;
Arnaldo Carvalho de Melo4c1c9522010-08-12 12:37:51 -0300145 /*
146 * To allow builtin-annotate to cycle thru multiple symbols by
147 * examining the exit key for this function.
148 */
149 ui_browser__add_exit_key(&self->b, NEWT_KEY_RIGHT);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300150
151 nd = self->curr_hot;
152 if (nd) {
Arnaldo Carvalho de Melo4c1c9522010-08-12 12:37:51 -0300153 int tabs[] = { NEWT_KEY_TAB, NEWT_KEY_UNTAB, 0 };
154 ui_browser__add_exit_keys(&self->b, tabs);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300155 }
156
157 while (1) {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300158 key = ui_browser__run(&self->b);
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300159
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300160 switch (key) {
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300161 case NEWT_KEY_TAB:
162 nd = rb_prev(nd);
163 if (nd == NULL)
164 nd = rb_last(&self->entries);
165 annotate_browser__set_top(self, nd);
166 break;
167 case NEWT_KEY_UNTAB:
168 nd = rb_next(nd);
169 if (nd == NULL)
170 nd = rb_first(&self->entries);
171 annotate_browser__set_top(self, nd);
172 break;
173 default:
174 goto out;
175 }
176 }
177out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300178 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300179 return key;
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300180}
181
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300182int hist_entry__tui_annotate(struct hist_entry *self)
183{
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300184 struct objdump_line *pos, *n;
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300185 struct objdump_line_rb_node *rbpos;
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300186 LIST_HEAD(head);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300187 struct annotate_browser browser = {
188 .b = {
189 .entries = &head,
190 .refresh = ui_browser__list_head_refresh,
191 .seek = ui_browser__list_head_seek,
192 .write = annotate_browser__write,
193 .priv = self,
194 },
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300195 };
196 int ret;
197
198 if (self->ms.sym == NULL)
199 return -1;
200
201 if (self->ms.map->dso->annotate_warned)
202 return -1;
203
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300204 if (hist_entry__annotate(self, &head, sizeof(*rbpos)) < 0) {
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300205 ui__error_window(ui_helpline__last_msg);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300206 return -1;
207 }
208
209 ui_helpline__push("Press <- or ESC to exit");
210
211 list_for_each_entry(pos, &head, node) {
212 size_t line_len = strlen(pos->line);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300213 if (browser.b.width < line_len)
214 browser.b.width = line_len;
215 rbpos = objdump_line__rb(pos);
216 rbpos->idx = browser.b.nr_entries++;
217 rbpos->percent = objdump_line__calc_percent(pos, &head, self->ms.sym);
218 if (rbpos->percent < 0.01)
219 continue;
220 objdump__insert_line(&browser.entries, rbpos);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300221 }
222
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300223 /*
224 * Position the browser at the hottest line.
225 */
Arnaldo Carvalho de Melof1e92142010-08-10 15:14:53 -0300226 browser.curr_hot = rb_last(&browser.entries);
227 if (browser.curr_hot)
228 annotate_browser__set_top(&browser, browser.curr_hot);
Arnaldo Carvalho de Melo92221162010-08-09 15:30:40 -0300229
230 browser.b.width += 18; /* Percentage */
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300231 ret = annotate_browser__run(&browser);
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300232 list_for_each_entry_safe(pos, n, &head, node) {
233 list_del(&pos->node);
234 objdump_line__free(pos);
235 }
Arnaldo Carvalho de Melo211ef122010-08-10 14:54:09 -0300236 return ret;
237}