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