blob: 017eb5c42c37edffc0fb128207c21d40d9ba1587 [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))
82 hists__set_col_len(hists, HISTC_THREAD, len + 6);
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 }
120 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100121
122 if (h->mem_info) {
Stephane Eranian98a3b322013-01-24 16:10:35 +0100123 if (h->mem_info->daddr.sym) {
124 symlen = (int)h->mem_info->daddr.sym->namelen + 4
125 + unresolved_col_width + 2;
126 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
127 symlen);
Don Zickus9b32ba72014-06-01 15:38:29 +0200128 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
129 symlen + 1);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100130 } else {
131 symlen = unresolved_col_width + 4 + 2;
132 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
133 symlen);
Jiri Olsa08059092016-01-20 12:56:33 +0100134 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
135 symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100136 }
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200137
138 if (h->mem_info->iaddr.sym) {
139 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
140 + unresolved_col_width + 2;
141 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
142 symlen);
143 } else {
144 symlen = unresolved_col_width + 4 + 2;
145 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
146 symlen);
147 }
148
Stephane Eranian98a3b322013-01-24 16:10:35 +0100149 if (h->mem_info->daddr.map) {
150 symlen = dso__name_len(h->mem_info->daddr.map->dso);
151 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
152 symlen);
153 } else {
154 symlen = unresolved_col_width + 4 + 2;
155 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
156 }
157 } else {
158 symlen = unresolved_col_width + 4 + 2;
159 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200160 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100161 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
162 }
163
Arnaldo Carvalho de Meloa4978ec2015-09-09 12:14:00 -0300164 hists__new_col_len(hists, HISTC_CPU, 3);
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400165 hists__new_col_len(hists, HISTC_SOCKET, 6);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100166 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
167 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
168 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
169 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
170 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
171 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700172
Arnaldo Carvalho de Meloe8e6d372015-08-10 16:53:54 -0300173 if (h->srcline)
174 hists__new_col_len(hists, HISTC_SRCLINE, strlen(h->srcline));
175
Andi Kleen31191a82015-08-07 15:54:24 -0700176 if (h->srcfile)
177 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
178
Andi Kleen475eeab2013-09-20 07:40:43 -0700179 if (h->transaction)
180 hists__new_col_len(hists, HISTC_TRANSACTION,
181 hist_entry__transaction_len());
Namhyung Kim0c0af782016-02-21 23:22:38 +0900182
183 if (h->trace_output)
184 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300185}
186
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900187void hists__output_recalc_col_len(struct hists *hists, int max_rows)
188{
189 struct rb_node *next = rb_first(&hists->entries);
190 struct hist_entry *n;
191 int row = 0;
192
193 hists__reset_col_len(hists);
194
195 while (next && row++ < max_rows) {
196 n = rb_entry(next, struct hist_entry, rb_node);
197 if (!n->filtered)
198 hists__calc_col_len(hists, n);
199 next = rb_next(&n->rb_node);
200 }
201}
202
Namhyung Kimf39056f2014-01-14 14:25:37 +0900203static void he_stat__add_cpumode_period(struct he_stat *he_stat,
204 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800205{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300206 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800207 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900208 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800209 break;
210 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900211 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800212 break;
213 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900214 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800215 break;
216 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900217 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800218 break;
219 default:
220 break;
221 }
222}
223
Andi Kleen05484292013-01-24 16:10:29 +0100224static void he_stat__add_period(struct he_stat *he_stat, u64 period,
225 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900226{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100227
Namhyung Kim139c0812012-10-04 21:49:43 +0900228 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100229 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900230 he_stat->nr_events += 1;
231}
232
233static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
234{
235 dest->period += src->period;
236 dest->period_sys += src->period_sys;
237 dest->period_us += src->period_us;
238 dest->period_guest_sys += src->period_guest_sys;
239 dest->period_guest_us += src->period_guest_us;
240 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100241 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900242}
243
Namhyung Kimf39056f2014-01-14 14:25:37 +0900244static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300245{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900246 he_stat->period = (he_stat->period * 7) / 8;
247 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100248 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300249}
250
251static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
252{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900253 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900254 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200255
256 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300257 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200258
Namhyung Kimf39056f2014-01-14 14:25:37 +0900259 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900260 if (symbol_conf.cumulate_callchain)
261 he_stat__decay(he->stat_acc);
Namhyung Kim42b276a2016-01-05 12:06:00 +0900262 decay_callchain(he->callchain);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200263
Namhyung Kim3186b682014-04-22 13:44:23 +0900264 diff = prev_period - he->stat.period;
265
266 hists->stats.total_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200267 if (!he->filtered)
Namhyung Kim3186b682014-04-22 13:44:23 +0900268 hists->stats.total_non_filtered_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200269
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900270 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300271}
272
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300273static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
274{
275 rb_erase(&he->rb_node, &hists->entries);
276
277 if (sort__need_collapse)
278 rb_erase(&he->rb_node_in, &hists->entries_collapsed);
Namhyung Kim61fa0e92015-12-10 16:53:20 +0900279 else
280 rb_erase(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300281
282 --hists->nr_entries;
283 if (!he->filtered)
284 --hists->nr_non_filtered_entries;
285
286 hist_entry__delete(he);
287}
288
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900289void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300290{
291 struct rb_node *next = rb_first(&hists->entries);
292 struct hist_entry *n;
293
294 while (next) {
295 n = rb_entry(next, struct hist_entry, rb_node);
296 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200297 if (((zap_user && n->level == '.') ||
298 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300299 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300300 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300301 }
302 }
303}
304
Namhyung Kim701937b2014-08-12 17:16:05 +0900305void hists__delete_entries(struct hists *hists)
306{
307 struct rb_node *next = rb_first(&hists->entries);
308 struct hist_entry *n;
309
310 while (next) {
311 n = rb_entry(next, struct hist_entry, rb_node);
312 next = rb_next(&n->rb_node);
313
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300314 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900315 }
316}
317
John Kacur3d1d07e2009-09-28 15:32:55 +0200318/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300319 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200320 */
321
Namhyung Kima0b51af2012-09-11 13:34:27 +0900322static struct hist_entry *hist_entry__new(struct hist_entry *template,
323 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300324{
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900325 size_t callchain_size = 0;
326 struct hist_entry *he;
327
Namhyung Kim82aa0192014-12-22 13:44:14 +0900328 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900329 callchain_size = sizeof(struct callchain_root);
330
331 he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300332
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200333 if (he != NULL) {
334 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900335
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900336 if (symbol_conf.cumulate_callchain) {
337 he->stat_acc = malloc(sizeof(he->stat));
338 if (he->stat_acc == NULL) {
339 free(he);
340 return NULL;
341 }
342 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
Namhyung Kima0b51af2012-09-11 13:34:27 +0900343 if (!sample_self)
344 memset(&he->stat, 0, sizeof(he->stat));
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900345 }
346
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300347 map__get(he->ms.map);
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100348
349 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900350 /*
351 * This branch info is (a part of) allocated from
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -0300352 * sample__resolve_bstack() and will be freed after
Namhyung Kim26353a62013-04-01 20:35:17 +0900353 * adding new entries. So we need to save a copy.
354 */
355 he->branch_info = malloc(sizeof(*he->branch_info));
356 if (he->branch_info == NULL) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300357 map__zput(he->ms.map);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900358 free(he->stat_acc);
Namhyung Kim26353a62013-04-01 20:35:17 +0900359 free(he);
360 return NULL;
361 }
362
363 memcpy(he->branch_info, template->branch_info,
364 sizeof(*he->branch_info));
365
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300366 map__get(he->branch_info->from.map);
367 map__get(he->branch_info->to.map);
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100368 }
369
Stephane Eranian98a3b322013-01-24 16:10:35 +0100370 if (he->mem_info) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300371 map__get(he->mem_info->iaddr.map);
372 map__get(he->mem_info->daddr.map);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100373 }
374
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300375 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200376 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200377
Namhyung Kim72392832015-12-24 11:16:17 +0900378 if (he->raw_data) {
379 he->raw_data = memdup(he->raw_data, he->raw_size);
380
381 if (he->raw_data == NULL) {
382 map__put(he->ms.map);
383 if (he->branch_info) {
384 map__put(he->branch_info->from.map);
385 map__put(he->branch_info->to.map);
386 free(he->branch_info);
387 }
388 if (he->mem_info) {
389 map__put(he->mem_info->iaddr.map);
390 map__put(he->mem_info->daddr.map);
391 }
392 free(he->stat_acc);
393 free(he);
394 return NULL;
395 }
396 }
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200397 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300398 thread__get(he->thread);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300399 }
400
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200401 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300402}
403
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300404static u8 symbol__parent_filter(const struct symbol *parent)
405{
406 if (symbol_conf.exclude_other && parent == NULL)
407 return 1 << HIST_FILTER__PARENT;
408 return 0;
409}
410
Namhyung Kim467ef102016-02-16 23:08:19 +0900411static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
412{
413 if (!symbol_conf.use_callchain)
414 return;
415
416 he->hists->callchain_period += period;
417 if (!he->filtered)
418 he->hists->callchain_non_filtered_period += period;
419}
420
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300421static struct hist_entry *hists__findnew_entry(struct hists *hists,
422 struct hist_entry *entry,
423 struct addr_location *al,
424 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300425{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300426 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300427 struct rb_node *parent = NULL;
428 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700429 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900430 u64 period = entry->stat.period;
431 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300432
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300433 p = &hists->entries_in->rb_node;
434
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300435 while (*p != NULL) {
436 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300437 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300438
Namhyung Kim9afcf932012-12-10 17:29:54 +0900439 /*
440 * Make sure that it receives arguments in a same order as
441 * hist_entry__collapse() so that we can use an appropriate
442 * function when searching an entry regardless which sort
443 * keys were used.
444 */
445 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300446
447 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900448 if (sample_self) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900449 he_stat__add_period(&he->stat, period, weight);
Namhyung Kim467ef102016-02-16 23:08:19 +0900450 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900451 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900452 if (symbol_conf.cumulate_callchain)
453 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400454
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900455 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300456 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900457 * and will not be used anymore.
458 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300459 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900460
David Miller63fa4712012-03-27 03:14:18 -0400461 /* If the map of an existing hist_entry has
462 * become out-of-date due to an exec() or
463 * similar, update it. Otherwise we will
464 * mis-adjust symbol addresses when computing
465 * the history counter to increment.
466 */
467 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300468 map__put(he->ms.map);
469 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400470 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300471 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300472 }
473
474 if (cmp < 0)
475 p = &(*p)->rb_left;
476 else
477 p = &(*p)->rb_right;
478 }
479
Namhyung Kima0b51af2012-09-11 13:34:27 +0900480 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300481 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900482 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300483
Namhyung Kim0f584742016-01-28 00:40:49 +0900484 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900485 hist_entry__add_callchain_period(he, period);
486 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900487
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300488 rb_link_node(&he->rb_node_in, parent, p);
489 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300490out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900491 if (sample_self)
492 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900493 if (symbol_conf.cumulate_callchain)
494 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300495 return he;
496}
497
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300498struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100499 struct addr_location *al,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900500 struct symbol *sym_parent,
501 struct branch_info *bi,
502 struct mem_info *mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900503 struct perf_sample *sample,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900504 bool sample_self)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100505{
506 struct hist_entry entry = {
507 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900508 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100509 .ms = {
510 .map = al->map,
511 .sym = al->sym,
512 },
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400513 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400514 .cpu = al->cpu,
515 .cpumode = al->cpumode,
516 .ip = al->addr,
517 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900518 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900519 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900520 .period = sample->period,
521 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900522 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100523 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300524 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300525 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900526 .branch_info = bi,
527 .mem_info = mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900528 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900529 .raw_data = sample->raw_data,
530 .raw_size = sample->raw_size,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100531 };
532
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300533 return hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100534}
535
Namhyung Kim69bcb012013-10-30 09:40:34 +0900536static int
537iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
538 struct addr_location *al __maybe_unused)
539{
540 return 0;
541}
542
543static int
544iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
545 struct addr_location *al __maybe_unused)
546{
547 return 0;
548}
549
550static int
551iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
552{
553 struct perf_sample *sample = iter->sample;
554 struct mem_info *mi;
555
556 mi = sample__resolve_mem(sample, al);
557 if (mi == NULL)
558 return -ENOMEM;
559
560 iter->priv = mi;
561 return 0;
562}
563
564static int
565iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
566{
567 u64 cost;
568 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300569 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900570 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900571 struct hist_entry *he;
572
573 if (mi == NULL)
574 return -EINVAL;
575
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900576 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900577 if (!cost)
578 cost = 1;
579
580 /*
581 * must pass period=weight in order to get the correct
582 * sorting from hists__collapse_resort() which is solely
583 * based on periods. We want sorting be done on nr_events * weight
584 * and this is indirectly achieved by passing period=weight here
585 * and the he_stat__add_period() function.
586 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900587 sample->period = cost;
588
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300589 he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900590 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900591 if (!he)
592 return -ENOMEM;
593
594 iter->he = he;
595 return 0;
596}
597
598static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900599iter_finish_mem_entry(struct hist_entry_iter *iter,
600 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900601{
602 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300603 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900604 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900605 int err = -EINVAL;
606
607 if (he == NULL)
608 goto out;
609
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300610 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900611
612 err = hist_entry__append_callchain(he, iter->sample);
613
614out:
615 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300616 * We don't need to free iter->priv (mem_info) here since the mem info
617 * was either already freed in hists__findnew_entry() or passed to a
618 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900619 */
620 iter->priv = NULL;
621
622 iter->he = NULL;
623 return err;
624}
625
626static int
627iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
628{
629 struct branch_info *bi;
630 struct perf_sample *sample = iter->sample;
631
632 bi = sample__resolve_bstack(sample, al);
633 if (!bi)
634 return -ENOMEM;
635
636 iter->curr = 0;
637 iter->total = sample->branch_stack->nr;
638
639 iter->priv = bi;
640 return 0;
641}
642
643static int
644iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
645 struct addr_location *al __maybe_unused)
646{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900647 /* to avoid calling callback function */
648 iter->he = NULL;
649
Namhyung Kim69bcb012013-10-30 09:40:34 +0900650 return 0;
651}
652
653static int
654iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
655{
656 struct branch_info *bi = iter->priv;
657 int i = iter->curr;
658
659 if (bi == NULL)
660 return 0;
661
662 if (iter->curr >= iter->total)
663 return 0;
664
665 al->map = bi[i].to.map;
666 al->sym = bi[i].to.sym;
667 al->addr = bi[i].to.addr;
668 return 1;
669}
670
671static int
672iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
673{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900674 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900675 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300676 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900677 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900678 struct hist_entry *he = NULL;
679 int i = iter->curr;
680 int err = 0;
681
682 bi = iter->priv;
683
684 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
685 goto out;
686
687 /*
688 * The report shows the percentage of total branches captured
689 * and not events sampled. Thus we use a pseudo period of 1.
690 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900691 sample->period = 1;
692 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
693
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300694 he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900695 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900696 if (he == NULL)
697 return -ENOMEM;
698
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300699 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900700
701out:
702 iter->he = he;
703 iter->curr++;
704 return err;
705}
706
707static int
708iter_finish_branch_entry(struct hist_entry_iter *iter,
709 struct addr_location *al __maybe_unused)
710{
711 zfree(&iter->priv);
712 iter->he = NULL;
713
714 return iter->curr >= iter->total ? 0 : -1;
715}
716
717static int
718iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
719 struct addr_location *al __maybe_unused)
720{
721 return 0;
722}
723
724static int
725iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
726{
727 struct perf_evsel *evsel = iter->evsel;
728 struct perf_sample *sample = iter->sample;
729 struct hist_entry *he;
730
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300731 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900732 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900733 if (he == NULL)
734 return -ENOMEM;
735
736 iter->he = he;
737 return 0;
738}
739
740static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900741iter_finish_normal_entry(struct hist_entry_iter *iter,
742 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900743{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900744 struct hist_entry *he = iter->he;
745 struct perf_evsel *evsel = iter->evsel;
746 struct perf_sample *sample = iter->sample;
747
748 if (he == NULL)
749 return 0;
750
751 iter->he = NULL;
752
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300753 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900754
755 return hist_entry__append_callchain(he, sample);
756}
757
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900758static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300759iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900760 struct addr_location *al __maybe_unused)
761{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900762 struct hist_entry **he_cache;
763
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900764 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900765
766 /*
767 * This is for detecting cycles or recursions so that they're
768 * cumulated only one time to prevent entries more than 100%
769 * overhead.
770 */
Adrian Hunter96b40f32015-09-25 16:15:47 +0300771 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900772 if (he_cache == NULL)
773 return -ENOMEM;
774
775 iter->priv = he_cache;
776 iter->curr = 0;
777
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900778 return 0;
779}
780
781static int
782iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
783 struct addr_location *al)
784{
785 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300786 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900787 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900788 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900789 struct hist_entry *he;
790 int err = 0;
791
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300792 he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900793 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900794 if (he == NULL)
795 return -ENOMEM;
796
797 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900798 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900799
Namhyung Kim82aa0192014-12-22 13:44:14 +0900800 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900801
802 /*
803 * We need to re-initialize the cursor since callchain_append()
804 * advanced the cursor to the end.
805 */
806 callchain_cursor_commit(&callchain_cursor);
807
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300808 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900809
810 return err;
811}
812
813static int
814iter_next_cumulative_entry(struct hist_entry_iter *iter,
815 struct addr_location *al)
816{
817 struct callchain_cursor_node *node;
818
819 node = callchain_cursor_current(&callchain_cursor);
820 if (node == NULL)
821 return 0;
822
Namhyung Kimc7405d82013-10-31 13:58:30 +0900823 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900824}
825
826static int
827iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
828 struct addr_location *al)
829{
830 struct perf_evsel *evsel = iter->evsel;
831 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900832 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900833 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900834 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -0300835 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900836 .cpu = al->cpu,
837 .thread = al->thread,
838 .comm = thread__comm(al->thread),
839 .ip = al->addr,
840 .ms = {
841 .map = al->map,
842 .sym = al->sym,
843 },
844 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +0900845 .raw_data = sample->raw_data,
846 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900847 };
848 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900849 struct callchain_cursor cursor;
850
851 callchain_cursor_snapshot(&cursor, &callchain_cursor);
852
853 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900854
855 /*
856 * Check if there's duplicate entries in the callchain.
857 * It's possible that it has cycles or recursive calls.
858 */
859 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900860 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
861 /* to avoid calling callback function */
862 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900863 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900864 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900865 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900866
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300867 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900868 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900869 if (he == NULL)
870 return -ENOMEM;
871
872 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900873 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900874
Namhyung Kim82aa0192014-12-22 13:44:14 +0900875 if (symbol_conf.use_callchain)
876 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900877 return 0;
878}
879
880static int
881iter_finish_cumulative_entry(struct hist_entry_iter *iter,
882 struct addr_location *al __maybe_unused)
883{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900884 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900885 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900886
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900887 return 0;
888}
889
Namhyung Kim69bcb012013-10-30 09:40:34 +0900890const struct hist_iter_ops hist_iter_mem = {
891 .prepare_entry = iter_prepare_mem_entry,
892 .add_single_entry = iter_add_single_mem_entry,
893 .next_entry = iter_next_nop_entry,
894 .add_next_entry = iter_add_next_nop_entry,
895 .finish_entry = iter_finish_mem_entry,
896};
897
898const struct hist_iter_ops hist_iter_branch = {
899 .prepare_entry = iter_prepare_branch_entry,
900 .add_single_entry = iter_add_single_branch_entry,
901 .next_entry = iter_next_branch_entry,
902 .add_next_entry = iter_add_next_branch_entry,
903 .finish_entry = iter_finish_branch_entry,
904};
905
906const struct hist_iter_ops hist_iter_normal = {
907 .prepare_entry = iter_prepare_normal_entry,
908 .add_single_entry = iter_add_single_normal_entry,
909 .next_entry = iter_next_nop_entry,
910 .add_next_entry = iter_add_next_nop_entry,
911 .finish_entry = iter_finish_normal_entry,
912};
913
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900914const struct hist_iter_ops hist_iter_cumulative = {
915 .prepare_entry = iter_prepare_cumulative_entry,
916 .add_single_entry = iter_add_single_cumulative_entry,
917 .next_entry = iter_next_cumulative_entry,
918 .add_next_entry = iter_add_next_cumulative_entry,
919 .finish_entry = iter_finish_cumulative_entry,
920};
921
Namhyung Kim69bcb012013-10-30 09:40:34 +0900922int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900923 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900924{
925 int err, err2;
926
Namhyung Kim063bd932015-05-19 17:04:10 +0900927 err = sample__resolve_callchain(iter->sample, &iter->parent,
928 iter->evsel, al, max_stack_depth);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900929 if (err)
930 return err;
931
Adrian Hunter96b40f32015-09-25 16:15:47 +0300932 iter->max_stack = max_stack_depth;
933
Namhyung Kim69bcb012013-10-30 09:40:34 +0900934 err = iter->ops->prepare_entry(iter, al);
935 if (err)
936 goto out;
937
938 err = iter->ops->add_single_entry(iter, al);
939 if (err)
940 goto out;
941
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900942 if (iter->he && iter->add_entry_cb) {
943 err = iter->add_entry_cb(iter, al, true, arg);
944 if (err)
945 goto out;
946 }
947
Namhyung Kim69bcb012013-10-30 09:40:34 +0900948 while (iter->ops->next_entry(iter, al)) {
949 err = iter->ops->add_next_entry(iter, al);
950 if (err)
951 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900952
953 if (iter->he && iter->add_entry_cb) {
954 err = iter->add_entry_cb(iter, al, false, arg);
955 if (err)
956 goto out;
957 }
Namhyung Kim69bcb012013-10-30 09:40:34 +0900958 }
959
960out:
961 err2 = iter->ops->finish_entry(iter, al);
962 if (!err)
963 err = err2;
964
965 return err;
966}
967
John Kacur3d1d07e2009-09-28 15:32:55 +0200968int64_t
969hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
970{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +0100971 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900972 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200973 int64_t cmp = 0;
974
Jiri Olsaaa6f50a2016-01-18 10:24:24 +0100975 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900976 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200977 if (cmp)
978 break;
979 }
980
981 return cmp;
982}
983
984int64_t
985hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
986{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +0100987 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900988 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200989 int64_t cmp = 0;
990
Jiri Olsaaa6f50a2016-01-18 10:24:24 +0100991 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900992 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200993 if (cmp)
994 break;
995 }
996
997 return cmp;
998}
999
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001000void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001001{
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001002 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001003 map__zput(he->ms.map);
1004
1005 if (he->branch_info) {
1006 map__zput(he->branch_info->from.map);
1007 map__zput(he->branch_info->to.map);
1008 zfree(&he->branch_info);
1009 }
1010
1011 if (he->mem_info) {
1012 map__zput(he->mem_info->iaddr.map);
1013 map__zput(he->mem_info->daddr.map);
1014 zfree(&he->mem_info);
1015 }
1016
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001017 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +09001018 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -07001019 if (he->srcfile && he->srcfile[0])
1020 free(he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001021 free_callchain(he->callchain);
Namhyung Kim60517d22015-12-23 02:07:03 +09001022 free(he->trace_output);
Namhyung Kim72392832015-12-24 11:16:17 +09001023 free(he->raw_data);
John Kacur3d1d07e2009-09-28 15:32:55 +02001024 free(he);
1025}
1026
1027/*
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001028 * If this is not the last column, then we need to pad it according to the
1029 * pre-calculated max lenght for this column, otherwise don't bother adding
1030 * spaces because that would break viewing this with, for instance, 'less',
1031 * that would show tons of trailing spaces when a long C++ demangled method
1032 * names is sampled.
1033*/
1034int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1035 struct perf_hpp_fmt *fmt, int printed)
1036{
1037 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
1038 const int width = fmt->width(fmt, hpp, hists_to_evsel(he->hists));
1039 if (printed < width) {
1040 advance_hpp(hpp, printed);
1041 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1042 }
1043 }
1044
1045 return printed;
1046}
1047
1048/*
John Kacur3d1d07e2009-09-28 15:32:55 +02001049 * collapse the histogram
1050 */
1051
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001052int hists__collapse_insert_entry(struct hists *hists, struct rb_root *root,
1053 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001054{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001055 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001056 struct rb_node *parent = NULL;
1057 struct hist_entry *iter;
1058 int64_t cmp;
1059
1060 while (*p != NULL) {
1061 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001062 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001063
1064 cmp = hist_entry__collapse(iter, he);
1065
1066 if (!cmp) {
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001067 int ret = 0;
1068
Namhyung Kim139c0812012-10-04 21:49:43 +09001069 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001070 if (symbol_conf.cumulate_callchain)
1071 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001072
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001073 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001074 callchain_cursor_reset(&callchain_cursor);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001075 if (callchain_merge(&callchain_cursor,
1076 iter->callchain,
1077 he->callchain) < 0)
1078 ret = -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001079 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001080 hist_entry__delete(he);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001081 return ret;
John Kacur3d1d07e2009-09-28 15:32:55 +02001082 }
1083
1084 if (cmp < 0)
1085 p = &(*p)->rb_left;
1086 else
1087 p = &(*p)->rb_right;
1088 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001089 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001090
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001091 rb_link_node(&he->rb_node_in, parent, p);
1092 rb_insert_color(&he->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001093 return 1;
John Kacur3d1d07e2009-09-28 15:32:55 +02001094}
1095
Namhyung Kimfc284be2016-01-07 10:14:10 +01001096struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001097{
1098 struct rb_root *root;
1099
1100 pthread_mutex_lock(&hists->lock);
1101
1102 root = hists->entries_in;
1103 if (++hists->entries_in > &hists->entries_in_array[1])
1104 hists->entries_in = &hists->entries_in_array[0];
1105
1106 pthread_mutex_unlock(&hists->lock);
1107
1108 return root;
1109}
1110
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001111static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1112{
1113 hists__filter_entry_by_dso(hists, he);
1114 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001115 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001116 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001117}
1118
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001119int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001120{
1121 struct rb_root *root;
1122 struct rb_node *next;
1123 struct hist_entry *n;
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001124 int ret;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001125
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001126 if (!sort__need_collapse)
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001127 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001128
Namhyung Kim740b97f2014-12-22 13:44:10 +09001129 hists->nr_entries = 0;
1130
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001131 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001132
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001133 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001134
1135 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001136 if (session_done())
1137 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001138 n = rb_entry(next, struct hist_entry, rb_node_in);
1139 next = rb_next(&n->rb_node_in);
1140
1141 rb_erase(&n->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001142 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1143 if (ret < 0)
1144 return -1;
1145
1146 if (ret) {
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001147 /*
1148 * If it wasn't combined with one of the entries already
1149 * collapsed, we need to apply the filters that may have
1150 * been set by, say, the hist_browser.
1151 */
1152 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001153 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001154 if (prog)
1155 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001156 }
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001157 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001158}
1159
Namhyung Kim043ca3892014-03-03 14:18:00 +09001160static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001161{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001162 struct hists *hists = a->hists;
Namhyung Kim043ca3892014-03-03 14:18:00 +09001163 struct perf_hpp_fmt *fmt;
1164 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001165
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001166 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001167 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001168 continue;
1169
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001170 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001171 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001172 break;
1173 }
1174
Namhyung Kim043ca3892014-03-03 14:18:00 +09001175 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001176}
1177
Namhyung Kim9283ba92014-04-24 16:37:26 +09001178static void hists__reset_filter_stats(struct hists *hists)
1179{
1180 hists->nr_non_filtered_entries = 0;
1181 hists->stats.total_non_filtered_period = 0;
1182}
1183
1184void hists__reset_stats(struct hists *hists)
1185{
1186 hists->nr_entries = 0;
1187 hists->stats.total_period = 0;
1188
1189 hists__reset_filter_stats(hists);
1190}
1191
1192static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1193{
1194 hists->nr_non_filtered_entries++;
1195 hists->stats.total_non_filtered_period += h->stat.period;
1196}
1197
1198void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1199{
1200 if (!h->filtered)
1201 hists__inc_filter_stats(hists, h);
1202
1203 hists->nr_entries++;
1204 hists->stats.total_period += h->stat.period;
1205}
1206
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001207static void __hists__insert_output_entry(struct rb_root *entries,
1208 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001209 u64 min_callchain_hits,
1210 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001211{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001212 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001213 struct rb_node *parent = NULL;
1214 struct hist_entry *iter;
1215
Namhyung Kim744070e2016-01-28 00:40:48 +09001216 if (use_callchain) {
1217 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1218 u64 total = he->stat.period;
1219
1220 if (symbol_conf.cumulate_callchain)
1221 total = he->stat_acc->period;
1222
1223 min_callchain_hits = total * (callchain_param.min_percent / 100);
1224 }
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001225 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001226 min_callchain_hits, &callchain_param);
Namhyung Kim744070e2016-01-28 00:40:48 +09001227 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001228
1229 while (*p != NULL) {
1230 parent = *p;
1231 iter = rb_entry(parent, struct hist_entry, rb_node);
1232
Namhyung Kim043ca3892014-03-03 14:18:00 +09001233 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001234 p = &(*p)->rb_left;
1235 else
1236 p = &(*p)->rb_right;
1237 }
1238
1239 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001240 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +02001241}
1242
Jiri Olsa01441af2016-01-18 10:23:59 +01001243static void output_resort(struct hists *hists, struct ui_progress *prog,
1244 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001245{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001246 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001247 struct rb_node *next;
1248 struct hist_entry *n;
Namhyung Kim467ef102016-02-16 23:08:19 +09001249 u64 callchain_total;
John Kacur3d1d07e2009-09-28 15:32:55 +02001250 u64 min_callchain_hits;
1251
Namhyung Kim467ef102016-02-16 23:08:19 +09001252 callchain_total = hists->callchain_period;
1253 if (symbol_conf.filter_relative)
1254 callchain_total = hists->callchain_non_filtered_period;
1255
1256 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001257
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001258 if (sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001259 root = &hists->entries_collapsed;
1260 else
1261 root = hists->entries_in;
1262
1263 next = rb_first(root);
1264 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001265
Namhyung Kim9283ba92014-04-24 16:37:26 +09001266 hists__reset_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001267 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001268
John Kacur3d1d07e2009-09-28 15:32:55 +02001269 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001270 n = rb_entry(next, struct hist_entry, rb_node_in);
1271 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001272
Kan Liangf9db0d02015-08-11 06:30:48 -04001273 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001274 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001275
1276 if (!n->filtered)
1277 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001278
1279 if (prog)
1280 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001281 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001282}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001283
Jiri Olsa452ce032016-01-18 10:24:00 +01001284void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
Jiri Olsa01441af2016-01-18 10:23:59 +01001285{
Jiri Olsa01441af2016-01-18 10:23:59 +01001286 bool use_callchain;
1287
1288 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1289 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1290 else
1291 use_callchain = symbol_conf.use_callchain;
1292
Jiri Olsa452ce032016-01-18 10:24:00 +01001293 output_resort(evsel__hists(evsel), prog, use_callchain);
1294}
1295
1296void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1297{
1298 output_resort(hists, prog, symbol_conf.use_callchain);
Jiri Olsa01441af2016-01-18 10:23:59 +01001299}
1300
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001301static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001302 enum hist_filter filter)
1303{
1304 h->filtered &= ~(1 << filter);
1305 if (h->filtered)
1306 return;
1307
Namhyung Kim87e90f42014-04-24 16:44:16 +09001308 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001309 h->unfolded = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001310 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001311 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001312
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001313 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001314
Namhyung Kim9283ba92014-04-24 16:37:26 +09001315 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001316 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001317}
1318
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001319
1320static bool hists__filter_entry_by_dso(struct hists *hists,
1321 struct hist_entry *he)
1322{
1323 if (hists->dso_filter != NULL &&
1324 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1325 he->filtered |= (1 << HIST_FILTER__DSO);
1326 return true;
1327 }
1328
1329 return false;
1330}
1331
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001332static bool hists__filter_entry_by_thread(struct hists *hists,
1333 struct hist_entry *he)
1334{
1335 if (hists->thread_filter != NULL &&
1336 he->thread != hists->thread_filter) {
1337 he->filtered |= (1 << HIST_FILTER__THREAD);
1338 return true;
1339 }
1340
1341 return false;
1342}
1343
Namhyung Kime94d53e2012-03-16 17:50:51 +09001344static bool hists__filter_entry_by_symbol(struct hists *hists,
1345 struct hist_entry *he)
1346{
1347 if (hists->symbol_filter_str != NULL &&
1348 (!he->ms.sym || strstr(he->ms.sym->name,
1349 hists->symbol_filter_str) == NULL)) {
1350 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1351 return true;
1352 }
1353
1354 return false;
1355}
1356
Kan Liang21394d92015-09-04 10:45:44 -04001357static bool hists__filter_entry_by_socket(struct hists *hists,
1358 struct hist_entry *he)
1359{
1360 if ((hists->socket_filter > -1) &&
1361 (he->socket != hists->socket_filter)) {
1362 he->filtered |= (1 << HIST_FILTER__SOCKET);
1363 return true;
1364 }
1365
1366 return false;
1367}
1368
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001369typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1370
1371static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04001372{
1373 struct rb_node *nd;
1374
1375 hists->stats.nr_non_filtered_samples = 0;
1376
1377 hists__reset_filter_stats(hists);
1378 hists__reset_col_len(hists);
1379
1380 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1381 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1382
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001383 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04001384 continue;
1385
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001386 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04001387 }
1388}
1389
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001390void hists__filter_by_thread(struct hists *hists)
1391{
1392 hists__filter_by_type(hists, HIST_FILTER__THREAD,
1393 hists__filter_entry_by_thread);
1394}
1395
1396void hists__filter_by_dso(struct hists *hists)
1397{
1398 hists__filter_by_type(hists, HIST_FILTER__DSO,
1399 hists__filter_entry_by_dso);
1400}
1401
1402void hists__filter_by_symbol(struct hists *hists)
1403{
1404 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
1405 hists__filter_entry_by_symbol);
1406}
1407
1408void hists__filter_by_socket(struct hists *hists)
1409{
1410 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
1411 hists__filter_entry_by_socket);
1412}
1413
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001414void events_stats__inc(struct events_stats *stats, u32 type)
1415{
1416 ++stats->nr_events[0];
1417 ++stats->nr_events[type];
1418}
1419
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001420void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001421{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001422 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001423}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001424
Namhyung Kim1844dbc2014-05-28 14:12:18 +09001425void hists__inc_nr_samples(struct hists *hists, bool filtered)
1426{
1427 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1428 if (!filtered)
1429 hists->stats.nr_non_filtered_samples++;
1430}
1431
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001432static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1433 struct hist_entry *pair)
1434{
Namhyung Kimce74f602012-12-10 17:29:55 +09001435 struct rb_root *root;
1436 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001437 struct rb_node *parent = NULL;
1438 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07001439 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001440
Namhyung Kimce74f602012-12-10 17:29:55 +09001441 if (sort__need_collapse)
1442 root = &hists->entries_collapsed;
1443 else
1444 root = hists->entries_in;
1445
1446 p = &root->rb_node;
1447
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001448 while (*p != NULL) {
1449 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09001450 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001451
Namhyung Kimce74f602012-12-10 17:29:55 +09001452 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001453
1454 if (!cmp)
1455 goto out;
1456
1457 if (cmp < 0)
1458 p = &(*p)->rb_left;
1459 else
1460 p = &(*p)->rb_right;
1461 }
1462
Namhyung Kima0b51af2012-09-11 13:34:27 +09001463 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001464 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03001465 memset(&he->stat, 0, sizeof(he->stat));
1466 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +09001467 rb_link_node(&he->rb_node_in, parent, p);
1468 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09001469 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01001470 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001471 }
1472out:
1473 return he;
1474}
1475
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001476static struct hist_entry *hists__find_entry(struct hists *hists,
1477 struct hist_entry *he)
1478{
Namhyung Kimce74f602012-12-10 17:29:55 +09001479 struct rb_node *n;
1480
1481 if (sort__need_collapse)
1482 n = hists->entries_collapsed.rb_node;
1483 else
1484 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001485
1486 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09001487 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1488 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001489
1490 if (cmp < 0)
1491 n = n->rb_left;
1492 else if (cmp > 0)
1493 n = n->rb_right;
1494 else
1495 return iter;
1496 }
1497
1498 return NULL;
1499}
1500
1501/*
1502 * Look for pairs to link to the leader buckets (hist_entries):
1503 */
1504void hists__match(struct hists *leader, struct hists *other)
1505{
Namhyung Kimce74f602012-12-10 17:29:55 +09001506 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001507 struct rb_node *nd;
1508 struct hist_entry *pos, *pair;
1509
Namhyung Kimce74f602012-12-10 17:29:55 +09001510 if (sort__need_collapse)
1511 root = &leader->entries_collapsed;
1512 else
1513 root = leader->entries_in;
1514
1515 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1516 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001517 pair = hists__find_entry(other, pos);
1518
1519 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09001520 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001521 }
1522}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001523
1524/*
1525 * Look for entries in the other hists that are not present in the leader, if
1526 * we find them, just add a dummy entry on the leader hists, with period=0,
1527 * nr_events=0, to serve as the list header.
1528 */
1529int hists__link(struct hists *leader, struct hists *other)
1530{
Namhyung Kimce74f602012-12-10 17:29:55 +09001531 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001532 struct rb_node *nd;
1533 struct hist_entry *pos, *pair;
1534
Namhyung Kimce74f602012-12-10 17:29:55 +09001535 if (sort__need_collapse)
1536 root = &other->entries_collapsed;
1537 else
1538 root = other->entries_in;
1539
1540 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1541 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001542
1543 if (!hist_entry__has_pairs(pos)) {
1544 pair = hists__add_dummy_entry(leader, pos);
1545 if (pair == NULL)
1546 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09001547 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001548 }
1549 }
1550
1551 return 0;
1552}
Namhyung Kimf2148332014-01-14 11:52:48 +09001553
Andi Kleen578499982015-07-18 08:24:49 -07001554void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
1555 struct perf_sample *sample, bool nonany_branch_mode)
1556{
1557 struct branch_info *bi;
1558
1559 /* If we have branch cycles always annotate them. */
1560 if (bs && bs->nr && bs->entries[0].flags.cycles) {
1561 int i;
1562
1563 bi = sample__resolve_bstack(sample, al);
1564 if (bi) {
1565 struct addr_map_symbol *prev = NULL;
1566
1567 /*
1568 * Ignore errors, still want to process the
1569 * other entries.
1570 *
1571 * For non standard branch modes always
1572 * force no IPC (prev == NULL)
1573 *
1574 * Note that perf stores branches reversed from
1575 * program order!
1576 */
1577 for (i = bs->nr - 1; i >= 0; i--) {
1578 addr_map_symbol__account_cycles(&bi[i].from,
1579 nonany_branch_mode ? NULL : prev,
1580 bi[i].flags.cycles);
1581 prev = &bi[i].to;
1582 }
1583 free(bi);
1584 }
1585 }
1586}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03001587
1588size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
1589{
1590 struct perf_evsel *pos;
1591 size_t ret = 0;
1592
1593 evlist__for_each(evlist, pos) {
1594 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1595 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1596 }
1597
1598 return ret;
1599}
1600
1601
Namhyung Kimf2148332014-01-14 11:52:48 +09001602u64 hists__total_period(struct hists *hists)
1603{
1604 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1605 hists->stats.total_period;
1606}
Namhyung Kim33db4562014-02-07 12:06:07 +09001607
1608int parse_filter_percentage(const struct option *opt __maybe_unused,
1609 const char *arg, int unset __maybe_unused)
1610{
1611 if (!strcmp(arg, "relative"))
1612 symbol_conf.filter_relative = true;
1613 else if (!strcmp(arg, "absolute"))
1614 symbol_conf.filter_relative = false;
1615 else
1616 return -1;
1617
1618 return 0;
1619}
Namhyung Kim0b93da12014-01-14 12:02:15 +09001620
1621int perf_hist_config(const char *var, const char *value)
1622{
1623 if (!strcmp(var, "hist.percentage"))
1624 return parse_filter_percentage(NULL, value, 0);
1625
1626 return 0;
1627}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001628
Jiri Olsa5b658552016-01-18 10:24:22 +01001629int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001630{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001631 memset(hists, 0, sizeof(*hists));
1632 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1633 hists->entries_in = &hists->entries_in_array[0];
1634 hists->entries_collapsed = RB_ROOT;
1635 hists->entries = RB_ROOT;
1636 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04001637 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01001638 hists->hpp_list = hpp_list;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001639 return 0;
1640}
1641
Namhyung Kim61fa0e92015-12-10 16:53:20 +09001642static void hists__delete_remaining_entries(struct rb_root *root)
1643{
1644 struct rb_node *node;
1645 struct hist_entry *he;
1646
1647 while (!RB_EMPTY_ROOT(root)) {
1648 node = rb_first(root);
1649 rb_erase(node, root);
1650
1651 he = rb_entry(node, struct hist_entry, rb_node_in);
1652 hist_entry__delete(he);
1653 }
1654}
1655
1656static void hists__delete_all_entries(struct hists *hists)
1657{
1658 hists__delete_entries(hists);
1659 hists__delete_remaining_entries(&hists->entries_in_array[0]);
1660 hists__delete_remaining_entries(&hists->entries_in_array[1]);
1661 hists__delete_remaining_entries(&hists->entries_collapsed);
1662}
1663
Masami Hiramatsu17577de2015-12-09 11:11:29 +09001664static void hists_evsel__exit(struct perf_evsel *evsel)
1665{
1666 struct hists *hists = evsel__hists(evsel);
1667
Namhyung Kim61fa0e92015-12-10 16:53:20 +09001668 hists__delete_all_entries(hists);
Masami Hiramatsu17577de2015-12-09 11:11:29 +09001669}
1670
Namhyung Kimfc284be2016-01-07 10:14:10 +01001671static int hists_evsel__init(struct perf_evsel *evsel)
1672{
1673 struct hists *hists = evsel__hists(evsel);
1674
Jiri Olsa5b658552016-01-18 10:24:22 +01001675 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01001676 return 0;
1677}
1678
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001679/*
1680 * XXX We probably need a hists_evsel__exit() to free the hist_entries
1681 * stored in the rbtree...
1682 */
1683
1684int hists__init(void)
1685{
1686 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09001687 hists_evsel__init,
1688 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001689 if (err)
1690 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
1691
1692 return err;
1693}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01001694
1695void perf_hpp_list__init(struct perf_hpp_list *list)
1696{
1697 INIT_LIST_HEAD(&list->fields);
1698 INIT_LIST_HEAD(&list->sorts);
1699}