blob: 9ece8435353865e6d6b262e6d24a527821022a99 [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
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -030010#include "../../evsel.h"
11#include "../../evlist.h"
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030012#include "../../hist.h"
13#include "../../pstack.h"
14#include "../../sort.h"
15#include "../../util.h"
16
17#include "../browser.h"
18#include "../helpline.h"
19#include "../util.h"
20#include "map.h"
21
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030022struct hist_browser {
23 struct ui_browser b;
24 struct hists *hists;
25 struct hist_entry *he_selection;
26 struct map_symbol *selection;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -030027 const struct thread *thread_filter;
28 const struct dso *dso_filter;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -030029 bool has_symbols;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030030};
31
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -030032static int hists__browser_title(struct hists *self, char *bf, size_t size,
33 const char *ev_name, const struct dso *dso,
34 const struct thread *thread);
35
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030036static void hist_browser__refresh_dimensions(struct hist_browser *self)
37{
38 /* 3 == +/- toggle symbol before actual hist_entry rendering */
39 self->b.width = 3 + (hists__sort_list_width(self->hists) +
40 sizeof("[k]"));
41}
42
43static void hist_browser__reset(struct hist_browser *self)
44{
45 self->b.nr_entries = self->hists->nr_entries;
46 hist_browser__refresh_dimensions(self);
47 ui_browser__reset_index(&self->b);
48}
49
50static char tree__folded_sign(bool unfolded)
51{
52 return unfolded ? '-' : '+';
53}
54
55static char map_symbol__folded(const struct map_symbol *self)
56{
57 return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
58}
59
60static char hist_entry__folded(const struct hist_entry *self)
61{
62 return map_symbol__folded(&self->ms);
63}
64
65static char callchain_list__folded(const struct callchain_list *self)
66{
67 return map_symbol__folded(&self->ms);
68}
69
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -030070static void map_symbol__set_folding(struct map_symbol *self, bool unfold)
71{
72 self->unfolded = unfold ? self->has_children : false;
73}
74
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030075static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
76{
77 int n = 0;
78 struct rb_node *nd;
79
80 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
81 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
82 struct callchain_list *chain;
83 char folded_sign = ' '; /* No children */
84
85 list_for_each_entry(chain, &child->val, list) {
86 ++n;
87 /* We need this because we may not have children */
88 folded_sign = callchain_list__folded(chain);
89 if (folded_sign == '+')
90 break;
91 }
92
93 if (folded_sign == '-') /* Have children and they're unfolded */
94 n += callchain_node__count_rows_rb_tree(child);
95 }
96
97 return n;
98}
99
100static int callchain_node__count_rows(struct callchain_node *node)
101{
102 struct callchain_list *chain;
103 bool unfolded = false;
104 int n = 0;
105
106 list_for_each_entry(chain, &node->val, list) {
107 ++n;
108 unfolded = chain->ms.unfolded;
109 }
110
111 if (unfolded)
112 n += callchain_node__count_rows_rb_tree(node);
113
114 return n;
115}
116
117static int callchain__count_rows(struct rb_root *chain)
118{
119 struct rb_node *nd;
120 int n = 0;
121
122 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
123 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
124 n += callchain_node__count_rows(node);
125 }
126
127 return n;
128}
129
130static bool map_symbol__toggle_fold(struct map_symbol *self)
131{
132 if (!self->has_children)
133 return false;
134
135 self->unfolded = !self->unfolded;
136 return true;
137}
138
139static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
140{
141 struct rb_node *nd = rb_first(&self->rb_root);
142
143 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
144 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
145 struct callchain_list *chain;
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300146 bool first = true;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300147
148 list_for_each_entry(chain, &child->val, list) {
149 if (first) {
150 first = false;
151 chain->ms.has_children = chain->list.next != &child->val ||
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300152 !RB_EMPTY_ROOT(&child->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300153 } else
154 chain->ms.has_children = chain->list.next == &child->val &&
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300155 !RB_EMPTY_ROOT(&child->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300156 }
157
158 callchain_node__init_have_children_rb_tree(child);
159 }
160}
161
162static void callchain_node__init_have_children(struct callchain_node *self)
163{
164 struct callchain_list *chain;
165
166 list_for_each_entry(chain, &self->val, list)
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300167 chain->ms.has_children = !RB_EMPTY_ROOT(&self->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300168
169 callchain_node__init_have_children_rb_tree(self);
170}
171
172static void callchain__init_have_children(struct rb_root *self)
173{
174 struct rb_node *nd;
175
176 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
177 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
178 callchain_node__init_have_children(node);
179 }
180}
181
182static void hist_entry__init_have_children(struct hist_entry *self)
183{
184 if (!self->init_have_children) {
Arnaldo Carvalho de Melo18b308d2010-08-25 12:47:44 -0300185 self->ms.has_children = !RB_EMPTY_ROOT(&self->sorted_chain);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300186 callchain__init_have_children(&self->sorted_chain);
187 self->init_have_children = true;
188 }
189}
190
191static bool hist_browser__toggle_fold(struct hist_browser *self)
192{
193 if (map_symbol__toggle_fold(self->selection)) {
194 struct hist_entry *he = self->he_selection;
195
196 hist_entry__init_have_children(he);
197 self->hists->nr_entries -= he->nr_rows;
198
199 if (he->ms.unfolded)
200 he->nr_rows = callchain__count_rows(&he->sorted_chain);
201 else
202 he->nr_rows = 0;
203 self->hists->nr_entries += he->nr_rows;
204 self->b.nr_entries = self->hists->nr_entries;
205
206 return true;
207 }
208
209 /* If it doesn't have children, no toggling performed */
210 return false;
211}
212
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300213static int callchain_node__set_folding_rb_tree(struct callchain_node *self, bool unfold)
214{
215 int n = 0;
216 struct rb_node *nd;
217
218 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
219 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
220 struct callchain_list *chain;
221 bool has_children = false;
222
223 list_for_each_entry(chain, &child->val, list) {
224 ++n;
225 map_symbol__set_folding(&chain->ms, unfold);
226 has_children = chain->ms.has_children;
227 }
228
229 if (has_children)
230 n += callchain_node__set_folding_rb_tree(child, unfold);
231 }
232
233 return n;
234}
235
236static int callchain_node__set_folding(struct callchain_node *node, bool unfold)
237{
238 struct callchain_list *chain;
239 bool has_children = false;
240 int n = 0;
241
242 list_for_each_entry(chain, &node->val, list) {
243 ++n;
244 map_symbol__set_folding(&chain->ms, unfold);
245 has_children = chain->ms.has_children;
246 }
247
248 if (has_children)
249 n += callchain_node__set_folding_rb_tree(node, unfold);
250
251 return n;
252}
253
254static int callchain__set_folding(struct rb_root *chain, bool unfold)
255{
256 struct rb_node *nd;
257 int n = 0;
258
259 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
260 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
261 n += callchain_node__set_folding(node, unfold);
262 }
263
264 return n;
265}
266
267static void hist_entry__set_folding(struct hist_entry *self, bool unfold)
268{
269 hist_entry__init_have_children(self);
270 map_symbol__set_folding(&self->ms, unfold);
271
272 if (self->ms.has_children) {
273 int n = callchain__set_folding(&self->sorted_chain, unfold);
274 self->nr_rows = unfold ? n : 0;
275 } else
276 self->nr_rows = 0;
277}
278
279static void hists__set_folding(struct hists *self, bool unfold)
280{
281 struct rb_node *nd;
282
283 self->nr_entries = 0;
284
285 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
286 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
287 hist_entry__set_folding(he, unfold);
288 self->nr_entries += 1 + he->nr_rows;
289 }
290}
291
292static void hist_browser__set_folding(struct hist_browser *self, bool unfold)
293{
294 hists__set_folding(self->hists, unfold);
295 self->b.nr_entries = self->hists->nr_entries;
296 /* Go to the start, we may be way after valid entries after a collapse */
297 ui_browser__reset_index(&self->b);
298}
299
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300300static int hist_browser__run(struct hist_browser *self, const char *ev_name,
301 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300302{
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300303 int key;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300304 int delay_msecs = delay_secs * 1000;
305 char title[160];
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300306 int sym_exit_keys[] = { 'a', 'h', 'C', 'd', 'E', 't', 0, };
307 int exit_keys[] = { '?', 'h', 'D', NEWT_KEY_LEFT, NEWT_KEY_RIGHT,
308 NEWT_KEY_TAB, NEWT_KEY_UNTAB, NEWT_KEY_ENTER, 0, };
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300309
310 self->b.entries = &self->hists->entries;
311 self->b.nr_entries = self->hists->nr_entries;
312
313 hist_browser__refresh_dimensions(self);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300314 hists__browser_title(self->hists, title, sizeof(title), ev_name,
315 self->dso_filter, self->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300316
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300317 if (ui_browser__show(&self->b, title,
318 "Press '?' for help on key bindings") < 0)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300319 return -1;
320
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300321 if (timer != NULL)
322 newtFormSetTimer(self->b.form, delay_msecs);
323
Arnaldo Carvalho de Melo4c1c9522010-08-12 12:37:51 -0300324 ui_browser__add_exit_keys(&self->b, exit_keys);
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300325 if (self->has_symbols)
326 ui_browser__add_exit_keys(&self->b, sym_exit_keys);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300327
328 while (1) {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300329 key = ui_browser__run(&self->b);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300330
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300331 switch (key) {
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300332 case -1:
333 /* FIXME we need to check if it was es.reason == NEWT_EXIT_TIMER */
334 timer(arg);
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300335 ui_browser__update_nr_entries(&self->b, self->hists->nr_entries);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300336 hists__browser_title(self->hists, title, sizeof(title),
337 ev_name, self->dso_filter,
338 self->thread_filter);
339 ui_browser__show_title(&self->b, title);
340 continue;
Arnaldo Carvalho de Melo46941532010-08-10 15:50:07 -0300341 case 'D': { /* Debug */
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300342 static int seq;
343 struct hist_entry *h = rb_entry(self->b.top,
344 struct hist_entry, rb_node);
345 ui_helpline__pop();
346 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
347 seq++, self->b.nr_entries,
348 self->hists->nr_entries,
349 self->b.height,
350 self->b.index,
351 self->b.top_idx,
352 h->row_offset, h->nr_rows);
353 }
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300354 break;
355 case 'C':
356 /* Collapse the whole world. */
357 hist_browser__set_folding(self, false);
358 break;
359 case 'E':
360 /* Expand the whole world. */
361 hist_browser__set_folding(self, true);
362 break;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300363 case NEWT_KEY_ENTER:
364 if (hist_browser__toggle_fold(self))
365 break;
366 /* fall thru */
367 default:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300368 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300369 }
370 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300371out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300372 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300373 return key;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300374}
375
376static char *callchain_list__sym_name(struct callchain_list *self,
377 char *bf, size_t bfsize)
378{
379 if (self->ms.sym)
380 return self->ms.sym->name;
381
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200382 snprintf(bf, bfsize, "%#" PRIx64, self->ip);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300383 return bf;
384}
385
386#define LEVEL_OFFSET_STEP 3
387
388static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
389 struct callchain_node *chain_node,
390 u64 total, int level,
391 unsigned short row,
392 off_t *row_offset,
393 bool *is_current_entry)
394{
395 struct rb_node *node;
396 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
397 u64 new_total, remaining;
398
399 if (callchain_param.mode == CHAIN_GRAPH_REL)
400 new_total = chain_node->children_hit;
401 else
402 new_total = total;
403
404 remaining = new_total;
405 node = rb_first(&chain_node->rb_root);
406 while (node) {
407 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
408 struct rb_node *next = rb_next(node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100409 u64 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300410 struct callchain_list *chain;
411 char folded_sign = ' ';
412 int first = true;
413 int extra_offset = 0;
414
415 remaining -= cumul;
416
417 list_for_each_entry(chain, &child->val, list) {
418 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
419 const char *str;
420 int color;
421 bool was_first = first;
422
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300423 if (first)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300424 first = false;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300425 else
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300426 extra_offset = LEVEL_OFFSET_STEP;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300427
428 folded_sign = callchain_list__folded(chain);
429 if (*row_offset != 0) {
430 --*row_offset;
431 goto do_next;
432 }
433
434 alloc_str = NULL;
435 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
436 if (was_first) {
437 double percent = cumul * 100.0 / new_total;
438
439 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
440 str = "Not enough memory!";
441 else
442 str = alloc_str;
443 }
444
445 color = HE_COLORSET_NORMAL;
446 width = self->b.width - (offset + extra_offset + 2);
447 if (ui_browser__is_current_entry(&self->b, row)) {
448 self->selection = &chain->ms;
449 color = HE_COLORSET_SELECTED;
450 *is_current_entry = true;
451 }
452
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300453 ui_browser__set_color(&self->b, color);
454 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300455 slsmg_write_nstring(" ", offset + extra_offset);
456 slsmg_printf("%c ", folded_sign);
457 slsmg_write_nstring(str, width);
458 free(alloc_str);
459
460 if (++row == self->b.height)
461 goto out;
462do_next:
463 if (folded_sign == '+')
464 break;
465 }
466
467 if (folded_sign == '-') {
468 const int new_level = level + (extra_offset ? 2 : 1);
469 row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
470 new_level, row, row_offset,
471 is_current_entry);
472 }
473 if (row == self->b.height)
474 goto out;
475 node = next;
476 }
477out:
478 return row - first_row;
479}
480
481static int hist_browser__show_callchain_node(struct hist_browser *self,
482 struct callchain_node *node,
483 int level, unsigned short row,
484 off_t *row_offset,
485 bool *is_current_entry)
486{
487 struct callchain_list *chain;
488 int first_row = row,
489 offset = level * LEVEL_OFFSET_STEP,
490 width = self->b.width - offset;
491 char folded_sign = ' ';
492
493 list_for_each_entry(chain, &node->val, list) {
494 char ipstr[BITS_PER_LONG / 4 + 1], *s;
495 int color;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300496
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300497 folded_sign = callchain_list__folded(chain);
498
499 if (*row_offset != 0) {
500 --*row_offset;
501 continue;
502 }
503
504 color = HE_COLORSET_NORMAL;
505 if (ui_browser__is_current_entry(&self->b, row)) {
506 self->selection = &chain->ms;
507 color = HE_COLORSET_SELECTED;
508 *is_current_entry = true;
509 }
510
511 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300512 ui_browser__gotorc(&self->b, row, 0);
513 ui_browser__set_color(&self->b, color);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300514 slsmg_write_nstring(" ", offset);
515 slsmg_printf("%c ", folded_sign);
516 slsmg_write_nstring(s, width - 2);
517
518 if (++row == self->b.height)
519 goto out;
520 }
521
522 if (folded_sign == '-')
523 row += hist_browser__show_callchain_node_rb_tree(self, node,
524 self->hists->stats.total_period,
525 level + 1, row,
526 row_offset,
527 is_current_entry);
528out:
529 return row - first_row;
530}
531
532static int hist_browser__show_callchain(struct hist_browser *self,
533 struct rb_root *chain,
534 int level, unsigned short row,
535 off_t *row_offset,
536 bool *is_current_entry)
537{
538 struct rb_node *nd;
539 int first_row = row;
540
541 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
542 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
543
544 row += hist_browser__show_callchain_node(self, node, level,
545 row, row_offset,
546 is_current_entry);
547 if (row == self->b.height)
548 break;
549 }
550
551 return row - first_row;
552}
553
554static int hist_browser__show_entry(struct hist_browser *self,
555 struct hist_entry *entry,
556 unsigned short row)
557{
558 char s[256];
559 double percent;
560 int printed = 0;
561 int color, width = self->b.width;
562 char folded_sign = ' ';
563 bool current_entry = ui_browser__is_current_entry(&self->b, row);
564 off_t row_offset = entry->row_offset;
565
566 if (current_entry) {
567 self->he_selection = entry;
568 self->selection = &entry->ms;
569 }
570
571 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300572 hist_entry__init_have_children(entry);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300573 folded_sign = hist_entry__folded(entry);
574 }
575
576 if (row_offset == 0) {
577 hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false,
578 0, false, self->hists->stats.total_period);
579 percent = (entry->period * 100.0) / self->hists->stats.total_period;
580
581 color = HE_COLORSET_SELECTED;
582 if (!current_entry) {
583 if (percent >= MIN_RED)
584 color = HE_COLORSET_TOP;
585 else if (percent >= MIN_GREEN)
586 color = HE_COLORSET_MEDIUM;
587 else
588 color = HE_COLORSET_NORMAL;
589 }
590
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300591 ui_browser__set_color(&self->b, color);
592 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300593 if (symbol_conf.use_callchain) {
594 slsmg_printf("%c ", folded_sign);
595 width -= 2;
596 }
597 slsmg_write_nstring(s, width);
598 ++row;
599 ++printed;
600 } else
601 --row_offset;
602
603 if (folded_sign == '-' && row != self->b.height) {
604 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
605 1, row, &row_offset,
606 &current_entry);
607 if (current_entry)
608 self->he_selection = entry;
609 }
610
611 return printed;
612}
613
614static unsigned int hist_browser__refresh(struct ui_browser *self)
615{
616 unsigned row = 0;
617 struct rb_node *nd;
618 struct hist_browser *hb = container_of(self, struct hist_browser, b);
619
620 if (self->top == NULL)
621 self->top = rb_first(&hb->hists->entries);
622
623 for (nd = self->top; nd; nd = rb_next(nd)) {
624 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
625
626 if (h->filtered)
627 continue;
628
629 row += hist_browser__show_entry(hb, h, row);
630 if (row == self->height)
631 break;
632 }
633
634 return row;
635}
636
637static struct rb_node *hists__filter_entries(struct rb_node *nd)
638{
639 while (nd != NULL) {
640 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
641 if (!h->filtered)
642 return nd;
643
644 nd = rb_next(nd);
645 }
646
647 return NULL;
648}
649
650static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
651{
652 while (nd != NULL) {
653 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
654 if (!h->filtered)
655 return nd;
656
657 nd = rb_prev(nd);
658 }
659
660 return NULL;
661}
662
663static void ui_browser__hists_seek(struct ui_browser *self,
664 off_t offset, int whence)
665{
666 struct hist_entry *h;
667 struct rb_node *nd;
668 bool first = true;
669
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300670 if (self->nr_entries == 0)
671 return;
672
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300673 switch (whence) {
674 case SEEK_SET:
675 nd = hists__filter_entries(rb_first(self->entries));
676 break;
677 case SEEK_CUR:
678 nd = self->top;
679 goto do_offset;
680 case SEEK_END:
681 nd = hists__filter_prev_entries(rb_last(self->entries));
682 first = false;
683 break;
684 default:
685 return;
686 }
687
688 /*
689 * Moves not relative to the first visible entry invalidates its
690 * row_offset:
691 */
692 h = rb_entry(self->top, struct hist_entry, rb_node);
693 h->row_offset = 0;
694
695 /*
696 * Here we have to check if nd is expanded (+), if it is we can't go
697 * the next top level hist_entry, instead we must compute an offset of
698 * what _not_ to show and not change the first visible entry.
699 *
700 * This offset increments when we are going from top to bottom and
701 * decreases when we're going from bottom to top.
702 *
703 * As we don't have backpointers to the top level in the callchains
704 * structure, we need to always print the whole hist_entry callchain,
705 * skipping the first ones that are before the first visible entry
706 * and stop when we printed enough lines to fill the screen.
707 */
708do_offset:
709 if (offset > 0) {
710 do {
711 h = rb_entry(nd, struct hist_entry, rb_node);
712 if (h->ms.unfolded) {
713 u16 remaining = h->nr_rows - h->row_offset;
714 if (offset > remaining) {
715 offset -= remaining;
716 h->row_offset = 0;
717 } else {
718 h->row_offset += offset;
719 offset = 0;
720 self->top = nd;
721 break;
722 }
723 }
724 nd = hists__filter_entries(rb_next(nd));
725 if (nd == NULL)
726 break;
727 --offset;
728 self->top = nd;
729 } while (offset != 0);
730 } else if (offset < 0) {
731 while (1) {
732 h = rb_entry(nd, struct hist_entry, rb_node);
733 if (h->ms.unfolded) {
734 if (first) {
735 if (-offset > h->row_offset) {
736 offset += h->row_offset;
737 h->row_offset = 0;
738 } else {
739 h->row_offset += offset;
740 offset = 0;
741 self->top = nd;
742 break;
743 }
744 } else {
745 if (-offset > h->nr_rows) {
746 offset += h->nr_rows;
747 h->row_offset = 0;
748 } else {
749 h->row_offset = h->nr_rows + offset;
750 offset = 0;
751 self->top = nd;
752 break;
753 }
754 }
755 }
756
757 nd = hists__filter_prev_entries(rb_prev(nd));
758 if (nd == NULL)
759 break;
760 ++offset;
761 self->top = nd;
762 if (offset == 0) {
763 /*
764 * Last unfiltered hist_entry, check if it is
765 * unfolded, if it is then we should have
766 * row_offset at its last entry.
767 */
768 h = rb_entry(nd, struct hist_entry, rb_node);
769 if (h->ms.unfolded)
770 h->row_offset = h->nr_rows;
771 break;
772 }
773 first = false;
774 }
775 } else {
776 self->top = nd;
777 h = rb_entry(nd, struct hist_entry, rb_node);
778 h->row_offset = 0;
779 }
780}
781
782static struct hist_browser *hist_browser__new(struct hists *hists)
783{
784 struct hist_browser *self = zalloc(sizeof(*self));
785
786 if (self) {
787 self->hists = hists;
788 self->b.refresh = hist_browser__refresh;
789 self->b.seek = ui_browser__hists_seek;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300790 self->has_symbols = sort_sym.list.next != NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300791 }
792
793 return self;
794}
795
796static void hist_browser__delete(struct hist_browser *self)
797{
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300798 free(self);
799}
800
801static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
802{
803 return self->he_selection;
804}
805
806static struct thread *hist_browser__selected_thread(struct hist_browser *self)
807{
808 return self->he_selection->thread;
809}
810
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300811static int hists__browser_title(struct hists *self, char *bf, size_t size,
812 const char *ev_name, const struct dso *dso,
813 const struct thread *thread)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300814{
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300815 char unit;
816 int printed;
817 unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE];
818
819 nr_events = convert_unit(nr_events, &unit);
820 printed = snprintf(bf, size, "Events: %lu%c %s", nr_events, unit, ev_name);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300821
822 if (thread)
823 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300824 ", Thread: %s(%d)",
825 (thread->comm_set ? thread->comm : ""),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300826 thread->pid);
827 if (dso)
828 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300829 ", DSO: %s", dso->short_name);
830 return printed;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300831}
832
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300833static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300834 const char *helpline, const char *ev_name,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300835 bool left_exits,
836 void(*timer)(void *arg), void *arg,
837 int delay_secs)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300838{
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300839 struct hists *self = &evsel->hists;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300840 struct hist_browser *browser = hist_browser__new(self);
841 struct pstack *fstack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300842 int key = -1;
843
844 if (browser == NULL)
845 return -1;
846
847 fstack = pstack__new(2);
848 if (fstack == NULL)
849 goto out;
850
851 ui_helpline__push(helpline);
852
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300853 while (1) {
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300854 const struct thread *thread = NULL;
855 const struct dso *dso = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300856 char *options[16];
857 int nr_options = 0, choice = 0, i,
858 annotate = -2, zoom_dso = -2, zoom_thread = -2,
859 browse_map = -2;
860
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300861 key = hist_browser__run(browser, ev_name, timer, arg, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300862
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300863 if (browser->he_selection != NULL) {
864 thread = hist_browser__selected_thread(browser);
865 dso = browser->selection->map ? browser->selection->map->dso : NULL;
866 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300867
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300868 switch (key) {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300869 case NEWT_KEY_TAB:
870 case NEWT_KEY_UNTAB:
871 /*
872 * Exit the browser, let hists__browser_tree
873 * go to the next or previous
874 */
875 goto out_free_stack;
876 case 'a':
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300877 if (browser->selection == NULL ||
Lin Mingdb9a9cb2011-04-08 14:31:26 +0800878 browser->selection->sym == NULL ||
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300879 browser->selection->map->dso->annotate_warned)
880 continue;
881 goto do_annotate;
882 case 'd':
883 goto zoom_dso;
884 case 't':
885 goto zoom_thread;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300886 case NEWT_KEY_F1:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300887 case 'h':
888 case '?':
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300889 ui__help_window("h/?/F1 Show this window\n"
890 "TAB/UNTAB Switch events\n"
891 "q/CTRL+C Exit browser\n\n"
892 "For symbolic views (--sort has sym):\n\n"
893 "-> Zoom into DSO/Threads & Annotate current symbol\n"
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300894 "<- Zoom out\n"
895 "a Annotate current symbol\n"
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300896 "C Collapse all callchains\n"
897 "E Expand all callchains\n"
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300898 "d Zoom into current DSO\n"
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300899 "t Zoom into current Thread\n");
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300900 continue;
901 case NEWT_KEY_ENTER:
902 case NEWT_KEY_RIGHT:
903 /* menu */
904 break;
905 case NEWT_KEY_LEFT: {
906 const void *top;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300907
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300908 if (pstack__empty(fstack)) {
909 /*
910 * Go back to the perf_evsel_menu__run or other user
911 */
912 if (left_exits)
913 goto out_free_stack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300914 continue;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300915 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300916 top = pstack__pop(fstack);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300917 if (top == &browser->dso_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300918 goto zoom_out_dso;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300919 if (top == &browser->thread_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300920 goto zoom_out_thread;
921 continue;
922 }
923 case NEWT_KEY_ESCAPE:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300924 if (!left_exits &&
925 !ui__dialog_yesno("Do you really want to exit?"))
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300926 continue;
927 /* Fall thru */
928 default:
929 goto out_free_stack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300930 }
931
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300932 if (!browser->has_symbols)
933 goto add_exit_option;
934
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300935 if (browser->selection != NULL &&
936 browser->selection->sym != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300937 !browser->selection->map->dso->annotate_warned &&
938 asprintf(&options[nr_options], "Annotate %s",
939 browser->selection->sym->name) > 0)
940 annotate = nr_options++;
941
942 if (thread != NULL &&
943 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300944 (browser->thread_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300945 (thread->comm_set ? thread->comm : ""),
946 thread->pid) > 0)
947 zoom_thread = nr_options++;
948
949 if (dso != NULL &&
950 asprintf(&options[nr_options], "Zoom %s %s DSO",
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300951 (browser->dso_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300952 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
953 zoom_dso = nr_options++;
954
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300955 if (browser->selection != NULL &&
956 browser->selection->map != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300957 asprintf(&options[nr_options], "Browse map details") > 0)
958 browse_map = nr_options++;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300959add_exit_option:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300960 options[nr_options++] = (char *)"Exit";
961
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300962 choice = ui__popup_menu(nr_options, options);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300963
964 for (i = 0; i < nr_options - 1; ++i)
965 free(options[i]);
966
967 if (choice == nr_options - 1)
968 break;
969
970 if (choice == -1)
971 continue;
972
973 if (choice == annotate) {
974 struct hist_entry *he;
975do_annotate:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300976 he = hist_browser__selected_entry(browser);
977 if (he == NULL)
978 continue;
979
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300980 hist_entry__tui_annotate(he, evsel->idx, nr_events,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300981 timer, arg, delay_secs);
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300982 ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300983 } else if (choice == browse_map)
984 map__browse(browser->selection->map);
985 else if (choice == zoom_dso) {
986zoom_dso:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300987 if (browser->dso_filter) {
988 pstack__remove(fstack, &browser->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300989zoom_out_dso:
990 ui_helpline__pop();
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300991 browser->dso_filter = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300992 } else {
993 if (dso == NULL)
994 continue;
995 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
996 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300997 browser->dso_filter = dso;
998 pstack__push(fstack, &browser->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300999 }
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001000 hists__filter_by_dso(self, browser->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001001 hist_browser__reset(browser);
1002 } else if (choice == zoom_thread) {
1003zoom_thread:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001004 if (browser->thread_filter) {
1005 pstack__remove(fstack, &browser->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001006zoom_out_thread:
1007 ui_helpline__pop();
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001008 browser->thread_filter = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001009 } else {
1010 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
1011 thread->comm_set ? thread->comm : "",
1012 thread->pid);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001013 browser->thread_filter = thread;
1014 pstack__push(fstack, &browser->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001015 }
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001016 hists__filter_by_thread(self, browser->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001017 hist_browser__reset(browser);
1018 }
1019 }
1020out_free_stack:
1021 pstack__delete(fstack);
1022out:
1023 hist_browser__delete(browser);
1024 return key;
1025}
1026
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001027struct perf_evsel_menu {
1028 struct ui_browser b;
1029 struct perf_evsel *selection;
1030};
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001031
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001032static void perf_evsel_menu__write(struct ui_browser *browser,
1033 void *entry, int row)
1034{
1035 struct perf_evsel_menu *menu = container_of(browser,
1036 struct perf_evsel_menu, b);
1037 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
1038 bool current_entry = ui_browser__is_current_entry(browser, row);
1039 unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1040 const char *ev_name = event_name(evsel);
1041 char bf[256], unit;
1042
1043 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
1044 HE_COLORSET_NORMAL);
1045
1046 nr_events = convert_unit(nr_events, &unit);
1047 snprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
1048 unit, unit == ' ' ? "" : " ", ev_name);
1049 slsmg_write_nstring(bf, browser->width);
1050
1051 if (current_entry)
1052 menu->selection = evsel;
1053}
1054
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001055static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
1056 int nr_events, const char *help,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001057 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001058{
1059 int exit_keys[] = { NEWT_KEY_ENTER, NEWT_KEY_RIGHT, 0, };
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001060 int delay_msecs = delay_secs * 1000;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001061 struct perf_evlist *evlist = menu->b.priv;
1062 struct perf_evsel *pos;
1063 const char *ev_name, *title = "Available samples";
1064 int key;
1065
1066 if (ui_browser__show(&menu->b, title,
1067 "ESC: exit, ENTER|->: Browse histograms") < 0)
1068 return -1;
1069
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001070 if (timer != NULL)
1071 newtFormSetTimer(menu->b.form, delay_msecs);
1072
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001073 ui_browser__add_exit_keys(&menu->b, exit_keys);
1074
1075 while (1) {
1076 key = ui_browser__run(&menu->b);
1077
1078 switch (key) {
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001079 case -1:
1080 /* FIXME we need to check if it was es.reason == NEWT_EXIT_TIMER */
1081 timer(arg);
1082 continue;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001083 case NEWT_KEY_RIGHT:
1084 case NEWT_KEY_ENTER:
1085 if (!menu->selection)
1086 continue;
1087 pos = menu->selection;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001088 perf_evlist__set_selected(evlist, pos);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001089browse_hists:
1090 ev_name = event_name(pos);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001091 key = perf_evsel__hists_browse(pos, nr_events, help,
1092 ev_name, true, timer,
1093 arg, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001094 ui_browser__show_title(&menu->b, title);
1095 break;
1096 case NEWT_KEY_LEFT:
1097 continue;
1098 case NEWT_KEY_ESCAPE:
1099 if (!ui__dialog_yesno("Do you really want to exit?"))
1100 continue;
1101 /* Fall thru */
1102 default:
1103 goto out;
1104 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001105
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001106 switch (key) {
1107 case NEWT_KEY_TAB:
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001108 if (pos->node.next == &evlist->entries)
1109 pos = list_entry(evlist->entries.next, struct perf_evsel, node);
1110 else
1111 pos = list_entry(pos->node.next, struct perf_evsel, node);
Arnaldo Carvalho de Melo7d163202011-10-06 10:46:45 -03001112 perf_evlist__set_selected(evlist, pos);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001113 goto browse_hists;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001114 case NEWT_KEY_UNTAB:
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001115 if (pos->node.prev == &evlist->entries)
1116 pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
1117 else
1118 pos = list_entry(pos->node.prev, struct perf_evsel, node);
Arnaldo Carvalho de Melo7d163202011-10-06 10:46:45 -03001119 perf_evlist__set_selected(evlist, pos);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001120 goto browse_hists;
1121 case 'q':
1122 case CTRL('c'):
1123 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001124 default:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001125 break;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001126 }
1127 }
1128
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001129out:
1130 ui_browser__hide(&menu->b);
1131 return key;
1132}
1133
1134static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001135 const char *help,
1136 void(*timer)(void *arg), void *arg,
1137 int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001138{
1139 struct perf_evsel *pos;
1140 struct perf_evsel_menu menu = {
1141 .b = {
1142 .entries = &evlist->entries,
1143 .refresh = ui_browser__list_head_refresh,
1144 .seek = ui_browser__list_head_seek,
1145 .write = perf_evsel_menu__write,
1146 .nr_entries = evlist->nr_entries,
1147 .priv = evlist,
1148 },
1149 };
1150
1151 ui_helpline__push("Press ESC to exit");
1152
1153 list_for_each_entry(pos, &evlist->entries, node) {
1154 const char *ev_name = event_name(pos);
1155 size_t line_len = strlen(ev_name) + 7;
1156
1157 if (menu.b.width < line_len)
1158 menu.b.width = line_len;
1159 /*
1160 * Cache the evsel name, tracepoints have a _high_ cost per
1161 * event_name() call.
1162 */
1163 if (pos->name == NULL)
1164 pos->name = strdup(ev_name);
1165 }
1166
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001167 return perf_evsel_menu__run(&menu, evlist->nr_entries, help, timer,
1168 arg, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001169}
1170
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001171int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
1172 void(*timer)(void *arg), void *arg,
1173 int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001174{
1175
1176 if (evlist->nr_entries == 1) {
1177 struct perf_evsel *first = list_entry(evlist->entries.next,
1178 struct perf_evsel, node);
1179 const char *ev_name = event_name(first);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001180 return perf_evsel__hists_browse(first, evlist->nr_entries, help,
1181 ev_name, false, timer, arg,
1182 delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001183 }
1184
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001185 return __perf_evlist__tui_browse_hists(evlist, help,
1186 timer, arg, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001187}