blob: 37a08f20730af9b5eb6af41e84a5525b050030a6 [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
Arnaldo Carvalho de Meloe8e6d372015-08-10 16:53:54 -0300180 if (h->srcline)
181 hists__new_col_len(hists, HISTC_SRCLINE, strlen(h->srcline));
182
Andi Kleen31191a82015-08-07 15:54:24 -0700183 if (h->srcfile)
184 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
185
Andi Kleen475eeab2013-09-20 07:40:43 -0700186 if (h->transaction)
187 hists__new_col_len(hists, HISTC_TRANSACTION,
188 hist_entry__transaction_len());
Namhyung Kim0c0af782016-02-21 23:22:38 +0900189
190 if (h->trace_output)
191 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300192}
193
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900194void hists__output_recalc_col_len(struct hists *hists, int max_rows)
195{
196 struct rb_node *next = rb_first(&hists->entries);
197 struct hist_entry *n;
198 int row = 0;
199
200 hists__reset_col_len(hists);
201
202 while (next && row++ < max_rows) {
203 n = rb_entry(next, struct hist_entry, rb_node);
204 if (!n->filtered)
205 hists__calc_col_len(hists, n);
206 next = rb_next(&n->rb_node);
207 }
208}
209
Namhyung Kimf39056f2014-01-14 14:25:37 +0900210static void he_stat__add_cpumode_period(struct he_stat *he_stat,
211 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800212{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300213 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800214 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900215 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800216 break;
217 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900218 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800219 break;
220 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900221 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800222 break;
223 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900224 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800225 break;
226 default:
227 break;
228 }
229}
230
Andi Kleen05484292013-01-24 16:10:29 +0100231static void he_stat__add_period(struct he_stat *he_stat, u64 period,
232 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900233{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100234
Namhyung Kim139c0812012-10-04 21:49:43 +0900235 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100236 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900237 he_stat->nr_events += 1;
238}
239
240static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
241{
242 dest->period += src->period;
243 dest->period_sys += src->period_sys;
244 dest->period_us += src->period_us;
245 dest->period_guest_sys += src->period_guest_sys;
246 dest->period_guest_us += src->period_guest_us;
247 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100248 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900249}
250
Namhyung Kimf39056f2014-01-14 14:25:37 +0900251static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300252{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900253 he_stat->period = (he_stat->period * 7) / 8;
254 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100255 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300256}
257
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900258static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
259
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300260static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
261{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900262 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900263 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200264
265 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300266 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200267
Namhyung Kimf39056f2014-01-14 14:25:37 +0900268 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900269 if (symbol_conf.cumulate_callchain)
270 he_stat__decay(he->stat_acc);
Namhyung Kim42b276a2016-01-05 12:06:00 +0900271 decay_callchain(he->callchain);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200272
Namhyung Kim3186b682014-04-22 13:44:23 +0900273 diff = prev_period - he->stat.period;
274
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900275 if (!he->depth) {
276 hists->stats.total_period -= diff;
277 if (!he->filtered)
278 hists->stats.total_non_filtered_period -= diff;
279 }
280
281 if (!he->leaf) {
282 struct hist_entry *child;
283 struct rb_node *node = rb_first(&he->hroot_out);
284 while (node) {
285 child = rb_entry(node, struct hist_entry, rb_node);
286 node = rb_next(node);
287
288 if (hists__decay_entry(hists, child))
289 hists__delete_entry(hists, child);
290 }
291 }
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200292
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900293 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300294}
295
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300296static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
297{
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900298 struct rb_root *root_in;
299 struct rb_root *root_out;
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300300
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900301 if (he->parent_he) {
302 root_in = &he->parent_he->hroot_in;
303 root_out = &he->parent_he->hroot_out;
304 } else {
Jiri Olsa52225032016-05-03 13:54:42 +0200305 if (hists__has(hists, need_collapse))
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900306 root_in = &hists->entries_collapsed;
307 else
308 root_in = hists->entries_in;
309 root_out = &hists->entries;
310 }
311
312 rb_erase(&he->rb_node_in, root_in);
313 rb_erase(&he->rb_node, root_out);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300314
315 --hists->nr_entries;
316 if (!he->filtered)
317 --hists->nr_non_filtered_entries;
318
319 hist_entry__delete(he);
320}
321
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900322void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300323{
324 struct rb_node *next = rb_first(&hists->entries);
325 struct hist_entry *n;
326
327 while (next) {
328 n = rb_entry(next, struct hist_entry, rb_node);
329 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200330 if (((zap_user && n->level == '.') ||
331 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300332 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300333 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300334 }
335 }
336}
337
Namhyung Kim701937b2014-08-12 17:16:05 +0900338void hists__delete_entries(struct hists *hists)
339{
340 struct rb_node *next = rb_first(&hists->entries);
341 struct hist_entry *n;
342
343 while (next) {
344 n = rb_entry(next, struct hist_entry, rb_node);
345 next = rb_next(&n->rb_node);
346
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300347 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900348 }
349}
350
John Kacur3d1d07e2009-09-28 15:32:55 +0200351/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300352 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200353 */
354
Jiri Olsa0a269a62016-07-05 08:56:03 +0200355static int hist_entry__init(struct hist_entry *he,
356 struct hist_entry *template,
357 bool sample_self)
358{
359 *he = *template;
360
361 if (symbol_conf.cumulate_callchain) {
362 he->stat_acc = malloc(sizeof(he->stat));
363 if (he->stat_acc == NULL)
364 return -ENOMEM;
365 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
366 if (!sample_self)
367 memset(&he->stat, 0, sizeof(he->stat));
368 }
369
370 map__get(he->ms.map);
371
372 if (he->branch_info) {
373 /*
374 * This branch info is (a part of) allocated from
375 * sample__resolve_bstack() and will be freed after
376 * adding new entries. So we need to save a copy.
377 */
378 he->branch_info = malloc(sizeof(*he->branch_info));
379 if (he->branch_info == NULL) {
380 map__zput(he->ms.map);
381 free(he->stat_acc);
382 return -ENOMEM;
383 }
384
385 memcpy(he->branch_info, template->branch_info,
386 sizeof(*he->branch_info));
387
388 map__get(he->branch_info->from.map);
389 map__get(he->branch_info->to.map);
390 }
391
392 if (he->mem_info) {
393 map__get(he->mem_info->iaddr.map);
394 map__get(he->mem_info->daddr.map);
395 }
396
397 if (symbol_conf.use_callchain)
398 callchain_init(he->callchain);
399
400 if (he->raw_data) {
401 he->raw_data = memdup(he->raw_data, he->raw_size);
402
403 if (he->raw_data == NULL) {
404 map__put(he->ms.map);
405 if (he->branch_info) {
406 map__put(he->branch_info->from.map);
407 map__put(he->branch_info->to.map);
408 free(he->branch_info);
409 }
410 if (he->mem_info) {
411 map__put(he->mem_info->iaddr.map);
412 map__put(he->mem_info->daddr.map);
413 }
414 free(he->stat_acc);
415 return -ENOMEM;
416 }
417 }
418 INIT_LIST_HEAD(&he->pairs.node);
419 thread__get(he->thread);
Namhyung Kimd2580c72016-09-13 16:45:48 +0900420 he->hroot_in = RB_ROOT;
421 he->hroot_out = RB_ROOT;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200422
423 if (!symbol_conf.report_hierarchy)
424 he->leaf = true;
425
426 return 0;
427}
428
Jiri Olsaf542e762016-07-05 08:56:04 +0200429static void *hist_entry__zalloc(size_t size)
430{
431 return zalloc(size + sizeof(struct hist_entry));
432}
433
434static void hist_entry__free(void *ptr)
435{
436 free(ptr);
437}
438
439static struct hist_entry_ops default_ops = {
440 .new = hist_entry__zalloc,
441 .free = hist_entry__free,
442};
443
Namhyung Kima0b51af2012-09-11 13:34:27 +0900444static struct hist_entry *hist_entry__new(struct hist_entry *template,
445 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300446{
Jiri Olsaf542e762016-07-05 08:56:04 +0200447 struct hist_entry_ops *ops = template->ops;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900448 size_t callchain_size = 0;
449 struct hist_entry *he;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200450 int err = 0;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900451
Jiri Olsaf542e762016-07-05 08:56:04 +0200452 if (!ops)
453 ops = template->ops = &default_ops;
454
Namhyung Kim82aa0192014-12-22 13:44:14 +0900455 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900456 callchain_size = sizeof(struct callchain_root);
457
Jiri Olsaf542e762016-07-05 08:56:04 +0200458 he = ops->new(callchain_size);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200459 if (he) {
460 err = hist_entry__init(he, template, sample_self);
Jiri Olsaf542e762016-07-05 08:56:04 +0200461 if (err) {
462 ops->free(he);
463 he = NULL;
464 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300465 }
466
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200467 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300468}
469
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300470static u8 symbol__parent_filter(const struct symbol *parent)
471{
472 if (symbol_conf.exclude_other && parent == NULL)
473 return 1 << HIST_FILTER__PARENT;
474 return 0;
475}
476
Namhyung Kim467ef102016-02-16 23:08:19 +0900477static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
478{
479 if (!symbol_conf.use_callchain)
480 return;
481
482 he->hists->callchain_period += period;
483 if (!he->filtered)
484 he->hists->callchain_non_filtered_period += period;
485}
486
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300487static struct hist_entry *hists__findnew_entry(struct hists *hists,
488 struct hist_entry *entry,
489 struct addr_location *al,
490 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300491{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300492 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300493 struct rb_node *parent = NULL;
494 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700495 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900496 u64 period = entry->stat.period;
497 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300498
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300499 p = &hists->entries_in->rb_node;
500
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300501 while (*p != NULL) {
502 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300503 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300504
Namhyung Kim9afcf932012-12-10 17:29:54 +0900505 /*
506 * Make sure that it receives arguments in a same order as
507 * hist_entry__collapse() so that we can use an appropriate
508 * function when searching an entry regardless which sort
509 * keys were used.
510 */
511 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300512
513 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900514 if (sample_self) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900515 he_stat__add_period(&he->stat, period, weight);
Namhyung Kim467ef102016-02-16 23:08:19 +0900516 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900517 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900518 if (symbol_conf.cumulate_callchain)
519 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400520
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900521 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300522 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900523 * and will not be used anymore.
524 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300525 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900526
David Miller63fa4712012-03-27 03:14:18 -0400527 /* If the map of an existing hist_entry has
528 * become out-of-date due to an exec() or
529 * similar, update it. Otherwise we will
530 * mis-adjust symbol addresses when computing
531 * the history counter to increment.
532 */
533 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300534 map__put(he->ms.map);
535 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400536 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300537 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300538 }
539
540 if (cmp < 0)
541 p = &(*p)->rb_left;
542 else
543 p = &(*p)->rb_right;
544 }
545
Namhyung Kima0b51af2012-09-11 13:34:27 +0900546 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300547 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900548 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300549
Namhyung Kim0f584742016-01-28 00:40:49 +0900550 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900551 hist_entry__add_callchain_period(he, period);
552 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900553
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300554 rb_link_node(&he->rb_node_in, parent, p);
555 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300556out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900557 if (sample_self)
558 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900559 if (symbol_conf.cumulate_callchain)
560 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300561 return he;
562}
563
Jiri Olsaa5051972016-07-05 08:56:05 +0200564static struct hist_entry*
565__hists__add_entry(struct hists *hists,
566 struct addr_location *al,
567 struct symbol *sym_parent,
568 struct branch_info *bi,
569 struct mem_info *mi,
570 struct perf_sample *sample,
571 bool sample_self,
572 struct hist_entry_ops *ops)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100573{
574 struct hist_entry entry = {
575 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900576 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100577 .ms = {
578 .map = al->map,
579 .sym = al->sym,
580 },
Kan Liang0c4c4de2015-09-04 10:45:42 -0400581 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400582 .cpu = al->cpu,
583 .cpumode = al->cpumode,
584 .ip = al->addr,
585 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900586 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900587 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900588 .period = sample->period,
589 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900590 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100591 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300592 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300593 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900594 .branch_info = bi,
595 .mem_info = mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900596 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900597 .raw_data = sample->raw_data,
598 .raw_size = sample->raw_size,
Jiri Olsaa5051972016-07-05 08:56:05 +0200599 .ops = ops,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100600 };
601
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300602 return hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100603}
604
Jiri Olsaa5051972016-07-05 08:56:05 +0200605struct hist_entry *hists__add_entry(struct hists *hists,
606 struct addr_location *al,
607 struct symbol *sym_parent,
608 struct branch_info *bi,
609 struct mem_info *mi,
610 struct perf_sample *sample,
611 bool sample_self)
612{
613 return __hists__add_entry(hists, al, sym_parent, bi, mi,
614 sample, sample_self, NULL);
615}
616
617struct hist_entry *hists__add_entry_ops(struct hists *hists,
618 struct hist_entry_ops *ops,
619 struct addr_location *al,
620 struct symbol *sym_parent,
621 struct branch_info *bi,
622 struct mem_info *mi,
623 struct perf_sample *sample,
624 bool sample_self)
625{
626 return __hists__add_entry(hists, al, sym_parent, bi, mi,
627 sample, sample_self, ops);
628}
629
Namhyung Kim69bcb012013-10-30 09:40:34 +0900630static int
631iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
632 struct addr_location *al __maybe_unused)
633{
634 return 0;
635}
636
637static int
638iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
639 struct addr_location *al __maybe_unused)
640{
641 return 0;
642}
643
644static int
645iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
646{
647 struct perf_sample *sample = iter->sample;
648 struct mem_info *mi;
649
650 mi = sample__resolve_mem(sample, al);
651 if (mi == NULL)
652 return -ENOMEM;
653
654 iter->priv = mi;
655 return 0;
656}
657
658static int
659iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
660{
661 u64 cost;
662 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300663 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900664 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900665 struct hist_entry *he;
666
667 if (mi == NULL)
668 return -EINVAL;
669
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900670 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900671 if (!cost)
672 cost = 1;
673
674 /*
675 * must pass period=weight in order to get the correct
676 * sorting from hists__collapse_resort() which is solely
677 * based on periods. We want sorting be done on nr_events * weight
678 * and this is indirectly achieved by passing period=weight here
679 * and the he_stat__add_period() function.
680 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900681 sample->period = cost;
682
Jiri Olsa0102ef32016-06-14 20:19:21 +0200683 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
684 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900685 if (!he)
686 return -ENOMEM;
687
688 iter->he = he;
689 return 0;
690}
691
692static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900693iter_finish_mem_entry(struct hist_entry_iter *iter,
694 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900695{
696 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300697 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900698 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900699 int err = -EINVAL;
700
701 if (he == NULL)
702 goto out;
703
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300704 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900705
706 err = hist_entry__append_callchain(he, iter->sample);
707
708out:
709 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300710 * We don't need to free iter->priv (mem_info) here since the mem info
711 * was either already freed in hists__findnew_entry() or passed to a
712 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900713 */
714 iter->priv = NULL;
715
716 iter->he = NULL;
717 return err;
718}
719
720static int
721iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
722{
723 struct branch_info *bi;
724 struct perf_sample *sample = iter->sample;
725
726 bi = sample__resolve_bstack(sample, al);
727 if (!bi)
728 return -ENOMEM;
729
730 iter->curr = 0;
731 iter->total = sample->branch_stack->nr;
732
733 iter->priv = bi;
734 return 0;
735}
736
737static int
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -0300738iter_add_single_branch_entry(struct hist_entry_iter *iter,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900739 struct addr_location *al __maybe_unused)
740{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900741 /* to avoid calling callback function */
742 iter->he = NULL;
743
Namhyung Kim69bcb012013-10-30 09:40:34 +0900744 return 0;
745}
746
747static int
748iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
749{
750 struct branch_info *bi = iter->priv;
751 int i = iter->curr;
752
753 if (bi == NULL)
754 return 0;
755
756 if (iter->curr >= iter->total)
757 return 0;
758
759 al->map = bi[i].to.map;
760 al->sym = bi[i].to.sym;
761 al->addr = bi[i].to.addr;
762 return 1;
763}
764
765static int
766iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
767{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900768 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900769 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300770 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900771 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900772 struct hist_entry *he = NULL;
773 int i = iter->curr;
774 int err = 0;
775
776 bi = iter->priv;
777
778 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
779 goto out;
780
781 /*
782 * The report shows the percentage of total branches captured
783 * and not events sampled. Thus we use a pseudo period of 1.
784 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900785 sample->period = 1;
786 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
787
Jiri Olsa0102ef32016-06-14 20:19:21 +0200788 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
789 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900790 if (he == NULL)
791 return -ENOMEM;
792
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300793 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900794
795out:
796 iter->he = he;
797 iter->curr++;
798 return err;
799}
800
801static int
802iter_finish_branch_entry(struct hist_entry_iter *iter,
803 struct addr_location *al __maybe_unused)
804{
805 zfree(&iter->priv);
806 iter->he = NULL;
807
808 return iter->curr >= iter->total ? 0 : -1;
809}
810
811static int
812iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
813 struct addr_location *al __maybe_unused)
814{
815 return 0;
816}
817
818static int
819iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
820{
821 struct perf_evsel *evsel = iter->evsel;
822 struct perf_sample *sample = iter->sample;
823 struct hist_entry *he;
824
Jiri Olsa0102ef32016-06-14 20:19:21 +0200825 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
826 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900827 if (he == NULL)
828 return -ENOMEM;
829
830 iter->he = he;
831 return 0;
832}
833
834static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900835iter_finish_normal_entry(struct hist_entry_iter *iter,
836 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900837{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900838 struct hist_entry *he = iter->he;
839 struct perf_evsel *evsel = iter->evsel;
840 struct perf_sample *sample = iter->sample;
841
842 if (he == NULL)
843 return 0;
844
845 iter->he = NULL;
846
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300847 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900848
849 return hist_entry__append_callchain(he, sample);
850}
851
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900852static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300853iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900854 struct addr_location *al __maybe_unused)
855{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900856 struct hist_entry **he_cache;
857
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900858 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900859
860 /*
861 * This is for detecting cycles or recursions so that they're
862 * cumulated only one time to prevent entries more than 100%
863 * overhead.
864 */
Adrian Hunter96b40f32015-09-25 16:15:47 +0300865 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900866 if (he_cache == NULL)
867 return -ENOMEM;
868
869 iter->priv = he_cache;
870 iter->curr = 0;
871
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900872 return 0;
873}
874
875static int
876iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
877 struct addr_location *al)
878{
879 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300880 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900881 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900882 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900883 struct hist_entry *he;
884 int err = 0;
885
Jiri Olsa0102ef32016-06-14 20:19:21 +0200886 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
887 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900888 if (he == NULL)
889 return -ENOMEM;
890
891 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900892 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900893
Namhyung Kim82aa0192014-12-22 13:44:14 +0900894 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900895
896 /*
897 * We need to re-initialize the cursor since callchain_append()
898 * advanced the cursor to the end.
899 */
900 callchain_cursor_commit(&callchain_cursor);
901
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300902 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900903
904 return err;
905}
906
907static int
908iter_next_cumulative_entry(struct hist_entry_iter *iter,
909 struct addr_location *al)
910{
911 struct callchain_cursor_node *node;
912
913 node = callchain_cursor_current(&callchain_cursor);
914 if (node == NULL)
915 return 0;
916
Namhyung Kimc7405d82013-10-31 13:58:30 +0900917 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900918}
919
920static int
921iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
922 struct addr_location *al)
923{
924 struct perf_evsel *evsel = iter->evsel;
925 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900926 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900927 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900928 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -0300929 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900930 .cpu = al->cpu,
931 .thread = al->thread,
932 .comm = thread__comm(al->thread),
933 .ip = al->addr,
934 .ms = {
935 .map = al->map,
936 .sym = al->sym,
937 },
938 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +0900939 .raw_data = sample->raw_data,
940 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900941 };
942 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900943 struct callchain_cursor cursor;
944
945 callchain_cursor_snapshot(&cursor, &callchain_cursor);
946
947 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900948
949 /*
950 * Check if there's duplicate entries in the callchain.
951 * It's possible that it has cycles or recursive calls.
952 */
953 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900954 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
955 /* to avoid calling callback function */
956 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900957 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900958 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900959 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900960
Jiri Olsa0102ef32016-06-14 20:19:21 +0200961 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
962 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900963 if (he == NULL)
964 return -ENOMEM;
965
966 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900967 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900968
Namhyung Kim82aa0192014-12-22 13:44:14 +0900969 if (symbol_conf.use_callchain)
970 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900971 return 0;
972}
973
974static int
975iter_finish_cumulative_entry(struct hist_entry_iter *iter,
976 struct addr_location *al __maybe_unused)
977{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900978 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900979 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900980
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900981 return 0;
982}
983
Namhyung Kim69bcb012013-10-30 09:40:34 +0900984const struct hist_iter_ops hist_iter_mem = {
985 .prepare_entry = iter_prepare_mem_entry,
986 .add_single_entry = iter_add_single_mem_entry,
987 .next_entry = iter_next_nop_entry,
988 .add_next_entry = iter_add_next_nop_entry,
989 .finish_entry = iter_finish_mem_entry,
990};
991
992const struct hist_iter_ops hist_iter_branch = {
993 .prepare_entry = iter_prepare_branch_entry,
994 .add_single_entry = iter_add_single_branch_entry,
995 .next_entry = iter_next_branch_entry,
996 .add_next_entry = iter_add_next_branch_entry,
997 .finish_entry = iter_finish_branch_entry,
998};
999
1000const struct hist_iter_ops hist_iter_normal = {
1001 .prepare_entry = iter_prepare_normal_entry,
1002 .add_single_entry = iter_add_single_normal_entry,
1003 .next_entry = iter_next_nop_entry,
1004 .add_next_entry = iter_add_next_nop_entry,
1005 .finish_entry = iter_finish_normal_entry,
1006};
1007
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001008const struct hist_iter_ops hist_iter_cumulative = {
1009 .prepare_entry = iter_prepare_cumulative_entry,
1010 .add_single_entry = iter_add_single_cumulative_entry,
1011 .next_entry = iter_next_cumulative_entry,
1012 .add_next_entry = iter_add_next_cumulative_entry,
1013 .finish_entry = iter_finish_cumulative_entry,
1014};
1015
Namhyung Kim69bcb012013-10-30 09:40:34 +09001016int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001017 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +09001018{
1019 int err, err2;
1020
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001021 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
Namhyung Kim063bd932015-05-19 17:04:10 +09001022 iter->evsel, al, max_stack_depth);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001023 if (err)
1024 return err;
1025
Adrian Hunter96b40f32015-09-25 16:15:47 +03001026 iter->max_stack = max_stack_depth;
1027
Namhyung Kim69bcb012013-10-30 09:40:34 +09001028 err = iter->ops->prepare_entry(iter, al);
1029 if (err)
1030 goto out;
1031
1032 err = iter->ops->add_single_entry(iter, al);
1033 if (err)
1034 goto out;
1035
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001036 if (iter->he && iter->add_entry_cb) {
1037 err = iter->add_entry_cb(iter, al, true, arg);
1038 if (err)
1039 goto out;
1040 }
1041
Namhyung Kim69bcb012013-10-30 09:40:34 +09001042 while (iter->ops->next_entry(iter, al)) {
1043 err = iter->ops->add_next_entry(iter, al);
1044 if (err)
1045 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001046
1047 if (iter->he && iter->add_entry_cb) {
1048 err = iter->add_entry_cb(iter, al, false, arg);
1049 if (err)
1050 goto out;
1051 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001052 }
1053
1054out:
1055 err2 = iter->ops->finish_entry(iter, al);
1056 if (!err)
1057 err = err2;
1058
1059 return err;
1060}
1061
John Kacur3d1d07e2009-09-28 15:32:55 +02001062int64_t
1063hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1064{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001065 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001066 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001067 int64_t cmp = 0;
1068
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001069 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001070 if (perf_hpp__is_dynamic_entry(fmt) &&
1071 !perf_hpp__defined_dynamic_entry(fmt, hists))
1072 continue;
1073
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001074 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001075 if (cmp)
1076 break;
1077 }
1078
1079 return cmp;
1080}
1081
1082int64_t
1083hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1084{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001085 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001086 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001087 int64_t cmp = 0;
1088
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001089 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001090 if (perf_hpp__is_dynamic_entry(fmt) &&
1091 !perf_hpp__defined_dynamic_entry(fmt, hists))
1092 continue;
1093
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001094 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001095 if (cmp)
1096 break;
1097 }
1098
1099 return cmp;
1100}
1101
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001102void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001103{
Jiri Olsaf542e762016-07-05 08:56:04 +02001104 struct hist_entry_ops *ops = he->ops;
1105
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001106 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001107 map__zput(he->ms.map);
1108
1109 if (he->branch_info) {
1110 map__zput(he->branch_info->from.map);
1111 map__zput(he->branch_info->to.map);
Andi Kleen508be0d2016-05-20 13:15:08 -07001112 free_srcline(he->branch_info->srcline_from);
1113 free_srcline(he->branch_info->srcline_to);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001114 zfree(&he->branch_info);
1115 }
1116
1117 if (he->mem_info) {
1118 map__zput(he->mem_info->iaddr.map);
1119 map__zput(he->mem_info->daddr.map);
1120 zfree(&he->mem_info);
1121 }
1122
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001123 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +09001124 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -07001125 if (he->srcfile && he->srcfile[0])
1126 free(he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001127 free_callchain(he->callchain);
Namhyung Kim60517d22015-12-23 02:07:03 +09001128 free(he->trace_output);
Namhyung Kim72392832015-12-24 11:16:17 +09001129 free(he->raw_data);
Jiri Olsaf542e762016-07-05 08:56:04 +02001130 ops->free(he);
John Kacur3d1d07e2009-09-28 15:32:55 +02001131}
1132
1133/*
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001134 * If this is not the last column, then we need to pad it according to the
1135 * pre-calculated max lenght for this column, otherwise don't bother adding
1136 * spaces because that would break viewing this with, for instance, 'less',
1137 * that would show tons of trailing spaces when a long C++ demangled method
1138 * names is sampled.
1139*/
1140int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1141 struct perf_hpp_fmt *fmt, int printed)
1142{
1143 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
Jiri Olsada1b0402016-06-14 20:19:20 +02001144 const int width = fmt->width(fmt, hpp, he->hists);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001145 if (printed < width) {
1146 advance_hpp(hpp, printed);
1147 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1148 }
1149 }
1150
1151 return printed;
1152}
1153
1154/*
John Kacur3d1d07e2009-09-28 15:32:55 +02001155 * collapse the histogram
1156 */
1157
Namhyung Kimaef810e2016-02-25 00:13:34 +09001158static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
Namhyung Kimaec13a72016-03-09 22:46:58 +09001159static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1160 enum hist_filter type);
1161
1162typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1163
1164static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1165{
1166 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1167}
1168
1169static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1170 enum hist_filter type,
1171 fmt_chk_fn check)
1172{
1173 struct perf_hpp_fmt *fmt;
1174 bool type_match = false;
1175 struct hist_entry *parent = he->parent_he;
1176
1177 switch (type) {
1178 case HIST_FILTER__THREAD:
1179 if (symbol_conf.comm_list == NULL &&
1180 symbol_conf.pid_list == NULL &&
1181 symbol_conf.tid_list == NULL)
1182 return;
1183 break;
1184 case HIST_FILTER__DSO:
1185 if (symbol_conf.dso_list == NULL)
1186 return;
1187 break;
1188 case HIST_FILTER__SYMBOL:
1189 if (symbol_conf.sym_list == NULL)
1190 return;
1191 break;
1192 case HIST_FILTER__PARENT:
1193 case HIST_FILTER__GUEST:
1194 case HIST_FILTER__HOST:
1195 case HIST_FILTER__SOCKET:
1196 default:
1197 return;
1198 }
1199
1200 /* if it's filtered by own fmt, it has to have filter bits */
1201 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1202 if (check(fmt)) {
1203 type_match = true;
1204 break;
1205 }
1206 }
1207
1208 if (type_match) {
1209 /*
1210 * If the filter is for current level entry, propagate
1211 * filter marker to parents. The marker bit was
1212 * already set by default so it only needs to clear
1213 * non-filtered entries.
1214 */
1215 if (!(he->filtered & (1 << type))) {
1216 while (parent) {
1217 parent->filtered &= ~(1 << type);
1218 parent = parent->parent_he;
1219 }
1220 }
1221 } else {
1222 /*
1223 * If current entry doesn't have matching formats, set
1224 * filter marker for upper level entries. it will be
1225 * cleared if its lower level entries is not filtered.
1226 *
1227 * For lower-level entries, it inherits parent's
1228 * filter bit so that lower level entries of a
1229 * non-filtered entry won't set the filter marker.
1230 */
1231 if (parent == NULL)
1232 he->filtered |= (1 << type);
1233 else
1234 he->filtered |= (parent->filtered & (1 << type));
1235 }
1236}
1237
1238static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1239{
1240 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1241 check_thread_entry);
1242
1243 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1244 perf_hpp__is_dso_entry);
1245
1246 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1247 perf_hpp__is_sym_entry);
1248
1249 hists__apply_filters(he->hists, he);
1250}
Namhyung Kimaef810e2016-02-25 00:13:34 +09001251
1252static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1253 struct rb_root *root,
1254 struct hist_entry *he,
Namhyung Kimaec13a72016-03-09 22:46:58 +09001255 struct hist_entry *parent_he,
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001256 struct perf_hpp_list *hpp_list)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001257{
1258 struct rb_node **p = &root->rb_node;
1259 struct rb_node *parent = NULL;
1260 struct hist_entry *iter, *new;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001261 struct perf_hpp_fmt *fmt;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001262 int64_t cmp;
1263
1264 while (*p != NULL) {
1265 parent = *p;
1266 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1267
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001268 cmp = 0;
1269 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1270 cmp = fmt->collapse(fmt, iter, he);
1271 if (cmp)
1272 break;
1273 }
1274
Namhyung Kimaef810e2016-02-25 00:13:34 +09001275 if (!cmp) {
1276 he_stat__add_stat(&iter->stat, &he->stat);
1277 return iter;
1278 }
1279
1280 if (cmp < 0)
1281 p = &parent->rb_left;
1282 else
1283 p = &parent->rb_right;
1284 }
1285
1286 new = hist_entry__new(he, true);
1287 if (new == NULL)
1288 return NULL;
1289
Namhyung Kimaef810e2016-02-25 00:13:34 +09001290 hists->nr_entries++;
1291
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001292 /* save related format list for output */
1293 new->hpp_list = hpp_list;
Namhyung Kimaec13a72016-03-09 22:46:58 +09001294 new->parent_he = parent_he;
1295
1296 hist_entry__apply_hierarchy_filters(new);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001297
1298 /* some fields are now passed to 'new' */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001299 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1300 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1301 he->trace_output = NULL;
1302 else
1303 new->trace_output = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001304
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001305 if (perf_hpp__is_srcline_entry(fmt))
1306 he->srcline = NULL;
1307 else
1308 new->srcline = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001309
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001310 if (perf_hpp__is_srcfile_entry(fmt))
1311 he->srcfile = NULL;
1312 else
1313 new->srcfile = NULL;
1314 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001315
1316 rb_link_node(&new->rb_node_in, parent, p);
1317 rb_insert_color(&new->rb_node_in, root);
1318 return new;
1319}
1320
1321static int hists__hierarchy_insert_entry(struct hists *hists,
1322 struct rb_root *root,
1323 struct hist_entry *he)
1324{
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001325 struct perf_hpp_list_node *node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001326 struct hist_entry *new_he = NULL;
1327 struct hist_entry *parent = NULL;
1328 int depth = 0;
1329 int ret = 0;
1330
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001331 list_for_each_entry(node, &hists->hpp_formats, list) {
1332 /* skip period (overhead) and elided columns */
1333 if (node->level == 0 || node->skip)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001334 continue;
1335
1336 /* insert copy of 'he' for each fmt into the hierarchy */
Namhyung Kimaec13a72016-03-09 22:46:58 +09001337 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001338 if (new_he == NULL) {
1339 ret = -1;
1340 break;
1341 }
1342
1343 root = &new_he->hroot_in;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001344 new_he->depth = depth++;
1345 parent = new_he;
1346 }
1347
1348 if (new_he) {
1349 new_he->leaf = true;
1350
1351 if (symbol_conf.use_callchain) {
1352 callchain_cursor_reset(&callchain_cursor);
1353 if (callchain_merge(&callchain_cursor,
1354 new_he->callchain,
1355 he->callchain) < 0)
1356 ret = -1;
1357 }
1358 }
1359
1360 /* 'he' is no longer used */
1361 hist_entry__delete(he);
1362
1363 /* return 0 (or -1) since it already applied filters */
1364 return ret;
1365}
1366
Jiri Olsa592dac62016-03-24 13:52:17 +01001367static int hists__collapse_insert_entry(struct hists *hists,
1368 struct rb_root *root,
1369 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001370{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001371 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001372 struct rb_node *parent = NULL;
1373 struct hist_entry *iter;
1374 int64_t cmp;
1375
Namhyung Kimaef810e2016-02-25 00:13:34 +09001376 if (symbol_conf.report_hierarchy)
1377 return hists__hierarchy_insert_entry(hists, root, he);
1378
John Kacur3d1d07e2009-09-28 15:32:55 +02001379 while (*p != NULL) {
1380 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001381 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001382
1383 cmp = hist_entry__collapse(iter, he);
1384
1385 if (!cmp) {
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001386 int ret = 0;
1387
Namhyung Kim139c0812012-10-04 21:49:43 +09001388 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001389 if (symbol_conf.cumulate_callchain)
1390 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001391
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001392 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001393 callchain_cursor_reset(&callchain_cursor);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001394 if (callchain_merge(&callchain_cursor,
1395 iter->callchain,
1396 he->callchain) < 0)
1397 ret = -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001398 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001399 hist_entry__delete(he);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001400 return ret;
John Kacur3d1d07e2009-09-28 15:32:55 +02001401 }
1402
1403 if (cmp < 0)
1404 p = &(*p)->rb_left;
1405 else
1406 p = &(*p)->rb_right;
1407 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001408 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001409
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001410 rb_link_node(&he->rb_node_in, parent, p);
1411 rb_insert_color(&he->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001412 return 1;
John Kacur3d1d07e2009-09-28 15:32:55 +02001413}
1414
Namhyung Kimfc284be2016-01-07 10:14:10 +01001415struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001416{
1417 struct rb_root *root;
1418
1419 pthread_mutex_lock(&hists->lock);
1420
1421 root = hists->entries_in;
1422 if (++hists->entries_in > &hists->entries_in_array[1])
1423 hists->entries_in = &hists->entries_in_array[0];
1424
1425 pthread_mutex_unlock(&hists->lock);
1426
1427 return root;
1428}
1429
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001430static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1431{
1432 hists__filter_entry_by_dso(hists, he);
1433 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001434 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001435 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001436}
1437
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001438int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001439{
1440 struct rb_root *root;
1441 struct rb_node *next;
1442 struct hist_entry *n;
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001443 int ret;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001444
Jiri Olsa52225032016-05-03 13:54:42 +02001445 if (!hists__has(hists, need_collapse))
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001446 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001447
Namhyung Kim740b97f2014-12-22 13:44:10 +09001448 hists->nr_entries = 0;
1449
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001450 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001451
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001452 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001453
1454 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001455 if (session_done())
1456 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001457 n = rb_entry(next, struct hist_entry, rb_node_in);
1458 next = rb_next(&n->rb_node_in);
1459
1460 rb_erase(&n->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001461 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1462 if (ret < 0)
1463 return -1;
1464
1465 if (ret) {
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001466 /*
1467 * If it wasn't combined with one of the entries already
1468 * collapsed, we need to apply the filters that may have
1469 * been set by, say, the hist_browser.
1470 */
1471 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001472 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001473 if (prog)
1474 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001475 }
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001476 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001477}
1478
Namhyung Kim043ca3892014-03-03 14:18:00 +09001479static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001480{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001481 struct hists *hists = a->hists;
Namhyung Kim043ca3892014-03-03 14:18:00 +09001482 struct perf_hpp_fmt *fmt;
1483 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001484
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001485 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001486 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001487 continue;
1488
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001489 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001490 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001491 break;
1492 }
1493
Namhyung Kim043ca3892014-03-03 14:18:00 +09001494 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001495}
1496
Namhyung Kim9283ba92014-04-24 16:37:26 +09001497static void hists__reset_filter_stats(struct hists *hists)
1498{
1499 hists->nr_non_filtered_entries = 0;
1500 hists->stats.total_non_filtered_period = 0;
1501}
1502
1503void hists__reset_stats(struct hists *hists)
1504{
1505 hists->nr_entries = 0;
1506 hists->stats.total_period = 0;
1507
1508 hists__reset_filter_stats(hists);
1509}
1510
1511static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1512{
1513 hists->nr_non_filtered_entries++;
1514 hists->stats.total_non_filtered_period += h->stat.period;
1515}
1516
1517void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1518{
1519 if (!h->filtered)
1520 hists__inc_filter_stats(hists, h);
1521
1522 hists->nr_entries++;
1523 hists->stats.total_period += h->stat.period;
1524}
1525
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001526static void hierarchy_recalc_total_periods(struct hists *hists)
1527{
1528 struct rb_node *node;
1529 struct hist_entry *he;
1530
1531 node = rb_first(&hists->entries);
1532
1533 hists->stats.total_period = 0;
1534 hists->stats.total_non_filtered_period = 0;
1535
1536 /*
1537 * recalculate total period using top-level entries only
1538 * since lower level entries only see non-filtered entries
1539 * but upper level entries have sum of both entries.
1540 */
1541 while (node) {
1542 he = rb_entry(node, struct hist_entry, rb_node);
1543 node = rb_next(node);
1544
1545 hists->stats.total_period += he->stat.period;
1546 if (!he->filtered)
1547 hists->stats.total_non_filtered_period += he->stat.period;
1548 }
1549}
1550
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001551static void hierarchy_insert_output_entry(struct rb_root *root,
1552 struct hist_entry *he)
1553{
1554 struct rb_node **p = &root->rb_node;
1555 struct rb_node *parent = NULL;
1556 struct hist_entry *iter;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001557 struct perf_hpp_fmt *fmt;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001558
1559 while (*p != NULL) {
1560 parent = *p;
1561 iter = rb_entry(parent, struct hist_entry, rb_node);
1562
1563 if (hist_entry__sort(he, iter) > 0)
1564 p = &parent->rb_left;
1565 else
1566 p = &parent->rb_right;
1567 }
1568
1569 rb_link_node(&he->rb_node, parent, p);
1570 rb_insert_color(&he->rb_node, root);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001571
1572 /* update column width of dynamic entry */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001573 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1574 if (perf_hpp__is_dynamic_entry(fmt))
1575 fmt->sort(fmt, he, NULL);
1576 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001577}
1578
1579static void hists__hierarchy_output_resort(struct hists *hists,
1580 struct ui_progress *prog,
1581 struct rb_root *root_in,
1582 struct rb_root *root_out,
1583 u64 min_callchain_hits,
1584 bool use_callchain)
1585{
1586 struct rb_node *node;
1587 struct hist_entry *he;
1588
1589 *root_out = RB_ROOT;
1590 node = rb_first(root_in);
1591
1592 while (node) {
1593 he = rb_entry(node, struct hist_entry, rb_node_in);
1594 node = rb_next(node);
1595
1596 hierarchy_insert_output_entry(root_out, he);
1597
1598 if (prog)
1599 ui_progress__update(prog, 1);
1600
1601 if (!he->leaf) {
1602 hists__hierarchy_output_resort(hists, prog,
1603 &he->hroot_in,
1604 &he->hroot_out,
1605 min_callchain_hits,
1606 use_callchain);
1607 hists->nr_entries++;
1608 if (!he->filtered) {
1609 hists->nr_non_filtered_entries++;
1610 hists__calc_col_len(hists, he);
1611 }
1612
1613 continue;
1614 }
1615
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001616 if (!use_callchain)
1617 continue;
1618
1619 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1620 u64 total = he->stat.period;
1621
1622 if (symbol_conf.cumulate_callchain)
1623 total = he->stat_acc->period;
1624
1625 min_callchain_hits = total * (callchain_param.min_percent / 100);
1626 }
1627
1628 callchain_param.sort(&he->sorted_chain, he->callchain,
1629 min_callchain_hits, &callchain_param);
1630 }
1631}
1632
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001633static void __hists__insert_output_entry(struct rb_root *entries,
1634 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001635 u64 min_callchain_hits,
1636 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001637{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001638 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001639 struct rb_node *parent = NULL;
1640 struct hist_entry *iter;
Namhyung Kimabab5e72016-02-27 03:52:47 +09001641 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001642
Namhyung Kim744070e2016-01-28 00:40:48 +09001643 if (use_callchain) {
1644 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1645 u64 total = he->stat.period;
1646
1647 if (symbol_conf.cumulate_callchain)
1648 total = he->stat_acc->period;
1649
1650 min_callchain_hits = total * (callchain_param.min_percent / 100);
1651 }
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001652 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001653 min_callchain_hits, &callchain_param);
Namhyung Kim744070e2016-01-28 00:40:48 +09001654 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001655
1656 while (*p != NULL) {
1657 parent = *p;
1658 iter = rb_entry(parent, struct hist_entry, rb_node);
1659
Namhyung Kim043ca3892014-03-03 14:18:00 +09001660 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001661 p = &(*p)->rb_left;
1662 else
1663 p = &(*p)->rb_right;
1664 }
1665
1666 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001667 rb_insert_color(&he->rb_node, entries);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001668
1669 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1670 if (perf_hpp__is_dynamic_entry(fmt) &&
1671 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1672 fmt->sort(fmt, he, NULL); /* update column width */
1673 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001674}
1675
Jiri Olsa01441af2016-01-18 10:23:59 +01001676static void output_resort(struct hists *hists, struct ui_progress *prog,
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001677 bool use_callchain, hists__resort_cb_t cb)
John Kacur3d1d07e2009-09-28 15:32:55 +02001678{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001679 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001680 struct rb_node *next;
1681 struct hist_entry *n;
Namhyung Kim467ef102016-02-16 23:08:19 +09001682 u64 callchain_total;
John Kacur3d1d07e2009-09-28 15:32:55 +02001683 u64 min_callchain_hits;
1684
Namhyung Kim467ef102016-02-16 23:08:19 +09001685 callchain_total = hists->callchain_period;
1686 if (symbol_conf.filter_relative)
1687 callchain_total = hists->callchain_non_filtered_period;
1688
1689 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001690
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001691 hists__reset_stats(hists);
1692 hists__reset_col_len(hists);
1693
1694 if (symbol_conf.report_hierarchy) {
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001695 hists__hierarchy_output_resort(hists, prog,
1696 &hists->entries_collapsed,
1697 &hists->entries,
1698 min_callchain_hits,
1699 use_callchain);
1700 hierarchy_recalc_total_periods(hists);
1701 return;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001702 }
1703
Jiri Olsa52225032016-05-03 13:54:42 +02001704 if (hists__has(hists, need_collapse))
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001705 root = &hists->entries_collapsed;
1706 else
1707 root = hists->entries_in;
1708
1709 next = rb_first(root);
1710 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001711
1712 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001713 n = rb_entry(next, struct hist_entry, rb_node_in);
1714 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001715
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001716 if (cb && cb(n))
1717 continue;
1718
Kan Liangf9db0d02015-08-11 06:30:48 -04001719 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001720 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001721
1722 if (!n->filtered)
1723 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001724
1725 if (prog)
1726 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001727 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001728}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001729
Jiri Olsa452ce032016-01-18 10:24:00 +01001730void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
Jiri Olsa01441af2016-01-18 10:23:59 +01001731{
Jiri Olsa01441af2016-01-18 10:23:59 +01001732 bool use_callchain;
1733
1734 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1735 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1736 else
1737 use_callchain = symbol_conf.use_callchain;
1738
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001739 output_resort(evsel__hists(evsel), prog, use_callchain, NULL);
Jiri Olsa452ce032016-01-18 10:24:00 +01001740}
1741
1742void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1743{
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001744 output_resort(hists, prog, symbol_conf.use_callchain, NULL);
1745}
1746
1747void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1748 hists__resort_cb_t cb)
1749{
1750 output_resort(hists, prog, symbol_conf.use_callchain, cb);
Jiri Olsa01441af2016-01-18 10:23:59 +01001751}
1752
Namhyung Kim8c018722016-02-25 00:13:36 +09001753static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1754{
1755 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1756 return false;
1757
1758 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1759 return true;
1760
1761 return false;
1762}
1763
1764struct rb_node *rb_hierarchy_last(struct rb_node *node)
1765{
1766 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1767
1768 while (can_goto_child(he, HMD_NORMAL)) {
1769 node = rb_last(&he->hroot_out);
1770 he = rb_entry(node, struct hist_entry, rb_node);
1771 }
1772 return node;
1773}
1774
1775struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1776{
1777 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1778
1779 if (can_goto_child(he, hmd))
1780 node = rb_first(&he->hroot_out);
1781 else
1782 node = rb_next(node);
1783
1784 while (node == NULL) {
1785 he = he->parent_he;
1786 if (he == NULL)
1787 break;
1788
1789 node = rb_next(&he->rb_node);
1790 }
1791 return node;
1792}
1793
1794struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1795{
1796 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1797
1798 node = rb_prev(node);
1799 if (node)
1800 return rb_hierarchy_last(node);
1801
1802 he = he->parent_he;
1803 if (he == NULL)
1804 return NULL;
1805
1806 return &he->rb_node;
1807}
1808
Namhyung Kima7b58952016-02-26 21:13:16 +09001809bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1810{
1811 struct rb_node *node;
1812 struct hist_entry *child;
1813 float percent;
1814
1815 if (he->leaf)
1816 return false;
1817
1818 node = rb_first(&he->hroot_out);
1819 child = rb_entry(node, struct hist_entry, rb_node);
1820
1821 while (node && child->filtered) {
1822 node = rb_next(node);
1823 child = rb_entry(node, struct hist_entry, rb_node);
1824 }
1825
1826 if (node)
1827 percent = hist_entry__get_percent_limit(child);
1828 else
1829 percent = 0;
1830
1831 return node && percent >= limit;
1832}
1833
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001834static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001835 enum hist_filter filter)
1836{
1837 h->filtered &= ~(1 << filter);
Namhyung Kim155e9af2016-02-25 00:13:38 +09001838
1839 if (symbol_conf.report_hierarchy) {
1840 struct hist_entry *parent = h->parent_he;
1841
1842 while (parent) {
1843 he_stat__add_stat(&parent->stat, &h->stat);
1844
1845 parent->filtered &= ~(1 << filter);
1846
1847 if (parent->filtered)
1848 goto next;
1849
1850 /* force fold unfiltered entry for simplicity */
1851 parent->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001852 parent->has_no_entry = false;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001853 parent->row_offset = 0;
1854 parent->nr_rows = 0;
1855next:
1856 parent = parent->parent_he;
1857 }
1858 }
1859
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001860 if (h->filtered)
1861 return;
1862
Namhyung Kim87e90f42014-04-24 16:44:16 +09001863 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001864 h->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001865 h->has_no_entry = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001866 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001867 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001868
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001869 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001870
Namhyung Kim9283ba92014-04-24 16:37:26 +09001871 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001872 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001873}
1874
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001875
1876static bool hists__filter_entry_by_dso(struct hists *hists,
1877 struct hist_entry *he)
1878{
1879 if (hists->dso_filter != NULL &&
1880 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1881 he->filtered |= (1 << HIST_FILTER__DSO);
1882 return true;
1883 }
1884
1885 return false;
1886}
1887
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001888static bool hists__filter_entry_by_thread(struct hists *hists,
1889 struct hist_entry *he)
1890{
1891 if (hists->thread_filter != NULL &&
1892 he->thread != hists->thread_filter) {
1893 he->filtered |= (1 << HIST_FILTER__THREAD);
1894 return true;
1895 }
1896
1897 return false;
1898}
1899
Namhyung Kime94d53e2012-03-16 17:50:51 +09001900static bool hists__filter_entry_by_symbol(struct hists *hists,
1901 struct hist_entry *he)
1902{
1903 if (hists->symbol_filter_str != NULL &&
1904 (!he->ms.sym || strstr(he->ms.sym->name,
1905 hists->symbol_filter_str) == NULL)) {
1906 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1907 return true;
1908 }
1909
1910 return false;
1911}
1912
Kan Liang21394d92015-09-04 10:45:44 -04001913static bool hists__filter_entry_by_socket(struct hists *hists,
1914 struct hist_entry *he)
1915{
1916 if ((hists->socket_filter > -1) &&
1917 (he->socket != hists->socket_filter)) {
1918 he->filtered |= (1 << HIST_FILTER__SOCKET);
1919 return true;
1920 }
1921
1922 return false;
1923}
1924
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001925typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1926
1927static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04001928{
1929 struct rb_node *nd;
1930
1931 hists->stats.nr_non_filtered_samples = 0;
1932
1933 hists__reset_filter_stats(hists);
1934 hists__reset_col_len(hists);
1935
1936 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1937 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1938
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001939 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04001940 continue;
1941
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001942 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04001943 }
1944}
1945
Namhyung Kim70642852016-02-25 00:13:39 +09001946static void resort_filtered_entry(struct rb_root *root, struct hist_entry *he)
1947{
1948 struct rb_node **p = &root->rb_node;
1949 struct rb_node *parent = NULL;
1950 struct hist_entry *iter;
1951 struct rb_root new_root = RB_ROOT;
1952 struct rb_node *nd;
1953
1954 while (*p != NULL) {
1955 parent = *p;
1956 iter = rb_entry(parent, struct hist_entry, rb_node);
1957
1958 if (hist_entry__sort(he, iter) > 0)
1959 p = &(*p)->rb_left;
1960 else
1961 p = &(*p)->rb_right;
1962 }
1963
1964 rb_link_node(&he->rb_node, parent, p);
1965 rb_insert_color(&he->rb_node, root);
1966
1967 if (he->leaf || he->filtered)
1968 return;
1969
1970 nd = rb_first(&he->hroot_out);
1971 while (nd) {
1972 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1973
1974 nd = rb_next(nd);
1975 rb_erase(&h->rb_node, &he->hroot_out);
1976
1977 resort_filtered_entry(&new_root, h);
1978 }
1979
1980 he->hroot_out = new_root;
1981}
1982
Namhyung Kim155e9af2016-02-25 00:13:38 +09001983static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
1984{
1985 struct rb_node *nd;
Namhyung Kim70642852016-02-25 00:13:39 +09001986 struct rb_root new_root = RB_ROOT;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001987
1988 hists->stats.nr_non_filtered_samples = 0;
1989
1990 hists__reset_filter_stats(hists);
1991 hists__reset_col_len(hists);
1992
1993 nd = rb_first(&hists->entries);
1994 while (nd) {
1995 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1996 int ret;
1997
1998 ret = hist_entry__filter(h, type, arg);
1999
2000 /*
2001 * case 1. non-matching type
2002 * zero out the period, set filter marker and move to child
2003 */
2004 if (ret < 0) {
2005 memset(&h->stat, 0, sizeof(h->stat));
2006 h->filtered |= (1 << type);
2007
2008 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2009 }
2010 /*
2011 * case 2. matched type (filter out)
2012 * set filter marker and move to next
2013 */
2014 else if (ret == 1) {
2015 h->filtered |= (1 << type);
2016
2017 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2018 }
2019 /*
2020 * case 3. ok (not filtered)
2021 * add period to hists and parents, erase the filter marker
2022 * and move to next sibling
2023 */
2024 else {
2025 hists__remove_entry_filter(hists, h, type);
2026
2027 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2028 }
2029 }
Namhyung Kim70642852016-02-25 00:13:39 +09002030
Namhyung Kimf7fb5382016-03-09 22:47:02 +09002031 hierarchy_recalc_total_periods(hists);
2032
Namhyung Kim70642852016-02-25 00:13:39 +09002033 /*
2034 * resort output after applying a new filter since filter in a lower
2035 * hierarchy can change periods in a upper hierarchy.
2036 */
2037 nd = rb_first(&hists->entries);
2038 while (nd) {
2039 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2040
2041 nd = rb_next(nd);
2042 rb_erase(&h->rb_node, &hists->entries);
2043
2044 resort_filtered_entry(&new_root, h);
2045 }
2046
2047 hists->entries = new_root;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002048}
2049
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002050void hists__filter_by_thread(struct hists *hists)
2051{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002052 if (symbol_conf.report_hierarchy)
2053 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2054 hists->thread_filter);
2055 else
2056 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2057 hists__filter_entry_by_thread);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002058}
2059
2060void hists__filter_by_dso(struct hists *hists)
2061{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002062 if (symbol_conf.report_hierarchy)
2063 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2064 hists->dso_filter);
2065 else
2066 hists__filter_by_type(hists, HIST_FILTER__DSO,
2067 hists__filter_entry_by_dso);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002068}
2069
2070void hists__filter_by_symbol(struct hists *hists)
2071{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002072 if (symbol_conf.report_hierarchy)
2073 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2074 hists->symbol_filter_str);
2075 else
2076 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2077 hists__filter_entry_by_symbol);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002078}
2079
2080void hists__filter_by_socket(struct hists *hists)
2081{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002082 if (symbol_conf.report_hierarchy)
2083 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2084 &hists->socket_filter);
2085 else
2086 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2087 hists__filter_entry_by_socket);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002088}
2089
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002090void events_stats__inc(struct events_stats *stats, u32 type)
2091{
2092 ++stats->nr_events[0];
2093 ++stats->nr_events[type];
2094}
2095
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002096void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002097{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002098 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002099}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002100
Namhyung Kim1844dbc2014-05-28 14:12:18 +09002101void hists__inc_nr_samples(struct hists *hists, bool filtered)
2102{
2103 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2104 if (!filtered)
2105 hists->stats.nr_non_filtered_samples++;
2106}
2107
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002108static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2109 struct hist_entry *pair)
2110{
Namhyung Kimce74f602012-12-10 17:29:55 +09002111 struct rb_root *root;
2112 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002113 struct rb_node *parent = NULL;
2114 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07002115 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002116
Jiri Olsa52225032016-05-03 13:54:42 +02002117 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002118 root = &hists->entries_collapsed;
2119 else
2120 root = hists->entries_in;
2121
2122 p = &root->rb_node;
2123
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002124 while (*p != NULL) {
2125 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09002126 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002127
Namhyung Kimce74f602012-12-10 17:29:55 +09002128 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002129
2130 if (!cmp)
2131 goto out;
2132
2133 if (cmp < 0)
2134 p = &(*p)->rb_left;
2135 else
2136 p = &(*p)->rb_right;
2137 }
2138
Namhyung Kima0b51af2012-09-11 13:34:27 +09002139 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002140 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03002141 memset(&he->stat, 0, sizeof(he->stat));
2142 he->hists = hists;
Kan Liang09623d72016-04-24 23:28:09 -07002143 if (symbol_conf.cumulate_callchain)
2144 memset(he->stat_acc, 0, sizeof(he->stat));
Namhyung Kimce74f602012-12-10 17:29:55 +09002145 rb_link_node(&he->rb_node_in, parent, p);
2146 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09002147 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01002148 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002149 }
2150out:
2151 return he;
2152}
2153
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002154static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
2155 struct rb_root *root,
2156 struct hist_entry *pair)
2157{
2158 struct rb_node **p;
2159 struct rb_node *parent = NULL;
2160 struct hist_entry *he;
2161 struct perf_hpp_fmt *fmt;
2162
2163 p = &root->rb_node;
2164 while (*p != NULL) {
2165 int64_t cmp = 0;
2166
2167 parent = *p;
2168 he = rb_entry(parent, struct hist_entry, rb_node_in);
2169
2170 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2171 cmp = fmt->collapse(fmt, he, pair);
2172 if (cmp)
2173 break;
2174 }
2175 if (!cmp)
2176 goto out;
2177
2178 if (cmp < 0)
2179 p = &parent->rb_left;
2180 else
2181 p = &parent->rb_right;
2182 }
2183
2184 he = hist_entry__new(pair, true);
2185 if (he) {
2186 rb_link_node(&he->rb_node_in, parent, p);
2187 rb_insert_color(&he->rb_node_in, root);
2188
2189 he->dummy = true;
2190 he->hists = hists;
2191 memset(&he->stat, 0, sizeof(he->stat));
2192 hists__inc_stats(hists, he);
2193 }
2194out:
2195 return he;
2196}
2197
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002198static struct hist_entry *hists__find_entry(struct hists *hists,
2199 struct hist_entry *he)
2200{
Namhyung Kimce74f602012-12-10 17:29:55 +09002201 struct rb_node *n;
2202
Jiri Olsa52225032016-05-03 13:54:42 +02002203 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002204 n = hists->entries_collapsed.rb_node;
2205 else
2206 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002207
2208 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002209 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2210 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002211
2212 if (cmp < 0)
2213 n = n->rb_left;
2214 else if (cmp > 0)
2215 n = n->rb_right;
2216 else
2217 return iter;
2218 }
2219
2220 return NULL;
2221}
2222
Namhyung Kim09034de2016-09-13 16:45:46 +09002223static struct hist_entry *hists__find_hierarchy_entry(struct rb_root *root,
2224 struct hist_entry *he)
2225{
2226 struct rb_node *n = root->rb_node;
2227
2228 while (n) {
2229 struct hist_entry *iter;
2230 struct perf_hpp_fmt *fmt;
2231 int64_t cmp = 0;
2232
2233 iter = rb_entry(n, struct hist_entry, rb_node_in);
2234 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2235 cmp = fmt->collapse(fmt, iter, he);
2236 if (cmp)
2237 break;
2238 }
2239
2240 if (cmp < 0)
2241 n = n->rb_left;
2242 else if (cmp > 0)
2243 n = n->rb_right;
2244 else
2245 return iter;
2246 }
2247
2248 return NULL;
2249}
2250
2251static void hists__match_hierarchy(struct rb_root *leader_root,
2252 struct rb_root *other_root)
2253{
2254 struct rb_node *nd;
2255 struct hist_entry *pos, *pair;
2256
2257 for (nd = rb_first(leader_root); nd; nd = rb_next(nd)) {
2258 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2259 pair = hists__find_hierarchy_entry(other_root, pos);
2260
2261 if (pair) {
2262 hist_entry__add_pair(pair, pos);
2263 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2264 }
2265 }
2266}
2267
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002268/*
2269 * Look for pairs to link to the leader buckets (hist_entries):
2270 */
2271void hists__match(struct hists *leader, struct hists *other)
2272{
Namhyung Kimce74f602012-12-10 17:29:55 +09002273 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002274 struct rb_node *nd;
2275 struct hist_entry *pos, *pair;
2276
Namhyung Kim09034de2016-09-13 16:45:46 +09002277 if (symbol_conf.report_hierarchy) {
2278 /* hierarchy report always collapses entries */
2279 return hists__match_hierarchy(&leader->entries_collapsed,
2280 &other->entries_collapsed);
2281 }
2282
Jiri Olsa52225032016-05-03 13:54:42 +02002283 if (hists__has(leader, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002284 root = &leader->entries_collapsed;
2285 else
2286 root = leader->entries_in;
2287
2288 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2289 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002290 pair = hists__find_entry(other, pos);
2291
2292 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09002293 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002294 }
2295}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002296
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002297static int hists__link_hierarchy(struct hists *leader_hists,
2298 struct hist_entry *parent,
2299 struct rb_root *leader_root,
2300 struct rb_root *other_root)
2301{
2302 struct rb_node *nd;
2303 struct hist_entry *pos, *leader;
2304
2305 for (nd = rb_first(other_root); nd; nd = rb_next(nd)) {
2306 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2307
2308 if (hist_entry__has_pairs(pos)) {
2309 bool found = false;
2310
2311 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2312 if (leader->hists == leader_hists) {
2313 found = true;
2314 break;
2315 }
2316 }
2317 if (!found)
2318 return -1;
2319 } else {
2320 leader = add_dummy_hierarchy_entry(leader_hists,
2321 leader_root, pos);
2322 if (leader == NULL)
2323 return -1;
2324
2325 /* do not point parent in the pos */
2326 leader->parent_he = parent;
2327
2328 hist_entry__add_pair(pos, leader);
2329 }
2330
2331 if (!pos->leaf) {
2332 if (hists__link_hierarchy(leader_hists, leader,
2333 &leader->hroot_in,
2334 &pos->hroot_in) < 0)
2335 return -1;
2336 }
2337 }
2338 return 0;
2339}
2340
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002341/*
2342 * Look for entries in the other hists that are not present in the leader, if
2343 * we find them, just add a dummy entry on the leader hists, with period=0,
2344 * nr_events=0, to serve as the list header.
2345 */
2346int hists__link(struct hists *leader, struct hists *other)
2347{
Namhyung Kimce74f602012-12-10 17:29:55 +09002348 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002349 struct rb_node *nd;
2350 struct hist_entry *pos, *pair;
2351
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002352 if (symbol_conf.report_hierarchy) {
2353 /* hierarchy report always collapses entries */
2354 return hists__link_hierarchy(leader, NULL,
2355 &leader->entries_collapsed,
2356 &other->entries_collapsed);
2357 }
2358
Jiri Olsa52225032016-05-03 13:54:42 +02002359 if (hists__has(other, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002360 root = &other->entries_collapsed;
2361 else
2362 root = other->entries_in;
2363
2364 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2365 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002366
2367 if (!hist_entry__has_pairs(pos)) {
2368 pair = hists__add_dummy_entry(leader, pos);
2369 if (pair == NULL)
2370 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09002371 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002372 }
2373 }
2374
2375 return 0;
2376}
Namhyung Kimf2148332014-01-14 11:52:48 +09002377
Andi Kleen578499982015-07-18 08:24:49 -07002378void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2379 struct perf_sample *sample, bool nonany_branch_mode)
2380{
2381 struct branch_info *bi;
2382
2383 /* If we have branch cycles always annotate them. */
2384 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2385 int i;
2386
2387 bi = sample__resolve_bstack(sample, al);
2388 if (bi) {
2389 struct addr_map_symbol *prev = NULL;
2390
2391 /*
2392 * Ignore errors, still want to process the
2393 * other entries.
2394 *
2395 * For non standard branch modes always
2396 * force no IPC (prev == NULL)
2397 *
2398 * Note that perf stores branches reversed from
2399 * program order!
2400 */
2401 for (i = bs->nr - 1; i >= 0; i--) {
2402 addr_map_symbol__account_cycles(&bi[i].from,
2403 nonany_branch_mode ? NULL : prev,
2404 bi[i].flags.cycles);
2405 prev = &bi[i].to;
2406 }
2407 free(bi);
2408 }
2409 }
2410}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002411
2412size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
2413{
2414 struct perf_evsel *pos;
2415 size_t ret = 0;
2416
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002417 evlist__for_each_entry(evlist, pos) {
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002418 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2419 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2420 }
2421
2422 return ret;
2423}
2424
2425
Namhyung Kimf2148332014-01-14 11:52:48 +09002426u64 hists__total_period(struct hists *hists)
2427{
2428 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2429 hists->stats.total_period;
2430}
Namhyung Kim33db4562014-02-07 12:06:07 +09002431
2432int parse_filter_percentage(const struct option *opt __maybe_unused,
2433 const char *arg, int unset __maybe_unused)
2434{
2435 if (!strcmp(arg, "relative"))
2436 symbol_conf.filter_relative = true;
2437 else if (!strcmp(arg, "absolute"))
2438 symbol_conf.filter_relative = false;
2439 else
2440 return -1;
2441
2442 return 0;
2443}
Namhyung Kim0b93da12014-01-14 12:02:15 +09002444
2445int perf_hist_config(const char *var, const char *value)
2446{
2447 if (!strcmp(var, "hist.percentage"))
2448 return parse_filter_percentage(NULL, value, 0);
2449
2450 return 0;
2451}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002452
Jiri Olsa5b658552016-01-18 10:24:22 +01002453int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002454{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002455 memset(hists, 0, sizeof(*hists));
2456 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
2457 hists->entries_in = &hists->entries_in_array[0];
2458 hists->entries_collapsed = RB_ROOT;
2459 hists->entries = RB_ROOT;
2460 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04002461 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01002462 hists->hpp_list = hpp_list;
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002463 INIT_LIST_HEAD(&hists->hpp_formats);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002464 return 0;
2465}
2466
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002467static void hists__delete_remaining_entries(struct rb_root *root)
2468{
2469 struct rb_node *node;
2470 struct hist_entry *he;
2471
2472 while (!RB_EMPTY_ROOT(root)) {
2473 node = rb_first(root);
2474 rb_erase(node, root);
2475
2476 he = rb_entry(node, struct hist_entry, rb_node_in);
2477 hist_entry__delete(he);
2478 }
2479}
2480
2481static void hists__delete_all_entries(struct hists *hists)
2482{
2483 hists__delete_entries(hists);
2484 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2485 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2486 hists__delete_remaining_entries(&hists->entries_collapsed);
2487}
2488
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002489static void hists_evsel__exit(struct perf_evsel *evsel)
2490{
2491 struct hists *hists = evsel__hists(evsel);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002492 struct perf_hpp_fmt *fmt, *pos;
2493 struct perf_hpp_list_node *node, *tmp;
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002494
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002495 hists__delete_all_entries(hists);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002496
2497 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2498 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2499 list_del(&fmt->list);
2500 free(fmt);
2501 }
2502 list_del(&node->list);
2503 free(node);
2504 }
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002505}
2506
Namhyung Kimfc284be2016-01-07 10:14:10 +01002507static int hists_evsel__init(struct perf_evsel *evsel)
2508{
2509 struct hists *hists = evsel__hists(evsel);
2510
Jiri Olsa5b658552016-01-18 10:24:22 +01002511 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01002512 return 0;
2513}
2514
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002515/*
2516 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2517 * stored in the rbtree...
2518 */
2519
2520int hists__init(void)
2521{
2522 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002523 hists_evsel__init,
2524 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002525 if (err)
2526 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2527
2528 return err;
2529}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01002530
2531void perf_hpp_list__init(struct perf_hpp_list *list)
2532{
2533 INIT_LIST_HEAD(&list->fields);
2534 INIT_LIST_HEAD(&list->sorts);
2535}