blob: de1d6f0df8a772ce13f9225c83852ad2813441d6 [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 <stdlib.h>
3#include <string.h>
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03004#include <linux/rbtree.h>
5
Namhyung Kimaca7a942012-04-04 00:14:26 -07006#include "../../util/evsel.h"
7#include "../../util/evlist.h"
8#include "../../util/hist.h"
9#include "../../util/pstack.h"
10#include "../../util/sort.h"
11#include "../../util/util.h"
Namhyung Kim42337a22014-08-12 17:16:06 +090012#include "../../util/top.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"
Jiri Olsad7553302014-06-15 10:22:15 +020020#include "annotate.h"
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030021
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030022struct hist_browser {
23 struct ui_browser b;
24 struct hists *hists;
25 struct hist_entry *he_selection;
26 struct map_symbol *selection;
Namhyung Kimc2a51ab2015-04-22 16:18:15 +090027 struct hist_browser_timer *hbt;
Namhyung Kim01f00a12015-04-22 16:18:16 +090028 struct pstack *pstack;
Kan Liangce80d3b2015-08-28 05:48:04 -040029 struct perf_env *env;
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -030030 int print_seq;
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -030031 bool show_dso;
Arnaldo Carvalho de Melo025bf7e2014-06-14 15:44:52 +020032 bool show_headers;
Namhyung Kim064f1982013-05-14 11:09:04 +090033 float min_pcnt;
Namhyung Kim112f7612014-04-22 14:05:35 +090034 u64 nr_non_filtered_entries;
Namhyung Kimf5b763f2016-02-25 00:13:43 +090035 u64 nr_hierarchy_entries;
Namhyung Kimc3b78952014-04-22 15:56:17 +090036 u64 nr_callchain_rows;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030037};
38
Namhyung Kimf5951d52012-09-03 11:53:09 +090039extern void hist_browser__init_hpp(void);
40
Taeung Song1e378eb2014-10-07 16:13:15 +090041static int hists__browser_title(struct hists *hists,
42 struct hist_browser_timer *hbt,
43 char *bf, size_t size);
Namhyung Kim112f7612014-04-22 14:05:35 +090044static void hist_browser__update_nr_entries(struct hist_browser *hb);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -030045
Namhyung Kimc3b78952014-04-22 15:56:17 +090046static struct rb_node *hists__filter_entries(struct rb_node *nd,
Namhyung Kimc3b78952014-04-22 15:56:17 +090047 float min_pcnt);
48
Namhyung Kim268397c2014-04-22 14:49:31 +090049static bool hist_browser__has_filter(struct hist_browser *hb)
50{
Arnaldo Carvalho de Melo9c0fa8d2015-07-13 08:26:35 -030051 return hists__has_filter(hb->hists) || hb->min_pcnt || symbol_conf.has_filter;
Namhyung Kim268397c2014-04-22 14:49:31 +090052}
53
He Kuang4fabf3d2015-03-12 15:21:49 +080054static int hist_browser__get_folding(struct hist_browser *browser)
55{
56 struct rb_node *nd;
57 struct hists *hists = browser->hists;
58 int unfolded_rows = 0;
59
60 for (nd = rb_first(&hists->entries);
61 (nd = hists__filter_entries(nd, browser->min_pcnt)) != NULL;
Namhyung Kimf5b763f2016-02-25 00:13:43 +090062 nd = rb_hierarchy_next(nd)) {
He Kuang4fabf3d2015-03-12 15:21:49 +080063 struct hist_entry *he =
64 rb_entry(nd, struct hist_entry, rb_node);
65
Namhyung Kimf5b763f2016-02-25 00:13:43 +090066 if (he->leaf && he->unfolded)
He Kuang4fabf3d2015-03-12 15:21:49 +080067 unfolded_rows += he->nr_rows;
68 }
69 return unfolded_rows;
70}
71
Namhyung Kimc3b78952014-04-22 15:56:17 +090072static u32 hist_browser__nr_entries(struct hist_browser *hb)
73{
74 u32 nr_entries;
75
Namhyung Kimf5b763f2016-02-25 00:13:43 +090076 if (symbol_conf.report_hierarchy)
77 nr_entries = hb->nr_hierarchy_entries;
78 else if (hist_browser__has_filter(hb))
Namhyung Kimc3b78952014-04-22 15:56:17 +090079 nr_entries = hb->nr_non_filtered_entries;
80 else
81 nr_entries = hb->hists->nr_entries;
82
He Kuang4fabf3d2015-03-12 15:21:49 +080083 hb->nr_callchain_rows = hist_browser__get_folding(hb);
Namhyung Kimc3b78952014-04-22 15:56:17 +090084 return nr_entries + hb->nr_callchain_rows;
85}
86
Arnaldo Carvalho de Melo025bf7e2014-06-14 15:44:52 +020087static void hist_browser__update_rows(struct hist_browser *hb)
88{
89 struct ui_browser *browser = &hb->b;
90 u16 header_offset = hb->show_headers ? 1 : 0, index_row;
91
92 browser->rows = browser->height - header_offset;
93 /*
94 * Verify if we were at the last line and that line isn't
95 * visibe because we now show the header line(s).
96 */
97 index_row = browser->index - browser->top_idx;
98 if (index_row >= browser->rows)
99 browser->index -= index_row - browser->rows + 1;
100}
101
Arnaldo Carvalho de Melo357cfff2014-07-01 17:01:01 -0300102static void hist_browser__refresh_dimensions(struct ui_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300103{
Arnaldo Carvalho de Melo357cfff2014-07-01 17:01:01 -0300104 struct hist_browser *hb = container_of(browser, struct hist_browser, b);
105
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300106 /* 3 == +/- toggle symbol before actual hist_entry rendering */
Arnaldo Carvalho de Melo357cfff2014-07-01 17:01:01 -0300107 browser->width = 3 + (hists__sort_list_width(hb->hists) + sizeof("[k]"));
108 /*
109 * FIXME: Just keeping existing behaviour, but this really should be
110 * before updating browser->width, as it will invalidate the
111 * calculation above. Fix this and the fallout in another
112 * changeset.
113 */
114 ui_browser__refresh_dimensions(browser);
Arnaldo Carvalho de Melo025bf7e2014-06-14 15:44:52 +0200115 hist_browser__update_rows(hb);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300116}
117
Arnaldo Carvalho de Meloca3ff332014-07-01 16:42:24 -0300118static void hist_browser__gotorc(struct hist_browser *browser, int row, int column)
119{
Arnaldo Carvalho de Melo025bf7e2014-06-14 15:44:52 +0200120 u16 header_offset = browser->show_headers ? 1 : 0;
121
122 ui_browser__gotorc(&browser->b, row + header_offset, column);
Arnaldo Carvalho de Meloca3ff332014-07-01 16:42:24 -0300123}
124
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300125static void hist_browser__reset(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300126{
Namhyung Kimc3b78952014-04-22 15:56:17 +0900127 /*
128 * The hists__remove_entry_filter() already folds non-filtered
129 * entries so we can assume it has 0 callchain rows.
130 */
131 browser->nr_callchain_rows = 0;
132
Namhyung Kim268397c2014-04-22 14:49:31 +0900133 hist_browser__update_nr_entries(browser);
Namhyung Kimc3b78952014-04-22 15:56:17 +0900134 browser->b.nr_entries = hist_browser__nr_entries(browser);
Arnaldo Carvalho de Melo357cfff2014-07-01 17:01:01 -0300135 hist_browser__refresh_dimensions(&browser->b);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300136 ui_browser__reset_index(&browser->b);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300137}
138
139static char tree__folded_sign(bool unfolded)
140{
141 return unfolded ? '-' : '+';
142}
143
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300144static char hist_entry__folded(const struct hist_entry *he)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300145{
Namhyung Kim3698dab2015-05-05 23:55:46 +0900146 return he->has_children ? tree__folded_sign(he->unfolded) : ' ';
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300147}
148
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300149static char callchain_list__folded(const struct callchain_list *cl)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300150{
Namhyung Kim3698dab2015-05-05 23:55:46 +0900151 return cl->has_children ? tree__folded_sign(cl->unfolded) : ' ';
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300152}
153
Namhyung Kim3698dab2015-05-05 23:55:46 +0900154static void callchain_list__set_folding(struct callchain_list *cl, bool unfold)
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300155{
Namhyung Kim3698dab2015-05-05 23:55:46 +0900156 cl->unfolded = unfold ? cl->has_children : false;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300157}
158
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300159static int callchain_node__count_rows_rb_tree(struct callchain_node *node)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300160{
161 int n = 0;
162 struct rb_node *nd;
163
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300164 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300165 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
166 struct callchain_list *chain;
167 char folded_sign = ' '; /* No children */
168
169 list_for_each_entry(chain, &child->val, list) {
170 ++n;
171 /* We need this because we may not have children */
172 folded_sign = callchain_list__folded(chain);
173 if (folded_sign == '+')
174 break;
175 }
176
177 if (folded_sign == '-') /* Have children and they're unfolded */
178 n += callchain_node__count_rows_rb_tree(child);
179 }
180
181 return n;
182}
183
Namhyung Kim4b3a3212015-11-09 14:45:43 +0900184static int callchain_node__count_flat_rows(struct callchain_node *node)
185{
186 struct callchain_list *chain;
187 char folded_sign = 0;
188 int n = 0;
189
190 list_for_each_entry(chain, &node->parent_val, list) {
191 if (!folded_sign) {
192 /* only check first chain list entry */
193 folded_sign = callchain_list__folded(chain);
194 if (folded_sign == '+')
195 return 1;
196 }
197 n++;
198 }
199
200 list_for_each_entry(chain, &node->val, list) {
201 if (!folded_sign) {
202 /* node->parent_val list might be empty */
203 folded_sign = callchain_list__folded(chain);
204 if (folded_sign == '+')
205 return 1;
206 }
207 n++;
208 }
209
210 return n;
211}
212
Namhyung Kim8c430a32015-11-09 14:45:44 +0900213static int callchain_node__count_folded_rows(struct callchain_node *node __maybe_unused)
214{
215 return 1;
216}
217
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300218static int callchain_node__count_rows(struct callchain_node *node)
219{
220 struct callchain_list *chain;
221 bool unfolded = false;
222 int n = 0;
223
Namhyung Kim4b3a3212015-11-09 14:45:43 +0900224 if (callchain_param.mode == CHAIN_FLAT)
225 return callchain_node__count_flat_rows(node);
Namhyung Kim8c430a32015-11-09 14:45:44 +0900226 else if (callchain_param.mode == CHAIN_FOLDED)
227 return callchain_node__count_folded_rows(node);
Namhyung Kim4b3a3212015-11-09 14:45:43 +0900228
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300229 list_for_each_entry(chain, &node->val, list) {
230 ++n;
Namhyung Kim3698dab2015-05-05 23:55:46 +0900231 unfolded = chain->unfolded;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300232 }
233
234 if (unfolded)
235 n += callchain_node__count_rows_rb_tree(node);
236
237 return n;
238}
239
240static int callchain__count_rows(struct rb_root *chain)
241{
242 struct rb_node *nd;
243 int n = 0;
244
245 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
246 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
247 n += callchain_node__count_rows(node);
248 }
249
250 return n;
251}
252
Namhyung Kimf5b763f2016-02-25 00:13:43 +0900253static int hierarchy_count_rows(struct hist_browser *hb, struct hist_entry *he,
254 bool include_children)
255{
256 int count = 0;
257 struct rb_node *node;
258 struct hist_entry *child;
259
260 if (he->leaf)
261 return callchain__count_rows(&he->sorted_chain);
262
263 node = rb_first(&he->hroot_out);
264 while (node) {
265 float percent;
266
267 child = rb_entry(node, struct hist_entry, rb_node);
268 percent = hist_entry__get_percent_limit(child);
269
270 if (!child->filtered && percent >= hb->min_pcnt) {
271 count++;
272
273 if (include_children && child->unfolded)
274 count += hierarchy_count_rows(hb, child, true);
275 }
276
277 node = rb_next(node);
278 }
279 return count;
280}
281
Namhyung Kim3698dab2015-05-05 23:55:46 +0900282static bool hist_entry__toggle_fold(struct hist_entry *he)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300283{
Namhyung Kim3698dab2015-05-05 23:55:46 +0900284 if (!he)
Jiri Olsa8493fe12012-04-04 22:21:31 +0200285 return false;
286
Namhyung Kim3698dab2015-05-05 23:55:46 +0900287 if (!he->has_children)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300288 return false;
289
Namhyung Kim3698dab2015-05-05 23:55:46 +0900290 he->unfolded = !he->unfolded;
291 return true;
292}
293
294static bool callchain_list__toggle_fold(struct callchain_list *cl)
295{
296 if (!cl)
297 return false;
298
299 if (!cl->has_children)
300 return false;
301
302 cl->unfolded = !cl->unfolded;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300303 return true;
304}
305
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300306static void callchain_node__init_have_children_rb_tree(struct callchain_node *node)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300307{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300308 struct rb_node *nd = rb_first(&node->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300309
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300310 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300311 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
312 struct callchain_list *chain;
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300313 bool first = true;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300314
315 list_for_each_entry(chain, &child->val, list) {
316 if (first) {
317 first = false;
Namhyung Kim3698dab2015-05-05 23:55:46 +0900318 chain->has_children = chain->list.next != &child->val ||
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300319 !RB_EMPTY_ROOT(&child->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300320 } else
Namhyung Kim3698dab2015-05-05 23:55:46 +0900321 chain->has_children = chain->list.next == &child->val &&
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300322 !RB_EMPTY_ROOT(&child->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300323 }
324
325 callchain_node__init_have_children_rb_tree(child);
326 }
327}
328
Namhyung Kima7444af2014-11-24 17:13:27 +0900329static void callchain_node__init_have_children(struct callchain_node *node,
330 bool has_sibling)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300331{
332 struct callchain_list *chain;
333
Namhyung Kima7444af2014-11-24 17:13:27 +0900334 chain = list_entry(node->val.next, struct callchain_list, list);
Namhyung Kim3698dab2015-05-05 23:55:46 +0900335 chain->has_children = has_sibling;
Namhyung Kima7444af2014-11-24 17:13:27 +0900336
Namhyung Kim4b3a3212015-11-09 14:45:43 +0900337 if (node->val.next != node->val.prev) {
Namhyung Kim82162b52014-08-13 15:02:41 +0900338 chain = list_entry(node->val.prev, struct callchain_list, list);
Namhyung Kim3698dab2015-05-05 23:55:46 +0900339 chain->has_children = !RB_EMPTY_ROOT(&node->rb_root);
Namhyung Kim82162b52014-08-13 15:02:41 +0900340 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300341
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300342 callchain_node__init_have_children_rb_tree(node);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300343}
344
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300345static void callchain__init_have_children(struct rb_root *root)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300346{
Namhyung Kima7444af2014-11-24 17:13:27 +0900347 struct rb_node *nd = rb_first(root);
348 bool has_sibling = nd && rb_next(nd);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300349
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300350 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300351 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
Namhyung Kima7444af2014-11-24 17:13:27 +0900352 callchain_node__init_have_children(node, has_sibling);
Namhyung Kim8c430a32015-11-09 14:45:44 +0900353 if (callchain_param.mode == CHAIN_FLAT ||
354 callchain_param.mode == CHAIN_FOLDED)
Namhyung Kim4b3a3212015-11-09 14:45:43 +0900355 callchain_node__make_parent_list(node);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300356 }
357}
358
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300359static void hist_entry__init_have_children(struct hist_entry *he)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300360{
Namhyung Kimf5b763f2016-02-25 00:13:43 +0900361 if (he->init_have_children)
362 return;
363
364 if (he->leaf) {
Namhyung Kim3698dab2015-05-05 23:55:46 +0900365 he->has_children = !RB_EMPTY_ROOT(&he->sorted_chain);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300366 callchain__init_have_children(&he->sorted_chain);
Namhyung Kimf5b763f2016-02-25 00:13:43 +0900367 } else {
368 he->has_children = !RB_EMPTY_ROOT(&he->hroot_out);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300369 }
Namhyung Kimf5b763f2016-02-25 00:13:43 +0900370
371 he->init_have_children = true;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300372}
373
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300374static bool hist_browser__toggle_fold(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300375{
Namhyung Kim3698dab2015-05-05 23:55:46 +0900376 struct hist_entry *he = browser->he_selection;
377 struct map_symbol *ms = browser->selection;
378 struct callchain_list *cl = container_of(ms, struct callchain_list, ms);
379 bool has_children;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300380
Wang Nan4938cf02015-12-07 02:35:44 +0000381 if (!he || !ms)
382 return false;
383
Namhyung Kim3698dab2015-05-05 23:55:46 +0900384 if (ms == &he->ms)
385 has_children = hist_entry__toggle_fold(he);
386 else
387 has_children = callchain_list__toggle_fold(cl);
388
389 if (has_children) {
Namhyung Kimf5b763f2016-02-25 00:13:43 +0900390 int child_rows = 0;
391
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300392 hist_entry__init_have_children(he);
Namhyung Kimc3b78952014-04-22 15:56:17 +0900393 browser->b.nr_entries -= he->nr_rows;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300394
Namhyung Kimf5b763f2016-02-25 00:13:43 +0900395 if (he->leaf)
396 browser->nr_callchain_rows -= he->nr_rows;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300397 else
Namhyung Kimf5b763f2016-02-25 00:13:43 +0900398 browser->nr_hierarchy_entries -= he->nr_rows;
399
400 if (symbol_conf.report_hierarchy)
401 child_rows = hierarchy_count_rows(browser, he, true);
402
403 if (he->unfolded) {
404 if (he->leaf)
405 he->nr_rows = callchain__count_rows(&he->sorted_chain);
406 else
407 he->nr_rows = hierarchy_count_rows(browser, he, false);
408
409 /* account grand children */
410 if (symbol_conf.report_hierarchy)
411 browser->b.nr_entries += child_rows - he->nr_rows;
412 } else {
413 if (symbol_conf.report_hierarchy)
414 browser->b.nr_entries -= child_rows - he->nr_rows;
415
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300416 he->nr_rows = 0;
Namhyung Kimf5b763f2016-02-25 00:13:43 +0900417 }
Namhyung Kimc3b78952014-04-22 15:56:17 +0900418
419 browser->b.nr_entries += he->nr_rows;
Namhyung Kimf5b763f2016-02-25 00:13:43 +0900420
421 if (he->leaf)
422 browser->nr_callchain_rows += he->nr_rows;
423 else
424 browser->nr_hierarchy_entries += he->nr_rows;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300425
426 return true;
427 }
428
429 /* If it doesn't have children, no toggling performed */
430 return false;
431}
432
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300433static int callchain_node__set_folding_rb_tree(struct callchain_node *node, bool unfold)
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300434{
435 int n = 0;
436 struct rb_node *nd;
437
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300438 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300439 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
440 struct callchain_list *chain;
441 bool has_children = false;
442
443 list_for_each_entry(chain, &child->val, list) {
444 ++n;
Namhyung Kim3698dab2015-05-05 23:55:46 +0900445 callchain_list__set_folding(chain, unfold);
446 has_children = chain->has_children;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300447 }
448
449 if (has_children)
450 n += callchain_node__set_folding_rb_tree(child, unfold);
451 }
452
453 return n;
454}
455
456static int callchain_node__set_folding(struct callchain_node *node, bool unfold)
457{
458 struct callchain_list *chain;
459 bool has_children = false;
460 int n = 0;
461
462 list_for_each_entry(chain, &node->val, list) {
463 ++n;
Namhyung Kim3698dab2015-05-05 23:55:46 +0900464 callchain_list__set_folding(chain, unfold);
465 has_children = chain->has_children;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300466 }
467
468 if (has_children)
469 n += callchain_node__set_folding_rb_tree(node, unfold);
470
471 return n;
472}
473
474static int callchain__set_folding(struct rb_root *chain, bool unfold)
475{
476 struct rb_node *nd;
477 int n = 0;
478
479 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
480 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
481 n += callchain_node__set_folding(node, unfold);
482 }
483
484 return n;
485}
486
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300487static void hist_entry__set_folding(struct hist_entry *he, bool unfold)
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300488{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300489 hist_entry__init_have_children(he);
Namhyung Kim3698dab2015-05-05 23:55:46 +0900490 he->unfolded = unfold ? he->has_children : false;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300491
Namhyung Kim3698dab2015-05-05 23:55:46 +0900492 if (he->has_children) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300493 int n = callchain__set_folding(&he->sorted_chain, unfold);
494 he->nr_rows = unfold ? n : 0;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300495 } else
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300496 he->nr_rows = 0;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300497}
498
Namhyung Kimc3b78952014-04-22 15:56:17 +0900499static void
500__hist_browser__set_folding(struct hist_browser *browser, bool unfold)
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300501{
502 struct rb_node *nd;
Namhyung Kimc3b78952014-04-22 15:56:17 +0900503 struct hists *hists = browser->hists;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300504
Namhyung Kimc3b78952014-04-22 15:56:17 +0900505 for (nd = rb_first(&hists->entries);
Namhyung Kim14135662013-10-31 10:17:39 +0900506 (nd = hists__filter_entries(nd, browser->min_pcnt)) != NULL;
Namhyung Kimc3b78952014-04-22 15:56:17 +0900507 nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300508 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
509 hist_entry__set_folding(he, unfold);
Namhyung Kimc3b78952014-04-22 15:56:17 +0900510 browser->nr_callchain_rows += he->nr_rows;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300511 }
512}
513
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300514static void hist_browser__set_folding(struct hist_browser *browser, bool unfold)
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300515{
Namhyung Kimc3b78952014-04-22 15:56:17 +0900516 browser->nr_callchain_rows = 0;
517 __hist_browser__set_folding(browser, unfold);
518
519 browser->b.nr_entries = hist_browser__nr_entries(browser);
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300520 /* Go to the start, we may be way after valid entries after a collapse */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300521 ui_browser__reset_index(&browser->b);
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300522}
523
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200524static void ui_browser__warn_lost_events(struct ui_browser *browser)
525{
526 ui_browser__warning(browser, 4,
527 "Events are being lost, check IO/CPU overload!\n\n"
528 "You may want to run 'perf' using a RT scheduler policy:\n\n"
529 " perf top -r 80\n\n"
530 "Or reduce the sampling frequency.");
531}
532
Arnaldo Carvalho de Melo5f00b0f2015-06-19 17:30:20 -0300533static int hist_browser__run(struct hist_browser *browser, const char *help)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300534{
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300535 int key;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300536 char title[160];
Namhyung Kimc2a51ab2015-04-22 16:18:15 +0900537 struct hist_browser_timer *hbt = browser->hbt;
Namhyung Kim9783adf2012-11-02 14:50:05 +0900538 int delay_secs = hbt ? hbt->refresh : 0;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300539
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300540 browser->b.entries = &browser->hists->entries;
Namhyung Kimc3b78952014-04-22 15:56:17 +0900541 browser->b.nr_entries = hist_browser__nr_entries(browser);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300542
Taeung Song1e378eb2014-10-07 16:13:15 +0900543 hists__browser_title(browser->hists, hbt, title, sizeof(title));
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300544
Namhyung Kim090cff32016-01-11 19:53:14 +0900545 if (ui_browser__show(&browser->b, title, "%s", help) < 0)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300546 return -1;
547
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300548 while (1) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300549 key = ui_browser__run(&browser->b, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300550
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300551 switch (key) {
Namhyung Kimfa5df942013-05-14 11:09:05 +0900552 case K_TIMER: {
553 u64 nr_entries;
Namhyung Kim9783adf2012-11-02 14:50:05 +0900554 hbt->timer(hbt->arg);
Namhyung Kimfa5df942013-05-14 11:09:05 +0900555
Namhyung Kimc3b78952014-04-22 15:56:17 +0900556 if (hist_browser__has_filter(browser))
Namhyung Kim112f7612014-04-22 14:05:35 +0900557 hist_browser__update_nr_entries(browser);
Namhyung Kimfa5df942013-05-14 11:09:05 +0900558
Namhyung Kimc3b78952014-04-22 15:56:17 +0900559 nr_entries = hist_browser__nr_entries(browser);
Namhyung Kimfa5df942013-05-14 11:09:05 +0900560 ui_browser__update_nr_entries(&browser->b, nr_entries);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200561
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300562 if (browser->hists->stats.nr_lost_warned !=
563 browser->hists->stats.nr_events[PERF_RECORD_LOST]) {
564 browser->hists->stats.nr_lost_warned =
565 browser->hists->stats.nr_events[PERF_RECORD_LOST];
566 ui_browser__warn_lost_events(&browser->b);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200567 }
568
Taeung Song1e378eb2014-10-07 16:13:15 +0900569 hists__browser_title(browser->hists,
570 hbt, title, sizeof(title));
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300571 ui_browser__show_title(&browser->b, title);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300572 continue;
Namhyung Kimfa5df942013-05-14 11:09:05 +0900573 }
Arnaldo Carvalho de Melo46941532010-08-10 15:50:07 -0300574 case 'D': { /* Debug */
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300575 static int seq;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300576 struct hist_entry *h = rb_entry(browser->b.top,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300577 struct hist_entry, rb_node);
578 ui_helpline__pop();
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300579 ui_helpline__fpush("%d: nr_ent=(%d,%d), rows=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300580 seq++, browser->b.nr_entries,
581 browser->hists->nr_entries,
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -0300582 browser->b.rows,
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300583 browser->b.index,
584 browser->b.top_idx,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300585 h->row_offset, h->nr_rows);
586 }
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300587 break;
588 case 'C':
589 /* Collapse the whole world. */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300590 hist_browser__set_folding(browser, false);
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300591 break;
592 case 'E':
593 /* Expand the whole world. */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300594 hist_browser__set_folding(browser, true);
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300595 break;
Arnaldo Carvalho de Melo025bf7e2014-06-14 15:44:52 +0200596 case 'H':
597 browser->show_headers = !browser->show_headers;
598 hist_browser__update_rows(browser);
599 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200600 case K_ENTER:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300601 if (hist_browser__toggle_fold(browser))
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300602 break;
603 /* fall thru */
604 default:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300605 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300606 }
607 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300608out:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300609 ui_browser__hide(&browser->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300610 return key;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300611}
612
Namhyung Kim39ee5332014-08-22 09:13:21 +0900613struct callchain_print_arg {
614 /* for hists browser */
615 off_t row_offset;
616 bool is_current_entry;
617
618 /* for file dump */
619 FILE *fp;
620 int printed;
621};
622
623typedef void (*print_callchain_entry_fn)(struct hist_browser *browser,
624 struct callchain_list *chain,
625 const char *str, int offset,
626 unsigned short row,
627 struct callchain_print_arg *arg);
628
Namhyung Kimf4536dd2014-08-20 17:07:57 +0900629static void hist_browser__show_callchain_entry(struct hist_browser *browser,
630 struct callchain_list *chain,
Namhyung Kim39ee5332014-08-22 09:13:21 +0900631 const char *str, int offset,
632 unsigned short row,
633 struct callchain_print_arg *arg)
Namhyung Kimf4536dd2014-08-20 17:07:57 +0900634{
635 int color, width;
Namhyung Kim39ee5332014-08-22 09:13:21 +0900636 char folded_sign = callchain_list__folded(chain);
Arnaldo Carvalho de Melo70e97272015-03-19 16:07:21 -0300637 bool show_annotated = browser->show_dso && chain->ms.sym && symbol__annotation(chain->ms.sym)->src;
Namhyung Kimf4536dd2014-08-20 17:07:57 +0900638
639 color = HE_COLORSET_NORMAL;
640 width = browser->b.width - (offset + 2);
641 if (ui_browser__is_current_entry(&browser->b, row)) {
642 browser->selection = &chain->ms;
643 color = HE_COLORSET_SELECTED;
Namhyung Kim39ee5332014-08-22 09:13:21 +0900644 arg->is_current_entry = true;
Namhyung Kimf4536dd2014-08-20 17:07:57 +0900645 }
646
647 ui_browser__set_color(&browser->b, color);
648 hist_browser__gotorc(browser, row, 0);
Arnaldo Carvalho de Melo26270a02015-08-11 12:24:27 -0300649 ui_browser__write_nstring(&browser->b, " ", offset);
Arnaldo Carvalho de Melo517dfdb2015-08-11 12:50:55 -0300650 ui_browser__printf(&browser->b, "%c", folded_sign);
Arnaldo Carvalho de Melo70e97272015-03-19 16:07:21 -0300651 ui_browser__write_graph(&browser->b, show_annotated ? SLSMG_RARROW_CHAR : ' ');
Arnaldo Carvalho de Melo26270a02015-08-11 12:24:27 -0300652 ui_browser__write_nstring(&browser->b, str, width);
Namhyung Kimf4536dd2014-08-20 17:07:57 +0900653}
654
Namhyung Kim39ee5332014-08-22 09:13:21 +0900655static void hist_browser__fprintf_callchain_entry(struct hist_browser *b __maybe_unused,
656 struct callchain_list *chain,
657 const char *str, int offset,
658 unsigned short row __maybe_unused,
659 struct callchain_print_arg *arg)
660{
661 char folded_sign = callchain_list__folded(chain);
662
663 arg->printed += fprintf(arg->fp, "%*s%c %s\n", offset, " ",
664 folded_sign, str);
665}
666
667typedef bool (*check_output_full_fn)(struct hist_browser *browser,
668 unsigned short row);
669
670static bool hist_browser__check_output_full(struct hist_browser *browser,
671 unsigned short row)
672{
673 return browser->b.rows == row;
674}
675
676static bool hist_browser__check_dump_full(struct hist_browser *browser __maybe_unused,
677 unsigned short row __maybe_unused)
678{
679 return false;
680}
681
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300682#define LEVEL_OFFSET_STEP 3
683
Namhyung Kim18bb8382015-11-09 14:45:42 +0900684static int hist_browser__show_callchain_list(struct hist_browser *browser,
685 struct callchain_node *node,
686 struct callchain_list *chain,
687 unsigned short row, u64 total,
688 bool need_percent, int offset,
689 print_callchain_entry_fn print,
690 struct callchain_print_arg *arg)
691{
692 char bf[1024], *alloc_str;
693 const char *str;
694
695 if (arg->row_offset != 0) {
696 arg->row_offset--;
697 return 0;
698 }
699
700 alloc_str = NULL;
701 str = callchain_list__sym_name(chain, bf, sizeof(bf),
702 browser->show_dso);
703
704 if (need_percent) {
705 char buf[64];
706
707 callchain_node__scnprintf_value(node, buf, sizeof(buf),
708 total);
709
710 if (asprintf(&alloc_str, "%s %s", buf, str) < 0)
711 str = "Not enough memory!";
712 else
713 str = alloc_str;
714 }
715
716 print(browser, chain, str, offset, row, arg);
717
718 free(alloc_str);
719 return 1;
720}
721
Namhyung Kim59c624e2016-01-28 00:40:56 +0900722static bool check_percent_display(struct rb_node *node, u64 parent_total)
723{
724 struct callchain_node *child;
725
726 if (node == NULL)
727 return false;
728
729 if (rb_next(node))
730 return true;
731
732 child = rb_entry(node, struct callchain_node, rb_node);
733 return callchain_cumul_hits(child) != parent_total;
734}
735
Namhyung Kim4b3a3212015-11-09 14:45:43 +0900736static int hist_browser__show_callchain_flat(struct hist_browser *browser,
737 struct rb_root *root,
738 unsigned short row, u64 total,
Namhyung Kim59c624e2016-01-28 00:40:56 +0900739 u64 parent_total,
Namhyung Kim4b3a3212015-11-09 14:45:43 +0900740 print_callchain_entry_fn print,
741 struct callchain_print_arg *arg,
742 check_output_full_fn is_output_full)
743{
744 struct rb_node *node;
745 int first_row = row, offset = LEVEL_OFFSET_STEP;
746 bool need_percent;
747
748 node = rb_first(root);
Namhyung Kim59c624e2016-01-28 00:40:56 +0900749 need_percent = check_percent_display(node, parent_total);
Namhyung Kim4b3a3212015-11-09 14:45:43 +0900750
751 while (node) {
752 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
753 struct rb_node *next = rb_next(node);
754 struct callchain_list *chain;
755 char folded_sign = ' ';
756 int first = true;
757 int extra_offset = 0;
758
759 list_for_each_entry(chain, &child->parent_val, list) {
760 bool was_first = first;
761
762 if (first)
763 first = false;
764 else if (need_percent)
765 extra_offset = LEVEL_OFFSET_STEP;
766
767 folded_sign = callchain_list__folded(chain);
768
769 row += hist_browser__show_callchain_list(browser, child,
770 chain, row, total,
771 was_first && need_percent,
772 offset + extra_offset,
773 print, arg);
774
775 if (is_output_full(browser, row))
776 goto out;
777
778 if (folded_sign == '+')
779 goto next;
780 }
781
782 list_for_each_entry(chain, &child->val, list) {
783 bool was_first = first;
784
785 if (first)
786 first = false;
787 else if (need_percent)
788 extra_offset = LEVEL_OFFSET_STEP;
789
790 folded_sign = callchain_list__folded(chain);
791
792 row += hist_browser__show_callchain_list(browser, child,
793 chain, row, total,
794 was_first && need_percent,
795 offset + extra_offset,
796 print, arg);
797
798 if (is_output_full(browser, row))
799 goto out;
800
801 if (folded_sign == '+')
802 break;
803 }
804
805next:
806 if (is_output_full(browser, row))
807 break;
808 node = next;
809 }
810out:
811 return row - first_row;
812}
813
Namhyung Kim8c430a32015-11-09 14:45:44 +0900814static char *hist_browser__folded_callchain_str(struct hist_browser *browser,
815 struct callchain_list *chain,
816 char *value_str, char *old_str)
817{
818 char bf[1024];
819 const char *str;
820 char *new;
821
822 str = callchain_list__sym_name(chain, bf, sizeof(bf),
823 browser->show_dso);
824 if (old_str) {
825 if (asprintf(&new, "%s%s%s", old_str,
826 symbol_conf.field_sep ?: ";", str) < 0)
827 new = NULL;
828 } else {
829 if (value_str) {
830 if (asprintf(&new, "%s %s", value_str, str) < 0)
831 new = NULL;
832 } else {
833 if (asprintf(&new, "%s", str) < 0)
834 new = NULL;
835 }
836 }
837 return new;
838}
839
840static int hist_browser__show_callchain_folded(struct hist_browser *browser,
841 struct rb_root *root,
842 unsigned short row, u64 total,
Namhyung Kim59c624e2016-01-28 00:40:56 +0900843 u64 parent_total,
Namhyung Kim8c430a32015-11-09 14:45:44 +0900844 print_callchain_entry_fn print,
845 struct callchain_print_arg *arg,
846 check_output_full_fn is_output_full)
847{
848 struct rb_node *node;
849 int first_row = row, offset = LEVEL_OFFSET_STEP;
850 bool need_percent;
851
852 node = rb_first(root);
Namhyung Kim59c624e2016-01-28 00:40:56 +0900853 need_percent = check_percent_display(node, parent_total);
Namhyung Kim8c430a32015-11-09 14:45:44 +0900854
855 while (node) {
856 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
857 struct rb_node *next = rb_next(node);
858 struct callchain_list *chain, *first_chain = NULL;
859 int first = true;
860 char *value_str = NULL, *value_str_alloc = NULL;
861 char *chain_str = NULL, *chain_str_alloc = NULL;
862
863 if (arg->row_offset != 0) {
864 arg->row_offset--;
865 goto next;
866 }
867
868 if (need_percent) {
869 char buf[64];
870
871 callchain_node__scnprintf_value(child, buf, sizeof(buf), total);
872 if (asprintf(&value_str, "%s", buf) < 0) {
873 value_str = (char *)"<...>";
874 goto do_print;
875 }
876 value_str_alloc = value_str;
877 }
878
879 list_for_each_entry(chain, &child->parent_val, list) {
880 chain_str = hist_browser__folded_callchain_str(browser,
881 chain, value_str, chain_str);
882 if (first) {
883 first = false;
884 first_chain = chain;
885 }
886
887 if (chain_str == NULL) {
888 chain_str = (char *)"Not enough memory!";
889 goto do_print;
890 }
891
892 chain_str_alloc = chain_str;
893 }
894
895 list_for_each_entry(chain, &child->val, list) {
896 chain_str = hist_browser__folded_callchain_str(browser,
897 chain, value_str, chain_str);
898 if (first) {
899 first = false;
900 first_chain = chain;
901 }
902
903 if (chain_str == NULL) {
904 chain_str = (char *)"Not enough memory!";
905 goto do_print;
906 }
907
908 chain_str_alloc = chain_str;
909 }
910
911do_print:
912 print(browser, first_chain, chain_str, offset, row++, arg);
913 free(value_str_alloc);
914 free(chain_str_alloc);
915
916next:
917 if (is_output_full(browser, row))
918 break;
919 node = next;
920 }
921
922 return row - first_row;
923}
924
Namhyung Kim0c841c62016-01-28 00:40:54 +0900925static int hist_browser__show_callchain_graph(struct hist_browser *browser,
Namhyung Kimc09a7e72014-08-21 10:15:45 +0900926 struct rb_root *root, int level,
Namhyung Kim39ee5332014-08-22 09:13:21 +0900927 unsigned short row, u64 total,
Namhyung Kim5eca1042016-01-28 00:40:55 +0900928 u64 parent_total,
Namhyung Kim39ee5332014-08-22 09:13:21 +0900929 print_callchain_entry_fn print,
930 struct callchain_print_arg *arg,
931 check_output_full_fn is_output_full)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300932{
933 struct rb_node *node;
Namhyung Kimf4536dd2014-08-20 17:07:57 +0900934 int first_row = row, offset = level * LEVEL_OFFSET_STEP;
Namhyung Kim4087d112014-11-24 17:13:26 +0900935 bool need_percent;
Namhyung Kim5eca1042016-01-28 00:40:55 +0900936 u64 percent_total = total;
937
938 if (callchain_param.mode == CHAIN_GRAPH_REL)
939 percent_total = parent_total;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300940
Namhyung Kimc09a7e72014-08-21 10:15:45 +0900941 node = rb_first(root);
Namhyung Kim59c624e2016-01-28 00:40:56 +0900942 need_percent = check_percent_display(node, parent_total);
Namhyung Kim4087d112014-11-24 17:13:26 +0900943
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300944 while (node) {
945 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
946 struct rb_node *next = rb_next(node);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300947 struct callchain_list *chain;
948 char folded_sign = ' ';
949 int first = true;
950 int extra_offset = 0;
951
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300952 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300953 bool was_first = first;
954
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300955 if (first)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300956 first = false;
Namhyung Kim4087d112014-11-24 17:13:26 +0900957 else if (need_percent)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300958 extra_offset = LEVEL_OFFSET_STEP;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300959
960 folded_sign = callchain_list__folded(chain);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300961
Namhyung Kim18bb8382015-11-09 14:45:42 +0900962 row += hist_browser__show_callchain_list(browser, child,
Namhyung Kim5eca1042016-01-28 00:40:55 +0900963 chain, row, percent_total,
Namhyung Kim18bb8382015-11-09 14:45:42 +0900964 was_first && need_percent,
965 offset + extra_offset,
966 print, arg);
Namhyung Kimc09a7e72014-08-21 10:15:45 +0900967
Namhyung Kim18bb8382015-11-09 14:45:42 +0900968 if (is_output_full(browser, row))
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300969 goto out;
Namhyung Kim18bb8382015-11-09 14:45:42 +0900970
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300971 if (folded_sign == '+')
972 break;
973 }
974
975 if (folded_sign == '-') {
976 const int new_level = level + (extra_offset ? 2 : 1);
Namhyung Kimc09a7e72014-08-21 10:15:45 +0900977
Namhyung Kim0c841c62016-01-28 00:40:54 +0900978 row += hist_browser__show_callchain_graph(browser, &child->rb_root,
Namhyung Kim5eca1042016-01-28 00:40:55 +0900979 new_level, row, total,
980 child->children_hit,
Namhyung Kim39ee5332014-08-22 09:13:21 +0900981 print, arg, is_output_full);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300982 }
Namhyung Kim39ee5332014-08-22 09:13:21 +0900983 if (is_output_full(browser, row))
Namhyung Kimc09a7e72014-08-21 10:15:45 +0900984 break;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300985 node = next;
986 }
987out:
988 return row - first_row;
989}
990
Namhyung Kim0c841c62016-01-28 00:40:54 +0900991static int hist_browser__show_callchain(struct hist_browser *browser,
992 struct hist_entry *entry, int level,
993 unsigned short row,
994 print_callchain_entry_fn print,
995 struct callchain_print_arg *arg,
996 check_output_full_fn is_output_full)
997{
998 u64 total = hists__total_period(entry->hists);
Namhyung Kim5eca1042016-01-28 00:40:55 +0900999 u64 parent_total;
Namhyung Kim0c841c62016-01-28 00:40:54 +09001000 int printed;
1001
Namhyung Kim5eca1042016-01-28 00:40:55 +09001002 if (symbol_conf.cumulate_callchain)
1003 parent_total = entry->stat_acc->period;
1004 else
1005 parent_total = entry->stat.period;
Namhyung Kim0c841c62016-01-28 00:40:54 +09001006
1007 if (callchain_param.mode == CHAIN_FLAT) {
1008 printed = hist_browser__show_callchain_flat(browser,
Namhyung Kim5eca1042016-01-28 00:40:55 +09001009 &entry->sorted_chain, row,
1010 total, parent_total, print, arg,
1011 is_output_full);
Namhyung Kim0c841c62016-01-28 00:40:54 +09001012 } else if (callchain_param.mode == CHAIN_FOLDED) {
1013 printed = hist_browser__show_callchain_folded(browser,
Namhyung Kim5eca1042016-01-28 00:40:55 +09001014 &entry->sorted_chain, row,
1015 total, parent_total, print, arg,
1016 is_output_full);
Namhyung Kim0c841c62016-01-28 00:40:54 +09001017 } else {
1018 printed = hist_browser__show_callchain_graph(browser,
Namhyung Kim5eca1042016-01-28 00:40:55 +09001019 &entry->sorted_chain, level, row,
1020 total, parent_total, print, arg,
1021 is_output_full);
Namhyung Kim0c841c62016-01-28 00:40:54 +09001022 }
1023
1024 if (arg->is_current_entry)
1025 browser->he_selection = entry;
1026
1027 return printed;
1028}
1029
Namhyung Kim89701462013-01-22 18:09:38 +09001030struct hpp_arg {
1031 struct ui_browser *b;
1032 char folded_sign;
1033 bool current_entry;
1034};
1035
Namhyung Kim2f6d9002014-03-03 10:14:05 +09001036static int __hpp__slsmg_color_printf(struct perf_hpp *hpp, const char *fmt, ...)
1037{
1038 struct hpp_arg *arg = hpp->ptr;
Namhyung Kimd6751072014-07-31 14:47:36 +09001039 int ret, len;
Namhyung Kim2f6d9002014-03-03 10:14:05 +09001040 va_list args;
1041 double percent;
1042
1043 va_start(args, fmt);
Namhyung Kimd6751072014-07-31 14:47:36 +09001044 len = va_arg(args, int);
Namhyung Kim2f6d9002014-03-03 10:14:05 +09001045 percent = va_arg(args, double);
1046 va_end(args);
Namhyung Kim5aed9d22013-01-22 18:09:35 +09001047
Namhyung Kim89701462013-01-22 18:09:38 +09001048 ui_browser__set_percent_color(arg->b, percent, arg->current_entry);
Namhyung Kim5aed9d22013-01-22 18:09:35 +09001049
Namhyung Kimd6751072014-07-31 14:47:36 +09001050 ret = scnprintf(hpp->buf, hpp->size, fmt, len, percent);
Arnaldo Carvalho de Melo517dfdb2015-08-11 12:50:55 -03001051 ui_browser__printf(arg->b, "%s", hpp->buf);
Namhyung Kim89701462013-01-22 18:09:38 +09001052
Namhyung Kim2f6d9002014-03-03 10:14:05 +09001053 advance_hpp(hpp, ret);
Namhyung Kim5aed9d22013-01-22 18:09:35 +09001054 return ret;
Namhyung Kimf5951d52012-09-03 11:53:09 +09001055}
1056
Namhyung Kimfb821c9e2014-03-03 17:05:19 +09001057#define __HPP_COLOR_PERCENT_FN(_type, _field) \
Namhyung Kim5aed9d22013-01-22 18:09:35 +09001058static u64 __hpp_get_##_field(struct hist_entry *he) \
1059{ \
1060 return he->stat._field; \
1061} \
1062 \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +01001063static int \
Namhyung Kim5b591662014-07-31 14:47:38 +09001064hist_browser__hpp_color_##_type(struct perf_hpp_fmt *fmt, \
Jiri Olsa2c5d4b42013-01-31 23:31:11 +01001065 struct perf_hpp *hpp, \
1066 struct hist_entry *he) \
Namhyung Kim5aed9d22013-01-22 18:09:35 +09001067{ \
Namhyung Kim5b591662014-07-31 14:47:38 +09001068 return hpp__fmt(fmt, hpp, he, __hpp_get_##_field, " %*.2f%%", \
1069 __hpp__slsmg_color_printf, true); \
Namhyung Kim5aed9d22013-01-22 18:09:35 +09001070}
Namhyung Kimf5951d52012-09-03 11:53:09 +09001071
Namhyung Kim0434ddd2013-10-30 16:12:59 +09001072#define __HPP_COLOR_ACC_PERCENT_FN(_type, _field) \
1073static u64 __hpp_get_acc_##_field(struct hist_entry *he) \
1074{ \
1075 return he->stat_acc->_field; \
1076} \
1077 \
1078static int \
Namhyung Kim5b591662014-07-31 14:47:38 +09001079hist_browser__hpp_color_##_type(struct perf_hpp_fmt *fmt, \
Namhyung Kim0434ddd2013-10-30 16:12:59 +09001080 struct perf_hpp *hpp, \
1081 struct hist_entry *he) \
1082{ \
1083 if (!symbol_conf.cumulate_callchain) { \
Arnaldo Carvalho de Melo517dfdb2015-08-11 12:50:55 -03001084 struct hpp_arg *arg = hpp->ptr; \
Namhyung Kim5b591662014-07-31 14:47:38 +09001085 int len = fmt->user_len ?: fmt->len; \
Namhyung Kimd6751072014-07-31 14:47:36 +09001086 int ret = scnprintf(hpp->buf, hpp->size, \
Namhyung Kim5b591662014-07-31 14:47:38 +09001087 "%*s", len, "N/A"); \
Arnaldo Carvalho de Melo517dfdb2015-08-11 12:50:55 -03001088 ui_browser__printf(arg->b, "%s", hpp->buf); \
Namhyung Kim0434ddd2013-10-30 16:12:59 +09001089 \
1090 return ret; \
1091 } \
Namhyung Kim5b591662014-07-31 14:47:38 +09001092 return hpp__fmt(fmt, hpp, he, __hpp_get_acc_##_field, \
1093 " %*.2f%%", __hpp__slsmg_color_printf, true); \
Namhyung Kim0434ddd2013-10-30 16:12:59 +09001094}
1095
Namhyung Kimfb821c9e2014-03-03 17:05:19 +09001096__HPP_COLOR_PERCENT_FN(overhead, period)
1097__HPP_COLOR_PERCENT_FN(overhead_sys, period_sys)
1098__HPP_COLOR_PERCENT_FN(overhead_us, period_us)
1099__HPP_COLOR_PERCENT_FN(overhead_guest_sys, period_guest_sys)
1100__HPP_COLOR_PERCENT_FN(overhead_guest_us, period_guest_us)
Namhyung Kim0434ddd2013-10-30 16:12:59 +09001101__HPP_COLOR_ACC_PERCENT_FN(overhead_acc, period)
Namhyung Kim5aed9d22013-01-22 18:09:35 +09001102
1103#undef __HPP_COLOR_PERCENT_FN
Namhyung Kim0434ddd2013-10-30 16:12:59 +09001104#undef __HPP_COLOR_ACC_PERCENT_FN
Namhyung Kimf5951d52012-09-03 11:53:09 +09001105
1106void hist_browser__init_hpp(void)
1107{
Namhyung Kimf5951d52012-09-03 11:53:09 +09001108 perf_hpp__format[PERF_HPP__OVERHEAD].color =
1109 hist_browser__hpp_color_overhead;
1110 perf_hpp__format[PERF_HPP__OVERHEAD_SYS].color =
1111 hist_browser__hpp_color_overhead_sys;
1112 perf_hpp__format[PERF_HPP__OVERHEAD_US].color =
1113 hist_browser__hpp_color_overhead_us;
1114 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].color =
1115 hist_browser__hpp_color_overhead_guest_sys;
1116 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].color =
1117 hist_browser__hpp_color_overhead_guest_us;
Namhyung Kim0434ddd2013-10-30 16:12:59 +09001118 perf_hpp__format[PERF_HPP__OVERHEAD_ACC].color =
1119 hist_browser__hpp_color_overhead_acc;
Namhyung Kimf5951d52012-09-03 11:53:09 +09001120}
1121
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001122static int hist_browser__show_entry(struct hist_browser *browser,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001123 struct hist_entry *entry,
1124 unsigned short row)
1125{
Jiri Olsa12400052012-10-13 00:06:16 +02001126 int printed = 0;
Namhyung Kim67d25912012-09-12 15:35:06 +09001127 int width = browser->b.width;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001128 char folded_sign = ' ';
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001129 bool current_entry = ui_browser__is_current_entry(&browser->b, row);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001130 off_t row_offset = entry->row_offset;
Namhyung Kim63a1a3d2012-10-15 18:14:35 -03001131 bool first = true;
Jiri Olsa12400052012-10-13 00:06:16 +02001132 struct perf_hpp_fmt *fmt;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001133
1134 if (current_entry) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001135 browser->he_selection = entry;
1136 browser->selection = &entry->ms;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001137 }
1138
1139 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -03001140 hist_entry__init_have_children(entry);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001141 folded_sign = hist_entry__folded(entry);
1142 }
1143
1144 if (row_offset == 0) {
Namhyung Kim89701462013-01-22 18:09:38 +09001145 struct hpp_arg arg = {
Namhyung Kimfb821c9e2014-03-03 17:05:19 +09001146 .b = &browser->b,
Namhyung Kim89701462013-01-22 18:09:38 +09001147 .folded_sign = folded_sign,
1148 .current_entry = current_entry,
1149 };
Arnaldo Carvalho de Meloc6c3c022015-08-11 17:22:43 -03001150 int column = 0;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001151
Arnaldo Carvalho de Meloca3ff332014-07-01 16:42:24 -03001152 hist_browser__gotorc(browser, row, 0);
Namhyung Kimf5951d52012-09-03 11:53:09 +09001153
Jiri Olsaf0786af2016-01-18 10:24:23 +01001154 hists__for_each_format(browser->hists, fmt) {
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001155 char s[2048];
1156 struct perf_hpp hpp = {
1157 .buf = s,
1158 .size = sizeof(s),
1159 .ptr = &arg,
1160 };
1161
Namhyung Kim361459f2015-12-23 02:07:08 +09001162 if (perf_hpp__should_skip(fmt, entry->hists) ||
1163 column++ < browser->b.horiz_scroll)
Namhyung Kime67d49a2014-03-18 13:00:59 +09001164 continue;
1165
Namhyung Kimfb821c9e2014-03-03 17:05:19 +09001166 if (current_entry && browser->b.navkeypressed) {
1167 ui_browser__set_color(&browser->b,
1168 HE_COLORSET_SELECTED);
1169 } else {
1170 ui_browser__set_color(&browser->b,
1171 HE_COLORSET_NORMAL);
1172 }
1173
1174 if (first) {
1175 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo517dfdb2015-08-11 12:50:55 -03001176 ui_browser__printf(&browser->b, "%c ", folded_sign);
Namhyung Kimfb821c9e2014-03-03 17:05:19 +09001177 width -= 2;
1178 }
1179 first = false;
1180 } else {
Arnaldo Carvalho de Melo517dfdb2015-08-11 12:50:55 -03001181 ui_browser__printf(&browser->b, " ");
Namhyung Kimf5951d52012-09-03 11:53:09 +09001182 width -= 2;
1183 }
1184
Jiri Olsa12400052012-10-13 00:06:16 +02001185 if (fmt->color) {
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001186 int ret = fmt->color(fmt, &hpp, entry);
1187 hist_entry__snprintf_alignment(entry, &hpp, fmt, ret);
1188 /*
1189 * fmt->color() already used ui_browser to
1190 * print the non alignment bits, skip it (+ret):
1191 */
1192 ui_browser__printf(&browser->b, "%s", s + ret);
Namhyung Kimf5951d52012-09-03 11:53:09 +09001193 } else {
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001194 hist_entry__snprintf_alignment(entry, &hpp, fmt, fmt->entry(fmt, &hpp, entry));
Arnaldo Carvalho de Melo517dfdb2015-08-11 12:50:55 -03001195 ui_browser__printf(&browser->b, "%s", s);
Namhyung Kimf5951d52012-09-03 11:53:09 +09001196 }
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001197 width -= hpp.buf - s;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001198 }
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -02001199
1200 /* The scroll bar isn't being used */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001201 if (!browser->b.navkeypressed)
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -02001202 width += 1;
1203
Arnaldo Carvalho de Melo26270a02015-08-11 12:24:27 -03001204 ui_browser__write_nstring(&browser->b, "", width);
Namhyung Kim26d8b332014-03-03 16:16:20 +09001205
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001206 ++row;
1207 ++printed;
1208 } else
1209 --row_offset;
1210
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -03001211 if (folded_sign == '-' && row != browser->b.rows) {
Namhyung Kim39ee5332014-08-22 09:13:21 +09001212 struct callchain_print_arg arg = {
1213 .row_offset = row_offset,
1214 .is_current_entry = current_entry,
1215 };
Namhyung Kimc09a7e72014-08-21 10:15:45 +09001216
Namhyung Kim0c841c62016-01-28 00:40:54 +09001217 printed += hist_browser__show_callchain(browser, entry, 1, row,
Namhyung Kim4b3a3212015-11-09 14:45:43 +09001218 hist_browser__show_callchain_entry, &arg,
1219 hist_browser__check_output_full);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001220 }
1221
1222 return printed;
1223}
1224
Jiri Olsa81a888f2014-06-14 15:44:52 +02001225static int advance_hpp_check(struct perf_hpp *hpp, int inc)
1226{
1227 advance_hpp(hpp, inc);
1228 return hpp->size <= 0;
1229}
1230
Arnaldo Carvalho de Meloc6c3c022015-08-11 17:22:43 -03001231static int hists_browser__scnprintf_headers(struct hist_browser *browser, char *buf, size_t size)
Jiri Olsa81a888f2014-06-14 15:44:52 +02001232{
Arnaldo Carvalho de Meloc6c3c022015-08-11 17:22:43 -03001233 struct hists *hists = browser->hists;
Jiri Olsa81a888f2014-06-14 15:44:52 +02001234 struct perf_hpp dummy_hpp = {
1235 .buf = buf,
1236 .size = size,
1237 };
1238 struct perf_hpp_fmt *fmt;
1239 size_t ret = 0;
Arnaldo Carvalho de Meloc6c3c022015-08-11 17:22:43 -03001240 int column = 0;
Jiri Olsa81a888f2014-06-14 15:44:52 +02001241
1242 if (symbol_conf.use_callchain) {
1243 ret = scnprintf(buf, size, " ");
1244 if (advance_hpp_check(&dummy_hpp, ret))
1245 return ret;
1246 }
1247
Jiri Olsaf0786af2016-01-18 10:24:23 +01001248 hists__for_each_format(browser->hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001249 if (perf_hpp__should_skip(fmt, hists) || column++ < browser->b.horiz_scroll)
Jiri Olsa81a888f2014-06-14 15:44:52 +02001250 continue;
1251
Jiri Olsa81a888f2014-06-14 15:44:52 +02001252 ret = fmt->header(fmt, &dummy_hpp, hists_to_evsel(hists));
1253 if (advance_hpp_check(&dummy_hpp, ret))
1254 break;
1255
1256 ret = scnprintf(dummy_hpp.buf, dummy_hpp.size, " ");
1257 if (advance_hpp_check(&dummy_hpp, ret))
1258 break;
1259 }
1260
1261 return ret;
1262}
1263
Arnaldo Carvalho de Melo025bf7e2014-06-14 15:44:52 +02001264static void hist_browser__show_headers(struct hist_browser *browser)
1265{
Jiri Olsa81a888f2014-06-14 15:44:52 +02001266 char headers[1024];
1267
Arnaldo Carvalho de Meloc6c3c022015-08-11 17:22:43 -03001268 hists_browser__scnprintf_headers(browser, headers, sizeof(headers));
Arnaldo Carvalho de Melo025bf7e2014-06-14 15:44:52 +02001269 ui_browser__gotorc(&browser->b, 0, 0);
1270 ui_browser__set_color(&browser->b, HE_COLORSET_ROOT);
Arnaldo Carvalho de Melo26270a02015-08-11 12:24:27 -03001271 ui_browser__write_nstring(&browser->b, headers, browser->b.width + 1);
Arnaldo Carvalho de Melo025bf7e2014-06-14 15:44:52 +02001272}
1273
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -03001274static void ui_browser__hists_init_top(struct ui_browser *browser)
1275{
1276 if (browser->top == NULL) {
1277 struct hist_browser *hb;
1278
1279 hb = container_of(browser, struct hist_browser, b);
1280 browser->top = rb_first(&hb->hists->entries);
1281 }
1282}
1283
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001284static unsigned int hist_browser__refresh(struct ui_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001285{
1286 unsigned row = 0;
Arnaldo Carvalho de Melo025bf7e2014-06-14 15:44:52 +02001287 u16 header_offset = 0;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001288 struct rb_node *nd;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001289 struct hist_browser *hb = container_of(browser, struct hist_browser, b);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001290
Arnaldo Carvalho de Melo025bf7e2014-06-14 15:44:52 +02001291 if (hb->show_headers) {
1292 hist_browser__show_headers(hb);
1293 header_offset = 1;
1294 }
1295
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001296 ui_browser__hists_init_top(browser);
Wang Nan979d2ca2015-12-07 02:35:46 +00001297 hb->he_selection = NULL;
1298 hb->selection = NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001299
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001300 for (nd = browser->top; nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001301 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
Namhyung Kim14135662013-10-31 10:17:39 +09001302 float percent;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001303
1304 if (h->filtered)
1305 continue;
1306
Namhyung Kim14135662013-10-31 10:17:39 +09001307 percent = hist_entry__get_percent_limit(h);
Namhyung Kim064f1982013-05-14 11:09:04 +09001308 if (percent < hb->min_pcnt)
1309 continue;
1310
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001311 row += hist_browser__show_entry(hb, h, row);
Arnaldo Carvalho de Melo62c95ae2014-07-01 11:07:54 -03001312 if (row == browser->rows)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001313 break;
1314 }
1315
Arnaldo Carvalho de Melo025bf7e2014-06-14 15:44:52 +02001316 return row + header_offset;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001317}
1318
Namhyung Kim064f1982013-05-14 11:09:04 +09001319static struct rb_node *hists__filter_entries(struct rb_node *nd,
Namhyung Kim064f1982013-05-14 11:09:04 +09001320 float min_pcnt)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001321{
1322 while (nd != NULL) {
1323 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
Namhyung Kim14135662013-10-31 10:17:39 +09001324 float percent = hist_entry__get_percent_limit(h);
Namhyung Kim064f1982013-05-14 11:09:04 +09001325
Namhyung Kimc0f15272014-04-16 11:16:33 +09001326 if (!h->filtered && percent >= min_pcnt)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001327 return nd;
1328
1329 nd = rb_next(nd);
1330 }
1331
1332 return NULL;
1333}
1334
Namhyung Kim064f1982013-05-14 11:09:04 +09001335static struct rb_node *hists__filter_prev_entries(struct rb_node *nd,
Namhyung Kim064f1982013-05-14 11:09:04 +09001336 float min_pcnt)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001337{
1338 while (nd != NULL) {
1339 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
Namhyung Kim14135662013-10-31 10:17:39 +09001340 float percent = hist_entry__get_percent_limit(h);
Namhyung Kim064f1982013-05-14 11:09:04 +09001341
1342 if (!h->filtered && percent >= min_pcnt)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001343 return nd;
1344
1345 nd = rb_prev(nd);
1346 }
1347
1348 return NULL;
1349}
1350
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001351static void ui_browser__hists_seek(struct ui_browser *browser,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001352 off_t offset, int whence)
1353{
1354 struct hist_entry *h;
1355 struct rb_node *nd;
1356 bool first = true;
Namhyung Kim064f1982013-05-14 11:09:04 +09001357 struct hist_browser *hb;
1358
1359 hb = container_of(browser, struct hist_browser, b);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001360
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001361 if (browser->nr_entries == 0)
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -03001362 return;
1363
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001364 ui_browser__hists_init_top(browser);
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -03001365
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001366 switch (whence) {
1367 case SEEK_SET:
Namhyung Kim064f1982013-05-14 11:09:04 +09001368 nd = hists__filter_entries(rb_first(browser->entries),
Namhyung Kim14135662013-10-31 10:17:39 +09001369 hb->min_pcnt);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001370 break;
1371 case SEEK_CUR:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001372 nd = browser->top;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001373 goto do_offset;
1374 case SEEK_END:
Namhyung Kim064f1982013-05-14 11:09:04 +09001375 nd = hists__filter_prev_entries(rb_last(browser->entries),
Namhyung Kim14135662013-10-31 10:17:39 +09001376 hb->min_pcnt);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001377 first = false;
1378 break;
1379 default:
1380 return;
1381 }
1382
1383 /*
1384 * Moves not relative to the first visible entry invalidates its
1385 * row_offset:
1386 */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001387 h = rb_entry(browser->top, struct hist_entry, rb_node);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001388 h->row_offset = 0;
1389
1390 /*
1391 * Here we have to check if nd is expanded (+), if it is we can't go
1392 * the next top level hist_entry, instead we must compute an offset of
1393 * what _not_ to show and not change the first visible entry.
1394 *
1395 * This offset increments when we are going from top to bottom and
1396 * decreases when we're going from bottom to top.
1397 *
1398 * As we don't have backpointers to the top level in the callchains
1399 * structure, we need to always print the whole hist_entry callchain,
1400 * skipping the first ones that are before the first visible entry
1401 * and stop when we printed enough lines to fill the screen.
1402 */
1403do_offset:
Wang Nan837eeb72015-12-07 02:35:45 +00001404 if (!nd)
1405 return;
1406
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001407 if (offset > 0) {
1408 do {
1409 h = rb_entry(nd, struct hist_entry, rb_node);
Namhyung Kim3698dab2015-05-05 23:55:46 +09001410 if (h->unfolded) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001411 u16 remaining = h->nr_rows - h->row_offset;
1412 if (offset > remaining) {
1413 offset -= remaining;
1414 h->row_offset = 0;
1415 } else {
1416 h->row_offset += offset;
1417 offset = 0;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001418 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001419 break;
1420 }
1421 }
Namhyung Kim14135662013-10-31 10:17:39 +09001422 nd = hists__filter_entries(rb_next(nd), hb->min_pcnt);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001423 if (nd == NULL)
1424 break;
1425 --offset;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001426 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001427 } while (offset != 0);
1428 } else if (offset < 0) {
1429 while (1) {
1430 h = rb_entry(nd, struct hist_entry, rb_node);
Namhyung Kim3698dab2015-05-05 23:55:46 +09001431 if (h->unfolded) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001432 if (first) {
1433 if (-offset > h->row_offset) {
1434 offset += h->row_offset;
1435 h->row_offset = 0;
1436 } else {
1437 h->row_offset += offset;
1438 offset = 0;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001439 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001440 break;
1441 }
1442 } else {
1443 if (-offset > h->nr_rows) {
1444 offset += h->nr_rows;
1445 h->row_offset = 0;
1446 } else {
1447 h->row_offset = h->nr_rows + offset;
1448 offset = 0;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001449 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001450 break;
1451 }
1452 }
1453 }
1454
Namhyung Kim14135662013-10-31 10:17:39 +09001455 nd = hists__filter_prev_entries(rb_prev(nd),
Namhyung Kim064f1982013-05-14 11:09:04 +09001456 hb->min_pcnt);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001457 if (nd == NULL)
1458 break;
1459 ++offset;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001460 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001461 if (offset == 0) {
1462 /*
1463 * Last unfiltered hist_entry, check if it is
1464 * unfolded, if it is then we should have
1465 * row_offset at its last entry.
1466 */
1467 h = rb_entry(nd, struct hist_entry, rb_node);
Namhyung Kim3698dab2015-05-05 23:55:46 +09001468 if (h->unfolded)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001469 h->row_offset = h->nr_rows;
1470 break;
1471 }
1472 first = false;
1473 }
1474 } else {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001475 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001476 h = rb_entry(nd, struct hist_entry, rb_node);
1477 h->row_offset = 0;
1478 }
1479}
1480
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001481static int hist_browser__fprintf_callchain(struct hist_browser *browser,
Namhyung Kim39ee5332014-08-22 09:13:21 +09001482 struct hist_entry *he, FILE *fp)
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001483{
Namhyung Kim39ee5332014-08-22 09:13:21 +09001484 struct callchain_print_arg arg = {
1485 .fp = fp,
1486 };
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001487
Namhyung Kim0c841c62016-01-28 00:40:54 +09001488 hist_browser__show_callchain(browser, he, 1, 0,
Namhyung Kim39ee5332014-08-22 09:13:21 +09001489 hist_browser__fprintf_callchain_entry, &arg,
1490 hist_browser__check_dump_full);
1491 return arg.printed;
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001492}
1493
1494static int hist_browser__fprintf_entry(struct hist_browser *browser,
1495 struct hist_entry *he, FILE *fp)
1496{
1497 char s[8192];
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001498 int printed = 0;
1499 char folded_sign = ' ';
Namhyung Kim26d8b332014-03-03 16:16:20 +09001500 struct perf_hpp hpp = {
1501 .buf = s,
1502 .size = sizeof(s),
1503 };
1504 struct perf_hpp_fmt *fmt;
1505 bool first = true;
1506 int ret;
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001507
1508 if (symbol_conf.use_callchain)
1509 folded_sign = hist_entry__folded(he);
1510
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001511 if (symbol_conf.use_callchain)
1512 printed += fprintf(fp, "%c ", folded_sign);
1513
Jiri Olsaf0786af2016-01-18 10:24:23 +01001514 hists__for_each_format(browser->hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001515 if (perf_hpp__should_skip(fmt, he->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001516 continue;
1517
Namhyung Kim26d8b332014-03-03 16:16:20 +09001518 if (!first) {
1519 ret = scnprintf(hpp.buf, hpp.size, " ");
1520 advance_hpp(&hpp, ret);
1521 } else
1522 first = false;
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001523
Namhyung Kim26d8b332014-03-03 16:16:20 +09001524 ret = fmt->entry(fmt, &hpp, he);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001525 ret = hist_entry__snprintf_alignment(he, &hpp, fmt, ret);
Namhyung Kim26d8b332014-03-03 16:16:20 +09001526 advance_hpp(&hpp, ret);
1527 }
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001528 printed += fprintf(fp, "%s\n", s);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001529
1530 if (folded_sign == '-')
Namhyung Kim39ee5332014-08-22 09:13:21 +09001531 printed += hist_browser__fprintf_callchain(browser, he, fp);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001532
1533 return printed;
1534}
1535
1536static int hist_browser__fprintf(struct hist_browser *browser, FILE *fp)
1537{
Namhyung Kim064f1982013-05-14 11:09:04 +09001538 struct rb_node *nd = hists__filter_entries(rb_first(browser->b.entries),
Namhyung Kim064f1982013-05-14 11:09:04 +09001539 browser->min_pcnt);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001540 int printed = 0;
1541
1542 while (nd) {
1543 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1544
1545 printed += hist_browser__fprintf_entry(browser, h, fp);
Namhyung Kim14135662013-10-31 10:17:39 +09001546 nd = hists__filter_entries(rb_next(nd), browser->min_pcnt);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001547 }
1548
1549 return printed;
1550}
1551
1552static int hist_browser__dump(struct hist_browser *browser)
1553{
1554 char filename[64];
1555 FILE *fp;
1556
1557 while (1) {
1558 scnprintf(filename, sizeof(filename), "perf.hist.%d", browser->print_seq);
1559 if (access(filename, F_OK))
1560 break;
1561 /*
1562 * XXX: Just an arbitrary lazy upper limit
1563 */
1564 if (++browser->print_seq == 8192) {
1565 ui_helpline__fpush("Too many perf.hist.N files, nothing written!");
1566 return -1;
1567 }
1568 }
1569
1570 fp = fopen(filename, "w");
1571 if (fp == NULL) {
1572 char bf[64];
Kirill A. Shutemov4cc49d42012-07-24 00:06:54 +03001573 const char *err = strerror_r(errno, bf, sizeof(bf));
1574 ui_helpline__fpush("Couldn't write to %s: %s", filename, err);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001575 return -1;
1576 }
1577
1578 ++browser->print_seq;
1579 hist_browser__fprintf(browser, fp);
1580 fclose(fp);
1581 ui_helpline__fpush("%s written!", filename);
1582
1583 return 0;
1584}
1585
Namhyung Kimc2a51ab2015-04-22 16:18:15 +09001586static struct hist_browser *hist_browser__new(struct hists *hists,
Namhyung Kimb1a9cee2015-04-22 16:18:17 +09001587 struct hist_browser_timer *hbt,
Kan Liangce80d3b2015-08-28 05:48:04 -04001588 struct perf_env *env)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001589{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001590 struct hist_browser *browser = zalloc(sizeof(*browser));
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001591
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001592 if (browser) {
1593 browser->hists = hists;
1594 browser->b.refresh = hist_browser__refresh;
Arnaldo Carvalho de Melo357cfff2014-07-01 17:01:01 -03001595 browser->b.refresh_dimensions = hist_browser__refresh_dimensions;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001596 browser->b.seek = ui_browser__hists_seek;
1597 browser->b.use_navkeypressed = true;
Jiri Olsac8302362014-06-27 18:26:58 +02001598 browser->show_headers = symbol_conf.show_hist_headers;
Namhyung Kimc2a51ab2015-04-22 16:18:15 +09001599 browser->hbt = hbt;
Namhyung Kimb1a9cee2015-04-22 16:18:17 +09001600 browser->env = env;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001601 }
1602
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001603 return browser;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001604}
1605
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001606static void hist_browser__delete(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001607{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001608 free(browser);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001609}
1610
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001611static struct hist_entry *hist_browser__selected_entry(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001612{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001613 return browser->he_selection;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001614}
1615
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001616static struct thread *hist_browser__selected_thread(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001617{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001618 return browser->he_selection->thread;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001619}
1620
Taeung Song1e378eb2014-10-07 16:13:15 +09001621/* Check whether the browser is for 'top' or 'report' */
1622static inline bool is_report_browser(void *timer)
1623{
1624 return timer == NULL;
1625}
1626
1627static int hists__browser_title(struct hists *hists,
1628 struct hist_browser_timer *hbt,
1629 char *bf, size_t size)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001630{
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -03001631 char unit;
1632 int printed;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001633 const struct dso *dso = hists->dso_filter;
1634 const struct thread *thread = hists->thread_filter;
Kan Liang84734b02015-09-04 10:45:45 -04001635 int socket_id = hists->socket_filter;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001636 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
1637 u64 nr_events = hists->stats.total_period;
Namhyung Kim717e2632013-01-22 18:09:44 +09001638 struct perf_evsel *evsel = hists_to_evsel(hists);
Jiri Olsadd00d482014-06-19 13:41:13 +02001639 const char *ev_name = perf_evsel__name(evsel);
Namhyung Kim717e2632013-01-22 18:09:44 +09001640 char buf[512];
1641 size_t buflen = sizeof(buf);
Kan Liang9e207dd2015-08-11 06:30:49 -04001642 char ref[30] = " show reference callgraph, ";
1643 bool enable_ref = false;
Namhyung Kim717e2632013-01-22 18:09:44 +09001644
Namhyung Kimf2148332014-01-14 11:52:48 +09001645 if (symbol_conf.filter_relative) {
1646 nr_samples = hists->stats.nr_non_filtered_samples;
1647 nr_events = hists->stats.total_non_filtered_period;
1648 }
1649
Namhyung Kim759ff492013-03-05 14:53:26 +09001650 if (perf_evsel__is_group_event(evsel)) {
Namhyung Kim717e2632013-01-22 18:09:44 +09001651 struct perf_evsel *pos;
1652
1653 perf_evsel__group_desc(evsel, buf, buflen);
1654 ev_name = buf;
1655
1656 for_each_group_member(pos, evsel) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001657 struct hists *pos_hists = evsel__hists(pos);
1658
Namhyung Kimf2148332014-01-14 11:52:48 +09001659 if (symbol_conf.filter_relative) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001660 nr_samples += pos_hists->stats.nr_non_filtered_samples;
1661 nr_events += pos_hists->stats.total_non_filtered_period;
Namhyung Kimf2148332014-01-14 11:52:48 +09001662 } else {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001663 nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
1664 nr_events += pos_hists->stats.total_period;
Namhyung Kimf2148332014-01-14 11:52:48 +09001665 }
Namhyung Kim717e2632013-01-22 18:09:44 +09001666 }
1667 }
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -03001668
Kan Liang9e207dd2015-08-11 06:30:49 -04001669 if (symbol_conf.show_ref_callgraph &&
1670 strstr(ev_name, "call-graph=no"))
1671 enable_ref = true;
Ashay Ranecc6862802012-04-05 21:01:01 -05001672 nr_samples = convert_unit(nr_samples, &unit);
1673 printed = scnprintf(bf, size,
Kan Liang9e207dd2015-08-11 06:30:49 -04001674 "Samples: %lu%c of event '%s',%sEvent count (approx.): %" PRIu64,
1675 nr_samples, unit, ev_name, enable_ref ? ref : " ", nr_events);
Ashay Ranecc6862802012-04-05 21:01:01 -05001676
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001677
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001678 if (hists->uid_filter_str)
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -02001679 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001680 ", UID: %s", hists->uid_filter_str);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001681 if (thread)
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -03001682 printed += scnprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -03001683 ", Thread: %s(%d)",
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +02001684 (thread->comm_set ? thread__comm_str(thread) : ""),
Adrian Hunter38051232013-07-04 16:20:31 +03001685 thread->tid);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001686 if (dso)
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -03001687 printed += scnprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -03001688 ", DSO: %s", dso->short_name);
Kan Liang84734b02015-09-04 10:45:45 -04001689 if (socket_id > -1)
Kan Liang21394d92015-09-04 10:45:44 -04001690 printed += scnprintf(bf + printed, size - printed,
Kan Liang84734b02015-09-04 10:45:45 -04001691 ", Processor Socket: %d", socket_id);
Taeung Song1e378eb2014-10-07 16:13:15 +09001692 if (!is_report_browser(hbt)) {
1693 struct perf_top *top = hbt->arg;
1694
1695 if (top->zero)
1696 printed += scnprintf(bf + printed, size - printed, " [z]");
1697 }
1698
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -03001699 return printed;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001700}
1701
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001702static inline void free_popup_options(char **options, int n)
1703{
1704 int i;
1705
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001706 for (i = 0; i < n; ++i)
1707 zfree(&options[i]);
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001708}
1709
Feng Tang341487ab2013-02-03 14:38:20 +08001710/*
1711 * Only runtime switching of perf data file will make "input_name" point
1712 * to a malloced buffer. So add "is_input_name_malloced" flag to decide
1713 * whether we need to call free() for current "input_name" during the switch.
1714 */
1715static bool is_input_name_malloced = false;
1716
1717static int switch_data_file(void)
1718{
1719 char *pwd, *options[32], *abs_path[32], *tmp;
1720 DIR *pwd_dir;
1721 int nr_options = 0, choice = -1, ret = -1;
1722 struct dirent *dent;
1723
1724 pwd = getenv("PWD");
1725 if (!pwd)
1726 return ret;
1727
1728 pwd_dir = opendir(pwd);
1729 if (!pwd_dir)
1730 return ret;
1731
1732 memset(options, 0, sizeof(options));
1733 memset(options, 0, sizeof(abs_path));
1734
1735 while ((dent = readdir(pwd_dir))) {
1736 char path[PATH_MAX];
1737 u64 magic;
1738 char *name = dent->d_name;
1739 FILE *file;
1740
1741 if (!(dent->d_type == DT_REG))
1742 continue;
1743
1744 snprintf(path, sizeof(path), "%s/%s", pwd, name);
1745
1746 file = fopen(path, "r");
1747 if (!file)
1748 continue;
1749
1750 if (fread(&magic, 1, 8, file) < 8)
1751 goto close_file_and_continue;
1752
1753 if (is_perf_magic(magic)) {
1754 options[nr_options] = strdup(name);
1755 if (!options[nr_options])
1756 goto close_file_and_continue;
1757
1758 abs_path[nr_options] = strdup(path);
1759 if (!abs_path[nr_options]) {
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -03001760 zfree(&options[nr_options]);
Feng Tang341487ab2013-02-03 14:38:20 +08001761 ui__warning("Can't search all data files due to memory shortage.\n");
1762 fclose(file);
1763 break;
1764 }
1765
1766 nr_options++;
1767 }
1768
1769close_file_and_continue:
1770 fclose(file);
1771 if (nr_options >= 32) {
1772 ui__warning("Too many perf data files in PWD!\n"
1773 "Only the first 32 files will be listed.\n");
1774 break;
1775 }
1776 }
1777 closedir(pwd_dir);
1778
1779 if (nr_options) {
1780 choice = ui__popup_menu(nr_options, options);
1781 if (choice < nr_options && choice >= 0) {
1782 tmp = strdup(abs_path[choice]);
1783 if (tmp) {
1784 if (is_input_name_malloced)
1785 free((void *)input_name);
1786 input_name = tmp;
1787 is_input_name_malloced = true;
1788 ret = 0;
1789 } else
1790 ui__warning("Data switch failed due to memory shortage!\n");
1791 }
1792 }
1793
1794 free_popup_options(options, nr_options);
1795 free_popup_options(abs_path, nr_options);
1796 return ret;
1797}
1798
Namhyung Kimea7cd592015-04-22 16:18:19 +09001799struct popup_action {
1800 struct thread *thread;
Namhyung Kimea7cd592015-04-22 16:18:19 +09001801 struct map_symbol ms;
Kan Liang84734b02015-09-04 10:45:45 -04001802 int socket;
Namhyung Kimea7cd592015-04-22 16:18:19 +09001803
1804 int (*fn)(struct hist_browser *browser, struct popup_action *act);
1805};
1806
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001807static int
Namhyung Kimea7cd592015-04-22 16:18:19 +09001808do_annotate(struct hist_browser *browser, struct popup_action *act)
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001809{
1810 struct perf_evsel *evsel;
1811 struct annotation *notes;
1812 struct hist_entry *he;
1813 int err;
1814
Arnaldo Carvalho de Meloeebd0bf2015-09-08 15:52:20 -03001815 if (!objdump_path && perf_env__lookup_objdump(browser->env))
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001816 return 0;
1817
Namhyung Kimea7cd592015-04-22 16:18:19 +09001818 notes = symbol__annotation(act->ms.sym);
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001819 if (!notes->src)
1820 return 0;
1821
1822 evsel = hists_to_evsel(browser->hists);
Namhyung Kimea7cd592015-04-22 16:18:19 +09001823 err = map_symbol__tui_annotate(&act->ms, evsel, browser->hbt);
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001824 he = hist_browser__selected_entry(browser);
1825 /*
1826 * offer option to annotate the other branch source or target
1827 * (if they exists) when returning from annotate
1828 */
1829 if ((err == 'q' || err == CTRL('c')) && he->branch_info)
1830 return 1;
1831
1832 ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
1833 if (err)
1834 ui_browser__handle_resize(&browser->b);
1835 return 0;
1836}
1837
1838static int
Namhyung Kimea7cd592015-04-22 16:18:19 +09001839add_annotate_opt(struct hist_browser *browser __maybe_unused,
1840 struct popup_action *act, char **optstr,
1841 struct map *map, struct symbol *sym)
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001842{
Namhyung Kimea7cd592015-04-22 16:18:19 +09001843 if (sym == NULL || map->dso->annotate_warned)
1844 return 0;
1845
1846 if (asprintf(optstr, "Annotate %s", sym->name) < 0)
1847 return 0;
1848
1849 act->ms.map = map;
1850 act->ms.sym = sym;
1851 act->fn = do_annotate;
1852 return 1;
1853}
1854
1855static int
1856do_zoom_thread(struct hist_browser *browser, struct popup_action *act)
1857{
1858 struct thread *thread = act->thread;
1859
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001860 if (browser->hists->thread_filter) {
1861 pstack__remove(browser->pstack, &browser->hists->thread_filter);
1862 perf_hpp__set_elide(HISTC_THREAD, false);
1863 thread__zput(browser->hists->thread_filter);
1864 ui_helpline__pop();
1865 } else {
Arnaldo Carvalho de Melo7727a922015-10-12 13:56:50 -03001866 ui_helpline__fpush("To zoom out press ESC or ENTER + \"Zoom out of %s(%d) thread\"",
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001867 thread->comm_set ? thread__comm_str(thread) : "",
1868 thread->tid);
1869 browser->hists->thread_filter = thread__get(thread);
1870 perf_hpp__set_elide(HISTC_THREAD, false);
1871 pstack__push(browser->pstack, &browser->hists->thread_filter);
1872 }
1873
1874 hists__filter_by_thread(browser->hists);
1875 hist_browser__reset(browser);
1876 return 0;
1877}
1878
1879static int
Namhyung Kimea7cd592015-04-22 16:18:19 +09001880add_thread_opt(struct hist_browser *browser, struct popup_action *act,
1881 char **optstr, struct thread *thread)
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001882{
Namhyung Kim2eafd412016-01-21 19:13:24 -03001883 if (!sort__has_thread || thread == NULL)
Namhyung Kimea7cd592015-04-22 16:18:19 +09001884 return 0;
1885
1886 if (asprintf(optstr, "Zoom %s %s(%d) thread",
1887 browser->hists->thread_filter ? "out of" : "into",
1888 thread->comm_set ? thread__comm_str(thread) : "",
1889 thread->tid) < 0)
1890 return 0;
1891
1892 act->thread = thread;
1893 act->fn = do_zoom_thread;
1894 return 1;
1895}
1896
1897static int
1898do_zoom_dso(struct hist_browser *browser, struct popup_action *act)
1899{
Arnaldo Carvalho de Melo045b80d2015-09-23 15:38:55 -03001900 struct map *map = act->ms.map;
Namhyung Kimea7cd592015-04-22 16:18:19 +09001901
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001902 if (browser->hists->dso_filter) {
1903 pstack__remove(browser->pstack, &browser->hists->dso_filter);
1904 perf_hpp__set_elide(HISTC_DSO, false);
1905 browser->hists->dso_filter = NULL;
1906 ui_helpline__pop();
1907 } else {
Arnaldo Carvalho de Melo045b80d2015-09-23 15:38:55 -03001908 if (map == NULL)
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001909 return 0;
Arnaldo Carvalho de Melo7727a922015-10-12 13:56:50 -03001910 ui_helpline__fpush("To zoom out press ESC or ENTER + \"Zoom out of %s DSO\"",
Arnaldo Carvalho de Melo045b80d2015-09-23 15:38:55 -03001911 __map__is_kernel(map) ? "the Kernel" : map->dso->short_name);
1912 browser->hists->dso_filter = map->dso;
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001913 perf_hpp__set_elide(HISTC_DSO, true);
1914 pstack__push(browser->pstack, &browser->hists->dso_filter);
1915 }
1916
1917 hists__filter_by_dso(browser->hists);
1918 hist_browser__reset(browser);
1919 return 0;
1920}
1921
1922static int
Namhyung Kimea7cd592015-04-22 16:18:19 +09001923add_dso_opt(struct hist_browser *browser, struct popup_action *act,
Arnaldo Carvalho de Melo045b80d2015-09-23 15:38:55 -03001924 char **optstr, struct map *map)
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001925{
Namhyung Kimb1447a542016-01-22 11:22:41 -03001926 if (!sort__has_dso || map == NULL)
Namhyung Kimea7cd592015-04-22 16:18:19 +09001927 return 0;
1928
1929 if (asprintf(optstr, "Zoom %s %s DSO",
1930 browser->hists->dso_filter ? "out of" : "into",
Arnaldo Carvalho de Melo045b80d2015-09-23 15:38:55 -03001931 __map__is_kernel(map) ? "the Kernel" : map->dso->short_name) < 0)
Namhyung Kimea7cd592015-04-22 16:18:19 +09001932 return 0;
1933
Arnaldo Carvalho de Melo045b80d2015-09-23 15:38:55 -03001934 act->ms.map = map;
Namhyung Kimea7cd592015-04-22 16:18:19 +09001935 act->fn = do_zoom_dso;
1936 return 1;
1937}
1938
1939static int
1940do_browse_map(struct hist_browser *browser __maybe_unused,
1941 struct popup_action *act)
1942{
1943 map__browse(act->ms.map);
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001944 return 0;
1945}
1946
1947static int
Namhyung Kimea7cd592015-04-22 16:18:19 +09001948add_map_opt(struct hist_browser *browser __maybe_unused,
1949 struct popup_action *act, char **optstr, struct map *map)
1950{
Namhyung Kimb1447a542016-01-22 11:22:41 -03001951 if (!sort__has_dso || map == NULL)
Namhyung Kimea7cd592015-04-22 16:18:19 +09001952 return 0;
1953
1954 if (asprintf(optstr, "Browse map details") < 0)
1955 return 0;
1956
1957 act->ms.map = map;
1958 act->fn = do_browse_map;
1959 return 1;
1960}
1961
1962static int
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001963do_run_script(struct hist_browser *browser __maybe_unused,
Namhyung Kimea7cd592015-04-22 16:18:19 +09001964 struct popup_action *act)
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001965{
1966 char script_opt[64];
1967 memset(script_opt, 0, sizeof(script_opt));
1968
Namhyung Kimea7cd592015-04-22 16:18:19 +09001969 if (act->thread) {
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001970 scnprintf(script_opt, sizeof(script_opt), " -c %s ",
Namhyung Kimea7cd592015-04-22 16:18:19 +09001971 thread__comm_str(act->thread));
1972 } else if (act->ms.sym) {
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001973 scnprintf(script_opt, sizeof(script_opt), " -S %s ",
Namhyung Kimea7cd592015-04-22 16:18:19 +09001974 act->ms.sym->name);
Namhyung Kimbc7cad42015-04-22 16:18:18 +09001975 }
1976
1977 script_browse(script_opt);
1978 return 0;
1979}
1980
1981static int
Namhyung Kimea7cd592015-04-22 16:18:19 +09001982add_script_opt(struct hist_browser *browser __maybe_unused,
1983 struct popup_action *act, char **optstr,
1984 struct thread *thread, struct symbol *sym)
1985{
1986 if (thread) {
1987 if (asprintf(optstr, "Run scripts for samples of thread [%s]",
1988 thread__comm_str(thread)) < 0)
1989 return 0;
1990 } else if (sym) {
1991 if (asprintf(optstr, "Run scripts for samples of symbol [%s]",
1992 sym->name) < 0)
1993 return 0;
1994 } else {
1995 if (asprintf(optstr, "Run scripts for all samples") < 0)
1996 return 0;
1997 }
1998
1999 act->thread = thread;
2000 act->ms.sym = sym;
2001 act->fn = do_run_script;
2002 return 1;
2003}
2004
2005static int
2006do_switch_data(struct hist_browser *browser __maybe_unused,
2007 struct popup_action *act __maybe_unused)
Namhyung Kimbc7cad42015-04-22 16:18:18 +09002008{
2009 if (switch_data_file()) {
2010 ui__warning("Won't switch the data files due to\n"
2011 "no valid data file get selected!\n");
Namhyung Kimea7cd592015-04-22 16:18:19 +09002012 return 0;
Namhyung Kimbc7cad42015-04-22 16:18:18 +09002013 }
2014
2015 return K_SWITCH_INPUT_DATA;
2016}
2017
Namhyung Kimea7cd592015-04-22 16:18:19 +09002018static int
2019add_switch_opt(struct hist_browser *browser,
2020 struct popup_action *act, char **optstr)
2021{
2022 if (!is_report_browser(browser->hbt))
2023 return 0;
2024
2025 if (asprintf(optstr, "Switch to another data file in PWD") < 0)
2026 return 0;
2027
2028 act->fn = do_switch_data;
2029 return 1;
2030}
2031
2032static int
2033do_exit_browser(struct hist_browser *browser __maybe_unused,
2034 struct popup_action *act __maybe_unused)
2035{
2036 return 0;
2037}
2038
2039static int
2040add_exit_opt(struct hist_browser *browser __maybe_unused,
2041 struct popup_action *act, char **optstr)
2042{
2043 if (asprintf(optstr, "Exit") < 0)
2044 return 0;
2045
2046 act->fn = do_exit_browser;
2047 return 1;
2048}
2049
Kan Liang84734b02015-09-04 10:45:45 -04002050static int
2051do_zoom_socket(struct hist_browser *browser, struct popup_action *act)
2052{
2053 if (browser->hists->socket_filter > -1) {
2054 pstack__remove(browser->pstack, &browser->hists->socket_filter);
2055 browser->hists->socket_filter = -1;
2056 perf_hpp__set_elide(HISTC_SOCKET, false);
2057 } else {
2058 browser->hists->socket_filter = act->socket;
2059 perf_hpp__set_elide(HISTC_SOCKET, true);
2060 pstack__push(browser->pstack, &browser->hists->socket_filter);
2061 }
2062
2063 hists__filter_by_socket(browser->hists);
2064 hist_browser__reset(browser);
2065 return 0;
2066}
2067
2068static int
2069add_socket_opt(struct hist_browser *browser, struct popup_action *act,
2070 char **optstr, int socket_id)
2071{
Namhyung Kimd9695d92016-01-22 12:20:18 -03002072 if (!sort__has_socket || socket_id < 0)
Kan Liang84734b02015-09-04 10:45:45 -04002073 return 0;
2074
2075 if (asprintf(optstr, "Zoom %s Processor Socket %d",
2076 (browser->hists->socket_filter > -1) ? "out of" : "into",
2077 socket_id) < 0)
2078 return 0;
2079
2080 act->socket = socket_id;
2081 act->fn = do_zoom_socket;
2082 return 1;
2083}
2084
Namhyung Kim112f7612014-04-22 14:05:35 +09002085static void hist_browser__update_nr_entries(struct hist_browser *hb)
Namhyung Kim064f1982013-05-14 11:09:04 +09002086{
2087 u64 nr_entries = 0;
2088 struct rb_node *nd = rb_first(&hb->hists->entries);
2089
Namhyung Kimf5b763f2016-02-25 00:13:43 +09002090 if (hb->min_pcnt == 0 && !symbol_conf.report_hierarchy) {
Namhyung Kim268397c2014-04-22 14:49:31 +09002091 hb->nr_non_filtered_entries = hb->hists->nr_non_filtered_entries;
2092 return;
2093 }
2094
Namhyung Kim14135662013-10-31 10:17:39 +09002095 while ((nd = hists__filter_entries(nd, hb->min_pcnt)) != NULL) {
Namhyung Kim064f1982013-05-14 11:09:04 +09002096 nr_entries++;
Namhyung Kimf5b763f2016-02-25 00:13:43 +09002097 nd = rb_hierarchy_next(nd);
Namhyung Kim064f1982013-05-14 11:09:04 +09002098 }
2099
Namhyung Kim112f7612014-04-22 14:05:35 +09002100 hb->nr_non_filtered_entries = nr_entries;
Namhyung Kimf5b763f2016-02-25 00:13:43 +09002101 hb->nr_hierarchy_entries = nr_entries;
Namhyung Kim064f1982013-05-14 11:09:04 +09002102}
Feng Tang341487ab2013-02-03 14:38:20 +08002103
Namhyung Kimb62e8df2016-02-03 23:11:23 +09002104static void hist_browser__update_percent_limit(struct hist_browser *hb,
2105 double percent)
2106{
2107 struct hist_entry *he;
2108 struct rb_node *nd = rb_first(&hb->hists->entries);
2109 u64 total = hists__total_period(hb->hists);
2110 u64 min_callchain_hits = total * (percent / 100);
2111
2112 hb->min_pcnt = callchain_param.min_percent = percent;
2113
2114 if (!symbol_conf.use_callchain)
2115 return;
2116
2117 while ((nd = hists__filter_entries(nd, hb->min_pcnt)) != NULL) {
2118 he = rb_entry(nd, struct hist_entry, rb_node);
2119
2120 if (callchain_param.mode == CHAIN_GRAPH_REL) {
2121 total = he->stat.period;
2122
2123 if (symbol_conf.cumulate_callchain)
2124 total = he->stat_acc->period;
2125
2126 min_callchain_hits = total * (percent / 100);
2127 }
2128
2129 callchain_param.sort(&he->sorted_chain, he->callchain,
2130 min_callchain_hits, &callchain_param);
2131
2132 /* force to re-evaluate folding state of callchains */
2133 he->init_have_children = false;
2134 hist_entry__set_folding(he, false);
2135
2136 nd = rb_next(nd);
2137 }
2138}
2139
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03002140static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
Jiri Olsadd00d482014-06-19 13:41:13 +02002141 const char *helpline,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03002142 bool left_exits,
Namhyung Kim68d80752012-11-02 14:50:06 +09002143 struct hist_browser_timer *hbt,
Namhyung Kim064f1982013-05-14 11:09:04 +09002144 float min_pcnt,
Kan Liangce80d3b2015-08-28 05:48:04 -04002145 struct perf_env *env)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002146{
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03002147 struct hists *hists = evsel__hists(evsel);
Namhyung Kimb1a9cee2015-04-22 16:18:17 +09002148 struct hist_browser *browser = hist_browser__new(hists, hbt, env);
Stephane Eraniana68c2c52012-03-08 23:47:48 +01002149 struct branch_info *bi;
Namhyung Kimf2b487d2015-04-22 16:18:14 +09002150#define MAX_OPTIONS 16
2151 char *options[MAX_OPTIONS];
Namhyung Kimea7cd592015-04-22 16:18:19 +09002152 struct popup_action actions[MAX_OPTIONS];
Stephane Eranian24bff2d2012-03-12 16:13:30 +01002153 int nr_options = 0;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002154 int key = -1;
Namhyung Kim938a23a2012-03-16 17:50:53 +09002155 char buf[64];
Namhyung Kim9783adf2012-11-02 14:50:05 +09002156 int delay_secs = hbt ? hbt->refresh : 0;
Namhyung Kim59dc9f22014-07-31 14:47:41 +09002157 struct perf_hpp_fmt *fmt;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002158
Namhyung Kime8e684a2013-12-26 14:37:58 +09002159#define HIST_BROWSER_HELP_COMMON \
2160 "h/?/F1 Show this window\n" \
2161 "UP/DOWN/PGUP\n" \
2162 "PGDN/SPACE Navigate\n" \
2163 "q/ESC/CTRL+C Exit browser\n\n" \
2164 "For multiple event sessions:\n\n" \
2165 "TAB/UNTAB Switch events\n\n" \
2166 "For symbolic views (--sort has sym):\n\n" \
Arnaldo Carvalho de Melo7727a922015-10-12 13:56:50 -03002167 "ENTER Zoom into DSO/Threads & Annotate current symbol\n" \
2168 "ESC Zoom out\n" \
Namhyung Kime8e684a2013-12-26 14:37:58 +09002169 "a Annotate current symbol\n" \
2170 "C Collapse all callchains\n" \
2171 "d Zoom into current DSO\n" \
2172 "E Expand all callchains\n" \
Namhyung Kim105eb302014-02-10 11:20:10 +09002173 "F Toggle percentage of filtered entries\n" \
Arnaldo Carvalho de Melo025bf7e2014-06-14 15:44:52 +02002174 "H Display column headers\n" \
Namhyung Kimb62e8df2016-02-03 23:11:23 +09002175 "L Change percent limit\n" \
Namhyung Kim31eb4362015-10-13 09:02:01 +09002176 "m Display context menu\n" \
Kan Liang84734b02015-09-04 10:45:45 -04002177 "S Zoom into current Processor Socket\n" \
Namhyung Kime8e684a2013-12-26 14:37:58 +09002178
2179 /* help messages are sorted by lexical order of the hotkey */
2180 const char report_help[] = HIST_BROWSER_HELP_COMMON
Namhyung Kim6dd60132013-12-26 14:37:59 +09002181 "i Show header information\n"
Namhyung Kime8e684a2013-12-26 14:37:58 +09002182 "P Print histograms to perf.hist.N\n"
2183 "r Run available scripts\n"
2184 "s Switch to another data file in PWD\n"
2185 "t Zoom into current Thread\n"
2186 "V Verbose (DSO names in callchains, etc)\n"
2187 "/ Filter symbol by name";
2188 const char top_help[] = HIST_BROWSER_HELP_COMMON
2189 "P Print histograms to perf.hist.N\n"
2190 "t Zoom into current Thread\n"
2191 "V Verbose (DSO names in callchains, etc)\n"
Namhyung Kim42337a22014-08-12 17:16:06 +09002192 "z Toggle zeroing of samples\n"
Arnaldo Carvalho de Melofbb79972015-06-19 16:56:04 -03002193 "f Enable/Disable events\n"
Namhyung Kime8e684a2013-12-26 14:37:58 +09002194 "/ Filter symbol by name";
2195
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002196 if (browser == NULL)
2197 return -1;
2198
Namhyung Kimed426912015-05-29 21:53:44 +09002199 /* reset abort key so that it can get Ctrl-C as a key */
2200 SLang_reset_tty();
2201 SLang_init_tty(0, 0, 0);
2202
Namhyung Kim03905042015-11-28 02:32:39 +09002203 if (min_pcnt)
Namhyung Kim064f1982013-05-14 11:09:04 +09002204 browser->min_pcnt = min_pcnt;
Namhyung Kim03905042015-11-28 02:32:39 +09002205 hist_browser__update_nr_entries(browser);
Namhyung Kim064f1982013-05-14 11:09:04 +09002206
Kan Liang84734b02015-09-04 10:45:45 -04002207 browser->pstack = pstack__new(3);
Namhyung Kim01f00a12015-04-22 16:18:16 +09002208 if (browser->pstack == NULL)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002209 goto out;
2210
2211 ui_helpline__push(helpline);
2212
Stephane Eranian24bff2d2012-03-12 16:13:30 +01002213 memset(options, 0, sizeof(options));
Namhyung Kimea7cd592015-04-22 16:18:19 +09002214 memset(actions, 0, sizeof(actions));
Stephane Eranian24bff2d2012-03-12 16:13:30 +01002215
Jiri Olsaf0786af2016-01-18 10:24:23 +01002216 hists__for_each_format(browser->hists, fmt) {
Namhyung Kim59dc9f22014-07-31 14:47:41 +09002217 perf_hpp__reset_width(fmt, hists);
Arnaldo Carvalho de Meloc6c3c022015-08-11 17:22:43 -03002218 /*
2219 * This is done just once, and activates the horizontal scrolling
2220 * code in the ui_browser code, it would be better to have a the
2221 * counter in the perf_hpp code, but I couldn't find doing it here
2222 * works, FIXME by setting this in hist_browser__new, for now, be
2223 * clever 8-)
2224 */
2225 ++browser->b.columns;
2226 }
Namhyung Kim59dc9f22014-07-31 14:47:41 +09002227
Namhyung Kim5b591662014-07-31 14:47:38 +09002228 if (symbol_conf.col_width_list_str)
2229 perf_hpp__set_user_width(symbol_conf.col_width_list_str);
2230
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002231 while (1) {
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03002232 struct thread *thread = NULL;
Arnaldo Carvalho de Melo045b80d2015-09-23 15:38:55 -03002233 struct map *map = NULL;
Namhyung Kimea7cd592015-04-22 16:18:19 +09002234 int choice = 0;
Kan Liang84734b02015-09-04 10:45:45 -04002235 int socked_id = -1;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002236
Stephane Eranian24bff2d2012-03-12 16:13:30 +01002237 nr_options = 0;
2238
Arnaldo Carvalho de Melo5f00b0f2015-06-19 17:30:20 -03002239 key = hist_browser__run(browser, helpline);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002240
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -03002241 if (browser->he_selection != NULL) {
2242 thread = hist_browser__selected_thread(browser);
Arnaldo Carvalho de Melo045b80d2015-09-23 15:38:55 -03002243 map = browser->selection->map;
Kan Liang84734b02015-09-04 10:45:45 -04002244 socked_id = browser->he_selection->socket;
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -03002245 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03002246 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02002247 case K_TAB:
2248 case K_UNTAB:
David Aherne4419b82011-10-19 11:37:47 -06002249 if (nr_events == 1)
2250 continue;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03002251 /*
2252 * Exit the browser, let hists__browser_tree
2253 * go to the next or previous
2254 */
2255 goto out_free_stack;
2256 case 'a':
Arnaldo Carvalho de Melo9c796ec2013-04-26 14:28:46 -03002257 if (!sort__has_sym) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02002258 ui_browser__warning(&browser->b, delay_secs * 2,
Arnaldo Carvalho de Meloa6e51f92011-10-21 10:58:24 -02002259 "Annotation is only available for symbolic views, "
Stephane Eraniana68c2c52012-03-08 23:47:48 +01002260 "include \"sym*\" in --sort to use it.");
Arnaldo Carvalho de Meloa6e51f92011-10-21 10:58:24 -02002261 continue;
2262 }
2263
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -03002264 if (browser->selection == NULL ||
Lin Mingdb9a9cbc2011-04-08 14:31:26 +08002265 browser->selection->sym == NULL ||
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03002266 browser->selection->map->dso->annotate_warned)
2267 continue;
Namhyung Kimbc7cad42015-04-22 16:18:18 +09002268
Namhyung Kimea7cd592015-04-22 16:18:19 +09002269 actions->ms.map = browser->selection->map;
2270 actions->ms.sym = browser->selection->sym;
2271 do_annotate(browser, actions);
Namhyung Kimbc7cad42015-04-22 16:18:18 +09002272 continue;
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03002273 case 'P':
2274 hist_browser__dump(browser);
2275 continue;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03002276 case 'd':
Arnaldo Carvalho de Melofae00652015-11-12 15:59:26 -03002277 actions->ms.map = map;
Namhyung Kimea7cd592015-04-22 16:18:19 +09002278 do_zoom_dso(browser, actions);
Namhyung Kimbc7cad42015-04-22 16:18:18 +09002279 continue;
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -03002280 case 'V':
2281 browser->show_dso = !browser->show_dso;
2282 continue;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03002283 case 't':
Namhyung Kimea7cd592015-04-22 16:18:19 +09002284 actions->thread = thread;
2285 do_zoom_thread(browser, actions);
Namhyung Kimbc7cad42015-04-22 16:18:18 +09002286 continue;
Kan Liang84734b02015-09-04 10:45:45 -04002287 case 'S':
2288 actions->socket = socked_id;
2289 do_zoom_socket(browser, actions);
2290 continue;
Arnaldo Carvalho de Melo5a5626b2012-05-09 12:21:22 -03002291 case '/':
Namhyung Kim938a23a2012-03-16 17:50:53 +09002292 if (ui_browser__input_window("Symbol to show",
Arnaldo Carvalho de Melo4aa8e4542015-10-12 14:02:29 -03002293 "Please enter the name of symbol you want to see.\n"
2294 "To remove the filter later, press / + ENTER.",
Namhyung Kim938a23a2012-03-16 17:50:53 +09002295 buf, "ENTER: OK, ESC: Cancel",
2296 delay_secs * 2) == K_ENTER) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03002297 hists->symbol_filter_str = *buf ? buf : NULL;
2298 hists__filter_by_symbol(hists);
Namhyung Kim938a23a2012-03-16 17:50:53 +09002299 hist_browser__reset(browser);
2300 }
2301 continue;
Feng Tangcdbab7c2012-10-30 11:56:06 +08002302 case 'r':
Namhyung Kimea7cd592015-04-22 16:18:19 +09002303 if (is_report_browser(hbt)) {
2304 actions->thread = NULL;
2305 actions->ms.sym = NULL;
2306 do_run_script(browser, actions);
2307 }
Feng Tangc77d8d72012-11-01 00:00:55 +08002308 continue;
Feng Tang341487ab2013-02-03 14:38:20 +08002309 case 's':
Namhyung Kimbc7cad42015-04-22 16:18:18 +09002310 if (is_report_browser(hbt)) {
Namhyung Kimea7cd592015-04-22 16:18:19 +09002311 key = do_switch_data(browser, actions);
Namhyung Kimbc7cad42015-04-22 16:18:18 +09002312 if (key == K_SWITCH_INPUT_DATA)
2313 goto out_free_stack;
2314 }
Feng Tang341487ab2013-02-03 14:38:20 +08002315 continue;
Namhyung Kim6dd60132013-12-26 14:37:59 +09002316 case 'i':
2317 /* env->arch is NULL for live-mode (i.e. perf top) */
2318 if (env->arch)
2319 tui__header_window(env);
2320 continue;
Namhyung Kim105eb302014-02-10 11:20:10 +09002321 case 'F':
2322 symbol_conf.filter_relative ^= 1;
2323 continue;
Namhyung Kim42337a22014-08-12 17:16:06 +09002324 case 'z':
2325 if (!is_report_browser(hbt)) {
2326 struct perf_top *top = hbt->arg;
2327
2328 top->zero = !top->zero;
2329 }
2330 continue;
Namhyung Kimb62e8df2016-02-03 23:11:23 +09002331 case 'L':
2332 if (ui_browser__input_window("Percent Limit",
2333 "Please enter the value you want to hide entries under that percent.",
2334 buf, "ENTER: OK, ESC: Cancel",
2335 delay_secs * 2) == K_ENTER) {
2336 char *end;
2337 double new_percent = strtod(buf, &end);
2338
2339 if (new_percent < 0 || new_percent > 100) {
2340 ui_browser__warning(&browser->b, delay_secs * 2,
2341 "Invalid percent: %.2f", new_percent);
2342 continue;
2343 }
2344
2345 hist_browser__update_percent_limit(browser, new_percent);
2346 hist_browser__reset(browser);
2347 }
2348 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02002349 case K_F1:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03002350 case 'h':
2351 case '?':
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02002352 ui_browser__help_window(&browser->b,
Namhyung Kime8e684a2013-12-26 14:37:58 +09002353 is_report_browser(hbt) ? report_help : top_help);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03002354 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02002355 case K_ENTER:
2356 case K_RIGHT:
Namhyung Kim31eb4362015-10-13 09:02:01 +09002357 case 'm':
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03002358 /* menu */
2359 break;
Arnaldo Carvalho de Melo63ab1742015-08-12 12:42:58 -03002360 case K_ESC:
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02002361 case K_LEFT: {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03002362 const void *top;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002363
Namhyung Kim01f00a12015-04-22 16:18:16 +09002364 if (pstack__empty(browser->pstack)) {
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002365 /*
2366 * Go back to the perf_evsel_menu__run or other user
2367 */
2368 if (left_exits)
2369 goto out_free_stack;
Arnaldo Carvalho de Melo63ab1742015-08-12 12:42:58 -03002370
2371 if (key == K_ESC &&
2372 ui_browser__dialog_yesno(&browser->b,
2373 "Do you really want to exit?"))
2374 goto out_free_stack;
2375
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002376 continue;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002377 }
Namhyung Kim64221842015-04-24 10:15:33 +09002378 top = pstack__peek(browser->pstack);
Namhyung Kimbc7cad42015-04-22 16:18:18 +09002379 if (top == &browser->hists->dso_filter) {
Namhyung Kim64221842015-04-24 10:15:33 +09002380 /*
2381 * No need to set actions->dso here since
2382 * it's just to remove the current filter.
2383 * Ditto for thread below.
2384 */
2385 do_zoom_dso(browser, actions);
Kan Liang84734b02015-09-04 10:45:45 -04002386 } else if (top == &browser->hists->thread_filter) {
Namhyung Kim64221842015-04-24 10:15:33 +09002387 do_zoom_thread(browser, actions);
Kan Liang84734b02015-09-04 10:45:45 -04002388 } else if (top == &browser->hists->socket_filter) {
2389 do_zoom_socket(browser, actions);
2390 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03002391 continue;
2392 }
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -03002393 case 'q':
2394 case CTRL('c'):
Arnaldo Carvalho de Melo516e5362015-06-19 16:59:43 -03002395 goto out_free_stack;
Arnaldo Carvalho de Melofbb79972015-06-19 16:56:04 -03002396 case 'f':
Namhyung Kim13d1e532015-06-21 12:41:16 +09002397 if (!is_report_browser(hbt)) {
2398 struct perf_top *top = hbt->arg;
2399
2400 perf_evlist__toggle_enable(top->evlist);
2401 /*
2402 * No need to refresh, resort/decay histogram
2403 * entries if we are not collecting samples:
2404 */
2405 if (top->evlist->enabled) {
2406 helpline = "Press 'f' to disable the events or 'h' to see other hotkeys";
2407 hbt->refresh = delay_secs;
2408 } else {
2409 helpline = "Press 'f' again to re-enable the events";
2410 hbt->refresh = 0;
2411 }
2412 continue;
2413 }
Arnaldo Carvalho de Melo3e323dc2015-06-19 17:49:29 -03002414 /* Fall thru */
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -03002415 default:
Arnaldo Carvalho de Melo3e323dc2015-06-19 17:49:29 -03002416 helpline = "Press '?' for help on key bindings";
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -03002417 continue;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002418 }
2419
Namhyung Kim40561322016-01-22 12:26:06 -03002420 if (!sort__has_sym || browser->selection == NULL)
Arnaldo Carvalho de Melo0ba332f2015-03-16 17:41:52 -03002421 goto skip_annotation;
2422
Namhyung Kim55369fc2013-04-01 20:35:20 +09002423 if (sort__mode == SORT_MODE__BRANCH) {
Stephane Eraniana68c2c52012-03-08 23:47:48 +01002424 bi = browser->he_selection->branch_info;
Arnaldo Carvalho de Melo0ba332f2015-03-16 17:41:52 -03002425
2426 if (bi == NULL)
2427 goto skip_annotation;
2428
Namhyung Kimea7cd592015-04-22 16:18:19 +09002429 nr_options += add_annotate_opt(browser,
2430 &actions[nr_options],
2431 &options[nr_options],
2432 bi->from.map,
2433 bi->from.sym);
2434 if (bi->to.sym != bi->from.sym)
2435 nr_options += add_annotate_opt(browser,
2436 &actions[nr_options],
2437 &options[nr_options],
2438 bi->to.map,
2439 bi->to.sym);
Stephane Eraniana68c2c52012-03-08 23:47:48 +01002440 } else {
Namhyung Kimea7cd592015-04-22 16:18:19 +09002441 nr_options += add_annotate_opt(browser,
2442 &actions[nr_options],
2443 &options[nr_options],
2444 browser->selection->map,
2445 browser->selection->sym);
Stephane Eraniana68c2c52012-03-08 23:47:48 +01002446 }
Arnaldo Carvalho de Melo0ba332f2015-03-16 17:41:52 -03002447skip_annotation:
Namhyung Kimea7cd592015-04-22 16:18:19 +09002448 nr_options += add_thread_opt(browser, &actions[nr_options],
2449 &options[nr_options], thread);
2450 nr_options += add_dso_opt(browser, &actions[nr_options],
Arnaldo Carvalho de Melo045b80d2015-09-23 15:38:55 -03002451 &options[nr_options], map);
Namhyung Kimea7cd592015-04-22 16:18:19 +09002452 nr_options += add_map_opt(browser, &actions[nr_options],
2453 &options[nr_options],
Wang Nanbd315aa2015-09-14 10:23:55 +00002454 browser->selection ?
2455 browser->selection->map : NULL);
Kan Liang84734b02015-09-04 10:45:45 -04002456 nr_options += add_socket_opt(browser, &actions[nr_options],
2457 &options[nr_options],
2458 socked_id);
Feng Tangcdbab7c2012-10-30 11:56:06 +08002459 /* perf script support */
Namhyung Kimb1baae82016-01-26 15:37:30 -03002460 if (!is_report_browser(hbt))
2461 goto skip_scripting;
2462
Feng Tangcdbab7c2012-10-30 11:56:06 +08002463 if (browser->he_selection) {
Namhyung Kim2eafd412016-01-21 19:13:24 -03002464 if (sort__has_thread && thread) {
2465 nr_options += add_script_opt(browser,
2466 &actions[nr_options],
2467 &options[nr_options],
2468 thread, NULL);
2469 }
Wang Nanbd315aa2015-09-14 10:23:55 +00002470 /*
2471 * Note that browser->selection != NULL
2472 * when browser->he_selection is not NULL,
2473 * so we don't need to check browser->selection
2474 * before fetching browser->selection->sym like what
2475 * we do before fetching browser->selection->map.
2476 *
2477 * See hist_browser__show_entry.
2478 */
Namhyung Kimc221acb2016-01-21 19:50:09 -03002479 if (sort__has_sym && browser->selection->sym) {
2480 nr_options += add_script_opt(browser,
2481 &actions[nr_options],
2482 &options[nr_options],
2483 NULL, browser->selection->sym);
2484 }
Feng Tangcdbab7c2012-10-30 11:56:06 +08002485 }
Namhyung Kimea7cd592015-04-22 16:18:19 +09002486 nr_options += add_script_opt(browser, &actions[nr_options],
2487 &options[nr_options], NULL, NULL);
2488 nr_options += add_switch_opt(browser, &actions[nr_options],
2489 &options[nr_options]);
Namhyung Kimb1baae82016-01-26 15:37:30 -03002490skip_scripting:
Namhyung Kimea7cd592015-04-22 16:18:19 +09002491 nr_options += add_exit_opt(browser, &actions[nr_options],
2492 &options[nr_options]);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002493
Namhyung Kimea7cd592015-04-22 16:18:19 +09002494 do {
2495 struct popup_action *act;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002496
Namhyung Kimea7cd592015-04-22 16:18:19 +09002497 choice = ui__popup_menu(nr_options, options);
2498 if (choice == -1 || choice >= nr_options)
Feng Tang341487ab2013-02-03 14:38:20 +08002499 break;
Namhyung Kimea7cd592015-04-22 16:18:19 +09002500
2501 act = &actions[choice];
2502 key = act->fn(browser, act);
2503 } while (key == 1);
2504
2505 if (key == K_SWITCH_INPUT_DATA)
2506 break;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002507 }
2508out_free_stack:
Namhyung Kim01f00a12015-04-22 16:18:16 +09002509 pstack__delete(browser->pstack);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002510out:
2511 hist_browser__delete(browser);
Namhyung Kimf2b487d2015-04-22 16:18:14 +09002512 free_popup_options(options, MAX_OPTIONS);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002513 return key;
2514}
2515
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002516struct perf_evsel_menu {
2517 struct ui_browser b;
2518 struct perf_evsel *selection;
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02002519 bool lost_events, lost_events_warned;
Namhyung Kim064f1982013-05-14 11:09:04 +09002520 float min_pcnt;
Kan Liangce80d3b2015-08-28 05:48:04 -04002521 struct perf_env *env;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002522};
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002523
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002524static void perf_evsel_menu__write(struct ui_browser *browser,
2525 void *entry, int row)
2526{
2527 struct perf_evsel_menu *menu = container_of(browser,
2528 struct perf_evsel_menu, b);
2529 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03002530 struct hists *hists = evsel__hists(evsel);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002531 bool current_entry = ui_browser__is_current_entry(browser, row);
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03002532 unsigned long nr_events = hists->stats.nr_events[PERF_RECORD_SAMPLE];
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03002533 const char *ev_name = perf_evsel__name(evsel);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002534 char bf[256], unit;
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02002535 const char *warn = " ";
2536 size_t printed;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002537
2538 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
2539 HE_COLORSET_NORMAL);
2540
Namhyung Kim759ff492013-03-05 14:53:26 +09002541 if (perf_evsel__is_group_event(evsel)) {
Namhyung Kim717e2632013-01-22 18:09:44 +09002542 struct perf_evsel *pos;
2543
2544 ev_name = perf_evsel__group_name(evsel);
2545
2546 for_each_group_member(pos, evsel) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03002547 struct hists *pos_hists = evsel__hists(pos);
2548 nr_events += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
Namhyung Kim717e2632013-01-22 18:09:44 +09002549 }
2550 }
2551
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002552 nr_events = convert_unit(nr_events, &unit);
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -03002553 printed = scnprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02002554 unit, unit == ' ' ? "" : " ", ev_name);
Arnaldo Carvalho de Melo517dfdb2015-08-11 12:50:55 -03002555 ui_browser__printf(browser, "%s", bf);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02002556
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03002557 nr_events = hists->stats.nr_events[PERF_RECORD_LOST];
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02002558 if (nr_events != 0) {
2559 menu->lost_events = true;
2560 if (!current_entry)
2561 ui_browser__set_color(browser, HE_COLORSET_TOP);
2562 nr_events = convert_unit(nr_events, &unit);
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -03002563 printed += scnprintf(bf, sizeof(bf), ": %ld%c%schunks LOST!",
2564 nr_events, unit, unit == ' ' ? "" : " ");
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02002565 warn = bf;
2566 }
2567
Arnaldo Carvalho de Melo26270a02015-08-11 12:24:27 -03002568 ui_browser__write_nstring(browser, warn, browser->width - printed);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002569
2570 if (current_entry)
2571 menu->selection = evsel;
2572}
2573
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03002574static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
2575 int nr_events, const char *help,
Namhyung Kim9783adf2012-11-02 14:50:05 +09002576 struct hist_browser_timer *hbt)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002577{
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002578 struct perf_evlist *evlist = menu->b.priv;
2579 struct perf_evsel *pos;
Jiri Olsadd00d482014-06-19 13:41:13 +02002580 const char *title = "Available samples";
Namhyung Kim9783adf2012-11-02 14:50:05 +09002581 int delay_secs = hbt ? hbt->refresh : 0;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002582 int key;
2583
2584 if (ui_browser__show(&menu->b, title,
2585 "ESC: exit, ENTER|->: Browse histograms") < 0)
2586 return -1;
2587
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002588 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03002589 key = ui_browser__run(&menu->b, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002590
2591 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02002592 case K_TIMER:
Namhyung Kim9783adf2012-11-02 14:50:05 +09002593 hbt->timer(hbt->arg);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02002594
2595 if (!menu->lost_events_warned && menu->lost_events) {
2596 ui_browser__warn_lost_events(&menu->b);
2597 menu->lost_events_warned = true;
2598 }
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03002599 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02002600 case K_RIGHT:
2601 case K_ENTER:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002602 if (!menu->selection)
2603 continue;
2604 pos = menu->selection;
2605browse_hists:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03002606 perf_evlist__set_selected(evlist, pos);
2607 /*
2608 * Give the calling tool a chance to populate the non
2609 * default evsel resorted hists tree.
2610 */
Namhyung Kim9783adf2012-11-02 14:50:05 +09002611 if (hbt)
2612 hbt->timer(hbt->arg);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03002613 key = perf_evsel__hists_browse(pos, nr_events, help,
Jiri Olsadd00d482014-06-19 13:41:13 +02002614 true, hbt,
Namhyung Kim064f1982013-05-14 11:09:04 +09002615 menu->min_pcnt,
Namhyung Kim68d80752012-11-02 14:50:06 +09002616 menu->env);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002617 ui_browser__show_title(&menu->b, title);
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03002618 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02002619 case K_TAB:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03002620 if (pos->node.next == &evlist->entries)
Arnaldo Carvalho de Melo9a354cd2013-11-13 15:54:30 -03002621 pos = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03002622 else
Arnaldo Carvalho de Melo9a354cd2013-11-13 15:54:30 -03002623 pos = perf_evsel__next(pos);
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03002624 goto browse_hists;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02002625 case K_UNTAB:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03002626 if (pos->node.prev == &evlist->entries)
Arnaldo Carvalho de Melo9a354cd2013-11-13 15:54:30 -03002627 pos = perf_evlist__last(evlist);
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03002628 else
Arnaldo Carvalho de Melod87fcb42013-11-13 15:56:40 -03002629 pos = perf_evsel__prev(pos);
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03002630 goto browse_hists;
Feng Tang341487ab2013-02-03 14:38:20 +08002631 case K_SWITCH_INPUT_DATA:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03002632 case 'q':
2633 case CTRL('c'):
2634 goto out;
Arnaldo Carvalho de Melo63ab1742015-08-12 12:42:58 -03002635 case K_ESC:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03002636 default:
2637 continue;
2638 }
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02002639 case K_LEFT:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002640 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02002641 case K_ESC:
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02002642 if (!ui_browser__dialog_yesno(&menu->b,
2643 "Do you really want to exit?"))
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -03002644 continue;
2645 /* Fall thru */
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002646 case 'q':
2647 case CTRL('c'):
2648 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002649 default:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03002650 continue;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002651 }
2652 }
2653
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002654out:
2655 ui_browser__hide(&menu->b);
2656 return key;
2657}
2658
Arnaldo Carvalho de Melo316c7132013-11-05 15:32:36 -03002659static bool filter_group_entries(struct ui_browser *browser __maybe_unused,
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09002660 void *entry)
2661{
2662 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
2663
2664 if (symbol_conf.event_group && !perf_evsel__is_group_leader(evsel))
2665 return true;
2666
2667 return false;
2668}
2669
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002670static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09002671 int nr_entries, const char *help,
Namhyung Kim68d80752012-11-02 14:50:06 +09002672 struct hist_browser_timer *hbt,
Namhyung Kim064f1982013-05-14 11:09:04 +09002673 float min_pcnt,
Kan Liangce80d3b2015-08-28 05:48:04 -04002674 struct perf_env *env)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002675{
2676 struct perf_evsel *pos;
2677 struct perf_evsel_menu menu = {
2678 .b = {
2679 .entries = &evlist->entries,
2680 .refresh = ui_browser__list_head_refresh,
2681 .seek = ui_browser__list_head_seek,
2682 .write = perf_evsel_menu__write,
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09002683 .filter = filter_group_entries,
2684 .nr_entries = nr_entries,
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002685 .priv = evlist,
2686 },
Namhyung Kim064f1982013-05-14 11:09:04 +09002687 .min_pcnt = min_pcnt,
Namhyung Kim68d80752012-11-02 14:50:06 +09002688 .env = env,
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002689 };
2690
2691 ui_helpline__push("Press ESC to exit");
2692
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002693 evlist__for_each(evlist, pos) {
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03002694 const char *ev_name = perf_evsel__name(pos);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002695 size_t line_len = strlen(ev_name) + 7;
2696
2697 if (menu.b.width < line_len)
2698 menu.b.width = line_len;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002699 }
2700
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09002701 return perf_evsel_menu__run(&menu, nr_entries, help, hbt);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002702}
2703
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03002704int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
Namhyung Kim68d80752012-11-02 14:50:06 +09002705 struct hist_browser_timer *hbt,
Namhyung Kim064f1982013-05-14 11:09:04 +09002706 float min_pcnt,
Kan Liangce80d3b2015-08-28 05:48:04 -04002707 struct perf_env *env)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002708{
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09002709 int nr_entries = evlist->nr_entries;
2710
2711single_entry:
2712 if (nr_entries == 1) {
Arnaldo Carvalho de Melo9a354cd2013-11-13 15:54:30 -03002713 struct perf_evsel *first = perf_evlist__first(evlist);
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09002714
2715 return perf_evsel__hists_browse(first, nr_entries, help,
Jiri Olsadd00d482014-06-19 13:41:13 +02002716 false, hbt, min_pcnt,
Namhyung Kim064f1982013-05-14 11:09:04 +09002717 env);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03002718 }
2719
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09002720 if (symbol_conf.event_group) {
2721 struct perf_evsel *pos;
2722
2723 nr_entries = 0;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002724 evlist__for_each(evlist, pos) {
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09002725 if (perf_evsel__is_group_leader(pos))
2726 nr_entries++;
Arnaldo Carvalho de Melo0050f7a2014-01-10 10:37:27 -03002727 }
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09002728
2729 if (nr_entries == 1)
2730 goto single_entry;
2731 }
2732
2733 return __perf_evlist__tui_browse_hists(evlist, nr_entries, help,
Namhyung Kim064f1982013-05-14 11:09:04 +09002734 hbt, min_pcnt, env);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002735}