]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - tools/perf/util/ui/browsers/annotate.c
perf annotate: Support multiple histograms in annotation
[mirror_ubuntu-zesty-kernel.git] / tools / perf / util / ui / browsers / annotate.c
1 #include "../browser.h"
2 #include "../helpline.h"
3 #include "../libslang.h"
4 #include "../../annotate.h"
5 #include "../../hist.h"
6 #include "../../sort.h"
7 #include "../../symbol.h"
8 #include "../../annotate.h"
9
10 static 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
19 struct annotate_browser {
20 struct ui_browser b;
21 struct rb_root entries;
22 struct rb_node *curr_hot;
23 };
24
25 struct objdump_line_rb_node {
26 struct rb_node rb_node;
27 double percent;
28 u32 idx;
29 };
30
31 static inline
32 struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self)
33 {
34 return (struct objdump_line_rb_node *)(self + 1);
35 }
36
37 static 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) {
44 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
45 ui_browser__set_percent_color(self, olrb->percent, current_entry);
46 slsmg_printf(" %7.2f ", olrb->percent);
47 if (!current_entry)
48 ui_browser__set_color(self, HE_COLORSET_CODE);
49 } else {
50 ui_browser__set_percent_color(self, 0, current_entry);
51 slsmg_write_nstring(" ", 9);
52 }
53
54 SLsmg_write_char(':');
55 slsmg_write_nstring(" ", 8);
56 if (!*ol->line)
57 slsmg_write_nstring(" ", width - 18);
58 else
59 slsmg_write_nstring(ol->line, width - 18);
60 }
61
62 static double objdump_line__calc_percent(struct objdump_line *self,
63 struct list_head *head,
64 struct symbol *sym, int evidx)
65 {
66 double percent = 0.0;
67
68 if (self->offset != -1) {
69 int len = sym->end - sym->start;
70 unsigned int hits = 0;
71 struct annotation *notes = symbol__annotation(sym);
72 struct source_line *src_line = notes->src_line;
73 struct sym_hist *h = annotation__histogram(notes, evidx);
74 s64 offset = self->offset;
75 struct objdump_line *next = objdump__get_next_ip_line(head, self);
76
77 while (offset < (s64)len &&
78 (next == NULL || offset < next->offset)) {
79 if (src_line) {
80 percent += src_line[offset].percent;
81 } else
82 hits += h->addr[offset];
83
84 ++offset;
85 }
86 /*
87 * If the percentage wasn't already calculated in
88 * symbol__get_source_line, do it now:
89 */
90 if (src_line == NULL && h->sum)
91 percent = 100.0 * hits / h->sum;
92 }
93
94 return percent;
95 }
96
97 static void objdump__insert_line(struct rb_root *self,
98 struct objdump_line_rb_node *line)
99 {
100 struct rb_node **p = &self->rb_node;
101 struct rb_node *parent = NULL;
102 struct objdump_line_rb_node *l;
103
104 while (*p != NULL) {
105 parent = *p;
106 l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
107 if (line->percent < l->percent)
108 p = &(*p)->rb_left;
109 else
110 p = &(*p)->rb_right;
111 }
112 rb_link_node(&line->rb_node, parent, p);
113 rb_insert_color(&line->rb_node, self);
114 }
115
116 static void annotate_browser__set_top(struct annotate_browser *self,
117 struct rb_node *nd)
118 {
119 struct objdump_line_rb_node *rbpos;
120 struct objdump_line *pos;
121 unsigned back;
122
123 ui_browser__refresh_dimensions(&self->b);
124 back = self->b.height / 2;
125 rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
126 pos = ((struct objdump_line *)rbpos) - 1;
127 self->b.top_idx = self->b.index = rbpos->idx;
128
129 while (self->b.top_idx != 0 && back != 0) {
130 pos = list_entry(pos->node.prev, struct objdump_line, node);
131
132 --self->b.top_idx;
133 --back;
134 }
135
136 self->b.top = pos;
137 self->curr_hot = nd;
138 }
139
140 static int annotate_browser__run(struct annotate_browser *self)
141 {
142 struct rb_node *nd;
143 struct symbol *sym = self->b.priv;
144 int key;
145
146 if (ui_browser__show(&self->b, sym->name,
147 "<-, -> or ESC: exit, TAB/shift+TAB: cycle thru samples") < 0)
148 return -1;
149 /*
150 * To allow builtin-annotate to cycle thru multiple symbols by
151 * examining the exit key for this function.
152 */
153 ui_browser__add_exit_key(&self->b, NEWT_KEY_RIGHT);
154
155 nd = self->curr_hot;
156 if (nd) {
157 int tabs[] = { NEWT_KEY_TAB, NEWT_KEY_UNTAB, 0 };
158 ui_browser__add_exit_keys(&self->b, tabs);
159 }
160
161 while (1) {
162 key = ui_browser__run(&self->b);
163
164 switch (key) {
165 case NEWT_KEY_TAB:
166 nd = rb_prev(nd);
167 if (nd == NULL)
168 nd = rb_last(&self->entries);
169 annotate_browser__set_top(self, nd);
170 break;
171 case NEWT_KEY_UNTAB:
172 nd = rb_next(nd);
173 if (nd == NULL)
174 nd = rb_first(&self->entries);
175 annotate_browser__set_top(self, nd);
176 break;
177 default:
178 goto out;
179 }
180 }
181 out:
182 ui_browser__hide(&self->b);
183 return key;
184 }
185
186 int hist_entry__tui_annotate(struct hist_entry *he, int evidx)
187 {
188 return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx);
189 }
190
191 int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx)
192 {
193 struct objdump_line *pos, *n;
194 struct objdump_line_rb_node *rbpos;
195 LIST_HEAD(head);
196 struct annotate_browser browser = {
197 .b = {
198 .entries = &head,
199 .refresh = ui_browser__list_head_refresh,
200 .seek = ui_browser__list_head_seek,
201 .write = annotate_browser__write,
202 .priv = sym,
203 },
204 };
205 int ret;
206
207 if (sym == NULL)
208 return -1;
209
210 if (map->dso->annotate_warned)
211 return -1;
212
213 if (symbol__annotate(sym, map, &head, sizeof(*rbpos)) < 0) {
214 ui__error_window(ui_helpline__last_msg);
215 return -1;
216 }
217
218 ui_helpline__push("Press <- or ESC to exit");
219
220 list_for_each_entry(pos, &head, node) {
221 size_t line_len = strlen(pos->line);
222 if (browser.b.width < line_len)
223 browser.b.width = line_len;
224 rbpos = objdump_line__rb(pos);
225 rbpos->idx = browser.b.nr_entries++;
226 rbpos->percent = objdump_line__calc_percent(pos, &head, sym, evidx);
227 if (rbpos->percent < 0.01)
228 continue;
229 objdump__insert_line(&browser.entries, rbpos);
230 }
231
232 /*
233 * Position the browser at the hottest line.
234 */
235 browser.curr_hot = rb_last(&browser.entries);
236 if (browser.curr_hot)
237 annotate_browser__set_top(&browser, browser.curr_hot);
238
239 browser.b.width += 18; /* Percentage */
240 ret = annotate_browser__run(&browser);
241 list_for_each_entry_safe(pos, n, &head, node) {
242 list_del(&pos->node);
243 objdump_line__free(pos);
244 }
245 return ret;
246 }