blob: 355b7601ddb7cb68c8de4867afc16bbf38c0a2bd [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
Jiri Olsaf542e762016-07-05 08:56:04 +0200427static void *hist_entry__zalloc(size_t size)
428{
429 return zalloc(size + sizeof(struct hist_entry));
430}
431
432static void hist_entry__free(void *ptr)
433{
434 free(ptr);
435}
436
437static struct hist_entry_ops default_ops = {
438 .new = hist_entry__zalloc,
439 .free = hist_entry__free,
440};
441
Namhyung Kima0b51af2012-09-11 13:34:27 +0900442static struct hist_entry *hist_entry__new(struct hist_entry *template,
443 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300444{
Jiri Olsaf542e762016-07-05 08:56:04 +0200445 struct hist_entry_ops *ops = template->ops;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900446 size_t callchain_size = 0;
447 struct hist_entry *he;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200448 int err = 0;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900449
Jiri Olsaf542e762016-07-05 08:56:04 +0200450 if (!ops)
451 ops = template->ops = &default_ops;
452
Namhyung Kim82aa0192014-12-22 13:44:14 +0900453 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900454 callchain_size = sizeof(struct callchain_root);
455
Jiri Olsaf542e762016-07-05 08:56:04 +0200456 he = ops->new(callchain_size);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200457 if (he) {
458 err = hist_entry__init(he, template, sample_self);
Jiri Olsaf542e762016-07-05 08:56:04 +0200459 if (err) {
460 ops->free(he);
461 he = NULL;
462 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300463 }
464
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200465 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300466}
467
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300468static u8 symbol__parent_filter(const struct symbol *parent)
469{
470 if (symbol_conf.exclude_other && parent == NULL)
471 return 1 << HIST_FILTER__PARENT;
472 return 0;
473}
474
Namhyung Kim467ef102016-02-16 23:08:19 +0900475static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
476{
477 if (!symbol_conf.use_callchain)
478 return;
479
480 he->hists->callchain_period += period;
481 if (!he->filtered)
482 he->hists->callchain_non_filtered_period += period;
483}
484
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300485static struct hist_entry *hists__findnew_entry(struct hists *hists,
486 struct hist_entry *entry,
487 struct addr_location *al,
488 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300489{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300490 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300491 struct rb_node *parent = NULL;
492 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700493 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900494 u64 period = entry->stat.period;
495 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300496
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300497 p = &hists->entries_in->rb_node;
498
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300499 while (*p != NULL) {
500 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300501 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300502
Namhyung Kim9afcf932012-12-10 17:29:54 +0900503 /*
504 * Make sure that it receives arguments in a same order as
505 * hist_entry__collapse() so that we can use an appropriate
506 * function when searching an entry regardless which sort
507 * keys were used.
508 */
509 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300510
511 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900512 if (sample_self) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900513 he_stat__add_period(&he->stat, period, weight);
Namhyung Kim467ef102016-02-16 23:08:19 +0900514 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900515 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900516 if (symbol_conf.cumulate_callchain)
517 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400518
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900519 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300520 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900521 * and will not be used anymore.
522 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300523 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900524
David Miller63fa4712012-03-27 03:14:18 -0400525 /* If the map of an existing hist_entry has
526 * become out-of-date due to an exec() or
527 * similar, update it. Otherwise we will
528 * mis-adjust symbol addresses when computing
529 * the history counter to increment.
530 */
531 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300532 map__put(he->ms.map);
533 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400534 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300535 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300536 }
537
538 if (cmp < 0)
539 p = &(*p)->rb_left;
540 else
541 p = &(*p)->rb_right;
542 }
543
Namhyung Kima0b51af2012-09-11 13:34:27 +0900544 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300545 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900546 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300547
Namhyung Kim0f584742016-01-28 00:40:49 +0900548 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900549 hist_entry__add_callchain_period(he, period);
550 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900551
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300552 rb_link_node(&he->rb_node_in, parent, p);
553 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300554out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900555 if (sample_self)
556 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900557 if (symbol_conf.cumulate_callchain)
558 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300559 return he;
560}
561
Jiri Olsa0102ef32016-06-14 20:19:21 +0200562struct hist_entry *hists__add_entry(struct hists *hists,
563 struct addr_location *al,
564 struct symbol *sym_parent,
565 struct branch_info *bi,
566 struct mem_info *mi,
567 struct perf_sample *sample,
568 bool sample_self)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100569{
570 struct hist_entry entry = {
571 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900572 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100573 .ms = {
574 .map = al->map,
575 .sym = al->sym,
576 },
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400577 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400578 .cpu = al->cpu,
579 .cpumode = al->cpumode,
580 .ip = al->addr,
581 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900582 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900583 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900584 .period = sample->period,
585 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900586 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100587 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300588 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300589 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900590 .branch_info = bi,
591 .mem_info = mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900592 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900593 .raw_data = sample->raw_data,
594 .raw_size = sample->raw_size,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100595 };
596
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300597 return hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100598}
599
Namhyung Kim69bcb012013-10-30 09:40:34 +0900600static int
601iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
602 struct addr_location *al __maybe_unused)
603{
604 return 0;
605}
606
607static int
608iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
609 struct addr_location *al __maybe_unused)
610{
611 return 0;
612}
613
614static int
615iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
616{
617 struct perf_sample *sample = iter->sample;
618 struct mem_info *mi;
619
620 mi = sample__resolve_mem(sample, al);
621 if (mi == NULL)
622 return -ENOMEM;
623
624 iter->priv = mi;
625 return 0;
626}
627
628static int
629iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
630{
631 u64 cost;
632 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300633 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900634 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900635 struct hist_entry *he;
636
637 if (mi == NULL)
638 return -EINVAL;
639
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900640 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900641 if (!cost)
642 cost = 1;
643
644 /*
645 * must pass period=weight in order to get the correct
646 * sorting from hists__collapse_resort() which is solely
647 * based on periods. We want sorting be done on nr_events * weight
648 * and this is indirectly achieved by passing period=weight here
649 * and the he_stat__add_period() function.
650 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900651 sample->period = cost;
652
Jiri Olsa0102ef32016-06-14 20:19:21 +0200653 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
654 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900655 if (!he)
656 return -ENOMEM;
657
658 iter->he = he;
659 return 0;
660}
661
662static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900663iter_finish_mem_entry(struct hist_entry_iter *iter,
664 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900665{
666 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300667 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900668 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900669 int err = -EINVAL;
670
671 if (he == NULL)
672 goto out;
673
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300674 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900675
676 err = hist_entry__append_callchain(he, iter->sample);
677
678out:
679 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300680 * We don't need to free iter->priv (mem_info) here since the mem info
681 * was either already freed in hists__findnew_entry() or passed to a
682 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900683 */
684 iter->priv = NULL;
685
686 iter->he = NULL;
687 return err;
688}
689
690static int
691iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
692{
693 struct branch_info *bi;
694 struct perf_sample *sample = iter->sample;
695
696 bi = sample__resolve_bstack(sample, al);
697 if (!bi)
698 return -ENOMEM;
699
700 iter->curr = 0;
701 iter->total = sample->branch_stack->nr;
702
703 iter->priv = bi;
704 return 0;
705}
706
707static int
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -0300708iter_add_single_branch_entry(struct hist_entry_iter *iter,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900709 struct addr_location *al __maybe_unused)
710{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900711 /* to avoid calling callback function */
712 iter->he = NULL;
713
Namhyung Kim69bcb012013-10-30 09:40:34 +0900714 return 0;
715}
716
717static int
718iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
719{
720 struct branch_info *bi = iter->priv;
721 int i = iter->curr;
722
723 if (bi == NULL)
724 return 0;
725
726 if (iter->curr >= iter->total)
727 return 0;
728
729 al->map = bi[i].to.map;
730 al->sym = bi[i].to.sym;
731 al->addr = bi[i].to.addr;
732 return 1;
733}
734
735static int
736iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
737{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900738 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900739 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300740 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900741 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900742 struct hist_entry *he = NULL;
743 int i = iter->curr;
744 int err = 0;
745
746 bi = iter->priv;
747
748 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
749 goto out;
750
751 /*
752 * The report shows the percentage of total branches captured
753 * and not events sampled. Thus we use a pseudo period of 1.
754 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900755 sample->period = 1;
756 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
757
Jiri Olsa0102ef32016-06-14 20:19:21 +0200758 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
759 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900760 if (he == NULL)
761 return -ENOMEM;
762
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300763 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900764
765out:
766 iter->he = he;
767 iter->curr++;
768 return err;
769}
770
771static int
772iter_finish_branch_entry(struct hist_entry_iter *iter,
773 struct addr_location *al __maybe_unused)
774{
775 zfree(&iter->priv);
776 iter->he = NULL;
777
778 return iter->curr >= iter->total ? 0 : -1;
779}
780
781static int
782iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
783 struct addr_location *al __maybe_unused)
784{
785 return 0;
786}
787
788static int
789iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
790{
791 struct perf_evsel *evsel = iter->evsel;
792 struct perf_sample *sample = iter->sample;
793 struct hist_entry *he;
794
Jiri Olsa0102ef32016-06-14 20:19:21 +0200795 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
796 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900797 if (he == NULL)
798 return -ENOMEM;
799
800 iter->he = he;
801 return 0;
802}
803
804static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900805iter_finish_normal_entry(struct hist_entry_iter *iter,
806 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900807{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900808 struct hist_entry *he = iter->he;
809 struct perf_evsel *evsel = iter->evsel;
810 struct perf_sample *sample = iter->sample;
811
812 if (he == NULL)
813 return 0;
814
815 iter->he = NULL;
816
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300817 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900818
819 return hist_entry__append_callchain(he, sample);
820}
821
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900822static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300823iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900824 struct addr_location *al __maybe_unused)
825{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900826 struct hist_entry **he_cache;
827
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900828 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900829
830 /*
831 * This is for detecting cycles or recursions so that they're
832 * cumulated only one time to prevent entries more than 100%
833 * overhead.
834 */
Adrian Hunter96b40f32015-09-25 16:15:47 +0300835 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900836 if (he_cache == NULL)
837 return -ENOMEM;
838
839 iter->priv = he_cache;
840 iter->curr = 0;
841
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900842 return 0;
843}
844
845static int
846iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
847 struct addr_location *al)
848{
849 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300850 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900851 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900852 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900853 struct hist_entry *he;
854 int err = 0;
855
Jiri Olsa0102ef32016-06-14 20:19:21 +0200856 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
857 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900858 if (he == NULL)
859 return -ENOMEM;
860
861 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900862 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900863
Namhyung Kim82aa0192014-12-22 13:44:14 +0900864 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900865
866 /*
867 * We need to re-initialize the cursor since callchain_append()
868 * advanced the cursor to the end.
869 */
870 callchain_cursor_commit(&callchain_cursor);
871
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300872 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900873
874 return err;
875}
876
877static int
878iter_next_cumulative_entry(struct hist_entry_iter *iter,
879 struct addr_location *al)
880{
881 struct callchain_cursor_node *node;
882
883 node = callchain_cursor_current(&callchain_cursor);
884 if (node == NULL)
885 return 0;
886
Namhyung Kimc7405d82013-10-31 13:58:30 +0900887 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900888}
889
890static int
891iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
892 struct addr_location *al)
893{
894 struct perf_evsel *evsel = iter->evsel;
895 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900896 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900897 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900898 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -0300899 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900900 .cpu = al->cpu,
901 .thread = al->thread,
902 .comm = thread__comm(al->thread),
903 .ip = al->addr,
904 .ms = {
905 .map = al->map,
906 .sym = al->sym,
907 },
908 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +0900909 .raw_data = sample->raw_data,
910 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900911 };
912 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900913 struct callchain_cursor cursor;
914
915 callchain_cursor_snapshot(&cursor, &callchain_cursor);
916
917 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900918
919 /*
920 * Check if there's duplicate entries in the callchain.
921 * It's possible that it has cycles or recursive calls.
922 */
923 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900924 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
925 /* to avoid calling callback function */
926 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900927 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900928 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900929 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900930
Jiri Olsa0102ef32016-06-14 20:19:21 +0200931 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
932 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900933 if (he == NULL)
934 return -ENOMEM;
935
936 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900937 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900938
Namhyung Kim82aa0192014-12-22 13:44:14 +0900939 if (symbol_conf.use_callchain)
940 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900941 return 0;
942}
943
944static int
945iter_finish_cumulative_entry(struct hist_entry_iter *iter,
946 struct addr_location *al __maybe_unused)
947{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900948 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900949 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900950
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900951 return 0;
952}
953
Namhyung Kim69bcb012013-10-30 09:40:34 +0900954const struct hist_iter_ops hist_iter_mem = {
955 .prepare_entry = iter_prepare_mem_entry,
956 .add_single_entry = iter_add_single_mem_entry,
957 .next_entry = iter_next_nop_entry,
958 .add_next_entry = iter_add_next_nop_entry,
959 .finish_entry = iter_finish_mem_entry,
960};
961
962const struct hist_iter_ops hist_iter_branch = {
963 .prepare_entry = iter_prepare_branch_entry,
964 .add_single_entry = iter_add_single_branch_entry,
965 .next_entry = iter_next_branch_entry,
966 .add_next_entry = iter_add_next_branch_entry,
967 .finish_entry = iter_finish_branch_entry,
968};
969
970const struct hist_iter_ops hist_iter_normal = {
971 .prepare_entry = iter_prepare_normal_entry,
972 .add_single_entry = iter_add_single_normal_entry,
973 .next_entry = iter_next_nop_entry,
974 .add_next_entry = iter_add_next_nop_entry,
975 .finish_entry = iter_finish_normal_entry,
976};
977
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900978const struct hist_iter_ops hist_iter_cumulative = {
979 .prepare_entry = iter_prepare_cumulative_entry,
980 .add_single_entry = iter_add_single_cumulative_entry,
981 .next_entry = iter_next_cumulative_entry,
982 .add_next_entry = iter_add_next_cumulative_entry,
983 .finish_entry = iter_finish_cumulative_entry,
984};
985
Namhyung Kim69bcb012013-10-30 09:40:34 +0900986int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900987 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900988{
989 int err, err2;
990
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300991 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
Namhyung Kim063bd932015-05-19 17:04:10 +0900992 iter->evsel, al, max_stack_depth);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900993 if (err)
994 return err;
995
Adrian Hunter96b40f32015-09-25 16:15:47 +0300996 iter->max_stack = max_stack_depth;
997
Namhyung Kim69bcb012013-10-30 09:40:34 +0900998 err = iter->ops->prepare_entry(iter, al);
999 if (err)
1000 goto out;
1001
1002 err = iter->ops->add_single_entry(iter, al);
1003 if (err)
1004 goto out;
1005
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001006 if (iter->he && iter->add_entry_cb) {
1007 err = iter->add_entry_cb(iter, al, true, arg);
1008 if (err)
1009 goto out;
1010 }
1011
Namhyung Kim69bcb012013-10-30 09:40:34 +09001012 while (iter->ops->next_entry(iter, al)) {
1013 err = iter->ops->add_next_entry(iter, al);
1014 if (err)
1015 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001016
1017 if (iter->he && iter->add_entry_cb) {
1018 err = iter->add_entry_cb(iter, al, false, arg);
1019 if (err)
1020 goto out;
1021 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001022 }
1023
1024out:
1025 err2 = iter->ops->finish_entry(iter, al);
1026 if (!err)
1027 err = err2;
1028
1029 return err;
1030}
1031
John Kacur3d1d07e2009-09-28 15:32:55 +02001032int64_t
1033hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1034{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001035 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001036 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001037 int64_t cmp = 0;
1038
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001039 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001040 if (perf_hpp__is_dynamic_entry(fmt) &&
1041 !perf_hpp__defined_dynamic_entry(fmt, hists))
1042 continue;
1043
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001044 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001045 if (cmp)
1046 break;
1047 }
1048
1049 return cmp;
1050}
1051
1052int64_t
1053hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1054{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001055 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001056 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001057 int64_t cmp = 0;
1058
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001059 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001060 if (perf_hpp__is_dynamic_entry(fmt) &&
1061 !perf_hpp__defined_dynamic_entry(fmt, hists))
1062 continue;
1063
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001064 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001065 if (cmp)
1066 break;
1067 }
1068
1069 return cmp;
1070}
1071
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001072void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001073{
Jiri Olsaf542e762016-07-05 08:56:04 +02001074 struct hist_entry_ops *ops = he->ops;
1075
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001076 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001077 map__zput(he->ms.map);
1078
1079 if (he->branch_info) {
1080 map__zput(he->branch_info->from.map);
1081 map__zput(he->branch_info->to.map);
Andi Kleen508be0d2016-05-20 13:15:08 -07001082 free_srcline(he->branch_info->srcline_from);
1083 free_srcline(he->branch_info->srcline_to);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001084 zfree(&he->branch_info);
1085 }
1086
1087 if (he->mem_info) {
1088 map__zput(he->mem_info->iaddr.map);
1089 map__zput(he->mem_info->daddr.map);
1090 zfree(&he->mem_info);
1091 }
1092
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001093 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +09001094 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -07001095 if (he->srcfile && he->srcfile[0])
1096 free(he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001097 free_callchain(he->callchain);
Namhyung Kim60517d22015-12-23 02:07:03 +09001098 free(he->trace_output);
Namhyung Kim72392832015-12-24 11:16:17 +09001099 free(he->raw_data);
Jiri Olsaf542e762016-07-05 08:56:04 +02001100 ops->free(he);
John Kacur3d1d07e2009-09-28 15:32:55 +02001101}
1102
1103/*
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001104 * If this is not the last column, then we need to pad it according to the
1105 * pre-calculated max lenght for this column, otherwise don't bother adding
1106 * spaces because that would break viewing this with, for instance, 'less',
1107 * that would show tons of trailing spaces when a long C++ demangled method
1108 * names is sampled.
1109*/
1110int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1111 struct perf_hpp_fmt *fmt, int printed)
1112{
1113 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
Jiri Olsada1b0402016-06-14 20:19:20 +02001114 const int width = fmt->width(fmt, hpp, he->hists);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001115 if (printed < width) {
1116 advance_hpp(hpp, printed);
1117 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1118 }
1119 }
1120
1121 return printed;
1122}
1123
1124/*
John Kacur3d1d07e2009-09-28 15:32:55 +02001125 * collapse the histogram
1126 */
1127
Namhyung Kimaef810e2016-02-25 00:13:34 +09001128static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
Namhyung Kimaec13a72016-03-09 22:46:58 +09001129static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1130 enum hist_filter type);
1131
1132typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1133
1134static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1135{
1136 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1137}
1138
1139static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1140 enum hist_filter type,
1141 fmt_chk_fn check)
1142{
1143 struct perf_hpp_fmt *fmt;
1144 bool type_match = false;
1145 struct hist_entry *parent = he->parent_he;
1146
1147 switch (type) {
1148 case HIST_FILTER__THREAD:
1149 if (symbol_conf.comm_list == NULL &&
1150 symbol_conf.pid_list == NULL &&
1151 symbol_conf.tid_list == NULL)
1152 return;
1153 break;
1154 case HIST_FILTER__DSO:
1155 if (symbol_conf.dso_list == NULL)
1156 return;
1157 break;
1158 case HIST_FILTER__SYMBOL:
1159 if (symbol_conf.sym_list == NULL)
1160 return;
1161 break;
1162 case HIST_FILTER__PARENT:
1163 case HIST_FILTER__GUEST:
1164 case HIST_FILTER__HOST:
1165 case HIST_FILTER__SOCKET:
1166 default:
1167 return;
1168 }
1169
1170 /* if it's filtered by own fmt, it has to have filter bits */
1171 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1172 if (check(fmt)) {
1173 type_match = true;
1174 break;
1175 }
1176 }
1177
1178 if (type_match) {
1179 /*
1180 * If the filter is for current level entry, propagate
1181 * filter marker to parents. The marker bit was
1182 * already set by default so it only needs to clear
1183 * non-filtered entries.
1184 */
1185 if (!(he->filtered & (1 << type))) {
1186 while (parent) {
1187 parent->filtered &= ~(1 << type);
1188 parent = parent->parent_he;
1189 }
1190 }
1191 } else {
1192 /*
1193 * If current entry doesn't have matching formats, set
1194 * filter marker for upper level entries. it will be
1195 * cleared if its lower level entries is not filtered.
1196 *
1197 * For lower-level entries, it inherits parent's
1198 * filter bit so that lower level entries of a
1199 * non-filtered entry won't set the filter marker.
1200 */
1201 if (parent == NULL)
1202 he->filtered |= (1 << type);
1203 else
1204 he->filtered |= (parent->filtered & (1 << type));
1205 }
1206}
1207
1208static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1209{
1210 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1211 check_thread_entry);
1212
1213 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1214 perf_hpp__is_dso_entry);
1215
1216 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1217 perf_hpp__is_sym_entry);
1218
1219 hists__apply_filters(he->hists, he);
1220}
Namhyung Kimaef810e2016-02-25 00:13:34 +09001221
1222static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1223 struct rb_root *root,
1224 struct hist_entry *he,
Namhyung Kimaec13a72016-03-09 22:46:58 +09001225 struct hist_entry *parent_he,
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001226 struct perf_hpp_list *hpp_list)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001227{
1228 struct rb_node **p = &root->rb_node;
1229 struct rb_node *parent = NULL;
1230 struct hist_entry *iter, *new;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001231 struct perf_hpp_fmt *fmt;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001232 int64_t cmp;
1233
1234 while (*p != NULL) {
1235 parent = *p;
1236 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1237
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001238 cmp = 0;
1239 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1240 cmp = fmt->collapse(fmt, iter, he);
1241 if (cmp)
1242 break;
1243 }
1244
Namhyung Kimaef810e2016-02-25 00:13:34 +09001245 if (!cmp) {
1246 he_stat__add_stat(&iter->stat, &he->stat);
1247 return iter;
1248 }
1249
1250 if (cmp < 0)
1251 p = &parent->rb_left;
1252 else
1253 p = &parent->rb_right;
1254 }
1255
1256 new = hist_entry__new(he, true);
1257 if (new == NULL)
1258 return NULL;
1259
Namhyung Kimaef810e2016-02-25 00:13:34 +09001260 hists->nr_entries++;
1261
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001262 /* save related format list for output */
1263 new->hpp_list = hpp_list;
Namhyung Kimaec13a72016-03-09 22:46:58 +09001264 new->parent_he = parent_he;
1265
1266 hist_entry__apply_hierarchy_filters(new);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001267
1268 /* some fields are now passed to 'new' */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001269 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1270 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1271 he->trace_output = NULL;
1272 else
1273 new->trace_output = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001274
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001275 if (perf_hpp__is_srcline_entry(fmt))
1276 he->srcline = NULL;
1277 else
1278 new->srcline = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001279
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001280 if (perf_hpp__is_srcfile_entry(fmt))
1281 he->srcfile = NULL;
1282 else
1283 new->srcfile = NULL;
1284 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001285
1286 rb_link_node(&new->rb_node_in, parent, p);
1287 rb_insert_color(&new->rb_node_in, root);
1288 return new;
1289}
1290
1291static int hists__hierarchy_insert_entry(struct hists *hists,
1292 struct rb_root *root,
1293 struct hist_entry *he)
1294{
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001295 struct perf_hpp_list_node *node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001296 struct hist_entry *new_he = NULL;
1297 struct hist_entry *parent = NULL;
1298 int depth = 0;
1299 int ret = 0;
1300
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001301 list_for_each_entry(node, &hists->hpp_formats, list) {
1302 /* skip period (overhead) and elided columns */
1303 if (node->level == 0 || node->skip)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001304 continue;
1305
1306 /* insert copy of 'he' for each fmt into the hierarchy */
Namhyung Kimaec13a72016-03-09 22:46:58 +09001307 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001308 if (new_he == NULL) {
1309 ret = -1;
1310 break;
1311 }
1312
1313 root = &new_he->hroot_in;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001314 new_he->depth = depth++;
1315 parent = new_he;
1316 }
1317
1318 if (new_he) {
1319 new_he->leaf = true;
1320
1321 if (symbol_conf.use_callchain) {
1322 callchain_cursor_reset(&callchain_cursor);
1323 if (callchain_merge(&callchain_cursor,
1324 new_he->callchain,
1325 he->callchain) < 0)
1326 ret = -1;
1327 }
1328 }
1329
1330 /* 'he' is no longer used */
1331 hist_entry__delete(he);
1332
1333 /* return 0 (or -1) since it already applied filters */
1334 return ret;
1335}
1336
Jiri Olsa592dac62016-03-24 13:52:17 +01001337static int hists__collapse_insert_entry(struct hists *hists,
1338 struct rb_root *root,
1339 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001340{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001341 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001342 struct rb_node *parent = NULL;
1343 struct hist_entry *iter;
1344 int64_t cmp;
1345
Namhyung Kimaef810e2016-02-25 00:13:34 +09001346 if (symbol_conf.report_hierarchy)
1347 return hists__hierarchy_insert_entry(hists, root, he);
1348
John Kacur3d1d07e2009-09-28 15:32:55 +02001349 while (*p != NULL) {
1350 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001351 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001352
1353 cmp = hist_entry__collapse(iter, he);
1354
1355 if (!cmp) {
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001356 int ret = 0;
1357
Namhyung Kim139c0812012-10-04 21:49:43 +09001358 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001359 if (symbol_conf.cumulate_callchain)
1360 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001361
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001362 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001363 callchain_cursor_reset(&callchain_cursor);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001364 if (callchain_merge(&callchain_cursor,
1365 iter->callchain,
1366 he->callchain) < 0)
1367 ret = -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001368 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001369 hist_entry__delete(he);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001370 return ret;
John Kacur3d1d07e2009-09-28 15:32:55 +02001371 }
1372
1373 if (cmp < 0)
1374 p = &(*p)->rb_left;
1375 else
1376 p = &(*p)->rb_right;
1377 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001378 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001379
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001380 rb_link_node(&he->rb_node_in, parent, p);
1381 rb_insert_color(&he->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001382 return 1;
John Kacur3d1d07e2009-09-28 15:32:55 +02001383}
1384
Namhyung Kimfc284be2016-01-07 10:14:10 +01001385struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001386{
1387 struct rb_root *root;
1388
1389 pthread_mutex_lock(&hists->lock);
1390
1391 root = hists->entries_in;
1392 if (++hists->entries_in > &hists->entries_in_array[1])
1393 hists->entries_in = &hists->entries_in_array[0];
1394
1395 pthread_mutex_unlock(&hists->lock);
1396
1397 return root;
1398}
1399
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001400static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1401{
1402 hists__filter_entry_by_dso(hists, he);
1403 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001404 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001405 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001406}
1407
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001408int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001409{
1410 struct rb_root *root;
1411 struct rb_node *next;
1412 struct hist_entry *n;
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001413 int ret;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001414
Jiri Olsa52225032016-05-03 13:54:42 +02001415 if (!hists__has(hists, need_collapse))
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001416 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001417
Namhyung Kim740b97f2014-12-22 13:44:10 +09001418 hists->nr_entries = 0;
1419
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001420 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001421
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001422 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001423
1424 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001425 if (session_done())
1426 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001427 n = rb_entry(next, struct hist_entry, rb_node_in);
1428 next = rb_next(&n->rb_node_in);
1429
1430 rb_erase(&n->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001431 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1432 if (ret < 0)
1433 return -1;
1434
1435 if (ret) {
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001436 /*
1437 * If it wasn't combined with one of the entries already
1438 * collapsed, we need to apply the filters that may have
1439 * been set by, say, the hist_browser.
1440 */
1441 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001442 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001443 if (prog)
1444 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001445 }
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001446 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001447}
1448
Namhyung Kim043ca3892014-03-03 14:18:00 +09001449static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001450{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001451 struct hists *hists = a->hists;
Namhyung Kim043ca3892014-03-03 14:18:00 +09001452 struct perf_hpp_fmt *fmt;
1453 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001454
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001455 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001456 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001457 continue;
1458
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001459 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001460 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001461 break;
1462 }
1463
Namhyung Kim043ca3892014-03-03 14:18:00 +09001464 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001465}
1466
Namhyung Kim9283ba92014-04-24 16:37:26 +09001467static void hists__reset_filter_stats(struct hists *hists)
1468{
1469 hists->nr_non_filtered_entries = 0;
1470 hists->stats.total_non_filtered_period = 0;
1471}
1472
1473void hists__reset_stats(struct hists *hists)
1474{
1475 hists->nr_entries = 0;
1476 hists->stats.total_period = 0;
1477
1478 hists__reset_filter_stats(hists);
1479}
1480
1481static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1482{
1483 hists->nr_non_filtered_entries++;
1484 hists->stats.total_non_filtered_period += h->stat.period;
1485}
1486
1487void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1488{
1489 if (!h->filtered)
1490 hists__inc_filter_stats(hists, h);
1491
1492 hists->nr_entries++;
1493 hists->stats.total_period += h->stat.period;
1494}
1495
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001496static void hierarchy_recalc_total_periods(struct hists *hists)
1497{
1498 struct rb_node *node;
1499 struct hist_entry *he;
1500
1501 node = rb_first(&hists->entries);
1502
1503 hists->stats.total_period = 0;
1504 hists->stats.total_non_filtered_period = 0;
1505
1506 /*
1507 * recalculate total period using top-level entries only
1508 * since lower level entries only see non-filtered entries
1509 * but upper level entries have sum of both entries.
1510 */
1511 while (node) {
1512 he = rb_entry(node, struct hist_entry, rb_node);
1513 node = rb_next(node);
1514
1515 hists->stats.total_period += he->stat.period;
1516 if (!he->filtered)
1517 hists->stats.total_non_filtered_period += he->stat.period;
1518 }
1519}
1520
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001521static void hierarchy_insert_output_entry(struct rb_root *root,
1522 struct hist_entry *he)
1523{
1524 struct rb_node **p = &root->rb_node;
1525 struct rb_node *parent = NULL;
1526 struct hist_entry *iter;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001527 struct perf_hpp_fmt *fmt;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001528
1529 while (*p != NULL) {
1530 parent = *p;
1531 iter = rb_entry(parent, struct hist_entry, rb_node);
1532
1533 if (hist_entry__sort(he, iter) > 0)
1534 p = &parent->rb_left;
1535 else
1536 p = &parent->rb_right;
1537 }
1538
1539 rb_link_node(&he->rb_node, parent, p);
1540 rb_insert_color(&he->rb_node, root);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001541
1542 /* update column width of dynamic entry */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001543 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1544 if (perf_hpp__is_dynamic_entry(fmt))
1545 fmt->sort(fmt, he, NULL);
1546 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001547}
1548
1549static void hists__hierarchy_output_resort(struct hists *hists,
1550 struct ui_progress *prog,
1551 struct rb_root *root_in,
1552 struct rb_root *root_out,
1553 u64 min_callchain_hits,
1554 bool use_callchain)
1555{
1556 struct rb_node *node;
1557 struct hist_entry *he;
1558
1559 *root_out = RB_ROOT;
1560 node = rb_first(root_in);
1561
1562 while (node) {
1563 he = rb_entry(node, struct hist_entry, rb_node_in);
1564 node = rb_next(node);
1565
1566 hierarchy_insert_output_entry(root_out, he);
1567
1568 if (prog)
1569 ui_progress__update(prog, 1);
1570
1571 if (!he->leaf) {
1572 hists__hierarchy_output_resort(hists, prog,
1573 &he->hroot_in,
1574 &he->hroot_out,
1575 min_callchain_hits,
1576 use_callchain);
1577 hists->nr_entries++;
1578 if (!he->filtered) {
1579 hists->nr_non_filtered_entries++;
1580 hists__calc_col_len(hists, he);
1581 }
1582
1583 continue;
1584 }
1585
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001586 if (!use_callchain)
1587 continue;
1588
1589 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1590 u64 total = he->stat.period;
1591
1592 if (symbol_conf.cumulate_callchain)
1593 total = he->stat_acc->period;
1594
1595 min_callchain_hits = total * (callchain_param.min_percent / 100);
1596 }
1597
1598 callchain_param.sort(&he->sorted_chain, he->callchain,
1599 min_callchain_hits, &callchain_param);
1600 }
1601}
1602
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001603static void __hists__insert_output_entry(struct rb_root *entries,
1604 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001605 u64 min_callchain_hits,
1606 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001607{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001608 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001609 struct rb_node *parent = NULL;
1610 struct hist_entry *iter;
Namhyung Kimabab5e72016-02-27 03:52:47 +09001611 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001612
Namhyung Kim744070e2016-01-28 00:40:48 +09001613 if (use_callchain) {
1614 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1615 u64 total = he->stat.period;
1616
1617 if (symbol_conf.cumulate_callchain)
1618 total = he->stat_acc->period;
1619
1620 min_callchain_hits = total * (callchain_param.min_percent / 100);
1621 }
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001622 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001623 min_callchain_hits, &callchain_param);
Namhyung Kim744070e2016-01-28 00:40:48 +09001624 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001625
1626 while (*p != NULL) {
1627 parent = *p;
1628 iter = rb_entry(parent, struct hist_entry, rb_node);
1629
Namhyung Kim043ca3892014-03-03 14:18:00 +09001630 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001631 p = &(*p)->rb_left;
1632 else
1633 p = &(*p)->rb_right;
1634 }
1635
1636 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001637 rb_insert_color(&he->rb_node, entries);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001638
1639 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1640 if (perf_hpp__is_dynamic_entry(fmt) &&
1641 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1642 fmt->sort(fmt, he, NULL); /* update column width */
1643 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001644}
1645
Jiri Olsa01441af2016-01-18 10:23:59 +01001646static void output_resort(struct hists *hists, struct ui_progress *prog,
1647 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001648{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001649 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001650 struct rb_node *next;
1651 struct hist_entry *n;
Namhyung Kim467ef102016-02-16 23:08:19 +09001652 u64 callchain_total;
John Kacur3d1d07e2009-09-28 15:32:55 +02001653 u64 min_callchain_hits;
1654
Namhyung Kim467ef102016-02-16 23:08:19 +09001655 callchain_total = hists->callchain_period;
1656 if (symbol_conf.filter_relative)
1657 callchain_total = hists->callchain_non_filtered_period;
1658
1659 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001660
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001661 hists__reset_stats(hists);
1662 hists__reset_col_len(hists);
1663
1664 if (symbol_conf.report_hierarchy) {
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001665 hists__hierarchy_output_resort(hists, prog,
1666 &hists->entries_collapsed,
1667 &hists->entries,
1668 min_callchain_hits,
1669 use_callchain);
1670 hierarchy_recalc_total_periods(hists);
1671 return;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001672 }
1673
Jiri Olsa52225032016-05-03 13:54:42 +02001674 if (hists__has(hists, need_collapse))
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001675 root = &hists->entries_collapsed;
1676 else
1677 root = hists->entries_in;
1678
1679 next = rb_first(root);
1680 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001681
1682 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001683 n = rb_entry(next, struct hist_entry, rb_node_in);
1684 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001685
Kan Liangf9db0d02015-08-11 06:30:48 -04001686 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001687 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001688
1689 if (!n->filtered)
1690 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001691
1692 if (prog)
1693 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001694 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001695}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001696
Jiri Olsa452ce032016-01-18 10:24:00 +01001697void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
Jiri Olsa01441af2016-01-18 10:23:59 +01001698{
Jiri Olsa01441af2016-01-18 10:23:59 +01001699 bool use_callchain;
1700
1701 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1702 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1703 else
1704 use_callchain = symbol_conf.use_callchain;
1705
Jiri Olsa452ce032016-01-18 10:24:00 +01001706 output_resort(evsel__hists(evsel), prog, use_callchain);
1707}
1708
1709void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1710{
1711 output_resort(hists, prog, symbol_conf.use_callchain);
Jiri Olsa01441af2016-01-18 10:23:59 +01001712}
1713
Namhyung Kim8c018722016-02-25 00:13:36 +09001714static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1715{
1716 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1717 return false;
1718
1719 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1720 return true;
1721
1722 return false;
1723}
1724
1725struct rb_node *rb_hierarchy_last(struct rb_node *node)
1726{
1727 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1728
1729 while (can_goto_child(he, HMD_NORMAL)) {
1730 node = rb_last(&he->hroot_out);
1731 he = rb_entry(node, struct hist_entry, rb_node);
1732 }
1733 return node;
1734}
1735
1736struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1737{
1738 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1739
1740 if (can_goto_child(he, hmd))
1741 node = rb_first(&he->hroot_out);
1742 else
1743 node = rb_next(node);
1744
1745 while (node == NULL) {
1746 he = he->parent_he;
1747 if (he == NULL)
1748 break;
1749
1750 node = rb_next(&he->rb_node);
1751 }
1752 return node;
1753}
1754
1755struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1756{
1757 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1758
1759 node = rb_prev(node);
1760 if (node)
1761 return rb_hierarchy_last(node);
1762
1763 he = he->parent_he;
1764 if (he == NULL)
1765 return NULL;
1766
1767 return &he->rb_node;
1768}
1769
Namhyung Kima7b58952016-02-26 21:13:16 +09001770bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1771{
1772 struct rb_node *node;
1773 struct hist_entry *child;
1774 float percent;
1775
1776 if (he->leaf)
1777 return false;
1778
1779 node = rb_first(&he->hroot_out);
1780 child = rb_entry(node, struct hist_entry, rb_node);
1781
1782 while (node && child->filtered) {
1783 node = rb_next(node);
1784 child = rb_entry(node, struct hist_entry, rb_node);
1785 }
1786
1787 if (node)
1788 percent = hist_entry__get_percent_limit(child);
1789 else
1790 percent = 0;
1791
1792 return node && percent >= limit;
1793}
1794
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001795static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001796 enum hist_filter filter)
1797{
1798 h->filtered &= ~(1 << filter);
Namhyung Kim155e9af2016-02-25 00:13:38 +09001799
1800 if (symbol_conf.report_hierarchy) {
1801 struct hist_entry *parent = h->parent_he;
1802
1803 while (parent) {
1804 he_stat__add_stat(&parent->stat, &h->stat);
1805
1806 parent->filtered &= ~(1 << filter);
1807
1808 if (parent->filtered)
1809 goto next;
1810
1811 /* force fold unfiltered entry for simplicity */
1812 parent->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001813 parent->has_no_entry = false;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001814 parent->row_offset = 0;
1815 parent->nr_rows = 0;
1816next:
1817 parent = parent->parent_he;
1818 }
1819 }
1820
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001821 if (h->filtered)
1822 return;
1823
Namhyung Kim87e90f42014-04-24 16:44:16 +09001824 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001825 h->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001826 h->has_no_entry = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001827 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001828 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001829
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001830 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001831
Namhyung Kim9283ba92014-04-24 16:37:26 +09001832 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001833 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001834}
1835
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001836
1837static bool hists__filter_entry_by_dso(struct hists *hists,
1838 struct hist_entry *he)
1839{
1840 if (hists->dso_filter != NULL &&
1841 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1842 he->filtered |= (1 << HIST_FILTER__DSO);
1843 return true;
1844 }
1845
1846 return false;
1847}
1848
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001849static bool hists__filter_entry_by_thread(struct hists *hists,
1850 struct hist_entry *he)
1851{
1852 if (hists->thread_filter != NULL &&
1853 he->thread != hists->thread_filter) {
1854 he->filtered |= (1 << HIST_FILTER__THREAD);
1855 return true;
1856 }
1857
1858 return false;
1859}
1860
Namhyung Kime94d53e2012-03-16 17:50:51 +09001861static bool hists__filter_entry_by_symbol(struct hists *hists,
1862 struct hist_entry *he)
1863{
1864 if (hists->symbol_filter_str != NULL &&
1865 (!he->ms.sym || strstr(he->ms.sym->name,
1866 hists->symbol_filter_str) == NULL)) {
1867 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1868 return true;
1869 }
1870
1871 return false;
1872}
1873
Kan Liang21394d92015-09-04 10:45:44 -04001874static bool hists__filter_entry_by_socket(struct hists *hists,
1875 struct hist_entry *he)
1876{
1877 if ((hists->socket_filter > -1) &&
1878 (he->socket != hists->socket_filter)) {
1879 he->filtered |= (1 << HIST_FILTER__SOCKET);
1880 return true;
1881 }
1882
1883 return false;
1884}
1885
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001886typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1887
1888static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04001889{
1890 struct rb_node *nd;
1891
1892 hists->stats.nr_non_filtered_samples = 0;
1893
1894 hists__reset_filter_stats(hists);
1895 hists__reset_col_len(hists);
1896
1897 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1898 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1899
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001900 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04001901 continue;
1902
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001903 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04001904 }
1905}
1906
Namhyung Kim70642852016-02-25 00:13:39 +09001907static void resort_filtered_entry(struct rb_root *root, struct hist_entry *he)
1908{
1909 struct rb_node **p = &root->rb_node;
1910 struct rb_node *parent = NULL;
1911 struct hist_entry *iter;
1912 struct rb_root new_root = RB_ROOT;
1913 struct rb_node *nd;
1914
1915 while (*p != NULL) {
1916 parent = *p;
1917 iter = rb_entry(parent, struct hist_entry, rb_node);
1918
1919 if (hist_entry__sort(he, iter) > 0)
1920 p = &(*p)->rb_left;
1921 else
1922 p = &(*p)->rb_right;
1923 }
1924
1925 rb_link_node(&he->rb_node, parent, p);
1926 rb_insert_color(&he->rb_node, root);
1927
1928 if (he->leaf || he->filtered)
1929 return;
1930
1931 nd = rb_first(&he->hroot_out);
1932 while (nd) {
1933 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1934
1935 nd = rb_next(nd);
1936 rb_erase(&h->rb_node, &he->hroot_out);
1937
1938 resort_filtered_entry(&new_root, h);
1939 }
1940
1941 he->hroot_out = new_root;
1942}
1943
Namhyung Kim155e9af2016-02-25 00:13:38 +09001944static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
1945{
1946 struct rb_node *nd;
Namhyung Kim70642852016-02-25 00:13:39 +09001947 struct rb_root new_root = RB_ROOT;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001948
1949 hists->stats.nr_non_filtered_samples = 0;
1950
1951 hists__reset_filter_stats(hists);
1952 hists__reset_col_len(hists);
1953
1954 nd = rb_first(&hists->entries);
1955 while (nd) {
1956 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1957 int ret;
1958
1959 ret = hist_entry__filter(h, type, arg);
1960
1961 /*
1962 * case 1. non-matching type
1963 * zero out the period, set filter marker and move to child
1964 */
1965 if (ret < 0) {
1966 memset(&h->stat, 0, sizeof(h->stat));
1967 h->filtered |= (1 << type);
1968
1969 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
1970 }
1971 /*
1972 * case 2. matched type (filter out)
1973 * set filter marker and move to next
1974 */
1975 else if (ret == 1) {
1976 h->filtered |= (1 << type);
1977
1978 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
1979 }
1980 /*
1981 * case 3. ok (not filtered)
1982 * add period to hists and parents, erase the filter marker
1983 * and move to next sibling
1984 */
1985 else {
1986 hists__remove_entry_filter(hists, h, type);
1987
1988 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
1989 }
1990 }
Namhyung Kim70642852016-02-25 00:13:39 +09001991
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001992 hierarchy_recalc_total_periods(hists);
1993
Namhyung Kim70642852016-02-25 00:13:39 +09001994 /*
1995 * resort output after applying a new filter since filter in a lower
1996 * hierarchy can change periods in a upper hierarchy.
1997 */
1998 nd = rb_first(&hists->entries);
1999 while (nd) {
2000 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2001
2002 nd = rb_next(nd);
2003 rb_erase(&h->rb_node, &hists->entries);
2004
2005 resort_filtered_entry(&new_root, h);
2006 }
2007
2008 hists->entries = new_root;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002009}
2010
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002011void hists__filter_by_thread(struct hists *hists)
2012{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002013 if (symbol_conf.report_hierarchy)
2014 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2015 hists->thread_filter);
2016 else
2017 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2018 hists__filter_entry_by_thread);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002019}
2020
2021void hists__filter_by_dso(struct hists *hists)
2022{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002023 if (symbol_conf.report_hierarchy)
2024 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2025 hists->dso_filter);
2026 else
2027 hists__filter_by_type(hists, HIST_FILTER__DSO,
2028 hists__filter_entry_by_dso);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002029}
2030
2031void hists__filter_by_symbol(struct hists *hists)
2032{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002033 if (symbol_conf.report_hierarchy)
2034 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2035 hists->symbol_filter_str);
2036 else
2037 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2038 hists__filter_entry_by_symbol);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002039}
2040
2041void hists__filter_by_socket(struct hists *hists)
2042{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002043 if (symbol_conf.report_hierarchy)
2044 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2045 &hists->socket_filter);
2046 else
2047 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2048 hists__filter_entry_by_socket);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002049}
2050
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002051void events_stats__inc(struct events_stats *stats, u32 type)
2052{
2053 ++stats->nr_events[0];
2054 ++stats->nr_events[type];
2055}
2056
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002057void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002058{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002059 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002060}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002061
Namhyung Kim1844dbc2014-05-28 14:12:18 +09002062void hists__inc_nr_samples(struct hists *hists, bool filtered)
2063{
2064 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2065 if (!filtered)
2066 hists->stats.nr_non_filtered_samples++;
2067}
2068
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002069static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2070 struct hist_entry *pair)
2071{
Namhyung Kimce74f602012-12-10 17:29:55 +09002072 struct rb_root *root;
2073 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002074 struct rb_node *parent = NULL;
2075 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07002076 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002077
Jiri Olsa52225032016-05-03 13:54:42 +02002078 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002079 root = &hists->entries_collapsed;
2080 else
2081 root = hists->entries_in;
2082
2083 p = &root->rb_node;
2084
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002085 while (*p != NULL) {
2086 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09002087 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002088
Namhyung Kimce74f602012-12-10 17:29:55 +09002089 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002090
2091 if (!cmp)
2092 goto out;
2093
2094 if (cmp < 0)
2095 p = &(*p)->rb_left;
2096 else
2097 p = &(*p)->rb_right;
2098 }
2099
Namhyung Kima0b51af2012-09-11 13:34:27 +09002100 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002101 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03002102 memset(&he->stat, 0, sizeof(he->stat));
2103 he->hists = hists;
Kan Liang09623d72016-04-24 23:28:09 -07002104 if (symbol_conf.cumulate_callchain)
2105 memset(he->stat_acc, 0, sizeof(he->stat));
Namhyung Kimce74f602012-12-10 17:29:55 +09002106 rb_link_node(&he->rb_node_in, parent, p);
2107 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09002108 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01002109 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002110 }
2111out:
2112 return he;
2113}
2114
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002115static struct hist_entry *hists__find_entry(struct hists *hists,
2116 struct hist_entry *he)
2117{
Namhyung Kimce74f602012-12-10 17:29:55 +09002118 struct rb_node *n;
2119
Jiri Olsa52225032016-05-03 13:54:42 +02002120 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002121 n = hists->entries_collapsed.rb_node;
2122 else
2123 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002124
2125 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002126 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2127 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002128
2129 if (cmp < 0)
2130 n = n->rb_left;
2131 else if (cmp > 0)
2132 n = n->rb_right;
2133 else
2134 return iter;
2135 }
2136
2137 return NULL;
2138}
2139
2140/*
2141 * Look for pairs to link to the leader buckets (hist_entries):
2142 */
2143void hists__match(struct hists *leader, struct hists *other)
2144{
Namhyung Kimce74f602012-12-10 17:29:55 +09002145 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002146 struct rb_node *nd;
2147 struct hist_entry *pos, *pair;
2148
Jiri Olsa52225032016-05-03 13:54:42 +02002149 if (hists__has(leader, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002150 root = &leader->entries_collapsed;
2151 else
2152 root = leader->entries_in;
2153
2154 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2155 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002156 pair = hists__find_entry(other, pos);
2157
2158 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09002159 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002160 }
2161}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002162
2163/*
2164 * Look for entries in the other hists that are not present in the leader, if
2165 * we find them, just add a dummy entry on the leader hists, with period=0,
2166 * nr_events=0, to serve as the list header.
2167 */
2168int hists__link(struct hists *leader, struct hists *other)
2169{
Namhyung Kimce74f602012-12-10 17:29:55 +09002170 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002171 struct rb_node *nd;
2172 struct hist_entry *pos, *pair;
2173
Jiri Olsa52225032016-05-03 13:54:42 +02002174 if (hists__has(other, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002175 root = &other->entries_collapsed;
2176 else
2177 root = other->entries_in;
2178
2179 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2180 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002181
2182 if (!hist_entry__has_pairs(pos)) {
2183 pair = hists__add_dummy_entry(leader, pos);
2184 if (pair == NULL)
2185 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09002186 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002187 }
2188 }
2189
2190 return 0;
2191}
Namhyung Kimf2148332014-01-14 11:52:48 +09002192
Andi Kleen578499982015-07-18 08:24:49 -07002193void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2194 struct perf_sample *sample, bool nonany_branch_mode)
2195{
2196 struct branch_info *bi;
2197
2198 /* If we have branch cycles always annotate them. */
2199 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2200 int i;
2201
2202 bi = sample__resolve_bstack(sample, al);
2203 if (bi) {
2204 struct addr_map_symbol *prev = NULL;
2205
2206 /*
2207 * Ignore errors, still want to process the
2208 * other entries.
2209 *
2210 * For non standard branch modes always
2211 * force no IPC (prev == NULL)
2212 *
2213 * Note that perf stores branches reversed from
2214 * program order!
2215 */
2216 for (i = bs->nr - 1; i >= 0; i--) {
2217 addr_map_symbol__account_cycles(&bi[i].from,
2218 nonany_branch_mode ? NULL : prev,
2219 bi[i].flags.cycles);
2220 prev = &bi[i].to;
2221 }
2222 free(bi);
2223 }
2224 }
2225}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002226
2227size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
2228{
2229 struct perf_evsel *pos;
2230 size_t ret = 0;
2231
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002232 evlist__for_each_entry(evlist, pos) {
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002233 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2234 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2235 }
2236
2237 return ret;
2238}
2239
2240
Namhyung Kimf2148332014-01-14 11:52:48 +09002241u64 hists__total_period(struct hists *hists)
2242{
2243 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2244 hists->stats.total_period;
2245}
Namhyung Kim33db4562014-02-07 12:06:07 +09002246
2247int parse_filter_percentage(const struct option *opt __maybe_unused,
2248 const char *arg, int unset __maybe_unused)
2249{
2250 if (!strcmp(arg, "relative"))
2251 symbol_conf.filter_relative = true;
2252 else if (!strcmp(arg, "absolute"))
2253 symbol_conf.filter_relative = false;
2254 else
2255 return -1;
2256
2257 return 0;
2258}
Namhyung Kim0b93da12014-01-14 12:02:15 +09002259
2260int perf_hist_config(const char *var, const char *value)
2261{
2262 if (!strcmp(var, "hist.percentage"))
2263 return parse_filter_percentage(NULL, value, 0);
2264
2265 return 0;
2266}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002267
Jiri Olsa5b658552016-01-18 10:24:22 +01002268int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002269{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002270 memset(hists, 0, sizeof(*hists));
2271 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
2272 hists->entries_in = &hists->entries_in_array[0];
2273 hists->entries_collapsed = RB_ROOT;
2274 hists->entries = RB_ROOT;
2275 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04002276 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01002277 hists->hpp_list = hpp_list;
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002278 INIT_LIST_HEAD(&hists->hpp_formats);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002279 return 0;
2280}
2281
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002282static void hists__delete_remaining_entries(struct rb_root *root)
2283{
2284 struct rb_node *node;
2285 struct hist_entry *he;
2286
2287 while (!RB_EMPTY_ROOT(root)) {
2288 node = rb_first(root);
2289 rb_erase(node, root);
2290
2291 he = rb_entry(node, struct hist_entry, rb_node_in);
2292 hist_entry__delete(he);
2293 }
2294}
2295
2296static void hists__delete_all_entries(struct hists *hists)
2297{
2298 hists__delete_entries(hists);
2299 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2300 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2301 hists__delete_remaining_entries(&hists->entries_collapsed);
2302}
2303
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002304static void hists_evsel__exit(struct perf_evsel *evsel)
2305{
2306 struct hists *hists = evsel__hists(evsel);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002307 struct perf_hpp_fmt *fmt, *pos;
2308 struct perf_hpp_list_node *node, *tmp;
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002309
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002310 hists__delete_all_entries(hists);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002311
2312 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2313 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2314 list_del(&fmt->list);
2315 free(fmt);
2316 }
2317 list_del(&node->list);
2318 free(node);
2319 }
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002320}
2321
Namhyung Kimfc284be2016-01-07 10:14:10 +01002322static int hists_evsel__init(struct perf_evsel *evsel)
2323{
2324 struct hists *hists = evsel__hists(evsel);
2325
Jiri Olsa5b658552016-01-18 10:24:22 +01002326 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01002327 return 0;
2328}
2329
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002330/*
2331 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2332 * stored in the rbtree...
2333 */
2334
2335int hists__init(void)
2336{
2337 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002338 hists_evsel__init,
2339 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002340 if (err)
2341 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2342
2343 return err;
2344}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01002345
2346void perf_hpp_list__init(struct perf_hpp_list *list)
2347{
2348 INIT_LIST_HEAD(&list->fields);
2349 INIT_LIST_HEAD(&list->sorts);
2350}