blob: 2cb39164a1cd7f7dd292fafba4716f8f044a451c [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>
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03005#include <linux/rbtree.h>
6
Namhyung Kimaca7a942012-04-04 00:14:26 -07007#include "../../util/evsel.h"
8#include "../../util/evlist.h"
9#include "../../util/hist.h"
10#include "../../util/pstack.h"
11#include "../../util/sort.h"
12#include "../../util/util.h"
Namhyung Kim68d80752012-11-02 14:50:06 +090013#include "../../arch/common.h"
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030014
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 Meloaff3f3f2012-06-07 19:31:28 -030026 int print_seq;
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -030027 bool show_dso;
Namhyung Kim064f1982013-05-14 11:09:04 +090028 float min_pcnt;
29 u64 nr_pcnt_entries;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030030};
31
Namhyung Kimf5951d52012-09-03 11:53:09 +090032extern void hist_browser__init_hpp(void);
33
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030034static int hists__browser_title(struct hists *hists, char *bf, size_t size,
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -020035 const char *ev_name);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -030036
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030037static void hist_browser__refresh_dimensions(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030038{
39 /* 3 == +/- toggle symbol before actual hist_entry rendering */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030040 browser->b.width = 3 + (hists__sort_list_width(browser->hists) +
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030041 sizeof("[k]"));
42}
43
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030044static void hist_browser__reset(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030045{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030046 browser->b.nr_entries = browser->hists->nr_entries;
47 hist_browser__refresh_dimensions(browser);
48 ui_browser__reset_index(&browser->b);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030049}
50
51static char tree__folded_sign(bool unfolded)
52{
53 return unfolded ? '-' : '+';
54}
55
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030056static char map_symbol__folded(const struct map_symbol *ms)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030057{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030058 return ms->has_children ? tree__folded_sign(ms->unfolded) : ' ';
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030059}
60
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030061static char hist_entry__folded(const struct hist_entry *he)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030062{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030063 return map_symbol__folded(&he->ms);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030064}
65
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030066static char callchain_list__folded(const struct callchain_list *cl)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030067{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030068 return map_symbol__folded(&cl->ms);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030069}
70
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030071static void map_symbol__set_folding(struct map_symbol *ms, bool unfold)
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -030072{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030073 ms->unfolded = unfold ? ms->has_children : false;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -030074}
75
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030076static int callchain_node__count_rows_rb_tree(struct callchain_node *node)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030077{
78 int n = 0;
79 struct rb_node *nd;
80
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030081 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030082 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
83 struct callchain_list *chain;
84 char folded_sign = ' '; /* No children */
85
86 list_for_each_entry(chain, &child->val, list) {
87 ++n;
88 /* We need this because we may not have children */
89 folded_sign = callchain_list__folded(chain);
90 if (folded_sign == '+')
91 break;
92 }
93
94 if (folded_sign == '-') /* Have children and they're unfolded */
95 n += callchain_node__count_rows_rb_tree(child);
96 }
97
98 return n;
99}
100
101static int callchain_node__count_rows(struct callchain_node *node)
102{
103 struct callchain_list *chain;
104 bool unfolded = false;
105 int n = 0;
106
107 list_for_each_entry(chain, &node->val, list) {
108 ++n;
109 unfolded = chain->ms.unfolded;
110 }
111
112 if (unfolded)
113 n += callchain_node__count_rows_rb_tree(node);
114
115 return n;
116}
117
118static int callchain__count_rows(struct rb_root *chain)
119{
120 struct rb_node *nd;
121 int n = 0;
122
123 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
124 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
125 n += callchain_node__count_rows(node);
126 }
127
128 return n;
129}
130
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300131static bool map_symbol__toggle_fold(struct map_symbol *ms)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300132{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300133 if (!ms)
Jiri Olsa8493fe12012-04-04 22:21:31 +0200134 return false;
135
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300136 if (!ms->has_children)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300137 return false;
138
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300139 ms->unfolded = !ms->unfolded;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300140 return true;
141}
142
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300143static void callchain_node__init_have_children_rb_tree(struct callchain_node *node)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300144{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300145 struct rb_node *nd = rb_first(&node->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300146
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300147 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300148 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
149 struct callchain_list *chain;
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300150 bool first = true;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300151
152 list_for_each_entry(chain, &child->val, list) {
153 if (first) {
154 first = false;
155 chain->ms.has_children = chain->list.next != &child->val ||
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300156 !RB_EMPTY_ROOT(&child->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300157 } else
158 chain->ms.has_children = chain->list.next == &child->val &&
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300159 !RB_EMPTY_ROOT(&child->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300160 }
161
162 callchain_node__init_have_children_rb_tree(child);
163 }
164}
165
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300166static void callchain_node__init_have_children(struct callchain_node *node)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300167{
168 struct callchain_list *chain;
169
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300170 list_for_each_entry(chain, &node->val, list)
171 chain->ms.has_children = !RB_EMPTY_ROOT(&node->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300172
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300173 callchain_node__init_have_children_rb_tree(node);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300174}
175
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300176static void callchain__init_have_children(struct rb_root *root)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300177{
178 struct rb_node *nd;
179
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300180 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300181 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
182 callchain_node__init_have_children(node);
183 }
184}
185
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300186static void hist_entry__init_have_children(struct hist_entry *he)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300187{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300188 if (!he->init_have_children) {
189 he->ms.has_children = !RB_EMPTY_ROOT(&he->sorted_chain);
190 callchain__init_have_children(&he->sorted_chain);
191 he->init_have_children = true;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300192 }
193}
194
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300195static bool hist_browser__toggle_fold(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300196{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300197 if (map_symbol__toggle_fold(browser->selection)) {
198 struct hist_entry *he = browser->he_selection;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300199
200 hist_entry__init_have_children(he);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300201 browser->hists->nr_entries -= he->nr_rows;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300202
203 if (he->ms.unfolded)
204 he->nr_rows = callchain__count_rows(&he->sorted_chain);
205 else
206 he->nr_rows = 0;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300207 browser->hists->nr_entries += he->nr_rows;
208 browser->b.nr_entries = browser->hists->nr_entries;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300209
210 return true;
211 }
212
213 /* If it doesn't have children, no toggling performed */
214 return false;
215}
216
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300217static int callchain_node__set_folding_rb_tree(struct callchain_node *node, bool unfold)
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300218{
219 int n = 0;
220 struct rb_node *nd;
221
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300222 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300223 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
224 struct callchain_list *chain;
225 bool has_children = false;
226
227 list_for_each_entry(chain, &child->val, list) {
228 ++n;
229 map_symbol__set_folding(&chain->ms, unfold);
230 has_children = chain->ms.has_children;
231 }
232
233 if (has_children)
234 n += callchain_node__set_folding_rb_tree(child, unfold);
235 }
236
237 return n;
238}
239
240static int callchain_node__set_folding(struct callchain_node *node, bool unfold)
241{
242 struct callchain_list *chain;
243 bool has_children = false;
244 int n = 0;
245
246 list_for_each_entry(chain, &node->val, list) {
247 ++n;
248 map_symbol__set_folding(&chain->ms, unfold);
249 has_children = chain->ms.has_children;
250 }
251
252 if (has_children)
253 n += callchain_node__set_folding_rb_tree(node, unfold);
254
255 return n;
256}
257
258static int callchain__set_folding(struct rb_root *chain, bool unfold)
259{
260 struct rb_node *nd;
261 int n = 0;
262
263 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
264 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
265 n += callchain_node__set_folding(node, unfold);
266 }
267
268 return n;
269}
270
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300271static void hist_entry__set_folding(struct hist_entry *he, bool unfold)
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300272{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300273 hist_entry__init_have_children(he);
274 map_symbol__set_folding(&he->ms, unfold);
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300275
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300276 if (he->ms.has_children) {
277 int n = callchain__set_folding(&he->sorted_chain, unfold);
278 he->nr_rows = unfold ? n : 0;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300279 } else
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300280 he->nr_rows = 0;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300281}
282
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300283static void hists__set_folding(struct hists *hists, bool unfold)
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300284{
285 struct rb_node *nd;
286
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300287 hists->nr_entries = 0;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300288
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300289 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300290 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
291 hist_entry__set_folding(he, unfold);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300292 hists->nr_entries += 1 + he->nr_rows;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300293 }
294}
295
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300296static void hist_browser__set_folding(struct hist_browser *browser, bool unfold)
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300297{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300298 hists__set_folding(browser->hists, unfold);
299 browser->b.nr_entries = browser->hists->nr_entries;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300300 /* Go to the start, we may be way after valid entries after a collapse */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300301 ui_browser__reset_index(&browser->b);
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300302}
303
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200304static void ui_browser__warn_lost_events(struct ui_browser *browser)
305{
306 ui_browser__warning(browser, 4,
307 "Events are being lost, check IO/CPU overload!\n\n"
308 "You may want to run 'perf' using a RT scheduler policy:\n\n"
309 " perf top -r 80\n\n"
310 "Or reduce the sampling frequency.");
311}
312
Namhyung Kimfa5df942013-05-14 11:09:05 +0900313static void hist_browser__update_pcnt_entries(struct hist_browser *hb);
314
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300315static int hist_browser__run(struct hist_browser *browser, const char *ev_name,
Namhyung Kim9783adf2012-11-02 14:50:05 +0900316 struct hist_browser_timer *hbt)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300317{
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300318 int key;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300319 char title[160];
Namhyung Kim9783adf2012-11-02 14:50:05 +0900320 int delay_secs = hbt ? hbt->refresh : 0;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300321
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300322 browser->b.entries = &browser->hists->entries;
323 browser->b.nr_entries = browser->hists->nr_entries;
Namhyung Kim064f1982013-05-14 11:09:04 +0900324 if (browser->min_pcnt)
325 browser->b.nr_entries = browser->nr_pcnt_entries;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300326
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300327 hist_browser__refresh_dimensions(browser);
328 hists__browser_title(browser->hists, title, sizeof(title), ev_name);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300329
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300330 if (ui_browser__show(&browser->b, title,
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300331 "Press '?' for help on key bindings") < 0)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300332 return -1;
333
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300334 while (1) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300335 key = ui_browser__run(&browser->b, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300336
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300337 switch (key) {
Namhyung Kimfa5df942013-05-14 11:09:05 +0900338 case K_TIMER: {
339 u64 nr_entries;
Namhyung Kim9783adf2012-11-02 14:50:05 +0900340 hbt->timer(hbt->arg);
Namhyung Kimfa5df942013-05-14 11:09:05 +0900341
342 if (browser->min_pcnt) {
343 hist_browser__update_pcnt_entries(browser);
344 nr_entries = browser->nr_pcnt_entries;
345 } else {
346 nr_entries = browser->hists->nr_entries;
347 }
348
349 ui_browser__update_nr_entries(&browser->b, nr_entries);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200350
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300351 if (browser->hists->stats.nr_lost_warned !=
352 browser->hists->stats.nr_events[PERF_RECORD_LOST]) {
353 browser->hists->stats.nr_lost_warned =
354 browser->hists->stats.nr_events[PERF_RECORD_LOST];
355 ui_browser__warn_lost_events(&browser->b);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200356 }
357
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300358 hists__browser_title(browser->hists, title, sizeof(title), ev_name);
359 ui_browser__show_title(&browser->b, title);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300360 continue;
Namhyung Kimfa5df942013-05-14 11:09:05 +0900361 }
Arnaldo Carvalho de Melo46941532010-08-10 15:50:07 -0300362 case 'D': { /* Debug */
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300363 static int seq;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300364 struct hist_entry *h = rb_entry(browser->b.top,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300365 struct hist_entry, rb_node);
366 ui_helpline__pop();
367 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300368 seq++, browser->b.nr_entries,
369 browser->hists->nr_entries,
370 browser->b.height,
371 browser->b.index,
372 browser->b.top_idx,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300373 h->row_offset, h->nr_rows);
374 }
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300375 break;
376 case 'C':
377 /* Collapse the whole world. */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300378 hist_browser__set_folding(browser, false);
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300379 break;
380 case 'E':
381 /* Expand the whole world. */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300382 hist_browser__set_folding(browser, true);
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300383 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200384 case K_ENTER:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300385 if (hist_browser__toggle_fold(browser))
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300386 break;
387 /* fall thru */
388 default:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300389 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300390 }
391 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300392out:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300393 ui_browser__hide(&browser->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300394 return key;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300395}
396
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300397static char *callchain_list__sym_name(struct callchain_list *cl,
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -0300398 char *bf, size_t bfsize, bool show_dso)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300399{
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -0300400 int printed;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300401
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -0300402 if (cl->ms.sym)
403 printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
404 else
405 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
406
407 if (show_dso)
408 scnprintf(bf + printed, bfsize - printed, " %s",
409 cl->ms.map ? cl->ms.map->dso->short_name : "unknown");
410
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300411 return bf;
412}
413
414#define LEVEL_OFFSET_STEP 3
415
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300416static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *browser,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300417 struct callchain_node *chain_node,
418 u64 total, int level,
419 unsigned short row,
420 off_t *row_offset,
421 bool *is_current_entry)
422{
423 struct rb_node *node;
424 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
425 u64 new_total, remaining;
426
427 if (callchain_param.mode == CHAIN_GRAPH_REL)
428 new_total = chain_node->children_hit;
429 else
430 new_total = total;
431
432 remaining = new_total;
433 node = rb_first(&chain_node->rb_root);
434 while (node) {
435 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
436 struct rb_node *next = rb_next(node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100437 u64 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300438 struct callchain_list *chain;
439 char folded_sign = ' ';
440 int first = true;
441 int extra_offset = 0;
442
443 remaining -= cumul;
444
445 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -0300446 char bf[1024], *alloc_str;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300447 const char *str;
448 int color;
449 bool was_first = first;
450
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300451 if (first)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300452 first = false;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300453 else
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300454 extra_offset = LEVEL_OFFSET_STEP;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300455
456 folded_sign = callchain_list__folded(chain);
457 if (*row_offset != 0) {
458 --*row_offset;
459 goto do_next;
460 }
461
462 alloc_str = NULL;
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -0300463 str = callchain_list__sym_name(chain, bf, sizeof(bf),
464 browser->show_dso);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300465 if (was_first) {
466 double percent = cumul * 100.0 / new_total;
467
468 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
469 str = "Not enough memory!";
470 else
471 str = alloc_str;
472 }
473
474 color = HE_COLORSET_NORMAL;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300475 width = browser->b.width - (offset + extra_offset + 2);
476 if (ui_browser__is_current_entry(&browser->b, row)) {
477 browser->selection = &chain->ms;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300478 color = HE_COLORSET_SELECTED;
479 *is_current_entry = true;
480 }
481
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300482 ui_browser__set_color(&browser->b, color);
483 ui_browser__gotorc(&browser->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300484 slsmg_write_nstring(" ", offset + extra_offset);
485 slsmg_printf("%c ", folded_sign);
486 slsmg_write_nstring(str, width);
487 free(alloc_str);
488
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300489 if (++row == browser->b.height)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300490 goto out;
491do_next:
492 if (folded_sign == '+')
493 break;
494 }
495
496 if (folded_sign == '-') {
497 const int new_level = level + (extra_offset ? 2 : 1);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300498 row += hist_browser__show_callchain_node_rb_tree(browser, child, new_total,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300499 new_level, row, row_offset,
500 is_current_entry);
501 }
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300502 if (row == browser->b.height)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300503 goto out;
504 node = next;
505 }
506out:
507 return row - first_row;
508}
509
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300510static int hist_browser__show_callchain_node(struct hist_browser *browser,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300511 struct callchain_node *node,
512 int level, unsigned short row,
513 off_t *row_offset,
514 bool *is_current_entry)
515{
516 struct callchain_list *chain;
517 int first_row = row,
518 offset = level * LEVEL_OFFSET_STEP,
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300519 width = browser->b.width - offset;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300520 char folded_sign = ' ';
521
522 list_for_each_entry(chain, &node->val, list) {
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -0300523 char bf[1024], *s;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300524 int color;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300525
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300526 folded_sign = callchain_list__folded(chain);
527
528 if (*row_offset != 0) {
529 --*row_offset;
530 continue;
531 }
532
533 color = HE_COLORSET_NORMAL;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300534 if (ui_browser__is_current_entry(&browser->b, row)) {
535 browser->selection = &chain->ms;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300536 color = HE_COLORSET_SELECTED;
537 *is_current_entry = true;
538 }
539
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -0300540 s = callchain_list__sym_name(chain, bf, sizeof(bf),
541 browser->show_dso);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300542 ui_browser__gotorc(&browser->b, row, 0);
543 ui_browser__set_color(&browser->b, color);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300544 slsmg_write_nstring(" ", offset);
545 slsmg_printf("%c ", folded_sign);
546 slsmg_write_nstring(s, width - 2);
547
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300548 if (++row == browser->b.height)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300549 goto out;
550 }
551
552 if (folded_sign == '-')
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300553 row += hist_browser__show_callchain_node_rb_tree(browser, node,
554 browser->hists->stats.total_period,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300555 level + 1, row,
556 row_offset,
557 is_current_entry);
558out:
559 return row - first_row;
560}
561
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300562static int hist_browser__show_callchain(struct hist_browser *browser,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300563 struct rb_root *chain,
564 int level, unsigned short row,
565 off_t *row_offset,
566 bool *is_current_entry)
567{
568 struct rb_node *nd;
569 int first_row = row;
570
571 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
572 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
573
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300574 row += hist_browser__show_callchain_node(browser, node, level,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300575 row, row_offset,
576 is_current_entry);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300577 if (row == browser->b.height)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300578 break;
579 }
580
581 return row - first_row;
582}
583
Namhyung Kim89701462013-01-22 18:09:38 +0900584struct hpp_arg {
585 struct ui_browser *b;
586 char folded_sign;
587 bool current_entry;
588};
589
590static int __hpp__color_callchain(struct hpp_arg *arg)
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900591{
Namhyung Kim89701462013-01-22 18:09:38 +0900592 if (!symbol_conf.use_callchain)
593 return 0;
594
595 slsmg_printf("%c ", arg->folded_sign);
596 return 2;
597}
598
599static int __hpp__color_fmt(struct perf_hpp *hpp, struct hist_entry *he,
600 u64 (*get_field)(struct hist_entry *),
601 int (*callchain_cb)(struct hpp_arg *))
602{
603 int ret = 0;
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900604 double percent = 0.0;
605 struct hists *hists = he->hists;
Namhyung Kim89701462013-01-22 18:09:38 +0900606 struct hpp_arg *arg = hpp->ptr;
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900607
608 if (hists->stats.total_period)
609 percent = 100.0 * get_field(he) / hists->stats.total_period;
610
Namhyung Kim89701462013-01-22 18:09:38 +0900611 ui_browser__set_percent_color(arg->b, percent, arg->current_entry);
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900612
Namhyung Kim89701462013-01-22 18:09:38 +0900613 if (callchain_cb)
614 ret += callchain_cb(arg);
615
616 ret += scnprintf(hpp->buf, hpp->size, "%6.2f%%", percent);
617 slsmg_printf("%s", hpp->buf);
618
Namhyung Kim371d8c42013-01-22 18:09:39 +0900619 if (symbol_conf.event_group) {
620 int prev_idx, idx_delta;
621 struct perf_evsel *evsel = hists_to_evsel(hists);
622 struct hist_entry *pair;
623 int nr_members = evsel->nr_members;
624
625 if (nr_members <= 1)
626 goto out;
627
628 prev_idx = perf_evsel__group_idx(evsel);
629
630 list_for_each_entry(pair, &he->pairs.head, pairs.node) {
631 u64 period = get_field(pair);
632 u64 total = pair->hists->stats.total_period;
633
634 if (!total)
635 continue;
636
637 evsel = hists_to_evsel(pair->hists);
638 idx_delta = perf_evsel__group_idx(evsel) - prev_idx - 1;
639
640 while (idx_delta--) {
641 /*
642 * zero-fill group members in the middle which
643 * have no sample
644 */
645 ui_browser__set_percent_color(arg->b, 0.0,
646 arg->current_entry);
647 ret += scnprintf(hpp->buf, hpp->size,
648 " %6.2f%%", 0.0);
649 slsmg_printf("%s", hpp->buf);
650 }
651
652 percent = 100.0 * period / total;
653 ui_browser__set_percent_color(arg->b, percent,
654 arg->current_entry);
655 ret += scnprintf(hpp->buf, hpp->size,
656 " %6.2f%%", percent);
657 slsmg_printf("%s", hpp->buf);
658
659 prev_idx = perf_evsel__group_idx(evsel);
660 }
661
662 idx_delta = nr_members - prev_idx - 1;
663
664 while (idx_delta--) {
665 /*
666 * zero-fill group members at last which have no sample
667 */
668 ui_browser__set_percent_color(arg->b, 0.0,
669 arg->current_entry);
670 ret += scnprintf(hpp->buf, hpp->size,
671 " %6.2f%%", 0.0);
672 slsmg_printf("%s", hpp->buf);
673 }
674 }
675out:
Namhyung Kim89701462013-01-22 18:09:38 +0900676 if (!arg->current_entry || !arg->b->navkeypressed)
677 ui_browser__set_color(arg->b, HE_COLORSET_NORMAL);
678
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900679 return ret;
Namhyung Kimf5951d52012-09-03 11:53:09 +0900680}
681
Namhyung Kim89701462013-01-22 18:09:38 +0900682#define __HPP_COLOR_PERCENT_FN(_type, _field, _cb) \
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900683static u64 __hpp_get_##_field(struct hist_entry *he) \
684{ \
685 return he->stat._field; \
686} \
687 \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100688static int \
689hist_browser__hpp_color_##_type(struct perf_hpp_fmt *fmt __maybe_unused,\
690 struct perf_hpp *hpp, \
691 struct hist_entry *he) \
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900692{ \
Namhyung Kim89701462013-01-22 18:09:38 +0900693 return __hpp__color_fmt(hpp, he, __hpp_get_##_field, _cb); \
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900694}
Namhyung Kimf5951d52012-09-03 11:53:09 +0900695
Namhyung Kim89701462013-01-22 18:09:38 +0900696__HPP_COLOR_PERCENT_FN(overhead, period, __hpp__color_callchain)
697__HPP_COLOR_PERCENT_FN(overhead_sys, period_sys, NULL)
698__HPP_COLOR_PERCENT_FN(overhead_us, period_us, NULL)
699__HPP_COLOR_PERCENT_FN(overhead_guest_sys, period_guest_sys, NULL)
700__HPP_COLOR_PERCENT_FN(overhead_guest_us, period_guest_us, NULL)
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900701
702#undef __HPP_COLOR_PERCENT_FN
Namhyung Kimf5951d52012-09-03 11:53:09 +0900703
704void hist_browser__init_hpp(void)
705{
Jiri Olsa12400052012-10-13 00:06:16 +0200706 perf_hpp__column_enable(PERF_HPP__OVERHEAD);
707
Jiri Olsa1d778222012-10-04 21:49:39 +0900708 perf_hpp__init();
Namhyung Kimf5951d52012-09-03 11:53:09 +0900709
710 perf_hpp__format[PERF_HPP__OVERHEAD].color =
711 hist_browser__hpp_color_overhead;
712 perf_hpp__format[PERF_HPP__OVERHEAD_SYS].color =
713 hist_browser__hpp_color_overhead_sys;
714 perf_hpp__format[PERF_HPP__OVERHEAD_US].color =
715 hist_browser__hpp_color_overhead_us;
716 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].color =
717 hist_browser__hpp_color_overhead_guest_sys;
718 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].color =
719 hist_browser__hpp_color_overhead_guest_us;
720}
721
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300722static int hist_browser__show_entry(struct hist_browser *browser,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300723 struct hist_entry *entry,
724 unsigned short row)
725{
726 char s[256];
Jiri Olsa12400052012-10-13 00:06:16 +0200727 int printed = 0;
Namhyung Kim67d25912012-09-12 15:35:06 +0900728 int width = browser->b.width;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300729 char folded_sign = ' ';
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300730 bool current_entry = ui_browser__is_current_entry(&browser->b, row);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300731 off_t row_offset = entry->row_offset;
Namhyung Kim63a1a3d2012-10-15 18:14:35 -0300732 bool first = true;
Jiri Olsa12400052012-10-13 00:06:16 +0200733 struct perf_hpp_fmt *fmt;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300734
735 if (current_entry) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300736 browser->he_selection = entry;
737 browser->selection = &entry->ms;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300738 }
739
740 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300741 hist_entry__init_have_children(entry);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300742 folded_sign = hist_entry__folded(entry);
743 }
744
745 if (row_offset == 0) {
Namhyung Kim89701462013-01-22 18:09:38 +0900746 struct hpp_arg arg = {
747 .b = &browser->b,
748 .folded_sign = folded_sign,
749 .current_entry = current_entry,
750 };
Namhyung Kimf5951d52012-09-03 11:53:09 +0900751 struct perf_hpp hpp = {
752 .buf = s,
753 .size = sizeof(s),
Namhyung Kim89701462013-01-22 18:09:38 +0900754 .ptr = &arg,
Namhyung Kimf5951d52012-09-03 11:53:09 +0900755 };
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300756
Namhyung Kim67d25912012-09-12 15:35:06 +0900757 ui_browser__gotorc(&browser->b, row, 0);
Namhyung Kimf5951d52012-09-03 11:53:09 +0900758
Jiri Olsa12400052012-10-13 00:06:16 +0200759 perf_hpp__for_each_format(fmt) {
Namhyung Kim63a1a3d2012-10-15 18:14:35 -0300760 if (!first) {
Namhyung Kimf5951d52012-09-03 11:53:09 +0900761 slsmg_printf(" ");
762 width -= 2;
763 }
Namhyung Kim63a1a3d2012-10-15 18:14:35 -0300764 first = false;
Namhyung Kimf5951d52012-09-03 11:53:09 +0900765
Jiri Olsa12400052012-10-13 00:06:16 +0200766 if (fmt->color) {
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100767 width -= fmt->color(fmt, &hpp, entry);
Namhyung Kimf5951d52012-09-03 11:53:09 +0900768 } else {
Jiri Olsa2c5d4b42013-01-31 23:31:11 +0100769 width -= fmt->entry(fmt, &hpp, entry);
Namhyung Kimf5951d52012-09-03 11:53:09 +0900770 slsmg_printf("%s", s);
771 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300772 }
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200773
774 /* The scroll bar isn't being used */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300775 if (!browser->b.navkeypressed)
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200776 width += 1;
777
Namhyung Kimf5951d52012-09-03 11:53:09 +0900778 hist_entry__sort_snprintf(entry, s, sizeof(s), browser->hists);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300779 slsmg_write_nstring(s, width);
780 ++row;
781 ++printed;
782 } else
783 --row_offset;
784
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300785 if (folded_sign == '-' && row != browser->b.height) {
786 printed += hist_browser__show_callchain(browser, &entry->sorted_chain,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300787 1, row, &row_offset,
788 &current_entry);
789 if (current_entry)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300790 browser->he_selection = entry;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300791 }
792
793 return printed;
794}
795
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300796static void ui_browser__hists_init_top(struct ui_browser *browser)
797{
798 if (browser->top == NULL) {
799 struct hist_browser *hb;
800
801 hb = container_of(browser, struct hist_browser, b);
802 browser->top = rb_first(&hb->hists->entries);
803 }
804}
805
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300806static unsigned int hist_browser__refresh(struct ui_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300807{
808 unsigned row = 0;
809 struct rb_node *nd;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300810 struct hist_browser *hb = container_of(browser, struct hist_browser, b);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300811
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300812 ui_browser__hists_init_top(browser);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300813
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300814 for (nd = browser->top; nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300815 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
Namhyung Kim064f1982013-05-14 11:09:04 +0900816 float percent = h->stat.period * 100.0 /
817 hb->hists->stats.total_period;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300818
819 if (h->filtered)
820 continue;
821
Namhyung Kim064f1982013-05-14 11:09:04 +0900822 if (percent < hb->min_pcnt)
823 continue;
824
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300825 row += hist_browser__show_entry(hb, h, row);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300826 if (row == browser->height)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300827 break;
828 }
829
830 return row;
831}
832
Namhyung Kim064f1982013-05-14 11:09:04 +0900833static struct rb_node *hists__filter_entries(struct rb_node *nd,
834 struct hists *hists,
835 float min_pcnt)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300836{
837 while (nd != NULL) {
838 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
Namhyung Kim064f1982013-05-14 11:09:04 +0900839 float percent = h->stat.period * 100.0 /
840 hists->stats.total_period;
841
842 if (percent < min_pcnt)
843 return NULL;
844
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300845 if (!h->filtered)
846 return nd;
847
848 nd = rb_next(nd);
849 }
850
851 return NULL;
852}
853
Namhyung Kim064f1982013-05-14 11:09:04 +0900854static struct rb_node *hists__filter_prev_entries(struct rb_node *nd,
855 struct hists *hists,
856 float min_pcnt)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300857{
858 while (nd != NULL) {
859 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
Namhyung Kim064f1982013-05-14 11:09:04 +0900860 float percent = h->stat.period * 100.0 /
861 hists->stats.total_period;
862
863 if (!h->filtered && percent >= min_pcnt)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300864 return nd;
865
866 nd = rb_prev(nd);
867 }
868
869 return NULL;
870}
871
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300872static void ui_browser__hists_seek(struct ui_browser *browser,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300873 off_t offset, int whence)
874{
875 struct hist_entry *h;
876 struct rb_node *nd;
877 bool first = true;
Namhyung Kim064f1982013-05-14 11:09:04 +0900878 struct hist_browser *hb;
879
880 hb = container_of(browser, struct hist_browser, b);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300881
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300882 if (browser->nr_entries == 0)
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300883 return;
884
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300885 ui_browser__hists_init_top(browser);
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300886
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300887 switch (whence) {
888 case SEEK_SET:
Namhyung Kim064f1982013-05-14 11:09:04 +0900889 nd = hists__filter_entries(rb_first(browser->entries),
890 hb->hists, hb->min_pcnt);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300891 break;
892 case SEEK_CUR:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300893 nd = browser->top;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300894 goto do_offset;
895 case SEEK_END:
Namhyung Kim064f1982013-05-14 11:09:04 +0900896 nd = hists__filter_prev_entries(rb_last(browser->entries),
897 hb->hists, hb->min_pcnt);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300898 first = false;
899 break;
900 default:
901 return;
902 }
903
904 /*
905 * Moves not relative to the first visible entry invalidates its
906 * row_offset:
907 */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300908 h = rb_entry(browser->top, struct hist_entry, rb_node);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300909 h->row_offset = 0;
910
911 /*
912 * Here we have to check if nd is expanded (+), if it is we can't go
913 * the next top level hist_entry, instead we must compute an offset of
914 * what _not_ to show and not change the first visible entry.
915 *
916 * This offset increments when we are going from top to bottom and
917 * decreases when we're going from bottom to top.
918 *
919 * As we don't have backpointers to the top level in the callchains
920 * structure, we need to always print the whole hist_entry callchain,
921 * skipping the first ones that are before the first visible entry
922 * and stop when we printed enough lines to fill the screen.
923 */
924do_offset:
925 if (offset > 0) {
926 do {
927 h = rb_entry(nd, struct hist_entry, rb_node);
928 if (h->ms.unfolded) {
929 u16 remaining = h->nr_rows - h->row_offset;
930 if (offset > remaining) {
931 offset -= remaining;
932 h->row_offset = 0;
933 } else {
934 h->row_offset += offset;
935 offset = 0;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300936 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300937 break;
938 }
939 }
Namhyung Kim064f1982013-05-14 11:09:04 +0900940 nd = hists__filter_entries(rb_next(nd), hb->hists,
941 hb->min_pcnt);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300942 if (nd == NULL)
943 break;
944 --offset;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300945 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300946 } while (offset != 0);
947 } else if (offset < 0) {
948 while (1) {
949 h = rb_entry(nd, struct hist_entry, rb_node);
950 if (h->ms.unfolded) {
951 if (first) {
952 if (-offset > h->row_offset) {
953 offset += h->row_offset;
954 h->row_offset = 0;
955 } else {
956 h->row_offset += offset;
957 offset = 0;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300958 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300959 break;
960 }
961 } else {
962 if (-offset > h->nr_rows) {
963 offset += h->nr_rows;
964 h->row_offset = 0;
965 } else {
966 h->row_offset = h->nr_rows + offset;
967 offset = 0;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300968 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300969 break;
970 }
971 }
972 }
973
Namhyung Kim064f1982013-05-14 11:09:04 +0900974 nd = hists__filter_prev_entries(rb_prev(nd), hb->hists,
975 hb->min_pcnt);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300976 if (nd == NULL)
977 break;
978 ++offset;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300979 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300980 if (offset == 0) {
981 /*
982 * Last unfiltered hist_entry, check if it is
983 * unfolded, if it is then we should have
984 * row_offset at its last entry.
985 */
986 h = rb_entry(nd, struct hist_entry, rb_node);
987 if (h->ms.unfolded)
988 h->row_offset = h->nr_rows;
989 break;
990 }
991 first = false;
992 }
993 } else {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300994 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300995 h = rb_entry(nd, struct hist_entry, rb_node);
996 h->row_offset = 0;
997 }
998}
999
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001000static int hist_browser__fprintf_callchain_node_rb_tree(struct hist_browser *browser,
1001 struct callchain_node *chain_node,
1002 u64 total, int level,
1003 FILE *fp)
1004{
1005 struct rb_node *node;
1006 int offset = level * LEVEL_OFFSET_STEP;
1007 u64 new_total, remaining;
1008 int printed = 0;
1009
1010 if (callchain_param.mode == CHAIN_GRAPH_REL)
1011 new_total = chain_node->children_hit;
1012 else
1013 new_total = total;
1014
1015 remaining = new_total;
1016 node = rb_first(&chain_node->rb_root);
1017 while (node) {
1018 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
1019 struct rb_node *next = rb_next(node);
1020 u64 cumul = callchain_cumul_hits(child);
1021 struct callchain_list *chain;
1022 char folded_sign = ' ';
1023 int first = true;
1024 int extra_offset = 0;
1025
1026 remaining -= cumul;
1027
1028 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -03001029 char bf[1024], *alloc_str;
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001030 const char *str;
1031 bool was_first = first;
1032
1033 if (first)
1034 first = false;
1035 else
1036 extra_offset = LEVEL_OFFSET_STEP;
1037
1038 folded_sign = callchain_list__folded(chain);
1039
1040 alloc_str = NULL;
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -03001041 str = callchain_list__sym_name(chain, bf, sizeof(bf),
1042 browser->show_dso);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001043 if (was_first) {
1044 double percent = cumul * 100.0 / new_total;
1045
1046 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
1047 str = "Not enough memory!";
1048 else
1049 str = alloc_str;
1050 }
1051
1052 printed += fprintf(fp, "%*s%c %s\n", offset + extra_offset, " ", folded_sign, str);
1053 free(alloc_str);
1054 if (folded_sign == '+')
1055 break;
1056 }
1057
1058 if (folded_sign == '-') {
1059 const int new_level = level + (extra_offset ? 2 : 1);
1060 printed += hist_browser__fprintf_callchain_node_rb_tree(browser, child, new_total,
1061 new_level, fp);
1062 }
1063
1064 node = next;
1065 }
1066
1067 return printed;
1068}
1069
1070static int hist_browser__fprintf_callchain_node(struct hist_browser *browser,
1071 struct callchain_node *node,
1072 int level, FILE *fp)
1073{
1074 struct callchain_list *chain;
1075 int offset = level * LEVEL_OFFSET_STEP;
1076 char folded_sign = ' ';
1077 int printed = 0;
1078
1079 list_for_each_entry(chain, &node->val, list) {
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -03001080 char bf[1024], *s;
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001081
1082 folded_sign = callchain_list__folded(chain);
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -03001083 s = callchain_list__sym_name(chain, bf, sizeof(bf), browser->show_dso);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001084 printed += fprintf(fp, "%*s%c %s\n", offset, " ", folded_sign, s);
1085 }
1086
1087 if (folded_sign == '-')
1088 printed += hist_browser__fprintf_callchain_node_rb_tree(browser, node,
1089 browser->hists->stats.total_period,
1090 level + 1, fp);
1091 return printed;
1092}
1093
1094static int hist_browser__fprintf_callchain(struct hist_browser *browser,
1095 struct rb_root *chain, int level, FILE *fp)
1096{
1097 struct rb_node *nd;
1098 int printed = 0;
1099
1100 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
1101 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
1102
1103 printed += hist_browser__fprintf_callchain_node(browser, node, level, fp);
1104 }
1105
1106 return printed;
1107}
1108
1109static int hist_browser__fprintf_entry(struct hist_browser *browser,
1110 struct hist_entry *he, FILE *fp)
1111{
1112 char s[8192];
1113 double percent;
1114 int printed = 0;
1115 char folded_sign = ' ';
1116
1117 if (symbol_conf.use_callchain)
1118 folded_sign = hist_entry__folded(he);
1119
Namhyung Kim000078b2012-08-20 13:52:06 +09001120 hist_entry__sort_snprintf(he, s, sizeof(s), browser->hists);
Namhyung Kimb24c28f2012-10-04 21:49:41 +09001121 percent = (he->stat.period * 100.0) / browser->hists->stats.total_period;
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001122
1123 if (symbol_conf.use_callchain)
1124 printed += fprintf(fp, "%c ", folded_sign);
1125
1126 printed += fprintf(fp, " %5.2f%%", percent);
1127
1128 if (symbol_conf.show_nr_samples)
Namhyung Kimb24c28f2012-10-04 21:49:41 +09001129 printed += fprintf(fp, " %11u", he->stat.nr_events);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001130
1131 if (symbol_conf.show_total_period)
Namhyung Kimb24c28f2012-10-04 21:49:41 +09001132 printed += fprintf(fp, " %12" PRIu64, he->stat.period);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001133
1134 printed += fprintf(fp, "%s\n", rtrim(s));
1135
1136 if (folded_sign == '-')
1137 printed += hist_browser__fprintf_callchain(browser, &he->sorted_chain, 1, fp);
1138
1139 return printed;
1140}
1141
1142static int hist_browser__fprintf(struct hist_browser *browser, FILE *fp)
1143{
Namhyung Kim064f1982013-05-14 11:09:04 +09001144 struct rb_node *nd = hists__filter_entries(rb_first(browser->b.entries),
1145 browser->hists,
1146 browser->min_pcnt);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001147 int printed = 0;
1148
1149 while (nd) {
1150 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1151
1152 printed += hist_browser__fprintf_entry(browser, h, fp);
Namhyung Kim064f1982013-05-14 11:09:04 +09001153 nd = hists__filter_entries(rb_next(nd), browser->hists,
1154 browser->min_pcnt);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001155 }
1156
1157 return printed;
1158}
1159
1160static int hist_browser__dump(struct hist_browser *browser)
1161{
1162 char filename[64];
1163 FILE *fp;
1164
1165 while (1) {
1166 scnprintf(filename, sizeof(filename), "perf.hist.%d", browser->print_seq);
1167 if (access(filename, F_OK))
1168 break;
1169 /*
1170 * XXX: Just an arbitrary lazy upper limit
1171 */
1172 if (++browser->print_seq == 8192) {
1173 ui_helpline__fpush("Too many perf.hist.N files, nothing written!");
1174 return -1;
1175 }
1176 }
1177
1178 fp = fopen(filename, "w");
1179 if (fp == NULL) {
1180 char bf[64];
Kirill A. Shutemov4cc49d42012-07-24 00:06:54 +03001181 const char *err = strerror_r(errno, bf, sizeof(bf));
1182 ui_helpline__fpush("Couldn't write to %s: %s", filename, err);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001183 return -1;
1184 }
1185
1186 ++browser->print_seq;
1187 hist_browser__fprintf(browser, fp);
1188 fclose(fp);
1189 ui_helpline__fpush("%s written!", filename);
1190
1191 return 0;
1192}
1193
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001194static struct hist_browser *hist_browser__new(struct hists *hists)
1195{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001196 struct hist_browser *browser = zalloc(sizeof(*browser));
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001197
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001198 if (browser) {
1199 browser->hists = hists;
1200 browser->b.refresh = hist_browser__refresh;
1201 browser->b.seek = ui_browser__hists_seek;
1202 browser->b.use_navkeypressed = true;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001203 }
1204
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001205 return browser;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001206}
1207
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001208static void hist_browser__delete(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001209{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001210 free(browser);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001211}
1212
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001213static struct hist_entry *hist_browser__selected_entry(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001214{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001215 return browser->he_selection;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001216}
1217
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001218static struct thread *hist_browser__selected_thread(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001219{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001220 return browser->he_selection->thread;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001221}
1222
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001223static int hists__browser_title(struct hists *hists, char *bf, size_t size,
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001224 const char *ev_name)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001225{
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -03001226 char unit;
1227 int printed;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001228 const struct dso *dso = hists->dso_filter;
1229 const struct thread *thread = hists->thread_filter;
1230 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
1231 u64 nr_events = hists->stats.total_period;
Namhyung Kim717e2632013-01-22 18:09:44 +09001232 struct perf_evsel *evsel = hists_to_evsel(hists);
1233 char buf[512];
1234 size_t buflen = sizeof(buf);
1235
Namhyung Kim759ff492013-03-05 14:53:26 +09001236 if (perf_evsel__is_group_event(evsel)) {
Namhyung Kim717e2632013-01-22 18:09:44 +09001237 struct perf_evsel *pos;
1238
1239 perf_evsel__group_desc(evsel, buf, buflen);
1240 ev_name = buf;
1241
1242 for_each_group_member(pos, evsel) {
1243 nr_samples += pos->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1244 nr_events += pos->hists.stats.total_period;
1245 }
1246 }
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -03001247
Ashay Ranecc6862802012-04-05 21:01:01 -05001248 nr_samples = convert_unit(nr_samples, &unit);
1249 printed = scnprintf(bf, size,
1250 "Samples: %lu%c of event '%s', Event count (approx.): %lu",
1251 nr_samples, unit, ev_name, nr_events);
1252
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001253
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001254 if (hists->uid_filter_str)
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -02001255 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001256 ", UID: %s", hists->uid_filter_str);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001257 if (thread)
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -03001258 printed += scnprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -03001259 ", Thread: %s(%d)",
1260 (thread->comm_set ? thread->comm : ""),
Adrian Hunter38051232013-07-04 16:20:31 +03001261 thread->tid);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001262 if (dso)
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -03001263 printed += scnprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -03001264 ", DSO: %s", dso->short_name);
1265 return printed;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001266}
1267
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001268static inline void free_popup_options(char **options, int n)
1269{
1270 int i;
1271
1272 for (i = 0; i < n; ++i) {
1273 free(options[i]);
1274 options[i] = NULL;
1275 }
1276}
1277
Feng Tangc77d8d72012-11-01 00:00:55 +08001278/* Check whether the browser is for 'top' or 'report' */
1279static inline bool is_report_browser(void *timer)
1280{
1281 return timer == NULL;
1282}
1283
Feng Tang341487ab2013-02-03 14:38:20 +08001284/*
1285 * Only runtime switching of perf data file will make "input_name" point
1286 * to a malloced buffer. So add "is_input_name_malloced" flag to decide
1287 * whether we need to call free() for current "input_name" during the switch.
1288 */
1289static bool is_input_name_malloced = false;
1290
1291static int switch_data_file(void)
1292{
1293 char *pwd, *options[32], *abs_path[32], *tmp;
1294 DIR *pwd_dir;
1295 int nr_options = 0, choice = -1, ret = -1;
1296 struct dirent *dent;
1297
1298 pwd = getenv("PWD");
1299 if (!pwd)
1300 return ret;
1301
1302 pwd_dir = opendir(pwd);
1303 if (!pwd_dir)
1304 return ret;
1305
1306 memset(options, 0, sizeof(options));
1307 memset(options, 0, sizeof(abs_path));
1308
1309 while ((dent = readdir(pwd_dir))) {
1310 char path[PATH_MAX];
1311 u64 magic;
1312 char *name = dent->d_name;
1313 FILE *file;
1314
1315 if (!(dent->d_type == DT_REG))
1316 continue;
1317
1318 snprintf(path, sizeof(path), "%s/%s", pwd, name);
1319
1320 file = fopen(path, "r");
1321 if (!file)
1322 continue;
1323
1324 if (fread(&magic, 1, 8, file) < 8)
1325 goto close_file_and_continue;
1326
1327 if (is_perf_magic(magic)) {
1328 options[nr_options] = strdup(name);
1329 if (!options[nr_options])
1330 goto close_file_and_continue;
1331
1332 abs_path[nr_options] = strdup(path);
1333 if (!abs_path[nr_options]) {
1334 free(options[nr_options]);
1335 ui__warning("Can't search all data files due to memory shortage.\n");
1336 fclose(file);
1337 break;
1338 }
1339
1340 nr_options++;
1341 }
1342
1343close_file_and_continue:
1344 fclose(file);
1345 if (nr_options >= 32) {
1346 ui__warning("Too many perf data files in PWD!\n"
1347 "Only the first 32 files will be listed.\n");
1348 break;
1349 }
1350 }
1351 closedir(pwd_dir);
1352
1353 if (nr_options) {
1354 choice = ui__popup_menu(nr_options, options);
1355 if (choice < nr_options && choice >= 0) {
1356 tmp = strdup(abs_path[choice]);
1357 if (tmp) {
1358 if (is_input_name_malloced)
1359 free((void *)input_name);
1360 input_name = tmp;
1361 is_input_name_malloced = true;
1362 ret = 0;
1363 } else
1364 ui__warning("Data switch failed due to memory shortage!\n");
1365 }
1366 }
1367
1368 free_popup_options(options, nr_options);
1369 free_popup_options(abs_path, nr_options);
1370 return ret;
1371}
1372
Namhyung Kim064f1982013-05-14 11:09:04 +09001373static void hist_browser__update_pcnt_entries(struct hist_browser *hb)
1374{
1375 u64 nr_entries = 0;
1376 struct rb_node *nd = rb_first(&hb->hists->entries);
1377
1378 while (nd) {
1379 nr_entries++;
1380 nd = hists__filter_entries(rb_next(nd), hb->hists,
1381 hb->min_pcnt);
1382 }
1383
1384 hb->nr_pcnt_entries = nr_entries;
1385}
Feng Tang341487ab2013-02-03 14:38:20 +08001386
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001387static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001388 const char *helpline, const char *ev_name,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001389 bool left_exits,
Namhyung Kim68d80752012-11-02 14:50:06 +09001390 struct hist_browser_timer *hbt,
Namhyung Kim064f1982013-05-14 11:09:04 +09001391 float min_pcnt,
Namhyung Kim68d80752012-11-02 14:50:06 +09001392 struct perf_session_env *env)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001393{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001394 struct hists *hists = &evsel->hists;
1395 struct hist_browser *browser = hist_browser__new(hists);
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001396 struct branch_info *bi;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001397 struct pstack *fstack;
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001398 char *options[16];
1399 int nr_options = 0;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001400 int key = -1;
Namhyung Kim938a23a2012-03-16 17:50:53 +09001401 char buf[64];
Feng Tangcdbab7c2012-10-30 11:56:06 +08001402 char script_opt[64];
Namhyung Kim9783adf2012-11-02 14:50:05 +09001403 int delay_secs = hbt ? hbt->refresh : 0;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001404
1405 if (browser == NULL)
1406 return -1;
1407
Namhyung Kim064f1982013-05-14 11:09:04 +09001408 if (min_pcnt) {
1409 browser->min_pcnt = min_pcnt;
1410 hist_browser__update_pcnt_entries(browser);
1411 }
1412
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001413 fstack = pstack__new(2);
1414 if (fstack == NULL)
1415 goto out;
1416
1417 ui_helpline__push(helpline);
1418
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001419 memset(options, 0, sizeof(options));
1420
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001421 while (1) {
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -03001422 const struct thread *thread = NULL;
1423 const struct dso *dso = NULL;
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001424 int choice = 0,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001425 annotate = -2, zoom_dso = -2, zoom_thread = -2,
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001426 annotate_f = -2, annotate_t = -2, browse_map = -2;
Feng Tang341487ab2013-02-03 14:38:20 +08001427 int scripts_comm = -2, scripts_symbol = -2,
1428 scripts_all = -2, switch_data = -2;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001429
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001430 nr_options = 0;
1431
Namhyung Kim9783adf2012-11-02 14:50:05 +09001432 key = hist_browser__run(browser, ev_name, hbt);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001433
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -03001434 if (browser->he_selection != NULL) {
1435 thread = hist_browser__selected_thread(browser);
1436 dso = browser->selection->map ? browser->selection->map->dso : NULL;
1437 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001438 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001439 case K_TAB:
1440 case K_UNTAB:
David Aherne4419b82011-10-19 11:37:47 -06001441 if (nr_events == 1)
1442 continue;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001443 /*
1444 * Exit the browser, let hists__browser_tree
1445 * go to the next or previous
1446 */
1447 goto out_free_stack;
1448 case 'a':
Arnaldo Carvalho de Melo9c796ec2013-04-26 14:28:46 -03001449 if (!sort__has_sym) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001450 ui_browser__warning(&browser->b, delay_secs * 2,
Arnaldo Carvalho de Meloa6e51f92011-10-21 10:58:24 -02001451 "Annotation is only available for symbolic views, "
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001452 "include \"sym*\" in --sort to use it.");
Arnaldo Carvalho de Meloa6e51f92011-10-21 10:58:24 -02001453 continue;
1454 }
1455
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -03001456 if (browser->selection == NULL ||
Lin Mingdb9a9cbc2011-04-08 14:31:26 +08001457 browser->selection->sym == NULL ||
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001458 browser->selection->map->dso->annotate_warned)
1459 continue;
1460 goto do_annotate;
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001461 case 'P':
1462 hist_browser__dump(browser);
1463 continue;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001464 case 'd':
1465 goto zoom_dso;
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -03001466 case 'V':
1467 browser->show_dso = !browser->show_dso;
1468 continue;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001469 case 't':
1470 goto zoom_thread;
Arnaldo Carvalho de Melo5a5626b2012-05-09 12:21:22 -03001471 case '/':
Namhyung Kim938a23a2012-03-16 17:50:53 +09001472 if (ui_browser__input_window("Symbol to show",
1473 "Please enter the name of symbol you want to see",
1474 buf, "ENTER: OK, ESC: Cancel",
1475 delay_secs * 2) == K_ENTER) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001476 hists->symbol_filter_str = *buf ? buf : NULL;
1477 hists__filter_by_symbol(hists);
Namhyung Kim938a23a2012-03-16 17:50:53 +09001478 hist_browser__reset(browser);
1479 }
1480 continue;
Feng Tangcdbab7c2012-10-30 11:56:06 +08001481 case 'r':
Namhyung Kim9783adf2012-11-02 14:50:05 +09001482 if (is_report_browser(hbt))
Feng Tangc77d8d72012-11-01 00:00:55 +08001483 goto do_scripts;
1484 continue;
Feng Tang341487ab2013-02-03 14:38:20 +08001485 case 's':
1486 if (is_report_browser(hbt))
1487 goto do_data_switch;
1488 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001489 case K_F1:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001490 case 'h':
1491 case '?':
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001492 ui_browser__help_window(&browser->b,
1493 "h/?/F1 Show this window\n"
Arnaldo Carvalho de Melo2d5646c2011-10-18 13:02:52 -02001494 "UP/DOWN/PGUP\n"
1495 "PGDN/SPACE Navigate\n"
1496 "q/ESC/CTRL+C Exit browser\n\n"
1497 "For multiple event sessions:\n\n"
1498 "TAB/UNTAB Switch events\n\n"
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -03001499 "For symbolic views (--sort has sym):\n\n"
Arnaldo Carvalho de Melo2d5646c2011-10-18 13:02:52 -02001500 "-> Zoom into DSO/Threads & Annotate current symbol\n"
1501 "<- Zoom out\n"
1502 "a Annotate current symbol\n"
1503 "C Collapse all callchains\n"
1504 "E Expand all callchains\n"
1505 "d Zoom into current DSO\n"
Namhyung Kim938a23a2012-03-16 17:50:53 +09001506 "t Zoom into current Thread\n"
Feng Tangc77d8d72012-11-01 00:00:55 +08001507 "r Run available scripts('perf report' only)\n"
Feng Tang341487ab2013-02-03 14:38:20 +08001508 "s Switch to another data file in PWD ('perf report' only)\n"
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001509 "P Print histograms to perf.hist.N\n"
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -03001510 "V Verbose (DSO names in callchains, etc)\n"
Arnaldo Carvalho de Melo5a5626b2012-05-09 12:21:22 -03001511 "/ Filter symbol by name");
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001512 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001513 case K_ENTER:
1514 case K_RIGHT:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001515 /* menu */
1516 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001517 case K_LEFT: {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001518 const void *top;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001519
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001520 if (pstack__empty(fstack)) {
1521 /*
1522 * Go back to the perf_evsel_menu__run or other user
1523 */
1524 if (left_exits)
1525 goto out_free_stack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001526 continue;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001527 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001528 top = pstack__pop(fstack);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001529 if (top == &browser->hists->dso_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001530 goto zoom_out_dso;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001531 if (top == &browser->hists->thread_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001532 goto zoom_out_thread;
1533 continue;
1534 }
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001535 case K_ESC:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001536 if (!left_exits &&
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001537 !ui_browser__dialog_yesno(&browser->b,
1538 "Do you really want to exit?"))
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001539 continue;
1540 /* Fall thru */
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -03001541 case 'q':
1542 case CTRL('c'):
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001543 goto out_free_stack;
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -03001544 default:
1545 continue;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001546 }
1547
Arnaldo Carvalho de Melo9c796ec2013-04-26 14:28:46 -03001548 if (!sort__has_sym)
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -03001549 goto add_exit_option;
1550
Namhyung Kim55369fc2013-04-01 20:35:20 +09001551 if (sort__mode == SORT_MODE__BRANCH) {
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001552 bi = browser->he_selection->branch_info;
1553 if (browser->selection != NULL &&
1554 bi &&
1555 bi->from.sym != NULL &&
1556 !bi->from.map->dso->annotate_warned &&
1557 asprintf(&options[nr_options], "Annotate %s",
1558 bi->from.sym->name) > 0)
1559 annotate_f = nr_options++;
1560
1561 if (browser->selection != NULL &&
1562 bi &&
1563 bi->to.sym != NULL &&
1564 !bi->to.map->dso->annotate_warned &&
Stephane Eranian8bcd65f2012-03-12 16:13:29 +01001565 (bi->to.sym != bi->from.sym ||
1566 bi->to.map->dso != bi->from.map->dso) &&
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001567 asprintf(&options[nr_options], "Annotate %s",
1568 bi->to.sym->name) > 0)
1569 annotate_t = nr_options++;
1570 } else {
1571
1572 if (browser->selection != NULL &&
1573 browser->selection->sym != NULL &&
1574 !browser->selection->map->dso->annotate_warned &&
1575 asprintf(&options[nr_options], "Annotate %s",
1576 browser->selection->sym->name) > 0)
1577 annotate = nr_options++;
1578 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001579
1580 if (thread != NULL &&
1581 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001582 (browser->hists->thread_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001583 (thread->comm_set ? thread->comm : ""),
Adrian Hunter38051232013-07-04 16:20:31 +03001584 thread->tid) > 0)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001585 zoom_thread = nr_options++;
1586
1587 if (dso != NULL &&
1588 asprintf(&options[nr_options], "Zoom %s %s DSO",
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001589 (browser->hists->dso_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001590 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
1591 zoom_dso = nr_options++;
1592
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -03001593 if (browser->selection != NULL &&
1594 browser->selection->map != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001595 asprintf(&options[nr_options], "Browse map details") > 0)
1596 browse_map = nr_options++;
Feng Tangcdbab7c2012-10-30 11:56:06 +08001597
1598 /* perf script support */
1599 if (browser->he_selection) {
1600 struct symbol *sym;
1601
1602 if (asprintf(&options[nr_options], "Run scripts for samples of thread [%s]",
1603 browser->he_selection->thread->comm) > 0)
1604 scripts_comm = nr_options++;
1605
1606 sym = browser->he_selection->ms.sym;
1607 if (sym && sym->namelen &&
1608 asprintf(&options[nr_options], "Run scripts for samples of symbol [%s]",
1609 sym->name) > 0)
1610 scripts_symbol = nr_options++;
1611 }
1612
1613 if (asprintf(&options[nr_options], "Run scripts for all samples") > 0)
1614 scripts_all = nr_options++;
1615
Feng Tang341487ab2013-02-03 14:38:20 +08001616 if (is_report_browser(hbt) && asprintf(&options[nr_options],
1617 "Switch to another data file in PWD") > 0)
1618 switch_data = nr_options++;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -03001619add_exit_option:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001620 options[nr_options++] = (char *)"Exit";
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001621retry_popup_menu:
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -03001622 choice = ui__popup_menu(nr_options, options);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001623
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001624 if (choice == nr_options - 1)
1625 break;
1626
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001627 if (choice == -1) {
1628 free_popup_options(options, nr_options - 1);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001629 continue;
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001630 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001631
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001632 if (choice == annotate || choice == annotate_t || choice == annotate_f) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001633 struct hist_entry *he;
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001634 int err;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001635do_annotate:
Namhyung Kim68d80752012-11-02 14:50:06 +09001636 if (!objdump_path && perf_session_env__lookup_objdump(env))
1637 continue;
1638
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001639 he = hist_browser__selected_entry(browser);
1640 if (he == NULL)
1641 continue;
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001642
1643 /*
1644 * we stash the branch_info symbol + map into the
1645 * the ms so we don't have to rewrite all the annotation
1646 * code to use branch_info.
1647 * in branch mode, the ms struct is not used
1648 */
1649 if (choice == annotate_f) {
1650 he->ms.sym = he->branch_info->from.sym;
1651 he->ms.map = he->branch_info->from.map;
1652 } else if (choice == annotate_t) {
1653 he->ms.sym = he->branch_info->to.sym;
1654 he->ms.map = he->branch_info->to.map;
1655 }
1656
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -03001657 /*
1658 * Don't let this be freed, say, by hists__decay_entry.
1659 */
1660 he->used = true;
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001661 err = hist_entry__tui_annotate(he, evsel, hbt);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -03001662 he->used = false;
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001663 /*
1664 * offer option to annotate the other branch source or target
1665 * (if they exists) when returning from annotate
1666 */
1667 if ((err == 'q' || err == CTRL('c'))
1668 && annotate_t != -2 && annotate_f != -2)
1669 goto retry_popup_menu;
1670
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -03001671 ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001672 if (err)
1673 ui_browser__handle_resize(&browser->b);
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001674
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001675 } else if (choice == browse_map)
1676 map__browse(browser->selection->map);
1677 else if (choice == zoom_dso) {
1678zoom_dso:
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001679 if (browser->hists->dso_filter) {
1680 pstack__remove(fstack, &browser->hists->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001681zoom_out_dso:
1682 ui_helpline__pop();
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001683 browser->hists->dso_filter = NULL;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001684 sort_dso.elide = false;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001685 } else {
1686 if (dso == NULL)
1687 continue;
1688 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
1689 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001690 browser->hists->dso_filter = dso;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001691 sort_dso.elide = true;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001692 pstack__push(fstack, &browser->hists->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001693 }
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001694 hists__filter_by_dso(hists);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001695 hist_browser__reset(browser);
1696 } else if (choice == zoom_thread) {
1697zoom_thread:
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001698 if (browser->hists->thread_filter) {
1699 pstack__remove(fstack, &browser->hists->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001700zoom_out_thread:
1701 ui_helpline__pop();
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001702 browser->hists->thread_filter = NULL;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001703 sort_thread.elide = false;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001704 } else {
1705 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
1706 thread->comm_set ? thread->comm : "",
Adrian Hunter38051232013-07-04 16:20:31 +03001707 thread->tid);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001708 browser->hists->thread_filter = thread;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001709 sort_thread.elide = true;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001710 pstack__push(fstack, &browser->hists->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001711 }
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001712 hists__filter_by_thread(hists);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001713 hist_browser__reset(browser);
1714 }
Feng Tangcdbab7c2012-10-30 11:56:06 +08001715 /* perf scripts support */
1716 else if (choice == scripts_all || choice == scripts_comm ||
1717 choice == scripts_symbol) {
1718do_scripts:
1719 memset(script_opt, 0, 64);
1720
1721 if (choice == scripts_comm)
1722 sprintf(script_opt, " -c %s ", browser->he_selection->thread->comm);
1723
1724 if (choice == scripts_symbol)
1725 sprintf(script_opt, " -S %s ", browser->he_selection->ms.sym->name);
1726
1727 script_browse(script_opt);
1728 }
Feng Tang341487ab2013-02-03 14:38:20 +08001729 /* Switch to another data file */
1730 else if (choice == switch_data) {
1731do_data_switch:
1732 if (!switch_data_file()) {
1733 key = K_SWITCH_INPUT_DATA;
1734 break;
1735 } else
1736 ui__warning("Won't switch the data files due to\n"
1737 "no valid data file get selected!\n");
1738 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001739 }
1740out_free_stack:
1741 pstack__delete(fstack);
1742out:
1743 hist_browser__delete(browser);
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001744 free_popup_options(options, nr_options - 1);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001745 return key;
1746}
1747
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001748struct perf_evsel_menu {
1749 struct ui_browser b;
1750 struct perf_evsel *selection;
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001751 bool lost_events, lost_events_warned;
Namhyung Kim064f1982013-05-14 11:09:04 +09001752 float min_pcnt;
Namhyung Kim68d80752012-11-02 14:50:06 +09001753 struct perf_session_env *env;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001754};
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001755
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001756static void perf_evsel_menu__write(struct ui_browser *browser,
1757 void *entry, int row)
1758{
1759 struct perf_evsel_menu *menu = container_of(browser,
1760 struct perf_evsel_menu, b);
1761 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
1762 bool current_entry = ui_browser__is_current_entry(browser, row);
1763 unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03001764 const char *ev_name = perf_evsel__name(evsel);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001765 char bf[256], unit;
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001766 const char *warn = " ";
1767 size_t printed;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001768
1769 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
1770 HE_COLORSET_NORMAL);
1771
Namhyung Kim759ff492013-03-05 14:53:26 +09001772 if (perf_evsel__is_group_event(evsel)) {
Namhyung Kim717e2632013-01-22 18:09:44 +09001773 struct perf_evsel *pos;
1774
1775 ev_name = perf_evsel__group_name(evsel);
1776
1777 for_each_group_member(pos, evsel) {
1778 nr_events += pos->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1779 }
1780 }
1781
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001782 nr_events = convert_unit(nr_events, &unit);
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -03001783 printed = scnprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001784 unit, unit == ' ' ? "" : " ", ev_name);
1785 slsmg_printf("%s", bf);
1786
1787 nr_events = evsel->hists.stats.nr_events[PERF_RECORD_LOST];
1788 if (nr_events != 0) {
1789 menu->lost_events = true;
1790 if (!current_entry)
1791 ui_browser__set_color(browser, HE_COLORSET_TOP);
1792 nr_events = convert_unit(nr_events, &unit);
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -03001793 printed += scnprintf(bf, sizeof(bf), ": %ld%c%schunks LOST!",
1794 nr_events, unit, unit == ' ' ? "" : " ");
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001795 warn = bf;
1796 }
1797
1798 slsmg_write_nstring(warn, browser->width - printed);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001799
1800 if (current_entry)
1801 menu->selection = evsel;
1802}
1803
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001804static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
1805 int nr_events, const char *help,
Namhyung Kim9783adf2012-11-02 14:50:05 +09001806 struct hist_browser_timer *hbt)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001807{
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001808 struct perf_evlist *evlist = menu->b.priv;
1809 struct perf_evsel *pos;
1810 const char *ev_name, *title = "Available samples";
Namhyung Kim9783adf2012-11-02 14:50:05 +09001811 int delay_secs = hbt ? hbt->refresh : 0;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001812 int key;
1813
1814 if (ui_browser__show(&menu->b, title,
1815 "ESC: exit, ENTER|->: Browse histograms") < 0)
1816 return -1;
1817
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001818 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03001819 key = ui_browser__run(&menu->b, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001820
1821 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001822 case K_TIMER:
Namhyung Kim9783adf2012-11-02 14:50:05 +09001823 hbt->timer(hbt->arg);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001824
1825 if (!menu->lost_events_warned && menu->lost_events) {
1826 ui_browser__warn_lost_events(&menu->b);
1827 menu->lost_events_warned = true;
1828 }
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001829 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001830 case K_RIGHT:
1831 case K_ENTER:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001832 if (!menu->selection)
1833 continue;
1834 pos = menu->selection;
1835browse_hists:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001836 perf_evlist__set_selected(evlist, pos);
1837 /*
1838 * Give the calling tool a chance to populate the non
1839 * default evsel resorted hists tree.
1840 */
Namhyung Kim9783adf2012-11-02 14:50:05 +09001841 if (hbt)
1842 hbt->timer(hbt->arg);
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03001843 ev_name = perf_evsel__name(pos);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001844 key = perf_evsel__hists_browse(pos, nr_events, help,
Namhyung Kim68d80752012-11-02 14:50:06 +09001845 ev_name, true, hbt,
Namhyung Kim064f1982013-05-14 11:09:04 +09001846 menu->min_pcnt,
Namhyung Kim68d80752012-11-02 14:50:06 +09001847 menu->env);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001848 ui_browser__show_title(&menu->b, title);
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001849 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001850 case K_TAB:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001851 if (pos->node.next == &evlist->entries)
1852 pos = list_entry(evlist->entries.next, struct perf_evsel, node);
1853 else
1854 pos = list_entry(pos->node.next, struct perf_evsel, node);
1855 goto browse_hists;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001856 case K_UNTAB:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001857 if (pos->node.prev == &evlist->entries)
1858 pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
1859 else
1860 pos = list_entry(pos->node.prev, struct perf_evsel, node);
1861 goto browse_hists;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001862 case K_ESC:
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001863 if (!ui_browser__dialog_yesno(&menu->b,
1864 "Do you really want to exit?"))
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001865 continue;
1866 /* Fall thru */
Feng Tang341487ab2013-02-03 14:38:20 +08001867 case K_SWITCH_INPUT_DATA:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001868 case 'q':
1869 case CTRL('c'):
1870 goto out;
1871 default:
1872 continue;
1873 }
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001874 case K_LEFT:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001875 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001876 case K_ESC:
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001877 if (!ui_browser__dialog_yesno(&menu->b,
1878 "Do you really want to exit?"))
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -03001879 continue;
1880 /* Fall thru */
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001881 case 'q':
1882 case CTRL('c'):
1883 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001884 default:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001885 continue;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001886 }
1887 }
1888
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001889out:
1890 ui_browser__hide(&menu->b);
1891 return key;
1892}
1893
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09001894static bool filter_group_entries(struct ui_browser *self __maybe_unused,
1895 void *entry)
1896{
1897 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
1898
1899 if (symbol_conf.event_group && !perf_evsel__is_group_leader(evsel))
1900 return true;
1901
1902 return false;
1903}
1904
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001905static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09001906 int nr_entries, const char *help,
Namhyung Kim68d80752012-11-02 14:50:06 +09001907 struct hist_browser_timer *hbt,
Namhyung Kim064f1982013-05-14 11:09:04 +09001908 float min_pcnt,
Namhyung Kim68d80752012-11-02 14:50:06 +09001909 struct perf_session_env *env)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001910{
1911 struct perf_evsel *pos;
1912 struct perf_evsel_menu menu = {
1913 .b = {
1914 .entries = &evlist->entries,
1915 .refresh = ui_browser__list_head_refresh,
1916 .seek = ui_browser__list_head_seek,
1917 .write = perf_evsel_menu__write,
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09001918 .filter = filter_group_entries,
1919 .nr_entries = nr_entries,
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001920 .priv = evlist,
1921 },
Namhyung Kim064f1982013-05-14 11:09:04 +09001922 .min_pcnt = min_pcnt,
Namhyung Kim68d80752012-11-02 14:50:06 +09001923 .env = env,
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001924 };
1925
1926 ui_helpline__push("Press ESC to exit");
1927
1928 list_for_each_entry(pos, &evlist->entries, node) {
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03001929 const char *ev_name = perf_evsel__name(pos);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001930 size_t line_len = strlen(ev_name) + 7;
1931
1932 if (menu.b.width < line_len)
1933 menu.b.width = line_len;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001934 }
1935
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09001936 return perf_evsel_menu__run(&menu, nr_entries, help, hbt);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001937}
1938
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001939int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
Namhyung Kim68d80752012-11-02 14:50:06 +09001940 struct hist_browser_timer *hbt,
Namhyung Kim064f1982013-05-14 11:09:04 +09001941 float min_pcnt,
Namhyung Kim68d80752012-11-02 14:50:06 +09001942 struct perf_session_env *env)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001943{
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09001944 int nr_entries = evlist->nr_entries;
1945
1946single_entry:
1947 if (nr_entries == 1) {
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001948 struct perf_evsel *first = list_entry(evlist->entries.next,
1949 struct perf_evsel, node);
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03001950 const char *ev_name = perf_evsel__name(first);
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09001951
1952 return perf_evsel__hists_browse(first, nr_entries, help,
Namhyung Kim064f1982013-05-14 11:09:04 +09001953 ev_name, false, hbt, min_pcnt,
1954 env);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001955 }
1956
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09001957 if (symbol_conf.event_group) {
1958 struct perf_evsel *pos;
1959
1960 nr_entries = 0;
1961 list_for_each_entry(pos, &evlist->entries, node)
1962 if (perf_evsel__is_group_leader(pos))
1963 nr_entries++;
1964
1965 if (nr_entries == 1)
1966 goto single_entry;
1967 }
1968
1969 return __perf_evlist__tui_browse_hists(evlist, nr_entries, help,
Namhyung Kim064f1982013-05-14 11:09:04 +09001970 hbt, min_pcnt, env);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001971}