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