blob: b7901099eedb326a66d186f124fd4ab398f2ad7f [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);
335 hists__browser_title(self->hists, title, sizeof(title),
336 ev_name, self->dso_filter,
337 self->thread_filter);
338 ui_browser__show_title(&self->b, title);
339 continue;
Arnaldo Carvalho de Melo46941532010-08-10 15:50:07 -0300340 case 'D': { /* Debug */
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300341 static int seq;
342 struct hist_entry *h = rb_entry(self->b.top,
343 struct hist_entry, rb_node);
344 ui_helpline__pop();
345 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
346 seq++, self->b.nr_entries,
347 self->hists->nr_entries,
348 self->b.height,
349 self->b.index,
350 self->b.top_idx,
351 h->row_offset, h->nr_rows);
352 }
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300353 break;
354 case 'C':
355 /* Collapse the whole world. */
356 hist_browser__set_folding(self, false);
357 break;
358 case 'E':
359 /* Expand the whole world. */
360 hist_browser__set_folding(self, true);
361 break;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300362 case NEWT_KEY_ENTER:
363 if (hist_browser__toggle_fold(self))
364 break;
365 /* fall thru */
366 default:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300367 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300368 }
369 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300370out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300371 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300372 return key;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300373}
374
375static char *callchain_list__sym_name(struct callchain_list *self,
376 char *bf, size_t bfsize)
377{
378 if (self->ms.sym)
379 return self->ms.sym->name;
380
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200381 snprintf(bf, bfsize, "%#" PRIx64, self->ip);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300382 return bf;
383}
384
385#define LEVEL_OFFSET_STEP 3
386
387static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
388 struct callchain_node *chain_node,
389 u64 total, int level,
390 unsigned short row,
391 off_t *row_offset,
392 bool *is_current_entry)
393{
394 struct rb_node *node;
395 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
396 u64 new_total, remaining;
397
398 if (callchain_param.mode == CHAIN_GRAPH_REL)
399 new_total = chain_node->children_hit;
400 else
401 new_total = total;
402
403 remaining = new_total;
404 node = rb_first(&chain_node->rb_root);
405 while (node) {
406 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
407 struct rb_node *next = rb_next(node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100408 u64 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300409 struct callchain_list *chain;
410 char folded_sign = ' ';
411 int first = true;
412 int extra_offset = 0;
413
414 remaining -= cumul;
415
416 list_for_each_entry(chain, &child->val, list) {
417 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
418 const char *str;
419 int color;
420 bool was_first = first;
421
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300422 if (first)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300423 first = false;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300424 else
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300425 extra_offset = LEVEL_OFFSET_STEP;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300426
427 folded_sign = callchain_list__folded(chain);
428 if (*row_offset != 0) {
429 --*row_offset;
430 goto do_next;
431 }
432
433 alloc_str = NULL;
434 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
435 if (was_first) {
436 double percent = cumul * 100.0 / new_total;
437
438 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
439 str = "Not enough memory!";
440 else
441 str = alloc_str;
442 }
443
444 color = HE_COLORSET_NORMAL;
445 width = self->b.width - (offset + extra_offset + 2);
446 if (ui_browser__is_current_entry(&self->b, row)) {
447 self->selection = &chain->ms;
448 color = HE_COLORSET_SELECTED;
449 *is_current_entry = true;
450 }
451
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300452 ui_browser__set_color(&self->b, color);
453 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300454 slsmg_write_nstring(" ", offset + extra_offset);
455 slsmg_printf("%c ", folded_sign);
456 slsmg_write_nstring(str, width);
457 free(alloc_str);
458
459 if (++row == self->b.height)
460 goto out;
461do_next:
462 if (folded_sign == '+')
463 break;
464 }
465
466 if (folded_sign == '-') {
467 const int new_level = level + (extra_offset ? 2 : 1);
468 row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
469 new_level, row, row_offset,
470 is_current_entry);
471 }
472 if (row == self->b.height)
473 goto out;
474 node = next;
475 }
476out:
477 return row - first_row;
478}
479
480static int hist_browser__show_callchain_node(struct hist_browser *self,
481 struct callchain_node *node,
482 int level, unsigned short row,
483 off_t *row_offset,
484 bool *is_current_entry)
485{
486 struct callchain_list *chain;
487 int first_row = row,
488 offset = level * LEVEL_OFFSET_STEP,
489 width = self->b.width - offset;
490 char folded_sign = ' ';
491
492 list_for_each_entry(chain, &node->val, list) {
493 char ipstr[BITS_PER_LONG / 4 + 1], *s;
494 int color;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300495
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300496 folded_sign = callchain_list__folded(chain);
497
498 if (*row_offset != 0) {
499 --*row_offset;
500 continue;
501 }
502
503 color = HE_COLORSET_NORMAL;
504 if (ui_browser__is_current_entry(&self->b, row)) {
505 self->selection = &chain->ms;
506 color = HE_COLORSET_SELECTED;
507 *is_current_entry = true;
508 }
509
510 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300511 ui_browser__gotorc(&self->b, row, 0);
512 ui_browser__set_color(&self->b, color);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300513 slsmg_write_nstring(" ", offset);
514 slsmg_printf("%c ", folded_sign);
515 slsmg_write_nstring(s, width - 2);
516
517 if (++row == self->b.height)
518 goto out;
519 }
520
521 if (folded_sign == '-')
522 row += hist_browser__show_callchain_node_rb_tree(self, node,
523 self->hists->stats.total_period,
524 level + 1, row,
525 row_offset,
526 is_current_entry);
527out:
528 return row - first_row;
529}
530
531static int hist_browser__show_callchain(struct hist_browser *self,
532 struct rb_root *chain,
533 int level, unsigned short row,
534 off_t *row_offset,
535 bool *is_current_entry)
536{
537 struct rb_node *nd;
538 int first_row = row;
539
540 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
541 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
542
543 row += hist_browser__show_callchain_node(self, node, level,
544 row, row_offset,
545 is_current_entry);
546 if (row == self->b.height)
547 break;
548 }
549
550 return row - first_row;
551}
552
553static int hist_browser__show_entry(struct hist_browser *self,
554 struct hist_entry *entry,
555 unsigned short row)
556{
557 char s[256];
558 double percent;
559 int printed = 0;
560 int color, width = self->b.width;
561 char folded_sign = ' ';
562 bool current_entry = ui_browser__is_current_entry(&self->b, row);
563 off_t row_offset = entry->row_offset;
564
565 if (current_entry) {
566 self->he_selection = entry;
567 self->selection = &entry->ms;
568 }
569
570 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300571 hist_entry__init_have_children(entry);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300572 folded_sign = hist_entry__folded(entry);
573 }
574
575 if (row_offset == 0) {
576 hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false,
577 0, false, self->hists->stats.total_period);
578 percent = (entry->period * 100.0) / self->hists->stats.total_period;
579
580 color = HE_COLORSET_SELECTED;
581 if (!current_entry) {
582 if (percent >= MIN_RED)
583 color = HE_COLORSET_TOP;
584 else if (percent >= MIN_GREEN)
585 color = HE_COLORSET_MEDIUM;
586 else
587 color = HE_COLORSET_NORMAL;
588 }
589
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300590 ui_browser__set_color(&self->b, color);
591 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300592 if (symbol_conf.use_callchain) {
593 slsmg_printf("%c ", folded_sign);
594 width -= 2;
595 }
596 slsmg_write_nstring(s, width);
597 ++row;
598 ++printed;
599 } else
600 --row_offset;
601
602 if (folded_sign == '-' && row != self->b.height) {
603 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
604 1, row, &row_offset,
605 &current_entry);
606 if (current_entry)
607 self->he_selection = entry;
608 }
609
610 return printed;
611}
612
613static unsigned int hist_browser__refresh(struct ui_browser *self)
614{
615 unsigned row = 0;
616 struct rb_node *nd;
617 struct hist_browser *hb = container_of(self, struct hist_browser, b);
618
619 if (self->top == NULL)
620 self->top = rb_first(&hb->hists->entries);
621
622 for (nd = self->top; nd; nd = rb_next(nd)) {
623 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
624
625 if (h->filtered)
626 continue;
627
628 row += hist_browser__show_entry(hb, h, row);
629 if (row == self->height)
630 break;
631 }
632
633 return row;
634}
635
636static struct rb_node *hists__filter_entries(struct rb_node *nd)
637{
638 while (nd != NULL) {
639 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
640 if (!h->filtered)
641 return nd;
642
643 nd = rb_next(nd);
644 }
645
646 return NULL;
647}
648
649static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
650{
651 while (nd != NULL) {
652 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
653 if (!h->filtered)
654 return nd;
655
656 nd = rb_prev(nd);
657 }
658
659 return NULL;
660}
661
662static void ui_browser__hists_seek(struct ui_browser *self,
663 off_t offset, int whence)
664{
665 struct hist_entry *h;
666 struct rb_node *nd;
667 bool first = true;
668
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300669 if (self->nr_entries == 0)
670 return;
671
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300672 switch (whence) {
673 case SEEK_SET:
674 nd = hists__filter_entries(rb_first(self->entries));
675 break;
676 case SEEK_CUR:
677 nd = self->top;
678 goto do_offset;
679 case SEEK_END:
680 nd = hists__filter_prev_entries(rb_last(self->entries));
681 first = false;
682 break;
683 default:
684 return;
685 }
686
687 /*
688 * Moves not relative to the first visible entry invalidates its
689 * row_offset:
690 */
691 h = rb_entry(self->top, struct hist_entry, rb_node);
692 h->row_offset = 0;
693
694 /*
695 * Here we have to check if nd is expanded (+), if it is we can't go
696 * the next top level hist_entry, instead we must compute an offset of
697 * what _not_ to show and not change the first visible entry.
698 *
699 * This offset increments when we are going from top to bottom and
700 * decreases when we're going from bottom to top.
701 *
702 * As we don't have backpointers to the top level in the callchains
703 * structure, we need to always print the whole hist_entry callchain,
704 * skipping the first ones that are before the first visible entry
705 * and stop when we printed enough lines to fill the screen.
706 */
707do_offset:
708 if (offset > 0) {
709 do {
710 h = rb_entry(nd, struct hist_entry, rb_node);
711 if (h->ms.unfolded) {
712 u16 remaining = h->nr_rows - h->row_offset;
713 if (offset > remaining) {
714 offset -= remaining;
715 h->row_offset = 0;
716 } else {
717 h->row_offset += offset;
718 offset = 0;
719 self->top = nd;
720 break;
721 }
722 }
723 nd = hists__filter_entries(rb_next(nd));
724 if (nd == NULL)
725 break;
726 --offset;
727 self->top = nd;
728 } while (offset != 0);
729 } else if (offset < 0) {
730 while (1) {
731 h = rb_entry(nd, struct hist_entry, rb_node);
732 if (h->ms.unfolded) {
733 if (first) {
734 if (-offset > h->row_offset) {
735 offset += h->row_offset;
736 h->row_offset = 0;
737 } else {
738 h->row_offset += offset;
739 offset = 0;
740 self->top = nd;
741 break;
742 }
743 } else {
744 if (-offset > h->nr_rows) {
745 offset += h->nr_rows;
746 h->row_offset = 0;
747 } else {
748 h->row_offset = h->nr_rows + offset;
749 offset = 0;
750 self->top = nd;
751 break;
752 }
753 }
754 }
755
756 nd = hists__filter_prev_entries(rb_prev(nd));
757 if (nd == NULL)
758 break;
759 ++offset;
760 self->top = nd;
761 if (offset == 0) {
762 /*
763 * Last unfiltered hist_entry, check if it is
764 * unfolded, if it is then we should have
765 * row_offset at its last entry.
766 */
767 h = rb_entry(nd, struct hist_entry, rb_node);
768 if (h->ms.unfolded)
769 h->row_offset = h->nr_rows;
770 break;
771 }
772 first = false;
773 }
774 } else {
775 self->top = nd;
776 h = rb_entry(nd, struct hist_entry, rb_node);
777 h->row_offset = 0;
778 }
779}
780
781static struct hist_browser *hist_browser__new(struct hists *hists)
782{
783 struct hist_browser *self = zalloc(sizeof(*self));
784
785 if (self) {
786 self->hists = hists;
787 self->b.refresh = hist_browser__refresh;
788 self->b.seek = ui_browser__hists_seek;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300789 self->has_symbols = sort_sym.list.next != NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300790 }
791
792 return self;
793}
794
795static void hist_browser__delete(struct hist_browser *self)
796{
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300797 free(self);
798}
799
800static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
801{
802 return self->he_selection;
803}
804
805static struct thread *hist_browser__selected_thread(struct hist_browser *self)
806{
807 return self->he_selection->thread;
808}
809
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300810static int hists__browser_title(struct hists *self, char *bf, size_t size,
811 const char *ev_name, const struct dso *dso,
812 const struct thread *thread)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300813{
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300814 char unit;
815 int printed;
816 unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE];
817
818 nr_events = convert_unit(nr_events, &unit);
819 printed = snprintf(bf, size, "Events: %lu%c %s", nr_events, unit, ev_name);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300820
821 if (thread)
822 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300823 ", Thread: %s(%d)",
824 (thread->comm_set ? thread->comm : ""),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300825 thread->pid);
826 if (dso)
827 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300828 ", DSO: %s", dso->short_name);
829 return printed;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300830}
831
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300832static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300833 const char *helpline, const char *ev_name,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300834 bool left_exits,
835 void(*timer)(void *arg), void *arg,
836 int delay_secs)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300837{
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300838 struct hists *self = &evsel->hists;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300839 struct hist_browser *browser = hist_browser__new(self);
840 struct pstack *fstack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300841 int key = -1;
842
843 if (browser == NULL)
844 return -1;
845
846 fstack = pstack__new(2);
847 if (fstack == NULL)
848 goto out;
849
850 ui_helpline__push(helpline);
851
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300852 while (1) {
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300853 const struct thread *thread = NULL;
854 const struct dso *dso = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300855 char *options[16];
856 int nr_options = 0, choice = 0, i,
857 annotate = -2, zoom_dso = -2, zoom_thread = -2,
858 browse_map = -2;
859
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300860 key = hist_browser__run(browser, ev_name, timer, arg, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300861
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300862 if (browser->he_selection != NULL) {
863 thread = hist_browser__selected_thread(browser);
864 dso = browser->selection->map ? browser->selection->map->dso : NULL;
865 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300866
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300867 switch (key) {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300868 case NEWT_KEY_TAB:
869 case NEWT_KEY_UNTAB:
870 /*
871 * Exit the browser, let hists__browser_tree
872 * go to the next or previous
873 */
874 goto out_free_stack;
875 case 'a':
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300876 if (browser->selection == NULL ||
Lin Mingdb9a9cb2011-04-08 14:31:26 +0800877 browser->selection->sym == NULL ||
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300878 browser->selection->map->dso->annotate_warned)
879 continue;
880 goto do_annotate;
881 case 'd':
882 goto zoom_dso;
883 case 't':
884 goto zoom_thread;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300885 case NEWT_KEY_F1:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300886 case 'h':
887 case '?':
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300888 ui__help_window("h/?/F1 Show this window\n"
889 "TAB/UNTAB Switch events\n"
890 "q/CTRL+C Exit browser\n\n"
891 "For symbolic views (--sort has sym):\n\n"
892 "-> Zoom into DSO/Threads & Annotate current symbol\n"
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300893 "<- Zoom out\n"
894 "a Annotate current symbol\n"
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300895 "C Collapse all callchains\n"
896 "E Expand all callchains\n"
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300897 "d Zoom into current DSO\n"
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300898 "t Zoom into current Thread\n");
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300899 continue;
900 case NEWT_KEY_ENTER:
901 case NEWT_KEY_RIGHT:
902 /* menu */
903 break;
904 case NEWT_KEY_LEFT: {
905 const void *top;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300906
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300907 if (pstack__empty(fstack)) {
908 /*
909 * Go back to the perf_evsel_menu__run or other user
910 */
911 if (left_exits)
912 goto out_free_stack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300913 continue;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300914 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300915 top = pstack__pop(fstack);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300916 if (top == &browser->dso_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300917 goto zoom_out_dso;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300918 if (top == &browser->thread_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300919 goto zoom_out_thread;
920 continue;
921 }
922 case NEWT_KEY_ESCAPE:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300923 if (!left_exits &&
924 !ui__dialog_yesno("Do you really want to exit?"))
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300925 continue;
926 /* Fall thru */
927 default:
928 goto out_free_stack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300929 }
930
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300931 if (!browser->has_symbols)
932 goto add_exit_option;
933
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300934 if (browser->selection != NULL &&
935 browser->selection->sym != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300936 !browser->selection->map->dso->annotate_warned &&
937 asprintf(&options[nr_options], "Annotate %s",
938 browser->selection->sym->name) > 0)
939 annotate = nr_options++;
940
941 if (thread != NULL &&
942 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300943 (browser->thread_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300944 (thread->comm_set ? thread->comm : ""),
945 thread->pid) > 0)
946 zoom_thread = nr_options++;
947
948 if (dso != NULL &&
949 asprintf(&options[nr_options], "Zoom %s %s DSO",
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300950 (browser->dso_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300951 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
952 zoom_dso = nr_options++;
953
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300954 if (browser->selection != NULL &&
955 browser->selection->map != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300956 asprintf(&options[nr_options], "Browse map details") > 0)
957 browse_map = nr_options++;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300958add_exit_option:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300959 options[nr_options++] = (char *)"Exit";
960
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300961 choice = ui__popup_menu(nr_options, options);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300962
963 for (i = 0; i < nr_options - 1; ++i)
964 free(options[i]);
965
966 if (choice == nr_options - 1)
967 break;
968
969 if (choice == -1)
970 continue;
971
972 if (choice == annotate) {
973 struct hist_entry *he;
974do_annotate:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300975 he = hist_browser__selected_entry(browser);
976 if (he == NULL)
977 continue;
978
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 Melod1b4f242010-08-10 15:49:07 -0300981 } else if (choice == browse_map)
982 map__browse(browser->selection->map);
983 else if (choice == zoom_dso) {
984zoom_dso:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300985 if (browser->dso_filter) {
986 pstack__remove(fstack, &browser->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300987zoom_out_dso:
988 ui_helpline__pop();
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300989 browser->dso_filter = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300990 } else {
991 if (dso == NULL)
992 continue;
993 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
994 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300995 browser->dso_filter = dso;
996 pstack__push(fstack, &browser->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300997 }
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300998 hists__filter_by_dso(self, browser->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300999 hist_browser__reset(browser);
1000 } else if (choice == zoom_thread) {
1001zoom_thread:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001002 if (browser->thread_filter) {
1003 pstack__remove(fstack, &browser->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001004zoom_out_thread:
1005 ui_helpline__pop();
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001006 browser->thread_filter = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001007 } else {
1008 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
1009 thread->comm_set ? thread->comm : "",
1010 thread->pid);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001011 browser->thread_filter = thread;
1012 pstack__push(fstack, &browser->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001013 }
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001014 hists__filter_by_thread(self, browser->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001015 hist_browser__reset(browser);
1016 }
1017 }
1018out_free_stack:
1019 pstack__delete(fstack);
1020out:
1021 hist_browser__delete(browser);
1022 return key;
1023}
1024
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001025struct perf_evsel_menu {
1026 struct ui_browser b;
1027 struct perf_evsel *selection;
1028};
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001029
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001030static void perf_evsel_menu__write(struct ui_browser *browser,
1031 void *entry, int row)
1032{
1033 struct perf_evsel_menu *menu = container_of(browser,
1034 struct perf_evsel_menu, b);
1035 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
1036 bool current_entry = ui_browser__is_current_entry(browser, row);
1037 unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1038 const char *ev_name = event_name(evsel);
1039 char bf[256], unit;
1040
1041 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
1042 HE_COLORSET_NORMAL);
1043
1044 nr_events = convert_unit(nr_events, &unit);
1045 snprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
1046 unit, unit == ' ' ? "" : " ", ev_name);
1047 slsmg_write_nstring(bf, browser->width);
1048
1049 if (current_entry)
1050 menu->selection = evsel;
1051}
1052
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001053static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
1054 int nr_events, const char *help,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001055 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001056{
1057 int exit_keys[] = { NEWT_KEY_ENTER, NEWT_KEY_RIGHT, 0, };
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001058 int delay_msecs = delay_secs * 1000;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001059 struct perf_evlist *evlist = menu->b.priv;
1060 struct perf_evsel *pos;
1061 const char *ev_name, *title = "Available samples";
1062 int key;
1063
1064 if (ui_browser__show(&menu->b, title,
1065 "ESC: exit, ENTER|->: Browse histograms") < 0)
1066 return -1;
1067
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001068 if (timer != NULL)
1069 newtFormSetTimer(menu->b.form, delay_msecs);
1070
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001071 ui_browser__add_exit_keys(&menu->b, exit_keys);
1072
1073 while (1) {
1074 key = ui_browser__run(&menu->b);
1075
1076 switch (key) {
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001077 case -1:
1078 /* FIXME we need to check if it was es.reason == NEWT_EXIT_TIMER */
1079 timer(arg);
1080 continue;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001081 case NEWT_KEY_RIGHT:
1082 case NEWT_KEY_ENTER:
1083 if (!menu->selection)
1084 continue;
1085 pos = menu->selection;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001086 perf_evlist__set_selected(evlist, pos);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001087browse_hists:
1088 ev_name = event_name(pos);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001089 key = perf_evsel__hists_browse(pos, nr_events, help,
1090 ev_name, true, timer,
1091 arg, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001092 ui_browser__show_title(&menu->b, title);
1093 break;
1094 case NEWT_KEY_LEFT:
1095 continue;
1096 case NEWT_KEY_ESCAPE:
1097 if (!ui__dialog_yesno("Do you really want to exit?"))
1098 continue;
1099 /* Fall thru */
1100 default:
1101 goto out;
1102 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001103
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001104 switch (key) {
1105 case NEWT_KEY_TAB:
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001106 if (pos->node.next == &evlist->entries)
1107 pos = list_entry(evlist->entries.next, struct perf_evsel, node);
1108 else
1109 pos = list_entry(pos->node.next, struct perf_evsel, node);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001110 goto browse_hists;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001111 case NEWT_KEY_UNTAB:
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001112 if (pos->node.prev == &evlist->entries)
1113 pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
1114 else
1115 pos = list_entry(pos->node.prev, struct perf_evsel, node);
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}