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