Commit | Line | Data |
---|---|---|
f9224c5c ACM |
1 | #define _GNU_SOURCE |
2 | #include <stdio.h> | |
3 | #undef _GNU_SOURCE | |
4 | ||
5 | #include <stdlib.h> | |
6 | #include <newt.h> | |
7081e087 | 7 | #include <sys/ttydefaults.h> |
f9224c5c ACM |
8 | |
9 | #include "cache.h" | |
10 | #include "hist.h" | |
11 | #include "session.h" | |
12 | #include "sort.h" | |
13 | #include "symbol.h" | |
14 | ||
5f4d3f88 ACM |
15 | struct ui_progress { |
16 | newtComponent form, scale; | |
17 | }; | |
18 | ||
19 | struct ui_progress *ui_progress__new(const char *title, u64 total) | |
20 | { | |
21 | struct ui_progress *self = malloc(sizeof(*self)); | |
22 | ||
23 | if (self != NULL) { | |
24 | int cols; | |
25 | newtGetScreenSize(&cols, NULL); | |
26 | cols -= 4; | |
27 | newtCenteredWindow(cols, 1, title); | |
28 | self->form = newtForm(NULL, NULL, 0); | |
29 | if (self->form == NULL) | |
30 | goto out_free_self; | |
31 | self->scale = newtScale(0, 0, cols, total); | |
32 | if (self->scale == NULL) | |
33 | goto out_free_form; | |
34 | newtFormAddComponents(self->form, self->scale, NULL); | |
35 | newtRefresh(); | |
36 | } | |
37 | ||
38 | return self; | |
39 | ||
40 | out_free_form: | |
41 | newtFormDestroy(self->form); | |
42 | out_free_self: | |
43 | free(self); | |
44 | return NULL; | |
45 | } | |
46 | ||
47 | void ui_progress__update(struct ui_progress *self, u64 curr) | |
48 | { | |
49 | newtScaleSet(self->scale, curr); | |
50 | newtRefresh(); | |
51 | } | |
52 | ||
53 | void ui_progress__delete(struct ui_progress *self) | |
54 | { | |
55 | newtFormDestroy(self->form); | |
56 | newtPopWindow(); | |
57 | free(self); | |
58 | } | |
59 | ||
60 | static char browser__last_msg[1024]; | |
61 | ||
62 | int browser__show_help(const char *format, va_list ap) | |
63 | { | |
64 | int ret; | |
65 | static int backlog; | |
66 | ||
67 | ret = vsnprintf(browser__last_msg + backlog, | |
68 | sizeof(browser__last_msg) - backlog, format, ap); | |
69 | backlog += ret; | |
70 | ||
71 | if (browser__last_msg[backlog - 1] == '\n') { | |
72 | newtPopHelpLine(); | |
73 | newtPushHelpLine(browser__last_msg); | |
74 | newtRefresh(); | |
75 | backlog = 0; | |
76 | } | |
77 | ||
78 | return ret; | |
79 | } | |
80 | ||
7081e087 ACM |
81 | static void newt_form__set_exit_keys(newtComponent self) |
82 | { | |
83 | newtFormAddHotKey(self, NEWT_KEY_ESCAPE); | |
84 | newtFormAddHotKey(self, 'Q'); | |
85 | newtFormAddHotKey(self, 'q'); | |
86 | newtFormAddHotKey(self, CTRL('c')); | |
87 | } | |
88 | ||
89 | static newtComponent newt_form__new(void) | |
90 | { | |
91 | newtComponent self = newtForm(NULL, NULL, 0); | |
92 | if (self) | |
93 | newt_form__set_exit_keys(self); | |
94 | return self; | |
95 | } | |
96 | ||
53c54019 ACM |
97 | static int popup_menu(int argc, const char *argv[]) |
98 | { | |
99 | struct newtExitStruct es; | |
100 | int i, rc = -1, max_len = 5; | |
101 | newtComponent listbox, form = newt_form__new(); | |
102 | ||
103 | if (form == NULL) | |
104 | return -1; | |
105 | ||
106 | listbox = newtListbox(0, 0, argc, NEWT_FLAG_RETURNEXIT); | |
107 | if (listbox == NULL) | |
108 | goto out_destroy_form; | |
109 | ||
110 | newtFormAddComponents(form, listbox, NULL); | |
111 | ||
112 | for (i = 0; i < argc; ++i) { | |
113 | int len = strlen(argv[i]); | |
114 | if (len > max_len) | |
115 | max_len = len; | |
116 | if (newtListboxAddEntry(listbox, argv[i], (void *)(long)i)) | |
117 | goto out_destroy_form; | |
118 | } | |
119 | ||
120 | newtCenteredWindow(max_len, argc, NULL); | |
121 | newtFormRun(form, &es); | |
122 | rc = newtListboxGetCurrent(listbox) - NULL; | |
123 | if (es.reason == NEWT_EXIT_HOTKEY) | |
124 | rc = -1; | |
125 | newtPopWindow(); | |
126 | out_destroy_form: | |
127 | newtFormDestroy(form); | |
128 | return rc; | |
129 | } | |
130 | ||
131 | static bool dialog_yesno(const char *msg) | |
132 | { | |
133 | /* newtWinChoice should really be accepting const char pointers... */ | |
134 | char yes[] = "Yes", no[] = "No"; | |
135 | return newtWinChoice(NULL, no, yes, (char *)msg) == 2; | |
136 | } | |
137 | ||
4ded2b25 ACM |
138 | /* |
139 | * When debugging newt problems it was useful to be able to "unroll" | |
140 | * the calls to newtCheckBoxTreeAdd{Array,Item}, so that we can generate | |
141 | * a source file with the sequence of calls to these methods, to then | |
142 | * tweak the arrays to get the intended results, so I'm keeping this code | |
143 | * here, may be useful again in the future. | |
144 | */ | |
145 | #undef NEWT_DEBUG | |
146 | ||
147 | static void newt_checkbox_tree__add(newtComponent tree, const char *str, | |
148 | void *priv, int *indexes) | |
149 | { | |
150 | #ifdef NEWT_DEBUG | |
151 | /* Print the newtCheckboxTreeAddArray to tinker with its index arrays */ | |
152 | int i = 0, len = 40 - strlen(str); | |
153 | ||
154 | fprintf(stderr, | |
155 | "\tnewtCheckboxTreeAddItem(tree, %*.*s\"%s\", (void *)%p, 0, ", | |
156 | len, len, " ", str, priv); | |
157 | while (indexes[i] != NEWT_ARG_LAST) { | |
158 | if (indexes[i] != NEWT_ARG_APPEND) | |
159 | fprintf(stderr, " %d,", indexes[i]); | |
160 | else | |
161 | fprintf(stderr, " %s,", "NEWT_ARG_APPEND"); | |
162 | ++i; | |
163 | } | |
164 | fprintf(stderr, " %s", " NEWT_ARG_LAST);\n"); | |
165 | fflush(stderr); | |
166 | #endif | |
167 | newtCheckboxTreeAddArray(tree, str, priv, 0, indexes); | |
168 | } | |
169 | ||
170 | static char *callchain_list__sym_name(struct callchain_list *self, | |
171 | char *bf, size_t bfsize) | |
172 | { | |
b3c9ac08 ACM |
173 | if (self->ms.sym) |
174 | return self->ms.sym->name; | |
4ded2b25 ACM |
175 | |
176 | snprintf(bf, bfsize, "%#Lx", self->ip); | |
177 | return bf; | |
178 | } | |
179 | ||
180 | static void __callchain__append_graph_browser(struct callchain_node *self, | |
181 | newtComponent tree, u64 total, | |
182 | int *indexes, int depth) | |
183 | { | |
184 | struct rb_node *node; | |
185 | u64 new_total, remaining; | |
186 | int idx = 0; | |
187 | ||
188 | if (callchain_param.mode == CHAIN_GRAPH_REL) | |
189 | new_total = self->children_hit; | |
190 | else | |
191 | new_total = total; | |
192 | ||
193 | remaining = new_total; | |
194 | node = rb_first(&self->rb_root); | |
195 | while (node) { | |
196 | struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node); | |
197 | struct rb_node *next = rb_next(node); | |
198 | u64 cumul = cumul_hits(child); | |
199 | struct callchain_list *chain; | |
200 | int first = true, printed = 0; | |
201 | int chain_idx = -1; | |
202 | remaining -= cumul; | |
203 | ||
204 | indexes[depth] = NEWT_ARG_APPEND; | |
205 | indexes[depth + 1] = NEWT_ARG_LAST; | |
206 | ||
207 | list_for_each_entry(chain, &child->val, list) { | |
208 | char ipstr[BITS_PER_LONG / 4 + 1], | |
209 | *alloc_str = NULL; | |
210 | const char *str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr)); | |
211 | ||
212 | if (first) { | |
213 | double percent = cumul * 100.0 / new_total; | |
214 | ||
215 | first = false; | |
216 | if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0) | |
217 | str = "Not enough memory!"; | |
218 | else | |
219 | str = alloc_str; | |
220 | } else { | |
221 | indexes[depth] = idx; | |
222 | indexes[depth + 1] = NEWT_ARG_APPEND; | |
223 | indexes[depth + 2] = NEWT_ARG_LAST; | |
224 | ++chain_idx; | |
225 | } | |
d5679ae4 | 226 | newt_checkbox_tree__add(tree, str, &chain->ms, indexes); |
4ded2b25 ACM |
227 | free(alloc_str); |
228 | ++printed; | |
229 | } | |
230 | ||
231 | indexes[depth] = idx; | |
232 | if (chain_idx != -1) | |
233 | indexes[depth + 1] = chain_idx; | |
234 | if (printed != 0) | |
235 | ++idx; | |
236 | __callchain__append_graph_browser(child, tree, new_total, indexes, | |
237 | depth + (chain_idx != -1 ? 2 : 1)); | |
238 | node = next; | |
239 | } | |
240 | } | |
241 | ||
242 | static void callchain__append_graph_browser(struct callchain_node *self, | |
243 | newtComponent tree, u64 total, | |
244 | int *indexes, int parent_idx) | |
245 | { | |
246 | struct callchain_list *chain; | |
247 | int i = 0; | |
248 | ||
249 | indexes[1] = NEWT_ARG_APPEND; | |
250 | indexes[2] = NEWT_ARG_LAST; | |
251 | ||
252 | list_for_each_entry(chain, &self->val, list) { | |
253 | char ipstr[BITS_PER_LONG / 4 + 1], *str; | |
254 | ||
255 | if (chain->ip >= PERF_CONTEXT_MAX) | |
256 | continue; | |
257 | ||
258 | if (!i++ && sort__first_dimension == SORT_SYM) | |
259 | continue; | |
260 | ||
261 | str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr)); | |
d5679ae4 | 262 | newt_checkbox_tree__add(tree, str, &chain->ms, indexes); |
4ded2b25 ACM |
263 | } |
264 | ||
265 | indexes[1] = parent_idx; | |
266 | indexes[2] = NEWT_ARG_APPEND; | |
267 | indexes[3] = NEWT_ARG_LAST; | |
268 | __callchain__append_graph_browser(self, tree, total, indexes, 2); | |
269 | } | |
270 | ||
271 | static void hist_entry__append_callchain_browser(struct hist_entry *self, | |
272 | newtComponent tree, u64 total, int parent_idx) | |
273 | { | |
274 | struct rb_node *rb_node; | |
275 | int indexes[1024] = { [0] = parent_idx, }; | |
276 | int idx = 0; | |
277 | struct callchain_node *chain; | |
278 | ||
279 | rb_node = rb_first(&self->sorted_chain); | |
280 | while (rb_node) { | |
281 | chain = rb_entry(rb_node, struct callchain_node, rb_node); | |
282 | switch (callchain_param.mode) { | |
283 | case CHAIN_FLAT: | |
284 | break; | |
285 | case CHAIN_GRAPH_ABS: /* falldown */ | |
286 | case CHAIN_GRAPH_REL: | |
287 | callchain__append_graph_browser(chain, tree, total, indexes, idx++); | |
288 | break; | |
289 | case CHAIN_NONE: | |
290 | default: | |
291 | break; | |
292 | } | |
293 | rb_node = rb_next(rb_node); | |
294 | } | |
295 | } | |
296 | ||
f9224c5c | 297 | static size_t hist_entry__append_browser(struct hist_entry *self, |
4ded2b25 | 298 | newtComponent tree, u64 total) |
f9224c5c | 299 | { |
a4e3b956 ACM |
300 | char s[256]; |
301 | size_t ret; | |
f9224c5c ACM |
302 | |
303 | if (symbol_conf.exclude_other && !self->parent) | |
304 | return 0; | |
305 | ||
a4e3b956 ACM |
306 | ret = hist_entry__snprintf(self, s, sizeof(s), NULL, |
307 | false, 0, false, total); | |
4ded2b25 ACM |
308 | if (symbol_conf.use_callchain) { |
309 | int indexes[2]; | |
310 | ||
311 | indexes[0] = NEWT_ARG_APPEND; | |
312 | indexes[1] = NEWT_ARG_LAST; | |
d5679ae4 | 313 | newt_checkbox_tree__add(tree, s, &self->ms, indexes); |
4ded2b25 | 314 | } else |
d5679ae4 | 315 | newtListboxAppendEntry(tree, s, &self->ms); |
4ded2b25 | 316 | |
a4e3b956 | 317 | return ret; |
f9224c5c ACM |
318 | } |
319 | ||
d5679ae4 | 320 | static void map_symbol__annotate_browser(const struct map_symbol *self) |
f9224c5c ACM |
321 | { |
322 | FILE *fp; | |
cb7afb70 | 323 | int cols, rows; |
4ded2b25 | 324 | newtComponent form, tree; |
f9224c5c ACM |
325 | struct newtExitStruct es; |
326 | char *str; | |
327 | size_t line_len, max_line_len = 0; | |
328 | size_t max_usable_width; | |
329 | char *line = NULL; | |
330 | ||
d5679ae4 | 331 | if (self->sym == NULL) |
f9224c5c ACM |
332 | return; |
333 | ||
d5679ae4 ACM |
334 | if (asprintf(&str, "perf annotate -d \"%s\" %s 2>&1 | expand", |
335 | self->map->dso->name, self->sym->name) < 0) | |
f9224c5c ACM |
336 | return; |
337 | ||
338 | fp = popen(str, "r"); | |
339 | if (fp == NULL) | |
340 | goto out_free_str; | |
341 | ||
342 | newtPushHelpLine("Press ESC to exit"); | |
cb7afb70 | 343 | newtGetScreenSize(&cols, &rows); |
4ded2b25 | 344 | tree = newtListbox(0, 0, rows - 5, NEWT_FLAG_SCROLL); |
f9224c5c ACM |
345 | |
346 | while (!feof(fp)) { | |
347 | if (getline(&line, &line_len, fp) < 0 || !line_len) | |
348 | break; | |
349 | while (line_len != 0 && isspace(line[line_len - 1])) | |
350 | line[--line_len] = '\0'; | |
351 | ||
352 | if (line_len > max_line_len) | |
353 | max_line_len = line_len; | |
4ded2b25 | 354 | newtListboxAppendEntry(tree, line, NULL); |
f9224c5c ACM |
355 | } |
356 | fclose(fp); | |
357 | free(line); | |
358 | ||
cb7afb70 | 359 | max_usable_width = cols - 22; |
f9224c5c ACM |
360 | if (max_line_len > max_usable_width) |
361 | max_line_len = max_usable_width; | |
362 | ||
4ded2b25 | 363 | newtListboxSetWidth(tree, max_line_len); |
f9224c5c | 364 | |
d5679ae4 | 365 | newtCenteredWindow(max_line_len + 2, rows - 5, self->sym->name); |
7081e087 | 366 | form = newt_form__new(); |
4ded2b25 | 367 | newtFormAddComponents(form, tree, NULL); |
f9224c5c ACM |
368 | |
369 | newtFormRun(form, &es); | |
370 | newtFormDestroy(form); | |
371 | newtPopWindow(); | |
372 | newtPopHelpLine(); | |
373 | out_free_str: | |
374 | free(str); | |
375 | } | |
376 | ||
53c54019 ACM |
377 | static const void *newt__symbol_tree_get_current(newtComponent self) |
378 | { | |
379 | if (symbol_conf.use_callchain) | |
380 | return newtCheckboxTreeGetCurrent(self); | |
381 | return newtListboxGetCurrent(self); | |
382 | } | |
383 | ||
e65713ea | 384 | static void hist_browser__selection(newtComponent self, void *data) |
53c54019 | 385 | { |
d5679ae4 | 386 | const struct map_symbol **symbol_ptr = data; |
53c54019 ACM |
387 | *symbol_ptr = newt__symbol_tree_get_current(self); |
388 | } | |
389 | ||
e65713ea ACM |
390 | struct hist_browser { |
391 | newtComponent form, tree; | |
392 | const struct map_symbol *selection; | |
393 | }; | |
394 | ||
395 | static struct hist_browser *hist_browser__new(void) | |
396 | { | |
397 | struct hist_browser *self = malloc(sizeof(*self)); | |
398 | ||
399 | if (self != NULL) { | |
400 | char seq[] = "."; | |
401 | int rows; | |
402 | ||
403 | newtGetScreenSize(NULL, &rows); | |
404 | ||
405 | if (symbol_conf.use_callchain) | |
406 | self->tree = newtCheckboxTreeMulti(0, 0, rows - 5, seq, | |
407 | NEWT_FLAG_SCROLL); | |
408 | else | |
409 | self->tree = newtListbox(0, 0, rows - 5, | |
410 | (NEWT_FLAG_SCROLL | | |
411 | NEWT_FLAG_RETURNEXIT)); | |
412 | newtComponentAddCallback(self->tree, hist_browser__selection, | |
413 | &self->selection); | |
414 | } | |
415 | ||
416 | return self; | |
417 | } | |
418 | ||
419 | static void hist_browser__delete(struct hist_browser *self) | |
420 | { | |
421 | newtFormDestroy(self->form); | |
422 | newtPopWindow(); | |
423 | free(self); | |
424 | } | |
425 | ||
426 | static int hist_browser__populate(struct hist_browser *self, struct rb_root *hists, | |
427 | u64 nr_hists, u64 session_total) | |
f9224c5c | 428 | { |
e65713ea ACM |
429 | int max_len = 0, idx, cols, rows; |
430 | struct ui_progress *progress; | |
f9224c5c | 431 | struct rb_node *nd; |
5f4d3f88 | 432 | u64 curr_hist = 0; |
5f4d3f88 ACM |
433 | |
434 | progress = ui_progress__new("Adding entries to the browser...", nr_hists); | |
435 | if (progress == NULL) | |
436 | return -1; | |
f9224c5c | 437 | |
4ded2b25 | 438 | idx = 0; |
f9224c5c ACM |
439 | for (nd = rb_first(hists); nd; nd = rb_next(nd)) { |
440 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); | |
e65713ea | 441 | int len = hist_entry__append_browser(h, self->tree, session_total); |
f9224c5c ACM |
442 | if (len > max_len) |
443 | max_len = len; | |
d5679ae4 | 444 | if (symbol_conf.use_callchain) |
e65713ea ACM |
445 | hist_entry__append_callchain_browser(h, self->tree, |
446 | session_total, idx++); | |
5f4d3f88 ACM |
447 | ++curr_hist; |
448 | if (curr_hist % 5) | |
449 | ui_progress__update(progress, curr_hist); | |
f9224c5c ACM |
450 | } |
451 | ||
5f4d3f88 ACM |
452 | ui_progress__delete(progress); |
453 | ||
e65713ea ACM |
454 | newtGetScreenSize(&cols, &rows); |
455 | ||
4ded2b25 ACM |
456 | if (max_len > cols) |
457 | max_len = cols - 3; | |
458 | ||
459 | if (!symbol_conf.use_callchain) | |
e65713ea | 460 | newtListboxSetWidth(self->tree, max_len); |
4ded2b25 ACM |
461 | |
462 | newtCenteredWindow(max_len + (symbol_conf.use_callchain ? 5 : 0), | |
463 | rows - 5, "Report"); | |
e65713ea ACM |
464 | self->form = newt_form__new(); |
465 | newtFormAddHotKey(self->form, 'A'); | |
466 | newtFormAddHotKey(self->form, 'a'); | |
467 | newtFormAddHotKey(self->form, NEWT_KEY_RIGHT); | |
468 | newtFormAddComponents(self->form, self->tree, NULL); | |
469 | self->selection = newt__symbol_tree_get_current(self->tree); | |
470 | ||
471 | return 0; | |
472 | } | |
473 | ||
474 | int perf_session__browse_hists(struct rb_root *hists, u64 nr_hists, | |
475 | u64 session_total, const char *helpline) | |
476 | { | |
477 | struct newtExitStruct es; | |
478 | char str[1024]; | |
479 | int err = -1; | |
480 | struct hist_browser *browser = hist_browser__new(); | |
481 | ||
482 | if (browser == NULL) | |
483 | return -1; | |
484 | ||
485 | snprintf(str, sizeof(str), "Samples: %Ld", session_total); | |
486 | newtDrawRootText(0, 0, str); | |
487 | newtPushHelpLine(helpline); | |
488 | ||
489 | if (hist_browser__populate(browser, hists, nr_hists, session_total) < 0) | |
490 | goto out; | |
f9224c5c ACM |
491 | |
492 | while (1) { | |
53c54019 ACM |
493 | char annotate[512]; |
494 | const char *options[2]; | |
d5679ae4 | 495 | int nr_options = 0, choice = 0; |
f9224c5c | 496 | |
e65713ea | 497 | newtFormRun(browser->form, &es); |
53c54019 | 498 | if (es.reason == NEWT_EXIT_HOTKEY) { |
d5679ae4 ACM |
499 | if (toupper(es.u.key) == 'A') |
500 | goto do_annotate; | |
53c54019 ACM |
501 | if (es.u.key == NEWT_KEY_ESCAPE || |
502 | toupper(es.u.key) == 'Q' || | |
503 | es.u.key == CTRL('c')) { | |
504 | if (dialog_yesno("Do you really want to exit?")) | |
505 | break; | |
506 | else | |
507 | continue; | |
508 | } | |
509 | } | |
510 | ||
e65713ea | 511 | if (browser->selection->sym != NULL) { |
53c54019 | 512 | snprintf(annotate, sizeof(annotate), |
e65713ea | 513 | "Annotate %s", browser->selection->sym->name); |
53c54019 ACM |
514 | options[nr_options++] = annotate; |
515 | } | |
516 | ||
517 | options[nr_options++] = "Exit"; | |
518 | choice = popup_menu(nr_options, options); | |
519 | if (choice == nr_options - 1) | |
f9224c5c | 520 | break; |
d5679ae4 | 521 | do_annotate: |
e65713ea ACM |
522 | if (browser->selection->sym != NULL && choice >= 0) { |
523 | if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) { | |
d5679ae4 ACM |
524 | newtPopHelpLine(); |
525 | newtPushHelpLine("No vmlinux file found, can't " | |
526 | "annotate with just a " | |
527 | "kallsyms file"); | |
528 | continue; | |
529 | } | |
e65713ea | 530 | map_symbol__annotate_browser(browser->selection); |
d5679ae4 | 531 | } |
f9224c5c | 532 | } |
e65713ea ACM |
533 | err = 0; |
534 | out: | |
535 | hist_browser__delete(browser); | |
536 | return err; | |
f9224c5c ACM |
537 | } |
538 | ||
f9224c5c ACM |
539 | void setup_browser(void) |
540 | { | |
541 | if (!isatty(1)) | |
542 | return; | |
543 | ||
544 | use_browser = true; | |
545 | newtInit(); | |
546 | newtCls(); | |
547 | newtPushHelpLine(" "); | |
548 | } | |
549 | ||
f3a1f0ea | 550 | void exit_browser(bool wait_for_ok) |
f9224c5c | 551 | { |
f3a1f0ea ACM |
552 | if (use_browser) { |
553 | if (wait_for_ok) { | |
554 | char title[] = "Fatal Error", ok[] = "Ok"; | |
555 | newtWinMessage(title, ok, browser__last_msg); | |
556 | } | |
f9224c5c | 557 | newtFinished(); |
f3a1f0ea | 558 | } |
f9224c5c | 559 | } |