blob: b8733c0770cd9e81050e4f8411b7f1d3dd72c3e6 [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"
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -020020#include "../ui.h"
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030021#include "map.h"
22
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030023struct hist_browser {
24 struct ui_browser b;
25 struct hists *hists;
26 struct hist_entry *he_selection;
27 struct map_symbol *selection;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -030028 bool has_symbols;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030029};
30
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -030031static int hists__browser_title(struct hists *self, char *bf, size_t size,
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -020032 const char *ev_name);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -030033
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030034static void hist_browser__refresh_dimensions(struct hist_browser *self)
35{
36 /* 3 == +/- toggle symbol before actual hist_entry rendering */
37 self->b.width = 3 + (hists__sort_list_width(self->hists) +
38 sizeof("[k]"));
39}
40
41static void hist_browser__reset(struct hist_browser *self)
42{
43 self->b.nr_entries = self->hists->nr_entries;
44 hist_browser__refresh_dimensions(self);
45 ui_browser__reset_index(&self->b);
46}
47
48static char tree__folded_sign(bool unfolded)
49{
50 return unfolded ? '-' : '+';
51}
52
53static char map_symbol__folded(const struct map_symbol *self)
54{
55 return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
56}
57
58static char hist_entry__folded(const struct hist_entry *self)
59{
60 return map_symbol__folded(&self->ms);
61}
62
63static char callchain_list__folded(const struct callchain_list *self)
64{
65 return map_symbol__folded(&self->ms);
66}
67
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -030068static void map_symbol__set_folding(struct map_symbol *self, bool unfold)
69{
70 self->unfolded = unfold ? self->has_children : false;
71}
72
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030073static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
74{
75 int n = 0;
76 struct rb_node *nd;
77
78 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
79 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
80 struct callchain_list *chain;
81 char folded_sign = ' '; /* No children */
82
83 list_for_each_entry(chain, &child->val, list) {
84 ++n;
85 /* We need this because we may not have children */
86 folded_sign = callchain_list__folded(chain);
87 if (folded_sign == '+')
88 break;
89 }
90
91 if (folded_sign == '-') /* Have children and they're unfolded */
92 n += callchain_node__count_rows_rb_tree(child);
93 }
94
95 return n;
96}
97
98static int callchain_node__count_rows(struct callchain_node *node)
99{
100 struct callchain_list *chain;
101 bool unfolded = false;
102 int n = 0;
103
104 list_for_each_entry(chain, &node->val, list) {
105 ++n;
106 unfolded = chain->ms.unfolded;
107 }
108
109 if (unfolded)
110 n += callchain_node__count_rows_rb_tree(node);
111
112 return n;
113}
114
115static int callchain__count_rows(struct rb_root *chain)
116{
117 struct rb_node *nd;
118 int n = 0;
119
120 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
121 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
122 n += callchain_node__count_rows(node);
123 }
124
125 return n;
126}
127
128static bool map_symbol__toggle_fold(struct map_symbol *self)
129{
130 if (!self->has_children)
131 return false;
132
133 self->unfolded = !self->unfolded;
134 return true;
135}
136
137static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
138{
139 struct rb_node *nd = rb_first(&self->rb_root);
140
141 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
142 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
143 struct callchain_list *chain;
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300144 bool first = true;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300145
146 list_for_each_entry(chain, &child->val, list) {
147 if (first) {
148 first = false;
149 chain->ms.has_children = chain->list.next != &child->val ||
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300150 !RB_EMPTY_ROOT(&child->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300151 } else
152 chain->ms.has_children = chain->list.next == &child->val &&
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300153 !RB_EMPTY_ROOT(&child->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300154 }
155
156 callchain_node__init_have_children_rb_tree(child);
157 }
158}
159
160static void callchain_node__init_have_children(struct callchain_node *self)
161{
162 struct callchain_list *chain;
163
164 list_for_each_entry(chain, &self->val, list)
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300165 chain->ms.has_children = !RB_EMPTY_ROOT(&self->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300166
167 callchain_node__init_have_children_rb_tree(self);
168}
169
170static void callchain__init_have_children(struct rb_root *self)
171{
172 struct rb_node *nd;
173
174 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
175 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
176 callchain_node__init_have_children(node);
177 }
178}
179
180static void hist_entry__init_have_children(struct hist_entry *self)
181{
182 if (!self->init_have_children) {
Arnaldo Carvalho de Melo18b308d2010-08-25 12:47:44 -0300183 self->ms.has_children = !RB_EMPTY_ROOT(&self->sorted_chain);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300184 callchain__init_have_children(&self->sorted_chain);
185 self->init_have_children = true;
186 }
187}
188
189static bool hist_browser__toggle_fold(struct hist_browser *self)
190{
191 if (map_symbol__toggle_fold(self->selection)) {
192 struct hist_entry *he = self->he_selection;
193
194 hist_entry__init_have_children(he);
195 self->hists->nr_entries -= he->nr_rows;
196
197 if (he->ms.unfolded)
198 he->nr_rows = callchain__count_rows(&he->sorted_chain);
199 else
200 he->nr_rows = 0;
201 self->hists->nr_entries += he->nr_rows;
202 self->b.nr_entries = self->hists->nr_entries;
203
204 return true;
205 }
206
207 /* If it doesn't have children, no toggling performed */
208 return false;
209}
210
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300211static int callchain_node__set_folding_rb_tree(struct callchain_node *self, bool unfold)
212{
213 int n = 0;
214 struct rb_node *nd;
215
216 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
217 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
218 struct callchain_list *chain;
219 bool has_children = false;
220
221 list_for_each_entry(chain, &child->val, list) {
222 ++n;
223 map_symbol__set_folding(&chain->ms, unfold);
224 has_children = chain->ms.has_children;
225 }
226
227 if (has_children)
228 n += callchain_node__set_folding_rb_tree(child, unfold);
229 }
230
231 return n;
232}
233
234static int callchain_node__set_folding(struct callchain_node *node, bool unfold)
235{
236 struct callchain_list *chain;
237 bool has_children = false;
238 int n = 0;
239
240 list_for_each_entry(chain, &node->val, list) {
241 ++n;
242 map_symbol__set_folding(&chain->ms, unfold);
243 has_children = chain->ms.has_children;
244 }
245
246 if (has_children)
247 n += callchain_node__set_folding_rb_tree(node, unfold);
248
249 return n;
250}
251
252static int callchain__set_folding(struct rb_root *chain, bool unfold)
253{
254 struct rb_node *nd;
255 int n = 0;
256
257 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
258 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
259 n += callchain_node__set_folding(node, unfold);
260 }
261
262 return n;
263}
264
265static void hist_entry__set_folding(struct hist_entry *self, bool unfold)
266{
267 hist_entry__init_have_children(self);
268 map_symbol__set_folding(&self->ms, unfold);
269
270 if (self->ms.has_children) {
271 int n = callchain__set_folding(&self->sorted_chain, unfold);
272 self->nr_rows = unfold ? n : 0;
273 } else
274 self->nr_rows = 0;
275}
276
277static void hists__set_folding(struct hists *self, bool unfold)
278{
279 struct rb_node *nd;
280
281 self->nr_entries = 0;
282
283 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
284 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
285 hist_entry__set_folding(he, unfold);
286 self->nr_entries += 1 + he->nr_rows;
287 }
288}
289
290static void hist_browser__set_folding(struct hist_browser *self, bool unfold)
291{
292 hists__set_folding(self->hists, unfold);
293 self->b.nr_entries = self->hists->nr_entries;
294 /* Go to the start, we may be way after valid entries after a collapse */
295 ui_browser__reset_index(&self->b);
296}
297
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300298static int hist_browser__run(struct hist_browser *self, const char *ev_name,
299 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300300{
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300301 int key;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300302 char title[160];
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300303
304 self->b.entries = &self->hists->entries;
305 self->b.nr_entries = self->hists->nr_entries;
306
307 hist_browser__refresh_dimensions(self);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200308 hists__browser_title(self->hists, title, sizeof(title), ev_name);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300309
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300310 if (ui_browser__show(&self->b, title,
311 "Press '?' for help on key bindings") < 0)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300312 return -1;
313
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300314 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300315 key = ui_browser__run(&self->b, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300316
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300317 switch (key) {
Arnaldo Carvalho de Melo13d8f962011-10-26 08:05:34 -0200318 case K_TIMER:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300319 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 Melocf958002011-10-20 16:59:15 -0200347 case K_ENTER:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300348 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 Melo2cf9ceb2011-10-19 14:37:59 -0200580 if (symbol_conf.show_nr_samples) {
581 slsmg_printf(" %11u", entry->nr_events);
582 width -= 12;
583 }
584
585 if (symbol_conf.show_total_period) {
586 slsmg_printf(" %12" PRIu64, entry->period);
587 width -= 13;
588 }
589
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300590 slsmg_write_nstring(s, width);
591 ++row;
592 ++printed;
593 } else
594 --row_offset;
595
596 if (folded_sign == '-' && row != self->b.height) {
597 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
598 1, row, &row_offset,
599 &current_entry);
600 if (current_entry)
601 self->he_selection = entry;
602 }
603
604 return printed;
605}
606
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300607static void ui_browser__hists_init_top(struct ui_browser *browser)
608{
609 if (browser->top == NULL) {
610 struct hist_browser *hb;
611
612 hb = container_of(browser, struct hist_browser, b);
613 browser->top = rb_first(&hb->hists->entries);
614 }
615}
616
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300617static unsigned int hist_browser__refresh(struct ui_browser *self)
618{
619 unsigned row = 0;
620 struct rb_node *nd;
621 struct hist_browser *hb = container_of(self, struct hist_browser, b);
622
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300623 ui_browser__hists_init_top(self);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300624
625 for (nd = self->top; nd; nd = rb_next(nd)) {
626 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
627
628 if (h->filtered)
629 continue;
630
631 row += hist_browser__show_entry(hb, h, row);
632 if (row == self->height)
633 break;
634 }
635
636 return row;
637}
638
639static struct rb_node *hists__filter_entries(struct rb_node *nd)
640{
641 while (nd != NULL) {
642 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
643 if (!h->filtered)
644 return nd;
645
646 nd = rb_next(nd);
647 }
648
649 return NULL;
650}
651
652static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
653{
654 while (nd != NULL) {
655 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
656 if (!h->filtered)
657 return nd;
658
659 nd = rb_prev(nd);
660 }
661
662 return NULL;
663}
664
665static void ui_browser__hists_seek(struct ui_browser *self,
666 off_t offset, int whence)
667{
668 struct hist_entry *h;
669 struct rb_node *nd;
670 bool first = true;
671
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300672 if (self->nr_entries == 0)
673 return;
674
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300675 ui_browser__hists_init_top(self);
676
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300677 switch (whence) {
678 case SEEK_SET:
679 nd = hists__filter_entries(rb_first(self->entries));
680 break;
681 case SEEK_CUR:
682 nd = self->top;
683 goto do_offset;
684 case SEEK_END:
685 nd = hists__filter_prev_entries(rb_last(self->entries));
686 first = false;
687 break;
688 default:
689 return;
690 }
691
692 /*
693 * Moves not relative to the first visible entry invalidates its
694 * row_offset:
695 */
696 h = rb_entry(self->top, struct hist_entry, rb_node);
697 h->row_offset = 0;
698
699 /*
700 * Here we have to check if nd is expanded (+), if it is we can't go
701 * the next top level hist_entry, instead we must compute an offset of
702 * what _not_ to show and not change the first visible entry.
703 *
704 * This offset increments when we are going from top to bottom and
705 * decreases when we're going from bottom to top.
706 *
707 * As we don't have backpointers to the top level in the callchains
708 * structure, we need to always print the whole hist_entry callchain,
709 * skipping the first ones that are before the first visible entry
710 * and stop when we printed enough lines to fill the screen.
711 */
712do_offset:
713 if (offset > 0) {
714 do {
715 h = rb_entry(nd, struct hist_entry, rb_node);
716 if (h->ms.unfolded) {
717 u16 remaining = h->nr_rows - h->row_offset;
718 if (offset > remaining) {
719 offset -= remaining;
720 h->row_offset = 0;
721 } else {
722 h->row_offset += offset;
723 offset = 0;
724 self->top = nd;
725 break;
726 }
727 }
728 nd = hists__filter_entries(rb_next(nd));
729 if (nd == NULL)
730 break;
731 --offset;
732 self->top = nd;
733 } while (offset != 0);
734 } else if (offset < 0) {
735 while (1) {
736 h = rb_entry(nd, struct hist_entry, rb_node);
737 if (h->ms.unfolded) {
738 if (first) {
739 if (-offset > h->row_offset) {
740 offset += h->row_offset;
741 h->row_offset = 0;
742 } else {
743 h->row_offset += offset;
744 offset = 0;
745 self->top = nd;
746 break;
747 }
748 } else {
749 if (-offset > h->nr_rows) {
750 offset += h->nr_rows;
751 h->row_offset = 0;
752 } else {
753 h->row_offset = h->nr_rows + offset;
754 offset = 0;
755 self->top = nd;
756 break;
757 }
758 }
759 }
760
761 nd = hists__filter_prev_entries(rb_prev(nd));
762 if (nd == NULL)
763 break;
764 ++offset;
765 self->top = nd;
766 if (offset == 0) {
767 /*
768 * Last unfiltered hist_entry, check if it is
769 * unfolded, if it is then we should have
770 * row_offset at its last entry.
771 */
772 h = rb_entry(nd, struct hist_entry, rb_node);
773 if (h->ms.unfolded)
774 h->row_offset = h->nr_rows;
775 break;
776 }
777 first = false;
778 }
779 } else {
780 self->top = nd;
781 h = rb_entry(nd, struct hist_entry, rb_node);
782 h->row_offset = 0;
783 }
784}
785
786static struct hist_browser *hist_browser__new(struct hists *hists)
787{
788 struct hist_browser *self = zalloc(sizeof(*self));
789
790 if (self) {
791 self->hists = hists;
792 self->b.refresh = hist_browser__refresh;
793 self->b.seek = ui_browser__hists_seek;
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200794 self->b.use_navkeypressed = true,
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300795 self->has_symbols = sort_sym.list.next != NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300796 }
797
798 return self;
799}
800
801static void hist_browser__delete(struct hist_browser *self)
802{
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300803 free(self);
804}
805
806static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
807{
808 return self->he_selection;
809}
810
811static struct thread *hist_browser__selected_thread(struct hist_browser *self)
812{
813 return self->he_selection->thread;
814}
815
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300816static int hists__browser_title(struct hists *self, char *bf, size_t size,
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200817 const char *ev_name)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300818{
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300819 char unit;
820 int printed;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200821 const struct dso *dso = self->dso_filter;
822 const struct thread *thread = self->thread_filter;
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300823 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 Melocf958002011-10-20 16:59:15 -0200875 case K_TAB:
876 case K_UNTAB:
David Aherne4419b82011-10-19 11:37:47 -0600877 if (nr_events == 1)
878 continue;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300879 /*
880 * Exit the browser, let hists__browser_tree
881 * go to the next or previous
882 */
883 goto out_free_stack;
884 case 'a':
Arnaldo Carvalho de Meloa6e51f92011-10-21 10:58:24 -0200885 if (!browser->has_symbols) {
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200886 ui_browser__warning(&browser->b,
Arnaldo Carvalho de Meloa6e51f92011-10-21 10:58:24 -0200887 "Annotation is only available for symbolic views, "
888 "include \"sym\" in --sort to use it.");
889 continue;
890 }
891
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300892 if (browser->selection == NULL ||
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800893 browser->selection->sym == NULL ||
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300894 browser->selection->map->dso->annotate_warned)
895 continue;
896 goto do_annotate;
897 case 'd':
898 goto zoom_dso;
899 case 't':
900 goto zoom_thread;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200901 case K_F1:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300902 case 'h':
903 case '?':
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200904 ui_browser__help_window(&browser->b,
905 "h/?/F1 Show this window\n"
Arnaldo Carvalho de Melo2d5646c2011-10-18 13:02:52 -0200906 "UP/DOWN/PGUP\n"
907 "PGDN/SPACE Navigate\n"
908 "q/ESC/CTRL+C Exit browser\n\n"
909 "For multiple event sessions:\n\n"
910 "TAB/UNTAB Switch events\n\n"
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300911 "For symbolic views (--sort has sym):\n\n"
Arnaldo Carvalho de Melo2d5646c2011-10-18 13:02:52 -0200912 "-> Zoom into DSO/Threads & Annotate current symbol\n"
913 "<- Zoom out\n"
914 "a Annotate current symbol\n"
915 "C Collapse all callchains\n"
916 "E Expand all callchains\n"
917 "d Zoom into current DSO\n"
Arnaldo Carvalho de Melo13d8f962011-10-26 08:05:34 -0200918 "t Zoom into current Thread");
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300919 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200920 case K_ENTER:
921 case K_RIGHT:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300922 /* menu */
923 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200924 case K_LEFT: {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300925 const void *top;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300926
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300927 if (pstack__empty(fstack)) {
928 /*
929 * Go back to the perf_evsel_menu__run or other user
930 */
931 if (left_exits)
932 goto out_free_stack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300933 continue;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300934 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300935 top = pstack__pop(fstack);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200936 if (top == &browser->hists->dso_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300937 goto zoom_out_dso;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200938 if (top == &browser->hists->thread_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300939 goto zoom_out_thread;
940 continue;
941 }
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200942 case K_ESC:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300943 if (!left_exits &&
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200944 !ui_browser__dialog_yesno(&browser->b,
945 "Do you really want to exit?"))
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300946 continue;
947 /* Fall thru */
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300948 case 'q':
949 case CTRL('c'):
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300950 goto out_free_stack;
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300951 default:
952 continue;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300953 }
954
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300955 if (!browser->has_symbols)
956 goto add_exit_option;
957
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300958 if (browser->selection != NULL &&
959 browser->selection->sym != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300960 !browser->selection->map->dso->annotate_warned &&
961 asprintf(&options[nr_options], "Annotate %s",
962 browser->selection->sym->name) > 0)
963 annotate = nr_options++;
964
965 if (thread != NULL &&
966 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200967 (browser->hists->thread_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300968 (thread->comm_set ? thread->comm : ""),
969 thread->pid) > 0)
970 zoom_thread = nr_options++;
971
972 if (dso != NULL &&
973 asprintf(&options[nr_options], "Zoom %s %s DSO",
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200974 (browser->hists->dso_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300975 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
976 zoom_dso = nr_options++;
977
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300978 if (browser->selection != NULL &&
979 browser->selection->map != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300980 asprintf(&options[nr_options], "Browse map details") > 0)
981 browse_map = nr_options++;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300982add_exit_option:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300983 options[nr_options++] = (char *)"Exit";
984
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -0300985 choice = ui__popup_menu(nr_options, options);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300986
987 for (i = 0; i < nr_options - 1; ++i)
988 free(options[i]);
989
990 if (choice == nr_options - 1)
991 break;
992
993 if (choice == -1)
994 continue;
995
996 if (choice == annotate) {
997 struct hist_entry *he;
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200998 int err;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300999do_annotate:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001000 he = hist_browser__selected_entry(browser);
1001 if (he == NULL)
1002 continue;
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -03001003 /*
1004 * Don't let this be freed, say, by hists__decay_entry.
1005 */
1006 he->used = true;
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001007 err = hist_entry__tui_annotate(he, evsel->idx, nr_events,
1008 timer, arg, delay_secs);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -03001009 he->used = false;
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -03001010 ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001011 if (err)
1012 ui_browser__handle_resize(&browser->b);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001013 } else if (choice == browse_map)
1014 map__browse(browser->selection->map);
1015 else if (choice == zoom_dso) {
1016zoom_dso:
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001017 if (browser->hists->dso_filter) {
1018 pstack__remove(fstack, &browser->hists->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001019zoom_out_dso:
1020 ui_helpline__pop();
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001021 browser->hists->dso_filter = NULL;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001022 sort_dso.elide = false;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001023 } else {
1024 if (dso == NULL)
1025 continue;
1026 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
1027 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001028 browser->hists->dso_filter = dso;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001029 sort_dso.elide = true;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001030 pstack__push(fstack, &browser->hists->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001031 }
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001032 hists__filter_by_dso(self);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001033 hist_browser__reset(browser);
1034 } else if (choice == zoom_thread) {
1035zoom_thread:
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001036 if (browser->hists->thread_filter) {
1037 pstack__remove(fstack, &browser->hists->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001038zoom_out_thread:
1039 ui_helpline__pop();
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001040 browser->hists->thread_filter = NULL;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001041 sort_thread.elide = false;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001042 } else {
1043 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
1044 thread->comm_set ? thread->comm : "",
1045 thread->pid);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001046 browser->hists->thread_filter = thread;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001047 sort_thread.elide = true;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001048 pstack__push(fstack, &browser->hists->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001049 }
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001050 hists__filter_by_thread(self);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001051 hist_browser__reset(browser);
1052 }
1053 }
1054out_free_stack:
1055 pstack__delete(fstack);
1056out:
1057 hist_browser__delete(browser);
1058 return key;
1059}
1060
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001061struct perf_evsel_menu {
1062 struct ui_browser b;
1063 struct perf_evsel *selection;
1064};
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001065
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001066static void perf_evsel_menu__write(struct ui_browser *browser,
1067 void *entry, int row)
1068{
1069 struct perf_evsel_menu *menu = container_of(browser,
1070 struct perf_evsel_menu, b);
1071 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
1072 bool current_entry = ui_browser__is_current_entry(browser, row);
1073 unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1074 const char *ev_name = event_name(evsel);
1075 char bf[256], unit;
1076
1077 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
1078 HE_COLORSET_NORMAL);
1079
1080 nr_events = convert_unit(nr_events, &unit);
1081 snprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
1082 unit, unit == ' ' ? "" : " ", ev_name);
1083 slsmg_write_nstring(bf, browser->width);
1084
1085 if (current_entry)
1086 menu->selection = evsel;
1087}
1088
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001089static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
1090 int nr_events, const char *help,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001091 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001092{
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001093 struct perf_evlist *evlist = menu->b.priv;
1094 struct perf_evsel *pos;
1095 const char *ev_name, *title = "Available samples";
1096 int key;
1097
1098 if (ui_browser__show(&menu->b, title,
1099 "ESC: exit, ENTER|->: Browse histograms") < 0)
1100 return -1;
1101
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001102 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03001103 key = ui_browser__run(&menu->b, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001104
1105 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001106 case K_TIMER:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001107 timer(arg);
1108 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001109 case K_RIGHT:
1110 case K_ENTER:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001111 if (!menu->selection)
1112 continue;
1113 pos = menu->selection;
1114browse_hists:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001115 perf_evlist__set_selected(evlist, pos);
1116 /*
1117 * Give the calling tool a chance to populate the non
1118 * default evsel resorted hists tree.
1119 */
1120 if (timer)
1121 timer(arg);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001122 ev_name = event_name(pos);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001123 key = perf_evsel__hists_browse(pos, nr_events, help,
1124 ev_name, true, timer,
1125 arg, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001126 ui_browser__show_title(&menu->b, title);
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001127 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001128 case K_TAB:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001129 if (pos->node.next == &evlist->entries)
1130 pos = list_entry(evlist->entries.next, struct perf_evsel, node);
1131 else
1132 pos = list_entry(pos->node.next, struct perf_evsel, node);
1133 goto browse_hists;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001134 case K_UNTAB:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001135 if (pos->node.prev == &evlist->entries)
1136 pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
1137 else
1138 pos = list_entry(pos->node.prev, struct perf_evsel, node);
1139 goto browse_hists;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001140 case K_ESC:
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001141 if (!ui_browser__dialog_yesno(&menu->b,
1142 "Do you really want to exit?"))
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001143 continue;
1144 /* Fall thru */
1145 case 'q':
1146 case CTRL('c'):
1147 goto out;
1148 default:
1149 continue;
1150 }
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001151 case K_LEFT:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001152 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001153 case K_ESC:
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001154 if (!ui_browser__dialog_yesno(&menu->b,
1155 "Do you really want to exit?"))
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -03001156 continue;
1157 /* Fall thru */
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001158 case 'q':
1159 case CTRL('c'):
1160 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001161 default:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001162 continue;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001163 }
1164 }
1165
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001166out:
1167 ui_browser__hide(&menu->b);
1168 return key;
1169}
1170
1171static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001172 const char *help,
1173 void(*timer)(void *arg), void *arg,
1174 int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001175{
1176 struct perf_evsel *pos;
1177 struct perf_evsel_menu menu = {
1178 .b = {
1179 .entries = &evlist->entries,
1180 .refresh = ui_browser__list_head_refresh,
1181 .seek = ui_browser__list_head_seek,
1182 .write = perf_evsel_menu__write,
1183 .nr_entries = evlist->nr_entries,
1184 .priv = evlist,
1185 },
1186 };
1187
1188 ui_helpline__push("Press ESC to exit");
1189
1190 list_for_each_entry(pos, &evlist->entries, node) {
1191 const char *ev_name = event_name(pos);
1192 size_t line_len = strlen(ev_name) + 7;
1193
1194 if (menu.b.width < line_len)
1195 menu.b.width = line_len;
1196 /*
1197 * Cache the evsel name, tracepoints have a _high_ cost per
1198 * event_name() call.
1199 */
1200 if (pos->name == NULL)
1201 pos->name = strdup(ev_name);
1202 }
1203
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001204 return perf_evsel_menu__run(&menu, evlist->nr_entries, help, timer,
1205 arg, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001206}
1207
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001208int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
1209 void(*timer)(void *arg), void *arg,
1210 int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001211{
1212
1213 if (evlist->nr_entries == 1) {
1214 struct perf_evsel *first = list_entry(evlist->entries.next,
1215 struct perf_evsel, node);
1216 const char *ev_name = event_name(first);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001217 return perf_evsel__hists_browse(first, evlist->nr_entries, help,
1218 ev_name, false, timer, arg,
1219 delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001220 }
1221
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001222 return __perf_evlist__tui_browse_hists(evlist, help,
1223 timer, arg, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001224}