blob: 04f3b52a319c6d655adc89e590e5573ddc2ecc15 [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 Meloab81f3fd2011-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 Meloab81f3fd2011-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 Meloab81f3fd2011-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 Meloab81f3fd2011-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 Meloab81f3fd2011-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 Meloab81f3fd2011-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);
420
421 if (!symbol_conf.report_hierarchy)
422 he->leaf = true;
423
424 return 0;
425}
426
Namhyung Kima0b51af2012-09-11 13:34:27 +0900427static struct hist_entry *hist_entry__new(struct hist_entry *template,
428 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300429{
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900430 size_t callchain_size = 0;
431 struct hist_entry *he;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200432 int err = 0;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900433
Namhyung Kim82aa0192014-12-22 13:44:14 +0900434 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900435 callchain_size = sizeof(struct callchain_root);
436
437 he = zalloc(sizeof(*he) + callchain_size);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200438 if (he) {
439 err = hist_entry__init(he, template, sample_self);
440 if (err)
441 zfree(&he);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300442 }
443
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200444 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300445}
446
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300447static u8 symbol__parent_filter(const struct symbol *parent)
448{
449 if (symbol_conf.exclude_other && parent == NULL)
450 return 1 << HIST_FILTER__PARENT;
451 return 0;
452}
453
Namhyung Kim467ef102016-02-16 23:08:19 +0900454static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
455{
456 if (!symbol_conf.use_callchain)
457 return;
458
459 he->hists->callchain_period += period;
460 if (!he->filtered)
461 he->hists->callchain_non_filtered_period += period;
462}
463
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300464static struct hist_entry *hists__findnew_entry(struct hists *hists,
465 struct hist_entry *entry,
466 struct addr_location *al,
467 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300468{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300469 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300470 struct rb_node *parent = NULL;
471 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700472 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900473 u64 period = entry->stat.period;
474 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300475
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300476 p = &hists->entries_in->rb_node;
477
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300478 while (*p != NULL) {
479 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300480 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300481
Namhyung Kim9afcf932012-12-10 17:29:54 +0900482 /*
483 * Make sure that it receives arguments in a same order as
484 * hist_entry__collapse() so that we can use an appropriate
485 * function when searching an entry regardless which sort
486 * keys were used.
487 */
488 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300489
490 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900491 if (sample_self) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900492 he_stat__add_period(&he->stat, period, weight);
Namhyung Kim467ef102016-02-16 23:08:19 +0900493 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900494 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900495 if (symbol_conf.cumulate_callchain)
496 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400497
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900498 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300499 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900500 * and will not be used anymore.
501 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300502 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900503
David Miller63fa4712012-03-27 03:14:18 -0400504 /* If the map of an existing hist_entry has
505 * become out-of-date due to an exec() or
506 * similar, update it. Otherwise we will
507 * mis-adjust symbol addresses when computing
508 * the history counter to increment.
509 */
510 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300511 map__put(he->ms.map);
512 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400513 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300514 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300515 }
516
517 if (cmp < 0)
518 p = &(*p)->rb_left;
519 else
520 p = &(*p)->rb_right;
521 }
522
Namhyung Kima0b51af2012-09-11 13:34:27 +0900523 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300524 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900525 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300526
Namhyung Kim0f584742016-01-28 00:40:49 +0900527 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900528 hist_entry__add_callchain_period(he, period);
529 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900530
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300531 rb_link_node(&he->rb_node_in, parent, p);
532 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300533out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900534 if (sample_self)
535 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900536 if (symbol_conf.cumulate_callchain)
537 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300538 return he;
539}
540
Jiri Olsa0102ef32016-06-14 20:19:21 +0200541struct hist_entry *hists__add_entry(struct hists *hists,
542 struct addr_location *al,
543 struct symbol *sym_parent,
544 struct branch_info *bi,
545 struct mem_info *mi,
546 struct perf_sample *sample,
547 bool sample_self)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100548{
549 struct hist_entry entry = {
550 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900551 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100552 .ms = {
553 .map = al->map,
554 .sym = al->sym,
555 },
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400556 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400557 .cpu = al->cpu,
558 .cpumode = al->cpumode,
559 .ip = al->addr,
560 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900561 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900562 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900563 .period = sample->period,
564 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900565 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100566 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300567 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300568 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900569 .branch_info = bi,
570 .mem_info = mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900571 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900572 .raw_data = sample->raw_data,
573 .raw_size = sample->raw_size,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100574 };
575
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300576 return hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100577}
578
Namhyung Kim69bcb012013-10-30 09:40:34 +0900579static int
580iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
581 struct addr_location *al __maybe_unused)
582{
583 return 0;
584}
585
586static int
587iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
588 struct addr_location *al __maybe_unused)
589{
590 return 0;
591}
592
593static int
594iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
595{
596 struct perf_sample *sample = iter->sample;
597 struct mem_info *mi;
598
599 mi = sample__resolve_mem(sample, al);
600 if (mi == NULL)
601 return -ENOMEM;
602
603 iter->priv = mi;
604 return 0;
605}
606
607static int
608iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
609{
610 u64 cost;
611 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300612 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900613 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900614 struct hist_entry *he;
615
616 if (mi == NULL)
617 return -EINVAL;
618
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900619 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900620 if (!cost)
621 cost = 1;
622
623 /*
624 * must pass period=weight in order to get the correct
625 * sorting from hists__collapse_resort() which is solely
626 * based on periods. We want sorting be done on nr_events * weight
627 * and this is indirectly achieved by passing period=weight here
628 * and the he_stat__add_period() function.
629 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900630 sample->period = cost;
631
Jiri Olsa0102ef32016-06-14 20:19:21 +0200632 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
633 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900634 if (!he)
635 return -ENOMEM;
636
637 iter->he = he;
638 return 0;
639}
640
641static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900642iter_finish_mem_entry(struct hist_entry_iter *iter,
643 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900644{
645 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300646 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900647 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900648 int err = -EINVAL;
649
650 if (he == NULL)
651 goto out;
652
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300653 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900654
655 err = hist_entry__append_callchain(he, iter->sample);
656
657out:
658 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300659 * We don't need to free iter->priv (mem_info) here since the mem info
660 * was either already freed in hists__findnew_entry() or passed to a
661 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900662 */
663 iter->priv = NULL;
664
665 iter->he = NULL;
666 return err;
667}
668
669static int
670iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
671{
672 struct branch_info *bi;
673 struct perf_sample *sample = iter->sample;
674
675 bi = sample__resolve_bstack(sample, al);
676 if (!bi)
677 return -ENOMEM;
678
679 iter->curr = 0;
680 iter->total = sample->branch_stack->nr;
681
682 iter->priv = bi;
683 return 0;
684}
685
686static int
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -0300687iter_add_single_branch_entry(struct hist_entry_iter *iter,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900688 struct addr_location *al __maybe_unused)
689{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900690 /* to avoid calling callback function */
691 iter->he = NULL;
692
Namhyung Kim69bcb012013-10-30 09:40:34 +0900693 return 0;
694}
695
696static int
697iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
698{
699 struct branch_info *bi = iter->priv;
700 int i = iter->curr;
701
702 if (bi == NULL)
703 return 0;
704
705 if (iter->curr >= iter->total)
706 return 0;
707
708 al->map = bi[i].to.map;
709 al->sym = bi[i].to.sym;
710 al->addr = bi[i].to.addr;
711 return 1;
712}
713
714static int
715iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
716{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900717 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900718 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300719 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900720 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900721 struct hist_entry *he = NULL;
722 int i = iter->curr;
723 int err = 0;
724
725 bi = iter->priv;
726
727 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
728 goto out;
729
730 /*
731 * The report shows the percentage of total branches captured
732 * and not events sampled. Thus we use a pseudo period of 1.
733 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900734 sample->period = 1;
735 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
736
Jiri Olsa0102ef32016-06-14 20:19:21 +0200737 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
738 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900739 if (he == NULL)
740 return -ENOMEM;
741
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300742 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900743
744out:
745 iter->he = he;
746 iter->curr++;
747 return err;
748}
749
750static int
751iter_finish_branch_entry(struct hist_entry_iter *iter,
752 struct addr_location *al __maybe_unused)
753{
754 zfree(&iter->priv);
755 iter->he = NULL;
756
757 return iter->curr >= iter->total ? 0 : -1;
758}
759
760static int
761iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
762 struct addr_location *al __maybe_unused)
763{
764 return 0;
765}
766
767static int
768iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
769{
770 struct perf_evsel *evsel = iter->evsel;
771 struct perf_sample *sample = iter->sample;
772 struct hist_entry *he;
773
Jiri Olsa0102ef32016-06-14 20:19:21 +0200774 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
775 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900776 if (he == NULL)
777 return -ENOMEM;
778
779 iter->he = he;
780 return 0;
781}
782
783static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900784iter_finish_normal_entry(struct hist_entry_iter *iter,
785 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900786{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900787 struct hist_entry *he = iter->he;
788 struct perf_evsel *evsel = iter->evsel;
789 struct perf_sample *sample = iter->sample;
790
791 if (he == NULL)
792 return 0;
793
794 iter->he = NULL;
795
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300796 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900797
798 return hist_entry__append_callchain(he, sample);
799}
800
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900801static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300802iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900803 struct addr_location *al __maybe_unused)
804{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900805 struct hist_entry **he_cache;
806
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900807 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900808
809 /*
810 * This is for detecting cycles or recursions so that they're
811 * cumulated only one time to prevent entries more than 100%
812 * overhead.
813 */
Adrian Hunter96b40f32015-09-25 16:15:47 +0300814 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900815 if (he_cache == NULL)
816 return -ENOMEM;
817
818 iter->priv = he_cache;
819 iter->curr = 0;
820
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900821 return 0;
822}
823
824static int
825iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
826 struct addr_location *al)
827{
828 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300829 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900830 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900831 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900832 struct hist_entry *he;
833 int err = 0;
834
Jiri Olsa0102ef32016-06-14 20:19:21 +0200835 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
836 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900837 if (he == NULL)
838 return -ENOMEM;
839
840 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900841 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900842
Namhyung Kim82aa0192014-12-22 13:44:14 +0900843 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900844
845 /*
846 * We need to re-initialize the cursor since callchain_append()
847 * advanced the cursor to the end.
848 */
849 callchain_cursor_commit(&callchain_cursor);
850
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300851 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900852
853 return err;
854}
855
856static int
857iter_next_cumulative_entry(struct hist_entry_iter *iter,
858 struct addr_location *al)
859{
860 struct callchain_cursor_node *node;
861
862 node = callchain_cursor_current(&callchain_cursor);
863 if (node == NULL)
864 return 0;
865
Namhyung Kimc7405d82013-10-31 13:58:30 +0900866 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900867}
868
869static int
870iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
871 struct addr_location *al)
872{
873 struct perf_evsel *evsel = iter->evsel;
874 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900875 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900876 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900877 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -0300878 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900879 .cpu = al->cpu,
880 .thread = al->thread,
881 .comm = thread__comm(al->thread),
882 .ip = al->addr,
883 .ms = {
884 .map = al->map,
885 .sym = al->sym,
886 },
887 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +0900888 .raw_data = sample->raw_data,
889 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900890 };
891 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900892 struct callchain_cursor cursor;
893
894 callchain_cursor_snapshot(&cursor, &callchain_cursor);
895
896 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900897
898 /*
899 * Check if there's duplicate entries in the callchain.
900 * It's possible that it has cycles or recursive calls.
901 */
902 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900903 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
904 /* to avoid calling callback function */
905 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900906 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900907 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900908 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900909
Jiri Olsa0102ef32016-06-14 20:19:21 +0200910 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
911 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900912 if (he == NULL)
913 return -ENOMEM;
914
915 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900916 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900917
Namhyung Kim82aa0192014-12-22 13:44:14 +0900918 if (symbol_conf.use_callchain)
919 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900920 return 0;
921}
922
923static int
924iter_finish_cumulative_entry(struct hist_entry_iter *iter,
925 struct addr_location *al __maybe_unused)
926{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900927 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900928 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900929
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900930 return 0;
931}
932
Namhyung Kim69bcb012013-10-30 09:40:34 +0900933const struct hist_iter_ops hist_iter_mem = {
934 .prepare_entry = iter_prepare_mem_entry,
935 .add_single_entry = iter_add_single_mem_entry,
936 .next_entry = iter_next_nop_entry,
937 .add_next_entry = iter_add_next_nop_entry,
938 .finish_entry = iter_finish_mem_entry,
939};
940
941const struct hist_iter_ops hist_iter_branch = {
942 .prepare_entry = iter_prepare_branch_entry,
943 .add_single_entry = iter_add_single_branch_entry,
944 .next_entry = iter_next_branch_entry,
945 .add_next_entry = iter_add_next_branch_entry,
946 .finish_entry = iter_finish_branch_entry,
947};
948
949const struct hist_iter_ops hist_iter_normal = {
950 .prepare_entry = iter_prepare_normal_entry,
951 .add_single_entry = iter_add_single_normal_entry,
952 .next_entry = iter_next_nop_entry,
953 .add_next_entry = iter_add_next_nop_entry,
954 .finish_entry = iter_finish_normal_entry,
955};
956
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900957const struct hist_iter_ops hist_iter_cumulative = {
958 .prepare_entry = iter_prepare_cumulative_entry,
959 .add_single_entry = iter_add_single_cumulative_entry,
960 .next_entry = iter_next_cumulative_entry,
961 .add_next_entry = iter_add_next_cumulative_entry,
962 .finish_entry = iter_finish_cumulative_entry,
963};
964
Namhyung Kim69bcb012013-10-30 09:40:34 +0900965int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900966 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900967{
968 int err, err2;
969
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300970 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
Namhyung Kim063bd932015-05-19 17:04:10 +0900971 iter->evsel, al, max_stack_depth);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900972 if (err)
973 return err;
974
Adrian Hunter96b40f32015-09-25 16:15:47 +0300975 iter->max_stack = max_stack_depth;
976
Namhyung Kim69bcb012013-10-30 09:40:34 +0900977 err = iter->ops->prepare_entry(iter, al);
978 if (err)
979 goto out;
980
981 err = iter->ops->add_single_entry(iter, al);
982 if (err)
983 goto out;
984
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900985 if (iter->he && iter->add_entry_cb) {
986 err = iter->add_entry_cb(iter, al, true, arg);
987 if (err)
988 goto out;
989 }
990
Namhyung Kim69bcb012013-10-30 09:40:34 +0900991 while (iter->ops->next_entry(iter, al)) {
992 err = iter->ops->add_next_entry(iter, al);
993 if (err)
994 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900995
996 if (iter->he && iter->add_entry_cb) {
997 err = iter->add_entry_cb(iter, al, false, arg);
998 if (err)
999 goto out;
1000 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001001 }
1002
1003out:
1004 err2 = iter->ops->finish_entry(iter, al);
1005 if (!err)
1006 err = err2;
1007
1008 return err;
1009}
1010
John Kacur3d1d07e2009-09-28 15:32:55 +02001011int64_t
1012hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1013{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001014 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001015 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001016 int64_t cmp = 0;
1017
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001018 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001019 if (perf_hpp__is_dynamic_entry(fmt) &&
1020 !perf_hpp__defined_dynamic_entry(fmt, hists))
1021 continue;
1022
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001023 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001024 if (cmp)
1025 break;
1026 }
1027
1028 return cmp;
1029}
1030
1031int64_t
1032hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1033{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001034 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001035 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001036 int64_t cmp = 0;
1037
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001038 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001039 if (perf_hpp__is_dynamic_entry(fmt) &&
1040 !perf_hpp__defined_dynamic_entry(fmt, hists))
1041 continue;
1042
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001043 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001044 if (cmp)
1045 break;
1046 }
1047
1048 return cmp;
1049}
1050
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001051void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001052{
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001053 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001054 map__zput(he->ms.map);
1055
1056 if (he->branch_info) {
1057 map__zput(he->branch_info->from.map);
1058 map__zput(he->branch_info->to.map);
Andi Kleen508be0d2016-05-20 13:15:08 -07001059 free_srcline(he->branch_info->srcline_from);
1060 free_srcline(he->branch_info->srcline_to);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001061 zfree(&he->branch_info);
1062 }
1063
1064 if (he->mem_info) {
1065 map__zput(he->mem_info->iaddr.map);
1066 map__zput(he->mem_info->daddr.map);
1067 zfree(&he->mem_info);
1068 }
1069
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001070 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +09001071 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -07001072 if (he->srcfile && he->srcfile[0])
1073 free(he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001074 free_callchain(he->callchain);
Namhyung Kim60517d22015-12-23 02:07:03 +09001075 free(he->trace_output);
Namhyung Kim72392832015-12-24 11:16:17 +09001076 free(he->raw_data);
John Kacur3d1d07e2009-09-28 15:32:55 +02001077 free(he);
1078}
1079
1080/*
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001081 * If this is not the last column, then we need to pad it according to the
1082 * pre-calculated max lenght for this column, otherwise don't bother adding
1083 * spaces because that would break viewing this with, for instance, 'less',
1084 * that would show tons of trailing spaces when a long C++ demangled method
1085 * names is sampled.
1086*/
1087int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1088 struct perf_hpp_fmt *fmt, int printed)
1089{
1090 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
Jiri Olsada1b0402016-06-14 20:19:20 +02001091 const int width = fmt->width(fmt, hpp, he->hists);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001092 if (printed < width) {
1093 advance_hpp(hpp, printed);
1094 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1095 }
1096 }
1097
1098 return printed;
1099}
1100
1101/*
John Kacur3d1d07e2009-09-28 15:32:55 +02001102 * collapse the histogram
1103 */
1104
Namhyung Kimaef810e2016-02-25 00:13:34 +09001105static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
Namhyung Kimaec13a72016-03-09 22:46:58 +09001106static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1107 enum hist_filter type);
1108
1109typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1110
1111static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1112{
1113 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1114}
1115
1116static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1117 enum hist_filter type,
1118 fmt_chk_fn check)
1119{
1120 struct perf_hpp_fmt *fmt;
1121 bool type_match = false;
1122 struct hist_entry *parent = he->parent_he;
1123
1124 switch (type) {
1125 case HIST_FILTER__THREAD:
1126 if (symbol_conf.comm_list == NULL &&
1127 symbol_conf.pid_list == NULL &&
1128 symbol_conf.tid_list == NULL)
1129 return;
1130 break;
1131 case HIST_FILTER__DSO:
1132 if (symbol_conf.dso_list == NULL)
1133 return;
1134 break;
1135 case HIST_FILTER__SYMBOL:
1136 if (symbol_conf.sym_list == NULL)
1137 return;
1138 break;
1139 case HIST_FILTER__PARENT:
1140 case HIST_FILTER__GUEST:
1141 case HIST_FILTER__HOST:
1142 case HIST_FILTER__SOCKET:
1143 default:
1144 return;
1145 }
1146
1147 /* if it's filtered by own fmt, it has to have filter bits */
1148 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1149 if (check(fmt)) {
1150 type_match = true;
1151 break;
1152 }
1153 }
1154
1155 if (type_match) {
1156 /*
1157 * If the filter is for current level entry, propagate
1158 * filter marker to parents. The marker bit was
1159 * already set by default so it only needs to clear
1160 * non-filtered entries.
1161 */
1162 if (!(he->filtered & (1 << type))) {
1163 while (parent) {
1164 parent->filtered &= ~(1 << type);
1165 parent = parent->parent_he;
1166 }
1167 }
1168 } else {
1169 /*
1170 * If current entry doesn't have matching formats, set
1171 * filter marker for upper level entries. it will be
1172 * cleared if its lower level entries is not filtered.
1173 *
1174 * For lower-level entries, it inherits parent's
1175 * filter bit so that lower level entries of a
1176 * non-filtered entry won't set the filter marker.
1177 */
1178 if (parent == NULL)
1179 he->filtered |= (1 << type);
1180 else
1181 he->filtered |= (parent->filtered & (1 << type));
1182 }
1183}
1184
1185static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1186{
1187 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1188 check_thread_entry);
1189
1190 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1191 perf_hpp__is_dso_entry);
1192
1193 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1194 perf_hpp__is_sym_entry);
1195
1196 hists__apply_filters(he->hists, he);
1197}
Namhyung Kimaef810e2016-02-25 00:13:34 +09001198
1199static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1200 struct rb_root *root,
1201 struct hist_entry *he,
Namhyung Kimaec13a72016-03-09 22:46:58 +09001202 struct hist_entry *parent_he,
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001203 struct perf_hpp_list *hpp_list)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001204{
1205 struct rb_node **p = &root->rb_node;
1206 struct rb_node *parent = NULL;
1207 struct hist_entry *iter, *new;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001208 struct perf_hpp_fmt *fmt;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001209 int64_t cmp;
1210
1211 while (*p != NULL) {
1212 parent = *p;
1213 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1214
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001215 cmp = 0;
1216 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1217 cmp = fmt->collapse(fmt, iter, he);
1218 if (cmp)
1219 break;
1220 }
1221
Namhyung Kimaef810e2016-02-25 00:13:34 +09001222 if (!cmp) {
1223 he_stat__add_stat(&iter->stat, &he->stat);
1224 return iter;
1225 }
1226
1227 if (cmp < 0)
1228 p = &parent->rb_left;
1229 else
1230 p = &parent->rb_right;
1231 }
1232
1233 new = hist_entry__new(he, true);
1234 if (new == NULL)
1235 return NULL;
1236
Namhyung Kimaef810e2016-02-25 00:13:34 +09001237 hists->nr_entries++;
1238
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001239 /* save related format list for output */
1240 new->hpp_list = hpp_list;
Namhyung Kimaec13a72016-03-09 22:46:58 +09001241 new->parent_he = parent_he;
1242
1243 hist_entry__apply_hierarchy_filters(new);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001244
1245 /* some fields are now passed to 'new' */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001246 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1247 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1248 he->trace_output = NULL;
1249 else
1250 new->trace_output = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001251
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001252 if (perf_hpp__is_srcline_entry(fmt))
1253 he->srcline = NULL;
1254 else
1255 new->srcline = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001256
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001257 if (perf_hpp__is_srcfile_entry(fmt))
1258 he->srcfile = NULL;
1259 else
1260 new->srcfile = NULL;
1261 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001262
1263 rb_link_node(&new->rb_node_in, parent, p);
1264 rb_insert_color(&new->rb_node_in, root);
1265 return new;
1266}
1267
1268static int hists__hierarchy_insert_entry(struct hists *hists,
1269 struct rb_root *root,
1270 struct hist_entry *he)
1271{
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001272 struct perf_hpp_list_node *node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001273 struct hist_entry *new_he = NULL;
1274 struct hist_entry *parent = NULL;
1275 int depth = 0;
1276 int ret = 0;
1277
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001278 list_for_each_entry(node, &hists->hpp_formats, list) {
1279 /* skip period (overhead) and elided columns */
1280 if (node->level == 0 || node->skip)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001281 continue;
1282
1283 /* insert copy of 'he' for each fmt into the hierarchy */
Namhyung Kimaec13a72016-03-09 22:46:58 +09001284 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001285 if (new_he == NULL) {
1286 ret = -1;
1287 break;
1288 }
1289
1290 root = &new_he->hroot_in;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001291 new_he->depth = depth++;
1292 parent = new_he;
1293 }
1294
1295 if (new_he) {
1296 new_he->leaf = true;
1297
1298 if (symbol_conf.use_callchain) {
1299 callchain_cursor_reset(&callchain_cursor);
1300 if (callchain_merge(&callchain_cursor,
1301 new_he->callchain,
1302 he->callchain) < 0)
1303 ret = -1;
1304 }
1305 }
1306
1307 /* 'he' is no longer used */
1308 hist_entry__delete(he);
1309
1310 /* return 0 (or -1) since it already applied filters */
1311 return ret;
1312}
1313
Jiri Olsa592dac62016-03-24 13:52:17 +01001314static int hists__collapse_insert_entry(struct hists *hists,
1315 struct rb_root *root,
1316 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001317{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001318 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001319 struct rb_node *parent = NULL;
1320 struct hist_entry *iter;
1321 int64_t cmp;
1322
Namhyung Kimaef810e2016-02-25 00:13:34 +09001323 if (symbol_conf.report_hierarchy)
1324 return hists__hierarchy_insert_entry(hists, root, he);
1325
John Kacur3d1d07e2009-09-28 15:32:55 +02001326 while (*p != NULL) {
1327 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001328 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001329
1330 cmp = hist_entry__collapse(iter, he);
1331
1332 if (!cmp) {
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001333 int ret = 0;
1334
Namhyung Kim139c0812012-10-04 21:49:43 +09001335 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001336 if (symbol_conf.cumulate_callchain)
1337 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001338
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001339 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001340 callchain_cursor_reset(&callchain_cursor);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001341 if (callchain_merge(&callchain_cursor,
1342 iter->callchain,
1343 he->callchain) < 0)
1344 ret = -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001345 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001346 hist_entry__delete(he);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001347 return ret;
John Kacur3d1d07e2009-09-28 15:32:55 +02001348 }
1349
1350 if (cmp < 0)
1351 p = &(*p)->rb_left;
1352 else
1353 p = &(*p)->rb_right;
1354 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001355 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001356
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001357 rb_link_node(&he->rb_node_in, parent, p);
1358 rb_insert_color(&he->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001359 return 1;
John Kacur3d1d07e2009-09-28 15:32:55 +02001360}
1361
Namhyung Kimfc284be2016-01-07 10:14:10 +01001362struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001363{
1364 struct rb_root *root;
1365
1366 pthread_mutex_lock(&hists->lock);
1367
1368 root = hists->entries_in;
1369 if (++hists->entries_in > &hists->entries_in_array[1])
1370 hists->entries_in = &hists->entries_in_array[0];
1371
1372 pthread_mutex_unlock(&hists->lock);
1373
1374 return root;
1375}
1376
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001377static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1378{
1379 hists__filter_entry_by_dso(hists, he);
1380 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001381 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001382 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001383}
1384
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001385int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001386{
1387 struct rb_root *root;
1388 struct rb_node *next;
1389 struct hist_entry *n;
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001390 int ret;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001391
Jiri Olsa52225032016-05-03 13:54:42 +02001392 if (!hists__has(hists, need_collapse))
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001393 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001394
Namhyung Kim740b97f2014-12-22 13:44:10 +09001395 hists->nr_entries = 0;
1396
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001397 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001398
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001399 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001400
1401 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001402 if (session_done())
1403 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001404 n = rb_entry(next, struct hist_entry, rb_node_in);
1405 next = rb_next(&n->rb_node_in);
1406
1407 rb_erase(&n->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001408 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1409 if (ret < 0)
1410 return -1;
1411
1412 if (ret) {
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001413 /*
1414 * If it wasn't combined with one of the entries already
1415 * collapsed, we need to apply the filters that may have
1416 * been set by, say, the hist_browser.
1417 */
1418 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001419 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001420 if (prog)
1421 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001422 }
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001423 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001424}
1425
Namhyung Kim043ca3892014-03-03 14:18:00 +09001426static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001427{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001428 struct hists *hists = a->hists;
Namhyung Kim043ca3892014-03-03 14:18:00 +09001429 struct perf_hpp_fmt *fmt;
1430 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001431
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001432 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001433 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001434 continue;
1435
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001436 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001437 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001438 break;
1439 }
1440
Namhyung Kim043ca3892014-03-03 14:18:00 +09001441 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001442}
1443
Namhyung Kim9283ba92014-04-24 16:37:26 +09001444static void hists__reset_filter_stats(struct hists *hists)
1445{
1446 hists->nr_non_filtered_entries = 0;
1447 hists->stats.total_non_filtered_period = 0;
1448}
1449
1450void hists__reset_stats(struct hists *hists)
1451{
1452 hists->nr_entries = 0;
1453 hists->stats.total_period = 0;
1454
1455 hists__reset_filter_stats(hists);
1456}
1457
1458static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1459{
1460 hists->nr_non_filtered_entries++;
1461 hists->stats.total_non_filtered_period += h->stat.period;
1462}
1463
1464void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1465{
1466 if (!h->filtered)
1467 hists__inc_filter_stats(hists, h);
1468
1469 hists->nr_entries++;
1470 hists->stats.total_period += h->stat.period;
1471}
1472
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001473static void hierarchy_recalc_total_periods(struct hists *hists)
1474{
1475 struct rb_node *node;
1476 struct hist_entry *he;
1477
1478 node = rb_first(&hists->entries);
1479
1480 hists->stats.total_period = 0;
1481 hists->stats.total_non_filtered_period = 0;
1482
1483 /*
1484 * recalculate total period using top-level entries only
1485 * since lower level entries only see non-filtered entries
1486 * but upper level entries have sum of both entries.
1487 */
1488 while (node) {
1489 he = rb_entry(node, struct hist_entry, rb_node);
1490 node = rb_next(node);
1491
1492 hists->stats.total_period += he->stat.period;
1493 if (!he->filtered)
1494 hists->stats.total_non_filtered_period += he->stat.period;
1495 }
1496}
1497
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001498static void hierarchy_insert_output_entry(struct rb_root *root,
1499 struct hist_entry *he)
1500{
1501 struct rb_node **p = &root->rb_node;
1502 struct rb_node *parent = NULL;
1503 struct hist_entry *iter;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001504 struct perf_hpp_fmt *fmt;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001505
1506 while (*p != NULL) {
1507 parent = *p;
1508 iter = rb_entry(parent, struct hist_entry, rb_node);
1509
1510 if (hist_entry__sort(he, iter) > 0)
1511 p = &parent->rb_left;
1512 else
1513 p = &parent->rb_right;
1514 }
1515
1516 rb_link_node(&he->rb_node, parent, p);
1517 rb_insert_color(&he->rb_node, root);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001518
1519 /* update column width of dynamic entry */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001520 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1521 if (perf_hpp__is_dynamic_entry(fmt))
1522 fmt->sort(fmt, he, NULL);
1523 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001524}
1525
1526static void hists__hierarchy_output_resort(struct hists *hists,
1527 struct ui_progress *prog,
1528 struct rb_root *root_in,
1529 struct rb_root *root_out,
1530 u64 min_callchain_hits,
1531 bool use_callchain)
1532{
1533 struct rb_node *node;
1534 struct hist_entry *he;
1535
1536 *root_out = RB_ROOT;
1537 node = rb_first(root_in);
1538
1539 while (node) {
1540 he = rb_entry(node, struct hist_entry, rb_node_in);
1541 node = rb_next(node);
1542
1543 hierarchy_insert_output_entry(root_out, he);
1544
1545 if (prog)
1546 ui_progress__update(prog, 1);
1547
1548 if (!he->leaf) {
1549 hists__hierarchy_output_resort(hists, prog,
1550 &he->hroot_in,
1551 &he->hroot_out,
1552 min_callchain_hits,
1553 use_callchain);
1554 hists->nr_entries++;
1555 if (!he->filtered) {
1556 hists->nr_non_filtered_entries++;
1557 hists__calc_col_len(hists, he);
1558 }
1559
1560 continue;
1561 }
1562
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001563 if (!use_callchain)
1564 continue;
1565
1566 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1567 u64 total = he->stat.period;
1568
1569 if (symbol_conf.cumulate_callchain)
1570 total = he->stat_acc->period;
1571
1572 min_callchain_hits = total * (callchain_param.min_percent / 100);
1573 }
1574
1575 callchain_param.sort(&he->sorted_chain, he->callchain,
1576 min_callchain_hits, &callchain_param);
1577 }
1578}
1579
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001580static void __hists__insert_output_entry(struct rb_root *entries,
1581 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001582 u64 min_callchain_hits,
1583 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001584{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001585 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001586 struct rb_node *parent = NULL;
1587 struct hist_entry *iter;
Namhyung Kimabab5e72016-02-27 03:52:47 +09001588 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001589
Namhyung Kim744070e2016-01-28 00:40:48 +09001590 if (use_callchain) {
1591 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1592 u64 total = he->stat.period;
1593
1594 if (symbol_conf.cumulate_callchain)
1595 total = he->stat_acc->period;
1596
1597 min_callchain_hits = total * (callchain_param.min_percent / 100);
1598 }
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001599 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001600 min_callchain_hits, &callchain_param);
Namhyung Kim744070e2016-01-28 00:40:48 +09001601 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001602
1603 while (*p != NULL) {
1604 parent = *p;
1605 iter = rb_entry(parent, struct hist_entry, rb_node);
1606
Namhyung Kim043ca3892014-03-03 14:18:00 +09001607 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001608 p = &(*p)->rb_left;
1609 else
1610 p = &(*p)->rb_right;
1611 }
1612
1613 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001614 rb_insert_color(&he->rb_node, entries);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001615
1616 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1617 if (perf_hpp__is_dynamic_entry(fmt) &&
1618 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1619 fmt->sort(fmt, he, NULL); /* update column width */
1620 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001621}
1622
Jiri Olsa01441af2016-01-18 10:23:59 +01001623static void output_resort(struct hists *hists, struct ui_progress *prog,
1624 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001625{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001626 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001627 struct rb_node *next;
1628 struct hist_entry *n;
Namhyung Kim467ef102016-02-16 23:08:19 +09001629 u64 callchain_total;
John Kacur3d1d07e2009-09-28 15:32:55 +02001630 u64 min_callchain_hits;
1631
Namhyung Kim467ef102016-02-16 23:08:19 +09001632 callchain_total = hists->callchain_period;
1633 if (symbol_conf.filter_relative)
1634 callchain_total = hists->callchain_non_filtered_period;
1635
1636 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001637
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001638 hists__reset_stats(hists);
1639 hists__reset_col_len(hists);
1640
1641 if (symbol_conf.report_hierarchy) {
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001642 hists__hierarchy_output_resort(hists, prog,
1643 &hists->entries_collapsed,
1644 &hists->entries,
1645 min_callchain_hits,
1646 use_callchain);
1647 hierarchy_recalc_total_periods(hists);
1648 return;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001649 }
1650
Jiri Olsa52225032016-05-03 13:54:42 +02001651 if (hists__has(hists, need_collapse))
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001652 root = &hists->entries_collapsed;
1653 else
1654 root = hists->entries_in;
1655
1656 next = rb_first(root);
1657 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001658
1659 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001660 n = rb_entry(next, struct hist_entry, rb_node_in);
1661 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001662
Kan Liangf9db0d02015-08-11 06:30:48 -04001663 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001664 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001665
1666 if (!n->filtered)
1667 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001668
1669 if (prog)
1670 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001671 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001672}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001673
Jiri Olsa452ce032016-01-18 10:24:00 +01001674void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
Jiri Olsa01441af2016-01-18 10:23:59 +01001675{
Jiri Olsa01441af2016-01-18 10:23:59 +01001676 bool use_callchain;
1677
1678 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1679 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1680 else
1681 use_callchain = symbol_conf.use_callchain;
1682
Jiri Olsa452ce032016-01-18 10:24:00 +01001683 output_resort(evsel__hists(evsel), prog, use_callchain);
1684}
1685
1686void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1687{
1688 output_resort(hists, prog, symbol_conf.use_callchain);
Jiri Olsa01441af2016-01-18 10:23:59 +01001689}
1690
Namhyung Kim8c018722016-02-25 00:13:36 +09001691static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1692{
1693 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1694 return false;
1695
1696 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1697 return true;
1698
1699 return false;
1700}
1701
1702struct rb_node *rb_hierarchy_last(struct rb_node *node)
1703{
1704 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1705
1706 while (can_goto_child(he, HMD_NORMAL)) {
1707 node = rb_last(&he->hroot_out);
1708 he = rb_entry(node, struct hist_entry, rb_node);
1709 }
1710 return node;
1711}
1712
1713struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1714{
1715 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1716
1717 if (can_goto_child(he, hmd))
1718 node = rb_first(&he->hroot_out);
1719 else
1720 node = rb_next(node);
1721
1722 while (node == NULL) {
1723 he = he->parent_he;
1724 if (he == NULL)
1725 break;
1726
1727 node = rb_next(&he->rb_node);
1728 }
1729 return node;
1730}
1731
1732struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1733{
1734 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1735
1736 node = rb_prev(node);
1737 if (node)
1738 return rb_hierarchy_last(node);
1739
1740 he = he->parent_he;
1741 if (he == NULL)
1742 return NULL;
1743
1744 return &he->rb_node;
1745}
1746
Namhyung Kima7b58952016-02-26 21:13:16 +09001747bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1748{
1749 struct rb_node *node;
1750 struct hist_entry *child;
1751 float percent;
1752
1753 if (he->leaf)
1754 return false;
1755
1756 node = rb_first(&he->hroot_out);
1757 child = rb_entry(node, struct hist_entry, rb_node);
1758
1759 while (node && child->filtered) {
1760 node = rb_next(node);
1761 child = rb_entry(node, struct hist_entry, rb_node);
1762 }
1763
1764 if (node)
1765 percent = hist_entry__get_percent_limit(child);
1766 else
1767 percent = 0;
1768
1769 return node && percent >= limit;
1770}
1771
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001772static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001773 enum hist_filter filter)
1774{
1775 h->filtered &= ~(1 << filter);
Namhyung Kim155e9af2016-02-25 00:13:38 +09001776
1777 if (symbol_conf.report_hierarchy) {
1778 struct hist_entry *parent = h->parent_he;
1779
1780 while (parent) {
1781 he_stat__add_stat(&parent->stat, &h->stat);
1782
1783 parent->filtered &= ~(1 << filter);
1784
1785 if (parent->filtered)
1786 goto next;
1787
1788 /* force fold unfiltered entry for simplicity */
1789 parent->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001790 parent->has_no_entry = false;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001791 parent->row_offset = 0;
1792 parent->nr_rows = 0;
1793next:
1794 parent = parent->parent_he;
1795 }
1796 }
1797
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001798 if (h->filtered)
1799 return;
1800
Namhyung Kim87e90f42014-04-24 16:44:16 +09001801 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001802 h->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001803 h->has_no_entry = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001804 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001805 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001806
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001807 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001808
Namhyung Kim9283ba92014-04-24 16:37:26 +09001809 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001810 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001811}
1812
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001813
1814static bool hists__filter_entry_by_dso(struct hists *hists,
1815 struct hist_entry *he)
1816{
1817 if (hists->dso_filter != NULL &&
1818 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1819 he->filtered |= (1 << HIST_FILTER__DSO);
1820 return true;
1821 }
1822
1823 return false;
1824}
1825
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001826static bool hists__filter_entry_by_thread(struct hists *hists,
1827 struct hist_entry *he)
1828{
1829 if (hists->thread_filter != NULL &&
1830 he->thread != hists->thread_filter) {
1831 he->filtered |= (1 << HIST_FILTER__THREAD);
1832 return true;
1833 }
1834
1835 return false;
1836}
1837
Namhyung Kime94d53e2012-03-16 17:50:51 +09001838static bool hists__filter_entry_by_symbol(struct hists *hists,
1839 struct hist_entry *he)
1840{
1841 if (hists->symbol_filter_str != NULL &&
1842 (!he->ms.sym || strstr(he->ms.sym->name,
1843 hists->symbol_filter_str) == NULL)) {
1844 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1845 return true;
1846 }
1847
1848 return false;
1849}
1850
Kan Liang21394d92015-09-04 10:45:44 -04001851static bool hists__filter_entry_by_socket(struct hists *hists,
1852 struct hist_entry *he)
1853{
1854 if ((hists->socket_filter > -1) &&
1855 (he->socket != hists->socket_filter)) {
1856 he->filtered |= (1 << HIST_FILTER__SOCKET);
1857 return true;
1858 }
1859
1860 return false;
1861}
1862
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001863typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1864
1865static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04001866{
1867 struct rb_node *nd;
1868
1869 hists->stats.nr_non_filtered_samples = 0;
1870
1871 hists__reset_filter_stats(hists);
1872 hists__reset_col_len(hists);
1873
1874 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1875 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1876
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001877 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04001878 continue;
1879
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001880 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04001881 }
1882}
1883
Namhyung Kim70642852016-02-25 00:13:39 +09001884static void resort_filtered_entry(struct rb_root *root, struct hist_entry *he)
1885{
1886 struct rb_node **p = &root->rb_node;
1887 struct rb_node *parent = NULL;
1888 struct hist_entry *iter;
1889 struct rb_root new_root = RB_ROOT;
1890 struct rb_node *nd;
1891
1892 while (*p != NULL) {
1893 parent = *p;
1894 iter = rb_entry(parent, struct hist_entry, rb_node);
1895
1896 if (hist_entry__sort(he, iter) > 0)
1897 p = &(*p)->rb_left;
1898 else
1899 p = &(*p)->rb_right;
1900 }
1901
1902 rb_link_node(&he->rb_node, parent, p);
1903 rb_insert_color(&he->rb_node, root);
1904
1905 if (he->leaf || he->filtered)
1906 return;
1907
1908 nd = rb_first(&he->hroot_out);
1909 while (nd) {
1910 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1911
1912 nd = rb_next(nd);
1913 rb_erase(&h->rb_node, &he->hroot_out);
1914
1915 resort_filtered_entry(&new_root, h);
1916 }
1917
1918 he->hroot_out = new_root;
1919}
1920
Namhyung Kim155e9af2016-02-25 00:13:38 +09001921static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
1922{
1923 struct rb_node *nd;
Namhyung Kim70642852016-02-25 00:13:39 +09001924 struct rb_root new_root = RB_ROOT;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001925
1926 hists->stats.nr_non_filtered_samples = 0;
1927
1928 hists__reset_filter_stats(hists);
1929 hists__reset_col_len(hists);
1930
1931 nd = rb_first(&hists->entries);
1932 while (nd) {
1933 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1934 int ret;
1935
1936 ret = hist_entry__filter(h, type, arg);
1937
1938 /*
1939 * case 1. non-matching type
1940 * zero out the period, set filter marker and move to child
1941 */
1942 if (ret < 0) {
1943 memset(&h->stat, 0, sizeof(h->stat));
1944 h->filtered |= (1 << type);
1945
1946 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
1947 }
1948 /*
1949 * case 2. matched type (filter out)
1950 * set filter marker and move to next
1951 */
1952 else if (ret == 1) {
1953 h->filtered |= (1 << type);
1954
1955 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
1956 }
1957 /*
1958 * case 3. ok (not filtered)
1959 * add period to hists and parents, erase the filter marker
1960 * and move to next sibling
1961 */
1962 else {
1963 hists__remove_entry_filter(hists, h, type);
1964
1965 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
1966 }
1967 }
Namhyung Kim70642852016-02-25 00:13:39 +09001968
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001969 hierarchy_recalc_total_periods(hists);
1970
Namhyung Kim70642852016-02-25 00:13:39 +09001971 /*
1972 * resort output after applying a new filter since filter in a lower
1973 * hierarchy can change periods in a upper hierarchy.
1974 */
1975 nd = rb_first(&hists->entries);
1976 while (nd) {
1977 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1978
1979 nd = rb_next(nd);
1980 rb_erase(&h->rb_node, &hists->entries);
1981
1982 resort_filtered_entry(&new_root, h);
1983 }
1984
1985 hists->entries = new_root;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001986}
1987
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001988void hists__filter_by_thread(struct hists *hists)
1989{
Namhyung Kim155e9af2016-02-25 00:13:38 +09001990 if (symbol_conf.report_hierarchy)
1991 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
1992 hists->thread_filter);
1993 else
1994 hists__filter_by_type(hists, HIST_FILTER__THREAD,
1995 hists__filter_entry_by_thread);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001996}
1997
1998void hists__filter_by_dso(struct hists *hists)
1999{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002000 if (symbol_conf.report_hierarchy)
2001 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2002 hists->dso_filter);
2003 else
2004 hists__filter_by_type(hists, HIST_FILTER__DSO,
2005 hists__filter_entry_by_dso);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002006}
2007
2008void hists__filter_by_symbol(struct hists *hists)
2009{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002010 if (symbol_conf.report_hierarchy)
2011 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2012 hists->symbol_filter_str);
2013 else
2014 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2015 hists__filter_entry_by_symbol);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002016}
2017
2018void hists__filter_by_socket(struct hists *hists)
2019{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002020 if (symbol_conf.report_hierarchy)
2021 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2022 &hists->socket_filter);
2023 else
2024 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2025 hists__filter_entry_by_socket);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002026}
2027
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002028void events_stats__inc(struct events_stats *stats, u32 type)
2029{
2030 ++stats->nr_events[0];
2031 ++stats->nr_events[type];
2032}
2033
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002034void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002035{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002036 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002037}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002038
Namhyung Kim1844dbc2014-05-28 14:12:18 +09002039void hists__inc_nr_samples(struct hists *hists, bool filtered)
2040{
2041 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2042 if (!filtered)
2043 hists->stats.nr_non_filtered_samples++;
2044}
2045
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002046static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2047 struct hist_entry *pair)
2048{
Namhyung Kimce74f602012-12-10 17:29:55 +09002049 struct rb_root *root;
2050 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002051 struct rb_node *parent = NULL;
2052 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07002053 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002054
Jiri Olsa52225032016-05-03 13:54:42 +02002055 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002056 root = &hists->entries_collapsed;
2057 else
2058 root = hists->entries_in;
2059
2060 p = &root->rb_node;
2061
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002062 while (*p != NULL) {
2063 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09002064 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002065
Namhyung Kimce74f602012-12-10 17:29:55 +09002066 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002067
2068 if (!cmp)
2069 goto out;
2070
2071 if (cmp < 0)
2072 p = &(*p)->rb_left;
2073 else
2074 p = &(*p)->rb_right;
2075 }
2076
Namhyung Kima0b51af2012-09-11 13:34:27 +09002077 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002078 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03002079 memset(&he->stat, 0, sizeof(he->stat));
2080 he->hists = hists;
Kan Liang09623d72016-04-24 23:28:09 -07002081 if (symbol_conf.cumulate_callchain)
2082 memset(he->stat_acc, 0, sizeof(he->stat));
Namhyung Kimce74f602012-12-10 17:29:55 +09002083 rb_link_node(&he->rb_node_in, parent, p);
2084 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09002085 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01002086 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002087 }
2088out:
2089 return he;
2090}
2091
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002092static struct hist_entry *hists__find_entry(struct hists *hists,
2093 struct hist_entry *he)
2094{
Namhyung Kimce74f602012-12-10 17:29:55 +09002095 struct rb_node *n;
2096
Jiri Olsa52225032016-05-03 13:54:42 +02002097 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002098 n = hists->entries_collapsed.rb_node;
2099 else
2100 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002101
2102 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002103 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2104 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002105
2106 if (cmp < 0)
2107 n = n->rb_left;
2108 else if (cmp > 0)
2109 n = n->rb_right;
2110 else
2111 return iter;
2112 }
2113
2114 return NULL;
2115}
2116
2117/*
2118 * Look for pairs to link to the leader buckets (hist_entries):
2119 */
2120void hists__match(struct hists *leader, struct hists *other)
2121{
Namhyung Kimce74f602012-12-10 17:29:55 +09002122 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002123 struct rb_node *nd;
2124 struct hist_entry *pos, *pair;
2125
Jiri Olsa52225032016-05-03 13:54:42 +02002126 if (hists__has(leader, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002127 root = &leader->entries_collapsed;
2128 else
2129 root = leader->entries_in;
2130
2131 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2132 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002133 pair = hists__find_entry(other, pos);
2134
2135 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09002136 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002137 }
2138}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002139
2140/*
2141 * Look for entries in the other hists that are not present in the leader, if
2142 * we find them, just add a dummy entry on the leader hists, with period=0,
2143 * nr_events=0, to serve as the list header.
2144 */
2145int hists__link(struct hists *leader, struct hists *other)
2146{
Namhyung Kimce74f602012-12-10 17:29:55 +09002147 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002148 struct rb_node *nd;
2149 struct hist_entry *pos, *pair;
2150
Jiri Olsa52225032016-05-03 13:54:42 +02002151 if (hists__has(other, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002152 root = &other->entries_collapsed;
2153 else
2154 root = other->entries_in;
2155
2156 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2157 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002158
2159 if (!hist_entry__has_pairs(pos)) {
2160 pair = hists__add_dummy_entry(leader, pos);
2161 if (pair == NULL)
2162 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09002163 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002164 }
2165 }
2166
2167 return 0;
2168}
Namhyung Kimf2148332014-01-14 11:52:48 +09002169
Andi Kleen578499982015-07-18 08:24:49 -07002170void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2171 struct perf_sample *sample, bool nonany_branch_mode)
2172{
2173 struct branch_info *bi;
2174
2175 /* If we have branch cycles always annotate them. */
2176 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2177 int i;
2178
2179 bi = sample__resolve_bstack(sample, al);
2180 if (bi) {
2181 struct addr_map_symbol *prev = NULL;
2182
2183 /*
2184 * Ignore errors, still want to process the
2185 * other entries.
2186 *
2187 * For non standard branch modes always
2188 * force no IPC (prev == NULL)
2189 *
2190 * Note that perf stores branches reversed from
2191 * program order!
2192 */
2193 for (i = bs->nr - 1; i >= 0; i--) {
2194 addr_map_symbol__account_cycles(&bi[i].from,
2195 nonany_branch_mode ? NULL : prev,
2196 bi[i].flags.cycles);
2197 prev = &bi[i].to;
2198 }
2199 free(bi);
2200 }
2201 }
2202}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002203
2204size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
2205{
2206 struct perf_evsel *pos;
2207 size_t ret = 0;
2208
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002209 evlist__for_each_entry(evlist, pos) {
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002210 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2211 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2212 }
2213
2214 return ret;
2215}
2216
2217
Namhyung Kimf2148332014-01-14 11:52:48 +09002218u64 hists__total_period(struct hists *hists)
2219{
2220 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2221 hists->stats.total_period;
2222}
Namhyung Kim33db4562014-02-07 12:06:07 +09002223
2224int parse_filter_percentage(const struct option *opt __maybe_unused,
2225 const char *arg, int unset __maybe_unused)
2226{
2227 if (!strcmp(arg, "relative"))
2228 symbol_conf.filter_relative = true;
2229 else if (!strcmp(arg, "absolute"))
2230 symbol_conf.filter_relative = false;
2231 else
2232 return -1;
2233
2234 return 0;
2235}
Namhyung Kim0b93da12014-01-14 12:02:15 +09002236
2237int perf_hist_config(const char *var, const char *value)
2238{
2239 if (!strcmp(var, "hist.percentage"))
2240 return parse_filter_percentage(NULL, value, 0);
2241
2242 return 0;
2243}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002244
Jiri Olsa5b658552016-01-18 10:24:22 +01002245int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002246{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002247 memset(hists, 0, sizeof(*hists));
2248 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
2249 hists->entries_in = &hists->entries_in_array[0];
2250 hists->entries_collapsed = RB_ROOT;
2251 hists->entries = RB_ROOT;
2252 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04002253 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01002254 hists->hpp_list = hpp_list;
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002255 INIT_LIST_HEAD(&hists->hpp_formats);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002256 return 0;
2257}
2258
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002259static void hists__delete_remaining_entries(struct rb_root *root)
2260{
2261 struct rb_node *node;
2262 struct hist_entry *he;
2263
2264 while (!RB_EMPTY_ROOT(root)) {
2265 node = rb_first(root);
2266 rb_erase(node, root);
2267
2268 he = rb_entry(node, struct hist_entry, rb_node_in);
2269 hist_entry__delete(he);
2270 }
2271}
2272
2273static void hists__delete_all_entries(struct hists *hists)
2274{
2275 hists__delete_entries(hists);
2276 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2277 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2278 hists__delete_remaining_entries(&hists->entries_collapsed);
2279}
2280
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002281static void hists_evsel__exit(struct perf_evsel *evsel)
2282{
2283 struct hists *hists = evsel__hists(evsel);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002284 struct perf_hpp_fmt *fmt, *pos;
2285 struct perf_hpp_list_node *node, *tmp;
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002286
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002287 hists__delete_all_entries(hists);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002288
2289 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2290 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2291 list_del(&fmt->list);
2292 free(fmt);
2293 }
2294 list_del(&node->list);
2295 free(node);
2296 }
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002297}
2298
Namhyung Kimfc284be2016-01-07 10:14:10 +01002299static int hists_evsel__init(struct perf_evsel *evsel)
2300{
2301 struct hists *hists = evsel__hists(evsel);
2302
Jiri Olsa5b658552016-01-18 10:24:22 +01002303 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01002304 return 0;
2305}
2306
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002307/*
2308 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2309 * stored in the rbtree...
2310 */
2311
2312int hists__init(void)
2313{
2314 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002315 hists_evsel__init,
2316 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002317 if (err)
2318 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2319
2320 return err;
2321}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01002322
2323void perf_hpp_list__init(struct perf_hpp_list *list)
2324{
2325 INIT_LIST_HEAD(&list->fields);
2326 INIT_LIST_HEAD(&list->sorts);
2327}