Frederic Weisbecker | 8a0ecfb | 2010-05-13 19:47:16 +0200 | [diff] [blame] | 1 | #include "util.h" |
Frederic Weisbecker | 598357e | 2010-05-21 12:48:39 +0200 | [diff] [blame] | 2 | #include "build-id.h" |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 3 | #include "hist.h" |
Arnaldo Carvalho de Melo | 4e4f06e | 2009-12-14 13:10:39 -0200 | [diff] [blame] | 4 | #include "session.h" |
| 5 | #include "sort.h" |
Namhyung Kim | 29d720e | 2013-01-22 18:09:33 +0900 | [diff] [blame] | 6 | #include "evsel.h" |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 7 | #include "annotate.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 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 17 | struct callchain_param callchain_param = { |
| 18 | .mode = CHAIN_GRAPH_REL, |
Sam Liao | d797fdc | 2011-06-07 23:49:46 +0800 | [diff] [blame] | 19 | .min_percent = 0.5, |
Andi Kleen | 99571ab | 2013-07-18 15:33:57 -0700 | [diff] [blame] | 20 | .order = ORDER_CALLEE, |
| 21 | .key = CCKEY_FUNCTION |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 22 | }; |
| 23 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 24 | 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] | 25 | { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 26 | return hists->col_len[col]; |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 27 | } |
| 28 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 29 | 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] | 30 | { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 31 | hists->col_len[col] = len; |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 32 | } |
| 33 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 34 | 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] | 35 | { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 36 | if (len > hists__col_len(hists, col)) { |
| 37 | hists__set_col_len(hists, col, len); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 38 | return true; |
| 39 | } |
| 40 | return false; |
| 41 | } |
| 42 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 43 | void hists__reset_col_len(struct hists *hists) |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 44 | { |
| 45 | enum hist_column col; |
| 46 | |
| 47 | for (col = 0; col < HISTC_NR_COLS; ++col) |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 48 | hists__set_col_len(hists, col, 0); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 49 | } |
| 50 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 51 | static void hists__set_unres_dso_col_len(struct hists *hists, int dso) |
| 52 | { |
| 53 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; |
| 54 | |
| 55 | if (hists__col_len(hists, dso) < unresolved_col_width && |
| 56 | !symbol_conf.col_width_list_str && !symbol_conf.field_sep && |
| 57 | !symbol_conf.dso_list) |
| 58 | hists__set_col_len(hists, dso, unresolved_col_width); |
| 59 | } |
| 60 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 61 | 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] | 62 | { |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 63 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 64 | int symlen; |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 65 | u16 len; |
| 66 | |
Namhyung Kim | ded19d5 | 2013-04-01 20:35:19 +0900 | [diff] [blame] | 67 | /* |
| 68 | * +4 accounts for '[x] ' priv level info |
| 69 | * +2 accounts for 0x prefix on raw addresses |
| 70 | * +3 accounts for ' y ' symtab origin info |
| 71 | */ |
| 72 | if (h->ms.sym) { |
| 73 | symlen = h->ms.sym->namelen + 4; |
| 74 | if (verbose) |
| 75 | symlen += BITS_PER_LONG / 4 + 2 + 3; |
| 76 | hists__new_col_len(hists, HISTC_SYMBOL, symlen); |
| 77 | } else { |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 78 | symlen = unresolved_col_width + 4 + 2; |
| 79 | hists__new_col_len(hists, HISTC_SYMBOL, symlen); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 80 | hists__set_unres_dso_col_len(hists, HISTC_DSO); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 81 | } |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 82 | |
| 83 | len = thread__comm_len(h->thread); |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 84 | if (hists__new_col_len(hists, HISTC_COMM, len)) |
| 85 | hists__set_col_len(hists, HISTC_THREAD, len + 6); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 86 | |
| 87 | if (h->ms.map) { |
| 88 | len = dso__name_len(h->ms.map->dso); |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 89 | hists__new_col_len(hists, HISTC_DSO, len); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 90 | } |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 91 | |
Namhyung Kim | cb99374 | 2012-12-27 18:11:42 +0900 | [diff] [blame] | 92 | if (h->parent) |
| 93 | hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen); |
| 94 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 95 | if (h->branch_info) { |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 96 | if (h->branch_info->from.sym) { |
| 97 | symlen = (int)h->branch_info->from.sym->namelen + 4; |
Namhyung Kim | ded19d5 | 2013-04-01 20:35:19 +0900 | [diff] [blame] | 98 | if (verbose) |
| 99 | symlen += BITS_PER_LONG / 4 + 2 + 3; |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 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; |
Namhyung Kim | ded19d5 | 2013-04-01 20:35:19 +0900 | [diff] [blame] | 112 | if (verbose) |
| 113 | symlen += BITS_PER_LONG / 4 + 2 + 3; |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 114 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); |
| 115 | |
| 116 | symlen = dso__name_len(h->branch_info->to.map->dso); |
| 117 | hists__new_col_len(hists, HISTC_DSO_TO, symlen); |
| 118 | } else { |
| 119 | symlen = unresolved_col_width + 4 + 2; |
| 120 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); |
| 121 | hists__set_unres_dso_col_len(hists, HISTC_DSO_TO); |
| 122 | } |
| 123 | } |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 124 | |
| 125 | if (h->mem_info) { |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 126 | if (h->mem_info->daddr.sym) { |
| 127 | symlen = (int)h->mem_info->daddr.sym->namelen + 4 |
| 128 | + unresolved_col_width + 2; |
| 129 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, |
| 130 | symlen); |
Don Zickus | 9b32ba7 | 2014-06-01 15:38:29 +0200 | [diff] [blame] | 131 | hists__new_col_len(hists, HISTC_MEM_DCACHELINE, |
| 132 | symlen + 1); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 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); |
Andi Kleen | 475eeab | 2013-09-20 07:40:43 -0700 | [diff] [blame] | 158 | |
| 159 | if (h->transaction) |
| 160 | hists__new_col_len(hists, HISTC_TRANSACTION, |
| 161 | hist_entry__transaction_len()); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 162 | } |
| 163 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 164 | void hists__output_recalc_col_len(struct hists *hists, int max_rows) |
| 165 | { |
| 166 | struct rb_node *next = rb_first(&hists->entries); |
| 167 | struct hist_entry *n; |
| 168 | int row = 0; |
| 169 | |
| 170 | hists__reset_col_len(hists); |
| 171 | |
| 172 | while (next && row++ < max_rows) { |
| 173 | n = rb_entry(next, struct hist_entry, rb_node); |
| 174 | if (!n->filtered) |
| 175 | hists__calc_col_len(hists, n); |
| 176 | next = rb_next(&n->rb_node); |
| 177 | } |
| 178 | } |
| 179 | |
Namhyung Kim | f39056f | 2014-01-14 14:25:37 +0900 | [diff] [blame] | 180 | static void he_stat__add_cpumode_period(struct he_stat *he_stat, |
| 181 | unsigned int cpumode, u64 period) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 182 | { |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 183 | switch (cpumode) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 184 | case PERF_RECORD_MISC_KERNEL: |
Namhyung Kim | f39056f | 2014-01-14 14:25:37 +0900 | [diff] [blame] | 185 | he_stat->period_sys += period; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 186 | break; |
| 187 | case PERF_RECORD_MISC_USER: |
Namhyung Kim | f39056f | 2014-01-14 14:25:37 +0900 | [diff] [blame] | 188 | he_stat->period_us += period; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 189 | break; |
| 190 | case PERF_RECORD_MISC_GUEST_KERNEL: |
Namhyung Kim | f39056f | 2014-01-14 14:25:37 +0900 | [diff] [blame] | 191 | he_stat->period_guest_sys += period; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 192 | break; |
| 193 | case PERF_RECORD_MISC_GUEST_USER: |
Namhyung Kim | f39056f | 2014-01-14 14:25:37 +0900 | [diff] [blame] | 194 | he_stat->period_guest_us += period; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 195 | break; |
| 196 | default: |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 201 | static void he_stat__add_period(struct he_stat *he_stat, u64 period, |
| 202 | u64 weight) |
Namhyung Kim | 139c081 | 2012-10-04 21:49:43 +0900 | [diff] [blame] | 203 | { |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 204 | |
Namhyung Kim | 139c081 | 2012-10-04 21:49:43 +0900 | [diff] [blame] | 205 | he_stat->period += period; |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 206 | he_stat->weight += weight; |
Namhyung Kim | 139c081 | 2012-10-04 21:49:43 +0900 | [diff] [blame] | 207 | he_stat->nr_events += 1; |
| 208 | } |
| 209 | |
| 210 | static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src) |
| 211 | { |
| 212 | dest->period += src->period; |
| 213 | dest->period_sys += src->period_sys; |
| 214 | dest->period_us += src->period_us; |
| 215 | dest->period_guest_sys += src->period_guest_sys; |
| 216 | dest->period_guest_us += src->period_guest_us; |
| 217 | dest->nr_events += src->nr_events; |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 218 | dest->weight += src->weight; |
Namhyung Kim | 139c081 | 2012-10-04 21:49:43 +0900 | [diff] [blame] | 219 | } |
| 220 | |
Namhyung Kim | f39056f | 2014-01-14 14:25:37 +0900 | [diff] [blame] | 221 | static void he_stat__decay(struct he_stat *he_stat) |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 222 | { |
Namhyung Kim | f39056f | 2014-01-14 14:25:37 +0900 | [diff] [blame] | 223 | he_stat->period = (he_stat->period * 7) / 8; |
| 224 | he_stat->nr_events = (he_stat->nr_events * 7) / 8; |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 225 | /* XXX need decay for weight too? */ |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static bool hists__decay_entry(struct hists *hists, struct hist_entry *he) |
| 229 | { |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 230 | u64 prev_period = he->stat.period; |
Namhyung Kim | 3186b68 | 2014-04-22 13:44:23 +0900 | [diff] [blame] | 231 | u64 diff; |
Arnaldo Carvalho de Melo | c64550c | 2011-10-20 06:45:44 -0200 | [diff] [blame] | 232 | |
| 233 | if (prev_period == 0) |
Arnaldo Carvalho de Melo | df71d95 | 2011-10-13 08:01:33 -0300 | [diff] [blame] | 234 | return true; |
Arnaldo Carvalho de Melo | c64550c | 2011-10-20 06:45:44 -0200 | [diff] [blame] | 235 | |
Namhyung Kim | f39056f | 2014-01-14 14:25:37 +0900 | [diff] [blame] | 236 | he_stat__decay(&he->stat); |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 237 | if (symbol_conf.cumulate_callchain) |
| 238 | he_stat__decay(he->stat_acc); |
Arnaldo Carvalho de Melo | c64550c | 2011-10-20 06:45:44 -0200 | [diff] [blame] | 239 | |
Namhyung Kim | 3186b68 | 2014-04-22 13:44:23 +0900 | [diff] [blame] | 240 | diff = prev_period - he->stat.period; |
| 241 | |
| 242 | hists->stats.total_period -= diff; |
Arnaldo Carvalho de Melo | c64550c | 2011-10-20 06:45:44 -0200 | [diff] [blame] | 243 | if (!he->filtered) |
Namhyung Kim | 3186b68 | 2014-04-22 13:44:23 +0900 | [diff] [blame] | 244 | hists->stats.total_non_filtered_period -= diff; |
Arnaldo Carvalho de Melo | c64550c | 2011-10-20 06:45:44 -0200 | [diff] [blame] | 245 | |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 246 | return he->stat.period == 0; |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 247 | } |
| 248 | |
Namhyung Kim | 3a5714f | 2013-05-14 11:09:01 +0900 | [diff] [blame] | 249 | void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel) |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 250 | { |
| 251 | struct rb_node *next = rb_first(&hists->entries); |
| 252 | struct hist_entry *n; |
| 253 | |
| 254 | while (next) { |
| 255 | n = rb_entry(next, struct hist_entry, rb_node); |
| 256 | next = rb_next(&n->rb_node); |
Arnaldo Carvalho de Melo | df71d95 | 2011-10-13 08:01:33 -0300 | [diff] [blame] | 257 | /* |
| 258 | * We may be annotating this, for instance, so keep it here in |
| 259 | * case some it gets new samples, we'll eventually free it when |
| 260 | * the user stops browsing and it agains gets fully decayed. |
| 261 | */ |
Arnaldo Carvalho de Melo | b079d4e | 2011-10-17 09:05:04 -0200 | [diff] [blame] | 262 | if (((zap_user && n->level == '.') || |
| 263 | (zap_kernel && n->level != '.') || |
| 264 | hists__decay_entry(hists, n)) && |
| 265 | !n->used) { |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 266 | rb_erase(&n->rb_node, &hists->entries); |
| 267 | |
Namhyung Kim | 3a5714f | 2013-05-14 11:09:01 +0900 | [diff] [blame] | 268 | if (sort__need_collapse) |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 269 | rb_erase(&n->rb_node_in, &hists->entries_collapsed); |
| 270 | |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 271 | --hists->nr_entries; |
Namhyung Kim | 3186b68 | 2014-04-22 13:44:23 +0900 | [diff] [blame] | 272 | if (!n->filtered) |
| 273 | --hists->nr_non_filtered_entries; |
| 274 | |
| 275 | hist_entry__free(n); |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 280 | /* |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 281 | * histogram, sorted on item, collects periods |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 282 | */ |
| 283 | |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 284 | static struct hist_entry *hist_entry__new(struct hist_entry *template, |
| 285 | bool sample_self) |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 286 | { |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 287 | size_t callchain_size = 0; |
| 288 | struct hist_entry *he; |
| 289 | |
| 290 | if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) |
| 291 | callchain_size = sizeof(struct callchain_root); |
| 292 | |
| 293 | he = zalloc(sizeof(*he) + callchain_size); |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 294 | |
Arnaldo Carvalho de Melo | 12c1427 | 2012-01-04 12:27:03 -0200 | [diff] [blame] | 295 | if (he != NULL) { |
| 296 | *he = *template; |
Namhyung Kim | c4b3535 | 2012-10-04 21:49:42 +0900 | [diff] [blame] | 297 | |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 298 | if (symbol_conf.cumulate_callchain) { |
| 299 | he->stat_acc = malloc(sizeof(he->stat)); |
| 300 | if (he->stat_acc == NULL) { |
| 301 | free(he); |
| 302 | return NULL; |
| 303 | } |
| 304 | memcpy(he->stat_acc, &he->stat, sizeof(he->stat)); |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 305 | if (!sample_self) |
| 306 | memset(&he->stat, 0, sizeof(he->stat)); |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 307 | } |
| 308 | |
Arnaldo Carvalho de Melo | 12c1427 | 2012-01-04 12:27:03 -0200 | [diff] [blame] | 309 | if (he->ms.map) |
| 310 | he->ms.map->referenced = true; |
Stephane Eranian | 3cf0cb1 | 2013-01-14 15:02:45 +0100 | [diff] [blame] | 311 | |
| 312 | if (he->branch_info) { |
Namhyung Kim | 26353a6 | 2013-04-01 20:35:17 +0900 | [diff] [blame] | 313 | /* |
| 314 | * This branch info is (a part of) allocated from |
Arnaldo Carvalho de Melo | 644f2df | 2014-01-22 13:15:36 -0300 | [diff] [blame] | 315 | * sample__resolve_bstack() and will be freed after |
Namhyung Kim | 26353a6 | 2013-04-01 20:35:17 +0900 | [diff] [blame] | 316 | * adding new entries. So we need to save a copy. |
| 317 | */ |
| 318 | he->branch_info = malloc(sizeof(*he->branch_info)); |
| 319 | if (he->branch_info == NULL) { |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 320 | free(he->stat_acc); |
Namhyung Kim | 26353a6 | 2013-04-01 20:35:17 +0900 | [diff] [blame] | 321 | free(he); |
| 322 | return NULL; |
| 323 | } |
| 324 | |
| 325 | memcpy(he->branch_info, template->branch_info, |
| 326 | sizeof(*he->branch_info)); |
| 327 | |
Stephane Eranian | 3cf0cb1 | 2013-01-14 15:02:45 +0100 | [diff] [blame] | 328 | if (he->branch_info->from.map) |
| 329 | he->branch_info->from.map->referenced = true; |
| 330 | if (he->branch_info->to.map) |
| 331 | he->branch_info->to.map->referenced = true; |
| 332 | } |
| 333 | |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 334 | if (he->mem_info) { |
| 335 | if (he->mem_info->iaddr.map) |
| 336 | he->mem_info->iaddr.map->referenced = true; |
| 337 | if (he->mem_info->daddr.map) |
| 338 | he->mem_info->daddr.map->referenced = true; |
| 339 | } |
| 340 | |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 341 | if (symbol_conf.use_callchain) |
Arnaldo Carvalho de Melo | 12c1427 | 2012-01-04 12:27:03 -0200 | [diff] [blame] | 342 | callchain_init(he->callchain); |
Arnaldo Carvalho de Melo | b821c73 | 2012-10-25 14:42:45 -0200 | [diff] [blame] | 343 | |
| 344 | INIT_LIST_HEAD(&he->pairs.node); |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 345 | } |
| 346 | |
Arnaldo Carvalho de Melo | 12c1427 | 2012-01-04 12:27:03 -0200 | [diff] [blame] | 347 | return he; |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 348 | } |
| 349 | |
Arnaldo Carvalho de Melo | 7a007ca | 2010-07-21 09:19:41 -0300 | [diff] [blame] | 350 | static u8 symbol__parent_filter(const struct symbol *parent) |
| 351 | { |
| 352 | if (symbol_conf.exclude_other && parent == NULL) |
| 353 | return 1 << HIST_FILTER__PARENT; |
| 354 | return 0; |
| 355 | } |
| 356 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 357 | static struct hist_entry *add_hist_entry(struct hists *hists, |
Namhyung Kim | f1cbf78 | 2013-12-18 14:21:11 +0900 | [diff] [blame] | 358 | struct hist_entry *entry, |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 359 | struct addr_location *al, |
| 360 | bool sample_self) |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 361 | { |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 362 | struct rb_node **p; |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 363 | struct rb_node *parent = NULL; |
| 364 | struct hist_entry *he; |
Andi Kleen | 354cc40 | 2013-10-01 07:22:15 -0700 | [diff] [blame] | 365 | int64_t cmp; |
Namhyung Kim | f1cbf78 | 2013-12-18 14:21:11 +0900 | [diff] [blame] | 366 | u64 period = entry->stat.period; |
| 367 | u64 weight = entry->stat.weight; |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 368 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 369 | p = &hists->entries_in->rb_node; |
| 370 | |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 371 | while (*p != NULL) { |
| 372 | parent = *p; |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 373 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 374 | |
Namhyung Kim | 9afcf93 | 2012-12-10 17:29:54 +0900 | [diff] [blame] | 375 | /* |
| 376 | * Make sure that it receives arguments in a same order as |
| 377 | * hist_entry__collapse() so that we can use an appropriate |
| 378 | * function when searching an entry regardless which sort |
| 379 | * keys were used. |
| 380 | */ |
| 381 | cmp = hist_entry__cmp(he, entry); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 382 | |
| 383 | if (!cmp) { |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 384 | if (sample_self) |
| 385 | he_stat__add_period(&he->stat, period, weight); |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 386 | if (symbol_conf.cumulate_callchain) |
| 387 | he_stat__add_period(he->stat_acc, period, weight); |
David Miller | 63fa471 | 2012-03-27 03:14:18 -0400 | [diff] [blame] | 388 | |
Namhyung Kim | ceb2acb | 2013-04-01 20:35:18 +0900 | [diff] [blame] | 389 | /* |
Arnaldo Carvalho de Melo | e80faac | 2014-01-22 13:05:06 -0300 | [diff] [blame] | 390 | * This mem info was allocated from sample__resolve_mem |
Namhyung Kim | ceb2acb | 2013-04-01 20:35:18 +0900 | [diff] [blame] | 391 | * and will not be used anymore. |
| 392 | */ |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 393 | zfree(&entry->mem_info); |
Namhyung Kim | ceb2acb | 2013-04-01 20:35:18 +0900 | [diff] [blame] | 394 | |
David Miller | 63fa471 | 2012-03-27 03:14:18 -0400 | [diff] [blame] | 395 | /* If the map of an existing hist_entry has |
| 396 | * become out-of-date due to an exec() or |
| 397 | * similar, update it. Otherwise we will |
| 398 | * mis-adjust symbol addresses when computing |
| 399 | * the history counter to increment. |
| 400 | */ |
| 401 | if (he->ms.map != entry->ms.map) { |
| 402 | he->ms.map = entry->ms.map; |
| 403 | if (he->ms.map) |
| 404 | he->ms.map->referenced = true; |
| 405 | } |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 406 | goto out; |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | if (cmp < 0) |
| 410 | p = &(*p)->rb_left; |
| 411 | else |
| 412 | p = &(*p)->rb_right; |
| 413 | } |
| 414 | |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 415 | he = hist_entry__new(entry, sample_self); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 416 | if (!he) |
Namhyung Kim | 27a0dcb | 2013-05-14 11:09:02 +0900 | [diff] [blame] | 417 | return NULL; |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 418 | |
| 419 | rb_link_node(&he->rb_node_in, parent, p); |
| 420 | rb_insert_color(&he->rb_node_in, hists->entries_in); |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 421 | out: |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 422 | if (sample_self) |
| 423 | he_stat__add_cpumode_period(&he->stat, al->cpumode, period); |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 424 | if (symbol_conf.cumulate_callchain) |
| 425 | he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 426 | return he; |
| 427 | } |
| 428 | |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 429 | struct hist_entry *__hists__add_entry(struct hists *hists, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 430 | struct addr_location *al, |
Namhyung Kim | 41a4e6e | 2013-10-31 15:56:03 +0900 | [diff] [blame] | 431 | struct symbol *sym_parent, |
| 432 | struct branch_info *bi, |
| 433 | struct mem_info *mi, |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 434 | u64 period, u64 weight, u64 transaction, |
| 435 | bool sample_self) |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 436 | { |
| 437 | struct hist_entry entry = { |
| 438 | .thread = al->thread, |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 439 | .comm = thread__comm(al->thread), |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 440 | .ms = { |
| 441 | .map = al->map, |
| 442 | .sym = al->sym, |
| 443 | }, |
Don Zickus | 7365be5 | 2014-05-27 12:28:05 -0400 | [diff] [blame] | 444 | .cpu = al->cpu, |
| 445 | .cpumode = al->cpumode, |
| 446 | .ip = al->addr, |
| 447 | .level = al->level, |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 448 | .stat = { |
Namhyung Kim | c4b3535 | 2012-10-04 21:49:42 +0900 | [diff] [blame] | 449 | .nr_events = 1, |
Namhyung Kim | 41a4e6e | 2013-10-31 15:56:03 +0900 | [diff] [blame] | 450 | .period = period, |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 451 | .weight = weight, |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 452 | }, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 453 | .parent = sym_parent, |
Namhyung Kim | 2c86c7c | 2014-03-17 18:18:54 -0300 | [diff] [blame] | 454 | .filtered = symbol__parent_filter(sym_parent) | al->filtered, |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 455 | .hists = hists, |
Namhyung Kim | 41a4e6e | 2013-10-31 15:56:03 +0900 | [diff] [blame] | 456 | .branch_info = bi, |
| 457 | .mem_info = mi, |
Andi Kleen | 475eeab | 2013-09-20 07:40:43 -0700 | [diff] [blame] | 458 | .transaction = transaction, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 459 | }; |
| 460 | |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 461 | return add_hist_entry(hists, &entry, al, sample_self); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 464 | static int |
| 465 | iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused, |
| 466 | struct addr_location *al __maybe_unused) |
| 467 | { |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static int |
| 472 | iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused, |
| 473 | struct addr_location *al __maybe_unused) |
| 474 | { |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | static int |
| 479 | iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al) |
| 480 | { |
| 481 | struct perf_sample *sample = iter->sample; |
| 482 | struct mem_info *mi; |
| 483 | |
| 484 | mi = sample__resolve_mem(sample, al); |
| 485 | if (mi == NULL) |
| 486 | return -ENOMEM; |
| 487 | |
| 488 | iter->priv = mi; |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | static int |
| 493 | iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al) |
| 494 | { |
| 495 | u64 cost; |
| 496 | struct mem_info *mi = iter->priv; |
| 497 | struct hist_entry *he; |
| 498 | |
| 499 | if (mi == NULL) |
| 500 | return -EINVAL; |
| 501 | |
| 502 | cost = iter->sample->weight; |
| 503 | if (!cost) |
| 504 | cost = 1; |
| 505 | |
| 506 | /* |
| 507 | * must pass period=weight in order to get the correct |
| 508 | * sorting from hists__collapse_resort() which is solely |
| 509 | * based on periods. We want sorting be done on nr_events * weight |
| 510 | * and this is indirectly achieved by passing period=weight here |
| 511 | * and the he_stat__add_period() function. |
| 512 | */ |
| 513 | he = __hists__add_entry(&iter->evsel->hists, al, iter->parent, NULL, mi, |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 514 | cost, cost, 0, true); |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 515 | if (!he) |
| 516 | return -ENOMEM; |
| 517 | |
| 518 | iter->he = he; |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static int |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 523 | iter_finish_mem_entry(struct hist_entry_iter *iter, |
| 524 | struct addr_location *al __maybe_unused) |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 525 | { |
| 526 | struct perf_evsel *evsel = iter->evsel; |
| 527 | struct hist_entry *he = iter->he; |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 528 | int err = -EINVAL; |
| 529 | |
| 530 | if (he == NULL) |
| 531 | goto out; |
| 532 | |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 533 | hists__inc_nr_samples(&evsel->hists, he->filtered); |
| 534 | |
| 535 | err = hist_entry__append_callchain(he, iter->sample); |
| 536 | |
| 537 | out: |
| 538 | /* |
| 539 | * We don't need to free iter->priv (mem_info) here since |
| 540 | * the mem info was either already freed in add_hist_entry() or |
| 541 | * passed to a new hist entry by hist_entry__new(). |
| 542 | */ |
| 543 | iter->priv = NULL; |
| 544 | |
| 545 | iter->he = NULL; |
| 546 | return err; |
| 547 | } |
| 548 | |
| 549 | static int |
| 550 | iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) |
| 551 | { |
| 552 | struct branch_info *bi; |
| 553 | struct perf_sample *sample = iter->sample; |
| 554 | |
| 555 | bi = sample__resolve_bstack(sample, al); |
| 556 | if (!bi) |
| 557 | return -ENOMEM; |
| 558 | |
| 559 | iter->curr = 0; |
| 560 | iter->total = sample->branch_stack->nr; |
| 561 | |
| 562 | iter->priv = bi; |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | static int |
| 567 | iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused, |
| 568 | struct addr_location *al __maybe_unused) |
| 569 | { |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 570 | /* to avoid calling callback function */ |
| 571 | iter->he = NULL; |
| 572 | |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | static int |
| 577 | iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) |
| 578 | { |
| 579 | struct branch_info *bi = iter->priv; |
| 580 | int i = iter->curr; |
| 581 | |
| 582 | if (bi == NULL) |
| 583 | return 0; |
| 584 | |
| 585 | if (iter->curr >= iter->total) |
| 586 | return 0; |
| 587 | |
| 588 | al->map = bi[i].to.map; |
| 589 | al->sym = bi[i].to.sym; |
| 590 | al->addr = bi[i].to.addr; |
| 591 | return 1; |
| 592 | } |
| 593 | |
| 594 | static int |
| 595 | iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) |
| 596 | { |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 597 | struct branch_info *bi; |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 598 | struct perf_evsel *evsel = iter->evsel; |
| 599 | struct hist_entry *he = NULL; |
| 600 | int i = iter->curr; |
| 601 | int err = 0; |
| 602 | |
| 603 | bi = iter->priv; |
| 604 | |
| 605 | if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym)) |
| 606 | goto out; |
| 607 | |
| 608 | /* |
| 609 | * The report shows the percentage of total branches captured |
| 610 | * and not events sampled. Thus we use a pseudo period of 1. |
| 611 | */ |
| 612 | he = __hists__add_entry(&evsel->hists, al, iter->parent, &bi[i], NULL, |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 613 | 1, 1, 0, true); |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 614 | if (he == NULL) |
| 615 | return -ENOMEM; |
| 616 | |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 617 | hists__inc_nr_samples(&evsel->hists, he->filtered); |
| 618 | |
| 619 | out: |
| 620 | iter->he = he; |
| 621 | iter->curr++; |
| 622 | return err; |
| 623 | } |
| 624 | |
| 625 | static int |
| 626 | iter_finish_branch_entry(struct hist_entry_iter *iter, |
| 627 | struct addr_location *al __maybe_unused) |
| 628 | { |
| 629 | zfree(&iter->priv); |
| 630 | iter->he = NULL; |
| 631 | |
| 632 | return iter->curr >= iter->total ? 0 : -1; |
| 633 | } |
| 634 | |
| 635 | static int |
| 636 | iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused, |
| 637 | struct addr_location *al __maybe_unused) |
| 638 | { |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | static int |
| 643 | iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al) |
| 644 | { |
| 645 | struct perf_evsel *evsel = iter->evsel; |
| 646 | struct perf_sample *sample = iter->sample; |
| 647 | struct hist_entry *he; |
| 648 | |
| 649 | he = __hists__add_entry(&evsel->hists, al, iter->parent, NULL, NULL, |
| 650 | sample->period, sample->weight, |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 651 | sample->transaction, true); |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 652 | if (he == NULL) |
| 653 | return -ENOMEM; |
| 654 | |
| 655 | iter->he = he; |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | static int |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 660 | iter_finish_normal_entry(struct hist_entry_iter *iter, |
| 661 | struct addr_location *al __maybe_unused) |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 662 | { |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 663 | struct hist_entry *he = iter->he; |
| 664 | struct perf_evsel *evsel = iter->evsel; |
| 665 | struct perf_sample *sample = iter->sample; |
| 666 | |
| 667 | if (he == NULL) |
| 668 | return 0; |
| 669 | |
| 670 | iter->he = NULL; |
| 671 | |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 672 | hists__inc_nr_samples(&evsel->hists, he->filtered); |
| 673 | |
| 674 | return hist_entry__append_callchain(he, sample); |
| 675 | } |
| 676 | |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 677 | static int |
| 678 | iter_prepare_cumulative_entry(struct hist_entry_iter *iter __maybe_unused, |
| 679 | struct addr_location *al __maybe_unused) |
| 680 | { |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 681 | struct hist_entry **he_cache; |
| 682 | |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 683 | callchain_cursor_commit(&callchain_cursor); |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 684 | |
| 685 | /* |
| 686 | * This is for detecting cycles or recursions so that they're |
| 687 | * cumulated only one time to prevent entries more than 100% |
| 688 | * overhead. |
| 689 | */ |
| 690 | he_cache = malloc(sizeof(*he_cache) * (PERF_MAX_STACK_DEPTH + 1)); |
| 691 | if (he_cache == NULL) |
| 692 | return -ENOMEM; |
| 693 | |
| 694 | iter->priv = he_cache; |
| 695 | iter->curr = 0; |
| 696 | |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | static int |
| 701 | iter_add_single_cumulative_entry(struct hist_entry_iter *iter, |
| 702 | struct addr_location *al) |
| 703 | { |
| 704 | struct perf_evsel *evsel = iter->evsel; |
| 705 | struct perf_sample *sample = iter->sample; |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 706 | struct hist_entry **he_cache = iter->priv; |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 707 | struct hist_entry *he; |
| 708 | int err = 0; |
| 709 | |
| 710 | he = __hists__add_entry(&evsel->hists, al, iter->parent, NULL, NULL, |
| 711 | sample->period, sample->weight, |
| 712 | sample->transaction, true); |
| 713 | if (he == NULL) |
| 714 | return -ENOMEM; |
| 715 | |
| 716 | iter->he = he; |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 717 | he_cache[iter->curr++] = he; |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 718 | |
Namhyung Kim | be7f855 | 2013-12-26 17:44:10 +0900 | [diff] [blame] | 719 | callchain_append(he->callchain, &callchain_cursor, sample->period); |
| 720 | |
| 721 | /* |
| 722 | * We need to re-initialize the cursor since callchain_append() |
| 723 | * advanced the cursor to the end. |
| 724 | */ |
| 725 | callchain_cursor_commit(&callchain_cursor); |
| 726 | |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 727 | hists__inc_nr_samples(&evsel->hists, he->filtered); |
| 728 | |
| 729 | return err; |
| 730 | } |
| 731 | |
| 732 | static int |
| 733 | iter_next_cumulative_entry(struct hist_entry_iter *iter, |
| 734 | struct addr_location *al) |
| 735 | { |
| 736 | struct callchain_cursor_node *node; |
| 737 | |
| 738 | node = callchain_cursor_current(&callchain_cursor); |
| 739 | if (node == NULL) |
| 740 | return 0; |
| 741 | |
Namhyung Kim | c7405d8 | 2013-10-31 13:58:30 +0900 | [diff] [blame] | 742 | return fill_callchain_info(al, node, iter->hide_unresolved); |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | static int |
| 746 | iter_add_next_cumulative_entry(struct hist_entry_iter *iter, |
| 747 | struct addr_location *al) |
| 748 | { |
| 749 | struct perf_evsel *evsel = iter->evsel; |
| 750 | struct perf_sample *sample = iter->sample; |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 751 | struct hist_entry **he_cache = iter->priv; |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 752 | struct hist_entry *he; |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 753 | struct hist_entry he_tmp = { |
| 754 | .cpu = al->cpu, |
| 755 | .thread = al->thread, |
| 756 | .comm = thread__comm(al->thread), |
| 757 | .ip = al->addr, |
| 758 | .ms = { |
| 759 | .map = al->map, |
| 760 | .sym = al->sym, |
| 761 | }, |
| 762 | .parent = iter->parent, |
| 763 | }; |
| 764 | int i; |
Namhyung Kim | be7f855 | 2013-12-26 17:44:10 +0900 | [diff] [blame] | 765 | struct callchain_cursor cursor; |
| 766 | |
| 767 | callchain_cursor_snapshot(&cursor, &callchain_cursor); |
| 768 | |
| 769 | callchain_cursor_advance(&callchain_cursor); |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 770 | |
| 771 | /* |
| 772 | * Check if there's duplicate entries in the callchain. |
| 773 | * It's possible that it has cycles or recursive calls. |
| 774 | */ |
| 775 | for (i = 0; i < iter->curr; i++) { |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 776 | if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) { |
| 777 | /* to avoid calling callback function */ |
| 778 | iter->he = NULL; |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 779 | return 0; |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 780 | } |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 781 | } |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 782 | |
| 783 | he = __hists__add_entry(&evsel->hists, al, iter->parent, NULL, NULL, |
| 784 | sample->period, sample->weight, |
| 785 | sample->transaction, false); |
| 786 | if (he == NULL) |
| 787 | return -ENOMEM; |
| 788 | |
| 789 | iter->he = he; |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 790 | he_cache[iter->curr++] = he; |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 791 | |
Namhyung Kim | be7f855 | 2013-12-26 17:44:10 +0900 | [diff] [blame] | 792 | callchain_append(he->callchain, &cursor, sample->period); |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | static int |
| 797 | iter_finish_cumulative_entry(struct hist_entry_iter *iter, |
| 798 | struct addr_location *al __maybe_unused) |
| 799 | { |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 800 | zfree(&iter->priv); |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 801 | iter->he = NULL; |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 802 | |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 803 | return 0; |
| 804 | } |
| 805 | |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 806 | const struct hist_iter_ops hist_iter_mem = { |
| 807 | .prepare_entry = iter_prepare_mem_entry, |
| 808 | .add_single_entry = iter_add_single_mem_entry, |
| 809 | .next_entry = iter_next_nop_entry, |
| 810 | .add_next_entry = iter_add_next_nop_entry, |
| 811 | .finish_entry = iter_finish_mem_entry, |
| 812 | }; |
| 813 | |
| 814 | const struct hist_iter_ops hist_iter_branch = { |
| 815 | .prepare_entry = iter_prepare_branch_entry, |
| 816 | .add_single_entry = iter_add_single_branch_entry, |
| 817 | .next_entry = iter_next_branch_entry, |
| 818 | .add_next_entry = iter_add_next_branch_entry, |
| 819 | .finish_entry = iter_finish_branch_entry, |
| 820 | }; |
| 821 | |
| 822 | const struct hist_iter_ops hist_iter_normal = { |
| 823 | .prepare_entry = iter_prepare_normal_entry, |
| 824 | .add_single_entry = iter_add_single_normal_entry, |
| 825 | .next_entry = iter_next_nop_entry, |
| 826 | .add_next_entry = iter_add_next_nop_entry, |
| 827 | .finish_entry = iter_finish_normal_entry, |
| 828 | }; |
| 829 | |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 830 | const struct hist_iter_ops hist_iter_cumulative = { |
| 831 | .prepare_entry = iter_prepare_cumulative_entry, |
| 832 | .add_single_entry = iter_add_single_cumulative_entry, |
| 833 | .next_entry = iter_next_cumulative_entry, |
| 834 | .add_next_entry = iter_add_next_cumulative_entry, |
| 835 | .finish_entry = iter_finish_cumulative_entry, |
| 836 | }; |
| 837 | |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 838 | int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al, |
| 839 | struct perf_evsel *evsel, struct perf_sample *sample, |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 840 | int max_stack_depth, void *arg) |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 841 | { |
| 842 | int err, err2; |
| 843 | |
| 844 | err = sample__resolve_callchain(sample, &iter->parent, evsel, al, |
| 845 | max_stack_depth); |
| 846 | if (err) |
| 847 | return err; |
| 848 | |
| 849 | iter->evsel = evsel; |
| 850 | iter->sample = sample; |
| 851 | |
| 852 | err = iter->ops->prepare_entry(iter, al); |
| 853 | if (err) |
| 854 | goto out; |
| 855 | |
| 856 | err = iter->ops->add_single_entry(iter, al); |
| 857 | if (err) |
| 858 | goto out; |
| 859 | |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 860 | if (iter->he && iter->add_entry_cb) { |
| 861 | err = iter->add_entry_cb(iter, al, true, arg); |
| 862 | if (err) |
| 863 | goto out; |
| 864 | } |
| 865 | |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 866 | while (iter->ops->next_entry(iter, al)) { |
| 867 | err = iter->ops->add_next_entry(iter, al); |
| 868 | if (err) |
| 869 | break; |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 870 | |
| 871 | if (iter->he && iter->add_entry_cb) { |
| 872 | err = iter->add_entry_cb(iter, al, false, arg); |
| 873 | if (err) |
| 874 | goto out; |
| 875 | } |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | out: |
| 879 | err2 = iter->ops->finish_entry(iter, al); |
| 880 | if (!err) |
| 881 | err = err2; |
| 882 | |
| 883 | return err; |
| 884 | } |
| 885 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 886 | int64_t |
| 887 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) |
| 888 | { |
Namhyung Kim | 093f0ef3 | 2014-03-03 12:07:47 +0900 | [diff] [blame] | 889 | struct perf_hpp_fmt *fmt; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 890 | int64_t cmp = 0; |
| 891 | |
Namhyung Kim | 093f0ef3 | 2014-03-03 12:07:47 +0900 | [diff] [blame] | 892 | perf_hpp__for_each_sort_list(fmt) { |
Namhyung Kim | e67d49a | 2014-03-18 13:00:59 +0900 | [diff] [blame] | 893 | if (perf_hpp__should_skip(fmt)) |
| 894 | continue; |
| 895 | |
Namhyung Kim | 093f0ef3 | 2014-03-03 12:07:47 +0900 | [diff] [blame] | 896 | cmp = fmt->cmp(left, right); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 897 | if (cmp) |
| 898 | break; |
| 899 | } |
| 900 | |
| 901 | return cmp; |
| 902 | } |
| 903 | |
| 904 | int64_t |
| 905 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) |
| 906 | { |
Namhyung Kim | 093f0ef3 | 2014-03-03 12:07:47 +0900 | [diff] [blame] | 907 | struct perf_hpp_fmt *fmt; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 908 | int64_t cmp = 0; |
| 909 | |
Namhyung Kim | 093f0ef3 | 2014-03-03 12:07:47 +0900 | [diff] [blame] | 910 | perf_hpp__for_each_sort_list(fmt) { |
Namhyung Kim | e67d49a | 2014-03-18 13:00:59 +0900 | [diff] [blame] | 911 | if (perf_hpp__should_skip(fmt)) |
| 912 | continue; |
| 913 | |
Namhyung Kim | 093f0ef3 | 2014-03-03 12:07:47 +0900 | [diff] [blame] | 914 | cmp = fmt->collapse(left, right); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 915 | if (cmp) |
| 916 | break; |
| 917 | } |
| 918 | |
| 919 | return cmp; |
| 920 | } |
| 921 | |
| 922 | void hist_entry__free(struct hist_entry *he) |
| 923 | { |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 924 | zfree(&he->branch_info); |
| 925 | zfree(&he->mem_info); |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 926 | zfree(&he->stat_acc); |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 927 | free_srcline(he->srcline); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 928 | free(he); |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | * collapse the histogram |
| 933 | */ |
| 934 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 935 | static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused, |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 936 | struct rb_root *root, |
| 937 | struct hist_entry *he) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 938 | { |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 939 | struct rb_node **p = &root->rb_node; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 940 | struct rb_node *parent = NULL; |
| 941 | struct hist_entry *iter; |
| 942 | int64_t cmp; |
| 943 | |
| 944 | while (*p != NULL) { |
| 945 | parent = *p; |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 946 | iter = rb_entry(parent, struct hist_entry, rb_node_in); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 947 | |
| 948 | cmp = hist_entry__collapse(iter, he); |
| 949 | |
| 950 | if (!cmp) { |
Namhyung Kim | 139c081 | 2012-10-04 21:49:43 +0900 | [diff] [blame] | 951 | he_stat__add_stat(&iter->stat, &he->stat); |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 952 | if (symbol_conf.cumulate_callchain) |
| 953 | he_stat__add_stat(iter->stat_acc, he->stat_acc); |
Namhyung Kim | 9ec6097 | 2012-09-26 16:47:28 +0900 | [diff] [blame] | 954 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 955 | if (symbol_conf.use_callchain) { |
Namhyung Kim | 4726064 | 2012-05-31 14:43:26 +0900 | [diff] [blame] | 956 | callchain_cursor_reset(&callchain_cursor); |
| 957 | callchain_merge(&callchain_cursor, |
| 958 | iter->callchain, |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 959 | he->callchain); |
| 960 | } |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 961 | hist_entry__free(he); |
Arnaldo Carvalho de Melo | fefb0b9 | 2010-05-10 13:57:51 -0300 | [diff] [blame] | 962 | return false; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | if (cmp < 0) |
| 966 | p = &(*p)->rb_left; |
| 967 | else |
| 968 | p = &(*p)->rb_right; |
| 969 | } |
| 970 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 971 | rb_link_node(&he->rb_node_in, parent, p); |
| 972 | rb_insert_color(&he->rb_node_in, root); |
Arnaldo Carvalho de Melo | fefb0b9 | 2010-05-10 13:57:51 -0300 | [diff] [blame] | 973 | return true; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 974 | } |
| 975 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 976 | static struct rb_root *hists__get_rotate_entries_in(struct hists *hists) |
| 977 | { |
| 978 | struct rb_root *root; |
| 979 | |
| 980 | pthread_mutex_lock(&hists->lock); |
| 981 | |
| 982 | root = hists->entries_in; |
| 983 | if (++hists->entries_in > &hists->entries_in_array[1]) |
| 984 | hists->entries_in = &hists->entries_in_array[0]; |
| 985 | |
| 986 | pthread_mutex_unlock(&hists->lock); |
| 987 | |
| 988 | return root; |
| 989 | } |
| 990 | |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 991 | static void hists__apply_filters(struct hists *hists, struct hist_entry *he) |
| 992 | { |
| 993 | hists__filter_entry_by_dso(hists, he); |
| 994 | hists__filter_entry_by_thread(hists, he); |
Namhyung Kim | e94d53e | 2012-03-16 17:50:51 +0900 | [diff] [blame] | 995 | hists__filter_entry_by_symbol(hists, he); |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 996 | } |
| 997 | |
Namhyung Kim | c1fb565 | 2013-10-11 14:15:38 +0900 | [diff] [blame] | 998 | void hists__collapse_resort(struct hists *hists, struct ui_progress *prog) |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 999 | { |
| 1000 | struct rb_root *root; |
| 1001 | struct rb_node *next; |
| 1002 | struct hist_entry *n; |
| 1003 | |
Namhyung Kim | 3a5714f | 2013-05-14 11:09:01 +0900 | [diff] [blame] | 1004 | if (!sort__need_collapse) |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1005 | return; |
| 1006 | |
| 1007 | root = hists__get_rotate_entries_in(hists); |
| 1008 | next = rb_first(root); |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1009 | |
| 1010 | while (next) { |
Arnaldo Carvalho de Melo | 33e940a | 2013-09-17 16:34:28 -0300 | [diff] [blame] | 1011 | if (session_done()) |
| 1012 | break; |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1013 | n = rb_entry(next, struct hist_entry, rb_node_in); |
| 1014 | next = rb_next(&n->rb_node_in); |
| 1015 | |
| 1016 | rb_erase(&n->rb_node_in, root); |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 1017 | if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) { |
| 1018 | /* |
| 1019 | * If it wasn't combined with one of the entries already |
| 1020 | * collapsed, we need to apply the filters that may have |
| 1021 | * been set by, say, the hist_browser. |
| 1022 | */ |
| 1023 | hists__apply_filters(hists, n); |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 1024 | } |
Namhyung Kim | c1fb565 | 2013-10-11 14:15:38 +0900 | [diff] [blame] | 1025 | if (prog) |
| 1026 | ui_progress__update(prog, 1); |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1027 | } |
| 1028 | } |
| 1029 | |
Namhyung Kim | 043ca389 | 2014-03-03 14:18:00 +0900 | [diff] [blame] | 1030 | static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b) |
Namhyung Kim | 29d720e | 2013-01-22 18:09:33 +0900 | [diff] [blame] | 1031 | { |
Namhyung Kim | 043ca389 | 2014-03-03 14:18:00 +0900 | [diff] [blame] | 1032 | struct perf_hpp_fmt *fmt; |
| 1033 | int64_t cmp = 0; |
Namhyung Kim | 29d720e | 2013-01-22 18:09:33 +0900 | [diff] [blame] | 1034 | |
Namhyung Kim | 26d8b33 | 2014-03-03 16:16:20 +0900 | [diff] [blame] | 1035 | perf_hpp__for_each_sort_list(fmt) { |
Namhyung Kim | e67d49a | 2014-03-18 13:00:59 +0900 | [diff] [blame] | 1036 | if (perf_hpp__should_skip(fmt)) |
| 1037 | continue; |
| 1038 | |
Namhyung Kim | 043ca389 | 2014-03-03 14:18:00 +0900 | [diff] [blame] | 1039 | cmp = fmt->sort(a, b); |
| 1040 | if (cmp) |
Namhyung Kim | 29d720e | 2013-01-22 18:09:33 +0900 | [diff] [blame] | 1041 | break; |
| 1042 | } |
| 1043 | |
Namhyung Kim | 043ca389 | 2014-03-03 14:18:00 +0900 | [diff] [blame] | 1044 | return cmp; |
Namhyung Kim | 29d720e | 2013-01-22 18:09:33 +0900 | [diff] [blame] | 1045 | } |
| 1046 | |
Namhyung Kim | 9283ba9 | 2014-04-24 16:37:26 +0900 | [diff] [blame] | 1047 | static void hists__reset_filter_stats(struct hists *hists) |
| 1048 | { |
| 1049 | hists->nr_non_filtered_entries = 0; |
| 1050 | hists->stats.total_non_filtered_period = 0; |
| 1051 | } |
| 1052 | |
| 1053 | void hists__reset_stats(struct hists *hists) |
| 1054 | { |
| 1055 | hists->nr_entries = 0; |
| 1056 | hists->stats.total_period = 0; |
| 1057 | |
| 1058 | hists__reset_filter_stats(hists); |
| 1059 | } |
| 1060 | |
| 1061 | static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h) |
| 1062 | { |
| 1063 | hists->nr_non_filtered_entries++; |
| 1064 | hists->stats.total_non_filtered_period += h->stat.period; |
| 1065 | } |
| 1066 | |
| 1067 | void hists__inc_stats(struct hists *hists, struct hist_entry *h) |
| 1068 | { |
| 1069 | if (!h->filtered) |
| 1070 | hists__inc_filter_stats(hists, h); |
| 1071 | |
| 1072 | hists->nr_entries++; |
| 1073 | hists->stats.total_period += h->stat.period; |
| 1074 | } |
| 1075 | |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 1076 | static void __hists__insert_output_entry(struct rb_root *entries, |
| 1077 | struct hist_entry *he, |
| 1078 | u64 min_callchain_hits) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1079 | { |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 1080 | struct rb_node **p = &entries->rb_node; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1081 | struct rb_node *parent = NULL; |
| 1082 | struct hist_entry *iter; |
| 1083 | |
Arnaldo Carvalho de Melo | d599db3 | 2009-12-15 20:04:42 -0200 | [diff] [blame] | 1084 | if (symbol_conf.use_callchain) |
Arnaldo Carvalho de Melo | b9fb930 | 2010-04-02 09:50:42 -0300 | [diff] [blame] | 1085 | callchain_param.sort(&he->sorted_chain, he->callchain, |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1086 | min_callchain_hits, &callchain_param); |
| 1087 | |
| 1088 | while (*p != NULL) { |
| 1089 | parent = *p; |
| 1090 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 1091 | |
Namhyung Kim | 043ca389 | 2014-03-03 14:18:00 +0900 | [diff] [blame] | 1092 | if (hist_entry__sort(he, iter) > 0) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1093 | p = &(*p)->rb_left; |
| 1094 | else |
| 1095 | p = &(*p)->rb_right; |
| 1096 | } |
| 1097 | |
| 1098 | rb_link_node(&he->rb_node, parent, p); |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 1099 | rb_insert_color(&he->rb_node, entries); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1100 | } |
| 1101 | |
Namhyung Kim | 3a5714f | 2013-05-14 11:09:01 +0900 | [diff] [blame] | 1102 | void hists__output_resort(struct hists *hists) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1103 | { |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1104 | struct rb_root *root; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1105 | struct rb_node *next; |
| 1106 | struct hist_entry *n; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1107 | u64 min_callchain_hits; |
| 1108 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1109 | min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1110 | |
Namhyung Kim | 3a5714f | 2013-05-14 11:09:01 +0900 | [diff] [blame] | 1111 | if (sort__need_collapse) |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1112 | root = &hists->entries_collapsed; |
| 1113 | else |
| 1114 | root = hists->entries_in; |
| 1115 | |
| 1116 | next = rb_first(root); |
| 1117 | hists->entries = RB_ROOT; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1118 | |
Namhyung Kim | 9283ba9 | 2014-04-24 16:37:26 +0900 | [diff] [blame] | 1119 | hists__reset_stats(hists); |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1120 | hists__reset_col_len(hists); |
Arnaldo Carvalho de Melo | fefb0b9 | 2010-05-10 13:57:51 -0300 | [diff] [blame] | 1121 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1122 | while (next) { |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1123 | n = rb_entry(next, struct hist_entry, rb_node_in); |
| 1124 | next = rb_next(&n->rb_node_in); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1125 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1126 | __hists__insert_output_entry(&hists->entries, n, min_callchain_hits); |
Namhyung Kim | 6263835 | 2014-04-24 16:21:46 +0900 | [diff] [blame] | 1127 | hists__inc_stats(hists, n); |
Namhyung Kim | ae993ef | 2014-04-24 16:25:19 +0900 | [diff] [blame] | 1128 | |
| 1129 | if (!n->filtered) |
| 1130 | hists__calc_col_len(hists, n); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1131 | } |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1132 | } |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 1133 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1134 | 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] | 1135 | enum hist_filter filter) |
| 1136 | { |
| 1137 | h->filtered &= ~(1 << filter); |
| 1138 | if (h->filtered) |
| 1139 | return; |
| 1140 | |
Namhyung Kim | 87e90f4 | 2014-04-24 16:44:16 +0900 | [diff] [blame] | 1141 | /* force fold unfiltered entry for simplicity */ |
| 1142 | h->ms.unfolded = false; |
Arnaldo Carvalho de Melo | 0f0cbf7 | 2010-07-26 17:13:40 -0300 | [diff] [blame] | 1143 | h->row_offset = 0; |
Namhyung Kim | 9283ba9 | 2014-04-24 16:37:26 +0900 | [diff] [blame] | 1144 | |
Namhyung Kim | 1ab1fa5 | 2013-12-26 15:11:52 +0900 | [diff] [blame] | 1145 | hists->stats.nr_non_filtered_samples += h->stat.nr_events; |
Arnaldo Carvalho de Melo | cc5edb0 | 2010-07-16 12:35:07 -0300 | [diff] [blame] | 1146 | |
Namhyung Kim | 9283ba9 | 2014-04-24 16:37:26 +0900 | [diff] [blame] | 1147 | hists__inc_filter_stats(hists, h); |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1148 | hists__calc_col_len(hists, h); |
Arnaldo Carvalho de Melo | cc5edb0 | 2010-07-16 12:35:07 -0300 | [diff] [blame] | 1149 | } |
| 1150 | |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 1151 | |
| 1152 | static bool hists__filter_entry_by_dso(struct hists *hists, |
| 1153 | struct hist_entry *he) |
| 1154 | { |
| 1155 | if (hists->dso_filter != NULL && |
| 1156 | (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) { |
| 1157 | he->filtered |= (1 << HIST_FILTER__DSO); |
| 1158 | return true; |
| 1159 | } |
| 1160 | |
| 1161 | return false; |
| 1162 | } |
| 1163 | |
Arnaldo Carvalho de Melo | d7b76f0 | 2011-10-18 19:07:34 -0200 | [diff] [blame] | 1164 | void hists__filter_by_dso(struct hists *hists) |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1165 | { |
| 1166 | struct rb_node *nd; |
| 1167 | |
Namhyung Kim | 1ab1fa5 | 2013-12-26 15:11:52 +0900 | [diff] [blame] | 1168 | hists->stats.nr_non_filtered_samples = 0; |
Namhyung Kim | 9283ba9 | 2014-04-24 16:37:26 +0900 | [diff] [blame] | 1169 | |
| 1170 | hists__reset_filter_stats(hists); |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1171 | hists__reset_col_len(hists); |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1172 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1173 | 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] | 1174 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 1175 | |
| 1176 | if (symbol_conf.exclude_other && !h->parent) |
| 1177 | continue; |
| 1178 | |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 1179 | if (hists__filter_entry_by_dso(hists, h)) |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1180 | continue; |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1181 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1182 | hists__remove_entry_filter(hists, h, HIST_FILTER__DSO); |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1183 | } |
| 1184 | } |
| 1185 | |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 1186 | static bool hists__filter_entry_by_thread(struct hists *hists, |
| 1187 | struct hist_entry *he) |
| 1188 | { |
| 1189 | if (hists->thread_filter != NULL && |
| 1190 | he->thread != hists->thread_filter) { |
| 1191 | he->filtered |= (1 << HIST_FILTER__THREAD); |
| 1192 | return true; |
| 1193 | } |
| 1194 | |
| 1195 | return false; |
| 1196 | } |
| 1197 | |
Arnaldo Carvalho de Melo | d7b76f0 | 2011-10-18 19:07:34 -0200 | [diff] [blame] | 1198 | void hists__filter_by_thread(struct hists *hists) |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1199 | { |
| 1200 | struct rb_node *nd; |
| 1201 | |
Namhyung Kim | 1ab1fa5 | 2013-12-26 15:11:52 +0900 | [diff] [blame] | 1202 | hists->stats.nr_non_filtered_samples = 0; |
Namhyung Kim | 9283ba9 | 2014-04-24 16:37:26 +0900 | [diff] [blame] | 1203 | |
| 1204 | hists__reset_filter_stats(hists); |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1205 | hists__reset_col_len(hists); |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1206 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1207 | 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] | 1208 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 1209 | |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 1210 | if (hists__filter_entry_by_thread(hists, h)) |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1211 | continue; |
Arnaldo Carvalho de Melo | cc5edb0 | 2010-07-16 12:35:07 -0300 | [diff] [blame] | 1212 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1213 | hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD); |
Arnaldo Carvalho de Melo | b09e019 | 2010-05-11 11:10:15 -0300 | [diff] [blame] | 1214 | } |
| 1215 | } |
Arnaldo Carvalho de Melo | ef7b93a | 2010-05-11 23:18:06 -0300 | [diff] [blame] | 1216 | |
Namhyung Kim | e94d53e | 2012-03-16 17:50:51 +0900 | [diff] [blame] | 1217 | static bool hists__filter_entry_by_symbol(struct hists *hists, |
| 1218 | struct hist_entry *he) |
| 1219 | { |
| 1220 | if (hists->symbol_filter_str != NULL && |
| 1221 | (!he->ms.sym || strstr(he->ms.sym->name, |
| 1222 | hists->symbol_filter_str) == NULL)) { |
| 1223 | he->filtered |= (1 << HIST_FILTER__SYMBOL); |
| 1224 | return true; |
| 1225 | } |
| 1226 | |
| 1227 | return false; |
| 1228 | } |
| 1229 | |
| 1230 | void hists__filter_by_symbol(struct hists *hists) |
| 1231 | { |
| 1232 | struct rb_node *nd; |
| 1233 | |
Namhyung Kim | 1ab1fa5 | 2013-12-26 15:11:52 +0900 | [diff] [blame] | 1234 | hists->stats.nr_non_filtered_samples = 0; |
Namhyung Kim | 9283ba9 | 2014-04-24 16:37:26 +0900 | [diff] [blame] | 1235 | |
| 1236 | hists__reset_filter_stats(hists); |
Namhyung Kim | e94d53e | 2012-03-16 17:50:51 +0900 | [diff] [blame] | 1237 | hists__reset_col_len(hists); |
| 1238 | |
| 1239 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
| 1240 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 1241 | |
| 1242 | if (hists__filter_entry_by_symbol(hists, h)) |
| 1243 | continue; |
| 1244 | |
| 1245 | hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL); |
| 1246 | } |
| 1247 | } |
| 1248 | |
Arnaldo Carvalho de Melo | 28a6b6a | 2012-12-18 16:24:46 -0300 | [diff] [blame] | 1249 | void events_stats__inc(struct events_stats *stats, u32 type) |
| 1250 | { |
| 1251 | ++stats->nr_events[0]; |
| 1252 | ++stats->nr_events[type]; |
| 1253 | } |
| 1254 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1255 | void hists__inc_nr_events(struct hists *hists, u32 type) |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 1256 | { |
Arnaldo Carvalho de Melo | 28a6b6a | 2012-12-18 16:24:46 -0300 | [diff] [blame] | 1257 | events_stats__inc(&hists->stats, type); |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 1258 | } |
Arnaldo Carvalho de Melo | 95529be | 2012-11-08 17:54:33 -0300 | [diff] [blame] | 1259 | |
Namhyung Kim | 1844dbc | 2014-05-28 14:12:18 +0900 | [diff] [blame] | 1260 | void hists__inc_nr_samples(struct hists *hists, bool filtered) |
| 1261 | { |
| 1262 | events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE); |
| 1263 | if (!filtered) |
| 1264 | hists->stats.nr_non_filtered_samples++; |
| 1265 | } |
| 1266 | |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 1267 | static struct hist_entry *hists__add_dummy_entry(struct hists *hists, |
| 1268 | struct hist_entry *pair) |
| 1269 | { |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 1270 | struct rb_root *root; |
| 1271 | struct rb_node **p; |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 1272 | struct rb_node *parent = NULL; |
| 1273 | struct hist_entry *he; |
Andi Kleen | 354cc40 | 2013-10-01 07:22:15 -0700 | [diff] [blame] | 1274 | int64_t cmp; |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 1275 | |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 1276 | if (sort__need_collapse) |
| 1277 | root = &hists->entries_collapsed; |
| 1278 | else |
| 1279 | root = hists->entries_in; |
| 1280 | |
| 1281 | p = &root->rb_node; |
| 1282 | |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 1283 | while (*p != NULL) { |
| 1284 | parent = *p; |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 1285 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 1286 | |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 1287 | cmp = hist_entry__collapse(he, pair); |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 1288 | |
| 1289 | if (!cmp) |
| 1290 | goto out; |
| 1291 | |
| 1292 | if (cmp < 0) |
| 1293 | p = &(*p)->rb_left; |
| 1294 | else |
| 1295 | p = &(*p)->rb_right; |
| 1296 | } |
| 1297 | |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 1298 | he = hist_entry__new(pair, true); |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 1299 | if (he) { |
Arnaldo Carvalho de Melo | 30193d7 | 2012-11-12 13:20:03 -0300 | [diff] [blame] | 1300 | memset(&he->stat, 0, sizeof(he->stat)); |
| 1301 | he->hists = hists; |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 1302 | rb_link_node(&he->rb_node_in, parent, p); |
| 1303 | rb_insert_color(&he->rb_node_in, root); |
Namhyung Kim | 6263835 | 2014-04-24 16:21:46 +0900 | [diff] [blame] | 1304 | hists__inc_stats(hists, he); |
Jiri Olsa | e0af43d | 2012-12-01 21:18:20 +0100 | [diff] [blame] | 1305 | he->dummy = true; |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 1306 | } |
| 1307 | out: |
| 1308 | return he; |
| 1309 | } |
| 1310 | |
Arnaldo Carvalho de Melo | 95529be | 2012-11-08 17:54:33 -0300 | [diff] [blame] | 1311 | static struct hist_entry *hists__find_entry(struct hists *hists, |
| 1312 | struct hist_entry *he) |
| 1313 | { |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 1314 | struct rb_node *n; |
| 1315 | |
| 1316 | if (sort__need_collapse) |
| 1317 | n = hists->entries_collapsed.rb_node; |
| 1318 | else |
| 1319 | n = hists->entries_in->rb_node; |
Arnaldo Carvalho de Melo | 95529be | 2012-11-08 17:54:33 -0300 | [diff] [blame] | 1320 | |
| 1321 | while (n) { |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 1322 | struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in); |
| 1323 | int64_t cmp = hist_entry__collapse(iter, he); |
Arnaldo Carvalho de Melo | 95529be | 2012-11-08 17:54:33 -0300 | [diff] [blame] | 1324 | |
| 1325 | if (cmp < 0) |
| 1326 | n = n->rb_left; |
| 1327 | else if (cmp > 0) |
| 1328 | n = n->rb_right; |
| 1329 | else |
| 1330 | return iter; |
| 1331 | } |
| 1332 | |
| 1333 | return NULL; |
| 1334 | } |
| 1335 | |
| 1336 | /* |
| 1337 | * Look for pairs to link to the leader buckets (hist_entries): |
| 1338 | */ |
| 1339 | void hists__match(struct hists *leader, struct hists *other) |
| 1340 | { |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 1341 | struct rb_root *root; |
Arnaldo Carvalho de Melo | 95529be | 2012-11-08 17:54:33 -0300 | [diff] [blame] | 1342 | struct rb_node *nd; |
| 1343 | struct hist_entry *pos, *pair; |
| 1344 | |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 1345 | if (sort__need_collapse) |
| 1346 | root = &leader->entries_collapsed; |
| 1347 | else |
| 1348 | root = leader->entries_in; |
| 1349 | |
| 1350 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { |
| 1351 | pos = rb_entry(nd, struct hist_entry, rb_node_in); |
Arnaldo Carvalho de Melo | 95529be | 2012-11-08 17:54:33 -0300 | [diff] [blame] | 1352 | pair = hists__find_entry(other, pos); |
| 1353 | |
| 1354 | if (pair) |
Namhyung Kim | 5fa9041 | 2012-11-29 15:38:34 +0900 | [diff] [blame] | 1355 | hist_entry__add_pair(pair, pos); |
Arnaldo Carvalho de Melo | 95529be | 2012-11-08 17:54:33 -0300 | [diff] [blame] | 1356 | } |
| 1357 | } |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 1358 | |
| 1359 | /* |
| 1360 | * Look for entries in the other hists that are not present in the leader, if |
| 1361 | * we find them, just add a dummy entry on the leader hists, with period=0, |
| 1362 | * nr_events=0, to serve as the list header. |
| 1363 | */ |
| 1364 | int hists__link(struct hists *leader, struct hists *other) |
| 1365 | { |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 1366 | struct rb_root *root; |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 1367 | struct rb_node *nd; |
| 1368 | struct hist_entry *pos, *pair; |
| 1369 | |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 1370 | if (sort__need_collapse) |
| 1371 | root = &other->entries_collapsed; |
| 1372 | else |
| 1373 | root = other->entries_in; |
| 1374 | |
| 1375 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { |
| 1376 | pos = rb_entry(nd, struct hist_entry, rb_node_in); |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 1377 | |
| 1378 | if (!hist_entry__has_pairs(pos)) { |
| 1379 | pair = hists__add_dummy_entry(leader, pos); |
| 1380 | if (pair == NULL) |
| 1381 | return -1; |
Namhyung Kim | 5fa9041 | 2012-11-29 15:38:34 +0900 | [diff] [blame] | 1382 | hist_entry__add_pair(pos, pair); |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | return 0; |
| 1387 | } |
Namhyung Kim | f214833 | 2014-01-14 11:52:48 +0900 | [diff] [blame] | 1388 | |
| 1389 | u64 hists__total_period(struct hists *hists) |
| 1390 | { |
| 1391 | return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period : |
| 1392 | hists->stats.total_period; |
| 1393 | } |
Namhyung Kim | 33db456 | 2014-02-07 12:06:07 +0900 | [diff] [blame] | 1394 | |
| 1395 | int parse_filter_percentage(const struct option *opt __maybe_unused, |
| 1396 | const char *arg, int unset __maybe_unused) |
| 1397 | { |
| 1398 | if (!strcmp(arg, "relative")) |
| 1399 | symbol_conf.filter_relative = true; |
| 1400 | else if (!strcmp(arg, "absolute")) |
| 1401 | symbol_conf.filter_relative = false; |
| 1402 | else |
| 1403 | return -1; |
| 1404 | |
| 1405 | return 0; |
| 1406 | } |
Namhyung Kim | 0b93da1 | 2014-01-14 12:02:15 +0900 | [diff] [blame] | 1407 | |
| 1408 | int perf_hist_config(const char *var, const char *value) |
| 1409 | { |
| 1410 | if (!strcmp(var, "hist.percentage")) |
| 1411 | return parse_filter_percentage(NULL, value, 0); |
| 1412 | |
| 1413 | return 0; |
| 1414 | } |