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; |
| 20 | }; |
| 21 | |
| 22 | struct objdump_line_rb_node { |
| 23 | struct rb_node rb_node; |
| 24 | double percent; |
| 25 | u32 idx; |
| 26 | }; |
| 27 | |
| 28 | static inline |
| 29 | struct objdump_line_rb_node *objdump_line__rb(struct objdump_line *self) |
| 30 | { |
| 31 | return (struct objdump_line_rb_node *)(self + 1); |
| 32 | } |
| 33 | |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 34 | static void annotate_browser__write(struct ui_browser *self, void *entry, int row) |
| 35 | { |
| 36 | struct objdump_line *ol = rb_entry(entry, struct objdump_line, node); |
| 37 | bool current_entry = ui_browser__is_current_entry(self, row); |
| 38 | int width = self->width; |
| 39 | |
| 40 | if (ol->offset != -1) { |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame^] | 41 | struct objdump_line_rb_node *olrb = objdump_line__rb(ol); |
| 42 | int color = ui_browser__percent_color(olrb->percent, current_entry); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 43 | SLsmg_set_color(color); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame^] | 44 | slsmg_printf(" %7.2f ", olrb->percent); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 45 | if (!current_entry) |
| 46 | SLsmg_set_color(HE_COLORSET_CODE); |
| 47 | } else { |
| 48 | int color = ui_browser__percent_color(0, current_entry); |
| 49 | SLsmg_set_color(color); |
| 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); |
| 59 | } |
| 60 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame^] | 61 | static double objdump_line__calc_percent(struct objdump_line *self, |
| 62 | struct list_head *head, |
| 63 | struct symbol *sym) |
| 64 | { |
| 65 | double percent = 0.0; |
| 66 | |
| 67 | if (self->offset != -1) { |
| 68 | int len = sym->end - sym->start; |
| 69 | unsigned int hits = 0; |
| 70 | struct sym_priv *priv = symbol__priv(sym); |
| 71 | struct sym_ext *sym_ext = priv->ext; |
| 72 | struct sym_hist *h = priv->hist; |
| 73 | s64 offset = self->offset; |
| 74 | struct objdump_line *next = objdump__get_next_ip_line(head, self); |
| 75 | |
| 76 | |
| 77 | while (offset < (s64)len && |
| 78 | (next == NULL || offset < next->offset)) { |
| 79 | if (sym_ext) { |
| 80 | percent += sym_ext[offset].percent; |
| 81 | } else |
| 82 | hits += h->ip[offset]; |
| 83 | |
| 84 | ++offset; |
| 85 | } |
| 86 | |
| 87 | if (sym_ext == NULL && h->sum) |
| 88 | percent = 100.0 * hits / h->sum; |
| 89 | } |
| 90 | |
| 91 | return percent; |
| 92 | } |
| 93 | |
| 94 | static void objdump__insert_line(struct rb_root *self, |
| 95 | struct objdump_line_rb_node *line) |
| 96 | { |
| 97 | struct rb_node **p = &self->rb_node; |
| 98 | struct rb_node *parent = NULL; |
| 99 | struct objdump_line_rb_node *l; |
| 100 | |
| 101 | while (*p != NULL) { |
| 102 | parent = *p; |
| 103 | l = rb_entry(parent, struct objdump_line_rb_node, rb_node); |
| 104 | if (line->percent < l->percent) |
| 105 | p = &(*p)->rb_left; |
| 106 | else |
| 107 | p = &(*p)->rb_right; |
| 108 | } |
| 109 | rb_link_node(&line->rb_node, parent, p); |
| 110 | rb_insert_color(&line->rb_node, self); |
| 111 | } |
| 112 | |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 113 | int hist_entry__tui_annotate(struct hist_entry *self) |
| 114 | { |
| 115 | struct newtExitStruct es; |
| 116 | struct objdump_line *pos, *n; |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame^] | 117 | struct objdump_line_rb_node *rbpos; |
| 118 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 119 | LIST_HEAD(head); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame^] | 120 | struct annotate_browser browser = { |
| 121 | .b = { |
| 122 | .entries = &head, |
| 123 | .refresh = ui_browser__list_head_refresh, |
| 124 | .seek = ui_browser__list_head_seek, |
| 125 | .write = annotate_browser__write, |
| 126 | .priv = self, |
| 127 | }, |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 128 | }; |
| 129 | int ret; |
| 130 | |
| 131 | if (self->ms.sym == NULL) |
| 132 | return -1; |
| 133 | |
| 134 | if (self->ms.map->dso->annotate_warned) |
| 135 | return -1; |
| 136 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame^] | 137 | if (hist_entry__annotate(self, &head, sizeof(*rbpos)) < 0) { |
Arnaldo Carvalho de Melo | 1e6dd07 | 2010-08-10 15:58:50 -0300 | [diff] [blame] | 138 | ui__error_window(ui_helpline__last_msg); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | ui_helpline__push("Press <- or ESC to exit"); |
| 143 | |
| 144 | list_for_each_entry(pos, &head, node) { |
| 145 | size_t line_len = strlen(pos->line); |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame^] | 146 | if (browser.b.width < line_len) |
| 147 | browser.b.width = line_len; |
| 148 | rbpos = objdump_line__rb(pos); |
| 149 | rbpos->idx = browser.b.nr_entries++; |
| 150 | rbpos->percent = objdump_line__calc_percent(pos, &head, self->ms.sym); |
| 151 | if (rbpos->percent < 0.01) |
| 152 | continue; |
| 153 | objdump__insert_line(&browser.entries, rbpos); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 154 | } |
| 155 | |
Arnaldo Carvalho de Melo | 9222116 | 2010-08-09 15:30:40 -0300 | [diff] [blame^] | 156 | /* |
| 157 | * Position the browser at the hottest line. |
| 158 | */ |
| 159 | nd = rb_last(&browser.entries); |
| 160 | if (nd != NULL) { |
| 161 | unsigned back; |
| 162 | |
| 163 | ui_browser__refresh_dimensions(&browser.b); |
| 164 | back = browser.b.height / 2; |
| 165 | rbpos = rb_entry(nd, struct objdump_line_rb_node, rb_node); |
| 166 | pos = ((struct objdump_line *)rbpos) - 1; |
| 167 | browser.b.top_idx = browser.b.index = rbpos->idx; |
| 168 | |
| 169 | while (browser.b.top_idx != 0 && back != 0) { |
| 170 | pos = list_entry(pos->node.prev, struct objdump_line, node); |
| 171 | |
| 172 | --browser.b.top_idx; |
| 173 | --back; |
| 174 | } |
| 175 | |
| 176 | browser.b.top = pos; |
| 177 | } |
| 178 | |
| 179 | browser.b.width += 18; /* Percentage */ |
| 180 | ui_browser__show(&browser.b, self->ms.sym->name); |
| 181 | newtFormAddHotKey(browser.b.form, ' '); |
| 182 | ret = ui_browser__run(&browser.b, &es); |
| 183 | newtFormDestroy(browser.b.form); |
Arnaldo Carvalho de Melo | 211ef12 | 2010-08-10 14:54:09 -0300 | [diff] [blame] | 184 | newtPopWindow(); |
| 185 | list_for_each_entry_safe(pos, n, &head, node) { |
| 186 | list_del(&pos->node); |
| 187 | objdump_line__free(pos); |
| 188 | } |
| 189 | ui_helpline__pop(); |
| 190 | return ret; |
| 191 | } |