blob: 65d42758aaddb275c97f0faa68078e4ecaea33b5 [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"
Krister Johansen9c68ae92017-01-05 22:23:31 -08004#include "map.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02005#include "session.h"
Hari Bathinid890a982017-03-08 02:12:13 +05306#include "namespaces.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -02007#include "sort.h"
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03008#include "evlist.h"
Namhyung Kim29d720e2013-01-22 18:09:33 +09009#include "evsel.h"
Namhyung Kim69bcb012013-10-30 09:40:34 +090010#include "annotate.h"
Arnaldo Carvalho de Melo632a5ca2017-04-17 16:30:49 -030011#include "srcline.h"
Namhyung Kim740b97f2014-12-22 13:44:10 +090012#include "ui/progress.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030013#include <errno.h>
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -020014#include <math.h>
John Kacur3d1d07e2009-09-28 15:32:55 +020015
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020016static bool hists__filter_entry_by_dso(struct hists *hists,
17 struct hist_entry *he);
18static bool hists__filter_entry_by_thread(struct hists *hists,
19 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090020static bool hists__filter_entry_by_symbol(struct hists *hists,
21 struct hist_entry *he);
Kan Liang21394d92015-09-04 10:45:44 -040022static bool hists__filter_entry_by_socket(struct hists *hists,
23 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020024
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030025u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030026{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030027 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030028}
29
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030030void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030031{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030032 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030033}
34
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030035bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030036{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030037 if (len > hists__col_len(hists, col)) {
38 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030039 return true;
40 }
41 return false;
42}
43
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090044void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030045{
46 enum hist_column col;
47
48 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030049 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030050}
51
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010052static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
53{
54 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
55
56 if (hists__col_len(hists, dso) < unresolved_col_width &&
57 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
58 !symbol_conf.dso_list)
59 hists__set_col_len(hists, dso, unresolved_col_width);
60}
61
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090062void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030063{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010064 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Stephane Eranian98a3b322013-01-24 16:10:35 +010065 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030066 u16 len;
67
Namhyung Kimded19d52013-04-01 20:35:19 +090068 /*
69 * +4 accounts for '[x] ' priv level info
70 * +2 accounts for 0x prefix on raw addresses
71 * +3 accounts for ' y ' symtab origin info
72 */
73 if (h->ms.sym) {
74 symlen = h->ms.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +090075 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +090076 symlen += BITS_PER_LONG / 4 + 2 + 3;
77 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
78 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010079 symlen = unresolved_col_width + 4 + 2;
80 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010081 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +010082 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030083
84 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030085 if (hists__new_col_len(hists, HISTC_COMM, len))
Jiri Olsa89c7cb22016-06-20 23:58:19 +020086 hists__set_col_len(hists, HISTC_THREAD, len + 8);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030087
88 if (h->ms.map) {
89 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030090 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030091 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010092
Namhyung Kimcb993742012-12-27 18:11:42 +090093 if (h->parent)
94 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
95
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010096 if (h->branch_info) {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010097 if (h->branch_info->from.sym) {
98 symlen = (int)h->branch_info->from.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +090099 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +0900100 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100101 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
102
103 symlen = dso__name_len(h->branch_info->from.map->dso);
104 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
105 } else {
106 symlen = unresolved_col_width + 4 + 2;
107 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
108 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
109 }
110
111 if (h->branch_info->to.sym) {
112 symlen = (int)h->branch_info->to.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +0900113 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +0900114 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100115 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
116
117 symlen = dso__name_len(h->branch_info->to.map->dso);
118 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
119 } else {
120 symlen = unresolved_col_width + 4 + 2;
121 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
122 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
123 }
Andi Kleen508be0d2016-05-20 13:15:08 -0700124
125 if (h->branch_info->srcline_from)
126 hists__new_col_len(hists, HISTC_SRCLINE_FROM,
127 strlen(h->branch_info->srcline_from));
128 if (h->branch_info->srcline_to)
129 hists__new_col_len(hists, HISTC_SRCLINE_TO,
130 strlen(h->branch_info->srcline_to));
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100131 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100132
133 if (h->mem_info) {
Stephane Eranian98a3b322013-01-24 16:10:35 +0100134 if (h->mem_info->daddr.sym) {
135 symlen = (int)h->mem_info->daddr.sym->namelen + 4
136 + unresolved_col_width + 2;
137 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
138 symlen);
Don Zickus9b32ba72014-06-01 15:38:29 +0200139 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
140 symlen + 1);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100141 } else {
142 symlen = unresolved_col_width + 4 + 2;
143 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
144 symlen);
Jiri Olsa08059092016-01-20 12:56:33 +0100145 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
146 symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100147 }
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200148
149 if (h->mem_info->iaddr.sym) {
150 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
151 + unresolved_col_width + 2;
152 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
153 symlen);
154 } else {
155 symlen = unresolved_col_width + 4 + 2;
156 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
157 symlen);
158 }
159
Stephane Eranian98a3b322013-01-24 16:10:35 +0100160 if (h->mem_info->daddr.map) {
161 symlen = dso__name_len(h->mem_info->daddr.map->dso);
162 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
163 symlen);
164 } else {
165 symlen = unresolved_col_width + 4 + 2;
166 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
167 }
168 } else {
169 symlen = unresolved_col_width + 4 + 2;
170 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200171 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100172 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
173 }
174
Hari Bathinid890a982017-03-08 02:12:13 +0530175 hists__new_col_len(hists, HISTC_CGROUP_ID, 20);
Arnaldo Carvalho de Meloa4978ec2015-09-09 12:14:00 -0300176 hists__new_col_len(hists, HISTC_CPU, 3);
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400177 hists__new_col_len(hists, HISTC_SOCKET, 6);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100178 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
179 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
180 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
181 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
182 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
183 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700184
Jiri Olsaf666ac02016-09-19 15:10:10 +0200185 if (h->srcline) {
186 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
187 hists__new_col_len(hists, HISTC_SRCLINE, len);
188 }
Arnaldo Carvalho de Meloe8e6d372015-08-10 16:53:54 -0300189
Andi Kleen31191a82015-08-07 15:54:24 -0700190 if (h->srcfile)
191 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
192
Andi Kleen475eeab2013-09-20 07:40:43 -0700193 if (h->transaction)
194 hists__new_col_len(hists, HISTC_TRANSACTION,
195 hist_entry__transaction_len());
Namhyung Kim0c0af782016-02-21 23:22:38 +0900196
197 if (h->trace_output)
198 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300199}
200
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900201void hists__output_recalc_col_len(struct hists *hists, int max_rows)
202{
203 struct rb_node *next = rb_first(&hists->entries);
204 struct hist_entry *n;
205 int row = 0;
206
207 hists__reset_col_len(hists);
208
209 while (next && row++ < max_rows) {
210 n = rb_entry(next, struct hist_entry, rb_node);
211 if (!n->filtered)
212 hists__calc_col_len(hists, n);
213 next = rb_next(&n->rb_node);
214 }
215}
216
Namhyung Kimf39056f2014-01-14 14:25:37 +0900217static void he_stat__add_cpumode_period(struct he_stat *he_stat,
218 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800219{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300220 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800221 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900222 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800223 break;
224 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900225 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800226 break;
227 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900228 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800229 break;
230 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900231 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800232 break;
233 default:
234 break;
235 }
236}
237
Andi Kleen05484292013-01-24 16:10:29 +0100238static void he_stat__add_period(struct he_stat *he_stat, u64 period,
239 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900240{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100241
Namhyung Kim139c0812012-10-04 21:49:43 +0900242 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100243 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900244 he_stat->nr_events += 1;
245}
246
247static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
248{
249 dest->period += src->period;
250 dest->period_sys += src->period_sys;
251 dest->period_us += src->period_us;
252 dest->period_guest_sys += src->period_guest_sys;
253 dest->period_guest_us += src->period_guest_us;
254 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100255 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900256}
257
Namhyung Kimf39056f2014-01-14 14:25:37 +0900258static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300259{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900260 he_stat->period = (he_stat->period * 7) / 8;
261 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100262 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300263}
264
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900265static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
266
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300267static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
268{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900269 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900270 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200271
272 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300273 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200274
Namhyung Kimf39056f2014-01-14 14:25:37 +0900275 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900276 if (symbol_conf.cumulate_callchain)
277 he_stat__decay(he->stat_acc);
Namhyung Kim42b276a2016-01-05 12:06:00 +0900278 decay_callchain(he->callchain);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200279
Namhyung Kim3186b682014-04-22 13:44:23 +0900280 diff = prev_period - he->stat.period;
281
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900282 if (!he->depth) {
283 hists->stats.total_period -= diff;
284 if (!he->filtered)
285 hists->stats.total_non_filtered_period -= diff;
286 }
287
288 if (!he->leaf) {
289 struct hist_entry *child;
290 struct rb_node *node = rb_first(&he->hroot_out);
291 while (node) {
292 child = rb_entry(node, struct hist_entry, rb_node);
293 node = rb_next(node);
294
295 if (hists__decay_entry(hists, child))
296 hists__delete_entry(hists, child);
297 }
298 }
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200299
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900300 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300301}
302
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300303static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
304{
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900305 struct rb_root *root_in;
306 struct rb_root *root_out;
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300307
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900308 if (he->parent_he) {
309 root_in = &he->parent_he->hroot_in;
310 root_out = &he->parent_he->hroot_out;
311 } else {
Jiri Olsa52225032016-05-03 13:54:42 +0200312 if (hists__has(hists, need_collapse))
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900313 root_in = &hists->entries_collapsed;
314 else
315 root_in = hists->entries_in;
316 root_out = &hists->entries;
317 }
318
319 rb_erase(&he->rb_node_in, root_in);
320 rb_erase(&he->rb_node, root_out);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300321
322 --hists->nr_entries;
323 if (!he->filtered)
324 --hists->nr_non_filtered_entries;
325
326 hist_entry__delete(he);
327}
328
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900329void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300330{
331 struct rb_node *next = rb_first(&hists->entries);
332 struct hist_entry *n;
333
334 while (next) {
335 n = rb_entry(next, struct hist_entry, rb_node);
336 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200337 if (((zap_user && n->level == '.') ||
338 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300339 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300340 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300341 }
342 }
343}
344
Namhyung Kim701937b2014-08-12 17:16:05 +0900345void hists__delete_entries(struct hists *hists)
346{
347 struct rb_node *next = rb_first(&hists->entries);
348 struct hist_entry *n;
349
350 while (next) {
351 n = rb_entry(next, struct hist_entry, rb_node);
352 next = rb_next(&n->rb_node);
353
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300354 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900355 }
356}
357
John Kacur3d1d07e2009-09-28 15:32:55 +0200358/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300359 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200360 */
361
Jiri Olsa0a269a62016-07-05 08:56:03 +0200362static int hist_entry__init(struct hist_entry *he,
363 struct hist_entry *template,
364 bool sample_self)
365{
366 *he = *template;
367
368 if (symbol_conf.cumulate_callchain) {
369 he->stat_acc = malloc(sizeof(he->stat));
370 if (he->stat_acc == NULL)
371 return -ENOMEM;
372 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
373 if (!sample_self)
374 memset(&he->stat, 0, sizeof(he->stat));
375 }
376
377 map__get(he->ms.map);
378
379 if (he->branch_info) {
380 /*
381 * This branch info is (a part of) allocated from
382 * sample__resolve_bstack() and will be freed after
383 * adding new entries. So we need to save a copy.
384 */
385 he->branch_info = malloc(sizeof(*he->branch_info));
386 if (he->branch_info == NULL) {
387 map__zput(he->ms.map);
388 free(he->stat_acc);
389 return -ENOMEM;
390 }
391
392 memcpy(he->branch_info, template->branch_info,
393 sizeof(*he->branch_info));
394
395 map__get(he->branch_info->from.map);
396 map__get(he->branch_info->to.map);
397 }
398
399 if (he->mem_info) {
400 map__get(he->mem_info->iaddr.map);
401 map__get(he->mem_info->daddr.map);
402 }
403
404 if (symbol_conf.use_callchain)
405 callchain_init(he->callchain);
406
407 if (he->raw_data) {
408 he->raw_data = memdup(he->raw_data, he->raw_size);
409
410 if (he->raw_data == NULL) {
411 map__put(he->ms.map);
412 if (he->branch_info) {
413 map__put(he->branch_info->from.map);
414 map__put(he->branch_info->to.map);
415 free(he->branch_info);
416 }
417 if (he->mem_info) {
418 map__put(he->mem_info->iaddr.map);
419 map__put(he->mem_info->daddr.map);
420 }
421 free(he->stat_acc);
422 return -ENOMEM;
423 }
424 }
425 INIT_LIST_HEAD(&he->pairs.node);
426 thread__get(he->thread);
Namhyung Kimd2580c72016-09-13 16:45:48 +0900427 he->hroot_in = RB_ROOT;
428 he->hroot_out = RB_ROOT;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200429
430 if (!symbol_conf.report_hierarchy)
431 he->leaf = true;
432
433 return 0;
434}
435
Jiri Olsaf542e762016-07-05 08:56:04 +0200436static void *hist_entry__zalloc(size_t size)
437{
438 return zalloc(size + sizeof(struct hist_entry));
439}
440
441static void hist_entry__free(void *ptr)
442{
443 free(ptr);
444}
445
446static struct hist_entry_ops default_ops = {
447 .new = hist_entry__zalloc,
448 .free = hist_entry__free,
449};
450
Namhyung Kima0b51af2012-09-11 13:34:27 +0900451static struct hist_entry *hist_entry__new(struct hist_entry *template,
452 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300453{
Jiri Olsaf542e762016-07-05 08:56:04 +0200454 struct hist_entry_ops *ops = template->ops;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900455 size_t callchain_size = 0;
456 struct hist_entry *he;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200457 int err = 0;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900458
Jiri Olsaf542e762016-07-05 08:56:04 +0200459 if (!ops)
460 ops = template->ops = &default_ops;
461
Namhyung Kim82aa0192014-12-22 13:44:14 +0900462 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900463 callchain_size = sizeof(struct callchain_root);
464
Jiri Olsaf542e762016-07-05 08:56:04 +0200465 he = ops->new(callchain_size);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200466 if (he) {
467 err = hist_entry__init(he, template, sample_self);
Jiri Olsaf542e762016-07-05 08:56:04 +0200468 if (err) {
469 ops->free(he);
470 he = NULL;
471 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300472 }
473
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200474 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300475}
476
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300477static u8 symbol__parent_filter(const struct symbol *parent)
478{
479 if (symbol_conf.exclude_other && parent == NULL)
480 return 1 << HIST_FILTER__PARENT;
481 return 0;
482}
483
Namhyung Kim467ef102016-02-16 23:08:19 +0900484static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
485{
486 if (!symbol_conf.use_callchain)
487 return;
488
489 he->hists->callchain_period += period;
490 if (!he->filtered)
491 he->hists->callchain_non_filtered_period += period;
492}
493
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300494static struct hist_entry *hists__findnew_entry(struct hists *hists,
495 struct hist_entry *entry,
496 struct addr_location *al,
497 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300498{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300499 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300500 struct rb_node *parent = NULL;
501 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700502 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900503 u64 period = entry->stat.period;
504 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300505
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300506 p = &hists->entries_in->rb_node;
507
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300508 while (*p != NULL) {
509 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300510 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300511
Namhyung Kim9afcf932012-12-10 17:29:54 +0900512 /*
513 * Make sure that it receives arguments in a same order as
514 * hist_entry__collapse() so that we can use an appropriate
515 * function when searching an entry regardless which sort
516 * keys were used.
517 */
518 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300519
520 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900521 if (sample_self) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900522 he_stat__add_period(&he->stat, period, weight);
Namhyung Kim467ef102016-02-16 23:08:19 +0900523 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900524 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900525 if (symbol_conf.cumulate_callchain)
526 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400527
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900528 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300529 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900530 * and will not be used anymore.
531 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300532 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900533
David Miller63fa4712012-03-27 03:14:18 -0400534 /* If the map of an existing hist_entry has
535 * become out-of-date due to an exec() or
536 * similar, update it. Otherwise we will
537 * mis-adjust symbol addresses when computing
538 * the history counter to increment.
539 */
540 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300541 map__put(he->ms.map);
542 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400543 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300544 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300545 }
546
547 if (cmp < 0)
548 p = &(*p)->rb_left;
549 else
550 p = &(*p)->rb_right;
551 }
552
Namhyung Kima0b51af2012-09-11 13:34:27 +0900553 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300554 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900555 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300556
Namhyung Kim0f584742016-01-28 00:40:49 +0900557 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900558 hist_entry__add_callchain_period(he, period);
559 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900560
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300561 rb_link_node(&he->rb_node_in, parent, p);
562 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300563out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900564 if (sample_self)
565 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900566 if (symbol_conf.cumulate_callchain)
567 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300568 return he;
569}
570
Jiri Olsaa5051972016-07-05 08:56:05 +0200571static struct hist_entry*
572__hists__add_entry(struct hists *hists,
573 struct addr_location *al,
574 struct symbol *sym_parent,
575 struct branch_info *bi,
576 struct mem_info *mi,
577 struct perf_sample *sample,
578 bool sample_self,
579 struct hist_entry_ops *ops)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100580{
Hari Bathinid890a982017-03-08 02:12:13 +0530581 struct namespaces *ns = thread__namespaces(al->thread);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100582 struct hist_entry entry = {
583 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900584 .comm = thread__comm(al->thread),
Hari Bathinid890a982017-03-08 02:12:13 +0530585 .cgroup_id = {
586 .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0,
587 .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0,
588 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100589 .ms = {
590 .map = al->map,
591 .sym = al->sym,
592 },
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400593 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400594 .cpu = al->cpu,
595 .cpumode = al->cpumode,
596 .ip = al->addr,
597 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900598 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900599 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900600 .period = sample->period,
601 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900602 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100603 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300604 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300605 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900606 .branch_info = bi,
607 .mem_info = mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900608 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900609 .raw_data = sample->raw_data,
610 .raw_size = sample->raw_size,
Jiri Olsaa5051972016-07-05 08:56:05 +0200611 .ops = ops,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100612 };
613
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300614 return hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100615}
616
Jiri Olsaa5051972016-07-05 08:56:05 +0200617struct hist_entry *hists__add_entry(struct hists *hists,
618 struct addr_location *al,
619 struct symbol *sym_parent,
620 struct branch_info *bi,
621 struct mem_info *mi,
622 struct perf_sample *sample,
623 bool sample_self)
624{
625 return __hists__add_entry(hists, al, sym_parent, bi, mi,
626 sample, sample_self, NULL);
627}
628
629struct hist_entry *hists__add_entry_ops(struct hists *hists,
630 struct hist_entry_ops *ops,
631 struct addr_location *al,
632 struct symbol *sym_parent,
633 struct branch_info *bi,
634 struct mem_info *mi,
635 struct perf_sample *sample,
636 bool sample_self)
637{
638 return __hists__add_entry(hists, al, sym_parent, bi, mi,
639 sample, sample_self, ops);
640}
641
Namhyung Kim69bcb012013-10-30 09:40:34 +0900642static int
643iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
644 struct addr_location *al __maybe_unused)
645{
646 return 0;
647}
648
649static int
650iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
651 struct addr_location *al __maybe_unused)
652{
653 return 0;
654}
655
656static int
657iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
658{
659 struct perf_sample *sample = iter->sample;
660 struct mem_info *mi;
661
662 mi = sample__resolve_mem(sample, al);
663 if (mi == NULL)
664 return -ENOMEM;
665
666 iter->priv = mi;
667 return 0;
668}
669
670static int
671iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
672{
673 u64 cost;
674 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300675 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900676 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900677 struct hist_entry *he;
678
679 if (mi == NULL)
680 return -EINVAL;
681
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900682 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900683 if (!cost)
684 cost = 1;
685
686 /*
687 * must pass period=weight in order to get the correct
688 * sorting from hists__collapse_resort() which is solely
689 * based on periods. We want sorting be done on nr_events * weight
690 * and this is indirectly achieved by passing period=weight here
691 * and the he_stat__add_period() function.
692 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900693 sample->period = cost;
694
Jiri Olsa0102ef32016-06-14 20:19:21 +0200695 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
696 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900697 if (!he)
698 return -ENOMEM;
699
700 iter->he = he;
701 return 0;
702}
703
704static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900705iter_finish_mem_entry(struct hist_entry_iter *iter,
706 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900707{
708 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300709 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900710 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900711 int err = -EINVAL;
712
713 if (he == NULL)
714 goto out;
715
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300716 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900717
718 err = hist_entry__append_callchain(he, iter->sample);
719
720out:
721 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300722 * We don't need to free iter->priv (mem_info) here since the mem info
723 * was either already freed in hists__findnew_entry() or passed to a
724 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900725 */
726 iter->priv = NULL;
727
728 iter->he = NULL;
729 return err;
730}
731
732static int
733iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
734{
735 struct branch_info *bi;
736 struct perf_sample *sample = iter->sample;
737
738 bi = sample__resolve_bstack(sample, al);
739 if (!bi)
740 return -ENOMEM;
741
742 iter->curr = 0;
743 iter->total = sample->branch_stack->nr;
744
745 iter->priv = bi;
746 return 0;
747}
748
749static int
Arnaldo Carvalho de Melob8f8eb82016-03-22 13:09:37 -0300750iter_add_single_branch_entry(struct hist_entry_iter *iter,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900751 struct addr_location *al __maybe_unused)
752{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900753 /* to avoid calling callback function */
754 iter->he = NULL;
755
Namhyung Kim69bcb012013-10-30 09:40:34 +0900756 return 0;
757}
758
759static int
760iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
761{
762 struct branch_info *bi = iter->priv;
763 int i = iter->curr;
764
765 if (bi == NULL)
766 return 0;
767
768 if (iter->curr >= iter->total)
769 return 0;
770
771 al->map = bi[i].to.map;
772 al->sym = bi[i].to.sym;
773 al->addr = bi[i].to.addr;
774 return 1;
775}
776
777static int
778iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
779{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900780 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900781 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300782 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900783 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900784 struct hist_entry *he = NULL;
785 int i = iter->curr;
786 int err = 0;
787
788 bi = iter->priv;
789
790 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
791 goto out;
792
793 /*
794 * The report shows the percentage of total branches captured
795 * and not events sampled. Thus we use a pseudo period of 1.
796 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900797 sample->period = 1;
798 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
799
Jiri Olsa0102ef32016-06-14 20:19:21 +0200800 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
801 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900802 if (he == NULL)
803 return -ENOMEM;
804
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300805 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900806
807out:
808 iter->he = he;
809 iter->curr++;
810 return err;
811}
812
813static int
814iter_finish_branch_entry(struct hist_entry_iter *iter,
815 struct addr_location *al __maybe_unused)
816{
817 zfree(&iter->priv);
818 iter->he = NULL;
819
820 return iter->curr >= iter->total ? 0 : -1;
821}
822
823static int
824iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
825 struct addr_location *al __maybe_unused)
826{
827 return 0;
828}
829
830static int
831iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
832{
833 struct perf_evsel *evsel = iter->evsel;
834 struct perf_sample *sample = iter->sample;
835 struct hist_entry *he;
836
Jiri Olsa0102ef32016-06-14 20:19:21 +0200837 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
838 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900839 if (he == NULL)
840 return -ENOMEM;
841
842 iter->he = he;
843 return 0;
844}
845
846static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900847iter_finish_normal_entry(struct hist_entry_iter *iter,
848 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900849{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900850 struct hist_entry *he = iter->he;
851 struct perf_evsel *evsel = iter->evsel;
852 struct perf_sample *sample = iter->sample;
853
854 if (he == NULL)
855 return 0;
856
857 iter->he = NULL;
858
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300859 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900860
861 return hist_entry__append_callchain(he, sample);
862}
863
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900864static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300865iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900866 struct addr_location *al __maybe_unused)
867{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900868 struct hist_entry **he_cache;
869
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900870 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900871
872 /*
873 * This is for detecting cycles or recursions so that they're
874 * cumulated only one time to prevent entries more than 100%
875 * overhead.
876 */
Adrian Hunter96b40f32015-09-25 16:15:47 +0300877 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900878 if (he_cache == NULL)
879 return -ENOMEM;
880
881 iter->priv = he_cache;
882 iter->curr = 0;
883
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900884 return 0;
885}
886
887static int
888iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
889 struct addr_location *al)
890{
891 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300892 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900893 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900894 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900895 struct hist_entry *he;
896 int err = 0;
897
Jiri Olsa0102ef32016-06-14 20:19:21 +0200898 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
899 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900900 if (he == NULL)
901 return -ENOMEM;
902
903 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900904 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900905
Namhyung Kim82aa0192014-12-22 13:44:14 +0900906 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900907
908 /*
909 * We need to re-initialize the cursor since callchain_append()
910 * advanced the cursor to the end.
911 */
912 callchain_cursor_commit(&callchain_cursor);
913
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300914 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900915
916 return err;
917}
918
919static int
920iter_next_cumulative_entry(struct hist_entry_iter *iter,
921 struct addr_location *al)
922{
923 struct callchain_cursor_node *node;
924
925 node = callchain_cursor_current(&callchain_cursor);
926 if (node == NULL)
927 return 0;
928
Namhyung Kimc7405d82013-10-31 13:58:30 +0900929 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900930}
931
932static int
933iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
934 struct addr_location *al)
935{
936 struct perf_evsel *evsel = iter->evsel;
937 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900938 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900939 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900940 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -0300941 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900942 .cpu = al->cpu,
943 .thread = al->thread,
944 .comm = thread__comm(al->thread),
945 .ip = al->addr,
946 .ms = {
947 .map = al->map,
948 .sym = al->sym,
949 },
950 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +0900951 .raw_data = sample->raw_data,
952 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900953 };
954 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900955 struct callchain_cursor cursor;
956
957 callchain_cursor_snapshot(&cursor, &callchain_cursor);
958
959 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900960
961 /*
962 * Check if there's duplicate entries in the callchain.
963 * It's possible that it has cycles or recursive calls.
964 */
965 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900966 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
967 /* to avoid calling callback function */
968 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900969 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900970 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900971 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900972
Jiri Olsa0102ef32016-06-14 20:19:21 +0200973 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
974 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900975 if (he == NULL)
976 return -ENOMEM;
977
978 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900979 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900980
Namhyung Kim82aa0192014-12-22 13:44:14 +0900981 if (symbol_conf.use_callchain)
982 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900983 return 0;
984}
985
986static int
987iter_finish_cumulative_entry(struct hist_entry_iter *iter,
988 struct addr_location *al __maybe_unused)
989{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900990 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900991 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900992
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900993 return 0;
994}
995
Namhyung Kim69bcb012013-10-30 09:40:34 +0900996const struct hist_iter_ops hist_iter_mem = {
997 .prepare_entry = iter_prepare_mem_entry,
998 .add_single_entry = iter_add_single_mem_entry,
999 .next_entry = iter_next_nop_entry,
1000 .add_next_entry = iter_add_next_nop_entry,
1001 .finish_entry = iter_finish_mem_entry,
1002};
1003
1004const struct hist_iter_ops hist_iter_branch = {
1005 .prepare_entry = iter_prepare_branch_entry,
1006 .add_single_entry = iter_add_single_branch_entry,
1007 .next_entry = iter_next_branch_entry,
1008 .add_next_entry = iter_add_next_branch_entry,
1009 .finish_entry = iter_finish_branch_entry,
1010};
1011
1012const struct hist_iter_ops hist_iter_normal = {
1013 .prepare_entry = iter_prepare_normal_entry,
1014 .add_single_entry = iter_add_single_normal_entry,
1015 .next_entry = iter_next_nop_entry,
1016 .add_next_entry = iter_add_next_nop_entry,
1017 .finish_entry = iter_finish_normal_entry,
1018};
1019
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001020const struct hist_iter_ops hist_iter_cumulative = {
1021 .prepare_entry = iter_prepare_cumulative_entry,
1022 .add_single_entry = iter_add_single_cumulative_entry,
1023 .next_entry = iter_next_cumulative_entry,
1024 .add_next_entry = iter_add_next_cumulative_entry,
1025 .finish_entry = iter_finish_cumulative_entry,
1026};
1027
Namhyung Kim69bcb012013-10-30 09:40:34 +09001028int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001029 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +09001030{
1031 int err, err2;
Krister Johansen9c68ae92017-01-05 22:23:31 -08001032 struct map *alm = NULL;
1033
1034 if (al && al->map)
1035 alm = map__get(al->map);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001036
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001037 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
Namhyung Kim063bd932015-05-19 17:04:10 +09001038 iter->evsel, al, max_stack_depth);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001039 if (err)
1040 return err;
1041
Adrian Hunter96b40f32015-09-25 16:15:47 +03001042 iter->max_stack = max_stack_depth;
1043
Namhyung Kim69bcb012013-10-30 09:40:34 +09001044 err = iter->ops->prepare_entry(iter, al);
1045 if (err)
1046 goto out;
1047
1048 err = iter->ops->add_single_entry(iter, al);
1049 if (err)
1050 goto out;
1051
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001052 if (iter->he && iter->add_entry_cb) {
1053 err = iter->add_entry_cb(iter, al, true, arg);
1054 if (err)
1055 goto out;
1056 }
1057
Namhyung Kim69bcb012013-10-30 09:40:34 +09001058 while (iter->ops->next_entry(iter, al)) {
1059 err = iter->ops->add_next_entry(iter, al);
1060 if (err)
1061 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001062
1063 if (iter->he && iter->add_entry_cb) {
1064 err = iter->add_entry_cb(iter, al, false, arg);
1065 if (err)
1066 goto out;
1067 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001068 }
1069
1070out:
1071 err2 = iter->ops->finish_entry(iter, al);
1072 if (!err)
1073 err = err2;
1074
Krister Johansen9c68ae92017-01-05 22:23:31 -08001075 map__put(alm);
1076
Namhyung Kim69bcb012013-10-30 09:40:34 +09001077 return err;
1078}
1079
John Kacur3d1d07e2009-09-28 15:32:55 +02001080int64_t
1081hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1082{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001083 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001084 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001085 int64_t cmp = 0;
1086
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001087 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001088 if (perf_hpp__is_dynamic_entry(fmt) &&
1089 !perf_hpp__defined_dynamic_entry(fmt, hists))
1090 continue;
1091
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001092 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001093 if (cmp)
1094 break;
1095 }
1096
1097 return cmp;
1098}
1099
1100int64_t
1101hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1102{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001103 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001104 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001105 int64_t cmp = 0;
1106
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001107 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001108 if (perf_hpp__is_dynamic_entry(fmt) &&
1109 !perf_hpp__defined_dynamic_entry(fmt, hists))
1110 continue;
1111
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001112 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001113 if (cmp)
1114 break;
1115 }
1116
1117 return cmp;
1118}
1119
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001120void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001121{
Jiri Olsaf542e762016-07-05 08:56:04 +02001122 struct hist_entry_ops *ops = he->ops;
1123
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001124 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001125 map__zput(he->ms.map);
1126
1127 if (he->branch_info) {
1128 map__zput(he->branch_info->from.map);
1129 map__zput(he->branch_info->to.map);
Andi Kleen508be0d2016-05-20 13:15:08 -07001130 free_srcline(he->branch_info->srcline_from);
1131 free_srcline(he->branch_info->srcline_to);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001132 zfree(&he->branch_info);
1133 }
1134
1135 if (he->mem_info) {
1136 map__zput(he->mem_info->iaddr.map);
1137 map__zput(he->mem_info->daddr.map);
1138 zfree(&he->mem_info);
1139 }
1140
Jin Yao0d3eb0b2017-03-26 04:34:29 +08001141 if (he->inline_node) {
1142 inline_node__delete(he->inline_node);
1143 he->inline_node = NULL;
1144 }
1145
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001146 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +09001147 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -07001148 if (he->srcfile && he->srcfile[0])
1149 free(he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001150 free_callchain(he->callchain);
Namhyung Kim60517d22015-12-23 02:07:03 +09001151 free(he->trace_output);
Namhyung Kim72392832015-12-24 11:16:17 +09001152 free(he->raw_data);
Jiri Olsaf542e762016-07-05 08:56:04 +02001153 ops->free(he);
John Kacur3d1d07e2009-09-28 15:32:55 +02001154}
1155
1156/*
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001157 * If this is not the last column, then we need to pad it according to the
1158 * pre-calculated max lenght for this column, otherwise don't bother adding
1159 * spaces because that would break viewing this with, for instance, 'less',
1160 * that would show tons of trailing spaces when a long C++ demangled method
1161 * names is sampled.
1162*/
1163int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1164 struct perf_hpp_fmt *fmt, int printed)
1165{
1166 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
Jiri Olsada1b0402016-06-14 20:19:20 +02001167 const int width = fmt->width(fmt, hpp, he->hists);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001168 if (printed < width) {
1169 advance_hpp(hpp, printed);
1170 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1171 }
1172 }
1173
1174 return printed;
1175}
1176
1177/*
John Kacur3d1d07e2009-09-28 15:32:55 +02001178 * collapse the histogram
1179 */
1180
Namhyung Kimaef810e2016-02-25 00:13:34 +09001181static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
Namhyung Kimaec13a72016-03-09 22:46:58 +09001182static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1183 enum hist_filter type);
1184
1185typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1186
1187static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1188{
1189 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1190}
1191
1192static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1193 enum hist_filter type,
1194 fmt_chk_fn check)
1195{
1196 struct perf_hpp_fmt *fmt;
1197 bool type_match = false;
1198 struct hist_entry *parent = he->parent_he;
1199
1200 switch (type) {
1201 case HIST_FILTER__THREAD:
1202 if (symbol_conf.comm_list == NULL &&
1203 symbol_conf.pid_list == NULL &&
1204 symbol_conf.tid_list == NULL)
1205 return;
1206 break;
1207 case HIST_FILTER__DSO:
1208 if (symbol_conf.dso_list == NULL)
1209 return;
1210 break;
1211 case HIST_FILTER__SYMBOL:
1212 if (symbol_conf.sym_list == NULL)
1213 return;
1214 break;
1215 case HIST_FILTER__PARENT:
1216 case HIST_FILTER__GUEST:
1217 case HIST_FILTER__HOST:
1218 case HIST_FILTER__SOCKET:
Jiri Olsa9857b712016-08-17 14:55:23 +02001219 case HIST_FILTER__C2C:
Namhyung Kimaec13a72016-03-09 22:46:58 +09001220 default:
1221 return;
1222 }
1223
1224 /* if it's filtered by own fmt, it has to have filter bits */
1225 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1226 if (check(fmt)) {
1227 type_match = true;
1228 break;
1229 }
1230 }
1231
1232 if (type_match) {
1233 /*
1234 * If the filter is for current level entry, propagate
1235 * filter marker to parents. The marker bit was
1236 * already set by default so it only needs to clear
1237 * non-filtered entries.
1238 */
1239 if (!(he->filtered & (1 << type))) {
1240 while (parent) {
1241 parent->filtered &= ~(1 << type);
1242 parent = parent->parent_he;
1243 }
1244 }
1245 } else {
1246 /*
1247 * If current entry doesn't have matching formats, set
1248 * filter marker for upper level entries. it will be
1249 * cleared if its lower level entries is not filtered.
1250 *
1251 * For lower-level entries, it inherits parent's
1252 * filter bit so that lower level entries of a
1253 * non-filtered entry won't set the filter marker.
1254 */
1255 if (parent == NULL)
1256 he->filtered |= (1 << type);
1257 else
1258 he->filtered |= (parent->filtered & (1 << type));
1259 }
1260}
1261
1262static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1263{
1264 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1265 check_thread_entry);
1266
1267 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1268 perf_hpp__is_dso_entry);
1269
1270 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1271 perf_hpp__is_sym_entry);
1272
1273 hists__apply_filters(he->hists, he);
1274}
Namhyung Kimaef810e2016-02-25 00:13:34 +09001275
1276static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1277 struct rb_root *root,
1278 struct hist_entry *he,
Namhyung Kimaec13a72016-03-09 22:46:58 +09001279 struct hist_entry *parent_he,
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001280 struct perf_hpp_list *hpp_list)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001281{
1282 struct rb_node **p = &root->rb_node;
1283 struct rb_node *parent = NULL;
1284 struct hist_entry *iter, *new;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001285 struct perf_hpp_fmt *fmt;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001286 int64_t cmp;
1287
1288 while (*p != NULL) {
1289 parent = *p;
1290 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1291
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001292 cmp = 0;
1293 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1294 cmp = fmt->collapse(fmt, iter, he);
1295 if (cmp)
1296 break;
1297 }
1298
Namhyung Kimaef810e2016-02-25 00:13:34 +09001299 if (!cmp) {
1300 he_stat__add_stat(&iter->stat, &he->stat);
1301 return iter;
1302 }
1303
1304 if (cmp < 0)
1305 p = &parent->rb_left;
1306 else
1307 p = &parent->rb_right;
1308 }
1309
1310 new = hist_entry__new(he, true);
1311 if (new == NULL)
1312 return NULL;
1313
Namhyung Kimaef810e2016-02-25 00:13:34 +09001314 hists->nr_entries++;
1315
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001316 /* save related format list for output */
1317 new->hpp_list = hpp_list;
Namhyung Kimaec13a72016-03-09 22:46:58 +09001318 new->parent_he = parent_he;
1319
1320 hist_entry__apply_hierarchy_filters(new);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001321
1322 /* some fields are now passed to 'new' */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001323 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1324 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1325 he->trace_output = NULL;
1326 else
1327 new->trace_output = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001328
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001329 if (perf_hpp__is_srcline_entry(fmt))
1330 he->srcline = NULL;
1331 else
1332 new->srcline = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001333
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001334 if (perf_hpp__is_srcfile_entry(fmt))
1335 he->srcfile = NULL;
1336 else
1337 new->srcfile = NULL;
1338 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001339
1340 rb_link_node(&new->rb_node_in, parent, p);
1341 rb_insert_color(&new->rb_node_in, root);
1342 return new;
1343}
1344
1345static int hists__hierarchy_insert_entry(struct hists *hists,
1346 struct rb_root *root,
1347 struct hist_entry *he)
1348{
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001349 struct perf_hpp_list_node *node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001350 struct hist_entry *new_he = NULL;
1351 struct hist_entry *parent = NULL;
1352 int depth = 0;
1353 int ret = 0;
1354
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001355 list_for_each_entry(node, &hists->hpp_formats, list) {
1356 /* skip period (overhead) and elided columns */
1357 if (node->level == 0 || node->skip)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001358 continue;
1359
1360 /* insert copy of 'he' for each fmt into the hierarchy */
Namhyung Kimaec13a72016-03-09 22:46:58 +09001361 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001362 if (new_he == NULL) {
1363 ret = -1;
1364 break;
1365 }
1366
1367 root = &new_he->hroot_in;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001368 new_he->depth = depth++;
1369 parent = new_he;
1370 }
1371
1372 if (new_he) {
1373 new_he->leaf = true;
1374
1375 if (symbol_conf.use_callchain) {
1376 callchain_cursor_reset(&callchain_cursor);
1377 if (callchain_merge(&callchain_cursor,
1378 new_he->callchain,
1379 he->callchain) < 0)
1380 ret = -1;
1381 }
1382 }
1383
1384 /* 'he' is no longer used */
1385 hist_entry__delete(he);
1386
1387 /* return 0 (or -1) since it already applied filters */
1388 return ret;
1389}
1390
Jiri Olsa592dac62016-03-24 13:52:17 +01001391static int hists__collapse_insert_entry(struct hists *hists,
1392 struct rb_root *root,
1393 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001394{
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001395 struct rb_node **p = &root->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001396 struct rb_node *parent = NULL;
1397 struct hist_entry *iter;
1398 int64_t cmp;
1399
Namhyung Kimaef810e2016-02-25 00:13:34 +09001400 if (symbol_conf.report_hierarchy)
1401 return hists__hierarchy_insert_entry(hists, root, he);
1402
John Kacur3d1d07e2009-09-28 15:32:55 +02001403 while (*p != NULL) {
1404 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001405 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001406
1407 cmp = hist_entry__collapse(iter, he);
1408
1409 if (!cmp) {
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001410 int ret = 0;
1411
Namhyung Kim139c0812012-10-04 21:49:43 +09001412 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001413 if (symbol_conf.cumulate_callchain)
1414 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001415
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001416 if (symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001417 callchain_cursor_reset(&callchain_cursor);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001418 if (callchain_merge(&callchain_cursor,
1419 iter->callchain,
1420 he->callchain) < 0)
1421 ret = -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001422 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001423 hist_entry__delete(he);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001424 return ret;
John Kacur3d1d07e2009-09-28 15:32:55 +02001425 }
1426
1427 if (cmp < 0)
1428 p = &(*p)->rb_left;
1429 else
1430 p = &(*p)->rb_right;
1431 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001432 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001433
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001434 rb_link_node(&he->rb_node_in, parent, p);
1435 rb_insert_color(&he->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001436 return 1;
John Kacur3d1d07e2009-09-28 15:32:55 +02001437}
1438
Namhyung Kimfc284be2016-01-07 10:14:10 +01001439struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001440{
1441 struct rb_root *root;
1442
1443 pthread_mutex_lock(&hists->lock);
1444
1445 root = hists->entries_in;
1446 if (++hists->entries_in > &hists->entries_in_array[1])
1447 hists->entries_in = &hists->entries_in_array[0];
1448
1449 pthread_mutex_unlock(&hists->lock);
1450
1451 return root;
1452}
1453
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001454static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1455{
1456 hists__filter_entry_by_dso(hists, he);
1457 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001458 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001459 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001460}
1461
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001462int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001463{
1464 struct rb_root *root;
1465 struct rb_node *next;
1466 struct hist_entry *n;
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001467 int ret;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001468
Jiri Olsa52225032016-05-03 13:54:42 +02001469 if (!hists__has(hists, need_collapse))
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001470 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001471
Namhyung Kim740b97f2014-12-22 13:44:10 +09001472 hists->nr_entries = 0;
1473
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001474 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001475
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001476 next = rb_first(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001477
1478 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001479 if (session_done())
1480 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001481 n = rb_entry(next, struct hist_entry, rb_node_in);
1482 next = rb_next(&n->rb_node_in);
1483
1484 rb_erase(&n->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001485 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1486 if (ret < 0)
1487 return -1;
1488
1489 if (ret) {
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001490 /*
1491 * If it wasn't combined with one of the entries already
1492 * collapsed, we need to apply the filters that may have
1493 * been set by, say, the hist_browser.
1494 */
1495 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001496 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001497 if (prog)
1498 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001499 }
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001500 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001501}
1502
Namhyung Kim043ca3892014-03-03 14:18:00 +09001503static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001504{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001505 struct hists *hists = a->hists;
Namhyung Kim043ca3892014-03-03 14:18:00 +09001506 struct perf_hpp_fmt *fmt;
1507 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001508
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001509 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001510 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001511 continue;
1512
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001513 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001514 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001515 break;
1516 }
1517
Namhyung Kim043ca3892014-03-03 14:18:00 +09001518 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001519}
1520
Namhyung Kim9283ba92014-04-24 16:37:26 +09001521static void hists__reset_filter_stats(struct hists *hists)
1522{
1523 hists->nr_non_filtered_entries = 0;
1524 hists->stats.total_non_filtered_period = 0;
1525}
1526
1527void hists__reset_stats(struct hists *hists)
1528{
1529 hists->nr_entries = 0;
1530 hists->stats.total_period = 0;
1531
1532 hists__reset_filter_stats(hists);
1533}
1534
1535static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1536{
1537 hists->nr_non_filtered_entries++;
1538 hists->stats.total_non_filtered_period += h->stat.period;
1539}
1540
1541void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1542{
1543 if (!h->filtered)
1544 hists__inc_filter_stats(hists, h);
1545
1546 hists->nr_entries++;
1547 hists->stats.total_period += h->stat.period;
1548}
1549
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001550static void hierarchy_recalc_total_periods(struct hists *hists)
1551{
1552 struct rb_node *node;
1553 struct hist_entry *he;
1554
1555 node = rb_first(&hists->entries);
1556
1557 hists->stats.total_period = 0;
1558 hists->stats.total_non_filtered_period = 0;
1559
1560 /*
1561 * recalculate total period using top-level entries only
1562 * since lower level entries only see non-filtered entries
1563 * but upper level entries have sum of both entries.
1564 */
1565 while (node) {
1566 he = rb_entry(node, struct hist_entry, rb_node);
1567 node = rb_next(node);
1568
1569 hists->stats.total_period += he->stat.period;
1570 if (!he->filtered)
1571 hists->stats.total_non_filtered_period += he->stat.period;
1572 }
1573}
1574
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001575static void hierarchy_insert_output_entry(struct rb_root *root,
1576 struct hist_entry *he)
1577{
1578 struct rb_node **p = &root->rb_node;
1579 struct rb_node *parent = NULL;
1580 struct hist_entry *iter;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001581 struct perf_hpp_fmt *fmt;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001582
1583 while (*p != NULL) {
1584 parent = *p;
1585 iter = rb_entry(parent, struct hist_entry, rb_node);
1586
1587 if (hist_entry__sort(he, iter) > 0)
1588 p = &parent->rb_left;
1589 else
1590 p = &parent->rb_right;
1591 }
1592
1593 rb_link_node(&he->rb_node, parent, p);
1594 rb_insert_color(&he->rb_node, root);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001595
1596 /* update column width of dynamic entry */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001597 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1598 if (perf_hpp__is_dynamic_entry(fmt))
1599 fmt->sort(fmt, he, NULL);
1600 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001601}
1602
1603static void hists__hierarchy_output_resort(struct hists *hists,
1604 struct ui_progress *prog,
1605 struct rb_root *root_in,
1606 struct rb_root *root_out,
1607 u64 min_callchain_hits,
1608 bool use_callchain)
1609{
1610 struct rb_node *node;
1611 struct hist_entry *he;
1612
1613 *root_out = RB_ROOT;
1614 node = rb_first(root_in);
1615
1616 while (node) {
1617 he = rb_entry(node, struct hist_entry, rb_node_in);
1618 node = rb_next(node);
1619
1620 hierarchy_insert_output_entry(root_out, he);
1621
1622 if (prog)
1623 ui_progress__update(prog, 1);
1624
Namhyung Kimc72ab442016-11-08 22:08:33 +09001625 hists->nr_entries++;
1626 if (!he->filtered) {
1627 hists->nr_non_filtered_entries++;
1628 hists__calc_col_len(hists, he);
1629 }
1630
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001631 if (!he->leaf) {
1632 hists__hierarchy_output_resort(hists, prog,
1633 &he->hroot_in,
1634 &he->hroot_out,
1635 min_callchain_hits,
1636 use_callchain);
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001637 continue;
1638 }
1639
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001640 if (!use_callchain)
1641 continue;
1642
1643 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1644 u64 total = he->stat.period;
1645
1646 if (symbol_conf.cumulate_callchain)
1647 total = he->stat_acc->period;
1648
1649 min_callchain_hits = total * (callchain_param.min_percent / 100);
1650 }
1651
1652 callchain_param.sort(&he->sorted_chain, he->callchain,
1653 min_callchain_hits, &callchain_param);
1654 }
1655}
1656
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001657static void __hists__insert_output_entry(struct rb_root *entries,
1658 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001659 u64 min_callchain_hits,
1660 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001661{
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001662 struct rb_node **p = &entries->rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001663 struct rb_node *parent = NULL;
1664 struct hist_entry *iter;
Namhyung Kimabab5e72016-02-27 03:52:47 +09001665 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001666
Namhyung Kim744070e2016-01-28 00:40:48 +09001667 if (use_callchain) {
1668 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1669 u64 total = he->stat.period;
1670
1671 if (symbol_conf.cumulate_callchain)
1672 total = he->stat_acc->period;
1673
1674 min_callchain_hits = total * (callchain_param.min_percent / 100);
1675 }
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001676 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001677 min_callchain_hits, &callchain_param);
Namhyung Kim744070e2016-01-28 00:40:48 +09001678 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001679
1680 while (*p != NULL) {
1681 parent = *p;
1682 iter = rb_entry(parent, struct hist_entry, rb_node);
1683
Namhyung Kim043ca3892014-03-03 14:18:00 +09001684 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001685 p = &(*p)->rb_left;
1686 else
1687 p = &(*p)->rb_right;
1688 }
1689
1690 rb_link_node(&he->rb_node, parent, p);
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001691 rb_insert_color(&he->rb_node, entries);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001692
1693 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1694 if (perf_hpp__is_dynamic_entry(fmt) &&
1695 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1696 fmt->sort(fmt, he, NULL); /* update column width */
1697 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001698}
1699
Jiri Olsa01441af2016-01-18 10:23:59 +01001700static void output_resort(struct hists *hists, struct ui_progress *prog,
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001701 bool use_callchain, hists__resort_cb_t cb)
John Kacur3d1d07e2009-09-28 15:32:55 +02001702{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001703 struct rb_root *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001704 struct rb_node *next;
1705 struct hist_entry *n;
Namhyung Kim467ef102016-02-16 23:08:19 +09001706 u64 callchain_total;
John Kacur3d1d07e2009-09-28 15:32:55 +02001707 u64 min_callchain_hits;
1708
Namhyung Kim467ef102016-02-16 23:08:19 +09001709 callchain_total = hists->callchain_period;
1710 if (symbol_conf.filter_relative)
1711 callchain_total = hists->callchain_non_filtered_period;
1712
1713 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001714
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001715 hists__reset_stats(hists);
1716 hists__reset_col_len(hists);
1717
1718 if (symbol_conf.report_hierarchy) {
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001719 hists__hierarchy_output_resort(hists, prog,
1720 &hists->entries_collapsed,
1721 &hists->entries,
1722 min_callchain_hits,
1723 use_callchain);
1724 hierarchy_recalc_total_periods(hists);
1725 return;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001726 }
1727
Jiri Olsa52225032016-05-03 13:54:42 +02001728 if (hists__has(hists, need_collapse))
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001729 root = &hists->entries_collapsed;
1730 else
1731 root = hists->entries_in;
1732
1733 next = rb_first(root);
1734 hists->entries = RB_ROOT;
John Kacur3d1d07e2009-09-28 15:32:55 +02001735
1736 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001737 n = rb_entry(next, struct hist_entry, rb_node_in);
1738 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001739
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001740 if (cb && cb(n))
1741 continue;
1742
Kan Liangf9db0d02015-08-11 06:30:48 -04001743 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001744 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001745
1746 if (!n->filtered)
1747 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001748
1749 if (prog)
1750 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001751 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001752}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001753
Jiri Olsa452ce032016-01-18 10:24:00 +01001754void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
Jiri Olsa01441af2016-01-18 10:23:59 +01001755{
Jiri Olsa01441af2016-01-18 10:23:59 +01001756 bool use_callchain;
1757
1758 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1759 use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1760 else
1761 use_callchain = symbol_conf.use_callchain;
1762
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001763 output_resort(evsel__hists(evsel), prog, use_callchain, NULL);
Jiri Olsa452ce032016-01-18 10:24:00 +01001764}
1765
1766void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1767{
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001768 output_resort(hists, prog, symbol_conf.use_callchain, NULL);
1769}
1770
1771void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1772 hists__resort_cb_t cb)
1773{
1774 output_resort(hists, prog, symbol_conf.use_callchain, cb);
Jiri Olsa01441af2016-01-18 10:23:59 +01001775}
1776
Namhyung Kim8c018722016-02-25 00:13:36 +09001777static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1778{
1779 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1780 return false;
1781
1782 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1783 return true;
1784
1785 return false;
1786}
1787
1788struct rb_node *rb_hierarchy_last(struct rb_node *node)
1789{
1790 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1791
1792 while (can_goto_child(he, HMD_NORMAL)) {
1793 node = rb_last(&he->hroot_out);
1794 he = rb_entry(node, struct hist_entry, rb_node);
1795 }
1796 return node;
1797}
1798
1799struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1800{
1801 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1802
1803 if (can_goto_child(he, hmd))
1804 node = rb_first(&he->hroot_out);
1805 else
1806 node = rb_next(node);
1807
1808 while (node == NULL) {
1809 he = he->parent_he;
1810 if (he == NULL)
1811 break;
1812
1813 node = rb_next(&he->rb_node);
1814 }
1815 return node;
1816}
1817
1818struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1819{
1820 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1821
1822 node = rb_prev(node);
1823 if (node)
1824 return rb_hierarchy_last(node);
1825
1826 he = he->parent_he;
1827 if (he == NULL)
1828 return NULL;
1829
1830 return &he->rb_node;
1831}
1832
Namhyung Kima7b58952016-02-26 21:13:16 +09001833bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1834{
1835 struct rb_node *node;
1836 struct hist_entry *child;
1837 float percent;
1838
1839 if (he->leaf)
1840 return false;
1841
1842 node = rb_first(&he->hroot_out);
1843 child = rb_entry(node, struct hist_entry, rb_node);
1844
1845 while (node && child->filtered) {
1846 node = rb_next(node);
1847 child = rb_entry(node, struct hist_entry, rb_node);
1848 }
1849
1850 if (node)
1851 percent = hist_entry__get_percent_limit(child);
1852 else
1853 percent = 0;
1854
1855 return node && percent >= limit;
1856}
1857
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001858static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001859 enum hist_filter filter)
1860{
1861 h->filtered &= ~(1 << filter);
Namhyung Kim155e9af2016-02-25 00:13:38 +09001862
1863 if (symbol_conf.report_hierarchy) {
1864 struct hist_entry *parent = h->parent_he;
1865
1866 while (parent) {
1867 he_stat__add_stat(&parent->stat, &h->stat);
1868
1869 parent->filtered &= ~(1 << filter);
1870
1871 if (parent->filtered)
1872 goto next;
1873
1874 /* force fold unfiltered entry for simplicity */
1875 parent->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001876 parent->has_no_entry = false;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001877 parent->row_offset = 0;
1878 parent->nr_rows = 0;
1879next:
1880 parent = parent->parent_he;
1881 }
1882 }
1883
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001884 if (h->filtered)
1885 return;
1886
Namhyung Kim87e90f42014-04-24 16:44:16 +09001887 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001888 h->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001889 h->has_no_entry = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001890 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001891 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001892
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001893 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001894
Namhyung Kim9283ba92014-04-24 16:37:26 +09001895 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001896 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001897}
1898
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001899
1900static bool hists__filter_entry_by_dso(struct hists *hists,
1901 struct hist_entry *he)
1902{
1903 if (hists->dso_filter != NULL &&
1904 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1905 he->filtered |= (1 << HIST_FILTER__DSO);
1906 return true;
1907 }
1908
1909 return false;
1910}
1911
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001912static bool hists__filter_entry_by_thread(struct hists *hists,
1913 struct hist_entry *he)
1914{
1915 if (hists->thread_filter != NULL &&
1916 he->thread != hists->thread_filter) {
1917 he->filtered |= (1 << HIST_FILTER__THREAD);
1918 return true;
1919 }
1920
1921 return false;
1922}
1923
Namhyung Kime94d53e2012-03-16 17:50:51 +09001924static bool hists__filter_entry_by_symbol(struct hists *hists,
1925 struct hist_entry *he)
1926{
1927 if (hists->symbol_filter_str != NULL &&
1928 (!he->ms.sym || strstr(he->ms.sym->name,
1929 hists->symbol_filter_str) == NULL)) {
1930 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1931 return true;
1932 }
1933
1934 return false;
1935}
1936
Kan Liang21394d92015-09-04 10:45:44 -04001937static bool hists__filter_entry_by_socket(struct hists *hists,
1938 struct hist_entry *he)
1939{
1940 if ((hists->socket_filter > -1) &&
1941 (he->socket != hists->socket_filter)) {
1942 he->filtered |= (1 << HIST_FILTER__SOCKET);
1943 return true;
1944 }
1945
1946 return false;
1947}
1948
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001949typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1950
1951static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04001952{
1953 struct rb_node *nd;
1954
1955 hists->stats.nr_non_filtered_samples = 0;
1956
1957 hists__reset_filter_stats(hists);
1958 hists__reset_col_len(hists);
1959
1960 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1961 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1962
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001963 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04001964 continue;
1965
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001966 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04001967 }
1968}
1969
Namhyung Kim70642852016-02-25 00:13:39 +09001970static void resort_filtered_entry(struct rb_root *root, struct hist_entry *he)
1971{
1972 struct rb_node **p = &root->rb_node;
1973 struct rb_node *parent = NULL;
1974 struct hist_entry *iter;
1975 struct rb_root new_root = RB_ROOT;
1976 struct rb_node *nd;
1977
1978 while (*p != NULL) {
1979 parent = *p;
1980 iter = rb_entry(parent, struct hist_entry, rb_node);
1981
1982 if (hist_entry__sort(he, iter) > 0)
1983 p = &(*p)->rb_left;
1984 else
1985 p = &(*p)->rb_right;
1986 }
1987
1988 rb_link_node(&he->rb_node, parent, p);
1989 rb_insert_color(&he->rb_node, root);
1990
1991 if (he->leaf || he->filtered)
1992 return;
1993
1994 nd = rb_first(&he->hroot_out);
1995 while (nd) {
1996 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1997
1998 nd = rb_next(nd);
1999 rb_erase(&h->rb_node, &he->hroot_out);
2000
2001 resort_filtered_entry(&new_root, h);
2002 }
2003
2004 he->hroot_out = new_root;
2005}
2006
Namhyung Kim155e9af2016-02-25 00:13:38 +09002007static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2008{
2009 struct rb_node *nd;
Namhyung Kim70642852016-02-25 00:13:39 +09002010 struct rb_root new_root = RB_ROOT;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002011
2012 hists->stats.nr_non_filtered_samples = 0;
2013
2014 hists__reset_filter_stats(hists);
2015 hists__reset_col_len(hists);
2016
2017 nd = rb_first(&hists->entries);
2018 while (nd) {
2019 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2020 int ret;
2021
2022 ret = hist_entry__filter(h, type, arg);
2023
2024 /*
2025 * case 1. non-matching type
2026 * zero out the period, set filter marker and move to child
2027 */
2028 if (ret < 0) {
2029 memset(&h->stat, 0, sizeof(h->stat));
2030 h->filtered |= (1 << type);
2031
2032 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2033 }
2034 /*
2035 * case 2. matched type (filter out)
2036 * set filter marker and move to next
2037 */
2038 else if (ret == 1) {
2039 h->filtered |= (1 << type);
2040
2041 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2042 }
2043 /*
2044 * case 3. ok (not filtered)
2045 * add period to hists and parents, erase the filter marker
2046 * and move to next sibling
2047 */
2048 else {
2049 hists__remove_entry_filter(hists, h, type);
2050
2051 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2052 }
2053 }
Namhyung Kim70642852016-02-25 00:13:39 +09002054
Namhyung Kimf7fb5382016-03-09 22:47:02 +09002055 hierarchy_recalc_total_periods(hists);
2056
Namhyung Kim70642852016-02-25 00:13:39 +09002057 /*
2058 * resort output after applying a new filter since filter in a lower
2059 * hierarchy can change periods in a upper hierarchy.
2060 */
2061 nd = rb_first(&hists->entries);
2062 while (nd) {
2063 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2064
2065 nd = rb_next(nd);
2066 rb_erase(&h->rb_node, &hists->entries);
2067
2068 resort_filtered_entry(&new_root, h);
2069 }
2070
2071 hists->entries = new_root;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002072}
2073
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002074void hists__filter_by_thread(struct hists *hists)
2075{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002076 if (symbol_conf.report_hierarchy)
2077 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2078 hists->thread_filter);
2079 else
2080 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2081 hists__filter_entry_by_thread);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002082}
2083
2084void hists__filter_by_dso(struct hists *hists)
2085{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002086 if (symbol_conf.report_hierarchy)
2087 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2088 hists->dso_filter);
2089 else
2090 hists__filter_by_type(hists, HIST_FILTER__DSO,
2091 hists__filter_entry_by_dso);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002092}
2093
2094void hists__filter_by_symbol(struct hists *hists)
2095{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002096 if (symbol_conf.report_hierarchy)
2097 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2098 hists->symbol_filter_str);
2099 else
2100 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2101 hists__filter_entry_by_symbol);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002102}
2103
2104void hists__filter_by_socket(struct hists *hists)
2105{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002106 if (symbol_conf.report_hierarchy)
2107 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2108 &hists->socket_filter);
2109 else
2110 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2111 hists__filter_entry_by_socket);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002112}
2113
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002114void events_stats__inc(struct events_stats *stats, u32 type)
2115{
2116 ++stats->nr_events[0];
2117 ++stats->nr_events[type];
2118}
2119
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002120void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002121{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002122 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002123}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002124
Namhyung Kim1844dbc2014-05-28 14:12:18 +09002125void hists__inc_nr_samples(struct hists *hists, bool filtered)
2126{
2127 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2128 if (!filtered)
2129 hists->stats.nr_non_filtered_samples++;
2130}
2131
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002132static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2133 struct hist_entry *pair)
2134{
Namhyung Kimce74f602012-12-10 17:29:55 +09002135 struct rb_root *root;
2136 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002137 struct rb_node *parent = NULL;
2138 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07002139 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002140
Jiri Olsa52225032016-05-03 13:54:42 +02002141 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002142 root = &hists->entries_collapsed;
2143 else
2144 root = hists->entries_in;
2145
2146 p = &root->rb_node;
2147
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002148 while (*p != NULL) {
2149 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09002150 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002151
Namhyung Kimce74f602012-12-10 17:29:55 +09002152 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002153
2154 if (!cmp)
2155 goto out;
2156
2157 if (cmp < 0)
2158 p = &(*p)->rb_left;
2159 else
2160 p = &(*p)->rb_right;
2161 }
2162
Namhyung Kima0b51af2012-09-11 13:34:27 +09002163 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002164 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03002165 memset(&he->stat, 0, sizeof(he->stat));
2166 he->hists = hists;
Kan Liang09623d72016-04-24 23:28:09 -07002167 if (symbol_conf.cumulate_callchain)
2168 memset(he->stat_acc, 0, sizeof(he->stat));
Namhyung Kimce74f602012-12-10 17:29:55 +09002169 rb_link_node(&he->rb_node_in, parent, p);
2170 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09002171 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01002172 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002173 }
2174out:
2175 return he;
2176}
2177
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002178static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
2179 struct rb_root *root,
2180 struct hist_entry *pair)
2181{
2182 struct rb_node **p;
2183 struct rb_node *parent = NULL;
2184 struct hist_entry *he;
2185 struct perf_hpp_fmt *fmt;
2186
2187 p = &root->rb_node;
2188 while (*p != NULL) {
2189 int64_t cmp = 0;
2190
2191 parent = *p;
2192 he = rb_entry(parent, struct hist_entry, rb_node_in);
2193
2194 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2195 cmp = fmt->collapse(fmt, he, pair);
2196 if (cmp)
2197 break;
2198 }
2199 if (!cmp)
2200 goto out;
2201
2202 if (cmp < 0)
2203 p = &parent->rb_left;
2204 else
2205 p = &parent->rb_right;
2206 }
2207
2208 he = hist_entry__new(pair, true);
2209 if (he) {
2210 rb_link_node(&he->rb_node_in, parent, p);
2211 rb_insert_color(&he->rb_node_in, root);
2212
2213 he->dummy = true;
2214 he->hists = hists;
2215 memset(&he->stat, 0, sizeof(he->stat));
2216 hists__inc_stats(hists, he);
2217 }
2218out:
2219 return he;
2220}
2221
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002222static struct hist_entry *hists__find_entry(struct hists *hists,
2223 struct hist_entry *he)
2224{
Namhyung Kimce74f602012-12-10 17:29:55 +09002225 struct rb_node *n;
2226
Jiri Olsa52225032016-05-03 13:54:42 +02002227 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002228 n = hists->entries_collapsed.rb_node;
2229 else
2230 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002231
2232 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002233 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2234 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002235
2236 if (cmp < 0)
2237 n = n->rb_left;
2238 else if (cmp > 0)
2239 n = n->rb_right;
2240 else
2241 return iter;
2242 }
2243
2244 return NULL;
2245}
2246
Namhyung Kim09034de2016-09-13 16:45:46 +09002247static struct hist_entry *hists__find_hierarchy_entry(struct rb_root *root,
2248 struct hist_entry *he)
2249{
2250 struct rb_node *n = root->rb_node;
2251
2252 while (n) {
2253 struct hist_entry *iter;
2254 struct perf_hpp_fmt *fmt;
2255 int64_t cmp = 0;
2256
2257 iter = rb_entry(n, struct hist_entry, rb_node_in);
2258 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2259 cmp = fmt->collapse(fmt, iter, he);
2260 if (cmp)
2261 break;
2262 }
2263
2264 if (cmp < 0)
2265 n = n->rb_left;
2266 else if (cmp > 0)
2267 n = n->rb_right;
2268 else
2269 return iter;
2270 }
2271
2272 return NULL;
2273}
2274
2275static void hists__match_hierarchy(struct rb_root *leader_root,
2276 struct rb_root *other_root)
2277{
2278 struct rb_node *nd;
2279 struct hist_entry *pos, *pair;
2280
2281 for (nd = rb_first(leader_root); nd; nd = rb_next(nd)) {
2282 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2283 pair = hists__find_hierarchy_entry(other_root, pos);
2284
2285 if (pair) {
2286 hist_entry__add_pair(pair, pos);
2287 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2288 }
2289 }
2290}
2291
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002292/*
2293 * Look for pairs to link to the leader buckets (hist_entries):
2294 */
2295void hists__match(struct hists *leader, struct hists *other)
2296{
Namhyung Kimce74f602012-12-10 17:29:55 +09002297 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002298 struct rb_node *nd;
2299 struct hist_entry *pos, *pair;
2300
Namhyung Kim09034de2016-09-13 16:45:46 +09002301 if (symbol_conf.report_hierarchy) {
2302 /* hierarchy report always collapses entries */
2303 return hists__match_hierarchy(&leader->entries_collapsed,
2304 &other->entries_collapsed);
2305 }
2306
Jiri Olsa52225032016-05-03 13:54:42 +02002307 if (hists__has(leader, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002308 root = &leader->entries_collapsed;
2309 else
2310 root = leader->entries_in;
2311
2312 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2313 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002314 pair = hists__find_entry(other, pos);
2315
2316 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09002317 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002318 }
2319}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002320
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002321static int hists__link_hierarchy(struct hists *leader_hists,
2322 struct hist_entry *parent,
2323 struct rb_root *leader_root,
2324 struct rb_root *other_root)
2325{
2326 struct rb_node *nd;
2327 struct hist_entry *pos, *leader;
2328
2329 for (nd = rb_first(other_root); nd; nd = rb_next(nd)) {
2330 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2331
2332 if (hist_entry__has_pairs(pos)) {
2333 bool found = false;
2334
2335 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2336 if (leader->hists == leader_hists) {
2337 found = true;
2338 break;
2339 }
2340 }
2341 if (!found)
2342 return -1;
2343 } else {
2344 leader = add_dummy_hierarchy_entry(leader_hists,
2345 leader_root, pos);
2346 if (leader == NULL)
2347 return -1;
2348
2349 /* do not point parent in the pos */
2350 leader->parent_he = parent;
2351
2352 hist_entry__add_pair(pos, leader);
2353 }
2354
2355 if (!pos->leaf) {
2356 if (hists__link_hierarchy(leader_hists, leader,
2357 &leader->hroot_in,
2358 &pos->hroot_in) < 0)
2359 return -1;
2360 }
2361 }
2362 return 0;
2363}
2364
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002365/*
2366 * Look for entries in the other hists that are not present in the leader, if
2367 * we find them, just add a dummy entry on the leader hists, with period=0,
2368 * nr_events=0, to serve as the list header.
2369 */
2370int hists__link(struct hists *leader, struct hists *other)
2371{
Namhyung Kimce74f602012-12-10 17:29:55 +09002372 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002373 struct rb_node *nd;
2374 struct hist_entry *pos, *pair;
2375
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002376 if (symbol_conf.report_hierarchy) {
2377 /* hierarchy report always collapses entries */
2378 return hists__link_hierarchy(leader, NULL,
2379 &leader->entries_collapsed,
2380 &other->entries_collapsed);
2381 }
2382
Jiri Olsa52225032016-05-03 13:54:42 +02002383 if (hists__has(other, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002384 root = &other->entries_collapsed;
2385 else
2386 root = other->entries_in;
2387
2388 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2389 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002390
2391 if (!hist_entry__has_pairs(pos)) {
2392 pair = hists__add_dummy_entry(leader, pos);
2393 if (pair == NULL)
2394 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09002395 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002396 }
2397 }
2398
2399 return 0;
2400}
Namhyung Kimf2148332014-01-14 11:52:48 +09002401
Andi Kleen578499982015-07-18 08:24:49 -07002402void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2403 struct perf_sample *sample, bool nonany_branch_mode)
2404{
2405 struct branch_info *bi;
2406
2407 /* If we have branch cycles always annotate them. */
2408 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2409 int i;
2410
2411 bi = sample__resolve_bstack(sample, al);
2412 if (bi) {
2413 struct addr_map_symbol *prev = NULL;
2414
2415 /*
2416 * Ignore errors, still want to process the
2417 * other entries.
2418 *
2419 * For non standard branch modes always
2420 * force no IPC (prev == NULL)
2421 *
2422 * Note that perf stores branches reversed from
2423 * program order!
2424 */
2425 for (i = bs->nr - 1; i >= 0; i--) {
2426 addr_map_symbol__account_cycles(&bi[i].from,
2427 nonany_branch_mode ? NULL : prev,
2428 bi[i].flags.cycles);
2429 prev = &bi[i].to;
2430 }
2431 free(bi);
2432 }
2433 }
2434}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002435
2436size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
2437{
2438 struct perf_evsel *pos;
2439 size_t ret = 0;
2440
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002441 evlist__for_each_entry(evlist, pos) {
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002442 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2443 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2444 }
2445
2446 return ret;
2447}
2448
2449
Namhyung Kimf2148332014-01-14 11:52:48 +09002450u64 hists__total_period(struct hists *hists)
2451{
2452 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2453 hists->stats.total_period;
2454}
Namhyung Kim33db4562014-02-07 12:06:07 +09002455
2456int parse_filter_percentage(const struct option *opt __maybe_unused,
2457 const char *arg, int unset __maybe_unused)
2458{
2459 if (!strcmp(arg, "relative"))
2460 symbol_conf.filter_relative = true;
2461 else if (!strcmp(arg, "absolute"))
2462 symbol_conf.filter_relative = false;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002463 else {
Colin Ian Kinga596a872017-03-30 10:54:40 +01002464 pr_debug("Invalid percentage: %s\n", arg);
Namhyung Kim33db4562014-02-07 12:06:07 +09002465 return -1;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002466 }
Namhyung Kim33db4562014-02-07 12:06:07 +09002467
2468 return 0;
2469}
Namhyung Kim0b93da12014-01-14 12:02:15 +09002470
2471int perf_hist_config(const char *var, const char *value)
2472{
2473 if (!strcmp(var, "hist.percentage"))
2474 return parse_filter_percentage(NULL, value, 0);
2475
2476 return 0;
2477}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002478
Jiri Olsa5b658552016-01-18 10:24:22 +01002479int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002480{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002481 memset(hists, 0, sizeof(*hists));
2482 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
2483 hists->entries_in = &hists->entries_in_array[0];
2484 hists->entries_collapsed = RB_ROOT;
2485 hists->entries = RB_ROOT;
2486 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04002487 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01002488 hists->hpp_list = hpp_list;
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002489 INIT_LIST_HEAD(&hists->hpp_formats);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002490 return 0;
2491}
2492
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002493static void hists__delete_remaining_entries(struct rb_root *root)
2494{
2495 struct rb_node *node;
2496 struct hist_entry *he;
2497
2498 while (!RB_EMPTY_ROOT(root)) {
2499 node = rb_first(root);
2500 rb_erase(node, root);
2501
2502 he = rb_entry(node, struct hist_entry, rb_node_in);
2503 hist_entry__delete(he);
2504 }
2505}
2506
2507static void hists__delete_all_entries(struct hists *hists)
2508{
2509 hists__delete_entries(hists);
2510 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2511 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2512 hists__delete_remaining_entries(&hists->entries_collapsed);
2513}
2514
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002515static void hists_evsel__exit(struct perf_evsel *evsel)
2516{
2517 struct hists *hists = evsel__hists(evsel);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002518 struct perf_hpp_fmt *fmt, *pos;
2519 struct perf_hpp_list_node *node, *tmp;
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002520
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002521 hists__delete_all_entries(hists);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002522
2523 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2524 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2525 list_del(&fmt->list);
2526 free(fmt);
2527 }
2528 list_del(&node->list);
2529 free(node);
2530 }
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002531}
2532
Namhyung Kimfc284be2016-01-07 10:14:10 +01002533static int hists_evsel__init(struct perf_evsel *evsel)
2534{
2535 struct hists *hists = evsel__hists(evsel);
2536
Jiri Olsa5b658552016-01-18 10:24:22 +01002537 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01002538 return 0;
2539}
2540
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002541/*
2542 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2543 * stored in the rbtree...
2544 */
2545
2546int hists__init(void)
2547{
2548 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002549 hists_evsel__init,
2550 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002551 if (err)
2552 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2553
2554 return err;
2555}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01002556
2557void perf_hpp_list__init(struct perf_hpp_list *list)
2558{
2559 INIT_LIST_HEAD(&list->fields);
2560 INIT_LIST_HEAD(&list->sorts);
2561}