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); |
Kan Liang | 21394d9 | 2015-09-04 10:45:44 -0400 | [diff] [blame] | 18 | static bool hists__filter_entry_by_socket(struct hists *hists, |
| 19 | struct hist_entry *he); |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 20 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 21 | 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] | 22 | { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 23 | return hists->col_len[col]; |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 24 | } |
| 25 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 26 | 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] | 27 | { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 28 | hists->col_len[col] = len; |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 29 | } |
| 30 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 31 | 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] | 32 | { |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 33 | if (len > hists__col_len(hists, col)) { |
| 34 | hists__set_col_len(hists, col, len); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 35 | return true; |
| 36 | } |
| 37 | return false; |
| 38 | } |
| 39 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 40 | void hists__reset_col_len(struct hists *hists) |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 41 | { |
| 42 | enum hist_column col; |
| 43 | |
| 44 | for (col = 0; col < HISTC_NR_COLS; ++col) |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 45 | hists__set_col_len(hists, col, 0); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 46 | } |
| 47 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 48 | static void hists__set_unres_dso_col_len(struct hists *hists, int dso) |
| 49 | { |
| 50 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; |
| 51 | |
| 52 | if (hists__col_len(hists, dso) < unresolved_col_width && |
| 53 | !symbol_conf.col_width_list_str && !symbol_conf.field_sep && |
| 54 | !symbol_conf.dso_list) |
| 55 | hists__set_col_len(hists, dso, unresolved_col_width); |
| 56 | } |
| 57 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 58 | 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] | 59 | { |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 60 | const unsigned int unresolved_col_width = BITS_PER_LONG / 4; |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 61 | int symlen; |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 62 | u16 len; |
| 63 | |
Namhyung Kim | ded19d5 | 2013-04-01 20:35:19 +0900 | [diff] [blame] | 64 | /* |
| 65 | * +4 accounts for '[x] ' priv level info |
| 66 | * +2 accounts for 0x prefix on raw addresses |
| 67 | * +3 accounts for ' y ' symtab origin info |
| 68 | */ |
| 69 | if (h->ms.sym) { |
| 70 | symlen = h->ms.sym->namelen + 4; |
| 71 | if (verbose) |
| 72 | symlen += BITS_PER_LONG / 4 + 2 + 3; |
| 73 | hists__new_col_len(hists, HISTC_SYMBOL, symlen); |
| 74 | } else { |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 75 | symlen = unresolved_col_width + 4 + 2; |
| 76 | hists__new_col_len(hists, HISTC_SYMBOL, symlen); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 77 | hists__set_unres_dso_col_len(hists, HISTC_DSO); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 78 | } |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 79 | |
| 80 | len = thread__comm_len(h->thread); |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 81 | if (hists__new_col_len(hists, HISTC_COMM, len)) |
Jiri Olsa | 89c7cb2 | 2016-06-20 23:58:19 +0200 | [diff] [blame] | 82 | hists__set_col_len(hists, HISTC_THREAD, len + 8); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 83 | |
| 84 | if (h->ms.map) { |
| 85 | len = dso__name_len(h->ms.map->dso); |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 86 | hists__new_col_len(hists, HISTC_DSO, len); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 87 | } |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 88 | |
Namhyung Kim | cb99374 | 2012-12-27 18:11:42 +0900 | [diff] [blame] | 89 | if (h->parent) |
| 90 | hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen); |
| 91 | |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 92 | if (h->branch_info) { |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 93 | if (h->branch_info->from.sym) { |
| 94 | symlen = (int)h->branch_info->from.sym->namelen + 4; |
Namhyung Kim | ded19d5 | 2013-04-01 20:35:19 +0900 | [diff] [blame] | 95 | if (verbose) |
| 96 | symlen += BITS_PER_LONG / 4 + 2 + 3; |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 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; |
Namhyung Kim | ded19d5 | 2013-04-01 20:35:19 +0900 | [diff] [blame] | 109 | if (verbose) |
| 110 | symlen += BITS_PER_LONG / 4 + 2 + 3; |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 111 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); |
| 112 | |
| 113 | symlen = dso__name_len(h->branch_info->to.map->dso); |
| 114 | hists__new_col_len(hists, HISTC_DSO_TO, symlen); |
| 115 | } else { |
| 116 | symlen = unresolved_col_width + 4 + 2; |
| 117 | hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen); |
| 118 | hists__set_unres_dso_col_len(hists, HISTC_DSO_TO); |
| 119 | } |
Andi Kleen | 508be0d | 2016-05-20 13:15:08 -0700 | [diff] [blame] | 120 | |
| 121 | if (h->branch_info->srcline_from) |
| 122 | hists__new_col_len(hists, HISTC_SRCLINE_FROM, |
| 123 | strlen(h->branch_info->srcline_from)); |
| 124 | if (h->branch_info->srcline_to) |
| 125 | hists__new_col_len(hists, HISTC_SRCLINE_TO, |
| 126 | strlen(h->branch_info->srcline_to)); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 127 | } |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 128 | |
| 129 | if (h->mem_info) { |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 130 | if (h->mem_info->daddr.sym) { |
| 131 | symlen = (int)h->mem_info->daddr.sym->namelen + 4 |
| 132 | + unresolved_col_width + 2; |
| 133 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, |
| 134 | symlen); |
Don Zickus | 9b32ba7 | 2014-06-01 15:38:29 +0200 | [diff] [blame] | 135 | hists__new_col_len(hists, HISTC_MEM_DCACHELINE, |
| 136 | symlen + 1); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 137 | } else { |
| 138 | symlen = unresolved_col_width + 4 + 2; |
| 139 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, |
| 140 | symlen); |
Jiri Olsa | 0805909 | 2016-01-20 12:56:33 +0100 | [diff] [blame] | 141 | hists__new_col_len(hists, HISTC_MEM_DCACHELINE, |
| 142 | symlen); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 143 | } |
Jiri Olsa | b34b3bf | 2015-10-05 20:06:08 +0200 | [diff] [blame] | 144 | |
| 145 | if (h->mem_info->iaddr.sym) { |
| 146 | symlen = (int)h->mem_info->iaddr.sym->namelen + 4 |
| 147 | + unresolved_col_width + 2; |
| 148 | hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, |
| 149 | symlen); |
| 150 | } else { |
| 151 | symlen = unresolved_col_width + 4 + 2; |
| 152 | hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, |
| 153 | symlen); |
| 154 | } |
| 155 | |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 156 | if (h->mem_info->daddr.map) { |
| 157 | symlen = dso__name_len(h->mem_info->daddr.map->dso); |
| 158 | hists__new_col_len(hists, HISTC_MEM_DADDR_DSO, |
| 159 | symlen); |
| 160 | } else { |
| 161 | symlen = unresolved_col_width + 4 + 2; |
| 162 | hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO); |
| 163 | } |
| 164 | } else { |
| 165 | symlen = unresolved_col_width + 4 + 2; |
| 166 | hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen); |
Jiri Olsa | b34b3bf | 2015-10-05 20:06:08 +0200 | [diff] [blame] | 167 | hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 168 | hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO); |
| 169 | } |
| 170 | |
Arnaldo Carvalho de Melo | a4978ec | 2015-09-09 12:14:00 -0300 | [diff] [blame] | 171 | hists__new_col_len(hists, HISTC_CPU, 3); |
Kan Liang | 2e7ea3a | 2015-09-04 10:45:43 -0400 | [diff] [blame] | 172 | hists__new_col_len(hists, HISTC_SOCKET, 6); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 173 | hists__new_col_len(hists, HISTC_MEM_LOCKED, 6); |
| 174 | hists__new_col_len(hists, HISTC_MEM_TLB, 22); |
| 175 | hists__new_col_len(hists, HISTC_MEM_SNOOP, 12); |
| 176 | hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3); |
| 177 | hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12); |
| 178 | hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12); |
Andi Kleen | 475eeab | 2013-09-20 07:40:43 -0700 | [diff] [blame] | 179 | |
Arnaldo Carvalho de Melo | e8e6d37 | 2015-08-10 16:53:54 -0300 | [diff] [blame] | 180 | if (h->srcline) |
| 181 | hists__new_col_len(hists, HISTC_SRCLINE, strlen(h->srcline)); |
| 182 | |
Andi Kleen | 31191a8 | 2015-08-07 15:54:24 -0700 | [diff] [blame] | 183 | if (h->srcfile) |
| 184 | hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile)); |
| 185 | |
Andi Kleen | 475eeab | 2013-09-20 07:40:43 -0700 | [diff] [blame] | 186 | if (h->transaction) |
| 187 | hists__new_col_len(hists, HISTC_TRANSACTION, |
| 188 | hist_entry__transaction_len()); |
Namhyung Kim | 0c0af78 | 2016-02-21 23:22:38 +0900 | [diff] [blame] | 189 | |
| 190 | if (h->trace_output) |
| 191 | hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output)); |
Arnaldo Carvalho de Melo | 8a6c5b2 | 2010-07-20 14:42:52 -0300 | [diff] [blame] | 192 | } |
| 193 | |
Namhyung Kim | 7ccf4f9 | 2012-08-20 13:52:05 +0900 | [diff] [blame] | 194 | void hists__output_recalc_col_len(struct hists *hists, int max_rows) |
| 195 | { |
| 196 | struct rb_node *next = rb_first(&hists->entries); |
| 197 | struct hist_entry *n; |
| 198 | int row = 0; |
| 199 | |
| 200 | hists__reset_col_len(hists); |
| 201 | |
| 202 | while (next && row++ < max_rows) { |
| 203 | n = rb_entry(next, struct hist_entry, rb_node); |
| 204 | if (!n->filtered) |
| 205 | hists__calc_col_len(hists, n); |
| 206 | next = rb_next(&n->rb_node); |
| 207 | } |
| 208 | } |
| 209 | |
Namhyung Kim | f39056f | 2014-01-14 14:25:37 +0900 | [diff] [blame] | 210 | static void he_stat__add_cpumode_period(struct he_stat *he_stat, |
| 211 | unsigned int cpumode, u64 period) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 212 | { |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 213 | switch (cpumode) { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 214 | case PERF_RECORD_MISC_KERNEL: |
Namhyung Kim | f39056f | 2014-01-14 14:25:37 +0900 | [diff] [blame] | 215 | he_stat->period_sys += period; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 216 | break; |
| 217 | case PERF_RECORD_MISC_USER: |
Namhyung Kim | f39056f | 2014-01-14 14:25:37 +0900 | [diff] [blame] | 218 | he_stat->period_us += period; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 219 | break; |
| 220 | case PERF_RECORD_MISC_GUEST_KERNEL: |
Namhyung Kim | f39056f | 2014-01-14 14:25:37 +0900 | [diff] [blame] | 221 | he_stat->period_guest_sys += period; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 222 | break; |
| 223 | case PERF_RECORD_MISC_GUEST_USER: |
Namhyung Kim | f39056f | 2014-01-14 14:25:37 +0900 | [diff] [blame] | 224 | he_stat->period_guest_us += period; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 225 | break; |
| 226 | default: |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 231 | static void he_stat__add_period(struct he_stat *he_stat, u64 period, |
| 232 | u64 weight) |
Namhyung Kim | 139c081 | 2012-10-04 21:49:43 +0900 | [diff] [blame] | 233 | { |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 234 | |
Namhyung Kim | 139c081 | 2012-10-04 21:49:43 +0900 | [diff] [blame] | 235 | he_stat->period += period; |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 236 | he_stat->weight += weight; |
Namhyung Kim | 139c081 | 2012-10-04 21:49:43 +0900 | [diff] [blame] | 237 | he_stat->nr_events += 1; |
| 238 | } |
| 239 | |
| 240 | static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src) |
| 241 | { |
| 242 | dest->period += src->period; |
| 243 | dest->period_sys += src->period_sys; |
| 244 | dest->period_us += src->period_us; |
| 245 | dest->period_guest_sys += src->period_guest_sys; |
| 246 | dest->period_guest_us += src->period_guest_us; |
| 247 | dest->nr_events += src->nr_events; |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 248 | dest->weight += src->weight; |
Namhyung Kim | 139c081 | 2012-10-04 21:49:43 +0900 | [diff] [blame] | 249 | } |
| 250 | |
Namhyung Kim | f39056f | 2014-01-14 14:25:37 +0900 | [diff] [blame] | 251 | static void he_stat__decay(struct he_stat *he_stat) |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 252 | { |
Namhyung Kim | f39056f | 2014-01-14 14:25:37 +0900 | [diff] [blame] | 253 | he_stat->period = (he_stat->period * 7) / 8; |
| 254 | he_stat->nr_events = (he_stat->nr_events * 7) / 8; |
Andi Kleen | 0548429 | 2013-01-24 16:10:29 +0100 | [diff] [blame] | 255 | /* XXX need decay for weight too? */ |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 256 | } |
| 257 | |
Namhyung Kim | 5d8200a | 2016-02-25 00:13:49 +0900 | [diff] [blame] | 258 | static void hists__delete_entry(struct hists *hists, struct hist_entry *he); |
| 259 | |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 260 | static bool hists__decay_entry(struct hists *hists, struct hist_entry *he) |
| 261 | { |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 262 | u64 prev_period = he->stat.period; |
Namhyung Kim | 3186b68 | 2014-04-22 13:44:23 +0900 | [diff] [blame] | 263 | u64 diff; |
Arnaldo Carvalho de Melo | c64550c | 2011-10-20 06:45:44 -0200 | [diff] [blame] | 264 | |
| 265 | if (prev_period == 0) |
Arnaldo Carvalho de Melo | df71d95 | 2011-10-13 08:01:33 -0300 | [diff] [blame] | 266 | return true; |
Arnaldo Carvalho de Melo | c64550c | 2011-10-20 06:45:44 -0200 | [diff] [blame] | 267 | |
Namhyung Kim | f39056f | 2014-01-14 14:25:37 +0900 | [diff] [blame] | 268 | he_stat__decay(&he->stat); |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 269 | if (symbol_conf.cumulate_callchain) |
| 270 | he_stat__decay(he->stat_acc); |
Namhyung Kim | 42b276a | 2016-01-05 12:06:00 +0900 | [diff] [blame] | 271 | decay_callchain(he->callchain); |
Arnaldo Carvalho de Melo | c64550c | 2011-10-20 06:45:44 -0200 | [diff] [blame] | 272 | |
Namhyung Kim | 3186b68 | 2014-04-22 13:44:23 +0900 | [diff] [blame] | 273 | diff = prev_period - he->stat.period; |
| 274 | |
Namhyung Kim | 5d8200a | 2016-02-25 00:13:49 +0900 | [diff] [blame] | 275 | if (!he->depth) { |
| 276 | hists->stats.total_period -= diff; |
| 277 | if (!he->filtered) |
| 278 | hists->stats.total_non_filtered_period -= diff; |
| 279 | } |
| 280 | |
| 281 | if (!he->leaf) { |
| 282 | struct hist_entry *child; |
| 283 | struct rb_node *node = rb_first(&he->hroot_out); |
| 284 | while (node) { |
| 285 | child = rb_entry(node, struct hist_entry, rb_node); |
| 286 | node = rb_next(node); |
| 287 | |
| 288 | if (hists__decay_entry(hists, child)) |
| 289 | hists__delete_entry(hists, child); |
| 290 | } |
| 291 | } |
Arnaldo Carvalho de Melo | c64550c | 2011-10-20 06:45:44 -0200 | [diff] [blame] | 292 | |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 293 | return he->stat.period == 0; |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 294 | } |
| 295 | |
Arnaldo Carvalho de Melo | 956b65e | 2014-12-19 12:41:28 -0300 | [diff] [blame] | 296 | static void hists__delete_entry(struct hists *hists, struct hist_entry *he) |
| 297 | { |
Namhyung Kim | 5d8200a | 2016-02-25 00:13:49 +0900 | [diff] [blame] | 298 | struct rb_root *root_in; |
| 299 | struct rb_root *root_out; |
Arnaldo Carvalho de Melo | 956b65e | 2014-12-19 12:41:28 -0300 | [diff] [blame] | 300 | |
Namhyung Kim | 5d8200a | 2016-02-25 00:13:49 +0900 | [diff] [blame] | 301 | if (he->parent_he) { |
| 302 | root_in = &he->parent_he->hroot_in; |
| 303 | root_out = &he->parent_he->hroot_out; |
| 304 | } else { |
Jiri Olsa | 5222503 | 2016-05-03 13:54:42 +0200 | [diff] [blame] | 305 | if (hists__has(hists, need_collapse)) |
Namhyung Kim | 5d8200a | 2016-02-25 00:13:49 +0900 | [diff] [blame] | 306 | root_in = &hists->entries_collapsed; |
| 307 | else |
| 308 | root_in = hists->entries_in; |
| 309 | root_out = &hists->entries; |
| 310 | } |
| 311 | |
| 312 | rb_erase(&he->rb_node_in, root_in); |
| 313 | rb_erase(&he->rb_node, root_out); |
Arnaldo Carvalho de Melo | 956b65e | 2014-12-19 12:41:28 -0300 | [diff] [blame] | 314 | |
| 315 | --hists->nr_entries; |
| 316 | if (!he->filtered) |
| 317 | --hists->nr_non_filtered_entries; |
| 318 | |
| 319 | hist_entry__delete(he); |
| 320 | } |
| 321 | |
Namhyung Kim | 3a5714f | 2013-05-14 11:09:01 +0900 | [diff] [blame] | 322 | 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] | 323 | { |
| 324 | struct rb_node *next = rb_first(&hists->entries); |
| 325 | struct hist_entry *n; |
| 326 | |
| 327 | while (next) { |
| 328 | n = rb_entry(next, struct hist_entry, rb_node); |
| 329 | next = rb_next(&n->rb_node); |
Arnaldo Carvalho de Melo | b079d4e | 2011-10-17 09:05:04 -0200 | [diff] [blame] | 330 | if (((zap_user && n->level == '.') || |
| 331 | (zap_kernel && n->level != '.') || |
Arnaldo Carvalho de Melo | 4c47f4f | 2015-03-17 17:18:58 -0300 | [diff] [blame] | 332 | hists__decay_entry(hists, n))) { |
Arnaldo Carvalho de Melo | 956b65e | 2014-12-19 12:41:28 -0300 | [diff] [blame] | 333 | hists__delete_entry(hists, n); |
Arnaldo Carvalho de Melo | ab81f3f | 2011-10-05 19:16:15 -0300 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
Namhyung Kim | 701937b | 2014-08-12 17:16:05 +0900 | [diff] [blame] | 338 | void hists__delete_entries(struct hists *hists) |
| 339 | { |
| 340 | struct rb_node *next = rb_first(&hists->entries); |
| 341 | struct hist_entry *n; |
| 342 | |
| 343 | while (next) { |
| 344 | n = rb_entry(next, struct hist_entry, rb_node); |
| 345 | next = rb_next(&n->rb_node); |
| 346 | |
Arnaldo Carvalho de Melo | 956b65e | 2014-12-19 12:41:28 -0300 | [diff] [blame] | 347 | hists__delete_entry(hists, n); |
Namhyung Kim | 701937b | 2014-08-12 17:16:05 +0900 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 351 | /* |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 352 | * histogram, sorted on item, collects periods |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 353 | */ |
| 354 | |
Jiri Olsa | 0a269a6 | 2016-07-05 08:56:03 +0200 | [diff] [blame] | 355 | static int hist_entry__init(struct hist_entry *he, |
| 356 | struct hist_entry *template, |
| 357 | bool sample_self) |
| 358 | { |
| 359 | *he = *template; |
| 360 | |
| 361 | if (symbol_conf.cumulate_callchain) { |
| 362 | he->stat_acc = malloc(sizeof(he->stat)); |
| 363 | if (he->stat_acc == NULL) |
| 364 | return -ENOMEM; |
| 365 | memcpy(he->stat_acc, &he->stat, sizeof(he->stat)); |
| 366 | if (!sample_self) |
| 367 | memset(&he->stat, 0, sizeof(he->stat)); |
| 368 | } |
| 369 | |
| 370 | map__get(he->ms.map); |
| 371 | |
| 372 | if (he->branch_info) { |
| 373 | /* |
| 374 | * This branch info is (a part of) allocated from |
| 375 | * sample__resolve_bstack() and will be freed after |
| 376 | * adding new entries. So we need to save a copy. |
| 377 | */ |
| 378 | he->branch_info = malloc(sizeof(*he->branch_info)); |
| 379 | if (he->branch_info == NULL) { |
| 380 | map__zput(he->ms.map); |
| 381 | free(he->stat_acc); |
| 382 | return -ENOMEM; |
| 383 | } |
| 384 | |
| 385 | memcpy(he->branch_info, template->branch_info, |
| 386 | sizeof(*he->branch_info)); |
| 387 | |
| 388 | map__get(he->branch_info->from.map); |
| 389 | map__get(he->branch_info->to.map); |
| 390 | } |
| 391 | |
| 392 | if (he->mem_info) { |
| 393 | map__get(he->mem_info->iaddr.map); |
| 394 | map__get(he->mem_info->daddr.map); |
| 395 | } |
| 396 | |
| 397 | if (symbol_conf.use_callchain) |
| 398 | callchain_init(he->callchain); |
| 399 | |
| 400 | if (he->raw_data) { |
| 401 | he->raw_data = memdup(he->raw_data, he->raw_size); |
| 402 | |
| 403 | if (he->raw_data == NULL) { |
| 404 | map__put(he->ms.map); |
| 405 | if (he->branch_info) { |
| 406 | map__put(he->branch_info->from.map); |
| 407 | map__put(he->branch_info->to.map); |
| 408 | free(he->branch_info); |
| 409 | } |
| 410 | if (he->mem_info) { |
| 411 | map__put(he->mem_info->iaddr.map); |
| 412 | map__put(he->mem_info->daddr.map); |
| 413 | } |
| 414 | free(he->stat_acc); |
| 415 | return -ENOMEM; |
| 416 | } |
| 417 | } |
| 418 | INIT_LIST_HEAD(&he->pairs.node); |
| 419 | thread__get(he->thread); |
| 420 | |
| 421 | if (!symbol_conf.report_hierarchy) |
| 422 | he->leaf = true; |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | |
Jiri Olsa | f542e76 | 2016-07-05 08:56:04 +0200 | [diff] [blame] | 427 | static void *hist_entry__zalloc(size_t size) |
| 428 | { |
| 429 | return zalloc(size + sizeof(struct hist_entry)); |
| 430 | } |
| 431 | |
| 432 | static void hist_entry__free(void *ptr) |
| 433 | { |
| 434 | free(ptr); |
| 435 | } |
| 436 | |
| 437 | static struct hist_entry_ops default_ops = { |
| 438 | .new = hist_entry__zalloc, |
| 439 | .free = hist_entry__free, |
| 440 | }; |
| 441 | |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 442 | static struct hist_entry *hist_entry__new(struct hist_entry *template, |
| 443 | bool sample_self) |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 444 | { |
Jiri Olsa | f542e76 | 2016-07-05 08:56:04 +0200 | [diff] [blame] | 445 | struct hist_entry_ops *ops = template->ops; |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 446 | size_t callchain_size = 0; |
| 447 | struct hist_entry *he; |
Jiri Olsa | 0a269a6 | 2016-07-05 08:56:03 +0200 | [diff] [blame] | 448 | int err = 0; |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 449 | |
Jiri Olsa | f542e76 | 2016-07-05 08:56:04 +0200 | [diff] [blame] | 450 | if (!ops) |
| 451 | ops = template->ops = &default_ops; |
| 452 | |
Namhyung Kim | 82aa019 | 2014-12-22 13:44:14 +0900 | [diff] [blame] | 453 | if (symbol_conf.use_callchain) |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 454 | callchain_size = sizeof(struct callchain_root); |
| 455 | |
Jiri Olsa | f542e76 | 2016-07-05 08:56:04 +0200 | [diff] [blame] | 456 | he = ops->new(callchain_size); |
Jiri Olsa | 0a269a6 | 2016-07-05 08:56:03 +0200 | [diff] [blame] | 457 | if (he) { |
| 458 | err = hist_entry__init(he, template, sample_self); |
Jiri Olsa | f542e76 | 2016-07-05 08:56:04 +0200 | [diff] [blame] | 459 | if (err) { |
| 460 | ops->free(he); |
| 461 | he = NULL; |
| 462 | } |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 463 | } |
| 464 | |
Arnaldo Carvalho de Melo | 12c1427 | 2012-01-04 12:27:03 -0200 | [diff] [blame] | 465 | return he; |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 466 | } |
| 467 | |
Arnaldo Carvalho de Melo | 7a007ca | 2010-07-21 09:19:41 -0300 | [diff] [blame] | 468 | static u8 symbol__parent_filter(const struct symbol *parent) |
| 469 | { |
| 470 | if (symbol_conf.exclude_other && parent == NULL) |
| 471 | return 1 << HIST_FILTER__PARENT; |
| 472 | return 0; |
| 473 | } |
| 474 | |
Namhyung Kim | 467ef10 | 2016-02-16 23:08:19 +0900 | [diff] [blame] | 475 | static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period) |
| 476 | { |
| 477 | if (!symbol_conf.use_callchain) |
| 478 | return; |
| 479 | |
| 480 | he->hists->callchain_period += period; |
| 481 | if (!he->filtered) |
| 482 | he->hists->callchain_non_filtered_period += period; |
| 483 | } |
| 484 | |
Arnaldo Carvalho de Melo | e7e0efc | 2015-05-19 11:31:22 -0300 | [diff] [blame] | 485 | static struct hist_entry *hists__findnew_entry(struct hists *hists, |
| 486 | struct hist_entry *entry, |
| 487 | struct addr_location *al, |
| 488 | bool sample_self) |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 489 | { |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 490 | struct rb_node **p; |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 491 | struct rb_node *parent = NULL; |
| 492 | struct hist_entry *he; |
Andi Kleen | 354cc40 | 2013-10-01 07:22:15 -0700 | [diff] [blame] | 493 | int64_t cmp; |
Namhyung Kim | f1cbf78 | 2013-12-18 14:21:11 +0900 | [diff] [blame] | 494 | u64 period = entry->stat.period; |
| 495 | u64 weight = entry->stat.weight; |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 496 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 497 | p = &hists->entries_in->rb_node; |
| 498 | |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 499 | while (*p != NULL) { |
| 500 | parent = *p; |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 501 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 502 | |
Namhyung Kim | 9afcf93 | 2012-12-10 17:29:54 +0900 | [diff] [blame] | 503 | /* |
| 504 | * Make sure that it receives arguments in a same order as |
| 505 | * hist_entry__collapse() so that we can use an appropriate |
| 506 | * function when searching an entry regardless which sort |
| 507 | * keys were used. |
| 508 | */ |
| 509 | cmp = hist_entry__cmp(he, entry); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 510 | |
| 511 | if (!cmp) { |
Namhyung Kim | 0f58474 | 2016-01-28 00:40:49 +0900 | [diff] [blame] | 512 | if (sample_self) { |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 513 | he_stat__add_period(&he->stat, period, weight); |
Namhyung Kim | 467ef10 | 2016-02-16 23:08:19 +0900 | [diff] [blame] | 514 | hist_entry__add_callchain_period(he, period); |
Namhyung Kim | 0f58474 | 2016-01-28 00:40:49 +0900 | [diff] [blame] | 515 | } |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 516 | if (symbol_conf.cumulate_callchain) |
| 517 | he_stat__add_period(he->stat_acc, period, weight); |
David Miller | 63fa471 | 2012-03-27 03:14:18 -0400 | [diff] [blame] | 518 | |
Namhyung Kim | ceb2acb | 2013-04-01 20:35:18 +0900 | [diff] [blame] | 519 | /* |
Arnaldo Carvalho de Melo | e80faac | 2014-01-22 13:05:06 -0300 | [diff] [blame] | 520 | * This mem info was allocated from sample__resolve_mem |
Namhyung Kim | ceb2acb | 2013-04-01 20:35:18 +0900 | [diff] [blame] | 521 | * and will not be used anymore. |
| 522 | */ |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 523 | zfree(&entry->mem_info); |
Namhyung Kim | ceb2acb | 2013-04-01 20:35:18 +0900 | [diff] [blame] | 524 | |
David Miller | 63fa471 | 2012-03-27 03:14:18 -0400 | [diff] [blame] | 525 | /* If the map of an existing hist_entry has |
| 526 | * become out-of-date due to an exec() or |
| 527 | * similar, update it. Otherwise we will |
| 528 | * mis-adjust symbol addresses when computing |
| 529 | * the history counter to increment. |
| 530 | */ |
| 531 | if (he->ms.map != entry->ms.map) { |
Arnaldo Carvalho de Melo | 5c24b67 | 2015-06-15 23:29:51 -0300 | [diff] [blame] | 532 | map__put(he->ms.map); |
| 533 | he->ms.map = map__get(entry->ms.map); |
David Miller | 63fa471 | 2012-03-27 03:14:18 -0400 | [diff] [blame] | 534 | } |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 535 | goto out; |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | if (cmp < 0) |
| 539 | p = &(*p)->rb_left; |
| 540 | else |
| 541 | p = &(*p)->rb_right; |
| 542 | } |
| 543 | |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 544 | he = hist_entry__new(entry, sample_self); |
Arnaldo Carvalho de Melo | 9735abf | 2009-10-03 10:42:45 -0300 | [diff] [blame] | 545 | if (!he) |
Namhyung Kim | 27a0dcb | 2013-05-14 11:09:02 +0900 | [diff] [blame] | 546 | return NULL; |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 547 | |
Namhyung Kim | 0f58474 | 2016-01-28 00:40:49 +0900 | [diff] [blame] | 548 | if (sample_self) |
Namhyung Kim | 467ef10 | 2016-02-16 23:08:19 +0900 | [diff] [blame] | 549 | hist_entry__add_callchain_period(he, period); |
| 550 | hists->nr_entries++; |
Namhyung Kim | 590cd34 | 2014-12-22 13:44:09 +0900 | [diff] [blame] | 551 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 552 | rb_link_node(&he->rb_node_in, parent, p); |
| 553 | rb_insert_color(&he->rb_node_in, hists->entries_in); |
Arnaldo Carvalho de Melo | 28e2a10 | 2010-05-09 13:02:23 -0300 | [diff] [blame] | 554 | out: |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 555 | if (sample_self) |
| 556 | he_stat__add_cpumode_period(&he->stat, al->cpumode, period); |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 557 | if (symbol_conf.cumulate_callchain) |
| 558 | 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] | 559 | return he; |
| 560 | } |
| 561 | |
Jiri Olsa | a505197 | 2016-07-05 08:56:05 +0200 | [diff] [blame] | 562 | static struct hist_entry* |
| 563 | __hists__add_entry(struct hists *hists, |
| 564 | struct addr_location *al, |
| 565 | struct symbol *sym_parent, |
| 566 | struct branch_info *bi, |
| 567 | struct mem_info *mi, |
| 568 | struct perf_sample *sample, |
| 569 | bool sample_self, |
| 570 | struct hist_entry_ops *ops) |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 571 | { |
| 572 | struct hist_entry entry = { |
| 573 | .thread = al->thread, |
Namhyung Kim | 4dfced3 | 2013-09-13 16:28:57 +0900 | [diff] [blame] | 574 | .comm = thread__comm(al->thread), |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 575 | .ms = { |
| 576 | .map = al->map, |
| 577 | .sym = al->sym, |
| 578 | }, |
Kan Liang | 0c4c4de | 2015-09-04 10:45:42 -0400 | [diff] [blame] | 579 | .socket = al->socket, |
Don Zickus | 7365be5 | 2014-05-27 12:28:05 -0400 | [diff] [blame] | 580 | .cpu = al->cpu, |
| 581 | .cpumode = al->cpumode, |
| 582 | .ip = al->addr, |
| 583 | .level = al->level, |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 584 | .stat = { |
Namhyung Kim | c4b3535 | 2012-10-04 21:49:42 +0900 | [diff] [blame] | 585 | .nr_events = 1, |
Namhyung Kim | fd36f3d | 2015-12-23 02:06:58 +0900 | [diff] [blame] | 586 | .period = sample->period, |
| 587 | .weight = sample->weight, |
Namhyung Kim | b24c28f | 2012-10-04 21:49:41 +0900 | [diff] [blame] | 588 | }, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 589 | .parent = sym_parent, |
Namhyung Kim | 2c86c7c | 2014-03-17 18:18:54 -0300 | [diff] [blame] | 590 | .filtered = symbol__parent_filter(sym_parent) | al->filtered, |
Arnaldo Carvalho de Melo | c824c43 | 2013-10-22 19:01:31 -0300 | [diff] [blame] | 591 | .hists = hists, |
Namhyung Kim | 41a4e6e | 2013-10-31 15:56:03 +0900 | [diff] [blame] | 592 | .branch_info = bi, |
| 593 | .mem_info = mi, |
Namhyung Kim | fd36f3d | 2015-12-23 02:06:58 +0900 | [diff] [blame] | 594 | .transaction = sample->transaction, |
Namhyung Kim | 7239283 | 2015-12-24 11:16:17 +0900 | [diff] [blame] | 595 | .raw_data = sample->raw_data, |
| 596 | .raw_size = sample->raw_size, |
Jiri Olsa | a505197 | 2016-07-05 08:56:05 +0200 | [diff] [blame] | 597 | .ops = ops, |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 598 | }; |
| 599 | |
Arnaldo Carvalho de Melo | e7e0efc | 2015-05-19 11:31:22 -0300 | [diff] [blame] | 600 | return hists__findnew_entry(hists, &entry, al, sample_self); |
Roberto Agostino Vitillo | b538752 | 2012-02-09 23:21:01 +0100 | [diff] [blame] | 601 | } |
| 602 | |
Jiri Olsa | a505197 | 2016-07-05 08:56:05 +0200 | [diff] [blame] | 603 | struct hist_entry *hists__add_entry(struct hists *hists, |
| 604 | struct addr_location *al, |
| 605 | struct symbol *sym_parent, |
| 606 | struct branch_info *bi, |
| 607 | struct mem_info *mi, |
| 608 | struct perf_sample *sample, |
| 609 | bool sample_self) |
| 610 | { |
| 611 | return __hists__add_entry(hists, al, sym_parent, bi, mi, |
| 612 | sample, sample_self, NULL); |
| 613 | } |
| 614 | |
| 615 | struct hist_entry *hists__add_entry_ops(struct hists *hists, |
| 616 | struct hist_entry_ops *ops, |
| 617 | struct addr_location *al, |
| 618 | struct symbol *sym_parent, |
| 619 | struct branch_info *bi, |
| 620 | struct mem_info *mi, |
| 621 | struct perf_sample *sample, |
| 622 | bool sample_self) |
| 623 | { |
| 624 | return __hists__add_entry(hists, al, sym_parent, bi, mi, |
| 625 | sample, sample_self, ops); |
| 626 | } |
| 627 | |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 628 | static int |
| 629 | iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused, |
| 630 | struct addr_location *al __maybe_unused) |
| 631 | { |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | static int |
| 636 | iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused, |
| 637 | struct addr_location *al __maybe_unused) |
| 638 | { |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | static int |
| 643 | iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al) |
| 644 | { |
| 645 | struct perf_sample *sample = iter->sample; |
| 646 | struct mem_info *mi; |
| 647 | |
| 648 | mi = sample__resolve_mem(sample, al); |
| 649 | if (mi == NULL) |
| 650 | return -ENOMEM; |
| 651 | |
| 652 | iter->priv = mi; |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | static int |
| 657 | iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al) |
| 658 | { |
| 659 | u64 cost; |
| 660 | struct mem_info *mi = iter->priv; |
Arnaldo Carvalho de Melo | 4ea062ed | 2014-10-09 13:13:41 -0300 | [diff] [blame] | 661 | struct hists *hists = evsel__hists(iter->evsel); |
Namhyung Kim | fd36f3d | 2015-12-23 02:06:58 +0900 | [diff] [blame] | 662 | struct perf_sample *sample = iter->sample; |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 663 | struct hist_entry *he; |
| 664 | |
| 665 | if (mi == NULL) |
| 666 | return -EINVAL; |
| 667 | |
Namhyung Kim | fd36f3d | 2015-12-23 02:06:58 +0900 | [diff] [blame] | 668 | cost = sample->weight; |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 669 | if (!cost) |
| 670 | cost = 1; |
| 671 | |
| 672 | /* |
| 673 | * must pass period=weight in order to get the correct |
| 674 | * sorting from hists__collapse_resort() which is solely |
| 675 | * based on periods. We want sorting be done on nr_events * weight |
| 676 | * and this is indirectly achieved by passing period=weight here |
| 677 | * and the he_stat__add_period() function. |
| 678 | */ |
Namhyung Kim | fd36f3d | 2015-12-23 02:06:58 +0900 | [diff] [blame] | 679 | sample->period = cost; |
| 680 | |
Jiri Olsa | 0102ef3 | 2016-06-14 20:19:21 +0200 | [diff] [blame] | 681 | he = hists__add_entry(hists, al, iter->parent, NULL, mi, |
| 682 | sample, true); |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 683 | if (!he) |
| 684 | return -ENOMEM; |
| 685 | |
| 686 | iter->he = he; |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | static int |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 691 | iter_finish_mem_entry(struct hist_entry_iter *iter, |
| 692 | struct addr_location *al __maybe_unused) |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 693 | { |
| 694 | struct perf_evsel *evsel = iter->evsel; |
Arnaldo Carvalho de Melo | 4ea062ed | 2014-10-09 13:13:41 -0300 | [diff] [blame] | 695 | struct hists *hists = evsel__hists(evsel); |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 696 | struct hist_entry *he = iter->he; |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 697 | int err = -EINVAL; |
| 698 | |
| 699 | if (he == NULL) |
| 700 | goto out; |
| 701 | |
Arnaldo Carvalho de Melo | 4ea062ed | 2014-10-09 13:13:41 -0300 | [diff] [blame] | 702 | hists__inc_nr_samples(hists, he->filtered); |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 703 | |
| 704 | err = hist_entry__append_callchain(he, iter->sample); |
| 705 | |
| 706 | out: |
| 707 | /* |
Arnaldo Carvalho de Melo | e7e0efc | 2015-05-19 11:31:22 -0300 | [diff] [blame] | 708 | * We don't need to free iter->priv (mem_info) here since the mem info |
| 709 | * was either already freed in hists__findnew_entry() or passed to a |
| 710 | * new hist entry by hist_entry__new(). |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 711 | */ |
| 712 | iter->priv = NULL; |
| 713 | |
| 714 | iter->he = NULL; |
| 715 | return err; |
| 716 | } |
| 717 | |
| 718 | static int |
| 719 | iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) |
| 720 | { |
| 721 | struct branch_info *bi; |
| 722 | struct perf_sample *sample = iter->sample; |
| 723 | |
| 724 | bi = sample__resolve_bstack(sample, al); |
| 725 | if (!bi) |
| 726 | return -ENOMEM; |
| 727 | |
| 728 | iter->curr = 0; |
| 729 | iter->total = sample->branch_stack->nr; |
| 730 | |
| 731 | iter->priv = bi; |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | static int |
Arnaldo Carvalho de Melo | b8f8eb8 | 2016-03-22 13:09:37 -0300 | [diff] [blame] | 736 | iter_add_single_branch_entry(struct hist_entry_iter *iter, |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 737 | struct addr_location *al __maybe_unused) |
| 738 | { |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 739 | /* to avoid calling callback function */ |
| 740 | iter->he = NULL; |
| 741 | |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | static int |
| 746 | iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) |
| 747 | { |
| 748 | struct branch_info *bi = iter->priv; |
| 749 | int i = iter->curr; |
| 750 | |
| 751 | if (bi == NULL) |
| 752 | return 0; |
| 753 | |
| 754 | if (iter->curr >= iter->total) |
| 755 | return 0; |
| 756 | |
| 757 | al->map = bi[i].to.map; |
| 758 | al->sym = bi[i].to.sym; |
| 759 | al->addr = bi[i].to.addr; |
| 760 | return 1; |
| 761 | } |
| 762 | |
| 763 | static int |
| 764 | iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al) |
| 765 | { |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 766 | struct branch_info *bi; |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 767 | struct perf_evsel *evsel = iter->evsel; |
Arnaldo Carvalho de Melo | 4ea062ed | 2014-10-09 13:13:41 -0300 | [diff] [blame] | 768 | struct hists *hists = evsel__hists(evsel); |
Namhyung Kim | fd36f3d | 2015-12-23 02:06:58 +0900 | [diff] [blame] | 769 | struct perf_sample *sample = iter->sample; |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 770 | struct hist_entry *he = NULL; |
| 771 | int i = iter->curr; |
| 772 | int err = 0; |
| 773 | |
| 774 | bi = iter->priv; |
| 775 | |
| 776 | if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym)) |
| 777 | goto out; |
| 778 | |
| 779 | /* |
| 780 | * The report shows the percentage of total branches captured |
| 781 | * and not events sampled. Thus we use a pseudo period of 1. |
| 782 | */ |
Namhyung Kim | fd36f3d | 2015-12-23 02:06:58 +0900 | [diff] [blame] | 783 | sample->period = 1; |
| 784 | sample->weight = bi->flags.cycles ? bi->flags.cycles : 1; |
| 785 | |
Jiri Olsa | 0102ef3 | 2016-06-14 20:19:21 +0200 | [diff] [blame] | 786 | he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL, |
| 787 | sample, true); |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 788 | if (he == NULL) |
| 789 | return -ENOMEM; |
| 790 | |
Arnaldo Carvalho de Melo | 4ea062ed | 2014-10-09 13:13:41 -0300 | [diff] [blame] | 791 | hists__inc_nr_samples(hists, he->filtered); |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 792 | |
| 793 | out: |
| 794 | iter->he = he; |
| 795 | iter->curr++; |
| 796 | return err; |
| 797 | } |
| 798 | |
| 799 | static int |
| 800 | iter_finish_branch_entry(struct hist_entry_iter *iter, |
| 801 | struct addr_location *al __maybe_unused) |
| 802 | { |
| 803 | zfree(&iter->priv); |
| 804 | iter->he = NULL; |
| 805 | |
| 806 | return iter->curr >= iter->total ? 0 : -1; |
| 807 | } |
| 808 | |
| 809 | static int |
| 810 | iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused, |
| 811 | struct addr_location *al __maybe_unused) |
| 812 | { |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | static int |
| 817 | iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al) |
| 818 | { |
| 819 | struct perf_evsel *evsel = iter->evsel; |
| 820 | struct perf_sample *sample = iter->sample; |
| 821 | struct hist_entry *he; |
| 822 | |
Jiri Olsa | 0102ef3 | 2016-06-14 20:19:21 +0200 | [diff] [blame] | 823 | he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL, |
| 824 | sample, true); |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 825 | if (he == NULL) |
| 826 | return -ENOMEM; |
| 827 | |
| 828 | iter->he = he; |
| 829 | return 0; |
| 830 | } |
| 831 | |
| 832 | static int |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 833 | iter_finish_normal_entry(struct hist_entry_iter *iter, |
| 834 | struct addr_location *al __maybe_unused) |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 835 | { |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 836 | struct hist_entry *he = iter->he; |
| 837 | struct perf_evsel *evsel = iter->evsel; |
| 838 | struct perf_sample *sample = iter->sample; |
| 839 | |
| 840 | if (he == NULL) |
| 841 | return 0; |
| 842 | |
| 843 | iter->he = NULL; |
| 844 | |
Arnaldo Carvalho de Melo | 4ea062ed | 2014-10-09 13:13:41 -0300 | [diff] [blame] | 845 | hists__inc_nr_samples(evsel__hists(evsel), he->filtered); |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 846 | |
| 847 | return hist_entry__append_callchain(he, sample); |
| 848 | } |
| 849 | |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 850 | static int |
Adrian Hunter | 96b40f3 | 2015-09-25 16:15:47 +0300 | [diff] [blame] | 851 | iter_prepare_cumulative_entry(struct hist_entry_iter *iter, |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 852 | struct addr_location *al __maybe_unused) |
| 853 | { |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 854 | struct hist_entry **he_cache; |
| 855 | |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 856 | callchain_cursor_commit(&callchain_cursor); |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 857 | |
| 858 | /* |
| 859 | * This is for detecting cycles or recursions so that they're |
| 860 | * cumulated only one time to prevent entries more than 100% |
| 861 | * overhead. |
| 862 | */ |
Adrian Hunter | 96b40f3 | 2015-09-25 16:15:47 +0300 | [diff] [blame] | 863 | he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1)); |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 864 | if (he_cache == NULL) |
| 865 | return -ENOMEM; |
| 866 | |
| 867 | iter->priv = he_cache; |
| 868 | iter->curr = 0; |
| 869 | |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | static int |
| 874 | iter_add_single_cumulative_entry(struct hist_entry_iter *iter, |
| 875 | struct addr_location *al) |
| 876 | { |
| 877 | struct perf_evsel *evsel = iter->evsel; |
Arnaldo Carvalho de Melo | 4ea062ed | 2014-10-09 13:13:41 -0300 | [diff] [blame] | 878 | struct hists *hists = evsel__hists(evsel); |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 879 | struct perf_sample *sample = iter->sample; |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 880 | struct hist_entry **he_cache = iter->priv; |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 881 | struct hist_entry *he; |
| 882 | int err = 0; |
| 883 | |
Jiri Olsa | 0102ef3 | 2016-06-14 20:19:21 +0200 | [diff] [blame] | 884 | he = hists__add_entry(hists, al, iter->parent, NULL, NULL, |
| 885 | sample, true); |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 886 | if (he == NULL) |
| 887 | return -ENOMEM; |
| 888 | |
| 889 | iter->he = he; |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 890 | he_cache[iter->curr++] = he; |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 891 | |
Namhyung Kim | 82aa019 | 2014-12-22 13:44:14 +0900 | [diff] [blame] | 892 | hist_entry__append_callchain(he, sample); |
Namhyung Kim | be7f855 | 2013-12-26 17:44:10 +0900 | [diff] [blame] | 893 | |
| 894 | /* |
| 895 | * We need to re-initialize the cursor since callchain_append() |
| 896 | * advanced the cursor to the end. |
| 897 | */ |
| 898 | callchain_cursor_commit(&callchain_cursor); |
| 899 | |
Arnaldo Carvalho de Melo | 4ea062ed | 2014-10-09 13:13:41 -0300 | [diff] [blame] | 900 | hists__inc_nr_samples(hists, he->filtered); |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 901 | |
| 902 | return err; |
| 903 | } |
| 904 | |
| 905 | static int |
| 906 | iter_next_cumulative_entry(struct hist_entry_iter *iter, |
| 907 | struct addr_location *al) |
| 908 | { |
| 909 | struct callchain_cursor_node *node; |
| 910 | |
| 911 | node = callchain_cursor_current(&callchain_cursor); |
| 912 | if (node == NULL) |
| 913 | return 0; |
| 914 | |
Namhyung Kim | c7405d8 | 2013-10-31 13:58:30 +0900 | [diff] [blame] | 915 | return fill_callchain_info(al, node, iter->hide_unresolved); |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | static int |
| 919 | iter_add_next_cumulative_entry(struct hist_entry_iter *iter, |
| 920 | struct addr_location *al) |
| 921 | { |
| 922 | struct perf_evsel *evsel = iter->evsel; |
| 923 | struct perf_sample *sample = iter->sample; |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 924 | struct hist_entry **he_cache = iter->priv; |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 925 | struct hist_entry *he; |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 926 | struct hist_entry he_tmp = { |
Arnaldo Carvalho de Melo | 5cef897 | 2015-08-10 15:45:55 -0300 | [diff] [blame] | 927 | .hists = evsel__hists(evsel), |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 928 | .cpu = al->cpu, |
| 929 | .thread = al->thread, |
| 930 | .comm = thread__comm(al->thread), |
| 931 | .ip = al->addr, |
| 932 | .ms = { |
| 933 | .map = al->map, |
| 934 | .sym = al->sym, |
| 935 | }, |
| 936 | .parent = iter->parent, |
Namhyung Kim | 7239283 | 2015-12-24 11:16:17 +0900 | [diff] [blame] | 937 | .raw_data = sample->raw_data, |
| 938 | .raw_size = sample->raw_size, |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 939 | }; |
| 940 | int i; |
Namhyung Kim | be7f855 | 2013-12-26 17:44:10 +0900 | [diff] [blame] | 941 | struct callchain_cursor cursor; |
| 942 | |
| 943 | callchain_cursor_snapshot(&cursor, &callchain_cursor); |
| 944 | |
| 945 | callchain_cursor_advance(&callchain_cursor); |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 946 | |
| 947 | /* |
| 948 | * Check if there's duplicate entries in the callchain. |
| 949 | * It's possible that it has cycles or recursive calls. |
| 950 | */ |
| 951 | for (i = 0; i < iter->curr; i++) { |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 952 | if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) { |
| 953 | /* to avoid calling callback function */ |
| 954 | iter->he = NULL; |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 955 | return 0; |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 956 | } |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 957 | } |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 958 | |
Jiri Olsa | 0102ef3 | 2016-06-14 20:19:21 +0200 | [diff] [blame] | 959 | he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL, |
| 960 | sample, false); |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 961 | if (he == NULL) |
| 962 | return -ENOMEM; |
| 963 | |
| 964 | iter->he = he; |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 965 | he_cache[iter->curr++] = he; |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 966 | |
Namhyung Kim | 82aa019 | 2014-12-22 13:44:14 +0900 | [diff] [blame] | 967 | if (symbol_conf.use_callchain) |
| 968 | callchain_append(he->callchain, &cursor, sample->period); |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 969 | return 0; |
| 970 | } |
| 971 | |
| 972 | static int |
| 973 | iter_finish_cumulative_entry(struct hist_entry_iter *iter, |
| 974 | struct addr_location *al __maybe_unused) |
| 975 | { |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 976 | zfree(&iter->priv); |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 977 | iter->he = NULL; |
Namhyung Kim | b4d3c8b | 2013-10-31 10:05:29 +0900 | [diff] [blame] | 978 | |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 979 | return 0; |
| 980 | } |
| 981 | |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 982 | const struct hist_iter_ops hist_iter_mem = { |
| 983 | .prepare_entry = iter_prepare_mem_entry, |
| 984 | .add_single_entry = iter_add_single_mem_entry, |
| 985 | .next_entry = iter_next_nop_entry, |
| 986 | .add_next_entry = iter_add_next_nop_entry, |
| 987 | .finish_entry = iter_finish_mem_entry, |
| 988 | }; |
| 989 | |
| 990 | const struct hist_iter_ops hist_iter_branch = { |
| 991 | .prepare_entry = iter_prepare_branch_entry, |
| 992 | .add_single_entry = iter_add_single_branch_entry, |
| 993 | .next_entry = iter_next_branch_entry, |
| 994 | .add_next_entry = iter_add_next_branch_entry, |
| 995 | .finish_entry = iter_finish_branch_entry, |
| 996 | }; |
| 997 | |
| 998 | const struct hist_iter_ops hist_iter_normal = { |
| 999 | .prepare_entry = iter_prepare_normal_entry, |
| 1000 | .add_single_entry = iter_add_single_normal_entry, |
| 1001 | .next_entry = iter_next_nop_entry, |
| 1002 | .add_next_entry = iter_add_next_nop_entry, |
| 1003 | .finish_entry = iter_finish_normal_entry, |
| 1004 | }; |
| 1005 | |
Namhyung Kim | 7a13aa2 | 2012-09-11 14:13:04 +0900 | [diff] [blame] | 1006 | const struct hist_iter_ops hist_iter_cumulative = { |
| 1007 | .prepare_entry = iter_prepare_cumulative_entry, |
| 1008 | .add_single_entry = iter_add_single_cumulative_entry, |
| 1009 | .next_entry = iter_next_cumulative_entry, |
| 1010 | .add_next_entry = iter_add_next_cumulative_entry, |
| 1011 | .finish_entry = iter_finish_cumulative_entry, |
| 1012 | }; |
| 1013 | |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 1014 | 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] | 1015 | int max_stack_depth, void *arg) |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 1016 | { |
| 1017 | int err, err2; |
| 1018 | |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 1019 | err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent, |
Namhyung Kim | 063bd93 | 2015-05-19 17:04:10 +0900 | [diff] [blame] | 1020 | iter->evsel, al, max_stack_depth); |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 1021 | if (err) |
| 1022 | return err; |
| 1023 | |
Adrian Hunter | 96b40f3 | 2015-09-25 16:15:47 +0300 | [diff] [blame] | 1024 | iter->max_stack = max_stack_depth; |
| 1025 | |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 1026 | err = iter->ops->prepare_entry(iter, al); |
| 1027 | if (err) |
| 1028 | goto out; |
| 1029 | |
| 1030 | err = iter->ops->add_single_entry(iter, al); |
| 1031 | if (err) |
| 1032 | goto out; |
| 1033 | |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 1034 | if (iter->he && iter->add_entry_cb) { |
| 1035 | err = iter->add_entry_cb(iter, al, true, arg); |
| 1036 | if (err) |
| 1037 | goto out; |
| 1038 | } |
| 1039 | |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 1040 | while (iter->ops->next_entry(iter, al)) { |
| 1041 | err = iter->ops->add_next_entry(iter, al); |
| 1042 | if (err) |
| 1043 | break; |
Namhyung Kim | 9d3c02d | 2014-01-07 17:02:25 +0900 | [diff] [blame] | 1044 | |
| 1045 | if (iter->he && iter->add_entry_cb) { |
| 1046 | err = iter->add_entry_cb(iter, al, false, arg); |
| 1047 | if (err) |
| 1048 | goto out; |
| 1049 | } |
Namhyung Kim | 69bcb01 | 2013-10-30 09:40:34 +0900 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | out: |
| 1053 | err2 = iter->ops->finish_entry(iter, al); |
| 1054 | if (!err) |
| 1055 | err = err2; |
| 1056 | |
| 1057 | return err; |
| 1058 | } |
| 1059 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1060 | int64_t |
| 1061 | hist_entry__cmp(struct hist_entry *left, struct hist_entry *right) |
| 1062 | { |
Jiri Olsa | aa6f50a | 2016-01-18 10:24:24 +0100 | [diff] [blame] | 1063 | struct hists *hists = left->hists; |
Namhyung Kim | 093f0ef3 | 2014-03-03 12:07:47 +0900 | [diff] [blame] | 1064 | struct perf_hpp_fmt *fmt; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1065 | int64_t cmp = 0; |
| 1066 | |
Jiri Olsa | aa6f50a | 2016-01-18 10:24:24 +0100 | [diff] [blame] | 1067 | hists__for_each_sort_list(hists, fmt) { |
Namhyung Kim | 84b6ee8 | 2016-02-27 03:52:43 +0900 | [diff] [blame] | 1068 | if (perf_hpp__is_dynamic_entry(fmt) && |
| 1069 | !perf_hpp__defined_dynamic_entry(fmt, hists)) |
| 1070 | continue; |
| 1071 | |
Namhyung Kim | 87bbdf7 | 2015-01-08 09:45:46 +0900 | [diff] [blame] | 1072 | cmp = fmt->cmp(fmt, left, right); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1073 | if (cmp) |
| 1074 | break; |
| 1075 | } |
| 1076 | |
| 1077 | return cmp; |
| 1078 | } |
| 1079 | |
| 1080 | int64_t |
| 1081 | hist_entry__collapse(struct hist_entry *left, struct hist_entry *right) |
| 1082 | { |
Jiri Olsa | aa6f50a | 2016-01-18 10:24:24 +0100 | [diff] [blame] | 1083 | struct hists *hists = left->hists; |
Namhyung Kim | 093f0ef3 | 2014-03-03 12:07:47 +0900 | [diff] [blame] | 1084 | struct perf_hpp_fmt *fmt; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1085 | int64_t cmp = 0; |
| 1086 | |
Jiri Olsa | aa6f50a | 2016-01-18 10:24:24 +0100 | [diff] [blame] | 1087 | hists__for_each_sort_list(hists, fmt) { |
Namhyung Kim | 84b6ee8 | 2016-02-27 03:52:43 +0900 | [diff] [blame] | 1088 | if (perf_hpp__is_dynamic_entry(fmt) && |
| 1089 | !perf_hpp__defined_dynamic_entry(fmt, hists)) |
| 1090 | continue; |
| 1091 | |
Namhyung Kim | 87bbdf7 | 2015-01-08 09:45:46 +0900 | [diff] [blame] | 1092 | cmp = fmt->collapse(fmt, left, right); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1093 | if (cmp) |
| 1094 | break; |
| 1095 | } |
| 1096 | |
| 1097 | return cmp; |
| 1098 | } |
| 1099 | |
Arnaldo Carvalho de Melo | 6733d1b | 2014-12-19 12:31:40 -0300 | [diff] [blame] | 1100 | void hist_entry__delete(struct hist_entry *he) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1101 | { |
Jiri Olsa | f542e76 | 2016-07-05 08:56:04 +0200 | [diff] [blame] | 1102 | struct hist_entry_ops *ops = he->ops; |
| 1103 | |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 1104 | thread__zput(he->thread); |
Arnaldo Carvalho de Melo | 5c24b67 | 2015-06-15 23:29:51 -0300 | [diff] [blame] | 1105 | map__zput(he->ms.map); |
| 1106 | |
| 1107 | if (he->branch_info) { |
| 1108 | map__zput(he->branch_info->from.map); |
| 1109 | map__zput(he->branch_info->to.map); |
Andi Kleen | 508be0d | 2016-05-20 13:15:08 -0700 | [diff] [blame] | 1110 | free_srcline(he->branch_info->srcline_from); |
| 1111 | free_srcline(he->branch_info->srcline_to); |
Arnaldo Carvalho de Melo | 5c24b67 | 2015-06-15 23:29:51 -0300 | [diff] [blame] | 1112 | zfree(&he->branch_info); |
| 1113 | } |
| 1114 | |
| 1115 | if (he->mem_info) { |
| 1116 | map__zput(he->mem_info->iaddr.map); |
| 1117 | map__zput(he->mem_info->daddr.map); |
| 1118 | zfree(&he->mem_info); |
| 1119 | } |
| 1120 | |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 1121 | zfree(&he->stat_acc); |
Namhyung Kim | f048d54 | 2013-09-11 14:09:28 +0900 | [diff] [blame] | 1122 | free_srcline(he->srcline); |
Andi Kleen | 31191a8 | 2015-08-07 15:54:24 -0700 | [diff] [blame] | 1123 | if (he->srcfile && he->srcfile[0]) |
| 1124 | free(he->srcfile); |
Namhyung Kim | d114960 | 2014-12-30 14:38:13 +0900 | [diff] [blame] | 1125 | free_callchain(he->callchain); |
Namhyung Kim | 60517d2 | 2015-12-23 02:07:03 +0900 | [diff] [blame] | 1126 | free(he->trace_output); |
Namhyung Kim | 7239283 | 2015-12-24 11:16:17 +0900 | [diff] [blame] | 1127 | free(he->raw_data); |
Jiri Olsa | f542e76 | 2016-07-05 08:56:04 +0200 | [diff] [blame] | 1128 | ops->free(he); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /* |
Arnaldo Carvalho de Melo | 89fee70 | 2016-02-11 17:14:13 -0300 | [diff] [blame] | 1132 | * If this is not the last column, then we need to pad it according to the |
| 1133 | * pre-calculated max lenght for this column, otherwise don't bother adding |
| 1134 | * spaces because that would break viewing this with, for instance, 'less', |
| 1135 | * that would show tons of trailing spaces when a long C++ demangled method |
| 1136 | * names is sampled. |
| 1137 | */ |
| 1138 | int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp, |
| 1139 | struct perf_hpp_fmt *fmt, int printed) |
| 1140 | { |
| 1141 | if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) { |
Jiri Olsa | da1b040 | 2016-06-14 20:19:20 +0200 | [diff] [blame] | 1142 | const int width = fmt->width(fmt, hpp, he->hists); |
Arnaldo Carvalho de Melo | 89fee70 | 2016-02-11 17:14:13 -0300 | [diff] [blame] | 1143 | if (printed < width) { |
| 1144 | advance_hpp(hpp, printed); |
| 1145 | printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " "); |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | return printed; |
| 1150 | } |
| 1151 | |
| 1152 | /* |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1153 | * collapse the histogram |
| 1154 | */ |
| 1155 | |
Namhyung Kim | aef810e | 2016-02-25 00:13:34 +0900 | [diff] [blame] | 1156 | static void hists__apply_filters(struct hists *hists, struct hist_entry *he); |
Namhyung Kim | aec13a7 | 2016-03-09 22:46:58 +0900 | [diff] [blame] | 1157 | static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he, |
| 1158 | enum hist_filter type); |
| 1159 | |
| 1160 | typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt); |
| 1161 | |
| 1162 | static bool check_thread_entry(struct perf_hpp_fmt *fmt) |
| 1163 | { |
| 1164 | return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt); |
| 1165 | } |
| 1166 | |
| 1167 | static void hist_entry__check_and_remove_filter(struct hist_entry *he, |
| 1168 | enum hist_filter type, |
| 1169 | fmt_chk_fn check) |
| 1170 | { |
| 1171 | struct perf_hpp_fmt *fmt; |
| 1172 | bool type_match = false; |
| 1173 | struct hist_entry *parent = he->parent_he; |
| 1174 | |
| 1175 | switch (type) { |
| 1176 | case HIST_FILTER__THREAD: |
| 1177 | if (symbol_conf.comm_list == NULL && |
| 1178 | symbol_conf.pid_list == NULL && |
| 1179 | symbol_conf.tid_list == NULL) |
| 1180 | return; |
| 1181 | break; |
| 1182 | case HIST_FILTER__DSO: |
| 1183 | if (symbol_conf.dso_list == NULL) |
| 1184 | return; |
| 1185 | break; |
| 1186 | case HIST_FILTER__SYMBOL: |
| 1187 | if (symbol_conf.sym_list == NULL) |
| 1188 | return; |
| 1189 | break; |
| 1190 | case HIST_FILTER__PARENT: |
| 1191 | case HIST_FILTER__GUEST: |
| 1192 | case HIST_FILTER__HOST: |
| 1193 | case HIST_FILTER__SOCKET: |
| 1194 | default: |
| 1195 | return; |
| 1196 | } |
| 1197 | |
| 1198 | /* if it's filtered by own fmt, it has to have filter bits */ |
| 1199 | perf_hpp_list__for_each_format(he->hpp_list, fmt) { |
| 1200 | if (check(fmt)) { |
| 1201 | type_match = true; |
| 1202 | break; |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | if (type_match) { |
| 1207 | /* |
| 1208 | * If the filter is for current level entry, propagate |
| 1209 | * filter marker to parents. The marker bit was |
| 1210 | * already set by default so it only needs to clear |
| 1211 | * non-filtered entries. |
| 1212 | */ |
| 1213 | if (!(he->filtered & (1 << type))) { |
| 1214 | while (parent) { |
| 1215 | parent->filtered &= ~(1 << type); |
| 1216 | parent = parent->parent_he; |
| 1217 | } |
| 1218 | } |
| 1219 | } else { |
| 1220 | /* |
| 1221 | * If current entry doesn't have matching formats, set |
| 1222 | * filter marker for upper level entries. it will be |
| 1223 | * cleared if its lower level entries is not filtered. |
| 1224 | * |
| 1225 | * For lower-level entries, it inherits parent's |
| 1226 | * filter bit so that lower level entries of a |
| 1227 | * non-filtered entry won't set the filter marker. |
| 1228 | */ |
| 1229 | if (parent == NULL) |
| 1230 | he->filtered |= (1 << type); |
| 1231 | else |
| 1232 | he->filtered |= (parent->filtered & (1 << type)); |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | static void hist_entry__apply_hierarchy_filters(struct hist_entry *he) |
| 1237 | { |
| 1238 | hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD, |
| 1239 | check_thread_entry); |
| 1240 | |
| 1241 | hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO, |
| 1242 | perf_hpp__is_dso_entry); |
| 1243 | |
| 1244 | hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL, |
| 1245 | perf_hpp__is_sym_entry); |
| 1246 | |
| 1247 | hists__apply_filters(he->hists, he); |
| 1248 | } |
Namhyung Kim | aef810e | 2016-02-25 00:13:34 +0900 | [diff] [blame] | 1249 | |
| 1250 | static struct hist_entry *hierarchy_insert_entry(struct hists *hists, |
| 1251 | struct rb_root *root, |
| 1252 | struct hist_entry *he, |
Namhyung Kim | aec13a7 | 2016-03-09 22:46:58 +0900 | [diff] [blame] | 1253 | struct hist_entry *parent_he, |
Namhyung Kim | 1b2dbbf | 2016-03-07 16:44:46 -0300 | [diff] [blame] | 1254 | struct perf_hpp_list *hpp_list) |
Namhyung Kim | aef810e | 2016-02-25 00:13:34 +0900 | [diff] [blame] | 1255 | { |
| 1256 | struct rb_node **p = &root->rb_node; |
| 1257 | struct rb_node *parent = NULL; |
| 1258 | struct hist_entry *iter, *new; |
Namhyung Kim | 1b2dbbf | 2016-03-07 16:44:46 -0300 | [diff] [blame] | 1259 | struct perf_hpp_fmt *fmt; |
Namhyung Kim | aef810e | 2016-02-25 00:13:34 +0900 | [diff] [blame] | 1260 | int64_t cmp; |
| 1261 | |
| 1262 | while (*p != NULL) { |
| 1263 | parent = *p; |
| 1264 | iter = rb_entry(parent, struct hist_entry, rb_node_in); |
| 1265 | |
Namhyung Kim | 1b2dbbf | 2016-03-07 16:44:46 -0300 | [diff] [blame] | 1266 | cmp = 0; |
| 1267 | perf_hpp_list__for_each_sort_list(hpp_list, fmt) { |
| 1268 | cmp = fmt->collapse(fmt, iter, he); |
| 1269 | if (cmp) |
| 1270 | break; |
| 1271 | } |
| 1272 | |
Namhyung Kim | aef810e | 2016-02-25 00:13:34 +0900 | [diff] [blame] | 1273 | if (!cmp) { |
| 1274 | he_stat__add_stat(&iter->stat, &he->stat); |
| 1275 | return iter; |
| 1276 | } |
| 1277 | |
| 1278 | if (cmp < 0) |
| 1279 | p = &parent->rb_left; |
| 1280 | else |
| 1281 | p = &parent->rb_right; |
| 1282 | } |
| 1283 | |
| 1284 | new = hist_entry__new(he, true); |
| 1285 | if (new == NULL) |
| 1286 | return NULL; |
| 1287 | |
Namhyung Kim | aef810e | 2016-02-25 00:13:34 +0900 | [diff] [blame] | 1288 | hists->nr_entries++; |
| 1289 | |
Namhyung Kim | 1b2dbbf | 2016-03-07 16:44:46 -0300 | [diff] [blame] | 1290 | /* save related format list for output */ |
| 1291 | new->hpp_list = hpp_list; |
Namhyung Kim | aec13a7 | 2016-03-09 22:46:58 +0900 | [diff] [blame] | 1292 | new->parent_he = parent_he; |
| 1293 | |
| 1294 | hist_entry__apply_hierarchy_filters(new); |
Namhyung Kim | aef810e | 2016-02-25 00:13:34 +0900 | [diff] [blame] | 1295 | |
| 1296 | /* some fields are now passed to 'new' */ |
Namhyung Kim | 1b2dbbf | 2016-03-07 16:44:46 -0300 | [diff] [blame] | 1297 | perf_hpp_list__for_each_sort_list(hpp_list, fmt) { |
| 1298 | if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt)) |
| 1299 | he->trace_output = NULL; |
| 1300 | else |
| 1301 | new->trace_output = NULL; |
Namhyung Kim | aef810e | 2016-02-25 00:13:34 +0900 | [diff] [blame] | 1302 | |
Namhyung Kim | 1b2dbbf | 2016-03-07 16:44:46 -0300 | [diff] [blame] | 1303 | if (perf_hpp__is_srcline_entry(fmt)) |
| 1304 | he->srcline = NULL; |
| 1305 | else |
| 1306 | new->srcline = NULL; |
Namhyung Kim | aef810e | 2016-02-25 00:13:34 +0900 | [diff] [blame] | 1307 | |
Namhyung Kim | 1b2dbbf | 2016-03-07 16:44:46 -0300 | [diff] [blame] | 1308 | if (perf_hpp__is_srcfile_entry(fmt)) |
| 1309 | he->srcfile = NULL; |
| 1310 | else |
| 1311 | new->srcfile = NULL; |
| 1312 | } |
Namhyung Kim | aef810e | 2016-02-25 00:13:34 +0900 | [diff] [blame] | 1313 | |
| 1314 | rb_link_node(&new->rb_node_in, parent, p); |
| 1315 | rb_insert_color(&new->rb_node_in, root); |
| 1316 | return new; |
| 1317 | } |
| 1318 | |
| 1319 | static int hists__hierarchy_insert_entry(struct hists *hists, |
| 1320 | struct rb_root *root, |
| 1321 | struct hist_entry *he) |
| 1322 | { |
Namhyung Kim | 1b2dbbf | 2016-03-07 16:44:46 -0300 | [diff] [blame] | 1323 | struct perf_hpp_list_node *node; |
Namhyung Kim | aef810e | 2016-02-25 00:13:34 +0900 | [diff] [blame] | 1324 | struct hist_entry *new_he = NULL; |
| 1325 | struct hist_entry *parent = NULL; |
| 1326 | int depth = 0; |
| 1327 | int ret = 0; |
| 1328 | |
Namhyung Kim | 1b2dbbf | 2016-03-07 16:44:46 -0300 | [diff] [blame] | 1329 | list_for_each_entry(node, &hists->hpp_formats, list) { |
| 1330 | /* skip period (overhead) and elided columns */ |
| 1331 | if (node->level == 0 || node->skip) |
Namhyung Kim | aef810e | 2016-02-25 00:13:34 +0900 | [diff] [blame] | 1332 | continue; |
| 1333 | |
| 1334 | /* insert copy of 'he' for each fmt into the hierarchy */ |
Namhyung Kim | aec13a7 | 2016-03-09 22:46:58 +0900 | [diff] [blame] | 1335 | new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp); |
Namhyung Kim | aef810e | 2016-02-25 00:13:34 +0900 | [diff] [blame] | 1336 | if (new_he == NULL) { |
| 1337 | ret = -1; |
| 1338 | break; |
| 1339 | } |
| 1340 | |
| 1341 | root = &new_he->hroot_in; |
Namhyung Kim | aef810e | 2016-02-25 00:13:34 +0900 | [diff] [blame] | 1342 | new_he->depth = depth++; |
| 1343 | parent = new_he; |
| 1344 | } |
| 1345 | |
| 1346 | if (new_he) { |
| 1347 | new_he->leaf = true; |
| 1348 | |
| 1349 | if (symbol_conf.use_callchain) { |
| 1350 | callchain_cursor_reset(&callchain_cursor); |
| 1351 | if (callchain_merge(&callchain_cursor, |
| 1352 | new_he->callchain, |
| 1353 | he->callchain) < 0) |
| 1354 | ret = -1; |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | /* 'he' is no longer used */ |
| 1359 | hist_entry__delete(he); |
| 1360 | |
| 1361 | /* return 0 (or -1) since it already applied filters */ |
| 1362 | return ret; |
| 1363 | } |
| 1364 | |
Jiri Olsa | 592dac6 | 2016-03-24 13:52:17 +0100 | [diff] [blame] | 1365 | static int hists__collapse_insert_entry(struct hists *hists, |
| 1366 | struct rb_root *root, |
| 1367 | struct hist_entry *he) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1368 | { |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 1369 | struct rb_node **p = &root->rb_node; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1370 | struct rb_node *parent = NULL; |
| 1371 | struct hist_entry *iter; |
| 1372 | int64_t cmp; |
| 1373 | |
Namhyung Kim | aef810e | 2016-02-25 00:13:34 +0900 | [diff] [blame] | 1374 | if (symbol_conf.report_hierarchy) |
| 1375 | return hists__hierarchy_insert_entry(hists, root, he); |
| 1376 | |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1377 | while (*p != NULL) { |
| 1378 | parent = *p; |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1379 | iter = rb_entry(parent, struct hist_entry, rb_node_in); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1380 | |
| 1381 | cmp = hist_entry__collapse(iter, he); |
| 1382 | |
| 1383 | if (!cmp) { |
Namhyung Kim | bba58cd | 2016-02-16 23:08:25 +0900 | [diff] [blame] | 1384 | int ret = 0; |
| 1385 | |
Namhyung Kim | 139c081 | 2012-10-04 21:49:43 +0900 | [diff] [blame] | 1386 | he_stat__add_stat(&iter->stat, &he->stat); |
Namhyung Kim | f8be1c8 | 2012-09-11 13:15:07 +0900 | [diff] [blame] | 1387 | if (symbol_conf.cumulate_callchain) |
| 1388 | he_stat__add_stat(iter->stat_acc, he->stat_acc); |
Namhyung Kim | 9ec6097 | 2012-09-26 16:47:28 +0900 | [diff] [blame] | 1389 | |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 1390 | if (symbol_conf.use_callchain) { |
Namhyung Kim | 4726064 | 2012-05-31 14:43:26 +0900 | [diff] [blame] | 1391 | callchain_cursor_reset(&callchain_cursor); |
Namhyung Kim | bba58cd | 2016-02-16 23:08:25 +0900 | [diff] [blame] | 1392 | if (callchain_merge(&callchain_cursor, |
| 1393 | iter->callchain, |
| 1394 | he->callchain) < 0) |
| 1395 | ret = -1; |
Frederic Weisbecker | 1b3a0e9 | 2011-01-14 04:51:58 +0100 | [diff] [blame] | 1396 | } |
Arnaldo Carvalho de Melo | 6733d1b | 2014-12-19 12:31:40 -0300 | [diff] [blame] | 1397 | hist_entry__delete(he); |
Namhyung Kim | bba58cd | 2016-02-16 23:08:25 +0900 | [diff] [blame] | 1398 | return ret; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | if (cmp < 0) |
| 1402 | p = &(*p)->rb_left; |
| 1403 | else |
| 1404 | p = &(*p)->rb_right; |
| 1405 | } |
Namhyung Kim | 740b97f | 2014-12-22 13:44:10 +0900 | [diff] [blame] | 1406 | hists->nr_entries++; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1407 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1408 | rb_link_node(&he->rb_node_in, parent, p); |
| 1409 | rb_insert_color(&he->rb_node_in, root); |
Namhyung Kim | bba58cd | 2016-02-16 23:08:25 +0900 | [diff] [blame] | 1410 | return 1; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1411 | } |
| 1412 | |
Namhyung Kim | fc284be | 2016-01-07 10:14:10 +0100 | [diff] [blame] | 1413 | struct rb_root *hists__get_rotate_entries_in(struct hists *hists) |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1414 | { |
| 1415 | struct rb_root *root; |
| 1416 | |
| 1417 | pthread_mutex_lock(&hists->lock); |
| 1418 | |
| 1419 | root = hists->entries_in; |
| 1420 | if (++hists->entries_in > &hists->entries_in_array[1]) |
| 1421 | hists->entries_in = &hists->entries_in_array[0]; |
| 1422 | |
| 1423 | pthread_mutex_unlock(&hists->lock); |
| 1424 | |
| 1425 | return root; |
| 1426 | } |
| 1427 | |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 1428 | static void hists__apply_filters(struct hists *hists, struct hist_entry *he) |
| 1429 | { |
| 1430 | hists__filter_entry_by_dso(hists, he); |
| 1431 | hists__filter_entry_by_thread(hists, he); |
Namhyung Kim | e94d53e | 2012-03-16 17:50:51 +0900 | [diff] [blame] | 1432 | hists__filter_entry_by_symbol(hists, he); |
Kan Liang | 21394d9 | 2015-09-04 10:45:44 -0400 | [diff] [blame] | 1433 | hists__filter_entry_by_socket(hists, he); |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 1434 | } |
| 1435 | |
Namhyung Kim | bba58cd | 2016-02-16 23:08:25 +0900 | [diff] [blame] | 1436 | int hists__collapse_resort(struct hists *hists, struct ui_progress *prog) |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1437 | { |
| 1438 | struct rb_root *root; |
| 1439 | struct rb_node *next; |
| 1440 | struct hist_entry *n; |
Namhyung Kim | bba58cd | 2016-02-16 23:08:25 +0900 | [diff] [blame] | 1441 | int ret; |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1442 | |
Jiri Olsa | 5222503 | 2016-05-03 13:54:42 +0200 | [diff] [blame] | 1443 | if (!hists__has(hists, need_collapse)) |
Namhyung Kim | bba58cd | 2016-02-16 23:08:25 +0900 | [diff] [blame] | 1444 | return 0; |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1445 | |
Namhyung Kim | 740b97f | 2014-12-22 13:44:10 +0900 | [diff] [blame] | 1446 | hists->nr_entries = 0; |
| 1447 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1448 | root = hists__get_rotate_entries_in(hists); |
Namhyung Kim | 740b97f | 2014-12-22 13:44:10 +0900 | [diff] [blame] | 1449 | |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1450 | next = rb_first(root); |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1451 | |
| 1452 | while (next) { |
Arnaldo Carvalho de Melo | 33e940a | 2013-09-17 16:34:28 -0300 | [diff] [blame] | 1453 | if (session_done()) |
| 1454 | break; |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1455 | n = rb_entry(next, struct hist_entry, rb_node_in); |
| 1456 | next = rb_next(&n->rb_node_in); |
| 1457 | |
| 1458 | rb_erase(&n->rb_node_in, root); |
Namhyung Kim | bba58cd | 2016-02-16 23:08:25 +0900 | [diff] [blame] | 1459 | ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n); |
| 1460 | if (ret < 0) |
| 1461 | return -1; |
| 1462 | |
| 1463 | if (ret) { |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 1464 | /* |
| 1465 | * If it wasn't combined with one of the entries already |
| 1466 | * collapsed, we need to apply the filters that may have |
| 1467 | * been set by, say, the hist_browser. |
| 1468 | */ |
| 1469 | hists__apply_filters(hists, n); |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 1470 | } |
Namhyung Kim | c1fb565 | 2013-10-11 14:15:38 +0900 | [diff] [blame] | 1471 | if (prog) |
| 1472 | ui_progress__update(prog, 1); |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1473 | } |
Namhyung Kim | bba58cd | 2016-02-16 23:08:25 +0900 | [diff] [blame] | 1474 | return 0; |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1475 | } |
| 1476 | |
Namhyung Kim | 043ca389 | 2014-03-03 14:18:00 +0900 | [diff] [blame] | 1477 | 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] | 1478 | { |
Jiri Olsa | aa6f50a | 2016-01-18 10:24:24 +0100 | [diff] [blame] | 1479 | struct hists *hists = a->hists; |
Namhyung Kim | 043ca389 | 2014-03-03 14:18:00 +0900 | [diff] [blame] | 1480 | struct perf_hpp_fmt *fmt; |
| 1481 | int64_t cmp = 0; |
Namhyung Kim | 29d720e | 2013-01-22 18:09:33 +0900 | [diff] [blame] | 1482 | |
Jiri Olsa | aa6f50a | 2016-01-18 10:24:24 +0100 | [diff] [blame] | 1483 | hists__for_each_sort_list(hists, fmt) { |
Namhyung Kim | 361459f | 2015-12-23 02:07:08 +0900 | [diff] [blame] | 1484 | if (perf_hpp__should_skip(fmt, a->hists)) |
Namhyung Kim | e67d49a | 2014-03-18 13:00:59 +0900 | [diff] [blame] | 1485 | continue; |
| 1486 | |
Namhyung Kim | 87bbdf7 | 2015-01-08 09:45:46 +0900 | [diff] [blame] | 1487 | cmp = fmt->sort(fmt, a, b); |
Namhyung Kim | 043ca389 | 2014-03-03 14:18:00 +0900 | [diff] [blame] | 1488 | if (cmp) |
Namhyung Kim | 29d720e | 2013-01-22 18:09:33 +0900 | [diff] [blame] | 1489 | break; |
| 1490 | } |
| 1491 | |
Namhyung Kim | 043ca389 | 2014-03-03 14:18:00 +0900 | [diff] [blame] | 1492 | return cmp; |
Namhyung Kim | 29d720e | 2013-01-22 18:09:33 +0900 | [diff] [blame] | 1493 | } |
| 1494 | |
Namhyung Kim | 9283ba9 | 2014-04-24 16:37:26 +0900 | [diff] [blame] | 1495 | static void hists__reset_filter_stats(struct hists *hists) |
| 1496 | { |
| 1497 | hists->nr_non_filtered_entries = 0; |
| 1498 | hists->stats.total_non_filtered_period = 0; |
| 1499 | } |
| 1500 | |
| 1501 | void hists__reset_stats(struct hists *hists) |
| 1502 | { |
| 1503 | hists->nr_entries = 0; |
| 1504 | hists->stats.total_period = 0; |
| 1505 | |
| 1506 | hists__reset_filter_stats(hists); |
| 1507 | } |
| 1508 | |
| 1509 | static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h) |
| 1510 | { |
| 1511 | hists->nr_non_filtered_entries++; |
| 1512 | hists->stats.total_non_filtered_period += h->stat.period; |
| 1513 | } |
| 1514 | |
| 1515 | void hists__inc_stats(struct hists *hists, struct hist_entry *h) |
| 1516 | { |
| 1517 | if (!h->filtered) |
| 1518 | hists__inc_filter_stats(hists, h); |
| 1519 | |
| 1520 | hists->nr_entries++; |
| 1521 | hists->stats.total_period += h->stat.period; |
| 1522 | } |
| 1523 | |
Namhyung Kim | f7fb538 | 2016-03-09 22:47:02 +0900 | [diff] [blame] | 1524 | static void hierarchy_recalc_total_periods(struct hists *hists) |
| 1525 | { |
| 1526 | struct rb_node *node; |
| 1527 | struct hist_entry *he; |
| 1528 | |
| 1529 | node = rb_first(&hists->entries); |
| 1530 | |
| 1531 | hists->stats.total_period = 0; |
| 1532 | hists->stats.total_non_filtered_period = 0; |
| 1533 | |
| 1534 | /* |
| 1535 | * recalculate total period using top-level entries only |
| 1536 | * since lower level entries only see non-filtered entries |
| 1537 | * but upper level entries have sum of both entries. |
| 1538 | */ |
| 1539 | while (node) { |
| 1540 | he = rb_entry(node, struct hist_entry, rb_node); |
| 1541 | node = rb_next(node); |
| 1542 | |
| 1543 | hists->stats.total_period += he->stat.period; |
| 1544 | if (!he->filtered) |
| 1545 | hists->stats.total_non_filtered_period += he->stat.period; |
| 1546 | } |
| 1547 | } |
| 1548 | |
Namhyung Kim | 1a3906a | 2016-02-25 00:13:35 +0900 | [diff] [blame] | 1549 | static void hierarchy_insert_output_entry(struct rb_root *root, |
| 1550 | struct hist_entry *he) |
| 1551 | { |
| 1552 | struct rb_node **p = &root->rb_node; |
| 1553 | struct rb_node *parent = NULL; |
| 1554 | struct hist_entry *iter; |
Namhyung Kim | 1b2dbbf | 2016-03-07 16:44:46 -0300 | [diff] [blame] | 1555 | struct perf_hpp_fmt *fmt; |
Namhyung Kim | 1a3906a | 2016-02-25 00:13:35 +0900 | [diff] [blame] | 1556 | |
| 1557 | while (*p != NULL) { |
| 1558 | parent = *p; |
| 1559 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 1560 | |
| 1561 | if (hist_entry__sort(he, iter) > 0) |
| 1562 | p = &parent->rb_left; |
| 1563 | else |
| 1564 | p = &parent->rb_right; |
| 1565 | } |
| 1566 | |
| 1567 | rb_link_node(&he->rb_node, parent, p); |
| 1568 | rb_insert_color(&he->rb_node, root); |
Namhyung Kim | abab5e7 | 2016-02-27 03:52:47 +0900 | [diff] [blame] | 1569 | |
| 1570 | /* update column width of dynamic entry */ |
Namhyung Kim | 1b2dbbf | 2016-03-07 16:44:46 -0300 | [diff] [blame] | 1571 | perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) { |
| 1572 | if (perf_hpp__is_dynamic_entry(fmt)) |
| 1573 | fmt->sort(fmt, he, NULL); |
| 1574 | } |
Namhyung Kim | 1a3906a | 2016-02-25 00:13:35 +0900 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | static void hists__hierarchy_output_resort(struct hists *hists, |
| 1578 | struct ui_progress *prog, |
| 1579 | struct rb_root *root_in, |
| 1580 | struct rb_root *root_out, |
| 1581 | u64 min_callchain_hits, |
| 1582 | bool use_callchain) |
| 1583 | { |
| 1584 | struct rb_node *node; |
| 1585 | struct hist_entry *he; |
| 1586 | |
| 1587 | *root_out = RB_ROOT; |
| 1588 | node = rb_first(root_in); |
| 1589 | |
| 1590 | while (node) { |
| 1591 | he = rb_entry(node, struct hist_entry, rb_node_in); |
| 1592 | node = rb_next(node); |
| 1593 | |
| 1594 | hierarchy_insert_output_entry(root_out, he); |
| 1595 | |
| 1596 | if (prog) |
| 1597 | ui_progress__update(prog, 1); |
| 1598 | |
| 1599 | if (!he->leaf) { |
| 1600 | hists__hierarchy_output_resort(hists, prog, |
| 1601 | &he->hroot_in, |
| 1602 | &he->hroot_out, |
| 1603 | min_callchain_hits, |
| 1604 | use_callchain); |
| 1605 | hists->nr_entries++; |
| 1606 | if (!he->filtered) { |
| 1607 | hists->nr_non_filtered_entries++; |
| 1608 | hists__calc_col_len(hists, he); |
| 1609 | } |
| 1610 | |
| 1611 | continue; |
| 1612 | } |
| 1613 | |
Namhyung Kim | 1a3906a | 2016-02-25 00:13:35 +0900 | [diff] [blame] | 1614 | if (!use_callchain) |
| 1615 | continue; |
| 1616 | |
| 1617 | if (callchain_param.mode == CHAIN_GRAPH_REL) { |
| 1618 | u64 total = he->stat.period; |
| 1619 | |
| 1620 | if (symbol_conf.cumulate_callchain) |
| 1621 | total = he->stat_acc->period; |
| 1622 | |
| 1623 | min_callchain_hits = total * (callchain_param.min_percent / 100); |
| 1624 | } |
| 1625 | |
| 1626 | callchain_param.sort(&he->sorted_chain, he->callchain, |
| 1627 | min_callchain_hits, &callchain_param); |
| 1628 | } |
| 1629 | } |
| 1630 | |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 1631 | static void __hists__insert_output_entry(struct rb_root *entries, |
| 1632 | struct hist_entry *he, |
Kan Liang | f9db0d0 | 2015-08-11 06:30:48 -0400 | [diff] [blame] | 1633 | u64 min_callchain_hits, |
| 1634 | bool use_callchain) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1635 | { |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 1636 | struct rb_node **p = &entries->rb_node; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1637 | struct rb_node *parent = NULL; |
| 1638 | struct hist_entry *iter; |
Namhyung Kim | abab5e7 | 2016-02-27 03:52:47 +0900 | [diff] [blame] | 1639 | struct perf_hpp_fmt *fmt; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1640 | |
Namhyung Kim | 744070e | 2016-01-28 00:40:48 +0900 | [diff] [blame] | 1641 | if (use_callchain) { |
| 1642 | if (callchain_param.mode == CHAIN_GRAPH_REL) { |
| 1643 | u64 total = he->stat.period; |
| 1644 | |
| 1645 | if (symbol_conf.cumulate_callchain) |
| 1646 | total = he->stat_acc->period; |
| 1647 | |
| 1648 | min_callchain_hits = total * (callchain_param.min_percent / 100); |
| 1649 | } |
Arnaldo Carvalho de Melo | b9fb930 | 2010-04-02 09:50:42 -0300 | [diff] [blame] | 1650 | callchain_param.sort(&he->sorted_chain, he->callchain, |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1651 | min_callchain_hits, &callchain_param); |
Namhyung Kim | 744070e | 2016-01-28 00:40:48 +0900 | [diff] [blame] | 1652 | } |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1653 | |
| 1654 | while (*p != NULL) { |
| 1655 | parent = *p; |
| 1656 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 1657 | |
Namhyung Kim | 043ca389 | 2014-03-03 14:18:00 +0900 | [diff] [blame] | 1658 | if (hist_entry__sort(he, iter) > 0) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1659 | p = &(*p)->rb_left; |
| 1660 | else |
| 1661 | p = &(*p)->rb_right; |
| 1662 | } |
| 1663 | |
| 1664 | rb_link_node(&he->rb_node, parent, p); |
Arnaldo Carvalho de Melo | 1c02c4d | 2010-05-10 13:04:11 -0300 | [diff] [blame] | 1665 | rb_insert_color(&he->rb_node, entries); |
Namhyung Kim | abab5e7 | 2016-02-27 03:52:47 +0900 | [diff] [blame] | 1666 | |
| 1667 | perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) { |
| 1668 | if (perf_hpp__is_dynamic_entry(fmt) && |
| 1669 | perf_hpp__defined_dynamic_entry(fmt, he->hists)) |
| 1670 | fmt->sort(fmt, he, NULL); /* update column width */ |
| 1671 | } |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1672 | } |
| 1673 | |
Jiri Olsa | 01441af | 2016-01-18 10:23:59 +0100 | [diff] [blame] | 1674 | static void output_resort(struct hists *hists, struct ui_progress *prog, |
Jiri Olsa | 52c5cc3 | 2016-08-01 20:02:34 +0200 | [diff] [blame^] | 1675 | bool use_callchain, hists__resort_cb_t cb) |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1676 | { |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1677 | struct rb_root *root; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1678 | struct rb_node *next; |
| 1679 | struct hist_entry *n; |
Namhyung Kim | 467ef10 | 2016-02-16 23:08:19 +0900 | [diff] [blame] | 1680 | u64 callchain_total; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1681 | u64 min_callchain_hits; |
| 1682 | |
Namhyung Kim | 467ef10 | 2016-02-16 23:08:19 +0900 | [diff] [blame] | 1683 | callchain_total = hists->callchain_period; |
| 1684 | if (symbol_conf.filter_relative) |
| 1685 | callchain_total = hists->callchain_non_filtered_period; |
| 1686 | |
| 1687 | min_callchain_hits = callchain_total * (callchain_param.min_percent / 100); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1688 | |
Namhyung Kim | 1a3906a | 2016-02-25 00:13:35 +0900 | [diff] [blame] | 1689 | hists__reset_stats(hists); |
| 1690 | hists__reset_col_len(hists); |
| 1691 | |
| 1692 | if (symbol_conf.report_hierarchy) { |
Namhyung Kim | f7fb538 | 2016-03-09 22:47:02 +0900 | [diff] [blame] | 1693 | hists__hierarchy_output_resort(hists, prog, |
| 1694 | &hists->entries_collapsed, |
| 1695 | &hists->entries, |
| 1696 | min_callchain_hits, |
| 1697 | use_callchain); |
| 1698 | hierarchy_recalc_total_periods(hists); |
| 1699 | return; |
Namhyung Kim | 1a3906a | 2016-02-25 00:13:35 +0900 | [diff] [blame] | 1700 | } |
| 1701 | |
Jiri Olsa | 5222503 | 2016-05-03 13:54:42 +0200 | [diff] [blame] | 1702 | if (hists__has(hists, need_collapse)) |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1703 | root = &hists->entries_collapsed; |
| 1704 | else |
| 1705 | root = hists->entries_in; |
| 1706 | |
| 1707 | next = rb_first(root); |
| 1708 | hists->entries = RB_ROOT; |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1709 | |
| 1710 | while (next) { |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1711 | n = rb_entry(next, struct hist_entry, rb_node_in); |
| 1712 | next = rb_next(&n->rb_node_in); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1713 | |
Jiri Olsa | 52c5cc3 | 2016-08-01 20:02:34 +0200 | [diff] [blame^] | 1714 | if (cb && cb(n)) |
| 1715 | continue; |
| 1716 | |
Kan Liang | f9db0d0 | 2015-08-11 06:30:48 -0400 | [diff] [blame] | 1717 | __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain); |
Namhyung Kim | 6263835 | 2014-04-24 16:21:46 +0900 | [diff] [blame] | 1718 | hists__inc_stats(hists, n); |
Namhyung Kim | ae993ef | 2014-04-24 16:25:19 +0900 | [diff] [blame] | 1719 | |
| 1720 | if (!n->filtered) |
| 1721 | hists__calc_col_len(hists, n); |
Namhyung Kim | 740b97f | 2014-12-22 13:44:10 +0900 | [diff] [blame] | 1722 | |
| 1723 | if (prog) |
| 1724 | ui_progress__update(prog, 1); |
John Kacur | 3d1d07e | 2009-09-28 15:32:55 +0200 | [diff] [blame] | 1725 | } |
Arnaldo Carvalho de Melo | 1980c2eb | 2011-10-05 17:50:23 -0300 | [diff] [blame] | 1726 | } |
Arnaldo Carvalho de Melo | b9bf089 | 2009-12-14 11:37:11 -0200 | [diff] [blame] | 1727 | |
Jiri Olsa | 452ce03 | 2016-01-18 10:24:00 +0100 | [diff] [blame] | 1728 | void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog) |
Jiri Olsa | 01441af | 2016-01-18 10:23:59 +0100 | [diff] [blame] | 1729 | { |
Jiri Olsa | 01441af | 2016-01-18 10:23:59 +0100 | [diff] [blame] | 1730 | bool use_callchain; |
| 1731 | |
| 1732 | if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph) |
| 1733 | use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN; |
| 1734 | else |
| 1735 | use_callchain = symbol_conf.use_callchain; |
| 1736 | |
Jiri Olsa | 52c5cc3 | 2016-08-01 20:02:34 +0200 | [diff] [blame^] | 1737 | output_resort(evsel__hists(evsel), prog, use_callchain, NULL); |
Jiri Olsa | 452ce03 | 2016-01-18 10:24:00 +0100 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | void hists__output_resort(struct hists *hists, struct ui_progress *prog) |
| 1741 | { |
Jiri Olsa | 52c5cc3 | 2016-08-01 20:02:34 +0200 | [diff] [blame^] | 1742 | output_resort(hists, prog, symbol_conf.use_callchain, NULL); |
| 1743 | } |
| 1744 | |
| 1745 | void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog, |
| 1746 | hists__resort_cb_t cb) |
| 1747 | { |
| 1748 | output_resort(hists, prog, symbol_conf.use_callchain, cb); |
Jiri Olsa | 01441af | 2016-01-18 10:23:59 +0100 | [diff] [blame] | 1749 | } |
| 1750 | |
Namhyung Kim | 8c01872 | 2016-02-25 00:13:36 +0900 | [diff] [blame] | 1751 | static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd) |
| 1752 | { |
| 1753 | if (he->leaf || hmd == HMD_FORCE_SIBLING) |
| 1754 | return false; |
| 1755 | |
| 1756 | if (he->unfolded || hmd == HMD_FORCE_CHILD) |
| 1757 | return true; |
| 1758 | |
| 1759 | return false; |
| 1760 | } |
| 1761 | |
| 1762 | struct rb_node *rb_hierarchy_last(struct rb_node *node) |
| 1763 | { |
| 1764 | struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node); |
| 1765 | |
| 1766 | while (can_goto_child(he, HMD_NORMAL)) { |
| 1767 | node = rb_last(&he->hroot_out); |
| 1768 | he = rb_entry(node, struct hist_entry, rb_node); |
| 1769 | } |
| 1770 | return node; |
| 1771 | } |
| 1772 | |
| 1773 | struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd) |
| 1774 | { |
| 1775 | struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node); |
| 1776 | |
| 1777 | if (can_goto_child(he, hmd)) |
| 1778 | node = rb_first(&he->hroot_out); |
| 1779 | else |
| 1780 | node = rb_next(node); |
| 1781 | |
| 1782 | while (node == NULL) { |
| 1783 | he = he->parent_he; |
| 1784 | if (he == NULL) |
| 1785 | break; |
| 1786 | |
| 1787 | node = rb_next(&he->rb_node); |
| 1788 | } |
| 1789 | return node; |
| 1790 | } |
| 1791 | |
| 1792 | struct rb_node *rb_hierarchy_prev(struct rb_node *node) |
| 1793 | { |
| 1794 | struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node); |
| 1795 | |
| 1796 | node = rb_prev(node); |
| 1797 | if (node) |
| 1798 | return rb_hierarchy_last(node); |
| 1799 | |
| 1800 | he = he->parent_he; |
| 1801 | if (he == NULL) |
| 1802 | return NULL; |
| 1803 | |
| 1804 | return &he->rb_node; |
| 1805 | } |
| 1806 | |
Namhyung Kim | a7b5895 | 2016-02-26 21:13:16 +0900 | [diff] [blame] | 1807 | bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit) |
| 1808 | { |
| 1809 | struct rb_node *node; |
| 1810 | struct hist_entry *child; |
| 1811 | float percent; |
| 1812 | |
| 1813 | if (he->leaf) |
| 1814 | return false; |
| 1815 | |
| 1816 | node = rb_first(&he->hroot_out); |
| 1817 | child = rb_entry(node, struct hist_entry, rb_node); |
| 1818 | |
| 1819 | while (node && child->filtered) { |
| 1820 | node = rb_next(node); |
| 1821 | child = rb_entry(node, struct hist_entry, rb_node); |
| 1822 | } |
| 1823 | |
| 1824 | if (node) |
| 1825 | percent = hist_entry__get_percent_limit(child); |
| 1826 | else |
| 1827 | percent = 0; |
| 1828 | |
| 1829 | return node && percent >= limit; |
| 1830 | } |
| 1831 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1832 | 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] | 1833 | enum hist_filter filter) |
| 1834 | { |
| 1835 | h->filtered &= ~(1 << filter); |
Namhyung Kim | 155e9af | 2016-02-25 00:13:38 +0900 | [diff] [blame] | 1836 | |
| 1837 | if (symbol_conf.report_hierarchy) { |
| 1838 | struct hist_entry *parent = h->parent_he; |
| 1839 | |
| 1840 | while (parent) { |
| 1841 | he_stat__add_stat(&parent->stat, &h->stat); |
| 1842 | |
| 1843 | parent->filtered &= ~(1 << filter); |
| 1844 | |
| 1845 | if (parent->filtered) |
| 1846 | goto next; |
| 1847 | |
| 1848 | /* force fold unfiltered entry for simplicity */ |
| 1849 | parent->unfolded = false; |
Namhyung Kim | 79dded8 | 2016-02-26 21:13:19 +0900 | [diff] [blame] | 1850 | parent->has_no_entry = false; |
Namhyung Kim | 155e9af | 2016-02-25 00:13:38 +0900 | [diff] [blame] | 1851 | parent->row_offset = 0; |
| 1852 | parent->nr_rows = 0; |
| 1853 | next: |
| 1854 | parent = parent->parent_he; |
| 1855 | } |
| 1856 | } |
| 1857 | |
Arnaldo Carvalho de Melo | cc5edb0 | 2010-07-16 12:35:07 -0300 | [diff] [blame] | 1858 | if (h->filtered) |
| 1859 | return; |
| 1860 | |
Namhyung Kim | 87e90f4 | 2014-04-24 16:44:16 +0900 | [diff] [blame] | 1861 | /* force fold unfiltered entry for simplicity */ |
Namhyung Kim | 3698dab | 2015-05-05 23:55:46 +0900 | [diff] [blame] | 1862 | h->unfolded = false; |
Namhyung Kim | 79dded8 | 2016-02-26 21:13:19 +0900 | [diff] [blame] | 1863 | h->has_no_entry = false; |
Arnaldo Carvalho de Melo | 0f0cbf7 | 2010-07-26 17:13:40 -0300 | [diff] [blame] | 1864 | h->row_offset = 0; |
He Kuang | a8cd1f4 | 2015-03-11 20:36:03 +0800 | [diff] [blame] | 1865 | h->nr_rows = 0; |
Namhyung Kim | 9283ba9 | 2014-04-24 16:37:26 +0900 | [diff] [blame] | 1866 | |
Namhyung Kim | 1ab1fa5 | 2013-12-26 15:11:52 +0900 | [diff] [blame] | 1867 | hists->stats.nr_non_filtered_samples += h->stat.nr_events; |
Arnaldo Carvalho de Melo | cc5edb0 | 2010-07-16 12:35:07 -0300 | [diff] [blame] | 1868 | |
Namhyung Kim | 9283ba9 | 2014-04-24 16:37:26 +0900 | [diff] [blame] | 1869 | hists__inc_filter_stats(hists, h); |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 1870 | hists__calc_col_len(hists, h); |
Arnaldo Carvalho de Melo | cc5edb0 | 2010-07-16 12:35:07 -0300 | [diff] [blame] | 1871 | } |
| 1872 | |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 1873 | |
| 1874 | static bool hists__filter_entry_by_dso(struct hists *hists, |
| 1875 | struct hist_entry *he) |
| 1876 | { |
| 1877 | if (hists->dso_filter != NULL && |
| 1878 | (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) { |
| 1879 | he->filtered |= (1 << HIST_FILTER__DSO); |
| 1880 | return true; |
| 1881 | } |
| 1882 | |
| 1883 | return false; |
| 1884 | } |
| 1885 | |
Arnaldo Carvalho de Melo | 90cf1fb | 2011-10-19 13:09:10 -0200 | [diff] [blame] | 1886 | static bool hists__filter_entry_by_thread(struct hists *hists, |
| 1887 | struct hist_entry *he) |
| 1888 | { |
| 1889 | if (hists->thread_filter != NULL && |
| 1890 | he->thread != hists->thread_filter) { |
| 1891 | he->filtered |= (1 << HIST_FILTER__THREAD); |
| 1892 | return true; |
| 1893 | } |
| 1894 | |
| 1895 | return false; |
| 1896 | } |
| 1897 | |
Namhyung Kim | e94d53e | 2012-03-16 17:50:51 +0900 | [diff] [blame] | 1898 | static bool hists__filter_entry_by_symbol(struct hists *hists, |
| 1899 | struct hist_entry *he) |
| 1900 | { |
| 1901 | if (hists->symbol_filter_str != NULL && |
| 1902 | (!he->ms.sym || strstr(he->ms.sym->name, |
| 1903 | hists->symbol_filter_str) == NULL)) { |
| 1904 | he->filtered |= (1 << HIST_FILTER__SYMBOL); |
| 1905 | return true; |
| 1906 | } |
| 1907 | |
| 1908 | return false; |
| 1909 | } |
| 1910 | |
Kan Liang | 21394d9 | 2015-09-04 10:45:44 -0400 | [diff] [blame] | 1911 | static bool hists__filter_entry_by_socket(struct hists *hists, |
| 1912 | struct hist_entry *he) |
| 1913 | { |
| 1914 | if ((hists->socket_filter > -1) && |
| 1915 | (he->socket != hists->socket_filter)) { |
| 1916 | he->filtered |= (1 << HIST_FILTER__SOCKET); |
| 1917 | return true; |
| 1918 | } |
| 1919 | |
| 1920 | return false; |
| 1921 | } |
| 1922 | |
Namhyung Kim | 1f7c254 | 2016-01-20 10:15:21 +0900 | [diff] [blame] | 1923 | typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he); |
| 1924 | |
| 1925 | static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter) |
Kan Liang | 84734b0 | 2015-09-04 10:45:45 -0400 | [diff] [blame] | 1926 | { |
| 1927 | struct rb_node *nd; |
| 1928 | |
| 1929 | hists->stats.nr_non_filtered_samples = 0; |
| 1930 | |
| 1931 | hists__reset_filter_stats(hists); |
| 1932 | hists__reset_col_len(hists); |
| 1933 | |
| 1934 | for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) { |
| 1935 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 1936 | |
Namhyung Kim | 1f7c254 | 2016-01-20 10:15:21 +0900 | [diff] [blame] | 1937 | if (filter(hists, h)) |
Kan Liang | 84734b0 | 2015-09-04 10:45:45 -0400 | [diff] [blame] | 1938 | continue; |
| 1939 | |
Namhyung Kim | 1f7c254 | 2016-01-20 10:15:21 +0900 | [diff] [blame] | 1940 | hists__remove_entry_filter(hists, h, type); |
Kan Liang | 84734b0 | 2015-09-04 10:45:45 -0400 | [diff] [blame] | 1941 | } |
| 1942 | } |
| 1943 | |
Namhyung Kim | 7064285 | 2016-02-25 00:13:39 +0900 | [diff] [blame] | 1944 | static void resort_filtered_entry(struct rb_root *root, struct hist_entry *he) |
| 1945 | { |
| 1946 | struct rb_node **p = &root->rb_node; |
| 1947 | struct rb_node *parent = NULL; |
| 1948 | struct hist_entry *iter; |
| 1949 | struct rb_root new_root = RB_ROOT; |
| 1950 | struct rb_node *nd; |
| 1951 | |
| 1952 | while (*p != NULL) { |
| 1953 | parent = *p; |
| 1954 | iter = rb_entry(parent, struct hist_entry, rb_node); |
| 1955 | |
| 1956 | if (hist_entry__sort(he, iter) > 0) |
| 1957 | p = &(*p)->rb_left; |
| 1958 | else |
| 1959 | p = &(*p)->rb_right; |
| 1960 | } |
| 1961 | |
| 1962 | rb_link_node(&he->rb_node, parent, p); |
| 1963 | rb_insert_color(&he->rb_node, root); |
| 1964 | |
| 1965 | if (he->leaf || he->filtered) |
| 1966 | return; |
| 1967 | |
| 1968 | nd = rb_first(&he->hroot_out); |
| 1969 | while (nd) { |
| 1970 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 1971 | |
| 1972 | nd = rb_next(nd); |
| 1973 | rb_erase(&h->rb_node, &he->hroot_out); |
| 1974 | |
| 1975 | resort_filtered_entry(&new_root, h); |
| 1976 | } |
| 1977 | |
| 1978 | he->hroot_out = new_root; |
| 1979 | } |
| 1980 | |
Namhyung Kim | 155e9af | 2016-02-25 00:13:38 +0900 | [diff] [blame] | 1981 | static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg) |
| 1982 | { |
| 1983 | struct rb_node *nd; |
Namhyung Kim | 7064285 | 2016-02-25 00:13:39 +0900 | [diff] [blame] | 1984 | struct rb_root new_root = RB_ROOT; |
Namhyung Kim | 155e9af | 2016-02-25 00:13:38 +0900 | [diff] [blame] | 1985 | |
| 1986 | hists->stats.nr_non_filtered_samples = 0; |
| 1987 | |
| 1988 | hists__reset_filter_stats(hists); |
| 1989 | hists__reset_col_len(hists); |
| 1990 | |
| 1991 | nd = rb_first(&hists->entries); |
| 1992 | while (nd) { |
| 1993 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 1994 | int ret; |
| 1995 | |
| 1996 | ret = hist_entry__filter(h, type, arg); |
| 1997 | |
| 1998 | /* |
| 1999 | * case 1. non-matching type |
| 2000 | * zero out the period, set filter marker and move to child |
| 2001 | */ |
| 2002 | if (ret < 0) { |
| 2003 | memset(&h->stat, 0, sizeof(h->stat)); |
| 2004 | h->filtered |= (1 << type); |
| 2005 | |
| 2006 | nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD); |
| 2007 | } |
| 2008 | /* |
| 2009 | * case 2. matched type (filter out) |
| 2010 | * set filter marker and move to next |
| 2011 | */ |
| 2012 | else if (ret == 1) { |
| 2013 | h->filtered |= (1 << type); |
| 2014 | |
| 2015 | nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING); |
| 2016 | } |
| 2017 | /* |
| 2018 | * case 3. ok (not filtered) |
| 2019 | * add period to hists and parents, erase the filter marker |
| 2020 | * and move to next sibling |
| 2021 | */ |
| 2022 | else { |
| 2023 | hists__remove_entry_filter(hists, h, type); |
| 2024 | |
| 2025 | nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING); |
| 2026 | } |
| 2027 | } |
Namhyung Kim | 7064285 | 2016-02-25 00:13:39 +0900 | [diff] [blame] | 2028 | |
Namhyung Kim | f7fb538 | 2016-03-09 22:47:02 +0900 | [diff] [blame] | 2029 | hierarchy_recalc_total_periods(hists); |
| 2030 | |
Namhyung Kim | 7064285 | 2016-02-25 00:13:39 +0900 | [diff] [blame] | 2031 | /* |
| 2032 | * resort output after applying a new filter since filter in a lower |
| 2033 | * hierarchy can change periods in a upper hierarchy. |
| 2034 | */ |
| 2035 | nd = rb_first(&hists->entries); |
| 2036 | while (nd) { |
| 2037 | struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node); |
| 2038 | |
| 2039 | nd = rb_next(nd); |
| 2040 | rb_erase(&h->rb_node, &hists->entries); |
| 2041 | |
| 2042 | resort_filtered_entry(&new_root, h); |
| 2043 | } |
| 2044 | |
| 2045 | hists->entries = new_root; |
Namhyung Kim | 155e9af | 2016-02-25 00:13:38 +0900 | [diff] [blame] | 2046 | } |
| 2047 | |
Namhyung Kim | 1f7c254 | 2016-01-20 10:15:21 +0900 | [diff] [blame] | 2048 | void hists__filter_by_thread(struct hists *hists) |
| 2049 | { |
Namhyung Kim | 155e9af | 2016-02-25 00:13:38 +0900 | [diff] [blame] | 2050 | if (symbol_conf.report_hierarchy) |
| 2051 | hists__filter_hierarchy(hists, HIST_FILTER__THREAD, |
| 2052 | hists->thread_filter); |
| 2053 | else |
| 2054 | hists__filter_by_type(hists, HIST_FILTER__THREAD, |
| 2055 | hists__filter_entry_by_thread); |
Namhyung Kim | 1f7c254 | 2016-01-20 10:15:21 +0900 | [diff] [blame] | 2056 | } |
| 2057 | |
| 2058 | void hists__filter_by_dso(struct hists *hists) |
| 2059 | { |
Namhyung Kim | 155e9af | 2016-02-25 00:13:38 +0900 | [diff] [blame] | 2060 | if (symbol_conf.report_hierarchy) |
| 2061 | hists__filter_hierarchy(hists, HIST_FILTER__DSO, |
| 2062 | hists->dso_filter); |
| 2063 | else |
| 2064 | hists__filter_by_type(hists, HIST_FILTER__DSO, |
| 2065 | hists__filter_entry_by_dso); |
Namhyung Kim | 1f7c254 | 2016-01-20 10:15:21 +0900 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | void hists__filter_by_symbol(struct hists *hists) |
| 2069 | { |
Namhyung Kim | 155e9af | 2016-02-25 00:13:38 +0900 | [diff] [blame] | 2070 | if (symbol_conf.report_hierarchy) |
| 2071 | hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL, |
| 2072 | hists->symbol_filter_str); |
| 2073 | else |
| 2074 | hists__filter_by_type(hists, HIST_FILTER__SYMBOL, |
| 2075 | hists__filter_entry_by_symbol); |
Namhyung Kim | 1f7c254 | 2016-01-20 10:15:21 +0900 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | void hists__filter_by_socket(struct hists *hists) |
| 2079 | { |
Namhyung Kim | 155e9af | 2016-02-25 00:13:38 +0900 | [diff] [blame] | 2080 | if (symbol_conf.report_hierarchy) |
| 2081 | hists__filter_hierarchy(hists, HIST_FILTER__SOCKET, |
| 2082 | &hists->socket_filter); |
| 2083 | else |
| 2084 | hists__filter_by_type(hists, HIST_FILTER__SOCKET, |
| 2085 | hists__filter_entry_by_socket); |
Namhyung Kim | 1f7c254 | 2016-01-20 10:15:21 +0900 | [diff] [blame] | 2086 | } |
| 2087 | |
Arnaldo Carvalho de Melo | 28a6b6a | 2012-12-18 16:24:46 -0300 | [diff] [blame] | 2088 | void events_stats__inc(struct events_stats *stats, u32 type) |
| 2089 | { |
| 2090 | ++stats->nr_events[0]; |
| 2091 | ++stats->nr_events[type]; |
| 2092 | } |
| 2093 | |
Arnaldo Carvalho de Melo | 42b28ac | 2011-09-26 12:33:28 -0300 | [diff] [blame] | 2094 | void hists__inc_nr_events(struct hists *hists, u32 type) |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 2095 | { |
Arnaldo Carvalho de Melo | 28a6b6a | 2012-12-18 16:24:46 -0300 | [diff] [blame] | 2096 | events_stats__inc(&hists->stats, type); |
Arnaldo Carvalho de Melo | c8446b9 | 2010-05-14 10:36:42 -0300 | [diff] [blame] | 2097 | } |
Arnaldo Carvalho de Melo | 95529be | 2012-11-08 17:54:33 -0300 | [diff] [blame] | 2098 | |
Namhyung Kim | 1844dbc | 2014-05-28 14:12:18 +0900 | [diff] [blame] | 2099 | void hists__inc_nr_samples(struct hists *hists, bool filtered) |
| 2100 | { |
| 2101 | events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE); |
| 2102 | if (!filtered) |
| 2103 | hists->stats.nr_non_filtered_samples++; |
| 2104 | } |
| 2105 | |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 2106 | static struct hist_entry *hists__add_dummy_entry(struct hists *hists, |
| 2107 | struct hist_entry *pair) |
| 2108 | { |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 2109 | struct rb_root *root; |
| 2110 | struct rb_node **p; |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 2111 | struct rb_node *parent = NULL; |
| 2112 | struct hist_entry *he; |
Andi Kleen | 354cc40 | 2013-10-01 07:22:15 -0700 | [diff] [blame] | 2113 | int64_t cmp; |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 2114 | |
Jiri Olsa | 5222503 | 2016-05-03 13:54:42 +0200 | [diff] [blame] | 2115 | if (hists__has(hists, need_collapse)) |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 2116 | root = &hists->entries_collapsed; |
| 2117 | else |
| 2118 | root = hists->entries_in; |
| 2119 | |
| 2120 | p = &root->rb_node; |
| 2121 | |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 2122 | while (*p != NULL) { |
| 2123 | parent = *p; |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 2124 | he = rb_entry(parent, struct hist_entry, rb_node_in); |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 2125 | |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 2126 | cmp = hist_entry__collapse(he, pair); |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 2127 | |
| 2128 | if (!cmp) |
| 2129 | goto out; |
| 2130 | |
| 2131 | if (cmp < 0) |
| 2132 | p = &(*p)->rb_left; |
| 2133 | else |
| 2134 | p = &(*p)->rb_right; |
| 2135 | } |
| 2136 | |
Namhyung Kim | a0b51af | 2012-09-11 13:34:27 +0900 | [diff] [blame] | 2137 | he = hist_entry__new(pair, true); |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 2138 | if (he) { |
Arnaldo Carvalho de Melo | 30193d7 | 2012-11-12 13:20:03 -0300 | [diff] [blame] | 2139 | memset(&he->stat, 0, sizeof(he->stat)); |
| 2140 | he->hists = hists; |
Kan Liang | 09623d7 | 2016-04-24 23:28:09 -0700 | [diff] [blame] | 2141 | if (symbol_conf.cumulate_callchain) |
| 2142 | memset(he->stat_acc, 0, sizeof(he->stat)); |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 2143 | rb_link_node(&he->rb_node_in, parent, p); |
| 2144 | rb_insert_color(&he->rb_node_in, root); |
Namhyung Kim | 6263835 | 2014-04-24 16:21:46 +0900 | [diff] [blame] | 2145 | hists__inc_stats(hists, he); |
Jiri Olsa | e0af43d | 2012-12-01 21:18:20 +0100 | [diff] [blame] | 2146 | he->dummy = true; |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 2147 | } |
| 2148 | out: |
| 2149 | return he; |
| 2150 | } |
| 2151 | |
Arnaldo Carvalho de Melo | 95529be | 2012-11-08 17:54:33 -0300 | [diff] [blame] | 2152 | static struct hist_entry *hists__find_entry(struct hists *hists, |
| 2153 | struct hist_entry *he) |
| 2154 | { |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 2155 | struct rb_node *n; |
| 2156 | |
Jiri Olsa | 5222503 | 2016-05-03 13:54:42 +0200 | [diff] [blame] | 2157 | if (hists__has(hists, need_collapse)) |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 2158 | n = hists->entries_collapsed.rb_node; |
| 2159 | else |
| 2160 | n = hists->entries_in->rb_node; |
Arnaldo Carvalho de Melo | 95529be | 2012-11-08 17:54:33 -0300 | [diff] [blame] | 2161 | |
| 2162 | while (n) { |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 2163 | struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in); |
| 2164 | int64_t cmp = hist_entry__collapse(iter, he); |
Arnaldo Carvalho de Melo | 95529be | 2012-11-08 17:54:33 -0300 | [diff] [blame] | 2165 | |
| 2166 | if (cmp < 0) |
| 2167 | n = n->rb_left; |
| 2168 | else if (cmp > 0) |
| 2169 | n = n->rb_right; |
| 2170 | else |
| 2171 | return iter; |
| 2172 | } |
| 2173 | |
| 2174 | return NULL; |
| 2175 | } |
| 2176 | |
| 2177 | /* |
| 2178 | * Look for pairs to link to the leader buckets (hist_entries): |
| 2179 | */ |
| 2180 | void hists__match(struct hists *leader, struct hists *other) |
| 2181 | { |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 2182 | struct rb_root *root; |
Arnaldo Carvalho de Melo | 95529be | 2012-11-08 17:54:33 -0300 | [diff] [blame] | 2183 | struct rb_node *nd; |
| 2184 | struct hist_entry *pos, *pair; |
| 2185 | |
Jiri Olsa | 5222503 | 2016-05-03 13:54:42 +0200 | [diff] [blame] | 2186 | if (hists__has(leader, need_collapse)) |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 2187 | root = &leader->entries_collapsed; |
| 2188 | else |
| 2189 | root = leader->entries_in; |
| 2190 | |
| 2191 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { |
| 2192 | pos = rb_entry(nd, struct hist_entry, rb_node_in); |
Arnaldo Carvalho de Melo | 95529be | 2012-11-08 17:54:33 -0300 | [diff] [blame] | 2193 | pair = hists__find_entry(other, pos); |
| 2194 | |
| 2195 | if (pair) |
Namhyung Kim | 5fa9041 | 2012-11-29 15:38:34 +0900 | [diff] [blame] | 2196 | hist_entry__add_pair(pair, pos); |
Arnaldo Carvalho de Melo | 95529be | 2012-11-08 17:54:33 -0300 | [diff] [blame] | 2197 | } |
| 2198 | } |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 2199 | |
| 2200 | /* |
| 2201 | * Look for entries in the other hists that are not present in the leader, if |
| 2202 | * we find them, just add a dummy entry on the leader hists, with period=0, |
| 2203 | * nr_events=0, to serve as the list header. |
| 2204 | */ |
| 2205 | int hists__link(struct hists *leader, struct hists *other) |
| 2206 | { |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 2207 | struct rb_root *root; |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 2208 | struct rb_node *nd; |
| 2209 | struct hist_entry *pos, *pair; |
| 2210 | |
Jiri Olsa | 5222503 | 2016-05-03 13:54:42 +0200 | [diff] [blame] | 2211 | if (hists__has(other, need_collapse)) |
Namhyung Kim | ce74f60 | 2012-12-10 17:29:55 +0900 | [diff] [blame] | 2212 | root = &other->entries_collapsed; |
| 2213 | else |
| 2214 | root = other->entries_in; |
| 2215 | |
| 2216 | for (nd = rb_first(root); nd; nd = rb_next(nd)) { |
| 2217 | pos = rb_entry(nd, struct hist_entry, rb_node_in); |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 2218 | |
| 2219 | if (!hist_entry__has_pairs(pos)) { |
| 2220 | pair = hists__add_dummy_entry(leader, pos); |
| 2221 | if (pair == NULL) |
| 2222 | return -1; |
Namhyung Kim | 5fa9041 | 2012-11-29 15:38:34 +0900 | [diff] [blame] | 2223 | hist_entry__add_pair(pos, pair); |
Arnaldo Carvalho de Melo | 494d70a | 2012-11-08 18:03:09 -0300 | [diff] [blame] | 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | return 0; |
| 2228 | } |
Namhyung Kim | f214833 | 2014-01-14 11:52:48 +0900 | [diff] [blame] | 2229 | |
Andi Kleen | 57849998 | 2015-07-18 08:24:49 -0700 | [diff] [blame] | 2230 | void hist__account_cycles(struct branch_stack *bs, struct addr_location *al, |
| 2231 | struct perf_sample *sample, bool nonany_branch_mode) |
| 2232 | { |
| 2233 | struct branch_info *bi; |
| 2234 | |
| 2235 | /* If we have branch cycles always annotate them. */ |
| 2236 | if (bs && bs->nr && bs->entries[0].flags.cycles) { |
| 2237 | int i; |
| 2238 | |
| 2239 | bi = sample__resolve_bstack(sample, al); |
| 2240 | if (bi) { |
| 2241 | struct addr_map_symbol *prev = NULL; |
| 2242 | |
| 2243 | /* |
| 2244 | * Ignore errors, still want to process the |
| 2245 | * other entries. |
| 2246 | * |
| 2247 | * For non standard branch modes always |
| 2248 | * force no IPC (prev == NULL) |
| 2249 | * |
| 2250 | * Note that perf stores branches reversed from |
| 2251 | * program order! |
| 2252 | */ |
| 2253 | for (i = bs->nr - 1; i >= 0; i--) { |
| 2254 | addr_map_symbol__account_cycles(&bi[i].from, |
| 2255 | nonany_branch_mode ? NULL : prev, |
| 2256 | bi[i].flags.cycles); |
| 2257 | prev = &bi[i].to; |
| 2258 | } |
| 2259 | free(bi); |
| 2260 | } |
| 2261 | } |
| 2262 | } |
Arnaldo Carvalho de Melo | 2a1731f | 2014-10-10 15:49:21 -0300 | [diff] [blame] | 2263 | |
| 2264 | size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp) |
| 2265 | { |
| 2266 | struct perf_evsel *pos; |
| 2267 | size_t ret = 0; |
| 2268 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 2269 | evlist__for_each_entry(evlist, pos) { |
Arnaldo Carvalho de Melo | 2a1731f | 2014-10-10 15:49:21 -0300 | [diff] [blame] | 2270 | ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos)); |
| 2271 | ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp); |
| 2272 | } |
| 2273 | |
| 2274 | return ret; |
| 2275 | } |
| 2276 | |
| 2277 | |
Namhyung Kim | f214833 | 2014-01-14 11:52:48 +0900 | [diff] [blame] | 2278 | u64 hists__total_period(struct hists *hists) |
| 2279 | { |
| 2280 | return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period : |
| 2281 | hists->stats.total_period; |
| 2282 | } |
Namhyung Kim | 33db456 | 2014-02-07 12:06:07 +0900 | [diff] [blame] | 2283 | |
| 2284 | int parse_filter_percentage(const struct option *opt __maybe_unused, |
| 2285 | const char *arg, int unset __maybe_unused) |
| 2286 | { |
| 2287 | if (!strcmp(arg, "relative")) |
| 2288 | symbol_conf.filter_relative = true; |
| 2289 | else if (!strcmp(arg, "absolute")) |
| 2290 | symbol_conf.filter_relative = false; |
| 2291 | else |
| 2292 | return -1; |
| 2293 | |
| 2294 | return 0; |
| 2295 | } |
Namhyung Kim | 0b93da1 | 2014-01-14 12:02:15 +0900 | [diff] [blame] | 2296 | |
| 2297 | int perf_hist_config(const char *var, const char *value) |
| 2298 | { |
| 2299 | if (!strcmp(var, "hist.percentage")) |
| 2300 | return parse_filter_percentage(NULL, value, 0); |
| 2301 | |
| 2302 | return 0; |
| 2303 | } |
Arnaldo Carvalho de Melo | a635fc5 | 2014-10-09 16:16:00 -0300 | [diff] [blame] | 2304 | |
Jiri Olsa | 5b65855 | 2016-01-18 10:24:22 +0100 | [diff] [blame] | 2305 | int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list) |
Arnaldo Carvalho de Melo | a635fc5 | 2014-10-09 16:16:00 -0300 | [diff] [blame] | 2306 | { |
Arnaldo Carvalho de Melo | a635fc5 | 2014-10-09 16:16:00 -0300 | [diff] [blame] | 2307 | memset(hists, 0, sizeof(*hists)); |
| 2308 | hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT; |
| 2309 | hists->entries_in = &hists->entries_in_array[0]; |
| 2310 | hists->entries_collapsed = RB_ROOT; |
| 2311 | hists->entries = RB_ROOT; |
| 2312 | pthread_mutex_init(&hists->lock, NULL); |
Kan Liang | 21394d9 | 2015-09-04 10:45:44 -0400 | [diff] [blame] | 2313 | hists->socket_filter = -1; |
Jiri Olsa | 5b65855 | 2016-01-18 10:24:22 +0100 | [diff] [blame] | 2314 | hists->hpp_list = hpp_list; |
Namhyung Kim | c3bc0c4 | 2016-03-07 16:44:45 -0300 | [diff] [blame] | 2315 | INIT_LIST_HEAD(&hists->hpp_formats); |
Arnaldo Carvalho de Melo | a635fc5 | 2014-10-09 16:16:00 -0300 | [diff] [blame] | 2316 | return 0; |
| 2317 | } |
| 2318 | |
Namhyung Kim | 61fa0e9 | 2015-12-10 16:53:20 +0900 | [diff] [blame] | 2319 | static void hists__delete_remaining_entries(struct rb_root *root) |
| 2320 | { |
| 2321 | struct rb_node *node; |
| 2322 | struct hist_entry *he; |
| 2323 | |
| 2324 | while (!RB_EMPTY_ROOT(root)) { |
| 2325 | node = rb_first(root); |
| 2326 | rb_erase(node, root); |
| 2327 | |
| 2328 | he = rb_entry(node, struct hist_entry, rb_node_in); |
| 2329 | hist_entry__delete(he); |
| 2330 | } |
| 2331 | } |
| 2332 | |
| 2333 | static void hists__delete_all_entries(struct hists *hists) |
| 2334 | { |
| 2335 | hists__delete_entries(hists); |
| 2336 | hists__delete_remaining_entries(&hists->entries_in_array[0]); |
| 2337 | hists__delete_remaining_entries(&hists->entries_in_array[1]); |
| 2338 | hists__delete_remaining_entries(&hists->entries_collapsed); |
| 2339 | } |
| 2340 | |
Masami Hiramatsu | 17577de | 2015-12-09 11:11:29 +0900 | [diff] [blame] | 2341 | static void hists_evsel__exit(struct perf_evsel *evsel) |
| 2342 | { |
| 2343 | struct hists *hists = evsel__hists(evsel); |
Namhyung Kim | c3bc0c4 | 2016-03-07 16:44:45 -0300 | [diff] [blame] | 2344 | struct perf_hpp_fmt *fmt, *pos; |
| 2345 | struct perf_hpp_list_node *node, *tmp; |
Masami Hiramatsu | 17577de | 2015-12-09 11:11:29 +0900 | [diff] [blame] | 2346 | |
Namhyung Kim | 61fa0e9 | 2015-12-10 16:53:20 +0900 | [diff] [blame] | 2347 | hists__delete_all_entries(hists); |
Namhyung Kim | c3bc0c4 | 2016-03-07 16:44:45 -0300 | [diff] [blame] | 2348 | |
| 2349 | list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) { |
| 2350 | perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) { |
| 2351 | list_del(&fmt->list); |
| 2352 | free(fmt); |
| 2353 | } |
| 2354 | list_del(&node->list); |
| 2355 | free(node); |
| 2356 | } |
Masami Hiramatsu | 17577de | 2015-12-09 11:11:29 +0900 | [diff] [blame] | 2357 | } |
| 2358 | |
Namhyung Kim | fc284be | 2016-01-07 10:14:10 +0100 | [diff] [blame] | 2359 | static int hists_evsel__init(struct perf_evsel *evsel) |
| 2360 | { |
| 2361 | struct hists *hists = evsel__hists(evsel); |
| 2362 | |
Jiri Olsa | 5b65855 | 2016-01-18 10:24:22 +0100 | [diff] [blame] | 2363 | __hists__init(hists, &perf_hpp_list); |
Namhyung Kim | fc284be | 2016-01-07 10:14:10 +0100 | [diff] [blame] | 2364 | return 0; |
| 2365 | } |
| 2366 | |
Arnaldo Carvalho de Melo | a635fc5 | 2014-10-09 16:16:00 -0300 | [diff] [blame] | 2367 | /* |
| 2368 | * XXX We probably need a hists_evsel__exit() to free the hist_entries |
| 2369 | * stored in the rbtree... |
| 2370 | */ |
| 2371 | |
| 2372 | int hists__init(void) |
| 2373 | { |
| 2374 | int err = perf_evsel__object_config(sizeof(struct hists_evsel), |
Masami Hiramatsu | 17577de | 2015-12-09 11:11:29 +0900 | [diff] [blame] | 2375 | hists_evsel__init, |
| 2376 | hists_evsel__exit); |
Arnaldo Carvalho de Melo | a635fc5 | 2014-10-09 16:16:00 -0300 | [diff] [blame] | 2377 | if (err) |
| 2378 | fputs("FATAL ERROR: Couldn't setup hists class\n", stderr); |
| 2379 | |
| 2380 | return err; |
| 2381 | } |
Jiri Olsa | 94b3dc3 | 2016-01-18 10:24:13 +0100 | [diff] [blame] | 2382 | |
| 2383 | void perf_hpp_list__init(struct perf_hpp_list *list) |
| 2384 | { |
| 2385 | INIT_LIST_HEAD(&list->fields); |
| 2386 | INIT_LIST_HEAD(&list->sorts); |
| 2387 | } |