blob: 44210d471719bd6ba1d557fd3bd3252c0b9f5d35 [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 Melo13d8f962011-10-26 08:05:34 -0200317 case K_TIMER:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300318 timer(arg);
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300319 ui_browser__update_nr_entries(&self->b, self->hists->nr_entries);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300320 hists__browser_title(self->hists, title, sizeof(title),
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200321 ev_name);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300322 ui_browser__show_title(&self->b, title);
323 continue;
Arnaldo Carvalho de Melo46941532010-08-10 15:50:07 -0300324 case 'D': { /* Debug */
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300325 static int seq;
326 struct hist_entry *h = rb_entry(self->b.top,
327 struct hist_entry, rb_node);
328 ui_helpline__pop();
329 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
330 seq++, self->b.nr_entries,
331 self->hists->nr_entries,
332 self->b.height,
333 self->b.index,
334 self->b.top_idx,
335 h->row_offset, h->nr_rows);
336 }
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300337 break;
338 case 'C':
339 /* Collapse the whole world. */
340 hist_browser__set_folding(self, false);
341 break;
342 case 'E':
343 /* Expand the whole world. */
344 hist_browser__set_folding(self, true);
345 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200346 case K_ENTER:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300347 if (hist_browser__toggle_fold(self))
348 break;
349 /* fall thru */
350 default:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300351 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300352 }
353 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300354out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300355 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300356 return key;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300357}
358
359static char *callchain_list__sym_name(struct callchain_list *self,
360 char *bf, size_t bfsize)
361{
362 if (self->ms.sym)
363 return self->ms.sym->name;
364
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200365 snprintf(bf, bfsize, "%#" PRIx64, self->ip);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300366 return bf;
367}
368
369#define LEVEL_OFFSET_STEP 3
370
371static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
372 struct callchain_node *chain_node,
373 u64 total, int level,
374 unsigned short row,
375 off_t *row_offset,
376 bool *is_current_entry)
377{
378 struct rb_node *node;
379 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
380 u64 new_total, remaining;
381
382 if (callchain_param.mode == CHAIN_GRAPH_REL)
383 new_total = chain_node->children_hit;
384 else
385 new_total = total;
386
387 remaining = new_total;
388 node = rb_first(&chain_node->rb_root);
389 while (node) {
390 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
391 struct rb_node *next = rb_next(node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100392 u64 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300393 struct callchain_list *chain;
394 char folded_sign = ' ';
395 int first = true;
396 int extra_offset = 0;
397
398 remaining -= cumul;
399
400 list_for_each_entry(chain, &child->val, list) {
401 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
402 const char *str;
403 int color;
404 bool was_first = first;
405
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300406 if (first)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300407 first = false;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300408 else
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300409 extra_offset = LEVEL_OFFSET_STEP;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300410
411 folded_sign = callchain_list__folded(chain);
412 if (*row_offset != 0) {
413 --*row_offset;
414 goto do_next;
415 }
416
417 alloc_str = NULL;
418 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
419 if (was_first) {
420 double percent = cumul * 100.0 / new_total;
421
422 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
423 str = "Not enough memory!";
424 else
425 str = alloc_str;
426 }
427
428 color = HE_COLORSET_NORMAL;
429 width = self->b.width - (offset + extra_offset + 2);
430 if (ui_browser__is_current_entry(&self->b, row)) {
431 self->selection = &chain->ms;
432 color = HE_COLORSET_SELECTED;
433 *is_current_entry = true;
434 }
435
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300436 ui_browser__set_color(&self->b, color);
437 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300438 slsmg_write_nstring(" ", offset + extra_offset);
439 slsmg_printf("%c ", folded_sign);
440 slsmg_write_nstring(str, width);
441 free(alloc_str);
442
443 if (++row == self->b.height)
444 goto out;
445do_next:
446 if (folded_sign == '+')
447 break;
448 }
449
450 if (folded_sign == '-') {
451 const int new_level = level + (extra_offset ? 2 : 1);
452 row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
453 new_level, row, row_offset,
454 is_current_entry);
455 }
456 if (row == self->b.height)
457 goto out;
458 node = next;
459 }
460out:
461 return row - first_row;
462}
463
464static int hist_browser__show_callchain_node(struct hist_browser *self,
465 struct callchain_node *node,
466 int level, unsigned short row,
467 off_t *row_offset,
468 bool *is_current_entry)
469{
470 struct callchain_list *chain;
471 int first_row = row,
472 offset = level * LEVEL_OFFSET_STEP,
473 width = self->b.width - offset;
474 char folded_sign = ' ';
475
476 list_for_each_entry(chain, &node->val, list) {
477 char ipstr[BITS_PER_LONG / 4 + 1], *s;
478 int color;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300479
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300480 folded_sign = callchain_list__folded(chain);
481
482 if (*row_offset != 0) {
483 --*row_offset;
484 continue;
485 }
486
487 color = HE_COLORSET_NORMAL;
488 if (ui_browser__is_current_entry(&self->b, row)) {
489 self->selection = &chain->ms;
490 color = HE_COLORSET_SELECTED;
491 *is_current_entry = true;
492 }
493
494 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300495 ui_browser__gotorc(&self->b, row, 0);
496 ui_browser__set_color(&self->b, color);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300497 slsmg_write_nstring(" ", offset);
498 slsmg_printf("%c ", folded_sign);
499 slsmg_write_nstring(s, width - 2);
500
501 if (++row == self->b.height)
502 goto out;
503 }
504
505 if (folded_sign == '-')
506 row += hist_browser__show_callchain_node_rb_tree(self, node,
507 self->hists->stats.total_period,
508 level + 1, row,
509 row_offset,
510 is_current_entry);
511out:
512 return row - first_row;
513}
514
515static int hist_browser__show_callchain(struct hist_browser *self,
516 struct rb_root *chain,
517 int level, unsigned short row,
518 off_t *row_offset,
519 bool *is_current_entry)
520{
521 struct rb_node *nd;
522 int first_row = row;
523
524 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
525 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
526
527 row += hist_browser__show_callchain_node(self, node, level,
528 row, row_offset,
529 is_current_entry);
530 if (row == self->b.height)
531 break;
532 }
533
534 return row - first_row;
535}
536
537static int hist_browser__show_entry(struct hist_browser *self,
538 struct hist_entry *entry,
539 unsigned short row)
540{
541 char s[256];
542 double percent;
543 int printed = 0;
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200544 int width = self->b.width - 6; /* The percentage */
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300545 char folded_sign = ' ';
546 bool current_entry = ui_browser__is_current_entry(&self->b, row);
547 off_t row_offset = entry->row_offset;
548
549 if (current_entry) {
550 self->he_selection = entry;
551 self->selection = &entry->ms;
552 }
553
554 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300555 hist_entry__init_have_children(entry);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300556 folded_sign = hist_entry__folded(entry);
557 }
558
559 if (row_offset == 0) {
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200560 hist_entry__snprintf(entry, s, sizeof(s), self->hists);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300561 percent = (entry->period * 100.0) / self->hists->stats.total_period;
562
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200563 ui_browser__set_percent_color(&self->b, percent, current_entry);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300564 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300565 if (symbol_conf.use_callchain) {
566 slsmg_printf("%c ", folded_sign);
567 width -= 2;
568 }
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200569
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200570 slsmg_printf(" %5.2f%%", percent);
571
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200572 /* The scroll bar isn't being used */
573 if (!self->b.navkeypressed)
574 width += 1;
575
Arnaldo Carvalho de Melo33f62b32011-10-18 15:54:24 -0200576 if (!current_entry || !self->b.navkeypressed)
577 ui_browser__set_color(&self->b, HE_COLORSET_NORMAL);
578
Arnaldo Carvalho de Melo2cf9ceb2011-10-19 14:37:59 -0200579 if (symbol_conf.show_nr_samples) {
580 slsmg_printf(" %11u", entry->nr_events);
581 width -= 12;
582 }
583
584 if (symbol_conf.show_total_period) {
585 slsmg_printf(" %12" PRIu64, entry->period);
586 width -= 13;
587 }
588
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300589 slsmg_write_nstring(s, width);
590 ++row;
591 ++printed;
592 } else
593 --row_offset;
594
595 if (folded_sign == '-' && row != self->b.height) {
596 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
597 1, row, &row_offset,
598 &current_entry);
599 if (current_entry)
600 self->he_selection = entry;
601 }
602
603 return printed;
604}
605
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300606static void ui_browser__hists_init_top(struct ui_browser *browser)
607{
608 if (browser->top == NULL) {
609 struct hist_browser *hb;
610
611 hb = container_of(browser, struct hist_browser, b);
612 browser->top = rb_first(&hb->hists->entries);
613 }
614}
615
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300616static unsigned int hist_browser__refresh(struct ui_browser *self)
617{
618 unsigned row = 0;
619 struct rb_node *nd;
620 struct hist_browser *hb = container_of(self, struct hist_browser, b);
621
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300622 ui_browser__hists_init_top(self);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300623
624 for (nd = self->top; nd; nd = rb_next(nd)) {
625 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
626
627 if (h->filtered)
628 continue;
629
630 row += hist_browser__show_entry(hb, h, row);
631 if (row == self->height)
632 break;
633 }
634
635 return row;
636}
637
638static struct rb_node *hists__filter_entries(struct rb_node *nd)
639{
640 while (nd != NULL) {
641 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
642 if (!h->filtered)
643 return nd;
644
645 nd = rb_next(nd);
646 }
647
648 return NULL;
649}
650
651static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
652{
653 while (nd != NULL) {
654 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
655 if (!h->filtered)
656 return nd;
657
658 nd = rb_prev(nd);
659 }
660
661 return NULL;
662}
663
664static void ui_browser__hists_seek(struct ui_browser *self,
665 off_t offset, int whence)
666{
667 struct hist_entry *h;
668 struct rb_node *nd;
669 bool first = true;
670
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300671 if (self->nr_entries == 0)
672 return;
673
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300674 ui_browser__hists_init_top(self);
675
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300676 switch (whence) {
677 case SEEK_SET:
678 nd = hists__filter_entries(rb_first(self->entries));
679 break;
680 case SEEK_CUR:
681 nd = self->top;
682 goto do_offset;
683 case SEEK_END:
684 nd = hists__filter_prev_entries(rb_last(self->entries));
685 first = false;
686 break;
687 default:
688 return;
689 }
690
691 /*
692 * Moves not relative to the first visible entry invalidates its
693 * row_offset:
694 */
695 h = rb_entry(self->top, struct hist_entry, rb_node);
696 h->row_offset = 0;
697
698 /*
699 * Here we have to check if nd is expanded (+), if it is we can't go
700 * the next top level hist_entry, instead we must compute an offset of
701 * what _not_ to show and not change the first visible entry.
702 *
703 * This offset increments when we are going from top to bottom and
704 * decreases when we're going from bottom to top.
705 *
706 * As we don't have backpointers to the top level in the callchains
707 * structure, we need to always print the whole hist_entry callchain,
708 * skipping the first ones that are before the first visible entry
709 * and stop when we printed enough lines to fill the screen.
710 */
711do_offset:
712 if (offset > 0) {
713 do {
714 h = rb_entry(nd, struct hist_entry, rb_node);
715 if (h->ms.unfolded) {
716 u16 remaining = h->nr_rows - h->row_offset;
717 if (offset > remaining) {
718 offset -= remaining;
719 h->row_offset = 0;
720 } else {
721 h->row_offset += offset;
722 offset = 0;
723 self->top = nd;
724 break;
725 }
726 }
727 nd = hists__filter_entries(rb_next(nd));
728 if (nd == NULL)
729 break;
730 --offset;
731 self->top = nd;
732 } while (offset != 0);
733 } else if (offset < 0) {
734 while (1) {
735 h = rb_entry(nd, struct hist_entry, rb_node);
736 if (h->ms.unfolded) {
737 if (first) {
738 if (-offset > h->row_offset) {
739 offset += h->row_offset;
740 h->row_offset = 0;
741 } else {
742 h->row_offset += offset;
743 offset = 0;
744 self->top = nd;
745 break;
746 }
747 } else {
748 if (-offset > h->nr_rows) {
749 offset += h->nr_rows;
750 h->row_offset = 0;
751 } else {
752 h->row_offset = h->nr_rows + offset;
753 offset = 0;
754 self->top = nd;
755 break;
756 }
757 }
758 }
759
760 nd = hists__filter_prev_entries(rb_prev(nd));
761 if (nd == NULL)
762 break;
763 ++offset;
764 self->top = nd;
765 if (offset == 0) {
766 /*
767 * Last unfiltered hist_entry, check if it is
768 * unfolded, if it is then we should have
769 * row_offset at its last entry.
770 */
771 h = rb_entry(nd, struct hist_entry, rb_node);
772 if (h->ms.unfolded)
773 h->row_offset = h->nr_rows;
774 break;
775 }
776 first = false;
777 }
778 } else {
779 self->top = nd;
780 h = rb_entry(nd, struct hist_entry, rb_node);
781 h->row_offset = 0;
782 }
783}
784
785static struct hist_browser *hist_browser__new(struct hists *hists)
786{
787 struct hist_browser *self = zalloc(sizeof(*self));
788
789 if (self) {
790 self->hists = hists;
791 self->b.refresh = hist_browser__refresh;
792 self->b.seek = ui_browser__hists_seek;
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200793 self->b.use_navkeypressed = true,
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300794 self->has_symbols = sort_sym.list.next != NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300795 }
796
797 return self;
798}
799
800static void hist_browser__delete(struct hist_browser *self)
801{
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300802 free(self);
803}
804
805static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
806{
807 return self->he_selection;
808}
809
810static struct thread *hist_browser__selected_thread(struct hist_browser *self)
811{
812 return self->he_selection->thread;
813}
814
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300815static int hists__browser_title(struct hists *self, char *bf, size_t size,
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200816 const char *ev_name)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300817{
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300818 char unit;
819 int printed;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200820 const struct dso *dso = self->dso_filter;
821 const struct thread *thread = self->thread_filter;
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300822 unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE];
823
824 nr_events = convert_unit(nr_events, &unit);
825 printed = snprintf(bf, size, "Events: %lu%c %s", nr_events, unit, ev_name);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300826
827 if (thread)
828 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300829 ", Thread: %s(%d)",
830 (thread->comm_set ? thread->comm : ""),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300831 thread->pid);
832 if (dso)
833 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300834 ", DSO: %s", dso->short_name);
835 return printed;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300836}
837
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300838static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300839 const char *helpline, const char *ev_name,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300840 bool left_exits,
841 void(*timer)(void *arg), void *arg,
842 int delay_secs)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300843{
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300844 struct hists *self = &evsel->hists;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300845 struct hist_browser *browser = hist_browser__new(self);
846 struct pstack *fstack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300847 int key = -1;
848
849 if (browser == NULL)
850 return -1;
851
852 fstack = pstack__new(2);
853 if (fstack == NULL)
854 goto out;
855
856 ui_helpline__push(helpline);
857
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300858 while (1) {
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300859 const struct thread *thread = NULL;
860 const struct dso *dso = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300861 char *options[16];
862 int nr_options = 0, choice = 0, i,
863 annotate = -2, zoom_dso = -2, zoom_thread = -2,
864 browse_map = -2;
865
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300866 key = hist_browser__run(browser, ev_name, timer, arg, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300867
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300868 if (browser->he_selection != NULL) {
869 thread = hist_browser__selected_thread(browser);
870 dso = browser->selection->map ? browser->selection->map->dso : NULL;
871 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300872
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300873 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200874 case K_TAB:
875 case K_UNTAB:
David Aherne4419b82011-10-19 11:37:47 -0600876 if (nr_events == 1)
877 continue;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300878 /*
879 * Exit the browser, let hists__browser_tree
880 * go to the next or previous
881 */
882 goto out_free_stack;
883 case 'a':
Arnaldo Carvalho de Meloa6e51f92011-10-21 10:58:24 -0200884 if (!browser->has_symbols) {
885 ui__warning(
886 "Annotation is only available for symbolic views, "
887 "include \"sym\" in --sort to use it.");
888 continue;
889 }
890
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300891 if (browser->selection == NULL ||
Lin Mingdb9a9cb2011-04-08 14:31:26 +0800892 browser->selection->sym == NULL ||
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300893 browser->selection->map->dso->annotate_warned)
894 continue;
895 goto do_annotate;
896 case 'd':
897 goto zoom_dso;
898 case 't':
899 goto zoom_thread;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200900 case K_F1:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300901 case 'h':
902 case '?':
Arnaldo Carvalho de Melo2d5646c2011-10-18 13:02:52 -0200903 ui__help_window("h/?/F1 Show this window\n"
904 "UP/DOWN/PGUP\n"
905 "PGDN/SPACE Navigate\n"
906 "q/ESC/CTRL+C Exit browser\n\n"
907 "For multiple event sessions:\n\n"
908 "TAB/UNTAB Switch events\n\n"
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300909 "For symbolic views (--sort has sym):\n\n"
Arnaldo Carvalho de Melo2d5646c2011-10-18 13:02:52 -0200910 "-> Zoom into DSO/Threads & Annotate current symbol\n"
911 "<- Zoom out\n"
912 "a Annotate current symbol\n"
913 "C Collapse all callchains\n"
914 "E Expand all callchains\n"
915 "d Zoom into current DSO\n"
Arnaldo Carvalho de Melo13d8f962011-10-26 08:05:34 -0200916 "t Zoom into current Thread");
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300917 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200918 case K_ENTER:
919 case K_RIGHT:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300920 /* menu */
921 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200922 case K_LEFT: {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300923 const void *top;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300924
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300925 if (pstack__empty(fstack)) {
926 /*
927 * Go back to the perf_evsel_menu__run or other user
928 */
929 if (left_exits)
930 goto out_free_stack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300931 continue;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300932 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300933 top = pstack__pop(fstack);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200934 if (top == &browser->hists->dso_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300935 goto zoom_out_dso;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200936 if (top == &browser->hists->thread_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300937 goto zoom_out_thread;
938 continue;
939 }
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200940 case K_ESC:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300941 if (!left_exits &&
942 !ui__dialog_yesno("Do you really want to exit?"))
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300943 continue;
944 /* Fall thru */
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300945 case 'q':
946 case CTRL('c'):
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300947 goto out_free_stack;
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300948 default:
949 continue;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300950 }
951
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300952 if (!browser->has_symbols)
953 goto add_exit_option;
954
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300955 if (browser->selection != NULL &&
956 browser->selection->sym != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300957 !browser->selection->map->dso->annotate_warned &&
958 asprintf(&options[nr_options], "Annotate %s",
959 browser->selection->sym->name) > 0)
960 annotate = nr_options++;
961
962 if (thread != NULL &&
963 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200964 (browser->hists->thread_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300965 (thread->comm_set ? thread->comm : ""),
966 thread->pid) > 0)
967 zoom_thread = nr_options++;
968
969 if (dso != NULL &&
970 asprintf(&options[nr_options], "Zoom %s %s DSO",
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200971 (browser->hists->dso_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300972 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
973 zoom_dso = nr_options++;
974
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300975 if (browser->selection != NULL &&
976 browser->selection->map != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300977 asprintf(&options[nr_options], "Browse map details") > 0)
978 browse_map = nr_options++;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300979add_exit_option:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300980 options[nr_options++] = (char *)"Exit";
981
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300982 choice = ui__popup_menu(nr_options, options);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300983
984 for (i = 0; i < nr_options - 1; ++i)
985 free(options[i]);
986
987 if (choice == nr_options - 1)
988 break;
989
990 if (choice == -1)
991 continue;
992
993 if (choice == annotate) {
994 struct hist_entry *he;
995do_annotate:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300996 he = hist_browser__selected_entry(browser);
997 if (he == NULL)
998 continue;
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300999 /*
1000 * Don't let this be freed, say, by hists__decay_entry.
1001 */
1002 he->used = true;
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001003 hist_entry__tui_annotate(he, evsel->idx, nr_events,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001004 timer, arg, delay_secs);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -03001005 he->used = false;
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -03001006 ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001007 } else if (choice == browse_map)
1008 map__browse(browser->selection->map);
1009 else if (choice == zoom_dso) {
1010zoom_dso:
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001011 if (browser->hists->dso_filter) {
1012 pstack__remove(fstack, &browser->hists->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001013zoom_out_dso:
1014 ui_helpline__pop();
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001015 browser->hists->dso_filter = NULL;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001016 sort_dso.elide = false;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001017 } else {
1018 if (dso == NULL)
1019 continue;
1020 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
1021 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001022 browser->hists->dso_filter = dso;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001023 sort_dso.elide = true;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001024 pstack__push(fstack, &browser->hists->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001025 }
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001026 hists__filter_by_dso(self);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001027 hist_browser__reset(browser);
1028 } else if (choice == zoom_thread) {
1029zoom_thread:
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001030 if (browser->hists->thread_filter) {
1031 pstack__remove(fstack, &browser->hists->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001032zoom_out_thread:
1033 ui_helpline__pop();
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001034 browser->hists->thread_filter = NULL;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001035 sort_thread.elide = false;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001036 } else {
1037 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
1038 thread->comm_set ? thread->comm : "",
1039 thread->pid);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001040 browser->hists->thread_filter = thread;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001041 sort_thread.elide = true;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001042 pstack__push(fstack, &browser->hists->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001043 }
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001044 hists__filter_by_thread(self);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001045 hist_browser__reset(browser);
1046 }
1047 }
1048out_free_stack:
1049 pstack__delete(fstack);
1050out:
1051 hist_browser__delete(browser);
1052 return key;
1053}
1054
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001055struct perf_evsel_menu {
1056 struct ui_browser b;
1057 struct perf_evsel *selection;
1058};
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001059
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001060static void perf_evsel_menu__write(struct ui_browser *browser,
1061 void *entry, int row)
1062{
1063 struct perf_evsel_menu *menu = container_of(browser,
1064 struct perf_evsel_menu, b);
1065 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
1066 bool current_entry = ui_browser__is_current_entry(browser, row);
1067 unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1068 const char *ev_name = event_name(evsel);
1069 char bf[256], unit;
1070
1071 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
1072 HE_COLORSET_NORMAL);
1073
1074 nr_events = convert_unit(nr_events, &unit);
1075 snprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
1076 unit, unit == ' ' ? "" : " ", ev_name);
1077 slsmg_write_nstring(bf, browser->width);
1078
1079 if (current_entry)
1080 menu->selection = evsel;
1081}
1082
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001083static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
1084 int nr_events, const char *help,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001085 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001086{
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001087 struct perf_evlist *evlist = menu->b.priv;
1088 struct perf_evsel *pos;
1089 const char *ev_name, *title = "Available samples";
1090 int key;
1091
1092 if (ui_browser__show(&menu->b, title,
1093 "ESC: exit, ENTER|->: Browse histograms") < 0)
1094 return -1;
1095
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001096 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03001097 key = ui_browser__run(&menu->b, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001098
1099 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001100 case K_TIMER:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001101 timer(arg);
1102 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001103 case K_RIGHT:
1104 case K_ENTER:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001105 if (!menu->selection)
1106 continue;
1107 pos = menu->selection;
1108browse_hists:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001109 perf_evlist__set_selected(evlist, pos);
1110 /*
1111 * Give the calling tool a chance to populate the non
1112 * default evsel resorted hists tree.
1113 */
1114 if (timer)
1115 timer(arg);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001116 ev_name = event_name(pos);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001117 key = perf_evsel__hists_browse(pos, nr_events, help,
1118 ev_name, true, timer,
1119 arg, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001120 ui_browser__show_title(&menu->b, title);
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001121 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001122 case K_TAB:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001123 if (pos->node.next == &evlist->entries)
1124 pos = list_entry(evlist->entries.next, struct perf_evsel, node);
1125 else
1126 pos = list_entry(pos->node.next, struct perf_evsel, node);
1127 goto browse_hists;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001128 case K_UNTAB:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001129 if (pos->node.prev == &evlist->entries)
1130 pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
1131 else
1132 pos = list_entry(pos->node.prev, struct perf_evsel, node);
1133 goto browse_hists;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001134 case K_ESC:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001135 if (!ui__dialog_yesno("Do you really want to exit?"))
1136 continue;
1137 /* Fall thru */
1138 case 'q':
1139 case CTRL('c'):
1140 goto out;
1141 default:
1142 continue;
1143 }
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001144 case K_LEFT:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001145 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001146 case K_ESC:
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -03001147 if (!ui__dialog_yesno("Do you really want to exit?"))
1148 continue;
1149 /* Fall thru */
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001150 case 'q':
1151 case CTRL('c'):
1152 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001153 default:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001154 continue;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001155 }
1156 }
1157
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001158out:
1159 ui_browser__hide(&menu->b);
1160 return key;
1161}
1162
1163static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001164 const char *help,
1165 void(*timer)(void *arg), void *arg,
1166 int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001167{
1168 struct perf_evsel *pos;
1169 struct perf_evsel_menu menu = {
1170 .b = {
1171 .entries = &evlist->entries,
1172 .refresh = ui_browser__list_head_refresh,
1173 .seek = ui_browser__list_head_seek,
1174 .write = perf_evsel_menu__write,
1175 .nr_entries = evlist->nr_entries,
1176 .priv = evlist,
1177 },
1178 };
1179
1180 ui_helpline__push("Press ESC to exit");
1181
1182 list_for_each_entry(pos, &evlist->entries, node) {
1183 const char *ev_name = event_name(pos);
1184 size_t line_len = strlen(ev_name) + 7;
1185
1186 if (menu.b.width < line_len)
1187 menu.b.width = line_len;
1188 /*
1189 * Cache the evsel name, tracepoints have a _high_ cost per
1190 * event_name() call.
1191 */
1192 if (pos->name == NULL)
1193 pos->name = strdup(ev_name);
1194 }
1195
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001196 return perf_evsel_menu__run(&menu, evlist->nr_entries, help, timer,
1197 arg, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001198}
1199
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001200int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
1201 void(*timer)(void *arg), void *arg,
1202 int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001203{
1204
1205 if (evlist->nr_entries == 1) {
1206 struct perf_evsel *first = list_entry(evlist->entries.next,
1207 struct perf_evsel, node);
1208 const char *ev_name = event_name(first);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001209 return perf_evsel__hists_browse(first, evlist->nr_entries, help,
1210 ev_name, false, timer, arg,
1211 delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001212 }
1213
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001214 return __perf_evlist__tui_browse_hists(evlist, help,
1215 timer, arg, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001216}