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" |
| 4 | #include "../../hist.h" |
| 5 | #include "../../sort.h" |
| 6 | #include "../../symbol.h" |
| 7 | |
| 8 | static void ui__error_window(const char *fmt, ...) |
| 9 | { |
| 10 | va_list ap; |
| 11 | |
| 12 | va_start(ap, fmt); |
| 13 | newtWinMessagev((char *)"Error", (char *)"Ok", (char *)fmt, ap); |
| 14 | va_end(ap); |
| 15 | } |
| 16 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 17 | struct annotate_browser { |
| 18 | struct ui_browser b; |
| 19 | struct rb_root entries; |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame^] | 20 | struct rb_node *curr_hot; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | struct objdump_line_rb_node { |
| 24 | struct rb_node rb_node; |
| 25 | double percent; |
| 26 | u32 idx; |
| 27 | }; |
| 28 | |
| 29 | static inline |
| 30 | struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self) |
| 31 | { |
| 32 | return (struct objdump_line_rb_node *)(self + 1); |
| 33 | } |
| 34 | |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 35 | static void annotate_browser__write(struct ui_browser *self, void *entry, int row) |
| 36 | { |
| 37 | struct objdump_line *ol = rb_entry(entry, struct objdump_line, node); |
| 38 | bool current_entry = ui_browser__is_current_entry(self, row); |
| 39 | int width = self->width; |
| 40 | |
| 41 | if (ol->offset != -1) { |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 42 | struct objdump_line_rb_node *olrb = objdump_line__rb(ol); |
| 43 | int color = ui_browser__percent_color(olrb->percent, current_entry); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 44 | SLsmg_set_color(color); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 45 | slsmg_printf(" %7.2f ", olrb->percent); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 46 | if (!current_entry) |
| 47 | SLsmg_set_color(HE_COLORSET_CODE); |
| 48 | } else { |
| 49 | int color = ui_browser__percent_color(0, current_entry); |
| 50 | SLsmg_set_color(color); |
| 51 | slsmg_write_nstring(" ", 9); |
| 52 | } |
| 53 | |
| 54 | SLsmg_write_char(':'); |
| 55 | slsmg_write_nstring(" ", 8); |
| 56 | if (!*ol->line) |
| 57 | slsmg_write_nstring(" ", width - 18); |
| 58 | else |
| 59 | slsmg_write_nstring(ol->line, width - 18); |
| 60 | } |
| 61 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 62 | static double objdump_line__calc_percent(struct objdump_line *self, |
| 63 | struct list_head *head, |
| 64 | struct symbol *sym) |
| 65 | { |
| 66 | double percent = 0.0; |
| 67 | |
| 68 | if (self->offset != -1) { |
| 69 | int len = sym->end - sym->start; |
| 70 | unsigned int hits = 0; |
| 71 | struct sym_priv *priv = symbol__priv(sym); |
| 72 | struct sym_ext *sym_ext = priv->ext; |
| 73 | struct sym_hist *h = priv->hist; |
| 74 | s64 offset = self->offset; |
| 75 | struct objdump_line *next = objdump__get_next_ip_line(head, self); |
| 76 | |
| 77 | |
| 78 | while (offset < (s64)len && |
| 79 | (next == NULL || offset < next->offset)) { |
| 80 | if (sym_ext) { |
| 81 | percent += sym_ext[offset].percent; |
| 82 | } else |
| 83 | hits += h->ip[offset]; |
| 84 | |
| 85 | ++offset; |
| 86 | } |
| 87 | |
| 88 | if (sym_ext == NULL && h->sum) |
| 89 | percent = 100.0 * hits / h->sum; |
| 90 | } |
| 91 | |
| 92 | return percent; |
| 93 | } |
| 94 | |
| 95 | static void objdump__insert_line(struct rb_root *self, |
| 96 | struct objdump_line_rb_node *line) |
| 97 | { |
| 98 | struct rb_node **p = &self->rb_node; |
| 99 | struct rb_node *parent = NULL; |
| 100 | struct objdump_line_rb_node *l; |
| 101 | |
| 102 | while (*p != NULL) { |
| 103 | parent = *p; |
| 104 | l = rb_entry(parent, struct objdump_line_rb_node, rb_node); |
| 105 | if (line->percent < l->percent) |
| 106 | p = &(*p)->rb_left; |
| 107 | else |
| 108 | p = &(*p)->rb_right; |
| 109 | } |
| 110 | rb_link_node(&line->rb_node, parent, p); |
| 111 | rb_insert_color(&line->rb_node, self); |
| 112 | } |
| 113 | |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame^] | 114 | static void annotate_browser__set_top(struct annotate_browser *self, |
| 115 | struct rb_node *nd) |
| 116 | { |
| 117 | struct objdump_line_rb_node *rbpos; |
| 118 | struct objdump_line *pos; |
| 119 | unsigned back; |
| 120 | |
| 121 | ui_browser__refresh_dimensions(&self->b); |
| 122 | back = self->b.height / 2; |
| 123 | rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node); |
| 124 | pos = ((struct objdump_line *)rbpos) - 1; |
| 125 | self->b.top_idx = self->b.index = rbpos->idx; |
| 126 | |
| 127 | while (self->b.top_idx != 0 && back != 0) { |
| 128 | pos = list_entry(pos->node.prev, struct objdump_line, node); |
| 129 | |
| 130 | --self->b.top_idx; |
| 131 | --back; |
| 132 | } |
| 133 | |
| 134 | self->b.top = pos; |
| 135 | self->curr_hot = nd; |
| 136 | } |
| 137 | |
| 138 | static int annotate_browser__run(struct annotate_browser *self, |
| 139 | struct newtExitStruct *es) |
| 140 | { |
| 141 | struct rb_node *nd; |
| 142 | struct hist_entry *he = self->b.priv; |
| 143 | |
| 144 | if (ui_browser__show(&self->b, he->ms.sym->name) < 0) |
| 145 | return -1; |
| 146 | |
| 147 | ui_helpline__fpush("<- or ESC: exit, TAB/shift+TAB: cycle thru samples"); |
| 148 | newtFormAddHotKey(self->b.form, NEWT_KEY_LEFT); |
| 149 | |
| 150 | nd = self->curr_hot; |
| 151 | if (nd) { |
| 152 | newtFormAddHotKey(self->b.form, NEWT_KEY_TAB); |
| 153 | newtFormAddHotKey(self->b.form, NEWT_KEY_UNTAB); |
| 154 | } |
| 155 | |
| 156 | while (1) { |
| 157 | ui_browser__run(&self->b, es); |
| 158 | |
| 159 | if (es->reason != NEWT_EXIT_HOTKEY) |
| 160 | break; |
| 161 | |
| 162 | switch (es->u.key) { |
| 163 | case NEWT_KEY_TAB: |
| 164 | nd = rb_prev(nd); |
| 165 | if (nd == NULL) |
| 166 | nd = rb_last(&self->entries); |
| 167 | annotate_browser__set_top(self, nd); |
| 168 | break; |
| 169 | case NEWT_KEY_UNTAB: |
| 170 | nd = rb_next(nd); |
| 171 | if (nd == NULL) |
| 172 | nd = rb_first(&self->entries); |
| 173 | annotate_browser__set_top(self, nd); |
| 174 | break; |
| 175 | default: |
| 176 | goto out; |
| 177 | } |
| 178 | } |
| 179 | out: |
| 180 | newtFormDestroy(self->b.form); |
| 181 | newtPopWindow(); |
| 182 | ui_helpline__pop(); |
| 183 | return 0; |
| 184 | } |
| 185 | |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 186 | int hist_entry__tui_annotate(struct hist_entry *self) |
| 187 | { |
| 188 | struct newtExitStruct es; |
| 189 | struct objdump_line *pos, *n; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 190 | struct objdump_line_rb_node *rbpos; |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 191 | LIST_HEAD(head); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 192 | struct annotate_browser browser = { |
| 193 | .b = { |
| 194 | .entries = &head, |
| 195 | .refresh = ui_browser__list_head_refresh, |
| 196 | .seek = ui_browser__list_head_seek, |
| 197 | .write = annotate_browser__write, |
| 198 | .priv = self, |
| 199 | }, |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 200 | }; |
| 201 | int ret; |
| 202 | |
| 203 | if (self->ms.sym == NULL) |
| 204 | return -1; |
| 205 | |
| 206 | if (self->ms.map->dso->annotate_warned) |
| 207 | return -1; |
| 208 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 209 | if (hist_entry__annotate(self, &head, sizeof(*rbpos)) < 0) { |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 210 | ui__error_window(ui_helpline__last_msg); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | ui_helpline__push("Press <- or ESC to exit"); |
| 215 | |
| 216 | list_for_each_entry(pos, &head, node) { |
| 217 | size_t line_len = strlen(pos->line); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 218 | if (browser.b.width < line_len) |
| 219 | browser.b.width = line_len; |
| 220 | rbpos = objdump_line__rb(pos); |
| 221 | rbpos->idx = browser.b.nr_entries++; |
| 222 | rbpos->percent = objdump_line__calc_percent(pos, &head, self->ms.sym); |
| 223 | if (rbpos->percent < 0.01) |
| 224 | continue; |
| 225 | objdump__insert_line(&browser.entries, rbpos); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 226 | } |
| 227 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 228 | /* |
| 229 | * Position the browser at the hottest line. |
| 230 | */ |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame^] | 231 | browser.curr_hot = rb_last(&browser.entries); |
| 232 | if (browser.curr_hot) |
| 233 | annotate_browser__set_top(&browser, browser.curr_hot); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame] | 234 | |
| 235 | browser.b.width += 18; /* Percentage */ |
Arnaldo Carvalho de Melo | f1e9214 | 2010-08-10 15:14:53 -0300 | [diff] [blame^] | 236 | ret = annotate_browser__run(&browser, &es); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 237 | list_for_each_entry_safe(pos, n, &head, node) { |
| 238 | list_del(&pos->node); |
| 239 | objdump_line__free(pos); |
| 240 | } |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 241 | return ret; |
| 242 | } |