blob: 56e97f5af5984f20a3af0157874a1e91aae6ffb9 [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);
134 }
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200135
136 if (h->mem_info->iaddr.sym) {
137 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
138 + unresolved_col_width + 2;
139 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
140 symlen);
141 } else {
142 symlen = unresolved_col_width + 4 + 2;
143 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
144 symlen);
145 }
146
Stephane Eranian98a3b322013-01-24 16:10:35 +0100147 if (h->mem_info->daddr.map) {
148 symlen = dso__name_len(h->mem_info->daddr.map->dso);
149 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
150 symlen);
151 } else {
152 symlen = unresolved_col_width + 4 + 2;
153 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
154 }
155 } else {
156 symlen = unresolved_col_width + 4 + 2;
157 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200158 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100159 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
160 }
161
Arnaldo Carvalho de Meloa4978ec2015-09-09 12:14:00 -0300162 hists__new_col_len(hists, HISTC_CPU, 3);
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400163 hists__new_col_len(hists, HISTC_SOCKET, 6);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100164 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
165 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
166 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
167 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
168 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
169 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700170
Arnaldo Carvalho de Meloe8e6d372015-08-10 16:53:54 -0300171 if (h->srcline)
172 hists__new_col_len(hists, HISTC_SRCLINE, strlen(h->srcline));
173
Andi Kleen31191a82015-08-07 15:54:24 -0700174 if (h->srcfile)
175 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
176
Andi Kleen475eeab2013-09-20 07:40:43 -0700177 if (h->transaction)
178 hists__new_col_len(hists, HISTC_TRANSACTION,
179 hist_entry__transaction_len());
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300180}
181
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900182void hists__output_recalc_col_len(struct hists *hists, int max_rows)
183{
184 struct rb_node *next = rb_first(&hists->entries);
185 struct hist_entry *n;
186 int row = 0;
187
188 hists__reset_col_len(hists);
189
190 while (next && row++ < max_rows) {
191 n = rb_entry(next, struct hist_entry, rb_node);
192 if (!n->filtered)
193 hists__calc_col_len(hists, n);
194 next = rb_next(&n->rb_node);
195 }
196}
197
Namhyung Kimf39056f2014-01-14 14:25:37 +0900198static void he_stat__add_cpumode_period(struct he_stat *he_stat,
199 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800200{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300201 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800202 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900203 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800204 break;
205 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900206 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800207 break;
208 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900209 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800210 break;
211 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900212 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800213 break;
214 default:
215 break;
216 }
217}
218
Andi Kleen05484292013-01-24 16:10:29 +0100219static void he_stat__add_period(struct he_stat *he_stat, u64 period,
220 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900221{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100222
Namhyung Kim139c0812012-10-04 21:49:43 +0900223 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100224 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900225 he_stat->nr_events += 1;
226}
227
228static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
229{
230 dest->period += src->period;
231 dest->period_sys += src->period_sys;
232 dest->period_us += src->period_us;
233 dest->period_guest_sys += src->period_guest_sys;
234 dest->period_guest_us += src->period_guest_us;
235 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100236 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900237}
238
Namhyung Kimf39056f2014-01-14 14:25:37 +0900239static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300240{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900241 he_stat->period = (he_stat->period * 7) / 8;
242 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100243 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300244}
245
246static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
247{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900248 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900249 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200250
251 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300252 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200253
Namhyung Kimf39056f2014-01-14 14:25:37 +0900254 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900255 if (symbol_conf.cumulate_callchain)
256 he_stat__decay(he->stat_acc);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200257
Namhyung Kim3186b682014-04-22 13:44:23 +0900258 diff = prev_period - he->stat.period;
259
260 hists->stats.total_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200261 if (!he->filtered)
Namhyung Kim3186b682014-04-22 13:44:23 +0900262 hists->stats.total_non_filtered_period -= diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200263
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900264 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300265}
266
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300267static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
268{
269 rb_erase(&he->rb_node, &hists->entries);
270
271 if (sort__need_collapse)
272 rb_erase(&he->rb_node_in, &hists->entries_collapsed);
Namhyung Kim61fa0e92015-12-10 16:53:20 +0900273 else
274 rb_erase(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300275
276 --hists->nr_entries;
277 if (!he->filtered)
278 --hists->nr_non_filtered_entries;
279
280 hist_entry__delete(he);
281}
282
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900283void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300284{
285 struct rb_node *next = rb_first(&hists->entries);
286 struct hist_entry *n;
287
288 while (next) {
289 n = rb_entry(next, struct hist_entry, rb_node);
290 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200291 if (((zap_user && n->level == '.') ||
292 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300293 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300294 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3f2011-10-05 19:16:15 -0300295 }
296 }
297}
298
Namhyung Kim701937b2014-08-12 17:16:05 +0900299void hists__delete_entries(struct hists *hists)
300{
301 struct rb_node *next = rb_first(&hists->entries);
302 struct hist_entry *n;
303
304 while (next) {
305 n = rb_entry(next, struct hist_entry, rb_node);
306 next = rb_next(&n->rb_node);
307
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300308 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900309 }
310}
311
John Kacur3d1d07e2009-09-28 15:32:55 +0200312/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300313 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200314 */
315
Namhyung Kima0b51af2012-09-11 13:34:27 +0900316static struct hist_entry *hist_entry__new(struct hist_entry *template,
317 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300318{
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900319 size_t callchain_size = 0;
320 struct hist_entry *he;
321
Namhyung Kim82aa0192014-12-22 13:44:14 +0900322 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900323 callchain_size = sizeof(struct callchain_root);
324
325 he = zalloc(sizeof(*he) + callchain_size);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300326
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200327 if (he != NULL) {
328 *he = *template;
Namhyung Kimc4b35352012-10-04 21:49:42 +0900329
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900330 if (symbol_conf.cumulate_callchain) {
331 he->stat_acc = malloc(sizeof(he->stat));
332 if (he->stat_acc == NULL) {
333 free(he);
334 return NULL;
335 }
336 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
Namhyung Kima0b51af2012-09-11 13:34:27 +0900337 if (!sample_self)
338 memset(&he->stat, 0, sizeof(he->stat));
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900339 }
340
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300341 map__get(he->ms.map);
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100342
343 if (he->branch_info) {
Namhyung Kim26353a62013-04-01 20:35:17 +0900344 /*
345 * This branch info is (a part of) allocated from
Arnaldo Carvalho de Melo644f2df2014-01-22 13:15:36 -0300346 * sample__resolve_bstack() and will be freed after
Namhyung Kim26353a62013-04-01 20:35:17 +0900347 * adding new entries. So we need to save a copy.
348 */
349 he->branch_info = malloc(sizeof(*he->branch_info));
350 if (he->branch_info == NULL) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300351 map__zput(he->ms.map);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900352 free(he->stat_acc);
Namhyung Kim26353a62013-04-01 20:35:17 +0900353 free(he);
354 return NULL;
355 }
356
357 memcpy(he->branch_info, template->branch_info,
358 sizeof(*he->branch_info));
359
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300360 map__get(he->branch_info->from.map);
361 map__get(he->branch_info->to.map);
Stephane Eranian3cf0cb12013-01-14 15:02:45 +0100362 }
363
Stephane Eranian98a3b322013-01-24 16:10:35 +0100364 if (he->mem_info) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300365 map__get(he->mem_info->iaddr.map);
366 map__get(he->mem_info->daddr.map);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100367 }
368
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300369 if (symbol_conf.use_callchain)
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200370 callchain_init(he->callchain);
Arnaldo Carvalho de Melob821c732012-10-25 14:42:45 -0200371
372 INIT_LIST_HEAD(&he->pairs.node);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300373 thread__get(he->thread);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300374 }
375
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200376 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300377}
378
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300379static u8 symbol__parent_filter(const struct symbol *parent)
380{
381 if (symbol_conf.exclude_other && parent == NULL)
382 return 1 << HIST_FILTER__PARENT;
383 return 0;
384}
385
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300386static struct hist_entry *hists__findnew_entry(struct hists *hists,
387 struct hist_entry *entry,
388 struct addr_location *al,
389 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300390{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300391 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300392 struct rb_node *parent = NULL;
393 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700394 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900395 u64 period = entry->stat.period;
396 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300397
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300398 p = &hists->entries_in->rb_node;
399
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300400 while (*p != NULL) {
401 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300402 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300403
Namhyung Kim9afcf932012-12-10 17:29:54 +0900404 /*
405 * Make sure that it receives arguments in a same order as
406 * hist_entry__collapse() so that we can use an appropriate
407 * function when searching an entry regardless which sort
408 * keys were used.
409 */
410 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300411
412 if (!cmp) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900413 if (sample_self)
414 he_stat__add_period(&he->stat, period, weight);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900415 if (symbol_conf.cumulate_callchain)
416 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400417
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900418 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300419 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900420 * and will not be used anymore.
421 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300422 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900423
David Miller63fa4712012-03-27 03:14:18 -0400424 /* If the map of an existing hist_entry has
425 * become out-of-date due to an exec() or
426 * similar, update it. Otherwise we will
427 * mis-adjust symbol addresses when computing
428 * the history counter to increment.
429 */
430 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300431 map__put(he->ms.map);
432 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400433 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300434 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300435 }
436
437 if (cmp < 0)
438 p = &(*p)->rb_left;
439 else
440 p = &(*p)->rb_right;
441 }
442
Namhyung Kima0b51af2012-09-11 13:34:27 +0900443 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300444 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900445 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300446
Namhyung Kim590cd342014-12-22 13:44:09 +0900447 hists->nr_entries++;
448
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300449 rb_link_node(&he->rb_node_in, parent, p);
450 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300451out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900452 if (sample_self)
453 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900454 if (symbol_conf.cumulate_callchain)
455 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300456 return he;
457}
458
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300459struct hist_entry *__hists__add_entry(struct hists *hists,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100460 struct addr_location *al,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900461 struct symbol *sym_parent,
462 struct branch_info *bi,
463 struct mem_info *mi,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900464 u64 period, u64 weight, u64 transaction,
465 bool sample_self)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100466{
467 struct hist_entry entry = {
468 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900469 .comm = thread__comm(al->thread),
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100470 .ms = {
471 .map = al->map,
472 .sym = al->sym,
473 },
Kan Liang0c4c4de2015-09-04 10:45:42 -0400474 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400475 .cpu = al->cpu,
476 .cpumode = al->cpumode,
477 .ip = al->addr,
478 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900479 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900480 .nr_events = 1,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900481 .period = period,
Andi Kleen05484292013-01-24 16:10:29 +0100482 .weight = weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900483 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100484 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300485 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300486 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900487 .branch_info = bi,
488 .mem_info = mi,
Andi Kleen475eeab2013-09-20 07:40:43 -0700489 .transaction = transaction,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100490 };
491
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300492 return hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100493}
494
Namhyung Kim69bcb012013-10-30 09:40:34 +0900495static int
496iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
497 struct addr_location *al __maybe_unused)
498{
499 return 0;
500}
501
502static int
503iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
504 struct addr_location *al __maybe_unused)
505{
506 return 0;
507}
508
509static int
510iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
511{
512 struct perf_sample *sample = iter->sample;
513 struct mem_info *mi;
514
515 mi = sample__resolve_mem(sample, al);
516 if (mi == NULL)
517 return -ENOMEM;
518
519 iter->priv = mi;
520 return 0;
521}
522
523static int
524iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
525{
526 u64 cost;
527 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300528 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900529 struct hist_entry *he;
530
531 if (mi == NULL)
532 return -EINVAL;
533
534 cost = iter->sample->weight;
535 if (!cost)
536 cost = 1;
537
538 /*
539 * must pass period=weight in order to get the correct
540 * sorting from hists__collapse_resort() which is solely
541 * based on periods. We want sorting be done on nr_events * weight
542 * and this is indirectly achieved by passing period=weight here
543 * and the he_stat__add_period() function.
544 */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300545 he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900546 cost, cost, 0, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900547 if (!he)
548 return -ENOMEM;
549
550 iter->he = he;
551 return 0;
552}
553
554static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900555iter_finish_mem_entry(struct hist_entry_iter *iter,
556 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900557{
558 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300559 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900560 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900561 int err = -EINVAL;
562
563 if (he == NULL)
564 goto out;
565
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300566 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900567
568 err = hist_entry__append_callchain(he, iter->sample);
569
570out:
571 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300572 * We don't need to free iter->priv (mem_info) here since the mem info
573 * was either already freed in hists__findnew_entry() or passed to a
574 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900575 */
576 iter->priv = NULL;
577
578 iter->he = NULL;
579 return err;
580}
581
582static int
583iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
584{
585 struct branch_info *bi;
586 struct perf_sample *sample = iter->sample;
587
588 bi = sample__resolve_bstack(sample, al);
589 if (!bi)
590 return -ENOMEM;
591
592 iter->curr = 0;
593 iter->total = sample->branch_stack->nr;
594
595 iter->priv = bi;
596 return 0;
597}
598
599static int
600iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
601 struct addr_location *al __maybe_unused)
602{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900603 /* to avoid calling callback function */
604 iter->he = NULL;
605
Namhyung Kim69bcb012013-10-30 09:40:34 +0900606 return 0;
607}
608
609static int
610iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
611{
612 struct branch_info *bi = iter->priv;
613 int i = iter->curr;
614
615 if (bi == NULL)
616 return 0;
617
618 if (iter->curr >= iter->total)
619 return 0;
620
621 al->map = bi[i].to.map;
622 al->sym = bi[i].to.sym;
623 al->addr = bi[i].to.addr;
624 return 1;
625}
626
627static int
628iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
629{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900630 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900631 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300632 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900633 struct hist_entry *he = NULL;
634 int i = iter->curr;
635 int err = 0;
636
637 bi = iter->priv;
638
639 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
640 goto out;
641
642 /*
643 * The report shows the percentage of total branches captured
644 * and not events sampled. Thus we use a pseudo period of 1.
645 */
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300646 he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
Andi Kleen0e332f02015-07-18 08:24:46 -0700647 1, bi->flags.cycles ? bi->flags.cycles : 1,
648 0, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900649 if (he == NULL)
650 return -ENOMEM;
651
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300652 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900653
654out:
655 iter->he = he;
656 iter->curr++;
657 return err;
658}
659
660static int
661iter_finish_branch_entry(struct hist_entry_iter *iter,
662 struct addr_location *al __maybe_unused)
663{
664 zfree(&iter->priv);
665 iter->he = NULL;
666
667 return iter->curr >= iter->total ? 0 : -1;
668}
669
670static int
671iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
672 struct addr_location *al __maybe_unused)
673{
674 return 0;
675}
676
677static int
678iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
679{
680 struct perf_evsel *evsel = iter->evsel;
681 struct perf_sample *sample = iter->sample;
682 struct hist_entry *he;
683
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300684 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900685 sample->period, sample->weight,
Namhyung Kima0b51af2012-09-11 13:34:27 +0900686 sample->transaction, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900687 if (he == NULL)
688 return -ENOMEM;
689
690 iter->he = he;
691 return 0;
692}
693
694static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900695iter_finish_normal_entry(struct hist_entry_iter *iter,
696 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900697{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900698 struct hist_entry *he = iter->he;
699 struct perf_evsel *evsel = iter->evsel;
700 struct perf_sample *sample = iter->sample;
701
702 if (he == NULL)
703 return 0;
704
705 iter->he = NULL;
706
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300707 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900708
709 return hist_entry__append_callchain(he, sample);
710}
711
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900712static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300713iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900714 struct addr_location *al __maybe_unused)
715{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900716 struct hist_entry **he_cache;
717
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900718 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900719
720 /*
721 * This is for detecting cycles or recursions so that they're
722 * cumulated only one time to prevent entries more than 100%
723 * overhead.
724 */
Adrian Hunter96b40f32015-09-25 16:15:47 +0300725 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900726 if (he_cache == NULL)
727 return -ENOMEM;
728
729 iter->priv = he_cache;
730 iter->curr = 0;
731
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900732 return 0;
733}
734
735static int
736iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
737 struct addr_location *al)
738{
739 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300740 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900741 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900742 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900743 struct hist_entry *he;
744 int err = 0;
745
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300746 he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900747 sample->period, sample->weight,
748 sample->transaction, true);
749 if (he == NULL)
750 return -ENOMEM;
751
752 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900753 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900754
Namhyung Kim82aa0192014-12-22 13:44:14 +0900755 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900756
757 /*
758 * We need to re-initialize the cursor since callchain_append()
759 * advanced the cursor to the end.
760 */
761 callchain_cursor_commit(&callchain_cursor);
762
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300763 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900764
765 return err;
766}
767
768static int
769iter_next_cumulative_entry(struct hist_entry_iter *iter,
770 struct addr_location *al)
771{
772 struct callchain_cursor_node *node;
773
774 node = callchain_cursor_current(&callchain_cursor);
775 if (node == NULL)
776 return 0;
777
Namhyung Kimc7405d82013-10-31 13:58:30 +0900778 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900779}
780
781static int
782iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
783 struct addr_location *al)
784{
785 struct perf_evsel *evsel = iter->evsel;
786 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900787 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900788 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900789 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -0300790 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900791 .cpu = al->cpu,
792 .thread = al->thread,
793 .comm = thread__comm(al->thread),
794 .ip = al->addr,
795 .ms = {
796 .map = al->map,
797 .sym = al->sym,
798 },
799 .parent = iter->parent,
800 };
801 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900802 struct callchain_cursor cursor;
803
804 callchain_cursor_snapshot(&cursor, &callchain_cursor);
805
806 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900807
808 /*
809 * Check if there's duplicate entries in the callchain.
810 * It's possible that it has cycles or recursive calls.
811 */
812 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900813 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
814 /* to avoid calling callback function */
815 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900816 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900817 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900818 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900819
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300820 he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900821 sample->period, sample->weight,
822 sample->transaction, false);
823 if (he == NULL)
824 return -ENOMEM;
825
826 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900827 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900828
Namhyung Kim82aa0192014-12-22 13:44:14 +0900829 if (symbol_conf.use_callchain)
830 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900831 return 0;
832}
833
834static int
835iter_finish_cumulative_entry(struct hist_entry_iter *iter,
836 struct addr_location *al __maybe_unused)
837{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900838 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900839 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900840
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900841 return 0;
842}
843
Namhyung Kim69bcb012013-10-30 09:40:34 +0900844const struct hist_iter_ops hist_iter_mem = {
845 .prepare_entry = iter_prepare_mem_entry,
846 .add_single_entry = iter_add_single_mem_entry,
847 .next_entry = iter_next_nop_entry,
848 .add_next_entry = iter_add_next_nop_entry,
849 .finish_entry = iter_finish_mem_entry,
850};
851
852const struct hist_iter_ops hist_iter_branch = {
853 .prepare_entry = iter_prepare_branch_entry,
854 .add_single_entry = iter_add_single_branch_entry,
855 .next_entry = iter_next_branch_entry,
856 .add_next_entry = iter_add_next_branch_entry,
857 .finish_entry = iter_finish_branch_entry,
858};
859
860const struct hist_iter_ops hist_iter_normal = {
861 .prepare_entry = iter_prepare_normal_entry,
862 .add_single_entry = iter_add_single_normal_entry,
863 .next_entry = iter_next_nop_entry,
864 .add_next_entry = iter_add_next_nop_entry,
865 .finish_entry = iter_finish_normal_entry,
866};
867
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900868const struct hist_iter_ops hist_iter_cumulative = {
869 .prepare_entry = iter_prepare_cumulative_entry,
870 .add_single_entry = iter_add_single_cumulative_entry,
871 .next_entry = iter_next_cumulative_entry,
872 .add_next_entry = iter_add_next_cumulative_entry,
873 .finish_entry = iter_finish_cumulative_entry,
874};
875
Namhyung Kim69bcb012013-10-30 09:40:34 +0900876int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900877 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900878{
879 int err, err2;
880
Namhyung Kim063bd932015-05-19 17:04:10 +0900881 err = sample__resolve_callchain(iter->sample, &iter->parent,
882 iter->evsel, al, max_stack_depth);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900883 if (err)
884 return err;
885
Adrian Hunter96b40f32015-09-25 16:15:47 +0300886 iter->max_stack = max_stack_depth;
887
Namhyung Kim69bcb012013-10-30 09:40:34 +0900888 err = iter->ops->prepare_entry(iter, al);
889 if (err)
890 goto out;
891
892 err = iter->ops->add_single_entry(iter, al);
893 if (err)
894 goto out;
895
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900896 if (iter->he && iter->add_entry_cb) {
897 err = iter->add_entry_cb(iter, al, true, arg);
898 if (err)
899 goto out;
900 }
901
Namhyung Kim69bcb012013-10-30 09:40:34 +0900902 while (iter->ops->next_entry(iter, al)) {
903 err = iter->ops->add_next_entry(iter, al);
904 if (err)
905 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900906
907 if (iter->he && iter->add_entry_cb) {
908 err = iter->add_entry_cb(iter, al, false, arg);
909 if (err)
910 goto out;
911 }
Namhyung Kim69bcb012013-10-30 09:40:34 +0900912 }
913
914out:
915 err2 = iter->ops->finish_entry(iter, al);
916 if (!err)
917 err = err2;
918
919 return err;
920}
921
John Kacur3d1d07e2009-09-28 15:32:55 +0200922int64_t
923hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
924{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900925 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200926 int64_t cmp = 0;
927
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900928 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900929 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200930 if (cmp)
931 break;
932 }
933
934 return cmp;
935}
936
937int64_t
938hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
939{
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900940 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +0200941 int64_t cmp = 0;
942
Namhyung Kim093f0ef32014-03-03 12:07:47 +0900943 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kim87bbdf72015-01-08 09:45:46 +0900944 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +0200945 if (cmp)
946 break;
947 }
948
949 return cmp;
950}
951
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -0300952void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200953{
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300954 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300955 map__zput(he->ms.map);
956
957 if (he->branch_info) {
958 map__zput(he->branch_info->from.map);
959 map__zput(he->branch_info->to.map);
960 zfree(&he->branch_info);
961 }
962
963 if (he->mem_info) {
964 map__zput(he->mem_info->iaddr.map);
965 map__zput(he->mem_info->daddr.map);
966 zfree(&he->mem_info);
967 }
968
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900969 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +0900970 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -0700971 if (he->srcfile && he->srcfile[0])
972 free(he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +0900973 free_callchain(he->callchain);
John Kacur3d1d07e2009-09-28 15:32:55 +0200974 free(he);
975}
976
977/*
978 * collapse the histogram
979 */
980
Irina Tirdea1d037ca2012-09-11 01:15:03 +0300981static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +0100982 struct rb_root *root,
983 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +0200984{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -0200985 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +0200986 struct rb_node *parent = NULL;
987 struct hist_entry *iter;
988 int64_t cmp;
989
990 while (*p != NULL) {
991 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300992 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +0200993
994 cmp = hist_entry__collapse(iter, he);
995
996 if (!cmp) {
Namhyung Kim139c0812012-10-04 21:49:43 +0900997 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900998 if (symbol_conf.cumulate_callchain)
999 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001000
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001001 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001002 callchain_cursor_reset(&callchain_cursor);
1003 callchain_merge(&callchain_cursor,
1004 iter->callchain,
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001005 he->callchain);
1006 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001007 hist_entry__delete(he);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001008 return false;
John Kacur3d1d07e2009-09-28 15:32:55 +02001009 }
1010
1011 if (cmp < 0)
1012 p = &(*p)->rb_left;
1013 else
1014 p = &(*p)->rb_right;
1015 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001016 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001017
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001018 rb_link_node(&he->rb_node_in, parent, p);
1019 rb_insert_color(&he->rb_node_in, root);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001020 return true;
John Kacur3d1d07e2009-09-28 15:32:55 +02001021}
1022
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001023static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
1024{
1025 struct rb_root *root;
1026
1027 pthread_mutex_lock(&hists->lock);
1028
1029 root = hists->entries_in;
1030 if (++hists->entries_in > &hists->entries_in_array[1])
1031 hists->entries_in = &hists->entries_in_array[0];
1032
1033 pthread_mutex_unlock(&hists->lock);
1034
1035 return root;
1036}
1037
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001038static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1039{
1040 hists__filter_entry_by_dso(hists, he);
1041 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001042 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001043 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001044}
1045
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001046void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001047{
1048 struct rb_root *root;
1049 struct rb_node *next;
1050 struct hist_entry *n;
1051
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001052 if (!sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001053 return;
1054
Namhyung Kim740b97f2014-12-22 13:44:10 +09001055 hists->nr_entries = 0;
1056
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001057 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001058
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001059 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001060
1061 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001062 if (session_done())
1063 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001064 n = rb_entry(next, struct hist_entry, rb_node_in);
1065 next = rb_next(&n->rb_node_in);
1066
1067 rb_erase(&n->rb_node_in, root);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001068 if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
1069 /*
1070 * If it wasn't combined with one of the entries already
1071 * collapsed, we need to apply the filters that may have
1072 * been set by, say, the hist_browser.
1073 */
1074 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001075 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001076 if (prog)
1077 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001078 }
1079}
1080
Namhyung Kim043ca3892014-03-03 14:18:00 +09001081static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001082{
Namhyung Kim043ca3892014-03-03 14:18:00 +09001083 struct perf_hpp_fmt *fmt;
1084 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001085
Namhyung Kim26d8b332014-03-03 16:16:20 +09001086 perf_hpp__for_each_sort_list(fmt) {
Namhyung Kime67d49a2014-03-18 13:00:59 +09001087 if (perf_hpp__should_skip(fmt))
1088 continue;
1089
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001090 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001091 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001092 break;
1093 }
1094
Namhyung Kim043ca3892014-03-03 14:18:00 +09001095 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001096}
1097
Namhyung Kim9283ba92014-04-24 16:37:26 +09001098static void hists__reset_filter_stats(struct hists *hists)
1099{
1100 hists->nr_non_filtered_entries = 0;
1101 hists->stats.total_non_filtered_period = 0;
1102}
1103
1104void hists__reset_stats(struct hists *hists)
1105{
1106 hists->nr_entries = 0;
1107 hists->stats.total_period = 0;
1108
1109 hists__reset_filter_stats(hists);
1110}
1111
1112static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1113{
1114 hists->nr_non_filtered_entries++;
1115 hists->stats.total_non_filtered_period += h->stat.period;
1116}
1117
1118void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1119{
1120 if (!h->filtered)
1121 hists__inc_filter_stats(hists, h);
1122
1123 hists->nr_entries++;
1124 hists->stats.total_period += h->stat.period;
1125}
1126
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001127static void __hists__insert_output_entry(struct rb_root *entries,
1128 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001129 u64 min_callchain_hits,
1130 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001131{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001132 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001133 struct rb_node *parent = NULL;
1134 struct hist_entry *iter;
1135
Kan Liangf9db0d02015-08-11 06:30:48 -04001136 if (use_callchain)
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001137 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001138 min_callchain_hits, &callchain_param);
1139
1140 while (*p != NULL) {
1141 parent = *p;
1142 iter = rb_entry(parent, struct hist_entry, rb_node);
1143
Namhyung Kim043ca3892014-03-03 14:18:00 +09001144 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001145 p = &(*p)->rb_left;
1146 else
1147 p = &(*p)->rb_right;
1148 }
1149
1150 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001151 rb_insert_color(&he->rb_node, entries);
John Kacur3d1d07e2009-09-28 15:32:55 +02001152}
1153
Namhyung Kim740b97f2014-12-22 13:44:10 +09001154void hists__output_resort(struct hists *hists, struct ui_progress *prog)
John Kacur3d1d07e2009-09-28 15:32:55 +02001155{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001156 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001157 struct rb_node *next;
1158 struct hist_entry *n;
John Kacur3d1d07e2009-09-28 15:32:55 +02001159 u64 min_callchain_hits;
Kan Liangf9db0d02015-08-11 06:30:48 -04001160 struct perf_evsel *evsel = hists_to_evsel(hists);
Kan Liang9e207dd2015-08-11 06:30:49 -04001161 bool use_callchain;
1162
Namhyung Kim208e7602015-09-30 13:34:00 +09001163 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
Kan Liang9e207dd2015-08-11 06:30:49 -04001164 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1165 else
1166 use_callchain = symbol_conf.use_callchain;
John Kacur3d1d07e2009-09-28 15:32:55 +02001167
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001168 min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001169
Namhyung Kim3a5714f2013-05-14 11:09:01 +09001170 if (sort__need_collapse)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001171 root = &hists->entries_collapsed;
1172 else
1173 root = hists->entries_in;
1174
1175 next = rb_first(root);
1176 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001177
Namhyung Kim9283ba92014-04-24 16:37:26 +09001178 hists__reset_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001179 hists__reset_col_len(hists);
Arnaldo Carvalho de Melofefb0b92010-05-10 13:57:51 -03001180
John Kacur3d1d07e2009-09-28 15:32:55 +02001181 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001182 n = rb_entry(next, struct hist_entry, rb_node_in);
1183 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001184
Kan Liangf9db0d02015-08-11 06:30:48 -04001185 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001186 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001187
1188 if (!n->filtered)
1189 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001190
1191 if (prog)
1192 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001193 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001194}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001195
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001196static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001197 enum hist_filter filter)
1198{
1199 h->filtered &= ~(1 << filter);
1200 if (h->filtered)
1201 return;
1202
Namhyung Kim87e90f42014-04-24 16:44:16 +09001203 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001204 h->unfolded = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001205 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001206 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001207
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001208 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001209
Namhyung Kim9283ba92014-04-24 16:37:26 +09001210 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001211 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001212}
1213
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001214
1215static bool hists__filter_entry_by_dso(struct hists *hists,
1216 struct hist_entry *he)
1217{
1218 if (hists->dso_filter != NULL &&
1219 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1220 he->filtered |= (1 << HIST_FILTER__DSO);
1221 return true;
1222 }
1223
1224 return false;
1225}
1226
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001227void hists__filter_by_dso(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001228{
1229 struct rb_node *nd;
1230
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001231 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001232
1233 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001234 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001235
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001236 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001237 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1238
1239 if (symbol_conf.exclude_other && !h->parent)
1240 continue;
1241
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001242 if (hists__filter_entry_by_dso(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001243 continue;
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001244
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001245 hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001246 }
1247}
1248
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001249static bool hists__filter_entry_by_thread(struct hists *hists,
1250 struct hist_entry *he)
1251{
1252 if (hists->thread_filter != NULL &&
1253 he->thread != hists->thread_filter) {
1254 he->filtered |= (1 << HIST_FILTER__THREAD);
1255 return true;
1256 }
1257
1258 return false;
1259}
1260
Arnaldo Carvalho de Melod7b76f02011-10-18 19:07:34 -02001261void hists__filter_by_thread(struct hists *hists)
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001262{
1263 struct rb_node *nd;
1264
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001265 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001266
1267 hists__reset_filter_stats(hists);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001268 hists__reset_col_len(hists);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001269
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001270 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001271 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1272
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001273 if (hists__filter_entry_by_thread(hists, h))
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001274 continue;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001275
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001276 hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
Arnaldo Carvalho de Melob09e0192010-05-11 11:10:15 -03001277 }
1278}
Arnaldo Carvalho de Meloef7b93a2010-05-11 23:18:06 -03001279
Namhyung Kime94d53e2012-03-16 17:50:51 +09001280static bool hists__filter_entry_by_symbol(struct hists *hists,
1281 struct hist_entry *he)
1282{
1283 if (hists->symbol_filter_str != NULL &&
1284 (!he->ms.sym || strstr(he->ms.sym->name,
1285 hists->symbol_filter_str) == NULL)) {
1286 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1287 return true;
1288 }
1289
1290 return false;
1291}
1292
1293void hists__filter_by_symbol(struct hists *hists)
1294{
1295 struct rb_node *nd;
1296
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001297 hists->stats.nr_non_filtered_samples = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001298
1299 hists__reset_filter_stats(hists);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001300 hists__reset_col_len(hists);
1301
1302 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1303 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1304
1305 if (hists__filter_entry_by_symbol(hists, h))
1306 continue;
1307
1308 hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1309 }
1310}
1311
Kan Liang21394d92015-09-04 10:45:44 -04001312static bool hists__filter_entry_by_socket(struct hists *hists,
1313 struct hist_entry *he)
1314{
1315 if ((hists->socket_filter > -1) &&
1316 (he->socket != hists->socket_filter)) {
1317 he->filtered |= (1 << HIST_FILTER__SOCKET);
1318 return true;
1319 }
1320
1321 return false;
1322}
1323
Kan Liang84734b02015-09-04 10:45:45 -04001324void hists__filter_by_socket(struct hists *hists)
1325{
1326 struct rb_node *nd;
1327
1328 hists->stats.nr_non_filtered_samples = 0;
1329
1330 hists__reset_filter_stats(hists);
1331 hists__reset_col_len(hists);
1332
1333 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1334 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1335
1336 if (hists__filter_entry_by_socket(hists, h))
1337 continue;
1338
1339 hists__remove_entry_filter(hists, h, HIST_FILTER__SOCKET);
1340 }
1341}
1342
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001343void events_stats__inc(struct events_stats *stats, u32 type)
1344{
1345 ++stats->nr_events[0];
1346 ++stats->nr_events[type];
1347}
1348
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001349void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001350{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03001351 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03001352}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001353
Namhyung Kim1844dbc2014-05-28 14:12:18 +09001354void hists__inc_nr_samples(struct hists *hists, bool filtered)
1355{
1356 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1357 if (!filtered)
1358 hists->stats.nr_non_filtered_samples++;
1359}
1360
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001361static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1362 struct hist_entry *pair)
1363{
Namhyung Kimce74f602012-12-10 17:29:55 +09001364 struct rb_root *root;
1365 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001366 struct rb_node *parent = NULL;
1367 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07001368 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001369
Namhyung Kimce74f602012-12-10 17:29:55 +09001370 if (sort__need_collapse)
1371 root = &hists->entries_collapsed;
1372 else
1373 root = hists->entries_in;
1374
1375 p = &root->rb_node;
1376
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001377 while (*p != NULL) {
1378 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09001379 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001380
Namhyung Kimce74f602012-12-10 17:29:55 +09001381 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001382
1383 if (!cmp)
1384 goto out;
1385
1386 if (cmp < 0)
1387 p = &(*p)->rb_left;
1388 else
1389 p = &(*p)->rb_right;
1390 }
1391
Namhyung Kima0b51af2012-09-11 13:34:27 +09001392 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001393 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03001394 memset(&he->stat, 0, sizeof(he->stat));
1395 he->hists = hists;
Namhyung Kimce74f602012-12-10 17:29:55 +09001396 rb_link_node(&he->rb_node_in, parent, p);
1397 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09001398 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01001399 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001400 }
1401out:
1402 return he;
1403}
1404
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001405static struct hist_entry *hists__find_entry(struct hists *hists,
1406 struct hist_entry *he)
1407{
Namhyung Kimce74f602012-12-10 17:29:55 +09001408 struct rb_node *n;
1409
1410 if (sort__need_collapse)
1411 n = hists->entries_collapsed.rb_node;
1412 else
1413 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001414
1415 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09001416 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1417 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001418
1419 if (cmp < 0)
1420 n = n->rb_left;
1421 else if (cmp > 0)
1422 n = n->rb_right;
1423 else
1424 return iter;
1425 }
1426
1427 return NULL;
1428}
1429
1430/*
1431 * Look for pairs to link to the leader buckets (hist_entries):
1432 */
1433void hists__match(struct hists *leader, struct hists *other)
1434{
Namhyung Kimce74f602012-12-10 17:29:55 +09001435 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001436 struct rb_node *nd;
1437 struct hist_entry *pos, *pair;
1438
Namhyung Kimce74f602012-12-10 17:29:55 +09001439 if (sort__need_collapse)
1440 root = &leader->entries_collapsed;
1441 else
1442 root = leader->entries_in;
1443
1444 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1445 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001446 pair = hists__find_entry(other, pos);
1447
1448 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09001449 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03001450 }
1451}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001452
1453/*
1454 * Look for entries in the other hists that are not present in the leader, if
1455 * we find them, just add a dummy entry on the leader hists, with period=0,
1456 * nr_events=0, to serve as the list header.
1457 */
1458int hists__link(struct hists *leader, struct hists *other)
1459{
Namhyung Kimce74f602012-12-10 17:29:55 +09001460 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001461 struct rb_node *nd;
1462 struct hist_entry *pos, *pair;
1463
Namhyung Kimce74f602012-12-10 17:29:55 +09001464 if (sort__need_collapse)
1465 root = &other->entries_collapsed;
1466 else
1467 root = other->entries_in;
1468
1469 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1470 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001471
1472 if (!hist_entry__has_pairs(pos)) {
1473 pair = hists__add_dummy_entry(leader, pos);
1474 if (pair == NULL)
1475 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09001476 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03001477 }
1478 }
1479
1480 return 0;
1481}
Namhyung Kimf2148332014-01-14 11:52:48 +09001482
Andi Kleen578499982015-07-18 08:24:49 -07001483void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
1484 struct perf_sample *sample, bool nonany_branch_mode)
1485{
1486 struct branch_info *bi;
1487
1488 /* If we have branch cycles always annotate them. */
1489 if (bs && bs->nr && bs->entries[0].flags.cycles) {
1490 int i;
1491
1492 bi = sample__resolve_bstack(sample, al);
1493 if (bi) {
1494 struct addr_map_symbol *prev = NULL;
1495
1496 /*
1497 * Ignore errors, still want to process the
1498 * other entries.
1499 *
1500 * For non standard branch modes always
1501 * force no IPC (prev == NULL)
1502 *
1503 * Note that perf stores branches reversed from
1504 * program order!
1505 */
1506 for (i = bs->nr - 1; i >= 0; i--) {
1507 addr_map_symbol__account_cycles(&bi[i].from,
1508 nonany_branch_mode ? NULL : prev,
1509 bi[i].flags.cycles);
1510 prev = &bi[i].to;
1511 }
1512 free(bi);
1513 }
1514 }
1515}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03001516
1517size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
1518{
1519 struct perf_evsel *pos;
1520 size_t ret = 0;
1521
1522 evlist__for_each(evlist, pos) {
1523 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1524 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1525 }
1526
1527 return ret;
1528}
1529
1530
Namhyung Kimf2148332014-01-14 11:52:48 +09001531u64 hists__total_period(struct hists *hists)
1532{
1533 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1534 hists->stats.total_period;
1535}
Namhyung Kim33db4562014-02-07 12:06:07 +09001536
1537int parse_filter_percentage(const struct option *opt __maybe_unused,
1538 const char *arg, int unset __maybe_unused)
1539{
1540 if (!strcmp(arg, "relative"))
1541 symbol_conf.filter_relative = true;
1542 else if (!strcmp(arg, "absolute"))
1543 symbol_conf.filter_relative = false;
1544 else
1545 return -1;
1546
1547 return 0;
1548}
Namhyung Kim0b93da12014-01-14 12:02:15 +09001549
1550int perf_hist_config(const char *var, const char *value)
1551{
1552 if (!strcmp(var, "hist.percentage"))
1553 return parse_filter_percentage(NULL, value, 0);
1554
1555 return 0;
1556}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001557
1558static int hists_evsel__init(struct perf_evsel *evsel)
1559{
1560 struct hists *hists = evsel__hists(evsel);
1561
1562 memset(hists, 0, sizeof(*hists));
1563 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1564 hists->entries_in = &hists->entries_in_array[0];
1565 hists->entries_collapsed = RB_ROOT;
1566 hists->entries = RB_ROOT;
1567 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04001568 hists->socket_filter = -1;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001569 return 0;
1570}
1571
Namhyung Kim61fa0e92015-12-10 16:53:20 +09001572static void hists__delete_remaining_entries(struct rb_root *root)
1573{
1574 struct rb_node *node;
1575 struct hist_entry *he;
1576
1577 while (!RB_EMPTY_ROOT(root)) {
1578 node = rb_first(root);
1579 rb_erase(node, root);
1580
1581 he = rb_entry(node, struct hist_entry, rb_node_in);
1582 hist_entry__delete(he);
1583 }
1584}
1585
1586static void hists__delete_all_entries(struct hists *hists)
1587{
1588 hists__delete_entries(hists);
1589 hists__delete_remaining_entries(&hists->entries_in_array[0]);
1590 hists__delete_remaining_entries(&hists->entries_in_array[1]);
1591 hists__delete_remaining_entries(&hists->entries_collapsed);
1592}
1593
Masami Hiramatsu17577de2015-12-09 11:11:29 +09001594static void hists_evsel__exit(struct perf_evsel *evsel)
1595{
1596 struct hists *hists = evsel__hists(evsel);
1597
Namhyung Kim61fa0e92015-12-10 16:53:20 +09001598 hists__delete_all_entries(hists);
Masami Hiramatsu17577de2015-12-09 11:11:29 +09001599}
1600
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001601/*
1602 * XXX We probably need a hists_evsel__exit() to free the hist_entries
1603 * stored in the rbtree...
1604 */
1605
1606int hists__init(void)
1607{
1608 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09001609 hists_evsel__init,
1610 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03001611 if (err)
1612 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
1613
1614 return err;
1615}