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