blob: e64d9527f14ed45608a388872884d8c60bb841e2 [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 Melobe83f5e2011-10-06 10:56:05 -0300335 /*
336 * The timer may have changed the number of entries.
337 * XXX: Find better way to keep this in synch, probably
338 * removing this timer function altogether and just sync
339 * using the hists->lock...
340 */
341 self->b.nr_entries = self->hists->nr_entries;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300342 hists__browser_title(self->hists, title, sizeof(title),
343 ev_name, self->dso_filter,
344 self->thread_filter);
345 ui_browser__show_title(&self->b, title);
346 continue;
Arnaldo Carvalho de Melo46941532010-08-10 15:50:07 -0300347 case 'D': { /* Debug */
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300348 static int seq;
349 struct hist_entry *h = rb_entry(self->b.top,
350 struct hist_entry, rb_node);
351 ui_helpline__pop();
352 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
353 seq++, self->b.nr_entries,
354 self->hists->nr_entries,
355 self->b.height,
356 self->b.index,
357 self->b.top_idx,
358 h->row_offset, h->nr_rows);
359 }
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300360 break;
361 case 'C':
362 /* Collapse the whole world. */
363 hist_browser__set_folding(self, false);
364 break;
365 case 'E':
366 /* Expand the whole world. */
367 hist_browser__set_folding(self, true);
368 break;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300369 case NEWT_KEY_ENTER:
370 if (hist_browser__toggle_fold(self))
371 break;
372 /* fall thru */
373 default:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300374 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300375 }
376 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300377out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300378 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300379 return key;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300380}
381
382static char *callchain_list__sym_name(struct callchain_list *self,
383 char *bf, size_t bfsize)
384{
385 if (self->ms.sym)
386 return self->ms.sym->name;
387
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200388 snprintf(bf, bfsize, "%#" PRIx64, self->ip);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300389 return bf;
390}
391
392#define LEVEL_OFFSET_STEP 3
393
394static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
395 struct callchain_node *chain_node,
396 u64 total, int level,
397 unsigned short row,
398 off_t *row_offset,
399 bool *is_current_entry)
400{
401 struct rb_node *node;
402 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
403 u64 new_total, remaining;
404
405 if (callchain_param.mode == CHAIN_GRAPH_REL)
406 new_total = chain_node->children_hit;
407 else
408 new_total = total;
409
410 remaining = new_total;
411 node = rb_first(&chain_node->rb_root);
412 while (node) {
413 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
414 struct rb_node *next = rb_next(node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100415 u64 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300416 struct callchain_list *chain;
417 char folded_sign = ' ';
418 int first = true;
419 int extra_offset = 0;
420
421 remaining -= cumul;
422
423 list_for_each_entry(chain, &child->val, list) {
424 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
425 const char *str;
426 int color;
427 bool was_first = first;
428
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300429 if (first)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300430 first = false;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300431 else
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300432 extra_offset = LEVEL_OFFSET_STEP;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300433
434 folded_sign = callchain_list__folded(chain);
435 if (*row_offset != 0) {
436 --*row_offset;
437 goto do_next;
438 }
439
440 alloc_str = NULL;
441 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
442 if (was_first) {
443 double percent = cumul * 100.0 / new_total;
444
445 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
446 str = "Not enough memory!";
447 else
448 str = alloc_str;
449 }
450
451 color = HE_COLORSET_NORMAL;
452 width = self->b.width - (offset + extra_offset + 2);
453 if (ui_browser__is_current_entry(&self->b, row)) {
454 self->selection = &chain->ms;
455 color = HE_COLORSET_SELECTED;
456 *is_current_entry = true;
457 }
458
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300459 ui_browser__set_color(&self->b, color);
460 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300461 slsmg_write_nstring(" ", offset + extra_offset);
462 slsmg_printf("%c ", folded_sign);
463 slsmg_write_nstring(str, width);
464 free(alloc_str);
465
466 if (++row == self->b.height)
467 goto out;
468do_next:
469 if (folded_sign == '+')
470 break;
471 }
472
473 if (folded_sign == '-') {
474 const int new_level = level + (extra_offset ? 2 : 1);
475 row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
476 new_level, row, row_offset,
477 is_current_entry);
478 }
479 if (row == self->b.height)
480 goto out;
481 node = next;
482 }
483out:
484 return row - first_row;
485}
486
487static int hist_browser__show_callchain_node(struct hist_browser *self,
488 struct callchain_node *node,
489 int level, unsigned short row,
490 off_t *row_offset,
491 bool *is_current_entry)
492{
493 struct callchain_list *chain;
494 int first_row = row,
495 offset = level * LEVEL_OFFSET_STEP,
496 width = self->b.width - offset;
497 char folded_sign = ' ';
498
499 list_for_each_entry(chain, &node->val, list) {
500 char ipstr[BITS_PER_LONG / 4 + 1], *s;
501 int color;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300502
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300503 folded_sign = callchain_list__folded(chain);
504
505 if (*row_offset != 0) {
506 --*row_offset;
507 continue;
508 }
509
510 color = HE_COLORSET_NORMAL;
511 if (ui_browser__is_current_entry(&self->b, row)) {
512 self->selection = &chain->ms;
513 color = HE_COLORSET_SELECTED;
514 *is_current_entry = true;
515 }
516
517 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300518 ui_browser__gotorc(&self->b, row, 0);
519 ui_browser__set_color(&self->b, color);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300520 slsmg_write_nstring(" ", offset);
521 slsmg_printf("%c ", folded_sign);
522 slsmg_write_nstring(s, width - 2);
523
524 if (++row == self->b.height)
525 goto out;
526 }
527
528 if (folded_sign == '-')
529 row += hist_browser__show_callchain_node_rb_tree(self, node,
530 self->hists->stats.total_period,
531 level + 1, row,
532 row_offset,
533 is_current_entry);
534out:
535 return row - first_row;
536}
537
538static int hist_browser__show_callchain(struct hist_browser *self,
539 struct rb_root *chain,
540 int level, unsigned short row,
541 off_t *row_offset,
542 bool *is_current_entry)
543{
544 struct rb_node *nd;
545 int first_row = row;
546
547 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
548 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
549
550 row += hist_browser__show_callchain_node(self, node, level,
551 row, row_offset,
552 is_current_entry);
553 if (row == self->b.height)
554 break;
555 }
556
557 return row - first_row;
558}
559
560static int hist_browser__show_entry(struct hist_browser *self,
561 struct hist_entry *entry,
562 unsigned short row)
563{
564 char s[256];
565 double percent;
566 int printed = 0;
567 int color, width = self->b.width;
568 char folded_sign = ' ';
569 bool current_entry = ui_browser__is_current_entry(&self->b, row);
570 off_t row_offset = entry->row_offset;
571
572 if (current_entry) {
573 self->he_selection = entry;
574 self->selection = &entry->ms;
575 }
576
577 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300578 hist_entry__init_have_children(entry);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300579 folded_sign = hist_entry__folded(entry);
580 }
581
582 if (row_offset == 0) {
583 hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false,
584 0, false, self->hists->stats.total_period);
585 percent = (entry->period * 100.0) / self->hists->stats.total_period;
586
587 color = HE_COLORSET_SELECTED;
588 if (!current_entry) {
589 if (percent >= MIN_RED)
590 color = HE_COLORSET_TOP;
591 else if (percent >= MIN_GREEN)
592 color = HE_COLORSET_MEDIUM;
593 else
594 color = HE_COLORSET_NORMAL;
595 }
596
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300597 ui_browser__set_color(&self->b, color);
598 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300599 if (symbol_conf.use_callchain) {
600 slsmg_printf("%c ", folded_sign);
601 width -= 2;
602 }
603 slsmg_write_nstring(s, width);
604 ++row;
605 ++printed;
606 } else
607 --row_offset;
608
609 if (folded_sign == '-' && row != self->b.height) {
610 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
611 1, row, &row_offset,
612 &current_entry);
613 if (current_entry)
614 self->he_selection = entry;
615 }
616
617 return printed;
618}
619
620static unsigned int hist_browser__refresh(struct ui_browser *self)
621{
622 unsigned row = 0;
623 struct rb_node *nd;
624 struct hist_browser *hb = container_of(self, struct hist_browser, b);
625
626 if (self->top == NULL)
627 self->top = rb_first(&hb->hists->entries);
628
629 for (nd = self->top; nd; nd = rb_next(nd)) {
630 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
631
632 if (h->filtered)
633 continue;
634
635 row += hist_browser__show_entry(hb, h, row);
636 if (row == self->height)
637 break;
638 }
639
640 return row;
641}
642
643static struct rb_node *hists__filter_entries(struct rb_node *nd)
644{
645 while (nd != NULL) {
646 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
647 if (!h->filtered)
648 return nd;
649
650 nd = rb_next(nd);
651 }
652
653 return NULL;
654}
655
656static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
657{
658 while (nd != NULL) {
659 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
660 if (!h->filtered)
661 return nd;
662
663 nd = rb_prev(nd);
664 }
665
666 return NULL;
667}
668
669static void ui_browser__hists_seek(struct ui_browser *self,
670 off_t offset, int whence)
671{
672 struct hist_entry *h;
673 struct rb_node *nd;
674 bool first = true;
675
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300676 if (self->nr_entries == 0)
677 return;
678
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300679 switch (whence) {
680 case SEEK_SET:
681 nd = hists__filter_entries(rb_first(self->entries));
682 break;
683 case SEEK_CUR:
684 nd = self->top;
685 goto do_offset;
686 case SEEK_END:
687 nd = hists__filter_prev_entries(rb_last(self->entries));
688 first = false;
689 break;
690 default:
691 return;
692 }
693
694 /*
695 * Moves not relative to the first visible entry invalidates its
696 * row_offset:
697 */
698 h = rb_entry(self->top, struct hist_entry, rb_node);
699 h->row_offset = 0;
700
701 /*
702 * Here we have to check if nd is expanded (+), if it is we can't go
703 * the next top level hist_entry, instead we must compute an offset of
704 * what _not_ to show and not change the first visible entry.
705 *
706 * This offset increments when we are going from top to bottom and
707 * decreases when we're going from bottom to top.
708 *
709 * As we don't have backpointers to the top level in the callchains
710 * structure, we need to always print the whole hist_entry callchain,
711 * skipping the first ones that are before the first visible entry
712 * and stop when we printed enough lines to fill the screen.
713 */
714do_offset:
715 if (offset > 0) {
716 do {
717 h = rb_entry(nd, struct hist_entry, rb_node);
718 if (h->ms.unfolded) {
719 u16 remaining = h->nr_rows - h->row_offset;
720 if (offset > remaining) {
721 offset -= remaining;
722 h->row_offset = 0;
723 } else {
724 h->row_offset += offset;
725 offset = 0;
726 self->top = nd;
727 break;
728 }
729 }
730 nd = hists__filter_entries(rb_next(nd));
731 if (nd == NULL)
732 break;
733 --offset;
734 self->top = nd;
735 } while (offset != 0);
736 } else if (offset < 0) {
737 while (1) {
738 h = rb_entry(nd, struct hist_entry, rb_node);
739 if (h->ms.unfolded) {
740 if (first) {
741 if (-offset > h->row_offset) {
742 offset += h->row_offset;
743 h->row_offset = 0;
744 } else {
745 h->row_offset += offset;
746 offset = 0;
747 self->top = nd;
748 break;
749 }
750 } else {
751 if (-offset > h->nr_rows) {
752 offset += h->nr_rows;
753 h->row_offset = 0;
754 } else {
755 h->row_offset = h->nr_rows + offset;
756 offset = 0;
757 self->top = nd;
758 break;
759 }
760 }
761 }
762
763 nd = hists__filter_prev_entries(rb_prev(nd));
764 if (nd == NULL)
765 break;
766 ++offset;
767 self->top = nd;
768 if (offset == 0) {
769 /*
770 * Last unfiltered hist_entry, check if it is
771 * unfolded, if it is then we should have
772 * row_offset at its last entry.
773 */
774 h = rb_entry(nd, struct hist_entry, rb_node);
775 if (h->ms.unfolded)
776 h->row_offset = h->nr_rows;
777 break;
778 }
779 first = false;
780 }
781 } else {
782 self->top = nd;
783 h = rb_entry(nd, struct hist_entry, rb_node);
784 h->row_offset = 0;
785 }
786}
787
788static struct hist_browser *hist_browser__new(struct hists *hists)
789{
790 struct hist_browser *self = zalloc(sizeof(*self));
791
792 if (self) {
793 self->hists = hists;
794 self->b.refresh = hist_browser__refresh;
795 self->b.seek = ui_browser__hists_seek;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300796 self->has_symbols = sort_sym.list.next != NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300797 }
798
799 return self;
800}
801
802static void hist_browser__delete(struct hist_browser *self)
803{
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300804 free(self);
805}
806
807static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
808{
809 return self->he_selection;
810}
811
812static struct thread *hist_browser__selected_thread(struct hist_browser *self)
813{
814 return self->he_selection->thread;
815}
816
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300817static int hists__browser_title(struct hists *self, char *bf, size_t size,
818 const char *ev_name, const struct dso *dso,
819 const struct thread *thread)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300820{
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300821 char unit;
822 int printed;
823 unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE];
824
825 nr_events = convert_unit(nr_events, &unit);
826 printed = snprintf(bf, size, "Events: %lu%c %s", nr_events, unit, ev_name);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300827
828 if (thread)
829 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300830 ", Thread: %s(%d)",
831 (thread->comm_set ? thread->comm : ""),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300832 thread->pid);
833 if (dso)
834 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300835 ", DSO: %s", dso->short_name);
836 return printed;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300837}
838
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300839static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300840 const char *helpline, const char *ev_name,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300841 bool left_exits,
842 void(*timer)(void *arg), void *arg,
843 int delay_secs)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300844{
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300845 struct hists *self = &evsel->hists;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300846 struct hist_browser *browser = hist_browser__new(self);
847 struct pstack *fstack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300848 int key = -1;
849
850 if (browser == NULL)
851 return -1;
852
853 fstack = pstack__new(2);
854 if (fstack == NULL)
855 goto out;
856
857 ui_helpline__push(helpline);
858
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300859 while (1) {
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300860 const struct thread *thread = NULL;
861 const struct dso *dso = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300862 char *options[16];
863 int nr_options = 0, choice = 0, i,
864 annotate = -2, zoom_dso = -2, zoom_thread = -2,
865 browse_map = -2;
866
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300867 key = hist_browser__run(browser, ev_name, timer, arg, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300868
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300869 if (browser->he_selection != NULL) {
870 thread = hist_browser__selected_thread(browser);
871 dso = browser->selection->map ? browser->selection->map->dso : NULL;
872 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300873
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300874 switch (key) {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300875 case NEWT_KEY_TAB:
876 case NEWT_KEY_UNTAB:
877 /*
878 * Exit the browser, let hists__browser_tree
879 * go to the next or previous
880 */
881 goto out_free_stack;
882 case 'a':
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300883 if (browser->selection == NULL ||
Lin Mingdb9a9cb2011-04-08 14:31:26 +0800884 browser->selection->sym == NULL ||
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300885 browser->selection->map->dso->annotate_warned)
886 continue;
887 goto do_annotate;
888 case 'd':
889 goto zoom_dso;
890 case 't':
891 goto zoom_thread;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300892 case NEWT_KEY_F1:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300893 case 'h':
894 case '?':
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300895 ui__help_window("h/?/F1 Show this window\n"
896 "TAB/UNTAB Switch events\n"
897 "q/CTRL+C Exit browser\n\n"
898 "For symbolic views (--sort has sym):\n\n"
899 "-> Zoom into DSO/Threads & Annotate current symbol\n"
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300900 "<- Zoom out\n"
901 "a Annotate current symbol\n"
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300902 "C Collapse all callchains\n"
903 "E Expand all callchains\n"
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300904 "d Zoom into current DSO\n"
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300905 "t Zoom into current Thread\n");
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300906 continue;
907 case NEWT_KEY_ENTER:
908 case NEWT_KEY_RIGHT:
909 /* menu */
910 break;
911 case NEWT_KEY_LEFT: {
912 const void *top;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300913
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300914 if (pstack__empty(fstack)) {
915 /*
916 * Go back to the perf_evsel_menu__run or other user
917 */
918 if (left_exits)
919 goto out_free_stack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300920 continue;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300921 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300922 top = pstack__pop(fstack);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300923 if (top == &browser->dso_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300924 goto zoom_out_dso;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300925 if (top == &browser->thread_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300926 goto zoom_out_thread;
927 continue;
928 }
929 case NEWT_KEY_ESCAPE:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300930 if (!left_exits &&
931 !ui__dialog_yesno("Do you really want to exit?"))
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300932 continue;
933 /* Fall thru */
934 default:
935 goto out_free_stack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300936 }
937
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300938 if (!browser->has_symbols)
939 goto add_exit_option;
940
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300941 if (browser->selection != NULL &&
942 browser->selection->sym != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300943 !browser->selection->map->dso->annotate_warned &&
944 asprintf(&options[nr_options], "Annotate %s",
945 browser->selection->sym->name) > 0)
946 annotate = nr_options++;
947
948 if (thread != NULL &&
949 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300950 (browser->thread_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300951 (thread->comm_set ? thread->comm : ""),
952 thread->pid) > 0)
953 zoom_thread = nr_options++;
954
955 if (dso != NULL &&
956 asprintf(&options[nr_options], "Zoom %s %s DSO",
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300957 (browser->dso_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300958 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
959 zoom_dso = nr_options++;
960
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300961 if (browser->selection != NULL &&
962 browser->selection->map != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300963 asprintf(&options[nr_options], "Browse map details") > 0)
964 browse_map = nr_options++;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300965add_exit_option:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300966 options[nr_options++] = (char *)"Exit";
967
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300968 choice = ui__popup_menu(nr_options, options);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300969
970 for (i = 0; i < nr_options - 1; ++i)
971 free(options[i]);
972
973 if (choice == nr_options - 1)
974 break;
975
976 if (choice == -1)
977 continue;
978
979 if (choice == annotate) {
980 struct hist_entry *he;
981do_annotate:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300982 he = hist_browser__selected_entry(browser);
983 if (he == NULL)
984 continue;
985
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300986 hist_entry__tui_annotate(he, evsel->idx, nr_events,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300987 timer, arg, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300988 } else if (choice == browse_map)
989 map__browse(browser->selection->map);
990 else if (choice == zoom_dso) {
991zoom_dso:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300992 if (browser->dso_filter) {
993 pstack__remove(fstack, &browser->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300994zoom_out_dso:
995 ui_helpline__pop();
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300996 browser->dso_filter = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300997 } else {
998 if (dso == NULL)
999 continue;
1000 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
1001 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001002 browser->dso_filter = dso;
1003 pstack__push(fstack, &browser->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001004 }
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001005 hists__filter_by_dso(self, browser->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001006 hist_browser__reset(browser);
1007 } else if (choice == zoom_thread) {
1008zoom_thread:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001009 if (browser->thread_filter) {
1010 pstack__remove(fstack, &browser->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001011zoom_out_thread:
1012 ui_helpline__pop();
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001013 browser->thread_filter = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001014 } else {
1015 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
1016 thread->comm_set ? thread->comm : "",
1017 thread->pid);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001018 browser->thread_filter = thread;
1019 pstack__push(fstack, &browser->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001020 }
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001021 hists__filter_by_thread(self, browser->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001022 hist_browser__reset(browser);
1023 }
1024 }
1025out_free_stack:
1026 pstack__delete(fstack);
1027out:
1028 hist_browser__delete(browser);
1029 return key;
1030}
1031
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001032struct perf_evsel_menu {
1033 struct ui_browser b;
1034 struct perf_evsel *selection;
1035};
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001036
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001037static void perf_evsel_menu__write(struct ui_browser *browser,
1038 void *entry, int row)
1039{
1040 struct perf_evsel_menu *menu = container_of(browser,
1041 struct perf_evsel_menu, b);
1042 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
1043 bool current_entry = ui_browser__is_current_entry(browser, row);
1044 unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1045 const char *ev_name = event_name(evsel);
1046 char bf[256], unit;
1047
1048 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
1049 HE_COLORSET_NORMAL);
1050
1051 nr_events = convert_unit(nr_events, &unit);
1052 snprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
1053 unit, unit == ' ' ? "" : " ", ev_name);
1054 slsmg_write_nstring(bf, browser->width);
1055
1056 if (current_entry)
1057 menu->selection = evsel;
1058}
1059
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001060static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
1061 int nr_events, const char *help,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001062 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001063{
1064 int exit_keys[] = { NEWT_KEY_ENTER, NEWT_KEY_RIGHT, 0, };
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001065 int delay_msecs = delay_secs * 1000;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001066 struct perf_evlist *evlist = menu->b.priv;
1067 struct perf_evsel *pos;
1068 const char *ev_name, *title = "Available samples";
1069 int key;
1070
1071 if (ui_browser__show(&menu->b, title,
1072 "ESC: exit, ENTER|->: Browse histograms") < 0)
1073 return -1;
1074
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001075 if (timer != NULL)
1076 newtFormSetTimer(menu->b.form, delay_msecs);
1077
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001078 ui_browser__add_exit_keys(&menu->b, exit_keys);
1079
1080 while (1) {
1081 key = ui_browser__run(&menu->b);
1082
1083 switch (key) {
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001084 case -1:
1085 /* FIXME we need to check if it was es.reason == NEWT_EXIT_TIMER */
1086 timer(arg);
1087 continue;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001088 case NEWT_KEY_RIGHT:
1089 case NEWT_KEY_ENTER:
1090 if (!menu->selection)
1091 continue;
1092 pos = menu->selection;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001093 perf_evlist__set_selected(evlist, pos);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001094browse_hists:
1095 ev_name = event_name(pos);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001096 key = perf_evsel__hists_browse(pos, nr_events, help,
1097 ev_name, true, timer,
1098 arg, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001099 ui_browser__show_title(&menu->b, title);
1100 break;
1101 case NEWT_KEY_LEFT:
1102 continue;
1103 case NEWT_KEY_ESCAPE:
1104 if (!ui__dialog_yesno("Do you really want to exit?"))
1105 continue;
1106 /* Fall thru */
1107 default:
1108 goto out;
1109 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001110
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001111 switch (key) {
1112 case NEWT_KEY_TAB:
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001113 if (pos->node.next == &evlist->entries)
1114 pos = list_entry(evlist->entries.next, struct perf_evsel, node);
1115 else
1116 pos = list_entry(pos->node.next, struct perf_evsel, node);
Arnaldo Carvalho de Melo7d163202011-10-06 10:46:45 -03001117 perf_evlist__set_selected(evlist, pos);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001118 goto browse_hists;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001119 case NEWT_KEY_UNTAB:
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03001120 if (pos->node.prev == &evlist->entries)
1121 pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
1122 else
1123 pos = list_entry(pos->node.prev, struct perf_evsel, node);
Arnaldo Carvalho de Melo7d163202011-10-06 10:46:45 -03001124 perf_evlist__set_selected(evlist, pos);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001125 goto browse_hists;
1126 case 'q':
1127 case CTRL('c'):
1128 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001129 default:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001130 break;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001131 }
1132 }
1133
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001134out:
1135 ui_browser__hide(&menu->b);
1136 return key;
1137}
1138
1139static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001140 const char *help,
1141 void(*timer)(void *arg), void *arg,
1142 int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001143{
1144 struct perf_evsel *pos;
1145 struct perf_evsel_menu menu = {
1146 .b = {
1147 .entries = &evlist->entries,
1148 .refresh = ui_browser__list_head_refresh,
1149 .seek = ui_browser__list_head_seek,
1150 .write = perf_evsel_menu__write,
1151 .nr_entries = evlist->nr_entries,
1152 .priv = evlist,
1153 },
1154 };
1155
1156 ui_helpline__push("Press ESC to exit");
1157
1158 list_for_each_entry(pos, &evlist->entries, node) {
1159 const char *ev_name = event_name(pos);
1160 size_t line_len = strlen(ev_name) + 7;
1161
1162 if (menu.b.width < line_len)
1163 menu.b.width = line_len;
1164 /*
1165 * Cache the evsel name, tracepoints have a _high_ cost per
1166 * event_name() call.
1167 */
1168 if (pos->name == NULL)
1169 pos->name = strdup(ev_name);
1170 }
1171
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001172 return perf_evsel_menu__run(&menu, evlist->nr_entries, help, timer,
1173 arg, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001174}
1175
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001176int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
1177 void(*timer)(void *arg), void *arg,
1178 int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001179{
1180
1181 if (evlist->nr_entries == 1) {
1182 struct perf_evsel *first = list_entry(evlist->entries.next,
1183 struct perf_evsel, node);
1184 const char *ev_name = event_name(first);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001185 return perf_evsel__hists_browse(first, evlist->nr_entries, help,
1186 ev_name, false, timer, arg,
1187 delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001188 }
1189
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001190 return __perf_evlist__tui_browse_hists(evlist, help,
1191 timer, arg, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001192}