aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/ui
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/util/ui')
-rw-r--r--tools/perf/util/ui/browser.c76
-rw-r--r--tools/perf/util/ui/browser.h11
-rw-r--r--tools/perf/util/ui/browsers/annotate.c240
-rw-r--r--tools/perf/util/ui/browsers/hists.c948
-rw-r--r--tools/perf/util/ui/browsers/map.c161
-rw-r--r--tools/perf/util/ui/browsers/map.h6
-rw-r--r--tools/perf/util/ui/helpline.c69
-rw-r--r--tools/perf/util/ui/helpline.h11
-rw-r--r--tools/perf/util/ui/libslang.h27
-rw-r--r--tools/perf/util/ui/progress.c60
-rw-r--r--tools/perf/util/ui/progress.h11
-rw-r--r--tools/perf/util/ui/setup.c42
-rw-r--r--tools/perf/util/ui/util.c114
-rw-r--r--tools/perf/util/ui/util.h10
14 files changed, 1752 insertions, 34 deletions
diff --git a/tools/perf/util/ui/browser.c b/tools/perf/util/ui/browser.c
index 0b2b9306312d..66f2d583d8c4 100644
--- a/tools/perf/util/ui/browser.c
+++ b/tools/perf/util/ui/browser.c
@@ -16,6 +16,7 @@
16#include <stdlib.h> 16#include <stdlib.h>
17#include <sys/ttydefaults.h> 17#include <sys/ttydefaults.h>
18#include "browser.h" 18#include "browser.h"
19#include "helpline.h"
19#include "../color.h" 20#include "../color.h"
20#include "../util.h" 21#include "../util.h"
21 22
@@ -49,7 +50,7 @@ void ui_browser__list_head_seek(struct ui_browser *self, off_t offset, int whenc
49 pos = head->next; 50 pos = head->next;
50 break; 51 break;
51 case SEEK_CUR: 52 case SEEK_CUR:
52 pos = self->first_visible_entry; 53 pos = self->top;
53 break; 54 break;
54 case SEEK_END: 55 case SEEK_END:
55 pos = head->prev; 56 pos = head->prev;
@@ -66,7 +67,7 @@ void ui_browser__list_head_seek(struct ui_browser *self, off_t offset, int whenc
66 pos = pos->prev; 67 pos = pos->prev;
67 } 68 }
68 69
69 self->first_visible_entry = pos; 70 self->top = pos;
70} 71}
71 72
72void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence) 73void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence)
@@ -79,7 +80,7 @@ void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence)
79 nd = rb_first(root); 80 nd = rb_first(root);
80 break; 81 break;
81 case SEEK_CUR: 82 case SEEK_CUR:
82 nd = self->first_visible_entry; 83 nd = self->top;
83 break; 84 break;
84 case SEEK_END: 85 case SEEK_END:
85 nd = rb_last(root); 86 nd = rb_last(root);
@@ -96,7 +97,7 @@ void ui_browser__rb_tree_seek(struct ui_browser *self, off_t offset, int whence)
96 nd = rb_prev(nd); 97 nd = rb_prev(nd);
97 } 98 }
98 99
99 self->first_visible_entry = nd; 100 self->top = nd;
100} 101}
101 102
102unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self) 103unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self)
@@ -104,13 +105,13 @@ unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self)
104 struct rb_node *nd; 105 struct rb_node *nd;
105 int row = 0; 106 int row = 0;
106 107
107 if (self->first_visible_entry == NULL) 108 if (self->top == NULL)
108 self->first_visible_entry = rb_first(self->entries); 109 self->top = rb_first(self->entries);
109 110
110 nd = self->first_visible_entry; 111 nd = self->top;
111 112
112 while (nd != NULL) { 113 while (nd != NULL) {
113 SLsmg_gotorc(self->top + row, self->left); 114 SLsmg_gotorc(self->y + row, self->x);
114 self->write(self, nd, row); 115 self->write(self, nd, row);
115 if (++row == self->height) 116 if (++row == self->height)
116 break; 117 break;
@@ -122,7 +123,7 @@ unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self)
122 123
123bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row) 124bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row)
124{ 125{
125 return (self->first_visible_entry_idx + row) == self->index; 126 return self->top_idx + row == self->index;
126} 127}
127 128
128void ui_browser__refresh_dimensions(struct ui_browser *self) 129void ui_browser__refresh_dimensions(struct ui_browser *self)
@@ -135,18 +136,21 @@ void ui_browser__refresh_dimensions(struct ui_browser *self)
135 self->height = rows - 5; 136 self->height = rows - 5;
136 if (self->height > self->nr_entries) 137 if (self->height > self->nr_entries)
137 self->height = self->nr_entries; 138 self->height = self->nr_entries;
138 self->top = (rows - self->height) / 2; 139 self->y = (rows - self->height) / 2;
139 self->left = (cols - self->width) / 2; 140 self->x = (cols - self->width) / 2;
140} 141}
141 142
142void ui_browser__reset_index(struct ui_browser *self) 143void ui_browser__reset_index(struct ui_browser *self)
143{ 144{
144 self->index = self->first_visible_entry_idx = 0; 145 self->index = self->top_idx = 0;
145 self->seek(self, 0, SEEK_SET); 146 self->seek(self, 0, SEEK_SET);
146} 147}
147 148
148int ui_browser__show(struct ui_browser *self, const char *title) 149int ui_browser__show(struct ui_browser *self, const char *title,
150 const char *helpline, ...)
149{ 151{
152 va_list ap;
153
150 if (self->form != NULL) { 154 if (self->form != NULL) {
151 newtFormDestroy(self->form); 155 newtFormDestroy(self->form);
152 newtPopWindow(); 156 newtPopWindow();
@@ -169,10 +173,23 @@ int ui_browser__show(struct ui_browser *self, const char *title)
169 newtFormAddHotKey(self->form, NEWT_KEY_PGDN); 173 newtFormAddHotKey(self->form, NEWT_KEY_PGDN);
170 newtFormAddHotKey(self->form, NEWT_KEY_HOME); 174 newtFormAddHotKey(self->form, NEWT_KEY_HOME);
171 newtFormAddHotKey(self->form, NEWT_KEY_END); 175 newtFormAddHotKey(self->form, NEWT_KEY_END);
176 newtFormAddHotKey(self->form, ' ');
172 newtFormAddComponent(self->form, self->sb); 177 newtFormAddComponent(self->form, self->sb);
178
179 va_start(ap, helpline);
180 ui_helpline__vpush(helpline, ap);
181 va_end(ap);
173 return 0; 182 return 0;
174} 183}
175 184
185void ui_browser__hide(struct ui_browser *self)
186{
187 newtFormDestroy(self->form);
188 newtPopWindow();
189 self->form = NULL;
190 ui_helpline__pop();
191}
192
176int ui_browser__refresh(struct ui_browser *self) 193int ui_browser__refresh(struct ui_browser *self)
177{ 194{
178 int row; 195 int row;
@@ -180,7 +197,7 @@ int ui_browser__refresh(struct ui_browser *self)
180 newtScrollbarSet(self->sb, self->index, self->nr_entries - 1); 197 newtScrollbarSet(self->sb, self->index, self->nr_entries - 1);
181 row = self->refresh(self); 198 row = self->refresh(self);
182 SLsmg_set_color(HE_COLORSET_NORMAL); 199 SLsmg_set_color(HE_COLORSET_NORMAL);
183 SLsmg_fill_region(self->top + row, self->left, 200 SLsmg_fill_region(self->y + row, self->x,
184 self->height - row, self->width, ' '); 201 self->height - row, self->width, ' ');
185 202
186 return 0; 203 return 0;
@@ -205,8 +222,8 @@ int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
205 if (self->index == self->nr_entries - 1) 222 if (self->index == self->nr_entries - 1)
206 break; 223 break;
207 ++self->index; 224 ++self->index;
208 if (self->index == self->first_visible_entry_idx + self->height) { 225 if (self->index == self->top_idx + self->height) {
209 ++self->first_visible_entry_idx; 226 ++self->top_idx;
210 self->seek(self, +1, SEEK_CUR); 227 self->seek(self, +1, SEEK_CUR);
211 } 228 }
212 break; 229 break;
@@ -214,34 +231,34 @@ int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
214 if (self->index == 0) 231 if (self->index == 0)
215 break; 232 break;
216 --self->index; 233 --self->index;
217 if (self->index < self->first_visible_entry_idx) { 234 if (self->index < self->top_idx) {
218 --self->first_visible_entry_idx; 235 --self->top_idx;
219 self->seek(self, -1, SEEK_CUR); 236 self->seek(self, -1, SEEK_CUR);
220 } 237 }
221 break; 238 break;
222 case NEWT_KEY_PGDN: 239 case NEWT_KEY_PGDN:
223 case ' ': 240 case ' ':
224 if (self->first_visible_entry_idx + self->height > self->nr_entries - 1) 241 if (self->top_idx + self->height > self->nr_entries - 1)
225 break; 242 break;
226 243
227 offset = self->height; 244 offset = self->height;
228 if (self->index + offset > self->nr_entries - 1) 245 if (self->index + offset > self->nr_entries - 1)
229 offset = self->nr_entries - 1 - self->index; 246 offset = self->nr_entries - 1 - self->index;
230 self->index += offset; 247 self->index += offset;
231 self->first_visible_entry_idx += offset; 248 self->top_idx += offset;
232 self->seek(self, +offset, SEEK_CUR); 249 self->seek(self, +offset, SEEK_CUR);
233 break; 250 break;
234 case NEWT_KEY_PGUP: 251 case NEWT_KEY_PGUP:
235 if (self->first_visible_entry_idx == 0) 252 if (self->top_idx == 0)
236 break; 253 break;
237 254
238 if (self->first_visible_entry_idx < self->height) 255 if (self->top_idx < self->height)
239 offset = self->first_visible_entry_idx; 256 offset = self->top_idx;
240 else 257 else
241 offset = self->height; 258 offset = self->height;
242 259
243 self->index -= offset; 260 self->index -= offset;
244 self->first_visible_entry_idx -= offset; 261 self->top_idx -= offset;
245 self->seek(self, -offset, SEEK_CUR); 262 self->seek(self, -offset, SEEK_CUR);
246 break; 263 break;
247 case NEWT_KEY_HOME: 264 case NEWT_KEY_HOME:
@@ -253,7 +270,7 @@ int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es)
253 offset = self->nr_entries - 1; 270 offset = self->nr_entries - 1;
254 271
255 self->index = self->nr_entries - 1; 272 self->index = self->nr_entries - 1;
256 self->first_visible_entry_idx = self->index - offset; 273 self->top_idx = self->index - offset;
257 self->seek(self, -offset, SEEK_END); 274 self->seek(self, -offset, SEEK_END);
258 break; 275 break;
259 default: 276 default:
@@ -271,14 +288,13 @@ unsigned int ui_browser__list_head_refresh(struct ui_browser *self)
271 struct list_head *head = self->entries; 288 struct list_head *head = self->entries;
272 int row = 0; 289 int row = 0;
273 290
274 if (self->first_visible_entry == NULL || 291 if (self->top == NULL || self->top == self->entries)
275 self->first_visible_entry == self->entries) 292 self->top = head->next;
276 self->first_visible_entry = head->next;
277 293
278 pos = self->first_visible_entry; 294 pos = self->top;
279 295
280 list_for_each_from(pos, head) { 296 list_for_each_from(pos, head) {
281 SLsmg_gotorc(self->top + row, self->left); 297 SLsmg_gotorc(self->y + row, self->x);
282 self->write(self, pos, row); 298 self->write(self, pos, row);
283 if (++row == self->height) 299 if (++row == self->height)
284 break; 300 break;
diff --git a/tools/perf/util/ui/browser.h b/tools/perf/util/ui/browser.h
index bcc4391405bd..0b9f829214f7 100644
--- a/tools/perf/util/ui/browser.h
+++ b/tools/perf/util/ui/browser.h
@@ -3,6 +3,7 @@
3 3
4#include <stdbool.h> 4#include <stdbool.h>
5#include <newt.h> 5#include <newt.h>
6#include <sys/types.h>
6#include "../types.h" 7#include "../types.h"
7 8
8#define HE_COLORSET_TOP 50 9#define HE_COLORSET_TOP 50
@@ -13,9 +14,9 @@
13 14
14struct ui_browser { 15struct ui_browser {
15 newtComponent form, sb; 16 newtComponent form, sb;
16 u64 index, first_visible_entry_idx; 17 u64 index, top_idx;
17 void *first_visible_entry, *entries; 18 void *top, *entries;
18 u16 top, left, width, height; 19 u16 y, x, width, height;
19 void *priv; 20 void *priv;
20 unsigned int (*refresh)(struct ui_browser *self); 21 unsigned int (*refresh)(struct ui_browser *self);
21 void (*write)(struct ui_browser *self, void *entry, int row); 22 void (*write)(struct ui_browser *self, void *entry, int row);
@@ -29,7 +30,9 @@ bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row);
29void ui_browser__refresh_dimensions(struct ui_browser *self); 30void ui_browser__refresh_dimensions(struct ui_browser *self);
30void ui_browser__reset_index(struct ui_browser *self); 31void ui_browser__reset_index(struct ui_browser *self);
31 32
32int ui_browser__show(struct ui_browser *self, const char *title); 33int ui_browser__show(struct ui_browser *self, const char *title,
34 const char *helpline, ...);
35void ui_browser__hide(struct ui_browser *self);
33int ui_browser__refresh(struct ui_browser *self); 36int ui_browser__refresh(struct ui_browser *self);
34int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es); 37int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es);
35 38
diff --git a/tools/perf/util/ui/browsers/annotate.c b/tools/perf/util/ui/browsers/annotate.c
new file mode 100644
index 000000000000..55ff792459ac
--- /dev/null
+++ b/tools/perf/util/ui/browsers/annotate.c
@@ -0,0 +1,240 @@
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
17struct annotate_browser {
18 struct ui_browser b;
19 struct rb_root entries;
20 struct rb_node *curr_hot;
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
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) {
42 struct objdump_line_rb_node *olrb = objdump_line__rb(ol);
43 int color = ui_browser__percent_color(olrb->percent, current_entry);
44 SLsmg_set_color(color);
45 slsmg_printf(" %7.2f ", olrb->percent);
46 if (!current_entry)
47 SLsmg_set_color(HE_COLORSET_CODE);
48 } else {
49 int color = ui_browser__percent_color(0, current_entry);
50 SLsmg_set_color(color);
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
62static double objdump_line__calc_percent(struct objdump_line *self,
63 struct list_head *head,
64 struct symbol *sym)
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 sym_priv *priv = symbol__priv(sym);
72 struct sym_ext *sym_ext = priv->ext;
73 struct sym_hist *h = priv->hist;
74 s64 offset = self->offset;
75 struct objdump_line *next = objdump__get_next_ip_line(head, self);
76
77
78 while (offset < (s64)len &&
79 (next == NULL || offset < next->offset)) {
80 if (sym_ext) {
81 percent += sym_ext[offset].percent;
82 } else
83 hits += h->ip[offset];
84
85 ++offset;
86 }
87
88 if (sym_ext == NULL && h->sum)
89 percent = 100.0 * hits / h->sum;
90 }
91
92 return percent;
93}
94
95static void objdump__insert_line(struct rb_root *self,
96 struct objdump_line_rb_node *line)
97{
98 struct rb_node **p = &self->rb_node;
99 struct rb_node *parent = NULL;
100 struct objdump_line_rb_node *l;
101
102 while (*p != NULL) {
103 parent = *p;
104 l = rb_entry(parent, struct objdump_line_rb_node, rb_node);
105 if (line->percent < l->percent)
106 p = &(*p)->rb_left;
107 else
108 p = &(*p)->rb_right;
109 }
110 rb_link_node(&line->rb_node, parent, p);
111 rb_insert_color(&line->rb_node, self);
112}
113
114static void annotate_browser__set_top(struct annotate_browser *self,
115 struct rb_node *nd)
116{
117 struct objdump_line_rb_node *rbpos;
118 struct objdump_line *pos;
119 unsigned back;
120
121 ui_browser__refresh_dimensions(&self->b);
122 back = self->b.height / 2;
123 rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node);
124 pos = ((struct objdump_line *)rbpos) - 1;
125 self->b.top_idx = self->b.index = rbpos->idx;
126
127 while (self->b.top_idx != 0 && back != 0) {
128 pos = list_entry(pos->node.prev, struct objdump_line, node);
129
130 --self->b.top_idx;
131 --back;
132 }
133
134 self->b.top = pos;
135 self->curr_hot = nd;
136}
137
138static int annotate_browser__run(struct annotate_browser *self,
139 struct newtExitStruct *es)
140{
141 struct rb_node *nd;
142 struct hist_entry *he = self->b.priv;
143
144 if (ui_browser__show(&self->b, he->ms.sym->name,
145 "<- or ESC: exit, TAB/shift+TAB: cycle thru samples") < 0)
146 return -1;
147
148 newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT);
149
150 nd = self->curr_hot;
151 if (nd) {
152 newtFormAddHotKey(self->b.form, NEWT_KEY_TAB);
153 newtFormAddHotKey(self->b.form, NEWT_KEY_UNTAB);
154 }
155
156 while (1) {
157 ui_browser__run(&self->b, es);
158
159 if (es->reason != NEWT_EXIT_HOTKEY)
160 break;
161
162 switch (es->u.key) {
163 case NEWT_KEY_TAB:
164 nd = rb_prev(nd);
165 if (nd == NULL)
166 nd = rb_last(&self->entries);
167 annotate_browser__set_top(self, nd);
168 break;
169 case NEWT_KEY_UNTAB:
170 nd = rb_next(nd);
171 if (nd == NULL)
172 nd = rb_first(&self->entries);
173 annotate_browser__set_top(self, nd);
174 break;
175 default:
176 goto out;
177 }
178 }
179out:
180 ui_browser__hide(&self->b);
181 return 0;
182}
183
184int hist_entry__tui_annotate(struct hist_entry *self)
185{
186 struct newtExitStruct es;
187 struct objdump_line *pos, *n;
188 struct objdump_line_rb_node *rbpos;
189 LIST_HEAD(head);
190 struct annotate_browser browser = {
191 .b = {
192 .entries = &head,
193 .refresh = ui_browser__list_head_refresh,
194 .seek = ui_browser__list_head_seek,
195 .write = annotate_browser__write,
196 .priv = self,
197 },
198 };
199 int ret;
200
201 if (self->ms.sym == NULL)
202 return -1;
203
204 if (self->ms.map->dso->annotate_warned)
205 return -1;
206
207 if (hist_entry__annotate(self, &head, sizeof(*rbpos)) < 0) {
208 ui__error_window(ui_helpline__last_msg);
209 return -1;
210 }
211
212 ui_helpline__push("Press <- or ESC to exit");
213
214 list_for_each_entry(pos, &head, node) {
215 size_t line_len = strlen(pos->line);
216 if (browser.b.width < line_len)
217 browser.b.width = line_len;
218 rbpos = objdump_line__rb(pos);
219 rbpos->idx = browser.b.nr_entries++;
220 rbpos->percent = objdump_line__calc_percent(pos, &head, self->ms.sym);
221 if (rbpos->percent < 0.01)
222 continue;
223 objdump__insert_line(&browser.entries, rbpos);
224 }
225
226 /*
227 * Position the browser at the hottest line.
228 */
229 browser.curr_hot = rb_last(&browser.entries);
230 if (browser.curr_hot)
231 annotate_browser__set_top(&browser, browser.curr_hot);
232
233 browser.b.width += 18; /* Percentage */
234 ret = annotate_browser__run(&browser, &es);
235 list_for_each_entry_safe(pos, n, &head, node) {
236 list_del(&pos->node);
237 objdump_line__free(pos);
238 }
239 return ret;
240}
diff --git a/tools/perf/util/ui/browsers/hists.c b/tools/perf/util/ui/browsers/hists.c
new file mode 100644
index 000000000000..dafdf6775d77
--- /dev/null
+++ b/tools/perf/util/ui/browsers/hists.c
@@ -0,0 +1,948 @@
1#define _GNU_SOURCE
2#include <stdio.h>
3#undef _GNU_SOURCE
4#include "../libslang.h"
5#include <stdlib.h>
6#include <string.h>
7#include <newt.h>
8#include <linux/rbtree.h>
9
10#include "../../hist.h"
11#include "../../pstack.h"
12#include "../../sort.h"
13#include "../../util.h"
14
15#include "../browser.h"
16#include "../helpline.h"
17#include "../util.h"
18#include "map.h"
19
20struct hist_browser {
21 struct ui_browser b;
22 struct hists *hists;
23 struct hist_entry *he_selection;
24 struct map_symbol *selection;
25};
26
27static void hist_browser__refresh_dimensions(struct hist_browser *self)
28{
29 /* 3 == +/- toggle symbol before actual hist_entry rendering */
30 self->b.width = 3 + (hists__sort_list_width(self->hists) +
31 sizeof("[k]"));
32}
33
34static void hist_browser__reset(struct hist_browser *self)
35{
36 self->b.nr_entries = self->hists->nr_entries;
37 hist_browser__refresh_dimensions(self);
38 ui_browser__reset_index(&self->b);
39}
40
41static char tree__folded_sign(bool unfolded)
42{
43 return unfolded ? '-' : '+';
44}
45
46static char map_symbol__folded(const struct map_symbol *self)
47{
48 return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
49}
50
51static char hist_entry__folded(const struct hist_entry *self)
52{
53 return map_symbol__folded(&self->ms);
54}
55
56static char callchain_list__folded(const struct callchain_list *self)
57{
58 return map_symbol__folded(&self->ms);
59}
60
61static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
62{
63 int n = 0;
64 struct rb_node *nd;
65
66 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
67 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
68 struct callchain_list *chain;
69 char folded_sign = ' '; /* No children */
70
71 list_for_each_entry(chain, &child->val, list) {
72 ++n;
73 /* We need this because we may not have children */
74 folded_sign = callchain_list__folded(chain);
75 if (folded_sign == '+')
76 break;
77 }
78
79 if (folded_sign == '-') /* Have children and they're unfolded */
80 n += callchain_node__count_rows_rb_tree(child);
81 }
82
83 return n;
84}
85
86static int callchain_node__count_rows(struct callchain_node *node)
87{
88 struct callchain_list *chain;
89 bool unfolded = false;
90 int n = 0;
91
92 list_for_each_entry(chain, &node->val, list) {
93 ++n;
94 unfolded = chain->ms.unfolded;
95 }
96
97 if (unfolded)
98 n += callchain_node__count_rows_rb_tree(node);
99
100 return n;
101}
102
103static int callchain__count_rows(struct rb_root *chain)
104{
105 struct rb_node *nd;
106 int n = 0;
107
108 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
109 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
110 n += callchain_node__count_rows(node);
111 }
112
113 return n;
114}
115
116static bool map_symbol__toggle_fold(struct map_symbol *self)
117{
118 if (!self->has_children)
119 return false;
120
121 self->unfolded = !self->unfolded;
122 return true;
123}
124
125static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
126{
127 struct rb_node *nd = rb_first(&self->rb_root);
128
129 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
130 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
131 struct callchain_list *chain;
132 int first = true;
133
134 list_for_each_entry(chain, &child->val, list) {
135 if (first) {
136 first = false;
137 chain->ms.has_children = chain->list.next != &child->val ||
138 rb_first(&child->rb_root) != NULL;
139 } else
140 chain->ms.has_children = chain->list.next == &child->val &&
141 rb_first(&child->rb_root) != NULL;
142 }
143
144 callchain_node__init_have_children_rb_tree(child);
145 }
146}
147
148static void callchain_node__init_have_children(struct callchain_node *self)
149{
150 struct callchain_list *chain;
151
152 list_for_each_entry(chain, &self->val, list)
153 chain->ms.has_children = rb_first(&self->rb_root) != NULL;
154
155 callchain_node__init_have_children_rb_tree(self);
156}
157
158static void callchain__init_have_children(struct rb_root *self)
159{
160 struct rb_node *nd;
161
162 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
163 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
164 callchain_node__init_have_children(node);
165 }
166}
167
168static void hist_entry__init_have_children(struct hist_entry *self)
169{
170 if (!self->init_have_children) {
171 callchain__init_have_children(&self->sorted_chain);
172 self->init_have_children = true;
173 }
174}
175
176static bool hist_browser__toggle_fold(struct hist_browser *self)
177{
178 if (map_symbol__toggle_fold(self->selection)) {
179 struct hist_entry *he = self->he_selection;
180
181 hist_entry__init_have_children(he);
182 self->hists->nr_entries -= he->nr_rows;
183
184 if (he->ms.unfolded)
185 he->nr_rows = callchain__count_rows(&he->sorted_chain);
186 else
187 he->nr_rows = 0;
188 self->hists->nr_entries += he->nr_rows;
189 self->b.nr_entries = self->hists->nr_entries;
190
191 return true;
192 }
193
194 /* If it doesn't have children, no toggling performed */
195 return false;
196}
197
198static int hist_browser__run(struct hist_browser *self, const char *title,
199 struct newtExitStruct *es)
200{
201 char str[256], unit;
202 unsigned long nr_events = self->hists->stats.nr_events[PERF_RECORD_SAMPLE];
203
204 self->b.entries = &self->hists->entries;
205 self->b.nr_entries = self->hists->nr_entries;
206
207 hist_browser__refresh_dimensions(self);
208
209 nr_events = convert_unit(nr_events, &unit);
210 snprintf(str, sizeof(str), "Events: %lu%c ",
211 nr_events, unit);
212 newtDrawRootText(0, 0, str);
213
214 if (ui_browser__show(&self->b, title,
215 "Press '?' for help on key bindings") < 0)
216 return -1;
217
218 newtFormAddHotKey(self->b.form, 'a');
219 newtFormAddHotKey(self->b.form, '?');
220 newtFormAddHotKey(self->b.form, 'h');
221 newtFormAddHotKey(self->b.form, 'd');
222 newtFormAddHotKey(self->b.form, 'D');
223 newtFormAddHotKey(self->b.form, 't');
224
225 newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT);
226 newtFormAddHotKey(self->b.form, NEWT_KEY_RIGHT);
227 newtFormAddHotKey(self->b.form, NEWT_KEY_ENTER);
228
229 while (1) {
230 ui_browser__run(&self->b, es);
231
232 if (es->reason != NEWT_EXIT_HOTKEY)
233 break;
234 switch (es->u.key) {
235 case 'D': { /* Debug */
236 static int seq;
237 struct hist_entry *h = rb_entry(self->b.top,
238 struct hist_entry, rb_node);
239 ui_helpline__pop();
240 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
241 seq++, self->b.nr_entries,
242 self->hists->nr_entries,
243 self->b.height,
244 self->b.index,
245 self->b.top_idx,
246 h->row_offset, h->nr_rows);
247 }
248 continue;
249 case NEWT_KEY_ENTER:
250 if (hist_browser__toggle_fold(self))
251 break;
252 /* fall thru */
253 default:
254 return 0;
255 }
256 }
257
258 ui_browser__hide(&self->b);
259 return 0;
260}
261
262static char *callchain_list__sym_name(struct callchain_list *self,
263 char *bf, size_t bfsize)
264{
265 if (self->ms.sym)
266 return self->ms.sym->name;
267
268 snprintf(bf, bfsize, "%#Lx", self->ip);
269 return bf;
270}
271
272#define LEVEL_OFFSET_STEP 3
273
274static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
275 struct callchain_node *chain_node,
276 u64 total, int level,
277 unsigned short row,
278 off_t *row_offset,
279 bool *is_current_entry)
280{
281 struct rb_node *node;
282 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
283 u64 new_total, remaining;
284
285 if (callchain_param.mode == CHAIN_GRAPH_REL)
286 new_total = chain_node->children_hit;
287 else
288 new_total = total;
289
290 remaining = new_total;
291 node = rb_first(&chain_node->rb_root);
292 while (node) {
293 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
294 struct rb_node *next = rb_next(node);
295 u64 cumul = cumul_hits(child);
296 struct callchain_list *chain;
297 char folded_sign = ' ';
298 int first = true;
299 int extra_offset = 0;
300
301 remaining -= cumul;
302
303 list_for_each_entry(chain, &child->val, list) {
304 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
305 const char *str;
306 int color;
307 bool was_first = first;
308
309 if (first) {
310 first = false;
311 chain->ms.has_children = chain->list.next != &child->val ||
312 rb_first(&child->rb_root) != NULL;
313 } else {
314 extra_offset = LEVEL_OFFSET_STEP;
315 chain->ms.has_children = chain->list.next == &child->val &&
316 rb_first(&child->rb_root) != NULL;
317 }
318
319 folded_sign = callchain_list__folded(chain);
320 if (*row_offset != 0) {
321 --*row_offset;
322 goto do_next;
323 }
324
325 alloc_str = NULL;
326 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
327 if (was_first) {
328 double percent = cumul * 100.0 / new_total;
329
330 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
331 str = "Not enough memory!";
332 else
333 str = alloc_str;
334 }
335
336 color = HE_COLORSET_NORMAL;
337 width = self->b.width - (offset + extra_offset + 2);
338 if (ui_browser__is_current_entry(&self->b, row)) {
339 self->selection = &chain->ms;
340 color = HE_COLORSET_SELECTED;
341 *is_current_entry = true;
342 }
343
344 SLsmg_set_color(color);
345 SLsmg_gotorc(self->b.y + row, self->b.x);
346 slsmg_write_nstring(" ", offset + extra_offset);
347 slsmg_printf("%c ", folded_sign);
348 slsmg_write_nstring(str, width);
349 free(alloc_str);
350
351 if (++row == self->b.height)
352 goto out;
353do_next:
354 if (folded_sign == '+')
355 break;
356 }
357
358 if (folded_sign == '-') {
359 const int new_level = level + (extra_offset ? 2 : 1);
360 row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
361 new_level, row, row_offset,
362 is_current_entry);
363 }
364 if (row == self->b.height)
365 goto out;
366 node = next;
367 }
368out:
369 return row - first_row;
370}
371
372static int hist_browser__show_callchain_node(struct hist_browser *self,
373 struct callchain_node *node,
374 int level, unsigned short row,
375 off_t *row_offset,
376 bool *is_current_entry)
377{
378 struct callchain_list *chain;
379 int first_row = row,
380 offset = level * LEVEL_OFFSET_STEP,
381 width = self->b.width - offset;
382 char folded_sign = ' ';
383
384 list_for_each_entry(chain, &node->val, list) {
385 char ipstr[BITS_PER_LONG / 4 + 1], *s;
386 int color;
387 /*
388 * FIXME: This should be moved to somewhere else,
389 * probably when the callchain is created, so as not to
390 * traverse it all over again
391 */
392 chain->ms.has_children = rb_first(&node->rb_root) != NULL;
393 folded_sign = callchain_list__folded(chain);
394
395 if (*row_offset != 0) {
396 --*row_offset;
397 continue;
398 }
399
400 color = HE_COLORSET_NORMAL;
401 if (ui_browser__is_current_entry(&self->b, row)) {
402 self->selection = &chain->ms;
403 color = HE_COLORSET_SELECTED;
404 *is_current_entry = true;
405 }
406
407 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
408 SLsmg_gotorc(self->b.y + row, self->b.x);
409 SLsmg_set_color(color);
410 slsmg_write_nstring(" ", offset);
411 slsmg_printf("%c ", folded_sign);
412 slsmg_write_nstring(s, width - 2);
413
414 if (++row == self->b.height)
415 goto out;
416 }
417
418 if (folded_sign == '-')
419 row += hist_browser__show_callchain_node_rb_tree(self, node,
420 self->hists->stats.total_period,
421 level + 1, row,
422 row_offset,
423 is_current_entry);
424out:
425 return row - first_row;
426}
427
428static int hist_browser__show_callchain(struct hist_browser *self,
429 struct rb_root *chain,
430 int level, unsigned short row,
431 off_t *row_offset,
432 bool *is_current_entry)
433{
434 struct rb_node *nd;
435 int first_row = row;
436
437 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
438 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
439
440 row += hist_browser__show_callchain_node(self, node, level,
441 row, row_offset,
442 is_current_entry);
443 if (row == self->b.height)
444 break;
445 }
446
447 return row - first_row;
448}
449
450static int hist_browser__show_entry(struct hist_browser *self,
451 struct hist_entry *entry,
452 unsigned short row)
453{
454 char s[256];
455 double percent;
456 int printed = 0;
457 int color, width = self->b.width;
458 char folded_sign = ' ';
459 bool current_entry = ui_browser__is_current_entry(&self->b, row);
460 off_t row_offset = entry->row_offset;
461
462 if (current_entry) {
463 self->he_selection = entry;
464 self->selection = &entry->ms;
465 }
466
467 if (symbol_conf.use_callchain) {
468 entry->ms.has_children = !RB_EMPTY_ROOT(&entry->sorted_chain);
469 folded_sign = hist_entry__folded(entry);
470 }
471
472 if (row_offset == 0) {
473 hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false,
474 0, false, self->hists->stats.total_period);
475 percent = (entry->period * 100.0) / self->hists->stats.total_period;
476
477 color = HE_COLORSET_SELECTED;
478 if (!current_entry) {
479 if (percent >= MIN_RED)
480 color = HE_COLORSET_TOP;
481 else if (percent >= MIN_GREEN)
482 color = HE_COLORSET_MEDIUM;
483 else
484 color = HE_COLORSET_NORMAL;
485 }
486
487 SLsmg_set_color(color);
488 SLsmg_gotorc(self->b.y + row, self->b.x);
489 if (symbol_conf.use_callchain) {
490 slsmg_printf("%c ", folded_sign);
491 width -= 2;
492 }
493 slsmg_write_nstring(s, width);
494 ++row;
495 ++printed;
496 } else
497 --row_offset;
498
499 if (folded_sign == '-' && row != self->b.height) {
500 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
501 1, row, &row_offset,
502 &current_entry);
503 if (current_entry)
504 self->he_selection = entry;
505 }
506
507 return printed;
508}
509
510static unsigned int hist_browser__refresh(struct ui_browser *self)
511{
512 unsigned row = 0;
513 struct rb_node *nd;
514 struct hist_browser *hb = container_of(self, struct hist_browser, b);
515
516 if (self->top == NULL)
517 self->top = rb_first(&hb->hists->entries);
518
519 for (nd = self->top; nd; nd = rb_next(nd)) {
520 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
521
522 if (h->filtered)
523 continue;
524
525 row += hist_browser__show_entry(hb, h, row);
526 if (row == self->height)
527 break;
528 }
529
530 return row;
531}
532
533static struct rb_node *hists__filter_entries(struct rb_node *nd)
534{
535 while (nd != NULL) {
536 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
537 if (!h->filtered)
538 return nd;
539
540 nd = rb_next(nd);
541 }
542
543 return NULL;
544}
545
546static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
547{
548 while (nd != NULL) {
549 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
550 if (!h->filtered)
551 return nd;
552
553 nd = rb_prev(nd);
554 }
555
556 return NULL;
557}
558
559static void ui_browser__hists_seek(struct ui_browser *self,
560 off_t offset, int whence)
561{
562 struct hist_entry *h;
563 struct rb_node *nd;
564 bool first = true;
565
566 switch (whence) {
567 case SEEK_SET:
568 nd = hists__filter_entries(rb_first(self->entries));
569 break;
570 case SEEK_CUR:
571 nd = self->top;
572 goto do_offset;
573 case SEEK_END:
574 nd = hists__filter_prev_entries(rb_last(self->entries));
575 first = false;
576 break;
577 default:
578 return;
579 }
580
581 /*
582 * Moves not relative to the first visible entry invalidates its
583 * row_offset:
584 */
585 h = rb_entry(self->top, struct hist_entry, rb_node);
586 h->row_offset = 0;
587
588 /*
589 * Here we have to check if nd is expanded (+), if it is we can't go
590 * the next top level hist_entry, instead we must compute an offset of
591 * what _not_ to show and not change the first visible entry.
592 *
593 * This offset increments when we are going from top to bottom and
594 * decreases when we're going from bottom to top.
595 *
596 * As we don't have backpointers to the top level in the callchains
597 * structure, we need to always print the whole hist_entry callchain,
598 * skipping the first ones that are before the first visible entry
599 * and stop when we printed enough lines to fill the screen.
600 */
601do_offset:
602 if (offset > 0) {
603 do {
604 h = rb_entry(nd, struct hist_entry, rb_node);
605 if (h->ms.unfolded) {
606 u16 remaining = h->nr_rows - h->row_offset;
607 if (offset > remaining) {
608 offset -= remaining;
609 h->row_offset = 0;
610 } else {
611 h->row_offset += offset;
612 offset = 0;
613 self->top = nd;
614 break;
615 }
616 }
617 nd = hists__filter_entries(rb_next(nd));
618 if (nd == NULL)
619 break;
620 --offset;
621 self->top = nd;
622 } while (offset != 0);
623 } else if (offset < 0) {
624 while (1) {
625 h = rb_entry(nd, struct hist_entry, rb_node);
626 if (h->ms.unfolded) {
627 if (first) {
628 if (-offset > h->row_offset) {
629 offset += h->row_offset;
630 h->row_offset = 0;
631 } else {
632 h->row_offset += offset;
633 offset = 0;
634 self->top = nd;
635 break;
636 }
637 } else {
638 if (-offset > h->nr_rows) {
639 offset += h->nr_rows;
640 h->row_offset = 0;
641 } else {
642 h->row_offset = h->nr_rows + offset;
643 offset = 0;
644 self->top = nd;
645 break;
646 }
647 }
648 }
649
650 nd = hists__filter_prev_entries(rb_prev(nd));
651 if (nd == NULL)
652 break;
653 ++offset;
654 self->top = nd;
655 if (offset == 0) {
656 /*
657 * Last unfiltered hist_entry, check if it is
658 * unfolded, if it is then we should have
659 * row_offset at its last entry.
660 */
661 h = rb_entry(nd, struct hist_entry, rb_node);
662 if (h->ms.unfolded)
663 h->row_offset = h->nr_rows;
664 break;
665 }
666 first = false;
667 }
668 } else {
669 self->top = nd;
670 h = rb_entry(nd, struct hist_entry, rb_node);
671 h->row_offset = 0;
672 }
673}
674
675static struct hist_browser *hist_browser__new(struct hists *hists)
676{
677 struct hist_browser *self = zalloc(sizeof(*self));
678
679 if (self) {
680 self->hists = hists;
681 self->b.refresh = hist_browser__refresh;
682 self->b.seek = ui_browser__hists_seek;
683 }
684
685 return self;
686}
687
688static void hist_browser__delete(struct hist_browser *self)
689{
690 newtFormDestroy(self->b.form);
691 newtPopWindow();
692 free(self);
693}
694
695static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
696{
697 return self->he_selection;
698}
699
700static struct thread *hist_browser__selected_thread(struct hist_browser *self)
701{
702 return self->he_selection->thread;
703}
704
705static int hist_browser__title(char *bf, size_t size, const char *ev_name,
706 const struct dso *dso, const struct thread *thread)
707{
708 int printed = 0;
709
710 if (thread)
711 printed += snprintf(bf + printed, size - printed,
712 "Thread: %s(%d)",
713 (thread->comm_set ? thread->comm : ""),
714 thread->pid);
715 if (dso)
716 printed += snprintf(bf + printed, size - printed,
717 "%sDSO: %s", thread ? " " : "",
718 dso->short_name);
719 return printed ?: snprintf(bf, size, "Event: %s", ev_name);
720}
721
722int hists__browse(struct hists *self, const char *helpline, const char *ev_name)
723{
724 struct hist_browser *browser = hist_browser__new(self);
725 struct pstack *fstack;
726 const struct thread *thread_filter = NULL;
727 const struct dso *dso_filter = NULL;
728 struct newtExitStruct es;
729 char msg[160];
730 int key = -1;
731
732 if (browser == NULL)
733 return -1;
734
735 fstack = pstack__new(2);
736 if (fstack == NULL)
737 goto out;
738
739 ui_helpline__push(helpline);
740
741 hist_browser__title(msg, sizeof(msg), ev_name,
742 dso_filter, thread_filter);
743
744 while (1) {
745 const struct thread *thread;
746 const struct dso *dso;
747 char *options[16];
748 int nr_options = 0, choice = 0, i,
749 annotate = -2, zoom_dso = -2, zoom_thread = -2,
750 browse_map = -2;
751
752 if (hist_browser__run(browser, msg, &es))
753 break;
754
755 thread = hist_browser__selected_thread(browser);
756 dso = browser->selection->map ? browser->selection->map->dso : NULL;
757
758 if (es.reason == NEWT_EXIT_HOTKEY) {
759 key = es.u.key;
760
761 switch (key) {
762 case NEWT_KEY_F1:
763 goto do_help;
764 case NEWT_KEY_TAB:
765 case NEWT_KEY_UNTAB:
766 /*
767 * Exit the browser, let hists__browser_tree
768 * go to the next or previous
769 */
770 goto out_free_stack;
771 default:;
772 }
773
774 switch (key) {
775 case 'a':
776 if (browser->selection->map == NULL &&
777 browser->selection->map->dso->annotate_warned)
778 continue;
779 goto do_annotate;
780 case 'd':
781 goto zoom_dso;
782 case 't':
783 goto zoom_thread;
784 case 'h':
785 case '?':
786do_help:
787 ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
788 "<- Zoom out\n"
789 "a Annotate current symbol\n"
790 "h/?/F1 Show this window\n"
791 "d Zoom into current DSO\n"
792 "t Zoom into current Thread\n"
793 "q/CTRL+C Exit browser");
794 continue;
795 default:;
796 }
797 if (is_exit_key(key)) {
798 if (key == NEWT_KEY_ESCAPE &&
799 !ui__dialog_yesno("Do you really want to exit?"))
800 continue;
801 break;
802 }
803
804 if (es.u.key == NEWT_KEY_LEFT) {
805 const void *top;
806
807 if (pstack__empty(fstack))
808 continue;
809 top = pstack__pop(fstack);
810 if (top == &dso_filter)
811 goto zoom_out_dso;
812 if (top == &thread_filter)
813 goto zoom_out_thread;
814 continue;
815 }
816 }
817
818 if (browser->selection->sym != NULL &&
819 !browser->selection->map->dso->annotate_warned &&
820 asprintf(&options[nr_options], "Annotate %s",
821 browser->selection->sym->name) > 0)
822 annotate = nr_options++;
823
824 if (thread != NULL &&
825 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
826 (thread_filter ? "out of" : "into"),
827 (thread->comm_set ? thread->comm : ""),
828 thread->pid) > 0)
829 zoom_thread = nr_options++;
830
831 if (dso != NULL &&
832 asprintf(&options[nr_options], "Zoom %s %s DSO",
833 (dso_filter ? "out of" : "into"),
834 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
835 zoom_dso = nr_options++;
836
837 if (browser->selection->map != NULL &&
838 asprintf(&options[nr_options], "Browse map details") > 0)
839 browse_map = nr_options++;
840
841 options[nr_options++] = (char *)"Exit";
842
843 choice = ui__popup_menu(nr_options, options);
844
845 for (i = 0; i < nr_options - 1; ++i)
846 free(options[i]);
847
848 if (choice == nr_options - 1)
849 break;
850
851 if (choice == -1)
852 continue;
853
854 if (choice == annotate) {
855 struct hist_entry *he;
856do_annotate:
857 if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) {
858 browser->selection->map->dso->annotate_warned = 1;
859 ui_helpline__puts("No vmlinux file found, can't "
860 "annotate with just a "
861 "kallsyms file");
862 continue;
863 }
864
865 he = hist_browser__selected_entry(browser);
866 if (he == NULL)
867 continue;
868
869 hist_entry__tui_annotate(he);
870 } else if (choice == browse_map)
871 map__browse(browser->selection->map);
872 else if (choice == zoom_dso) {
873zoom_dso:
874 if (dso_filter) {
875 pstack__remove(fstack, &dso_filter);
876zoom_out_dso:
877 ui_helpline__pop();
878 dso_filter = NULL;
879 } else {
880 if (dso == NULL)
881 continue;
882 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
883 dso->kernel ? "the Kernel" : dso->short_name);
884 dso_filter = dso;
885 pstack__push(fstack, &dso_filter);
886 }
887 hists__filter_by_dso(self, dso_filter);
888 hist_browser__title(msg, sizeof(msg), ev_name,
889 dso_filter, thread_filter);
890 hist_browser__reset(browser);
891 } else if (choice == zoom_thread) {
892zoom_thread:
893 if (thread_filter) {
894 pstack__remove(fstack, &thread_filter);
895zoom_out_thread:
896 ui_helpline__pop();
897 thread_filter = NULL;
898 } else {
899 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
900 thread->comm_set ? thread->comm : "",
901 thread->pid);
902 thread_filter = thread;
903 pstack__push(fstack, &thread_filter);
904 }
905 hists__filter_by_thread(self, thread_filter);
906 hist_browser__title(msg, sizeof(msg), ev_name,
907 dso_filter, thread_filter);
908 hist_browser__reset(browser);
909 }
910 }
911out_free_stack:
912 pstack__delete(fstack);
913out:
914 hist_browser__delete(browser);
915 return key;
916}
917
918int hists__tui_browse_tree(struct rb_root *self, const char *help)
919{
920 struct rb_node *first = rb_first(self), *nd = first, *next;
921 int key = 0;
922
923 while (nd) {
924 struct hists *hists = rb_entry(nd, struct hists, rb_node);
925 const char *ev_name = __event_name(hists->type, hists->config);
926
927 key = hists__browse(hists, help, ev_name);
928
929 if (is_exit_key(key))
930 break;
931
932 switch (key) {
933 case NEWT_KEY_TAB:
934 next = rb_next(nd);
935 if (next)
936 nd = next;
937 break;
938 case NEWT_KEY_UNTAB:
939 if (nd == first)
940 continue;
941 nd = rb_prev(nd);
942 default:
943 break;
944 }
945 }
946
947 return key;
948}
diff --git a/tools/perf/util/ui/browsers/map.c b/tools/perf/util/ui/browsers/map.c
new file mode 100644
index 000000000000..142b825b42bf
--- /dev/null
+++ b/tools/perf/util/ui/browsers/map.c
@@ -0,0 +1,161 @@
1#include "../libslang.h"
2#include <elf.h>
3#include <newt.h>
4#include <sys/ttydefaults.h>
5#include <ctype.h>
6#include <string.h>
7#include <linux/bitops.h>
8#include "../../debug.h"
9#include "../../symbol.h"
10#include "../browser.h"
11#include "../helpline.h"
12#include "map.h"
13
14static int ui_entry__read(const char *title, char *bf, size_t size, int width)
15{
16 struct newtExitStruct es;
17 newtComponent form, entry;
18 const char *result;
19 int err = -1;
20
21 newtCenteredWindow(width, 1, title);
22 form = newtForm(NULL, NULL, 0);
23 if (form == NULL)
24 return -1;
25
26 entry = newtEntry(0, 0, "0x", width, &result, NEWT_FLAG_SCROLL);
27 if (entry == NULL)
28 goto out_free_form;
29
30 newtFormAddComponent(form, entry);
31 newtFormAddHotKey(form, NEWT_KEY_ENTER);
32 newtFormAddHotKey(form, NEWT_KEY_ESCAPE);
33 newtFormAddHotKey(form, NEWT_KEY_LEFT);
34 newtFormAddHotKey(form, CTRL('c'));
35 newtFormRun(form, &es);
36
37 if (result != NULL) {
38 strncpy(bf, result, size);
39 err = 0;
40 }
41out_free_form:
42 newtPopWindow();
43 newtFormDestroy(form);
44 return 0;
45}
46
47struct map_browser {
48 struct ui_browser b;
49 struct map *map;
50 u16 namelen;
51 u8 addrlen;
52};
53
54static void map_browser__write(struct ui_browser *self, void *nd, int row)
55{
56 struct symbol *sym = rb_entry(nd, struct symbol, rb_node);
57 struct map_browser *mb = container_of(self, struct map_browser, b);
58 bool current_entry = ui_browser__is_current_entry(self, row);
59 int color = ui_browser__percent_color(0, current_entry);
60
61 SLsmg_set_color(color);
62 slsmg_printf("%*llx %*llx %c ",
63 mb->addrlen, sym->start, mb->addrlen, sym->end,
64 sym->binding == STB_GLOBAL ? 'g' :
65 sym->binding == STB_LOCAL ? 'l' : 'w');
66 slsmg_write_nstring(sym->name, mb->namelen);
67}
68
69/* FIXME uber-kludgy, see comment on cmd_report... */
70static u32 *symbol__browser_index(struct symbol *self)
71{
72 return ((void *)self) - sizeof(struct rb_node) - sizeof(u32);
73}
74
75static int map_browser__search(struct map_browser *self)
76{
77 char target[512];
78 struct symbol *sym;
79 int err = ui_entry__read("Search by name/addr", target, sizeof(target), 40);
80
81 if (err)
82 return err;
83
84 if (target[0] == '0' && tolower(target[1]) == 'x') {
85 u64 addr = strtoull(target, NULL, 16);
86 sym = map__find_symbol(self->map, addr, NULL);
87 } else
88 sym = map__find_symbol_by_name(self->map, target, NULL);
89
90 if (sym != NULL) {
91 u32 *idx = symbol__browser_index(sym);
92
93 self->b.top = &sym->rb_node;
94 self->b.index = self->b.top_idx = *idx;
95 } else
96 ui_helpline__fpush("%s not found!", target);
97
98 return 0;
99}
100
101static int map_browser__run(struct map_browser *self, struct newtExitStruct *es)
102{
103 if (ui_browser__show(&self->b, self->map->dso->long_name,
104 "Press <- or ESC to exit, %s / to search",
105 verbose ? "" : "restart with -v to use") < 0)
106 return -1;
107
108 newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT);
109 newtFormAddHotKey(self->b.form, NEWT_KEY_ENTER);
110 if (verbose)
111 newtFormAddHotKey(self->b.form, '/');
112
113 while (1) {
114 ui_browser__run(&self->b, es);
115
116 if (es->reason != NEWT_EXIT_HOTKEY)
117 break;
118 if (verbose && es->u.key == '/')
119 map_browser__search(self);
120 else
121 break;
122 }
123
124 ui_browser__hide(&self->b);
125 return 0;
126}
127
128int map__browse(struct map *self)
129{
130 struct map_browser mb = {
131 .b = {
132 .entries = &self->dso->symbols[self->type],
133 .refresh = ui_browser__rb_tree_refresh,
134 .seek = ui_browser__rb_tree_seek,
135 .write = map_browser__write,
136 },
137 .map = self,
138 };
139 struct newtExitStruct es;
140 struct rb_node *nd;
141 char tmp[BITS_PER_LONG / 4];
142 u64 maxaddr = 0;
143
144 for (nd = rb_first(mb.b.entries); nd; nd = rb_next(nd)) {
145 struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
146
147 if (mb.namelen < pos->namelen)
148 mb.namelen = pos->namelen;
149 if (maxaddr < pos->end)
150 maxaddr = pos->end;
151 if (verbose) {
152 u32 *idx = symbol__browser_index(pos);
153 *idx = mb.b.nr_entries;
154 }
155 ++mb.b.nr_entries;
156 }
157
158 mb.addrlen = snprintf(tmp, sizeof(tmp), "%llx", maxaddr);
159 mb.b.width += mb.addrlen * 2 + 4 + mb.namelen;
160 return map_browser__run(&mb, &es);
161}
diff --git a/tools/perf/util/ui/browsers/map.h b/tools/perf/util/ui/browsers/map.h
new file mode 100644
index 000000000000..df8581a43e17
--- /dev/null
+++ b/tools/perf/util/ui/browsers/map.h
@@ -0,0 +1,6 @@
1#ifndef _PERF_UI_MAP_BROWSER_H_
2#define _PERF_UI_MAP_BROWSER_H_ 1
3struct map;
4
5int map__browse(struct map *self);
6#endif /* _PERF_UI_MAP_BROWSER_H_ */
diff --git a/tools/perf/util/ui/helpline.c b/tools/perf/util/ui/helpline.c
new file mode 100644
index 000000000000..8d79daa4458a
--- /dev/null
+++ b/tools/perf/util/ui/helpline.c
@@ -0,0 +1,69 @@
1#define _GNU_SOURCE
2#include <stdio.h>
3#include <stdlib.h>
4#include <newt.h>
5
6#include "../debug.h"
7#include "helpline.h"
8
9void ui_helpline__pop(void)
10{
11 newtPopHelpLine();
12}
13
14void ui_helpline__push(const char *msg)
15{
16 newtPushHelpLine(msg);
17}
18
19void ui_helpline__vpush(const char *fmt, va_list ap)
20{
21 char *s;
22
23 if (vasprintf(&s, fmt, ap) < 0)
24 vfprintf(stderr, fmt, ap);
25 else {
26 ui_helpline__push(s);
27 free(s);
28 }
29}
30
31void ui_helpline__fpush(const char *fmt, ...)
32{
33 va_list ap;
34
35 va_start(ap, fmt);
36 ui_helpline__vpush(fmt, ap);
37 va_end(ap);
38}
39
40void ui_helpline__puts(const char *msg)
41{
42 ui_helpline__pop();
43 ui_helpline__push(msg);
44}
45
46void ui_helpline__init(void)
47{
48 ui_helpline__puts(" ");
49}
50
51char ui_helpline__last_msg[1024];
52
53int ui_helpline__show_help(const char *format, va_list ap)
54{
55 int ret;
56 static int backlog;
57
58 ret = vsnprintf(ui_helpline__last_msg + backlog,
59 sizeof(ui_helpline__last_msg) - backlog, format, ap);
60 backlog += ret;
61
62 if (ui_helpline__last_msg[backlog - 1] == '\n') {
63 ui_helpline__puts(ui_helpline__last_msg);
64 newtRefresh();
65 backlog = 0;
66 }
67
68 return ret;
69}
diff --git a/tools/perf/util/ui/helpline.h b/tools/perf/util/ui/helpline.h
new file mode 100644
index 000000000000..ab6028d0c401
--- /dev/null
+++ b/tools/perf/util/ui/helpline.h
@@ -0,0 +1,11 @@
1#ifndef _PERF_UI_HELPLINE_H_
2#define _PERF_UI_HELPLINE_H_ 1
3
4void ui_helpline__init(void);
5void ui_helpline__pop(void);
6void ui_helpline__push(const char *msg);
7void ui_helpline__vpush(const char *fmt, va_list ap);
8void ui_helpline__fpush(const char *fmt, ...);
9void ui_helpline__puts(const char *msg);
10
11#endif /* _PERF_UI_HELPLINE_H_ */
diff --git a/tools/perf/util/ui/libslang.h b/tools/perf/util/ui/libslang.h
new file mode 100644
index 000000000000..5623da8e8080
--- /dev/null
+++ b/tools/perf/util/ui/libslang.h
@@ -0,0 +1,27 @@
1#ifndef _PERF_UI_SLANG_H_
2#define _PERF_UI_SLANG_H_ 1
3/*
4 * slang versions <= 2.0.6 have a "#if HAVE_LONG_LONG" that breaks
5 * the build if it isn't defined. Use the equivalent one that glibc
6 * has on features.h.
7 */
8#include <features.h>
9#ifndef HAVE_LONG_LONG
10#define HAVE_LONG_LONG __GLIBC_HAVE_LONG_LONG
11#endif
12#include <slang.h>
13
14#if SLANG_VERSION < 20104
15#define slsmg_printf(msg, args...) \
16 SLsmg_printf((char *)msg, ##args)
17#define slsmg_write_nstring(msg, len) \
18 SLsmg_write_nstring((char *)msg, len)
19#define sltt_set_color(obj, name, fg, bg) \
20 SLtt_set_color(obj,(char *)name, (char *)fg, (char *)bg)
21#else
22#define slsmg_printf SLsmg_printf
23#define slsmg_write_nstring SLsmg_write_nstring
24#define sltt_set_color SLtt_set_color
25#endif
26
27#endif /* _PERF_UI_SLANG_H_ */
diff --git a/tools/perf/util/ui/progress.c b/tools/perf/util/ui/progress.c
new file mode 100644
index 000000000000..d7fc399d36b3
--- /dev/null
+++ b/tools/perf/util/ui/progress.c
@@ -0,0 +1,60 @@
1#include <stdlib.h>
2#include <newt.h>
3#include "../cache.h"
4#include "progress.h"
5
6struct ui_progress {
7 newtComponent form, scale;
8};
9
10struct ui_progress *ui_progress__new(const char *title, u64 total)
11{
12 struct ui_progress *self = malloc(sizeof(*self));
13
14 if (self != NULL) {
15 int cols;
16
17 if (use_browser <= 0)
18 return self;
19 newtGetScreenSize(&cols, NULL);
20 cols -= 4;
21 newtCenteredWindow(cols, 1, title);
22 self->form = newtForm(NULL, NULL, 0);
23 if (self->form == NULL)
24 goto out_free_self;
25 self->scale = newtScale(0, 0, cols, total);
26 if (self->scale == NULL)
27 goto out_free_form;
28 newtFormAddComponent(self->form, self->scale);
29 newtRefresh();
30 }
31
32 return self;
33
34out_free_form:
35 newtFormDestroy(self->form);
36out_free_self:
37 free(self);
38 return NULL;
39}
40
41void ui_progress__update(struct ui_progress *self, u64 curr)
42{
43 /*
44 * FIXME: We should have a per UI backend way of showing progress,
45 * stdio will just show a percentage as NN%, etc.
46 */
47 if (use_browser <= 0)
48 return;
49 newtScaleSet(self->scale, curr);
50 newtRefresh();
51}
52
53void ui_progress__delete(struct ui_progress *self)
54{
55 if (use_browser > 0) {
56 newtFormDestroy(self->form);
57 newtPopWindow();
58 }
59 free(self);
60}
diff --git a/tools/perf/util/ui/progress.h b/tools/perf/util/ui/progress.h
new file mode 100644
index 000000000000..a3820a0beb5b
--- /dev/null
+++ b/tools/perf/util/ui/progress.h
@@ -0,0 +1,11 @@
1#ifndef _PERF_UI_PROGRESS_H_
2#define _PERF_UI_PROGRESS_H_ 1
3
4struct ui_progress;
5
6struct ui_progress *ui_progress__new(const char *title, u64 total);
7void ui_progress__delete(struct ui_progress *self);
8
9void ui_progress__update(struct ui_progress *self, u64 curr);
10
11#endif
diff --git a/tools/perf/util/ui/setup.c b/tools/perf/util/ui/setup.c
new file mode 100644
index 000000000000..662085032eb7
--- /dev/null
+++ b/tools/perf/util/ui/setup.c
@@ -0,0 +1,42 @@
1#include <newt.h>
2#include <signal.h>
3#include <stdbool.h>
4
5#include "../cache.h"
6#include "../debug.h"
7#include "browser.h"
8#include "helpline.h"
9
10static void newt_suspend(void *d __used)
11{
12 newtSuspend();
13 raise(SIGTSTP);
14 newtResume();
15}
16
17void setup_browser(void)
18{
19 if (!isatty(1) || !use_browser || dump_trace) {
20 use_browser = 0;
21 setup_pager();
22 return;
23 }
24
25 use_browser = 1;
26 newtInit();
27 newtCls();
28 newtSetSuspendCallback(newt_suspend, NULL);
29 ui_helpline__init();
30 ui_browser__init();
31}
32
33void exit_browser(bool wait_for_ok)
34{
35 if (use_browser > 0) {
36 if (wait_for_ok) {
37 char title[] = "Fatal Error", ok[] = "Ok";
38 newtWinMessage(title, ok, ui_helpline__last_msg);
39 }
40 newtFinished();
41 }
42}
diff --git a/tools/perf/util/ui/util.c b/tools/perf/util/ui/util.c
new file mode 100644
index 000000000000..04600e26ceea
--- /dev/null
+++ b/tools/perf/util/ui/util.c
@@ -0,0 +1,114 @@
1#include <newt.h>
2#include <signal.h>
3#include <stdio.h>
4#include <stdbool.h>
5#include <string.h>
6#include <sys/ttydefaults.h>
7
8#include "../cache.h"
9#include "../debug.h"
10#include "browser.h"
11#include "helpline.h"
12#include "util.h"
13
14newtComponent newt_form__new(void);
15
16static void newt_form__set_exit_keys(newtComponent self)
17{
18 newtFormAddHotKey(self, NEWT_KEY_LEFT);
19 newtFormAddHotKey(self, NEWT_KEY_ESCAPE);
20 newtFormAddHotKey(self, 'Q');
21 newtFormAddHotKey(self, 'q');
22 newtFormAddHotKey(self, CTRL('c'));
23}
24
25newtComponent newt_form__new(void)
26{
27 newtComponent self = newtForm(NULL, NULL, 0);
28 if (self)
29 newt_form__set_exit_keys(self);
30 return self;
31}
32
33int ui__popup_menu(int argc, char * const argv[])
34{
35 struct newtExitStruct es;
36 int i, rc = -1, max_len = 5;
37 newtComponent listbox, form = newt_form__new();
38
39 if (form == NULL)
40 return -1;
41
42 listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT);
43 if (listbox == NULL)
44 goto out_destroy_form;
45
46 newtFormAddComponent(form, listbox);
47
48 for (i = 0; i < argc; ++i) {
49 int len = strlen(argv[i]);
50 if (len > max_len)
51 max_len = len;
52 if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i))
53 goto out_destroy_form;
54 }
55
56 newtCenteredWindow(max_len, argc, NULL);
57 newtFormRun(form, &es);
58 rc = newtListboxGetCurrent(listbox) - NULL;
59 if (es.reason == NEWT_EXIT_HOTKEY)
60 rc = -1;
61 newtPopWindow();
62out_destroy_form:
63 newtFormDestroy(form);
64 return rc;
65}
66
67int ui__help_window(const char *text)
68{
69 struct newtExitStruct es;
70 newtComponent tb, form = newt_form__new();
71 int rc = -1;
72 int max_len = 0, nr_lines = 0;
73 const char *t;
74
75 if (form == NULL)
76 return -1;
77
78 t = text;
79 while (1) {
80 const char *sep = strchr(t, '\n');
81 int len;
82
83 if (sep == NULL)
84 sep = strchr(t, '\0');
85 len = sep - t;
86 if (max_len < len)
87 max_len = len;
88 ++nr_lines;
89 if (*sep == '\0')
90 break;
91 t = sep + 1;
92 }
93
94 tb = newtTextbox(0, 0, max_len, nr_lines, 0);
95 if (tb == NULL)
96 goto out_destroy_form;
97
98 newtTextboxSetText(tb, text);
99 newtFormAddComponent(form, tb);
100 newtCenteredWindow(max_len, nr_lines, NULL);
101 newtFormRun(form, &es);
102 newtPopWindow();
103 rc = 0;
104out_destroy_form:
105 newtFormDestroy(form);
106 return rc;
107}
108
109bool ui__dialog_yesno(const char *msg)
110{
111 /* newtWinChoice should really be accepting const char pointers... */
112 char yes[] = "Yes", no[] = "No";
113 return newtWinChoice(NULL, yes, no, (char *)msg) == 1;
114}
diff --git a/tools/perf/util/ui/util.h b/tools/perf/util/ui/util.h
new file mode 100644
index 000000000000..afcbc1d99531
--- /dev/null
+++ b/tools/perf/util/ui/util.h
@@ -0,0 +1,10 @@
1#ifndef _PERF_UI_UTIL_H_
2#define _PERF_UI_UTIL_H_ 1
3
4#include <stdbool.h>
5
6int ui__popup_menu(int argc, char * const argv[]);
7int ui__help_window(const char *text);
8bool ui__dialog_yesno(const char *msg);
9
10#endif /* _PERF_UI_UTIL_H_ */