Arnaldo Carvalho de Melo | 78f7def | 2011-02-04 09:45:46 -0200 | [diff] [blame] | 1 | #include "annotate.h" |
Frederic Weisbecker | 8a0ecfb | 2010-05-13 19:47:16 +0200 | [diff] [blame] | 2 | #include "util.h" |
Frederic Weisbecker | 598357e | 2010-05-21 12:48:39 +0200 | [diff] [blame] | 3 | #include "build-id.h" |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 4 | #include "hist.h" |
Arnaldo Carvalho de Melo | 4e4f06e | 2009-12-14 13:10:39 -0200 | [diff] [blame] | 5 | #include "session.h" |
| 6 | #include "sort.h" |
Arnaldo Carvalho de Melo | 9b33827 | 2009-12-16 14:31:49 -0200 | [diff] [blame] | 7 | #include <math.h> |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 8 | |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 9 | static bool hists__filter_entry_by_dso(struct hists *hists, |
| 10 | struct hist_entry *he); |
| 11 | static bool hists__filter_entry_by_thread(struct hists *hists, |
| 12 | struct hist_entry *he); |
Namhyung Kim | e94d53e | 2012-03-16 17:50:51 +0900 | [diff] [blame] | 13 | static bool hists__filter_entry_by_symbol(struct hists *hists, |
| 14 | struct hist_entry *he); |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 15 | |
Arnaldo Carvalho de Melo | 7a007ca | 2010-07-21 09:19:41 -0300 | [diff] [blame] | 16 | enum hist_filter { |
| 17 | HIST_FILTER__DSO, |
| 18 | HIST_FILTER__THREAD, |
| 19 | HIST_FILTER__PARENT, |
Namhyung Kim | e94d53e | 2012-03-16 17:50:51 +0900 | [diff] [blame] | 20 | HIST_FILTER__SYMBOL, |
Arnaldo Carvalho de Melo | 7a007ca | 2010-07-21 09:19:41 -0300 | [diff] [blame] | 21 | }; |
| 22 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 23 | struct callchain_param callchain_param = { |
| 24 | .mode = CHAIN_GRAPH_REL, |
Sam Liao | d797fdc | 2011-06-07 23:49:46 +0800 | [diff] [blame] | 25 | .min_percent = 0.5, |
| 26 | .order = ORDER_CALLEE |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 27 | }; |
| 28 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 29 | u16 hists__col_len(struct hists *hists, enum hist_column col) |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 30 | { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 31 | return hists->col_len[col]; |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 32 | } |
| 33 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 34 | void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len) |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 35 | { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 36 | hists->col_len[col] = len; |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 37 | } |
| 38 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 39 | bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len) |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 40 | { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 41 | if (len > hists__col_len(hists, col)) { |
| 42 | hists__set_col_len(hists, col, len); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 43 | return true; |
| 44 | } |
| 45 | return false; |
| 46 | } |
| 47 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 48 | void hists__reset_col_len(struct hists *hists) |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 49 | { |
| 50 | enum hist_column col; |
| 51 | |
| 52 | for (col = 0; col < HISTC_NR_COLS; ++col) |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 53 | hists__set_col_len(hists, col, 0); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 54 | } |
| 55 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 56 | static void hists__set_unres_dso_col_len(struct hists *hists, int dso) |
| 57 | { |
| 58 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; |
| 59 | |
| 60 | if (hists__col_len(hists, dso) < unresolved_col_width && |
| 61 | !symbol_conf.col_width_list_str && !symbol_conf.field_sep && |
| 62 | !symbol_conf.dso_list) |
| 63 | hists__set_col_len(hists, dso, unresolved_col_width); |
| 64 | } |
| 65 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 66 | void hists__calc_col_len(struct hists *hists, struct hist_entry *h) |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 67 | { |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 68 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 69 | u16 len; |
| 70 | |
| 71 | if (h->ms.sym) |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 72 | hists__new_col_len(hists, HISTC_SYMBOL, h->ms.sym->namelen + 4); |
| 73 | else |
| 74 | hists__set_unres_dso_col_len(hists, HISTC_DSO); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 75 | |
| 76 | len = thread__comm_len(h->thread); |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 77 | if (hists__new_col_len(hists, HISTC_COMM, len)) |
| 78 | hists__set_col_len(hists, HISTC_THREAD, len + 6); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 79 | |
| 80 | if (h->ms.map) { |
| 81 | len = dso__name_len(h->ms.map->dso); |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 82 | hists__new_col_len(hists, HISTC_DSO, len); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 83 | } |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 84 | |
| 85 | if (h->branch_info) { |
| 86 | int symlen; |
| 87 | /* |
| 88 | * +4 accounts for '[x] ' priv level info |
| 89 | * +2 account of 0x prefix on raw addresses |
| 90 | */ |
| 91 | if (h->branch_info->from.sym) { |
| 92 | symlen = (int)h->branch_info->from.sym->namelen + 4; |
| 93 | hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen); |
| 94 | |
| 95 | symlen = dso__name_len(h->branch_info->from.map->dso); |
| 96 | hists__new_col_len(hists, HISTC_DSO_FROM, symlen); |
| 97 | } else { |
| 98 | symlen = unresolved_col_width + 4 + 2; |
| 99 | hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen); |
| 100 | hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM); |
| 101 | } |
| 102 | |
| 103 | if (h->branch_info->to.sym) { |
| 104 | symlen = (int)h->branch_info->to.sym->namelen + 4; |
| 105 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); |
| 106 | |
| 107 | symlen = dso__name_len(h->branch_info->to.map->dso); |
| 108 | hists__new_col_len(hists, HISTC_DSO_TO, symlen); |
| 109 | } else { |
| 110 | symlen = unresolved_col_width + 4 + 2; |
| 111 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); |
| 112 | hists__set_unres_dso_col_len(hists, HISTC_DSO_TO); |
| 113 | } |
| 114 | } |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 115 | } |
| 116 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 117 | void hists__output_recalc_col_len(struct hists *hists, int max_rows) |
| 118 | { |
| 119 | struct rb_node *next = rb_first(&hists->entries); |
| 120 | struct hist_entry *n; |
| 121 | int row = 0; |
| 122 | |
| 123 | hists__reset_col_len(hists); |
| 124 | |
| 125 | while (next && row++ < max_rows) { |
| 126 | n = rb_entry(next, struct hist_entry, rb_node); |
| 127 | if (!n->filtered) |
| 128 | hists__calc_col_len(hists, n); |
| 129 | next = rb_next(&n->rb_node); |
| 130 | } |
| 131 | } |
| 132 | |
Arnaldo Carvalho de Melo | 12c1427 | 2012-01-04 12:27:03 -0200 | [diff] [blame] | 133 | static void hist_entry__add_cpumode_period(struct hist_entry *he, |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 134 | unsigned int cpumode, u64 period) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 135 | { |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 136 | switch (cpumode) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 137 | case PERF_RECORD_MISC_KERNEL: |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 138 | he->stat.period_sys += period; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 139 | break; |
| 140 | case PERF_RECORD_MISC_USER: |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 141 | he->stat.period_us += period; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 142 | break; |
| 143 | case PERF_RECORD_MISC_GUEST_KERNEL: |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 144 | he->stat.period_guest_sys += period; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 145 | break; |
| 146 | case PERF_RECORD_MISC_GUEST_USER: |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 147 | he->stat.period_guest_us += period; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 148 | break; |
| 149 | default: |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | |
Namhyung Kim | 139c081 | 2012-10-04 21:49:43 +0900 | [diff] [blame] | 154 | static void he_stat__add_period(struct he_stat *he_stat, u64 period) |
| 155 | { |
| 156 | he_stat->period += period; |
| 157 | he_stat->nr_events += 1; |
| 158 | } |
| 159 | |
| 160 | static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src) |
| 161 | { |
| 162 | dest->period += src->period; |
| 163 | dest->period_sys += src->period_sys; |
| 164 | dest->period_us += src->period_us; |
| 165 | dest->period_guest_sys += src->period_guest_sys; |
| 166 | dest->period_guest_us += src->period_guest_us; |
| 167 | dest->nr_events += src->nr_events; |
| 168 | } |
| 169 | |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 170 | static void hist_entry__decay(struct hist_entry *he) |
| 171 | { |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 172 | he->stat.period = (he->stat.period * 7) / 8; |
| 173 | he->stat.nr_events = (he->stat.nr_events * 7) / 8; |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static bool hists__decay_entry(struct hists *hists, struct hist_entry *he) |
| 177 | { |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 178 | u64 prev_period = he->stat.period; |
Arnaldo Carvalho de Melo | c64550c | 2011-10-20 06:45:44 -0200 | [diff] [blame] | 179 | |
| 180 | if (prev_period == 0) |
Arnaldo Carvalho de Melo | df71d95 | 2011-10-13 08:01:33 -0300 | [diff] [blame] | 181 | return true; |
Arnaldo Carvalho de Melo | c64550c | 2011-10-20 06:45:44 -0200 | [diff] [blame] | 182 | |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 183 | hist_entry__decay(he); |
Arnaldo Carvalho de Melo | c64550c | 2011-10-20 06:45:44 -0200 | [diff] [blame] | 184 | |
| 185 | if (!he->filtered) |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 186 | hists->stats.total_period -= prev_period - he->stat.period; |
Arnaldo Carvalho de Melo | c64550c | 2011-10-20 06:45:44 -0200 | [diff] [blame] | 187 | |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 188 | return he->stat.period == 0; |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 189 | } |
| 190 | |
Arnaldo Carvalho de Melo | b079d4e | 2011-10-17 09:05:04 -0200 | [diff] [blame] | 191 | static void __hists__decay_entries(struct hists *hists, bool zap_user, |
| 192 | bool zap_kernel, bool threaded) |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 193 | { |
| 194 | struct rb_node *next = rb_first(&hists->entries); |
| 195 | struct hist_entry *n; |
| 196 | |
| 197 | while (next) { |
| 198 | n = rb_entry(next, struct hist_entry, rb_node); |
| 199 | next = rb_next(&n->rb_node); |
Arnaldo Carvalho de Melo | df71d95 | 2011-10-13 08:01:33 -0300 | [diff] [blame] | 200 | /* |
| 201 | * We may be annotating this, for instance, so keep it here in |
| 202 | * case some it gets new samples, we'll eventually free it when |
| 203 | * the user stops browsing and it agains gets fully decayed. |
| 204 | */ |
Arnaldo Carvalho de Melo | b079d4e | 2011-10-17 09:05:04 -0200 | [diff] [blame] | 205 | if (((zap_user && n->level == '.') || |
| 206 | (zap_kernel && n->level != '.') || |
| 207 | hists__decay_entry(hists, n)) && |
| 208 | !n->used) { |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 209 | rb_erase(&n->rb_node, &hists->entries); |
| 210 | |
Arnaldo Carvalho de Melo | e345fa1 | 2011-10-13 09:06:54 -0300 | [diff] [blame] | 211 | if (sort__need_collapse || threaded) |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 212 | rb_erase(&n->rb_node_in, &hists->entries_collapsed); |
| 213 | |
| 214 | hist_entry__free(n); |
| 215 | --hists->nr_entries; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
Arnaldo Carvalho de Melo | b079d4e | 2011-10-17 09:05:04 -0200 | [diff] [blame] | 220 | void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel) |
Arnaldo Carvalho de Melo | e345fa1 | 2011-10-13 09:06:54 -0300 | [diff] [blame] | 221 | { |
Arnaldo Carvalho de Melo | b079d4e | 2011-10-17 09:05:04 -0200 | [diff] [blame] | 222 | return __hists__decay_entries(hists, zap_user, zap_kernel, false); |
Arnaldo Carvalho de Melo | e345fa1 | 2011-10-13 09:06:54 -0300 | [diff] [blame] | 223 | } |
| 224 | |
Arnaldo Carvalho de Melo | b079d4e | 2011-10-17 09:05:04 -0200 | [diff] [blame] | 225 | void hists__decay_entries_threaded(struct hists *hists, |
| 226 | bool zap_user, bool zap_kernel) |
Arnaldo Carvalho de Melo | e345fa1 | 2011-10-13 09:06:54 -0300 | [diff] [blame] | 227 | { |
Arnaldo Carvalho de Melo | b079d4e | 2011-10-17 09:05:04 -0200 | [diff] [blame] | 228 | return __hists__decay_entries(hists, zap_user, zap_kernel, true); |
Arnaldo Carvalho de Melo | e345fa1 | 2011-10-13 09:06:54 -0300 | [diff] [blame] | 229 | } |
| 230 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 231 | /* |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 232 | * histogram, sorted on item, collects periods |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 233 | */ |
| 234 | |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 235 | static struct hist_entry *hist_entry__new(struct hist_entry *template) |
| 236 | { |
Frederic Weisbecker | d2009c5 | 2010-08-22 20:05:22 +0200 | [diff] [blame] | 237 | size_t callchain_size = symbol_conf.use_callchain ? sizeof(struct callchain_root) : 0; |
Arnaldo Carvalho de Melo | 12c1427 | 2012-01-04 12:27:03 -0200 | [diff] [blame] | 238 | struct hist_entry *he = malloc(sizeof(*he) + callchain_size); |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 239 | |
Arnaldo Carvalho de Melo | 12c1427 | 2012-01-04 12:27:03 -0200 | [diff] [blame] | 240 | if (he != NULL) { |
| 241 | *he = *template; |
Namhyung Kim | c4b3535 | 2012-10-04 21:49:42 +0900 | [diff] [blame] | 242 | |
Arnaldo Carvalho de Melo | 12c1427 | 2012-01-04 12:27:03 -0200 | [diff] [blame] | 243 | if (he->ms.map) |
| 244 | he->ms.map->referenced = true; |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 245 | if (symbol_conf.use_callchain) |
Arnaldo Carvalho de Melo | 12c1427 | 2012-01-04 12:27:03 -0200 | [diff] [blame] | 246 | callchain_init(he->callchain); |
Arnaldo Carvalho de Melo | b821c73 | 2012-10-25 14:42:45 -0200 | [diff] [blame] | 247 | |
| 248 | INIT_LIST_HEAD(&he->pairs.node); |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 249 | } |
| 250 | |
Arnaldo Carvalho de Melo | 12c1427 | 2012-01-04 12:27:03 -0200 | [diff] [blame] | 251 | return he; |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 252 | } |
| 253 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 254 | static void hists__inc_nr_entries(struct hists *hists, struct hist_entry *h) |
Arnaldo Carvalho de Melo | fefb0b9 | 2010-05-10 13:57:51 -0300 | [diff] [blame] | 255 | { |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 256 | if (!h->filtered) { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 257 | hists__calc_col_len(hists, h); |
| 258 | ++hists->nr_entries; |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 259 | hists->stats.total_period += h->stat.period; |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 260 | } |
Arnaldo Carvalho de Melo | fefb0b9 | 2010-05-10 13:57:51 -0300 | [diff] [blame] | 261 | } |
| 262 | |
Arnaldo Carvalho de Melo | 7a007ca | 2010-07-21 09:19:41 -0300 | [diff] [blame] | 263 | static u8 symbol__parent_filter(const struct symbol *parent) |
| 264 | { |
| 265 | if (symbol_conf.exclude_other && parent == NULL) |
| 266 | return 1 << HIST_FILTER__PARENT; |
| 267 | return 0; |
| 268 | } |
| 269 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 270 | static struct hist_entry *add_hist_entry(struct hists *hists, |
| 271 | struct hist_entry *entry, |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 272 | struct addr_location *al, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 273 | u64 period) |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 274 | { |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 275 | struct rb_node **p; |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 276 | struct rb_node *parent = NULL; |
| 277 | struct hist_entry *he; |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 278 | int cmp; |
| 279 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 280 | pthread_mutex_lock(&hists->lock); |
| 281 | |
| 282 | p = &hists->entries_in->rb_node; |
| 283 | |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 284 | while (*p != NULL) { |
| 285 | parent = *p; |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 286 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 287 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 288 | cmp = hist_entry__cmp(entry, he); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 289 | |
| 290 | if (!cmp) { |
Namhyung Kim | 139c081 | 2012-10-04 21:49:43 +0900 | [diff] [blame] | 291 | he_stat__add_period(&he->stat, period); |
David Miller | 63fa471 | 2012-03-27 03:14:18 -0400 | [diff] [blame] | 292 | |
| 293 | /* If the map of an existing hist_entry has |
| 294 | * become out-of-date due to an exec() or |
| 295 | * similar, update it. Otherwise we will |
| 296 | * mis-adjust symbol addresses when computing |
| 297 | * the history counter to increment. |
| 298 | */ |
| 299 | if (he->ms.map != entry->ms.map) { |
| 300 | he->ms.map = entry->ms.map; |
| 301 | if (he->ms.map) |
| 302 | he->ms.map->referenced = true; |
| 303 | } |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 304 | goto out; |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | if (cmp < 0) |
| 308 | p = &(*p)->rb_left; |
| 309 | else |
| 310 | p = &(*p)->rb_right; |
| 311 | } |
| 312 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 313 | he = hist_entry__new(entry); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 314 | if (!he) |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 315 | goto out_unlock; |
| 316 | |
| 317 | rb_link_node(&he->rb_node_in, parent, p); |
| 318 | rb_insert_color(&he->rb_node_in, hists->entries_in); |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 319 | out: |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 320 | hist_entry__add_cpumode_period(he, al->cpumode, period); |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 321 | out_unlock: |
| 322 | pthread_mutex_unlock(&hists->lock); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 323 | return he; |
| 324 | } |
| 325 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 326 | struct hist_entry *__hists__add_branch_entry(struct hists *self, |
| 327 | struct addr_location *al, |
| 328 | struct symbol *sym_parent, |
| 329 | struct branch_info *bi, |
| 330 | u64 period) |
| 331 | { |
| 332 | struct hist_entry entry = { |
| 333 | .thread = al->thread, |
| 334 | .ms = { |
| 335 | .map = bi->to.map, |
| 336 | .sym = bi->to.sym, |
| 337 | }, |
| 338 | .cpu = al->cpu, |
| 339 | .ip = bi->to.addr, |
| 340 | .level = al->level, |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 341 | .stat = { |
| 342 | .period = period, |
Namhyung Kim | c4b3535 | 2012-10-04 21:49:42 +0900 | [diff] [blame] | 343 | .nr_events = 1, |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 344 | }, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 345 | .parent = sym_parent, |
| 346 | .filtered = symbol__parent_filter(sym_parent), |
| 347 | .branch_info = bi, |
Jiri Olsa | ae359f1 | 2012-10-04 21:49:35 +0900 | [diff] [blame] | 348 | .hists = self, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 349 | }; |
| 350 | |
| 351 | return add_hist_entry(self, &entry, al, period); |
| 352 | } |
| 353 | |
| 354 | struct hist_entry *__hists__add_entry(struct hists *self, |
| 355 | struct addr_location *al, |
| 356 | struct symbol *sym_parent, u64 period) |
| 357 | { |
| 358 | struct hist_entry entry = { |
| 359 | .thread = al->thread, |
| 360 | .ms = { |
| 361 | .map = al->map, |
| 362 | .sym = al->sym, |
| 363 | }, |
| 364 | .cpu = al->cpu, |
| 365 | .ip = al->addr, |
| 366 | .level = al->level, |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 367 | .stat = { |
| 368 | .period = period, |
Namhyung Kim | c4b3535 | 2012-10-04 21:49:42 +0900 | [diff] [blame] | 369 | .nr_events = 1, |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 370 | }, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 371 | .parent = sym_parent, |
| 372 | .filtered = symbol__parent_filter(sym_parent), |
Jiri Olsa | ae359f1 | 2012-10-04 21:49:35 +0900 | [diff] [blame] | 373 | .hists = self, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 374 | }; |
| 375 | |
| 376 | return add_hist_entry(self, &entry, al, period); |
| 377 | } |
| 378 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 379 | int64_t |
| 380 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) |
| 381 | { |
| 382 | struct sort_entry *se; |
| 383 | int64_t cmp = 0; |
| 384 | |
| 385 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
Frederic Weisbecker | fcd1498 | 2010-04-14 19:11:29 +0200 | [diff] [blame] | 386 | cmp = se->se_cmp(left, right); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 387 | if (cmp) |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | return cmp; |
| 392 | } |
| 393 | |
| 394 | int64_t |
| 395 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) |
| 396 | { |
| 397 | struct sort_entry *se; |
| 398 | int64_t cmp = 0; |
| 399 | |
| 400 | list_for_each_entry(se, &hist_entry__sort_list, list) { |
| 401 | int64_t (*f)(struct hist_entry *, struct hist_entry *); |
| 402 | |
Frederic Weisbecker | fcd1498 | 2010-04-14 19:11:29 +0200 | [diff] [blame] | 403 | f = se->se_collapse ?: se->se_cmp; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 404 | |
| 405 | cmp = f(left, right); |
| 406 | if (cmp) |
| 407 | break; |
| 408 | } |
| 409 | |
| 410 | return cmp; |
| 411 | } |
| 412 | |
| 413 | void hist_entry__free(struct hist_entry *he) |
| 414 | { |
Namhyung Kim | 580e338 | 2012-11-07 16:27:14 +0900 | [diff] [blame] | 415 | free(he->branch_info); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 416 | free(he); |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * collapse the histogram |
| 421 | */ |
| 422 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 423 | static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused, |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 424 | struct rb_root *root, |
| 425 | struct hist_entry *he) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 426 | { |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 427 | struct rb_node **p = &root->rb_node; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 428 | struct rb_node *parent = NULL; |
| 429 | struct hist_entry *iter; |
| 430 | int64_t cmp; |
| 431 | |
| 432 | while (*p != NULL) { |
| 433 | parent = *p; |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 434 | iter = rb_entry(parent, struct hist_entry, rb_node_in); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 435 | |
| 436 | cmp = hist_entry__collapse(iter, he); |
| 437 | |
| 438 | if (!cmp) { |
Namhyung Kim | 139c081 | 2012-10-04 21:49:43 +0900 | [diff] [blame] | 439 | he_stat__add_stat(&iter->stat, &he->stat); |
Namhyung Kim | 9ec6097 | 2012-09-26 16:47:28 +0900 | [diff] [blame] | 440 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 441 | if (symbol_conf.use_callchain) { |
Namhyung Kim | 4726064 | 2012-05-31 14:43:26 +0900 | [diff] [blame] | 442 | callchain_cursor_reset(&callchain_cursor); |
| 443 | callchain_merge(&callchain_cursor, |
| 444 | iter->callchain, |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 445 | he->callchain); |
| 446 | } |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 447 | hist_entry__free(he); |
Arnaldo Carvalho de Melo | fefb0b9 | 2010-05-10 13:57:51 -0300 | [diff] [blame] | 448 | return false; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | if (cmp < 0) |
| 452 | p = &(*p)->rb_left; |
| 453 | else |
| 454 | p = &(*p)->rb_right; |
| 455 | } |
| 456 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 457 | rb_link_node(&he->rb_node_in, parent, p); |
| 458 | rb_insert_color(&he->rb_node_in, root); |
Arnaldo Carvalho de Melo | fefb0b9 | 2010-05-10 13:57:51 -0300 | [diff] [blame] | 459 | return true; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 462 | static struct rb_root *hists__get_rotate_entries_in(struct hists *hists) |
| 463 | { |
| 464 | struct rb_root *root; |
| 465 | |
| 466 | pthread_mutex_lock(&hists->lock); |
| 467 | |
| 468 | root = hists->entries_in; |
| 469 | if (++hists->entries_in > &hists->entries_in_array[1]) |
| 470 | hists->entries_in = &hists->entries_in_array[0]; |
| 471 | |
| 472 | pthread_mutex_unlock(&hists->lock); |
| 473 | |
| 474 | return root; |
| 475 | } |
| 476 | |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 477 | static void hists__apply_filters(struct hists *hists, struct hist_entry *he) |
| 478 | { |
| 479 | hists__filter_entry_by_dso(hists, he); |
| 480 | hists__filter_entry_by_thread(hists, he); |
Namhyung Kim | e94d53e | 2012-03-16 17:50:51 +0900 | [diff] [blame] | 481 | hists__filter_entry_by_symbol(hists, he); |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 482 | } |
| 483 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 484 | static void __hists__collapse_resort(struct hists *hists, bool threaded) |
| 485 | { |
| 486 | struct rb_root *root; |
| 487 | struct rb_node *next; |
| 488 | struct hist_entry *n; |
| 489 | |
| 490 | if (!sort__need_collapse && !threaded) |
| 491 | return; |
| 492 | |
| 493 | root = hists__get_rotate_entries_in(hists); |
| 494 | next = rb_first(root); |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 495 | |
| 496 | while (next) { |
| 497 | n = rb_entry(next, struct hist_entry, rb_node_in); |
| 498 | next = rb_next(&n->rb_node_in); |
| 499 | |
| 500 | rb_erase(&n->rb_node_in, root); |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 501 | if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) { |
| 502 | /* |
| 503 | * If it wasn't combined with one of the entries already |
| 504 | * collapsed, we need to apply the filters that may have |
| 505 | * been set by, say, the hist_browser. |
| 506 | */ |
| 507 | hists__apply_filters(hists, n); |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 508 | } |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 512 | void hists__collapse_resort(struct hists *hists) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 513 | { |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 514 | return __hists__collapse_resort(hists, false); |
| 515 | } |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 516 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 517 | void hists__collapse_resort_threaded(struct hists *hists) |
| 518 | { |
| 519 | return __hists__collapse_resort(hists, true); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /* |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 523 | * reverse the map, sort on period. |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 524 | */ |
| 525 | |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 526 | static void __hists__insert_output_entry(struct rb_root *entries, |
| 527 | struct hist_entry *he, |
| 528 | u64 min_callchain_hits) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 529 | { |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 530 | struct rb_node **p = &entries->rb_node; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 531 | struct rb_node *parent = NULL; |
| 532 | struct hist_entry *iter; |
| 533 | |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 534 | if (symbol_conf.use_callchain) |
Arnaldo Carvalho de Melo | b9fb930 | 2010-04-02 09:50:42 -0300 | [diff] [blame] | 535 | callchain_param.sort(&he->sorted_chain, he->callchain, |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 536 | min_callchain_hits, &callchain_param); |
| 537 | |
| 538 | while (*p != NULL) { |
| 539 | parent = *p; |
| 540 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 541 | |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 542 | if (he->stat.period > iter->stat.period) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 543 | p = &(*p)->rb_left; |
| 544 | else |
| 545 | p = &(*p)->rb_right; |
| 546 | } |
| 547 | |
| 548 | rb_link_node(&he->rb_node, parent, p); |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 549 | rb_insert_color(&he->rb_node, entries); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 550 | } |
| 551 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 552 | static void __hists__output_resort(struct hists *hists, bool threaded) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 553 | { |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 554 | struct rb_root *root; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 555 | struct rb_node *next; |
| 556 | struct hist_entry *n; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 557 | u64 min_callchain_hits; |
| 558 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 559 | min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 560 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 561 | if (sort__need_collapse || threaded) |
| 562 | root = &hists->entries_collapsed; |
| 563 | else |
| 564 | root = hists->entries_in; |
| 565 | |
| 566 | next = rb_first(root); |
| 567 | hists->entries = RB_ROOT; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 568 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 569 | hists->nr_entries = 0; |
Arnaldo Carvalho de Melo | 7928631 | 2011-10-27 09:19:48 -0200 | [diff] [blame] | 570 | hists->stats.total_period = 0; |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 571 | hists__reset_col_len(hists); |
Arnaldo Carvalho de Melo | fefb0b9 | 2010-05-10 13:57:51 -0300 | [diff] [blame] | 572 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 573 | while (next) { |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 574 | n = rb_entry(next, struct hist_entry, rb_node_in); |
| 575 | next = rb_next(&n->rb_node_in); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 576 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 577 | __hists__insert_output_entry(&hists->entries, n, min_callchain_hits); |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 578 | hists__inc_nr_entries(hists, n); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 579 | } |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 580 | } |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 581 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 582 | void hists__output_resort(struct hists *hists) |
| 583 | { |
| 584 | return __hists__output_resort(hists, false); |
| 585 | } |
| 586 | |
| 587 | void hists__output_resort_threaded(struct hists *hists) |
| 588 | { |
| 589 | return __hists__output_resort(hists, true); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 590 | } |
Arnaldo Carvalho de Melo | 4ecf84d0 | 2009-12-16 12:27:09 -0200 | [diff] [blame] | 591 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 592 | static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h, |
Arnaldo Carvalho de Melo | cc5edb0 | 2010-07-16 12:35:07 -0300 | [diff] [blame] | 593 | enum hist_filter filter) |
| 594 | { |
| 595 | h->filtered &= ~(1 << filter); |
| 596 | if (h->filtered) |
| 597 | return; |
| 598 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 599 | ++hists->nr_entries; |
Arnaldo Carvalho de Melo | 0f0cbf7 | 2010-07-26 17:13:40 -0300 | [diff] [blame] | 600 | if (h->ms.unfolded) |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 601 | hists->nr_entries += h->nr_rows; |
Arnaldo Carvalho de Melo | 0f0cbf7 | 2010-07-26 17:13:40 -0300 | [diff] [blame] | 602 | h->row_offset = 0; |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 603 | hists->stats.total_period += h->stat.period; |
| 604 | hists->stats.nr_events[PERF_RECORD_SAMPLE] += h->stat.nr_events; |
Arnaldo Carvalho de Melo | cc5edb0 | 2010-07-16 12:35:07 -0300 | [diff] [blame] | 605 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 606 | hists__calc_col_len(hists, h); |
Arnaldo Carvalho de Melo | cc5edb0 | 2010-07-16 12:35:07 -0300 | [diff] [blame] | 607 | } |
| 608 | |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 609 | |
| 610 | static bool hists__filter_entry_by_dso(struct hists *hists, |
| 611 | struct hist_entry *he) |
| 612 | { |
| 613 | if (hists->dso_filter != NULL && |
| 614 | (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) { |
| 615 | he->filtered |= (1 << HIST_FILTER__DSO); |
| 616 | return true; |
| 617 | } |
| 618 | |
| 619 | return false; |
| 620 | } |
| 621 | |
Arnaldo Carvalho de Melo | d7b76f0 | 2011-10-18 19:07:34 -0200 | [diff] [blame] | 622 | void hists__filter_by_dso(struct hists *hists) |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 623 | { |
| 624 | struct rb_node *nd; |
| 625 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 626 | hists->nr_entries = hists->stats.total_period = 0; |
| 627 | hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0; |
| 628 | hists__reset_col_len(hists); |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 629 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 630 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 631 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 632 | |
| 633 | if (symbol_conf.exclude_other && !h->parent) |
| 634 | continue; |
| 635 | |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 636 | if (hists__filter_entry_by_dso(hists, h)) |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 637 | continue; |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 638 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 639 | hists__remove_entry_filter(hists, h, HIST_FILTER__DSO); |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 643 | static bool hists__filter_entry_by_thread(struct hists *hists, |
| 644 | struct hist_entry *he) |
| 645 | { |
| 646 | if (hists->thread_filter != NULL && |
| 647 | he->thread != hists->thread_filter) { |
| 648 | he->filtered |= (1 << HIST_FILTER__THREAD); |
| 649 | return true; |
| 650 | } |
| 651 | |
| 652 | return false; |
| 653 | } |
| 654 | |
Arnaldo Carvalho de Melo | d7b76f0 | 2011-10-18 19:07:34 -0200 | [diff] [blame] | 655 | void hists__filter_by_thread(struct hists *hists) |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 656 | { |
| 657 | struct rb_node *nd; |
| 658 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 659 | hists->nr_entries = hists->stats.total_period = 0; |
| 660 | hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0; |
| 661 | hists__reset_col_len(hists); |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 662 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 663 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 664 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 665 | |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 666 | if (hists__filter_entry_by_thread(hists, h)) |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 667 | continue; |
Arnaldo Carvalho de Melo | cc5edb0 | 2010-07-16 12:35:07 -0300 | [diff] [blame] | 668 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 669 | hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD); |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 670 | } |
| 671 | } |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 672 | |
Namhyung Kim | e94d53e | 2012-03-16 17:50:51 +0900 | [diff] [blame] | 673 | static bool hists__filter_entry_by_symbol(struct hists *hists, |
| 674 | struct hist_entry *he) |
| 675 | { |
| 676 | if (hists->symbol_filter_str != NULL && |
| 677 | (!he->ms.sym || strstr(he->ms.sym->name, |
| 678 | hists->symbol_filter_str) == NULL)) { |
| 679 | he->filtered |= (1 << HIST_FILTER__SYMBOL); |
| 680 | return true; |
| 681 | } |
| 682 | |
| 683 | return false; |
| 684 | } |
| 685 | |
| 686 | void hists__filter_by_symbol(struct hists *hists) |
| 687 | { |
| 688 | struct rb_node *nd; |
| 689 | |
| 690 | hists->nr_entries = hists->stats.total_period = 0; |
| 691 | hists->stats.nr_events[PERF_RECORD_SAMPLE] = 0; |
| 692 | hists__reset_col_len(hists); |
| 693 | |
| 694 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
| 695 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 696 | |
| 697 | if (hists__filter_entry_by_symbol(hists, h)) |
| 698 | continue; |
| 699 | |
| 700 | hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL); |
| 701 | } |
| 702 | } |
| 703 | |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 704 | int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip) |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 705 | { |
Arnaldo Carvalho de Melo | 2f525d0 | 2011-02-04 13:43:24 -0200 | [diff] [blame] | 706 | return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip); |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 707 | } |
| 708 | |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 709 | int hist_entry__annotate(struct hist_entry *he, size_t privsize) |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 710 | { |
Arnaldo Carvalho de Melo | ce6f4fa | 2011-02-08 13:27:39 -0200 | [diff] [blame] | 711 | return symbol__annotate(he->ms.sym, he->ms.map, privsize); |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 712 | } |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 713 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 714 | void hists__inc_nr_events(struct hists *hists, u32 type) |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 715 | { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 716 | ++hists->stats.nr_events[0]; |
| 717 | ++hists->stats.nr_events[type]; |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 718 | } |
Arnaldo Carvalho de Melo | 95529be | 2012-11-08 17:54:33 -0300 | [diff] [blame] | 719 | |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 720 | static struct hist_entry *hists__add_dummy_entry(struct hists *hists, |
| 721 | struct hist_entry *pair) |
| 722 | { |
| 723 | struct rb_node **p = &hists->entries.rb_node; |
| 724 | struct rb_node *parent = NULL; |
| 725 | struct hist_entry *he; |
| 726 | int cmp; |
| 727 | |
| 728 | while (*p != NULL) { |
| 729 | parent = *p; |
| 730 | he = rb_entry(parent, struct hist_entry, rb_node); |
| 731 | |
| 732 | cmp = hist_entry__cmp(pair, he); |
| 733 | |
| 734 | if (!cmp) |
| 735 | goto out; |
| 736 | |
| 737 | if (cmp < 0) |
| 738 | p = &(*p)->rb_left; |
| 739 | else |
| 740 | p = &(*p)->rb_right; |
| 741 | } |
| 742 | |
| 743 | he = hist_entry__new(pair); |
| 744 | if (he) { |
Arnaldo Carvalho de Melo | 30193d7 | 2012-11-12 13:20:03 -0300 | [diff] [blame] | 745 | memset(&he->stat, 0, sizeof(he->stat)); |
| 746 | he->hists = hists; |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 747 | rb_link_node(&he->rb_node, parent, p); |
| 748 | rb_insert_color(&he->rb_node, &hists->entries); |
| 749 | hists__inc_nr_entries(hists, he); |
| 750 | } |
| 751 | out: |
| 752 | return he; |
| 753 | } |
| 754 | |
Arnaldo Carvalho de Melo | 95529be | 2012-11-08 17:54:33 -0300 | [diff] [blame] | 755 | static struct hist_entry *hists__find_entry(struct hists *hists, |
| 756 | struct hist_entry *he) |
| 757 | { |
| 758 | struct rb_node *n = hists->entries.rb_node; |
| 759 | |
| 760 | while (n) { |
| 761 | struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node); |
| 762 | int64_t cmp = hist_entry__cmp(he, iter); |
| 763 | |
| 764 | if (cmp < 0) |
| 765 | n = n->rb_left; |
| 766 | else if (cmp > 0) |
| 767 | n = n->rb_right; |
| 768 | else |
| 769 | return iter; |
| 770 | } |
| 771 | |
| 772 | return NULL; |
| 773 | } |
| 774 | |
| 775 | /* |
| 776 | * Look for pairs to link to the leader buckets (hist_entries): |
| 777 | */ |
| 778 | void hists__match(struct hists *leader, struct hists *other) |
| 779 | { |
| 780 | struct rb_node *nd; |
| 781 | struct hist_entry *pos, *pair; |
| 782 | |
| 783 | for (nd = rb_first(&leader->entries); nd; nd = rb_next(nd)) { |
| 784 | pos = rb_entry(nd, struct hist_entry, rb_node); |
| 785 | pair = hists__find_entry(other, pos); |
| 786 | |
| 787 | if (pair) |
| 788 | hist__entry_add_pair(pos, pair); |
| 789 | } |
| 790 | } |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 791 | |
| 792 | /* |
| 793 | * Look for entries in the other hists that are not present in the leader, if |
| 794 | * we find them, just add a dummy entry on the leader hists, with period=0, |
| 795 | * nr_events=0, to serve as the list header. |
| 796 | */ |
| 797 | int hists__link(struct hists *leader, struct hists *other) |
| 798 | { |
| 799 | struct rb_node *nd; |
| 800 | struct hist_entry *pos, *pair; |
| 801 | |
| 802 | for (nd = rb_first(&other->entries); nd; nd = rb_next(nd)) { |
| 803 | pos = rb_entry(nd, struct hist_entry, rb_node); |
| 804 | |
| 805 | if (!hist_entry__has_pairs(pos)) { |
| 806 | pair = hists__add_dummy_entry(leader, pos); |
| 807 | if (pair == NULL) |
| 808 | return -1; |
| 809 | hist__entry_add_pair(pair, pos); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | return 0; |
| 814 | } |