blob: d9826cce68013e1c7302239885cfd9e83bd82320 [file] [log] [blame]
Frederic Weisbecker8a0ecfb2010-05-13 19:47:16 +02001#include "util.h"
Frederic Weisbecker598357e2010-05-21 12:48:39 +02002#include "build-id.h"
John Kacur3d1d07e2009-09-28 15:32:55 +02003#include "hist.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02004#include "session.h"
5#include "sort.h"
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03006#include "evlist.h"
Namhyung Kim29d720e2013-01-22 18:09:33 +09007#include "evsel.h"
Namhyung Kim69bcb012013-10-30 09:40:34 +09008#include "annotate.h"
Namhyung Kim740b97f2014-12-22 13:44:10 +09009#include "ui/progress.h"
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -020010#include <math.h>
John Kacur3d1d07e2009-09-28 15:32:55 +020011
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020012static bool hists__filter_entry_by_dso(struct hists *hists,
13 struct hist_entry *he);
14static bool hists__filter_entry_by_thread(struct hists *hists,
15 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090016static bool hists__filter_entry_by_symbol(struct hists *hists,
17 struct hist_entry *he);
Kan Liang21394d92015-09-04 10:45:44 -040018static bool hists__filter_entry_by_socket(struct hists *hists,
19 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020020
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030021u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030022{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030023 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030024}
25
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030026void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030027{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030028 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030029}
30
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030031bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030032{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030033 if (len > hists__col_len(hists, col)) {
34 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030035 return true;
36 }
37 return false;
38}
39
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090040void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030041{
42 enum hist_column col;
43
44 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030045 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030046}
47
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010048static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
49{
50 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
51
52 if (hists__col_len(hists, dso) < unresolved_col_width &&
53 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
54 !symbol_conf.dso_list)
55 hists__set_col_len(hists, dso, unresolved_col_width);
56}
57
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090058void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030059{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010060 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Stephane Eranian98a3b322013-01-24 16:10:35 +010061 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030062 u16 len;
63
Namhyung Kimded19d52013-04-01 20:35:19 +090064 /*
65 * +4 accounts for '[x] ' priv level info
66 * +2 accounts for 0x prefix on raw addresses
67 * +3 accounts for ' y ' symtab origin info
68 */
69 if (h->ms.sym) {
70 symlen = h->ms.sym->namelen + 4;
71 if (verbose)
72 symlen += BITS_PER_LONG / 4 + 2 + 3;
73 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
74 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010075 symlen = unresolved_col_width + 4 + 2;
76 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010077 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +010078 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030079
80 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030081 if (hists__new_col_len(hists, HISTC_COMM, len))
Jiri Olsa89c7cb22016-06-20 23:58:19 +020082 hists__set_col_len(hists, HISTC_THREAD, len + 8);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030083
84 if (h->ms.map) {
85 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030086 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030087 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010088
Namhyung Kimcb993742012-12-27 18:11:42 +090089 if (h->parent)
90 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
91
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010092 if (h->branch_info) {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010093 if (h->branch_info->from.sym) {
94 symlen = (int)h->branch_info->from.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +090095 if (verbose)
96 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010097 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
98
99 symlen = dso__name_len(h->branch_info->from.map->dso);
100 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
101 } else {
102 symlen = unresolved_col_width + 4 + 2;
103 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
104 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
105 }
106
107 if (h->branch_info->to.sym) {
108 symlen = (int)h->branch_info->to.sym->namelen + 4;
Namhyung Kimded19d52013-04-01 20:35:19 +0900109 if (verbose)
110 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100111 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
112
113 symlen = dso__name_len(h->branch_info->to.map->dso);
114 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
115 } else {
116 symlen = unresolved_col_width + 4 + 2;
117 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
118 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
119 }
Andi Kleen508be0d2016-05-20 13:15:08 -0700120
121 if (h->branch_info->srcline_from)
122 hists__new_col_len(hists, HISTC_SRCLINE_FROM,
123 strlen(h->branch_info->srcline_from));
124 if (h->branch_info->srcline_to)
125 hists__new_col_len(hists, HISTC_SRCLINE_TO,
126 strlen(h->branch_info->srcline_to));
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100127 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100128
129 if (h->mem_info) {
Stephane Eranian98a3b322013-01-24 16:10:35 +0100130 if (h->mem_info->daddr.sym) {
131 symlen = (int)h->mem_info->daddr.sym->namelen + 4
132 + unresolved_col_width + 2;
133 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
134 symlen);
Don Zickus9b32ba72014-06-01 15:38:29 +0200135 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
136 symlen + 1);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100137 } else {
138 symlen = unresolved_col_width + 4 + 2;
139 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
140 symlen);
Jiri Olsa08059092016-01-20 12:56:33 +0100141 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
142 symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100143 }
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200144
145 if (h->mem_info->iaddr.sym) {
146 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
147 + unresolved_col_width + 2;
148 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
149 symlen);
150 } else {
151 symlen = unresolved_col_width + 4 + 2;
152 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
153 symlen);
154 }
155
Stephane Eranian98a3b322013-01-24 16:10:35 +0100156 if (h->mem_info->daddr.map) {
157 symlen = dso__name_len(h->mem_info->daddr.map->dso);
158 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
159 symlen);
160 } else {
161 symlen = unresolved_col_width + 4 + 2;
162 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
163 }
164 } else {
165 symlen = unresolved_col_width + 4 + 2;
166 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200167 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100168 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
169 }
170
Arnaldo Carvalho de Meloa4978ec2015-09-09 12:14:00 -0300171 hists__new_col_len(hists, HISTC_CPU, 3);
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400172 hists__new_col_len(hists, HISTC_SOCKET, 6);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100173 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
174 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
175 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
176 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
177 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
178 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700179
Arnaldo Carvalho de Meloe8e6d372015-08-10 16:53:54 -0300180 if (h->srcline)
181 hists__new_col_len(hists, HISTC_SRCLINE, strlen(h->srcline));
182
Andi Kleen31191a82015-08-07 15:54:24 -0700183 if (h->srcfile)
184 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
185
Andi Kleen475eeab2013-09-20 07:40:43 -0700186 if (h->transaction)
187 hists__new_col_len(hists, HISTC_TRANSACTION,
188 hist_entry__transaction_len());
Namhyung Kim0c0af782016-02-21 23:22:38 +0900189
190 if (h->trace_output)
191 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300192}
193
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900194void hists__output_recalc_col_len(struct hists *hists, int max_rows)
195{
196 struct rb_node *next = rb_first(&hists->entries);
197 struct hist_entry *n;
198 int row = 0;
199
200 hists__reset_col_len(hists);
201
202 while (next && row++ < max_rows) {
203 n = rb_entry(next, struct hist_entry, rb_node);
204 if (!n->filtered)
205 hists__calc_col_len(hists, n);
206 next = rb_next(&n->rb_node);
207 }
208}
209
Namhyung Kimf39056f2014-01-14 14:25:37 +0900210static void he_stat__add_cpumode_period(struct he_stat *he_stat,
211 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800212{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300213 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800214 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900215 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800216 break;
217 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900218 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800219 break;
220 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900221 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800222 break;
223 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900224 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800225 break;
226 default:
227 break;
228 }
229}
230
Andi Kleen05484292013-01-24 16:10:29 +0100231static void he_stat__add_period(struct he_stat *he_stat, u64 period,
232 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900233{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100234
Namhyung Kim139c0812012-10-04 21:49:43 +0900235 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100236 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900237 he_stat->nr_events += 1;
238}
239
240static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
241{
242 dest->period += src->period;
243 dest->period_sys += src->period_sys;
244 dest->period_us += src->period_us;
245 dest->period_guest_sys += src->period_guest_sys;
246 dest->period_guest_us += src->period_guest_us;
247 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100248 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900249}
250
Namhyung Kimf39056f2014-01-14 14:25:37 +0900251static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300252{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900253 he_stat->period = (he_stat->period * 7) / 8;
254 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100255 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300256}
257
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900258static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
259
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300260static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
261{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900262 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900263 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200264
265 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300266 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200267
Namhyung Kimf39056f2014-01-14 14:25:37 +0900268 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900269 if (symbol_conf.cumulate_callchain)
270 he_stat__decay(he->stat_acc);
Namhyung Kim42b276a2016-01-05 12:06:00 +0900271 decay_callchain(he->callchain);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200272
Namhyung Kim3186b682014-04-22 13:44:23 +0900273 diff = prev_period - he->stat.period;
274
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900275 if (!he->depth) {
276 hists->stats.total_period -= diff;
277 if (!he->filtered)
278 hists->stats.total_non_filtered_period -= diff;
279 }
280
281 if (!he->leaf) {
282 struct hist_entry *child;
283 struct rb_node *node = rb_first(&he->hroot_out);
284 while (node) {
285 child = rb_entry(node, struct hist_entry, rb_node);
286 node = rb_next(node);
287
288 if (hists__decay_entry(hists, child))
289 hists__delete_entry(hists, child);
290 }
291 }
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200292
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900293 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300294}
295
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300296static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
297{
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900298 struct rb_root *root_in;
299 struct rb_root *root_out;
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300300
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900301 if (he->parent_he) {
302 root_in = &he->parent_he->hroot_in;
303 root_out = &he->parent_he->hroot_out;
304 } else {
Jiri Olsa52225032016-05-03 13:54:42 +0200305 if (hists__has(hists, need_collapse))
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900306 root_in = &hists->entries_collapsed;
307 else
308 root_in = hists->entries_in;
309 root_out = &hists->entries;
310 }
311
312 rb_erase(&he->rb_node_in, root_in);
313 rb_erase(&he->rb_node, root_out);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300314
315 --hists->nr_entries;
316 if (!he->filtered)
317 --hists->nr_non_filtered_entries;
318
319 hist_entry__delete(he);
320}
321
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900322void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300323{
324 struct rb_node *next = rb_first(&hists->entries);
325 struct hist_entry *n;
326
327 while (next) {
328 n = rb_entry(next, struct hist_entry, rb_node);
329 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200330 if (((zap_user && n->level == '.') ||
331 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300332 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300333 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300334 }
335 }
336}
337
Namhyung Kim701937b2014-08-12 17:16:05 +0900338void hists__delete_entries(struct hists *hists)
339{
340 struct rb_node *next = rb_first(&hists->entries);
341 struct hist_entry *n;
342
343 while (next) {
344 n = rb_entry(next, struct hist_entry, rb_node);
345 next = rb_next(&n->rb_node);
346
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300347 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900348 }
349}
350
John Kacur3d1d07e2009-09-28 15:32:55 +0200351/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300352 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200353 */
354
Namhyung Kima0b51af2012-09-11 13:34:27 +0900355static struct hist_entry *hist_entry__new(struct hist_entry *template,
356 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300357{
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900358 size_t callchain_size = 0;
359 struct hist_entry *he;
360
Namhyung Kim82aa0192014-12-22 13:44:14 +0900361 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900362 callchain_size = sizeof(struct callchain_root);
363
364 he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300365
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200366 if (he != NULL) {
367 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900368
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900369 if (symbol_conf.cumulate_callchain) {
370 he->stat_acc = malloc(sizeof(he->stat));
371 if (he->stat_acc == NULL) {
372 free(he);
373 return NULL;
374 }
375 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
Namhyung Kima0b51af2012-09-11 13:34:27 +0900376 if (!sample_self)
377 memset(&he->stat, 0, sizeof(he->stat));
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900378 }
379
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300380 map__get(he->ms.map);
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100381
382 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900383 /*
384 * This branch info is (a part of) allocated from
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -0300385 * sample__resolve_bstack() and will be freed after
Namhyung Kim26353a62013-04-01 20:35:17 +0900386 * adding new entries. So we need to save a copy.
387 */
388 he->branch_info = malloc(sizeof(*he->branch_info));
389 if (he->branch_info == NULL) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300390 map__zput(he->ms.map);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900391 free(he->stat_acc);
Namhyung Kim26353a62013-04-01 20:35:17 +0900392 free(he);
393 return NULL;
394 }
395
396 memcpy(he->branch_info, template->branch_info,
397 sizeof(*he->branch_info));
398
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300399 map__get(he->branch_info->from.map);
400 map__get(he->branch_info->to.map);
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100401 }
402
Stephane Eranian98a3b322013-01-24 16:10:35 +0100403 if (he->mem_info) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300404 map__get(he->mem_info->iaddr.map);
405 map__get(he->mem_info->daddr.map);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100406 }
407
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300408 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200409 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200410
Namhyung Kim72392832015-12-24 11:16:17 +0900411 if (he->raw_data) {
412 he->raw_data = memdup(he->raw_data, he->raw_size);
413
414 if (he->raw_data == NULL) {
415 map__put(he->ms.map);
416 if (he->branch_info) {
417 map__put(he->branch_info->from.map);
418 map__put(he->branch_info->to.map);
419 free(he->branch_info);
420 }
421 if (he->mem_info) {
422 map__put(he->mem_info->iaddr.map);
423 map__put(he->mem_info->daddr.map);
424 }
425 free(he->stat_acc);
426 free(he);
427 return NULL;
428 }
429 }
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200430 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300431 thread__get(he->thread);
Namhyung Kimaef810e2016-02-25 00:13:34 +0900432
433 if (!symbol_conf.report_hierarchy)
434 he->leaf = true;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300435 }
436
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200437 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300438}
439
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300440static u8 symbol__parent_filter(const struct symbol *parent)
441{
442 if (symbol_conf.exclude_other && parent == NULL)
443 return 1 << HIST_FILTER__PARENT;
444 return 0;
445}
446
Namhyung Kim467ef102016-02-16 23:08:19 +0900447static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
448{
449 if (!symbol_conf.use_callchain)
450 return;
451
452 he->hists->callchain_period += period;
453 if (!he->filtered)
454 he->hists->callchain_non_filtered_period += period;
455}
456
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300457static struct hist_entry *hists__findnew_entry(struct hists *hists,
458 struct hist_entry *entry,
459 struct addr_location *al,
460 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300461{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300462 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300463 struct rb_node *parent = NULL;
464 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700465 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900466 u64 period = entry->stat.period;
467 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300468
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300469 p = &hists->entries_in->rb_node;
470
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300471 while (*p != NULL) {
472 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300473 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300474
Namhyung Kim9afcf932012-12-10 17:29:54 +0900475 /*
476 * Make sure that it receives arguments in a same order as
477 * hist_entry__collapse() so that we can use an appropriate
478 * function when searching an entry regardless which sort
479 * keys were used.
480 */
481 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300482
483 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900484 if (sample_self) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900485 he_stat__add_period(&he->stat, period, weight);
Namhyung Kim467ef102016-02-16 23:08:19 +0900486 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900487 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900488 if (symbol_conf.cumulate_callchain)
489 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400490
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900491 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300492 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900493 * and will not be used anymore.
494 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300495 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900496
David Miller63fa4712012-03-27 03:14:18 -0400497 /* If the map of an existing hist_entry has
498 * become out-of-date due to an exec() or
499 * similar, update it. Otherwise we will
500 * mis-adjust symbol addresses when computing
501 * the history counter to increment.
502 */
503 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300504 map__put(he->ms.map);
505 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400506 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300507 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300508 }
509
510 if (cmp < 0)
511 p = &(*p)->rb_left;
512 else
513 p = &(*p)->rb_right;
514 }
515
Namhyung Kima0b51af2012-09-11 13:34:27 +0900516 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300517 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900518 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300519
Namhyung Kim0f584742016-01-28 00:40:49 +0900520 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900521 hist_entry__add_callchain_period(he, period);
522 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900523
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300524 rb_link_node(&he->rb_node_in, parent, p);
525 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300526out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900527 if (sample_self)
528 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900529 if (symbol_conf.cumulate_callchain)
530 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300531 return he;
532}
533
Jiri Olsa0102ef32016-06-14 20:19:21 +0200534struct hist_entry *hists__add_entry(struct hists *hists,
535 struct addr_location *al,
536 struct symbol *sym_parent,
537 struct branch_info *bi,
538 struct mem_info *mi,
539 struct perf_sample *sample,
540 bool sample_self)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100541{
542 struct hist_entry entry = {
543 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900544 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100545 .ms = {
546 .map = al->map,
547 .sym = al->sym,
548 },
Kan Liang0c4c4de2015-09-04 10:45:42 -0400549 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400550 .cpu = al->cpu,
551 .cpumode = al->cpumode,
552 .ip = al->addr,
553 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900554 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900555 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900556 .period = sample->period,
557 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900558 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100559 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300560 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300561 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900562 .branch_info = bi,
563 .mem_info = mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900564 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900565 .raw_data = sample->raw_data,
566 .raw_size = sample->raw_size,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100567 };
568
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300569 return hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100570}
571
Namhyung Kim69bcb012013-10-30 09:40:34 +0900572static int
573iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
574 struct addr_location *al __maybe_unused)
575{
576 return 0;
577}
578
579static int
580iter_add_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_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
588{
589 struct perf_sample *sample = iter->sample;
590 struct mem_info *mi;
591
592 mi = sample__resolve_mem(sample, al);
593 if (mi == NULL)
594 return -ENOMEM;
595
596 iter->priv = mi;
597 return 0;
598}
599
600static int
601iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
602{
603 u64 cost;
604 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300605 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900606 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900607 struct hist_entry *he;
608
609 if (mi == NULL)
610 return -EINVAL;
611
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900612 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900613 if (!cost)
614 cost = 1;
615
616 /*
617 * must pass period=weight in order to get the correct
618 * sorting from hists__collapse_resort() which is solely
619 * based on periods. We want sorting be done on nr_events * weight
620 * and this is indirectly achieved by passing period=weight here
621 * and the he_stat__add_period() function.
622 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900623 sample->period = cost;
624
Jiri Olsa0102ef32016-06-14 20:19:21 +0200625 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
626 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900627 if (!he)
628 return -ENOMEM;
629
630 iter->he = he;
631 return 0;
632}
633
634static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900635iter_finish_mem_entry(struct hist_entry_iter *iter,
636 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900637{
638 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300639 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900640 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900641 int err = -EINVAL;
642
643 if (he == NULL)
644 goto out;
645
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300646 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900647
648 err = hist_entry__append_callchain(he, iter->sample);
649
650out:
651 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300652 * We don't need to free iter->priv (mem_info) here since the mem info
653 * was either already freed in hists__findnew_entry() or passed to a
654 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900655 */
656 iter->priv = NULL;
657
658 iter->he = NULL;
659 return err;
660}
661
662static int
663iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
664{
665 struct branch_info *bi;
666 struct perf_sample *sample = iter->sample;
667
668 bi = sample__resolve_bstack(sample, al);
669 if (!bi)
670 return -ENOMEM;
671
672 iter->curr = 0;
673 iter->total = sample->branch_stack->nr;
674
675 iter->priv = bi;
676 return 0;
677}
678
679static int
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -0300680iter_add_single_branch_entry(struct hist_entry_iter *iter,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900681 struct addr_location *al __maybe_unused)
682{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900683 /* to avoid calling callback function */
684 iter->he = NULL;
685
Namhyung Kim69bcb012013-10-30 09:40:34 +0900686 return 0;
687}
688
689static int
690iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
691{
692 struct branch_info *bi = iter->priv;
693 int i = iter->curr;
694
695 if (bi == NULL)
696 return 0;
697
698 if (iter->curr >= iter->total)
699 return 0;
700
701 al->map = bi[i].to.map;
702 al->sym = bi[i].to.sym;
703 al->addr = bi[i].to.addr;
704 return 1;
705}
706
707static int
708iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
709{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900710 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900711 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300712 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900713 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900714 struct hist_entry *he = NULL;
715 int i = iter->curr;
716 int err = 0;
717
718 bi = iter->priv;
719
720 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
721 goto out;
722
723 /*
724 * The report shows the percentage of total branches captured
725 * and not events sampled. Thus we use a pseudo period of 1.
726 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900727 sample->period = 1;
728 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
729
Jiri Olsa0102ef32016-06-14 20:19:21 +0200730 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
731 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900732 if (he == NULL)
733 return -ENOMEM;
734
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300735 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900736
737out:
738 iter->he = he;
739 iter->curr++;
740 return err;
741}
742
743static int
744iter_finish_branch_entry(struct hist_entry_iter *iter,
745 struct addr_location *al __maybe_unused)
746{
747 zfree(&iter->priv);
748 iter->he = NULL;
749
750 return iter->curr >= iter->total ? 0 : -1;
751}
752
753static int
754iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
755 struct addr_location *al __maybe_unused)
756{
757 return 0;
758}
759
760static int
761iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
762{
763 struct perf_evsel *evsel = iter->evsel;
764 struct perf_sample *sample = iter->sample;
765 struct hist_entry *he;
766
Jiri Olsa0102ef32016-06-14 20:19:21 +0200767 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
768 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900769 if (he == NULL)
770 return -ENOMEM;
771
772 iter->he = he;
773 return 0;
774}
775
776static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900777iter_finish_normal_entry(struct hist_entry_iter *iter,
778 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900779{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900780 struct hist_entry *he = iter->he;
781 struct perf_evsel *evsel = iter->evsel;
782 struct perf_sample *sample = iter->sample;
783
784 if (he == NULL)
785 return 0;
786
787 iter->he = NULL;
788
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300789 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900790
791 return hist_entry__append_callchain(he, sample);
792}
793
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900794static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300795iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900796 struct addr_location *al __maybe_unused)
797{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900798 struct hist_entry **he_cache;
799
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900800 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900801
802 /*
803 * This is for detecting cycles or recursions so that they're
804 * cumulated only one time to prevent entries more than 100%
805 * overhead.
806 */
Adrian Hunter96b40f32015-09-25 16:15:47 +0300807 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900808 if (he_cache == NULL)
809 return -ENOMEM;
810
811 iter->priv = he_cache;
812 iter->curr = 0;
813
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900814 return 0;
815}
816
817static int
818iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
819 struct addr_location *al)
820{
821 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300822 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900823 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900824 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900825 struct hist_entry *he;
826 int err = 0;
827
Jiri Olsa0102ef32016-06-14 20:19:21 +0200828 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
829 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900830 if (he == NULL)
831 return -ENOMEM;
832
833 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900834 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900835
Namhyung Kim82aa0192014-12-22 13:44:14 +0900836 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900837
838 /*
839 * We need to re-initialize the cursor since callchain_append()
840 * advanced the cursor to the end.
841 */
842 callchain_cursor_commit(&callchain_cursor);
843
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300844 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900845
846 return err;
847}
848
849static int
850iter_next_cumulative_entry(struct hist_entry_iter *iter,
851 struct addr_location *al)
852{
853 struct callchain_cursor_node *node;
854
855 node = callchain_cursor_current(&callchain_cursor);
856 if (node == NULL)
857 return 0;
858
Namhyung Kimc7405d82013-10-31 13:58:30 +0900859 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900860}
861
862static int
863iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
864 struct addr_location *al)
865{
866 struct perf_evsel *evsel = iter->evsel;
867 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900868 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900869 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900870 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -0300871 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900872 .cpu = al->cpu,
873 .thread = al->thread,
874 .comm = thread__comm(al->thread),
875 .ip = al->addr,
876 .ms = {
877 .map = al->map,
878 .sym = al->sym,
879 },
880 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +0900881 .raw_data = sample->raw_data,
882 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900883 };
884 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900885 struct callchain_cursor cursor;
886
887 callchain_cursor_snapshot(&cursor, &callchain_cursor);
888
889 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900890
891 /*
892 * Check if there's duplicate entries in the callchain.
893 * It's possible that it has cycles or recursive calls.
894 */
895 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900896 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
897 /* to avoid calling callback function */
898 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900899 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900900 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900901 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900902
Jiri Olsa0102ef32016-06-14 20:19:21 +0200903 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
904 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900905 if (he == NULL)
906 return -ENOMEM;
907
908 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900909 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900910
Namhyung Kim82aa0192014-12-22 13:44:14 +0900911 if (symbol_conf.use_callchain)
912 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900913 return 0;
914}
915
916static int
917iter_finish_cumulative_entry(struct hist_entry_iter *iter,
918 struct addr_location *al __maybe_unused)
919{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900920 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900921 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900922
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900923 return 0;
924}
925
Namhyung Kim69bcb012013-10-30 09:40:34 +0900926const struct hist_iter_ops hist_iter_mem = {
927 .prepare_entry = iter_prepare_mem_entry,
928 .add_single_entry = iter_add_single_mem_entry,
929 .next_entry = iter_next_nop_entry,
930 .add_next_entry = iter_add_next_nop_entry,
931 .finish_entry = iter_finish_mem_entry,
932};
933
934const struct hist_iter_ops hist_iter_branch = {
935 .prepare_entry = iter_prepare_branch_entry,
936 .add_single_entry = iter_add_single_branch_entry,
937 .next_entry = iter_next_branch_entry,
938 .add_next_entry = iter_add_next_branch_entry,
939 .finish_entry = iter_finish_branch_entry,
940};
941
942const struct hist_iter_ops hist_iter_normal = {
943 .prepare_entry = iter_prepare_normal_entry,
944 .add_single_entry = iter_add_single_normal_entry,
945 .next_entry = iter_next_nop_entry,
946 .add_next_entry = iter_add_next_nop_entry,
947 .finish_entry = iter_finish_normal_entry,
948};
949
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900950const struct hist_iter_ops hist_iter_cumulative = {
951 .prepare_entry = iter_prepare_cumulative_entry,
952 .add_single_entry = iter_add_single_cumulative_entry,
953 .next_entry = iter_next_cumulative_entry,
954 .add_next_entry = iter_add_next_cumulative_entry,
955 .finish_entry = iter_finish_cumulative_entry,
956};
957
Namhyung Kim69bcb012013-10-30 09:40:34 +0900958int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900959 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900960{
961 int err, err2;
962
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300963 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
Namhyung Kim063bd932015-05-19 17:04:10 +0900964 iter->evsel, al, max_stack_depth);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900965 if (err)
966 return err;
967
Adrian Hunter96b40f32015-09-25 16:15:47 +0300968 iter->max_stack = max_stack_depth;
969
Namhyung Kim69bcb012013-10-30 09:40:34 +0900970 err = iter->ops->prepare_entry(iter, al);
971 if (err)
972 goto out;
973
974 err = iter->ops->add_single_entry(iter, al);
975 if (err)
976 goto out;
977
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900978 if (iter->he && iter->add_entry_cb) {
979 err = iter->add_entry_cb(iter, al, true, arg);
980 if (err)
981 goto out;
982 }
983
Namhyung Kim69bcb012013-10-30 09:40:34 +0900984 while (iter->ops->next_entry(iter, al)) {
985 err = iter->ops->add_next_entry(iter, al);
986 if (err)
987 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900988
989 if (iter->he && iter->add_entry_cb) {
990 err = iter->add_entry_cb(iter, al, false, arg);
991 if (err)
992 goto out;
993 }
Namhyung Kim69bcb012013-10-30 09:40:34 +0900994 }
995
996out:
997 err2 = iter->ops->finish_entry(iter, al);
998 if (!err)
999 err = err2;
1000
1001 return err;
1002}
1003
John Kacur3d1d07e2009-09-28 15:32:55 +02001004int64_t
1005hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1006{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001007 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001008 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001009 int64_t cmp = 0;
1010
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001011 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001012 if (perf_hpp__is_dynamic_entry(fmt) &&
1013 !perf_hpp__defined_dynamic_entry(fmt, hists))
1014 continue;
1015
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001016 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001017 if (cmp)
1018 break;
1019 }
1020
1021 return cmp;
1022}
1023
1024int64_t
1025hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1026{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001027 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001028 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001029 int64_t cmp = 0;
1030
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001031 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001032 if (perf_hpp__is_dynamic_entry(fmt) &&
1033 !perf_hpp__defined_dynamic_entry(fmt, hists))
1034 continue;
1035
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001036 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001037 if (cmp)
1038 break;
1039 }
1040
1041 return cmp;
1042}
1043
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001044void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001045{
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001046 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001047 map__zput(he->ms.map);
1048
1049 if (he->branch_info) {
1050 map__zput(he->branch_info->from.map);
1051 map__zput(he->branch_info->to.map);
Andi Kleen508be0d2016-05-20 13:15:08 -07001052 free_srcline(he->branch_info->srcline_from);
1053 free_srcline(he->branch_info->srcline_to);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001054 zfree(&he->branch_info);
1055 }
1056
1057 if (he->mem_info) {
1058 map__zput(he->mem_info->iaddr.map);
1059 map__zput(he->mem_info->daddr.map);
1060 zfree(&he->mem_info);
1061 }
1062
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001063 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +09001064 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -07001065 if (he->srcfile && he->srcfile[0])
1066 free(he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001067 free_callchain(he->callchain);
Namhyung Kim60517d22015-12-23 02:07:03 +09001068 free(he->trace_output);
Namhyung Kim72392832015-12-24 11:16:17 +09001069 free(he->raw_data);
John Kacur3d1d07e2009-09-28 15:32:55 +02001070 free(he);
1071}
1072
1073/*
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001074 * If this is not the last column, then we need to pad it according to the
1075 * pre-calculated max lenght for this column, otherwise don't bother adding
1076 * spaces because that would break viewing this with, for instance, 'less',
1077 * that would show tons of trailing spaces when a long C++ demangled method
1078 * names is sampled.
1079*/
1080int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1081 struct perf_hpp_fmt *fmt, int printed)
1082{
1083 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
Jiri Olsada1b0402016-06-14 20:19:20 +02001084 const int width = fmt->width(fmt, hpp, he->hists);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001085 if (printed < width) {
1086 advance_hpp(hpp, printed);
1087 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1088 }
1089 }
1090
1091 return printed;
1092}
1093
1094/*
John Kacur3d1d07e2009-09-28 15:32:55 +02001095 * collapse the histogram
1096 */
1097
Namhyung Kimaef810e2016-02-25 00:13:34 +09001098static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
Namhyung Kimaec13a72016-03-09 22:46:58 +09001099static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1100 enum hist_filter type);
1101
1102typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1103
1104static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1105{
1106 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1107}
1108
1109static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1110 enum hist_filter type,
1111 fmt_chk_fn check)
1112{
1113 struct perf_hpp_fmt *fmt;
1114 bool type_match = false;
1115 struct hist_entry *parent = he->parent_he;
1116
1117 switch (type) {
1118 case HIST_FILTER__THREAD:
1119 if (symbol_conf.comm_list == NULL &&
1120 symbol_conf.pid_list == NULL &&
1121 symbol_conf.tid_list == NULL)
1122 return;
1123 break;
1124 case HIST_FILTER__DSO:
1125 if (symbol_conf.dso_list == NULL)
1126 return;
1127 break;
1128 case HIST_FILTER__SYMBOL:
1129 if (symbol_conf.sym_list == NULL)
1130 return;
1131 break;
1132 case HIST_FILTER__PARENT:
1133 case HIST_FILTER__GUEST:
1134 case HIST_FILTER__HOST:
1135 case HIST_FILTER__SOCKET:
1136 default:
1137 return;
1138 }
1139
1140 /* if it's filtered by own fmt, it has to have filter bits */
1141 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1142 if (check(fmt)) {
1143 type_match = true;
1144 break;
1145 }
1146 }
1147
1148 if (type_match) {
1149 /*
1150 * If the filter is for current level entry, propagate
1151 * filter marker to parents. The marker bit was
1152 * already set by default so it only needs to clear
1153 * non-filtered entries.
1154 */
1155 if (!(he->filtered & (1 << type))) {
1156 while (parent) {
1157 parent->filtered &= ~(1 << type);
1158 parent = parent->parent_he;
1159 }
1160 }
1161 } else {
1162 /*
1163 * If current entry doesn't have matching formats, set
1164 * filter marker for upper level entries. it will be
1165 * cleared if its lower level entries is not filtered.
1166 *
1167 * For lower-level entries, it inherits parent's
1168 * filter bit so that lower level entries of a
1169 * non-filtered entry won't set the filter marker.
1170 */
1171 if (parent == NULL)
1172 he->filtered |= (1 << type);
1173 else
1174 he->filtered |= (parent->filtered & (1 << type));
1175 }
1176}
1177
1178static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1179{
1180 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1181 check_thread_entry);
1182
1183 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1184 perf_hpp__is_dso_entry);
1185
1186 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1187 perf_hpp__is_sym_entry);
1188
1189 hists__apply_filters(he->hists, he);
1190}
Namhyung Kimaef810e2016-02-25 00:13:34 +09001191
1192static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1193 struct rb_root *root,
1194 struct hist_entry *he,
Namhyung Kimaec13a72016-03-09 22:46:58 +09001195 struct hist_entry *parent_he,
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001196 struct perf_hpp_list *hpp_list)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001197{
1198 struct rb_node **p = &root->rb_node;
1199 struct rb_node *parent = NULL;
1200 struct hist_entry *iter, *new;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001201 struct perf_hpp_fmt *fmt;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001202 int64_t cmp;
1203
1204 while (*p != NULL) {
1205 parent = *p;
1206 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1207
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001208 cmp = 0;
1209 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1210 cmp = fmt->collapse(fmt, iter, he);
1211 if (cmp)
1212 break;
1213 }
1214
Namhyung Kimaef810e2016-02-25 00:13:34 +09001215 if (!cmp) {
1216 he_stat__add_stat(&iter->stat, &he->stat);
1217 return iter;
1218 }
1219
1220 if (cmp < 0)
1221 p = &parent->rb_left;
1222 else
1223 p = &parent->rb_right;
1224 }
1225
1226 new = hist_entry__new(he, true);
1227 if (new == NULL)
1228 return NULL;
1229
Namhyung Kimaef810e2016-02-25 00:13:34 +09001230 hists->nr_entries++;
1231
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001232 /* save related format list for output */
1233 new->hpp_list = hpp_list;
Namhyung Kimaec13a72016-03-09 22:46:58 +09001234 new->parent_he = parent_he;
1235
1236 hist_entry__apply_hierarchy_filters(new);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001237
1238 /* some fields are now passed to 'new' */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001239 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1240 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1241 he->trace_output = NULL;
1242 else
1243 new->trace_output = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001244
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001245 if (perf_hpp__is_srcline_entry(fmt))
1246 he->srcline = NULL;
1247 else
1248 new->srcline = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001249
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001250 if (perf_hpp__is_srcfile_entry(fmt))
1251 he->srcfile = NULL;
1252 else
1253 new->srcfile = NULL;
1254 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001255
1256 rb_link_node(&new->rb_node_in, parent, p);
1257 rb_insert_color(&new->rb_node_in, root);
1258 return new;
1259}
1260
1261static int hists__hierarchy_insert_entry(struct hists *hists,
1262 struct rb_root *root,
1263 struct hist_entry *he)
1264{
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001265 struct perf_hpp_list_node *node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001266 struct hist_entry *new_he = NULL;
1267 struct hist_entry *parent = NULL;
1268 int depth = 0;
1269 int ret = 0;
1270
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001271 list_for_each_entry(node, &hists->hpp_formats, list) {
1272 /* skip period (overhead) and elided columns */
1273 if (node->level == 0 || node->skip)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001274 continue;
1275
1276 /* insert copy of 'he' for each fmt into the hierarchy */
Namhyung Kimaec13a72016-03-09 22:46:58 +09001277 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001278 if (new_he == NULL) {
1279 ret = -1;
1280 break;
1281 }
1282
1283 root = &new_he->hroot_in;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001284 new_he->depth = depth++;
1285 parent = new_he;
1286 }
1287
1288 if (new_he) {
1289 new_he->leaf = true;
1290
1291 if (symbol_conf.use_callchain) {
1292 callchain_cursor_reset(&callchain_cursor);
1293 if (callchain_merge(&callchain_cursor,
1294 new_he->callchain,
1295 he->callchain) < 0)
1296 ret = -1;
1297 }
1298 }
1299
1300 /* 'he' is no longer used */
1301 hist_entry__delete(he);
1302
1303 /* return 0 (or -1) since it already applied filters */
1304 return ret;
1305}
1306
Jiri Olsa592dac62016-03-24 13:52:17 +01001307static int hists__collapse_insert_entry(struct hists *hists,
1308 struct rb_root *root,
1309 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001310{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001311 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001312 struct rb_node *parent = NULL;
1313 struct hist_entry *iter;
1314 int64_t cmp;
1315
Namhyung Kimaef810e2016-02-25 00:13:34 +09001316 if (symbol_conf.report_hierarchy)
1317 return hists__hierarchy_insert_entry(hists, root, he);
1318
John Kacur3d1d07e2009-09-28 15:32:55 +02001319 while (*p != NULL) {
1320 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001321 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001322
1323 cmp = hist_entry__collapse(iter, he);
1324
1325 if (!cmp) {
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001326 int ret = 0;
1327
Namhyung Kim139c0812012-10-04 21:49:43 +09001328 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001329 if (symbol_conf.cumulate_callchain)
1330 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001331
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001332 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001333 callchain_cursor_reset(&callchain_cursor);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001334 if (callchain_merge(&callchain_cursor,
1335 iter->callchain,
1336 he->callchain) < 0)
1337 ret = -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001338 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001339 hist_entry__delete(he);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001340 return ret;
John Kacur3d1d07e2009-09-28 15:32:55 +02001341 }
1342
1343 if (cmp < 0)
1344 p = &(*p)->rb_left;
1345 else
1346 p = &(*p)->rb_right;
1347 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001348 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001349
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001350 rb_link_node(&he->rb_node_in, parent, p);
1351 rb_insert_color(&he->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001352 return 1;
John Kacur3d1d07e2009-09-28 15:32:55 +02001353}
1354
Namhyung Kimfc284be2016-01-07 10:14:10 +01001355struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001356{
1357 struct rb_root *root;
1358
1359 pthread_mutex_lock(&hists->lock);
1360
1361 root = hists->entries_in;
1362 if (++hists->entries_in > &hists->entries_in_array[1])
1363 hists->entries_in = &hists->entries_in_array[0];
1364
1365 pthread_mutex_unlock(&hists->lock);
1366
1367 return root;
1368}
1369
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001370static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1371{
1372 hists__filter_entry_by_dso(hists, he);
1373 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001374 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001375 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001376}
1377
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001378int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001379{
1380 struct rb_root *root;
1381 struct rb_node *next;
1382 struct hist_entry *n;
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001383 int ret;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001384
Jiri Olsa52225032016-05-03 13:54:42 +02001385 if (!hists__has(hists, need_collapse))
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001386 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001387
Namhyung Kim740b97f2014-12-22 13:44:10 +09001388 hists->nr_entries = 0;
1389
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001390 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001391
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001392 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001393
1394 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001395 if (session_done())
1396 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001397 n = rb_entry(next, struct hist_entry, rb_node_in);
1398 next = rb_next(&n->rb_node_in);
1399
1400 rb_erase(&n->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001401 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1402 if (ret < 0)
1403 return -1;
1404
1405 if (ret) {
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001406 /*
1407 * If it wasn't combined with one of the entries already
1408 * collapsed, we need to apply the filters that may have
1409 * been set by, say, the hist_browser.
1410 */
1411 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001412 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001413 if (prog)
1414 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001415 }
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001416 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001417}
1418
Namhyung Kim043ca3892014-03-03 14:18:00 +09001419static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001420{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001421 struct hists *hists = a->hists;
Namhyung Kim043ca3892014-03-03 14:18:00 +09001422 struct perf_hpp_fmt *fmt;
1423 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001424
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001425 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001426 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001427 continue;
1428
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001429 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001430 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001431 break;
1432 }
1433
Namhyung Kim043ca3892014-03-03 14:18:00 +09001434 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001435}
1436
Namhyung Kim9283ba92014-04-24 16:37:26 +09001437static void hists__reset_filter_stats(struct hists *hists)
1438{
1439 hists->nr_non_filtered_entries = 0;
1440 hists->stats.total_non_filtered_period = 0;
1441}
1442
1443void hists__reset_stats(struct hists *hists)
1444{
1445 hists->nr_entries = 0;
1446 hists->stats.total_period = 0;
1447
1448 hists__reset_filter_stats(hists);
1449}
1450
1451static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1452{
1453 hists->nr_non_filtered_entries++;
1454 hists->stats.total_non_filtered_period += h->stat.period;
1455}
1456
1457void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1458{
1459 if (!h->filtered)
1460 hists__inc_filter_stats(hists, h);
1461
1462 hists->nr_entries++;
1463 hists->stats.total_period += h->stat.period;
1464}
1465
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001466static void hierarchy_recalc_total_periods(struct hists *hists)
1467{
1468 struct rb_node *node;
1469 struct hist_entry *he;
1470
1471 node = rb_first(&hists->entries);
1472
1473 hists->stats.total_period = 0;
1474 hists->stats.total_non_filtered_period = 0;
1475
1476 /*
1477 * recalculate total period using top-level entries only
1478 * since lower level entries only see non-filtered entries
1479 * but upper level entries have sum of both entries.
1480 */
1481 while (node) {
1482 he = rb_entry(node, struct hist_entry, rb_node);
1483 node = rb_next(node);
1484
1485 hists->stats.total_period += he->stat.period;
1486 if (!he->filtered)
1487 hists->stats.total_non_filtered_period += he->stat.period;
1488 }
1489}
1490
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001491static void hierarchy_insert_output_entry(struct rb_root *root,
1492 struct hist_entry *he)
1493{
1494 struct rb_node **p = &root->rb_node;
1495 struct rb_node *parent = NULL;
1496 struct hist_entry *iter;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001497 struct perf_hpp_fmt *fmt;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001498
1499 while (*p != NULL) {
1500 parent = *p;
1501 iter = rb_entry(parent, struct hist_entry, rb_node);
1502
1503 if (hist_entry__sort(he, iter) > 0)
1504 p = &parent->rb_left;
1505 else
1506 p = &parent->rb_right;
1507 }
1508
1509 rb_link_node(&he->rb_node, parent, p);
1510 rb_insert_color(&he->rb_node, root);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001511
1512 /* update column width of dynamic entry */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001513 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1514 if (perf_hpp__is_dynamic_entry(fmt))
1515 fmt->sort(fmt, he, NULL);
1516 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001517}
1518
1519static void hists__hierarchy_output_resort(struct hists *hists,
1520 struct ui_progress *prog,
1521 struct rb_root *root_in,
1522 struct rb_root *root_out,
1523 u64 min_callchain_hits,
1524 bool use_callchain)
1525{
1526 struct rb_node *node;
1527 struct hist_entry *he;
1528
1529 *root_out = RB_ROOT;
1530 node = rb_first(root_in);
1531
1532 while (node) {
1533 he = rb_entry(node, struct hist_entry, rb_node_in);
1534 node = rb_next(node);
1535
1536 hierarchy_insert_output_entry(root_out, he);
1537
1538 if (prog)
1539 ui_progress__update(prog, 1);
1540
1541 if (!he->leaf) {
1542 hists__hierarchy_output_resort(hists, prog,
1543 &he->hroot_in,
1544 &he->hroot_out,
1545 min_callchain_hits,
1546 use_callchain);
1547 hists->nr_entries++;
1548 if (!he->filtered) {
1549 hists->nr_non_filtered_entries++;
1550 hists__calc_col_len(hists, he);
1551 }
1552
1553 continue;
1554 }
1555
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001556 if (!use_callchain)
1557 continue;
1558
1559 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1560 u64 total = he->stat.period;
1561
1562 if (symbol_conf.cumulate_callchain)
1563 total = he->stat_acc->period;
1564
1565 min_callchain_hits = total * (callchain_param.min_percent / 100);
1566 }
1567
1568 callchain_param.sort(&he->sorted_chain, he->callchain,
1569 min_callchain_hits, &callchain_param);
1570 }
1571}
1572
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001573static void __hists__insert_output_entry(struct rb_root *entries,
1574 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001575 u64 min_callchain_hits,
1576 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001577{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001578 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001579 struct rb_node *parent = NULL;
1580 struct hist_entry *iter;
Namhyung Kimabab5e72016-02-27 03:52:47 +09001581 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001582
Namhyung Kim744070e2016-01-28 00:40:48 +09001583 if (use_callchain) {
1584 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1585 u64 total = he->stat.period;
1586
1587 if (symbol_conf.cumulate_callchain)
1588 total = he->stat_acc->period;
1589
1590 min_callchain_hits = total * (callchain_param.min_percent / 100);
1591 }
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001592 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001593 min_callchain_hits, &callchain_param);
Namhyung Kim744070e2016-01-28 00:40:48 +09001594 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001595
1596 while (*p != NULL) {
1597 parent = *p;
1598 iter = rb_entry(parent, struct hist_entry, rb_node);
1599
Namhyung Kim043ca3892014-03-03 14:18:00 +09001600 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001601 p = &(*p)->rb_left;
1602 else
1603 p = &(*p)->rb_right;
1604 }
1605
1606 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001607 rb_insert_color(&he->rb_node, entries);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001608
1609 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1610 if (perf_hpp__is_dynamic_entry(fmt) &&
1611 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1612 fmt->sort(fmt, he, NULL); /* update column width */
1613 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001614}
1615
Jiri Olsa01441af2016-01-18 10:23:59 +01001616static void output_resort(struct hists *hists, struct ui_progress *prog,
1617 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001618{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001619 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001620 struct rb_node *next;
1621 struct hist_entry *n;
Namhyung Kim467ef102016-02-16 23:08:19 +09001622 u64 callchain_total;
John Kacur3d1d07e2009-09-28 15:32:55 +02001623 u64 min_callchain_hits;
1624
Namhyung Kim467ef102016-02-16 23:08:19 +09001625 callchain_total = hists->callchain_period;
1626 if (symbol_conf.filter_relative)
1627 callchain_total = hists->callchain_non_filtered_period;
1628
1629 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001630
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001631 hists__reset_stats(hists);
1632 hists__reset_col_len(hists);
1633
1634 if (symbol_conf.report_hierarchy) {
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001635 hists__hierarchy_output_resort(hists, prog,
1636 &hists->entries_collapsed,
1637 &hists->entries,
1638 min_callchain_hits,
1639 use_callchain);
1640 hierarchy_recalc_total_periods(hists);
1641 return;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001642 }
1643
Jiri Olsa52225032016-05-03 13:54:42 +02001644 if (hists__has(hists, need_collapse))
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001645 root = &hists->entries_collapsed;
1646 else
1647 root = hists->entries_in;
1648
1649 next = rb_first(root);
1650 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001651
1652 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001653 n = rb_entry(next, struct hist_entry, rb_node_in);
1654 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001655
Kan Liangf9db0d02015-08-11 06:30:48 -04001656 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001657 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001658
1659 if (!n->filtered)
1660 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001661
1662 if (prog)
1663 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001664 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001665}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001666
Jiri Olsa452ce032016-01-18 10:24:00 +01001667void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
Jiri Olsa01441af2016-01-18 10:23:59 +01001668{
Jiri Olsa01441af2016-01-18 10:23:59 +01001669 bool use_callchain;
1670
1671 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1672 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1673 else
1674 use_callchain = symbol_conf.use_callchain;
1675
Jiri Olsa452ce032016-01-18 10:24:00 +01001676 output_resort(evsel__hists(evsel), prog, use_callchain);
1677}
1678
1679void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1680{
1681 output_resort(hists, prog, symbol_conf.use_callchain);
Jiri Olsa01441af2016-01-18 10:23:59 +01001682}
1683
Namhyung Kim8c018722016-02-25 00:13:36 +09001684static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1685{
1686 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1687 return false;
1688
1689 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1690 return true;
1691
1692 return false;
1693}
1694
1695struct rb_node *rb_hierarchy_last(struct rb_node *node)
1696{
1697 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1698
1699 while (can_goto_child(he, HMD_NORMAL)) {
1700 node = rb_last(&he->hroot_out);
1701 he = rb_entry(node, struct hist_entry, rb_node);
1702 }
1703 return node;
1704}
1705
1706struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1707{
1708 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1709
1710 if (can_goto_child(he, hmd))
1711 node = rb_first(&he->hroot_out);
1712 else
1713 node = rb_next(node);
1714
1715 while (node == NULL) {
1716 he = he->parent_he;
1717 if (he == NULL)
1718 break;
1719
1720 node = rb_next(&he->rb_node);
1721 }
1722 return node;
1723}
1724
1725struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1726{
1727 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1728
1729 node = rb_prev(node);
1730 if (node)
1731 return rb_hierarchy_last(node);
1732
1733 he = he->parent_he;
1734 if (he == NULL)
1735 return NULL;
1736
1737 return &he->rb_node;
1738}
1739
Namhyung Kima7b58952016-02-26 21:13:16 +09001740bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1741{
1742 struct rb_node *node;
1743 struct hist_entry *child;
1744 float percent;
1745
1746 if (he->leaf)
1747 return false;
1748
1749 node = rb_first(&he->hroot_out);
1750 child = rb_entry(node, struct hist_entry, rb_node);
1751
1752 while (node && child->filtered) {
1753 node = rb_next(node);
1754 child = rb_entry(node, struct hist_entry, rb_node);
1755 }
1756
1757 if (node)
1758 percent = hist_entry__get_percent_limit(child);
1759 else
1760 percent = 0;
1761
1762 return node && percent >= limit;
1763}
1764
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001765static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001766 enum hist_filter filter)
1767{
1768 h->filtered &= ~(1 << filter);
Namhyung Kim155e9af2016-02-25 00:13:38 +09001769
1770 if (symbol_conf.report_hierarchy) {
1771 struct hist_entry *parent = h->parent_he;
1772
1773 while (parent) {
1774 he_stat__add_stat(&parent->stat, &h->stat);
1775
1776 parent->filtered &= ~(1 << filter);
1777
1778 if (parent->filtered)
1779 goto next;
1780
1781 /* force fold unfiltered entry for simplicity */
1782 parent->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001783 parent->has_no_entry = false;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001784 parent->row_offset = 0;
1785 parent->nr_rows = 0;
1786next:
1787 parent = parent->parent_he;
1788 }
1789 }
1790
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001791 if (h->filtered)
1792 return;
1793
Namhyung Kim87e90f42014-04-24 16:44:16 +09001794 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001795 h->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001796 h->has_no_entry = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001797 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001798 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001799
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001800 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001801
Namhyung Kim9283ba92014-04-24 16:37:26 +09001802 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001803 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001804}
1805
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001806
1807static bool hists__filter_entry_by_dso(struct hists *hists,
1808 struct hist_entry *he)
1809{
1810 if (hists->dso_filter != NULL &&
1811 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1812 he->filtered |= (1 << HIST_FILTER__DSO);
1813 return true;
1814 }
1815
1816 return false;
1817}
1818
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001819static bool hists__filter_entry_by_thread(struct hists *hists,
1820 struct hist_entry *he)
1821{
1822 if (hists->thread_filter != NULL &&
1823 he->thread != hists->thread_filter) {
1824 he->filtered |= (1 << HIST_FILTER__THREAD);
1825 return true;
1826 }
1827
1828 return false;
1829}
1830
Namhyung Kime94d53e2012-03-16 17:50:51 +09001831static bool hists__filter_entry_by_symbol(struct hists *hists,
1832 struct hist_entry *he)
1833{
1834 if (hists->symbol_filter_str != NULL &&
1835 (!he->ms.sym || strstr(he->ms.sym->name,
1836 hists->symbol_filter_str) == NULL)) {
1837 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1838 return true;
1839 }
1840
1841 return false;
1842}
1843
Kan Liang21394d92015-09-04 10:45:44 -04001844static bool hists__filter_entry_by_socket(struct hists *hists,
1845 struct hist_entry *he)
1846{
1847 if ((hists->socket_filter > -1) &&
1848 (he->socket != hists->socket_filter)) {
1849 he->filtered |= (1 << HIST_FILTER__SOCKET);
1850 return true;
1851 }
1852
1853 return false;
1854}
1855
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001856typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1857
1858static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04001859{
1860 struct rb_node *nd;
1861
1862 hists->stats.nr_non_filtered_samples = 0;
1863
1864 hists__reset_filter_stats(hists);
1865 hists__reset_col_len(hists);
1866
1867 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1868 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1869
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001870 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04001871 continue;
1872
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001873 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04001874 }
1875}
1876
Namhyung Kim70642852016-02-25 00:13:39 +09001877static void resort_filtered_entry(struct rb_root *root, struct hist_entry *he)
1878{
1879 struct rb_node **p = &root->rb_node;
1880 struct rb_node *parent = NULL;
1881 struct hist_entry *iter;
1882 struct rb_root new_root = RB_ROOT;
1883 struct rb_node *nd;
1884
1885 while (*p != NULL) {
1886 parent = *p;
1887 iter = rb_entry(parent, struct hist_entry, rb_node);
1888
1889 if (hist_entry__sort(he, iter) > 0)
1890 p = &(*p)->rb_left;
1891 else
1892 p = &(*p)->rb_right;
1893 }
1894
1895 rb_link_node(&he->rb_node, parent, p);
1896 rb_insert_color(&he->rb_node, root);
1897
1898 if (he->leaf || he->filtered)
1899 return;
1900
1901 nd = rb_first(&he->hroot_out);
1902 while (nd) {
1903 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1904
1905 nd = rb_next(nd);
1906 rb_erase(&h->rb_node, &he->hroot_out);
1907
1908 resort_filtered_entry(&new_root, h);
1909 }
1910
1911 he->hroot_out = new_root;
1912}
1913
Namhyung Kim155e9af2016-02-25 00:13:38 +09001914static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
1915{
1916 struct rb_node *nd;
Namhyung Kim70642852016-02-25 00:13:39 +09001917 struct rb_root new_root = RB_ROOT;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001918
1919 hists->stats.nr_non_filtered_samples = 0;
1920
1921 hists__reset_filter_stats(hists);
1922 hists__reset_col_len(hists);
1923
1924 nd = rb_first(&hists->entries);
1925 while (nd) {
1926 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1927 int ret;
1928
1929 ret = hist_entry__filter(h, type, arg);
1930
1931 /*
1932 * case 1. non-matching type
1933 * zero out the period, set filter marker and move to child
1934 */
1935 if (ret < 0) {
1936 memset(&h->stat, 0, sizeof(h->stat));
1937 h->filtered |= (1 << type);
1938
1939 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
1940 }
1941 /*
1942 * case 2. matched type (filter out)
1943 * set filter marker and move to next
1944 */
1945 else if (ret == 1) {
1946 h->filtered |= (1 << type);
1947
1948 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
1949 }
1950 /*
1951 * case 3. ok (not filtered)
1952 * add period to hists and parents, erase the filter marker
1953 * and move to next sibling
1954 */
1955 else {
1956 hists__remove_entry_filter(hists, h, type);
1957
1958 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
1959 }
1960 }
Namhyung Kim70642852016-02-25 00:13:39 +09001961
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001962 hierarchy_recalc_total_periods(hists);
1963
Namhyung Kim70642852016-02-25 00:13:39 +09001964 /*
1965 * resort output after applying a new filter since filter in a lower
1966 * hierarchy can change periods in a upper hierarchy.
1967 */
1968 nd = rb_first(&hists->entries);
1969 while (nd) {
1970 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1971
1972 nd = rb_next(nd);
1973 rb_erase(&h->rb_node, &hists->entries);
1974
1975 resort_filtered_entry(&new_root, h);
1976 }
1977
1978 hists->entries = new_root;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001979}
1980
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001981void hists__filter_by_thread(struct hists *hists)
1982{
Namhyung Kim155e9af2016-02-25 00:13:38 +09001983 if (symbol_conf.report_hierarchy)
1984 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
1985 hists->thread_filter);
1986 else
1987 hists__filter_by_type(hists, HIST_FILTER__THREAD,
1988 hists__filter_entry_by_thread);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001989}
1990
1991void hists__filter_by_dso(struct hists *hists)
1992{
Namhyung Kim155e9af2016-02-25 00:13:38 +09001993 if (symbol_conf.report_hierarchy)
1994 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
1995 hists->dso_filter);
1996 else
1997 hists__filter_by_type(hists, HIST_FILTER__DSO,
1998 hists__filter_entry_by_dso);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001999}
2000
2001void hists__filter_by_symbol(struct hists *hists)
2002{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002003 if (symbol_conf.report_hierarchy)
2004 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2005 hists->symbol_filter_str);
2006 else
2007 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2008 hists__filter_entry_by_symbol);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002009}
2010
2011void hists__filter_by_socket(struct hists *hists)
2012{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002013 if (symbol_conf.report_hierarchy)
2014 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2015 &hists->socket_filter);
2016 else
2017 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2018 hists__filter_entry_by_socket);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002019}
2020
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002021void events_stats__inc(struct events_stats *stats, u32 type)
2022{
2023 ++stats->nr_events[0];
2024 ++stats->nr_events[type];
2025}
2026
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002027void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002028{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002029 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002030}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002031
Namhyung Kim1844dbc2014-05-28 14:12:18 +09002032void hists__inc_nr_samples(struct hists *hists, bool filtered)
2033{
2034 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2035 if (!filtered)
2036 hists->stats.nr_non_filtered_samples++;
2037}
2038
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002039static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2040 struct hist_entry *pair)
2041{
Namhyung Kimce74f602012-12-10 17:29:55 +09002042 struct rb_root *root;
2043 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002044 struct rb_node *parent = NULL;
2045 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07002046 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002047
Jiri Olsa52225032016-05-03 13:54:42 +02002048 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002049 root = &hists->entries_collapsed;
2050 else
2051 root = hists->entries_in;
2052
2053 p = &root->rb_node;
2054
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002055 while (*p != NULL) {
2056 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09002057 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002058
Namhyung Kimce74f602012-12-10 17:29:55 +09002059 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002060
2061 if (!cmp)
2062 goto out;
2063
2064 if (cmp < 0)
2065 p = &(*p)->rb_left;
2066 else
2067 p = &(*p)->rb_right;
2068 }
2069
Namhyung Kima0b51af2012-09-11 13:34:27 +09002070 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002071 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03002072 memset(&he->stat, 0, sizeof(he->stat));
2073 he->hists = hists;
Kan Liang09623d72016-04-24 23:28:09 -07002074 if (symbol_conf.cumulate_callchain)
2075 memset(he->stat_acc, 0, sizeof(he->stat));
Namhyung Kimce74f602012-12-10 17:29:55 +09002076 rb_link_node(&he->rb_node_in, parent, p);
2077 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09002078 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01002079 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002080 }
2081out:
2082 return he;
2083}
2084
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002085static struct hist_entry *hists__find_entry(struct hists *hists,
2086 struct hist_entry *he)
2087{
Namhyung Kimce74f602012-12-10 17:29:55 +09002088 struct rb_node *n;
2089
Jiri Olsa52225032016-05-03 13:54:42 +02002090 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002091 n = hists->entries_collapsed.rb_node;
2092 else
2093 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002094
2095 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002096 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2097 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002098
2099 if (cmp < 0)
2100 n = n->rb_left;
2101 else if (cmp > 0)
2102 n = n->rb_right;
2103 else
2104 return iter;
2105 }
2106
2107 return NULL;
2108}
2109
2110/*
2111 * Look for pairs to link to the leader buckets (hist_entries):
2112 */
2113void hists__match(struct hists *leader, struct hists *other)
2114{
Namhyung Kimce74f602012-12-10 17:29:55 +09002115 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002116 struct rb_node *nd;
2117 struct hist_entry *pos, *pair;
2118
Jiri Olsa52225032016-05-03 13:54:42 +02002119 if (hists__has(leader, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002120 root = &leader->entries_collapsed;
2121 else
2122 root = leader->entries_in;
2123
2124 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2125 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002126 pair = hists__find_entry(other, pos);
2127
2128 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09002129 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002130 }
2131}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002132
2133/*
2134 * Look for entries in the other hists that are not present in the leader, if
2135 * we find them, just add a dummy entry on the leader hists, with period=0,
2136 * nr_events=0, to serve as the list header.
2137 */
2138int hists__link(struct hists *leader, struct hists *other)
2139{
Namhyung Kimce74f602012-12-10 17:29:55 +09002140 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002141 struct rb_node *nd;
2142 struct hist_entry *pos, *pair;
2143
Jiri Olsa52225032016-05-03 13:54:42 +02002144 if (hists__has(other, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002145 root = &other->entries_collapsed;
2146 else
2147 root = other->entries_in;
2148
2149 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2150 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002151
2152 if (!hist_entry__has_pairs(pos)) {
2153 pair = hists__add_dummy_entry(leader, pos);
2154 if (pair == NULL)
2155 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09002156 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002157 }
2158 }
2159
2160 return 0;
2161}
Namhyung Kimf2148332014-01-14 11:52:48 +09002162
Andi Kleen578499982015-07-18 08:24:49 -07002163void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2164 struct perf_sample *sample, bool nonany_branch_mode)
2165{
2166 struct branch_info *bi;
2167
2168 /* If we have branch cycles always annotate them. */
2169 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2170 int i;
2171
2172 bi = sample__resolve_bstack(sample, al);
2173 if (bi) {
2174 struct addr_map_symbol *prev = NULL;
2175
2176 /*
2177 * Ignore errors, still want to process the
2178 * other entries.
2179 *
2180 * For non standard branch modes always
2181 * force no IPC (prev == NULL)
2182 *
2183 * Note that perf stores branches reversed from
2184 * program order!
2185 */
2186 for (i = bs->nr - 1; i >= 0; i--) {
2187 addr_map_symbol__account_cycles(&bi[i].from,
2188 nonany_branch_mode ? NULL : prev,
2189 bi[i].flags.cycles);
2190 prev = &bi[i].to;
2191 }
2192 free(bi);
2193 }
2194 }
2195}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002196
2197size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
2198{
2199 struct perf_evsel *pos;
2200 size_t ret = 0;
2201
2202 evlist__for_each(evlist, pos) {
2203 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2204 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2205 }
2206
2207 return ret;
2208}
2209
2210
Namhyung Kimf2148332014-01-14 11:52:48 +09002211u64 hists__total_period(struct hists *hists)
2212{
2213 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2214 hists->stats.total_period;
2215}
Namhyung Kim33db4562014-02-07 12:06:07 +09002216
2217int parse_filter_percentage(const struct option *opt __maybe_unused,
2218 const char *arg, int unset __maybe_unused)
2219{
2220 if (!strcmp(arg, "relative"))
2221 symbol_conf.filter_relative = true;
2222 else if (!strcmp(arg, "absolute"))
2223 symbol_conf.filter_relative = false;
2224 else
2225 return -1;
2226
2227 return 0;
2228}
Namhyung Kim0b93da12014-01-14 12:02:15 +09002229
2230int perf_hist_config(const char *var, const char *value)
2231{
2232 if (!strcmp(var, "hist.percentage"))
2233 return parse_filter_percentage(NULL, value, 0);
2234
2235 return 0;
2236}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002237
Jiri Olsa5b658552016-01-18 10:24:22 +01002238int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002239{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002240 memset(hists, 0, sizeof(*hists));
2241 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
2242 hists->entries_in = &hists->entries_in_array[0];
2243 hists->entries_collapsed = RB_ROOT;
2244 hists->entries = RB_ROOT;
2245 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04002246 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01002247 hists->hpp_list = hpp_list;
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002248 INIT_LIST_HEAD(&hists->hpp_formats);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002249 return 0;
2250}
2251
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002252static void hists__delete_remaining_entries(struct rb_root *root)
2253{
2254 struct rb_node *node;
2255 struct hist_entry *he;
2256
2257 while (!RB_EMPTY_ROOT(root)) {
2258 node = rb_first(root);
2259 rb_erase(node, root);
2260
2261 he = rb_entry(node, struct hist_entry, rb_node_in);
2262 hist_entry__delete(he);
2263 }
2264}
2265
2266static void hists__delete_all_entries(struct hists *hists)
2267{
2268 hists__delete_entries(hists);
2269 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2270 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2271 hists__delete_remaining_entries(&hists->entries_collapsed);
2272}
2273
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002274static void hists_evsel__exit(struct perf_evsel *evsel)
2275{
2276 struct hists *hists = evsel__hists(evsel);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002277 struct perf_hpp_fmt *fmt, *pos;
2278 struct perf_hpp_list_node *node, *tmp;
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002279
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002280 hists__delete_all_entries(hists);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002281
2282 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2283 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2284 list_del(&fmt->list);
2285 free(fmt);
2286 }
2287 list_del(&node->list);
2288 free(node);
2289 }
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002290}
2291
Namhyung Kimfc284be2016-01-07 10:14:10 +01002292static int hists_evsel__init(struct perf_evsel *evsel)
2293{
2294 struct hists *hists = evsel__hists(evsel);
2295
Jiri Olsa5b658552016-01-18 10:24:22 +01002296 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01002297 return 0;
2298}
2299
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002300/*
2301 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2302 * stored in the rbtree...
2303 */
2304
2305int hists__init(void)
2306{
2307 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002308 hists_evsel__init,
2309 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002310 if (err)
2311 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2312
2313 return err;
2314}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01002315
2316void perf_hpp_list__init(struct perf_hpp_list *list)
2317{
2318 INIT_LIST_HEAD(&list->fields);
2319 INIT_LIST_HEAD(&list->sorts);
2320}