blob: 7b6669d1f11f74021e25c0660809523cb5a44a21 [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 Melo7b275092011-10-29 12:15:04 -0200298static void ui_browser__warn_lost_events(struct ui_browser *browser)
299{
300 ui_browser__warning(browser, 4,
301 "Events are being lost, check IO/CPU overload!\n\n"
302 "You may want to run 'perf' using a RT scheduler policy:\n\n"
303 " perf top -r 80\n\n"
304 "Or reduce the sampling frequency.");
305}
306
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300307static int hist_browser__run(struct hist_browser *self, const char *ev_name,
308 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300309{
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300310 int key;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300311 char title[160];
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300312
313 self->b.entries = &self->hists->entries;
314 self->b.nr_entries = self->hists->nr_entries;
315
316 hist_browser__refresh_dimensions(self);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200317 hists__browser_title(self->hists, title, sizeof(title), ev_name);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300318
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300319 if (ui_browser__show(&self->b, title,
320 "Press '?' for help on key bindings") < 0)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300321 return -1;
322
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300323 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300324 key = ui_browser__run(&self->b, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300325
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300326 switch (key) {
Arnaldo Carvalho de Melo13d8f962011-10-26 08:05:34 -0200327 case K_TIMER:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300328 timer(arg);
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300329 ui_browser__update_nr_entries(&self->b, self->hists->nr_entries);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200330
331 if (self->hists->stats.nr_lost_warned !=
332 self->hists->stats.nr_events[PERF_RECORD_LOST]) {
333 self->hists->stats.nr_lost_warned =
334 self->hists->stats.nr_events[PERF_RECORD_LOST];
335 ui_browser__warn_lost_events(&self->b);
336 }
337
338 hists__browser_title(self->hists, title, sizeof(title), ev_name);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300339 ui_browser__show_title(&self->b, title);
340 continue;
Arnaldo Carvalho de Melo46941532010-08-10 15:50:07 -0300341 case 'D': { /* Debug */
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300342 static int seq;
343 struct hist_entry *h = rb_entry(self->b.top,
344 struct hist_entry, rb_node);
345 ui_helpline__pop();
346 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
347 seq++, self->b.nr_entries,
348 self->hists->nr_entries,
349 self->b.height,
350 self->b.index,
351 self->b.top_idx,
352 h->row_offset, h->nr_rows);
353 }
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300354 break;
355 case 'C':
356 /* Collapse the whole world. */
357 hist_browser__set_folding(self, false);
358 break;
359 case 'E':
360 /* Expand the whole world. */
361 hist_browser__set_folding(self, true);
362 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200363 case K_ENTER:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300364 if (hist_browser__toggle_fold(self))
365 break;
366 /* fall thru */
367 default:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300368 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300369 }
370 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300371out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300372 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300373 return key;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300374}
375
376static char *callchain_list__sym_name(struct callchain_list *self,
377 char *bf, size_t bfsize)
378{
379 if (self->ms.sym)
380 return self->ms.sym->name;
381
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200382 snprintf(bf, bfsize, "%#" PRIx64, self->ip);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300383 return bf;
384}
385
386#define LEVEL_OFFSET_STEP 3
387
388static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
389 struct callchain_node *chain_node,
390 u64 total, int level,
391 unsigned short row,
392 off_t *row_offset,
393 bool *is_current_entry)
394{
395 struct rb_node *node;
396 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
397 u64 new_total, remaining;
398
399 if (callchain_param.mode == CHAIN_GRAPH_REL)
400 new_total = chain_node->children_hit;
401 else
402 new_total = total;
403
404 remaining = new_total;
405 node = rb_first(&chain_node->rb_root);
406 while (node) {
407 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
408 struct rb_node *next = rb_next(node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100409 u64 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300410 struct callchain_list *chain;
411 char folded_sign = ' ';
412 int first = true;
413 int extra_offset = 0;
414
415 remaining -= cumul;
416
417 list_for_each_entry(chain, &child->val, list) {
418 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
419 const char *str;
420 int color;
421 bool was_first = first;
422
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300423 if (first)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300424 first = false;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300425 else
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300426 extra_offset = LEVEL_OFFSET_STEP;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300427
428 folded_sign = callchain_list__folded(chain);
429 if (*row_offset != 0) {
430 --*row_offset;
431 goto do_next;
432 }
433
434 alloc_str = NULL;
435 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
436 if (was_first) {
437 double percent = cumul * 100.0 / new_total;
438
439 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
440 str = "Not enough memory!";
441 else
442 str = alloc_str;
443 }
444
445 color = HE_COLORSET_NORMAL;
446 width = self->b.width - (offset + extra_offset + 2);
447 if (ui_browser__is_current_entry(&self->b, row)) {
448 self->selection = &chain->ms;
449 color = HE_COLORSET_SELECTED;
450 *is_current_entry = true;
451 }
452
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300453 ui_browser__set_color(&self->b, color);
454 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300455 slsmg_write_nstring(" ", offset + extra_offset);
456 slsmg_printf("%c ", folded_sign);
457 slsmg_write_nstring(str, width);
458 free(alloc_str);
459
460 if (++row == self->b.height)
461 goto out;
462do_next:
463 if (folded_sign == '+')
464 break;
465 }
466
467 if (folded_sign == '-') {
468 const int new_level = level + (extra_offset ? 2 : 1);
469 row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
470 new_level, row, row_offset,
471 is_current_entry);
472 }
473 if (row == self->b.height)
474 goto out;
475 node = next;
476 }
477out:
478 return row - first_row;
479}
480
481static int hist_browser__show_callchain_node(struct hist_browser *self,
482 struct callchain_node *node,
483 int level, unsigned short row,
484 off_t *row_offset,
485 bool *is_current_entry)
486{
487 struct callchain_list *chain;
488 int first_row = row,
489 offset = level * LEVEL_OFFSET_STEP,
490 width = self->b.width - offset;
491 char folded_sign = ' ';
492
493 list_for_each_entry(chain, &node->val, list) {
494 char ipstr[BITS_PER_LONG / 4 + 1], *s;
495 int color;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300496
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300497 folded_sign = callchain_list__folded(chain);
498
499 if (*row_offset != 0) {
500 --*row_offset;
501 continue;
502 }
503
504 color = HE_COLORSET_NORMAL;
505 if (ui_browser__is_current_entry(&self->b, row)) {
506 self->selection = &chain->ms;
507 color = HE_COLORSET_SELECTED;
508 *is_current_entry = true;
509 }
510
511 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300512 ui_browser__gotorc(&self->b, row, 0);
513 ui_browser__set_color(&self->b, color);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300514 slsmg_write_nstring(" ", offset);
515 slsmg_printf("%c ", folded_sign);
516 slsmg_write_nstring(s, width - 2);
517
518 if (++row == self->b.height)
519 goto out;
520 }
521
522 if (folded_sign == '-')
523 row += hist_browser__show_callchain_node_rb_tree(self, node,
524 self->hists->stats.total_period,
525 level + 1, row,
526 row_offset,
527 is_current_entry);
528out:
529 return row - first_row;
530}
531
532static int hist_browser__show_callchain(struct hist_browser *self,
533 struct rb_root *chain,
534 int level, unsigned short row,
535 off_t *row_offset,
536 bool *is_current_entry)
537{
538 struct rb_node *nd;
539 int first_row = row;
540
541 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
542 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
543
544 row += hist_browser__show_callchain_node(self, node, level,
545 row, row_offset,
546 is_current_entry);
547 if (row == self->b.height)
548 break;
549 }
550
551 return row - first_row;
552}
553
554static int hist_browser__show_entry(struct hist_browser *self,
555 struct hist_entry *entry,
556 unsigned short row)
557{
558 char s[256];
559 double percent;
560 int printed = 0;
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200561 int width = self->b.width - 6; /* The percentage */
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300562 char folded_sign = ' ';
563 bool current_entry = ui_browser__is_current_entry(&self->b, row);
564 off_t row_offset = entry->row_offset;
565
566 if (current_entry) {
567 self->he_selection = entry;
568 self->selection = &entry->ms;
569 }
570
571 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300572 hist_entry__init_have_children(entry);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300573 folded_sign = hist_entry__folded(entry);
574 }
575
576 if (row_offset == 0) {
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200577 hist_entry__snprintf(entry, s, sizeof(s), self->hists);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300578 percent = (entry->period * 100.0) / self->hists->stats.total_period;
579
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200580 ui_browser__set_percent_color(&self->b, percent, current_entry);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300581 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300582 if (symbol_conf.use_callchain) {
583 slsmg_printf("%c ", folded_sign);
584 width -= 2;
585 }
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200586
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200587 slsmg_printf(" %5.2f%%", percent);
588
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200589 /* The scroll bar isn't being used */
590 if (!self->b.navkeypressed)
591 width += 1;
592
Arnaldo Carvalho de Melo33f62b32011-10-18 15:54:24 -0200593 if (!current_entry || !self->b.navkeypressed)
594 ui_browser__set_color(&self->b, HE_COLORSET_NORMAL);
595
Arnaldo Carvalho de Melo2cf9ceb2011-10-19 14:37:59 -0200596 if (symbol_conf.show_nr_samples) {
597 slsmg_printf(" %11u", entry->nr_events);
598 width -= 12;
599 }
600
601 if (symbol_conf.show_total_period) {
602 slsmg_printf(" %12" PRIu64, entry->period);
603 width -= 13;
604 }
605
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300606 slsmg_write_nstring(s, width);
607 ++row;
608 ++printed;
609 } else
610 --row_offset;
611
612 if (folded_sign == '-' && row != self->b.height) {
613 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
614 1, row, &row_offset,
615 &current_entry);
616 if (current_entry)
617 self->he_selection = entry;
618 }
619
620 return printed;
621}
622
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300623static void ui_browser__hists_init_top(struct ui_browser *browser)
624{
625 if (browser->top == NULL) {
626 struct hist_browser *hb;
627
628 hb = container_of(browser, struct hist_browser, b);
629 browser->top = rb_first(&hb->hists->entries);
630 }
631}
632
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300633static unsigned int hist_browser__refresh(struct ui_browser *self)
634{
635 unsigned row = 0;
636 struct rb_node *nd;
637 struct hist_browser *hb = container_of(self, struct hist_browser, b);
638
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300639 ui_browser__hists_init_top(self);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300640
641 for (nd = self->top; nd; nd = rb_next(nd)) {
642 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
643
644 if (h->filtered)
645 continue;
646
647 row += hist_browser__show_entry(hb, h, row);
648 if (row == self->height)
649 break;
650 }
651
652 return row;
653}
654
655static struct rb_node *hists__filter_entries(struct rb_node *nd)
656{
657 while (nd != NULL) {
658 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
659 if (!h->filtered)
660 return nd;
661
662 nd = rb_next(nd);
663 }
664
665 return NULL;
666}
667
668static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
669{
670 while (nd != NULL) {
671 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
672 if (!h->filtered)
673 return nd;
674
675 nd = rb_prev(nd);
676 }
677
678 return NULL;
679}
680
681static void ui_browser__hists_seek(struct ui_browser *self,
682 off_t offset, int whence)
683{
684 struct hist_entry *h;
685 struct rb_node *nd;
686 bool first = true;
687
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300688 if (self->nr_entries == 0)
689 return;
690
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300691 ui_browser__hists_init_top(self);
692
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300693 switch (whence) {
694 case SEEK_SET:
695 nd = hists__filter_entries(rb_first(self->entries));
696 break;
697 case SEEK_CUR:
698 nd = self->top;
699 goto do_offset;
700 case SEEK_END:
701 nd = hists__filter_prev_entries(rb_last(self->entries));
702 first = false;
703 break;
704 default:
705 return;
706 }
707
708 /*
709 * Moves not relative to the first visible entry invalidates its
710 * row_offset:
711 */
712 h = rb_entry(self->top, struct hist_entry, rb_node);
713 h->row_offset = 0;
714
715 /*
716 * Here we have to check if nd is expanded (+), if it is we can't go
717 * the next top level hist_entry, instead we must compute an offset of
718 * what _not_ to show and not change the first visible entry.
719 *
720 * This offset increments when we are going from top to bottom and
721 * decreases when we're going from bottom to top.
722 *
723 * As we don't have backpointers to the top level in the callchains
724 * structure, we need to always print the whole hist_entry callchain,
725 * skipping the first ones that are before the first visible entry
726 * and stop when we printed enough lines to fill the screen.
727 */
728do_offset:
729 if (offset > 0) {
730 do {
731 h = rb_entry(nd, struct hist_entry, rb_node);
732 if (h->ms.unfolded) {
733 u16 remaining = h->nr_rows - h->row_offset;
734 if (offset > remaining) {
735 offset -= remaining;
736 h->row_offset = 0;
737 } else {
738 h->row_offset += offset;
739 offset = 0;
740 self->top = nd;
741 break;
742 }
743 }
744 nd = hists__filter_entries(rb_next(nd));
745 if (nd == NULL)
746 break;
747 --offset;
748 self->top = nd;
749 } while (offset != 0);
750 } else if (offset < 0) {
751 while (1) {
752 h = rb_entry(nd, struct hist_entry, rb_node);
753 if (h->ms.unfolded) {
754 if (first) {
755 if (-offset > h->row_offset) {
756 offset += h->row_offset;
757 h->row_offset = 0;
758 } else {
759 h->row_offset += offset;
760 offset = 0;
761 self->top = nd;
762 break;
763 }
764 } else {
765 if (-offset > h->nr_rows) {
766 offset += h->nr_rows;
767 h->row_offset = 0;
768 } else {
769 h->row_offset = h->nr_rows + offset;
770 offset = 0;
771 self->top = nd;
772 break;
773 }
774 }
775 }
776
777 nd = hists__filter_prev_entries(rb_prev(nd));
778 if (nd == NULL)
779 break;
780 ++offset;
781 self->top = nd;
782 if (offset == 0) {
783 /*
784 * Last unfiltered hist_entry, check if it is
785 * unfolded, if it is then we should have
786 * row_offset at its last entry.
787 */
788 h = rb_entry(nd, struct hist_entry, rb_node);
789 if (h->ms.unfolded)
790 h->row_offset = h->nr_rows;
791 break;
792 }
793 first = false;
794 }
795 } else {
796 self->top = nd;
797 h = rb_entry(nd, struct hist_entry, rb_node);
798 h->row_offset = 0;
799 }
800}
801
802static struct hist_browser *hist_browser__new(struct hists *hists)
803{
804 struct hist_browser *self = zalloc(sizeof(*self));
805
806 if (self) {
807 self->hists = hists;
808 self->b.refresh = hist_browser__refresh;
809 self->b.seek = ui_browser__hists_seek;
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200810 self->b.use_navkeypressed = true,
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300811 self->has_symbols = sort_sym.list.next != NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300812 }
813
814 return self;
815}
816
817static void hist_browser__delete(struct hist_browser *self)
818{
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300819 free(self);
820}
821
822static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
823{
824 return self->he_selection;
825}
826
827static struct thread *hist_browser__selected_thread(struct hist_browser *self)
828{
829 return self->he_selection->thread;
830}
831
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300832static int hists__browser_title(struct hists *self, char *bf, size_t size,
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200833 const char *ev_name)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300834{
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300835 char unit;
836 int printed;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200837 const struct dso *dso = self->dso_filter;
838 const struct thread *thread = self->thread_filter;
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300839 unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE];
840
841 nr_events = convert_unit(nr_events, &unit);
842 printed = snprintf(bf, size, "Events: %lu%c %s", nr_events, unit, ev_name);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300843
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200844 if (self->uid_filter_str)
845 printed += snprintf(bf + printed, size - printed,
846 ", UID: %s", self->uid_filter_str);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300847 if (thread)
848 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300849 ", Thread: %s(%d)",
850 (thread->comm_set ? thread->comm : ""),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300851 thread->pid);
852 if (dso)
853 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300854 ", DSO: %s", dso->short_name);
855 return printed;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300856}
857
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300858static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300859 const char *helpline, const char *ev_name,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300860 bool left_exits,
861 void(*timer)(void *arg), void *arg,
862 int delay_secs)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300863{
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300864 struct hists *self = &evsel->hists;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300865 struct hist_browser *browser = hist_browser__new(self);
866 struct pstack *fstack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300867 int key = -1;
868
869 if (browser == NULL)
870 return -1;
871
872 fstack = pstack__new(2);
873 if (fstack == NULL)
874 goto out;
875
876 ui_helpline__push(helpline);
877
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300878 while (1) {
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300879 const struct thread *thread = NULL;
880 const struct dso *dso = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300881 char *options[16];
882 int nr_options = 0, choice = 0, i,
883 annotate = -2, zoom_dso = -2, zoom_thread = -2,
884 browse_map = -2;
885
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300886 key = hist_browser__run(browser, ev_name, timer, arg, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300887
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300888 if (browser->he_selection != NULL) {
889 thread = hist_browser__selected_thread(browser);
890 dso = browser->selection->map ? browser->selection->map->dso : NULL;
891 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300892
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300893 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200894 case K_TAB:
895 case K_UNTAB:
David Aherne4419b82011-10-19 11:37:47 -0600896 if (nr_events == 1)
897 continue;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300898 /*
899 * Exit the browser, let hists__browser_tree
900 * go to the next or previous
901 */
902 goto out_free_stack;
903 case 'a':
Arnaldo Carvalho de Meloa6e51f92011-10-21 10:58:24 -0200904 if (!browser->has_symbols) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200905 ui_browser__warning(&browser->b, delay_secs * 2,
Arnaldo Carvalho de Meloa6e51f92011-10-21 10:58:24 -0200906 "Annotation is only available for symbolic views, "
907 "include \"sym\" in --sort to use it.");
908 continue;
909 }
910
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300911 if (browser->selection == NULL ||
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800912 browser->selection->sym == NULL ||
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300913 browser->selection->map->dso->annotate_warned)
914 continue;
915 goto do_annotate;
916 case 'd':
917 goto zoom_dso;
918 case 't':
919 goto zoom_thread;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200920 case K_F1:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300921 case 'h':
922 case '?':
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200923 ui_browser__help_window(&browser->b,
924 "h/?/F1 Show this window\n"
Arnaldo Carvalho de Melo2d5646c2011-10-18 13:02:52 -0200925 "UP/DOWN/PGUP\n"
926 "PGDN/SPACE Navigate\n"
927 "q/ESC/CTRL+C Exit browser\n\n"
928 "For multiple event sessions:\n\n"
929 "TAB/UNTAB Switch events\n\n"
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300930 "For symbolic views (--sort has sym):\n\n"
Arnaldo Carvalho de Melo2d5646c2011-10-18 13:02:52 -0200931 "-> Zoom into DSO/Threads & Annotate current symbol\n"
932 "<- Zoom out\n"
933 "a Annotate current symbol\n"
934 "C Collapse all callchains\n"
935 "E Expand all callchains\n"
936 "d Zoom into current DSO\n"
Arnaldo Carvalho de Melo13d8f962011-10-26 08:05:34 -0200937 "t Zoom into current Thread");
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300938 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200939 case K_ENTER:
940 case K_RIGHT:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300941 /* menu */
942 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200943 case K_LEFT: {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300944 const void *top;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300945
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300946 if (pstack__empty(fstack)) {
947 /*
948 * Go back to the perf_evsel_menu__run or other user
949 */
950 if (left_exits)
951 goto out_free_stack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300952 continue;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300953 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300954 top = pstack__pop(fstack);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200955 if (top == &browser->hists->dso_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300956 goto zoom_out_dso;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200957 if (top == &browser->hists->thread_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300958 goto zoom_out_thread;
959 continue;
960 }
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200961 case K_ESC:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300962 if (!left_exits &&
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200963 !ui_browser__dialog_yesno(&browser->b,
964 "Do you really want to exit?"))
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300965 continue;
966 /* Fall thru */
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300967 case 'q':
968 case CTRL('c'):
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300969 goto out_free_stack;
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300970 default:
971 continue;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300972 }
973
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300974 if (!browser->has_symbols)
975 goto add_exit_option;
976
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300977 if (browser->selection != NULL &&
978 browser->selection->sym != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300979 !browser->selection->map->dso->annotate_warned &&
980 asprintf(&options[nr_options], "Annotate %s",
981 browser->selection->sym->name) > 0)
982 annotate = nr_options++;
983
984 if (thread != NULL &&
985 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200986 (browser->hists->thread_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300987 (thread->comm_set ? thread->comm : ""),
988 thread->pid) > 0)
989 zoom_thread = nr_options++;
990
991 if (dso != NULL &&
992 asprintf(&options[nr_options], "Zoom %s %s DSO",
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200993 (browser->hists->dso_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300994 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
995 zoom_dso = nr_options++;
996
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300997 if (browser->selection != NULL &&
998 browser->selection->map != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300999 asprintf(&options[nr_options], "Browse map details") > 0)
1000 browse_map = nr_options++;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -03001001add_exit_option:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001002 options[nr_options++] = (char *)"Exit";
1003
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -03001004 choice = ui__popup_menu(nr_options, options);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001005
1006 for (i = 0; i < nr_options - 1; ++i)
1007 free(options[i]);
1008
1009 if (choice == nr_options - 1)
1010 break;
1011
1012 if (choice == -1)
1013 continue;
1014
1015 if (choice == annotate) {
1016 struct hist_entry *he;
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001017 int err;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001018do_annotate:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001019 he = hist_browser__selected_entry(browser);
1020 if (he == NULL)
1021 continue;
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -03001022 /*
1023 * Don't let this be freed, say, by hists__decay_entry.
1024 */
1025 he->used = true;
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02001026 err = hist_entry__tui_annotate(he, evsel->idx,
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001027 timer, arg, delay_secs);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -03001028 he->used = false;
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -03001029 ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001030 if (err)
1031 ui_browser__handle_resize(&browser->b);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001032 } else if (choice == browse_map)
1033 map__browse(browser->selection->map);
1034 else if (choice == zoom_dso) {
1035zoom_dso:
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001036 if (browser->hists->dso_filter) {
1037 pstack__remove(fstack, &browser->hists->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001038zoom_out_dso:
1039 ui_helpline__pop();
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001040 browser->hists->dso_filter = NULL;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001041 sort_dso.elide = false;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001042 } else {
1043 if (dso == NULL)
1044 continue;
1045 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
1046 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001047 browser->hists->dso_filter = dso;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001048 sort_dso.elide = true;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001049 pstack__push(fstack, &browser->hists->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001050 }
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001051 hists__filter_by_dso(self);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001052 hist_browser__reset(browser);
1053 } else if (choice == zoom_thread) {
1054zoom_thread:
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001055 if (browser->hists->thread_filter) {
1056 pstack__remove(fstack, &browser->hists->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001057zoom_out_thread:
1058 ui_helpline__pop();
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001059 browser->hists->thread_filter = NULL;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001060 sort_thread.elide = false;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001061 } else {
1062 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
1063 thread->comm_set ? thread->comm : "",
1064 thread->pid);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001065 browser->hists->thread_filter = thread;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001066 sort_thread.elide = true;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001067 pstack__push(fstack, &browser->hists->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001068 }
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001069 hists__filter_by_thread(self);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001070 hist_browser__reset(browser);
1071 }
1072 }
1073out_free_stack:
1074 pstack__delete(fstack);
1075out:
1076 hist_browser__delete(browser);
1077 return key;
1078}
1079
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001080struct perf_evsel_menu {
1081 struct ui_browser b;
1082 struct perf_evsel *selection;
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001083 bool lost_events, lost_events_warned;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001084};
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001085
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001086static void perf_evsel_menu__write(struct ui_browser *browser,
1087 void *entry, int row)
1088{
1089 struct perf_evsel_menu *menu = container_of(browser,
1090 struct perf_evsel_menu, b);
1091 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
1092 bool current_entry = ui_browser__is_current_entry(browser, row);
1093 unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1094 const char *ev_name = event_name(evsel);
1095 char bf[256], unit;
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001096 const char *warn = " ";
1097 size_t printed;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001098
1099 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
1100 HE_COLORSET_NORMAL);
1101
1102 nr_events = convert_unit(nr_events, &unit);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001103 printed = snprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
1104 unit, unit == ' ' ? "" : " ", ev_name);
1105 slsmg_printf("%s", bf);
1106
1107 nr_events = evsel->hists.stats.nr_events[PERF_RECORD_LOST];
1108 if (nr_events != 0) {
1109 menu->lost_events = true;
1110 if (!current_entry)
1111 ui_browser__set_color(browser, HE_COLORSET_TOP);
1112 nr_events = convert_unit(nr_events, &unit);
1113 snprintf(bf, sizeof(bf), ": %ld%c%schunks LOST!", nr_events,
1114 unit, unit == ' ' ? "" : " ");
1115 warn = bf;
1116 }
1117
1118 slsmg_write_nstring(warn, browser->width - printed);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001119
1120 if (current_entry)
1121 menu->selection = evsel;
1122}
1123
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001124static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
1125 int nr_events, const char *help,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001126 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001127{
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001128 struct perf_evlist *evlist = menu->b.priv;
1129 struct perf_evsel *pos;
1130 const char *ev_name, *title = "Available samples";
1131 int key;
1132
1133 if (ui_browser__show(&menu->b, title,
1134 "ESC: exit, ENTER|->: Browse histograms") < 0)
1135 return -1;
1136
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001137 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03001138 key = ui_browser__run(&menu->b, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001139
1140 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001141 case K_TIMER:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001142 timer(arg);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001143
1144 if (!menu->lost_events_warned && menu->lost_events) {
1145 ui_browser__warn_lost_events(&menu->b);
1146 menu->lost_events_warned = true;
1147 }
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001148 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001149 case K_RIGHT:
1150 case K_ENTER:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001151 if (!menu->selection)
1152 continue;
1153 pos = menu->selection;
1154browse_hists:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001155 perf_evlist__set_selected(evlist, pos);
1156 /*
1157 * Give the calling tool a chance to populate the non
1158 * default evsel resorted hists tree.
1159 */
1160 if (timer)
1161 timer(arg);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001162 ev_name = event_name(pos);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001163 key = perf_evsel__hists_browse(pos, nr_events, help,
1164 ev_name, true, timer,
1165 arg, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001166 ui_browser__show_title(&menu->b, title);
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001167 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001168 case K_TAB:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001169 if (pos->node.next == &evlist->entries)
1170 pos = list_entry(evlist->entries.next, struct perf_evsel, node);
1171 else
1172 pos = list_entry(pos->node.next, struct perf_evsel, node);
1173 goto browse_hists;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001174 case K_UNTAB:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001175 if (pos->node.prev == &evlist->entries)
1176 pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
1177 else
1178 pos = list_entry(pos->node.prev, struct perf_evsel, node);
1179 goto browse_hists;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001180 case K_ESC:
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001181 if (!ui_browser__dialog_yesno(&menu->b,
1182 "Do you really want to exit?"))
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001183 continue;
1184 /* Fall thru */
1185 case 'q':
1186 case CTRL('c'):
1187 goto out;
1188 default:
1189 continue;
1190 }
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001191 case K_LEFT:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001192 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001193 case K_ESC:
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001194 if (!ui_browser__dialog_yesno(&menu->b,
1195 "Do you really want to exit?"))
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -03001196 continue;
1197 /* Fall thru */
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001198 case 'q':
1199 case CTRL('c'):
1200 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001201 default:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001202 continue;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001203 }
1204 }
1205
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001206out:
1207 ui_browser__hide(&menu->b);
1208 return key;
1209}
1210
1211static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001212 const char *help,
1213 void(*timer)(void *arg), void *arg,
1214 int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001215{
1216 struct perf_evsel *pos;
1217 struct perf_evsel_menu menu = {
1218 .b = {
1219 .entries = &evlist->entries,
1220 .refresh = ui_browser__list_head_refresh,
1221 .seek = ui_browser__list_head_seek,
1222 .write = perf_evsel_menu__write,
1223 .nr_entries = evlist->nr_entries,
1224 .priv = evlist,
1225 },
1226 };
1227
1228 ui_helpline__push("Press ESC to exit");
1229
1230 list_for_each_entry(pos, &evlist->entries, node) {
1231 const char *ev_name = event_name(pos);
1232 size_t line_len = strlen(ev_name) + 7;
1233
1234 if (menu.b.width < line_len)
1235 menu.b.width = line_len;
1236 /*
1237 * Cache the evsel name, tracepoints have a _high_ cost per
1238 * event_name() call.
1239 */
1240 if (pos->name == NULL)
1241 pos->name = strdup(ev_name);
1242 }
1243
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001244 return perf_evsel_menu__run(&menu, evlist->nr_entries, help, timer,
1245 arg, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001246}
1247
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001248int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
1249 void(*timer)(void *arg), void *arg,
1250 int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001251{
1252
1253 if (evlist->nr_entries == 1) {
1254 struct perf_evsel *first = list_entry(evlist->entries.next,
1255 struct perf_evsel, node);
1256 const char *ev_name = event_name(first);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001257 return perf_evsel__hists_browse(first, evlist->nr_entries, help,
1258 ev_name, false, timer, arg,
1259 delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001260 }
1261
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001262 return __perf_evlist__tui_browse_hists(evlist, help,
1263 timer, arg, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001264}