blob: cff2e9041b15f9d0967f0975a7c791992b6bc045 [file] [log] [blame]
Frederic Weisbecker8a0ecfb2010-05-13 19:47:16 +02001#include "util.h"
Frederic Weisbecker598357e2010-05-21 12:48:39 +02002#include "build-id.h"
John Kacur3d1d07e2009-09-28 15:32:55 +02003#include "hist.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02004#include "session.h"
5#include "sort.h"
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03006#include "evlist.h"
Namhyung Kim29d720e2013-01-22 18:09:33 +09007#include "evsel.h"
Namhyung Kim69bcb012013-10-30 09:40:34 +09008#include "annotate.h"
Namhyung Kim740b97f2014-12-22 13:44:10 +09009#include "ui/progress.h"
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -020010#include <math.h>
John Kacur3d1d07e2009-09-28 15:32:55 +020011
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020012static bool hists__filter_entry_by_dso(struct hists *hists,
13 struct hist_entry *he);
14static bool hists__filter_entry_by_thread(struct hists *hists,
15 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090016static bool hists__filter_entry_by_symbol(struct hists *hists,
17 struct hist_entry *he);
Kan Liang21394d92015-09-04 10:45:44 -040018static bool hists__filter_entry_by_socket(struct hists *hists,
19 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020020
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030021u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030022{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030023 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030024}
25
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030026void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030027{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030028 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030029}
30
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030031bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030032{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030033 if (len > hists__col_len(hists, col)) {
34 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030035 return true;
36 }
37 return false;
38}
39
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090040void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030041{
42 enum hist_column col;
43
44 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030045 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030046}
47
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010048static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
49{
50 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
51
52 if (hists__col_len(hists, dso) < unresolved_col_width &&
53 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
54 !symbol_conf.dso_list)
55 hists__set_col_len(hists, dso, unresolved_col_width);
56}
57
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090058void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030059{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010060 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Stephane Eranian98a3b322013-01-24 16:10:35 +010061 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030062 u16 len;
63
Namhyung Kimded19d52013-04-01 20:35:19 +090064 /*
65 * +4 accounts for '[x] ' priv level info
66 * +2 accounts for 0x prefix on raw addresses
67 * +3 accounts for ' y ' symtab origin info
68 */
69 if (h->ms.sym) {
70 symlen = h->ms.sym->namelen + 4;
71 if (verbose)
72 symlen += BITS_PER_LONG / 4 + 2 + 3;
73 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
74 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010075 symlen = unresolved_col_width + 4 + 2;
76 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010077 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +010078 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030079
80 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030081 if (hists__new_col_len(hists, HISTC_COMM, len))
Jiri Olsa89c7cb22016-06-20 23:58:19 +020082 hists__set_col_len(hists, HISTC_THREAD, len + 8);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030083
84 if (h->ms.map) {
85 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030086 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030087 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010088
Namhyung Kimcb993742012-12-27 18:11:42 +090089 if (h->parent)
90 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
91
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010092 if (h->branch_info) {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010093 if (h->branch_info->from.sym) {
94 symlen = (int)h->branch_info->from.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +090095 if (verbose)
96 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010097 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
98
99 symlen = dso__name_len(h->branch_info->from.map->dso);
100 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
101 } else {
102 symlen = unresolved_col_width + 4 + 2;
103 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
104 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
105 }
106
107 if (h->branch_info->to.sym) {
108 symlen = (int)h->branch_info->to.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +0900109 if (verbose)
110 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100111 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
112
113 symlen = dso__name_len(h->branch_info->to.map->dso);
114 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
115 } else {
116 symlen = unresolved_col_width + 4 + 2;
117 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
118 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
119 }
Andi Kleen508be0d2016-05-20 13:15:08 -0700120
121 if (h->branch_info->srcline_from)
122 hists__new_col_len(hists, HISTC_SRCLINE_FROM,
123 strlen(h->branch_info->srcline_from));
124 if (h->branch_info->srcline_to)
125 hists__new_col_len(hists, HISTC_SRCLINE_TO,
126 strlen(h->branch_info->srcline_to));
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100127 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100128
129 if (h->mem_info) {
Stephane Eranian98a3b322013-01-24 16:10:35 +0100130 if (h->mem_info->daddr.sym) {
131 symlen = (int)h->mem_info->daddr.sym->namelen + 4
132 + unresolved_col_width + 2;
133 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
134 symlen);
Don Zickus9b32ba72014-06-01 15:38:29 +0200135 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
136 symlen + 1);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100137 } else {
138 symlen = unresolved_col_width + 4 + 2;
139 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
140 symlen);
Jiri Olsa08059092016-01-20 12:56:33 +0100141 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
142 symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100143 }
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200144
145 if (h->mem_info->iaddr.sym) {
146 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
147 + unresolved_col_width + 2;
148 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
149 symlen);
150 } else {
151 symlen = unresolved_col_width + 4 + 2;
152 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
153 symlen);
154 }
155
Stephane Eranian98a3b322013-01-24 16:10:35 +0100156 if (h->mem_info->daddr.map) {
157 symlen = dso__name_len(h->mem_info->daddr.map->dso);
158 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
159 symlen);
160 } else {
161 symlen = unresolved_col_width + 4 + 2;
162 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
163 }
164 } else {
165 symlen = unresolved_col_width + 4 + 2;
166 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200167 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100168 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
169 }
170
Arnaldo Carvalho de Meloa4978ec2015-09-09 12:14:00 -0300171 hists__new_col_len(hists, HISTC_CPU, 3);
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400172 hists__new_col_len(hists, HISTC_SOCKET, 6);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100173 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
174 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
175 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
176 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
177 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
178 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700179
Jiri Olsaf666ac02016-09-19 15:10:10 +0200180 if (h->srcline) {
181 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
182 hists__new_col_len(hists, HISTC_SRCLINE, len);
183 }
Arnaldo Carvalho de Meloe8e6d372015-08-10 16:53:54 -0300184
Andi Kleen31191a82015-08-07 15:54:24 -0700185 if (h->srcfile)
186 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
187
Andi Kleen475eeab2013-09-20 07:40:43 -0700188 if (h->transaction)
189 hists__new_col_len(hists, HISTC_TRANSACTION,
190 hist_entry__transaction_len());
Namhyung Kim0c0af782016-02-21 23:22:38 +0900191
192 if (h->trace_output)
193 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300194}
195
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900196void hists__output_recalc_col_len(struct hists *hists, int max_rows)
197{
198 struct rb_node *next = rb_first(&hists->entries);
199 struct hist_entry *n;
200 int row = 0;
201
202 hists__reset_col_len(hists);
203
204 while (next && row++ < max_rows) {
205 n = rb_entry(next, struct hist_entry, rb_node);
206 if (!n->filtered)
207 hists__calc_col_len(hists, n);
208 next = rb_next(&n->rb_node);
209 }
210}
211
Namhyung Kimf39056f2014-01-14 14:25:37 +0900212static void he_stat__add_cpumode_period(struct he_stat *he_stat,
213 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800214{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300215 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800216 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900217 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800218 break;
219 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900220 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800221 break;
222 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900223 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800224 break;
225 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900226 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800227 break;
228 default:
229 break;
230 }
231}
232
Andi Kleen05484292013-01-24 16:10:29 +0100233static void he_stat__add_period(struct he_stat *he_stat, u64 period,
234 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900235{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100236
Namhyung Kim139c0812012-10-04 21:49:43 +0900237 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100238 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900239 he_stat->nr_events += 1;
240}
241
242static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
243{
244 dest->period += src->period;
245 dest->period_sys += src->period_sys;
246 dest->period_us += src->period_us;
247 dest->period_guest_sys += src->period_guest_sys;
248 dest->period_guest_us += src->period_guest_us;
249 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100250 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900251}
252
Namhyung Kimf39056f2014-01-14 14:25:37 +0900253static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300254{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900255 he_stat->period = (he_stat->period * 7) / 8;
256 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100257 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300258}
259
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900260static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
261
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300262static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
263{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900264 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900265 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200266
267 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300268 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200269
Namhyung Kimf39056f2014-01-14 14:25:37 +0900270 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900271 if (symbol_conf.cumulate_callchain)
272 he_stat__decay(he->stat_acc);
Namhyung Kim42b276a2016-01-05 12:06:00 +0900273 decay_callchain(he->callchain);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200274
Namhyung Kim3186b682014-04-22 13:44:23 +0900275 diff = prev_period - he->stat.period;
276
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900277 if (!he->depth) {
278 hists->stats.total_period -= diff;
279 if (!he->filtered)
280 hists->stats.total_non_filtered_period -= diff;
281 }
282
283 if (!he->leaf) {
284 struct hist_entry *child;
285 struct rb_node *node = rb_first(&he->hroot_out);
286 while (node) {
287 child = rb_entry(node, struct hist_entry, rb_node);
288 node = rb_next(node);
289
290 if (hists__decay_entry(hists, child))
291 hists__delete_entry(hists, child);
292 }
293 }
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200294
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900295 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300296}
297
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300298static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
299{
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900300 struct rb_root *root_in;
301 struct rb_root *root_out;
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300302
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900303 if (he->parent_he) {
304 root_in = &he->parent_he->hroot_in;
305 root_out = &he->parent_he->hroot_out;
306 } else {
Jiri Olsa52225032016-05-03 13:54:42 +0200307 if (hists__has(hists, need_collapse))
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900308 root_in = &hists->entries_collapsed;
309 else
310 root_in = hists->entries_in;
311 root_out = &hists->entries;
312 }
313
314 rb_erase(&he->rb_node_in, root_in);
315 rb_erase(&he->rb_node, root_out);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300316
317 --hists->nr_entries;
318 if (!he->filtered)
319 --hists->nr_non_filtered_entries;
320
321 hist_entry__delete(he);
322}
323
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900324void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300325{
326 struct rb_node *next = rb_first(&hists->entries);
327 struct hist_entry *n;
328
329 while (next) {
330 n = rb_entry(next, struct hist_entry, rb_node);
331 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200332 if (((zap_user && n->level == '.') ||
333 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300334 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300335 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300336 }
337 }
338}
339
Namhyung Kim701937b2014-08-12 17:16:05 +0900340void hists__delete_entries(struct hists *hists)
341{
342 struct rb_node *next = rb_first(&hists->entries);
343 struct hist_entry *n;
344
345 while (next) {
346 n = rb_entry(next, struct hist_entry, rb_node);
347 next = rb_next(&n->rb_node);
348
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300349 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900350 }
351}
352
John Kacur3d1d07e2009-09-28 15:32:55 +0200353/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300354 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200355 */
356
Jiri Olsa0a269a62016-07-05 08:56:03 +0200357static int hist_entry__init(struct hist_entry *he,
358 struct hist_entry *template,
359 bool sample_self)
360{
361 *he = *template;
362
363 if (symbol_conf.cumulate_callchain) {
364 he->stat_acc = malloc(sizeof(he->stat));
365 if (he->stat_acc == NULL)
366 return -ENOMEM;
367 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
368 if (!sample_self)
369 memset(&he->stat, 0, sizeof(he->stat));
370 }
371
372 map__get(he->ms.map);
373
374 if (he->branch_info) {
375 /*
376 * This branch info is (a part of) allocated from
377 * sample__resolve_bstack() and will be freed after
378 * adding new entries. So we need to save a copy.
379 */
380 he->branch_info = malloc(sizeof(*he->branch_info));
381 if (he->branch_info == NULL) {
382 map__zput(he->ms.map);
383 free(he->stat_acc);
384 return -ENOMEM;
385 }
386
387 memcpy(he->branch_info, template->branch_info,
388 sizeof(*he->branch_info));
389
390 map__get(he->branch_info->from.map);
391 map__get(he->branch_info->to.map);
392 }
393
394 if (he->mem_info) {
395 map__get(he->mem_info->iaddr.map);
396 map__get(he->mem_info->daddr.map);
397 }
398
399 if (symbol_conf.use_callchain)
400 callchain_init(he->callchain);
401
402 if (he->raw_data) {
403 he->raw_data = memdup(he->raw_data, he->raw_size);
404
405 if (he->raw_data == NULL) {
406 map__put(he->ms.map);
407 if (he->branch_info) {
408 map__put(he->branch_info->from.map);
409 map__put(he->branch_info->to.map);
410 free(he->branch_info);
411 }
412 if (he->mem_info) {
413 map__put(he->mem_info->iaddr.map);
414 map__put(he->mem_info->daddr.map);
415 }
416 free(he->stat_acc);
417 return -ENOMEM;
418 }
419 }
420 INIT_LIST_HEAD(&he->pairs.node);
421 thread__get(he->thread);
Namhyung Kimd2580c72016-09-13 16:45:48 +0900422 he->hroot_in = RB_ROOT;
423 he->hroot_out = RB_ROOT;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200424
425 if (!symbol_conf.report_hierarchy)
426 he->leaf = true;
427
428 return 0;
429}
430
Jiri Olsaf542e762016-07-05 08:56:04 +0200431static void *hist_entry__zalloc(size_t size)
432{
433 return zalloc(size + sizeof(struct hist_entry));
434}
435
436static void hist_entry__free(void *ptr)
437{
438 free(ptr);
439}
440
441static struct hist_entry_ops default_ops = {
442 .new = hist_entry__zalloc,
443 .free = hist_entry__free,
444};
445
Namhyung Kima0b51af2012-09-11 13:34:27 +0900446static struct hist_entry *hist_entry__new(struct hist_entry *template,
447 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300448{
Jiri Olsaf542e762016-07-05 08:56:04 +0200449 struct hist_entry_ops *ops = template->ops;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900450 size_t callchain_size = 0;
451 struct hist_entry *he;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200452 int err = 0;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900453
Jiri Olsaf542e762016-07-05 08:56:04 +0200454 if (!ops)
455 ops = template->ops = &default_ops;
456
Namhyung Kim82aa0192014-12-22 13:44:14 +0900457 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900458 callchain_size = sizeof(struct callchain_root);
459
Jiri Olsaf542e762016-07-05 08:56:04 +0200460 he = ops->new(callchain_size);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200461 if (he) {
462 err = hist_entry__init(he, template, sample_self);
Jiri Olsaf542e762016-07-05 08:56:04 +0200463 if (err) {
464 ops->free(he);
465 he = NULL;
466 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300467 }
468
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200469 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300470}
471
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300472static u8 symbol__parent_filter(const struct symbol *parent)
473{
474 if (symbol_conf.exclude_other && parent == NULL)
475 return 1 << HIST_FILTER__PARENT;
476 return 0;
477}
478
Namhyung Kim467ef102016-02-16 23:08:19 +0900479static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
480{
481 if (!symbol_conf.use_callchain)
482 return;
483
484 he->hists->callchain_period += period;
485 if (!he->filtered)
486 he->hists->callchain_non_filtered_period += period;
487}
488
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300489static struct hist_entry *hists__findnew_entry(struct hists *hists,
490 struct hist_entry *entry,
491 struct addr_location *al,
492 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300493{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300494 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300495 struct rb_node *parent = NULL;
496 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700497 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900498 u64 period = entry->stat.period;
499 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300500
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300501 p = &hists->entries_in->rb_node;
502
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300503 while (*p != NULL) {
504 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300505 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300506
Namhyung Kim9afcf932012-12-10 17:29:54 +0900507 /*
508 * Make sure that it receives arguments in a same order as
509 * hist_entry__collapse() so that we can use an appropriate
510 * function when searching an entry regardless which sort
511 * keys were used.
512 */
513 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300514
515 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900516 if (sample_self) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900517 he_stat__add_period(&he->stat, period, weight);
Namhyung Kim467ef102016-02-16 23:08:19 +0900518 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900519 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900520 if (symbol_conf.cumulate_callchain)
521 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400522
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900523 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300524 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900525 * and will not be used anymore.
526 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300527 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900528
David Miller63fa4712012-03-27 03:14:18 -0400529 /* If the map of an existing hist_entry has
530 * become out-of-date due to an exec() or
531 * similar, update it. Otherwise we will
532 * mis-adjust symbol addresses when computing
533 * the history counter to increment.
534 */
535 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300536 map__put(he->ms.map);
537 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400538 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300539 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300540 }
541
542 if (cmp < 0)
543 p = &(*p)->rb_left;
544 else
545 p = &(*p)->rb_right;
546 }
547
Namhyung Kima0b51af2012-09-11 13:34:27 +0900548 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300549 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900550 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300551
Namhyung Kim0f584742016-01-28 00:40:49 +0900552 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900553 hist_entry__add_callchain_period(he, period);
554 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900555
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300556 rb_link_node(&he->rb_node_in, parent, p);
557 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300558out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900559 if (sample_self)
560 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900561 if (symbol_conf.cumulate_callchain)
562 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300563 return he;
564}
565
Jiri Olsaa5051972016-07-05 08:56:05 +0200566static struct hist_entry*
567__hists__add_entry(struct hists *hists,
568 struct addr_location *al,
569 struct symbol *sym_parent,
570 struct branch_info *bi,
571 struct mem_info *mi,
572 struct perf_sample *sample,
573 bool sample_self,
574 struct hist_entry_ops *ops)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100575{
576 struct hist_entry entry = {
577 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900578 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100579 .ms = {
580 .map = al->map,
581 .sym = al->sym,
582 },
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400583 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400584 .cpu = al->cpu,
585 .cpumode = al->cpumode,
586 .ip = al->addr,
587 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900588 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900589 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900590 .period = sample->period,
591 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900592 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100593 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300594 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300595 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900596 .branch_info = bi,
597 .mem_info = mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900598 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900599 .raw_data = sample->raw_data,
600 .raw_size = sample->raw_size,
Jiri Olsaa5051972016-07-05 08:56:05 +0200601 .ops = ops,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100602 };
603
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300604 return hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100605}
606
Jiri Olsaa5051972016-07-05 08:56:05 +0200607struct hist_entry *hists__add_entry(struct hists *hists,
608 struct addr_location *al,
609 struct symbol *sym_parent,
610 struct branch_info *bi,
611 struct mem_info *mi,
612 struct perf_sample *sample,
613 bool sample_self)
614{
615 return __hists__add_entry(hists, al, sym_parent, bi, mi,
616 sample, sample_self, NULL);
617}
618
619struct hist_entry *hists__add_entry_ops(struct hists *hists,
620 struct hist_entry_ops *ops,
621 struct addr_location *al,
622 struct symbol *sym_parent,
623 struct branch_info *bi,
624 struct mem_info *mi,
625 struct perf_sample *sample,
626 bool sample_self)
627{
628 return __hists__add_entry(hists, al, sym_parent, bi, mi,
629 sample, sample_self, ops);
630}
631
Namhyung Kim69bcb012013-10-30 09:40:34 +0900632static int
633iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
634 struct addr_location *al __maybe_unused)
635{
636 return 0;
637}
638
639static int
640iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
641 struct addr_location *al __maybe_unused)
642{
643 return 0;
644}
645
646static int
647iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
648{
649 struct perf_sample *sample = iter->sample;
650 struct mem_info *mi;
651
652 mi = sample__resolve_mem(sample, al);
653 if (mi == NULL)
654 return -ENOMEM;
655
656 iter->priv = mi;
657 return 0;
658}
659
660static int
661iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
662{
663 u64 cost;
664 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300665 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900666 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900667 struct hist_entry *he;
668
669 if (mi == NULL)
670 return -EINVAL;
671
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900672 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900673 if (!cost)
674 cost = 1;
675
676 /*
677 * must pass period=weight in order to get the correct
678 * sorting from hists__collapse_resort() which is solely
679 * based on periods. We want sorting be done on nr_events * weight
680 * and this is indirectly achieved by passing period=weight here
681 * and the he_stat__add_period() function.
682 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900683 sample->period = cost;
684
Jiri Olsa0102ef32016-06-14 20:19:21 +0200685 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
686 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900687 if (!he)
688 return -ENOMEM;
689
690 iter->he = he;
691 return 0;
692}
693
694static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900695iter_finish_mem_entry(struct hist_entry_iter *iter,
696 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900697{
698 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300699 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900700 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900701 int err = -EINVAL;
702
703 if (he == NULL)
704 goto out;
705
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300706 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900707
708 err = hist_entry__append_callchain(he, iter->sample);
709
710out:
711 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300712 * We don't need to free iter->priv (mem_info) here since the mem info
713 * was either already freed in hists__findnew_entry() or passed to a
714 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900715 */
716 iter->priv = NULL;
717
718 iter->he = NULL;
719 return err;
720}
721
722static int
723iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
724{
725 struct branch_info *bi;
726 struct perf_sample *sample = iter->sample;
727
728 bi = sample__resolve_bstack(sample, al);
729 if (!bi)
730 return -ENOMEM;
731
732 iter->curr = 0;
733 iter->total = sample->branch_stack->nr;
734
735 iter->priv = bi;
736 return 0;
737}
738
739static int
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -0300740iter_add_single_branch_entry(struct hist_entry_iter *iter,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900741 struct addr_location *al __maybe_unused)
742{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900743 /* to avoid calling callback function */
744 iter->he = NULL;
745
Namhyung Kim69bcb012013-10-30 09:40:34 +0900746 return 0;
747}
748
749static int
750iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
751{
752 struct branch_info *bi = iter->priv;
753 int i = iter->curr;
754
755 if (bi == NULL)
756 return 0;
757
758 if (iter->curr >= iter->total)
759 return 0;
760
761 al->map = bi[i].to.map;
762 al->sym = bi[i].to.sym;
763 al->addr = bi[i].to.addr;
764 return 1;
765}
766
767static int
768iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
769{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900770 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900771 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300772 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900773 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900774 struct hist_entry *he = NULL;
775 int i = iter->curr;
776 int err = 0;
777
778 bi = iter->priv;
779
780 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
781 goto out;
782
783 /*
784 * The report shows the percentage of total branches captured
785 * and not events sampled. Thus we use a pseudo period of 1.
786 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900787 sample->period = 1;
788 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
789
Jiri Olsa0102ef32016-06-14 20:19:21 +0200790 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
791 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900792 if (he == NULL)
793 return -ENOMEM;
794
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300795 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900796
797out:
798 iter->he = he;
799 iter->curr++;
800 return err;
801}
802
803static int
804iter_finish_branch_entry(struct hist_entry_iter *iter,
805 struct addr_location *al __maybe_unused)
806{
807 zfree(&iter->priv);
808 iter->he = NULL;
809
810 return iter->curr >= iter->total ? 0 : -1;
811}
812
813static int
814iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
815 struct addr_location *al __maybe_unused)
816{
817 return 0;
818}
819
820static int
821iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
822{
823 struct perf_evsel *evsel = iter->evsel;
824 struct perf_sample *sample = iter->sample;
825 struct hist_entry *he;
826
Jiri Olsa0102ef32016-06-14 20:19:21 +0200827 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
828 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900829 if (he == NULL)
830 return -ENOMEM;
831
832 iter->he = he;
833 return 0;
834}
835
836static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900837iter_finish_normal_entry(struct hist_entry_iter *iter,
838 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900839{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900840 struct hist_entry *he = iter->he;
841 struct perf_evsel *evsel = iter->evsel;
842 struct perf_sample *sample = iter->sample;
843
844 if (he == NULL)
845 return 0;
846
847 iter->he = NULL;
848
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300849 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900850
851 return hist_entry__append_callchain(he, sample);
852}
853
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900854static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300855iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900856 struct addr_location *al __maybe_unused)
857{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900858 struct hist_entry **he_cache;
859
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900860 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900861
862 /*
863 * This is for detecting cycles or recursions so that they're
864 * cumulated only one time to prevent entries more than 100%
865 * overhead.
866 */
Adrian Hunter96b40f32015-09-25 16:15:47 +0300867 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900868 if (he_cache == NULL)
869 return -ENOMEM;
870
871 iter->priv = he_cache;
872 iter->curr = 0;
873
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900874 return 0;
875}
876
877static int
878iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
879 struct addr_location *al)
880{
881 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300882 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900883 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900884 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900885 struct hist_entry *he;
886 int err = 0;
887
Jiri Olsa0102ef32016-06-14 20:19:21 +0200888 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
889 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900890 if (he == NULL)
891 return -ENOMEM;
892
893 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900894 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900895
Namhyung Kim82aa0192014-12-22 13:44:14 +0900896 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900897
898 /*
899 * We need to re-initialize the cursor since callchain_append()
900 * advanced the cursor to the end.
901 */
902 callchain_cursor_commit(&callchain_cursor);
903
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300904 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900905
906 return err;
907}
908
909static int
910iter_next_cumulative_entry(struct hist_entry_iter *iter,
911 struct addr_location *al)
912{
913 struct callchain_cursor_node *node;
914
915 node = callchain_cursor_current(&callchain_cursor);
916 if (node == NULL)
917 return 0;
918
Namhyung Kimc7405d82013-10-31 13:58:30 +0900919 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900920}
921
922static int
923iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
924 struct addr_location *al)
925{
926 struct perf_evsel *evsel = iter->evsel;
927 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900928 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900929 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900930 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -0300931 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900932 .cpu = al->cpu,
933 .thread = al->thread,
934 .comm = thread__comm(al->thread),
935 .ip = al->addr,
936 .ms = {
937 .map = al->map,
938 .sym = al->sym,
939 },
940 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +0900941 .raw_data = sample->raw_data,
942 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900943 };
944 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900945 struct callchain_cursor cursor;
946
947 callchain_cursor_snapshot(&cursor, &callchain_cursor);
948
949 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900950
951 /*
952 * Check if there's duplicate entries in the callchain.
953 * It's possible that it has cycles or recursive calls.
954 */
955 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900956 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
957 /* to avoid calling callback function */
958 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900959 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900960 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900961 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900962
Jiri Olsa0102ef32016-06-14 20:19:21 +0200963 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
964 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900965 if (he == NULL)
966 return -ENOMEM;
967
968 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900969 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900970
Namhyung Kim82aa0192014-12-22 13:44:14 +0900971 if (symbol_conf.use_callchain)
972 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900973 return 0;
974}
975
976static int
977iter_finish_cumulative_entry(struct hist_entry_iter *iter,
978 struct addr_location *al __maybe_unused)
979{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900980 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900981 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900982
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900983 return 0;
984}
985
Namhyung Kim69bcb012013-10-30 09:40:34 +0900986const struct hist_iter_ops hist_iter_mem = {
987 .prepare_entry = iter_prepare_mem_entry,
988 .add_single_entry = iter_add_single_mem_entry,
989 .next_entry = iter_next_nop_entry,
990 .add_next_entry = iter_add_next_nop_entry,
991 .finish_entry = iter_finish_mem_entry,
992};
993
994const struct hist_iter_ops hist_iter_branch = {
995 .prepare_entry = iter_prepare_branch_entry,
996 .add_single_entry = iter_add_single_branch_entry,
997 .next_entry = iter_next_branch_entry,
998 .add_next_entry = iter_add_next_branch_entry,
999 .finish_entry = iter_finish_branch_entry,
1000};
1001
1002const struct hist_iter_ops hist_iter_normal = {
1003 .prepare_entry = iter_prepare_normal_entry,
1004 .add_single_entry = iter_add_single_normal_entry,
1005 .next_entry = iter_next_nop_entry,
1006 .add_next_entry = iter_add_next_nop_entry,
1007 .finish_entry = iter_finish_normal_entry,
1008};
1009
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001010const struct hist_iter_ops hist_iter_cumulative = {
1011 .prepare_entry = iter_prepare_cumulative_entry,
1012 .add_single_entry = iter_add_single_cumulative_entry,
1013 .next_entry = iter_next_cumulative_entry,
1014 .add_next_entry = iter_add_next_cumulative_entry,
1015 .finish_entry = iter_finish_cumulative_entry,
1016};
1017
Namhyung Kim69bcb012013-10-30 09:40:34 +09001018int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001019 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +09001020{
1021 int err, err2;
1022
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001023 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
Namhyung Kim063bd932015-05-19 17:04:10 +09001024 iter->evsel, al, max_stack_depth);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001025 if (err)
1026 return err;
1027
Adrian Hunter96b40f32015-09-25 16:15:47 +03001028 iter->max_stack = max_stack_depth;
1029
Namhyung Kim69bcb012013-10-30 09:40:34 +09001030 err = iter->ops->prepare_entry(iter, al);
1031 if (err)
1032 goto out;
1033
1034 err = iter->ops->add_single_entry(iter, al);
1035 if (err)
1036 goto out;
1037
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001038 if (iter->he && iter->add_entry_cb) {
1039 err = iter->add_entry_cb(iter, al, true, arg);
1040 if (err)
1041 goto out;
1042 }
1043
Namhyung Kim69bcb012013-10-30 09:40:34 +09001044 while (iter->ops->next_entry(iter, al)) {
1045 err = iter->ops->add_next_entry(iter, al);
1046 if (err)
1047 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001048
1049 if (iter->he && iter->add_entry_cb) {
1050 err = iter->add_entry_cb(iter, al, false, arg);
1051 if (err)
1052 goto out;
1053 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001054 }
1055
1056out:
1057 err2 = iter->ops->finish_entry(iter, al);
1058 if (!err)
1059 err = err2;
1060
1061 return err;
1062}
1063
John Kacur3d1d07e2009-09-28 15:32:55 +02001064int64_t
1065hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1066{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001067 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001068 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001069 int64_t cmp = 0;
1070
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001071 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001072 if (perf_hpp__is_dynamic_entry(fmt) &&
1073 !perf_hpp__defined_dynamic_entry(fmt, hists))
1074 continue;
1075
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001076 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001077 if (cmp)
1078 break;
1079 }
1080
1081 return cmp;
1082}
1083
1084int64_t
1085hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1086{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001087 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001088 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001089 int64_t cmp = 0;
1090
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001091 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001092 if (perf_hpp__is_dynamic_entry(fmt) &&
1093 !perf_hpp__defined_dynamic_entry(fmt, hists))
1094 continue;
1095
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001096 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001097 if (cmp)
1098 break;
1099 }
1100
1101 return cmp;
1102}
1103
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001104void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001105{
Jiri Olsaf542e762016-07-05 08:56:04 +02001106 struct hist_entry_ops *ops = he->ops;
1107
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001108 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001109 map__zput(he->ms.map);
1110
1111 if (he->branch_info) {
1112 map__zput(he->branch_info->from.map);
1113 map__zput(he->branch_info->to.map);
Andi Kleen508be0d2016-05-20 13:15:08 -07001114 free_srcline(he->branch_info->srcline_from);
1115 free_srcline(he->branch_info->srcline_to);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001116 zfree(&he->branch_info);
1117 }
1118
1119 if (he->mem_info) {
1120 map__zput(he->mem_info->iaddr.map);
1121 map__zput(he->mem_info->daddr.map);
1122 zfree(&he->mem_info);
1123 }
1124
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001125 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +09001126 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -07001127 if (he->srcfile && he->srcfile[0])
1128 free(he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001129 free_callchain(he->callchain);
Namhyung Kim60517d22015-12-23 02:07:03 +09001130 free(he->trace_output);
Namhyung Kim72392832015-12-24 11:16:17 +09001131 free(he->raw_data);
Jiri Olsaf542e762016-07-05 08:56:04 +02001132 ops->free(he);
John Kacur3d1d07e2009-09-28 15:32:55 +02001133}
1134
1135/*
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001136 * If this is not the last column, then we need to pad it according to the
1137 * pre-calculated max lenght for this column, otherwise don't bother adding
1138 * spaces because that would break viewing this with, for instance, 'less',
1139 * that would show tons of trailing spaces when a long C++ demangled method
1140 * names is sampled.
1141*/
1142int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1143 struct perf_hpp_fmt *fmt, int printed)
1144{
1145 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
Jiri Olsada1b0402016-06-14 20:19:20 +02001146 const int width = fmt->width(fmt, hpp, he->hists);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001147 if (printed < width) {
1148 advance_hpp(hpp, printed);
1149 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1150 }
1151 }
1152
1153 return printed;
1154}
1155
1156/*
John Kacur3d1d07e2009-09-28 15:32:55 +02001157 * collapse the histogram
1158 */
1159
Namhyung Kimaef810e2016-02-25 00:13:34 +09001160static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
Namhyung Kimaec13a72016-03-09 22:46:58 +09001161static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1162 enum hist_filter type);
1163
1164typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1165
1166static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1167{
1168 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1169}
1170
1171static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1172 enum hist_filter type,
1173 fmt_chk_fn check)
1174{
1175 struct perf_hpp_fmt *fmt;
1176 bool type_match = false;
1177 struct hist_entry *parent = he->parent_he;
1178
1179 switch (type) {
1180 case HIST_FILTER__THREAD:
1181 if (symbol_conf.comm_list == NULL &&
1182 symbol_conf.pid_list == NULL &&
1183 symbol_conf.tid_list == NULL)
1184 return;
1185 break;
1186 case HIST_FILTER__DSO:
1187 if (symbol_conf.dso_list == NULL)
1188 return;
1189 break;
1190 case HIST_FILTER__SYMBOL:
1191 if (symbol_conf.sym_list == NULL)
1192 return;
1193 break;
1194 case HIST_FILTER__PARENT:
1195 case HIST_FILTER__GUEST:
1196 case HIST_FILTER__HOST:
1197 case HIST_FILTER__SOCKET:
Jiri Olsa9857b712016-08-17 14:55:23 +02001198 case HIST_FILTER__C2C:
Namhyung Kimaec13a72016-03-09 22:46:58 +09001199 default:
1200 return;
1201 }
1202
1203 /* if it's filtered by own fmt, it has to have filter bits */
1204 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1205 if (check(fmt)) {
1206 type_match = true;
1207 break;
1208 }
1209 }
1210
1211 if (type_match) {
1212 /*
1213 * If the filter is for current level entry, propagate
1214 * filter marker to parents. The marker bit was
1215 * already set by default so it only needs to clear
1216 * non-filtered entries.
1217 */
1218 if (!(he->filtered & (1 << type))) {
1219 while (parent) {
1220 parent->filtered &= ~(1 << type);
1221 parent = parent->parent_he;
1222 }
1223 }
1224 } else {
1225 /*
1226 * If current entry doesn't have matching formats, set
1227 * filter marker for upper level entries. it will be
1228 * cleared if its lower level entries is not filtered.
1229 *
1230 * For lower-level entries, it inherits parent's
1231 * filter bit so that lower level entries of a
1232 * non-filtered entry won't set the filter marker.
1233 */
1234 if (parent == NULL)
1235 he->filtered |= (1 << type);
1236 else
1237 he->filtered |= (parent->filtered & (1 << type));
1238 }
1239}
1240
1241static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1242{
1243 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1244 check_thread_entry);
1245
1246 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1247 perf_hpp__is_dso_entry);
1248
1249 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1250 perf_hpp__is_sym_entry);
1251
1252 hists__apply_filters(he->hists, he);
1253}
Namhyung Kimaef810e2016-02-25 00:13:34 +09001254
1255static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1256 struct rb_root *root,
1257 struct hist_entry *he,
Namhyung Kimaec13a72016-03-09 22:46:58 +09001258 struct hist_entry *parent_he,
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001259 struct perf_hpp_list *hpp_list)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001260{
1261 struct rb_node **p = &root->rb_node;
1262 struct rb_node *parent = NULL;
1263 struct hist_entry *iter, *new;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001264 struct perf_hpp_fmt *fmt;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001265 int64_t cmp;
1266
1267 while (*p != NULL) {
1268 parent = *p;
1269 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1270
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001271 cmp = 0;
1272 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1273 cmp = fmt->collapse(fmt, iter, he);
1274 if (cmp)
1275 break;
1276 }
1277
Namhyung Kimaef810e2016-02-25 00:13:34 +09001278 if (!cmp) {
1279 he_stat__add_stat(&iter->stat, &he->stat);
1280 return iter;
1281 }
1282
1283 if (cmp < 0)
1284 p = &parent->rb_left;
1285 else
1286 p = &parent->rb_right;
1287 }
1288
1289 new = hist_entry__new(he, true);
1290 if (new == NULL)
1291 return NULL;
1292
Namhyung Kimaef810e2016-02-25 00:13:34 +09001293 hists->nr_entries++;
1294
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001295 /* save related format list for output */
1296 new->hpp_list = hpp_list;
Namhyung Kimaec13a72016-03-09 22:46:58 +09001297 new->parent_he = parent_he;
1298
1299 hist_entry__apply_hierarchy_filters(new);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001300
1301 /* some fields are now passed to 'new' */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001302 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1303 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1304 he->trace_output = NULL;
1305 else
1306 new->trace_output = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001307
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001308 if (perf_hpp__is_srcline_entry(fmt))
1309 he->srcline = NULL;
1310 else
1311 new->srcline = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001312
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001313 if (perf_hpp__is_srcfile_entry(fmt))
1314 he->srcfile = NULL;
1315 else
1316 new->srcfile = NULL;
1317 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001318
1319 rb_link_node(&new->rb_node_in, parent, p);
1320 rb_insert_color(&new->rb_node_in, root);
1321 return new;
1322}
1323
1324static int hists__hierarchy_insert_entry(struct hists *hists,
1325 struct rb_root *root,
1326 struct hist_entry *he)
1327{
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001328 struct perf_hpp_list_node *node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001329 struct hist_entry *new_he = NULL;
1330 struct hist_entry *parent = NULL;
1331 int depth = 0;
1332 int ret = 0;
1333
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001334 list_for_each_entry(node, &hists->hpp_formats, list) {
1335 /* skip period (overhead) and elided columns */
1336 if (node->level == 0 || node->skip)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001337 continue;
1338
1339 /* insert copy of 'he' for each fmt into the hierarchy */
Namhyung Kimaec13a72016-03-09 22:46:58 +09001340 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001341 if (new_he == NULL) {
1342 ret = -1;
1343 break;
1344 }
1345
1346 root = &new_he->hroot_in;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001347 new_he->depth = depth++;
1348 parent = new_he;
1349 }
1350
1351 if (new_he) {
1352 new_he->leaf = true;
1353
1354 if (symbol_conf.use_callchain) {
1355 callchain_cursor_reset(&callchain_cursor);
1356 if (callchain_merge(&callchain_cursor,
1357 new_he->callchain,
1358 he->callchain) < 0)
1359 ret = -1;
1360 }
1361 }
1362
1363 /* 'he' is no longer used */
1364 hist_entry__delete(he);
1365
1366 /* return 0 (or -1) since it already applied filters */
1367 return ret;
1368}
1369
Jiri Olsa592dac62016-03-24 13:52:17 +01001370static int hists__collapse_insert_entry(struct hists *hists,
1371 struct rb_root *root,
1372 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001373{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001374 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001375 struct rb_node *parent = NULL;
1376 struct hist_entry *iter;
1377 int64_t cmp;
1378
Namhyung Kimaef810e2016-02-25 00:13:34 +09001379 if (symbol_conf.report_hierarchy)
1380 return hists__hierarchy_insert_entry(hists, root, he);
1381
John Kacur3d1d07e2009-09-28 15:32:55 +02001382 while (*p != NULL) {
1383 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001384 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001385
1386 cmp = hist_entry__collapse(iter, he);
1387
1388 if (!cmp) {
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001389 int ret = 0;
1390
Namhyung Kim139c0812012-10-04 21:49:43 +09001391 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001392 if (symbol_conf.cumulate_callchain)
1393 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001394
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001395 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001396 callchain_cursor_reset(&callchain_cursor);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001397 if (callchain_merge(&callchain_cursor,
1398 iter->callchain,
1399 he->callchain) < 0)
1400 ret = -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001401 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001402 hist_entry__delete(he);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001403 return ret;
John Kacur3d1d07e2009-09-28 15:32:55 +02001404 }
1405
1406 if (cmp < 0)
1407 p = &(*p)->rb_left;
1408 else
1409 p = &(*p)->rb_right;
1410 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001411 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001412
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001413 rb_link_node(&he->rb_node_in, parent, p);
1414 rb_insert_color(&he->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001415 return 1;
John Kacur3d1d07e2009-09-28 15:32:55 +02001416}
1417
Namhyung Kimfc284be2016-01-07 10:14:10 +01001418struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001419{
1420 struct rb_root *root;
1421
1422 pthread_mutex_lock(&hists->lock);
1423
1424 root = hists->entries_in;
1425 if (++hists->entries_in > &hists->entries_in_array[1])
1426 hists->entries_in = &hists->entries_in_array[0];
1427
1428 pthread_mutex_unlock(&hists->lock);
1429
1430 return root;
1431}
1432
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001433static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1434{
1435 hists__filter_entry_by_dso(hists, he);
1436 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001437 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001438 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001439}
1440
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001441int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001442{
1443 struct rb_root *root;
1444 struct rb_node *next;
1445 struct hist_entry *n;
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001446 int ret;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001447
Jiri Olsa52225032016-05-03 13:54:42 +02001448 if (!hists__has(hists, need_collapse))
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001449 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001450
Namhyung Kim740b97f2014-12-22 13:44:10 +09001451 hists->nr_entries = 0;
1452
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001453 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001454
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001455 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001456
1457 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001458 if (session_done())
1459 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001460 n = rb_entry(next, struct hist_entry, rb_node_in);
1461 next = rb_next(&n->rb_node_in);
1462
1463 rb_erase(&n->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001464 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1465 if (ret < 0)
1466 return -1;
1467
1468 if (ret) {
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001469 /*
1470 * If it wasn't combined with one of the entries already
1471 * collapsed, we need to apply the filters that may have
1472 * been set by, say, the hist_browser.
1473 */
1474 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001475 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001476 if (prog)
1477 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001478 }
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001479 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001480}
1481
Namhyung Kim043ca3892014-03-03 14:18:00 +09001482static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001483{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001484 struct hists *hists = a->hists;
Namhyung Kim043ca3892014-03-03 14:18:00 +09001485 struct perf_hpp_fmt *fmt;
1486 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001487
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001488 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001489 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001490 continue;
1491
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001492 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001493 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001494 break;
1495 }
1496
Namhyung Kim043ca3892014-03-03 14:18:00 +09001497 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001498}
1499
Namhyung Kim9283ba92014-04-24 16:37:26 +09001500static void hists__reset_filter_stats(struct hists *hists)
1501{
1502 hists->nr_non_filtered_entries = 0;
1503 hists->stats.total_non_filtered_period = 0;
1504}
1505
1506void hists__reset_stats(struct hists *hists)
1507{
1508 hists->nr_entries = 0;
1509 hists->stats.total_period = 0;
1510
1511 hists__reset_filter_stats(hists);
1512}
1513
1514static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1515{
1516 hists->nr_non_filtered_entries++;
1517 hists->stats.total_non_filtered_period += h->stat.period;
1518}
1519
1520void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1521{
1522 if (!h->filtered)
1523 hists__inc_filter_stats(hists, h);
1524
1525 hists->nr_entries++;
1526 hists->stats.total_period += h->stat.period;
1527}
1528
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001529static void hierarchy_recalc_total_periods(struct hists *hists)
1530{
1531 struct rb_node *node;
1532 struct hist_entry *he;
1533
1534 node = rb_first(&hists->entries);
1535
1536 hists->stats.total_period = 0;
1537 hists->stats.total_non_filtered_period = 0;
1538
1539 /*
1540 * recalculate total period using top-level entries only
1541 * since lower level entries only see non-filtered entries
1542 * but upper level entries have sum of both entries.
1543 */
1544 while (node) {
1545 he = rb_entry(node, struct hist_entry, rb_node);
1546 node = rb_next(node);
1547
1548 hists->stats.total_period += he->stat.period;
1549 if (!he->filtered)
1550 hists->stats.total_non_filtered_period += he->stat.period;
1551 }
1552}
1553
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001554static void hierarchy_insert_output_entry(struct rb_root *root,
1555 struct hist_entry *he)
1556{
1557 struct rb_node **p = &root->rb_node;
1558 struct rb_node *parent = NULL;
1559 struct hist_entry *iter;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001560 struct perf_hpp_fmt *fmt;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001561
1562 while (*p != NULL) {
1563 parent = *p;
1564 iter = rb_entry(parent, struct hist_entry, rb_node);
1565
1566 if (hist_entry__sort(he, iter) > 0)
1567 p = &parent->rb_left;
1568 else
1569 p = &parent->rb_right;
1570 }
1571
1572 rb_link_node(&he->rb_node, parent, p);
1573 rb_insert_color(&he->rb_node, root);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001574
1575 /* update column width of dynamic entry */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001576 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1577 if (perf_hpp__is_dynamic_entry(fmt))
1578 fmt->sort(fmt, he, NULL);
1579 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001580}
1581
1582static void hists__hierarchy_output_resort(struct hists *hists,
1583 struct ui_progress *prog,
1584 struct rb_root *root_in,
1585 struct rb_root *root_out,
1586 u64 min_callchain_hits,
1587 bool use_callchain)
1588{
1589 struct rb_node *node;
1590 struct hist_entry *he;
1591
1592 *root_out = RB_ROOT;
1593 node = rb_first(root_in);
1594
1595 while (node) {
1596 he = rb_entry(node, struct hist_entry, rb_node_in);
1597 node = rb_next(node);
1598
1599 hierarchy_insert_output_entry(root_out, he);
1600
1601 if (prog)
1602 ui_progress__update(prog, 1);
1603
Namhyung Kimc72ab442016-11-08 22:08:33 +09001604 hists->nr_entries++;
1605 if (!he->filtered) {
1606 hists->nr_non_filtered_entries++;
1607 hists__calc_col_len(hists, he);
1608 }
1609
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001610 if (!he->leaf) {
1611 hists__hierarchy_output_resort(hists, prog,
1612 &he->hroot_in,
1613 &he->hroot_out,
1614 min_callchain_hits,
1615 use_callchain);
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001616 continue;
1617 }
1618
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001619 if (!use_callchain)
1620 continue;
1621
1622 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1623 u64 total = he->stat.period;
1624
1625 if (symbol_conf.cumulate_callchain)
1626 total = he->stat_acc->period;
1627
1628 min_callchain_hits = total * (callchain_param.min_percent / 100);
1629 }
1630
1631 callchain_param.sort(&he->sorted_chain, he->callchain,
1632 min_callchain_hits, &callchain_param);
1633 }
1634}
1635
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001636static void __hists__insert_output_entry(struct rb_root *entries,
1637 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001638 u64 min_callchain_hits,
1639 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001640{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001641 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001642 struct rb_node *parent = NULL;
1643 struct hist_entry *iter;
Namhyung Kimabab5e72016-02-27 03:52:47 +09001644 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001645
Namhyung Kim744070e2016-01-28 00:40:48 +09001646 if (use_callchain) {
1647 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1648 u64 total = he->stat.period;
1649
1650 if (symbol_conf.cumulate_callchain)
1651 total = he->stat_acc->period;
1652
1653 min_callchain_hits = total * (callchain_param.min_percent / 100);
1654 }
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001655 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001656 min_callchain_hits, &callchain_param);
Namhyung Kim744070e2016-01-28 00:40:48 +09001657 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001658
1659 while (*p != NULL) {
1660 parent = *p;
1661 iter = rb_entry(parent, struct hist_entry, rb_node);
1662
Namhyung Kim043ca3892014-03-03 14:18:00 +09001663 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001664 p = &(*p)->rb_left;
1665 else
1666 p = &(*p)->rb_right;
1667 }
1668
1669 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001670 rb_insert_color(&he->rb_node, entries);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001671
1672 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1673 if (perf_hpp__is_dynamic_entry(fmt) &&
1674 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1675 fmt->sort(fmt, he, NULL); /* update column width */
1676 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001677}
1678
Jiri Olsa01441af2016-01-18 10:23:59 +01001679static void output_resort(struct hists *hists, struct ui_progress *prog,
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001680 bool use_callchain, hists__resort_cb_t cb)
John Kacur3d1d07e2009-09-28 15:32:55 +02001681{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001682 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001683 struct rb_node *next;
1684 struct hist_entry *n;
Namhyung Kim467ef102016-02-16 23:08:19 +09001685 u64 callchain_total;
John Kacur3d1d07e2009-09-28 15:32:55 +02001686 u64 min_callchain_hits;
1687
Namhyung Kim467ef102016-02-16 23:08:19 +09001688 callchain_total = hists->callchain_period;
1689 if (symbol_conf.filter_relative)
1690 callchain_total = hists->callchain_non_filtered_period;
1691
1692 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001693
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001694 hists__reset_stats(hists);
1695 hists__reset_col_len(hists);
1696
1697 if (symbol_conf.report_hierarchy) {
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001698 hists__hierarchy_output_resort(hists, prog,
1699 &hists->entries_collapsed,
1700 &hists->entries,
1701 min_callchain_hits,
1702 use_callchain);
1703 hierarchy_recalc_total_periods(hists);
1704 return;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001705 }
1706
Jiri Olsa52225032016-05-03 13:54:42 +02001707 if (hists__has(hists, need_collapse))
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001708 root = &hists->entries_collapsed;
1709 else
1710 root = hists->entries_in;
1711
1712 next = rb_first(root);
1713 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001714
1715 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001716 n = rb_entry(next, struct hist_entry, rb_node_in);
1717 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001718
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001719 if (cb && cb(n))
1720 continue;
1721
Kan Liangf9db0d02015-08-11 06:30:48 -04001722 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001723 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001724
1725 if (!n->filtered)
1726 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001727
1728 if (prog)
1729 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001730 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001731}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001732
Jiri Olsa452ce032016-01-18 10:24:00 +01001733void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
Jiri Olsa01441af2016-01-18 10:23:59 +01001734{
Jiri Olsa01441af2016-01-18 10:23:59 +01001735 bool use_callchain;
1736
1737 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1738 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1739 else
1740 use_callchain = symbol_conf.use_callchain;
1741
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001742 output_resort(evsel__hists(evsel), prog, use_callchain, NULL);
Jiri Olsa452ce032016-01-18 10:24:00 +01001743}
1744
1745void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1746{
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001747 output_resort(hists, prog, symbol_conf.use_callchain, NULL);
1748}
1749
1750void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1751 hists__resort_cb_t cb)
1752{
1753 output_resort(hists, prog, symbol_conf.use_callchain, cb);
Jiri Olsa01441af2016-01-18 10:23:59 +01001754}
1755
Namhyung Kim8c018722016-02-25 00:13:36 +09001756static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1757{
1758 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1759 return false;
1760
1761 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1762 return true;
1763
1764 return false;
1765}
1766
1767struct rb_node *rb_hierarchy_last(struct rb_node *node)
1768{
1769 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1770
1771 while (can_goto_child(he, HMD_NORMAL)) {
1772 node = rb_last(&he->hroot_out);
1773 he = rb_entry(node, struct hist_entry, rb_node);
1774 }
1775 return node;
1776}
1777
1778struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1779{
1780 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1781
1782 if (can_goto_child(he, hmd))
1783 node = rb_first(&he->hroot_out);
1784 else
1785 node = rb_next(node);
1786
1787 while (node == NULL) {
1788 he = he->parent_he;
1789 if (he == NULL)
1790 break;
1791
1792 node = rb_next(&he->rb_node);
1793 }
1794 return node;
1795}
1796
1797struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1798{
1799 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1800
1801 node = rb_prev(node);
1802 if (node)
1803 return rb_hierarchy_last(node);
1804
1805 he = he->parent_he;
1806 if (he == NULL)
1807 return NULL;
1808
1809 return &he->rb_node;
1810}
1811
Namhyung Kima7b58952016-02-26 21:13:16 +09001812bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1813{
1814 struct rb_node *node;
1815 struct hist_entry *child;
1816 float percent;
1817
1818 if (he->leaf)
1819 return false;
1820
1821 node = rb_first(&he->hroot_out);
1822 child = rb_entry(node, struct hist_entry, rb_node);
1823
1824 while (node && child->filtered) {
1825 node = rb_next(node);
1826 child = rb_entry(node, struct hist_entry, rb_node);
1827 }
1828
1829 if (node)
1830 percent = hist_entry__get_percent_limit(child);
1831 else
1832 percent = 0;
1833
1834 return node && percent >= limit;
1835}
1836
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001837static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001838 enum hist_filter filter)
1839{
1840 h->filtered &= ~(1 << filter);
Namhyung Kim155e9af2016-02-25 00:13:38 +09001841
1842 if (symbol_conf.report_hierarchy) {
1843 struct hist_entry *parent = h->parent_he;
1844
1845 while (parent) {
1846 he_stat__add_stat(&parent->stat, &h->stat);
1847
1848 parent->filtered &= ~(1 << filter);
1849
1850 if (parent->filtered)
1851 goto next;
1852
1853 /* force fold unfiltered entry for simplicity */
1854 parent->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001855 parent->has_no_entry = false;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001856 parent->row_offset = 0;
1857 parent->nr_rows = 0;
1858next:
1859 parent = parent->parent_he;
1860 }
1861 }
1862
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001863 if (h->filtered)
1864 return;
1865
Namhyung Kim87e90f42014-04-24 16:44:16 +09001866 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001867 h->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001868 h->has_no_entry = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001869 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001870 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001871
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001872 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001873
Namhyung Kim9283ba92014-04-24 16:37:26 +09001874 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001875 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001876}
1877
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001878
1879static bool hists__filter_entry_by_dso(struct hists *hists,
1880 struct hist_entry *he)
1881{
1882 if (hists->dso_filter != NULL &&
1883 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1884 he->filtered |= (1 << HIST_FILTER__DSO);
1885 return true;
1886 }
1887
1888 return false;
1889}
1890
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001891static bool hists__filter_entry_by_thread(struct hists *hists,
1892 struct hist_entry *he)
1893{
1894 if (hists->thread_filter != NULL &&
1895 he->thread != hists->thread_filter) {
1896 he->filtered |= (1 << HIST_FILTER__THREAD);
1897 return true;
1898 }
1899
1900 return false;
1901}
1902
Namhyung Kime94d53e2012-03-16 17:50:51 +09001903static bool hists__filter_entry_by_symbol(struct hists *hists,
1904 struct hist_entry *he)
1905{
1906 if (hists->symbol_filter_str != NULL &&
1907 (!he->ms.sym || strstr(he->ms.sym->name,
1908 hists->symbol_filter_str) == NULL)) {
1909 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1910 return true;
1911 }
1912
1913 return false;
1914}
1915
Kan Liang21394d92015-09-04 10:45:44 -04001916static bool hists__filter_entry_by_socket(struct hists *hists,
1917 struct hist_entry *he)
1918{
1919 if ((hists->socket_filter > -1) &&
1920 (he->socket != hists->socket_filter)) {
1921 he->filtered |= (1 << HIST_FILTER__SOCKET);
1922 return true;
1923 }
1924
1925 return false;
1926}
1927
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001928typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1929
1930static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04001931{
1932 struct rb_node *nd;
1933
1934 hists->stats.nr_non_filtered_samples = 0;
1935
1936 hists__reset_filter_stats(hists);
1937 hists__reset_col_len(hists);
1938
1939 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1940 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1941
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001942 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04001943 continue;
1944
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001945 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04001946 }
1947}
1948
Namhyung Kim70642852016-02-25 00:13:39 +09001949static void resort_filtered_entry(struct rb_root *root, struct hist_entry *he)
1950{
1951 struct rb_node **p = &root->rb_node;
1952 struct rb_node *parent = NULL;
1953 struct hist_entry *iter;
1954 struct rb_root new_root = RB_ROOT;
1955 struct rb_node *nd;
1956
1957 while (*p != NULL) {
1958 parent = *p;
1959 iter = rb_entry(parent, struct hist_entry, rb_node);
1960
1961 if (hist_entry__sort(he, iter) > 0)
1962 p = &(*p)->rb_left;
1963 else
1964 p = &(*p)->rb_right;
1965 }
1966
1967 rb_link_node(&he->rb_node, parent, p);
1968 rb_insert_color(&he->rb_node, root);
1969
1970 if (he->leaf || he->filtered)
1971 return;
1972
1973 nd = rb_first(&he->hroot_out);
1974 while (nd) {
1975 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1976
1977 nd = rb_next(nd);
1978 rb_erase(&h->rb_node, &he->hroot_out);
1979
1980 resort_filtered_entry(&new_root, h);
1981 }
1982
1983 he->hroot_out = new_root;
1984}
1985
Namhyung Kim155e9af2016-02-25 00:13:38 +09001986static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
1987{
1988 struct rb_node *nd;
Namhyung Kim70642852016-02-25 00:13:39 +09001989 struct rb_root new_root = RB_ROOT;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001990
1991 hists->stats.nr_non_filtered_samples = 0;
1992
1993 hists__reset_filter_stats(hists);
1994 hists__reset_col_len(hists);
1995
1996 nd = rb_first(&hists->entries);
1997 while (nd) {
1998 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1999 int ret;
2000
2001 ret = hist_entry__filter(h, type, arg);
2002
2003 /*
2004 * case 1. non-matching type
2005 * zero out the period, set filter marker and move to child
2006 */
2007 if (ret < 0) {
2008 memset(&h->stat, 0, sizeof(h->stat));
2009 h->filtered |= (1 << type);
2010
2011 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2012 }
2013 /*
2014 * case 2. matched type (filter out)
2015 * set filter marker and move to next
2016 */
2017 else if (ret == 1) {
2018 h->filtered |= (1 << type);
2019
2020 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2021 }
2022 /*
2023 * case 3. ok (not filtered)
2024 * add period to hists and parents, erase the filter marker
2025 * and move to next sibling
2026 */
2027 else {
2028 hists__remove_entry_filter(hists, h, type);
2029
2030 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2031 }
2032 }
Namhyung Kim70642852016-02-25 00:13:39 +09002033
Namhyung Kimf7fb5382016-03-09 22:47:02 +09002034 hierarchy_recalc_total_periods(hists);
2035
Namhyung Kim70642852016-02-25 00:13:39 +09002036 /*
2037 * resort output after applying a new filter since filter in a lower
2038 * hierarchy can change periods in a upper hierarchy.
2039 */
2040 nd = rb_first(&hists->entries);
2041 while (nd) {
2042 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2043
2044 nd = rb_next(nd);
2045 rb_erase(&h->rb_node, &hists->entries);
2046
2047 resort_filtered_entry(&new_root, h);
2048 }
2049
2050 hists->entries = new_root;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002051}
2052
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002053void hists__filter_by_thread(struct hists *hists)
2054{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002055 if (symbol_conf.report_hierarchy)
2056 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2057 hists->thread_filter);
2058 else
2059 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2060 hists__filter_entry_by_thread);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002061}
2062
2063void hists__filter_by_dso(struct hists *hists)
2064{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002065 if (symbol_conf.report_hierarchy)
2066 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2067 hists->dso_filter);
2068 else
2069 hists__filter_by_type(hists, HIST_FILTER__DSO,
2070 hists__filter_entry_by_dso);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002071}
2072
2073void hists__filter_by_symbol(struct hists *hists)
2074{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002075 if (symbol_conf.report_hierarchy)
2076 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2077 hists->symbol_filter_str);
2078 else
2079 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2080 hists__filter_entry_by_symbol);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002081}
2082
2083void hists__filter_by_socket(struct hists *hists)
2084{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002085 if (symbol_conf.report_hierarchy)
2086 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2087 &hists->socket_filter);
2088 else
2089 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2090 hists__filter_entry_by_socket);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002091}
2092
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002093void events_stats__inc(struct events_stats *stats, u32 type)
2094{
2095 ++stats->nr_events[0];
2096 ++stats->nr_events[type];
2097}
2098
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002099void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002100{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002101 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002102}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002103
Namhyung Kim1844dbc2014-05-28 14:12:18 +09002104void hists__inc_nr_samples(struct hists *hists, bool filtered)
2105{
2106 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2107 if (!filtered)
2108 hists->stats.nr_non_filtered_samples++;
2109}
2110
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002111static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2112 struct hist_entry *pair)
2113{
Namhyung Kimce74f602012-12-10 17:29:55 +09002114 struct rb_root *root;
2115 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002116 struct rb_node *parent = NULL;
2117 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07002118 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002119
Jiri Olsa52225032016-05-03 13:54:42 +02002120 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002121 root = &hists->entries_collapsed;
2122 else
2123 root = hists->entries_in;
2124
2125 p = &root->rb_node;
2126
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002127 while (*p != NULL) {
2128 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09002129 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002130
Namhyung Kimce74f602012-12-10 17:29:55 +09002131 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002132
2133 if (!cmp)
2134 goto out;
2135
2136 if (cmp < 0)
2137 p = &(*p)->rb_left;
2138 else
2139 p = &(*p)->rb_right;
2140 }
2141
Namhyung Kima0b51af2012-09-11 13:34:27 +09002142 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002143 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03002144 memset(&he->stat, 0, sizeof(he->stat));
2145 he->hists = hists;
Kan Liang09623d72016-04-24 23:28:09 -07002146 if (symbol_conf.cumulate_callchain)
2147 memset(he->stat_acc, 0, sizeof(he->stat));
Namhyung Kimce74f602012-12-10 17:29:55 +09002148 rb_link_node(&he->rb_node_in, parent, p);
2149 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09002150 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01002151 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002152 }
2153out:
2154 return he;
2155}
2156
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002157static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
2158 struct rb_root *root,
2159 struct hist_entry *pair)
2160{
2161 struct rb_node **p;
2162 struct rb_node *parent = NULL;
2163 struct hist_entry *he;
2164 struct perf_hpp_fmt *fmt;
2165
2166 p = &root->rb_node;
2167 while (*p != NULL) {
2168 int64_t cmp = 0;
2169
2170 parent = *p;
2171 he = rb_entry(parent, struct hist_entry, rb_node_in);
2172
2173 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2174 cmp = fmt->collapse(fmt, he, pair);
2175 if (cmp)
2176 break;
2177 }
2178 if (!cmp)
2179 goto out;
2180
2181 if (cmp < 0)
2182 p = &parent->rb_left;
2183 else
2184 p = &parent->rb_right;
2185 }
2186
2187 he = hist_entry__new(pair, true);
2188 if (he) {
2189 rb_link_node(&he->rb_node_in, parent, p);
2190 rb_insert_color(&he->rb_node_in, root);
2191
2192 he->dummy = true;
2193 he->hists = hists;
2194 memset(&he->stat, 0, sizeof(he->stat));
2195 hists__inc_stats(hists, he);
2196 }
2197out:
2198 return he;
2199}
2200
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002201static struct hist_entry *hists__find_entry(struct hists *hists,
2202 struct hist_entry *he)
2203{
Namhyung Kimce74f602012-12-10 17:29:55 +09002204 struct rb_node *n;
2205
Jiri Olsa52225032016-05-03 13:54:42 +02002206 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002207 n = hists->entries_collapsed.rb_node;
2208 else
2209 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002210
2211 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002212 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2213 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002214
2215 if (cmp < 0)
2216 n = n->rb_left;
2217 else if (cmp > 0)
2218 n = n->rb_right;
2219 else
2220 return iter;
2221 }
2222
2223 return NULL;
2224}
2225
Namhyung Kim09034de2016-09-13 16:45:46 +09002226static struct hist_entry *hists__find_hierarchy_entry(struct rb_root *root,
2227 struct hist_entry *he)
2228{
2229 struct rb_node *n = root->rb_node;
2230
2231 while (n) {
2232 struct hist_entry *iter;
2233 struct perf_hpp_fmt *fmt;
2234 int64_t cmp = 0;
2235
2236 iter = rb_entry(n, struct hist_entry, rb_node_in);
2237 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2238 cmp = fmt->collapse(fmt, iter, he);
2239 if (cmp)
2240 break;
2241 }
2242
2243 if (cmp < 0)
2244 n = n->rb_left;
2245 else if (cmp > 0)
2246 n = n->rb_right;
2247 else
2248 return iter;
2249 }
2250
2251 return NULL;
2252}
2253
2254static void hists__match_hierarchy(struct rb_root *leader_root,
2255 struct rb_root *other_root)
2256{
2257 struct rb_node *nd;
2258 struct hist_entry *pos, *pair;
2259
2260 for (nd = rb_first(leader_root); nd; nd = rb_next(nd)) {
2261 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2262 pair = hists__find_hierarchy_entry(other_root, pos);
2263
2264 if (pair) {
2265 hist_entry__add_pair(pair, pos);
2266 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2267 }
2268 }
2269}
2270
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002271/*
2272 * Look for pairs to link to the leader buckets (hist_entries):
2273 */
2274void hists__match(struct hists *leader, struct hists *other)
2275{
Namhyung Kimce74f602012-12-10 17:29:55 +09002276 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002277 struct rb_node *nd;
2278 struct hist_entry *pos, *pair;
2279
Namhyung Kim09034de2016-09-13 16:45:46 +09002280 if (symbol_conf.report_hierarchy) {
2281 /* hierarchy report always collapses entries */
2282 return hists__match_hierarchy(&leader->entries_collapsed,
2283 &other->entries_collapsed);
2284 }
2285
Jiri Olsa52225032016-05-03 13:54:42 +02002286 if (hists__has(leader, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002287 root = &leader->entries_collapsed;
2288 else
2289 root = leader->entries_in;
2290
2291 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2292 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002293 pair = hists__find_entry(other, pos);
2294
2295 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09002296 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002297 }
2298}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002299
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002300static int hists__link_hierarchy(struct hists *leader_hists,
2301 struct hist_entry *parent,
2302 struct rb_root *leader_root,
2303 struct rb_root *other_root)
2304{
2305 struct rb_node *nd;
2306 struct hist_entry *pos, *leader;
2307
2308 for (nd = rb_first(other_root); nd; nd = rb_next(nd)) {
2309 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2310
2311 if (hist_entry__has_pairs(pos)) {
2312 bool found = false;
2313
2314 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2315 if (leader->hists == leader_hists) {
2316 found = true;
2317 break;
2318 }
2319 }
2320 if (!found)
2321 return -1;
2322 } else {
2323 leader = add_dummy_hierarchy_entry(leader_hists,
2324 leader_root, pos);
2325 if (leader == NULL)
2326 return -1;
2327
2328 /* do not point parent in the pos */
2329 leader->parent_he = parent;
2330
2331 hist_entry__add_pair(pos, leader);
2332 }
2333
2334 if (!pos->leaf) {
2335 if (hists__link_hierarchy(leader_hists, leader,
2336 &leader->hroot_in,
2337 &pos->hroot_in) < 0)
2338 return -1;
2339 }
2340 }
2341 return 0;
2342}
2343
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002344/*
2345 * Look for entries in the other hists that are not present in the leader, if
2346 * we find them, just add a dummy entry on the leader hists, with period=0,
2347 * nr_events=0, to serve as the list header.
2348 */
2349int hists__link(struct hists *leader, struct hists *other)
2350{
Namhyung Kimce74f602012-12-10 17:29:55 +09002351 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002352 struct rb_node *nd;
2353 struct hist_entry *pos, *pair;
2354
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002355 if (symbol_conf.report_hierarchy) {
2356 /* hierarchy report always collapses entries */
2357 return hists__link_hierarchy(leader, NULL,
2358 &leader->entries_collapsed,
2359 &other->entries_collapsed);
2360 }
2361
Jiri Olsa52225032016-05-03 13:54:42 +02002362 if (hists__has(other, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002363 root = &other->entries_collapsed;
2364 else
2365 root = other->entries_in;
2366
2367 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2368 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002369
2370 if (!hist_entry__has_pairs(pos)) {
2371 pair = hists__add_dummy_entry(leader, pos);
2372 if (pair == NULL)
2373 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09002374 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002375 }
2376 }
2377
2378 return 0;
2379}
Namhyung Kimf2148332014-01-14 11:52:48 +09002380
Andi Kleen578499982015-07-18 08:24:49 -07002381void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2382 struct perf_sample *sample, bool nonany_branch_mode)
2383{
2384 struct branch_info *bi;
2385
2386 /* If we have branch cycles always annotate them. */
2387 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2388 int i;
2389
2390 bi = sample__resolve_bstack(sample, al);
2391 if (bi) {
2392 struct addr_map_symbol *prev = NULL;
2393
2394 /*
2395 * Ignore errors, still want to process the
2396 * other entries.
2397 *
2398 * For non standard branch modes always
2399 * force no IPC (prev == NULL)
2400 *
2401 * Note that perf stores branches reversed from
2402 * program order!
2403 */
2404 for (i = bs->nr - 1; i >= 0; i--) {
2405 addr_map_symbol__account_cycles(&bi[i].from,
2406 nonany_branch_mode ? NULL : prev,
2407 bi[i].flags.cycles);
2408 prev = &bi[i].to;
2409 }
2410 free(bi);
2411 }
2412 }
2413}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002414
2415size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
2416{
2417 struct perf_evsel *pos;
2418 size_t ret = 0;
2419
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002420 evlist__for_each_entry(evlist, pos) {
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002421 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2422 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2423 }
2424
2425 return ret;
2426}
2427
2428
Namhyung Kimf2148332014-01-14 11:52:48 +09002429u64 hists__total_period(struct hists *hists)
2430{
2431 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2432 hists->stats.total_period;
2433}
Namhyung Kim33db4562014-02-07 12:06:07 +09002434
2435int parse_filter_percentage(const struct option *opt __maybe_unused,
2436 const char *arg, int unset __maybe_unused)
2437{
2438 if (!strcmp(arg, "relative"))
2439 symbol_conf.filter_relative = true;
2440 else if (!strcmp(arg, "absolute"))
2441 symbol_conf.filter_relative = false;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002442 else {
2443 pr_debug("Invalud percentage: %s\n", arg);
Namhyung Kim33db4562014-02-07 12:06:07 +09002444 return -1;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002445 }
Namhyung Kim33db4562014-02-07 12:06:07 +09002446
2447 return 0;
2448}
Namhyung Kim0b93da12014-01-14 12:02:15 +09002449
2450int perf_hist_config(const char *var, const char *value)
2451{
2452 if (!strcmp(var, "hist.percentage"))
2453 return parse_filter_percentage(NULL, value, 0);
2454
2455 return 0;
2456}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002457
Jiri Olsa5b658552016-01-18 10:24:22 +01002458int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002459{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002460 memset(hists, 0, sizeof(*hists));
2461 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
2462 hists->entries_in = &hists->entries_in_array[0];
2463 hists->entries_collapsed = RB_ROOT;
2464 hists->entries = RB_ROOT;
2465 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04002466 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01002467 hists->hpp_list = hpp_list;
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002468 INIT_LIST_HEAD(&hists->hpp_formats);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002469 return 0;
2470}
2471
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002472static void hists__delete_remaining_entries(struct rb_root *root)
2473{
2474 struct rb_node *node;
2475 struct hist_entry *he;
2476
2477 while (!RB_EMPTY_ROOT(root)) {
2478 node = rb_first(root);
2479 rb_erase(node, root);
2480
2481 he = rb_entry(node, struct hist_entry, rb_node_in);
2482 hist_entry__delete(he);
2483 }
2484}
2485
2486static void hists__delete_all_entries(struct hists *hists)
2487{
2488 hists__delete_entries(hists);
2489 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2490 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2491 hists__delete_remaining_entries(&hists->entries_collapsed);
2492}
2493
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002494static void hists_evsel__exit(struct perf_evsel *evsel)
2495{
2496 struct hists *hists = evsel__hists(evsel);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002497 struct perf_hpp_fmt *fmt, *pos;
2498 struct perf_hpp_list_node *node, *tmp;
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002499
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002500 hists__delete_all_entries(hists);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002501
2502 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2503 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2504 list_del(&fmt->list);
2505 free(fmt);
2506 }
2507 list_del(&node->list);
2508 free(node);
2509 }
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002510}
2511
Namhyung Kimfc284be2016-01-07 10:14:10 +01002512static int hists_evsel__init(struct perf_evsel *evsel)
2513{
2514 struct hists *hists = evsel__hists(evsel);
2515
Jiri Olsa5b658552016-01-18 10:24:22 +01002516 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01002517 return 0;
2518}
2519
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002520/*
2521 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2522 * stored in the rbtree...
2523 */
2524
2525int hists__init(void)
2526{
2527 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002528 hists_evsel__init,
2529 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002530 if (err)
2531 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2532
2533 return err;
2534}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01002535
2536void perf_hpp_list__init(struct perf_hpp_list *list)
2537{
2538 INIT_LIST_HEAD(&list->fields);
2539 INIT_LIST_HEAD(&list->sorts);
2540}