blob: e74a5acf66d948ebd424857686c081a43da5ae58 [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 = {
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -0300695 .maps = al->maps,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100696 .map = al->map,
697 .sym = al->sym,
698 },
Jiri Olsa26349582019-03-05 16:25:31 +0100699 .srcline = (char *) al->srcline,
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400700 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400701 .cpu = al->cpu,
702 .cpumode = al->cpumode,
703 .ip = al->addr,
704 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900705 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900706 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900707 .period = sample->period,
708 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900709 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100710 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300711 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300712 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900713 .branch_info = bi,
714 .mem_info = mi,
Jin Yaofe962452019-06-28 17:22:59 +0800715 .block_info = block_info,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900716 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900717 .raw_data = sample->raw_data,
718 .raw_size = sample->raw_size,
Jiri Olsaa5051972016-07-05 08:56:05 +0200719 .ops = ops,
Andi Kleen37239082019-03-11 07:44:54 -0700720 .time = hist_time(sample->time),
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300721 }, *he = hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100722
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300723 if (!hists->has_callchains && he && he->callchain_size != 0)
724 hists->has_callchains = true;
Andi Kleen4968ac82019-03-11 07:44:58 -0700725 if (he && symbol_conf.res_sample)
726 hists__res_sample(he, sample);
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300727 return he;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100728}
729
Jiri Olsaa5051972016-07-05 08:56:05 +0200730struct hist_entry *hists__add_entry(struct hists *hists,
731 struct addr_location *al,
732 struct symbol *sym_parent,
733 struct branch_info *bi,
734 struct mem_info *mi,
735 struct perf_sample *sample,
736 bool sample_self)
737{
Jin Yaofe962452019-06-28 17:22:59 +0800738 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
Jiri Olsaa5051972016-07-05 08:56:05 +0200739 sample, sample_self, NULL);
740}
741
742struct hist_entry *hists__add_entry_ops(struct hists *hists,
743 struct hist_entry_ops *ops,
744 struct addr_location *al,
745 struct symbol *sym_parent,
746 struct branch_info *bi,
747 struct mem_info *mi,
748 struct perf_sample *sample,
749 bool sample_self)
750{
Jin Yaofe962452019-06-28 17:22:59 +0800751 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
Jiri Olsaa5051972016-07-05 08:56:05 +0200752 sample, sample_self, ops);
753}
754
Jin Yaofe962452019-06-28 17:22:59 +0800755struct hist_entry *hists__add_entry_block(struct hists *hists,
756 struct addr_location *al,
757 struct block_info *block_info)
758{
759 struct hist_entry entry = {
760 .block_info = block_info,
761 .hists = hists,
Jin Yaob65a7d32019-11-07 15:47:16 +0800762 .ms = {
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -0300763 .maps = al->maps,
Jin Yaob65a7d32019-11-07 15:47:16 +0800764 .map = al->map,
765 .sym = al->sym,
766 },
Jin Yaofe962452019-06-28 17:22:59 +0800767 }, *he = hists__findnew_entry(hists, &entry, al, false);
768
769 return he;
770}
771
Namhyung Kim69bcb012013-10-30 09:40:34 +0900772static int
773iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
774 struct addr_location *al __maybe_unused)
775{
776 return 0;
777}
778
779static int
780iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
781 struct addr_location *al __maybe_unused)
782{
783 return 0;
784}
785
786static int
787iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
788{
789 struct perf_sample *sample = iter->sample;
790 struct mem_info *mi;
791
792 mi = sample__resolve_mem(sample, al);
793 if (mi == NULL)
794 return -ENOMEM;
795
796 iter->priv = mi;
797 return 0;
798}
799
800static int
801iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
802{
803 u64 cost;
804 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300805 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900806 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900807 struct hist_entry *he;
808
809 if (mi == NULL)
810 return -EINVAL;
811
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900812 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900813 if (!cost)
814 cost = 1;
815
816 /*
817 * must pass period=weight in order to get the correct
818 * sorting from hists__collapse_resort() which is solely
819 * based on periods. We want sorting be done on nr_events * weight
820 * and this is indirectly achieved by passing period=weight here
821 * and the he_stat__add_period() function.
822 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900823 sample->period = cost;
824
Jiri Olsa0102ef32016-06-14 20:19:21 +0200825 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
826 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900827 if (!he)
828 return -ENOMEM;
829
830 iter->he = he;
831 return 0;
832}
833
834static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900835iter_finish_mem_entry(struct hist_entry_iter *iter,
836 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900837{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200838 struct evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300839 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900840 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900841 int err = -EINVAL;
842
843 if (he == NULL)
844 goto out;
845
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300846 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900847
848 err = hist_entry__append_callchain(he, iter->sample);
849
850out:
851 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300852 * We don't need to free iter->priv (mem_info) here since the mem info
853 * was either already freed in hists__findnew_entry() or passed to a
854 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900855 */
856 iter->priv = NULL;
857
858 iter->he = NULL;
859 return err;
860}
861
862static int
863iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
864{
865 struct branch_info *bi;
866 struct perf_sample *sample = iter->sample;
867
868 bi = sample__resolve_bstack(sample, al);
869 if (!bi)
870 return -ENOMEM;
871
872 iter->curr = 0;
873 iter->total = sample->branch_stack->nr;
874
875 iter->priv = bi;
876 return 0;
877}
878
879static int
Jin Yao2d78b182017-07-18 20:13:14 +0800880iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900881 struct addr_location *al __maybe_unused)
882{
883 return 0;
884}
885
886static int
887iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
888{
889 struct branch_info *bi = iter->priv;
890 int i = iter->curr;
891
892 if (bi == NULL)
893 return 0;
894
895 if (iter->curr >= iter->total)
896 return 0;
897
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -0300898 al->maps = bi[i].to.ms.maps;
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300899 al->map = bi[i].to.ms.map;
900 al->sym = bi[i].to.ms.sym;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900901 al->addr = bi[i].to.addr;
902 return 1;
903}
904
905static int
906iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
907{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900908 struct branch_info *bi;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200909 struct evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300910 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900911 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900912 struct hist_entry *he = NULL;
913 int i = iter->curr;
914 int err = 0;
915
916 bi = iter->priv;
917
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300918 if (iter->hide_unresolved && !(bi[i].from.ms.sym && bi[i].to.ms.sym))
Namhyung Kim69bcb012013-10-30 09:40:34 +0900919 goto out;
920
921 /*
922 * The report shows the percentage of total branches captured
923 * and not events sampled. Thus we use a pseudo period of 1.
924 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900925 sample->period = 1;
926 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
927
Jiri Olsa0102ef32016-06-14 20:19:21 +0200928 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
929 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900930 if (he == NULL)
931 return -ENOMEM;
932
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300933 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900934
935out:
936 iter->he = he;
937 iter->curr++;
938 return err;
939}
940
941static int
942iter_finish_branch_entry(struct hist_entry_iter *iter,
943 struct addr_location *al __maybe_unused)
944{
945 zfree(&iter->priv);
946 iter->he = NULL;
947
948 return iter->curr >= iter->total ? 0 : -1;
949}
950
951static int
952iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
953 struct addr_location *al __maybe_unused)
954{
955 return 0;
956}
957
958static int
959iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
960{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200961 struct evsel *evsel = iter->evsel;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900962 struct perf_sample *sample = iter->sample;
963 struct hist_entry *he;
964
Jiri Olsa0102ef32016-06-14 20:19:21 +0200965 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
966 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900967 if (he == NULL)
968 return -ENOMEM;
969
970 iter->he = he;
971 return 0;
972}
973
974static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900975iter_finish_normal_entry(struct hist_entry_iter *iter,
976 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900977{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900978 struct hist_entry *he = iter->he;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200979 struct evsel *evsel = iter->evsel;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900980 struct perf_sample *sample = iter->sample;
981
982 if (he == NULL)
983 return 0;
984
985 iter->he = NULL;
986
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300987 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900988
989 return hist_entry__append_callchain(he, sample);
990}
991
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900992static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300993iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900994 struct addr_location *al __maybe_unused)
995{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900996 struct hist_entry **he_cache;
997
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900998 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900999
1000 /*
1001 * This is for detecting cycles or recursions so that they're
1002 * cumulated only one time to prevent entries more than 100%
1003 * overhead.
1004 */
Jiri Olsae3ebaa42018-02-16 13:36:19 +01001005 he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001006 if (he_cache == NULL)
1007 return -ENOMEM;
1008
1009 iter->priv = he_cache;
1010 iter->curr = 0;
1011
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001012 return 0;
1013}
1014
1015static int
1016iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
1017 struct addr_location *al)
1018{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001019 struct evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001020 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001021 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001022 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001023 struct hist_entry *he;
1024 int err = 0;
1025
Jiri Olsa0102ef32016-06-14 20:19:21 +02001026 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
1027 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001028 if (he == NULL)
1029 return -ENOMEM;
1030
1031 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001032 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001033
Namhyung Kim82aa0192014-12-22 13:44:14 +09001034 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +09001035
1036 /*
1037 * We need to re-initialize the cursor since callchain_append()
1038 * advanced the cursor to the end.
1039 */
1040 callchain_cursor_commit(&callchain_cursor);
1041
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001042 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001043
1044 return err;
1045}
1046
1047static int
1048iter_next_cumulative_entry(struct hist_entry_iter *iter,
1049 struct addr_location *al)
1050{
1051 struct callchain_cursor_node *node;
1052
1053 node = callchain_cursor_current(&callchain_cursor);
1054 if (node == NULL)
1055 return 0;
1056
Namhyung Kimc7405d82013-10-31 13:58:30 +09001057 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001058}
1059
1060static int
1061iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
1062 struct addr_location *al)
1063{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001064 struct evsel *evsel = iter->evsel;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001065 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001066 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001067 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001068 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -03001069 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001070 .cpu = al->cpu,
1071 .thread = al->thread,
1072 .comm = thread__comm(al->thread),
1073 .ip = al->addr,
1074 .ms = {
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -03001075 .maps = al->maps,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001076 .map = al->map,
1077 .sym = al->sym,
1078 },
Jiri Olsa26349582019-03-05 16:25:31 +01001079 .srcline = (char *) al->srcline,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001080 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +09001081 .raw_data = sample->raw_data,
1082 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001083 };
1084 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +09001085 struct callchain_cursor cursor;
1086
1087 callchain_cursor_snapshot(&cursor, &callchain_cursor);
1088
1089 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001090
1091 /*
1092 * Check if there's duplicate entries in the callchain.
1093 * It's possible that it has cycles or recursive calls.
1094 */
1095 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001096 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
1097 /* to avoid calling callback function */
1098 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001099 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001100 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001101 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001102
Jiri Olsa0102ef32016-06-14 20:19:21 +02001103 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
1104 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001105 if (he == NULL)
1106 return -ENOMEM;
1107
1108 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001109 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001110
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001111 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
Namhyung Kim82aa0192014-12-22 13:44:14 +09001112 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001113 return 0;
1114}
1115
1116static int
1117iter_finish_cumulative_entry(struct hist_entry_iter *iter,
1118 struct addr_location *al __maybe_unused)
1119{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001120 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001121 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001122
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001123 return 0;
1124}
1125
Namhyung Kim69bcb012013-10-30 09:40:34 +09001126const struct hist_iter_ops hist_iter_mem = {
1127 .prepare_entry = iter_prepare_mem_entry,
1128 .add_single_entry = iter_add_single_mem_entry,
1129 .next_entry = iter_next_nop_entry,
1130 .add_next_entry = iter_add_next_nop_entry,
1131 .finish_entry = iter_finish_mem_entry,
1132};
1133
1134const struct hist_iter_ops hist_iter_branch = {
1135 .prepare_entry = iter_prepare_branch_entry,
1136 .add_single_entry = iter_add_single_branch_entry,
1137 .next_entry = iter_next_branch_entry,
1138 .add_next_entry = iter_add_next_branch_entry,
1139 .finish_entry = iter_finish_branch_entry,
1140};
1141
1142const struct hist_iter_ops hist_iter_normal = {
1143 .prepare_entry = iter_prepare_normal_entry,
1144 .add_single_entry = iter_add_single_normal_entry,
1145 .next_entry = iter_next_nop_entry,
1146 .add_next_entry = iter_add_next_nop_entry,
1147 .finish_entry = iter_finish_normal_entry,
1148};
1149
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001150const struct hist_iter_ops hist_iter_cumulative = {
1151 .prepare_entry = iter_prepare_cumulative_entry,
1152 .add_single_entry = iter_add_single_cumulative_entry,
1153 .next_entry = iter_next_cumulative_entry,
1154 .add_next_entry = iter_add_next_cumulative_entry,
1155 .finish_entry = iter_finish_cumulative_entry,
1156};
1157
Namhyung Kim69bcb012013-10-30 09:40:34 +09001158int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001159 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +09001160{
1161 int err, err2;
Krister Johansen9c68ae92017-01-05 22:23:31 -08001162 struct map *alm = NULL;
1163
Arnaldo Carvalho de Melo362379a2018-05-24 11:20:39 -03001164 if (al)
Krister Johansen9c68ae92017-01-05 22:23:31 -08001165 alm = map__get(al->map);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001166
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001167 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
Namhyung Kim063bd932015-05-19 17:04:10 +09001168 iter->evsel, al, max_stack_depth);
Changbin Ducb6186a2019-03-16 16:05:49 +08001169 if (err) {
1170 map__put(alm);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001171 return err;
Changbin Ducb6186a2019-03-16 16:05:49 +08001172 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001173
Namhyung Kim69bcb012013-10-30 09:40:34 +09001174 err = iter->ops->prepare_entry(iter, al);
1175 if (err)
1176 goto out;
1177
1178 err = iter->ops->add_single_entry(iter, al);
1179 if (err)
1180 goto out;
1181
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001182 if (iter->he && iter->add_entry_cb) {
1183 err = iter->add_entry_cb(iter, al, true, arg);
1184 if (err)
1185 goto out;
1186 }
1187
Namhyung Kim69bcb012013-10-30 09:40:34 +09001188 while (iter->ops->next_entry(iter, al)) {
1189 err = iter->ops->add_next_entry(iter, al);
1190 if (err)
1191 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001192
1193 if (iter->he && iter->add_entry_cb) {
1194 err = iter->add_entry_cb(iter, al, false, arg);
1195 if (err)
1196 goto out;
1197 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001198 }
1199
1200out:
1201 err2 = iter->ops->finish_entry(iter, al);
1202 if (!err)
1203 err = err2;
1204
Krister Johansen9c68ae92017-01-05 22:23:31 -08001205 map__put(alm);
1206
Namhyung Kim69bcb012013-10-30 09:40:34 +09001207 return err;
1208}
1209
John Kacur3d1d07e2009-09-28 15:32:55 +02001210int64_t
1211hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1212{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001213 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001214 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001215 int64_t cmp = 0;
1216
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001217 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001218 if (perf_hpp__is_dynamic_entry(fmt) &&
1219 !perf_hpp__defined_dynamic_entry(fmt, hists))
1220 continue;
1221
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001222 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001223 if (cmp)
1224 break;
1225 }
1226
1227 return cmp;
1228}
1229
1230int64_t
1231hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1232{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001233 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001234 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001235 int64_t cmp = 0;
1236
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001237 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001238 if (perf_hpp__is_dynamic_entry(fmt) &&
1239 !perf_hpp__defined_dynamic_entry(fmt, hists))
1240 continue;
1241
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001242 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001243 if (cmp)
1244 break;
1245 }
1246
1247 return cmp;
1248}
1249
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001250void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001251{
Jiri Olsaf542e762016-07-05 08:56:04 +02001252 struct hist_entry_ops *ops = he->ops;
1253
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001254 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001255 map__zput(he->ms.map);
1256
1257 if (he->branch_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -03001258 map__zput(he->branch_info->from.ms.map);
1259 map__zput(he->branch_info->to.ms.map);
Andi Kleen508be0d2016-05-20 13:15:08 -07001260 free_srcline(he->branch_info->srcline_from);
1261 free_srcline(he->branch_info->srcline_to);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001262 zfree(&he->branch_info);
1263 }
1264
1265 if (he->mem_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -03001266 map__zput(he->mem_info->iaddr.ms.map);
1267 map__zput(he->mem_info->daddr.ms.map);
Jiri Olsa9f874982018-03-07 16:50:06 +01001268 mem_info__zput(he->mem_info);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001269 }
1270
Jin Yao99150a12019-06-28 17:23:01 +08001271 if (he->block_info)
1272 block_info__zput(he->block_info);
1273
Andi Kleen4968ac82019-03-11 07:44:58 -07001274 zfree(&he->res_samples);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001275 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +09001276 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -07001277 if (he->srcfile && he->srcfile[0])
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001278 zfree(&he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001279 free_callchain(he->callchain);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001280 zfree(&he->trace_output);
1281 zfree(&he->raw_data);
Jiri Olsaf542e762016-07-05 08:56:04 +02001282 ops->free(he);
John Kacur3d1d07e2009-09-28 15:32:55 +02001283}
1284
1285/*
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001286 * If this is not the last column, then we need to pad it according to the
Ingo Molnaradba1632018-12-03 11:22:00 +01001287 * pre-calculated max length for this column, otherwise don't bother adding
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001288 * spaces because that would break viewing this with, for instance, 'less',
1289 * that would show tons of trailing spaces when a long C++ demangled method
1290 * names is sampled.
1291*/
1292int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1293 struct perf_hpp_fmt *fmt, int printed)
1294{
1295 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
Jiri Olsada1b0402016-06-14 20:19:20 +02001296 const int width = fmt->width(fmt, hpp, he->hists);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001297 if (printed < width) {
1298 advance_hpp(hpp, printed);
1299 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1300 }
1301 }
1302
1303 return printed;
1304}
1305
1306/*
John Kacur3d1d07e2009-09-28 15:32:55 +02001307 * collapse the histogram
1308 */
1309
Namhyung Kimaef810e2016-02-25 00:13:34 +09001310static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
Namhyung Kimaec13a72016-03-09 22:46:58 +09001311static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1312 enum hist_filter type);
1313
1314typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1315
1316static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1317{
1318 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1319}
1320
1321static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1322 enum hist_filter type,
1323 fmt_chk_fn check)
1324{
1325 struct perf_hpp_fmt *fmt;
1326 bool type_match = false;
1327 struct hist_entry *parent = he->parent_he;
1328
1329 switch (type) {
1330 case HIST_FILTER__THREAD:
1331 if (symbol_conf.comm_list == NULL &&
1332 symbol_conf.pid_list == NULL &&
1333 symbol_conf.tid_list == NULL)
1334 return;
1335 break;
1336 case HIST_FILTER__DSO:
1337 if (symbol_conf.dso_list == NULL)
1338 return;
1339 break;
1340 case HIST_FILTER__SYMBOL:
1341 if (symbol_conf.sym_list == NULL)
1342 return;
1343 break;
1344 case HIST_FILTER__PARENT:
1345 case HIST_FILTER__GUEST:
1346 case HIST_FILTER__HOST:
1347 case HIST_FILTER__SOCKET:
Jiri Olsa9857b712016-08-17 14:55:23 +02001348 case HIST_FILTER__C2C:
Namhyung Kimaec13a72016-03-09 22:46:58 +09001349 default:
1350 return;
1351 }
1352
1353 /* if it's filtered by own fmt, it has to have filter bits */
1354 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1355 if (check(fmt)) {
1356 type_match = true;
1357 break;
1358 }
1359 }
1360
1361 if (type_match) {
1362 /*
1363 * If the filter is for current level entry, propagate
1364 * filter marker to parents. The marker bit was
1365 * already set by default so it only needs to clear
1366 * non-filtered entries.
1367 */
1368 if (!(he->filtered & (1 << type))) {
1369 while (parent) {
1370 parent->filtered &= ~(1 << type);
1371 parent = parent->parent_he;
1372 }
1373 }
1374 } else {
1375 /*
1376 * If current entry doesn't have matching formats, set
1377 * filter marker for upper level entries. it will be
1378 * cleared if its lower level entries is not filtered.
1379 *
1380 * For lower-level entries, it inherits parent's
1381 * filter bit so that lower level entries of a
1382 * non-filtered entry won't set the filter marker.
1383 */
1384 if (parent == NULL)
1385 he->filtered |= (1 << type);
1386 else
1387 he->filtered |= (parent->filtered & (1 << type));
1388 }
1389}
1390
1391static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1392{
1393 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1394 check_thread_entry);
1395
1396 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1397 perf_hpp__is_dso_entry);
1398
1399 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1400 perf_hpp__is_sym_entry);
1401
1402 hists__apply_filters(he->hists, he);
1403}
Namhyung Kimaef810e2016-02-25 00:13:34 +09001404
1405static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001406 struct rb_root_cached *root,
Namhyung Kimaef810e2016-02-25 00:13:34 +09001407 struct hist_entry *he,
Namhyung Kimaec13a72016-03-09 22:46:58 +09001408 struct hist_entry *parent_he,
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001409 struct perf_hpp_list *hpp_list)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001410{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001411 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001412 struct rb_node *parent = NULL;
1413 struct hist_entry *iter, *new;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001414 struct perf_hpp_fmt *fmt;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001415 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001416 bool leftmost = true;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001417
1418 while (*p != NULL) {
1419 parent = *p;
1420 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1421
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001422 cmp = 0;
1423 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1424 cmp = fmt->collapse(fmt, iter, he);
1425 if (cmp)
1426 break;
1427 }
1428
Namhyung Kimaef810e2016-02-25 00:13:34 +09001429 if (!cmp) {
1430 he_stat__add_stat(&iter->stat, &he->stat);
1431 return iter;
1432 }
1433
1434 if (cmp < 0)
1435 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001436 else {
Namhyung Kimaef810e2016-02-25 00:13:34 +09001437 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001438 leftmost = false;
1439 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001440 }
1441
1442 new = hist_entry__new(he, true);
1443 if (new == NULL)
1444 return NULL;
1445
Namhyung Kimaef810e2016-02-25 00:13:34 +09001446 hists->nr_entries++;
1447
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001448 /* save related format list for output */
1449 new->hpp_list = hpp_list;
Namhyung Kimaec13a72016-03-09 22:46:58 +09001450 new->parent_he = parent_he;
1451
1452 hist_entry__apply_hierarchy_filters(new);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001453
1454 /* some fields are now passed to 'new' */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001455 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1456 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1457 he->trace_output = NULL;
1458 else
1459 new->trace_output = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001460
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001461 if (perf_hpp__is_srcline_entry(fmt))
1462 he->srcline = NULL;
1463 else
1464 new->srcline = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001465
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001466 if (perf_hpp__is_srcfile_entry(fmt))
1467 he->srcfile = NULL;
1468 else
1469 new->srcfile = NULL;
1470 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001471
1472 rb_link_node(&new->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001473 rb_insert_color_cached(&new->rb_node_in, root, leftmost);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001474 return new;
1475}
1476
1477static int hists__hierarchy_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001478 struct rb_root_cached *root,
Namhyung Kimaef810e2016-02-25 00:13:34 +09001479 struct hist_entry *he)
1480{
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001481 struct perf_hpp_list_node *node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001482 struct hist_entry *new_he = NULL;
1483 struct hist_entry *parent = NULL;
1484 int depth = 0;
1485 int ret = 0;
1486
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001487 list_for_each_entry(node, &hists->hpp_formats, list) {
1488 /* skip period (overhead) and elided columns */
1489 if (node->level == 0 || node->skip)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001490 continue;
1491
1492 /* insert copy of 'he' for each fmt into the hierarchy */
Namhyung Kimaec13a72016-03-09 22:46:58 +09001493 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001494 if (new_he == NULL) {
1495 ret = -1;
1496 break;
1497 }
1498
1499 root = &new_he->hroot_in;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001500 new_he->depth = depth++;
1501 parent = new_he;
1502 }
1503
1504 if (new_he) {
1505 new_he->leaf = true;
1506
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001507 if (hist_entry__has_callchains(new_he) &&
1508 symbol_conf.use_callchain) {
Namhyung Kimaef810e2016-02-25 00:13:34 +09001509 callchain_cursor_reset(&callchain_cursor);
1510 if (callchain_merge(&callchain_cursor,
1511 new_he->callchain,
1512 he->callchain) < 0)
1513 ret = -1;
1514 }
1515 }
1516
1517 /* 'he' is no longer used */
1518 hist_entry__delete(he);
1519
1520 /* return 0 (or -1) since it already applied filters */
1521 return ret;
1522}
1523
Jiri Olsa592dac62016-03-24 13:52:17 +01001524static int hists__collapse_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001525 struct rb_root_cached *root,
Jiri Olsa592dac62016-03-24 13:52:17 +01001526 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001527{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001528 struct rb_node **p = &root->rb_root.rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001529 struct rb_node *parent = NULL;
1530 struct hist_entry *iter;
1531 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001532 bool leftmost = true;
John Kacur3d1d07e2009-09-28 15:32:55 +02001533
Namhyung Kimaef810e2016-02-25 00:13:34 +09001534 if (symbol_conf.report_hierarchy)
1535 return hists__hierarchy_insert_entry(hists, root, he);
1536
John Kacur3d1d07e2009-09-28 15:32:55 +02001537 while (*p != NULL) {
1538 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001539 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001540
1541 cmp = hist_entry__collapse(iter, he);
1542
1543 if (!cmp) {
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001544 int ret = 0;
1545
Namhyung Kim139c0812012-10-04 21:49:43 +09001546 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001547 if (symbol_conf.cumulate_callchain)
1548 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001549
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001550 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001551 callchain_cursor_reset(&callchain_cursor);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001552 if (callchain_merge(&callchain_cursor,
1553 iter->callchain,
1554 he->callchain) < 0)
1555 ret = -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001556 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001557 hist_entry__delete(he);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001558 return ret;
John Kacur3d1d07e2009-09-28 15:32:55 +02001559 }
1560
1561 if (cmp < 0)
1562 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001563 else {
John Kacur3d1d07e2009-09-28 15:32:55 +02001564 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001565 leftmost = false;
1566 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001567 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001568 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001569
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001570 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001571 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001572 return 1;
John Kacur3d1d07e2009-09-28 15:32:55 +02001573}
1574
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001575struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001576{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001577 struct rb_root_cached *root;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001578
1579 pthread_mutex_lock(&hists->lock);
1580
1581 root = hists->entries_in;
1582 if (++hists->entries_in > &hists->entries_in_array[1])
1583 hists->entries_in = &hists->entries_in_array[0];
1584
1585 pthread_mutex_unlock(&hists->lock);
1586
1587 return root;
1588}
1589
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001590static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1591{
1592 hists__filter_entry_by_dso(hists, he);
1593 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001594 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001595 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001596}
1597
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001598int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001599{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001600 struct rb_root_cached *root;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001601 struct rb_node *next;
1602 struct hist_entry *n;
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001603 int ret;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001604
Jiri Olsa52225032016-05-03 13:54:42 +02001605 if (!hists__has(hists, need_collapse))
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001606 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001607
Namhyung Kim740b97f2014-12-22 13:44:10 +09001608 hists->nr_entries = 0;
1609
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001610 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001611
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001612 next = rb_first_cached(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001613
1614 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001615 if (session_done())
1616 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001617 n = rb_entry(next, struct hist_entry, rb_node_in);
1618 next = rb_next(&n->rb_node_in);
1619
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001620 rb_erase_cached(&n->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001621 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1622 if (ret < 0)
1623 return -1;
1624
1625 if (ret) {
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001626 /*
1627 * If it wasn't combined with one of the entries already
1628 * collapsed, we need to apply the filters that may have
1629 * been set by, say, the hist_browser.
1630 */
1631 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001632 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001633 if (prog)
1634 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001635 }
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001636 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001637}
1638
Jiri Olsa722ddfd2019-11-05 00:27:11 +01001639static int64_t hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001640{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001641 struct hists *hists = a->hists;
Namhyung Kim043ca3892014-03-03 14:18:00 +09001642 struct perf_hpp_fmt *fmt;
1643 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001644
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001645 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001646 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001647 continue;
1648
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001649 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001650 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001651 break;
1652 }
1653
Namhyung Kim043ca3892014-03-03 14:18:00 +09001654 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001655}
1656
Namhyung Kim9283ba92014-04-24 16:37:26 +09001657static void hists__reset_filter_stats(struct hists *hists)
1658{
1659 hists->nr_non_filtered_entries = 0;
1660 hists->stats.total_non_filtered_period = 0;
1661}
1662
1663void hists__reset_stats(struct hists *hists)
1664{
1665 hists->nr_entries = 0;
1666 hists->stats.total_period = 0;
1667
1668 hists__reset_filter_stats(hists);
1669}
1670
1671static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1672{
1673 hists->nr_non_filtered_entries++;
1674 hists->stats.total_non_filtered_period += h->stat.period;
1675}
1676
1677void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1678{
1679 if (!h->filtered)
1680 hists__inc_filter_stats(hists, h);
1681
1682 hists->nr_entries++;
1683 hists->stats.total_period += h->stat.period;
1684}
1685
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001686static void hierarchy_recalc_total_periods(struct hists *hists)
1687{
1688 struct rb_node *node;
1689 struct hist_entry *he;
1690
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001691 node = rb_first_cached(&hists->entries);
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001692
1693 hists->stats.total_period = 0;
1694 hists->stats.total_non_filtered_period = 0;
1695
1696 /*
1697 * recalculate total period using top-level entries only
1698 * since lower level entries only see non-filtered entries
1699 * but upper level entries have sum of both entries.
1700 */
1701 while (node) {
1702 he = rb_entry(node, struct hist_entry, rb_node);
1703 node = rb_next(node);
1704
1705 hists->stats.total_period += he->stat.period;
1706 if (!he->filtered)
1707 hists->stats.total_non_filtered_period += he->stat.period;
1708 }
1709}
1710
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001711static void hierarchy_insert_output_entry(struct rb_root_cached *root,
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001712 struct hist_entry *he)
1713{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001714 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001715 struct rb_node *parent = NULL;
1716 struct hist_entry *iter;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001717 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001718 bool leftmost = true;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001719
1720 while (*p != NULL) {
1721 parent = *p;
1722 iter = rb_entry(parent, struct hist_entry, rb_node);
1723
1724 if (hist_entry__sort(he, iter) > 0)
1725 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001726 else {
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001727 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001728 leftmost = false;
1729 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001730 }
1731
1732 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001733 rb_insert_color_cached(&he->rb_node, root, leftmost);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001734
1735 /* update column width of dynamic entry */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001736 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1737 if (perf_hpp__is_dynamic_entry(fmt))
1738 fmt->sort(fmt, he, NULL);
1739 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001740}
1741
1742static void hists__hierarchy_output_resort(struct hists *hists,
1743 struct ui_progress *prog,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001744 struct rb_root_cached *root_in,
1745 struct rb_root_cached *root_out,
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001746 u64 min_callchain_hits,
1747 bool use_callchain)
1748{
1749 struct rb_node *node;
1750 struct hist_entry *he;
1751
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001752 *root_out = RB_ROOT_CACHED;
1753 node = rb_first_cached(root_in);
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001754
1755 while (node) {
1756 he = rb_entry(node, struct hist_entry, rb_node_in);
1757 node = rb_next(node);
1758
1759 hierarchy_insert_output_entry(root_out, he);
1760
1761 if (prog)
1762 ui_progress__update(prog, 1);
1763
Namhyung Kimc72ab442016-11-08 22:08:33 +09001764 hists->nr_entries++;
1765 if (!he->filtered) {
1766 hists->nr_non_filtered_entries++;
1767 hists__calc_col_len(hists, he);
1768 }
1769
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001770 if (!he->leaf) {
1771 hists__hierarchy_output_resort(hists, prog,
1772 &he->hroot_in,
1773 &he->hroot_out,
1774 min_callchain_hits,
1775 use_callchain);
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001776 continue;
1777 }
1778
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001779 if (!use_callchain)
1780 continue;
1781
1782 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1783 u64 total = he->stat.period;
1784
1785 if (symbol_conf.cumulate_callchain)
1786 total = he->stat_acc->period;
1787
1788 min_callchain_hits = total * (callchain_param.min_percent / 100);
1789 }
1790
1791 callchain_param.sort(&he->sorted_chain, he->callchain,
1792 min_callchain_hits, &callchain_param);
1793 }
1794}
1795
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001796static void __hists__insert_output_entry(struct rb_root_cached *entries,
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001797 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001798 u64 min_callchain_hits,
1799 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001800{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001801 struct rb_node **p = &entries->rb_root.rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001802 struct rb_node *parent = NULL;
1803 struct hist_entry *iter;
Namhyung Kimabab5e72016-02-27 03:52:47 +09001804 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001805 bool leftmost = true;
John Kacur3d1d07e2009-09-28 15:32:55 +02001806
Namhyung Kim744070e2016-01-28 00:40:48 +09001807 if (use_callchain) {
1808 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1809 u64 total = he->stat.period;
1810
1811 if (symbol_conf.cumulate_callchain)
1812 total = he->stat_acc->period;
1813
1814 min_callchain_hits = total * (callchain_param.min_percent / 100);
1815 }
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001816 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001817 min_callchain_hits, &callchain_param);
Namhyung Kim744070e2016-01-28 00:40:48 +09001818 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001819
1820 while (*p != NULL) {
1821 parent = *p;
1822 iter = rb_entry(parent, struct hist_entry, rb_node);
1823
Namhyung Kim043ca3892014-03-03 14:18:00 +09001824 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001825 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001826 else {
John Kacur3d1d07e2009-09-28 15:32:55 +02001827 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001828 leftmost = false;
1829 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001830 }
1831
1832 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001833 rb_insert_color_cached(&he->rb_node, entries, leftmost);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001834
1835 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1836 if (perf_hpp__is_dynamic_entry(fmt) &&
1837 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1838 fmt->sort(fmt, he, NULL); /* update column width */
1839 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001840}
1841
Jiri Olsa01441af2016-01-18 10:23:59 +01001842static void output_resort(struct hists *hists, struct ui_progress *prog,
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001843 bool use_callchain, hists__resort_cb_t cb,
1844 void *cb_arg)
John Kacur3d1d07e2009-09-28 15:32:55 +02001845{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001846 struct rb_root_cached *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001847 struct rb_node *next;
1848 struct hist_entry *n;
Namhyung Kim467ef102016-02-16 23:08:19 +09001849 u64 callchain_total;
John Kacur3d1d07e2009-09-28 15:32:55 +02001850 u64 min_callchain_hits;
1851
Namhyung Kim467ef102016-02-16 23:08:19 +09001852 callchain_total = hists->callchain_period;
1853 if (symbol_conf.filter_relative)
1854 callchain_total = hists->callchain_non_filtered_period;
1855
1856 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001857
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001858 hists__reset_stats(hists);
1859 hists__reset_col_len(hists);
1860
1861 if (symbol_conf.report_hierarchy) {
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001862 hists__hierarchy_output_resort(hists, prog,
1863 &hists->entries_collapsed,
1864 &hists->entries,
1865 min_callchain_hits,
1866 use_callchain);
1867 hierarchy_recalc_total_periods(hists);
1868 return;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001869 }
1870
Jiri Olsa52225032016-05-03 13:54:42 +02001871 if (hists__has(hists, need_collapse))
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001872 root = &hists->entries_collapsed;
1873 else
1874 root = hists->entries_in;
1875
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001876 next = rb_first_cached(root);
1877 hists->entries = RB_ROOT_CACHED;
John Kacur3d1d07e2009-09-28 15:32:55 +02001878
1879 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001880 n = rb_entry(next, struct hist_entry, rb_node_in);
1881 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001882
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001883 if (cb && cb(n, cb_arg))
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001884 continue;
1885
Kan Liangf9db0d02015-08-11 06:30:48 -04001886 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001887 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001888
1889 if (!n->filtered)
1890 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001891
1892 if (prog)
1893 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001894 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001895}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001896
Jiri Olsa32dcd022019-07-21 13:23:51 +02001897void perf_evsel__output_resort_cb(struct evsel *evsel, struct ui_progress *prog,
Jiri Olsa57496182019-02-04 15:18:07 +01001898 hists__resort_cb_t cb, void *cb_arg)
Jiri Olsa01441af2016-01-18 10:23:59 +01001899{
Jiri Olsa01441af2016-01-18 10:23:59 +01001900 bool use_callchain;
1901
1902 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -03001903 use_callchain = evsel__has_callchain(evsel);
Jiri Olsa01441af2016-01-18 10:23:59 +01001904 else
1905 use_callchain = symbol_conf.use_callchain;
1906
Jin Yaob49a8212017-05-08 18:43:02 +08001907 use_callchain |= symbol_conf.show_branchflag_count;
1908
Jiri Olsa57496182019-02-04 15:18:07 +01001909 output_resort(evsel__hists(evsel), prog, use_callchain, cb, cb_arg);
1910}
1911
Jiri Olsa32dcd022019-07-21 13:23:51 +02001912void perf_evsel__output_resort(struct evsel *evsel, struct ui_progress *prog)
Jiri Olsa57496182019-02-04 15:18:07 +01001913{
1914 return perf_evsel__output_resort_cb(evsel, prog, NULL, NULL);
Jiri Olsa452ce032016-01-18 10:24:00 +01001915}
1916
1917void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1918{
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001919 output_resort(hists, prog, symbol_conf.use_callchain, NULL, NULL);
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001920}
1921
1922void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1923 hists__resort_cb_t cb)
1924{
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001925 output_resort(hists, prog, symbol_conf.use_callchain, cb, NULL);
Jiri Olsa01441af2016-01-18 10:23:59 +01001926}
1927
Namhyung Kim8c018722016-02-25 00:13:36 +09001928static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1929{
1930 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1931 return false;
1932
1933 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1934 return true;
1935
1936 return false;
1937}
1938
1939struct rb_node *rb_hierarchy_last(struct rb_node *node)
1940{
1941 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1942
1943 while (can_goto_child(he, HMD_NORMAL)) {
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001944 node = rb_last(&he->hroot_out.rb_root);
Namhyung Kim8c018722016-02-25 00:13:36 +09001945 he = rb_entry(node, struct hist_entry, rb_node);
1946 }
1947 return node;
1948}
1949
1950struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1951{
1952 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1953
1954 if (can_goto_child(he, hmd))
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001955 node = rb_first_cached(&he->hroot_out);
Namhyung Kim8c018722016-02-25 00:13:36 +09001956 else
1957 node = rb_next(node);
1958
1959 while (node == NULL) {
1960 he = he->parent_he;
1961 if (he == NULL)
1962 break;
1963
1964 node = rb_next(&he->rb_node);
1965 }
1966 return node;
1967}
1968
1969struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1970{
1971 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1972
1973 node = rb_prev(node);
1974 if (node)
1975 return rb_hierarchy_last(node);
1976
1977 he = he->parent_he;
1978 if (he == NULL)
1979 return NULL;
1980
1981 return &he->rb_node;
1982}
1983
Namhyung Kima7b58952016-02-26 21:13:16 +09001984bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1985{
1986 struct rb_node *node;
1987 struct hist_entry *child;
1988 float percent;
1989
1990 if (he->leaf)
1991 return false;
1992
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001993 node = rb_first_cached(&he->hroot_out);
Namhyung Kima7b58952016-02-26 21:13:16 +09001994 child = rb_entry(node, struct hist_entry, rb_node);
1995
1996 while (node && child->filtered) {
1997 node = rb_next(node);
1998 child = rb_entry(node, struct hist_entry, rb_node);
1999 }
2000
2001 if (node)
2002 percent = hist_entry__get_percent_limit(child);
2003 else
2004 percent = 0;
2005
2006 return node && percent >= limit;
2007}
2008
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002009static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002010 enum hist_filter filter)
2011{
2012 h->filtered &= ~(1 << filter);
Namhyung Kim155e9af2016-02-25 00:13:38 +09002013
2014 if (symbol_conf.report_hierarchy) {
2015 struct hist_entry *parent = h->parent_he;
2016
2017 while (parent) {
2018 he_stat__add_stat(&parent->stat, &h->stat);
2019
2020 parent->filtered &= ~(1 << filter);
2021
2022 if (parent->filtered)
2023 goto next;
2024
2025 /* force fold unfiltered entry for simplicity */
2026 parent->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09002027 parent->has_no_entry = false;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002028 parent->row_offset = 0;
2029 parent->nr_rows = 0;
2030next:
2031 parent = parent->parent_he;
2032 }
2033 }
2034
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002035 if (h->filtered)
2036 return;
2037
Namhyung Kim87e90f42014-04-24 16:44:16 +09002038 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09002039 h->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09002040 h->has_no_entry = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03002041 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08002042 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09002043
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09002044 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002045
Namhyung Kim9283ba92014-04-24 16:37:26 +09002046 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002047 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002048}
2049
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02002050
2051static bool hists__filter_entry_by_dso(struct hists *hists,
2052 struct hist_entry *he)
2053{
2054 if (hists->dso_filter != NULL &&
2055 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
2056 he->filtered |= (1 << HIST_FILTER__DSO);
2057 return true;
2058 }
2059
2060 return false;
2061}
2062
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02002063static bool hists__filter_entry_by_thread(struct hists *hists,
2064 struct hist_entry *he)
2065{
2066 if (hists->thread_filter != NULL &&
2067 he->thread != hists->thread_filter) {
2068 he->filtered |= (1 << HIST_FILTER__THREAD);
2069 return true;
2070 }
2071
2072 return false;
2073}
2074
Namhyung Kime94d53e2012-03-16 17:50:51 +09002075static bool hists__filter_entry_by_symbol(struct hists *hists,
2076 struct hist_entry *he)
2077{
2078 if (hists->symbol_filter_str != NULL &&
2079 (!he->ms.sym || strstr(he->ms.sym->name,
2080 hists->symbol_filter_str) == NULL)) {
2081 he->filtered |= (1 << HIST_FILTER__SYMBOL);
2082 return true;
2083 }
2084
2085 return false;
2086}
2087
Kan Liang21394d92015-09-04 10:45:44 -04002088static bool hists__filter_entry_by_socket(struct hists *hists,
2089 struct hist_entry *he)
2090{
2091 if ((hists->socket_filter > -1) &&
2092 (he->socket != hists->socket_filter)) {
2093 he->filtered |= (1 << HIST_FILTER__SOCKET);
2094 return true;
2095 }
2096
2097 return false;
2098}
2099
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002100typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
2101
2102static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04002103{
2104 struct rb_node *nd;
2105
2106 hists->stats.nr_non_filtered_samples = 0;
2107
2108 hists__reset_filter_stats(hists);
2109 hists__reset_col_len(hists);
2110
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002111 for (nd = rb_first_cached(&hists->entries); nd; nd = rb_next(nd)) {
Kan Liang84734b02015-09-04 10:45:45 -04002112 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2113
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002114 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04002115 continue;
2116
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002117 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04002118 }
2119}
2120
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002121static void resort_filtered_entry(struct rb_root_cached *root,
2122 struct hist_entry *he)
Namhyung Kim70642852016-02-25 00:13:39 +09002123{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002124 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kim70642852016-02-25 00:13:39 +09002125 struct rb_node *parent = NULL;
2126 struct hist_entry *iter;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002127 struct rb_root_cached new_root = RB_ROOT_CACHED;
Namhyung Kim70642852016-02-25 00:13:39 +09002128 struct rb_node *nd;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002129 bool leftmost = true;
Namhyung Kim70642852016-02-25 00:13:39 +09002130
2131 while (*p != NULL) {
2132 parent = *p;
2133 iter = rb_entry(parent, struct hist_entry, rb_node);
2134
2135 if (hist_entry__sort(he, iter) > 0)
2136 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002137 else {
Namhyung Kim70642852016-02-25 00:13:39 +09002138 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002139 leftmost = false;
2140 }
Namhyung Kim70642852016-02-25 00:13:39 +09002141 }
2142
2143 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002144 rb_insert_color_cached(&he->rb_node, root, leftmost);
Namhyung Kim70642852016-02-25 00:13:39 +09002145
2146 if (he->leaf || he->filtered)
2147 return;
2148
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002149 nd = rb_first_cached(&he->hroot_out);
Namhyung Kim70642852016-02-25 00:13:39 +09002150 while (nd) {
2151 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2152
2153 nd = rb_next(nd);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002154 rb_erase_cached(&h->rb_node, &he->hroot_out);
Namhyung Kim70642852016-02-25 00:13:39 +09002155
2156 resort_filtered_entry(&new_root, h);
2157 }
2158
2159 he->hroot_out = new_root;
2160}
2161
Namhyung Kim155e9af2016-02-25 00:13:38 +09002162static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2163{
2164 struct rb_node *nd;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002165 struct rb_root_cached new_root = RB_ROOT_CACHED;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002166
2167 hists->stats.nr_non_filtered_samples = 0;
2168
2169 hists__reset_filter_stats(hists);
2170 hists__reset_col_len(hists);
2171
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002172 nd = rb_first_cached(&hists->entries);
Namhyung Kim155e9af2016-02-25 00:13:38 +09002173 while (nd) {
2174 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2175 int ret;
2176
2177 ret = hist_entry__filter(h, type, arg);
2178
2179 /*
2180 * case 1. non-matching type
2181 * zero out the period, set filter marker and move to child
2182 */
2183 if (ret < 0) {
2184 memset(&h->stat, 0, sizeof(h->stat));
2185 h->filtered |= (1 << type);
2186
2187 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2188 }
2189 /*
2190 * case 2. matched type (filter out)
2191 * set filter marker and move to next
2192 */
2193 else if (ret == 1) {
2194 h->filtered |= (1 << type);
2195
2196 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2197 }
2198 /*
2199 * case 3. ok (not filtered)
2200 * add period to hists and parents, erase the filter marker
2201 * and move to next sibling
2202 */
2203 else {
2204 hists__remove_entry_filter(hists, h, type);
2205
2206 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2207 }
2208 }
Namhyung Kim70642852016-02-25 00:13:39 +09002209
Namhyung Kimf7fb5382016-03-09 22:47:02 +09002210 hierarchy_recalc_total_periods(hists);
2211
Namhyung Kim70642852016-02-25 00:13:39 +09002212 /*
2213 * resort output after applying a new filter since filter in a lower
2214 * hierarchy can change periods in a upper hierarchy.
2215 */
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002216 nd = rb_first_cached(&hists->entries);
Namhyung Kim70642852016-02-25 00:13:39 +09002217 while (nd) {
2218 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2219
2220 nd = rb_next(nd);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002221 rb_erase_cached(&h->rb_node, &hists->entries);
Namhyung Kim70642852016-02-25 00:13:39 +09002222
2223 resort_filtered_entry(&new_root, h);
2224 }
2225
2226 hists->entries = new_root;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002227}
2228
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002229void hists__filter_by_thread(struct hists *hists)
2230{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002231 if (symbol_conf.report_hierarchy)
2232 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2233 hists->thread_filter);
2234 else
2235 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2236 hists__filter_entry_by_thread);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002237}
2238
2239void hists__filter_by_dso(struct hists *hists)
2240{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002241 if (symbol_conf.report_hierarchy)
2242 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2243 hists->dso_filter);
2244 else
2245 hists__filter_by_type(hists, HIST_FILTER__DSO,
2246 hists__filter_entry_by_dso);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002247}
2248
2249void hists__filter_by_symbol(struct hists *hists)
2250{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002251 if (symbol_conf.report_hierarchy)
2252 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2253 hists->symbol_filter_str);
2254 else
2255 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2256 hists__filter_entry_by_symbol);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002257}
2258
2259void hists__filter_by_socket(struct hists *hists)
2260{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002261 if (symbol_conf.report_hierarchy)
2262 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2263 &hists->socket_filter);
2264 else
2265 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2266 hists__filter_entry_by_socket);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002267}
2268
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002269void events_stats__inc(struct events_stats *stats, u32 type)
2270{
2271 ++stats->nr_events[0];
2272 ++stats->nr_events[type];
2273}
2274
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002275void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002276{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002277 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002278}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002279
Namhyung Kim1844dbc2014-05-28 14:12:18 +09002280void hists__inc_nr_samples(struct hists *hists, bool filtered)
2281{
2282 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2283 if (!filtered)
2284 hists->stats.nr_non_filtered_samples++;
2285}
2286
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002287static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2288 struct hist_entry *pair)
2289{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002290 struct rb_root_cached *root;
Namhyung Kimce74f602012-12-10 17:29:55 +09002291 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002292 struct rb_node *parent = NULL;
2293 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07002294 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002295 bool leftmost = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002296
Jiri Olsa52225032016-05-03 13:54:42 +02002297 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002298 root = &hists->entries_collapsed;
2299 else
2300 root = hists->entries_in;
2301
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002302 p = &root->rb_root.rb_node;
Namhyung Kimce74f602012-12-10 17:29:55 +09002303
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002304 while (*p != NULL) {
2305 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09002306 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002307
Namhyung Kimce74f602012-12-10 17:29:55 +09002308 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002309
2310 if (!cmp)
2311 goto out;
2312
2313 if (cmp < 0)
2314 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002315 else {
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002316 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002317 leftmost = false;
2318 }
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002319 }
2320
Namhyung Kima0b51af2012-09-11 13:34:27 +09002321 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002322 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03002323 memset(&he->stat, 0, sizeof(he->stat));
2324 he->hists = hists;
Kan Liang09623d72016-04-24 23:28:09 -07002325 if (symbol_conf.cumulate_callchain)
2326 memset(he->stat_acc, 0, sizeof(he->stat));
Namhyung Kimce74f602012-12-10 17:29:55 +09002327 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002328 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kim62638352014-04-24 16:21:46 +09002329 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01002330 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002331 }
2332out:
2333 return he;
2334}
2335
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002336static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002337 struct rb_root_cached *root,
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002338 struct hist_entry *pair)
2339{
2340 struct rb_node **p;
2341 struct rb_node *parent = NULL;
2342 struct hist_entry *he;
2343 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002344 bool leftmost = true;
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002345
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002346 p = &root->rb_root.rb_node;
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002347 while (*p != NULL) {
2348 int64_t cmp = 0;
2349
2350 parent = *p;
2351 he = rb_entry(parent, struct hist_entry, rb_node_in);
2352
2353 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2354 cmp = fmt->collapse(fmt, he, pair);
2355 if (cmp)
2356 break;
2357 }
2358 if (!cmp)
2359 goto out;
2360
2361 if (cmp < 0)
2362 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002363 else {
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002364 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002365 leftmost = false;
2366 }
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002367 }
2368
2369 he = hist_entry__new(pair, true);
2370 if (he) {
2371 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002372 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002373
2374 he->dummy = true;
2375 he->hists = hists;
2376 memset(&he->stat, 0, sizeof(he->stat));
2377 hists__inc_stats(hists, he);
2378 }
2379out:
2380 return he;
2381}
2382
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002383static struct hist_entry *hists__find_entry(struct hists *hists,
2384 struct hist_entry *he)
2385{
Namhyung Kimce74f602012-12-10 17:29:55 +09002386 struct rb_node *n;
2387
Jiri Olsa52225032016-05-03 13:54:42 +02002388 if (hists__has(hists, need_collapse))
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002389 n = hists->entries_collapsed.rb_root.rb_node;
Namhyung Kimce74f602012-12-10 17:29:55 +09002390 else
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002391 n = hists->entries_in->rb_root.rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002392
2393 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002394 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2395 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002396
2397 if (cmp < 0)
2398 n = n->rb_left;
2399 else if (cmp > 0)
2400 n = n->rb_right;
2401 else
2402 return iter;
2403 }
2404
2405 return NULL;
2406}
2407
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002408static struct hist_entry *hists__find_hierarchy_entry(struct rb_root_cached *root,
Namhyung Kim09034de2016-09-13 16:45:46 +09002409 struct hist_entry *he)
2410{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002411 struct rb_node *n = root->rb_root.rb_node;
Namhyung Kim09034de2016-09-13 16:45:46 +09002412
2413 while (n) {
2414 struct hist_entry *iter;
2415 struct perf_hpp_fmt *fmt;
2416 int64_t cmp = 0;
2417
2418 iter = rb_entry(n, struct hist_entry, rb_node_in);
2419 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2420 cmp = fmt->collapse(fmt, iter, he);
2421 if (cmp)
2422 break;
2423 }
2424
2425 if (cmp < 0)
2426 n = n->rb_left;
2427 else if (cmp > 0)
2428 n = n->rb_right;
2429 else
2430 return iter;
2431 }
2432
2433 return NULL;
2434}
2435
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002436static void hists__match_hierarchy(struct rb_root_cached *leader_root,
2437 struct rb_root_cached *other_root)
Namhyung Kim09034de2016-09-13 16:45:46 +09002438{
2439 struct rb_node *nd;
2440 struct hist_entry *pos, *pair;
2441
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002442 for (nd = rb_first_cached(leader_root); nd; nd = rb_next(nd)) {
Namhyung Kim09034de2016-09-13 16:45:46 +09002443 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2444 pair = hists__find_hierarchy_entry(other_root, pos);
2445
2446 if (pair) {
2447 hist_entry__add_pair(pair, pos);
2448 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2449 }
2450 }
2451}
2452
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002453/*
2454 * Look for pairs to link to the leader buckets (hist_entries):
2455 */
2456void hists__match(struct hists *leader, struct hists *other)
2457{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002458 struct rb_root_cached *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002459 struct rb_node *nd;
Namhyung Kimbe5863b2019-08-28 08:15:55 +09002460 struct hist_entry *pos, *pair;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002461
Namhyung Kim09034de2016-09-13 16:45:46 +09002462 if (symbol_conf.report_hierarchy) {
2463 /* hierarchy report always collapses entries */
2464 return hists__match_hierarchy(&leader->entries_collapsed,
2465 &other->entries_collapsed);
2466 }
2467
Jiri Olsa52225032016-05-03 13:54:42 +02002468 if (hists__has(leader, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002469 root = &leader->entries_collapsed;
2470 else
2471 root = leader->entries_in;
2472
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002473 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002474 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002475 pair = hists__find_entry(other, pos);
2476
Namhyung Kimbe5863b2019-08-28 08:15:55 +09002477 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09002478 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002479 }
2480}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002481
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002482static int hists__link_hierarchy(struct hists *leader_hists,
2483 struct hist_entry *parent,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002484 struct rb_root_cached *leader_root,
2485 struct rb_root_cached *other_root)
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002486{
2487 struct rb_node *nd;
2488 struct hist_entry *pos, *leader;
2489
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002490 for (nd = rb_first_cached(other_root); nd; nd = rb_next(nd)) {
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002491 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2492
2493 if (hist_entry__has_pairs(pos)) {
2494 bool found = false;
2495
2496 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2497 if (leader->hists == leader_hists) {
2498 found = true;
2499 break;
2500 }
2501 }
2502 if (!found)
2503 return -1;
2504 } else {
2505 leader = add_dummy_hierarchy_entry(leader_hists,
2506 leader_root, pos);
2507 if (leader == NULL)
2508 return -1;
2509
2510 /* do not point parent in the pos */
2511 leader->parent_he = parent;
2512
2513 hist_entry__add_pair(pos, leader);
2514 }
2515
2516 if (!pos->leaf) {
2517 if (hists__link_hierarchy(leader_hists, leader,
2518 &leader->hroot_in,
2519 &pos->hroot_in) < 0)
2520 return -1;
2521 }
2522 }
2523 return 0;
2524}
2525
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002526/*
2527 * Look for entries in the other hists that are not present in the leader, if
2528 * we find them, just add a dummy entry on the leader hists, with period=0,
2529 * nr_events=0, to serve as the list header.
2530 */
2531int hists__link(struct hists *leader, struct hists *other)
2532{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002533 struct rb_root_cached *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002534 struct rb_node *nd;
2535 struct hist_entry *pos, *pair;
2536
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002537 if (symbol_conf.report_hierarchy) {
2538 /* hierarchy report always collapses entries */
2539 return hists__link_hierarchy(leader, NULL,
2540 &leader->entries_collapsed,
2541 &other->entries_collapsed);
2542 }
2543
Jiri Olsa52225032016-05-03 13:54:42 +02002544 if (hists__has(other, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002545 root = &other->entries_collapsed;
2546 else
2547 root = other->entries_in;
2548
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002549 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002550 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002551
2552 if (!hist_entry__has_pairs(pos)) {
2553 pair = hists__add_dummy_entry(leader, pos);
2554 if (pair == NULL)
2555 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09002556 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002557 }
2558 }
2559
2560 return 0;
2561}
Namhyung Kimf2148332014-01-14 11:52:48 +09002562
Namhyung Kimbe5863b2019-08-28 08:15:55 +09002563int hists__unlink(struct hists *hists)
2564{
2565 struct rb_root_cached *root;
2566 struct rb_node *nd;
2567 struct hist_entry *pos;
2568
2569 if (hists__has(hists, need_collapse))
2570 root = &hists->entries_collapsed;
2571 else
2572 root = hists->entries_in;
2573
2574 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2575 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2576 list_del_init(&pos->pairs.node);
2577 }
2578
2579 return 0;
2580}
2581
Andi Kleen578499982015-07-18 08:24:49 -07002582void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
Jin Yao7841f402019-11-07 15:47:15 +08002583 struct perf_sample *sample, bool nonany_branch_mode,
2584 u64 *total_cycles)
Andi Kleen578499982015-07-18 08:24:49 -07002585{
2586 struct branch_info *bi;
Kan Liang42bbabe2020-02-28 08:30:00 -08002587 struct branch_entry *entries = perf_sample__branch_entries(sample);
Andi Kleen578499982015-07-18 08:24:49 -07002588
2589 /* If we have branch cycles always annotate them. */
Kan Liang42bbabe2020-02-28 08:30:00 -08002590 if (bs && bs->nr && entries[0].flags.cycles) {
Andi Kleen578499982015-07-18 08:24:49 -07002591 int i;
2592
2593 bi = sample__resolve_bstack(sample, al);
2594 if (bi) {
2595 struct addr_map_symbol *prev = NULL;
2596
2597 /*
2598 * Ignore errors, still want to process the
2599 * other entries.
2600 *
2601 * For non standard branch modes always
2602 * force no IPC (prev == NULL)
2603 *
2604 * Note that perf stores branches reversed from
2605 * program order!
2606 */
2607 for (i = bs->nr - 1; i >= 0; i--) {
2608 addr_map_symbol__account_cycles(&bi[i].from,
2609 nonany_branch_mode ? NULL : prev,
2610 bi[i].flags.cycles);
2611 prev = &bi[i].to;
Jin Yao7841f402019-11-07 15:47:15 +08002612
2613 if (total_cycles)
2614 *total_cycles += bi[i].flags.cycles;
Andi Kleen578499982015-07-18 08:24:49 -07002615 }
2616 free(bi);
2617 }
2618 }
2619}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002620
Jiri Olsa63503db2019-07-21 13:23:52 +02002621size_t perf_evlist__fprintf_nr_events(struct evlist *evlist, FILE *fp)
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002622{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002623 struct evsel *pos;
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002624 size_t ret = 0;
2625
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002626 evlist__for_each_entry(evlist, pos) {
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002627 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2628 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2629 }
2630
2631 return ret;
2632}
2633
2634
Namhyung Kimf2148332014-01-14 11:52:48 +09002635u64 hists__total_period(struct hists *hists)
2636{
2637 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2638 hists->stats.total_period;
2639}
Namhyung Kim33db4562014-02-07 12:06:07 +09002640
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002641int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq)
2642{
2643 char unit;
2644 int printed;
2645 const struct dso *dso = hists->dso_filter;
Namhyung Kim7cb10a082019-05-27 15:11:49 +09002646 struct thread *thread = hists->thread_filter;
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002647 int socket_id = hists->socket_filter;
2648 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
2649 u64 nr_events = hists->stats.total_period;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002650 struct evsel *evsel = hists_to_evsel(hists);
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002651 const char *ev_name = perf_evsel__name(evsel);
2652 char buf[512], sample_freq_str[64] = "";
2653 size_t buflen = sizeof(buf);
2654 char ref[30] = " show reference callgraph, ";
2655 bool enable_ref = false;
2656
2657 if (symbol_conf.filter_relative) {
2658 nr_samples = hists->stats.nr_non_filtered_samples;
2659 nr_events = hists->stats.total_non_filtered_period;
2660 }
2661
2662 if (perf_evsel__is_group_event(evsel)) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002663 struct evsel *pos;
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002664
2665 perf_evsel__group_desc(evsel, buf, buflen);
2666 ev_name = buf;
2667
2668 for_each_group_member(pos, evsel) {
2669 struct hists *pos_hists = evsel__hists(pos);
2670
2671 if (symbol_conf.filter_relative) {
2672 nr_samples += pos_hists->stats.nr_non_filtered_samples;
2673 nr_events += pos_hists->stats.total_non_filtered_period;
2674 } else {
2675 nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
2676 nr_events += pos_hists->stats.total_period;
2677 }
2678 }
2679 }
2680
2681 if (symbol_conf.show_ref_callgraph &&
2682 strstr(ev_name, "call-graph=no"))
2683 enable_ref = true;
2684
2685 if (show_freq)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002686 scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->core.attr.sample_freq);
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002687
2688 nr_samples = convert_unit(nr_samples, &unit);
2689 printed = scnprintf(bf, size,
2690 "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64,
Jiri Olsa5643b1a2019-07-21 13:24:46 +02002691 nr_samples, unit, evsel->core.nr_members > 1 ? "s" : "",
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002692 ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events);
2693
2694
2695 if (hists->uid_filter_str)
2696 printed += snprintf(bf + printed, size - printed,
2697 ", UID: %s", hists->uid_filter_str);
2698 if (thread) {
2699 if (hists__has(hists, thread)) {
2700 printed += scnprintf(bf + printed, size - printed,
2701 ", Thread: %s(%d)",
2702 (thread->comm_set ? thread__comm_str(thread) : ""),
2703 thread->tid);
2704 } else {
2705 printed += scnprintf(bf + printed, size - printed,
2706 ", Thread: %s",
2707 (thread->comm_set ? thread__comm_str(thread) : ""));
2708 }
2709 }
2710 if (dso)
2711 printed += scnprintf(bf + printed, size - printed,
2712 ", DSO: %s", dso->short_name);
2713 if (socket_id > -1)
2714 printed += scnprintf(bf + printed, size - printed,
2715 ", Processor Socket: %d", socket_id);
2716
2717 return printed;
2718}
2719
Namhyung Kim33db4562014-02-07 12:06:07 +09002720int parse_filter_percentage(const struct option *opt __maybe_unused,
2721 const char *arg, int unset __maybe_unused)
2722{
2723 if (!strcmp(arg, "relative"))
2724 symbol_conf.filter_relative = true;
2725 else if (!strcmp(arg, "absolute"))
2726 symbol_conf.filter_relative = false;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002727 else {
Colin Ian Kinga596a872017-03-30 10:54:40 +01002728 pr_debug("Invalid percentage: %s\n", arg);
Namhyung Kim33db4562014-02-07 12:06:07 +09002729 return -1;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002730 }
Namhyung Kim33db4562014-02-07 12:06:07 +09002731
2732 return 0;
2733}
Namhyung Kim0b93da12014-01-14 12:02:15 +09002734
2735int perf_hist_config(const char *var, const char *value)
2736{
2737 if (!strcmp(var, "hist.percentage"))
2738 return parse_filter_percentage(NULL, value, 0);
2739
2740 return 0;
2741}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002742
Jiri Olsa5b658552016-01-18 10:24:22 +01002743int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002744{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002745 memset(hists, 0, sizeof(*hists));
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002746 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT_CACHED;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002747 hists->entries_in = &hists->entries_in_array[0];
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002748 hists->entries_collapsed = RB_ROOT_CACHED;
2749 hists->entries = RB_ROOT_CACHED;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002750 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04002751 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01002752 hists->hpp_list = hpp_list;
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002753 INIT_LIST_HEAD(&hists->hpp_formats);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002754 return 0;
2755}
2756
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002757static void hists__delete_remaining_entries(struct rb_root_cached *root)
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002758{
2759 struct rb_node *node;
2760 struct hist_entry *he;
2761
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002762 while (!RB_EMPTY_ROOT(&root->rb_root)) {
2763 node = rb_first_cached(root);
2764 rb_erase_cached(node, root);
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002765
2766 he = rb_entry(node, struct hist_entry, rb_node_in);
2767 hist_entry__delete(he);
2768 }
2769}
2770
2771static void hists__delete_all_entries(struct hists *hists)
2772{
2773 hists__delete_entries(hists);
2774 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2775 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2776 hists__delete_remaining_entries(&hists->entries_collapsed);
2777}
2778
Jiri Olsa32dcd022019-07-21 13:23:51 +02002779static void hists_evsel__exit(struct evsel *evsel)
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002780{
2781 struct hists *hists = evsel__hists(evsel);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002782 struct perf_hpp_fmt *fmt, *pos;
2783 struct perf_hpp_list_node *node, *tmp;
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002784
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002785 hists__delete_all_entries(hists);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002786
2787 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2788 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03002789 list_del_init(&fmt->list);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002790 free(fmt);
2791 }
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03002792 list_del_init(&node->list);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002793 free(node);
2794 }
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002795}
2796
Jiri Olsa32dcd022019-07-21 13:23:51 +02002797static int hists_evsel__init(struct evsel *evsel)
Namhyung Kimfc284be2016-01-07 10:14:10 +01002798{
2799 struct hists *hists = evsel__hists(evsel);
2800
Jiri Olsa5b658552016-01-18 10:24:22 +01002801 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01002802 return 0;
2803}
2804
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002805/*
2806 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2807 * stored in the rbtree...
2808 */
2809
2810int hists__init(void)
2811{
2812 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002813 hists_evsel__init,
2814 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002815 if (err)
2816 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2817
2818 return err;
2819}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01002820
2821void perf_hpp_list__init(struct perf_hpp_list *list)
2822{
2823 INIT_LIST_HEAD(&list->fields);
2824 INIT_LIST_HEAD(&list->sorts);
2825}