blob: cad8e37f05d99285e3ae4dd0d93b16abcda2d5d3 [file] [log] [blame]
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001#include <stdio.h>
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03002#include "../libslang.h"
3#include <stdlib.h>
4#include <string.h>
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03005#include <linux/rbtree.h>
6
Namhyung Kimaca7a942012-04-04 00:14:26 -07007#include "../../util/evsel.h"
8#include "../../util/evlist.h"
9#include "../../util/hist.h"
10#include "../../util/pstack.h"
11#include "../../util/sort.h"
12#include "../../util/util.h"
Namhyung Kim68d80752012-11-02 14:50:06 +090013#include "../../arch/common.h"
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030014
15#include "../browser.h"
16#include "../helpline.h"
17#include "../util.h"
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -020018#include "../ui.h"
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030019#include "map.h"
20
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030021struct hist_browser {
22 struct ui_browser b;
23 struct hists *hists;
24 struct hist_entry *he_selection;
25 struct map_symbol *selection;
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -030026 int print_seq;
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -030027 bool show_dso;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -030028 bool has_symbols;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030029};
30
Namhyung Kimf5951d52012-09-03 11:53:09 +090031extern void hist_browser__init_hpp(void);
32
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030033static int hists__browser_title(struct hists *hists, char *bf, size_t size,
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -020034 const char *ev_name);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -030035
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030036static void hist_browser__refresh_dimensions(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030037{
38 /* 3 == +/- toggle symbol before actual hist_entry rendering */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030039 browser->b.width = 3 + (hists__sort_list_width(browser->hists) +
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030040 sizeof("[k]"));
41}
42
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030043static void hist_browser__reset(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030044{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030045 browser->b.nr_entries = browser->hists->nr_entries;
46 hist_browser__refresh_dimensions(browser);
47 ui_browser__reset_index(&browser->b);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030048}
49
50static char tree__folded_sign(bool unfolded)
51{
52 return unfolded ? '-' : '+';
53}
54
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030055static char map_symbol__folded(const struct map_symbol *ms)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030056{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030057 return ms->has_children ? tree__folded_sign(ms->unfolded) : ' ';
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030058}
59
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030060static char hist_entry__folded(const struct hist_entry *he)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030061{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030062 return map_symbol__folded(&he->ms);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030063}
64
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030065static char callchain_list__folded(const struct callchain_list *cl)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030066{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030067 return map_symbol__folded(&cl->ms);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030068}
69
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030070static void map_symbol__set_folding(struct map_symbol *ms, bool unfold)
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -030071{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030072 ms->unfolded = unfold ? ms->has_children : false;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -030073}
74
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030075static int callchain_node__count_rows_rb_tree(struct callchain_node *node)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030076{
77 int n = 0;
78 struct rb_node *nd;
79
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -030080 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -030081 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
82 struct callchain_list *chain;
83 char folded_sign = ' '; /* No children */
84
85 list_for_each_entry(chain, &child->val, list) {
86 ++n;
87 /* We need this because we may not have children */
88 folded_sign = callchain_list__folded(chain);
89 if (folded_sign == '+')
90 break;
91 }
92
93 if (folded_sign == '-') /* Have children and they're unfolded */
94 n += callchain_node__count_rows_rb_tree(child);
95 }
96
97 return n;
98}
99
100static int callchain_node__count_rows(struct callchain_node *node)
101{
102 struct callchain_list *chain;
103 bool unfolded = false;
104 int n = 0;
105
106 list_for_each_entry(chain, &node->val, list) {
107 ++n;
108 unfolded = chain->ms.unfolded;
109 }
110
111 if (unfolded)
112 n += callchain_node__count_rows_rb_tree(node);
113
114 return n;
115}
116
117static int callchain__count_rows(struct rb_root *chain)
118{
119 struct rb_node *nd;
120 int n = 0;
121
122 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
123 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
124 n += callchain_node__count_rows(node);
125 }
126
127 return n;
128}
129
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300130static bool map_symbol__toggle_fold(struct map_symbol *ms)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300131{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300132 if (!ms)
Jiri Olsa8493fe12012-04-04 22:21:31 +0200133 return false;
134
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300135 if (!ms->has_children)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300136 return false;
137
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300138 ms->unfolded = !ms->unfolded;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300139 return true;
140}
141
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300142static void callchain_node__init_have_children_rb_tree(struct callchain_node *node)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300143{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300144 struct rb_node *nd = rb_first(&node->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300145
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300146 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300147 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
148 struct callchain_list *chain;
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300149 bool first = true;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300150
151 list_for_each_entry(chain, &child->val, list) {
152 if (first) {
153 first = false;
154 chain->ms.has_children = chain->list.next != &child->val ||
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300155 !RB_EMPTY_ROOT(&child->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300156 } else
157 chain->ms.has_children = chain->list.next == &child->val &&
Arnaldo Carvalho de Melo293db472010-08-25 16:05:36 -0300158 !RB_EMPTY_ROOT(&child->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300159 }
160
161 callchain_node__init_have_children_rb_tree(child);
162 }
163}
164
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300165static void callchain_node__init_have_children(struct callchain_node *node)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300166{
167 struct callchain_list *chain;
168
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300169 list_for_each_entry(chain, &node->val, list)
170 chain->ms.has_children = !RB_EMPTY_ROOT(&node->rb_root);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300171
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300172 callchain_node__init_have_children_rb_tree(node);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300173}
174
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300175static void callchain__init_have_children(struct rb_root *root)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300176{
177 struct rb_node *nd;
178
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300179 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300180 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
181 callchain_node__init_have_children(node);
182 }
183}
184
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300185static void hist_entry__init_have_children(struct hist_entry *he)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300186{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300187 if (!he->init_have_children) {
188 he->ms.has_children = !RB_EMPTY_ROOT(&he->sorted_chain);
189 callchain__init_have_children(&he->sorted_chain);
190 he->init_have_children = true;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300191 }
192}
193
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300194static bool hist_browser__toggle_fold(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300195{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300196 if (map_symbol__toggle_fold(browser->selection)) {
197 struct hist_entry *he = browser->he_selection;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300198
199 hist_entry__init_have_children(he);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300200 browser->hists->nr_entries -= he->nr_rows;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300201
202 if (he->ms.unfolded)
203 he->nr_rows = callchain__count_rows(&he->sorted_chain);
204 else
205 he->nr_rows = 0;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300206 browser->hists->nr_entries += he->nr_rows;
207 browser->b.nr_entries = browser->hists->nr_entries;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300208
209 return true;
210 }
211
212 /* If it doesn't have children, no toggling performed */
213 return false;
214}
215
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300216static int callchain_node__set_folding_rb_tree(struct callchain_node *node, bool unfold)
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300217{
218 int n = 0;
219 struct rb_node *nd;
220
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300221 for (nd = rb_first(&node->rb_root); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300222 struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node);
223 struct callchain_list *chain;
224 bool has_children = false;
225
226 list_for_each_entry(chain, &child->val, list) {
227 ++n;
228 map_symbol__set_folding(&chain->ms, unfold);
229 has_children = chain->ms.has_children;
230 }
231
232 if (has_children)
233 n += callchain_node__set_folding_rb_tree(child, unfold);
234 }
235
236 return n;
237}
238
239static int callchain_node__set_folding(struct callchain_node *node, bool unfold)
240{
241 struct callchain_list *chain;
242 bool has_children = false;
243 int n = 0;
244
245 list_for_each_entry(chain, &node->val, list) {
246 ++n;
247 map_symbol__set_folding(&chain->ms, unfold);
248 has_children = chain->ms.has_children;
249 }
250
251 if (has_children)
252 n += callchain_node__set_folding_rb_tree(node, unfold);
253
254 return n;
255}
256
257static int callchain__set_folding(struct rb_root *chain, bool unfold)
258{
259 struct rb_node *nd;
260 int n = 0;
261
262 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
263 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
264 n += callchain_node__set_folding(node, unfold);
265 }
266
267 return n;
268}
269
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300270static void hist_entry__set_folding(struct hist_entry *he, bool unfold)
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300271{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300272 hist_entry__init_have_children(he);
273 map_symbol__set_folding(&he->ms, unfold);
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300274
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300275 if (he->ms.has_children) {
276 int n = callchain__set_folding(&he->sorted_chain, unfold);
277 he->nr_rows = unfold ? n : 0;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300278 } else
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300279 he->nr_rows = 0;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300280}
281
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300282static void hists__set_folding(struct hists *hists, bool unfold)
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300283{
284 struct rb_node *nd;
285
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300286 hists->nr_entries = 0;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300287
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300288 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300289 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
290 hist_entry__set_folding(he, unfold);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300291 hists->nr_entries += 1 + he->nr_rows;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300292 }
293}
294
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300295static void hist_browser__set_folding(struct hist_browser *browser, bool unfold)
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300296{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300297 hists__set_folding(browser->hists, unfold);
298 browser->b.nr_entries = browser->hists->nr_entries;
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300299 /* Go to the start, we may be way after valid entries after a collapse */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300300 ui_browser__reset_index(&browser->b);
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300301}
302
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200303static void ui_browser__warn_lost_events(struct ui_browser *browser)
304{
305 ui_browser__warning(browser, 4,
306 "Events are being lost, check IO/CPU overload!\n\n"
307 "You may want to run 'perf' using a RT scheduler policy:\n\n"
308 " perf top -r 80\n\n"
309 "Or reduce the sampling frequency.");
310}
311
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300312static int hist_browser__run(struct hist_browser *browser, const char *ev_name,
Namhyung Kim9783adf2012-11-02 14:50:05 +0900313 struct hist_browser_timer *hbt)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300314{
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300315 int key;
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300316 char title[160];
Namhyung Kim9783adf2012-11-02 14:50:05 +0900317 int delay_secs = hbt ? hbt->refresh : 0;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300318
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300319 browser->b.entries = &browser->hists->entries;
320 browser->b.nr_entries = browser->hists->nr_entries;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300321
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300322 hist_browser__refresh_dimensions(browser);
323 hists__browser_title(browser->hists, title, sizeof(title), ev_name);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300324
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300325 if (ui_browser__show(&browser->b, title,
Arnaldo Carvalho de Melo59e8fe32010-08-10 15:44:20 -0300326 "Press '?' for help on key bindings") < 0)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300327 return -1;
328
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300329 while (1) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300330 key = ui_browser__run(&browser->b, delay_secs);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300331
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300332 switch (key) {
Arnaldo Carvalho de Melo13d8f962011-10-26 08:05:34 -0200333 case K_TIMER:
Namhyung Kim9783adf2012-11-02 14:50:05 +0900334 hbt->timer(hbt->arg);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300335 ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200336
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300337 if (browser->hists->stats.nr_lost_warned !=
338 browser->hists->stats.nr_events[PERF_RECORD_LOST]) {
339 browser->hists->stats.nr_lost_warned =
340 browser->hists->stats.nr_events[PERF_RECORD_LOST];
341 ui_browser__warn_lost_events(&browser->b);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -0200342 }
343
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300344 hists__browser_title(browser->hists, title, sizeof(title), ev_name);
345 ui_browser__show_title(&browser->b, title);
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300346 continue;
Arnaldo Carvalho de Melo46941532010-08-10 15:50:07 -0300347 case 'D': { /* Debug */
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300348 static int seq;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300349 struct hist_entry *h = rb_entry(browser->b.top,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300350 struct hist_entry, rb_node);
351 ui_helpline__pop();
352 ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d",
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300353 seq++, browser->b.nr_entries,
354 browser->hists->nr_entries,
355 browser->b.height,
356 browser->b.index,
357 browser->b.top_idx,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300358 h->row_offset, h->nr_rows);
359 }
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300360 break;
361 case 'C':
362 /* Collapse the whole world. */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300363 hist_browser__set_folding(browser, false);
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300364 break;
365 case 'E':
366 /* Expand the whole world. */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300367 hist_browser__set_folding(browser, true);
Arnaldo Carvalho de Melo3c916cc2010-08-25 17:18:35 -0300368 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200369 case K_ENTER:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300370 if (hist_browser__toggle_fold(browser))
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300371 break;
372 /* fall thru */
373 default:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300374 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300375 }
376 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300377out:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300378 ui_browser__hide(&browser->b);
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300379 return key;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300380}
381
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300382static char *callchain_list__sym_name(struct callchain_list *cl,
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -0300383 char *bf, size_t bfsize, bool show_dso)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300384{
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -0300385 int printed;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300386
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -0300387 if (cl->ms.sym)
388 printed = scnprintf(bf, bfsize, "%s", cl->ms.sym->name);
389 else
390 printed = scnprintf(bf, bfsize, "%#" PRIx64, cl->ip);
391
392 if (show_dso)
393 scnprintf(bf + printed, bfsize - printed, " %s",
394 cl->ms.map ? cl->ms.map->dso->short_name : "unknown");
395
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300396 return bf;
397}
398
399#define LEVEL_OFFSET_STEP 3
400
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300401static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *browser,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300402 struct callchain_node *chain_node,
403 u64 total, int level,
404 unsigned short row,
405 off_t *row_offset,
406 bool *is_current_entry)
407{
408 struct rb_node *node;
409 int first_row = row, width, offset = level * LEVEL_OFFSET_STEP;
410 u64 new_total, remaining;
411
412 if (callchain_param.mode == CHAIN_GRAPH_REL)
413 new_total = chain_node->children_hit;
414 else
415 new_total = total;
416
417 remaining = new_total;
418 node = rb_first(&chain_node->rb_root);
419 while (node) {
420 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
421 struct rb_node *next = rb_next(node);
Frederic Weisbeckerf08c3152011-01-14 04:51:59 +0100422 u64 cumul = callchain_cumul_hits(child);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300423 struct callchain_list *chain;
424 char folded_sign = ' ';
425 int first = true;
426 int extra_offset = 0;
427
428 remaining -= cumul;
429
430 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -0300431 char bf[1024], *alloc_str;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300432 const char *str;
433 int color;
434 bool was_first = first;
435
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300436 if (first)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300437 first = false;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300438 else
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300439 extra_offset = LEVEL_OFFSET_STEP;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300440
441 folded_sign = callchain_list__folded(chain);
442 if (*row_offset != 0) {
443 --*row_offset;
444 goto do_next;
445 }
446
447 alloc_str = NULL;
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -0300448 str = callchain_list__sym_name(chain, bf, sizeof(bf),
449 browser->show_dso);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300450 if (was_first) {
451 double percent = cumul * 100.0 / new_total;
452
453 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
454 str = "Not enough memory!";
455 else
456 str = alloc_str;
457 }
458
459 color = HE_COLORSET_NORMAL;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300460 width = browser->b.width - (offset + extra_offset + 2);
461 if (ui_browser__is_current_entry(&browser->b, row)) {
462 browser->selection = &chain->ms;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300463 color = HE_COLORSET_SELECTED;
464 *is_current_entry = true;
465 }
466
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300467 ui_browser__set_color(&browser->b, color);
468 ui_browser__gotorc(&browser->b, row, 0);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300469 slsmg_write_nstring(" ", offset + extra_offset);
470 slsmg_printf("%c ", folded_sign);
471 slsmg_write_nstring(str, width);
472 free(alloc_str);
473
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300474 if (++row == browser->b.height)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300475 goto out;
476do_next:
477 if (folded_sign == '+')
478 break;
479 }
480
481 if (folded_sign == '-') {
482 const int new_level = level + (extra_offset ? 2 : 1);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300483 row += hist_browser__show_callchain_node_rb_tree(browser, child, new_total,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300484 new_level, row, row_offset,
485 is_current_entry);
486 }
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300487 if (row == browser->b.height)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300488 goto out;
489 node = next;
490 }
491out:
492 return row - first_row;
493}
494
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300495static int hist_browser__show_callchain_node(struct hist_browser *browser,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300496 struct callchain_node *node,
497 int level, unsigned short row,
498 off_t *row_offset,
499 bool *is_current_entry)
500{
501 struct callchain_list *chain;
502 int first_row = row,
503 offset = level * LEVEL_OFFSET_STEP,
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300504 width = browser->b.width - offset;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300505 char folded_sign = ' ';
506
507 list_for_each_entry(chain, &node->val, list) {
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -0300508 char bf[1024], *s;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300509 int color;
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300510
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300511 folded_sign = callchain_list__folded(chain);
512
513 if (*row_offset != 0) {
514 --*row_offset;
515 continue;
516 }
517
518 color = HE_COLORSET_NORMAL;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300519 if (ui_browser__is_current_entry(&browser->b, row)) {
520 browser->selection = &chain->ms;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300521 color = HE_COLORSET_SELECTED;
522 *is_current_entry = true;
523 }
524
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -0300525 s = callchain_list__sym_name(chain, bf, sizeof(bf),
526 browser->show_dso);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300527 ui_browser__gotorc(&browser->b, row, 0);
528 ui_browser__set_color(&browser->b, color);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300529 slsmg_write_nstring(" ", offset);
530 slsmg_printf("%c ", folded_sign);
531 slsmg_write_nstring(s, width - 2);
532
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300533 if (++row == browser->b.height)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300534 goto out;
535 }
536
537 if (folded_sign == '-')
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300538 row += hist_browser__show_callchain_node_rb_tree(browser, node,
539 browser->hists->stats.total_period,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300540 level + 1, row,
541 row_offset,
542 is_current_entry);
543out:
544 return row - first_row;
545}
546
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300547static int hist_browser__show_callchain(struct hist_browser *browser,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300548 struct rb_root *chain,
549 int level, unsigned short row,
550 off_t *row_offset,
551 bool *is_current_entry)
552{
553 struct rb_node *nd;
554 int first_row = row;
555
556 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
557 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
558
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300559 row += hist_browser__show_callchain_node(browser, node, level,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300560 row, row_offset,
561 is_current_entry);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300562 if (row == browser->b.height)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300563 break;
564 }
565
566 return row - first_row;
567}
568
Namhyung Kim89701462013-01-22 18:09:38 +0900569struct hpp_arg {
570 struct ui_browser *b;
571 char folded_sign;
572 bool current_entry;
573};
574
575static int __hpp__color_callchain(struct hpp_arg *arg)
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900576{
Namhyung Kim89701462013-01-22 18:09:38 +0900577 if (!symbol_conf.use_callchain)
578 return 0;
579
580 slsmg_printf("%c ", arg->folded_sign);
581 return 2;
582}
583
584static int __hpp__color_fmt(struct perf_hpp *hpp, struct hist_entry *he,
585 u64 (*get_field)(struct hist_entry *),
586 int (*callchain_cb)(struct hpp_arg *))
587{
588 int ret = 0;
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900589 double percent = 0.0;
590 struct hists *hists = he->hists;
Namhyung Kim89701462013-01-22 18:09:38 +0900591 struct hpp_arg *arg = hpp->ptr;
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900592
593 if (hists->stats.total_period)
594 percent = 100.0 * get_field(he) / hists->stats.total_period;
595
Namhyung Kim89701462013-01-22 18:09:38 +0900596 ui_browser__set_percent_color(arg->b, percent, arg->current_entry);
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900597
Namhyung Kim89701462013-01-22 18:09:38 +0900598 if (callchain_cb)
599 ret += callchain_cb(arg);
600
601 ret += scnprintf(hpp->buf, hpp->size, "%6.2f%%", percent);
602 slsmg_printf("%s", hpp->buf);
603
Namhyung Kim371d8c42013-01-22 18:09:39 +0900604 if (symbol_conf.event_group) {
605 int prev_idx, idx_delta;
606 struct perf_evsel *evsel = hists_to_evsel(hists);
607 struct hist_entry *pair;
608 int nr_members = evsel->nr_members;
609
610 if (nr_members <= 1)
611 goto out;
612
613 prev_idx = perf_evsel__group_idx(evsel);
614
615 list_for_each_entry(pair, &he->pairs.head, pairs.node) {
616 u64 period = get_field(pair);
617 u64 total = pair->hists->stats.total_period;
618
619 if (!total)
620 continue;
621
622 evsel = hists_to_evsel(pair->hists);
623 idx_delta = perf_evsel__group_idx(evsel) - prev_idx - 1;
624
625 while (idx_delta--) {
626 /*
627 * zero-fill group members in the middle which
628 * have no sample
629 */
630 ui_browser__set_percent_color(arg->b, 0.0,
631 arg->current_entry);
632 ret += scnprintf(hpp->buf, hpp->size,
633 " %6.2f%%", 0.0);
634 slsmg_printf("%s", hpp->buf);
635 }
636
637 percent = 100.0 * period / total;
638 ui_browser__set_percent_color(arg->b, percent,
639 arg->current_entry);
640 ret += scnprintf(hpp->buf, hpp->size,
641 " %6.2f%%", percent);
642 slsmg_printf("%s", hpp->buf);
643
644 prev_idx = perf_evsel__group_idx(evsel);
645 }
646
647 idx_delta = nr_members - prev_idx - 1;
648
649 while (idx_delta--) {
650 /*
651 * zero-fill group members at last which have no sample
652 */
653 ui_browser__set_percent_color(arg->b, 0.0,
654 arg->current_entry);
655 ret += scnprintf(hpp->buf, hpp->size,
656 " %6.2f%%", 0.0);
657 slsmg_printf("%s", hpp->buf);
658 }
659 }
660out:
Namhyung Kim89701462013-01-22 18:09:38 +0900661 if (!arg->current_entry || !arg->b->navkeypressed)
662 ui_browser__set_color(arg->b, HE_COLORSET_NORMAL);
663
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900664 return ret;
Namhyung Kimf5951d52012-09-03 11:53:09 +0900665}
666
Namhyung Kim89701462013-01-22 18:09:38 +0900667#define __HPP_COLOR_PERCENT_FN(_type, _field, _cb) \
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900668static u64 __hpp_get_##_field(struct hist_entry *he) \
669{ \
670 return he->stat._field; \
671} \
672 \
673static int hist_browser__hpp_color_##_type(struct perf_hpp *hpp, \
674 struct hist_entry *he) \
675{ \
Namhyung Kim89701462013-01-22 18:09:38 +0900676 return __hpp__color_fmt(hpp, he, __hpp_get_##_field, _cb); \
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900677}
Namhyung Kimf5951d52012-09-03 11:53:09 +0900678
Namhyung Kim89701462013-01-22 18:09:38 +0900679__HPP_COLOR_PERCENT_FN(overhead, period, __hpp__color_callchain)
680__HPP_COLOR_PERCENT_FN(overhead_sys, period_sys, NULL)
681__HPP_COLOR_PERCENT_FN(overhead_us, period_us, NULL)
682__HPP_COLOR_PERCENT_FN(overhead_guest_sys, period_guest_sys, NULL)
683__HPP_COLOR_PERCENT_FN(overhead_guest_us, period_guest_us, NULL)
Namhyung Kim5aed9d22013-01-22 18:09:35 +0900684
685#undef __HPP_COLOR_PERCENT_FN
Namhyung Kimf5951d52012-09-03 11:53:09 +0900686
687void hist_browser__init_hpp(void)
688{
Jiri Olsa12400052012-10-13 00:06:16 +0200689 perf_hpp__column_enable(PERF_HPP__OVERHEAD);
690
Jiri Olsa1d778222012-10-04 21:49:39 +0900691 perf_hpp__init();
Namhyung Kimf5951d52012-09-03 11:53:09 +0900692
693 perf_hpp__format[PERF_HPP__OVERHEAD].color =
694 hist_browser__hpp_color_overhead;
695 perf_hpp__format[PERF_HPP__OVERHEAD_SYS].color =
696 hist_browser__hpp_color_overhead_sys;
697 perf_hpp__format[PERF_HPP__OVERHEAD_US].color =
698 hist_browser__hpp_color_overhead_us;
699 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_SYS].color =
700 hist_browser__hpp_color_overhead_guest_sys;
701 perf_hpp__format[PERF_HPP__OVERHEAD_GUEST_US].color =
702 hist_browser__hpp_color_overhead_guest_us;
703}
704
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300705static int hist_browser__show_entry(struct hist_browser *browser,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300706 struct hist_entry *entry,
707 unsigned short row)
708{
709 char s[256];
Jiri Olsa12400052012-10-13 00:06:16 +0200710 int printed = 0;
Namhyung Kim67d25912012-09-12 15:35:06 +0900711 int width = browser->b.width;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300712 char folded_sign = ' ';
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300713 bool current_entry = ui_browser__is_current_entry(&browser->b, row);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300714 off_t row_offset = entry->row_offset;
Namhyung Kim63a1a3d2012-10-15 18:14:35 -0300715 bool first = true;
Jiri Olsa12400052012-10-13 00:06:16 +0200716 struct perf_hpp_fmt *fmt;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300717
718 if (current_entry) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300719 browser->he_selection = entry;
720 browser->selection = &entry->ms;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300721 }
722
723 if (symbol_conf.use_callchain) {
Arnaldo Carvalho de Melo163caed2010-08-25 16:30:03 -0300724 hist_entry__init_have_children(entry);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300725 folded_sign = hist_entry__folded(entry);
726 }
727
728 if (row_offset == 0) {
Namhyung Kim89701462013-01-22 18:09:38 +0900729 struct hpp_arg arg = {
730 .b = &browser->b,
731 .folded_sign = folded_sign,
732 .current_entry = current_entry,
733 };
Namhyung Kimf5951d52012-09-03 11:53:09 +0900734 struct perf_hpp hpp = {
735 .buf = s,
736 .size = sizeof(s),
Namhyung Kim89701462013-01-22 18:09:38 +0900737 .ptr = &arg,
Namhyung Kimf5951d52012-09-03 11:53:09 +0900738 };
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300739
Namhyung Kim67d25912012-09-12 15:35:06 +0900740 ui_browser__gotorc(&browser->b, row, 0);
Namhyung Kimf5951d52012-09-03 11:53:09 +0900741
Jiri Olsa12400052012-10-13 00:06:16 +0200742 perf_hpp__for_each_format(fmt) {
Namhyung Kim63a1a3d2012-10-15 18:14:35 -0300743 if (!first) {
Namhyung Kimf5951d52012-09-03 11:53:09 +0900744 slsmg_printf(" ");
745 width -= 2;
746 }
Namhyung Kim63a1a3d2012-10-15 18:14:35 -0300747 first = false;
Namhyung Kimf5951d52012-09-03 11:53:09 +0900748
Jiri Olsa12400052012-10-13 00:06:16 +0200749 if (fmt->color) {
Jiri Olsa12400052012-10-13 00:06:16 +0200750 width -= fmt->color(&hpp, entry);
Namhyung Kimf5951d52012-09-03 11:53:09 +0900751 } else {
Jiri Olsa12400052012-10-13 00:06:16 +0200752 width -= fmt->entry(&hpp, entry);
Namhyung Kimf5951d52012-09-03 11:53:09 +0900753 slsmg_printf("%s", s);
754 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300755 }
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200756
757 /* The scroll bar isn't being used */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300758 if (!browser->b.navkeypressed)
Arnaldo Carvalho de Meloc172f742011-10-18 14:31:35 -0200759 width += 1;
760
Namhyung Kimf5951d52012-09-03 11:53:09 +0900761 hist_entry__sort_snprintf(entry, s, sizeof(s), browser->hists);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300762 slsmg_write_nstring(s, width);
763 ++row;
764 ++printed;
765 } else
766 --row_offset;
767
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300768 if (folded_sign == '-' && row != browser->b.height) {
769 printed += hist_browser__show_callchain(browser, &entry->sorted_chain,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300770 1, row, &row_offset,
771 &current_entry);
772 if (current_entry)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300773 browser->he_selection = entry;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300774 }
775
776 return printed;
777}
778
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300779static void ui_browser__hists_init_top(struct ui_browser *browser)
780{
781 if (browser->top == NULL) {
782 struct hist_browser *hb;
783
784 hb = container_of(browser, struct hist_browser, b);
785 browser->top = rb_first(&hb->hists->entries);
786 }
787}
788
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300789static unsigned int hist_browser__refresh(struct ui_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300790{
791 unsigned row = 0;
792 struct rb_node *nd;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300793 struct hist_browser *hb = container_of(browser, struct hist_browser, b);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300794
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300795 ui_browser__hists_init_top(browser);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300796
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300797 for (nd = browser->top; nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300798 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
799
800 if (h->filtered)
801 continue;
802
803 row += hist_browser__show_entry(hb, h, row);
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300804 if (row == browser->height)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300805 break;
806 }
807
808 return row;
809}
810
811static struct rb_node *hists__filter_entries(struct rb_node *nd)
812{
813 while (nd != NULL) {
814 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
815 if (!h->filtered)
816 return nd;
817
818 nd = rb_next(nd);
819 }
820
821 return NULL;
822}
823
824static struct rb_node *hists__filter_prev_entries(struct rb_node *nd)
825{
826 while (nd != NULL) {
827 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
828 if (!h->filtered)
829 return nd;
830
831 nd = rb_prev(nd);
832 }
833
834 return NULL;
835}
836
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300837static void ui_browser__hists_seek(struct ui_browser *browser,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300838 off_t offset, int whence)
839{
840 struct hist_entry *h;
841 struct rb_node *nd;
842 bool first = true;
843
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300844 if (browser->nr_entries == 0)
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -0300845 return;
846
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300847 ui_browser__hists_init_top(browser);
Arnaldo Carvalho de Melo437cfe72011-10-14 09:31:53 -0300848
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300849 switch (whence) {
850 case SEEK_SET:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300851 nd = hists__filter_entries(rb_first(browser->entries));
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300852 break;
853 case SEEK_CUR:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300854 nd = browser->top;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300855 goto do_offset;
856 case SEEK_END:
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300857 nd = hists__filter_prev_entries(rb_last(browser->entries));
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300858 first = false;
859 break;
860 default:
861 return;
862 }
863
864 /*
865 * Moves not relative to the first visible entry invalidates its
866 * row_offset:
867 */
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300868 h = rb_entry(browser->top, struct hist_entry, rb_node);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300869 h->row_offset = 0;
870
871 /*
872 * Here we have to check if nd is expanded (+), if it is we can't go
873 * the next top level hist_entry, instead we must compute an offset of
874 * what _not_ to show and not change the first visible entry.
875 *
876 * This offset increments when we are going from top to bottom and
877 * decreases when we're going from bottom to top.
878 *
879 * As we don't have backpointers to the top level in the callchains
880 * structure, we need to always print the whole hist_entry callchain,
881 * skipping the first ones that are before the first visible entry
882 * and stop when we printed enough lines to fill the screen.
883 */
884do_offset:
885 if (offset > 0) {
886 do {
887 h = rb_entry(nd, struct hist_entry, rb_node);
888 if (h->ms.unfolded) {
889 u16 remaining = h->nr_rows - h->row_offset;
890 if (offset > remaining) {
891 offset -= remaining;
892 h->row_offset = 0;
893 } else {
894 h->row_offset += offset;
895 offset = 0;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300896 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300897 break;
898 }
899 }
900 nd = hists__filter_entries(rb_next(nd));
901 if (nd == NULL)
902 break;
903 --offset;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300904 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300905 } while (offset != 0);
906 } else if (offset < 0) {
907 while (1) {
908 h = rb_entry(nd, struct hist_entry, rb_node);
909 if (h->ms.unfolded) {
910 if (first) {
911 if (-offset > h->row_offset) {
912 offset += h->row_offset;
913 h->row_offset = 0;
914 } else {
915 h->row_offset += offset;
916 offset = 0;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300917 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300918 break;
919 }
920 } else {
921 if (-offset > h->nr_rows) {
922 offset += h->nr_rows;
923 h->row_offset = 0;
924 } else {
925 h->row_offset = h->nr_rows + offset;
926 offset = 0;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300927 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300928 break;
929 }
930 }
931 }
932
933 nd = hists__filter_prev_entries(rb_prev(nd));
934 if (nd == NULL)
935 break;
936 ++offset;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300937 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300938 if (offset == 0) {
939 /*
940 * Last unfiltered hist_entry, check if it is
941 * unfolded, if it is then we should have
942 * row_offset at its last entry.
943 */
944 h = rb_entry(nd, struct hist_entry, rb_node);
945 if (h->ms.unfolded)
946 h->row_offset = h->nr_rows;
947 break;
948 }
949 first = false;
950 }
951 } else {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -0300952 browser->top = nd;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -0300953 h = rb_entry(nd, struct hist_entry, rb_node);
954 h->row_offset = 0;
955 }
956}
957
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -0300958static int hist_browser__fprintf_callchain_node_rb_tree(struct hist_browser *browser,
959 struct callchain_node *chain_node,
960 u64 total, int level,
961 FILE *fp)
962{
963 struct rb_node *node;
964 int offset = level * LEVEL_OFFSET_STEP;
965 u64 new_total, remaining;
966 int printed = 0;
967
968 if (callchain_param.mode == CHAIN_GRAPH_REL)
969 new_total = chain_node->children_hit;
970 else
971 new_total = total;
972
973 remaining = new_total;
974 node = rb_first(&chain_node->rb_root);
975 while (node) {
976 struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node);
977 struct rb_node *next = rb_next(node);
978 u64 cumul = callchain_cumul_hits(child);
979 struct callchain_list *chain;
980 char folded_sign = ' ';
981 int first = true;
982 int extra_offset = 0;
983
984 remaining -= cumul;
985
986 list_for_each_entry(chain, &child->val, list) {
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -0300987 char bf[1024], *alloc_str;
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -0300988 const char *str;
989 bool was_first = first;
990
991 if (first)
992 first = false;
993 else
994 extra_offset = LEVEL_OFFSET_STEP;
995
996 folded_sign = callchain_list__folded(chain);
997
998 alloc_str = NULL;
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -0300999 str = callchain_list__sym_name(chain, bf, sizeof(bf),
1000 browser->show_dso);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001001 if (was_first) {
1002 double percent = cumul * 100.0 / new_total;
1003
1004 if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0)
1005 str = "Not enough memory!";
1006 else
1007 str = alloc_str;
1008 }
1009
1010 printed += fprintf(fp, "%*s%c %s\n", offset + extra_offset, " ", folded_sign, str);
1011 free(alloc_str);
1012 if (folded_sign == '+')
1013 break;
1014 }
1015
1016 if (folded_sign == '-') {
1017 const int new_level = level + (extra_offset ? 2 : 1);
1018 printed += hist_browser__fprintf_callchain_node_rb_tree(browser, child, new_total,
1019 new_level, fp);
1020 }
1021
1022 node = next;
1023 }
1024
1025 return printed;
1026}
1027
1028static int hist_browser__fprintf_callchain_node(struct hist_browser *browser,
1029 struct callchain_node *node,
1030 int level, FILE *fp)
1031{
1032 struct callchain_list *chain;
1033 int offset = level * LEVEL_OFFSET_STEP;
1034 char folded_sign = ' ';
1035 int printed = 0;
1036
1037 list_for_each_entry(chain, &node->val, list) {
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -03001038 char bf[1024], *s;
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001039
1040 folded_sign = callchain_list__folded(chain);
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -03001041 s = callchain_list__sym_name(chain, bf, sizeof(bf), browser->show_dso);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001042 printed += fprintf(fp, "%*s%c %s\n", offset, " ", folded_sign, s);
1043 }
1044
1045 if (folded_sign == '-')
1046 printed += hist_browser__fprintf_callchain_node_rb_tree(browser, node,
1047 browser->hists->stats.total_period,
1048 level + 1, fp);
1049 return printed;
1050}
1051
1052static int hist_browser__fprintf_callchain(struct hist_browser *browser,
1053 struct rb_root *chain, int level, FILE *fp)
1054{
1055 struct rb_node *nd;
1056 int printed = 0;
1057
1058 for (nd = rb_first(chain); nd; nd = rb_next(nd)) {
1059 struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node);
1060
1061 printed += hist_browser__fprintf_callchain_node(browser, node, level, fp);
1062 }
1063
1064 return printed;
1065}
1066
1067static int hist_browser__fprintf_entry(struct hist_browser *browser,
1068 struct hist_entry *he, FILE *fp)
1069{
1070 char s[8192];
1071 double percent;
1072 int printed = 0;
1073 char folded_sign = ' ';
1074
1075 if (symbol_conf.use_callchain)
1076 folded_sign = hist_entry__folded(he);
1077
Namhyung Kim000078b2012-08-20 13:52:06 +09001078 hist_entry__sort_snprintf(he, s, sizeof(s), browser->hists);
Namhyung Kimb24c28f2012-10-04 21:49:41 +09001079 percent = (he->stat.period * 100.0) / browser->hists->stats.total_period;
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001080
1081 if (symbol_conf.use_callchain)
1082 printed += fprintf(fp, "%c ", folded_sign);
1083
1084 printed += fprintf(fp, " %5.2f%%", percent);
1085
1086 if (symbol_conf.show_nr_samples)
Namhyung Kimb24c28f2012-10-04 21:49:41 +09001087 printed += fprintf(fp, " %11u", he->stat.nr_events);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001088
1089 if (symbol_conf.show_total_period)
Namhyung Kimb24c28f2012-10-04 21:49:41 +09001090 printed += fprintf(fp, " %12" PRIu64, he->stat.period);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001091
1092 printed += fprintf(fp, "%s\n", rtrim(s));
1093
1094 if (folded_sign == '-')
1095 printed += hist_browser__fprintf_callchain(browser, &he->sorted_chain, 1, fp);
1096
1097 return printed;
1098}
1099
1100static int hist_browser__fprintf(struct hist_browser *browser, FILE *fp)
1101{
1102 struct rb_node *nd = hists__filter_entries(rb_first(browser->b.entries));
1103 int printed = 0;
1104
1105 while (nd) {
1106 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1107
1108 printed += hist_browser__fprintf_entry(browser, h, fp);
1109 nd = hists__filter_entries(rb_next(nd));
1110 }
1111
1112 return printed;
1113}
1114
1115static int hist_browser__dump(struct hist_browser *browser)
1116{
1117 char filename[64];
1118 FILE *fp;
1119
1120 while (1) {
1121 scnprintf(filename, sizeof(filename), "perf.hist.%d", browser->print_seq);
1122 if (access(filename, F_OK))
1123 break;
1124 /*
1125 * XXX: Just an arbitrary lazy upper limit
1126 */
1127 if (++browser->print_seq == 8192) {
1128 ui_helpline__fpush("Too many perf.hist.N files, nothing written!");
1129 return -1;
1130 }
1131 }
1132
1133 fp = fopen(filename, "w");
1134 if (fp == NULL) {
1135 char bf[64];
Kirill A. Shutemov4cc49d42012-07-24 00:06:54 +03001136 const char *err = strerror_r(errno, bf, sizeof(bf));
1137 ui_helpline__fpush("Couldn't write to %s: %s", filename, err);
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001138 return -1;
1139 }
1140
1141 ++browser->print_seq;
1142 hist_browser__fprintf(browser, fp);
1143 fclose(fp);
1144 ui_helpline__fpush("%s written!", filename);
1145
1146 return 0;
1147}
1148
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001149static struct hist_browser *hist_browser__new(struct hists *hists)
1150{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001151 struct hist_browser *browser = zalloc(sizeof(*browser));
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001152
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001153 if (browser) {
1154 browser->hists = hists;
1155 browser->b.refresh = hist_browser__refresh;
1156 browser->b.seek = ui_browser__hists_seek;
1157 browser->b.use_navkeypressed = true;
Namhyung Kim55369fc2013-04-01 20:35:20 +09001158 if (sort__mode == SORT_MODE__BRANCH)
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001159 browser->has_symbols = sort_sym_from.list.next != NULL;
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001160 else
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001161 browser->has_symbols = sort_sym.list.next != NULL;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001162 }
1163
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001164 return browser;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001165}
1166
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001167static void hist_browser__delete(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001168{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001169 free(browser);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001170}
1171
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001172static struct hist_entry *hist_browser__selected_entry(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001173{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001174 return browser->he_selection;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001175}
1176
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001177static struct thread *hist_browser__selected_thread(struct hist_browser *browser)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001178{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001179 return browser->he_selection->thread;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001180}
1181
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001182static int hists__browser_title(struct hists *hists, char *bf, size_t size,
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001183 const char *ev_name)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001184{
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -03001185 char unit;
1186 int printed;
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001187 const struct dso *dso = hists->dso_filter;
1188 const struct thread *thread = hists->thread_filter;
1189 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
1190 u64 nr_events = hists->stats.total_period;
Namhyung Kim717e2632013-01-22 18:09:44 +09001191 struct perf_evsel *evsel = hists_to_evsel(hists);
1192 char buf[512];
1193 size_t buflen = sizeof(buf);
1194
Namhyung Kim759ff492013-03-05 14:53:26 +09001195 if (perf_evsel__is_group_event(evsel)) {
Namhyung Kim717e2632013-01-22 18:09:44 +09001196 struct perf_evsel *pos;
1197
1198 perf_evsel__group_desc(evsel, buf, buflen);
1199 ev_name = buf;
1200
1201 for_each_group_member(pos, evsel) {
1202 nr_samples += pos->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1203 nr_events += pos->hists.stats.total_period;
1204 }
1205 }
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -03001206
Ashay Ranecc6862802012-04-05 21:01:01 -05001207 nr_samples = convert_unit(nr_samples, &unit);
1208 printed = scnprintf(bf, size,
1209 "Samples: %lu%c of event '%s', Event count (approx.): %lu",
1210 nr_samples, unit, ev_name, nr_events);
1211
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001212
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001213 if (hists->uid_filter_str)
Arnaldo Carvalho de Melo0d37aa32012-01-19 14:08:15 -02001214 printed += snprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001215 ", UID: %s", hists->uid_filter_str);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001216 if (thread)
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -03001217 printed += scnprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -03001218 ", Thread: %s(%d)",
1219 (thread->comm_set ? thread->comm : ""),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001220 thread->pid);
1221 if (dso)
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -03001222 printed += scnprintf(bf + printed, size - printed,
Arnaldo Carvalho de Melo469917c2010-09-13 10:25:04 -03001223 ", DSO: %s", dso->short_name);
1224 return printed;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001225}
1226
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001227static inline void free_popup_options(char **options, int n)
1228{
1229 int i;
1230
1231 for (i = 0; i < n; ++i) {
1232 free(options[i]);
1233 options[i] = NULL;
1234 }
1235}
1236
Feng Tangc77d8d72012-11-01 00:00:55 +08001237/* Check whether the browser is for 'top' or 'report' */
1238static inline bool is_report_browser(void *timer)
1239{
1240 return timer == NULL;
1241}
1242
Feng Tang341487ab2013-02-03 14:38:20 +08001243/*
1244 * Only runtime switching of perf data file will make "input_name" point
1245 * to a malloced buffer. So add "is_input_name_malloced" flag to decide
1246 * whether we need to call free() for current "input_name" during the switch.
1247 */
1248static bool is_input_name_malloced = false;
1249
1250static int switch_data_file(void)
1251{
1252 char *pwd, *options[32], *abs_path[32], *tmp;
1253 DIR *pwd_dir;
1254 int nr_options = 0, choice = -1, ret = -1;
1255 struct dirent *dent;
1256
1257 pwd = getenv("PWD");
1258 if (!pwd)
1259 return ret;
1260
1261 pwd_dir = opendir(pwd);
1262 if (!pwd_dir)
1263 return ret;
1264
1265 memset(options, 0, sizeof(options));
1266 memset(options, 0, sizeof(abs_path));
1267
1268 while ((dent = readdir(pwd_dir))) {
1269 char path[PATH_MAX];
1270 u64 magic;
1271 char *name = dent->d_name;
1272 FILE *file;
1273
1274 if (!(dent->d_type == DT_REG))
1275 continue;
1276
1277 snprintf(path, sizeof(path), "%s/%s", pwd, name);
1278
1279 file = fopen(path, "r");
1280 if (!file)
1281 continue;
1282
1283 if (fread(&magic, 1, 8, file) < 8)
1284 goto close_file_and_continue;
1285
1286 if (is_perf_magic(magic)) {
1287 options[nr_options] = strdup(name);
1288 if (!options[nr_options])
1289 goto close_file_and_continue;
1290
1291 abs_path[nr_options] = strdup(path);
1292 if (!abs_path[nr_options]) {
1293 free(options[nr_options]);
1294 ui__warning("Can't search all data files due to memory shortage.\n");
1295 fclose(file);
1296 break;
1297 }
1298
1299 nr_options++;
1300 }
1301
1302close_file_and_continue:
1303 fclose(file);
1304 if (nr_options >= 32) {
1305 ui__warning("Too many perf data files in PWD!\n"
1306 "Only the first 32 files will be listed.\n");
1307 break;
1308 }
1309 }
1310 closedir(pwd_dir);
1311
1312 if (nr_options) {
1313 choice = ui__popup_menu(nr_options, options);
1314 if (choice < nr_options && choice >= 0) {
1315 tmp = strdup(abs_path[choice]);
1316 if (tmp) {
1317 if (is_input_name_malloced)
1318 free((void *)input_name);
1319 input_name = tmp;
1320 is_input_name_malloced = true;
1321 ret = 0;
1322 } else
1323 ui__warning("Data switch failed due to memory shortage!\n");
1324 }
1325 }
1326
1327 free_popup_options(options, nr_options);
1328 free_popup_options(abs_path, nr_options);
1329 return ret;
1330}
1331
1332
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001333static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events,
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001334 const char *helpline, const char *ev_name,
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001335 bool left_exits,
Namhyung Kim68d80752012-11-02 14:50:06 +09001336 struct hist_browser_timer *hbt,
1337 struct perf_session_env *env)
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001338{
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001339 struct hists *hists = &evsel->hists;
1340 struct hist_browser *browser = hist_browser__new(hists);
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001341 struct branch_info *bi;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001342 struct pstack *fstack;
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001343 char *options[16];
1344 int nr_options = 0;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001345 int key = -1;
Namhyung Kim938a23a2012-03-16 17:50:53 +09001346 char buf[64];
Feng Tangcdbab7c2012-10-30 11:56:06 +08001347 char script_opt[64];
Namhyung Kim9783adf2012-11-02 14:50:05 +09001348 int delay_secs = hbt ? hbt->refresh : 0;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001349
1350 if (browser == NULL)
1351 return -1;
1352
1353 fstack = pstack__new(2);
1354 if (fstack == NULL)
1355 goto out;
1356
1357 ui_helpline__push(helpline);
1358
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001359 memset(options, 0, sizeof(options));
1360
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001361 while (1) {
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -03001362 const struct thread *thread = NULL;
1363 const struct dso *dso = NULL;
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001364 int choice = 0,
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001365 annotate = -2, zoom_dso = -2, zoom_thread = -2,
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001366 annotate_f = -2, annotate_t = -2, browse_map = -2;
Feng Tang341487ab2013-02-03 14:38:20 +08001367 int scripts_comm = -2, scripts_symbol = -2,
1368 scripts_all = -2, switch_data = -2;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001369
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001370 nr_options = 0;
1371
Namhyung Kim9783adf2012-11-02 14:50:05 +09001372 key = hist_browser__run(browser, ev_name, hbt);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001373
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -03001374 if (browser->he_selection != NULL) {
1375 thread = hist_browser__selected_thread(browser);
1376 dso = browser->selection->map ? browser->selection->map->dso : NULL;
1377 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001378 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001379 case K_TAB:
1380 case K_UNTAB:
David Aherne4419b82011-10-19 11:37:47 -06001381 if (nr_events == 1)
1382 continue;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001383 /*
1384 * Exit the browser, let hists__browser_tree
1385 * go to the next or previous
1386 */
1387 goto out_free_stack;
1388 case 'a':
Arnaldo Carvalho de Meloa6e51f92011-10-21 10:58:24 -02001389 if (!browser->has_symbols) {
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001390 ui_browser__warning(&browser->b, delay_secs * 2,
Arnaldo Carvalho de Meloa6e51f92011-10-21 10:58:24 -02001391 "Annotation is only available for symbolic views, "
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001392 "include \"sym*\" in --sort to use it.");
Arnaldo Carvalho de Meloa6e51f92011-10-21 10:58:24 -02001393 continue;
1394 }
1395
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -03001396 if (browser->selection == NULL ||
Lin Mingdb9a9cbc2011-04-08 14:31:26 +08001397 browser->selection->sym == NULL ||
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001398 browser->selection->map->dso->annotate_warned)
1399 continue;
1400 goto do_annotate;
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001401 case 'P':
1402 hist_browser__dump(browser);
1403 continue;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001404 case 'd':
1405 goto zoom_dso;
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -03001406 case 'V':
1407 browser->show_dso = !browser->show_dso;
1408 continue;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001409 case 't':
1410 goto zoom_thread;
Arnaldo Carvalho de Melo5a5626b2012-05-09 12:21:22 -03001411 case '/':
Namhyung Kim938a23a2012-03-16 17:50:53 +09001412 if (ui_browser__input_window("Symbol to show",
1413 "Please enter the name of symbol you want to see",
1414 buf, "ENTER: OK, ESC: Cancel",
1415 delay_secs * 2) == K_ENTER) {
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001416 hists->symbol_filter_str = *buf ? buf : NULL;
1417 hists__filter_by_symbol(hists);
Namhyung Kim938a23a2012-03-16 17:50:53 +09001418 hist_browser__reset(browser);
1419 }
1420 continue;
Feng Tangcdbab7c2012-10-30 11:56:06 +08001421 case 'r':
Namhyung Kim9783adf2012-11-02 14:50:05 +09001422 if (is_report_browser(hbt))
Feng Tangc77d8d72012-11-01 00:00:55 +08001423 goto do_scripts;
1424 continue;
Feng Tang341487ab2013-02-03 14:38:20 +08001425 case 's':
1426 if (is_report_browser(hbt))
1427 goto do_data_switch;
1428 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001429 case K_F1:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001430 case 'h':
1431 case '?':
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001432 ui_browser__help_window(&browser->b,
1433 "h/?/F1 Show this window\n"
Arnaldo Carvalho de Melo2d5646c2011-10-18 13:02:52 -02001434 "UP/DOWN/PGUP\n"
1435 "PGDN/SPACE Navigate\n"
1436 "q/ESC/CTRL+C Exit browser\n\n"
1437 "For multiple event sessions:\n\n"
1438 "TAB/UNTAB Switch events\n\n"
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -03001439 "For symbolic views (--sort has sym):\n\n"
Arnaldo Carvalho de Melo2d5646c2011-10-18 13:02:52 -02001440 "-> Zoom into DSO/Threads & Annotate current symbol\n"
1441 "<- Zoom out\n"
1442 "a Annotate current symbol\n"
1443 "C Collapse all callchains\n"
1444 "E Expand all callchains\n"
1445 "d Zoom into current DSO\n"
Namhyung Kim938a23a2012-03-16 17:50:53 +09001446 "t Zoom into current Thread\n"
Feng Tangc77d8d72012-11-01 00:00:55 +08001447 "r Run available scripts('perf report' only)\n"
Feng Tang341487ab2013-02-03 14:38:20 +08001448 "s Switch to another data file in PWD ('perf report' only)\n"
Arnaldo Carvalho de Meloaff3f3f2012-06-07 19:31:28 -03001449 "P Print histograms to perf.hist.N\n"
Arnaldo Carvalho de Meloa7cb8862012-08-03 13:53:40 -03001450 "V Verbose (DSO names in callchains, etc)\n"
Arnaldo Carvalho de Melo5a5626b2012-05-09 12:21:22 -03001451 "/ Filter symbol by name");
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001452 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001453 case K_ENTER:
1454 case K_RIGHT:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001455 /* menu */
1456 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001457 case K_LEFT: {
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001458 const void *top;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001459
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001460 if (pstack__empty(fstack)) {
1461 /*
1462 * Go back to the perf_evsel_menu__run or other user
1463 */
1464 if (left_exits)
1465 goto out_free_stack;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001466 continue;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001467 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001468 top = pstack__pop(fstack);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001469 if (top == &browser->hists->dso_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001470 goto zoom_out_dso;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001471 if (top == &browser->hists->thread_filter)
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001472 goto zoom_out_thread;
1473 continue;
1474 }
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001475 case K_ESC:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001476 if (!left_exits &&
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001477 !ui_browser__dialog_yesno(&browser->b,
1478 "Do you really want to exit?"))
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001479 continue;
1480 /* Fall thru */
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -03001481 case 'q':
1482 case CTRL('c'):
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -03001483 goto out_free_stack;
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -03001484 default:
1485 continue;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001486 }
1487
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -03001488 if (!browser->has_symbols)
1489 goto add_exit_option;
1490
Namhyung Kim55369fc2013-04-01 20:35:20 +09001491 if (sort__mode == SORT_MODE__BRANCH) {
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001492 bi = browser->he_selection->branch_info;
1493 if (browser->selection != NULL &&
1494 bi &&
1495 bi->from.sym != NULL &&
1496 !bi->from.map->dso->annotate_warned &&
1497 asprintf(&options[nr_options], "Annotate %s",
1498 bi->from.sym->name) > 0)
1499 annotate_f = nr_options++;
1500
1501 if (browser->selection != NULL &&
1502 bi &&
1503 bi->to.sym != NULL &&
1504 !bi->to.map->dso->annotate_warned &&
Stephane Eranian8bcd65f2012-03-12 16:13:29 +01001505 (bi->to.sym != bi->from.sym ||
1506 bi->to.map->dso != bi->from.map->dso) &&
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001507 asprintf(&options[nr_options], "Annotate %s",
1508 bi->to.sym->name) > 0)
1509 annotate_t = nr_options++;
1510 } else {
1511
1512 if (browser->selection != NULL &&
1513 browser->selection->sym != NULL &&
1514 !browser->selection->map->dso->annotate_warned &&
1515 asprintf(&options[nr_options], "Annotate %s",
1516 browser->selection->sym->name) > 0)
1517 annotate = nr_options++;
1518 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001519
1520 if (thread != NULL &&
1521 asprintf(&options[nr_options], "Zoom %s %s(%d) thread",
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001522 (browser->hists->thread_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001523 (thread->comm_set ? thread->comm : ""),
1524 thread->pid) > 0)
1525 zoom_thread = nr_options++;
1526
1527 if (dso != NULL &&
1528 asprintf(&options[nr_options], "Zoom %s %s DSO",
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001529 (browser->hists->dso_filter ? "out of" : "into"),
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001530 (dso->kernel ? "the Kernel" : dso->short_name)) > 0)
1531 zoom_dso = nr_options++;
1532
Arnaldo Carvalho de Melo60098912011-03-04 21:19:21 -03001533 if (browser->selection != NULL &&
1534 browser->selection->map != NULL &&
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001535 asprintf(&options[nr_options], "Browse map details") > 0)
1536 browse_map = nr_options++;
Feng Tangcdbab7c2012-10-30 11:56:06 +08001537
1538 /* perf script support */
1539 if (browser->he_selection) {
1540 struct symbol *sym;
1541
1542 if (asprintf(&options[nr_options], "Run scripts for samples of thread [%s]",
1543 browser->he_selection->thread->comm) > 0)
1544 scripts_comm = nr_options++;
1545
1546 sym = browser->he_selection->ms.sym;
1547 if (sym && sym->namelen &&
1548 asprintf(&options[nr_options], "Run scripts for samples of symbol [%s]",
1549 sym->name) > 0)
1550 scripts_symbol = nr_options++;
1551 }
1552
1553 if (asprintf(&options[nr_options], "Run scripts for all samples") > 0)
1554 scripts_all = nr_options++;
1555
Feng Tang341487ab2013-02-03 14:38:20 +08001556 if (is_report_browser(hbt) && asprintf(&options[nr_options],
1557 "Switch to another data file in PWD") > 0)
1558 switch_data = nr_options++;
Arnaldo Carvalho de Melo724c9c92011-10-06 10:36:35 -03001559add_exit_option:
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001560 options[nr_options++] = (char *)"Exit";
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001561retry_popup_menu:
Arnaldo Carvalho de Melo1e6dd072010-08-10 15:58:50 -03001562 choice = ui__popup_menu(nr_options, options);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001563
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001564 if (choice == nr_options - 1)
1565 break;
1566
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001567 if (choice == -1) {
1568 free_popup_options(options, nr_options - 1);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001569 continue;
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001570 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001571
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001572 if (choice == annotate || choice == annotate_t || choice == annotate_f) {
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001573 struct hist_entry *he;
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001574 int err;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001575do_annotate:
Namhyung Kim68d80752012-11-02 14:50:06 +09001576 if (!objdump_path && perf_session_env__lookup_objdump(env))
1577 continue;
1578
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001579 he = hist_browser__selected_entry(browser);
1580 if (he == NULL)
1581 continue;
Stephane Eraniana68c2c52012-03-08 23:47:48 +01001582
1583 /*
1584 * we stash the branch_info symbol + map into the
1585 * the ms so we don't have to rewrite all the annotation
1586 * code to use branch_info.
1587 * in branch mode, the ms struct is not used
1588 */
1589 if (choice == annotate_f) {
1590 he->ms.sym = he->branch_info->from.sym;
1591 he->ms.map = he->branch_info->from.map;
1592 } else if (choice == annotate_t) {
1593 he->ms.sym = he->branch_info->to.sym;
1594 he->ms.map = he->branch_info->to.map;
1595 }
1596
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -03001597 /*
1598 * Don't let this be freed, say, by hists__decay_entry.
1599 */
1600 he->used = true;
Namhyung Kimdb8fd072013-03-05 14:53:21 +09001601 err = hist_entry__tui_annotate(he, evsel, hbt);
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -03001602 he->used = false;
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001603 /*
1604 * offer option to annotate the other branch source or target
1605 * (if they exists) when returning from annotate
1606 */
1607 if ((err == 'q' || err == CTRL('c'))
1608 && annotate_t != -2 && annotate_f != -2)
1609 goto retry_popup_menu;
1610
Arnaldo Carvalho de Melo900e14a2011-10-11 16:15:39 -03001611 ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries);
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001612 if (err)
1613 ui_browser__handle_resize(&browser->b);
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001614
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001615 } else if (choice == browse_map)
1616 map__browse(browser->selection->map);
1617 else if (choice == zoom_dso) {
1618zoom_dso:
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001619 if (browser->hists->dso_filter) {
1620 pstack__remove(fstack, &browser->hists->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001621zoom_out_dso:
1622 ui_helpline__pop();
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001623 browser->hists->dso_filter = NULL;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001624 sort_dso.elide = false;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001625 } else {
1626 if (dso == NULL)
1627 continue;
1628 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"",
1629 dso->kernel ? "the Kernel" : dso->short_name);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001630 browser->hists->dso_filter = dso;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001631 sort_dso.elide = true;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001632 pstack__push(fstack, &browser->hists->dso_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001633 }
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001634 hists__filter_by_dso(hists);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001635 hist_browser__reset(browser);
1636 } else if (choice == zoom_thread) {
1637zoom_thread:
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001638 if (browser->hists->thread_filter) {
1639 pstack__remove(fstack, &browser->hists->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001640zoom_out_thread:
1641 ui_helpline__pop();
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001642 browser->hists->thread_filter = NULL;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001643 sort_thread.elide = false;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001644 } else {
1645 ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"",
1646 thread->comm_set ? thread->comm : "",
1647 thread->pid);
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001648 browser->hists->thread_filter = thread;
Arnaldo Carvalho de Melocc02c922011-10-20 08:02:30 -02001649 sort_thread.elide = true;
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001650 pstack__push(fstack, &browser->hists->thread_filter);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001651 }
Arnaldo Carvalho de Melo05e8b082012-05-29 22:42:18 -03001652 hists__filter_by_thread(hists);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001653 hist_browser__reset(browser);
1654 }
Feng Tangcdbab7c2012-10-30 11:56:06 +08001655 /* perf scripts support */
1656 else if (choice == scripts_all || choice == scripts_comm ||
1657 choice == scripts_symbol) {
1658do_scripts:
1659 memset(script_opt, 0, 64);
1660
1661 if (choice == scripts_comm)
1662 sprintf(script_opt, " -c %s ", browser->he_selection->thread->comm);
1663
1664 if (choice == scripts_symbol)
1665 sprintf(script_opt, " -S %s ", browser->he_selection->ms.sym->name);
1666
1667 script_browse(script_opt);
1668 }
Feng Tang341487ab2013-02-03 14:38:20 +08001669 /* Switch to another data file */
1670 else if (choice == switch_data) {
1671do_data_switch:
1672 if (!switch_data_file()) {
1673 key = K_SWITCH_INPUT_DATA;
1674 break;
1675 } else
1676 ui__warning("Won't switch the data files due to\n"
1677 "no valid data file get selected!\n");
1678 }
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001679 }
1680out_free_stack:
1681 pstack__delete(fstack);
1682out:
1683 hist_browser__delete(browser);
Stephane Eranian24bff2d2012-03-12 16:13:30 +01001684 free_popup_options(options, nr_options - 1);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001685 return key;
1686}
1687
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001688struct perf_evsel_menu {
1689 struct ui_browser b;
1690 struct perf_evsel *selection;
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001691 bool lost_events, lost_events_warned;
Namhyung Kim68d80752012-11-02 14:50:06 +09001692 struct perf_session_env *env;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001693};
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001694
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001695static void perf_evsel_menu__write(struct ui_browser *browser,
1696 void *entry, int row)
1697{
1698 struct perf_evsel_menu *menu = container_of(browser,
1699 struct perf_evsel_menu, b);
1700 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
1701 bool current_entry = ui_browser__is_current_entry(browser, row);
1702 unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE];
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03001703 const char *ev_name = perf_evsel__name(evsel);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001704 char bf[256], unit;
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001705 const char *warn = " ";
1706 size_t printed;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001707
1708 ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED :
1709 HE_COLORSET_NORMAL);
1710
Namhyung Kim759ff492013-03-05 14:53:26 +09001711 if (perf_evsel__is_group_event(evsel)) {
Namhyung Kim717e2632013-01-22 18:09:44 +09001712 struct perf_evsel *pos;
1713
1714 ev_name = perf_evsel__group_name(evsel);
1715
1716 for_each_group_member(pos, evsel) {
1717 nr_events += pos->hists.stats.nr_events[PERF_RECORD_SAMPLE];
1718 }
1719 }
1720
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001721 nr_events = convert_unit(nr_events, &unit);
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -03001722 printed = scnprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events,
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001723 unit, unit == ' ' ? "" : " ", ev_name);
1724 slsmg_printf("%s", bf);
1725
1726 nr_events = evsel->hists.stats.nr_events[PERF_RECORD_LOST];
1727 if (nr_events != 0) {
1728 menu->lost_events = true;
1729 if (!current_entry)
1730 ui_browser__set_color(browser, HE_COLORSET_TOP);
1731 nr_events = convert_unit(nr_events, &unit);
Arnaldo Carvalho de Meloe7f01d12012-03-14 12:29:29 -03001732 printed += scnprintf(bf, sizeof(bf), ": %ld%c%schunks LOST!",
1733 nr_events, unit, unit == ' ' ? "" : " ");
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001734 warn = bf;
1735 }
1736
1737 slsmg_write_nstring(warn, browser->width - printed);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001738
1739 if (current_entry)
1740 menu->selection = evsel;
1741}
1742
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001743static int perf_evsel_menu__run(struct perf_evsel_menu *menu,
1744 int nr_events, const char *help,
Namhyung Kim9783adf2012-11-02 14:50:05 +09001745 struct hist_browser_timer *hbt)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001746{
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001747 struct perf_evlist *evlist = menu->b.priv;
1748 struct perf_evsel *pos;
1749 const char *ev_name, *title = "Available samples";
Namhyung Kim9783adf2012-11-02 14:50:05 +09001750 int delay_secs = hbt ? hbt->refresh : 0;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001751 int key;
1752
1753 if (ui_browser__show(&menu->b, title,
1754 "ESC: exit, ENTER|->: Browse histograms") < 0)
1755 return -1;
1756
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001757 while (1) {
Arnaldo Carvalho de Melo3af6e332011-10-13 08:52:46 -03001758 key = ui_browser__run(&menu->b, delay_secs);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001759
1760 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001761 case K_TIMER:
Namhyung Kim9783adf2012-11-02 14:50:05 +09001762 hbt->timer(hbt->arg);
Arnaldo Carvalho de Melo7b275092011-10-29 12:15:04 -02001763
1764 if (!menu->lost_events_warned && menu->lost_events) {
1765 ui_browser__warn_lost_events(&menu->b);
1766 menu->lost_events_warned = true;
1767 }
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001768 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001769 case K_RIGHT:
1770 case K_ENTER:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001771 if (!menu->selection)
1772 continue;
1773 pos = menu->selection;
1774browse_hists:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001775 perf_evlist__set_selected(evlist, pos);
1776 /*
1777 * Give the calling tool a chance to populate the non
1778 * default evsel resorted hists tree.
1779 */
Namhyung Kim9783adf2012-11-02 14:50:05 +09001780 if (hbt)
1781 hbt->timer(hbt->arg);
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03001782 ev_name = perf_evsel__name(pos);
Arnaldo Carvalho de Melo34958542011-10-05 19:35:54 -03001783 key = perf_evsel__hists_browse(pos, nr_events, help,
Namhyung Kim68d80752012-11-02 14:50:06 +09001784 ev_name, true, hbt,
1785 menu->env);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001786 ui_browser__show_title(&menu->b, title);
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001787 switch (key) {
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001788 case K_TAB:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001789 if (pos->node.next == &evlist->entries)
1790 pos = list_entry(evlist->entries.next, struct perf_evsel, node);
1791 else
1792 pos = list_entry(pos->node.next, struct perf_evsel, node);
1793 goto browse_hists;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001794 case K_UNTAB:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001795 if (pos->node.prev == &evlist->entries)
1796 pos = list_entry(evlist->entries.prev, struct perf_evsel, node);
1797 else
1798 pos = list_entry(pos->node.prev, struct perf_evsel, node);
1799 goto browse_hists;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001800 case K_ESC:
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001801 if (!ui_browser__dialog_yesno(&menu->b,
1802 "Do you really want to exit?"))
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001803 continue;
1804 /* Fall thru */
Feng Tang341487ab2013-02-03 14:38:20 +08001805 case K_SWITCH_INPUT_DATA:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001806 case 'q':
1807 case CTRL('c'):
1808 goto out;
1809 default:
1810 continue;
1811 }
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001812 case K_LEFT:
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001813 continue;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -02001814 case K_ESC:
Arnaldo Carvalho de Melo4610e412011-10-26 12:04:37 -02001815 if (!ui_browser__dialog_yesno(&menu->b,
1816 "Do you really want to exit?"))
Arnaldo Carvalho de Meloed7e5662011-10-13 08:31:22 -03001817 continue;
1818 /* Fall thru */
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001819 case 'q':
1820 case CTRL('c'):
1821 goto out;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001822 default:
Arnaldo Carvalho de Melo18eaf0b2011-10-13 12:22:28 -03001823 continue;
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001824 }
1825 }
1826
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001827out:
1828 ui_browser__hide(&menu->b);
1829 return key;
1830}
1831
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09001832static bool filter_group_entries(struct ui_browser *self __maybe_unused,
1833 void *entry)
1834{
1835 struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node);
1836
1837 if (symbol_conf.event_group && !perf_evsel__is_group_leader(evsel))
1838 return true;
1839
1840 return false;
1841}
1842
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001843static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist,
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09001844 int nr_entries, const char *help,
Namhyung Kim68d80752012-11-02 14:50:06 +09001845 struct hist_browser_timer *hbt,
1846 struct perf_session_env *env)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001847{
1848 struct perf_evsel *pos;
1849 struct perf_evsel_menu menu = {
1850 .b = {
1851 .entries = &evlist->entries,
1852 .refresh = ui_browser__list_head_refresh,
1853 .seek = ui_browser__list_head_seek,
1854 .write = perf_evsel_menu__write,
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09001855 .filter = filter_group_entries,
1856 .nr_entries = nr_entries,
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001857 .priv = evlist,
1858 },
Namhyung Kim68d80752012-11-02 14:50:06 +09001859 .env = env,
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001860 };
1861
1862 ui_helpline__push("Press ESC to exit");
1863
1864 list_for_each_entry(pos, &evlist->entries, node) {
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03001865 const char *ev_name = perf_evsel__name(pos);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001866 size_t line_len = strlen(ev_name) + 7;
1867
1868 if (menu.b.width < line_len)
1869 menu.b.width = line_len;
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001870 }
1871
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09001872 return perf_evsel_menu__run(&menu, nr_entries, help, hbt);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001873}
1874
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -03001875int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help,
Namhyung Kim68d80752012-11-02 14:50:06 +09001876 struct hist_browser_timer *hbt,
1877 struct perf_session_env *env)
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001878{
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09001879 int nr_entries = evlist->nr_entries;
1880
1881single_entry:
1882 if (nr_entries == 1) {
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001883 struct perf_evsel *first = list_entry(evlist->entries.next,
1884 struct perf_evsel, node);
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -03001885 const char *ev_name = perf_evsel__name(first);
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09001886
1887 return perf_evsel__hists_browse(first, nr_entries, help,
Namhyung Kim68d80752012-11-02 14:50:06 +09001888 ev_name, false, hbt, env);
Arnaldo Carvalho de Melo7f0030b2011-03-06 13:07:30 -03001889 }
1890
Namhyung Kimfc24d7c2013-01-22 18:09:43 +09001891 if (symbol_conf.event_group) {
1892 struct perf_evsel *pos;
1893
1894 nr_entries = 0;
1895 list_for_each_entry(pos, &evlist->entries, node)
1896 if (perf_evsel__is_group_leader(pos))
1897 nr_entries++;
1898
1899 if (nr_entries == 1)
1900 goto single_entry;
1901 }
1902
1903 return __perf_evlist__tui_browse_hists(evlist, nr_entries, help,
1904 hbt, env);
Arnaldo Carvalho de Melod1b4f242010-08-10 15:49:07 -03001905}