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