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