Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 1 | #include "../browser.h" |
| 2 | #include "../helpline.h" |
| 3 | #include "../libslang.h" |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 4 | #include "../../annotate.h" |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 5 | #include "../../hist.h" |
| 6 | #include "../../sort.h" |
| 7 | #include "../../symbol.h" |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 8 | #include "../../annotate.h" |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 9 | #include <pthread.h> |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 10 | |
| 11 | static void ui__error_window(const char *fmt, ...) |
| 12 | { |
| 13 | va_list ap; |
| 14 | |
| 15 | va_start(ap, fmt); |
| 16 | newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap); |
| 17 | va_end(ap); |
| 18 | } |
| 19 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 20 | struct annotate_browser { |
| 21 | struct ui_browser b; |
| 22 | struct rb_root entries; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 23 | struct rb_node *curr_hot; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | struct objdump_line_rb_node { |
| 27 | struct rb_node rb_node; |
| 28 | double percent; |
| 29 | u32 idx; |
| 30 | }; |
| 31 | |
| 32 | static inline |
| 33 | struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self) |
| 34 | { |
| 35 | return (struct objdump_line_rb_node *)(self + 1); |
| 36 | } |
| 37 | |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 38 | static void annotate_browser__write(struct ui_browser *self, void *entry, int row) |
| 39 | { |
| 40 | struct objdump_line *ol = rb_entry(entry, struct objdump_line, node); |
| 41 | bool current_entry = ui_browser__is_current_entry(self, row); |
| 42 | int width = self->width; |
| 43 | |
| 44 | if (ol->offset != -1) { |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 45 | struct objdump_line_rb_node *olrb = objdump_line__rb(ol); |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 46 | ui_browser__set_percent_color(self, olrb->percent, current_entry); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 47 | slsmg_printf(" %7.2f ", olrb->percent); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 48 | } else { |
Arnaldo Carvalho de Melo | 8f9bbc4 | 2010-08-11 14:51:47 -0300 | [diff] [blame] | 49 | ui_browser__set_percent_color(self, 0, current_entry); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 50 | slsmg_write_nstring(" ", 9); |
| 51 | } |
| 52 | |
| 53 | SLsmg_write_char(':'); |
| 54 | slsmg_write_nstring(" ", 8); |
| 55 | if (!*ol->line) |
| 56 | slsmg_write_nstring(" ", width - 18); |
| 57 | else |
| 58 | slsmg_write_nstring(ol->line, width - 18); |
Arnaldo Carvalho de Melo | b99976e | 2011-02-09 13:59:14 -0200 | [diff] [blame] | 59 | |
| 60 | if (!current_entry) |
| 61 | ui_browser__set_color(self, HE_COLORSET_CODE); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 62 | } |
| 63 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 64 | static double objdump_line__calc_percent(struct objdump_line *self, |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 65 | struct symbol *sym, int evidx) |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 66 | { |
| 67 | double percent = 0.0; |
| 68 | |
| 69 | if (self->offset != -1) { |
| 70 | int len = sym->end - sym->start; |
| 71 | unsigned int hits = 0; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 72 | struct annotation *notes = symbol__annotation(sym); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 73 | struct source_line *src_line = notes->src->lines; |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 74 | struct sym_hist *h = annotation__histogram(notes, evidx); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 75 | s64 offset = self->offset; |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 76 | struct objdump_line *next; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 77 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 78 | next = objdump__get_next_ip_line(¬es->src->source, self); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 79 | while (offset < (s64)len && |
| 80 | (next == NULL || offset < next->offset)) { |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 81 | if (src_line) { |
| 82 | percent += src_line[offset].percent; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 83 | } else |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 84 | hits += h->addr[offset]; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 85 | |
| 86 | ++offset; |
| 87 | } |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 88 | /* |
| 89 | * If the percentage wasn't already calculated in |
| 90 | * symbol__get_source_line, do it now: |
| 91 | */ |
| 92 | if (src_line == NULL && h->sum) |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 93 | percent = 100.0 * hits / h->sum; |
| 94 | } |
| 95 | |
| 96 | return percent; |
| 97 | } |
| 98 | |
| 99 | static void objdump__insert_line(struct rb_root *self, |
| 100 | struct objdump_line_rb_node *line) |
| 101 | { |
| 102 | struct rb_node **p = &self->rb_node; |
| 103 | struct rb_node *parent = NULL; |
| 104 | struct objdump_line_rb_node *l; |
| 105 | |
| 106 | while (*p != NULL) { |
| 107 | parent = *p; |
| 108 | l = rb_entry(parent, struct objdump_line_rb_node, rb_node); |
| 109 | if (line->percent < l->percent) |
| 110 | p = &(*p)->rb_left; |
| 111 | else |
| 112 | p = &(*p)->rb_right; |
| 113 | } |
| 114 | rb_link_node(&line->rb_node, parent, p); |
| 115 | rb_insert_color(&line->rb_node, self); |
| 116 | } |
| 117 | |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 118 | static void annotate_browser__set_top(struct annotate_browser *self, |
| 119 | struct rb_node *nd) |
| 120 | { |
| 121 | struct objdump_line_rb_node *rbpos; |
| 122 | struct objdump_line *pos; |
| 123 | unsigned back; |
| 124 | |
| 125 | ui_browser__refresh_dimensions(&self->b); |
| 126 | back = self->b.height / 2; |
| 127 | rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node); |
| 128 | pos = ((struct objdump_line *)rbpos) - 1; |
| 129 | self->b.top_idx = self->b.index = rbpos->idx; |
| 130 | |
| 131 | while (self->b.top_idx != 0 && back != 0) { |
| 132 | pos = list_entry(pos->node.prev, struct objdump_line, node); |
| 133 | |
| 134 | --self->b.top_idx; |
| 135 | --back; |
| 136 | } |
| 137 | |
| 138 | self->b.top = pos; |
| 139 | self->curr_hot = nd; |
| 140 | } |
| 141 | |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 142 | static void annotate_browser__calc_percent(struct annotate_browser *browser, |
| 143 | int evidx) |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 144 | { |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 145 | struct symbol *sym = browser->b.priv; |
| 146 | struct annotation *notes = symbol__annotation(sym); |
| 147 | struct objdump_line *pos; |
| 148 | |
| 149 | browser->entries = RB_ROOT; |
| 150 | |
| 151 | pthread_mutex_lock(¬es->lock); |
| 152 | |
| 153 | list_for_each_entry(pos, ¬es->src->source, node) { |
| 154 | struct objdump_line_rb_node *rbpos = objdump_line__rb(pos); |
| 155 | rbpos->percent = objdump_line__calc_percent(pos, sym, evidx); |
| 156 | if (rbpos->percent < 0.01) { |
| 157 | RB_CLEAR_NODE(&rbpos->rb_node); |
| 158 | continue; |
| 159 | } |
| 160 | objdump__insert_line(&browser->entries, rbpos); |
| 161 | } |
| 162 | pthread_mutex_unlock(¬es->lock); |
| 163 | |
| 164 | browser->curr_hot = rb_last(&browser->entries); |
| 165 | } |
| 166 | |
| 167 | static int annotate_browser__run(struct annotate_browser *self, int evidx, |
| 168 | int refresh) |
| 169 | { |
| 170 | struct rb_node *nd = NULL; |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 171 | struct symbol *sym = self->b.priv; |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 172 | /* |
| 173 | * RIGHT To allow builtin-annotate to cycle thru multiple symbols by |
| 174 | * examining the exit key for this function. |
| 175 | */ |
| 176 | int exit_keys[] = { 'H', NEWT_KEY_TAB, NEWT_KEY_UNTAB, |
| 177 | NEWT_KEY_RIGHT, 0 }; |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 178 | int key; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 179 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 180 | if (ui_browser__show(&self->b, sym->name, |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 181 | "<-, -> or ESC: exit, TAB/shift+TAB: " |
| 182 | "cycle hottest lines, H: Hottest") < 0) |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 183 | return -1; |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 184 | |
| 185 | ui_browser__add_exit_keys(&self->b, exit_keys); |
| 186 | annotate_browser__calc_percent(self, evidx); |
| 187 | |
| 188 | if (self->curr_hot) |
| 189 | annotate_browser__set_top(self, self->curr_hot); |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 190 | |
| 191 | nd = self->curr_hot; |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 192 | |
| 193 | if (refresh != 0) |
| 194 | newtFormSetTimer(self->b.form, refresh); |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 195 | |
| 196 | while (1) { |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 197 | key = ui_browser__run(&self->b); |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 198 | |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 199 | if (refresh != 0) { |
| 200 | annotate_browser__calc_percent(self, evidx); |
| 201 | /* |
| 202 | * Current line focus got out of the list of most active |
| 203 | * lines, NULL it so that if TAB|UNTAB is pressed, we |
| 204 | * move to curr_hot (current hottest line). |
| 205 | */ |
| 206 | if (nd != NULL && RB_EMPTY_NODE(nd)) |
| 207 | nd = NULL; |
| 208 | } |
| 209 | |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 210 | switch (key) { |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 211 | case -1: |
| 212 | /* |
| 213 | * FIXME we need to check if it was |
| 214 | * es.reason == NEWT_EXIT_TIMER |
| 215 | */ |
| 216 | if (refresh != 0) |
| 217 | symbol__annotate_decay_histogram(sym, evidx); |
| 218 | continue; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 219 | case NEWT_KEY_TAB: |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 220 | if (nd != NULL) { |
| 221 | nd = rb_prev(nd); |
| 222 | if (nd == NULL) |
| 223 | nd = rb_last(&self->entries); |
| 224 | } else |
| 225 | nd = self->curr_hot; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 226 | break; |
| 227 | case NEWT_KEY_UNTAB: |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 228 | if (nd != NULL) |
| 229 | nd = rb_next(nd); |
| 230 | if (nd == NULL) |
| 231 | nd = rb_first(&self->entries); |
| 232 | else |
| 233 | nd = self->curr_hot; |
| 234 | break; |
| 235 | case 'H': |
| 236 | nd = self->curr_hot; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 237 | break; |
| 238 | default: |
| 239 | goto out; |
| 240 | } |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 241 | |
| 242 | if (nd != NULL) |
| 243 | annotate_browser__set_top(self, nd); |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 244 | } |
| 245 | out: |
Arnaldo Carvalho de Melo | 59e8fe3 | 2010-08-10 15:44:20 -0300 | [diff] [blame] | 246 | ui_browser__hide(&self->b); |
Arnaldo Carvalho de Melo | b50e003 | 2010-08-11 10:07:43 -0300 | [diff] [blame] | 247 | return key; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame] | 248 | } |
| 249 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 250 | int hist_entry__tui_annotate(struct hist_entry *he, int evidx) |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 251 | { |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 252 | return symbol__tui_annotate(he->ms.sym, he->ms.map, evidx, 0); |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 253 | } |
| 254 | |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 255 | int symbol__tui_annotate(struct symbol *sym, struct map *map, int evidx, |
| 256 | int refresh) |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 257 | { |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 258 | struct objdump_line *pos, *n; |
Lin Ming | db9a9cbc | 2011-04-08 14:31:26 +0800 | [diff] [blame] | 259 | struct annotation *notes; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 260 | struct annotate_browser browser = { |
| 261 | .b = { |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 262 | .refresh = ui_browser__list_head_refresh, |
| 263 | .seek = ui_browser__list_head_seek, |
| 264 | .write = annotate_browser__write, |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 265 | .priv = sym, |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 266 | }, |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 267 | }; |
| 268 | int ret; |
| 269 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 270 | if (sym == NULL) |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 271 | return -1; |
| 272 | |
Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 273 | if (map->dso->annotate_warned) |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 274 | return -1; |
| 275 | |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 276 | if (symbol__annotate(sym, map, sizeof(struct objdump_line_rb_node)) < 0) { |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 277 | ui__error_window(ui_helpline__last_msg); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | ui_helpline__push("Press <- or ESC to exit"); |
| 282 | |
Lin Ming | db9a9cbc | 2011-04-08 14:31:26 +0800 | [diff] [blame] | 283 | notes = symbol__annotation(sym); |
| 284 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 285 | list_for_each_entry(pos, ¬es->src->source, node) { |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 286 | struct objdump_line_rb_node *rbpos; |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 287 | size_t line_len = strlen(pos->line); |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 288 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 289 | if (browser.b.width < line_len) |
| 290 | browser.b.width = line_len; |
| 291 | rbpos = objdump_line__rb(pos); |
| 292 | rbpos->idx = browser.b.nr_entries++; |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 293 | } |
| 294 | |
Lin Ming | db9a9cbc | 2011-04-08 14:31:26 +0800 | [diff] [blame] | 295 | browser.b.entries = ¬es->src->source, |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 296 | browser.b.width += 18; /* Percentage */ |
Arnaldo Carvalho de Melo | c97cf42 | 2011-02-22 12:02:07 -0300 | [diff] [blame] | 297 | ret = annotate_browser__run(&browser, evidx, refresh); |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 298 | list_for_each_entry_safe(pos, n, ¬es->src->source, node) { |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 299 | list_del(&pos->node); |
| 300 | objdump_line__free(pos); |
| 301 | } |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 302 | return ret; |
| 303 | } |