blob: a856617be74475182043e4bfad9d540abc1a167a [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());
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300182}
183
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900184void hists__output_recalc_col_len(struct hists *hists, int max_rows)
185{
186 struct rb_node *next = rb_first(&hists->entries);
187 struct hist_entry *n;
188 int row = 0;
189
190 hists__reset_col_len(hists);
191
192 while (next && row++ < max_rows) {
193 n = rb_entry(next, struct hist_entry, rb_node);
194 if (!n->filtered)
195 hists__calc_col_len(hists, n);
196 next = rb_next(&n->rb_node);
197 }
198}
199
Namhyung Kimf39056f2014-01-14 14:25:37 +0900200static void he_stat__add_cpumode_period(struct he_stat *he_stat,
201 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800202{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300203 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800204 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900205 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800206 break;
207 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900208 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800209 break;
210 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900211 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800212 break;
213 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900214 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800215 break;
216 default:
217 break;
218 }
219}
220
Andi Kleen05484292013-01-24 16:10:29 +0100221static void he_stat__add_period(struct he_stat *he_stat, u64 period,
222 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900223{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100224
Namhyung Kim139c0812012-10-04 21:49:43 +0900225 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100226 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900227 he_stat->nr_events += 1;
228}
229
230static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
231{
232 dest->period += src->period;
233 dest->period_sys += src->period_sys;
234 dest->period_us += src->period_us;
235 dest->period_guest_sys += src->period_guest_sys;
236 dest->period_guest_us += src->period_guest_us;
237 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100238 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900239}
240
Namhyung Kimf39056f2014-01-14 14:25:37 +0900241static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300242{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900243 he_stat->period = (he_stat->period * 7) / 8;
244 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100245 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300246}
247
248static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
249{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900250 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900251 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200252
253 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300254 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200255
Namhyung Kimf39056f2014-01-14 14:25:37 +0900256 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900257 if (symbol_conf.cumulate_callchain)
258 he_stat__decay(he->stat_acc);
Namhyung Kim42b276a2016-01-05 12:06:00 +0900259 decay_callchain(he->callchain);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200260
Namhyung Kim3186b682014-04-22 13:44:23 +0900261 diff = prev_period - he->stat.period;
262
263 hists->stats.total_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200264 if (!he->filtered)
Namhyung Kim3186b682014-04-22 13:44:23 +0900265 hists->stats.total_non_filtered_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200266
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900267 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300268}
269
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300270static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
271{
272 rb_erase(&he->rb_node, &hists->entries);
273
274 if (sort__need_collapse)
275 rb_erase(&he->rb_node_in, &hists->entries_collapsed);
Namhyung Kim61fa0e92015-12-10 16:53:20 +0900276 else
277 rb_erase(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300278
279 --hists->nr_entries;
280 if (!he->filtered)
281 --hists->nr_non_filtered_entries;
282
283 hist_entry__delete(he);
284}
285
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900286void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300287{
288 struct rb_node *next = rb_first(&hists->entries);
289 struct hist_entry *n;
290
291 while (next) {
292 n = rb_entry(next, struct hist_entry, rb_node);
293 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200294 if (((zap_user && n->level == '.') ||
295 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300296 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300297 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300298 }
299 }
300}
301
Namhyung Kim701937b2014-08-12 17:16:05 +0900302void hists__delete_entries(struct hists *hists)
303{
304 struct rb_node *next = rb_first(&hists->entries);
305 struct hist_entry *n;
306
307 while (next) {
308 n = rb_entry(next, struct hist_entry, rb_node);
309 next = rb_next(&n->rb_node);
310
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300311 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900312 }
313}
314
John Kacur3d1d07e2009-09-28 15:32:55 +0200315/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300316 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200317 */
318
Namhyung Kima0b51af2012-09-11 13:34:27 +0900319static struct hist_entry *hist_entry__new(struct hist_entry *template,
320 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300321{
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900322 size_t callchain_size = 0;
323 struct hist_entry *he;
324
Namhyung Kim82aa0192014-12-22 13:44:14 +0900325 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900326 callchain_size = sizeof(struct callchain_root);
327
328 he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300329
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200330 if (he != NULL) {
331 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900332
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900333 if (symbol_conf.cumulate_callchain) {
334 he->stat_acc = malloc(sizeof(he->stat));
335 if (he->stat_acc == NULL) {
336 free(he);
337 return NULL;
338 }
339 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
Namhyung Kima0b51af2012-09-11 13:34:27 +0900340 if (!sample_self)
341 memset(&he->stat, 0, sizeof(he->stat));
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900342 }
343
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300344 map__get(he->ms.map);
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100345
346 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900347 /*
348 * This branch info is (a part of) allocated from
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -0300349 * sample__resolve_bstack() and will be freed after
Namhyung Kim26353a62013-04-01 20:35:17 +0900350 * adding new entries. So we need to save a copy.
351 */
352 he->branch_info = malloc(sizeof(*he->branch_info));
353 if (he->branch_info == NULL) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300354 map__zput(he->ms.map);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900355 free(he->stat_acc);
Namhyung Kim26353a62013-04-01 20:35:17 +0900356 free(he);
357 return NULL;
358 }
359
360 memcpy(he->branch_info, template->branch_info,
361 sizeof(*he->branch_info));
362
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300363 map__get(he->branch_info->from.map);
364 map__get(he->branch_info->to.map);
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100365 }
366
Stephane Eranian98a3b322013-01-24 16:10:35 +0100367 if (he->mem_info) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300368 map__get(he->mem_info->iaddr.map);
369 map__get(he->mem_info->daddr.map);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100370 }
371
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300372 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200373 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200374
Namhyung Kim72392832015-12-24 11:16:17 +0900375 if (he->raw_data) {
376 he->raw_data = memdup(he->raw_data, he->raw_size);
377
378 if (he->raw_data == NULL) {
379 map__put(he->ms.map);
380 if (he->branch_info) {
381 map__put(he->branch_info->from.map);
382 map__put(he->branch_info->to.map);
383 free(he->branch_info);
384 }
385 if (he->mem_info) {
386 map__put(he->mem_info->iaddr.map);
387 map__put(he->mem_info->daddr.map);
388 }
389 free(he->stat_acc);
390 free(he);
391 return NULL;
392 }
393 }
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200394 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300395 thread__get(he->thread);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300396 }
397
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200398 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300399}
400
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300401static u8 symbol__parent_filter(const struct symbol *parent)
402{
403 if (symbol_conf.exclude_other && parent == NULL)
404 return 1 << HIST_FILTER__PARENT;
405 return 0;
406}
407
Namhyung Kim467ef102016-02-16 23:08:19 +0900408static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
409{
410 if (!symbol_conf.use_callchain)
411 return;
412
413 he->hists->callchain_period += period;
414 if (!he->filtered)
415 he->hists->callchain_non_filtered_period += period;
416}
417
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300418static struct hist_entry *hists__findnew_entry(struct hists *hists,
419 struct hist_entry *entry,
420 struct addr_location *al,
421 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300422{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300423 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300424 struct rb_node *parent = NULL;
425 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700426 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900427 u64 period = entry->stat.period;
428 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300429
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300430 p = &hists->entries_in->rb_node;
431
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300432 while (*p != NULL) {
433 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300434 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300435
Namhyung Kim9afcf932012-12-10 17:29:54 +0900436 /*
437 * Make sure that it receives arguments in a same order as
438 * hist_entry__collapse() so that we can use an appropriate
439 * function when searching an entry regardless which sort
440 * keys were used.
441 */
442 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300443
444 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900445 if (sample_self) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900446 he_stat__add_period(&he->stat, period, weight);
Namhyung Kim467ef102016-02-16 23:08:19 +0900447 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900448 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900449 if (symbol_conf.cumulate_callchain)
450 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400451
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900452 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300453 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900454 * and will not be used anymore.
455 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300456 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900457
David Miller63fa4712012-03-27 03:14:18 -0400458 /* If the map of an existing hist_entry has
459 * become out-of-date due to an exec() or
460 * similar, update it. Otherwise we will
461 * mis-adjust symbol addresses when computing
462 * the history counter to increment.
463 */
464 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300465 map__put(he->ms.map);
466 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400467 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300468 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300469 }
470
471 if (cmp < 0)
472 p = &(*p)->rb_left;
473 else
474 p = &(*p)->rb_right;
475 }
476
Namhyung Kima0b51af2012-09-11 13:34:27 +0900477 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300478 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900479 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300480
Namhyung Kim0f584742016-01-28 00:40:49 +0900481 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900482 hist_entry__add_callchain_period(he, period);
483 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900484
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300485 rb_link_node(&he->rb_node_in, parent, p);
486 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300487out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900488 if (sample_self)
489 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900490 if (symbol_conf.cumulate_callchain)
491 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300492 return he;
493}
494
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300495struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100496 struct addr_location *al,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900497 struct symbol *sym_parent,
498 struct branch_info *bi,
499 struct mem_info *mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900500 struct perf_sample *sample,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900501 bool sample_self)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100502{
503 struct hist_entry entry = {
504 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900505 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100506 .ms = {
507 .map = al->map,
508 .sym = al->sym,
509 },
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400510 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400511 .cpu = al->cpu,
512 .cpumode = al->cpumode,
513 .ip = al->addr,
514 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900515 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900516 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900517 .period = sample->period,
518 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900519 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100520 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300521 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300522 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900523 .branch_info = bi,
524 .mem_info = mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900525 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900526 .raw_data = sample->raw_data,
527 .raw_size = sample->raw_size,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100528 };
529
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300530 return hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100531}
532
Namhyung Kim69bcb012013-10-30 09:40:34 +0900533static int
534iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
535 struct addr_location *al __maybe_unused)
536{
537 return 0;
538}
539
540static int
541iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
542 struct addr_location *al __maybe_unused)
543{
544 return 0;
545}
546
547static int
548iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
549{
550 struct perf_sample *sample = iter->sample;
551 struct mem_info *mi;
552
553 mi = sample__resolve_mem(sample, al);
554 if (mi == NULL)
555 return -ENOMEM;
556
557 iter->priv = mi;
558 return 0;
559}
560
561static int
562iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
563{
564 u64 cost;
565 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300566 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900567 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900568 struct hist_entry *he;
569
570 if (mi == NULL)
571 return -EINVAL;
572
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900573 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900574 if (!cost)
575 cost = 1;
576
577 /*
578 * must pass period=weight in order to get the correct
579 * sorting from hists__collapse_resort() which is solely
580 * based on periods. We want sorting be done on nr_events * weight
581 * and this is indirectly achieved by passing period=weight here
582 * and the he_stat__add_period() function.
583 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900584 sample->period = cost;
585
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300586 he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900587 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900588 if (!he)
589 return -ENOMEM;
590
591 iter->he = he;
592 return 0;
593}
594
595static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900596iter_finish_mem_entry(struct hist_entry_iter *iter,
597 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900598{
599 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300600 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900601 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900602 int err = -EINVAL;
603
604 if (he == NULL)
605 goto out;
606
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300607 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900608
609 err = hist_entry__append_callchain(he, iter->sample);
610
611out:
612 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300613 * We don't need to free iter->priv (mem_info) here since the mem info
614 * was either already freed in hists__findnew_entry() or passed to a
615 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900616 */
617 iter->priv = NULL;
618
619 iter->he = NULL;
620 return err;
621}
622
623static int
624iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
625{
626 struct branch_info *bi;
627 struct perf_sample *sample = iter->sample;
628
629 bi = sample__resolve_bstack(sample, al);
630 if (!bi)
631 return -ENOMEM;
632
633 iter->curr = 0;
634 iter->total = sample->branch_stack->nr;
635
636 iter->priv = bi;
637 return 0;
638}
639
640static int
641iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
642 struct addr_location *al __maybe_unused)
643{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900644 /* to avoid calling callback function */
645 iter->he = NULL;
646
Namhyung Kim69bcb012013-10-30 09:40:34 +0900647 return 0;
648}
649
650static int
651iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
652{
653 struct branch_info *bi = iter->priv;
654 int i = iter->curr;
655
656 if (bi == NULL)
657 return 0;
658
659 if (iter->curr >= iter->total)
660 return 0;
661
662 al->map = bi[i].to.map;
663 al->sym = bi[i].to.sym;
664 al->addr = bi[i].to.addr;
665 return 1;
666}
667
668static int
669iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
670{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900671 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900672 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300673 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900674 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900675 struct hist_entry *he = NULL;
676 int i = iter->curr;
677 int err = 0;
678
679 bi = iter->priv;
680
681 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
682 goto out;
683
684 /*
685 * The report shows the percentage of total branches captured
686 * and not events sampled. Thus we use a pseudo period of 1.
687 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900688 sample->period = 1;
689 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
690
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300691 he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900692 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900693 if (he == NULL)
694 return -ENOMEM;
695
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300696 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900697
698out:
699 iter->he = he;
700 iter->curr++;
701 return err;
702}
703
704static int
705iter_finish_branch_entry(struct hist_entry_iter *iter,
706 struct addr_location *al __maybe_unused)
707{
708 zfree(&iter->priv);
709 iter->he = NULL;
710
711 return iter->curr >= iter->total ? 0 : -1;
712}
713
714static int
715iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
716 struct addr_location *al __maybe_unused)
717{
718 return 0;
719}
720
721static int
722iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
723{
724 struct perf_evsel *evsel = iter->evsel;
725 struct perf_sample *sample = iter->sample;
726 struct hist_entry *he;
727
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300728 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900729 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900730 if (he == NULL)
731 return -ENOMEM;
732
733 iter->he = he;
734 return 0;
735}
736
737static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900738iter_finish_normal_entry(struct hist_entry_iter *iter,
739 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900740{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900741 struct hist_entry *he = iter->he;
742 struct perf_evsel *evsel = iter->evsel;
743 struct perf_sample *sample = iter->sample;
744
745 if (he == NULL)
746 return 0;
747
748 iter->he = NULL;
749
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300750 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900751
752 return hist_entry__append_callchain(he, sample);
753}
754
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900755static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300756iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900757 struct addr_location *al __maybe_unused)
758{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900759 struct hist_entry **he_cache;
760
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900761 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900762
763 /*
764 * This is for detecting cycles or recursions so that they're
765 * cumulated only one time to prevent entries more than 100%
766 * overhead.
767 */
Adrian Hunter96b40f32015-09-25 16:15:47 +0300768 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900769 if (he_cache == NULL)
770 return -ENOMEM;
771
772 iter->priv = he_cache;
773 iter->curr = 0;
774
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900775 return 0;
776}
777
778static int
779iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
780 struct addr_location *al)
781{
782 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300783 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900784 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900785 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900786 struct hist_entry *he;
787 int err = 0;
788
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300789 he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900790 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900791 if (he == NULL)
792 return -ENOMEM;
793
794 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900795 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900796
Namhyung Kim82aa0192014-12-22 13:44:14 +0900797 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900798
799 /*
800 * We need to re-initialize the cursor since callchain_append()
801 * advanced the cursor to the end.
802 */
803 callchain_cursor_commit(&callchain_cursor);
804
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300805 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900806
807 return err;
808}
809
810static int
811iter_next_cumulative_entry(struct hist_entry_iter *iter,
812 struct addr_location *al)
813{
814 struct callchain_cursor_node *node;
815
816 node = callchain_cursor_current(&callchain_cursor);
817 if (node == NULL)
818 return 0;
819
Namhyung Kimc7405d82013-10-31 13:58:30 +0900820 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900821}
822
823static int
824iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
825 struct addr_location *al)
826{
827 struct perf_evsel *evsel = iter->evsel;
828 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900829 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900830 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900831 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -0300832 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900833 .cpu = al->cpu,
834 .thread = al->thread,
835 .comm = thread__comm(al->thread),
836 .ip = al->addr,
837 .ms = {
838 .map = al->map,
839 .sym = al->sym,
840 },
841 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +0900842 .raw_data = sample->raw_data,
843 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900844 };
845 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900846 struct callchain_cursor cursor;
847
848 callchain_cursor_snapshot(&cursor, &callchain_cursor);
849
850 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900851
852 /*
853 * Check if there's duplicate entries in the callchain.
854 * It's possible that it has cycles or recursive calls.
855 */
856 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900857 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
858 /* to avoid calling callback function */
859 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900860 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900861 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900862 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900863
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300864 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900865 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900866 if (he == NULL)
867 return -ENOMEM;
868
869 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900870 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900871
Namhyung Kim82aa0192014-12-22 13:44:14 +0900872 if (symbol_conf.use_callchain)
873 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900874 return 0;
875}
876
877static int
878iter_finish_cumulative_entry(struct hist_entry_iter *iter,
879 struct addr_location *al __maybe_unused)
880{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900881 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900882 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900883
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900884 return 0;
885}
886
Namhyung Kim69bcb012013-10-30 09:40:34 +0900887const struct hist_iter_ops hist_iter_mem = {
888 .prepare_entry = iter_prepare_mem_entry,
889 .add_single_entry = iter_add_single_mem_entry,
890 .next_entry = iter_next_nop_entry,
891 .add_next_entry = iter_add_next_nop_entry,
892 .finish_entry = iter_finish_mem_entry,
893};
894
895const struct hist_iter_ops hist_iter_branch = {
896 .prepare_entry = iter_prepare_branch_entry,
897 .add_single_entry = iter_add_single_branch_entry,
898 .next_entry = iter_next_branch_entry,
899 .add_next_entry = iter_add_next_branch_entry,
900 .finish_entry = iter_finish_branch_entry,
901};
902
903const struct hist_iter_ops hist_iter_normal = {
904 .prepare_entry = iter_prepare_normal_entry,
905 .add_single_entry = iter_add_single_normal_entry,
906 .next_entry = iter_next_nop_entry,
907 .add_next_entry = iter_add_next_nop_entry,
908 .finish_entry = iter_finish_normal_entry,
909};
910
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900911const struct hist_iter_ops hist_iter_cumulative = {
912 .prepare_entry = iter_prepare_cumulative_entry,
913 .add_single_entry = iter_add_single_cumulative_entry,
914 .next_entry = iter_next_cumulative_entry,
915 .add_next_entry = iter_add_next_cumulative_entry,
916 .finish_entry = iter_finish_cumulative_entry,
917};
918
Namhyung Kim69bcb012013-10-30 09:40:34 +0900919int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900920 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900921{
922 int err, err2;
923
Namhyung Kim063bd932015-05-19 17:04:10 +0900924 err = sample__resolve_callchain(iter->sample, &iter->parent,
925 iter->evsel, al, max_stack_depth);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900926 if (err)
927 return err;
928
Adrian Hunter96b40f32015-09-25 16:15:47 +0300929 iter->max_stack = max_stack_depth;
930
Namhyung Kim69bcb012013-10-30 09:40:34 +0900931 err = iter->ops->prepare_entry(iter, al);
932 if (err)
933 goto out;
934
935 err = iter->ops->add_single_entry(iter, al);
936 if (err)
937 goto out;
938
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900939 if (iter->he && iter->add_entry_cb) {
940 err = iter->add_entry_cb(iter, al, true, arg);
941 if (err)
942 goto out;
943 }
944
Namhyung Kim69bcb012013-10-30 09:40:34 +0900945 while (iter->ops->next_entry(iter, al)) {
946 err = iter->ops->add_next_entry(iter, al);
947 if (err)
948 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900949
950 if (iter->he && iter->add_entry_cb) {
951 err = iter->add_entry_cb(iter, al, false, arg);
952 if (err)
953 goto out;
954 }
Namhyung Kim69bcb012013-10-30 09:40:34 +0900955 }
956
957out:
958 err2 = iter->ops->finish_entry(iter, al);
959 if (!err)
960 err = err2;
961
962 return err;
963}
964
John Kacur3d1d07e2009-09-28 15:32:55 +0200965int64_t
966hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
967{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +0100968 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900969 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200970 int64_t cmp = 0;
971
Jiri Olsaaa6f50a2016-01-18 10:24:24 +0100972 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900973 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200974 if (cmp)
975 break;
976 }
977
978 return cmp;
979}
980
981int64_t
982hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
983{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +0100984 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900985 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200986 int64_t cmp = 0;
987
Jiri Olsaaa6f50a2016-01-18 10:24:24 +0100988 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900989 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200990 if (cmp)
991 break;
992 }
993
994 return cmp;
995}
996
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -0300997void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200998{
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300999 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001000 map__zput(he->ms.map);
1001
1002 if (he->branch_info) {
1003 map__zput(he->branch_info->from.map);
1004 map__zput(he->branch_info->to.map);
1005 zfree(&he->branch_info);
1006 }
1007
1008 if (he->mem_info) {
1009 map__zput(he->mem_info->iaddr.map);
1010 map__zput(he->mem_info->daddr.map);
1011 zfree(&he->mem_info);
1012 }
1013
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001014 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +09001015 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -07001016 if (he->srcfile && he->srcfile[0])
1017 free(he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001018 free_callchain(he->callchain);
Namhyung Kim60517d22015-12-23 02:07:03 +09001019 free(he->trace_output);
Namhyung Kim72392832015-12-24 11:16:17 +09001020 free(he->raw_data);
John Kacur3d1d07e2009-09-28 15:32:55 +02001021 free(he);
1022}
1023
1024/*
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001025 * If this is not the last column, then we need to pad it according to the
1026 * pre-calculated max lenght for this column, otherwise don't bother adding
1027 * spaces because that would break viewing this with, for instance, 'less',
1028 * that would show tons of trailing spaces when a long C++ demangled method
1029 * names is sampled.
1030*/
1031int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1032 struct perf_hpp_fmt *fmt, int printed)
1033{
1034 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
1035 const int width = fmt->width(fmt, hpp, hists_to_evsel(he->hists));
1036 if (printed < width) {
1037 advance_hpp(hpp, printed);
1038 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1039 }
1040 }
1041
1042 return printed;
1043}
1044
1045/*
John Kacur3d1d07e2009-09-28 15:32:55 +02001046 * collapse the histogram
1047 */
1048
Namhyung Kimfc284be2016-01-07 10:14:10 +01001049bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
1050 struct rb_root *root, struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001051{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001052 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001053 struct rb_node *parent = NULL;
1054 struct hist_entry *iter;
1055 int64_t cmp;
1056
1057 while (*p != NULL) {
1058 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001059 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001060
1061 cmp = hist_entry__collapse(iter, he);
1062
1063 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +09001064 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001065 if (symbol_conf.cumulate_callchain)
1066 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001067
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001068 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001069 callchain_cursor_reset(&callchain_cursor);
1070 callchain_merge(&callchain_cursor,
1071 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001072 he->callchain);
1073 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001074 hist_entry__delete(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001075 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +02001076 }
1077
1078 if (cmp < 0)
1079 p = &(*p)->rb_left;
1080 else
1081 p = &(*p)->rb_right;
1082 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001083 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001084
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001085 rb_link_node(&he->rb_node_in, parent, p);
1086 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001087 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +02001088}
1089
Namhyung Kimfc284be2016-01-07 10:14:10 +01001090struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001091{
1092 struct rb_root *root;
1093
1094 pthread_mutex_lock(&hists->lock);
1095
1096 root = hists->entries_in;
1097 if (++hists->entries_in > &hists->entries_in_array[1])
1098 hists->entries_in = &hists->entries_in_array[0];
1099
1100 pthread_mutex_unlock(&hists->lock);
1101
1102 return root;
1103}
1104
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001105static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1106{
1107 hists__filter_entry_by_dso(hists, he);
1108 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001109 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001110 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001111}
1112
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001113void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001114{
1115 struct rb_root *root;
1116 struct rb_node *next;
1117 struct hist_entry *n;
1118
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001119 if (!sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001120 return;
1121
Namhyung Kim740b97f2014-12-22 13:44:10 +09001122 hists->nr_entries = 0;
1123
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001124 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001125
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001126 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001127
1128 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001129 if (session_done())
1130 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001131 n = rb_entry(next, struct hist_entry, rb_node_in);
1132 next = rb_next(&n->rb_node_in);
1133
1134 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001135 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
1136 /*
1137 * If it wasn't combined with one of the entries already
1138 * collapsed, we need to apply the filters that may have
1139 * been set by, say, the hist_browser.
1140 */
1141 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001142 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001143 if (prog)
1144 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001145 }
1146}
1147
Namhyung Kim043ca3892014-03-03 14:18:00 +09001148static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001149{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001150 struct hists *hists = a->hists;
Namhyung Kim043ca3892014-03-03 14:18:00 +09001151 struct perf_hpp_fmt *fmt;
1152 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001153
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001154 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001155 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001156 continue;
1157
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001158 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001159 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001160 break;
1161 }
1162
Namhyung Kim043ca3892014-03-03 14:18:00 +09001163 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001164}
1165
Namhyung Kim9283ba92014-04-24 16:37:26 +09001166static void hists__reset_filter_stats(struct hists *hists)
1167{
1168 hists->nr_non_filtered_entries = 0;
1169 hists->stats.total_non_filtered_period = 0;
1170}
1171
1172void hists__reset_stats(struct hists *hists)
1173{
1174 hists->nr_entries = 0;
1175 hists->stats.total_period = 0;
1176
1177 hists__reset_filter_stats(hists);
1178}
1179
1180static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1181{
1182 hists->nr_non_filtered_entries++;
1183 hists->stats.total_non_filtered_period += h->stat.period;
1184}
1185
1186void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1187{
1188 if (!h->filtered)
1189 hists__inc_filter_stats(hists, h);
1190
1191 hists->nr_entries++;
1192 hists->stats.total_period += h->stat.period;
1193}
1194
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001195static void __hists__insert_output_entry(struct rb_root *entries,
1196 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001197 u64 min_callchain_hits,
1198 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001199{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001200 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001201 struct rb_node *parent = NULL;
1202 struct hist_entry *iter;
1203
Namhyung Kim744070e2016-01-28 00:40:48 +09001204 if (use_callchain) {
1205 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1206 u64 total = he->stat.period;
1207
1208 if (symbol_conf.cumulate_callchain)
1209 total = he->stat_acc->period;
1210
1211 min_callchain_hits = total * (callchain_param.min_percent / 100);
1212 }
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001213 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001214 min_callchain_hits, &callchain_param);
Namhyung Kim744070e2016-01-28 00:40:48 +09001215 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001216
1217 while (*p != NULL) {
1218 parent = *p;
1219 iter = rb_entry(parent, struct hist_entry, rb_node);
1220
Namhyung Kim043ca3892014-03-03 14:18:00 +09001221 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001222 p = &(*p)->rb_left;
1223 else
1224 p = &(*p)->rb_right;
1225 }
1226
1227 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001228 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +02001229}
1230
Jiri Olsa01441af2016-01-18 10:23:59 +01001231static void output_resort(struct hists *hists, struct ui_progress *prog,
1232 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001233{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001234 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001235 struct rb_node *next;
1236 struct hist_entry *n;
Namhyung Kim467ef102016-02-16 23:08:19 +09001237 u64 callchain_total;
John Kacur3d1d07e2009-09-28 15:32:55 +02001238 u64 min_callchain_hits;
1239
Namhyung Kim467ef102016-02-16 23:08:19 +09001240 callchain_total = hists->callchain_period;
1241 if (symbol_conf.filter_relative)
1242 callchain_total = hists->callchain_non_filtered_period;
1243
1244 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001245
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001246 if (sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001247 root = &hists->entries_collapsed;
1248 else
1249 root = hists->entries_in;
1250
1251 next = rb_first(root);
1252 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001253
Namhyung Kim9283ba92014-04-24 16:37:26 +09001254 hists__reset_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001255 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001256
John Kacur3d1d07e2009-09-28 15:32:55 +02001257 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001258 n = rb_entry(next, struct hist_entry, rb_node_in);
1259 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001260
Kan Liangf9db0d02015-08-11 06:30:48 -04001261 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001262 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001263
1264 if (!n->filtered)
1265 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001266
1267 if (prog)
1268 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001269 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001270}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001271
Jiri Olsa452ce032016-01-18 10:24:00 +01001272void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
Jiri Olsa01441af2016-01-18 10:23:59 +01001273{
Jiri Olsa01441af2016-01-18 10:23:59 +01001274 bool use_callchain;
1275
1276 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1277 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1278 else
1279 use_callchain = symbol_conf.use_callchain;
1280
Jiri Olsa452ce032016-01-18 10:24:00 +01001281 output_resort(evsel__hists(evsel), prog, use_callchain);
1282}
1283
1284void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1285{
1286 output_resort(hists, prog, symbol_conf.use_callchain);
Jiri Olsa01441af2016-01-18 10:23:59 +01001287}
1288
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001289static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001290 enum hist_filter filter)
1291{
1292 h->filtered &= ~(1 << filter);
1293 if (h->filtered)
1294 return;
1295
Namhyung Kim87e90f42014-04-24 16:44:16 +09001296 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001297 h->unfolded = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001298 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001299 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001300
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001301 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001302
Namhyung Kim9283ba92014-04-24 16:37:26 +09001303 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001304 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001305}
1306
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001307
1308static bool hists__filter_entry_by_dso(struct hists *hists,
1309 struct hist_entry *he)
1310{
1311 if (hists->dso_filter != NULL &&
1312 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1313 he->filtered |= (1 << HIST_FILTER__DSO);
1314 return true;
1315 }
1316
1317 return false;
1318}
1319
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001320static bool hists__filter_entry_by_thread(struct hists *hists,
1321 struct hist_entry *he)
1322{
1323 if (hists->thread_filter != NULL &&
1324 he->thread != hists->thread_filter) {
1325 he->filtered |= (1 << HIST_FILTER__THREAD);
1326 return true;
1327 }
1328
1329 return false;
1330}
1331
Namhyung Kime94d53e2012-03-16 17:50:51 +09001332static bool hists__filter_entry_by_symbol(struct hists *hists,
1333 struct hist_entry *he)
1334{
1335 if (hists->symbol_filter_str != NULL &&
1336 (!he->ms.sym || strstr(he->ms.sym->name,
1337 hists->symbol_filter_str) == NULL)) {
1338 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1339 return true;
1340 }
1341
1342 return false;
1343}
1344
Kan Liang21394d92015-09-04 10:45:44 -04001345static bool hists__filter_entry_by_socket(struct hists *hists,
1346 struct hist_entry *he)
1347{
1348 if ((hists->socket_filter > -1) &&
1349 (he->socket != hists->socket_filter)) {
1350 he->filtered |= (1 << HIST_FILTER__SOCKET);
1351 return true;
1352 }
1353
1354 return false;
1355}
1356
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001357typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1358
1359static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04001360{
1361 struct rb_node *nd;
1362
1363 hists->stats.nr_non_filtered_samples = 0;
1364
1365 hists__reset_filter_stats(hists);
1366 hists__reset_col_len(hists);
1367
1368 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1369 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1370
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001371 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04001372 continue;
1373
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001374 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04001375 }
1376}
1377
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001378void hists__filter_by_thread(struct hists *hists)
1379{
1380 hists__filter_by_type(hists, HIST_FILTER__THREAD,
1381 hists__filter_entry_by_thread);
1382}
1383
1384void hists__filter_by_dso(struct hists *hists)
1385{
1386 hists__filter_by_type(hists, HIST_FILTER__DSO,
1387 hists__filter_entry_by_dso);
1388}
1389
1390void hists__filter_by_symbol(struct hists *hists)
1391{
1392 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
1393 hists__filter_entry_by_symbol);
1394}
1395
1396void hists__filter_by_socket(struct hists *hists)
1397{
1398 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
1399 hists__filter_entry_by_socket);
1400}
1401
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001402void events_stats__inc(struct events_stats *stats, u32 type)
1403{
1404 ++stats->nr_events[0];
1405 ++stats->nr_events[type];
1406}
1407
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001408void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001409{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001410 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001411}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001412
Namhyung Kim1844dbc2014-05-28 14:12:18 +09001413void hists__inc_nr_samples(struct hists *hists, bool filtered)
1414{
1415 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1416 if (!filtered)
1417 hists->stats.nr_non_filtered_samples++;
1418}
1419
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001420static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1421 struct hist_entry *pair)
1422{
Namhyung Kimce74f602012-12-10 17:29:55 +09001423 struct rb_root *root;
1424 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001425 struct rb_node *parent = NULL;
1426 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07001427 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001428
Namhyung Kimce74f602012-12-10 17:29:55 +09001429 if (sort__need_collapse)
1430 root = &hists->entries_collapsed;
1431 else
1432 root = hists->entries_in;
1433
1434 p = &root->rb_node;
1435
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001436 while (*p != NULL) {
1437 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09001438 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001439
Namhyung Kimce74f602012-12-10 17:29:55 +09001440 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001441
1442 if (!cmp)
1443 goto out;
1444
1445 if (cmp < 0)
1446 p = &(*p)->rb_left;
1447 else
1448 p = &(*p)->rb_right;
1449 }
1450
Namhyung Kima0b51af2012-09-11 13:34:27 +09001451 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001452 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03001453 memset(&he->stat, 0, sizeof(he->stat));
1454 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +09001455 rb_link_node(&he->rb_node_in, parent, p);
1456 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09001457 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01001458 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001459 }
1460out:
1461 return he;
1462}
1463
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001464static struct hist_entry *hists__find_entry(struct hists *hists,
1465 struct hist_entry *he)
1466{
Namhyung Kimce74f602012-12-10 17:29:55 +09001467 struct rb_node *n;
1468
1469 if (sort__need_collapse)
1470 n = hists->entries_collapsed.rb_node;
1471 else
1472 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001473
1474 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09001475 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1476 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001477
1478 if (cmp < 0)
1479 n = n->rb_left;
1480 else if (cmp > 0)
1481 n = n->rb_right;
1482 else
1483 return iter;
1484 }
1485
1486 return NULL;
1487}
1488
1489/*
1490 * Look for pairs to link to the leader buckets (hist_entries):
1491 */
1492void hists__match(struct hists *leader, struct hists *other)
1493{
Namhyung Kimce74f602012-12-10 17:29:55 +09001494 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001495 struct rb_node *nd;
1496 struct hist_entry *pos, *pair;
1497
Namhyung Kimce74f602012-12-10 17:29:55 +09001498 if (sort__need_collapse)
1499 root = &leader->entries_collapsed;
1500 else
1501 root = leader->entries_in;
1502
1503 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1504 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001505 pair = hists__find_entry(other, pos);
1506
1507 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09001508 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001509 }
1510}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001511
1512/*
1513 * Look for entries in the other hists that are not present in the leader, if
1514 * we find them, just add a dummy entry on the leader hists, with period=0,
1515 * nr_events=0, to serve as the list header.
1516 */
1517int hists__link(struct hists *leader, struct hists *other)
1518{
Namhyung Kimce74f602012-12-10 17:29:55 +09001519 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001520 struct rb_node *nd;
1521 struct hist_entry *pos, *pair;
1522
Namhyung Kimce74f602012-12-10 17:29:55 +09001523 if (sort__need_collapse)
1524 root = &other->entries_collapsed;
1525 else
1526 root = other->entries_in;
1527
1528 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1529 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001530
1531 if (!hist_entry__has_pairs(pos)) {
1532 pair = hists__add_dummy_entry(leader, pos);
1533 if (pair == NULL)
1534 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09001535 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001536 }
1537 }
1538
1539 return 0;
1540}
Namhyung Kimf2148332014-01-14 11:52:48 +09001541
Andi Kleen578499982015-07-18 08:24:49 -07001542void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
1543 struct perf_sample *sample, bool nonany_branch_mode)
1544{
1545 struct branch_info *bi;
1546
1547 /* If we have branch cycles always annotate them. */
1548 if (bs && bs->nr && bs->entries[0].flags.cycles) {
1549 int i;
1550
1551 bi = sample__resolve_bstack(sample, al);
1552 if (bi) {
1553 struct addr_map_symbol *prev = NULL;
1554
1555 /*
1556 * Ignore errors, still want to process the
1557 * other entries.
1558 *
1559 * For non standard branch modes always
1560 * force no IPC (prev == NULL)
1561 *
1562 * Note that perf stores branches reversed from
1563 * program order!
1564 */
1565 for (i = bs->nr - 1; i >= 0; i--) {
1566 addr_map_symbol__account_cycles(&bi[i].from,
1567 nonany_branch_mode ? NULL : prev,
1568 bi[i].flags.cycles);
1569 prev = &bi[i].to;
1570 }
1571 free(bi);
1572 }
1573 }
1574}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03001575
1576size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
1577{
1578 struct perf_evsel *pos;
1579 size_t ret = 0;
1580
1581 evlist__for_each(evlist, pos) {
1582 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1583 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1584 }
1585
1586 return ret;
1587}
1588
1589
Namhyung Kimf2148332014-01-14 11:52:48 +09001590u64 hists__total_period(struct hists *hists)
1591{
1592 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1593 hists->stats.total_period;
1594}
Namhyung Kim33db4562014-02-07 12:06:07 +09001595
1596int parse_filter_percentage(const struct option *opt __maybe_unused,
1597 const char *arg, int unset __maybe_unused)
1598{
1599 if (!strcmp(arg, "relative"))
1600 symbol_conf.filter_relative = true;
1601 else if (!strcmp(arg, "absolute"))
1602 symbol_conf.filter_relative = false;
1603 else
1604 return -1;
1605
1606 return 0;
1607}
Namhyung Kim0b93da12014-01-14 12:02:15 +09001608
1609int perf_hist_config(const char *var, const char *value)
1610{
1611 if (!strcmp(var, "hist.percentage"))
1612 return parse_filter_percentage(NULL, value, 0);
1613
1614 return 0;
1615}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001616
Jiri Olsa5b658552016-01-18 10:24:22 +01001617int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001618{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001619 memset(hists, 0, sizeof(*hists));
1620 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1621 hists->entries_in = &hists->entries_in_array[0];
1622 hists->entries_collapsed = RB_ROOT;
1623 hists->entries = RB_ROOT;
1624 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04001625 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01001626 hists->hpp_list = hpp_list;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001627 return 0;
1628}
1629
Namhyung Kim61fa0e92015-12-10 16:53:20 +09001630static void hists__delete_remaining_entries(struct rb_root *root)
1631{
1632 struct rb_node *node;
1633 struct hist_entry *he;
1634
1635 while (!RB_EMPTY_ROOT(root)) {
1636 node = rb_first(root);
1637 rb_erase(node, root);
1638
1639 he = rb_entry(node, struct hist_entry, rb_node_in);
1640 hist_entry__delete(he);
1641 }
1642}
1643
1644static void hists__delete_all_entries(struct hists *hists)
1645{
1646 hists__delete_entries(hists);
1647 hists__delete_remaining_entries(&hists->entries_in_array[0]);
1648 hists__delete_remaining_entries(&hists->entries_in_array[1]);
1649 hists__delete_remaining_entries(&hists->entries_collapsed);
1650}
1651
Masami Hiramatsu17577de2015-12-09 11:11:29 +09001652static void hists_evsel__exit(struct perf_evsel *evsel)
1653{
1654 struct hists *hists = evsel__hists(evsel);
1655
Namhyung Kim61fa0e92015-12-10 16:53:20 +09001656 hists__delete_all_entries(hists);
Masami Hiramatsu17577de2015-12-09 11:11:29 +09001657}
1658
Namhyung Kimfc284be2016-01-07 10:14:10 +01001659static int hists_evsel__init(struct perf_evsel *evsel)
1660{
1661 struct hists *hists = evsel__hists(evsel);
1662
Jiri Olsa5b658552016-01-18 10:24:22 +01001663 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01001664 return 0;
1665}
1666
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001667/*
1668 * XXX We probably need a hists_evsel__exit() to free the hist_entries
1669 * stored in the rbtree...
1670 */
1671
1672int hists__init(void)
1673{
1674 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09001675 hists_evsel__init,
1676 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001677 if (err)
1678 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
1679
1680 return err;
1681}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01001682
1683void perf_hpp_list__init(struct perf_hpp_list *list)
1684{
1685 INIT_LIST_HEAD(&list->fields);
1686 INIT_LIST_HEAD(&list->sorts);
1687}