blob: c27ab35042f1da80b7fcf8b36ab9fab7cb9ddc87 [file] [log] [blame]
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001#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
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030020struct 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
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -030061static void map_symbol__set_folding(struct map_symbol *self, bool unfold)
62{
63 self->unfolded = unfold ? self->has_children : false;
64}
65
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030066static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
67{
68 int n = 0;
69 struct rb_node *nd;
70
71 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
72 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
73 struct callchain_list *chain;
74 char folded_sign = ' '; /* No children */
75
76 list_for_each_entry(chain, &child->val, list) {
77 ++n;
78 /* We need this because we may not have children */
79 folded_sign = callchain_list__folded(chain);
80 if (folded_sign == '+')
81 break;
82 }
83
84 if (folded_sign == '-') /* Have children and they're unfolded */
85 n += callchain_node__count_rows_rb_tree(child);
86 }
87
88 return n;
89}
90
91static int callchain_node__count_rows(struct callchain_node *node)
92{
93 struct callchain_list *chain;
94 bool unfolded = false;
95 int n = 0;
96
97 list_for_each_entry(chain, &node->val, list) {
98 ++n;
99 unfolded = chain->ms.unfolded;
100 }
101
102 if (unfolded)
103 n += callchain_node__count_rows_rb_tree(node);
104
105 return n;
106}
107
108static int callchain__count_rows(struct rb_root *chain)
109{
110 struct rb_node *nd;
111 int n = 0;
112
113 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
114 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
115 n += callchain_node__count_rows(node);
116 }
117
118 return n;
119}
120
121static bool map_symbol__toggle_fold(struct map_symbol *self)
122{
123 if (!self->has_children)
124 return false;
125
126 self->unfolded = !self->unfolded;
127 return true;
128}
129
130static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
131{
132 struct rb_node *nd = rb_first(&self->rb_root);
133
134 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
135 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
136 struct callchain_list *chain;
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300137 bool first = true;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300138
139 list_for_each_entry(chain, &child->val, list) {
140 if (first) {
141 first = false;
142 chain->ms.has_children = chain->list.next != &child->val ||
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300143 !RB_EMPTY_ROOT(&child->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300144 } else
145 chain->ms.has_children = chain->list.next == &child->val &&
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300146 !RB_EMPTY_ROOT(&child->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300147 }
148
149 callchain_node__init_have_children_rb_tree(child);
150 }
151}
152
153static void callchain_node__init_have_children(struct callchain_node *self)
154{
155 struct callchain_list *chain;
156
157 list_for_each_entry(chain, &self->val, list)
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300158 chain->ms.has_children = !RB_EMPTY_ROOT(&self->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300159
160 callchain_node__init_have_children_rb_tree(self);
161}
162
163static void callchain__init_have_children(struct rb_root *self)
164{
165 struct rb_node *nd;
166
167 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
168 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
169 callchain_node__init_have_children(node);
170 }
171}
172
173static void hist_entry__init_have_children(struct hist_entry *self)
174{
175 if (!self->init_have_children) {
Arnaldo Carvalho de Melo18b308d2010-08-25 12:47:44 -0300176 self->ms.has_children = !RB_EMPTY_ROOT(&self->sorted_chain);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300177 callchain__init_have_children(&self->sorted_chain);
178 self->init_have_children = true;
179 }
180}
181
182static bool hist_browser__toggle_fold(struct hist_browser *self)
183{
184 if (map_symbol__toggle_fold(self->selection)) {
185 struct hist_entry *he = self->he_selection;
186
187 hist_entry__init_have_children(he);
188 self->hists->nr_entries -= he->nr_rows;
189
190 if (he->ms.unfolded)
191 he->nr_rows = callchain__count_rows(&he->sorted_chain);
192 else
193 he->nr_rows = 0;
194 self->hists->nr_entries += he->nr_rows;
195 self->b.nr_entries = self->hists->nr_entries;
196
197 return true;
198 }
199
200 /* If it doesn't have children, no toggling performed */
201 return false;
202}
203
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300204static int callchain_node__set_folding_rb_tree(struct callchain_node *self, bool unfold)
205{
206 int n = 0;
207 struct rb_node *nd;
208
209 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
210 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
211 struct callchain_list *chain;
212 bool has_children = false;
213
214 list_for_each_entry(chain, &child->val, list) {
215 ++n;
216 map_symbol__set_folding(&chain->ms, unfold);
217 has_children = chain->ms.has_children;
218 }
219
220 if (has_children)
221 n += callchain_node__set_folding_rb_tree(child, unfold);
222 }
223
224 return n;
225}
226
227static int callchain_node__set_folding(struct callchain_node *node, bool unfold)
228{
229 struct callchain_list *chain;
230 bool has_children = false;
231 int n = 0;
232
233 list_for_each_entry(chain, &node->val, list) {
234 ++n;
235 map_symbol__set_folding(&chain->ms, unfold);
236 has_children = chain->ms.has_children;
237 }
238
239 if (has_children)
240 n += callchain_node__set_folding_rb_tree(node, unfold);
241
242 return n;
243}
244
245static int callchain__set_folding(struct rb_root *chain, bool unfold)
246{
247 struct rb_node *nd;
248 int n = 0;
249
250 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
251 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
252 n += callchain_node__set_folding(node, unfold);
253 }
254
255 return n;
256}
257
258static void hist_entry__set_folding(struct hist_entry *self, bool unfold)
259{
260 hist_entry__init_have_children(self);
261 map_symbol__set_folding(&self->ms, unfold);
262
263 if (self->ms.has_children) {
264 int n = callchain__set_folding(&self->sorted_chain, unfold);
265 self->nr_rows = unfold ? n : 0;
266 } else
267 self->nr_rows = 0;
268}
269
270static void hists__set_folding(struct hists *self, bool unfold)
271{
272 struct rb_node *nd;
273
274 self->nr_entries = 0;
275
276 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
277 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
278 hist_entry__set_folding(he, unfold);
279 self->nr_entries += 1 + he->nr_rows;
280 }
281}
282
283static void hist_browser__set_folding(struct hist_browser *self, bool unfold)
284{
285 hists__set_folding(self->hists, unfold);
286 self->b.nr_entries = self->hists->nr_entries;
287 /* Go to the start, we may be way after valid entries after a collapse */
288 ui_browser__reset_index(&self->b);
289}
290
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300291static int hist_browser__run(struct hist_browser *self, const char *title)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300292{
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300293 int key;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300294 int exit_keys[] = { 'a', '?', 'h', 'C', 'd', 'D', 'E', 't',
Arnaldo Carvalho de Meloa03f35c2011-03-03 16:43:03 -0300295 NEWT_KEY_ENTER, NEWT_KEY_RIGHT, NEWT_KEY_LEFT,
296 NEWT_KEY_TAB, NEWT_KEY_UNTAB, 0, };
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300297
298 self->b.entries = &self->hists->entries;
299 self->b.nr_entries = self->hists->nr_entries;
300
301 hist_browser__refresh_dimensions(self);
302
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300303 if (ui_browser__show(&self->b, title,
304 "Press '?' for help on key bindings") < 0)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300305 return -1;
306
Arnaldo Carvalho de Melo4c1c9522010-08-12 12:37:51 -0300307 ui_browser__add_exit_keys(&self->b, exit_keys);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300308
309 while (1) {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300310 key = ui_browser__run(&self->b);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300311
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300312 switch (key) {
Arnaldo Carvalho de Melo46941532010-08-10 15:50:07 -0300313 case 'D': { /* Debug */
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300314 static int seq;
315 struct hist_entry *h = rb_entry(self->b.top,
316 struct hist_entry, rb_node);
317 ui_helpline__pop();
318 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
319 seq++, self->b.nr_entries,
320 self->hists->nr_entries,
321 self->b.height,
322 self->b.index,
323 self->b.top_idx,
324 h->row_offset, h->nr_rows);
325 }
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300326 break;
327 case 'C':
328 /* Collapse the whole world. */
329 hist_browser__set_folding(self, false);
330 break;
331 case 'E':
332 /* Expand the whole world. */
333 hist_browser__set_folding(self, true);
334 break;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300335 case NEWT_KEY_ENTER:
336 if (hist_browser__toggle_fold(self))
337 break;
338 /* fall thru */
339 default:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300340 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300341 }
342 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300343out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300344 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300345 return key;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300346}
347
348static char *callchain_list__sym_name(struct callchain_list *self,
349 char *bf, size_t bfsize)
350{
351 if (self->ms.sym)
352 return self->ms.sym->name;
353
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200354 snprintf(bf, bfsize, "%#" PRIx64, self->ip);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300355 return bf;
356}
357
358#define LEVEL_OFFSET_STEP 3
359
360static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
361 struct callchain_node *chain_node,
362 u64 total, int level,
363 unsigned short row,
364 off_t *row_offset,
365 bool *is_current_entry)
366{
367 struct rb_node *node;
368 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
369 u64 new_total, remaining;
370
371 if (callchain_param.mode == CHAIN_GRAPH_REL)
372 new_total = chain_node->children_hit;
373 else
374 new_total = total;
375
376 remaining = new_total;
377 node = rb_first(&chain_node->rb_root);
378 while (node) {
379 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
380 struct rb_node *next = rb_next(node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100381 u64 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300382 struct callchain_list *chain;
383 char folded_sign = ' ';
384 int first = true;
385 int extra_offset = 0;
386
387 remaining -= cumul;
388
389 list_for_each_entry(chain, &child->val, list) {
390 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
391 const char *str;
392 int color;
393 bool was_first = first;
394
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300395 if (first)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300396 first = false;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300397 else
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300398 extra_offset = LEVEL_OFFSET_STEP;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300399
400 folded_sign = callchain_list__folded(chain);
401 if (*row_offset != 0) {
402 --*row_offset;
403 goto do_next;
404 }
405
406 alloc_str = NULL;
407 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
408 if (was_first) {
409 double percent = cumul * 100.0 / new_total;
410
411 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
412 str = "Not enough memory!";
413 else
414 str = alloc_str;
415 }
416
417 color = HE_COLORSET_NORMAL;
418 width = self->b.width - (offset + extra_offset + 2);
419 if (ui_browser__is_current_entry(&self->b, row)) {
420 self->selection = &chain->ms;
421 color = HE_COLORSET_SELECTED;
422 *is_current_entry = true;
423 }
424
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300425 ui_browser__set_color(&self->b, color);
426 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300427 slsmg_write_nstring(" ", offset + extra_offset);
428 slsmg_printf("%c ", folded_sign);
429 slsmg_write_nstring(str, width);
430 free(alloc_str);
431
432 if (++row == self->b.height)
433 goto out;
434do_next:
435 if (folded_sign == '+')
436 break;
437 }
438
439 if (folded_sign == '-') {
440 const int new_level = level + (extra_offset ? 2 : 1);
441 row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
442 new_level, row, row_offset,
443 is_current_entry);
444 }
445 if (row == self->b.height)
446 goto out;
447 node = next;
448 }
449out:
450 return row - first_row;
451}
452
453static int hist_browser__show_callchain_node(struct hist_browser *self,
454 struct callchain_node *node,
455 int level, unsigned short row,
456 off_t *row_offset,
457 bool *is_current_entry)
458{
459 struct callchain_list *chain;
460 int first_row = row,
461 offset = level * LEVEL_OFFSET_STEP,
462 width = self->b.width - offset;
463 char folded_sign = ' ';
464
465 list_for_each_entry(chain, &node->val, list) {
466 char ipstr[BITS_PER_LONG / 4 + 1], *s;
467 int color;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300468
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300469 folded_sign = callchain_list__folded(chain);
470
471 if (*row_offset != 0) {
472 --*row_offset;
473 continue;
474 }
475
476 color = HE_COLORSET_NORMAL;
477 if (ui_browser__is_current_entry(&self->b, row)) {
478 self->selection = &chain->ms;
479 color = HE_COLORSET_SELECTED;
480 *is_current_entry = true;
481 }
482
483 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300484 ui_browser__gotorc(&self->b, row, 0);
485 ui_browser__set_color(&self->b, color);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300486 slsmg_write_nstring(" ", offset);
487 slsmg_printf("%c ", folded_sign);
488 slsmg_write_nstring(s, width - 2);
489
490 if (++row == self->b.height)
491 goto out;
492 }
493
494 if (folded_sign == '-')
495 row += hist_browser__show_callchain_node_rb_tree(self, node,
496 self->hists->stats.total_period,
497 level + 1, row,
498 row_offset,
499 is_current_entry);
500out:
501 return row - first_row;
502}
503
504static int hist_browser__show_callchain(struct hist_browser *self,
505 struct rb_root *chain,
506 int level, unsigned short row,
507 off_t *row_offset,
508 bool *is_current_entry)
509{
510 struct rb_node *nd;
511 int first_row = row;
512
513 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
514 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
515
516 row += hist_browser__show_callchain_node(self, node, level,
517 row, row_offset,
518 is_current_entry);
519 if (row == self->b.height)
520 break;
521 }
522
523 return row - first_row;
524}
525
526static int hist_browser__show_entry(struct hist_browser *self,
527 struct hist_entry *entry,
528 unsigned short row)
529{
530 char s[256];
531 double percent;
532 int printed = 0;
533 int color, width = self->b.width;
534 char folded_sign = ' ';
535 bool current_entry = ui_browser__is_current_entry(&self->b, row);
536 off_t row_offset = entry->row_offset;
537
538 if (current_entry) {
539 self->he_selection = entry;
540 self->selection = &entry->ms;
541 }
542
543 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300544 hist_entry__init_have_children(entry);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300545 folded_sign = hist_entry__folded(entry);
546 }
547
548 if (row_offset == 0) {
549 hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false,
550 0, false, self->hists->stats.total_period);
551 percent = (entry->period * 100.0) / self->hists->stats.total_period;
552
553 color = HE_COLORSET_SELECTED;
554 if (!current_entry) {
555 if (percent >= MIN_RED)
556 color = HE_COLORSET_TOP;
557 else if (percent >= MIN_GREEN)
558 color = HE_COLORSET_MEDIUM;
559 else
560 color = HE_COLORSET_NORMAL;
561 }
562
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300563 ui_browser__set_color(&self->b, color);
564 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300565 if (symbol_conf.use_callchain) {
566 slsmg_printf("%c ", folded_sign);
567 width -= 2;
568 }
569 slsmg_write_nstring(s, width);
570 ++row;
571 ++printed;
572 } else
573 --row_offset;
574
575 if (folded_sign == '-' && row != self->b.height) {
576 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
577 1, row, &row_offset,
578 &current_entry);
579 if (current_entry)
580 self->he_selection = entry;
581 }
582
583 return printed;
584}
585
586static unsigned int hist_browser__refresh(struct ui_browser *self)
587{
588 unsigned row = 0;
589 struct rb_node *nd;
590 struct hist_browser *hb = container_of(self, struct hist_browser, b);
591
592 if (self->top == NULL)
593 self->top = rb_first(&hb->hists->entries);
594
595 for (nd = self->top; nd; nd = rb_next(nd)) {
596 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
597
598 if (h->filtered)
599 continue;
600
601 row += hist_browser__show_entry(hb, h, row);
602 if (row == self->height)
603 break;
604 }
605
606 return row;
607}
608
609static struct rb_node *hists__filter_entries(struct rb_node *nd)
610{
611 while (nd != NULL) {
612 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
613 if (!h->filtered)
614 return nd;
615
616 nd = rb_next(nd);
617 }
618
619 return NULL;
620}
621
622static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
623{
624 while (nd != NULL) {
625 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
626 if (!h->filtered)
627 return nd;
628
629 nd = rb_prev(nd);
630 }
631
632 return NULL;
633}
634
635static void ui_browser__hists_seek(struct ui_browser *self,
636 off_t offset, int whence)
637{
638 struct hist_entry *h;
639 struct rb_node *nd;
640 bool first = true;
641
642 switch (whence) {
643 case SEEK_SET:
644 nd = hists__filter_entries(rb_first(self->entries));
645 break;
646 case SEEK_CUR:
647 nd = self->top;
648 goto do_offset;
649 case SEEK_END:
650 nd = hists__filter_prev_entries(rb_last(self->entries));
651 first = false;
652 break;
653 default:
654 return;
655 }
656
657 /*
658 * Moves not relative to the first visible entry invalidates its
659 * row_offset:
660 */
661 h = rb_entry(self->top, struct hist_entry, rb_node);
662 h->row_offset = 0;
663
664 /*
665 * Here we have to check if nd is expanded (+), if it is we can't go
666 * the next top level hist_entry, instead we must compute an offset of
667 * what _not_ to show and not change the first visible entry.
668 *
669 * This offset increments when we are going from top to bottom and
670 * decreases when we're going from bottom to top.
671 *
672 * As we don't have backpointers to the top level in the callchains
673 * structure, we need to always print the whole hist_entry callchain,
674 * skipping the first ones that are before the first visible entry
675 * and stop when we printed enough lines to fill the screen.
676 */
677do_offset:
678 if (offset > 0) {
679 do {
680 h = rb_entry(nd, struct hist_entry, rb_node);
681 if (h->ms.unfolded) {
682 u16 remaining = h->nr_rows - h->row_offset;
683 if (offset > remaining) {
684 offset -= remaining;
685 h->row_offset = 0;
686 } else {
687 h->row_offset += offset;
688 offset = 0;
689 self->top = nd;
690 break;
691 }
692 }
693 nd = hists__filter_entries(rb_next(nd));
694 if (nd == NULL)
695 break;
696 --offset;
697 self->top = nd;
698 } while (offset != 0);
699 } else if (offset < 0) {
700 while (1) {
701 h = rb_entry(nd, struct hist_entry, rb_node);
702 if (h->ms.unfolded) {
703 if (first) {
704 if (-offset > h->row_offset) {
705 offset += h->row_offset;
706 h->row_offset = 0;
707 } else {
708 h->row_offset += offset;
709 offset = 0;
710 self->top = nd;
711 break;
712 }
713 } else {
714 if (-offset > h->nr_rows) {
715 offset += h->nr_rows;
716 h->row_offset = 0;
717 } else {
718 h->row_offset = h->nr_rows + offset;
719 offset = 0;
720 self->top = nd;
721 break;
722 }
723 }
724 }
725
726 nd = hists__filter_prev_entries(rb_prev(nd));
727 if (nd == NULL)
728 break;
729 ++offset;
730 self->top = nd;
731 if (offset == 0) {
732 /*
733 * Last unfiltered hist_entry, check if it is
734 * unfolded, if it is then we should have
735 * row_offset at its last entry.
736 */
737 h = rb_entry(nd, struct hist_entry, rb_node);
738 if (h->ms.unfolded)
739 h->row_offset = h->nr_rows;
740 break;
741 }
742 first = false;
743 }
744 } else {
745 self->top = nd;
746 h = rb_entry(nd, struct hist_entry, rb_node);
747 h->row_offset = 0;
748 }
749}
750
751static struct hist_browser *hist_browser__new(struct hists *hists)
752{
753 struct hist_browser *self = zalloc(sizeof(*self));
754
755 if (self) {
756 self->hists = hists;
757 self->b.refresh = hist_browser__refresh;
758 self->b.seek = ui_browser__hists_seek;
759 }
760
761 return self;
762}
763
764static void hist_browser__delete(struct hist_browser *self)
765{
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300766 free(self);
767}
768
769static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
770{
771 return self->he_selection;
772}
773
774static struct thread *hist_browser__selected_thread(struct hist_browser *self)
775{
776 return self->he_selection->thread;
777}
778
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300779static int hists__browser_title(struct hists *self, char *bf, size_t size,
780 const char *ev_name, const struct dso *dso,
781 const struct thread *thread)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300782{
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300783 char unit;
784 int printed;
785 unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE];
786
787 nr_events = convert_unit(nr_events, &unit);
788 printed = snprintf(bf, size, "Events: %lu%c %s", nr_events, unit, ev_name);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300789
790 if (thread)
791 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300792 ", Thread: %s(%d)",
793 (thread->comm_set ? thread->comm : ""),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300794 thread->pid);
795 if (dso)
796 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300797 ", DSO: %s", dso->short_name);
798 return printed;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300799}
800
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200801int hists__browse(struct hists *self, const char *helpline,
802 const char *ev_name, int evidx)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300803{
804 struct hist_browser *browser = hist_browser__new(self);
805 struct pstack *fstack;
806 const struct thread *thread_filter = NULL;
807 const struct dso *dso_filter = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300808 char msg[160];
809 int key = -1;
810
811 if (browser == NULL)
812 return -1;
813
814 fstack = pstack__new(2);
815 if (fstack == NULL)
816 goto out;
817
818 ui_helpline__push(helpline);
819
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300820 hists__browser_title(self, msg, sizeof(msg), ev_name,
821 dso_filter, thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300822 while (1) {
823 const struct thread *thread;
824 const struct dso *dso;
825 char *options[16];
826 int nr_options = 0, choice = 0, i,
827 annotate = -2, zoom_dso = -2, zoom_thread = -2,
828 browse_map = -2;
829
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300830 key = hist_browser__run(browser, msg);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300831
832 thread = hist_browser__selected_thread(browser);
833 dso = browser->selection->map ? browser->selection->map->dso : NULL;
834
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300835 switch (key) {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300836 case NEWT_KEY_TAB:
837 case NEWT_KEY_UNTAB:
838 /*
839 * Exit the browser, let hists__browser_tree
840 * go to the next or previous
841 */
842 goto out_free_stack;
843 case 'a':
844 if (browser->selection->map == NULL &&
845 browser->selection->map->dso->annotate_warned)
846 continue;
847 goto do_annotate;
848 case 'd':
849 goto zoom_dso;
850 case 't':
851 goto zoom_thread;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300852 case NEWT_KEY_F1:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300853 case 'h':
854 case '?':
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300855 ui__help_window("-> Zoom into DSO/Threads & Annotate current symbol\n"
856 "<- Zoom out\n"
857 "a Annotate current symbol\n"
858 "h/?/F1 Show this window\n"
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300859 "C Collapse all callchains\n"
860 "E Expand all callchains\n"
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300861 "d Zoom into current DSO\n"
862 "t Zoom into current Thread\n"
Arnaldo Carvalho de Meloa03f35c2011-03-03 16:43:03 -0300863 "TAB/UNTAB Switch events\n"
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300864 "q/CTRL+C Exit browser");
865 continue;
866 case NEWT_KEY_ENTER:
867 case NEWT_KEY_RIGHT:
868 /* menu */
869 break;
870 case NEWT_KEY_LEFT: {
871 const void *top;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300872
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300873 if (pstack__empty(fstack))
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300874 continue;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300875 top = pstack__pop(fstack);
876 if (top == &dso_filter)
877 goto zoom_out_dso;
878 if (top == &thread_filter)
879 goto zoom_out_thread;
880 continue;
881 }
882 case NEWT_KEY_ESCAPE:
883 if (!ui__dialog_yesno("Do you really want to exit?"))
884 continue;
885 /* Fall thru */
886 default:
887 goto out_free_stack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300888 }
889
890 if (browser->selection->sym != NULL &&
891 !browser->selection->map->dso->annotate_warned &&
892 asprintf(&options[nr_options], "Annotate %s",
893 browser->selection->sym->name) > 0)
894 annotate = nr_options++;
895
896 if (thread != NULL &&
897 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
898 (thread_filter ? "out of" : "into"),
899 (thread->comm_set ? thread->comm : ""),
900 thread->pid) > 0)
901 zoom_thread = nr_options++;
902
903 if (dso != NULL &&
904 asprintf(&options[nr_options], "Zoom %s %s DSO",
905 (dso_filter ? "out of" : "into"),
906 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
907 zoom_dso = nr_options++;
908
909 if (browser->selection->map != NULL &&
910 asprintf(&options[nr_options], "Browse map details") > 0)
911 browse_map = nr_options++;
912
913 options[nr_options++] = (char *)"Exit";
914
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300915 choice = ui__popup_menu(nr_options, options);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300916
917 for (i = 0; i < nr_options - 1; ++i)
918 free(options[i]);
919
920 if (choice == nr_options - 1)
921 break;
922
923 if (choice == -1)
924 continue;
925
926 if (choice == annotate) {
927 struct hist_entry *he;
928do_annotate:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300929 he = hist_browser__selected_entry(browser);
930 if (he == NULL)
931 continue;
932
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200933 hist_entry__tui_annotate(he, evidx);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300934 } else if (choice == browse_map)
935 map__browse(browser->selection->map);
936 else if (choice == zoom_dso) {
937zoom_dso:
938 if (dso_filter) {
939 pstack__remove(fstack, &dso_filter);
940zoom_out_dso:
941 ui_helpline__pop();
942 dso_filter = NULL;
943 } else {
944 if (dso == NULL)
945 continue;
946 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
947 dso->kernel ? "the Kernel" : dso->short_name);
948 dso_filter = dso;
949 pstack__push(fstack, &dso_filter);
950 }
951 hists__filter_by_dso(self, dso_filter);
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300952 hists__browser_title(self, msg, sizeof(msg), ev_name,
953 dso_filter, thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300954 hist_browser__reset(browser);
955 } else if (choice == zoom_thread) {
956zoom_thread:
957 if (thread_filter) {
958 pstack__remove(fstack, &thread_filter);
959zoom_out_thread:
960 ui_helpline__pop();
961 thread_filter = NULL;
962 } else {
963 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
964 thread->comm_set ? thread->comm : "",
965 thread->pid);
966 thread_filter = thread;
967 pstack__push(fstack, &thread_filter);
968 }
969 hists__filter_by_thread(self, thread_filter);
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300970 hists__browser_title(self, msg, sizeof(msg), ev_name,
971 dso_filter, thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300972 hist_browser__reset(browser);
973 }
974 }
975out_free_stack:
976 pstack__delete(fstack);
977out:
978 hist_browser__delete(browser);
979 return key;
980}
981
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200982int hists__tui_browse_tree(struct rb_root *self, const char *help, int evidx)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300983{
984 struct rb_node *first = rb_first(self), *nd = first, *next;
985 int key = 0;
986
987 while (nd) {
988 struct hists *hists = rb_entry(nd, struct hists, rb_node);
989 const char *ev_name = __event_name(hists->type, hists->config);
990
Arnaldo Carvalho de Melo2f525d02011-02-04 13:43:24 -0200991 key = hists__browse(hists, help, ev_name, evidx);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300992 switch (key) {
993 case NEWT_KEY_TAB:
994 next = rb_next(nd);
995 if (next)
996 nd = next;
997 break;
998 case NEWT_KEY_UNTAB:
999 if (nd == first)
1000 continue;
1001 nd = rb_prev(nd);
Arnaldo Carvalho de Meloa03f35c2011-03-03 16:43:03 -03001002 break;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001003 default:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001004 return key;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001005 }
1006 }
1007
1008 return key;
1009}