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