blob: 7ace7a10054d82da540825ad2eb36d5a85b0a6a4 [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"
Frederic Weisbecker8a0ecfb2010-05-13 19:47:16 +02003#include "util.h"
Frederic Weisbecker598357e2010-05-21 12:48:39 +02004#include "build-id.h"
John Kacur3d1d07e2009-09-28 15:32:55 +02005#include "hist.h"
Krister Johansen9c68ae92017-01-05 22:23:31 -08006#include "map.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02007#include "session.h"
Hari Bathinid890a982017-03-08 02:12:13 +05308#include "namespaces.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02009#include "sort.h"
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -030010#include "units.h"
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -030011#include "evlist.h"
Namhyung Kim29d720e2013-01-22 18:09:33 +090012#include "evsel.h"
Namhyung Kim69bcb012013-10-30 09:40:34 +090013#include "annotate.h"
Arnaldo Carvalho de Melo632a5ca2017-04-17 16:30:49 -030014#include "srcline.h"
Arnaldo Carvalho de Melodaecf9e2019-01-28 00:03:34 +010015#include "symbol.h"
Arnaldo Carvalho de Meloe7ff8922017-04-19 21:34:35 -030016#include "thread.h"
Namhyung Kim740b97f2014-12-22 13:44:10 +090017#include "ui/progress.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030018#include <errno.h>
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -020019#include <math.h>
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -030020#include <inttypes.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030021#include <sys/param.h>
Andi Kleen37239082019-03-11 07:44:54 -070022#include <linux/time64.h>
John Kacur3d1d07e2009-09-28 15:32:55 +020023
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020024static bool hists__filter_entry_by_dso(struct hists *hists,
25 struct hist_entry *he);
26static bool hists__filter_entry_by_thread(struct hists *hists,
27 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090028static bool hists__filter_entry_by_symbol(struct hists *hists,
29 struct hist_entry *he);
Kan Liang21394d92015-09-04 10:45:44 -040030static bool hists__filter_entry_by_socket(struct hists *hists,
31 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020032
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030033u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030034{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030035 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030036}
37
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030038void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030039{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030040 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030041}
42
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030043bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030044{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030045 if (len > hists__col_len(hists, col)) {
46 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030047 return true;
48 }
49 return false;
50}
51
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090052void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030053{
54 enum hist_column col;
55
56 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030057 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030058}
59
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010060static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
61{
62 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
63
64 if (hists__col_len(hists, dso) < unresolved_col_width &&
65 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
66 !symbol_conf.dso_list)
67 hists__set_col_len(hists, dso, unresolved_col_width);
68}
69
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090070void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030071{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010072 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Stephane Eranian98a3b322013-01-24 16:10:35 +010073 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030074 u16 len;
75
Namhyung Kimded19d52013-04-01 20:35:19 +090076 /*
77 * +4 accounts for '[x] ' priv level info
78 * +2 accounts for 0x prefix on raw addresses
79 * +3 accounts for ' y ' symtab origin info
80 */
81 if (h->ms.sym) {
82 symlen = h->ms.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +090083 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +090084 symlen += BITS_PER_LONG / 4 + 2 + 3;
85 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
86 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010087 symlen = unresolved_col_width + 4 + 2;
88 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010089 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +010090 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030091
92 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030093 if (hists__new_col_len(hists, HISTC_COMM, len))
Jiri Olsa89c7cb22016-06-20 23:58:19 +020094 hists__set_col_len(hists, HISTC_THREAD, len + 8);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030095
96 if (h->ms.map) {
97 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030098 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030099 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100100
Namhyung Kimcb993742012-12-27 18:11:42 +0900101 if (h->parent)
102 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
103
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100104 if (h->branch_info) {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100105 if (h->branch_info->from.sym) {
106 symlen = (int)h->branch_info->from.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +0900107 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +0900108 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100109 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
110
111 symlen = dso__name_len(h->branch_info->from.map->dso);
112 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
113 } else {
114 symlen = unresolved_col_width + 4 + 2;
115 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
116 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
117 }
118
119 if (h->branch_info->to.sym) {
120 symlen = (int)h->branch_info->to.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +0900121 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +0900122 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100123 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
124
125 symlen = dso__name_len(h->branch_info->to.map->dso);
126 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
127 } else {
128 symlen = unresolved_col_width + 4 + 2;
129 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
130 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
131 }
Andi Kleen508be0d2016-05-20 13:15:08 -0700132
133 if (h->branch_info->srcline_from)
134 hists__new_col_len(hists, HISTC_SRCLINE_FROM,
135 strlen(h->branch_info->srcline_from));
136 if (h->branch_info->srcline_to)
137 hists__new_col_len(hists, HISTC_SRCLINE_TO,
138 strlen(h->branch_info->srcline_to));
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100139 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100140
141 if (h->mem_info) {
Stephane Eranian98a3b322013-01-24 16:10:35 +0100142 if (h->mem_info->daddr.sym) {
143 symlen = (int)h->mem_info->daddr.sym->namelen + 4
144 + unresolved_col_width + 2;
145 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
146 symlen);
Don Zickus9b32ba72014-06-01 15:38:29 +0200147 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
148 symlen + 1);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100149 } else {
150 symlen = unresolved_col_width + 4 + 2;
151 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
152 symlen);
Jiri Olsa08059092016-01-20 12:56:33 +0100153 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
154 symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100155 }
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200156
157 if (h->mem_info->iaddr.sym) {
158 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
159 + unresolved_col_width + 2;
160 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
161 symlen);
162 } else {
163 symlen = unresolved_col_width + 4 + 2;
164 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
165 symlen);
166 }
167
Stephane Eranian98a3b322013-01-24 16:10:35 +0100168 if (h->mem_info->daddr.map) {
169 symlen = dso__name_len(h->mem_info->daddr.map->dso);
170 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
171 symlen);
172 } else {
173 symlen = unresolved_col_width + 4 + 2;
174 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
175 }
Kan Liang8780fb22017-08-29 13:11:09 -0400176
177 hists__new_col_len(hists, HISTC_MEM_PHYS_DADDR,
178 unresolved_col_width + 4 + 2);
179
Stephane Eranian98a3b322013-01-24 16:10:35 +0100180 } else {
181 symlen = unresolved_col_width + 4 + 2;
182 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200183 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100184 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
185 }
186
Hari Bathinid890a982017-03-08 02:12:13 +0530187 hists__new_col_len(hists, HISTC_CGROUP_ID, 20);
Arnaldo Carvalho de Meloa4978ec2015-09-09 12:14:00 -0300188 hists__new_col_len(hists, HISTC_CPU, 3);
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400189 hists__new_col_len(hists, HISTC_SOCKET, 6);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100190 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
191 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
192 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
193 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
194 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
195 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen37239082019-03-11 07:44:54 -0700196 hists__new_col_len(hists, HISTC_TIME, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700197
Jiri Olsaf666ac02016-09-19 15:10:10 +0200198 if (h->srcline) {
199 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
200 hists__new_col_len(hists, HISTC_SRCLINE, len);
201 }
Arnaldo Carvalho de Meloe8e6d372015-08-10 16:53:54 -0300202
Andi Kleen31191a82015-08-07 15:54:24 -0700203 if (h->srcfile)
204 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
205
Andi Kleen475eeab2013-09-20 07:40:43 -0700206 if (h->transaction)
207 hists__new_col_len(hists, HISTC_TRANSACTION,
208 hist_entry__transaction_len());
Namhyung Kim0c0af782016-02-21 23:22:38 +0900209
210 if (h->trace_output)
211 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300212}
213
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900214void hists__output_recalc_col_len(struct hists *hists, int max_rows)
215{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800216 struct rb_node *next = rb_first_cached(&hists->entries);
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900217 struct hist_entry *n;
218 int row = 0;
219
220 hists__reset_col_len(hists);
221
222 while (next && row++ < max_rows) {
223 n = rb_entry(next, struct hist_entry, rb_node);
224 if (!n->filtered)
225 hists__calc_col_len(hists, n);
226 next = rb_next(&n->rb_node);
227 }
228}
229
Namhyung Kimf39056f2014-01-14 14:25:37 +0900230static void he_stat__add_cpumode_period(struct he_stat *he_stat,
231 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800232{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300233 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800234 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900235 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800236 break;
237 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900238 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800239 break;
240 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900241 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800242 break;
243 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900244 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800245 break;
246 default:
247 break;
248 }
249}
250
Andi Kleen37239082019-03-11 07:44:54 -0700251static long hist_time(unsigned long htime)
252{
253 unsigned long time_quantum = symbol_conf.time_quantum;
254 if (time_quantum)
255 return (htime / time_quantum) * time_quantum;
256 return htime;
257}
258
Andi Kleen05484292013-01-24 16:10:29 +0100259static void he_stat__add_period(struct he_stat *he_stat, u64 period,
260 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900261{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100262
Namhyung Kim139c0812012-10-04 21:49:43 +0900263 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100264 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900265 he_stat->nr_events += 1;
266}
267
268static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
269{
270 dest->period += src->period;
271 dest->period_sys += src->period_sys;
272 dest->period_us += src->period_us;
273 dest->period_guest_sys += src->period_guest_sys;
274 dest->period_guest_us += src->period_guest_us;
275 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100276 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900277}
278
Namhyung Kimf39056f2014-01-14 14:25:37 +0900279static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300280{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900281 he_stat->period = (he_stat->period * 7) / 8;
282 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100283 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300284}
285
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900286static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
287
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300288static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
289{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900290 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900291 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200292
293 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300294 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200295
Namhyung Kimf39056f2014-01-14 14:25:37 +0900296 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900297 if (symbol_conf.cumulate_callchain)
298 he_stat__decay(he->stat_acc);
Namhyung Kim42b276a2016-01-05 12:06:00 +0900299 decay_callchain(he->callchain);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200300
Namhyung Kim3186b682014-04-22 13:44:23 +0900301 diff = prev_period - he->stat.period;
302
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900303 if (!he->depth) {
304 hists->stats.total_period -= diff;
305 if (!he->filtered)
306 hists->stats.total_non_filtered_period -= diff;
307 }
308
309 if (!he->leaf) {
310 struct hist_entry *child;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800311 struct rb_node *node = rb_first_cached(&he->hroot_out);
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900312 while (node) {
313 child = rb_entry(node, struct hist_entry, rb_node);
314 node = rb_next(node);
315
316 if (hists__decay_entry(hists, child))
317 hists__delete_entry(hists, child);
318 }
319 }
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200320
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900321 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300322}
323
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300324static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
325{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800326 struct rb_root_cached *root_in;
327 struct rb_root_cached *root_out;
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300328
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900329 if (he->parent_he) {
330 root_in = &he->parent_he->hroot_in;
331 root_out = &he->parent_he->hroot_out;
332 } else {
Jiri Olsa52225032016-05-03 13:54:42 +0200333 if (hists__has(hists, need_collapse))
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900334 root_in = &hists->entries_collapsed;
335 else
336 root_in = hists->entries_in;
337 root_out = &hists->entries;
338 }
339
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800340 rb_erase_cached(&he->rb_node_in, root_in);
341 rb_erase_cached(&he->rb_node, root_out);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300342
343 --hists->nr_entries;
344 if (!he->filtered)
345 --hists->nr_non_filtered_entries;
346
347 hist_entry__delete(he);
348}
349
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900350void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300351{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800352 struct rb_node *next = rb_first_cached(&hists->entries);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300353 struct hist_entry *n;
354
355 while (next) {
356 n = rb_entry(next, struct hist_entry, rb_node);
357 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200358 if (((zap_user && n->level == '.') ||
359 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300360 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300361 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300362 }
363 }
364}
365
Namhyung Kim701937b2014-08-12 17:16:05 +0900366void hists__delete_entries(struct hists *hists)
367{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800368 struct rb_node *next = rb_first_cached(&hists->entries);
Namhyung Kim701937b2014-08-12 17:16:05 +0900369 struct hist_entry *n;
370
371 while (next) {
372 n = rb_entry(next, struct hist_entry, rb_node);
373 next = rb_next(&n->rb_node);
374
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300375 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900376 }
377}
378
John Kacur3d1d07e2009-09-28 15:32:55 +0200379/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300380 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200381 */
382
Jiri Olsa0a269a62016-07-05 08:56:03 +0200383static int hist_entry__init(struct hist_entry *he,
384 struct hist_entry *template,
Arnaldo Carvalho de Melo41477ac2018-06-07 14:19:54 -0300385 bool sample_self,
386 size_t callchain_size)
Jiri Olsa0a269a62016-07-05 08:56:03 +0200387{
388 *he = *template;
Arnaldo Carvalho de Melo41477ac2018-06-07 14:19:54 -0300389 he->callchain_size = callchain_size;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200390
391 if (symbol_conf.cumulate_callchain) {
392 he->stat_acc = malloc(sizeof(he->stat));
393 if (he->stat_acc == NULL)
394 return -ENOMEM;
395 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
396 if (!sample_self)
397 memset(&he->stat, 0, sizeof(he->stat));
398 }
399
400 map__get(he->ms.map);
401
402 if (he->branch_info) {
403 /*
404 * This branch info is (a part of) allocated from
405 * sample__resolve_bstack() and will be freed after
406 * adding new entries. So we need to save a copy.
407 */
408 he->branch_info = malloc(sizeof(*he->branch_info));
Jiri Olsac5758912019-03-05 16:25:30 +0100409 if (he->branch_info == NULL)
410 goto err;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200411
412 memcpy(he->branch_info, template->branch_info,
413 sizeof(*he->branch_info));
414
415 map__get(he->branch_info->from.map);
416 map__get(he->branch_info->to.map);
417 }
418
419 if (he->mem_info) {
420 map__get(he->mem_info->iaddr.map);
421 map__get(he->mem_info->daddr.map);
422 }
423
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -0300424 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
Jiri Olsa0a269a62016-07-05 08:56:03 +0200425 callchain_init(he->callchain);
426
427 if (he->raw_data) {
428 he->raw_data = memdup(he->raw_data, he->raw_size);
Jiri Olsac5758912019-03-05 16:25:30 +0100429 if (he->raw_data == NULL)
430 goto err_infos;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200431 }
Jiri Olsa26349582019-03-05 16:25:31 +0100432
433 if (he->srcline) {
434 he->srcline = strdup(he->srcline);
435 if (he->srcline == NULL)
436 goto err_rawdata;
437 }
438
Andi Kleen4968ac82019-03-11 07:44:58 -0700439 if (symbol_conf.res_sample) {
440 he->res_samples = calloc(sizeof(struct res_sample),
441 symbol_conf.res_sample);
442 if (!he->res_samples)
443 goto err_srcline;
444 }
445
Jiri Olsa0a269a62016-07-05 08:56:03 +0200446 INIT_LIST_HEAD(&he->pairs.node);
447 thread__get(he->thread);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800448 he->hroot_in = RB_ROOT_CACHED;
449 he->hroot_out = RB_ROOT_CACHED;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200450
451 if (!symbol_conf.report_hierarchy)
452 he->leaf = true;
453
454 return 0;
Jiri Olsac5758912019-03-05 16:25:30 +0100455
Andi Kleen4968ac82019-03-11 07:44:58 -0700456err_srcline:
457 free(he->srcline);
458
Jiri Olsa26349582019-03-05 16:25:31 +0100459err_rawdata:
460 free(he->raw_data);
461
Jiri Olsac5758912019-03-05 16:25:30 +0100462err_infos:
463 if (he->branch_info) {
464 map__put(he->branch_info->from.map);
465 map__put(he->branch_info->to.map);
466 free(he->branch_info);
467 }
468 if (he->mem_info) {
469 map__put(he->mem_info->iaddr.map);
470 map__put(he->mem_info->daddr.map);
471 }
472err:
473 map__zput(he->ms.map);
474 free(he->stat_acc);
475 return -ENOMEM;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200476}
477
Jiri Olsaf542e762016-07-05 08:56:04 +0200478static void *hist_entry__zalloc(size_t size)
479{
480 return zalloc(size + sizeof(struct hist_entry));
481}
482
483static void hist_entry__free(void *ptr)
484{
485 free(ptr);
486}
487
488static struct hist_entry_ops default_ops = {
489 .new = hist_entry__zalloc,
490 .free = hist_entry__free,
491};
492
Namhyung Kima0b51af2012-09-11 13:34:27 +0900493static struct hist_entry *hist_entry__new(struct hist_entry *template,
494 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300495{
Jiri Olsaf542e762016-07-05 08:56:04 +0200496 struct hist_entry_ops *ops = template->ops;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900497 size_t callchain_size = 0;
498 struct hist_entry *he;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200499 int err = 0;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900500
Jiri Olsaf542e762016-07-05 08:56:04 +0200501 if (!ops)
502 ops = template->ops = &default_ops;
503
Namhyung Kim82aa0192014-12-22 13:44:14 +0900504 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900505 callchain_size = sizeof(struct callchain_root);
506
Jiri Olsaf542e762016-07-05 08:56:04 +0200507 he = ops->new(callchain_size);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200508 if (he) {
Arnaldo Carvalho de Melo41477ac2018-06-07 14:19:54 -0300509 err = hist_entry__init(he, template, sample_self, callchain_size);
Jiri Olsaf542e762016-07-05 08:56:04 +0200510 if (err) {
511 ops->free(he);
512 he = NULL;
513 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300514 }
515
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200516 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300517}
518
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300519static u8 symbol__parent_filter(const struct symbol *parent)
520{
521 if (symbol_conf.exclude_other && parent == NULL)
522 return 1 << HIST_FILTER__PARENT;
523 return 0;
524}
525
Namhyung Kim467ef102016-02-16 23:08:19 +0900526static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
527{
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -0300528 if (!hist_entry__has_callchains(he) || !symbol_conf.use_callchain)
Namhyung Kim467ef102016-02-16 23:08:19 +0900529 return;
530
531 he->hists->callchain_period += period;
532 if (!he->filtered)
533 he->hists->callchain_non_filtered_period += period;
534}
535
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300536static struct hist_entry *hists__findnew_entry(struct hists *hists,
537 struct hist_entry *entry,
538 struct addr_location *al,
539 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300540{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300541 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300542 struct rb_node *parent = NULL;
543 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700544 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900545 u64 period = entry->stat.period;
546 u64 weight = entry->stat.weight;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800547 bool leftmost = true;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300548
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800549 p = &hists->entries_in->rb_root.rb_node;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300550
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300551 while (*p != NULL) {
552 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300553 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300554
Namhyung Kim9afcf932012-12-10 17:29:54 +0900555 /*
556 * Make sure that it receives arguments in a same order as
557 * hist_entry__collapse() so that we can use an appropriate
558 * function when searching an entry regardless which sort
559 * keys were used.
560 */
561 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300562
563 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900564 if (sample_self) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900565 he_stat__add_period(&he->stat, period, weight);
Namhyung Kim467ef102016-02-16 23:08:19 +0900566 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900567 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900568 if (symbol_conf.cumulate_callchain)
569 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400570
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900571 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300572 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900573 * and will not be used anymore.
574 */
Jiri Olsa9f874982018-03-07 16:50:06 +0100575 mem_info__zput(entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900576
David Miller63fa4712012-03-27 03:14:18 -0400577 /* If the map of an existing hist_entry has
578 * become out-of-date due to an exec() or
579 * similar, update it. Otherwise we will
580 * mis-adjust symbol addresses when computing
581 * the history counter to increment.
582 */
583 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300584 map__put(he->ms.map);
585 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400586 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300587 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300588 }
589
590 if (cmp < 0)
591 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800592 else {
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300593 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800594 leftmost = false;
595 }
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300596 }
597
Namhyung Kima0b51af2012-09-11 13:34:27 +0900598 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300599 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900600 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300601
Namhyung Kim0f584742016-01-28 00:40:49 +0900602 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900603 hist_entry__add_callchain_period(he, period);
604 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900605
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300606 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800607 rb_insert_color_cached(&he->rb_node_in, hists->entries_in, leftmost);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300608out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900609 if (sample_self)
610 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900611 if (symbol_conf.cumulate_callchain)
612 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300613 return he;
614}
615
Andi Kleen4968ac82019-03-11 07:44:58 -0700616static unsigned random_max(unsigned high)
617{
618 unsigned thresh = -high % high;
619 for (;;) {
620 unsigned r = random();
621 if (r >= thresh)
622 return r % high;
623 }
624}
625
626static void hists__res_sample(struct hist_entry *he, struct perf_sample *sample)
627{
628 struct res_sample *r;
629 int j;
630
631 if (he->num_res < symbol_conf.res_sample) {
632 j = he->num_res++;
633 } else {
634 j = random_max(symbol_conf.res_sample);
635 }
636 r = &he->res_samples[j];
637 r->time = sample->time;
638 r->cpu = sample->cpu;
639 r->tid = sample->tid;
640}
641
Jiri Olsaa5051972016-07-05 08:56:05 +0200642static struct hist_entry*
643__hists__add_entry(struct hists *hists,
644 struct addr_location *al,
645 struct symbol *sym_parent,
646 struct branch_info *bi,
647 struct mem_info *mi,
648 struct perf_sample *sample,
649 bool sample_self,
650 struct hist_entry_ops *ops)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100651{
Hari Bathinid890a982017-03-08 02:12:13 +0530652 struct namespaces *ns = thread__namespaces(al->thread);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100653 struct hist_entry entry = {
654 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900655 .comm = thread__comm(al->thread),
Hari Bathinid890a982017-03-08 02:12:13 +0530656 .cgroup_id = {
657 .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0,
658 .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0,
659 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100660 .ms = {
661 .map = al->map,
662 .sym = al->sym,
663 },
Jiri Olsa26349582019-03-05 16:25:31 +0100664 .srcline = (char *) al->srcline,
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400665 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400666 .cpu = al->cpu,
667 .cpumode = al->cpumode,
668 .ip = al->addr,
669 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900670 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900671 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900672 .period = sample->period,
673 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900674 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100675 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300676 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300677 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900678 .branch_info = bi,
679 .mem_info = mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900680 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900681 .raw_data = sample->raw_data,
682 .raw_size = sample->raw_size,
Jiri Olsaa5051972016-07-05 08:56:05 +0200683 .ops = ops,
Andi Kleen37239082019-03-11 07:44:54 -0700684 .time = hist_time(sample->time),
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300685 }, *he = hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100686
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300687 if (!hists->has_callchains && he && he->callchain_size != 0)
688 hists->has_callchains = true;
Andi Kleen4968ac82019-03-11 07:44:58 -0700689 if (he && symbol_conf.res_sample)
690 hists__res_sample(he, sample);
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300691 return he;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100692}
693
Jiri Olsaa5051972016-07-05 08:56:05 +0200694struct hist_entry *hists__add_entry(struct hists *hists,
695 struct addr_location *al,
696 struct symbol *sym_parent,
697 struct branch_info *bi,
698 struct mem_info *mi,
699 struct perf_sample *sample,
700 bool sample_self)
701{
702 return __hists__add_entry(hists, al, sym_parent, bi, mi,
703 sample, sample_self, NULL);
704}
705
706struct hist_entry *hists__add_entry_ops(struct hists *hists,
707 struct hist_entry_ops *ops,
708 struct addr_location *al,
709 struct symbol *sym_parent,
710 struct branch_info *bi,
711 struct mem_info *mi,
712 struct perf_sample *sample,
713 bool sample_self)
714{
715 return __hists__add_entry(hists, al, sym_parent, bi, mi,
716 sample, sample_self, ops);
717}
718
Namhyung Kim69bcb012013-10-30 09:40:34 +0900719static int
720iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
721 struct addr_location *al __maybe_unused)
722{
723 return 0;
724}
725
726static int
727iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
728 struct addr_location *al __maybe_unused)
729{
730 return 0;
731}
732
733static int
734iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
735{
736 struct perf_sample *sample = iter->sample;
737 struct mem_info *mi;
738
739 mi = sample__resolve_mem(sample, al);
740 if (mi == NULL)
741 return -ENOMEM;
742
743 iter->priv = mi;
744 return 0;
745}
746
747static int
748iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
749{
750 u64 cost;
751 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300752 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900753 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900754 struct hist_entry *he;
755
756 if (mi == NULL)
757 return -EINVAL;
758
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900759 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900760 if (!cost)
761 cost = 1;
762
763 /*
764 * must pass period=weight in order to get the correct
765 * sorting from hists__collapse_resort() which is solely
766 * based on periods. We want sorting be done on nr_events * weight
767 * and this is indirectly achieved by passing period=weight here
768 * and the he_stat__add_period() function.
769 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900770 sample->period = cost;
771
Jiri Olsa0102ef32016-06-14 20:19:21 +0200772 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
773 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900774 if (!he)
775 return -ENOMEM;
776
777 iter->he = he;
778 return 0;
779}
780
781static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900782iter_finish_mem_entry(struct hist_entry_iter *iter,
783 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900784{
785 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300786 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900787 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900788 int err = -EINVAL;
789
790 if (he == NULL)
791 goto out;
792
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300793 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900794
795 err = hist_entry__append_callchain(he, iter->sample);
796
797out:
798 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300799 * We don't need to free iter->priv (mem_info) here since the mem info
800 * was either already freed in hists__findnew_entry() or passed to a
801 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900802 */
803 iter->priv = NULL;
804
805 iter->he = NULL;
806 return err;
807}
808
809static int
810iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
811{
812 struct branch_info *bi;
813 struct perf_sample *sample = iter->sample;
814
815 bi = sample__resolve_bstack(sample, al);
816 if (!bi)
817 return -ENOMEM;
818
819 iter->curr = 0;
820 iter->total = sample->branch_stack->nr;
821
822 iter->priv = bi;
823 return 0;
824}
825
826static int
Jin Yao2d78b182017-07-18 20:13:14 +0800827iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900828 struct addr_location *al __maybe_unused)
829{
830 return 0;
831}
832
833static int
834iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
835{
836 struct branch_info *bi = iter->priv;
837 int i = iter->curr;
838
839 if (bi == NULL)
840 return 0;
841
842 if (iter->curr >= iter->total)
843 return 0;
844
845 al->map = bi[i].to.map;
846 al->sym = bi[i].to.sym;
847 al->addr = bi[i].to.addr;
848 return 1;
849}
850
851static int
852iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
853{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900854 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900855 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300856 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900857 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900858 struct hist_entry *he = NULL;
859 int i = iter->curr;
860 int err = 0;
861
862 bi = iter->priv;
863
864 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
865 goto out;
866
867 /*
868 * The report shows the percentage of total branches captured
869 * and not events sampled. Thus we use a pseudo period of 1.
870 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900871 sample->period = 1;
872 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
873
Jiri Olsa0102ef32016-06-14 20:19:21 +0200874 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
875 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900876 if (he == NULL)
877 return -ENOMEM;
878
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300879 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900880
881out:
882 iter->he = he;
883 iter->curr++;
884 return err;
885}
886
887static int
888iter_finish_branch_entry(struct hist_entry_iter *iter,
889 struct addr_location *al __maybe_unused)
890{
891 zfree(&iter->priv);
892 iter->he = NULL;
893
894 return iter->curr >= iter->total ? 0 : -1;
895}
896
897static int
898iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
899 struct addr_location *al __maybe_unused)
900{
901 return 0;
902}
903
904static int
905iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
906{
907 struct perf_evsel *evsel = iter->evsel;
908 struct perf_sample *sample = iter->sample;
909 struct hist_entry *he;
910
Jiri Olsa0102ef32016-06-14 20:19:21 +0200911 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
912 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900913 if (he == NULL)
914 return -ENOMEM;
915
916 iter->he = he;
917 return 0;
918}
919
920static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900921iter_finish_normal_entry(struct hist_entry_iter *iter,
922 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900923{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900924 struct hist_entry *he = iter->he;
925 struct perf_evsel *evsel = iter->evsel;
926 struct perf_sample *sample = iter->sample;
927
928 if (he == NULL)
929 return 0;
930
931 iter->he = NULL;
932
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300933 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900934
935 return hist_entry__append_callchain(he, sample);
936}
937
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900938static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300939iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900940 struct addr_location *al __maybe_unused)
941{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900942 struct hist_entry **he_cache;
943
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900944 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900945
946 /*
947 * This is for detecting cycles or recursions so that they're
948 * cumulated only one time to prevent entries more than 100%
949 * overhead.
950 */
Jiri Olsae3ebaa42018-02-16 13:36:19 +0100951 he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900952 if (he_cache == NULL)
953 return -ENOMEM;
954
955 iter->priv = he_cache;
956 iter->curr = 0;
957
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900958 return 0;
959}
960
961static int
962iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
963 struct addr_location *al)
964{
965 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300966 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900967 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900968 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900969 struct hist_entry *he;
970 int err = 0;
971
Jiri Olsa0102ef32016-06-14 20:19:21 +0200972 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
973 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900974 if (he == NULL)
975 return -ENOMEM;
976
977 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900978 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900979
Namhyung Kim82aa0192014-12-22 13:44:14 +0900980 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900981
982 /*
983 * We need to re-initialize the cursor since callchain_append()
984 * advanced the cursor to the end.
985 */
986 callchain_cursor_commit(&callchain_cursor);
987
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300988 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900989
990 return err;
991}
992
993static int
994iter_next_cumulative_entry(struct hist_entry_iter *iter,
995 struct addr_location *al)
996{
997 struct callchain_cursor_node *node;
998
999 node = callchain_cursor_current(&callchain_cursor);
1000 if (node == NULL)
1001 return 0;
1002
Namhyung Kimc7405d82013-10-31 13:58:30 +09001003 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001004}
1005
1006static int
1007iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
1008 struct addr_location *al)
1009{
1010 struct perf_evsel *evsel = iter->evsel;
1011 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001012 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001013 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001014 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -03001015 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001016 .cpu = al->cpu,
1017 .thread = al->thread,
1018 .comm = thread__comm(al->thread),
1019 .ip = al->addr,
1020 .ms = {
1021 .map = al->map,
1022 .sym = al->sym,
1023 },
Jiri Olsa26349582019-03-05 16:25:31 +01001024 .srcline = (char *) al->srcline,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001025 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +09001026 .raw_data = sample->raw_data,
1027 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001028 };
1029 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +09001030 struct callchain_cursor cursor;
1031
1032 callchain_cursor_snapshot(&cursor, &callchain_cursor);
1033
1034 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001035
1036 /*
1037 * Check if there's duplicate entries in the callchain.
1038 * It's possible that it has cycles or recursive calls.
1039 */
1040 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001041 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
1042 /* to avoid calling callback function */
1043 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001044 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001045 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001046 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001047
Jiri Olsa0102ef32016-06-14 20:19:21 +02001048 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
1049 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001050 if (he == NULL)
1051 return -ENOMEM;
1052
1053 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001054 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001055
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001056 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
Namhyung Kim82aa0192014-12-22 13:44:14 +09001057 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001058 return 0;
1059}
1060
1061static int
1062iter_finish_cumulative_entry(struct hist_entry_iter *iter,
1063 struct addr_location *al __maybe_unused)
1064{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001065 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001066 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001067
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001068 return 0;
1069}
1070
Namhyung Kim69bcb012013-10-30 09:40:34 +09001071const struct hist_iter_ops hist_iter_mem = {
1072 .prepare_entry = iter_prepare_mem_entry,
1073 .add_single_entry = iter_add_single_mem_entry,
1074 .next_entry = iter_next_nop_entry,
1075 .add_next_entry = iter_add_next_nop_entry,
1076 .finish_entry = iter_finish_mem_entry,
1077};
1078
1079const struct hist_iter_ops hist_iter_branch = {
1080 .prepare_entry = iter_prepare_branch_entry,
1081 .add_single_entry = iter_add_single_branch_entry,
1082 .next_entry = iter_next_branch_entry,
1083 .add_next_entry = iter_add_next_branch_entry,
1084 .finish_entry = iter_finish_branch_entry,
1085};
1086
1087const struct hist_iter_ops hist_iter_normal = {
1088 .prepare_entry = iter_prepare_normal_entry,
1089 .add_single_entry = iter_add_single_normal_entry,
1090 .next_entry = iter_next_nop_entry,
1091 .add_next_entry = iter_add_next_nop_entry,
1092 .finish_entry = iter_finish_normal_entry,
1093};
1094
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001095const struct hist_iter_ops hist_iter_cumulative = {
1096 .prepare_entry = iter_prepare_cumulative_entry,
1097 .add_single_entry = iter_add_single_cumulative_entry,
1098 .next_entry = iter_next_cumulative_entry,
1099 .add_next_entry = iter_add_next_cumulative_entry,
1100 .finish_entry = iter_finish_cumulative_entry,
1101};
1102
Namhyung Kim69bcb012013-10-30 09:40:34 +09001103int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001104 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +09001105{
1106 int err, err2;
Krister Johansen9c68ae92017-01-05 22:23:31 -08001107 struct map *alm = NULL;
1108
Arnaldo Carvalho de Melo362379a2018-05-24 11:20:39 -03001109 if (al)
Krister Johansen9c68ae92017-01-05 22:23:31 -08001110 alm = map__get(al->map);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001111
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001112 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
Namhyung Kim063bd932015-05-19 17:04:10 +09001113 iter->evsel, al, max_stack_depth);
Changbin Ducb6186a2019-03-16 16:05:49 +08001114 if (err) {
1115 map__put(alm);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001116 return err;
Changbin Ducb6186a2019-03-16 16:05:49 +08001117 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001118
Namhyung Kim69bcb012013-10-30 09:40:34 +09001119 err = iter->ops->prepare_entry(iter, al);
1120 if (err)
1121 goto out;
1122
1123 err = iter->ops->add_single_entry(iter, al);
1124 if (err)
1125 goto out;
1126
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001127 if (iter->he && iter->add_entry_cb) {
1128 err = iter->add_entry_cb(iter, al, true, arg);
1129 if (err)
1130 goto out;
1131 }
1132
Namhyung Kim69bcb012013-10-30 09:40:34 +09001133 while (iter->ops->next_entry(iter, al)) {
1134 err = iter->ops->add_next_entry(iter, al);
1135 if (err)
1136 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001137
1138 if (iter->he && iter->add_entry_cb) {
1139 err = iter->add_entry_cb(iter, al, false, arg);
1140 if (err)
1141 goto out;
1142 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001143 }
1144
1145out:
1146 err2 = iter->ops->finish_entry(iter, al);
1147 if (!err)
1148 err = err2;
1149
Krister Johansen9c68ae92017-01-05 22:23:31 -08001150 map__put(alm);
1151
Namhyung Kim69bcb012013-10-30 09:40:34 +09001152 return err;
1153}
1154
John Kacur3d1d07e2009-09-28 15:32:55 +02001155int64_t
1156hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1157{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001158 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001159 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001160 int64_t cmp = 0;
1161
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001162 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001163 if (perf_hpp__is_dynamic_entry(fmt) &&
1164 !perf_hpp__defined_dynamic_entry(fmt, hists))
1165 continue;
1166
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001167 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001168 if (cmp)
1169 break;
1170 }
1171
1172 return cmp;
1173}
1174
1175int64_t
1176hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1177{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001178 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001179 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001180 int64_t cmp = 0;
1181
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001182 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001183 if (perf_hpp__is_dynamic_entry(fmt) &&
1184 !perf_hpp__defined_dynamic_entry(fmt, hists))
1185 continue;
1186
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001187 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001188 if (cmp)
1189 break;
1190 }
1191
1192 return cmp;
1193}
1194
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001195void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001196{
Jiri Olsaf542e762016-07-05 08:56:04 +02001197 struct hist_entry_ops *ops = he->ops;
1198
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001199 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001200 map__zput(he->ms.map);
1201
1202 if (he->branch_info) {
1203 map__zput(he->branch_info->from.map);
1204 map__zput(he->branch_info->to.map);
Andi Kleen508be0d2016-05-20 13:15:08 -07001205 free_srcline(he->branch_info->srcline_from);
1206 free_srcline(he->branch_info->srcline_to);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001207 zfree(&he->branch_info);
1208 }
1209
1210 if (he->mem_info) {
1211 map__zput(he->mem_info->iaddr.map);
1212 map__zput(he->mem_info->daddr.map);
Jiri Olsa9f874982018-03-07 16:50:06 +01001213 mem_info__zput(he->mem_info);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001214 }
1215
Andi Kleen4968ac82019-03-11 07:44:58 -07001216 zfree(&he->res_samples);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001217 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +09001218 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -07001219 if (he->srcfile && he->srcfile[0])
1220 free(he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001221 free_callchain(he->callchain);
Namhyung Kim60517d22015-12-23 02:07:03 +09001222 free(he->trace_output);
Namhyung Kim72392832015-12-24 11:16:17 +09001223 free(he->raw_data);
Jiri Olsaf542e762016-07-05 08:56:04 +02001224 ops->free(he);
John Kacur3d1d07e2009-09-28 15:32:55 +02001225}
1226
1227/*
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001228 * If this is not the last column, then we need to pad it according to the
Ingo Molnaradba1632018-12-03 11:22:00 +01001229 * pre-calculated max length for this column, otherwise don't bother adding
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001230 * spaces because that would break viewing this with, for instance, 'less',
1231 * that would show tons of trailing spaces when a long C++ demangled method
1232 * names is sampled.
1233*/
1234int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1235 struct perf_hpp_fmt *fmt, int printed)
1236{
1237 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
Jiri Olsada1b0402016-06-14 20:19:20 +02001238 const int width = fmt->width(fmt, hpp, he->hists);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001239 if (printed < width) {
1240 advance_hpp(hpp, printed);
1241 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1242 }
1243 }
1244
1245 return printed;
1246}
1247
1248/*
John Kacur3d1d07e2009-09-28 15:32:55 +02001249 * collapse the histogram
1250 */
1251
Namhyung Kimaef810e2016-02-25 00:13:34 +09001252static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
Namhyung Kimaec13a72016-03-09 22:46:58 +09001253static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1254 enum hist_filter type);
1255
1256typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1257
1258static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1259{
1260 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1261}
1262
1263static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1264 enum hist_filter type,
1265 fmt_chk_fn check)
1266{
1267 struct perf_hpp_fmt *fmt;
1268 bool type_match = false;
1269 struct hist_entry *parent = he->parent_he;
1270
1271 switch (type) {
1272 case HIST_FILTER__THREAD:
1273 if (symbol_conf.comm_list == NULL &&
1274 symbol_conf.pid_list == NULL &&
1275 symbol_conf.tid_list == NULL)
1276 return;
1277 break;
1278 case HIST_FILTER__DSO:
1279 if (symbol_conf.dso_list == NULL)
1280 return;
1281 break;
1282 case HIST_FILTER__SYMBOL:
1283 if (symbol_conf.sym_list == NULL)
1284 return;
1285 break;
1286 case HIST_FILTER__PARENT:
1287 case HIST_FILTER__GUEST:
1288 case HIST_FILTER__HOST:
1289 case HIST_FILTER__SOCKET:
Jiri Olsa9857b712016-08-17 14:55:23 +02001290 case HIST_FILTER__C2C:
Namhyung Kimaec13a72016-03-09 22:46:58 +09001291 default:
1292 return;
1293 }
1294
1295 /* if it's filtered by own fmt, it has to have filter bits */
1296 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1297 if (check(fmt)) {
1298 type_match = true;
1299 break;
1300 }
1301 }
1302
1303 if (type_match) {
1304 /*
1305 * If the filter is for current level entry, propagate
1306 * filter marker to parents. The marker bit was
1307 * already set by default so it only needs to clear
1308 * non-filtered entries.
1309 */
1310 if (!(he->filtered & (1 << type))) {
1311 while (parent) {
1312 parent->filtered &= ~(1 << type);
1313 parent = parent->parent_he;
1314 }
1315 }
1316 } else {
1317 /*
1318 * If current entry doesn't have matching formats, set
1319 * filter marker for upper level entries. it will be
1320 * cleared if its lower level entries is not filtered.
1321 *
1322 * For lower-level entries, it inherits parent's
1323 * filter bit so that lower level entries of a
1324 * non-filtered entry won't set the filter marker.
1325 */
1326 if (parent == NULL)
1327 he->filtered |= (1 << type);
1328 else
1329 he->filtered |= (parent->filtered & (1 << type));
1330 }
1331}
1332
1333static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1334{
1335 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1336 check_thread_entry);
1337
1338 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1339 perf_hpp__is_dso_entry);
1340
1341 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1342 perf_hpp__is_sym_entry);
1343
1344 hists__apply_filters(he->hists, he);
1345}
Namhyung Kimaef810e2016-02-25 00:13:34 +09001346
1347static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001348 struct rb_root_cached *root,
Namhyung Kimaef810e2016-02-25 00:13:34 +09001349 struct hist_entry *he,
Namhyung Kimaec13a72016-03-09 22:46:58 +09001350 struct hist_entry *parent_he,
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001351 struct perf_hpp_list *hpp_list)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001352{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001353 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001354 struct rb_node *parent = NULL;
1355 struct hist_entry *iter, *new;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001356 struct perf_hpp_fmt *fmt;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001357 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001358 bool leftmost = true;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001359
1360 while (*p != NULL) {
1361 parent = *p;
1362 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1363
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001364 cmp = 0;
1365 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1366 cmp = fmt->collapse(fmt, iter, he);
1367 if (cmp)
1368 break;
1369 }
1370
Namhyung Kimaef810e2016-02-25 00:13:34 +09001371 if (!cmp) {
1372 he_stat__add_stat(&iter->stat, &he->stat);
1373 return iter;
1374 }
1375
1376 if (cmp < 0)
1377 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001378 else {
Namhyung Kimaef810e2016-02-25 00:13:34 +09001379 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001380 leftmost = false;
1381 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001382 }
1383
1384 new = hist_entry__new(he, true);
1385 if (new == NULL)
1386 return NULL;
1387
Namhyung Kimaef810e2016-02-25 00:13:34 +09001388 hists->nr_entries++;
1389
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001390 /* save related format list for output */
1391 new->hpp_list = hpp_list;
Namhyung Kimaec13a72016-03-09 22:46:58 +09001392 new->parent_he = parent_he;
1393
1394 hist_entry__apply_hierarchy_filters(new);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001395
1396 /* some fields are now passed to 'new' */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001397 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1398 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1399 he->trace_output = NULL;
1400 else
1401 new->trace_output = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001402
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001403 if (perf_hpp__is_srcline_entry(fmt))
1404 he->srcline = NULL;
1405 else
1406 new->srcline = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001407
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001408 if (perf_hpp__is_srcfile_entry(fmt))
1409 he->srcfile = NULL;
1410 else
1411 new->srcfile = NULL;
1412 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001413
1414 rb_link_node(&new->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001415 rb_insert_color_cached(&new->rb_node_in, root, leftmost);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001416 return new;
1417}
1418
1419static int hists__hierarchy_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001420 struct rb_root_cached *root,
Namhyung Kimaef810e2016-02-25 00:13:34 +09001421 struct hist_entry *he)
1422{
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001423 struct perf_hpp_list_node *node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001424 struct hist_entry *new_he = NULL;
1425 struct hist_entry *parent = NULL;
1426 int depth = 0;
1427 int ret = 0;
1428
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001429 list_for_each_entry(node, &hists->hpp_formats, list) {
1430 /* skip period (overhead) and elided columns */
1431 if (node->level == 0 || node->skip)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001432 continue;
1433
1434 /* insert copy of 'he' for each fmt into the hierarchy */
Namhyung Kimaec13a72016-03-09 22:46:58 +09001435 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001436 if (new_he == NULL) {
1437 ret = -1;
1438 break;
1439 }
1440
1441 root = &new_he->hroot_in;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001442 new_he->depth = depth++;
1443 parent = new_he;
1444 }
1445
1446 if (new_he) {
1447 new_he->leaf = true;
1448
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001449 if (hist_entry__has_callchains(new_he) &&
1450 symbol_conf.use_callchain) {
Namhyung Kimaef810e2016-02-25 00:13:34 +09001451 callchain_cursor_reset(&callchain_cursor);
1452 if (callchain_merge(&callchain_cursor,
1453 new_he->callchain,
1454 he->callchain) < 0)
1455 ret = -1;
1456 }
1457 }
1458
1459 /* 'he' is no longer used */
1460 hist_entry__delete(he);
1461
1462 /* return 0 (or -1) since it already applied filters */
1463 return ret;
1464}
1465
Jiri Olsa592dac62016-03-24 13:52:17 +01001466static int hists__collapse_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001467 struct rb_root_cached *root,
Jiri Olsa592dac62016-03-24 13:52:17 +01001468 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001469{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001470 struct rb_node **p = &root->rb_root.rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001471 struct rb_node *parent = NULL;
1472 struct hist_entry *iter;
1473 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001474 bool leftmost = true;
John Kacur3d1d07e2009-09-28 15:32:55 +02001475
Namhyung Kimaef810e2016-02-25 00:13:34 +09001476 if (symbol_conf.report_hierarchy)
1477 return hists__hierarchy_insert_entry(hists, root, he);
1478
John Kacur3d1d07e2009-09-28 15:32:55 +02001479 while (*p != NULL) {
1480 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001481 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001482
1483 cmp = hist_entry__collapse(iter, he);
1484
1485 if (!cmp) {
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001486 int ret = 0;
1487
Namhyung Kim139c0812012-10-04 21:49:43 +09001488 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001489 if (symbol_conf.cumulate_callchain)
1490 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001491
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001492 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001493 callchain_cursor_reset(&callchain_cursor);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001494 if (callchain_merge(&callchain_cursor,
1495 iter->callchain,
1496 he->callchain) < 0)
1497 ret = -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001498 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001499 hist_entry__delete(he);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001500 return ret;
John Kacur3d1d07e2009-09-28 15:32:55 +02001501 }
1502
1503 if (cmp < 0)
1504 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001505 else {
John Kacur3d1d07e2009-09-28 15:32:55 +02001506 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001507 leftmost = false;
1508 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001509 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001510 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001511
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001512 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001513 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001514 return 1;
John Kacur3d1d07e2009-09-28 15:32:55 +02001515}
1516
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001517struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001518{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001519 struct rb_root_cached *root;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001520
1521 pthread_mutex_lock(&hists->lock);
1522
1523 root = hists->entries_in;
1524 if (++hists->entries_in > &hists->entries_in_array[1])
1525 hists->entries_in = &hists->entries_in_array[0];
1526
1527 pthread_mutex_unlock(&hists->lock);
1528
1529 return root;
1530}
1531
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001532static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1533{
1534 hists__filter_entry_by_dso(hists, he);
1535 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001536 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001537 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001538}
1539
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001540int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001541{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001542 struct rb_root_cached *root;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001543 struct rb_node *next;
1544 struct hist_entry *n;
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001545 int ret;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001546
Jiri Olsa52225032016-05-03 13:54:42 +02001547 if (!hists__has(hists, need_collapse))
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001548 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001549
Namhyung Kim740b97f2014-12-22 13:44:10 +09001550 hists->nr_entries = 0;
1551
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001552 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001553
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001554 next = rb_first_cached(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001555
1556 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001557 if (session_done())
1558 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001559 n = rb_entry(next, struct hist_entry, rb_node_in);
1560 next = rb_next(&n->rb_node_in);
1561
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001562 rb_erase_cached(&n->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001563 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1564 if (ret < 0)
1565 return -1;
1566
1567 if (ret) {
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001568 /*
1569 * If it wasn't combined with one of the entries already
1570 * collapsed, we need to apply the filters that may have
1571 * been set by, say, the hist_browser.
1572 */
1573 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001574 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001575 if (prog)
1576 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001577 }
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001578 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001579}
1580
Namhyung Kim043ca3892014-03-03 14:18:00 +09001581static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001582{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001583 struct hists *hists = a->hists;
Namhyung Kim043ca3892014-03-03 14:18:00 +09001584 struct perf_hpp_fmt *fmt;
1585 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001586
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001587 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001588 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001589 continue;
1590
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001591 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001592 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001593 break;
1594 }
1595
Namhyung Kim043ca3892014-03-03 14:18:00 +09001596 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001597}
1598
Namhyung Kim9283ba92014-04-24 16:37:26 +09001599static void hists__reset_filter_stats(struct hists *hists)
1600{
1601 hists->nr_non_filtered_entries = 0;
1602 hists->stats.total_non_filtered_period = 0;
1603}
1604
1605void hists__reset_stats(struct hists *hists)
1606{
1607 hists->nr_entries = 0;
1608 hists->stats.total_period = 0;
1609
1610 hists__reset_filter_stats(hists);
1611}
1612
1613static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1614{
1615 hists->nr_non_filtered_entries++;
1616 hists->stats.total_non_filtered_period += h->stat.period;
1617}
1618
1619void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1620{
1621 if (!h->filtered)
1622 hists__inc_filter_stats(hists, h);
1623
1624 hists->nr_entries++;
1625 hists->stats.total_period += h->stat.period;
1626}
1627
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001628static void hierarchy_recalc_total_periods(struct hists *hists)
1629{
1630 struct rb_node *node;
1631 struct hist_entry *he;
1632
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001633 node = rb_first_cached(&hists->entries);
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001634
1635 hists->stats.total_period = 0;
1636 hists->stats.total_non_filtered_period = 0;
1637
1638 /*
1639 * recalculate total period using top-level entries only
1640 * since lower level entries only see non-filtered entries
1641 * but upper level entries have sum of both entries.
1642 */
1643 while (node) {
1644 he = rb_entry(node, struct hist_entry, rb_node);
1645 node = rb_next(node);
1646
1647 hists->stats.total_period += he->stat.period;
1648 if (!he->filtered)
1649 hists->stats.total_non_filtered_period += he->stat.period;
1650 }
1651}
1652
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001653static void hierarchy_insert_output_entry(struct rb_root_cached *root,
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001654 struct hist_entry *he)
1655{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001656 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001657 struct rb_node *parent = NULL;
1658 struct hist_entry *iter;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001659 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001660 bool leftmost = true;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001661
1662 while (*p != NULL) {
1663 parent = *p;
1664 iter = rb_entry(parent, struct hist_entry, rb_node);
1665
1666 if (hist_entry__sort(he, iter) > 0)
1667 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001668 else {
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001669 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001670 leftmost = false;
1671 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001672 }
1673
1674 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001675 rb_insert_color_cached(&he->rb_node, root, leftmost);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001676
1677 /* update column width of dynamic entry */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001678 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1679 if (perf_hpp__is_dynamic_entry(fmt))
1680 fmt->sort(fmt, he, NULL);
1681 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001682}
1683
1684static void hists__hierarchy_output_resort(struct hists *hists,
1685 struct ui_progress *prog,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001686 struct rb_root_cached *root_in,
1687 struct rb_root_cached *root_out,
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001688 u64 min_callchain_hits,
1689 bool use_callchain)
1690{
1691 struct rb_node *node;
1692 struct hist_entry *he;
1693
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001694 *root_out = RB_ROOT_CACHED;
1695 node = rb_first_cached(root_in);
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001696
1697 while (node) {
1698 he = rb_entry(node, struct hist_entry, rb_node_in);
1699 node = rb_next(node);
1700
1701 hierarchy_insert_output_entry(root_out, he);
1702
1703 if (prog)
1704 ui_progress__update(prog, 1);
1705
Namhyung Kimc72ab442016-11-08 22:08:33 +09001706 hists->nr_entries++;
1707 if (!he->filtered) {
1708 hists->nr_non_filtered_entries++;
1709 hists__calc_col_len(hists, he);
1710 }
1711
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001712 if (!he->leaf) {
1713 hists__hierarchy_output_resort(hists, prog,
1714 &he->hroot_in,
1715 &he->hroot_out,
1716 min_callchain_hits,
1717 use_callchain);
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001718 continue;
1719 }
1720
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001721 if (!use_callchain)
1722 continue;
1723
1724 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1725 u64 total = he->stat.period;
1726
1727 if (symbol_conf.cumulate_callchain)
1728 total = he->stat_acc->period;
1729
1730 min_callchain_hits = total * (callchain_param.min_percent / 100);
1731 }
1732
1733 callchain_param.sort(&he->sorted_chain, he->callchain,
1734 min_callchain_hits, &callchain_param);
1735 }
1736}
1737
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001738static void __hists__insert_output_entry(struct rb_root_cached *entries,
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001739 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001740 u64 min_callchain_hits,
1741 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001742{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001743 struct rb_node **p = &entries->rb_root.rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001744 struct rb_node *parent = NULL;
1745 struct hist_entry *iter;
Namhyung Kimabab5e72016-02-27 03:52:47 +09001746 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001747 bool leftmost = true;
John Kacur3d1d07e2009-09-28 15:32:55 +02001748
Namhyung Kim744070e2016-01-28 00:40:48 +09001749 if (use_callchain) {
1750 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1751 u64 total = he->stat.period;
1752
1753 if (symbol_conf.cumulate_callchain)
1754 total = he->stat_acc->period;
1755
1756 min_callchain_hits = total * (callchain_param.min_percent / 100);
1757 }
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001758 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001759 min_callchain_hits, &callchain_param);
Namhyung Kim744070e2016-01-28 00:40:48 +09001760 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001761
1762 while (*p != NULL) {
1763 parent = *p;
1764 iter = rb_entry(parent, struct hist_entry, rb_node);
1765
Namhyung Kim043ca3892014-03-03 14:18:00 +09001766 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001767 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001768 else {
John Kacur3d1d07e2009-09-28 15:32:55 +02001769 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001770 leftmost = false;
1771 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001772 }
1773
1774 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001775 rb_insert_color_cached(&he->rb_node, entries, leftmost);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001776
1777 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1778 if (perf_hpp__is_dynamic_entry(fmt) &&
1779 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1780 fmt->sort(fmt, he, NULL); /* update column width */
1781 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001782}
1783
Jiri Olsa01441af2016-01-18 10:23:59 +01001784static void output_resort(struct hists *hists, struct ui_progress *prog,
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001785 bool use_callchain, hists__resort_cb_t cb,
1786 void *cb_arg)
John Kacur3d1d07e2009-09-28 15:32:55 +02001787{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001788 struct rb_root_cached *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001789 struct rb_node *next;
1790 struct hist_entry *n;
Namhyung Kim467ef102016-02-16 23:08:19 +09001791 u64 callchain_total;
John Kacur3d1d07e2009-09-28 15:32:55 +02001792 u64 min_callchain_hits;
1793
Namhyung Kim467ef102016-02-16 23:08:19 +09001794 callchain_total = hists->callchain_period;
1795 if (symbol_conf.filter_relative)
1796 callchain_total = hists->callchain_non_filtered_period;
1797
1798 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001799
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001800 hists__reset_stats(hists);
1801 hists__reset_col_len(hists);
1802
1803 if (symbol_conf.report_hierarchy) {
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001804 hists__hierarchy_output_resort(hists, prog,
1805 &hists->entries_collapsed,
1806 &hists->entries,
1807 min_callchain_hits,
1808 use_callchain);
1809 hierarchy_recalc_total_periods(hists);
1810 return;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001811 }
1812
Jiri Olsa52225032016-05-03 13:54:42 +02001813 if (hists__has(hists, need_collapse))
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001814 root = &hists->entries_collapsed;
1815 else
1816 root = hists->entries_in;
1817
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001818 next = rb_first_cached(root);
1819 hists->entries = RB_ROOT_CACHED;
John Kacur3d1d07e2009-09-28 15:32:55 +02001820
1821 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001822 n = rb_entry(next, struct hist_entry, rb_node_in);
1823 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001824
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001825 if (cb && cb(n, cb_arg))
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001826 continue;
1827
Kan Liangf9db0d02015-08-11 06:30:48 -04001828 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001829 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001830
1831 if (!n->filtered)
1832 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001833
1834 if (prog)
1835 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001836 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001837}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001838
Jiri Olsa57496182019-02-04 15:18:07 +01001839void perf_evsel__output_resort_cb(struct perf_evsel *evsel, struct ui_progress *prog,
1840 hists__resort_cb_t cb, void *cb_arg)
Jiri Olsa01441af2016-01-18 10:23:59 +01001841{
Jiri Olsa01441af2016-01-18 10:23:59 +01001842 bool use_callchain;
1843
1844 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -03001845 use_callchain = evsel__has_callchain(evsel);
Jiri Olsa01441af2016-01-18 10:23:59 +01001846 else
1847 use_callchain = symbol_conf.use_callchain;
1848
Jin Yaob49a8212017-05-08 18:43:02 +08001849 use_callchain |= symbol_conf.show_branchflag_count;
1850
Jiri Olsa57496182019-02-04 15:18:07 +01001851 output_resort(evsel__hists(evsel), prog, use_callchain, cb, cb_arg);
1852}
1853
1854void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
1855{
1856 return perf_evsel__output_resort_cb(evsel, prog, NULL, NULL);
Jiri Olsa452ce032016-01-18 10:24:00 +01001857}
1858
1859void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1860{
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001861 output_resort(hists, prog, symbol_conf.use_callchain, NULL, NULL);
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001862}
1863
1864void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1865 hists__resort_cb_t cb)
1866{
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001867 output_resort(hists, prog, symbol_conf.use_callchain, cb, NULL);
Jiri Olsa01441af2016-01-18 10:23:59 +01001868}
1869
Namhyung Kim8c018722016-02-25 00:13:36 +09001870static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1871{
1872 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1873 return false;
1874
1875 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1876 return true;
1877
1878 return false;
1879}
1880
1881struct rb_node *rb_hierarchy_last(struct rb_node *node)
1882{
1883 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1884
1885 while (can_goto_child(he, HMD_NORMAL)) {
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001886 node = rb_last(&he->hroot_out.rb_root);
Namhyung Kim8c018722016-02-25 00:13:36 +09001887 he = rb_entry(node, struct hist_entry, rb_node);
1888 }
1889 return node;
1890}
1891
1892struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1893{
1894 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1895
1896 if (can_goto_child(he, hmd))
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001897 node = rb_first_cached(&he->hroot_out);
Namhyung Kim8c018722016-02-25 00:13:36 +09001898 else
1899 node = rb_next(node);
1900
1901 while (node == NULL) {
1902 he = he->parent_he;
1903 if (he == NULL)
1904 break;
1905
1906 node = rb_next(&he->rb_node);
1907 }
1908 return node;
1909}
1910
1911struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1912{
1913 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1914
1915 node = rb_prev(node);
1916 if (node)
1917 return rb_hierarchy_last(node);
1918
1919 he = he->parent_he;
1920 if (he == NULL)
1921 return NULL;
1922
1923 return &he->rb_node;
1924}
1925
Namhyung Kima7b58952016-02-26 21:13:16 +09001926bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1927{
1928 struct rb_node *node;
1929 struct hist_entry *child;
1930 float percent;
1931
1932 if (he->leaf)
1933 return false;
1934
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001935 node = rb_first_cached(&he->hroot_out);
Namhyung Kima7b58952016-02-26 21:13:16 +09001936 child = rb_entry(node, struct hist_entry, rb_node);
1937
1938 while (node && child->filtered) {
1939 node = rb_next(node);
1940 child = rb_entry(node, struct hist_entry, rb_node);
1941 }
1942
1943 if (node)
1944 percent = hist_entry__get_percent_limit(child);
1945 else
1946 percent = 0;
1947
1948 return node && percent >= limit;
1949}
1950
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001951static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001952 enum hist_filter filter)
1953{
1954 h->filtered &= ~(1 << filter);
Namhyung Kim155e9af2016-02-25 00:13:38 +09001955
1956 if (symbol_conf.report_hierarchy) {
1957 struct hist_entry *parent = h->parent_he;
1958
1959 while (parent) {
1960 he_stat__add_stat(&parent->stat, &h->stat);
1961
1962 parent->filtered &= ~(1 << filter);
1963
1964 if (parent->filtered)
1965 goto next;
1966
1967 /* force fold unfiltered entry for simplicity */
1968 parent->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001969 parent->has_no_entry = false;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001970 parent->row_offset = 0;
1971 parent->nr_rows = 0;
1972next:
1973 parent = parent->parent_he;
1974 }
1975 }
1976
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001977 if (h->filtered)
1978 return;
1979
Namhyung Kim87e90f42014-04-24 16:44:16 +09001980 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001981 h->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001982 h->has_no_entry = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001983 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001984 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001985
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001986 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001987
Namhyung Kim9283ba92014-04-24 16:37:26 +09001988 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001989 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001990}
1991
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001992
1993static bool hists__filter_entry_by_dso(struct hists *hists,
1994 struct hist_entry *he)
1995{
1996 if (hists->dso_filter != NULL &&
1997 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1998 he->filtered |= (1 << HIST_FILTER__DSO);
1999 return true;
2000 }
2001
2002 return false;
2003}
2004
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02002005static bool hists__filter_entry_by_thread(struct hists *hists,
2006 struct hist_entry *he)
2007{
2008 if (hists->thread_filter != NULL &&
2009 he->thread != hists->thread_filter) {
2010 he->filtered |= (1 << HIST_FILTER__THREAD);
2011 return true;
2012 }
2013
2014 return false;
2015}
2016
Namhyung Kime94d53e2012-03-16 17:50:51 +09002017static bool hists__filter_entry_by_symbol(struct hists *hists,
2018 struct hist_entry *he)
2019{
2020 if (hists->symbol_filter_str != NULL &&
2021 (!he->ms.sym || strstr(he->ms.sym->name,
2022 hists->symbol_filter_str) == NULL)) {
2023 he->filtered |= (1 << HIST_FILTER__SYMBOL);
2024 return true;
2025 }
2026
2027 return false;
2028}
2029
Kan Liang21394d92015-09-04 10:45:44 -04002030static bool hists__filter_entry_by_socket(struct hists *hists,
2031 struct hist_entry *he)
2032{
2033 if ((hists->socket_filter > -1) &&
2034 (he->socket != hists->socket_filter)) {
2035 he->filtered |= (1 << HIST_FILTER__SOCKET);
2036 return true;
2037 }
2038
2039 return false;
2040}
2041
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002042typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
2043
2044static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04002045{
2046 struct rb_node *nd;
2047
2048 hists->stats.nr_non_filtered_samples = 0;
2049
2050 hists__reset_filter_stats(hists);
2051 hists__reset_col_len(hists);
2052
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002053 for (nd = rb_first_cached(&hists->entries); nd; nd = rb_next(nd)) {
Kan Liang84734b02015-09-04 10:45:45 -04002054 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2055
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002056 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04002057 continue;
2058
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002059 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04002060 }
2061}
2062
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002063static void resort_filtered_entry(struct rb_root_cached *root,
2064 struct hist_entry *he)
Namhyung Kim70642852016-02-25 00:13:39 +09002065{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002066 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kim70642852016-02-25 00:13:39 +09002067 struct rb_node *parent = NULL;
2068 struct hist_entry *iter;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002069 struct rb_root_cached new_root = RB_ROOT_CACHED;
Namhyung Kim70642852016-02-25 00:13:39 +09002070 struct rb_node *nd;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002071 bool leftmost = true;
Namhyung Kim70642852016-02-25 00:13:39 +09002072
2073 while (*p != NULL) {
2074 parent = *p;
2075 iter = rb_entry(parent, struct hist_entry, rb_node);
2076
2077 if (hist_entry__sort(he, iter) > 0)
2078 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002079 else {
Namhyung Kim70642852016-02-25 00:13:39 +09002080 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002081 leftmost = false;
2082 }
Namhyung Kim70642852016-02-25 00:13:39 +09002083 }
2084
2085 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002086 rb_insert_color_cached(&he->rb_node, root, leftmost);
Namhyung Kim70642852016-02-25 00:13:39 +09002087
2088 if (he->leaf || he->filtered)
2089 return;
2090
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002091 nd = rb_first_cached(&he->hroot_out);
Namhyung Kim70642852016-02-25 00:13:39 +09002092 while (nd) {
2093 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2094
2095 nd = rb_next(nd);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002096 rb_erase_cached(&h->rb_node, &he->hroot_out);
Namhyung Kim70642852016-02-25 00:13:39 +09002097
2098 resort_filtered_entry(&new_root, h);
2099 }
2100
2101 he->hroot_out = new_root;
2102}
2103
Namhyung Kim155e9af2016-02-25 00:13:38 +09002104static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2105{
2106 struct rb_node *nd;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002107 struct rb_root_cached new_root = RB_ROOT_CACHED;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002108
2109 hists->stats.nr_non_filtered_samples = 0;
2110
2111 hists__reset_filter_stats(hists);
2112 hists__reset_col_len(hists);
2113
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002114 nd = rb_first_cached(&hists->entries);
Namhyung Kim155e9af2016-02-25 00:13:38 +09002115 while (nd) {
2116 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2117 int ret;
2118
2119 ret = hist_entry__filter(h, type, arg);
2120
2121 /*
2122 * case 1. non-matching type
2123 * zero out the period, set filter marker and move to child
2124 */
2125 if (ret < 0) {
2126 memset(&h->stat, 0, sizeof(h->stat));
2127 h->filtered |= (1 << type);
2128
2129 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2130 }
2131 /*
2132 * case 2. matched type (filter out)
2133 * set filter marker and move to next
2134 */
2135 else if (ret == 1) {
2136 h->filtered |= (1 << type);
2137
2138 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2139 }
2140 /*
2141 * case 3. ok (not filtered)
2142 * add period to hists and parents, erase the filter marker
2143 * and move to next sibling
2144 */
2145 else {
2146 hists__remove_entry_filter(hists, h, type);
2147
2148 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2149 }
2150 }
Namhyung Kim70642852016-02-25 00:13:39 +09002151
Namhyung Kimf7fb5382016-03-09 22:47:02 +09002152 hierarchy_recalc_total_periods(hists);
2153
Namhyung Kim70642852016-02-25 00:13:39 +09002154 /*
2155 * resort output after applying a new filter since filter in a lower
2156 * hierarchy can change periods in a upper hierarchy.
2157 */
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002158 nd = rb_first_cached(&hists->entries);
Namhyung Kim70642852016-02-25 00:13:39 +09002159 while (nd) {
2160 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2161
2162 nd = rb_next(nd);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002163 rb_erase_cached(&h->rb_node, &hists->entries);
Namhyung Kim70642852016-02-25 00:13:39 +09002164
2165 resort_filtered_entry(&new_root, h);
2166 }
2167
2168 hists->entries = new_root;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002169}
2170
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002171void hists__filter_by_thread(struct hists *hists)
2172{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002173 if (symbol_conf.report_hierarchy)
2174 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2175 hists->thread_filter);
2176 else
2177 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2178 hists__filter_entry_by_thread);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002179}
2180
2181void hists__filter_by_dso(struct hists *hists)
2182{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002183 if (symbol_conf.report_hierarchy)
2184 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2185 hists->dso_filter);
2186 else
2187 hists__filter_by_type(hists, HIST_FILTER__DSO,
2188 hists__filter_entry_by_dso);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002189}
2190
2191void hists__filter_by_symbol(struct hists *hists)
2192{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002193 if (symbol_conf.report_hierarchy)
2194 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2195 hists->symbol_filter_str);
2196 else
2197 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2198 hists__filter_entry_by_symbol);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002199}
2200
2201void hists__filter_by_socket(struct hists *hists)
2202{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002203 if (symbol_conf.report_hierarchy)
2204 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2205 &hists->socket_filter);
2206 else
2207 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2208 hists__filter_entry_by_socket);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002209}
2210
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002211void events_stats__inc(struct events_stats *stats, u32 type)
2212{
2213 ++stats->nr_events[0];
2214 ++stats->nr_events[type];
2215}
2216
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002217void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002218{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002219 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002220}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002221
Namhyung Kim1844dbc2014-05-28 14:12:18 +09002222void hists__inc_nr_samples(struct hists *hists, bool filtered)
2223{
2224 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2225 if (!filtered)
2226 hists->stats.nr_non_filtered_samples++;
2227}
2228
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002229static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2230 struct hist_entry *pair)
2231{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002232 struct rb_root_cached *root;
Namhyung Kimce74f602012-12-10 17:29:55 +09002233 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002234 struct rb_node *parent = NULL;
2235 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07002236 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002237 bool leftmost = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002238
Jiri Olsa52225032016-05-03 13:54:42 +02002239 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002240 root = &hists->entries_collapsed;
2241 else
2242 root = hists->entries_in;
2243
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002244 p = &root->rb_root.rb_node;
Namhyung Kimce74f602012-12-10 17:29:55 +09002245
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002246 while (*p != NULL) {
2247 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09002248 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002249
Namhyung Kimce74f602012-12-10 17:29:55 +09002250 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002251
2252 if (!cmp)
2253 goto out;
2254
2255 if (cmp < 0)
2256 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002257 else {
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002258 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002259 leftmost = false;
2260 }
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002261 }
2262
Namhyung Kima0b51af2012-09-11 13:34:27 +09002263 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002264 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03002265 memset(&he->stat, 0, sizeof(he->stat));
2266 he->hists = hists;
Kan Liang09623d72016-04-24 23:28:09 -07002267 if (symbol_conf.cumulate_callchain)
2268 memset(he->stat_acc, 0, sizeof(he->stat));
Namhyung Kimce74f602012-12-10 17:29:55 +09002269 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002270 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kim62638352014-04-24 16:21:46 +09002271 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01002272 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002273 }
2274out:
2275 return he;
2276}
2277
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002278static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002279 struct rb_root_cached *root,
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002280 struct hist_entry *pair)
2281{
2282 struct rb_node **p;
2283 struct rb_node *parent = NULL;
2284 struct hist_entry *he;
2285 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002286 bool leftmost = true;
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002287
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002288 p = &root->rb_root.rb_node;
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002289 while (*p != NULL) {
2290 int64_t cmp = 0;
2291
2292 parent = *p;
2293 he = rb_entry(parent, struct hist_entry, rb_node_in);
2294
2295 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2296 cmp = fmt->collapse(fmt, he, pair);
2297 if (cmp)
2298 break;
2299 }
2300 if (!cmp)
2301 goto out;
2302
2303 if (cmp < 0)
2304 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002305 else {
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002306 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002307 leftmost = false;
2308 }
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002309 }
2310
2311 he = hist_entry__new(pair, true);
2312 if (he) {
2313 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002314 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002315
2316 he->dummy = true;
2317 he->hists = hists;
2318 memset(&he->stat, 0, sizeof(he->stat));
2319 hists__inc_stats(hists, he);
2320 }
2321out:
2322 return he;
2323}
2324
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002325static struct hist_entry *hists__find_entry(struct hists *hists,
2326 struct hist_entry *he)
2327{
Namhyung Kimce74f602012-12-10 17:29:55 +09002328 struct rb_node *n;
2329
Jiri Olsa52225032016-05-03 13:54:42 +02002330 if (hists__has(hists, need_collapse))
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002331 n = hists->entries_collapsed.rb_root.rb_node;
Namhyung Kimce74f602012-12-10 17:29:55 +09002332 else
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002333 n = hists->entries_in->rb_root.rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002334
2335 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002336 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2337 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002338
2339 if (cmp < 0)
2340 n = n->rb_left;
2341 else if (cmp > 0)
2342 n = n->rb_right;
2343 else
2344 return iter;
2345 }
2346
2347 return NULL;
2348}
2349
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002350static struct hist_entry *hists__find_hierarchy_entry(struct rb_root_cached *root,
Namhyung Kim09034de2016-09-13 16:45:46 +09002351 struct hist_entry *he)
2352{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002353 struct rb_node *n = root->rb_root.rb_node;
Namhyung Kim09034de2016-09-13 16:45:46 +09002354
2355 while (n) {
2356 struct hist_entry *iter;
2357 struct perf_hpp_fmt *fmt;
2358 int64_t cmp = 0;
2359
2360 iter = rb_entry(n, struct hist_entry, rb_node_in);
2361 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2362 cmp = fmt->collapse(fmt, iter, he);
2363 if (cmp)
2364 break;
2365 }
2366
2367 if (cmp < 0)
2368 n = n->rb_left;
2369 else if (cmp > 0)
2370 n = n->rb_right;
2371 else
2372 return iter;
2373 }
2374
2375 return NULL;
2376}
2377
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002378static void hists__match_hierarchy(struct rb_root_cached *leader_root,
2379 struct rb_root_cached *other_root)
Namhyung Kim09034de2016-09-13 16:45:46 +09002380{
2381 struct rb_node *nd;
2382 struct hist_entry *pos, *pair;
2383
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002384 for (nd = rb_first_cached(leader_root); nd; nd = rb_next(nd)) {
Namhyung Kim09034de2016-09-13 16:45:46 +09002385 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2386 pair = hists__find_hierarchy_entry(other_root, pos);
2387
2388 if (pair) {
2389 hist_entry__add_pair(pair, pos);
2390 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2391 }
2392 }
2393}
2394
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002395/*
2396 * Look for pairs to link to the leader buckets (hist_entries):
2397 */
2398void hists__match(struct hists *leader, struct hists *other)
2399{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002400 struct rb_root_cached *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002401 struct rb_node *nd;
2402 struct hist_entry *pos, *pair;
2403
Namhyung Kim09034de2016-09-13 16:45:46 +09002404 if (symbol_conf.report_hierarchy) {
2405 /* hierarchy report always collapses entries */
2406 return hists__match_hierarchy(&leader->entries_collapsed,
2407 &other->entries_collapsed);
2408 }
2409
Jiri Olsa52225032016-05-03 13:54:42 +02002410 if (hists__has(leader, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002411 root = &leader->entries_collapsed;
2412 else
2413 root = leader->entries_in;
2414
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002415 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002416 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002417 pair = hists__find_entry(other, pos);
2418
2419 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09002420 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002421 }
2422}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002423
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002424static int hists__link_hierarchy(struct hists *leader_hists,
2425 struct hist_entry *parent,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002426 struct rb_root_cached *leader_root,
2427 struct rb_root_cached *other_root)
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002428{
2429 struct rb_node *nd;
2430 struct hist_entry *pos, *leader;
2431
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002432 for (nd = rb_first_cached(other_root); nd; nd = rb_next(nd)) {
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002433 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2434
2435 if (hist_entry__has_pairs(pos)) {
2436 bool found = false;
2437
2438 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2439 if (leader->hists == leader_hists) {
2440 found = true;
2441 break;
2442 }
2443 }
2444 if (!found)
2445 return -1;
2446 } else {
2447 leader = add_dummy_hierarchy_entry(leader_hists,
2448 leader_root, pos);
2449 if (leader == NULL)
2450 return -1;
2451
2452 /* do not point parent in the pos */
2453 leader->parent_he = parent;
2454
2455 hist_entry__add_pair(pos, leader);
2456 }
2457
2458 if (!pos->leaf) {
2459 if (hists__link_hierarchy(leader_hists, leader,
2460 &leader->hroot_in,
2461 &pos->hroot_in) < 0)
2462 return -1;
2463 }
2464 }
2465 return 0;
2466}
2467
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002468/*
2469 * Look for entries in the other hists that are not present in the leader, if
2470 * we find them, just add a dummy entry on the leader hists, with period=0,
2471 * nr_events=0, to serve as the list header.
2472 */
2473int hists__link(struct hists *leader, struct hists *other)
2474{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002475 struct rb_root_cached *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002476 struct rb_node *nd;
2477 struct hist_entry *pos, *pair;
2478
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002479 if (symbol_conf.report_hierarchy) {
2480 /* hierarchy report always collapses entries */
2481 return hists__link_hierarchy(leader, NULL,
2482 &leader->entries_collapsed,
2483 &other->entries_collapsed);
2484 }
2485
Jiri Olsa52225032016-05-03 13:54:42 +02002486 if (hists__has(other, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002487 root = &other->entries_collapsed;
2488 else
2489 root = other->entries_in;
2490
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002491 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002492 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002493
2494 if (!hist_entry__has_pairs(pos)) {
2495 pair = hists__add_dummy_entry(leader, pos);
2496 if (pair == NULL)
2497 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09002498 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002499 }
2500 }
2501
2502 return 0;
2503}
Namhyung Kimf2148332014-01-14 11:52:48 +09002504
Andi Kleen578499982015-07-18 08:24:49 -07002505void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2506 struct perf_sample *sample, bool nonany_branch_mode)
2507{
2508 struct branch_info *bi;
2509
2510 /* If we have branch cycles always annotate them. */
2511 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2512 int i;
2513
2514 bi = sample__resolve_bstack(sample, al);
2515 if (bi) {
2516 struct addr_map_symbol *prev = NULL;
2517
2518 /*
2519 * Ignore errors, still want to process the
2520 * other entries.
2521 *
2522 * For non standard branch modes always
2523 * force no IPC (prev == NULL)
2524 *
2525 * Note that perf stores branches reversed from
2526 * program order!
2527 */
2528 for (i = bs->nr - 1; i >= 0; i--) {
2529 addr_map_symbol__account_cycles(&bi[i].from,
2530 nonany_branch_mode ? NULL : prev,
2531 bi[i].flags.cycles);
2532 prev = &bi[i].to;
2533 }
2534 free(bi);
2535 }
2536 }
2537}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002538
2539size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
2540{
2541 struct perf_evsel *pos;
2542 size_t ret = 0;
2543
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002544 evlist__for_each_entry(evlist, pos) {
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002545 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2546 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2547 }
2548
2549 return ret;
2550}
2551
2552
Namhyung Kimf2148332014-01-14 11:52:48 +09002553u64 hists__total_period(struct hists *hists)
2554{
2555 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2556 hists->stats.total_period;
2557}
Namhyung Kim33db4562014-02-07 12:06:07 +09002558
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002559int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq)
2560{
2561 char unit;
2562 int printed;
2563 const struct dso *dso = hists->dso_filter;
2564 const struct thread *thread = hists->thread_filter;
2565 int socket_id = hists->socket_filter;
2566 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
2567 u64 nr_events = hists->stats.total_period;
2568 struct perf_evsel *evsel = hists_to_evsel(hists);
2569 const char *ev_name = perf_evsel__name(evsel);
2570 char buf[512], sample_freq_str[64] = "";
2571 size_t buflen = sizeof(buf);
2572 char ref[30] = " show reference callgraph, ";
2573 bool enable_ref = false;
2574
2575 if (symbol_conf.filter_relative) {
2576 nr_samples = hists->stats.nr_non_filtered_samples;
2577 nr_events = hists->stats.total_non_filtered_period;
2578 }
2579
2580 if (perf_evsel__is_group_event(evsel)) {
2581 struct perf_evsel *pos;
2582
2583 perf_evsel__group_desc(evsel, buf, buflen);
2584 ev_name = buf;
2585
2586 for_each_group_member(pos, evsel) {
2587 struct hists *pos_hists = evsel__hists(pos);
2588
2589 if (symbol_conf.filter_relative) {
2590 nr_samples += pos_hists->stats.nr_non_filtered_samples;
2591 nr_events += pos_hists->stats.total_non_filtered_period;
2592 } else {
2593 nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
2594 nr_events += pos_hists->stats.total_period;
2595 }
2596 }
2597 }
2598
2599 if (symbol_conf.show_ref_callgraph &&
2600 strstr(ev_name, "call-graph=no"))
2601 enable_ref = true;
2602
2603 if (show_freq)
2604 scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->attr.sample_freq);
2605
2606 nr_samples = convert_unit(nr_samples, &unit);
2607 printed = scnprintf(bf, size,
2608 "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64,
2609 nr_samples, unit, evsel->nr_members > 1 ? "s" : "",
2610 ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events);
2611
2612
2613 if (hists->uid_filter_str)
2614 printed += snprintf(bf + printed, size - printed,
2615 ", UID: %s", hists->uid_filter_str);
2616 if (thread) {
2617 if (hists__has(hists, thread)) {
2618 printed += scnprintf(bf + printed, size - printed,
2619 ", Thread: %s(%d)",
2620 (thread->comm_set ? thread__comm_str(thread) : ""),
2621 thread->tid);
2622 } else {
2623 printed += scnprintf(bf + printed, size - printed,
2624 ", Thread: %s",
2625 (thread->comm_set ? thread__comm_str(thread) : ""));
2626 }
2627 }
2628 if (dso)
2629 printed += scnprintf(bf + printed, size - printed,
2630 ", DSO: %s", dso->short_name);
2631 if (socket_id > -1)
2632 printed += scnprintf(bf + printed, size - printed,
2633 ", Processor Socket: %d", socket_id);
2634
2635 return printed;
2636}
2637
Namhyung Kim33db4562014-02-07 12:06:07 +09002638int parse_filter_percentage(const struct option *opt __maybe_unused,
2639 const char *arg, int unset __maybe_unused)
2640{
2641 if (!strcmp(arg, "relative"))
2642 symbol_conf.filter_relative = true;
2643 else if (!strcmp(arg, "absolute"))
2644 symbol_conf.filter_relative = false;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002645 else {
Colin Ian Kinga596a872017-03-30 10:54:40 +01002646 pr_debug("Invalid percentage: %s\n", arg);
Namhyung Kim33db4562014-02-07 12:06:07 +09002647 return -1;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002648 }
Namhyung Kim33db4562014-02-07 12:06:07 +09002649
2650 return 0;
2651}
Namhyung Kim0b93da12014-01-14 12:02:15 +09002652
2653int perf_hist_config(const char *var, const char *value)
2654{
2655 if (!strcmp(var, "hist.percentage"))
2656 return parse_filter_percentage(NULL, value, 0);
2657
2658 return 0;
2659}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002660
Jiri Olsa5b658552016-01-18 10:24:22 +01002661int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002662{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002663 memset(hists, 0, sizeof(*hists));
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002664 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT_CACHED;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002665 hists->entries_in = &hists->entries_in_array[0];
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002666 hists->entries_collapsed = RB_ROOT_CACHED;
2667 hists->entries = RB_ROOT_CACHED;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002668 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04002669 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01002670 hists->hpp_list = hpp_list;
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002671 INIT_LIST_HEAD(&hists->hpp_formats);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002672 return 0;
2673}
2674
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002675static void hists__delete_remaining_entries(struct rb_root_cached *root)
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002676{
2677 struct rb_node *node;
2678 struct hist_entry *he;
2679
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002680 while (!RB_EMPTY_ROOT(&root->rb_root)) {
2681 node = rb_first_cached(root);
2682 rb_erase_cached(node, root);
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002683
2684 he = rb_entry(node, struct hist_entry, rb_node_in);
2685 hist_entry__delete(he);
2686 }
2687}
2688
2689static void hists__delete_all_entries(struct hists *hists)
2690{
2691 hists__delete_entries(hists);
2692 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2693 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2694 hists__delete_remaining_entries(&hists->entries_collapsed);
2695}
2696
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002697static void hists_evsel__exit(struct perf_evsel *evsel)
2698{
2699 struct hists *hists = evsel__hists(evsel);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002700 struct perf_hpp_fmt *fmt, *pos;
2701 struct perf_hpp_list_node *node, *tmp;
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002702
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002703 hists__delete_all_entries(hists);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002704
2705 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2706 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2707 list_del(&fmt->list);
2708 free(fmt);
2709 }
2710 list_del(&node->list);
2711 free(node);
2712 }
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002713}
2714
Namhyung Kimfc284be2016-01-07 10:14:10 +01002715static int hists_evsel__init(struct perf_evsel *evsel)
2716{
2717 struct hists *hists = evsel__hists(evsel);
2718
Jiri Olsa5b658552016-01-18 10:24:22 +01002719 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01002720 return 0;
2721}
2722
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002723/*
2724 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2725 * stored in the rbtree...
2726 */
2727
2728int hists__init(void)
2729{
2730 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002731 hists_evsel__init,
2732 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002733 if (err)
2734 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2735
2736 return err;
2737}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01002738
2739void perf_hpp_list__init(struct perf_hpp_list *list)
2740{
2741 INIT_LIST_HEAD(&list->fields);
2742 INIT_LIST_HEAD(&list->sorts);
2743}