]>
Commit | Line | Data |
---|---|---|
d1b4f249 ACM |
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 | ||
d1b4f249 ACM |
20 | struct hist_browser { |
21 | struct ui_browser b; | |
22 | struct hists *hists; | |
23 | struct hist_entry *he_selection; | |
24 | struct map_symbol *selection; | |
25 | }; | |
26 | ||
27 | static 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 | ||
34 | static 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 | ||
41 | static char tree__folded_sign(bool unfolded) | |
42 | { | |
43 | return unfolded ? '-' : '+'; | |
44 | } | |
45 | ||
46 | static char map_symbol__folded(const struct map_symbol *self) | |
47 | { | |
48 | return self->has_children ? tree__folded_sign(self->unfolded) : ' '; | |
49 | } | |
50 | ||
51 | static char hist_entry__folded(const struct hist_entry *self) | |
52 | { | |
53 | return map_symbol__folded(&self->ms); | |
54 | } | |
55 | ||
56 | static char callchain_list__folded(const struct callchain_list *self) | |
57 | { | |
58 | return map_symbol__folded(&self->ms); | |
59 | } | |
60 | ||
61 | static 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 | ||
86 | static 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 | ||
103 | static 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 | ||
116 | static 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 | ||
125 | static 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 | ||
148 | static 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 | ||
158 | static 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 | ||
168 | static 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 | ||
176 | static 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 | ||
198 | static 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 | ||
59e8fe32 ACM |
214 | if (ui_browser__show(&self->b, title, |
215 | "Press '?' for help on key bindings") < 0) | |
d1b4f249 ACM |
216 | return -1; |
217 | ||
218 | newtFormAddHotKey(self->b.form, 'A'); | |
219 | newtFormAddHotKey(self->b.form, 'a'); | |
220 | newtFormAddHotKey(self->b.form, '?'); | |
221 | newtFormAddHotKey(self->b.form, 'h'); | |
222 | newtFormAddHotKey(self->b.form, 'H'); | |
223 | newtFormAddHotKey(self->b.form, 'd'); | |
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 | } | |
59e8fe32 ACM |
257 | |
258 | ui_browser__hide(&self->b); | |
d1b4f249 ACM |
259 | return 0; |
260 | } | |
261 | ||
262 | static 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 | ||
274 | static 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; | |
353 | do_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 | } | |
368 | out: | |
369 | return row - first_row; | |
370 | } | |
371 | ||
372 | static 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); | |
424 | out: | |
425 | return row - first_row; | |
426 | } | |
427 | ||
428 | static 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 | ||
450 | static 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 | ¤t_entry); | |
503 | if (current_entry) | |
504 | self->he_selection = entry; | |
505 | } | |
506 | ||
507 | return printed; | |
508 | } | |
509 | ||
510 | static 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 | ||
533 | static 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 | ||
546 | static 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 | ||
559 | static 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 | */ | |
601 | do_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 | ||
675 | static 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 | ||
688 | static void hist_browser__delete(struct hist_browser *self) | |
689 | { | |
690 | newtFormDestroy(self->b.form); | |
691 | newtPopWindow(); | |
692 | free(self); | |
693 | } | |
694 | ||
695 | static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self) | |
696 | { | |
697 | return self->he_selection; | |
698 | } | |
699 | ||
700 | static struct thread *hist_browser__selected_thread(struct hist_browser *self) | |
701 | { | |
702 | return self->he_selection->thread; | |
703 | } | |
704 | ||
705 | static 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 | ||
722 | int 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 | key = toupper(key); | |
775 | switch (key) { | |
776 | case 'A': | |
777 | if (browser->selection->map == NULL && | |
778 | browser->selection->map->dso->annotate_warned) | |
779 | continue; | |
780 | goto do_annotate; | |
781 | case 'D': | |
782 | goto zoom_dso; | |
783 | case 'T': | |
784 | goto zoom_thread; | |
785 | case 'H': | |
786 | case '?': | |
787 | do_help: | |
788 | ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n" | |
789 | "<- Zoom out\n" | |
790 | "a Annotate current symbol\n" | |
791 | "h/?/F1 Show this window\n" | |
792 | "d Zoom into current DSO\n" | |
793 | "t Zoom into current Thread\n" | |
794 | "q/CTRL+C Exit browser"); | |
795 | continue; | |
796 | default:; | |
797 | } | |
798 | if (is_exit_key(key)) { | |
799 | if (key == NEWT_KEY_ESCAPE && | |
1e6dd077 | 800 | !ui__dialog_yesno("Do you really want to exit?")) |
d1b4f249 ACM |
801 | continue; |
802 | break; | |
803 | } | |
804 | ||
805 | if (es.u.key == NEWT_KEY_LEFT) { | |
806 | const void *top; | |
807 | ||
808 | if (pstack__empty(fstack)) | |
809 | continue; | |
810 | top = pstack__pop(fstack); | |
811 | if (top == &dso_filter) | |
812 | goto zoom_out_dso; | |
813 | if (top == &thread_filter) | |
814 | goto zoom_out_thread; | |
815 | continue; | |
816 | } | |
817 | } | |
818 | ||
819 | if (browser->selection->sym != NULL && | |
820 | !browser->selection->map->dso->annotate_warned && | |
821 | asprintf(&options[nr_options], "Annotate %s", | |
822 | browser->selection->sym->name) > 0) | |
823 | annotate = nr_options++; | |
824 | ||
825 | if (thread != NULL && | |
826 | asprintf(&options[nr_options], "Zoom %s %s(%d) thread", | |
827 | (thread_filter ? "out of" : "into"), | |
828 | (thread->comm_set ? thread->comm : ""), | |
829 | thread->pid) > 0) | |
830 | zoom_thread = nr_options++; | |
831 | ||
832 | if (dso != NULL && | |
833 | asprintf(&options[nr_options], "Zoom %s %s DSO", | |
834 | (dso_filter ? "out of" : "into"), | |
835 | (dso->kernel ? "the Kernel" : dso->short_name)) > 0) | |
836 | zoom_dso = nr_options++; | |
837 | ||
838 | if (browser->selection->map != NULL && | |
839 | asprintf(&options[nr_options], "Browse map details") > 0) | |
840 | browse_map = nr_options++; | |
841 | ||
842 | options[nr_options++] = (char *)"Exit"; | |
843 | ||
1e6dd077 | 844 | choice = ui__popup_menu(nr_options, options); |
d1b4f249 ACM |
845 | |
846 | for (i = 0; i < nr_options - 1; ++i) | |
847 | free(options[i]); | |
848 | ||
849 | if (choice == nr_options - 1) | |
850 | break; | |
851 | ||
852 | if (choice == -1) | |
853 | continue; | |
854 | ||
855 | if (choice == annotate) { | |
856 | struct hist_entry *he; | |
857 | do_annotate: | |
858 | if (browser->selection->map->dso->origin == DSO__ORIG_KERNEL) { | |
859 | browser->selection->map->dso->annotate_warned = 1; | |
860 | ui_helpline__puts("No vmlinux file found, can't " | |
861 | "annotate with just a " | |
862 | "kallsyms file"); | |
863 | continue; | |
864 | } | |
865 | ||
866 | he = hist_browser__selected_entry(browser); | |
867 | if (he == NULL) | |
868 | continue; | |
869 | ||
870 | hist_entry__tui_annotate(he); | |
871 | } else if (choice == browse_map) | |
872 | map__browse(browser->selection->map); | |
873 | else if (choice == zoom_dso) { | |
874 | zoom_dso: | |
875 | if (dso_filter) { | |
876 | pstack__remove(fstack, &dso_filter); | |
877 | zoom_out_dso: | |
878 | ui_helpline__pop(); | |
879 | dso_filter = NULL; | |
880 | } else { | |
881 | if (dso == NULL) | |
882 | continue; | |
883 | ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"", | |
884 | dso->kernel ? "the Kernel" : dso->short_name); | |
885 | dso_filter = dso; | |
886 | pstack__push(fstack, &dso_filter); | |
887 | } | |
888 | hists__filter_by_dso(self, dso_filter); | |
889 | hist_browser__title(msg, sizeof(msg), ev_name, | |
890 | dso_filter, thread_filter); | |
891 | hist_browser__reset(browser); | |
892 | } else if (choice == zoom_thread) { | |
893 | zoom_thread: | |
894 | if (thread_filter) { | |
895 | pstack__remove(fstack, &thread_filter); | |
896 | zoom_out_thread: | |
897 | ui_helpline__pop(); | |
898 | thread_filter = NULL; | |
899 | } else { | |
900 | ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"", | |
901 | thread->comm_set ? thread->comm : "", | |
902 | thread->pid); | |
903 | thread_filter = thread; | |
904 | pstack__push(fstack, &thread_filter); | |
905 | } | |
906 | hists__filter_by_thread(self, thread_filter); | |
907 | hist_browser__title(msg, sizeof(msg), ev_name, | |
908 | dso_filter, thread_filter); | |
909 | hist_browser__reset(browser); | |
910 | } | |
911 | } | |
912 | out_free_stack: | |
913 | pstack__delete(fstack); | |
914 | out: | |
915 | hist_browser__delete(browser); | |
916 | return key; | |
917 | } | |
918 | ||
919 | int hists__tui_browse_tree(struct rb_root *self, const char *help) | |
920 | { | |
921 | struct rb_node *first = rb_first(self), *nd = first, *next; | |
922 | int key = 0; | |
923 | ||
924 | while (nd) { | |
925 | struct hists *hists = rb_entry(nd, struct hists, rb_node); | |
926 | const char *ev_name = __event_name(hists->type, hists->config); | |
927 | ||
928 | key = hists__browse(hists, help, ev_name); | |
929 | ||
930 | if (is_exit_key(key)) | |
931 | break; | |
932 | ||
933 | switch (key) { | |
934 | case NEWT_KEY_TAB: | |
935 | next = rb_next(nd); | |
936 | if (next) | |
937 | nd = next; | |
938 | break; | |
939 | case NEWT_KEY_UNTAB: | |
940 | if (nd == first) | |
941 | continue; | |
942 | nd = rb_prev(nd); | |
943 | default: | |
944 | break; | |
945 | } | |
946 | } | |
947 | ||
948 | return key; | |
949 | } |