blob: 40fd424704475892277f67616bb3bbf808bcec03 [file] [log] [blame]
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001#include <stdio.h>
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002#include "../libslang.h"
3#include <stdlib.h>
4#include <string.h>
5#include <newt.h>
6#include <linux/rbtree.h>
7
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -03008#include "../../evsel.h"
9#include "../../evlist.h"
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030010#include "../../hist.h"
11#include "../../pstack.h"
12#include "../../sort.h"
13#include "../../util.h"
14
15#include "../browser.h"
16#include "../helpline.h"
17#include "../util.h"
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -020018#include "../ui.h"
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030019#include "map.h"
20
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030021struct hist_browser {
22 struct ui_browser b;
23 struct hists *hists;
24 struct hist_entry *he_selection;
25 struct map_symbol *selection;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -030026 bool has_symbols;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030027};
28
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -030029static int hists__browser_title(struct hists *self, char *bf, size_t size,
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -020030 const char *ev_name);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -030031
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030032static void hist_browser__refresh_dimensions(struct hist_browser *self)
33{
34 /* 3 == +/- toggle symbol before actual hist_entry rendering */
35 self->b.width = 3 + (hists__sort_list_width(self->hists) +
36 sizeof("[k]"));
37}
38
39static void hist_browser__reset(struct hist_browser *self)
40{
41 self->b.nr_entries = self->hists->nr_entries;
42 hist_browser__refresh_dimensions(self);
43 ui_browser__reset_index(&self->b);
44}
45
46static char tree__folded_sign(bool unfolded)
47{
48 return unfolded ? '-' : '+';
49}
50
51static char map_symbol__folded(const struct map_symbol *self)
52{
53 return self->has_children ? tree__folded_sign(self->unfolded) : ' ';
54}
55
56static char hist_entry__folded(const struct hist_entry *self)
57{
58 return map_symbol__folded(&self->ms);
59}
60
61static char callchain_list__folded(const struct callchain_list *self)
62{
63 return map_symbol__folded(&self->ms);
64}
65
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -030066static void map_symbol__set_folding(struct map_symbol *self, bool unfold)
67{
68 self->unfolded = unfold ? self->has_children : false;
69}
70
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030071static int callchain_node__count_rows_rb_tree(struct callchain_node *self)
72{
73 int n = 0;
74 struct rb_node *nd;
75
76 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
77 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
78 struct callchain_list *chain;
79 char folded_sign = ' '; /* No children */
80
81 list_for_each_entry(chain, &child->val, list) {
82 ++n;
83 /* We need this because we may not have children */
84 folded_sign = callchain_list__folded(chain);
85 if (folded_sign == '+')
86 break;
87 }
88
89 if (folded_sign == '-') /* Have children and they're unfolded */
90 n += callchain_node__count_rows_rb_tree(child);
91 }
92
93 return n;
94}
95
96static int callchain_node__count_rows(struct callchain_node *node)
97{
98 struct callchain_list *chain;
99 bool unfolded = false;
100 int n = 0;
101
102 list_for_each_entry(chain, &node->val, list) {
103 ++n;
104 unfolded = chain->ms.unfolded;
105 }
106
107 if (unfolded)
108 n += callchain_node__count_rows_rb_tree(node);
109
110 return n;
111}
112
113static int callchain__count_rows(struct rb_root *chain)
114{
115 struct rb_node *nd;
116 int n = 0;
117
118 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
119 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
120 n += callchain_node__count_rows(node);
121 }
122
123 return n;
124}
125
126static bool map_symbol__toggle_fold(struct map_symbol *self)
127{
128 if (!self->has_children)
129 return false;
130
131 self->unfolded = !self->unfolded;
132 return true;
133}
134
135static void callchain_node__init_have_children_rb_tree(struct callchain_node *self)
136{
137 struct rb_node *nd = rb_first(&self->rb_root);
138
139 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
140 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
141 struct callchain_list *chain;
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300142 bool first = true;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300143
144 list_for_each_entry(chain, &child->val, list) {
145 if (first) {
146 first = false;
147 chain->ms.has_children = chain->list.next != &child->val ||
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300148 !RB_EMPTY_ROOT(&child->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300149 } else
150 chain->ms.has_children = chain->list.next == &child->val &&
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300151 !RB_EMPTY_ROOT(&child->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300152 }
153
154 callchain_node__init_have_children_rb_tree(child);
155 }
156}
157
158static void callchain_node__init_have_children(struct callchain_node *self)
159{
160 struct callchain_list *chain;
161
162 list_for_each_entry(chain, &self->val, list)
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300163 chain->ms.has_children = !RB_EMPTY_ROOT(&self->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300164
165 callchain_node__init_have_children_rb_tree(self);
166}
167
168static void callchain__init_have_children(struct rb_root *self)
169{
170 struct rb_node *nd;
171
172 for (nd = rb_first(self); nd; nd = rb_next(nd)) {
173 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
174 callchain_node__init_have_children(node);
175 }
176}
177
178static void hist_entry__init_have_children(struct hist_entry *self)
179{
180 if (!self->init_have_children) {
Arnaldo Carvalho de Melo18b308d2010-08-25 12:47:44 -0300181 self->ms.has_children = !RB_EMPTY_ROOT(&self->sorted_chain);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300182 callchain__init_have_children(&self->sorted_chain);
183 self->init_have_children = true;
184 }
185}
186
187static bool hist_browser__toggle_fold(struct hist_browser *self)
188{
189 if (map_symbol__toggle_fold(self->selection)) {
190 struct hist_entry *he = self->he_selection;
191
192 hist_entry__init_have_children(he);
193 self->hists->nr_entries -= he->nr_rows;
194
195 if (he->ms.unfolded)
196 he->nr_rows = callchain__count_rows(&he->sorted_chain);
197 else
198 he->nr_rows = 0;
199 self->hists->nr_entries += he->nr_rows;
200 self->b.nr_entries = self->hists->nr_entries;
201
202 return true;
203 }
204
205 /* If it doesn't have children, no toggling performed */
206 return false;
207}
208
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300209static int callchain_node__set_folding_rb_tree(struct callchain_node *self, bool unfold)
210{
211 int n = 0;
212 struct rb_node *nd;
213
214 for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) {
215 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
216 struct callchain_list *chain;
217 bool has_children = false;
218
219 list_for_each_entry(chain, &child->val, list) {
220 ++n;
221 map_symbol__set_folding(&chain->ms, unfold);
222 has_children = chain->ms.has_children;
223 }
224
225 if (has_children)
226 n += callchain_node__set_folding_rb_tree(child, unfold);
227 }
228
229 return n;
230}
231
232static int callchain_node__set_folding(struct callchain_node *node, bool unfold)
233{
234 struct callchain_list *chain;
235 bool has_children = false;
236 int n = 0;
237
238 list_for_each_entry(chain, &node->val, list) {
239 ++n;
240 map_symbol__set_folding(&chain->ms, unfold);
241 has_children = chain->ms.has_children;
242 }
243
244 if (has_children)
245 n += callchain_node__set_folding_rb_tree(node, unfold);
246
247 return n;
248}
249
250static int callchain__set_folding(struct rb_root *chain, bool unfold)
251{
252 struct rb_node *nd;
253 int n = 0;
254
255 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
256 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
257 n += callchain_node__set_folding(node, unfold);
258 }
259
260 return n;
261}
262
263static void hist_entry__set_folding(struct hist_entry *self, bool unfold)
264{
265 hist_entry__init_have_children(self);
266 map_symbol__set_folding(&self->ms, unfold);
267
268 if (self->ms.has_children) {
269 int n = callchain__set_folding(&self->sorted_chain, unfold);
270 self->nr_rows = unfold ? n : 0;
271 } else
272 self->nr_rows = 0;
273}
274
275static void hists__set_folding(struct hists *self, bool unfold)
276{
277 struct rb_node *nd;
278
279 self->nr_entries = 0;
280
281 for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
282 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
283 hist_entry__set_folding(he, unfold);
284 self->nr_entries += 1 + he->nr_rows;
285 }
286}
287
288static void hist_browser__set_folding(struct hist_browser *self, bool unfold)
289{
290 hists__set_folding(self->hists, unfold);
291 self->b.nr_entries = self->hists->nr_entries;
292 /* Go to the start, we may be way after valid entries after a collapse */
293 ui_browser__reset_index(&self->b);
294}
295
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200296static void ui_browser__warn_lost_events(struct ui_browser *browser)
297{
298 ui_browser__warning(browser, 4,
299 "Events are being lost, check IO/CPU overload!\n\n"
300 "You may want to run 'perf' using a RT scheduler policy:\n\n"
301 " perf top -r 80\n\n"
302 "Or reduce the sampling frequency.");
303}
304
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300305static int hist_browser__run(struct hist_browser *self, const char *ev_name,
306 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300307{
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300308 int key;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300309 char title[160];
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300310
311 self->b.entries = &self->hists->entries;
312 self->b.nr_entries = self->hists->nr_entries;
313
314 hist_browser__refresh_dimensions(self);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200315 hists__browser_title(self->hists, title, sizeof(title), ev_name);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300316
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300317 if (ui_browser__show(&self->b, title,
318 "Press '?' for help on key bindings") < 0)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300319 return -1;
320
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300321 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -0300322 key = ui_browser__run(&self->b, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300323
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300324 switch (key) {
Arnaldo Carvalho de Melo13d8f962011-10-26 08:05:34 -0200325 case K_TIMER:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300326 timer(arg);
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -0300327 ui_browser__update_nr_entries(&self->b, self->hists->nr_entries);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200328
329 if (self->hists->stats.nr_lost_warned !=
330 self->hists->stats.nr_events[PERF_RECORD_LOST]) {
331 self->hists->stats.nr_lost_warned =
332 self->hists->stats.nr_events[PERF_RECORD_LOST];
333 ui_browser__warn_lost_events(&self->b);
334 }
335
336 hists__browser_title(self->hists, title, sizeof(title), ev_name);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300337 ui_browser__show_title(&self->b, title);
338 continue;
Arnaldo Carvalho de Melo46941532010-08-10 15:50:07 -0300339 case 'D': { /* Debug */
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300340 static int seq;
341 struct hist_entry *h = rb_entry(self->b.top,
342 struct hist_entry, rb_node);
343 ui_helpline__pop();
344 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
345 seq++, self->b.nr_entries,
346 self->hists->nr_entries,
347 self->b.height,
348 self->b.index,
349 self->b.top_idx,
350 h->row_offset, h->nr_rows);
351 }
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300352 break;
353 case 'C':
354 /* Collapse the whole world. */
355 hist_browser__set_folding(self, false);
356 break;
357 case 'E':
358 /* Expand the whole world. */
359 hist_browser__set_folding(self, true);
360 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200361 case K_ENTER:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300362 if (hist_browser__toggle_fold(self))
363 break;
364 /* fall thru */
365 default:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300366 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300367 }
368 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300369out:
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300370 ui_browser__hide(&self->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300371 return key;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300372}
373
374static char *callchain_list__sym_name(struct callchain_list *self,
375 char *bf, size_t bfsize)
376{
377 if (self->ms.sym)
378 return self->ms.sym->name;
379
Arnaldo Carvalho de Melo9486aa32011-01-22 20:37:02 -0200380 snprintf(bf, bfsize, "%#" PRIx64, self->ip);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300381 return bf;
382}
383
384#define LEVEL_OFFSET_STEP 3
385
386static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self,
387 struct callchain_node *chain_node,
388 u64 total, int level,
389 unsigned short row,
390 off_t *row_offset,
391 bool *is_current_entry)
392{
393 struct rb_node *node;
394 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
395 u64 new_total, remaining;
396
397 if (callchain_param.mode == CHAIN_GRAPH_REL)
398 new_total = chain_node->children_hit;
399 else
400 new_total = total;
401
402 remaining = new_total;
403 node = rb_first(&chain_node->rb_root);
404 while (node) {
405 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
406 struct rb_node *next = rb_next(node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100407 u64 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300408 struct callchain_list *chain;
409 char folded_sign = ' ';
410 int first = true;
411 int extra_offset = 0;
412
413 remaining -= cumul;
414
415 list_for_each_entry(chain, &child->val, list) {
416 char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str;
417 const char *str;
418 int color;
419 bool was_first = first;
420
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300421 if (first)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300422 first = false;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300423 else
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300424 extra_offset = LEVEL_OFFSET_STEP;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300425
426 folded_sign = callchain_list__folded(chain);
427 if (*row_offset != 0) {
428 --*row_offset;
429 goto do_next;
430 }
431
432 alloc_str = NULL;
433 str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
434 if (was_first) {
435 double percent = cumul * 100.0 / new_total;
436
437 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
438 str = "Not enough memory!";
439 else
440 str = alloc_str;
441 }
442
443 color = HE_COLORSET_NORMAL;
444 width = self->b.width - (offset + extra_offset + 2);
445 if (ui_browser__is_current_entry(&self->b, row)) {
446 self->selection = &chain->ms;
447 color = HE_COLORSET_SELECTED;
448 *is_current_entry = true;
449 }
450
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300451 ui_browser__set_color(&self->b, color);
452 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300453 slsmg_write_nstring(" ", offset + extra_offset);
454 slsmg_printf("%c ", folded_sign);
455 slsmg_write_nstring(str, width);
456 free(alloc_str);
457
458 if (++row == self->b.height)
459 goto out;
460do_next:
461 if (folded_sign == '+')
462 break;
463 }
464
465 if (folded_sign == '-') {
466 const int new_level = level + (extra_offset ? 2 : 1);
467 row += hist_browser__show_callchain_node_rb_tree(self, child, new_total,
468 new_level, row, row_offset,
469 is_current_entry);
470 }
471 if (row == self->b.height)
472 goto out;
473 node = next;
474 }
475out:
476 return row - first_row;
477}
478
479static int hist_browser__show_callchain_node(struct hist_browser *self,
480 struct callchain_node *node,
481 int level, unsigned short row,
482 off_t *row_offset,
483 bool *is_current_entry)
484{
485 struct callchain_list *chain;
486 int first_row = row,
487 offset = level * LEVEL_OFFSET_STEP,
488 width = self->b.width - offset;
489 char folded_sign = ' ';
490
491 list_for_each_entry(chain, &node->val, list) {
492 char ipstr[BITS_PER_LONG / 4 + 1], *s;
493 int color;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300494
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300495 folded_sign = callchain_list__folded(chain);
496
497 if (*row_offset != 0) {
498 --*row_offset;
499 continue;
500 }
501
502 color = HE_COLORSET_NORMAL;
503 if (ui_browser__is_current_entry(&self->b, row)) {
504 self->selection = &chain->ms;
505 color = HE_COLORSET_SELECTED;
506 *is_current_entry = true;
507 }
508
509 s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr));
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300510 ui_browser__gotorc(&self->b, row, 0);
511 ui_browser__set_color(&self->b, color);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300512 slsmg_write_nstring(" ", offset);
513 slsmg_printf("%c ", folded_sign);
514 slsmg_write_nstring(s, width - 2);
515
516 if (++row == self->b.height)
517 goto out;
518 }
519
520 if (folded_sign == '-')
521 row += hist_browser__show_callchain_node_rb_tree(self, node,
522 self->hists->stats.total_period,
523 level + 1, row,
524 row_offset,
525 is_current_entry);
526out:
527 return row - first_row;
528}
529
530static int hist_browser__show_callchain(struct hist_browser *self,
531 struct rb_root *chain,
532 int level, unsigned short row,
533 off_t *row_offset,
534 bool *is_current_entry)
535{
536 struct rb_node *nd;
537 int first_row = row;
538
539 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
540 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
541
542 row += hist_browser__show_callchain_node(self, node, level,
543 row, row_offset,
544 is_current_entry);
545 if (row == self->b.height)
546 break;
547 }
548
549 return row - first_row;
550}
551
552static int hist_browser__show_entry(struct hist_browser *self,
553 struct hist_entry *entry,
554 unsigned short row)
555{
556 char s[256];
557 double percent;
558 int printed = 0;
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200559 int width = self->b.width - 6; /* The percentage */
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300560 char folded_sign = ' ';
561 bool current_entry = ui_browser__is_current_entry(&self->b, row);
562 off_t row_offset = entry->row_offset;
563
564 if (current_entry) {
565 self->he_selection = entry;
566 self->selection = &entry->ms;
567 }
568
569 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300570 hist_entry__init_have_children(entry);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300571 folded_sign = hist_entry__folded(entry);
572 }
573
574 if (row_offset == 0) {
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200575 hist_entry__snprintf(entry, s, sizeof(s), self->hists);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300576 percent = (entry->period * 100.0) / self->hists->stats.total_period;
577
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200578 ui_browser__set_percent_color(&self->b, percent, current_entry);
Arnaldo Carvalho de Melo8f9bbc42010-08-11 14:51:47 -0300579 ui_browser__gotorc(&self->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300580 if (symbol_conf.use_callchain) {
581 slsmg_printf("%c ", folded_sign);
582 width -= 2;
583 }
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200584
Arnaldo Carvalho de Melof1cf6022011-10-18 14:37:34 -0200585 slsmg_printf(" %5.2f%%", percent);
586
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200587 /* The scroll bar isn't being used */
588 if (!self->b.navkeypressed)
589 width += 1;
590
Arnaldo Carvalho de Melo33f62b32011-10-18 15:54:24 -0200591 if (!current_entry || !self->b.navkeypressed)
592 ui_browser__set_color(&self->b, HE_COLORSET_NORMAL);
593
Arnaldo Carvalho de Melo2cf9ceb2011-10-19 14:37:59 -0200594 if (symbol_conf.show_nr_samples) {
595 slsmg_printf(" %11u", entry->nr_events);
596 width -= 12;
597 }
598
599 if (symbol_conf.show_total_period) {
600 slsmg_printf(" %12" PRIu64, entry->period);
601 width -= 13;
602 }
603
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300604 slsmg_write_nstring(s, width);
605 ++row;
606 ++printed;
607 } else
608 --row_offset;
609
610 if (folded_sign == '-' && row != self->b.height) {
611 printed += hist_browser__show_callchain(self, &entry->sorted_chain,
612 1, row, &row_offset,
613 &current_entry);
614 if (current_entry)
615 self->he_selection = entry;
616 }
617
618 return printed;
619}
620
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300621static void ui_browser__hists_init_top(struct ui_browser *browser)
622{
623 if (browser->top == NULL) {
624 struct hist_browser *hb;
625
626 hb = container_of(browser, struct hist_browser, b);
627 browser->top = rb_first(&hb->hists->entries);
628 }
629}
630
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300631static unsigned int hist_browser__refresh(struct ui_browser *self)
632{
633 unsigned row = 0;
634 struct rb_node *nd;
635 struct hist_browser *hb = container_of(self, struct hist_browser, b);
636
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300637 ui_browser__hists_init_top(self);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300638
639 for (nd = self->top; nd; nd = rb_next(nd)) {
640 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
641
642 if (h->filtered)
643 continue;
644
645 row += hist_browser__show_entry(hb, h, row);
646 if (row == self->height)
647 break;
648 }
649
650 return row;
651}
652
653static struct rb_node *hists__filter_entries(struct rb_node *nd)
654{
655 while (nd != NULL) {
656 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
657 if (!h->filtered)
658 return nd;
659
660 nd = rb_next(nd);
661 }
662
663 return NULL;
664}
665
666static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
667{
668 while (nd != NULL) {
669 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
670 if (!h->filtered)
671 return nd;
672
673 nd = rb_prev(nd);
674 }
675
676 return NULL;
677}
678
679static void ui_browser__hists_seek(struct ui_browser *self,
680 off_t offset, int whence)
681{
682 struct hist_entry *h;
683 struct rb_node *nd;
684 bool first = true;
685
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300686 if (self->nr_entries == 0)
687 return;
688
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300689 ui_browser__hists_init_top(self);
690
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300691 switch (whence) {
692 case SEEK_SET:
693 nd = hists__filter_entries(rb_first(self->entries));
694 break;
695 case SEEK_CUR:
696 nd = self->top;
697 goto do_offset;
698 case SEEK_END:
699 nd = hists__filter_prev_entries(rb_last(self->entries));
700 first = false;
701 break;
702 default:
703 return;
704 }
705
706 /*
707 * Moves not relative to the first visible entry invalidates its
708 * row_offset:
709 */
710 h = rb_entry(self->top, struct hist_entry, rb_node);
711 h->row_offset = 0;
712
713 /*
714 * Here we have to check if nd is expanded (+), if it is we can't go
715 * the next top level hist_entry, instead we must compute an offset of
716 * what _not_ to show and not change the first visible entry.
717 *
718 * This offset increments when we are going from top to bottom and
719 * decreases when we're going from bottom to top.
720 *
721 * As we don't have backpointers to the top level in the callchains
722 * structure, we need to always print the whole hist_entry callchain,
723 * skipping the first ones that are before the first visible entry
724 * and stop when we printed enough lines to fill the screen.
725 */
726do_offset:
727 if (offset > 0) {
728 do {
729 h = rb_entry(nd, struct hist_entry, rb_node);
730 if (h->ms.unfolded) {
731 u16 remaining = h->nr_rows - h->row_offset;
732 if (offset > remaining) {
733 offset -= remaining;
734 h->row_offset = 0;
735 } else {
736 h->row_offset += offset;
737 offset = 0;
738 self->top = nd;
739 break;
740 }
741 }
742 nd = hists__filter_entries(rb_next(nd));
743 if (nd == NULL)
744 break;
745 --offset;
746 self->top = nd;
747 } while (offset != 0);
748 } else if (offset < 0) {
749 while (1) {
750 h = rb_entry(nd, struct hist_entry, rb_node);
751 if (h->ms.unfolded) {
752 if (first) {
753 if (-offset > h->row_offset) {
754 offset += h->row_offset;
755 h->row_offset = 0;
756 } else {
757 h->row_offset += offset;
758 offset = 0;
759 self->top = nd;
760 break;
761 }
762 } else {
763 if (-offset > h->nr_rows) {
764 offset += h->nr_rows;
765 h->row_offset = 0;
766 } else {
767 h->row_offset = h->nr_rows + offset;
768 offset = 0;
769 self->top = nd;
770 break;
771 }
772 }
773 }
774
775 nd = hists__filter_prev_entries(rb_prev(nd));
776 if (nd == NULL)
777 break;
778 ++offset;
779 self->top = nd;
780 if (offset == 0) {
781 /*
782 * Last unfiltered hist_entry, check if it is
783 * unfolded, if it is then we should have
784 * row_offset at its last entry.
785 */
786 h = rb_entry(nd, struct hist_entry, rb_node);
787 if (h->ms.unfolded)
788 h->row_offset = h->nr_rows;
789 break;
790 }
791 first = false;
792 }
793 } else {
794 self->top = nd;
795 h = rb_entry(nd, struct hist_entry, rb_node);
796 h->row_offset = 0;
797 }
798}
799
800static struct hist_browser *hist_browser__new(struct hists *hists)
801{
802 struct hist_browser *self = zalloc(sizeof(*self));
803
804 if (self) {
805 self->hists = hists;
806 self->b.refresh = hist_browser__refresh;
807 self->b.seek = ui_browser__hists_seek;
Stephane Eraniana68c2c52012-03-08 23:47:48 +0100808 self->b.use_navkeypressed = true;
809 if (sort__branch_mode == 1)
810 self->has_symbols = sort_sym_from.list.next != NULL;
811 else
812 self->has_symbols = sort_sym.list.next != NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300813 }
814
815 return self;
816}
817
818static void hist_browser__delete(struct hist_browser *self)
819{
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300820 free(self);
821}
822
823static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self)
824{
825 return self->he_selection;
826}
827
828static struct thread *hist_browser__selected_thread(struct hist_browser *self)
829{
830 return self->he_selection->thread;
831}
832
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300833static int hists__browser_title(struct hists *self, char *bf, size_t size,
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200834 const char *ev_name)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300835{
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300836 char unit;
837 int printed;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200838 const struct dso *dso = self->dso_filter;
839 const struct thread *thread = self->thread_filter;
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300840 unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE];
841
842 nr_events = convert_unit(nr_events, &unit);
843 printed = snprintf(bf, size, "Events: %lu%c %s", nr_events, unit, ev_name);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300844
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -0200845 if (self->uid_filter_str)
846 printed += snprintf(bf + printed, size - printed,
847 ", UID: %s", self->uid_filter_str);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300848 if (thread)
849 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300850 ", Thread: %s(%d)",
851 (thread->comm_set ? thread->comm : ""),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300852 thread->pid);
853 if (dso)
854 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -0300855 ", DSO: %s", dso->short_name);
856 return printed;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300857}
858
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -0300859static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300860 const char *helpline, const char *ev_name,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300861 bool left_exits,
862 void(*timer)(void *arg), void *arg,
863 int delay_secs)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300864{
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300865 struct hists *self = &evsel->hists;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300866 struct hist_browser *browser = hist_browser__new(self);
Stephane Eraniana68c2c52012-03-08 23:47:48 +0100867 struct branch_info *bi;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300868 struct pstack *fstack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300869 int key = -1;
870
871 if (browser == NULL)
872 return -1;
873
874 fstack = pstack__new(2);
875 if (fstack == NULL)
876 goto out;
877
878 ui_helpline__push(helpline);
879
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300880 while (1) {
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300881 const struct thread *thread = NULL;
882 const struct dso *dso = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300883 char *options[16];
884 int nr_options = 0, choice = 0, i,
885 annotate = -2, zoom_dso = -2, zoom_thread = -2,
Stephane Eraniana68c2c52012-03-08 23:47:48 +0100886 annotate_f = -2, annotate_t = -2, browse_map = -2;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300887
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300888 key = hist_browser__run(browser, ev_name, timer, arg, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300889
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300890 if (browser->he_selection != NULL) {
891 thread = hist_browser__selected_thread(browser);
892 dso = browser->selection->map ? browser->selection->map->dso : NULL;
893 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300894 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200895 case K_TAB:
896 case K_UNTAB:
David Aherne4419b82011-10-19 11:37:47 -0600897 if (nr_events == 1)
898 continue;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300899 /*
900 * Exit the browser, let hists__browser_tree
901 * go to the next or previous
902 */
903 goto out_free_stack;
904 case 'a':
Arnaldo Carvalho de Meloa6e51f92011-10-21 10:58:24 -0200905 if (!browser->has_symbols) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200906 ui_browser__warning(&browser->b, delay_secs * 2,
Arnaldo Carvalho de Meloa6e51f92011-10-21 10:58:24 -0200907 "Annotation is only available for symbolic views, "
Stephane Eraniana68c2c52012-03-08 23:47:48 +0100908 "include \"sym*\" in --sort to use it.");
Arnaldo Carvalho de Meloa6e51f92011-10-21 10:58:24 -0200909 continue;
910 }
911
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300912 if (browser->selection == NULL ||
Lin Mingdb9a9cbc2011-04-08 14:31:26 +0800913 browser->selection->sym == NULL ||
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300914 browser->selection->map->dso->annotate_warned)
915 continue;
916 goto do_annotate;
917 case 'd':
918 goto zoom_dso;
919 case 't':
920 goto zoom_thread;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200921 case K_F1:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300922 case 'h':
923 case '?':
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200924 ui_browser__help_window(&browser->b,
925 "h/?/F1 Show this window\n"
Arnaldo Carvalho de Melo2d5646c2011-10-18 13:02:52 -0200926 "UP/DOWN/PGUP\n"
927 "PGDN/SPACE Navigate\n"
928 "q/ESC/CTRL+C Exit browser\n\n"
929 "For multiple event sessions:\n\n"
930 "TAB/UNTAB Switch events\n\n"
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300931 "For symbolic views (--sort has sym):\n\n"
Arnaldo Carvalho de Melo2d5646c2011-10-18 13:02:52 -0200932 "-> Zoom into DSO/Threads & Annotate current symbol\n"
933 "<- Zoom out\n"
934 "a Annotate current symbol\n"
935 "C Collapse all callchains\n"
936 "E Expand all callchains\n"
937 "d Zoom into current DSO\n"
Arnaldo Carvalho de Melo13d8f962011-10-26 08:05:34 -0200938 "t Zoom into current Thread");
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300939 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200940 case K_ENTER:
941 case K_RIGHT:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300942 /* menu */
943 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200944 case K_LEFT: {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300945 const void *top;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300946
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300947 if (pstack__empty(fstack)) {
948 /*
949 * Go back to the perf_evsel_menu__run or other user
950 */
951 if (left_exits)
952 goto out_free_stack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300953 continue;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300954 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300955 top = pstack__pop(fstack);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200956 if (top == &browser->hists->dso_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300957 goto zoom_out_dso;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -0200958 if (top == &browser->hists->thread_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300959 goto zoom_out_thread;
960 continue;
961 }
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200962 case K_ESC:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -0300963 if (!left_exits &&
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -0200964 !ui_browser__dialog_yesno(&browser->b,
965 "Do you really want to exit?"))
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300966 continue;
967 /* Fall thru */
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300968 case 'q':
969 case CTRL('c'):
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300970 goto out_free_stack;
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -0300971 default:
972 continue;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300973 }
974
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -0300975 if (!browser->has_symbols)
976 goto add_exit_option;
977
Stephane Eraniana68c2c52012-03-08 23:47:48 +0100978 if (sort__branch_mode == 1) {
979 bi = browser->he_selection->branch_info;
980 if (browser->selection != NULL &&
981 bi &&
982 bi->from.sym != NULL &&
983 !bi->from.map->dso->annotate_warned &&
984 asprintf(&options[nr_options], "Annotate %s",
985 bi->from.sym->name) > 0)
986 annotate_f = nr_options++;
987
988 if (browser->selection != NULL &&
989 bi &&
990 bi->to.sym != NULL &&
991 !bi->to.map->dso->annotate_warned &&
Stephane Eranian8bcd65f2012-03-12 16:13:29 +0100992 (bi->to.sym != bi->from.sym ||
993 bi->to.map->dso != bi->from.map->dso) &&
Stephane Eraniana68c2c52012-03-08 23:47:48 +0100994 asprintf(&options[nr_options], "Annotate %s",
995 bi->to.sym->name) > 0)
996 annotate_t = nr_options++;
997 } else {
998
999 if (browser->selection != NULL &&
1000 browser->selection->sym != NULL &&
1001 !browser->selection->map->dso->annotate_warned &&
1002 asprintf(&options[nr_options], "Annotate %s",
1003 browser->selection->sym->name) > 0)
1004 annotate = nr_options++;
1005 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001006
1007 if (thread != NULL &&
1008 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001009 (browser->hists->thread_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001010 (thread->comm_set ? thread->comm : ""),
1011 thread->pid) > 0)
1012 zoom_thread = nr_options++;
1013
1014 if (dso != NULL &&
1015 asprintf(&options[nr_options], "Zoom %s %s DSO",
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001016 (browser->hists->dso_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001017 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
1018 zoom_dso = nr_options++;
1019
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -03001020 if (browser->selection != NULL &&
1021 browser->selection->map != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001022 asprintf(&options[nr_options], "Browse map details") > 0)
1023 browse_map = nr_options++;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -03001024add_exit_option:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001025 options[nr_options++] = (char *)"Exit";
1026
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -03001027 choice = ui__popup_menu(nr_options, options);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001028
1029 for (i = 0; i < nr_options - 1; ++i)
1030 free(options[i]);
1031
1032 if (choice == nr_options - 1)
1033 break;
1034
1035 if (choice == -1)
1036 continue;
1037
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001038 if (choice == annotate || choice == annotate_t || choice == annotate_f) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001039 struct hist_entry *he;
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001040 int err;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001041do_annotate:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001042 he = hist_browser__selected_entry(browser);
1043 if (he == NULL)
1044 continue;
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001045
1046 /*
1047 * we stash the branch_info symbol + map into the
1048 * the ms so we don't have to rewrite all the annotation
1049 * code to use branch_info.
1050 * in branch mode, the ms struct is not used
1051 */
1052 if (choice == annotate_f) {
1053 he->ms.sym = he->branch_info->from.sym;
1054 he->ms.map = he->branch_info->from.map;
1055 } else if (choice == annotate_t) {
1056 he->ms.sym = he->branch_info->to.sym;
1057 he->ms.map = he->branch_info->to.map;
1058 }
1059
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -03001060 /*
1061 * Don't let this be freed, say, by hists__decay_entry.
1062 */
1063 he->used = true;
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -02001064 err = hist_entry__tui_annotate(he, evsel->idx,
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001065 timer, arg, delay_secs);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -03001066 he->used = false;
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -03001067 ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001068 if (err)
1069 ui_browser__handle_resize(&browser->b);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001070 } else if (choice == browse_map)
1071 map__browse(browser->selection->map);
1072 else if (choice == zoom_dso) {
1073zoom_dso:
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001074 if (browser->hists->dso_filter) {
1075 pstack__remove(fstack, &browser->hists->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001076zoom_out_dso:
1077 ui_helpline__pop();
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001078 browser->hists->dso_filter = NULL;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001079 sort_dso.elide = false;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001080 } else {
1081 if (dso == NULL)
1082 continue;
1083 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
1084 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001085 browser->hists->dso_filter = dso;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001086 sort_dso.elide = true;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001087 pstack__push(fstack, &browser->hists->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001088 }
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001089 hists__filter_by_dso(self);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001090 hist_browser__reset(browser);
1091 } else if (choice == zoom_thread) {
1092zoom_thread:
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001093 if (browser->hists->thread_filter) {
1094 pstack__remove(fstack, &browser->hists->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001095zoom_out_thread:
1096 ui_helpline__pop();
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001097 browser->hists->thread_filter = NULL;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001098 sort_thread.elide = false;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001099 } else {
1100 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
1101 thread->comm_set ? thread->comm : "",
1102 thread->pid);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001103 browser->hists->thread_filter = thread;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001104 sort_thread.elide = true;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001105 pstack__push(fstack, &browser->hists->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001106 }
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001107 hists__filter_by_thread(self);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001108 hist_browser__reset(browser);
1109 }
1110 }
1111out_free_stack:
1112 pstack__delete(fstack);
1113out:
1114 hist_browser__delete(browser);
1115 return key;
1116}
1117
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001118struct perf_evsel_menu {
1119 struct ui_browser b;
1120 struct perf_evsel *selection;
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001121 bool lost_events, lost_events_warned;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001122};
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001123
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001124static void perf_evsel_menu__write(struct ui_browser *browser,
1125 void *entry, int row)
1126{
1127 struct perf_evsel_menu *menu = container_of(browser,
1128 struct perf_evsel_menu, b);
1129 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
1130 bool current_entry = ui_browser__is_current_entry(browser, row);
1131 unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1132 const char *ev_name = event_name(evsel);
1133 char bf[256], unit;
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001134 const char *warn = " ";
1135 size_t printed;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001136
1137 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
1138 HE_COLORSET_NORMAL);
1139
1140 nr_events = convert_unit(nr_events, &unit);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001141 printed = snprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
1142 unit, unit == ' ' ? "" : " ", ev_name);
1143 slsmg_printf("%s", bf);
1144
1145 nr_events = evsel->hists.stats.nr_events[PERF_RECORD_LOST];
1146 if (nr_events != 0) {
1147 menu->lost_events = true;
1148 if (!current_entry)
1149 ui_browser__set_color(browser, HE_COLORSET_TOP);
1150 nr_events = convert_unit(nr_events, &unit);
1151 snprintf(bf, sizeof(bf), ": %ld%c%schunks LOST!", nr_events,
1152 unit, unit == ' ' ? "" : " ");
1153 warn = bf;
1154 }
1155
1156 slsmg_write_nstring(warn, browser->width - printed);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001157
1158 if (current_entry)
1159 menu->selection = evsel;
1160}
1161
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001162static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
1163 int nr_events, const char *help,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001164 void(*timer)(void *arg), void *arg, int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001165{
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001166 struct perf_evlist *evlist = menu->b.priv;
1167 struct perf_evsel *pos;
1168 const char *ev_name, *title = "Available samples";
1169 int key;
1170
1171 if (ui_browser__show(&menu->b, title,
1172 "ESC: exit, ENTER|->: Browse histograms") < 0)
1173 return -1;
1174
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001175 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03001176 key = ui_browser__run(&menu->b, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001177
1178 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001179 case K_TIMER:
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001180 timer(arg);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001181
1182 if (!menu->lost_events_warned && menu->lost_events) {
1183 ui_browser__warn_lost_events(&menu->b);
1184 menu->lost_events_warned = true;
1185 }
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001186 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001187 case K_RIGHT:
1188 case K_ENTER:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001189 if (!menu->selection)
1190 continue;
1191 pos = menu->selection;
1192browse_hists:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001193 perf_evlist__set_selected(evlist, pos);
1194 /*
1195 * Give the calling tool a chance to populate the non
1196 * default evsel resorted hists tree.
1197 */
1198 if (timer)
1199 timer(arg);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001200 ev_name = event_name(pos);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001201 key = perf_evsel__hists_browse(pos, nr_events, help,
1202 ev_name, true, timer,
1203 arg, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001204 ui_browser__show_title(&menu->b, title);
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001205 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001206 case K_TAB:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001207 if (pos->node.next == &evlist->entries)
1208 pos = list_entry(evlist->entries.next, struct perf_evsel, node);
1209 else
1210 pos = list_entry(pos->node.next, struct perf_evsel, node);
1211 goto browse_hists;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001212 case K_UNTAB:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001213 if (pos->node.prev == &evlist->entries)
1214 pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
1215 else
1216 pos = list_entry(pos->node.prev, struct perf_evsel, node);
1217 goto browse_hists;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001218 case K_ESC:
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001219 if (!ui_browser__dialog_yesno(&menu->b,
1220 "Do you really want to exit?"))
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001221 continue;
1222 /* Fall thru */
1223 case 'q':
1224 case CTRL('c'):
1225 goto out;
1226 default:
1227 continue;
1228 }
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001229 case K_LEFT:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001230 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001231 case K_ESC:
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001232 if (!ui_browser__dialog_yesno(&menu->b,
1233 "Do you really want to exit?"))
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -03001234 continue;
1235 /* Fall thru */
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001236 case 'q':
1237 case CTRL('c'):
1238 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001239 default:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001240 continue;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001241 }
1242 }
1243
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001244out:
1245 ui_browser__hide(&menu->b);
1246 return key;
1247}
1248
1249static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001250 const char *help,
1251 void(*timer)(void *arg), void *arg,
1252 int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001253{
1254 struct perf_evsel *pos;
1255 struct perf_evsel_menu menu = {
1256 .b = {
1257 .entries = &evlist->entries,
1258 .refresh = ui_browser__list_head_refresh,
1259 .seek = ui_browser__list_head_seek,
1260 .write = perf_evsel_menu__write,
1261 .nr_entries = evlist->nr_entries,
1262 .priv = evlist,
1263 },
1264 };
1265
1266 ui_helpline__push("Press ESC to exit");
1267
1268 list_for_each_entry(pos, &evlist->entries, node) {
1269 const char *ev_name = event_name(pos);
1270 size_t line_len = strlen(ev_name) + 7;
1271
1272 if (menu.b.width < line_len)
1273 menu.b.width = line_len;
1274 /*
1275 * Cache the evsel name, tracepoints have a _high_ cost per
1276 * event_name() call.
1277 */
1278 if (pos->name == NULL)
1279 pos->name = strdup(ev_name);
1280 }
1281
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001282 return perf_evsel_menu__run(&menu, evlist->nr_entries, help, timer,
1283 arg, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001284}
1285
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001286int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
1287 void(*timer)(void *arg), void *arg,
1288 int delay_secs)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001289{
1290
1291 if (evlist->nr_entries == 1) {
1292 struct perf_evsel *first = list_entry(evlist->entries.next,
1293 struct perf_evsel, node);
1294 const char *ev_name = event_name(first);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001295 return perf_evsel__hists_browse(first, evlist->nr_entries, help,
1296 ev_name, false, timer, arg,
1297 delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001298 }
1299
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001300 return __perf_evlist__tui_browse_hists(evlist, help,
1301 timer, arg, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001302}