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