blob: 94e1ab0badb1122ca23048dec6506a2544573c90 [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 Melo724c9c92011-10-06 10:36:35 -030027 bool has_symbols;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030028};
29
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -030030static int hists__browser_title(struct hists *self, char *bf, size_t size,
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -020031 const char *ev_name);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -030032
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030033static void hist_browser__refresh_dimensions(struct hist_browser *self)
34{
35 /* 3 == +/- toggle symbol before actual hist_entry rendering */
36 self->b.width = 3 + (hists__sort_list_width(self->hists) +
37 sizeof("[k]"));
38}
39
40static void hist_browser__reset(struct hist_browser *self)
41{
42 self->b.nr_entries = self->hists->nr_entries;
43 hist_browser__refresh_dimensions(self);
44 ui_browser__reset_index(&self->b);
45}
46
47static char tree__folded_sign(bool unfolded)
48{
49 return unfolded ? '-' : '+';
50}
51
52static char map_symbol__folded(const struct map_symbol *self)
53{
54 return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
55}
56
57static char hist_entry__folded(const struct hist_entry *self)
58{
59 return map_symbol__folded(&self->ms);
60}
61
62static char callchain_list__folded(const struct callchain_list *self)
63{
64 return map_symbol__folded(&self->ms);
65}
66
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -030067static void map_symbol__set_folding(struct map_symbol *self, bool unfold)
68{
69 self->unfolded = unfold ? self->has_children : false;
70}
71
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030072static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
73{
74 int n = 0;
75 struct rb_node *nd;
76
77 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
78 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
79 struct callchain_list *chain;
80 char folded_sign = ' '; /* No children */
81
82 list_for_each_entry(chain, &child->val, list) {
83 ++n;
84 /* We need this because we may not have children */
85 folded_sign = callchain_list__folded(chain);
86 if (folded_sign == '+')
87 break;
88 }
89
90 if (folded_sign == '-') /* Have children and they're unfolded */
91 n += callchain_node__count_rows_rb_tree(child);
92 }
93
94 return n;
95}
96
97static int callchain_node__count_rows(struct callchain_node *node)
98{
99 struct callchain_list *chain;
100 bool unfolded = false;
101 int n = 0;
102
103 list_for_each_entry(chain, &node->val, list) {
104 ++n;
105 unfolded = chain->ms.unfolded;
106 }
107
108 if (unfolded)
109 n += callchain_node__count_rows_rb_tree(node);
110
111 return n;
112}
113
114static int callchain__count_rows(struct rb_root *chain)
115{
116 struct rb_node *nd;
117 int n = 0;
118
119 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
120 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
121 n += callchain_node__count_rows(node);
122 }
123
124 return n;
125}
126
127static bool map_symbol__toggle_fold(struct map_symbol *self)
128{
129 if (!self->has_children)
130 return false;
131
132 self->unfolded = !self->unfolded;
133 return true;
134}
135
136static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
137{
138 struct rb_node *nd = rb_first(&self->rb_root);
139
140 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
141 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
142 struct callchain_list *chain;
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300143 bool first = true;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300144
145 list_for_each_entry(chain, &child->val, list) {
146 if (first) {
147 first = false;
148 chain->ms.has_children = chain->list.next != &child->val ||
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300149 !RB_EMPTY_ROOT(&child->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300150 } else
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 }
154
155 callchain_node__init_have_children_rb_tree(child);
156 }
157}
158
159static void callchain_node__init_have_children(struct callchain_node *self)
160{
161 struct callchain_list *chain;
162
163 list_for_each_entry(chain, &self->val, list)
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300164 chain->ms.has_children = !RB_EMPTY_ROOT(&self->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300165
166 callchain_node__init_have_children_rb_tree(self);
167}
168
169static void callchain__init_have_children(struct rb_root *self)
170{
171 struct rb_node *nd;
172
173 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
174 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
175 callchain_node__init_have_children(node);
176 }
177}
178
179static void hist_entry__init_have_children(struct hist_entry *self)
180{
181 if (!self->init_have_children) {
Arnaldo Carvalho de Melo18b308d2010-08-25 12:47:44 -0300182 self->ms.has_children = !RB_EMPTY_ROOT(&self->sorted_chain);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300183 callchain__init_have_children(&self->sorted_chain);
184 self->init_have_children = true;
185 }
186}
187
188static bool hist_browser__toggle_fold(struct hist_browser *self)
189{
190 if (map_symbol__toggle_fold(self->selection)) {
191 struct hist_entry *he = self->he_selection;
192
193 hist_entry__init_have_children(he);
194 self->hists->nr_entries -= he->nr_rows;
195
196 if (he->ms.unfolded)
197 he->nr_rows = callchain__count_rows(&he->sorted_chain);
198 else
199 he->nr_rows = 0;
200 self->hists->nr_entries += he->nr_rows;
201 self->b.nr_entries = self->hists->nr_entries;
202
203 return true;
204 }
205
206 /* If it doesn't have children, no toggling performed */
207 return false;
208}
209
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300210static int callchain_node__set_folding_rb_tree(struct callchain_node *self, bool unfold)
211{
212 int n = 0;
213 struct rb_node *nd;
214
215 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
216 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
217 struct callchain_list *chain;
218 bool has_children = false;
219
220 list_for_each_entry(chain, &child->val, list) {
221 ++n;
222 map_symbol__set_folding(&chain->ms, unfold);
223 has_children = chain->ms.has_children;
224 }
225
226 if (has_children)
227 n += callchain_node__set_folding_rb_tree(child, unfold);
228 }
229
230 return n;
231}
232
233static int callchain_node__set_folding(struct callchain_node *node, bool unfold)
234{
235 struct callchain_list *chain;
236 bool has_children = false;
237 int n = 0;
238
239 list_for_each_entry(chain, &node->val, list) {
240 ++n;
241 map_symbol__set_folding(&chain->ms, unfold);
242 has_children = chain->ms.has_children;
243 }
244
245 if (has_children)
246 n += callchain_node__set_folding_rb_tree(node, unfold);
247
248 return n;
249}
250
251static int callchain__set_folding(struct rb_root *chain, bool unfold)
252{
253 struct rb_node *nd;
254 int n = 0;
255
256 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
257 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
258 n += callchain_node__set_folding(node, unfold);
259 }
260
261 return n;
262}
263
264static void hist_entry__set_folding(struct hist_entry *self, bool unfold)
265{
266 hist_entry__init_have_children(self);
267 map_symbol__set_folding(&self->ms, unfold);
268
269 if (self->ms.has_children) {
270 int n = callchain__set_folding(&self->sorted_chain, unfold);
271 self->nr_rows = unfold ? n : 0;
272 } else
273 self->nr_rows = 0;
274}
275
276static void hists__set_folding(struct hists *self, bool unfold)
277{
278 struct rb_node *nd;
279
280 self->nr_entries = 0;
281
282 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
283 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
284 hist_entry__set_folding(he, unfold);
285 self->nr_entries += 1 + he->nr_rows;
286 }
287}
288
289static void hist_browser__set_folding(struct hist_browser *self, bool unfold)
290{
291 hists__set_folding(self->hists, unfold);
292 self->b.nr_entries = self->hists->nr_entries;
293 /* Go to the start, we may be way after valid entries after a collapse */
294 ui_browser__reset_index(&self->b);
295}
296
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300297static int hist_browser__run(struct hist_browser *self, const char *ev_name,
298 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300299{
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300300 int key;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300301 char title[160];
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300302
303 self->b.entries = &self->hists->entries;
304 self->b.nr_entries = self->hists->nr_entries;
305
306 hist_browser__refresh_dimensions(self);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200307 hists__browser_title(self->hists, title, sizeof(title), ev_name);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300308
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300309 if (ui_browser__show(&self->b, title,
310 "Press '?' for help on key bindings") < 0)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300311 return -1;
312
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300313 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300314 key = ui_browser__run(&self->b, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300315
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300316 switch (key) {
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300317 case -1:
318 /* FIXME we need to check if it was es.reason == NEWT_EXIT_TIMER */
319 timer(arg);
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300320 ui_browser__update_nr_entries(&self->b, self->hists->nr_entries);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300321 hists__browser_title(self->hists, title, sizeof(title),
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200322 ev_name);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300323 ui_browser__show_title(&self->b, title);
324 continue;
Arnaldo Carvalho de Melo46941532010-08-10 15:50:07 -0300325 case 'D': { /* Debug */
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300326 static int seq;
327 struct hist_entry *h = rb_entry(self->b.top,
328 struct hist_entry, rb_node);
329 ui_helpline__pop();
330 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
331 seq++, self->b.nr_entries,
332 self->hists->nr_entries,
333 self->b.height,
334 self->b.index,
335 self->b.top_idx,
336 h->row_offset, h->nr_rows);
337 }
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300338 break;
339 case 'C':
340 /* Collapse the whole world. */
341 hist_browser__set_folding(self, false);
342 break;
343 case 'E':
344 /* Expand the whole world. */
345 hist_browser__set_folding(self, true);
346 break;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300347 case NEWT_KEY_ENTER:
348 if (hist_browser__toggle_fold(self))
349 break;
350 /* fall thru */
351 default:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300352 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300353 }
354 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300355out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300356 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300357 return key;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300358}
359
360static char *callchain_list__sym_name(struct callchain_list *self,
361 char *bf, size_t bfsize)
362{
363 if (self->ms.sym)
364 return self->ms.sym->name;
365
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200366 snprintf(bf, bfsize, "%#" PRIx64, self->ip);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300367 return bf;
368}
369
370#define LEVEL_OFFSET_STEP 3
371
372static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
373 struct callchain_node *chain_node,
374 u64 total, int level,
375 unsigned short row,
376 off_t *row_offset,
377 bool *is_current_entry)
378{
379 struct rb_node *node;
380 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
381 u64 new_total, remaining;
382
383 if (callchain_param.mode == CHAIN_GRAPH_REL)
384 new_total = chain_node->children_hit;
385 else
386 new_total = total;
387
388 remaining = new_total;
389 node = rb_first(&chain_node->rb_root);
390 while (node) {
391 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
392 struct rb_node *next = rb_next(node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100393 u64 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300394 struct callchain_list *chain;
395 char folded_sign = ' ';
396 int first = true;
397 int extra_offset = 0;
398
399 remaining -= cumul;
400
401 list_for_each_entry(chain, &child->val, list) {
402 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
403 const char *str;
404 int color;
405 bool was_first = first;
406
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300407 if (first)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300408 first = false;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300409 else
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300410 extra_offset = LEVEL_OFFSET_STEP;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300411
412 folded_sign = callchain_list__folded(chain);
413 if (*row_offset != 0) {
414 --*row_offset;
415 goto do_next;
416 }
417
418 alloc_str = NULL;
419 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
420 if (was_first) {
421 double percent = cumul * 100.0 / new_total;
422
423 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
424 str = "Not enough memory!";
425 else
426 str = alloc_str;
427 }
428
429 color = HE_COLORSET_NORMAL;
430 width = self->b.width - (offset + extra_offset + 2);
431 if (ui_browser__is_current_entry(&self->b, row)) {
432 self->selection = &chain->ms;
433 color = HE_COLORSET_SELECTED;
434 *is_current_entry = true;
435 }
436
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300437 ui_browser__set_color(&self->b, color);
438 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300439 slsmg_write_nstring(" ", offset + extra_offset);
440 slsmg_printf("%c ", folded_sign);
441 slsmg_write_nstring(str, width);
442 free(alloc_str);
443
444 if (++row == self->b.height)
445 goto out;
446do_next:
447 if (folded_sign == '+')
448 break;
449 }
450
451 if (folded_sign == '-') {
452 const int new_level = level + (extra_offset ? 2 : 1);
453 row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
454 new_level, row, row_offset,
455 is_current_entry);
456 }
457 if (row == self->b.height)
458 goto out;
459 node = next;
460 }
461out:
462 return row - first_row;
463}
464
465static int hist_browser__show_callchain_node(struct hist_browser *self,
466 struct callchain_node *node,
467 int level, unsigned short row,
468 off_t *row_offset,
469 bool *is_current_entry)
470{
471 struct callchain_list *chain;
472 int first_row = row,
473 offset = level * LEVEL_OFFSET_STEP,
474 width = self->b.width - offset;
475 char folded_sign = ' ';
476
477 list_for_each_entry(chain, &node->val, list) {
478 char ipstr[BITS_PER_LONG / 4 + 1], *s;
479 int color;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300480
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300481 folded_sign = callchain_list__folded(chain);
482
483 if (*row_offset != 0) {
484 --*row_offset;
485 continue;
486 }
487
488 color = HE_COLORSET_NORMAL;
489 if (ui_browser__is_current_entry(&self->b, row)) {
490 self->selection = &chain->ms;
491 color = HE_COLORSET_SELECTED;
492 *is_current_entry = true;
493 }
494
495 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300496 ui_browser__gotorc(&self->b, row, 0);
497 ui_browser__set_color(&self->b, color);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300498 slsmg_write_nstring(" ", offset);
499 slsmg_printf("%c ", folded_sign);
500 slsmg_write_nstring(s, width - 2);
501
502 if (++row == self->b.height)
503 goto out;
504 }
505
506 if (folded_sign == '-')
507 row += hist_browser__show_callchain_node_rb_tree(self, node,
508 self->hists->stats.total_period,
509 level + 1, row,
510 row_offset,
511 is_current_entry);
512out:
513 return row - first_row;
514}
515
516static int hist_browser__show_callchain(struct hist_browser *self,
517 struct rb_root *chain,
518 int level, unsigned short row,
519 off_t *row_offset,
520 bool *is_current_entry)
521{
522 struct rb_node *nd;
523 int first_row = row;
524
525 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
526 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
527
528 row += hist_browser__show_callchain_node(self, node, level,
529 row, row_offset,
530 is_current_entry);
531 if (row == self->b.height)
532 break;
533 }
534
535 return row - first_row;
536}
537
538static int hist_browser__show_entry(struct hist_browser *self,
539 struct hist_entry *entry,
540 unsigned short row)
541{
542 char s[256];
543 double percent;
544 int printed = 0;
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200545 int width = self->b.width - 6; /* The percentage */
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300546 char folded_sign = ' ';
547 bool current_entry = ui_browser__is_current_entry(&self->b, row);
548 off_t row_offset = entry->row_offset;
549
550 if (current_entry) {
551 self->he_selection = entry;
552 self->selection = &entry->ms;
553 }
554
555 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300556 hist_entry__init_have_children(entry);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300557 folded_sign = hist_entry__folded(entry);
558 }
559
560 if (row_offset == 0) {
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200561 hist_entry__snprintf(entry, s, sizeof(s), self->hists);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300562 percent = (entry->period * 100.0) / self->hists->stats.total_period;
563
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200564 ui_browser__set_percent_color(&self->b, percent, current_entry);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300565 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300566 if (symbol_conf.use_callchain) {
567 slsmg_printf("%c ", folded_sign);
568 width -= 2;
569 }
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200570
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200571 slsmg_printf(" %5.2f%%", percent);
572
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200573 /* The scroll bar isn't being used */
574 if (!self->b.navkeypressed)
575 width += 1;
576
Arnaldo Carvalho de Melo33f62b32011-10-18 15:54:24 -0200577 if (!current_entry || !self->b.navkeypressed)
578 ui_browser__set_color(&self->b, HE_COLORSET_NORMAL);
579
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300580 slsmg_write_nstring(s, width);
581 ++row;
582 ++printed;
583 } else
584 --row_offset;
585
586 if (folded_sign == '-' && row != self->b.height) {
587 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
588 1, row, &row_offset,
589 &current_entry);
590 if (current_entry)
591 self->he_selection = entry;
592 }
593
594 return printed;
595}
596
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300597static void ui_browser__hists_init_top(struct ui_browser *browser)
598{
599 if (browser->top == NULL) {
600 struct hist_browser *hb;
601
602 hb = container_of(browser, struct hist_browser, b);
603 browser->top = rb_first(&hb->hists->entries);
604 }
605}
606
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300607static unsigned int hist_browser__refresh(struct ui_browser *self)
608{
609 unsigned row = 0;
610 struct rb_node *nd;
611 struct hist_browser *hb = container_of(self, struct hist_browser, b);
612
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300613 ui_browser__hists_init_top(self);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300614
615 for (nd = self->top; nd; nd = rb_next(nd)) {
616 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
617
618 if (h->filtered)
619 continue;
620
621 row += hist_browser__show_entry(hb, h, row);
622 if (row == self->height)
623 break;
624 }
625
626 return row;
627}
628
629static struct rb_node *hists__filter_entries(struct rb_node *nd)
630{
631 while (nd != NULL) {
632 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
633 if (!h->filtered)
634 return nd;
635
636 nd = rb_next(nd);
637 }
638
639 return NULL;
640}
641
642static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
643{
644 while (nd != NULL) {
645 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
646 if (!h->filtered)
647 return nd;
648
649 nd = rb_prev(nd);
650 }
651
652 return NULL;
653}
654
655static void ui_browser__hists_seek(struct ui_browser *self,
656 off_t offset, int whence)
657{
658 struct hist_entry *h;
659 struct rb_node *nd;
660 bool first = true;
661
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300662 if (self->nr_entries == 0)
663 return;
664
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300665 ui_browser__hists_init_top(self);
666
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300667 switch (whence) {
668 case SEEK_SET:
669 nd = hists__filter_entries(rb_first(self->entries));
670 break;
671 case SEEK_CUR:
672 nd = self->top;
673 goto do_offset;
674 case SEEK_END:
675 nd = hists__filter_prev_entries(rb_last(self->entries));
676 first = false;
677 break;
678 default:
679 return;
680 }
681
682 /*
683 * Moves not relative to the first visible entry invalidates its
684 * row_offset:
685 */
686 h = rb_entry(self->top, struct hist_entry, rb_node);
687 h->row_offset = 0;
688
689 /*
690 * Here we have to check if nd is expanded (+), if it is we can't go
691 * the next top level hist_entry, instead we must compute an offset of
692 * what _not_ to show and not change the first visible entry.
693 *
694 * This offset increments when we are going from top to bottom and
695 * decreases when we're going from bottom to top.
696 *
697 * As we don't have backpointers to the top level in the callchains
698 * structure, we need to always print the whole hist_entry callchain,
699 * skipping the first ones that are before the first visible entry
700 * and stop when we printed enough lines to fill the screen.
701 */
702do_offset:
703 if (offset > 0) {
704 do {
705 h = rb_entry(nd, struct hist_entry, rb_node);
706 if (h->ms.unfolded) {
707 u16 remaining = h->nr_rows - h->row_offset;
708 if (offset > remaining) {
709 offset -= remaining;
710 h->row_offset = 0;
711 } else {
712 h->row_offset += offset;
713 offset = 0;
714 self->top = nd;
715 break;
716 }
717 }
718 nd = hists__filter_entries(rb_next(nd));
719 if (nd == NULL)
720 break;
721 --offset;
722 self->top = nd;
723 } while (offset != 0);
724 } else if (offset < 0) {
725 while (1) {
726 h = rb_entry(nd, struct hist_entry, rb_node);
727 if (h->ms.unfolded) {
728 if (first) {
729 if (-offset > h->row_offset) {
730 offset += h->row_offset;
731 h->row_offset = 0;
732 } else {
733 h->row_offset += offset;
734 offset = 0;
735 self->top = nd;
736 break;
737 }
738 } else {
739 if (-offset > h->nr_rows) {
740 offset += h->nr_rows;
741 h->row_offset = 0;
742 } else {
743 h->row_offset = h->nr_rows + offset;
744 offset = 0;
745 self->top = nd;
746 break;
747 }
748 }
749 }
750
751 nd = hists__filter_prev_entries(rb_prev(nd));
752 if (nd == NULL)
753 break;
754 ++offset;
755 self->top = nd;
756 if (offset == 0) {
757 /*
758 * Last unfiltered hist_entry, check if it is
759 * unfolded, if it is then we should have
760 * row_offset at its last entry.
761 */
762 h = rb_entry(nd, struct hist_entry, rb_node);
763 if (h->ms.unfolded)
764 h->row_offset = h->nr_rows;
765 break;
766 }
767 first = false;
768 }
769 } else {
770 self->top = nd;
771 h = rb_entry(nd, struct hist_entry, rb_node);
772 h->row_offset = 0;
773 }
774}
775
776static struct hist_browser *hist_browser__new(struct hists *hists)
777{
778 struct hist_browser *self = zalloc(sizeof(*self));
779
780 if (self) {
781 self->hists = hists;
782 self->b.refresh = hist_browser__refresh;
783 self->b.seek = ui_browser__hists_seek;
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200784 self->b.use_navkeypressed = true,
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300785 self->has_symbols = sort_sym.list.next != NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300786 }
787
788 return self;
789}
790
791static void hist_browser__delete(struct hist_browser *self)
792{
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300793 free(self);
794}
795
796static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
797{
798 return self->he_selection;
799}
800
801static struct thread *hist_browser__selected_thread(struct hist_browser *self)
802{
803 return self->he_selection->thread;
804}
805
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300806static int hists__browser_title(struct hists *self, char *bf, size_t size,
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200807 const char *ev_name)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300808{
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300809 char unit;
810 int printed;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200811 const struct dso *dso = self->dso_filter;
812 const struct thread *thread = self->thread_filter;
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300813 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 Mingdb9a9cbc2011-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 Melo2d5646c2011-10-18 13:02:52 -0200885 ui__help_window("h/?/F1 Show this window\n"
886 "UP/DOWN/PGUP\n"
887 "PGDN/SPACE Navigate\n"
888 "q/ESC/CTRL+C Exit browser\n\n"
889 "For multiple event sessions:\n\n"
890 "TAB/UNTAB Switch events\n\n"
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300891 "For symbolic views (--sort has sym):\n\n"
Arnaldo Carvalho de Melo2d5646c2011-10-18 13:02:52 -0200892 "-> Zoom into DSO/Threads & Annotate current symbol\n"
893 "<- Zoom out\n"
894 "a Annotate current symbol\n"
895 "C Collapse all callchains\n"
896 "E Expand all callchains\n"
897 "d Zoom into current DSO\n"
898 "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 Melod7b76f02011-10-18 19:07:34 -0200916 if (top == &browser->hists->dso_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300917 goto zoom_out_dso;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200918 if (top == &browser->hists->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 */
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300927 case 'q':
928 case CTRL('c'):
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300929 goto out_free_stack;
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300930 default:
931 continue;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300932 }
933
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300934 if (!browser->has_symbols)
935 goto add_exit_option;
936
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300937 if (browser->selection != NULL &&
938 browser->selection->sym != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300939 !browser->selection->map->dso->annotate_warned &&
940 asprintf(&options[nr_options], "Annotate %s",
941 browser->selection->sym->name) > 0)
942 annotate = nr_options++;
943
944 if (thread != NULL &&
945 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200946 (browser->hists->thread_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300947 (thread->comm_set ? thread->comm : ""),
948 thread->pid) > 0)
949 zoom_thread = nr_options++;
950
951 if (dso != NULL &&
952 asprintf(&options[nr_options], "Zoom %s %s DSO",
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200953 (browser->hists->dso_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300954 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
955 zoom_dso = nr_options++;
956
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300957 if (browser->selection != NULL &&
958 browser->selection->map != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300959 asprintf(&options[nr_options], "Browse map details") > 0)
960 browse_map = nr_options++;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300961add_exit_option:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300962 options[nr_options++] = (char *)"Exit";
963
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300964 choice = ui__popup_menu(nr_options, options);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300965
966 for (i = 0; i < nr_options - 1; ++i)
967 free(options[i]);
968
969 if (choice == nr_options - 1)
970 break;
971
972 if (choice == -1)
973 continue;
974
975 if (choice == annotate) {
976 struct hist_entry *he;
977do_annotate:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300978 he = hist_browser__selected_entry(browser);
979 if (he == NULL)
980 continue;
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300981 /*
982 * Don't let this be freed, say, by hists__decay_entry.
983 */
984 he->used = true;
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300985 hist_entry__tui_annotate(he, evsel->idx, nr_events,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300986 timer, arg, delay_secs);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300987 he->used = false;
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300988 ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300989 } else if (choice == browse_map)
990 map__browse(browser->selection->map);
991 else if (choice == zoom_dso) {
992zoom_dso:
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200993 if (browser->hists->dso_filter) {
994 pstack__remove(fstack, &browser->hists->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300995zoom_out_dso:
996 ui_helpline__pop();
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200997 browser->hists->dso_filter = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300998 } else {
999 if (dso == NULL)
1000 continue;
1001 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
1002 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001003 browser->hists->dso_filter = dso;
1004 pstack__push(fstack, &browser->hists->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001005 }
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001006 hists__filter_by_dso(self);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001007 hist_browser__reset(browser);
1008 } else if (choice == zoom_thread) {
1009zoom_thread:
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001010 if (browser->hists->thread_filter) {
1011 pstack__remove(fstack, &browser->hists->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001012zoom_out_thread:
1013 ui_helpline__pop();
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001014 browser->hists->thread_filter = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001015 } else {
1016 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
1017 thread->comm_set ? thread->comm : "",
1018 thread->pid);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001019 browser->hists->thread_filter = thread;
1020 pstack__push(fstack, &browser->hists->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001021 }
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001022 hists__filter_by_thread(self);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001023 hist_browser__reset(browser);
1024 }
1025 }
1026out_free_stack:
1027 pstack__delete(fstack);
1028out:
1029 hist_browser__delete(browser);
1030 return key;
1031}
1032
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001033struct perf_evsel_menu {
1034 struct ui_browser b;
1035 struct perf_evsel *selection;
1036};
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001037
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001038static void perf_evsel_menu__write(struct ui_browser *browser,
1039 void *entry, int row)
1040{
1041 struct perf_evsel_menu *menu = container_of(browser,
1042 struct perf_evsel_menu, b);
1043 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
1044 bool current_entry = ui_browser__is_current_entry(browser, row);
1045 unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1046 const char *ev_name = event_name(evsel);
1047 char bf[256], unit;
1048
1049 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
1050 HE_COLORSET_NORMAL);
1051
1052 nr_events = convert_unit(nr_events, &unit);
1053 snprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
1054 unit, unit == ' ' ? "" : " ", ev_name);
1055 slsmg_write_nstring(bf, browser->width);
1056
1057 if (current_entry)
1058 menu->selection = evsel;
1059}
1060
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001061static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
1062 int nr_events, const char *help,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001063 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001064{
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001065 struct perf_evlist *evlist = menu->b.priv;
1066 struct perf_evsel *pos;
1067 const char *ev_name, *title = "Available samples";
1068 int key;
1069
1070 if (ui_browser__show(&menu->b, title,
1071 "ESC: exit, ENTER|->: Browse histograms") < 0)
1072 return -1;
1073
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001074 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03001075 key = ui_browser__run(&menu->b, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001076
1077 switch (key) {
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001078 case -1:
1079 /* FIXME we need to check if it was es.reason == NEWT_EXIT_TIMER */
1080 timer(arg);
1081 continue;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001082 case NEWT_KEY_RIGHT:
1083 case NEWT_KEY_ENTER:
1084 if (!menu->selection)
1085 continue;
1086 pos = menu->selection;
1087browse_hists:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001088 perf_evlist__set_selected(evlist, pos);
1089 /*
1090 * Give the calling tool a chance to populate the non
1091 * default evsel resorted hists tree.
1092 */
1093 if (timer)
1094 timer(arg);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001095 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);
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001100 switch (key) {
1101 case NEWT_KEY_TAB:
1102 if (pos->node.next == &evlist->entries)
1103 pos = list_entry(evlist->entries.next, struct perf_evsel, node);
1104 else
1105 pos = list_entry(pos->node.next, struct perf_evsel, node);
1106 goto browse_hists;
1107 case NEWT_KEY_UNTAB:
1108 if (pos->node.prev == &evlist->entries)
1109 pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
1110 else
1111 pos = list_entry(pos->node.prev, struct perf_evsel, node);
1112 goto browse_hists;
1113 case NEWT_KEY_ESCAPE:
1114 if (!ui__dialog_yesno("Do you really want to exit?"))
1115 continue;
1116 /* Fall thru */
1117 case 'q':
1118 case CTRL('c'):
1119 goto out;
1120 default:
1121 continue;
1122 }
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001123 case NEWT_KEY_LEFT:
1124 continue;
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -03001125 case NEWT_KEY_ESCAPE:
1126 if (!ui__dialog_yesno("Do you really want to exit?"))
1127 continue;
1128 /* Fall thru */
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001129 case 'q':
1130 case CTRL('c'):
1131 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001132 default:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001133 continue;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001134 }
1135 }
1136
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001137out:
1138 ui_browser__hide(&menu->b);
1139 return key;
1140}
1141
1142static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001143 const char *help,
1144 void(*timer)(void *arg), void *arg,
1145 int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001146{
1147 struct perf_evsel *pos;
1148 struct perf_evsel_menu menu = {
1149 .b = {
1150 .entries = &evlist->entries,
1151 .refresh = ui_browser__list_head_refresh,
1152 .seek = ui_browser__list_head_seek,
1153 .write = perf_evsel_menu__write,
1154 .nr_entries = evlist->nr_entries,
1155 .priv = evlist,
1156 },
1157 };
1158
1159 ui_helpline__push("Press ESC to exit");
1160
1161 list_for_each_entry(pos, &evlist->entries, node) {
1162 const char *ev_name = event_name(pos);
1163 size_t line_len = strlen(ev_name) + 7;
1164
1165 if (menu.b.width < line_len)
1166 menu.b.width = line_len;
1167 /*
1168 * Cache the evsel name, tracepoints have a _high_ cost per
1169 * event_name() call.
1170 */
1171 if (pos->name == NULL)
1172 pos->name = strdup(ev_name);
1173 }
1174
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001175 return perf_evsel_menu__run(&menu, evlist->nr_entries, help, timer,
1176 arg, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001177}
1178
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001179int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
1180 void(*timer)(void *arg), void *arg,
1181 int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001182{
1183
1184 if (evlist->nr_entries == 1) {
1185 struct perf_evsel *first = list_entry(evlist->entries.next,
1186 struct perf_evsel, node);
1187 const char *ev_name = event_name(first);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001188 return perf_evsel__hists_browse(first, evlist->nr_entries, help,
1189 ev_name, false, timer, arg,
1190 delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001191 }
1192
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001193 return __perf_evlist__tui_browse_hists(evlist, help,
1194 timer, arg, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001195}