blob: dec996133cdf608517bd50de01a4f00819270d16 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melob10ba7f2019-01-29 11:11:04 +01002#include "callchain.h"
Arnaldo Carvalho de Melob4209022019-08-29 15:56:40 -03003#include "debug.h"
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -03004#include "dso.h"
Frederic Weisbecker598357e2010-05-21 12:48:39 +02005#include "build-id.h"
John Kacur3d1d07e2009-09-28 15:32:55 +02006#include "hist.h"
Krister Johansen9c68ae92017-01-05 22:23:31 -08007#include "map.h"
Arnaldo Carvalho de Melod3300a32019-08-30 15:09:54 -03008#include "map_symbol.h"
9#include "branch.h"
10#include "mem-events.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -020011#include "session.h"
Hari Bathinid890a982017-03-08 02:12:13 +053012#include "namespaces.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -020013#include "sort.h"
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -030014#include "units.h"
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -030015#include "evlist.h"
Namhyung Kim29d720e2013-01-22 18:09:33 +090016#include "evsel.h"
Namhyung Kim69bcb012013-10-30 09:40:34 +090017#include "annotate.h"
Arnaldo Carvalho de Melo632a5ca2017-04-17 16:30:49 -030018#include "srcline.h"
Arnaldo Carvalho de Melodaecf9e2019-01-28 00:03:34 +010019#include "symbol.h"
Arnaldo Carvalho de Meloe7ff8922017-04-19 21:34:35 -030020#include "thread.h"
Jin Yao60414412019-11-07 15:47:14 +080021#include "block-info.h"
Namhyung Kim740b97f2014-12-22 13:44:10 +090022#include "ui/progress.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030023#include <errno.h>
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -020024#include <math.h>
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -030025#include <inttypes.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030026#include <sys/param.h>
Arnaldo Carvalho de Melo5c9dbe62019-08-30 11:54:00 -030027#include <linux/rbtree.h>
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030028#include <linux/string.h>
Andi Kleen37239082019-03-11 07:44:54 -070029#include <linux/time64.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030030#include <linux/zalloc.h>
John Kacur3d1d07e2009-09-28 15:32:55 +020031
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020032static bool hists__filter_entry_by_dso(struct hists *hists,
33 struct hist_entry *he);
34static bool hists__filter_entry_by_thread(struct hists *hists,
35 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090036static bool hists__filter_entry_by_symbol(struct hists *hists,
37 struct hist_entry *he);
Kan Liang21394d92015-09-04 10:45:44 -040038static bool hists__filter_entry_by_socket(struct hists *hists,
39 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020040
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030041u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030042{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030043 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030044}
45
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030046void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030047{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030048 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030049}
50
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030051bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030052{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030053 if (len > hists__col_len(hists, col)) {
54 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030055 return true;
56 }
57 return false;
58}
59
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090060void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030061{
62 enum hist_column col;
63
64 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030065 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030066}
67
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010068static 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 Kim7ccf4f92012-08-20 13:52:05 +090078void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030079{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010080 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Stephane Eranian98a3b322013-01-24 16:10:35 +010081 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030082 u16 len;
83
Jin Yao0bdf1812019-11-07 15:47:13 +080084 if (h->block_info)
85 return;
Namhyung Kimded19d52013-04-01 20:35:19 +090086 /*
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 Kimbb963e12017-02-17 17:17:38 +090093 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +090094 symlen += BITS_PER_LONG / 4 + 2 + 3;
95 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
96 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010097 symlen = unresolved_col_width + 4 + 2;
98 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010099 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100100 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300101
102 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300103 if (hists__new_col_len(hists, HISTC_COMM, len))
Jiri Olsa89c7cb22016-06-20 23:58:19 +0200104 hists__set_col_len(hists, HISTC_THREAD, len + 8);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300105
106 if (h->ms.map) {
107 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300108 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300109 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100110
Namhyung Kimcb993742012-12-27 18:11:42 +0900111 if (h->parent)
112 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
113
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100114 if (h->branch_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300115 if (h->branch_info->from.ms.sym) {
116 symlen = (int)h->branch_info->from.ms.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +0900117 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +0900118 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100119 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
120
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300121 symlen = dso__name_len(h->branch_info->from.ms.map->dso);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100122 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
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300129 if (h->branch_info->to.ms.sym) {
130 symlen = (int)h->branch_info->to.ms.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +0900131 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +0900132 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100133 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
134
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300135 symlen = dso__name_len(h->branch_info->to.ms.map->dso);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100136 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 Kleen508be0d2016-05-20 13:15:08 -0700142
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 Vitillob5387522012-02-09 23:21:01 +0100149 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100150
151 if (h->mem_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300152 if (h->mem_info->daddr.ms.sym) {
153 symlen = (int)h->mem_info->daddr.ms.sym->namelen + 4
Stephane Eranian98a3b322013-01-24 16:10:35 +0100154 + unresolved_col_width + 2;
155 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
156 symlen);
Don Zickus9b32ba72014-06-01 15:38:29 +0200157 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
158 symlen + 1);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100159 } else {
160 symlen = unresolved_col_width + 4 + 2;
161 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
162 symlen);
Jiri Olsa08059092016-01-20 12:56:33 +0100163 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
164 symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100165 }
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200166
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300167 if (h->mem_info->iaddr.ms.sym) {
168 symlen = (int)h->mem_info->iaddr.ms.sym->namelen + 4
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200169 + 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
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300178 if (h->mem_info->daddr.ms.map) {
179 symlen = dso__name_len(h->mem_info->daddr.ms.map->dso);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100180 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 Liang8780fb22017-08-29 13:11:09 -0400186
187 hists__new_col_len(hists, HISTC_MEM_PHYS_DADDR,
188 unresolved_col_width + 4 + 2);
189
Stephane Eranian98a3b322013-01-24 16:10:35 +0100190 } else {
191 symlen = unresolved_col_width + 4 + 2;
192 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200193 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100194 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
195 }
196
Hari Bathinid890a982017-03-08 02:12:13 +0530197 hists__new_col_len(hists, HISTC_CGROUP_ID, 20);
Arnaldo Carvalho de Meloa4978ec2015-09-09 12:14:00 -0300198 hists__new_col_len(hists, HISTC_CPU, 3);
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400199 hists__new_col_len(hists, HISTC_SOCKET, 6);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100200 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 Kleen3dab6ac2019-08-23 14:03:38 -0700206 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 Kleen475eeab2013-09-20 07:40:43 -0700210
Jiri Olsaf666ac02016-09-19 15:10:10 +0200211 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 Meloe8e6d372015-08-10 16:53:54 -0300215
Andi Kleen31191a82015-08-07 15:54:24 -0700216 if (h->srcfile)
217 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
218
Andi Kleen475eeab2013-09-20 07:40:43 -0700219 if (h->transaction)
220 hists__new_col_len(hists, HISTC_TRANSACTION,
221 hist_entry__transaction_len());
Namhyung Kim0c0af782016-02-21 23:22:38 +0900222
223 if (h->trace_output)
224 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300225}
226
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900227void hists__output_recalc_col_len(struct hists *hists, int max_rows)
228{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800229 struct rb_node *next = rb_first_cached(&hists->entries);
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900230 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 Kimf39056f2014-01-14 14:25:37 +0900243static void he_stat__add_cpumode_period(struct he_stat *he_stat,
244 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800245{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300246 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800247 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900248 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800249 break;
250 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900251 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800252 break;
253 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900254 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800255 break;
256 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900257 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800258 break;
259 default:
260 break;
261 }
262}
263
Andi Kleen37239082019-03-11 07:44:54 -0700264static 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 Kleen05484292013-01-24 16:10:29 +0100272static void he_stat__add_period(struct he_stat *he_stat, u64 period,
273 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900274{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100275
Namhyung Kim139c0812012-10-04 21:49:43 +0900276 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100277 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900278 he_stat->nr_events += 1;
279}
280
281static 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 Kleen05484292013-01-24 16:10:29 +0100289 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900290}
291
Namhyung Kimf39056f2014-01-14 14:25:37 +0900292static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300293{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900294 he_stat->period = (he_stat->period * 7) / 8;
295 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100296 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300297}
298
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900299static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
300
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300301static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
302{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900303 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900304 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200305
306 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300307 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200308
Namhyung Kimf39056f2014-01-14 14:25:37 +0900309 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900310 if (symbol_conf.cumulate_callchain)
311 he_stat__decay(he->stat_acc);
Namhyung Kim42b276a2016-01-05 12:06:00 +0900312 decay_callchain(he->callchain);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200313
Namhyung Kim3186b682014-04-22 13:44:23 +0900314 diff = prev_period - he->stat.period;
315
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900316 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 Bueso2eb3d682018-12-06 11:18:18 -0800324 struct rb_node *node = rb_first_cached(&he->hroot_out);
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900325 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 Meloc64550c2011-10-20 06:45:44 -0200333
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900334 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300335}
336
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300337static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
338{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800339 struct rb_root_cached *root_in;
340 struct rb_root_cached *root_out;
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300341
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900342 if (he->parent_he) {
343 root_in = &he->parent_he->hroot_in;
344 root_out = &he->parent_he->hroot_out;
345 } else {
Jiri Olsa52225032016-05-03 13:54:42 +0200346 if (hists__has(hists, need_collapse))
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900347 root_in = &hists->entries_collapsed;
348 else
349 root_in = hists->entries_in;
350 root_out = &hists->entries;
351 }
352
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800353 rb_erase_cached(&he->rb_node_in, root_in);
354 rb_erase_cached(&he->rb_node, root_out);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300355
356 --hists->nr_entries;
357 if (!he->filtered)
358 --hists->nr_non_filtered_entries;
359
360 hist_entry__delete(he);
361}
362
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900363void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300364{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800365 struct rb_node *next = rb_first_cached(&hists->entries);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300366 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 Melob079d4e2011-10-17 09:05:04 -0200371 if (((zap_user && n->level == '.') ||
372 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300373 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300374 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300375 }
376 }
377}
378
Namhyung Kim701937b2014-08-12 17:16:05 +0900379void hists__delete_entries(struct hists *hists)
380{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800381 struct rb_node *next = rb_first_cached(&hists->entries);
Namhyung Kim701937b2014-08-12 17:16:05 +0900382 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 Melo956b65e2014-12-19 12:41:28 -0300388 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900389 }
390}
391
Jin Yaob10c78c2019-06-28 17:23:03 +0800392struct 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 Kacur3d1d07e2009-09-28 15:32:55 +0200410/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300411 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200412 */
413
Jiri Olsa0a269a62016-07-05 08:56:03 +0200414static int hist_entry__init(struct hist_entry *he,
415 struct hist_entry *template,
Arnaldo Carvalho de Melo41477ac2018-06-07 14:19:54 -0300416 bool sample_self,
417 size_t callchain_size)
Jiri Olsa0a269a62016-07-05 08:56:03 +0200418{
419 *he = *template;
Arnaldo Carvalho de Melo41477ac2018-06-07 14:19:54 -0300420 he->callchain_size = callchain_size;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200421
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 Olsac5758912019-03-05 16:25:30 +0100440 if (he->branch_info == NULL)
441 goto err;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200442
443 memcpy(he->branch_info, template->branch_info,
444 sizeof(*he->branch_info));
445
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300446 map__get(he->branch_info->from.ms.map);
447 map__get(he->branch_info->to.ms.map);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200448 }
449
450 if (he->mem_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300451 map__get(he->mem_info->iaddr.ms.map);
452 map__get(he->mem_info->daddr.ms.map);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200453 }
454
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -0300455 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
Jiri Olsa0a269a62016-07-05 08:56:03 +0200456 callchain_init(he->callchain);
457
458 if (he->raw_data) {
459 he->raw_data = memdup(he->raw_data, he->raw_size);
Jiri Olsac5758912019-03-05 16:25:30 +0100460 if (he->raw_data == NULL)
461 goto err_infos;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200462 }
Jiri Olsa26349582019-03-05 16:25:31 +0100463
464 if (he->srcline) {
465 he->srcline = strdup(he->srcline);
466 if (he->srcline == NULL)
467 goto err_rawdata;
468 }
469
Andi Kleen4968ac82019-03-11 07:44:58 -0700470 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 Olsa0a269a62016-07-05 08:56:03 +0200477 INIT_LIST_HEAD(&he->pairs.node);
478 thread__get(he->thread);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800479 he->hroot_in = RB_ROOT_CACHED;
480 he->hroot_out = RB_ROOT_CACHED;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200481
482 if (!symbol_conf.report_hierarchy)
483 he->leaf = true;
484
485 return 0;
Jiri Olsac5758912019-03-05 16:25:30 +0100486
Andi Kleen4968ac82019-03-11 07:44:58 -0700487err_srcline:
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300488 zfree(&he->srcline);
Andi Kleen4968ac82019-03-11 07:44:58 -0700489
Jiri Olsa26349582019-03-05 16:25:31 +0100490err_rawdata:
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300491 zfree(&he->raw_data);
Jiri Olsa26349582019-03-05 16:25:31 +0100492
Jiri Olsac5758912019-03-05 16:25:30 +0100493err_infos:
494 if (he->branch_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300495 map__put(he->branch_info->from.ms.map);
496 map__put(he->branch_info->to.ms.map);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300497 zfree(&he->branch_info);
Jiri Olsac5758912019-03-05 16:25:30 +0100498 }
499 if (he->mem_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300500 map__put(he->mem_info->iaddr.ms.map);
501 map__put(he->mem_info->daddr.ms.map);
Jiri Olsac5758912019-03-05 16:25:30 +0100502 }
503err:
504 map__zput(he->ms.map);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300505 zfree(&he->stat_acc);
Jiri Olsac5758912019-03-05 16:25:30 +0100506 return -ENOMEM;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200507}
508
Jiri Olsaf542e762016-07-05 08:56:04 +0200509static void *hist_entry__zalloc(size_t size)
510{
511 return zalloc(size + sizeof(struct hist_entry));
512}
513
514static void hist_entry__free(void *ptr)
515{
516 free(ptr);
517}
518
519static struct hist_entry_ops default_ops = {
520 .new = hist_entry__zalloc,
521 .free = hist_entry__free,
522};
523
Namhyung Kima0b51af2012-09-11 13:34:27 +0900524static struct hist_entry *hist_entry__new(struct hist_entry *template,
525 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300526{
Jiri Olsaf542e762016-07-05 08:56:04 +0200527 struct hist_entry_ops *ops = template->ops;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900528 size_t callchain_size = 0;
529 struct hist_entry *he;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200530 int err = 0;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900531
Jiri Olsaf542e762016-07-05 08:56:04 +0200532 if (!ops)
533 ops = template->ops = &default_ops;
534
Namhyung Kim82aa0192014-12-22 13:44:14 +0900535 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900536 callchain_size = sizeof(struct callchain_root);
537
Jiri Olsaf542e762016-07-05 08:56:04 +0200538 he = ops->new(callchain_size);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200539 if (he) {
Arnaldo Carvalho de Melo41477ac2018-06-07 14:19:54 -0300540 err = hist_entry__init(he, template, sample_self, callchain_size);
Jiri Olsaf542e762016-07-05 08:56:04 +0200541 if (err) {
542 ops->free(he);
543 he = NULL;
544 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300545 }
546
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200547 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300548}
549
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300550static 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 Kim467ef102016-02-16 23:08:19 +0900557static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
558{
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -0300559 if (!hist_entry__has_callchains(he) || !symbol_conf.use_callchain)
Namhyung Kim467ef102016-02-16 23:08:19 +0900560 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 Meloe7e0efc2015-05-19 11:31:22 -0300567static 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 Melo9735abf2009-10-03 10:42:45 -0300571{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300572 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300573 struct rb_node *parent = NULL;
574 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700575 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900576 u64 period = entry->stat.period;
577 u64 weight = entry->stat.weight;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800578 bool leftmost = true;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300579
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800580 p = &hists->entries_in->rb_root.rb_node;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300581
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300582 while (*p != NULL) {
583 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300584 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300585
Namhyung Kim9afcf932012-12-10 17:29:54 +0900586 /*
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 Melo9735abf2009-10-03 10:42:45 -0300593
594 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900595 if (sample_self) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900596 he_stat__add_period(&he->stat, period, weight);
Namhyung Kim467ef102016-02-16 23:08:19 +0900597 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900598 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900599 if (symbol_conf.cumulate_callchain)
600 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400601
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900602 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300603 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900604 * and will not be used anymore.
605 */
Jiri Olsa9f874982018-03-07 16:50:06 +0100606 mem_info__zput(entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900607
Jin Yaofe962452019-06-28 17:22:59 +0800608 block_info__zput(entry->block_info);
609
David Miller63fa4712012-03-27 03:14:18 -0400610 /* 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 Melo5c24b672015-06-15 23:29:51 -0300617 map__put(he->ms.map);
618 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400619 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300620 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300621 }
622
623 if (cmp < 0)
624 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800625 else {
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300626 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800627 leftmost = false;
628 }
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300629 }
630
Namhyung Kima0b51af2012-09-11 13:34:27 +0900631 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300632 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900633 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300634
Namhyung Kim0f584742016-01-28 00:40:49 +0900635 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900636 hist_entry__add_callchain_period(he, period);
637 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900638
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300639 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800640 rb_insert_color_cached(&he->rb_node_in, hists->entries_in, leftmost);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300641out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900642 if (sample_self)
643 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900644 if (symbol_conf.cumulate_callchain)
645 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300646 return he;
647}
648
Andi Kleen4968ac82019-03-11 07:44:58 -0700649static 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
659static 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 Olsaa5051972016-07-05 08:56:05 +0200675static 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 Yaofe962452019-06-28 17:22:59 +0800681 struct block_info *block_info,
Jiri Olsaa5051972016-07-05 08:56:05 +0200682 struct perf_sample *sample,
683 bool sample_self,
684 struct hist_entry_ops *ops)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100685{
Hari Bathinid890a982017-03-08 02:12:13 +0530686 struct namespaces *ns = thread__namespaces(al->thread);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100687 struct hist_entry entry = {
688 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900689 .comm = thread__comm(al->thread),
Hari Bathinid890a982017-03-08 02:12:13 +0530690 .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 Vitillob5387522012-02-09 23:21:01 +0100694 .ms = {
695 .map = al->map,
696 .sym = al->sym,
697 },
Jiri Olsa26349582019-03-05 16:25:31 +0100698 .srcline = (char *) al->srcline,
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400699 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400700 .cpu = al->cpu,
701 .cpumode = al->cpumode,
702 .ip = al->addr,
703 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900704 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900705 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900706 .period = sample->period,
707 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900708 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100709 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300710 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300711 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900712 .branch_info = bi,
713 .mem_info = mi,
Jin Yaofe962452019-06-28 17:22:59 +0800714 .block_info = block_info,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900715 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900716 .raw_data = sample->raw_data,
717 .raw_size = sample->raw_size,
Jiri Olsaa5051972016-07-05 08:56:05 +0200718 .ops = ops,
Andi Kleen37239082019-03-11 07:44:54 -0700719 .time = hist_time(sample->time),
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300720 }, *he = hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100721
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300722 if (!hists->has_callchains && he && he->callchain_size != 0)
723 hists->has_callchains = true;
Andi Kleen4968ac82019-03-11 07:44:58 -0700724 if (he && symbol_conf.res_sample)
725 hists__res_sample(he, sample);
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300726 return he;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100727}
728
Jiri Olsaa5051972016-07-05 08:56:05 +0200729struct 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 Yaofe962452019-06-28 17:22:59 +0800737 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
Jiri Olsaa5051972016-07-05 08:56:05 +0200738 sample, sample_self, NULL);
739}
740
741struct 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 Yaofe962452019-06-28 17:22:59 +0800750 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
Jiri Olsaa5051972016-07-05 08:56:05 +0200751 sample, sample_self, ops);
752}
753
Jin Yaofe962452019-06-28 17:22:59 +0800754struct 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,
Jin Yaob65a7d32019-11-07 15:47:16 +0800761 .ms = {
762 .map = al->map,
763 .sym = al->sym,
764 },
Jin Yaofe962452019-06-28 17:22:59 +0800765 }, *he = hists__findnew_entry(hists, &entry, al, false);
766
767 return he;
768}
769
Namhyung Kim69bcb012013-10-30 09:40:34 +0900770static int
771iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
772 struct addr_location *al __maybe_unused)
773{
774 return 0;
775}
776
777static int
778iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
779 struct addr_location *al __maybe_unused)
780{
781 return 0;
782}
783
784static int
785iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
786{
787 struct perf_sample *sample = iter->sample;
788 struct mem_info *mi;
789
790 mi = sample__resolve_mem(sample, al);
791 if (mi == NULL)
792 return -ENOMEM;
793
794 iter->priv = mi;
795 return 0;
796}
797
798static int
799iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
800{
801 u64 cost;
802 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300803 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900804 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900805 struct hist_entry *he;
806
807 if (mi == NULL)
808 return -EINVAL;
809
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900810 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900811 if (!cost)
812 cost = 1;
813
814 /*
815 * must pass period=weight in order to get the correct
816 * sorting from hists__collapse_resort() which is solely
817 * based on periods. We want sorting be done on nr_events * weight
818 * and this is indirectly achieved by passing period=weight here
819 * and the he_stat__add_period() function.
820 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900821 sample->period = cost;
822
Jiri Olsa0102ef32016-06-14 20:19:21 +0200823 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
824 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900825 if (!he)
826 return -ENOMEM;
827
828 iter->he = he;
829 return 0;
830}
831
832static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900833iter_finish_mem_entry(struct hist_entry_iter *iter,
834 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900835{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200836 struct evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300837 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900838 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900839 int err = -EINVAL;
840
841 if (he == NULL)
842 goto out;
843
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300844 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900845
846 err = hist_entry__append_callchain(he, iter->sample);
847
848out:
849 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300850 * We don't need to free iter->priv (mem_info) here since the mem info
851 * was either already freed in hists__findnew_entry() or passed to a
852 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900853 */
854 iter->priv = NULL;
855
856 iter->he = NULL;
857 return err;
858}
859
860static int
861iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
862{
863 struct branch_info *bi;
864 struct perf_sample *sample = iter->sample;
865
866 bi = sample__resolve_bstack(sample, al);
867 if (!bi)
868 return -ENOMEM;
869
870 iter->curr = 0;
871 iter->total = sample->branch_stack->nr;
872
873 iter->priv = bi;
874 return 0;
875}
876
877static int
Jin Yao2d78b182017-07-18 20:13:14 +0800878iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900879 struct addr_location *al __maybe_unused)
880{
881 return 0;
882}
883
884static int
885iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
886{
887 struct branch_info *bi = iter->priv;
888 int i = iter->curr;
889
890 if (bi == NULL)
891 return 0;
892
893 if (iter->curr >= iter->total)
894 return 0;
895
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300896 al->map = bi[i].to.ms.map;
897 al->sym = bi[i].to.ms.sym;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900898 al->addr = bi[i].to.addr;
899 return 1;
900}
901
902static int
903iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
904{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900905 struct branch_info *bi;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200906 struct evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300907 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900908 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900909 struct hist_entry *he = NULL;
910 int i = iter->curr;
911 int err = 0;
912
913 bi = iter->priv;
914
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300915 if (iter->hide_unresolved && !(bi[i].from.ms.sym && bi[i].to.ms.sym))
Namhyung Kim69bcb012013-10-30 09:40:34 +0900916 goto out;
917
918 /*
919 * The report shows the percentage of total branches captured
920 * and not events sampled. Thus we use a pseudo period of 1.
921 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900922 sample->period = 1;
923 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
924
Jiri Olsa0102ef32016-06-14 20:19:21 +0200925 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
926 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900927 if (he == NULL)
928 return -ENOMEM;
929
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300930 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900931
932out:
933 iter->he = he;
934 iter->curr++;
935 return err;
936}
937
938static int
939iter_finish_branch_entry(struct hist_entry_iter *iter,
940 struct addr_location *al __maybe_unused)
941{
942 zfree(&iter->priv);
943 iter->he = NULL;
944
945 return iter->curr >= iter->total ? 0 : -1;
946}
947
948static int
949iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
950 struct addr_location *al __maybe_unused)
951{
952 return 0;
953}
954
955static int
956iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
957{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200958 struct evsel *evsel = iter->evsel;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900959 struct perf_sample *sample = iter->sample;
960 struct hist_entry *he;
961
Jiri Olsa0102ef32016-06-14 20:19:21 +0200962 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
963 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900964 if (he == NULL)
965 return -ENOMEM;
966
967 iter->he = he;
968 return 0;
969}
970
971static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900972iter_finish_normal_entry(struct hist_entry_iter *iter,
973 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900974{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900975 struct hist_entry *he = iter->he;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200976 struct evsel *evsel = iter->evsel;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900977 struct perf_sample *sample = iter->sample;
978
979 if (he == NULL)
980 return 0;
981
982 iter->he = NULL;
983
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300984 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900985
986 return hist_entry__append_callchain(he, sample);
987}
988
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900989static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300990iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900991 struct addr_location *al __maybe_unused)
992{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900993 struct hist_entry **he_cache;
994
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900995 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900996
997 /*
998 * This is for detecting cycles or recursions so that they're
999 * cumulated only one time to prevent entries more than 100%
1000 * overhead.
1001 */
Jiri Olsae3ebaa42018-02-16 13:36:19 +01001002 he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001003 if (he_cache == NULL)
1004 return -ENOMEM;
1005
1006 iter->priv = he_cache;
1007 iter->curr = 0;
1008
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001009 return 0;
1010}
1011
1012static int
1013iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
1014 struct addr_location *al)
1015{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001016 struct evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001017 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001018 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001019 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001020 struct hist_entry *he;
1021 int err = 0;
1022
Jiri Olsa0102ef32016-06-14 20:19:21 +02001023 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
1024 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001025 if (he == NULL)
1026 return -ENOMEM;
1027
1028 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001029 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001030
Namhyung Kim82aa0192014-12-22 13:44:14 +09001031 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +09001032
1033 /*
1034 * We need to re-initialize the cursor since callchain_append()
1035 * advanced the cursor to the end.
1036 */
1037 callchain_cursor_commit(&callchain_cursor);
1038
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001039 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001040
1041 return err;
1042}
1043
1044static int
1045iter_next_cumulative_entry(struct hist_entry_iter *iter,
1046 struct addr_location *al)
1047{
1048 struct callchain_cursor_node *node;
1049
1050 node = callchain_cursor_current(&callchain_cursor);
1051 if (node == NULL)
1052 return 0;
1053
Namhyung Kimc7405d82013-10-31 13:58:30 +09001054 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001055}
1056
1057static int
1058iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
1059 struct addr_location *al)
1060{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001061 struct evsel *evsel = iter->evsel;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001062 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001063 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001064 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001065 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -03001066 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001067 .cpu = al->cpu,
1068 .thread = al->thread,
1069 .comm = thread__comm(al->thread),
1070 .ip = al->addr,
1071 .ms = {
1072 .map = al->map,
1073 .sym = al->sym,
1074 },
Jiri Olsa26349582019-03-05 16:25:31 +01001075 .srcline = (char *) al->srcline,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001076 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +09001077 .raw_data = sample->raw_data,
1078 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001079 };
1080 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +09001081 struct callchain_cursor cursor;
1082
1083 callchain_cursor_snapshot(&cursor, &callchain_cursor);
1084
1085 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001086
1087 /*
1088 * Check if there's duplicate entries in the callchain.
1089 * It's possible that it has cycles or recursive calls.
1090 */
1091 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001092 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
1093 /* to avoid calling callback function */
1094 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001095 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001096 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001097 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001098
Jiri Olsa0102ef32016-06-14 20:19:21 +02001099 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
1100 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001101 if (he == NULL)
1102 return -ENOMEM;
1103
1104 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001105 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001106
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001107 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
Namhyung Kim82aa0192014-12-22 13:44:14 +09001108 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001109 return 0;
1110}
1111
1112static int
1113iter_finish_cumulative_entry(struct hist_entry_iter *iter,
1114 struct addr_location *al __maybe_unused)
1115{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001116 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001117 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001118
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001119 return 0;
1120}
1121
Namhyung Kim69bcb012013-10-30 09:40:34 +09001122const struct hist_iter_ops hist_iter_mem = {
1123 .prepare_entry = iter_prepare_mem_entry,
1124 .add_single_entry = iter_add_single_mem_entry,
1125 .next_entry = iter_next_nop_entry,
1126 .add_next_entry = iter_add_next_nop_entry,
1127 .finish_entry = iter_finish_mem_entry,
1128};
1129
1130const struct hist_iter_ops hist_iter_branch = {
1131 .prepare_entry = iter_prepare_branch_entry,
1132 .add_single_entry = iter_add_single_branch_entry,
1133 .next_entry = iter_next_branch_entry,
1134 .add_next_entry = iter_add_next_branch_entry,
1135 .finish_entry = iter_finish_branch_entry,
1136};
1137
1138const struct hist_iter_ops hist_iter_normal = {
1139 .prepare_entry = iter_prepare_normal_entry,
1140 .add_single_entry = iter_add_single_normal_entry,
1141 .next_entry = iter_next_nop_entry,
1142 .add_next_entry = iter_add_next_nop_entry,
1143 .finish_entry = iter_finish_normal_entry,
1144};
1145
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001146const struct hist_iter_ops hist_iter_cumulative = {
1147 .prepare_entry = iter_prepare_cumulative_entry,
1148 .add_single_entry = iter_add_single_cumulative_entry,
1149 .next_entry = iter_next_cumulative_entry,
1150 .add_next_entry = iter_add_next_cumulative_entry,
1151 .finish_entry = iter_finish_cumulative_entry,
1152};
1153
Namhyung Kim69bcb012013-10-30 09:40:34 +09001154int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001155 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +09001156{
1157 int err, err2;
Krister Johansen9c68ae92017-01-05 22:23:31 -08001158 struct map *alm = NULL;
1159
Arnaldo Carvalho de Melo362379a2018-05-24 11:20:39 -03001160 if (al)
Krister Johansen9c68ae92017-01-05 22:23:31 -08001161 alm = map__get(al->map);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001162
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001163 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
Namhyung Kim063bd932015-05-19 17:04:10 +09001164 iter->evsel, al, max_stack_depth);
Changbin Ducb6186a2019-03-16 16:05:49 +08001165 if (err) {
1166 map__put(alm);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001167 return err;
Changbin Ducb6186a2019-03-16 16:05:49 +08001168 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001169
Namhyung Kim69bcb012013-10-30 09:40:34 +09001170 err = iter->ops->prepare_entry(iter, al);
1171 if (err)
1172 goto out;
1173
1174 err = iter->ops->add_single_entry(iter, al);
1175 if (err)
1176 goto out;
1177
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001178 if (iter->he && iter->add_entry_cb) {
1179 err = iter->add_entry_cb(iter, al, true, arg);
1180 if (err)
1181 goto out;
1182 }
1183
Namhyung Kim69bcb012013-10-30 09:40:34 +09001184 while (iter->ops->next_entry(iter, al)) {
1185 err = iter->ops->add_next_entry(iter, al);
1186 if (err)
1187 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001188
1189 if (iter->he && iter->add_entry_cb) {
1190 err = iter->add_entry_cb(iter, al, false, arg);
1191 if (err)
1192 goto out;
1193 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001194 }
1195
1196out:
1197 err2 = iter->ops->finish_entry(iter, al);
1198 if (!err)
1199 err = err2;
1200
Krister Johansen9c68ae92017-01-05 22:23:31 -08001201 map__put(alm);
1202
Namhyung Kim69bcb012013-10-30 09:40:34 +09001203 return err;
1204}
1205
John Kacur3d1d07e2009-09-28 15:32:55 +02001206int64_t
1207hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1208{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001209 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001210 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001211 int64_t cmp = 0;
1212
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001213 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001214 if (perf_hpp__is_dynamic_entry(fmt) &&
1215 !perf_hpp__defined_dynamic_entry(fmt, hists))
1216 continue;
1217
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001218 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001219 if (cmp)
1220 break;
1221 }
1222
1223 return cmp;
1224}
1225
1226int64_t
1227hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1228{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001229 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001230 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001231 int64_t cmp = 0;
1232
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001233 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001234 if (perf_hpp__is_dynamic_entry(fmt) &&
1235 !perf_hpp__defined_dynamic_entry(fmt, hists))
1236 continue;
1237
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001238 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001239 if (cmp)
1240 break;
1241 }
1242
1243 return cmp;
1244}
1245
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001246void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001247{
Jiri Olsaf542e762016-07-05 08:56:04 +02001248 struct hist_entry_ops *ops = he->ops;
1249
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001250 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001251 map__zput(he->ms.map);
1252
1253 if (he->branch_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -03001254 map__zput(he->branch_info->from.ms.map);
1255 map__zput(he->branch_info->to.ms.map);
Andi Kleen508be0d2016-05-20 13:15:08 -07001256 free_srcline(he->branch_info->srcline_from);
1257 free_srcline(he->branch_info->srcline_to);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001258 zfree(&he->branch_info);
1259 }
1260
1261 if (he->mem_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -03001262 map__zput(he->mem_info->iaddr.ms.map);
1263 map__zput(he->mem_info->daddr.ms.map);
Jiri Olsa9f874982018-03-07 16:50:06 +01001264 mem_info__zput(he->mem_info);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001265 }
1266
Jin Yao99150a12019-06-28 17:23:01 +08001267 if (he->block_info)
1268 block_info__zput(he->block_info);
1269
Andi Kleen4968ac82019-03-11 07:44:58 -07001270 zfree(&he->res_samples);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001271 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +09001272 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -07001273 if (he->srcfile && he->srcfile[0])
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001274 zfree(&he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001275 free_callchain(he->callchain);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001276 zfree(&he->trace_output);
1277 zfree(&he->raw_data);
Jiri Olsaf542e762016-07-05 08:56:04 +02001278 ops->free(he);
John Kacur3d1d07e2009-09-28 15:32:55 +02001279}
1280
1281/*
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001282 * If this is not the last column, then we need to pad it according to the
Ingo Molnaradba1632018-12-03 11:22:00 +01001283 * pre-calculated max length for this column, otherwise don't bother adding
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001284 * spaces because that would break viewing this with, for instance, 'less',
1285 * that would show tons of trailing spaces when a long C++ demangled method
1286 * names is sampled.
1287*/
1288int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1289 struct perf_hpp_fmt *fmt, int printed)
1290{
1291 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
Jiri Olsada1b0402016-06-14 20:19:20 +02001292 const int width = fmt->width(fmt, hpp, he->hists);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001293 if (printed < width) {
1294 advance_hpp(hpp, printed);
1295 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1296 }
1297 }
1298
1299 return printed;
1300}
1301
1302/*
John Kacur3d1d07e2009-09-28 15:32:55 +02001303 * collapse the histogram
1304 */
1305
Namhyung Kimaef810e2016-02-25 00:13:34 +09001306static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
Namhyung Kimaec13a72016-03-09 22:46:58 +09001307static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1308 enum hist_filter type);
1309
1310typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1311
1312static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1313{
1314 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1315}
1316
1317static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1318 enum hist_filter type,
1319 fmt_chk_fn check)
1320{
1321 struct perf_hpp_fmt *fmt;
1322 bool type_match = false;
1323 struct hist_entry *parent = he->parent_he;
1324
1325 switch (type) {
1326 case HIST_FILTER__THREAD:
1327 if (symbol_conf.comm_list == NULL &&
1328 symbol_conf.pid_list == NULL &&
1329 symbol_conf.tid_list == NULL)
1330 return;
1331 break;
1332 case HIST_FILTER__DSO:
1333 if (symbol_conf.dso_list == NULL)
1334 return;
1335 break;
1336 case HIST_FILTER__SYMBOL:
1337 if (symbol_conf.sym_list == NULL)
1338 return;
1339 break;
1340 case HIST_FILTER__PARENT:
1341 case HIST_FILTER__GUEST:
1342 case HIST_FILTER__HOST:
1343 case HIST_FILTER__SOCKET:
Jiri Olsa9857b712016-08-17 14:55:23 +02001344 case HIST_FILTER__C2C:
Namhyung Kimaec13a72016-03-09 22:46:58 +09001345 default:
1346 return;
1347 }
1348
1349 /* if it's filtered by own fmt, it has to have filter bits */
1350 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1351 if (check(fmt)) {
1352 type_match = true;
1353 break;
1354 }
1355 }
1356
1357 if (type_match) {
1358 /*
1359 * If the filter is for current level entry, propagate
1360 * filter marker to parents. The marker bit was
1361 * already set by default so it only needs to clear
1362 * non-filtered entries.
1363 */
1364 if (!(he->filtered & (1 << type))) {
1365 while (parent) {
1366 parent->filtered &= ~(1 << type);
1367 parent = parent->parent_he;
1368 }
1369 }
1370 } else {
1371 /*
1372 * If current entry doesn't have matching formats, set
1373 * filter marker for upper level entries. it will be
1374 * cleared if its lower level entries is not filtered.
1375 *
1376 * For lower-level entries, it inherits parent's
1377 * filter bit so that lower level entries of a
1378 * non-filtered entry won't set the filter marker.
1379 */
1380 if (parent == NULL)
1381 he->filtered |= (1 << type);
1382 else
1383 he->filtered |= (parent->filtered & (1 << type));
1384 }
1385}
1386
1387static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1388{
1389 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1390 check_thread_entry);
1391
1392 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1393 perf_hpp__is_dso_entry);
1394
1395 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1396 perf_hpp__is_sym_entry);
1397
1398 hists__apply_filters(he->hists, he);
1399}
Namhyung Kimaef810e2016-02-25 00:13:34 +09001400
1401static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001402 struct rb_root_cached *root,
Namhyung Kimaef810e2016-02-25 00:13:34 +09001403 struct hist_entry *he,
Namhyung Kimaec13a72016-03-09 22:46:58 +09001404 struct hist_entry *parent_he,
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001405 struct perf_hpp_list *hpp_list)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001406{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001407 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001408 struct rb_node *parent = NULL;
1409 struct hist_entry *iter, *new;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001410 struct perf_hpp_fmt *fmt;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001411 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001412 bool leftmost = true;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001413
1414 while (*p != NULL) {
1415 parent = *p;
1416 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1417
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001418 cmp = 0;
1419 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1420 cmp = fmt->collapse(fmt, iter, he);
1421 if (cmp)
1422 break;
1423 }
1424
Namhyung Kimaef810e2016-02-25 00:13:34 +09001425 if (!cmp) {
1426 he_stat__add_stat(&iter->stat, &he->stat);
1427 return iter;
1428 }
1429
1430 if (cmp < 0)
1431 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001432 else {
Namhyung Kimaef810e2016-02-25 00:13:34 +09001433 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001434 leftmost = false;
1435 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001436 }
1437
1438 new = hist_entry__new(he, true);
1439 if (new == NULL)
1440 return NULL;
1441
Namhyung Kimaef810e2016-02-25 00:13:34 +09001442 hists->nr_entries++;
1443
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001444 /* save related format list for output */
1445 new->hpp_list = hpp_list;
Namhyung Kimaec13a72016-03-09 22:46:58 +09001446 new->parent_he = parent_he;
1447
1448 hist_entry__apply_hierarchy_filters(new);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001449
1450 /* some fields are now passed to 'new' */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001451 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1452 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1453 he->trace_output = NULL;
1454 else
1455 new->trace_output = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001456
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001457 if (perf_hpp__is_srcline_entry(fmt))
1458 he->srcline = NULL;
1459 else
1460 new->srcline = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001461
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001462 if (perf_hpp__is_srcfile_entry(fmt))
1463 he->srcfile = NULL;
1464 else
1465 new->srcfile = NULL;
1466 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001467
1468 rb_link_node(&new->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001469 rb_insert_color_cached(&new->rb_node_in, root, leftmost);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001470 return new;
1471}
1472
1473static int hists__hierarchy_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001474 struct rb_root_cached *root,
Namhyung Kimaef810e2016-02-25 00:13:34 +09001475 struct hist_entry *he)
1476{
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001477 struct perf_hpp_list_node *node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001478 struct hist_entry *new_he = NULL;
1479 struct hist_entry *parent = NULL;
1480 int depth = 0;
1481 int ret = 0;
1482
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001483 list_for_each_entry(node, &hists->hpp_formats, list) {
1484 /* skip period (overhead) and elided columns */
1485 if (node->level == 0 || node->skip)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001486 continue;
1487
1488 /* insert copy of 'he' for each fmt into the hierarchy */
Namhyung Kimaec13a72016-03-09 22:46:58 +09001489 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001490 if (new_he == NULL) {
1491 ret = -1;
1492 break;
1493 }
1494
1495 root = &new_he->hroot_in;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001496 new_he->depth = depth++;
1497 parent = new_he;
1498 }
1499
1500 if (new_he) {
1501 new_he->leaf = true;
1502
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001503 if (hist_entry__has_callchains(new_he) &&
1504 symbol_conf.use_callchain) {
Namhyung Kimaef810e2016-02-25 00:13:34 +09001505 callchain_cursor_reset(&callchain_cursor);
1506 if (callchain_merge(&callchain_cursor,
1507 new_he->callchain,
1508 he->callchain) < 0)
1509 ret = -1;
1510 }
1511 }
1512
1513 /* 'he' is no longer used */
1514 hist_entry__delete(he);
1515
1516 /* return 0 (or -1) since it already applied filters */
1517 return ret;
1518}
1519
Jiri Olsa592dac62016-03-24 13:52:17 +01001520static int hists__collapse_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001521 struct rb_root_cached *root,
Jiri Olsa592dac62016-03-24 13:52:17 +01001522 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001523{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001524 struct rb_node **p = &root->rb_root.rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001525 struct rb_node *parent = NULL;
1526 struct hist_entry *iter;
1527 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001528 bool leftmost = true;
John Kacur3d1d07e2009-09-28 15:32:55 +02001529
Namhyung Kimaef810e2016-02-25 00:13:34 +09001530 if (symbol_conf.report_hierarchy)
1531 return hists__hierarchy_insert_entry(hists, root, he);
1532
John Kacur3d1d07e2009-09-28 15:32:55 +02001533 while (*p != NULL) {
1534 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001535 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001536
1537 cmp = hist_entry__collapse(iter, he);
1538
1539 if (!cmp) {
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001540 int ret = 0;
1541
Namhyung Kim139c0812012-10-04 21:49:43 +09001542 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001543 if (symbol_conf.cumulate_callchain)
1544 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001545
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001546 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001547 callchain_cursor_reset(&callchain_cursor);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001548 if (callchain_merge(&callchain_cursor,
1549 iter->callchain,
1550 he->callchain) < 0)
1551 ret = -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001552 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001553 hist_entry__delete(he);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001554 return ret;
John Kacur3d1d07e2009-09-28 15:32:55 +02001555 }
1556
1557 if (cmp < 0)
1558 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001559 else {
John Kacur3d1d07e2009-09-28 15:32:55 +02001560 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001561 leftmost = false;
1562 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001563 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001564 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001565
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001566 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001567 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001568 return 1;
John Kacur3d1d07e2009-09-28 15:32:55 +02001569}
1570
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001571struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001572{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001573 struct rb_root_cached *root;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001574
1575 pthread_mutex_lock(&hists->lock);
1576
1577 root = hists->entries_in;
1578 if (++hists->entries_in > &hists->entries_in_array[1])
1579 hists->entries_in = &hists->entries_in_array[0];
1580
1581 pthread_mutex_unlock(&hists->lock);
1582
1583 return root;
1584}
1585
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001586static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1587{
1588 hists__filter_entry_by_dso(hists, he);
1589 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001590 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001591 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001592}
1593
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001594int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001595{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001596 struct rb_root_cached *root;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001597 struct rb_node *next;
1598 struct hist_entry *n;
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001599 int ret;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001600
Jiri Olsa52225032016-05-03 13:54:42 +02001601 if (!hists__has(hists, need_collapse))
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001602 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001603
Namhyung Kim740b97f2014-12-22 13:44:10 +09001604 hists->nr_entries = 0;
1605
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001606 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001607
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001608 next = rb_first_cached(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001609
1610 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001611 if (session_done())
1612 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001613 n = rb_entry(next, struct hist_entry, rb_node_in);
1614 next = rb_next(&n->rb_node_in);
1615
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001616 rb_erase_cached(&n->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001617 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1618 if (ret < 0)
1619 return -1;
1620
1621 if (ret) {
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001622 /*
1623 * If it wasn't combined with one of the entries already
1624 * collapsed, we need to apply the filters that may have
1625 * been set by, say, the hist_browser.
1626 */
1627 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001628 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001629 if (prog)
1630 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001631 }
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001632 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001633}
1634
Jiri Olsa722ddfd2019-11-05 00:27:11 +01001635static int64_t hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001636{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001637 struct hists *hists = a->hists;
Namhyung Kim043ca3892014-03-03 14:18:00 +09001638 struct perf_hpp_fmt *fmt;
1639 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001640
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001641 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001642 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001643 continue;
1644
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001645 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001646 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001647 break;
1648 }
1649
Namhyung Kim043ca3892014-03-03 14:18:00 +09001650 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001651}
1652
Namhyung Kim9283ba92014-04-24 16:37:26 +09001653static void hists__reset_filter_stats(struct hists *hists)
1654{
1655 hists->nr_non_filtered_entries = 0;
1656 hists->stats.total_non_filtered_period = 0;
1657}
1658
1659void hists__reset_stats(struct hists *hists)
1660{
1661 hists->nr_entries = 0;
1662 hists->stats.total_period = 0;
1663
1664 hists__reset_filter_stats(hists);
1665}
1666
1667static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1668{
1669 hists->nr_non_filtered_entries++;
1670 hists->stats.total_non_filtered_period += h->stat.period;
1671}
1672
1673void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1674{
1675 if (!h->filtered)
1676 hists__inc_filter_stats(hists, h);
1677
1678 hists->nr_entries++;
1679 hists->stats.total_period += h->stat.period;
1680}
1681
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001682static void hierarchy_recalc_total_periods(struct hists *hists)
1683{
1684 struct rb_node *node;
1685 struct hist_entry *he;
1686
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001687 node = rb_first_cached(&hists->entries);
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001688
1689 hists->stats.total_period = 0;
1690 hists->stats.total_non_filtered_period = 0;
1691
1692 /*
1693 * recalculate total period using top-level entries only
1694 * since lower level entries only see non-filtered entries
1695 * but upper level entries have sum of both entries.
1696 */
1697 while (node) {
1698 he = rb_entry(node, struct hist_entry, rb_node);
1699 node = rb_next(node);
1700
1701 hists->stats.total_period += he->stat.period;
1702 if (!he->filtered)
1703 hists->stats.total_non_filtered_period += he->stat.period;
1704 }
1705}
1706
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001707static void hierarchy_insert_output_entry(struct rb_root_cached *root,
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001708 struct hist_entry *he)
1709{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001710 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001711 struct rb_node *parent = NULL;
1712 struct hist_entry *iter;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001713 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001714 bool leftmost = true;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001715
1716 while (*p != NULL) {
1717 parent = *p;
1718 iter = rb_entry(parent, struct hist_entry, rb_node);
1719
1720 if (hist_entry__sort(he, iter) > 0)
1721 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001722 else {
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001723 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001724 leftmost = false;
1725 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001726 }
1727
1728 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001729 rb_insert_color_cached(&he->rb_node, root, leftmost);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001730
1731 /* update column width of dynamic entry */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001732 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1733 if (perf_hpp__is_dynamic_entry(fmt))
1734 fmt->sort(fmt, he, NULL);
1735 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001736}
1737
1738static void hists__hierarchy_output_resort(struct hists *hists,
1739 struct ui_progress *prog,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001740 struct rb_root_cached *root_in,
1741 struct rb_root_cached *root_out,
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001742 u64 min_callchain_hits,
1743 bool use_callchain)
1744{
1745 struct rb_node *node;
1746 struct hist_entry *he;
1747
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001748 *root_out = RB_ROOT_CACHED;
1749 node = rb_first_cached(root_in);
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001750
1751 while (node) {
1752 he = rb_entry(node, struct hist_entry, rb_node_in);
1753 node = rb_next(node);
1754
1755 hierarchy_insert_output_entry(root_out, he);
1756
1757 if (prog)
1758 ui_progress__update(prog, 1);
1759
Namhyung Kimc72ab442016-11-08 22:08:33 +09001760 hists->nr_entries++;
1761 if (!he->filtered) {
1762 hists->nr_non_filtered_entries++;
1763 hists__calc_col_len(hists, he);
1764 }
1765
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001766 if (!he->leaf) {
1767 hists__hierarchy_output_resort(hists, prog,
1768 &he->hroot_in,
1769 &he->hroot_out,
1770 min_callchain_hits,
1771 use_callchain);
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001772 continue;
1773 }
1774
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001775 if (!use_callchain)
1776 continue;
1777
1778 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1779 u64 total = he->stat.period;
1780
1781 if (symbol_conf.cumulate_callchain)
1782 total = he->stat_acc->period;
1783
1784 min_callchain_hits = total * (callchain_param.min_percent / 100);
1785 }
1786
1787 callchain_param.sort(&he->sorted_chain, he->callchain,
1788 min_callchain_hits, &callchain_param);
1789 }
1790}
1791
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001792static void __hists__insert_output_entry(struct rb_root_cached *entries,
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001793 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001794 u64 min_callchain_hits,
1795 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001796{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001797 struct rb_node **p = &entries->rb_root.rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001798 struct rb_node *parent = NULL;
1799 struct hist_entry *iter;
Namhyung Kimabab5e72016-02-27 03:52:47 +09001800 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001801 bool leftmost = true;
John Kacur3d1d07e2009-09-28 15:32:55 +02001802
Namhyung Kim744070e2016-01-28 00:40:48 +09001803 if (use_callchain) {
1804 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1805 u64 total = he->stat.period;
1806
1807 if (symbol_conf.cumulate_callchain)
1808 total = he->stat_acc->period;
1809
1810 min_callchain_hits = total * (callchain_param.min_percent / 100);
1811 }
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001812 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001813 min_callchain_hits, &callchain_param);
Namhyung Kim744070e2016-01-28 00:40:48 +09001814 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001815
1816 while (*p != NULL) {
1817 parent = *p;
1818 iter = rb_entry(parent, struct hist_entry, rb_node);
1819
Namhyung Kim043ca3892014-03-03 14:18:00 +09001820 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001821 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001822 else {
John Kacur3d1d07e2009-09-28 15:32:55 +02001823 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001824 leftmost = false;
1825 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001826 }
1827
1828 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001829 rb_insert_color_cached(&he->rb_node, entries, leftmost);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001830
1831 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1832 if (perf_hpp__is_dynamic_entry(fmt) &&
1833 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1834 fmt->sort(fmt, he, NULL); /* update column width */
1835 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001836}
1837
Jiri Olsa01441af2016-01-18 10:23:59 +01001838static void output_resort(struct hists *hists, struct ui_progress *prog,
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001839 bool use_callchain, hists__resort_cb_t cb,
1840 void *cb_arg)
John Kacur3d1d07e2009-09-28 15:32:55 +02001841{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001842 struct rb_root_cached *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001843 struct rb_node *next;
1844 struct hist_entry *n;
Namhyung Kim467ef102016-02-16 23:08:19 +09001845 u64 callchain_total;
John Kacur3d1d07e2009-09-28 15:32:55 +02001846 u64 min_callchain_hits;
1847
Namhyung Kim467ef102016-02-16 23:08:19 +09001848 callchain_total = hists->callchain_period;
1849 if (symbol_conf.filter_relative)
1850 callchain_total = hists->callchain_non_filtered_period;
1851
1852 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001853
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001854 hists__reset_stats(hists);
1855 hists__reset_col_len(hists);
1856
1857 if (symbol_conf.report_hierarchy) {
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001858 hists__hierarchy_output_resort(hists, prog,
1859 &hists->entries_collapsed,
1860 &hists->entries,
1861 min_callchain_hits,
1862 use_callchain);
1863 hierarchy_recalc_total_periods(hists);
1864 return;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001865 }
1866
Jiri Olsa52225032016-05-03 13:54:42 +02001867 if (hists__has(hists, need_collapse))
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001868 root = &hists->entries_collapsed;
1869 else
1870 root = hists->entries_in;
1871
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001872 next = rb_first_cached(root);
1873 hists->entries = RB_ROOT_CACHED;
John Kacur3d1d07e2009-09-28 15:32:55 +02001874
1875 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001876 n = rb_entry(next, struct hist_entry, rb_node_in);
1877 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001878
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001879 if (cb && cb(n, cb_arg))
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001880 continue;
1881
Kan Liangf9db0d02015-08-11 06:30:48 -04001882 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001883 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001884
1885 if (!n->filtered)
1886 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001887
1888 if (prog)
1889 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001890 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001891}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001892
Jiri Olsa32dcd022019-07-21 13:23:51 +02001893void perf_evsel__output_resort_cb(struct evsel *evsel, struct ui_progress *prog,
Jiri Olsa57496182019-02-04 15:18:07 +01001894 hists__resort_cb_t cb, void *cb_arg)
Jiri Olsa01441af2016-01-18 10:23:59 +01001895{
Jiri Olsa01441af2016-01-18 10:23:59 +01001896 bool use_callchain;
1897
1898 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -03001899 use_callchain = evsel__has_callchain(evsel);
Jiri Olsa01441af2016-01-18 10:23:59 +01001900 else
1901 use_callchain = symbol_conf.use_callchain;
1902
Jin Yaob49a8212017-05-08 18:43:02 +08001903 use_callchain |= symbol_conf.show_branchflag_count;
1904
Jiri Olsa57496182019-02-04 15:18:07 +01001905 output_resort(evsel__hists(evsel), prog, use_callchain, cb, cb_arg);
1906}
1907
Jiri Olsa32dcd022019-07-21 13:23:51 +02001908void perf_evsel__output_resort(struct evsel *evsel, struct ui_progress *prog)
Jiri Olsa57496182019-02-04 15:18:07 +01001909{
1910 return perf_evsel__output_resort_cb(evsel, prog, NULL, NULL);
Jiri Olsa452ce032016-01-18 10:24:00 +01001911}
1912
1913void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1914{
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001915 output_resort(hists, prog, symbol_conf.use_callchain, NULL, NULL);
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001916}
1917
1918void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1919 hists__resort_cb_t cb)
1920{
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001921 output_resort(hists, prog, symbol_conf.use_callchain, cb, NULL);
Jiri Olsa01441af2016-01-18 10:23:59 +01001922}
1923
Namhyung Kim8c018722016-02-25 00:13:36 +09001924static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1925{
1926 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1927 return false;
1928
1929 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1930 return true;
1931
1932 return false;
1933}
1934
1935struct rb_node *rb_hierarchy_last(struct rb_node *node)
1936{
1937 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1938
1939 while (can_goto_child(he, HMD_NORMAL)) {
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001940 node = rb_last(&he->hroot_out.rb_root);
Namhyung Kim8c018722016-02-25 00:13:36 +09001941 he = rb_entry(node, struct hist_entry, rb_node);
1942 }
1943 return node;
1944}
1945
1946struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1947{
1948 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1949
1950 if (can_goto_child(he, hmd))
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001951 node = rb_first_cached(&he->hroot_out);
Namhyung Kim8c018722016-02-25 00:13:36 +09001952 else
1953 node = rb_next(node);
1954
1955 while (node == NULL) {
1956 he = he->parent_he;
1957 if (he == NULL)
1958 break;
1959
1960 node = rb_next(&he->rb_node);
1961 }
1962 return node;
1963}
1964
1965struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1966{
1967 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1968
1969 node = rb_prev(node);
1970 if (node)
1971 return rb_hierarchy_last(node);
1972
1973 he = he->parent_he;
1974 if (he == NULL)
1975 return NULL;
1976
1977 return &he->rb_node;
1978}
1979
Namhyung Kima7b58952016-02-26 21:13:16 +09001980bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1981{
1982 struct rb_node *node;
1983 struct hist_entry *child;
1984 float percent;
1985
1986 if (he->leaf)
1987 return false;
1988
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001989 node = rb_first_cached(&he->hroot_out);
Namhyung Kima7b58952016-02-26 21:13:16 +09001990 child = rb_entry(node, struct hist_entry, rb_node);
1991
1992 while (node && child->filtered) {
1993 node = rb_next(node);
1994 child = rb_entry(node, struct hist_entry, rb_node);
1995 }
1996
1997 if (node)
1998 percent = hist_entry__get_percent_limit(child);
1999 else
2000 percent = 0;
2001
2002 return node && percent >= limit;
2003}
2004
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002005static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002006 enum hist_filter filter)
2007{
2008 h->filtered &= ~(1 << filter);
Namhyung Kim155e9af2016-02-25 00:13:38 +09002009
2010 if (symbol_conf.report_hierarchy) {
2011 struct hist_entry *parent = h->parent_he;
2012
2013 while (parent) {
2014 he_stat__add_stat(&parent->stat, &h->stat);
2015
2016 parent->filtered &= ~(1 << filter);
2017
2018 if (parent->filtered)
2019 goto next;
2020
2021 /* force fold unfiltered entry for simplicity */
2022 parent->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09002023 parent->has_no_entry = false;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002024 parent->row_offset = 0;
2025 parent->nr_rows = 0;
2026next:
2027 parent = parent->parent_he;
2028 }
2029 }
2030
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002031 if (h->filtered)
2032 return;
2033
Namhyung Kim87e90f42014-04-24 16:44:16 +09002034 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09002035 h->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09002036 h->has_no_entry = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03002037 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08002038 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09002039
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09002040 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002041
Namhyung Kim9283ba92014-04-24 16:37:26 +09002042 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002043 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002044}
2045
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02002046
2047static bool hists__filter_entry_by_dso(struct hists *hists,
2048 struct hist_entry *he)
2049{
2050 if (hists->dso_filter != NULL &&
2051 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
2052 he->filtered |= (1 << HIST_FILTER__DSO);
2053 return true;
2054 }
2055
2056 return false;
2057}
2058
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02002059static bool hists__filter_entry_by_thread(struct hists *hists,
2060 struct hist_entry *he)
2061{
2062 if (hists->thread_filter != NULL &&
2063 he->thread != hists->thread_filter) {
2064 he->filtered |= (1 << HIST_FILTER__THREAD);
2065 return true;
2066 }
2067
2068 return false;
2069}
2070
Namhyung Kime94d53e2012-03-16 17:50:51 +09002071static bool hists__filter_entry_by_symbol(struct hists *hists,
2072 struct hist_entry *he)
2073{
2074 if (hists->symbol_filter_str != NULL &&
2075 (!he->ms.sym || strstr(he->ms.sym->name,
2076 hists->symbol_filter_str) == NULL)) {
2077 he->filtered |= (1 << HIST_FILTER__SYMBOL);
2078 return true;
2079 }
2080
2081 return false;
2082}
2083
Kan Liang21394d92015-09-04 10:45:44 -04002084static bool hists__filter_entry_by_socket(struct hists *hists,
2085 struct hist_entry *he)
2086{
2087 if ((hists->socket_filter > -1) &&
2088 (he->socket != hists->socket_filter)) {
2089 he->filtered |= (1 << HIST_FILTER__SOCKET);
2090 return true;
2091 }
2092
2093 return false;
2094}
2095
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002096typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
2097
2098static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04002099{
2100 struct rb_node *nd;
2101
2102 hists->stats.nr_non_filtered_samples = 0;
2103
2104 hists__reset_filter_stats(hists);
2105 hists__reset_col_len(hists);
2106
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002107 for (nd = rb_first_cached(&hists->entries); nd; nd = rb_next(nd)) {
Kan Liang84734b02015-09-04 10:45:45 -04002108 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2109
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002110 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04002111 continue;
2112
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002113 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04002114 }
2115}
2116
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002117static void resort_filtered_entry(struct rb_root_cached *root,
2118 struct hist_entry *he)
Namhyung Kim70642852016-02-25 00:13:39 +09002119{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002120 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kim70642852016-02-25 00:13:39 +09002121 struct rb_node *parent = NULL;
2122 struct hist_entry *iter;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002123 struct rb_root_cached new_root = RB_ROOT_CACHED;
Namhyung Kim70642852016-02-25 00:13:39 +09002124 struct rb_node *nd;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002125 bool leftmost = true;
Namhyung Kim70642852016-02-25 00:13:39 +09002126
2127 while (*p != NULL) {
2128 parent = *p;
2129 iter = rb_entry(parent, struct hist_entry, rb_node);
2130
2131 if (hist_entry__sort(he, iter) > 0)
2132 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002133 else {
Namhyung Kim70642852016-02-25 00:13:39 +09002134 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002135 leftmost = false;
2136 }
Namhyung Kim70642852016-02-25 00:13:39 +09002137 }
2138
2139 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002140 rb_insert_color_cached(&he->rb_node, root, leftmost);
Namhyung Kim70642852016-02-25 00:13:39 +09002141
2142 if (he->leaf || he->filtered)
2143 return;
2144
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002145 nd = rb_first_cached(&he->hroot_out);
Namhyung Kim70642852016-02-25 00:13:39 +09002146 while (nd) {
2147 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2148
2149 nd = rb_next(nd);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002150 rb_erase_cached(&h->rb_node, &he->hroot_out);
Namhyung Kim70642852016-02-25 00:13:39 +09002151
2152 resort_filtered_entry(&new_root, h);
2153 }
2154
2155 he->hroot_out = new_root;
2156}
2157
Namhyung Kim155e9af2016-02-25 00:13:38 +09002158static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2159{
2160 struct rb_node *nd;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002161 struct rb_root_cached new_root = RB_ROOT_CACHED;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002162
2163 hists->stats.nr_non_filtered_samples = 0;
2164
2165 hists__reset_filter_stats(hists);
2166 hists__reset_col_len(hists);
2167
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002168 nd = rb_first_cached(&hists->entries);
Namhyung Kim155e9af2016-02-25 00:13:38 +09002169 while (nd) {
2170 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2171 int ret;
2172
2173 ret = hist_entry__filter(h, type, arg);
2174
2175 /*
2176 * case 1. non-matching type
2177 * zero out the period, set filter marker and move to child
2178 */
2179 if (ret < 0) {
2180 memset(&h->stat, 0, sizeof(h->stat));
2181 h->filtered |= (1 << type);
2182
2183 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2184 }
2185 /*
2186 * case 2. matched type (filter out)
2187 * set filter marker and move to next
2188 */
2189 else if (ret == 1) {
2190 h->filtered |= (1 << type);
2191
2192 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2193 }
2194 /*
2195 * case 3. ok (not filtered)
2196 * add period to hists and parents, erase the filter marker
2197 * and move to next sibling
2198 */
2199 else {
2200 hists__remove_entry_filter(hists, h, type);
2201
2202 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2203 }
2204 }
Namhyung Kim70642852016-02-25 00:13:39 +09002205
Namhyung Kimf7fb5382016-03-09 22:47:02 +09002206 hierarchy_recalc_total_periods(hists);
2207
Namhyung Kim70642852016-02-25 00:13:39 +09002208 /*
2209 * resort output after applying a new filter since filter in a lower
2210 * hierarchy can change periods in a upper hierarchy.
2211 */
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002212 nd = rb_first_cached(&hists->entries);
Namhyung Kim70642852016-02-25 00:13:39 +09002213 while (nd) {
2214 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2215
2216 nd = rb_next(nd);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002217 rb_erase_cached(&h->rb_node, &hists->entries);
Namhyung Kim70642852016-02-25 00:13:39 +09002218
2219 resort_filtered_entry(&new_root, h);
2220 }
2221
2222 hists->entries = new_root;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002223}
2224
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002225void hists__filter_by_thread(struct hists *hists)
2226{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002227 if (symbol_conf.report_hierarchy)
2228 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2229 hists->thread_filter);
2230 else
2231 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2232 hists__filter_entry_by_thread);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002233}
2234
2235void hists__filter_by_dso(struct hists *hists)
2236{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002237 if (symbol_conf.report_hierarchy)
2238 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2239 hists->dso_filter);
2240 else
2241 hists__filter_by_type(hists, HIST_FILTER__DSO,
2242 hists__filter_entry_by_dso);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002243}
2244
2245void hists__filter_by_symbol(struct hists *hists)
2246{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002247 if (symbol_conf.report_hierarchy)
2248 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2249 hists->symbol_filter_str);
2250 else
2251 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2252 hists__filter_entry_by_symbol);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002253}
2254
2255void hists__filter_by_socket(struct hists *hists)
2256{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002257 if (symbol_conf.report_hierarchy)
2258 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2259 &hists->socket_filter);
2260 else
2261 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2262 hists__filter_entry_by_socket);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002263}
2264
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002265void events_stats__inc(struct events_stats *stats, u32 type)
2266{
2267 ++stats->nr_events[0];
2268 ++stats->nr_events[type];
2269}
2270
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002271void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002272{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002273 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002274}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002275
Namhyung Kim1844dbc2014-05-28 14:12:18 +09002276void hists__inc_nr_samples(struct hists *hists, bool filtered)
2277{
2278 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2279 if (!filtered)
2280 hists->stats.nr_non_filtered_samples++;
2281}
2282
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002283static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2284 struct hist_entry *pair)
2285{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002286 struct rb_root_cached *root;
Namhyung Kimce74f602012-12-10 17:29:55 +09002287 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002288 struct rb_node *parent = NULL;
2289 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07002290 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002291 bool leftmost = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002292
Jiri Olsa52225032016-05-03 13:54:42 +02002293 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002294 root = &hists->entries_collapsed;
2295 else
2296 root = hists->entries_in;
2297
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002298 p = &root->rb_root.rb_node;
Namhyung Kimce74f602012-12-10 17:29:55 +09002299
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002300 while (*p != NULL) {
2301 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09002302 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002303
Namhyung Kimce74f602012-12-10 17:29:55 +09002304 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002305
2306 if (!cmp)
2307 goto out;
2308
2309 if (cmp < 0)
2310 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002311 else {
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002312 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002313 leftmost = false;
2314 }
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002315 }
2316
Namhyung Kima0b51af2012-09-11 13:34:27 +09002317 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002318 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03002319 memset(&he->stat, 0, sizeof(he->stat));
2320 he->hists = hists;
Kan Liang09623d72016-04-24 23:28:09 -07002321 if (symbol_conf.cumulate_callchain)
2322 memset(he->stat_acc, 0, sizeof(he->stat));
Namhyung Kimce74f602012-12-10 17:29:55 +09002323 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002324 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kim62638352014-04-24 16:21:46 +09002325 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01002326 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002327 }
2328out:
2329 return he;
2330}
2331
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002332static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002333 struct rb_root_cached *root,
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002334 struct hist_entry *pair)
2335{
2336 struct rb_node **p;
2337 struct rb_node *parent = NULL;
2338 struct hist_entry *he;
2339 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002340 bool leftmost = true;
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002341
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002342 p = &root->rb_root.rb_node;
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002343 while (*p != NULL) {
2344 int64_t cmp = 0;
2345
2346 parent = *p;
2347 he = rb_entry(parent, struct hist_entry, rb_node_in);
2348
2349 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2350 cmp = fmt->collapse(fmt, he, pair);
2351 if (cmp)
2352 break;
2353 }
2354 if (!cmp)
2355 goto out;
2356
2357 if (cmp < 0)
2358 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002359 else {
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002360 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002361 leftmost = false;
2362 }
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002363 }
2364
2365 he = hist_entry__new(pair, true);
2366 if (he) {
2367 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002368 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002369
2370 he->dummy = true;
2371 he->hists = hists;
2372 memset(&he->stat, 0, sizeof(he->stat));
2373 hists__inc_stats(hists, he);
2374 }
2375out:
2376 return he;
2377}
2378
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002379static struct hist_entry *hists__find_entry(struct hists *hists,
2380 struct hist_entry *he)
2381{
Namhyung Kimce74f602012-12-10 17:29:55 +09002382 struct rb_node *n;
2383
Jiri Olsa52225032016-05-03 13:54:42 +02002384 if (hists__has(hists, need_collapse))
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002385 n = hists->entries_collapsed.rb_root.rb_node;
Namhyung Kimce74f602012-12-10 17:29:55 +09002386 else
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002387 n = hists->entries_in->rb_root.rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002388
2389 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002390 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2391 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002392
2393 if (cmp < 0)
2394 n = n->rb_left;
2395 else if (cmp > 0)
2396 n = n->rb_right;
2397 else
2398 return iter;
2399 }
2400
2401 return NULL;
2402}
2403
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002404static struct hist_entry *hists__find_hierarchy_entry(struct rb_root_cached *root,
Namhyung Kim09034de2016-09-13 16:45:46 +09002405 struct hist_entry *he)
2406{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002407 struct rb_node *n = root->rb_root.rb_node;
Namhyung Kim09034de2016-09-13 16:45:46 +09002408
2409 while (n) {
2410 struct hist_entry *iter;
2411 struct perf_hpp_fmt *fmt;
2412 int64_t cmp = 0;
2413
2414 iter = rb_entry(n, struct hist_entry, rb_node_in);
2415 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2416 cmp = fmt->collapse(fmt, iter, he);
2417 if (cmp)
2418 break;
2419 }
2420
2421 if (cmp < 0)
2422 n = n->rb_left;
2423 else if (cmp > 0)
2424 n = n->rb_right;
2425 else
2426 return iter;
2427 }
2428
2429 return NULL;
2430}
2431
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002432static void hists__match_hierarchy(struct rb_root_cached *leader_root,
2433 struct rb_root_cached *other_root)
Namhyung Kim09034de2016-09-13 16:45:46 +09002434{
2435 struct rb_node *nd;
2436 struct hist_entry *pos, *pair;
2437
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002438 for (nd = rb_first_cached(leader_root); nd; nd = rb_next(nd)) {
Namhyung Kim09034de2016-09-13 16:45:46 +09002439 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2440 pair = hists__find_hierarchy_entry(other_root, pos);
2441
2442 if (pair) {
2443 hist_entry__add_pair(pair, pos);
2444 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2445 }
2446 }
2447}
2448
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002449/*
2450 * Look for pairs to link to the leader buckets (hist_entries):
2451 */
2452void hists__match(struct hists *leader, struct hists *other)
2453{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002454 struct rb_root_cached *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002455 struct rb_node *nd;
Namhyung Kimbe5863b2019-08-28 08:15:55 +09002456 struct hist_entry *pos, *pair;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002457
Namhyung Kim09034de2016-09-13 16:45:46 +09002458 if (symbol_conf.report_hierarchy) {
2459 /* hierarchy report always collapses entries */
2460 return hists__match_hierarchy(&leader->entries_collapsed,
2461 &other->entries_collapsed);
2462 }
2463
Jiri Olsa52225032016-05-03 13:54:42 +02002464 if (hists__has(leader, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002465 root = &leader->entries_collapsed;
2466 else
2467 root = leader->entries_in;
2468
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002469 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002470 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002471 pair = hists__find_entry(other, pos);
2472
Namhyung Kimbe5863b2019-08-28 08:15:55 +09002473 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09002474 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002475 }
2476}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002477
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002478static int hists__link_hierarchy(struct hists *leader_hists,
2479 struct hist_entry *parent,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002480 struct rb_root_cached *leader_root,
2481 struct rb_root_cached *other_root)
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002482{
2483 struct rb_node *nd;
2484 struct hist_entry *pos, *leader;
2485
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002486 for (nd = rb_first_cached(other_root); nd; nd = rb_next(nd)) {
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002487 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2488
2489 if (hist_entry__has_pairs(pos)) {
2490 bool found = false;
2491
2492 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2493 if (leader->hists == leader_hists) {
2494 found = true;
2495 break;
2496 }
2497 }
2498 if (!found)
2499 return -1;
2500 } else {
2501 leader = add_dummy_hierarchy_entry(leader_hists,
2502 leader_root, pos);
2503 if (leader == NULL)
2504 return -1;
2505
2506 /* do not point parent in the pos */
2507 leader->parent_he = parent;
2508
2509 hist_entry__add_pair(pos, leader);
2510 }
2511
2512 if (!pos->leaf) {
2513 if (hists__link_hierarchy(leader_hists, leader,
2514 &leader->hroot_in,
2515 &pos->hroot_in) < 0)
2516 return -1;
2517 }
2518 }
2519 return 0;
2520}
2521
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002522/*
2523 * Look for entries in the other hists that are not present in the leader, if
2524 * we find them, just add a dummy entry on the leader hists, with period=0,
2525 * nr_events=0, to serve as the list header.
2526 */
2527int hists__link(struct hists *leader, struct hists *other)
2528{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002529 struct rb_root_cached *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002530 struct rb_node *nd;
2531 struct hist_entry *pos, *pair;
2532
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002533 if (symbol_conf.report_hierarchy) {
2534 /* hierarchy report always collapses entries */
2535 return hists__link_hierarchy(leader, NULL,
2536 &leader->entries_collapsed,
2537 &other->entries_collapsed);
2538 }
2539
Jiri Olsa52225032016-05-03 13:54:42 +02002540 if (hists__has(other, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002541 root = &other->entries_collapsed;
2542 else
2543 root = other->entries_in;
2544
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002545 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002546 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002547
2548 if (!hist_entry__has_pairs(pos)) {
2549 pair = hists__add_dummy_entry(leader, pos);
2550 if (pair == NULL)
2551 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09002552 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002553 }
2554 }
2555
2556 return 0;
2557}
Namhyung Kimf2148332014-01-14 11:52:48 +09002558
Namhyung Kimbe5863b2019-08-28 08:15:55 +09002559int hists__unlink(struct hists *hists)
2560{
2561 struct rb_root_cached *root;
2562 struct rb_node *nd;
2563 struct hist_entry *pos;
2564
2565 if (hists__has(hists, need_collapse))
2566 root = &hists->entries_collapsed;
2567 else
2568 root = hists->entries_in;
2569
2570 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2571 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2572 list_del_init(&pos->pairs.node);
2573 }
2574
2575 return 0;
2576}
2577
Andi Kleen578499982015-07-18 08:24:49 -07002578void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
Jin Yao7841f402019-11-07 15:47:15 +08002579 struct perf_sample *sample, bool nonany_branch_mode,
2580 u64 *total_cycles)
Andi Kleen578499982015-07-18 08:24:49 -07002581{
2582 struct branch_info *bi;
2583
2584 /* If we have branch cycles always annotate them. */
2585 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2586 int i;
2587
2588 bi = sample__resolve_bstack(sample, al);
2589 if (bi) {
2590 struct addr_map_symbol *prev = NULL;
2591
2592 /*
2593 * Ignore errors, still want to process the
2594 * other entries.
2595 *
2596 * For non standard branch modes always
2597 * force no IPC (prev == NULL)
2598 *
2599 * Note that perf stores branches reversed from
2600 * program order!
2601 */
2602 for (i = bs->nr - 1; i >= 0; i--) {
2603 addr_map_symbol__account_cycles(&bi[i].from,
2604 nonany_branch_mode ? NULL : prev,
2605 bi[i].flags.cycles);
2606 prev = &bi[i].to;
Jin Yao7841f402019-11-07 15:47:15 +08002607
2608 if (total_cycles)
2609 *total_cycles += bi[i].flags.cycles;
Andi Kleen578499982015-07-18 08:24:49 -07002610 }
2611 free(bi);
2612 }
2613 }
2614}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002615
Jiri Olsa63503db2019-07-21 13:23:52 +02002616size_t perf_evlist__fprintf_nr_events(struct evlist *evlist, FILE *fp)
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002617{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002618 struct evsel *pos;
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002619 size_t ret = 0;
2620
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002621 evlist__for_each_entry(evlist, pos) {
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002622 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2623 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2624 }
2625
2626 return ret;
2627}
2628
2629
Namhyung Kimf2148332014-01-14 11:52:48 +09002630u64 hists__total_period(struct hists *hists)
2631{
2632 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2633 hists->stats.total_period;
2634}
Namhyung Kim33db4562014-02-07 12:06:07 +09002635
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002636int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq)
2637{
2638 char unit;
2639 int printed;
2640 const struct dso *dso = hists->dso_filter;
Namhyung Kim7cb10a082019-05-27 15:11:49 +09002641 struct thread *thread = hists->thread_filter;
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002642 int socket_id = hists->socket_filter;
2643 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
2644 u64 nr_events = hists->stats.total_period;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002645 struct evsel *evsel = hists_to_evsel(hists);
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002646 const char *ev_name = perf_evsel__name(evsel);
2647 char buf[512], sample_freq_str[64] = "";
2648 size_t buflen = sizeof(buf);
2649 char ref[30] = " show reference callgraph, ";
2650 bool enable_ref = false;
2651
2652 if (symbol_conf.filter_relative) {
2653 nr_samples = hists->stats.nr_non_filtered_samples;
2654 nr_events = hists->stats.total_non_filtered_period;
2655 }
2656
2657 if (perf_evsel__is_group_event(evsel)) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002658 struct evsel *pos;
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002659
2660 perf_evsel__group_desc(evsel, buf, buflen);
2661 ev_name = buf;
2662
2663 for_each_group_member(pos, evsel) {
2664 struct hists *pos_hists = evsel__hists(pos);
2665
2666 if (symbol_conf.filter_relative) {
2667 nr_samples += pos_hists->stats.nr_non_filtered_samples;
2668 nr_events += pos_hists->stats.total_non_filtered_period;
2669 } else {
2670 nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
2671 nr_events += pos_hists->stats.total_period;
2672 }
2673 }
2674 }
2675
2676 if (symbol_conf.show_ref_callgraph &&
2677 strstr(ev_name, "call-graph=no"))
2678 enable_ref = true;
2679
2680 if (show_freq)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002681 scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->core.attr.sample_freq);
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002682
2683 nr_samples = convert_unit(nr_samples, &unit);
2684 printed = scnprintf(bf, size,
2685 "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64,
Jiri Olsa5643b1a2019-07-21 13:24:46 +02002686 nr_samples, unit, evsel->core.nr_members > 1 ? "s" : "",
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002687 ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events);
2688
2689
2690 if (hists->uid_filter_str)
2691 printed += snprintf(bf + printed, size - printed,
2692 ", UID: %s", hists->uid_filter_str);
2693 if (thread) {
2694 if (hists__has(hists, thread)) {
2695 printed += scnprintf(bf + printed, size - printed,
2696 ", Thread: %s(%d)",
2697 (thread->comm_set ? thread__comm_str(thread) : ""),
2698 thread->tid);
2699 } else {
2700 printed += scnprintf(bf + printed, size - printed,
2701 ", Thread: %s",
2702 (thread->comm_set ? thread__comm_str(thread) : ""));
2703 }
2704 }
2705 if (dso)
2706 printed += scnprintf(bf + printed, size - printed,
2707 ", DSO: %s", dso->short_name);
2708 if (socket_id > -1)
2709 printed += scnprintf(bf + printed, size - printed,
2710 ", Processor Socket: %d", socket_id);
2711
2712 return printed;
2713}
2714
Namhyung Kim33db4562014-02-07 12:06:07 +09002715int parse_filter_percentage(const struct option *opt __maybe_unused,
2716 const char *arg, int unset __maybe_unused)
2717{
2718 if (!strcmp(arg, "relative"))
2719 symbol_conf.filter_relative = true;
2720 else if (!strcmp(arg, "absolute"))
2721 symbol_conf.filter_relative = false;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002722 else {
Colin Ian Kinga596a872017-03-30 10:54:40 +01002723 pr_debug("Invalid percentage: %s\n", arg);
Namhyung Kim33db4562014-02-07 12:06:07 +09002724 return -1;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002725 }
Namhyung Kim33db4562014-02-07 12:06:07 +09002726
2727 return 0;
2728}
Namhyung Kim0b93da12014-01-14 12:02:15 +09002729
2730int perf_hist_config(const char *var, const char *value)
2731{
2732 if (!strcmp(var, "hist.percentage"))
2733 return parse_filter_percentage(NULL, value, 0);
2734
2735 return 0;
2736}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002737
Jiri Olsa5b658552016-01-18 10:24:22 +01002738int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002739{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002740 memset(hists, 0, sizeof(*hists));
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002741 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT_CACHED;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002742 hists->entries_in = &hists->entries_in_array[0];
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002743 hists->entries_collapsed = RB_ROOT_CACHED;
2744 hists->entries = RB_ROOT_CACHED;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002745 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04002746 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01002747 hists->hpp_list = hpp_list;
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002748 INIT_LIST_HEAD(&hists->hpp_formats);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002749 return 0;
2750}
2751
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002752static void hists__delete_remaining_entries(struct rb_root_cached *root)
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002753{
2754 struct rb_node *node;
2755 struct hist_entry *he;
2756
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002757 while (!RB_EMPTY_ROOT(&root->rb_root)) {
2758 node = rb_first_cached(root);
2759 rb_erase_cached(node, root);
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002760
2761 he = rb_entry(node, struct hist_entry, rb_node_in);
2762 hist_entry__delete(he);
2763 }
2764}
2765
2766static void hists__delete_all_entries(struct hists *hists)
2767{
2768 hists__delete_entries(hists);
2769 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2770 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2771 hists__delete_remaining_entries(&hists->entries_collapsed);
2772}
2773
Jiri Olsa32dcd022019-07-21 13:23:51 +02002774static void hists_evsel__exit(struct evsel *evsel)
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002775{
2776 struct hists *hists = evsel__hists(evsel);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002777 struct perf_hpp_fmt *fmt, *pos;
2778 struct perf_hpp_list_node *node, *tmp;
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002779
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002780 hists__delete_all_entries(hists);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002781
2782 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2783 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03002784 list_del_init(&fmt->list);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002785 free(fmt);
2786 }
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03002787 list_del_init(&node->list);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002788 free(node);
2789 }
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002790}
2791
Jiri Olsa32dcd022019-07-21 13:23:51 +02002792static int hists_evsel__init(struct evsel *evsel)
Namhyung Kimfc284be2016-01-07 10:14:10 +01002793{
2794 struct hists *hists = evsel__hists(evsel);
2795
Jiri Olsa5b658552016-01-18 10:24:22 +01002796 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01002797 return 0;
2798}
2799
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002800/*
2801 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2802 * stored in the rbtree...
2803 */
2804
2805int hists__init(void)
2806{
2807 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002808 hists_evsel__init,
2809 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002810 if (err)
2811 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2812
2813 return err;
2814}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01002815
2816void perf_hpp_list__init(struct perf_hpp_list *list)
2817{
2818 INIT_LIST_HEAD(&list->fields);
2819 INIT_LIST_HEAD(&list->sorts);
2820}