blob: 603d6ee5a0d778588ab47644a2d1671e6d4ec585 [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 char title[160];
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300305 int sym_exit_keys[] = { 'a', 'h', 'C', 'd', 'E', 't', 0, };
306 int exit_keys[] = { '?', 'h', 'D', NEWT_KEY_LEFT, NEWT_KEY_RIGHT,
307 NEWT_KEY_TAB, NEWT_KEY_UNTAB, NEWT_KEY_ENTER, 0, };
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300308
309 self->b.entries = &self->hists->entries;
310 self->b.nr_entries = self->hists->nr_entries;
311
312 hist_browser__refresh_dimensions(self);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300313 hists__browser_title(self->hists, title, sizeof(title), ev_name,
314 self->dso_filter, self->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300315
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300316 if (ui_browser__show(&self->b, title,
317 "Press '?' for help on key bindings") < 0)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300318 return -1;
319
Arnaldo Carvalho de Melo4c1c9522010-08-12 12:37:51 -0300320 ui_browser__add_exit_keys(&self->b, exit_keys);
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300321 if (self->has_symbols)
322 ui_browser__add_exit_keys(&self->b, sym_exit_keys);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300323
324 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300325 key = ui_browser__run(&self->b, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300326
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300327 switch (key) {
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300328 case -1:
329 /* FIXME we need to check if it was es.reason == NEWT_EXIT_TIMER */
330 timer(arg);
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300331 ui_browser__update_nr_entries(&self->b, self->hists->nr_entries);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300332 hists__browser_title(self->hists, title, sizeof(title),
333 ev_name, self->dso_filter,
334 self->thread_filter);
335 ui_browser__show_title(&self->b, title);
336 continue;
Arnaldo Carvalho de Melo46941532010-08-10 15:50:07 -0300337 case 'D': { /* Debug */
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300338 static int seq;
339 struct hist_entry *h = rb_entry(self->b.top,
340 struct hist_entry, rb_node);
341 ui_helpline__pop();
342 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
343 seq++, self->b.nr_entries,
344 self->hists->nr_entries,
345 self->b.height,
346 self->b.index,
347 self->b.top_idx,
348 h->row_offset, h->nr_rows);
349 }
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300350 break;
351 case 'C':
352 /* Collapse the whole world. */
353 hist_browser__set_folding(self, false);
354 break;
355 case 'E':
356 /* Expand the whole world. */
357 hist_browser__set_folding(self, true);
358 break;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300359 case NEWT_KEY_ENTER:
360 if (hist_browser__toggle_fold(self))
361 break;
362 /* fall thru */
363 default:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300364 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300365 }
366 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300367out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300368 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300369 return key;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300370}
371
372static char *callchain_list__sym_name(struct callchain_list *self,
373 char *bf, size_t bfsize)
374{
375 if (self->ms.sym)
376 return self->ms.sym->name;
377
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200378 snprintf(bf, bfsize, "%#" PRIx64, self->ip);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300379 return bf;
380}
381
382#define LEVEL_OFFSET_STEP 3
383
384static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
385 struct callchain_node *chain_node,
386 u64 total, int level,
387 unsigned short row,
388 off_t *row_offset,
389 bool *is_current_entry)
390{
391 struct rb_node *node;
392 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
393 u64 new_total, remaining;
394
395 if (callchain_param.mode == CHAIN_GRAPH_REL)
396 new_total = chain_node->children_hit;
397 else
398 new_total = total;
399
400 remaining = new_total;
401 node = rb_first(&chain_node->rb_root);
402 while (node) {
403 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
404 struct rb_node *next = rb_next(node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100405 u64 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300406 struct callchain_list *chain;
407 char folded_sign = ' ';
408 int first = true;
409 int extra_offset = 0;
410
411 remaining -= cumul;
412
413 list_for_each_entry(chain, &child->val, list) {
414 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
415 const char *str;
416 int color;
417 bool was_first = first;
418
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300419 if (first)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300420 first = false;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300421 else
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300422 extra_offset = LEVEL_OFFSET_STEP;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300423
424 folded_sign = callchain_list__folded(chain);
425 if (*row_offset != 0) {
426 --*row_offset;
427 goto do_next;
428 }
429
430 alloc_str = NULL;
431 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
432 if (was_first) {
433 double percent = cumul * 100.0 / new_total;
434
435 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
436 str = "Not enough memory!";
437 else
438 str = alloc_str;
439 }
440
441 color = HE_COLORSET_NORMAL;
442 width = self->b.width - (offset + extra_offset + 2);
443 if (ui_browser__is_current_entry(&self->b, row)) {
444 self->selection = &chain->ms;
445 color = HE_COLORSET_SELECTED;
446 *is_current_entry = true;
447 }
448
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300449 ui_browser__set_color(&self->b, color);
450 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300451 slsmg_write_nstring(" ", offset + extra_offset);
452 slsmg_printf("%c ", folded_sign);
453 slsmg_write_nstring(str, width);
454 free(alloc_str);
455
456 if (++row == self->b.height)
457 goto out;
458do_next:
459 if (folded_sign == '+')
460 break;
461 }
462
463 if (folded_sign == '-') {
464 const int new_level = level + (extra_offset ? 2 : 1);
465 row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
466 new_level, row, row_offset,
467 is_current_entry);
468 }
469 if (row == self->b.height)
470 goto out;
471 node = next;
472 }
473out:
474 return row - first_row;
475}
476
477static int hist_browser__show_callchain_node(struct hist_browser *self,
478 struct callchain_node *node,
479 int level, unsigned short row,
480 off_t *row_offset,
481 bool *is_current_entry)
482{
483 struct callchain_list *chain;
484 int first_row = row,
485 offset = level * LEVEL_OFFSET_STEP,
486 width = self->b.width - offset;
487 char folded_sign = ' ';
488
489 list_for_each_entry(chain, &node->val, list) {
490 char ipstr[BITS_PER_LONG / 4 + 1], *s;
491 int color;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300492
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300493 folded_sign = callchain_list__folded(chain);
494
495 if (*row_offset != 0) {
496 --*row_offset;
497 continue;
498 }
499
500 color = HE_COLORSET_NORMAL;
501 if (ui_browser__is_current_entry(&self->b, row)) {
502 self->selection = &chain->ms;
503 color = HE_COLORSET_SELECTED;
504 *is_current_entry = true;
505 }
506
507 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300508 ui_browser__gotorc(&self->b, row, 0);
509 ui_browser__set_color(&self->b, color);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300510 slsmg_write_nstring(" ", offset);
511 slsmg_printf("%c ", folded_sign);
512 slsmg_write_nstring(s, width - 2);
513
514 if (++row == self->b.height)
515 goto out;
516 }
517
518 if (folded_sign == '-')
519 row += hist_browser__show_callchain_node_rb_tree(self, node,
520 self->hists->stats.total_period,
521 level + 1, row,
522 row_offset,
523 is_current_entry);
524out:
525 return row - first_row;
526}
527
528static int hist_browser__show_callchain(struct hist_browser *self,
529 struct rb_root *chain,
530 int level, unsigned short row,
531 off_t *row_offset,
532 bool *is_current_entry)
533{
534 struct rb_node *nd;
535 int first_row = row;
536
537 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
538 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
539
540 row += hist_browser__show_callchain_node(self, node, level,
541 row, row_offset,
542 is_current_entry);
543 if (row == self->b.height)
544 break;
545 }
546
547 return row - first_row;
548}
549
550static int hist_browser__show_entry(struct hist_browser *self,
551 struct hist_entry *entry,
552 unsigned short row)
553{
554 char s[256];
555 double percent;
556 int printed = 0;
557 int color, width = self->b.width;
558 char folded_sign = ' ';
559 bool current_entry = ui_browser__is_current_entry(&self->b, row);
560 off_t row_offset = entry->row_offset;
561
562 if (current_entry) {
563 self->he_selection = entry;
564 self->selection = &entry->ms;
565 }
566
567 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300568 hist_entry__init_have_children(entry);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300569 folded_sign = hist_entry__folded(entry);
570 }
571
572 if (row_offset == 0) {
573 hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false,
574 0, false, self->hists->stats.total_period);
575 percent = (entry->period * 100.0) / self->hists->stats.total_period;
576
577 color = HE_COLORSET_SELECTED;
578 if (!current_entry) {
579 if (percent >= MIN_RED)
580 color = HE_COLORSET_TOP;
581 else if (percent >= MIN_GREEN)
582 color = HE_COLORSET_MEDIUM;
583 else
584 color = HE_COLORSET_NORMAL;
585 }
586
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300587 ui_browser__set_color(&self->b, color);
588 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300589 if (symbol_conf.use_callchain) {
590 slsmg_printf("%c ", folded_sign);
591 width -= 2;
592 }
593 slsmg_write_nstring(s, width);
594 ++row;
595 ++printed;
596 } else
597 --row_offset;
598
599 if (folded_sign == '-' && row != self->b.height) {
600 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
601 1, row, &row_offset,
602 &current_entry);
603 if (current_entry)
604 self->he_selection = entry;
605 }
606
607 return printed;
608}
609
610static unsigned int hist_browser__refresh(struct ui_browser *self)
611{
612 unsigned row = 0;
613 struct rb_node *nd;
614 struct hist_browser *hb = container_of(self, struct hist_browser, b);
615
616 if (self->top == NULL)
617 self->top = rb_first(&hb->hists->entries);
618
619 for (nd = self->top; nd; nd = rb_next(nd)) {
620 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
621
622 if (h->filtered)
623 continue;
624
625 row += hist_browser__show_entry(hb, h, row);
626 if (row == self->height)
627 break;
628 }
629
630 return row;
631}
632
633static struct rb_node *hists__filter_entries(struct rb_node *nd)
634{
635 while (nd != NULL) {
636 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
637 if (!h->filtered)
638 return nd;
639
640 nd = rb_next(nd);
641 }
642
643 return NULL;
644}
645
646static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
647{
648 while (nd != NULL) {
649 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
650 if (!h->filtered)
651 return nd;
652
653 nd = rb_prev(nd);
654 }
655
656 return NULL;
657}
658
659static void ui_browser__hists_seek(struct ui_browser *self,
660 off_t offset, int whence)
661{
662 struct hist_entry *h;
663 struct rb_node *nd;
664 bool first = true;
665
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300666 if (self->nr_entries == 0)
667 return;
668
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300669 switch (whence) {
670 case SEEK_SET:
671 nd = hists__filter_entries(rb_first(self->entries));
672 break;
673 case SEEK_CUR:
674 nd = self->top;
675 goto do_offset;
676 case SEEK_END:
677 nd = hists__filter_prev_entries(rb_last(self->entries));
678 first = false;
679 break;
680 default:
681 return;
682 }
683
684 /*
685 * Moves not relative to the first visible entry invalidates its
686 * row_offset:
687 */
688 h = rb_entry(self->top, struct hist_entry, rb_node);
689 h->row_offset = 0;
690
691 /*
692 * Here we have to check if nd is expanded (+), if it is we can't go
693 * the next top level hist_entry, instead we must compute an offset of
694 * what _not_ to show and not change the first visible entry.
695 *
696 * This offset increments when we are going from top to bottom and
697 * decreases when we're going from bottom to top.
698 *
699 * As we don't have backpointers to the top level in the callchains
700 * structure, we need to always print the whole hist_entry callchain,
701 * skipping the first ones that are before the first visible entry
702 * and stop when we printed enough lines to fill the screen.
703 */
704do_offset:
705 if (offset > 0) {
706 do {
707 h = rb_entry(nd, struct hist_entry, rb_node);
708 if (h->ms.unfolded) {
709 u16 remaining = h->nr_rows - h->row_offset;
710 if (offset > remaining) {
711 offset -= remaining;
712 h->row_offset = 0;
713 } else {
714 h->row_offset += offset;
715 offset = 0;
716 self->top = nd;
717 break;
718 }
719 }
720 nd = hists__filter_entries(rb_next(nd));
721 if (nd == NULL)
722 break;
723 --offset;
724 self->top = nd;
725 } while (offset != 0);
726 } else if (offset < 0) {
727 while (1) {
728 h = rb_entry(nd, struct hist_entry, rb_node);
729 if (h->ms.unfolded) {
730 if (first) {
731 if (-offset > h->row_offset) {
732 offset += h->row_offset;
733 h->row_offset = 0;
734 } else {
735 h->row_offset += offset;
736 offset = 0;
737 self->top = nd;
738 break;
739 }
740 } else {
741 if (-offset > h->nr_rows) {
742 offset += h->nr_rows;
743 h->row_offset = 0;
744 } else {
745 h->row_offset = h->nr_rows + offset;
746 offset = 0;
747 self->top = nd;
748 break;
749 }
750 }
751 }
752
753 nd = hists__filter_prev_entries(rb_prev(nd));
754 if (nd == NULL)
755 break;
756 ++offset;
757 self->top = nd;
758 if (offset == 0) {
759 /*
760 * Last unfiltered hist_entry, check if it is
761 * unfolded, if it is then we should have
762 * row_offset at its last entry.
763 */
764 h = rb_entry(nd, struct hist_entry, rb_node);
765 if (h->ms.unfolded)
766 h->row_offset = h->nr_rows;
767 break;
768 }
769 first = false;
770 }
771 } else {
772 self->top = nd;
773 h = rb_entry(nd, struct hist_entry, rb_node);
774 h->row_offset = 0;
775 }
776}
777
778static struct hist_browser *hist_browser__new(struct hists *hists)
779{
780 struct hist_browser *self = zalloc(sizeof(*self));
781
782 if (self) {
783 self->hists = hists;
784 self->b.refresh = hist_browser__refresh;
785 self->b.seek = ui_browser__hists_seek;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300786 self->has_symbols = sort_sym.list.next != NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300787 }
788
789 return self;
790}
791
792static void hist_browser__delete(struct hist_browser *self)
793{
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300794 free(self);
795}
796
797static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
798{
799 return self->he_selection;
800}
801
802static struct thread *hist_browser__selected_thread(struct hist_browser *self)
803{
804 return self->he_selection->thread;
805}
806
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300807static int hists__browser_title(struct hists *self, char *bf, size_t size,
808 const char *ev_name, const struct dso *dso,
809 const struct thread *thread)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300810{
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300811 char unit;
812 int printed;
813 unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE];
814
815 nr_events = convert_unit(nr_events, &unit);
816 printed = snprintf(bf, size, "Events: %lu%c %s", nr_events, unit, ev_name);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300817
818 if (thread)
819 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300820 ", Thread: %s(%d)",
821 (thread->comm_set ? thread->comm : ""),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300822 thread->pid);
823 if (dso)
824 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300825 ", DSO: %s", dso->short_name);
826 return printed;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300827}
828
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300829static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300830 const char *helpline, const char *ev_name,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300831 bool left_exits,
832 void(*timer)(void *arg), void *arg,
833 int delay_secs)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300834{
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300835 struct hists *self = &evsel->hists;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300836 struct hist_browser *browser = hist_browser__new(self);
837 struct pstack *fstack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300838 int key = -1;
839
840 if (browser == NULL)
841 return -1;
842
843 fstack = pstack__new(2);
844 if (fstack == NULL)
845 goto out;
846
847 ui_helpline__push(helpline);
848
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300849 while (1) {
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300850 const struct thread *thread = NULL;
851 const struct dso *dso = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300852 char *options[16];
853 int nr_options = 0, choice = 0, i,
854 annotate = -2, zoom_dso = -2, zoom_thread = -2,
855 browse_map = -2;
856
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300857 key = hist_browser__run(browser, ev_name, timer, arg, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300858
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300859 if (browser->he_selection != NULL) {
860 thread = hist_browser__selected_thread(browser);
861 dso = browser->selection->map ? browser->selection->map->dso : NULL;
862 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300863
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300864 switch (key) {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300865 case NEWT_KEY_TAB:
866 case NEWT_KEY_UNTAB:
867 /*
868 * Exit the browser, let hists__browser_tree
869 * go to the next or previous
870 */
871 goto out_free_stack;
872 case 'a':
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300873 if (browser->selection == NULL ||
Lin Mingdb9a9cb2011-04-08 14:31:26 +0800874 browser->selection->sym == NULL ||
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300875 browser->selection->map->dso->annotate_warned)
876 continue;
877 goto do_annotate;
878 case 'd':
879 goto zoom_dso;
880 case 't':
881 goto zoom_thread;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300882 case NEWT_KEY_F1:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300883 case 'h':
884 case '?':
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300885 ui__help_window("h/?/F1 Show this window\n"
886 "TAB/UNTAB Switch events\n"
887 "q/CTRL+C Exit browser\n\n"
888 "For symbolic views (--sort has sym):\n\n"
889 "-> Zoom into DSO/Threads & Annotate current symbol\n"
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300890 "<- Zoom out\n"
891 "a Annotate current symbol\n"
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300892 "C Collapse all callchains\n"
893 "E Expand all callchains\n"
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300894 "d Zoom into current DSO\n"
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300895 "t Zoom into current Thread\n");
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300896 continue;
897 case NEWT_KEY_ENTER:
898 case NEWT_KEY_RIGHT:
899 /* menu */
900 break;
901 case NEWT_KEY_LEFT: {
902 const void *top;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300903
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300904 if (pstack__empty(fstack)) {
905 /*
906 * Go back to the perf_evsel_menu__run or other user
907 */
908 if (left_exits)
909 goto out_free_stack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300910 continue;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300911 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300912 top = pstack__pop(fstack);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300913 if (top == &browser->dso_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300914 goto zoom_out_dso;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300915 if (top == &browser->thread_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300916 goto zoom_out_thread;
917 continue;
918 }
919 case NEWT_KEY_ESCAPE:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300920 if (!left_exits &&
921 !ui__dialog_yesno("Do you really want to exit?"))
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300922 continue;
923 /* Fall thru */
924 default:
925 goto out_free_stack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300926 }
927
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300928 if (!browser->has_symbols)
929 goto add_exit_option;
930
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300931 if (browser->selection != NULL &&
932 browser->selection->sym != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300933 !browser->selection->map->dso->annotate_warned &&
934 asprintf(&options[nr_options], "Annotate %s",
935 browser->selection->sym->name) > 0)
936 annotate = nr_options++;
937
938 if (thread != NULL &&
939 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300940 (browser->thread_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300941 (thread->comm_set ? thread->comm : ""),
942 thread->pid) > 0)
943 zoom_thread = nr_options++;
944
945 if (dso != NULL &&
946 asprintf(&options[nr_options], "Zoom %s %s DSO",
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300947 (browser->dso_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300948 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
949 zoom_dso = nr_options++;
950
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300951 if (browser->selection != NULL &&
952 browser->selection->map != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300953 asprintf(&options[nr_options], "Browse map details") > 0)
954 browse_map = nr_options++;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300955add_exit_option:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300956 options[nr_options++] = (char *)"Exit";
957
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300958 choice = ui__popup_menu(nr_options, options);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300959
960 for (i = 0; i < nr_options - 1; ++i)
961 free(options[i]);
962
963 if (choice == nr_options - 1)
964 break;
965
966 if (choice == -1)
967 continue;
968
969 if (choice == annotate) {
970 struct hist_entry *he;
971do_annotate:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300972 he = hist_browser__selected_entry(browser);
973 if (he == NULL)
974 continue;
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300975 /*
976 * Don't let this be freed, say, by hists__decay_entry.
977 */
978 he->used = true;
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300979 hist_entry__tui_annotate(he, evsel->idx, nr_events,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300980 timer, arg, delay_secs);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300981 he->used = false;
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, };
1060 struct perf_evlist *evlist = menu->b.priv;
1061 struct perf_evsel *pos;
1062 const char *ev_name, *title = "Available samples";
1063 int key;
1064
1065 if (ui_browser__show(&menu->b, title,
1066 "ESC: exit, ENTER|->: Browse histograms") < 0)
1067 return -1;
1068
1069 ui_browser__add_exit_keys(&menu->b, exit_keys);
1070
1071 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03001072 key = ui_browser__run(&menu->b, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001073
1074 switch (key) {
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001075 case -1:
1076 /* FIXME we need to check if it was es.reason == NEWT_EXIT_TIMER */
1077 timer(arg);
1078 continue;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001079 case NEWT_KEY_RIGHT:
1080 case NEWT_KEY_ENTER:
1081 if (!menu->selection)
1082 continue;
1083 pos = menu->selection;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001084 perf_evlist__set_selected(evlist, pos);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001085browse_hists:
1086 ev_name = event_name(pos);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001087 key = perf_evsel__hists_browse(pos, nr_events, help,
1088 ev_name, true, timer,
1089 arg, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001090 ui_browser__show_title(&menu->b, title);
1091 break;
1092 case NEWT_KEY_LEFT:
1093 continue;
1094 case NEWT_KEY_ESCAPE:
1095 if (!ui__dialog_yesno("Do you really want to exit?"))
1096 continue;
1097 /* Fall thru */
1098 default:
1099 goto out;
1100 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001101
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001102 switch (key) {
1103 case NEWT_KEY_TAB:
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001104 if (pos->node.next == &evlist->entries)
1105 pos = list_entry(evlist->entries.next, struct perf_evsel, node);
1106 else
1107 pos = list_entry(pos->node.next, struct perf_evsel, node);
Arnaldo Carvalho de Melo7d163202011-10-06 10:46:45 -03001108 perf_evlist__set_selected(evlist, pos);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001109 goto browse_hists;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001110 case NEWT_KEY_UNTAB:
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001111 if (pos->node.prev == &evlist->entries)
1112 pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
1113 else
1114 pos = list_entry(pos->node.prev, struct perf_evsel, node);
Arnaldo Carvalho de Melo7d163202011-10-06 10:46:45 -03001115 perf_evlist__set_selected(evlist, pos);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001116 goto browse_hists;
1117 case 'q':
1118 case CTRL('c'):
1119 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001120 default:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001121 break;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001122 }
1123 }
1124
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001125out:
1126 ui_browser__hide(&menu->b);
1127 return key;
1128}
1129
1130static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001131 const char *help,
1132 void(*timer)(void *arg), void *arg,
1133 int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001134{
1135 struct perf_evsel *pos;
1136 struct perf_evsel_menu menu = {
1137 .b = {
1138 .entries = &evlist->entries,
1139 .refresh = ui_browser__list_head_refresh,
1140 .seek = ui_browser__list_head_seek,
1141 .write = perf_evsel_menu__write,
1142 .nr_entries = evlist->nr_entries,
1143 .priv = evlist,
1144 },
1145 };
1146
1147 ui_helpline__push("Press ESC to exit");
1148
1149 list_for_each_entry(pos, &evlist->entries, node) {
1150 const char *ev_name = event_name(pos);
1151 size_t line_len = strlen(ev_name) + 7;
1152
1153 if (menu.b.width < line_len)
1154 menu.b.width = line_len;
1155 /*
1156 * Cache the evsel name, tracepoints have a _high_ cost per
1157 * event_name() call.
1158 */
1159 if (pos->name == NULL)
1160 pos->name = strdup(ev_name);
1161 }
1162
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001163 return perf_evsel_menu__run(&menu, evlist->nr_entries, help, timer,
1164 arg, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001165}
1166
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001167int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
1168 void(*timer)(void *arg), void *arg,
1169 int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001170{
1171
1172 if (evlist->nr_entries == 1) {
1173 struct perf_evsel *first = list_entry(evlist->entries.next,
1174 struct perf_evsel, node);
1175 const char *ev_name = event_name(first);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001176 return perf_evsel__hists_browse(first, evlist->nr_entries, help,
1177 ev_name, false, timer, arg,
1178 delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001179 }
1180
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001181 return __perf_evlist__tui_browse_hists(evlist, help,
1182 timer, arg, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001183}