Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 1 | #define _GNU_SOURCE |
| 2 | #include <stdio.h> |
| 3 | #undef _GNU_SOURCE |
| 4 | #include "../libslang.h" |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <newt.h> |
| 8 | #include <linux/rbtree.h> |
| 9 | |
Arnaldo Carvalho de Melo | e248de3 | 2011-03-05 21:40:06 -0300 | [diff] [blame] | 10 | #include "../../evsel.h" |
| 11 | #include "../../evlist.h" |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 12 | #include "../../hist.h" |
| 13 | #include "../../pstack.h" |
| 14 | #include "../../sort.h" |
| 15 | #include "../../util.h" |
| 16 | |
| 17 | #include "../browser.h" |
| 18 | #include "../helpline.h" |
| 19 | #include "../util.h" |
| 20 | #include "map.h" |
| 21 | |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 22 | struct hist_browser { |
| 23 | struct ui_browser b; |
| 24 | struct hists *hists; |
| 25 | struct hist_entry *he_selection; |
| 26 | struct map_symbol *selection; |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 27 | const struct thread *thread_filter; |
| 28 | const struct dso *dso_filter; |
Arnaldo Carvalho de Melo | 724c9c9 | 2011-10-06 10:36:35 -0300 | [diff] [blame] | 29 | bool has_symbols; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 30 | }; |
| 31 | |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 32 | static int hists__browser_title(struct hists *self, char *bf, size_t size, |
| 33 | const char *ev_name, const struct dso *dso, |
| 34 | const struct thread *thread); |
| 35 | |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 36 | static void hist_browser__refresh_dimensions(struct hist_browser *self) |
| 37 | { |
| 38 | /* 3 == +/- toggle symbol before actual hist_entry rendering */ |
| 39 | self->b.width = 3 + (hists__sort_list_width(self->hists) + |
| 40 | sizeof("[k]")); |
| 41 | } |
| 42 | |
| 43 | static void hist_browser__reset(struct hist_browser *self) |
| 44 | { |
| 45 | self->b.nr_entries = self->hists->nr_entries; |
| 46 | hist_browser__refresh_dimensions(self); |
| 47 | ui_browser__reset_index(&self->b); |
| 48 | } |
| 49 | |
| 50 | static char tree__folded_sign(bool unfolded) |
| 51 | { |
| 52 | return unfolded ? '-' : '+'; |
| 53 | } |
| 54 | |
| 55 | static char map_symbol__folded(const struct map_symbol *self) |
| 56 | { |
| 57 | return self->has_children ? tree__folded_sign(self->unfolded) : ' '; |
| 58 | } |
| 59 | |
| 60 | static char hist_entry__folded(const struct hist_entry *self) |
| 61 | { |
| 62 | return map_symbol__folded(&self->ms); |
| 63 | } |
| 64 | |
| 65 | static char callchain_list__folded(const struct callchain_list *self) |
| 66 | { |
| 67 | return map_symbol__folded(&self->ms); |
| 68 | } |
| 69 | |
Arnaldo Carvalho de Melo | 3c916cc | 2010-08-25 17:18:35 -0300 | [diff] [blame] | 70 | static void map_symbol__set_folding(struct map_symbol *self, bool unfold) |
| 71 | { |
| 72 | self->unfolded = unfold ? self->has_children : false; |
| 73 | } |
| 74 | |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 75 | static int callchain_node__count_rows_rb_tree(struct callchain_node *self) |
| 76 | { |
| 77 | int n = 0; |
| 78 | struct rb_node *nd; |
| 79 | |
| 80 | for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) { |
| 81 | 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 | |
| 100 | static 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 | |
| 117 | static 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 | |
| 130 | static bool map_symbol__toggle_fold(struct map_symbol *self) |
| 131 | { |
| 132 | if (!self->has_children) |
| 133 | return false; |
| 134 | |
| 135 | self->unfolded = !self->unfolded; |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | static void callchain_node__init_have_children_rb_tree(struct callchain_node *self) |
| 140 | { |
| 141 | struct rb_node *nd = rb_first(&self->rb_root); |
| 142 | |
| 143 | for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) { |
| 144 | struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node); |
| 145 | struct callchain_list *chain; |
Arnaldo Carvalho de Melo | 293db47 | 2010-08-25 16:05:36 -0300 | [diff] [blame] | 146 | bool first = true; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 147 | |
| 148 | list_for_each_entry(chain, &child->val, list) { |
| 149 | if (first) { |
| 150 | first = false; |
| 151 | chain->ms.has_children = chain->list.next != &child->val || |
Arnaldo Carvalho de Melo | 293db47 | 2010-08-25 16:05:36 -0300 | [diff] [blame] | 152 | !RB_EMPTY_ROOT(&child->rb_root); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 153 | } else |
| 154 | chain->ms.has_children = chain->list.next == &child->val && |
Arnaldo Carvalho de Melo | 293db47 | 2010-08-25 16:05:36 -0300 | [diff] [blame] | 155 | !RB_EMPTY_ROOT(&child->rb_root); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | callchain_node__init_have_children_rb_tree(child); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | static void callchain_node__init_have_children(struct callchain_node *self) |
| 163 | { |
| 164 | struct callchain_list *chain; |
| 165 | |
| 166 | list_for_each_entry(chain, &self->val, list) |
Arnaldo Carvalho de Melo | 293db47 | 2010-08-25 16:05:36 -0300 | [diff] [blame] | 167 | chain->ms.has_children = !RB_EMPTY_ROOT(&self->rb_root); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 168 | |
| 169 | callchain_node__init_have_children_rb_tree(self); |
| 170 | } |
| 171 | |
| 172 | static void callchain__init_have_children(struct rb_root *self) |
| 173 | { |
| 174 | struct rb_node *nd; |
| 175 | |
| 176 | for (nd = rb_first(self); nd; nd = rb_next(nd)) { |
| 177 | struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node); |
| 178 | callchain_node__init_have_children(node); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | static void hist_entry__init_have_children(struct hist_entry *self) |
| 183 | { |
| 184 | if (!self->init_have_children) { |
Arnaldo Carvalho de Melo | 18b308d | 2010-08-25 12:47:44 -0300 | [diff] [blame] | 185 | self->ms.has_children = !RB_EMPTY_ROOT(&self->sorted_chain); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 186 | callchain__init_have_children(&self->sorted_chain); |
| 187 | self->init_have_children = true; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static bool hist_browser__toggle_fold(struct hist_browser *self) |
| 192 | { |
| 193 | if (map_symbol__toggle_fold(self->selection)) { |
| 194 | struct hist_entry *he = self->he_selection; |
| 195 | |
| 196 | hist_entry__init_have_children(he); |
| 197 | self->hists->nr_entries -= he->nr_rows; |
| 198 | |
| 199 | if (he->ms.unfolded) |
| 200 | he->nr_rows = callchain__count_rows(&he->sorted_chain); |
| 201 | else |
| 202 | he->nr_rows = 0; |
| 203 | self->hists->nr_entries += he->nr_rows; |
| 204 | self->b.nr_entries = self->hists->nr_entries; |
| 205 | |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | /* If it doesn't have children, no toggling performed */ |
| 210 | return false; |
| 211 | } |
| 212 | |
Arnaldo Carvalho de Melo | 3c916cc | 2010-08-25 17:18:35 -0300 | [diff] [blame] | 213 | static int callchain_node__set_folding_rb_tree(struct callchain_node *self, bool unfold) |
| 214 | { |
| 215 | int n = 0; |
| 216 | struct rb_node *nd; |
| 217 | |
| 218 | for (nd = rb_first(&self->rb_root); nd; nd = rb_next(nd)) { |
| 219 | struct callchain_node *child = rb_entry(nd, struct callchain_node, rb_node); |
| 220 | struct callchain_list *chain; |
| 221 | bool has_children = false; |
| 222 | |
| 223 | list_for_each_entry(chain, &child->val, list) { |
| 224 | ++n; |
| 225 | map_symbol__set_folding(&chain->ms, unfold); |
| 226 | has_children = chain->ms.has_children; |
| 227 | } |
| 228 | |
| 229 | if (has_children) |
| 230 | n += callchain_node__set_folding_rb_tree(child, unfold); |
| 231 | } |
| 232 | |
| 233 | return n; |
| 234 | } |
| 235 | |
| 236 | static int callchain_node__set_folding(struct callchain_node *node, bool unfold) |
| 237 | { |
| 238 | struct callchain_list *chain; |
| 239 | bool has_children = false; |
| 240 | int n = 0; |
| 241 | |
| 242 | list_for_each_entry(chain, &node->val, list) { |
| 243 | ++n; |
| 244 | map_symbol__set_folding(&chain->ms, unfold); |
| 245 | has_children = chain->ms.has_children; |
| 246 | } |
| 247 | |
| 248 | if (has_children) |
| 249 | n += callchain_node__set_folding_rb_tree(node, unfold); |
| 250 | |
| 251 | return n; |
| 252 | } |
| 253 | |
| 254 | static int callchain__set_folding(struct rb_root *chain, bool unfold) |
| 255 | { |
| 256 | struct rb_node *nd; |
| 257 | int n = 0; |
| 258 | |
| 259 | for (nd = rb_first(chain); nd; nd = rb_next(nd)) { |
| 260 | struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node); |
| 261 | n += callchain_node__set_folding(node, unfold); |
| 262 | } |
| 263 | |
| 264 | return n; |
| 265 | } |
| 266 | |
| 267 | static void hist_entry__set_folding(struct hist_entry *self, bool unfold) |
| 268 | { |
| 269 | hist_entry__init_have_children(self); |
| 270 | map_symbol__set_folding(&self->ms, unfold); |
| 271 | |
| 272 | if (self->ms.has_children) { |
| 273 | int n = callchain__set_folding(&self->sorted_chain, unfold); |
| 274 | self->nr_rows = unfold ? n : 0; |
| 275 | } else |
| 276 | self->nr_rows = 0; |
| 277 | } |
| 278 | |
| 279 | static void hists__set_folding(struct hists *self, bool unfold) |
| 280 | { |
| 281 | struct rb_node *nd; |
| 282 | |
| 283 | self->nr_entries = 0; |
| 284 | |
| 285 | for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) { |
| 286 | struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node); |
| 287 | hist_entry__set_folding(he, unfold); |
| 288 | self->nr_entries += 1 + he->nr_rows; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | static void hist_browser__set_folding(struct hist_browser *self, bool unfold) |
| 293 | { |
| 294 | hists__set_folding(self->hists, unfold); |
| 295 | self->b.nr_entries = self->hists->nr_entries; |
| 296 | /* Go to the start, we may be way after valid entries after a collapse */ |
| 297 | ui_browser__reset_index(&self->b); |
| 298 | } |
| 299 | |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 300 | static int hist_browser__run(struct hist_browser *self, const char *ev_name, |
| 301 | void(*timer)(void *arg), void *arg, int delay_secs) |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 302 | { |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 303 | int key; |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 304 | char title[160]; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 305 | |
| 306 | self->b.entries = &self->hists->entries; |
| 307 | self->b.nr_entries = self->hists->nr_entries; |
| 308 | |
| 309 | hist_browser__refresh_dimensions(self); |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 310 | hists__browser_title(self->hists, title, sizeof(title), ev_name, |
| 311 | self->dso_filter, self->thread_filter); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 312 | |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 313 | if (ui_browser__show(&self->b, title, |
| 314 | "Press '?' for help on key bindings") < 0) |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 315 | return -1; |
| 316 | |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 317 | while (1) { |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 318 | key = ui_browser__run(&self->b, delay_secs); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 319 | |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 320 | switch (key) { |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 321 | case -1: |
| 322 | /* FIXME we need to check if it was es.reason == NEWT_EXIT_TIMER */ |
| 323 | timer(arg); |
Arnaldo Carvalho de Melo | 900e14a | 2011-10-11 16:15:39 -0300 | [diff] [blame] | 324 | ui_browser__update_nr_entries(&self->b, self->hists->nr_entries); |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 325 | hists__browser_title(self->hists, title, sizeof(title), |
| 326 | ev_name, self->dso_filter, |
| 327 | self->thread_filter); |
| 328 | ui_browser__show_title(&self->b, title); |
| 329 | continue; |
Arnaldo Carvalho de Melo | 4694153 | 2010-08-10 15:50:07 -0300 | [diff] [blame] | 330 | case 'D': { /* Debug */ |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 331 | static int seq; |
| 332 | struct hist_entry *h = rb_entry(self->b.top, |
| 333 | struct hist_entry, rb_node); |
| 334 | ui_helpline__pop(); |
| 335 | ui_helpline__fpush("%d: nr_ent=(%d,%d), height=%d, idx=%d, fve: idx=%d, row_off=%d, nrows=%d", |
| 336 | seq++, self->b.nr_entries, |
| 337 | self->hists->nr_entries, |
| 338 | self->b.height, |
| 339 | self->b.index, |
| 340 | self->b.top_idx, |
| 341 | h->row_offset, h->nr_rows); |
| 342 | } |
Arnaldo Carvalho de Melo | 3c916cc | 2010-08-25 17:18:35 -0300 | [diff] [blame] | 343 | break; |
| 344 | case 'C': |
| 345 | /* Collapse the whole world. */ |
| 346 | hist_browser__set_folding(self, false); |
| 347 | break; |
| 348 | case 'E': |
| 349 | /* Expand the whole world. */ |
| 350 | hist_browser__set_folding(self, true); |
| 351 | break; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 352 | case NEWT_KEY_ENTER: |
| 353 | if (hist_browser__toggle_fold(self)) |
| 354 | break; |
| 355 | /* fall thru */ |
| 356 | default: |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 357 | goto out; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 358 | } |
| 359 | } |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 360 | out: |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 361 | ui_browser__hide(&self->b); |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 362 | return key; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static char *callchain_list__sym_name(struct callchain_list *self, |
| 366 | char *bf, size_t bfsize) |
| 367 | { |
| 368 | if (self->ms.sym) |
| 369 | return self->ms.sym->name; |
| 370 | |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 371 | snprintf(bf, bfsize, "%#" PRIx64, self->ip); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 372 | return bf; |
| 373 | } |
| 374 | |
| 375 | #define LEVEL_OFFSET_STEP 3 |
| 376 | |
| 377 | static int hist_browser__show_callchain_node_rb_tree(struct hist_browser *self, |
| 378 | struct callchain_node *chain_node, |
| 379 | u64 total, int level, |
| 380 | unsigned short row, |
| 381 | off_t *row_offset, |
| 382 | bool *is_current_entry) |
| 383 | { |
| 384 | struct rb_node *node; |
| 385 | int first_row = row, width, offset = level * LEVEL_OFFSET_STEP; |
| 386 | u64 new_total, remaining; |
| 387 | |
| 388 | if (callchain_param.mode == CHAIN_GRAPH_REL) |
| 389 | new_total = chain_node->children_hit; |
| 390 | else |
| 391 | new_total = total; |
| 392 | |
| 393 | remaining = new_total; |
| 394 | node = rb_first(&chain_node->rb_root); |
| 395 | while (node) { |
| 396 | struct callchain_node *child = rb_entry(node, struct callchain_node, rb_node); |
| 397 | struct rb_node *next = rb_next(node); |
Frederic Weisbecker | f08c315 | 2011-01-14 04:51:59 +0100 | [diff] [blame] | 398 | u64 cumul = callchain_cumul_hits(child); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 399 | struct callchain_list *chain; |
| 400 | char folded_sign = ' '; |
| 401 | int first = true; |
| 402 | int extra_offset = 0; |
| 403 | |
| 404 | remaining -= cumul; |
| 405 | |
| 406 | list_for_each_entry(chain, &child->val, list) { |
| 407 | char ipstr[BITS_PER_LONG / 4 + 1], *alloc_str; |
| 408 | const char *str; |
| 409 | int color; |
| 410 | bool was_first = first; |
| 411 | |
Arnaldo Carvalho de Melo | 163caed | 2010-08-25 16:30:03 -0300 | [diff] [blame] | 412 | if (first) |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 413 | first = false; |
Arnaldo Carvalho de Melo | 163caed | 2010-08-25 16:30:03 -0300 | [diff] [blame] | 414 | else |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 415 | extra_offset = LEVEL_OFFSET_STEP; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 416 | |
| 417 | folded_sign = callchain_list__folded(chain); |
| 418 | if (*row_offset != 0) { |
| 419 | --*row_offset; |
| 420 | goto do_next; |
| 421 | } |
| 422 | |
| 423 | alloc_str = NULL; |
| 424 | str = callchain_list__sym_name(chain, ipstr, sizeof(ipstr)); |
| 425 | if (was_first) { |
| 426 | double percent = cumul * 100.0 / new_total; |
| 427 | |
| 428 | if (asprintf(&alloc_str, "%2.2f%% %s", percent, str) < 0) |
| 429 | str = "Not enough memory!"; |
| 430 | else |
| 431 | str = alloc_str; |
| 432 | } |
| 433 | |
| 434 | color = HE_COLORSET_NORMAL; |
| 435 | width = self->b.width - (offset + extra_offset + 2); |
| 436 | if (ui_browser__is_current_entry(&self->b, row)) { |
| 437 | self->selection = &chain->ms; |
| 438 | color = HE_COLORSET_SELECTED; |
| 439 | *is_current_entry = true; |
| 440 | } |
| 441 | |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 442 | ui_browser__set_color(&self->b, color); |
| 443 | ui_browser__gotorc(&self->b, row, 0); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 444 | slsmg_write_nstring(" ", offset + extra_offset); |
| 445 | slsmg_printf("%c ", folded_sign); |
| 446 | slsmg_write_nstring(str, width); |
| 447 | free(alloc_str); |
| 448 | |
| 449 | if (++row == self->b.height) |
| 450 | goto out; |
| 451 | do_next: |
| 452 | if (folded_sign == '+') |
| 453 | break; |
| 454 | } |
| 455 | |
| 456 | if (folded_sign == '-') { |
| 457 | const int new_level = level + (extra_offset ? 2 : 1); |
| 458 | row += hist_browser__show_callchain_node_rb_tree(self, child, new_total, |
| 459 | new_level, row, row_offset, |
| 460 | is_current_entry); |
| 461 | } |
| 462 | if (row == self->b.height) |
| 463 | goto out; |
| 464 | node = next; |
| 465 | } |
| 466 | out: |
| 467 | return row - first_row; |
| 468 | } |
| 469 | |
| 470 | static int hist_browser__show_callchain_node(struct hist_browser *self, |
| 471 | struct callchain_node *node, |
| 472 | int level, unsigned short row, |
| 473 | off_t *row_offset, |
| 474 | bool *is_current_entry) |
| 475 | { |
| 476 | struct callchain_list *chain; |
| 477 | int first_row = row, |
| 478 | offset = level * LEVEL_OFFSET_STEP, |
| 479 | width = self->b.width - offset; |
| 480 | char folded_sign = ' '; |
| 481 | |
| 482 | list_for_each_entry(chain, &node->val, list) { |
| 483 | char ipstr[BITS_PER_LONG / 4 + 1], *s; |
| 484 | int color; |
Arnaldo Carvalho de Melo | 163caed | 2010-08-25 16:30:03 -0300 | [diff] [blame] | 485 | |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 486 | folded_sign = callchain_list__folded(chain); |
| 487 | |
| 488 | if (*row_offset != 0) { |
| 489 | --*row_offset; |
| 490 | continue; |
| 491 | } |
| 492 | |
| 493 | color = HE_COLORSET_NORMAL; |
| 494 | if (ui_browser__is_current_entry(&self->b, row)) { |
| 495 | self->selection = &chain->ms; |
| 496 | color = HE_COLORSET_SELECTED; |
| 497 | *is_current_entry = true; |
| 498 | } |
| 499 | |
| 500 | s = callchain_list__sym_name(chain, ipstr, sizeof(ipstr)); |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 501 | ui_browser__gotorc(&self->b, row, 0); |
| 502 | ui_browser__set_color(&self->b, color); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 503 | slsmg_write_nstring(" ", offset); |
| 504 | slsmg_printf("%c ", folded_sign); |
| 505 | slsmg_write_nstring(s, width - 2); |
| 506 | |
| 507 | if (++row == self->b.height) |
| 508 | goto out; |
| 509 | } |
| 510 | |
| 511 | if (folded_sign == '-') |
| 512 | row += hist_browser__show_callchain_node_rb_tree(self, node, |
| 513 | self->hists->stats.total_period, |
| 514 | level + 1, row, |
| 515 | row_offset, |
| 516 | is_current_entry); |
| 517 | out: |
| 518 | return row - first_row; |
| 519 | } |
| 520 | |
| 521 | static int hist_browser__show_callchain(struct hist_browser *self, |
| 522 | struct rb_root *chain, |
| 523 | int level, unsigned short row, |
| 524 | off_t *row_offset, |
| 525 | bool *is_current_entry) |
| 526 | { |
| 527 | struct rb_node *nd; |
| 528 | int first_row = row; |
| 529 | |
| 530 | for (nd = rb_first(chain); nd; nd = rb_next(nd)) { |
| 531 | struct callchain_node *node = rb_entry(nd, struct callchain_node, rb_node); |
| 532 | |
| 533 | row += hist_browser__show_callchain_node(self, node, level, |
| 534 | row, row_offset, |
| 535 | is_current_entry); |
| 536 | if (row == self->b.height) |
| 537 | break; |
| 538 | } |
| 539 | |
| 540 | return row - first_row; |
| 541 | } |
| 542 | |
| 543 | static int hist_browser__show_entry(struct hist_browser *self, |
| 544 | struct hist_entry *entry, |
| 545 | unsigned short row) |
| 546 | { |
| 547 | char s[256]; |
| 548 | double percent; |
| 549 | int printed = 0; |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame^] | 550 | int width = self->b.width; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 551 | char folded_sign = ' '; |
| 552 | bool current_entry = ui_browser__is_current_entry(&self->b, row); |
| 553 | off_t row_offset = entry->row_offset; |
| 554 | |
| 555 | if (current_entry) { |
| 556 | self->he_selection = entry; |
| 557 | self->selection = &entry->ms; |
| 558 | } |
| 559 | |
| 560 | if (symbol_conf.use_callchain) { |
Arnaldo Carvalho de Melo | 163caed | 2010-08-25 16:30:03 -0300 | [diff] [blame] | 561 | hist_entry__init_have_children(entry); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 562 | folded_sign = hist_entry__folded(entry); |
| 563 | } |
| 564 | |
| 565 | if (row_offset == 0) { |
| 566 | hist_entry__snprintf(entry, s, sizeof(s), self->hists, NULL, false, |
| 567 | 0, false, self->hists->stats.total_period); |
| 568 | percent = (entry->period * 100.0) / self->hists->stats.total_period; |
| 569 | |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame^] | 570 | ui_browser__set_percent_color(&self->b, percent, current_entry); |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 571 | ui_browser__gotorc(&self->b, row, 0); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 572 | if (symbol_conf.use_callchain) { |
| 573 | slsmg_printf("%c ", folded_sign); |
| 574 | width -= 2; |
| 575 | } |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame^] | 576 | |
| 577 | /* The scroll bar isn't being used */ |
| 578 | if (!self->b.navkeypressed) |
| 579 | width += 1; |
| 580 | |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 581 | slsmg_write_nstring(s, width); |
| 582 | ++row; |
| 583 | ++printed; |
| 584 | } else |
| 585 | --row_offset; |
| 586 | |
| 587 | if (folded_sign == '-' && row != self->b.height) { |
| 588 | printed += hist_browser__show_callchain(self, &entry->sorted_chain, |
| 589 | 1, row, &row_offset, |
| 590 | ¤t_entry); |
| 591 | if (current_entry) |
| 592 | self->he_selection = entry; |
| 593 | } |
| 594 | |
| 595 | return printed; |
| 596 | } |
| 597 | |
Arnaldo Carvalho de Melo | 437cfe7 | 2011-10-14 09:31:53 -0300 | [diff] [blame] | 598 | static void ui_browser__hists_init_top(struct ui_browser *browser) |
| 599 | { |
| 600 | if (browser->top == NULL) { |
| 601 | struct hist_browser *hb; |
| 602 | |
| 603 | hb = container_of(browser, struct hist_browser, b); |
| 604 | browser->top = rb_first(&hb->hists->entries); |
| 605 | } |
| 606 | } |
| 607 | |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 608 | static unsigned int hist_browser__refresh(struct ui_browser *self) |
| 609 | { |
| 610 | unsigned row = 0; |
| 611 | struct rb_node *nd; |
| 612 | struct hist_browser *hb = container_of(self, struct hist_browser, b); |
| 613 | |
Arnaldo Carvalho de Melo | 437cfe7 | 2011-10-14 09:31:53 -0300 | [diff] [blame] | 614 | ui_browser__hists_init_top(self); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 615 | |
| 616 | for (nd = self->top; nd; nd = rb_next(nd)) { |
| 617 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 618 | |
| 619 | if (h->filtered) |
| 620 | continue; |
| 621 | |
| 622 | row += hist_browser__show_entry(hb, h, row); |
| 623 | if (row == self->height) |
| 624 | break; |
| 625 | } |
| 626 | |
| 627 | return row; |
| 628 | } |
| 629 | |
| 630 | static struct rb_node *hists__filter_entries(struct rb_node *nd) |
| 631 | { |
| 632 | while (nd != NULL) { |
| 633 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 634 | if (!h->filtered) |
| 635 | return nd; |
| 636 | |
| 637 | nd = rb_next(nd); |
| 638 | } |
| 639 | |
| 640 | return NULL; |
| 641 | } |
| 642 | |
| 643 | static struct rb_node *hists__filter_prev_entries(struct rb_node *nd) |
| 644 | { |
| 645 | while (nd != NULL) { |
| 646 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 647 | if (!h->filtered) |
| 648 | return nd; |
| 649 | |
| 650 | nd = rb_prev(nd); |
| 651 | } |
| 652 | |
| 653 | return NULL; |
| 654 | } |
| 655 | |
| 656 | static void ui_browser__hists_seek(struct ui_browser *self, |
| 657 | off_t offset, int whence) |
| 658 | { |
| 659 | struct hist_entry *h; |
| 660 | struct rb_node *nd; |
| 661 | bool first = true; |
| 662 | |
Arnaldo Carvalho de Melo | 6009891 | 2011-03-04 21:19:21 -0300 | [diff] [blame] | 663 | if (self->nr_entries == 0) |
| 664 | return; |
| 665 | |
Arnaldo Carvalho de Melo | 437cfe7 | 2011-10-14 09:31:53 -0300 | [diff] [blame] | 666 | ui_browser__hists_init_top(self); |
| 667 | |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 668 | switch (whence) { |
| 669 | case SEEK_SET: |
| 670 | nd = hists__filter_entries(rb_first(self->entries)); |
| 671 | break; |
| 672 | case SEEK_CUR: |
| 673 | nd = self->top; |
| 674 | goto do_offset; |
| 675 | case SEEK_END: |
| 676 | nd = hists__filter_prev_entries(rb_last(self->entries)); |
| 677 | first = false; |
| 678 | break; |
| 679 | default: |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * Moves not relative to the first visible entry invalidates its |
| 685 | * row_offset: |
| 686 | */ |
| 687 | h = rb_entry(self->top, struct hist_entry, rb_node); |
| 688 | h->row_offset = 0; |
| 689 | |
| 690 | /* |
| 691 | * Here we have to check if nd is expanded (+), if it is we can't go |
| 692 | * the next top level hist_entry, instead we must compute an offset of |
| 693 | * what _not_ to show and not change the first visible entry. |
| 694 | * |
| 695 | * This offset increments when we are going from top to bottom and |
| 696 | * decreases when we're going from bottom to top. |
| 697 | * |
| 698 | * As we don't have backpointers to the top level in the callchains |
| 699 | * structure, we need to always print the whole hist_entry callchain, |
| 700 | * skipping the first ones that are before the first visible entry |
| 701 | * and stop when we printed enough lines to fill the screen. |
| 702 | */ |
| 703 | do_offset: |
| 704 | if (offset > 0) { |
| 705 | do { |
| 706 | h = rb_entry(nd, struct hist_entry, rb_node); |
| 707 | if (h->ms.unfolded) { |
| 708 | u16 remaining = h->nr_rows - h->row_offset; |
| 709 | if (offset > remaining) { |
| 710 | offset -= remaining; |
| 711 | h->row_offset = 0; |
| 712 | } else { |
| 713 | h->row_offset += offset; |
| 714 | offset = 0; |
| 715 | self->top = nd; |
| 716 | break; |
| 717 | } |
| 718 | } |
| 719 | nd = hists__filter_entries(rb_next(nd)); |
| 720 | if (nd == NULL) |
| 721 | break; |
| 722 | --offset; |
| 723 | self->top = nd; |
| 724 | } while (offset != 0); |
| 725 | } else if (offset < 0) { |
| 726 | while (1) { |
| 727 | h = rb_entry(nd, struct hist_entry, rb_node); |
| 728 | if (h->ms.unfolded) { |
| 729 | if (first) { |
| 730 | if (-offset > h->row_offset) { |
| 731 | offset += h->row_offset; |
| 732 | h->row_offset = 0; |
| 733 | } else { |
| 734 | h->row_offset += offset; |
| 735 | offset = 0; |
| 736 | self->top = nd; |
| 737 | break; |
| 738 | } |
| 739 | } else { |
| 740 | if (-offset > h->nr_rows) { |
| 741 | offset += h->nr_rows; |
| 742 | h->row_offset = 0; |
| 743 | } else { |
| 744 | h->row_offset = h->nr_rows + offset; |
| 745 | offset = 0; |
| 746 | self->top = nd; |
| 747 | break; |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | nd = hists__filter_prev_entries(rb_prev(nd)); |
| 753 | if (nd == NULL) |
| 754 | break; |
| 755 | ++offset; |
| 756 | self->top = nd; |
| 757 | if (offset == 0) { |
| 758 | /* |
| 759 | * Last unfiltered hist_entry, check if it is |
| 760 | * unfolded, if it is then we should have |
| 761 | * row_offset at its last entry. |
| 762 | */ |
| 763 | h = rb_entry(nd, struct hist_entry, rb_node); |
| 764 | if (h->ms.unfolded) |
| 765 | h->row_offset = h->nr_rows; |
| 766 | break; |
| 767 | } |
| 768 | first = false; |
| 769 | } |
| 770 | } else { |
| 771 | self->top = nd; |
| 772 | h = rb_entry(nd, struct hist_entry, rb_node); |
| 773 | h->row_offset = 0; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | static struct hist_browser *hist_browser__new(struct hists *hists) |
| 778 | { |
| 779 | struct hist_browser *self = zalloc(sizeof(*self)); |
| 780 | |
| 781 | if (self) { |
| 782 | self->hists = hists; |
| 783 | self->b.refresh = hist_browser__refresh; |
| 784 | self->b.seek = ui_browser__hists_seek; |
Arnaldo Carvalho de Melo | c172f74 | 2011-10-18 14:31:35 -0200 | [diff] [blame^] | 785 | self->b.use_navkeypressed = true, |
Arnaldo Carvalho de Melo | 724c9c9 | 2011-10-06 10:36:35 -0300 | [diff] [blame] | 786 | self->has_symbols = sort_sym.list.next != NULL; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | return self; |
| 790 | } |
| 791 | |
| 792 | static void hist_browser__delete(struct hist_browser *self) |
| 793 | { |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 794 | free(self); |
| 795 | } |
| 796 | |
| 797 | static struct hist_entry *hist_browser__selected_entry(struct hist_browser *self) |
| 798 | { |
| 799 | return self->he_selection; |
| 800 | } |
| 801 | |
| 802 | static struct thread *hist_browser__selected_thread(struct hist_browser *self) |
| 803 | { |
| 804 | return self->he_selection->thread; |
| 805 | } |
| 806 | |
Arnaldo Carvalho de Melo | 469917c | 2010-09-13 10:25:04 -0300 | [diff] [blame] | 807 | static int hists__browser_title(struct hists *self, char *bf, size_t size, |
| 808 | const char *ev_name, const struct dso *dso, |
| 809 | const struct thread *thread) |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 810 | { |
Arnaldo Carvalho de Melo | 469917c | 2010-09-13 10:25:04 -0300 | [diff] [blame] | 811 | char unit; |
| 812 | int printed; |
| 813 | unsigned long nr_events = self->stats.nr_events[PERF_RECORD_SAMPLE]; |
| 814 | |
| 815 | nr_events = convert_unit(nr_events, &unit); |
| 816 | printed = snprintf(bf, size, "Events: %lu%c %s", nr_events, unit, ev_name); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 817 | |
| 818 | if (thread) |
| 819 | printed += snprintf(bf + printed, size - printed, |
Arnaldo Carvalho de Melo | 469917c | 2010-09-13 10:25:04 -0300 | [diff] [blame] | 820 | ", Thread: %s(%d)", |
| 821 | (thread->comm_set ? thread->comm : ""), |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 822 | thread->pid); |
| 823 | if (dso) |
| 824 | printed += snprintf(bf + printed, size - printed, |
Arnaldo Carvalho de Melo | 469917c | 2010-09-13 10:25:04 -0300 | [diff] [blame] | 825 | ", DSO: %s", dso->short_name); |
| 826 | return printed; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 827 | } |
| 828 | |
Arnaldo Carvalho de Melo | 3495854 | 2011-10-05 19:35:54 -0300 | [diff] [blame] | 829 | static int perf_evsel__hists_browse(struct perf_evsel *evsel, int nr_events, |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 830 | const char *helpline, const char *ev_name, |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 831 | bool left_exits, |
| 832 | void(*timer)(void *arg), void *arg, |
| 833 | int delay_secs) |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 834 | { |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 835 | struct hists *self = &evsel->hists; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 836 | struct hist_browser *browser = hist_browser__new(self); |
| 837 | struct pstack *fstack; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 838 | int key = -1; |
| 839 | |
| 840 | if (browser == NULL) |
| 841 | return -1; |
| 842 | |
| 843 | fstack = pstack__new(2); |
| 844 | if (fstack == NULL) |
| 845 | goto out; |
| 846 | |
| 847 | ui_helpline__push(helpline); |
| 848 | |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 849 | while (1) { |
Arnaldo Carvalho de Melo | 6009891 | 2011-03-04 21:19:21 -0300 | [diff] [blame] | 850 | const struct thread *thread = NULL; |
| 851 | const struct dso *dso = NULL; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 852 | char *options[16]; |
| 853 | int nr_options = 0, choice = 0, i, |
| 854 | annotate = -2, zoom_dso = -2, zoom_thread = -2, |
| 855 | browse_map = -2; |
| 856 | |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 857 | key = hist_browser__run(browser, ev_name, timer, arg, delay_secs); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 858 | |
Arnaldo Carvalho de Melo | 6009891 | 2011-03-04 21:19:21 -0300 | [diff] [blame] | 859 | if (browser->he_selection != NULL) { |
| 860 | thread = hist_browser__selected_thread(browser); |
| 861 | dso = browser->selection->map ? browser->selection->map->dso : NULL; |
| 862 | } |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 863 | |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 864 | switch (key) { |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 865 | case NEWT_KEY_TAB: |
| 866 | case NEWT_KEY_UNTAB: |
| 867 | /* |
| 868 | * Exit the browser, let hists__browser_tree |
| 869 | * go to the next or previous |
| 870 | */ |
| 871 | goto out_free_stack; |
| 872 | case 'a': |
Arnaldo Carvalho de Melo | 6009891 | 2011-03-04 21:19:21 -0300 | [diff] [blame] | 873 | if (browser->selection == NULL || |
Lin Ming | db9a9cbc | 2011-04-08 14:31:26 +0800 | [diff] [blame] | 874 | browser->selection->sym == NULL || |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 875 | browser->selection->map->dso->annotate_warned) |
| 876 | continue; |
| 877 | goto do_annotate; |
| 878 | case 'd': |
| 879 | goto zoom_dso; |
| 880 | case 't': |
| 881 | goto zoom_thread; |
Arnaldo Carvalho de Melo | 3c916cc | 2010-08-25 17:18:35 -0300 | [diff] [blame] | 882 | case NEWT_KEY_F1: |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 883 | case 'h': |
| 884 | case '?': |
Arnaldo Carvalho de Melo | 2d5646c | 2011-10-18 13:02:52 -0200 | [diff] [blame] | 885 | ui__help_window("h/?/F1 Show this window\n" |
| 886 | "UP/DOWN/PGUP\n" |
| 887 | "PGDN/SPACE Navigate\n" |
| 888 | "q/ESC/CTRL+C Exit browser\n\n" |
| 889 | "For multiple event sessions:\n\n" |
| 890 | "TAB/UNTAB Switch events\n\n" |
Arnaldo Carvalho de Melo | 724c9c9 | 2011-10-06 10:36:35 -0300 | [diff] [blame] | 891 | "For symbolic views (--sort has sym):\n\n" |
Arnaldo Carvalho de Melo | 2d5646c | 2011-10-18 13:02:52 -0200 | [diff] [blame] | 892 | "-> Zoom into DSO/Threads & Annotate current symbol\n" |
| 893 | "<- Zoom out\n" |
| 894 | "a Annotate current symbol\n" |
| 895 | "C Collapse all callchains\n" |
| 896 | "E Expand all callchains\n" |
| 897 | "d Zoom into current DSO\n" |
| 898 | "t Zoom into current Thread\n"); |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 899 | continue; |
| 900 | case NEWT_KEY_ENTER: |
| 901 | case NEWT_KEY_RIGHT: |
| 902 | /* menu */ |
| 903 | break; |
| 904 | case NEWT_KEY_LEFT: { |
| 905 | const void *top; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 906 | |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 907 | if (pstack__empty(fstack)) { |
| 908 | /* |
| 909 | * Go back to the perf_evsel_menu__run or other user |
| 910 | */ |
| 911 | if (left_exits) |
| 912 | goto out_free_stack; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 913 | continue; |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 914 | } |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 915 | top = pstack__pop(fstack); |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 916 | if (top == &browser->dso_filter) |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 917 | goto zoom_out_dso; |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 918 | if (top == &browser->thread_filter) |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 919 | goto zoom_out_thread; |
| 920 | continue; |
| 921 | } |
| 922 | case NEWT_KEY_ESCAPE: |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 923 | if (!left_exits && |
| 924 | !ui__dialog_yesno("Do you really want to exit?")) |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 925 | continue; |
| 926 | /* Fall thru */ |
Arnaldo Carvalho de Melo | ed7e566 | 2011-10-13 08:31:22 -0300 | [diff] [blame] | 927 | case 'q': |
| 928 | case CTRL('c'): |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 929 | goto out_free_stack; |
Arnaldo Carvalho de Melo | ed7e566 | 2011-10-13 08:31:22 -0300 | [diff] [blame] | 930 | default: |
| 931 | continue; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 932 | } |
| 933 | |
Arnaldo Carvalho de Melo | 724c9c9 | 2011-10-06 10:36:35 -0300 | [diff] [blame] | 934 | if (!browser->has_symbols) |
| 935 | goto add_exit_option; |
| 936 | |
Arnaldo Carvalho de Melo | 6009891 | 2011-03-04 21:19:21 -0300 | [diff] [blame] | 937 | if (browser->selection != NULL && |
| 938 | browser->selection->sym != NULL && |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 939 | !browser->selection->map->dso->annotate_warned && |
| 940 | asprintf(&options[nr_options], "Annotate %s", |
| 941 | browser->selection->sym->name) > 0) |
| 942 | annotate = nr_options++; |
| 943 | |
| 944 | if (thread != NULL && |
| 945 | asprintf(&options[nr_options], "Zoom %s %s(%d) thread", |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 946 | (browser->thread_filter ? "out of" : "into"), |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 947 | (thread->comm_set ? thread->comm : ""), |
| 948 | thread->pid) > 0) |
| 949 | zoom_thread = nr_options++; |
| 950 | |
| 951 | if (dso != NULL && |
| 952 | asprintf(&options[nr_options], "Zoom %s %s DSO", |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 953 | (browser->dso_filter ? "out of" : "into"), |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 954 | (dso->kernel ? "the Kernel" : dso->short_name)) > 0) |
| 955 | zoom_dso = nr_options++; |
| 956 | |
Arnaldo Carvalho de Melo | 6009891 | 2011-03-04 21:19:21 -0300 | [diff] [blame] | 957 | if (browser->selection != NULL && |
| 958 | browser->selection->map != NULL && |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 959 | asprintf(&options[nr_options], "Browse map details") > 0) |
| 960 | browse_map = nr_options++; |
Arnaldo Carvalho de Melo | 724c9c9 | 2011-10-06 10:36:35 -0300 | [diff] [blame] | 961 | add_exit_option: |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 962 | options[nr_options++] = (char *)"Exit"; |
| 963 | |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 964 | choice = ui__popup_menu(nr_options, options); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 965 | |
| 966 | for (i = 0; i < nr_options - 1; ++i) |
| 967 | free(options[i]); |
| 968 | |
| 969 | if (choice == nr_options - 1) |
| 970 | break; |
| 971 | |
| 972 | if (choice == -1) |
| 973 | continue; |
| 974 | |
| 975 | if (choice == annotate) { |
| 976 | struct hist_entry *he; |
| 977 | do_annotate: |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 978 | he = hist_browser__selected_entry(browser); |
| 979 | if (he == NULL) |
| 980 | continue; |
Arnaldo Carvalho de Melo | df71d95 | 2011-10-13 08:01:33 -0300 | [diff] [blame] | 981 | /* |
| 982 | * Don't let this be freed, say, by hists__decay_entry. |
| 983 | */ |
| 984 | he->used = true; |
Arnaldo Carvalho de Melo | 3495854 | 2011-10-05 19:35:54 -0300 | [diff] [blame] | 985 | hist_entry__tui_annotate(he, evsel->idx, nr_events, |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 986 | timer, arg, delay_secs); |
Arnaldo Carvalho de Melo | df71d95 | 2011-10-13 08:01:33 -0300 | [diff] [blame] | 987 | he->used = false; |
Arnaldo Carvalho de Melo | 900e14a | 2011-10-11 16:15:39 -0300 | [diff] [blame] | 988 | ui_browser__update_nr_entries(&browser->b, browser->hists->nr_entries); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 989 | } else if (choice == browse_map) |
| 990 | map__browse(browser->selection->map); |
| 991 | else if (choice == zoom_dso) { |
| 992 | zoom_dso: |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 993 | if (browser->dso_filter) { |
| 994 | pstack__remove(fstack, &browser->dso_filter); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 995 | zoom_out_dso: |
| 996 | ui_helpline__pop(); |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 997 | browser->dso_filter = NULL; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 998 | } else { |
| 999 | if (dso == NULL) |
| 1000 | continue; |
| 1001 | ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s DSO\"", |
| 1002 | dso->kernel ? "the Kernel" : dso->short_name); |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 1003 | browser->dso_filter = dso; |
| 1004 | pstack__push(fstack, &browser->dso_filter); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 1005 | } |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 1006 | hists__filter_by_dso(self, browser->dso_filter); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 1007 | hist_browser__reset(browser); |
| 1008 | } else if (choice == zoom_thread) { |
| 1009 | zoom_thread: |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 1010 | if (browser->thread_filter) { |
| 1011 | pstack__remove(fstack, &browser->thread_filter); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 1012 | zoom_out_thread: |
| 1013 | ui_helpline__pop(); |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 1014 | browser->thread_filter = NULL; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 1015 | } else { |
| 1016 | ui_helpline__fpush("To zoom out press <- or -> + \"Zoom out of %s(%d) thread\"", |
| 1017 | thread->comm_set ? thread->comm : "", |
| 1018 | thread->pid); |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 1019 | browser->thread_filter = thread; |
| 1020 | pstack__push(fstack, &browser->thread_filter); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 1021 | } |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 1022 | hists__filter_by_thread(self, browser->thread_filter); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 1023 | hist_browser__reset(browser); |
| 1024 | } |
| 1025 | } |
| 1026 | out_free_stack: |
| 1027 | pstack__delete(fstack); |
| 1028 | out: |
| 1029 | hist_browser__delete(browser); |
| 1030 | return key; |
| 1031 | } |
| 1032 | |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 1033 | struct perf_evsel_menu { |
| 1034 | struct ui_browser b; |
| 1035 | struct perf_evsel *selection; |
| 1036 | }; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 1037 | |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 1038 | static void perf_evsel_menu__write(struct ui_browser *browser, |
| 1039 | void *entry, int row) |
| 1040 | { |
| 1041 | struct perf_evsel_menu *menu = container_of(browser, |
| 1042 | struct perf_evsel_menu, b); |
| 1043 | struct perf_evsel *evsel = list_entry(entry, struct perf_evsel, node); |
| 1044 | bool current_entry = ui_browser__is_current_entry(browser, row); |
| 1045 | unsigned long nr_events = evsel->hists.stats.nr_events[PERF_RECORD_SAMPLE]; |
| 1046 | const char *ev_name = event_name(evsel); |
| 1047 | char bf[256], unit; |
| 1048 | |
| 1049 | ui_browser__set_color(browser, current_entry ? HE_COLORSET_SELECTED : |
| 1050 | HE_COLORSET_NORMAL); |
| 1051 | |
| 1052 | nr_events = convert_unit(nr_events, &unit); |
| 1053 | snprintf(bf, sizeof(bf), "%lu%c%s%s", nr_events, |
| 1054 | unit, unit == ' ' ? "" : " ", ev_name); |
| 1055 | slsmg_write_nstring(bf, browser->width); |
| 1056 | |
| 1057 | if (current_entry) |
| 1058 | menu->selection = evsel; |
| 1059 | } |
| 1060 | |
Arnaldo Carvalho de Melo | 3495854 | 2011-10-05 19:35:54 -0300 | [diff] [blame] | 1061 | static int perf_evsel_menu__run(struct perf_evsel_menu *menu, |
| 1062 | int nr_events, const char *help, |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 1063 | void(*timer)(void *arg), void *arg, int delay_secs) |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 1064 | { |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 1065 | struct perf_evlist *evlist = menu->b.priv; |
| 1066 | struct perf_evsel *pos; |
| 1067 | const char *ev_name, *title = "Available samples"; |
| 1068 | int key; |
| 1069 | |
| 1070 | if (ui_browser__show(&menu->b, title, |
| 1071 | "ESC: exit, ENTER|->: Browse histograms") < 0) |
| 1072 | return -1; |
| 1073 | |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 1074 | while (1) { |
Arnaldo Carvalho de Melo | 3af6e33 | 2011-10-13 08:52:46 -0300 | [diff] [blame] | 1075 | key = ui_browser__run(&menu->b, delay_secs); |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 1076 | |
| 1077 | switch (key) { |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 1078 | case -1: |
| 1079 | /* FIXME we need to check if it was es.reason == NEWT_EXIT_TIMER */ |
| 1080 | timer(arg); |
| 1081 | continue; |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 1082 | case NEWT_KEY_RIGHT: |
| 1083 | case NEWT_KEY_ENTER: |
| 1084 | if (!menu->selection) |
| 1085 | continue; |
| 1086 | pos = menu->selection; |
| 1087 | browse_hists: |
Arnaldo Carvalho de Melo | 18eaf0b | 2011-10-13 12:22:28 -0300 | [diff] [blame] | 1088 | perf_evlist__set_selected(evlist, pos); |
| 1089 | /* |
| 1090 | * Give the calling tool a chance to populate the non |
| 1091 | * default evsel resorted hists tree. |
| 1092 | */ |
| 1093 | if (timer) |
| 1094 | timer(arg); |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 1095 | ev_name = event_name(pos); |
Arnaldo Carvalho de Melo | 3495854 | 2011-10-05 19:35:54 -0300 | [diff] [blame] | 1096 | key = perf_evsel__hists_browse(pos, nr_events, help, |
| 1097 | ev_name, true, timer, |
| 1098 | arg, delay_secs); |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 1099 | ui_browser__show_title(&menu->b, title); |
Arnaldo Carvalho de Melo | 18eaf0b | 2011-10-13 12:22:28 -0300 | [diff] [blame] | 1100 | switch (key) { |
| 1101 | case NEWT_KEY_TAB: |
| 1102 | if (pos->node.next == &evlist->entries) |
| 1103 | pos = list_entry(evlist->entries.next, struct perf_evsel, node); |
| 1104 | else |
| 1105 | pos = list_entry(pos->node.next, struct perf_evsel, node); |
| 1106 | goto browse_hists; |
| 1107 | case NEWT_KEY_UNTAB: |
| 1108 | if (pos->node.prev == &evlist->entries) |
| 1109 | pos = list_entry(evlist->entries.prev, struct perf_evsel, node); |
| 1110 | else |
| 1111 | pos = list_entry(pos->node.prev, struct perf_evsel, node); |
| 1112 | goto browse_hists; |
| 1113 | case NEWT_KEY_ESCAPE: |
| 1114 | if (!ui__dialog_yesno("Do you really want to exit?")) |
| 1115 | continue; |
| 1116 | /* Fall thru */ |
| 1117 | case 'q': |
| 1118 | case CTRL('c'): |
| 1119 | goto out; |
| 1120 | default: |
| 1121 | continue; |
| 1122 | } |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 1123 | case NEWT_KEY_LEFT: |
| 1124 | continue; |
Arnaldo Carvalho de Melo | ed7e566 | 2011-10-13 08:31:22 -0300 | [diff] [blame] | 1125 | case NEWT_KEY_ESCAPE: |
| 1126 | if (!ui__dialog_yesno("Do you really want to exit?")) |
| 1127 | continue; |
| 1128 | /* Fall thru */ |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 1129 | case 'q': |
| 1130 | case CTRL('c'): |
| 1131 | goto out; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 1132 | default: |
Arnaldo Carvalho de Melo | 18eaf0b | 2011-10-13 12:22:28 -0300 | [diff] [blame] | 1133 | continue; |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 1137 | out: |
| 1138 | ui_browser__hide(&menu->b); |
| 1139 | return key; |
| 1140 | } |
| 1141 | |
| 1142 | static int __perf_evlist__tui_browse_hists(struct perf_evlist *evlist, |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 1143 | const char *help, |
| 1144 | void(*timer)(void *arg), void *arg, |
| 1145 | int delay_secs) |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 1146 | { |
| 1147 | struct perf_evsel *pos; |
| 1148 | struct perf_evsel_menu menu = { |
| 1149 | .b = { |
| 1150 | .entries = &evlist->entries, |
| 1151 | .refresh = ui_browser__list_head_refresh, |
| 1152 | .seek = ui_browser__list_head_seek, |
| 1153 | .write = perf_evsel_menu__write, |
| 1154 | .nr_entries = evlist->nr_entries, |
| 1155 | .priv = evlist, |
| 1156 | }, |
| 1157 | }; |
| 1158 | |
| 1159 | ui_helpline__push("Press ESC to exit"); |
| 1160 | |
| 1161 | list_for_each_entry(pos, &evlist->entries, node) { |
| 1162 | const char *ev_name = event_name(pos); |
| 1163 | size_t line_len = strlen(ev_name) + 7; |
| 1164 | |
| 1165 | if (menu.b.width < line_len) |
| 1166 | menu.b.width = line_len; |
| 1167 | /* |
| 1168 | * Cache the evsel name, tracepoints have a _high_ cost per |
| 1169 | * event_name() call. |
| 1170 | */ |
| 1171 | if (pos->name == NULL) |
| 1172 | pos->name = strdup(ev_name); |
| 1173 | } |
| 1174 | |
Arnaldo Carvalho de Melo | 3495854 | 2011-10-05 19:35:54 -0300 | [diff] [blame] | 1175 | return perf_evsel_menu__run(&menu, evlist->nr_entries, help, timer, |
| 1176 | arg, delay_secs); |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 1177 | } |
| 1178 | |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 1179 | int perf_evlist__tui_browse_hists(struct perf_evlist *evlist, const char *help, |
| 1180 | void(*timer)(void *arg), void *arg, |
| 1181 | int delay_secs) |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 1182 | { |
| 1183 | |
| 1184 | if (evlist->nr_entries == 1) { |
| 1185 | struct perf_evsel *first = list_entry(evlist->entries.next, |
| 1186 | struct perf_evsel, node); |
| 1187 | const char *ev_name = event_name(first); |
Arnaldo Carvalho de Melo | 3495854 | 2011-10-05 19:35:54 -0300 | [diff] [blame] | 1188 | return perf_evsel__hists_browse(first, evlist->nr_entries, help, |
| 1189 | ev_name, false, timer, arg, |
| 1190 | delay_secs); |
Arnaldo Carvalho de Melo | 7f0030b | 2011-03-06 13:07:30 -0300 | [diff] [blame] | 1191 | } |
| 1192 | |
Arnaldo Carvalho de Melo | 81cce8d | 2011-10-05 19:11:32 -0300 | [diff] [blame] | 1193 | return __perf_evlist__tui_browse_hists(evlist, help, |
| 1194 | timer, arg, delay_secs); |
Arnaldo Carvalho de Melo | d1b4f24 | 2010-08-10 15:49:07 -0300 | [diff] [blame] | 1195 | } |