blob: 25d143053ab57b57a4b456e784dc6b7061d83420 [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"
Arnaldo Carvalho de Meloe7ff8922017-04-19 21:34:35 -030012#include "thread.h"
Namhyung Kim740b97f2014-12-22 13:44:10 +090013#include "ui/progress.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030014#include <errno.h>
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -020015#include <math.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030016#include <sys/param.h>
John Kacur3d1d07e2009-09-28 15:32:55 +020017
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020018static bool hists__filter_entry_by_dso(struct hists *hists,
19 struct hist_entry *he);
20static bool hists__filter_entry_by_thread(struct hists *hists,
21 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090022static bool hists__filter_entry_by_symbol(struct hists *hists,
23 struct hist_entry *he);
Kan Liang21394d92015-09-04 10:45:44 -040024static bool hists__filter_entry_by_socket(struct hists *hists,
25 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020026
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030027u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030028{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030029 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030030}
31
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030032void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030033{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030034 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030035}
36
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030037bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030038{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030039 if (len > hists__col_len(hists, col)) {
40 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030041 return true;
42 }
43 return false;
44}
45
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090046void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030047{
48 enum hist_column col;
49
50 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030051 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030052}
53
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010054static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
55{
56 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
57
58 if (hists__col_len(hists, dso) < unresolved_col_width &&
59 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
60 !symbol_conf.dso_list)
61 hists__set_col_len(hists, dso, unresolved_col_width);
62}
63
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090064void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030065{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010066 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Stephane Eranian98a3b322013-01-24 16:10:35 +010067 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030068 u16 len;
69
Namhyung Kimded19d52013-04-01 20:35:19 +090070 /*
71 * +4 accounts for '[x] ' priv level info
72 * +2 accounts for 0x prefix on raw addresses
73 * +3 accounts for ' y ' symtab origin info
74 */
75 if (h->ms.sym) {
76 symlen = h->ms.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +090077 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +090078 symlen += BITS_PER_LONG / 4 + 2 + 3;
79 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
80 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010081 symlen = unresolved_col_width + 4 + 2;
82 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010083 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +010084 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030085
86 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030087 if (hists__new_col_len(hists, HISTC_COMM, len))
Jiri Olsa89c7cb22016-06-20 23:58:19 +020088 hists__set_col_len(hists, HISTC_THREAD, len + 8);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030089
90 if (h->ms.map) {
91 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030092 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030093 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010094
Namhyung Kimcb993742012-12-27 18:11:42 +090095 if (h->parent)
96 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
97
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010098 if (h->branch_info) {
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010099 if (h->branch_info->from.sym) {
100 symlen = (int)h->branch_info->from.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +0900101 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +0900102 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100103 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
104
105 symlen = dso__name_len(h->branch_info->from.map->dso);
106 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
107 } else {
108 symlen = unresolved_col_width + 4 + 2;
109 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
110 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
111 }
112
113 if (h->branch_info->to.sym) {
114 symlen = (int)h->branch_info->to.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +0900115 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +0900116 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100117 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
118
119 symlen = dso__name_len(h->branch_info->to.map->dso);
120 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
121 } else {
122 symlen = unresolved_col_width + 4 + 2;
123 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
124 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
125 }
Andi Kleen508be0d2016-05-20 13:15:08 -0700126
127 if (h->branch_info->srcline_from)
128 hists__new_col_len(hists, HISTC_SRCLINE_FROM,
129 strlen(h->branch_info->srcline_from));
130 if (h->branch_info->srcline_to)
131 hists__new_col_len(hists, HISTC_SRCLINE_TO,
132 strlen(h->branch_info->srcline_to));
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100133 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100134
135 if (h->mem_info) {
Stephane Eranian98a3b322013-01-24 16:10:35 +0100136 if (h->mem_info->daddr.sym) {
137 symlen = (int)h->mem_info->daddr.sym->namelen + 4
138 + unresolved_col_width + 2;
139 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
140 symlen);
Don Zickus9b32ba72014-06-01 15:38:29 +0200141 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
142 symlen + 1);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100143 } else {
144 symlen = unresolved_col_width + 4 + 2;
145 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
146 symlen);
Jiri Olsa08059092016-01-20 12:56:33 +0100147 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
148 symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100149 }
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200150
151 if (h->mem_info->iaddr.sym) {
152 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
153 + unresolved_col_width + 2;
154 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
155 symlen);
156 } else {
157 symlen = unresolved_col_width + 4 + 2;
158 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
159 symlen);
160 }
161
Stephane Eranian98a3b322013-01-24 16:10:35 +0100162 if (h->mem_info->daddr.map) {
163 symlen = dso__name_len(h->mem_info->daddr.map->dso);
164 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
165 symlen);
166 } else {
167 symlen = unresolved_col_width + 4 + 2;
168 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
169 }
Kan Liang8780fb22017-08-29 13:11:09 -0400170
171 hists__new_col_len(hists, HISTC_MEM_PHYS_DADDR,
172 unresolved_col_width + 4 + 2);
173
Stephane Eranian98a3b322013-01-24 16:10:35 +0100174 } else {
175 symlen = unresolved_col_width + 4 + 2;
176 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200177 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100178 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
179 }
180
Hari Bathinid890a982017-03-08 02:12:13 +0530181 hists__new_col_len(hists, HISTC_CGROUP_ID, 20);
Arnaldo Carvalho de Meloa4978ec2015-09-09 12:14:00 -0300182 hists__new_col_len(hists, HISTC_CPU, 3);
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400183 hists__new_col_len(hists, HISTC_SOCKET, 6);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100184 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
185 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
186 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
187 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
188 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
189 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700190
Jiri Olsaf666ac02016-09-19 15:10:10 +0200191 if (h->srcline) {
192 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
193 hists__new_col_len(hists, HISTC_SRCLINE, len);
194 }
Arnaldo Carvalho de Meloe8e6d372015-08-10 16:53:54 -0300195
Andi Kleen31191a82015-08-07 15:54:24 -0700196 if (h->srcfile)
197 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
198
Andi Kleen475eeab2013-09-20 07:40:43 -0700199 if (h->transaction)
200 hists__new_col_len(hists, HISTC_TRANSACTION,
201 hist_entry__transaction_len());
Namhyung Kim0c0af782016-02-21 23:22:38 +0900202
203 if (h->trace_output)
204 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300205}
206
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900207void hists__output_recalc_col_len(struct hists *hists, int max_rows)
208{
209 struct rb_node *next = rb_first(&hists->entries);
210 struct hist_entry *n;
211 int row = 0;
212
213 hists__reset_col_len(hists);
214
215 while (next && row++ < max_rows) {
216 n = rb_entry(next, struct hist_entry, rb_node);
217 if (!n->filtered)
218 hists__calc_col_len(hists, n);
219 next = rb_next(&n->rb_node);
220 }
221}
222
Namhyung Kimf39056f2014-01-14 14:25:37 +0900223static void he_stat__add_cpumode_period(struct he_stat *he_stat,
224 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800225{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300226 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800227 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900228 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800229 break;
230 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900231 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800232 break;
233 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900234 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800235 break;
236 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900237 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800238 break;
239 default:
240 break;
241 }
242}
243
Andi Kleen05484292013-01-24 16:10:29 +0100244static void he_stat__add_period(struct he_stat *he_stat, u64 period,
245 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900246{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100247
Namhyung Kim139c0812012-10-04 21:49:43 +0900248 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100249 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900250 he_stat->nr_events += 1;
251}
252
253static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
254{
255 dest->period += src->period;
256 dest->period_sys += src->period_sys;
257 dest->period_us += src->period_us;
258 dest->period_guest_sys += src->period_guest_sys;
259 dest->period_guest_us += src->period_guest_us;
260 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100261 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900262}
263
Namhyung Kimf39056f2014-01-14 14:25:37 +0900264static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300265{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900266 he_stat->period = (he_stat->period * 7) / 8;
267 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100268 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300269}
270
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900271static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
272
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300273static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
274{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900275 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900276 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200277
278 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300279 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200280
Namhyung Kimf39056f2014-01-14 14:25:37 +0900281 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900282 if (symbol_conf.cumulate_callchain)
283 he_stat__decay(he->stat_acc);
Namhyung Kim42b276a2016-01-05 12:06:00 +0900284 decay_callchain(he->callchain);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200285
Namhyung Kim3186b682014-04-22 13:44:23 +0900286 diff = prev_period - he->stat.period;
287
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900288 if (!he->depth) {
289 hists->stats.total_period -= diff;
290 if (!he->filtered)
291 hists->stats.total_non_filtered_period -= diff;
292 }
293
294 if (!he->leaf) {
295 struct hist_entry *child;
296 struct rb_node *node = rb_first(&he->hroot_out);
297 while (node) {
298 child = rb_entry(node, struct hist_entry, rb_node);
299 node = rb_next(node);
300
301 if (hists__decay_entry(hists, child))
302 hists__delete_entry(hists, child);
303 }
304 }
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200305
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900306 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300307}
308
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300309static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
310{
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900311 struct rb_root *root_in;
312 struct rb_root *root_out;
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300313
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900314 if (he->parent_he) {
315 root_in = &he->parent_he->hroot_in;
316 root_out = &he->parent_he->hroot_out;
317 } else {
Jiri Olsa52225032016-05-03 13:54:42 +0200318 if (hists__has(hists, need_collapse))
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900319 root_in = &hists->entries_collapsed;
320 else
321 root_in = hists->entries_in;
322 root_out = &hists->entries;
323 }
324
325 rb_erase(&he->rb_node_in, root_in);
326 rb_erase(&he->rb_node, root_out);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300327
328 --hists->nr_entries;
329 if (!he->filtered)
330 --hists->nr_non_filtered_entries;
331
332 hist_entry__delete(he);
333}
334
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900335void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300336{
337 struct rb_node *next = rb_first(&hists->entries);
338 struct hist_entry *n;
339
340 while (next) {
341 n = rb_entry(next, struct hist_entry, rb_node);
342 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200343 if (((zap_user && n->level == '.') ||
344 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300345 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300346 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300347 }
348 }
349}
350
Namhyung Kim701937b2014-08-12 17:16:05 +0900351void hists__delete_entries(struct hists *hists)
352{
353 struct rb_node *next = rb_first(&hists->entries);
354 struct hist_entry *n;
355
356 while (next) {
357 n = rb_entry(next, struct hist_entry, rb_node);
358 next = rb_next(&n->rb_node);
359
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300360 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900361 }
362}
363
John Kacur3d1d07e2009-09-28 15:32:55 +0200364/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300365 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200366 */
367
Jiri Olsa0a269a62016-07-05 08:56:03 +0200368static int hist_entry__init(struct hist_entry *he,
369 struct hist_entry *template,
370 bool sample_self)
371{
372 *he = *template;
373
374 if (symbol_conf.cumulate_callchain) {
375 he->stat_acc = malloc(sizeof(he->stat));
376 if (he->stat_acc == NULL)
377 return -ENOMEM;
378 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
379 if (!sample_self)
380 memset(&he->stat, 0, sizeof(he->stat));
381 }
382
383 map__get(he->ms.map);
384
385 if (he->branch_info) {
386 /*
387 * This branch info is (a part of) allocated from
388 * sample__resolve_bstack() and will be freed after
389 * adding new entries. So we need to save a copy.
390 */
391 he->branch_info = malloc(sizeof(*he->branch_info));
392 if (he->branch_info == NULL) {
393 map__zput(he->ms.map);
394 free(he->stat_acc);
395 return -ENOMEM;
396 }
397
398 memcpy(he->branch_info, template->branch_info,
399 sizeof(*he->branch_info));
400
401 map__get(he->branch_info->from.map);
402 map__get(he->branch_info->to.map);
403 }
404
405 if (he->mem_info) {
406 map__get(he->mem_info->iaddr.map);
407 map__get(he->mem_info->daddr.map);
408 }
409
410 if (symbol_conf.use_callchain)
411 callchain_init(he->callchain);
412
413 if (he->raw_data) {
414 he->raw_data = memdup(he->raw_data, he->raw_size);
415
416 if (he->raw_data == NULL) {
417 map__put(he->ms.map);
418 if (he->branch_info) {
419 map__put(he->branch_info->from.map);
420 map__put(he->branch_info->to.map);
421 free(he->branch_info);
422 }
423 if (he->mem_info) {
424 map__put(he->mem_info->iaddr.map);
425 map__put(he->mem_info->daddr.map);
426 }
427 free(he->stat_acc);
428 return -ENOMEM;
429 }
430 }
431 INIT_LIST_HEAD(&he->pairs.node);
432 thread__get(he->thread);
Namhyung Kimd2580c72016-09-13 16:45:48 +0900433 he->hroot_in = RB_ROOT;
434 he->hroot_out = RB_ROOT;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200435
436 if (!symbol_conf.report_hierarchy)
437 he->leaf = true;
438
439 return 0;
440}
441
Jiri Olsaf542e762016-07-05 08:56:04 +0200442static void *hist_entry__zalloc(size_t size)
443{
444 return zalloc(size + sizeof(struct hist_entry));
445}
446
447static void hist_entry__free(void *ptr)
448{
449 free(ptr);
450}
451
452static struct hist_entry_ops default_ops = {
453 .new = hist_entry__zalloc,
454 .free = hist_entry__free,
455};
456
Namhyung Kima0b51af2012-09-11 13:34:27 +0900457static struct hist_entry *hist_entry__new(struct hist_entry *template,
458 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300459{
Jiri Olsaf542e762016-07-05 08:56:04 +0200460 struct hist_entry_ops *ops = template->ops;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900461 size_t callchain_size = 0;
462 struct hist_entry *he;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200463 int err = 0;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900464
Jiri Olsaf542e762016-07-05 08:56:04 +0200465 if (!ops)
466 ops = template->ops = &default_ops;
467
Namhyung Kim82aa0192014-12-22 13:44:14 +0900468 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900469 callchain_size = sizeof(struct callchain_root);
470
Jiri Olsaf542e762016-07-05 08:56:04 +0200471 he = ops->new(callchain_size);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200472 if (he) {
473 err = hist_entry__init(he, template, sample_self);
Jiri Olsaf542e762016-07-05 08:56:04 +0200474 if (err) {
475 ops->free(he);
476 he = NULL;
477 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300478 }
479
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200480 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300481}
482
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300483static u8 symbol__parent_filter(const struct symbol *parent)
484{
485 if (symbol_conf.exclude_other && parent == NULL)
486 return 1 << HIST_FILTER__PARENT;
487 return 0;
488}
489
Namhyung Kim467ef102016-02-16 23:08:19 +0900490static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
491{
492 if (!symbol_conf.use_callchain)
493 return;
494
495 he->hists->callchain_period += period;
496 if (!he->filtered)
497 he->hists->callchain_non_filtered_period += period;
498}
499
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300500static struct hist_entry *hists__findnew_entry(struct hists *hists,
501 struct hist_entry *entry,
502 struct addr_location *al,
503 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300504{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300505 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300506 struct rb_node *parent = NULL;
507 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700508 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900509 u64 period = entry->stat.period;
510 u64 weight = entry->stat.weight;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300511
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300512 p = &hists->entries_in->rb_node;
513
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300514 while (*p != NULL) {
515 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300516 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300517
Namhyung Kim9afcf932012-12-10 17:29:54 +0900518 /*
519 * Make sure that it receives arguments in a same order as
520 * hist_entry__collapse() so that we can use an appropriate
521 * function when searching an entry regardless which sort
522 * keys were used.
523 */
524 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300525
526 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900527 if (sample_self) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900528 he_stat__add_period(&he->stat, period, weight);
Namhyung Kim467ef102016-02-16 23:08:19 +0900529 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900530 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900531 if (symbol_conf.cumulate_callchain)
532 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400533
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900534 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300535 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900536 * and will not be used anymore.
537 */
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300538 zfree(&entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900539
David Miller63fa4712012-03-27 03:14:18 -0400540 /* If the map of an existing hist_entry has
541 * become out-of-date due to an exec() or
542 * similar, update it. Otherwise we will
543 * mis-adjust symbol addresses when computing
544 * the history counter to increment.
545 */
546 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300547 map__put(he->ms.map);
548 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400549 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300550 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300551 }
552
553 if (cmp < 0)
554 p = &(*p)->rb_left;
555 else
556 p = &(*p)->rb_right;
557 }
558
Namhyung Kima0b51af2012-09-11 13:34:27 +0900559 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300560 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900561 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300562
Namhyung Kim0f584742016-01-28 00:40:49 +0900563 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900564 hist_entry__add_callchain_period(he, period);
565 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900566
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300567 rb_link_node(&he->rb_node_in, parent, p);
568 rb_insert_color(&he->rb_node_in, hists->entries_in);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300569out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900570 if (sample_self)
571 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900572 if (symbol_conf.cumulate_callchain)
573 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300574 return he;
575}
576
Jiri Olsaa5051972016-07-05 08:56:05 +0200577static struct hist_entry*
578__hists__add_entry(struct hists *hists,
579 struct addr_location *al,
580 struct symbol *sym_parent,
581 struct branch_info *bi,
582 struct mem_info *mi,
583 struct perf_sample *sample,
584 bool sample_self,
585 struct hist_entry_ops *ops)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100586{
Hari Bathinid890a982017-03-08 02:12:13 +0530587 struct namespaces *ns = thread__namespaces(al->thread);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100588 struct hist_entry entry = {
589 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900590 .comm = thread__comm(al->thread),
Hari Bathinid890a982017-03-08 02:12:13 +0530591 .cgroup_id = {
592 .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0,
593 .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0,
594 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100595 .ms = {
596 .map = al->map,
597 .sym = al->sym,
598 },
Milian Wolff1fb7d062017-10-19 13:38:35 +0200599 .srcline = al->srcline ? strdup(al->srcline) : NULL,
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400600 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400601 .cpu = al->cpu,
602 .cpumode = al->cpumode,
603 .ip = al->addr,
604 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900605 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900606 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900607 .period = sample->period,
608 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900609 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100610 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300611 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300612 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900613 .branch_info = bi,
614 .mem_info = mi,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900615 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900616 .raw_data = sample->raw_data,
617 .raw_size = sample->raw_size,
Jiri Olsaa5051972016-07-05 08:56:05 +0200618 .ops = ops,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100619 };
620
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300621 return hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100622}
623
Jiri Olsaa5051972016-07-05 08:56:05 +0200624struct hist_entry *hists__add_entry(struct hists *hists,
625 struct addr_location *al,
626 struct symbol *sym_parent,
627 struct branch_info *bi,
628 struct mem_info *mi,
629 struct perf_sample *sample,
630 bool sample_self)
631{
632 return __hists__add_entry(hists, al, sym_parent, bi, mi,
633 sample, sample_self, NULL);
634}
635
636struct hist_entry *hists__add_entry_ops(struct hists *hists,
637 struct hist_entry_ops *ops,
638 struct addr_location *al,
639 struct symbol *sym_parent,
640 struct branch_info *bi,
641 struct mem_info *mi,
642 struct perf_sample *sample,
643 bool sample_self)
644{
645 return __hists__add_entry(hists, al, sym_parent, bi, mi,
646 sample, sample_self, ops);
647}
648
Namhyung Kim69bcb012013-10-30 09:40:34 +0900649static int
650iter_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_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
658 struct addr_location *al __maybe_unused)
659{
660 return 0;
661}
662
663static int
664iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
665{
666 struct perf_sample *sample = iter->sample;
667 struct mem_info *mi;
668
669 mi = sample__resolve_mem(sample, al);
670 if (mi == NULL)
671 return -ENOMEM;
672
673 iter->priv = mi;
674 return 0;
675}
676
677static int
678iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
679{
680 u64 cost;
681 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300682 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900683 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900684 struct hist_entry *he;
685
686 if (mi == NULL)
687 return -EINVAL;
688
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900689 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900690 if (!cost)
691 cost = 1;
692
693 /*
694 * must pass period=weight in order to get the correct
695 * sorting from hists__collapse_resort() which is solely
696 * based on periods. We want sorting be done on nr_events * weight
697 * and this is indirectly achieved by passing period=weight here
698 * and the he_stat__add_period() function.
699 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900700 sample->period = cost;
701
Jiri Olsa0102ef32016-06-14 20:19:21 +0200702 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
703 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900704 if (!he)
705 return -ENOMEM;
706
707 iter->he = he;
708 return 0;
709}
710
711static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900712iter_finish_mem_entry(struct hist_entry_iter *iter,
713 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900714{
715 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300716 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900717 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900718 int err = -EINVAL;
719
720 if (he == NULL)
721 goto out;
722
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300723 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900724
725 err = hist_entry__append_callchain(he, iter->sample);
726
727out:
728 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300729 * We don't need to free iter->priv (mem_info) here since the mem info
730 * was either already freed in hists__findnew_entry() or passed to a
731 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900732 */
733 iter->priv = NULL;
734
735 iter->he = NULL;
736 return err;
737}
738
739static int
740iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
741{
742 struct branch_info *bi;
743 struct perf_sample *sample = iter->sample;
744
745 bi = sample__resolve_bstack(sample, al);
746 if (!bi)
747 return -ENOMEM;
748
749 iter->curr = 0;
750 iter->total = sample->branch_stack->nr;
751
752 iter->priv = bi;
753 return 0;
754}
755
756static int
Jin Yao2d78b182017-07-18 20:13:14 +0800757iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900758 struct addr_location *al __maybe_unused)
759{
760 return 0;
761}
762
763static int
764iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
765{
766 struct branch_info *bi = iter->priv;
767 int i = iter->curr;
768
769 if (bi == NULL)
770 return 0;
771
772 if (iter->curr >= iter->total)
773 return 0;
774
775 al->map = bi[i].to.map;
776 al->sym = bi[i].to.sym;
777 al->addr = bi[i].to.addr;
778 return 1;
779}
780
781static int
782iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
783{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900784 struct branch_info *bi;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900785 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300786 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900787 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900788 struct hist_entry *he = NULL;
789 int i = iter->curr;
790 int err = 0;
791
792 bi = iter->priv;
793
794 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
795 goto out;
796
797 /*
798 * The report shows the percentage of total branches captured
799 * and not events sampled. Thus we use a pseudo period of 1.
800 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900801 sample->period = 1;
802 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
803
Jiri Olsa0102ef32016-06-14 20:19:21 +0200804 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
805 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900806 if (he == NULL)
807 return -ENOMEM;
808
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300809 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900810
811out:
812 iter->he = he;
813 iter->curr++;
814 return err;
815}
816
817static int
818iter_finish_branch_entry(struct hist_entry_iter *iter,
819 struct addr_location *al __maybe_unused)
820{
821 zfree(&iter->priv);
822 iter->he = NULL;
823
824 return iter->curr >= iter->total ? 0 : -1;
825}
826
827static int
828iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
829 struct addr_location *al __maybe_unused)
830{
831 return 0;
832}
833
834static int
835iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
836{
837 struct perf_evsel *evsel = iter->evsel;
838 struct perf_sample *sample = iter->sample;
839 struct hist_entry *he;
840
Jiri Olsa0102ef32016-06-14 20:19:21 +0200841 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
842 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900843 if (he == NULL)
844 return -ENOMEM;
845
846 iter->he = he;
847 return 0;
848}
849
850static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900851iter_finish_normal_entry(struct hist_entry_iter *iter,
852 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900853{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900854 struct hist_entry *he = iter->he;
855 struct perf_evsel *evsel = iter->evsel;
856 struct perf_sample *sample = iter->sample;
857
858 if (he == NULL)
859 return 0;
860
861 iter->he = NULL;
862
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300863 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900864
865 return hist_entry__append_callchain(he, sample);
866}
867
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900868static int
Adrian Hunter96b40f32015-09-25 16:15:47 +0300869iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900870 struct addr_location *al __maybe_unused)
871{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900872 struct hist_entry **he_cache;
873
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900874 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900875
876 /*
877 * This is for detecting cycles or recursions so that they're
878 * cumulated only one time to prevent entries more than 100%
879 * overhead.
880 */
Adrian Hunter96b40f32015-09-25 16:15:47 +0300881 he_cache = malloc(sizeof(*he_cache) * (iter->max_stack + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900882 if (he_cache == NULL)
883 return -ENOMEM;
884
885 iter->priv = he_cache;
886 iter->curr = 0;
887
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900888 return 0;
889}
890
891static int
892iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
893 struct addr_location *al)
894{
895 struct perf_evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300896 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900897 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900898 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900899 struct hist_entry *he;
900 int err = 0;
901
Jiri Olsa0102ef32016-06-14 20:19:21 +0200902 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
903 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900904 if (he == NULL)
905 return -ENOMEM;
906
907 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900908 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900909
Namhyung Kim82aa0192014-12-22 13:44:14 +0900910 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900911
912 /*
913 * We need to re-initialize the cursor since callchain_append()
914 * advanced the cursor to the end.
915 */
916 callchain_cursor_commit(&callchain_cursor);
917
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300918 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900919
920 return err;
921}
922
923static int
924iter_next_cumulative_entry(struct hist_entry_iter *iter,
925 struct addr_location *al)
926{
927 struct callchain_cursor_node *node;
928
929 node = callchain_cursor_current(&callchain_cursor);
930 if (node == NULL)
931 return 0;
932
Namhyung Kimc7405d82013-10-31 13:58:30 +0900933 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900934}
935
936static int
937iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
938 struct addr_location *al)
939{
940 struct perf_evsel *evsel = iter->evsel;
941 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900942 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900943 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900944 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -0300945 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900946 .cpu = al->cpu,
947 .thread = al->thread,
948 .comm = thread__comm(al->thread),
949 .ip = al->addr,
950 .ms = {
951 .map = al->map,
952 .sym = al->sym,
953 },
Milian Wolff1fb7d062017-10-19 13:38:35 +0200954 .srcline = al->srcline ? strdup(al->srcline) : NULL,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900955 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +0900956 .raw_data = sample->raw_data,
957 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900958 };
959 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +0900960 struct callchain_cursor cursor;
961
962 callchain_cursor_snapshot(&cursor, &callchain_cursor);
963
964 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900965
966 /*
967 * Check if there's duplicate entries in the callchain.
968 * It's possible that it has cycles or recursive calls.
969 */
970 for (i = 0; i < iter->curr; i++) {
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900971 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
972 /* to avoid calling callback function */
973 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900974 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900975 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900976 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900977
Jiri Olsa0102ef32016-06-14 20:19:21 +0200978 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
979 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900980 if (he == NULL)
981 return -ENOMEM;
982
983 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900984 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900985
Namhyung Kim82aa0192014-12-22 13:44:14 +0900986 if (symbol_conf.use_callchain)
987 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900988 return 0;
989}
990
991static int
992iter_finish_cumulative_entry(struct hist_entry_iter *iter,
993 struct addr_location *al __maybe_unused)
994{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900995 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900996 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +0900997
Namhyung Kim7a13aa22012-09-11 14:13:04 +0900998 return 0;
999}
1000
Namhyung Kim69bcb012013-10-30 09:40:34 +09001001const struct hist_iter_ops hist_iter_mem = {
1002 .prepare_entry = iter_prepare_mem_entry,
1003 .add_single_entry = iter_add_single_mem_entry,
1004 .next_entry = iter_next_nop_entry,
1005 .add_next_entry = iter_add_next_nop_entry,
1006 .finish_entry = iter_finish_mem_entry,
1007};
1008
1009const struct hist_iter_ops hist_iter_branch = {
1010 .prepare_entry = iter_prepare_branch_entry,
1011 .add_single_entry = iter_add_single_branch_entry,
1012 .next_entry = iter_next_branch_entry,
1013 .add_next_entry = iter_add_next_branch_entry,
1014 .finish_entry = iter_finish_branch_entry,
1015};
1016
1017const struct hist_iter_ops hist_iter_normal = {
1018 .prepare_entry = iter_prepare_normal_entry,
1019 .add_single_entry = iter_add_single_normal_entry,
1020 .next_entry = iter_next_nop_entry,
1021 .add_next_entry = iter_add_next_nop_entry,
1022 .finish_entry = iter_finish_normal_entry,
1023};
1024
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001025const struct hist_iter_ops hist_iter_cumulative = {
1026 .prepare_entry = iter_prepare_cumulative_entry,
1027 .add_single_entry = iter_add_single_cumulative_entry,
1028 .next_entry = iter_next_cumulative_entry,
1029 .add_next_entry = iter_add_next_cumulative_entry,
1030 .finish_entry = iter_finish_cumulative_entry,
1031};
1032
Namhyung Kim69bcb012013-10-30 09:40:34 +09001033int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001034 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +09001035{
1036 int err, err2;
Krister Johansen9c68ae92017-01-05 22:23:31 -08001037 struct map *alm = NULL;
1038
1039 if (al && al->map)
1040 alm = map__get(al->map);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001041
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001042 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
Namhyung Kim063bd932015-05-19 17:04:10 +09001043 iter->evsel, al, max_stack_depth);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001044 if (err)
1045 return err;
1046
Adrian Hunter96b40f32015-09-25 16:15:47 +03001047 iter->max_stack = max_stack_depth;
1048
Namhyung Kim69bcb012013-10-30 09:40:34 +09001049 err = iter->ops->prepare_entry(iter, al);
1050 if (err)
1051 goto out;
1052
1053 err = iter->ops->add_single_entry(iter, al);
1054 if (err)
1055 goto out;
1056
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001057 if (iter->he && iter->add_entry_cb) {
1058 err = iter->add_entry_cb(iter, al, true, arg);
1059 if (err)
1060 goto out;
1061 }
1062
Namhyung Kim69bcb012013-10-30 09:40:34 +09001063 while (iter->ops->next_entry(iter, al)) {
1064 err = iter->ops->add_next_entry(iter, al);
1065 if (err)
1066 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001067
1068 if (iter->he && iter->add_entry_cb) {
1069 err = iter->add_entry_cb(iter, al, false, arg);
1070 if (err)
1071 goto out;
1072 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001073 }
1074
1075out:
1076 err2 = iter->ops->finish_entry(iter, al);
1077 if (!err)
1078 err = err2;
1079
Krister Johansen9c68ae92017-01-05 22:23:31 -08001080 map__put(alm);
1081
Namhyung Kim69bcb012013-10-30 09:40:34 +09001082 return err;
1083}
1084
John Kacur3d1d07e2009-09-28 15:32:55 +02001085int64_t
1086hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1087{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001088 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001089 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001090 int64_t cmp = 0;
1091
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001092 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001093 if (perf_hpp__is_dynamic_entry(fmt) &&
1094 !perf_hpp__defined_dynamic_entry(fmt, hists))
1095 continue;
1096
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001097 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001098 if (cmp)
1099 break;
1100 }
1101
1102 return cmp;
1103}
1104
1105int64_t
1106hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1107{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001108 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001109 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001110 int64_t cmp = 0;
1111
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001112 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001113 if (perf_hpp__is_dynamic_entry(fmt) &&
1114 !perf_hpp__defined_dynamic_entry(fmt, hists))
1115 continue;
1116
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001117 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001118 if (cmp)
1119 break;
1120 }
1121
1122 return cmp;
1123}
1124
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001125void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001126{
Jiri Olsaf542e762016-07-05 08:56:04 +02001127 struct hist_entry_ops *ops = he->ops;
1128
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001129 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001130 map__zput(he->ms.map);
1131
1132 if (he->branch_info) {
1133 map__zput(he->branch_info->from.map);
1134 map__zput(he->branch_info->to.map);
Andi Kleen508be0d2016-05-20 13:15:08 -07001135 free_srcline(he->branch_info->srcline_from);
1136 free_srcline(he->branch_info->srcline_to);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001137 zfree(&he->branch_info);
1138 }
1139
1140 if (he->mem_info) {
1141 map__zput(he->mem_info->iaddr.map);
1142 map__zput(he->mem_info->daddr.map);
1143 zfree(&he->mem_info);
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
Jin Yaob49a8212017-05-08 18:43:02 +08001763 use_callchain |= symbol_conf.show_branchflag_count;
1764
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001765 output_resort(evsel__hists(evsel), prog, use_callchain, NULL);
Jiri Olsa452ce032016-01-18 10:24:00 +01001766}
1767
1768void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1769{
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001770 output_resort(hists, prog, symbol_conf.use_callchain, NULL);
1771}
1772
1773void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1774 hists__resort_cb_t cb)
1775{
1776 output_resort(hists, prog, symbol_conf.use_callchain, cb);
Jiri Olsa01441af2016-01-18 10:23:59 +01001777}
1778
Namhyung Kim8c018722016-02-25 00:13:36 +09001779static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1780{
1781 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1782 return false;
1783
1784 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1785 return true;
1786
1787 return false;
1788}
1789
1790struct rb_node *rb_hierarchy_last(struct rb_node *node)
1791{
1792 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1793
1794 while (can_goto_child(he, HMD_NORMAL)) {
1795 node = rb_last(&he->hroot_out);
1796 he = rb_entry(node, struct hist_entry, rb_node);
1797 }
1798 return node;
1799}
1800
1801struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1802{
1803 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1804
1805 if (can_goto_child(he, hmd))
1806 node = rb_first(&he->hroot_out);
1807 else
1808 node = rb_next(node);
1809
1810 while (node == NULL) {
1811 he = he->parent_he;
1812 if (he == NULL)
1813 break;
1814
1815 node = rb_next(&he->rb_node);
1816 }
1817 return node;
1818}
1819
1820struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1821{
1822 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1823
1824 node = rb_prev(node);
1825 if (node)
1826 return rb_hierarchy_last(node);
1827
1828 he = he->parent_he;
1829 if (he == NULL)
1830 return NULL;
1831
1832 return &he->rb_node;
1833}
1834
Namhyung Kima7b58952016-02-26 21:13:16 +09001835bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1836{
1837 struct rb_node *node;
1838 struct hist_entry *child;
1839 float percent;
1840
1841 if (he->leaf)
1842 return false;
1843
1844 node = rb_first(&he->hroot_out);
1845 child = rb_entry(node, struct hist_entry, rb_node);
1846
1847 while (node && child->filtered) {
1848 node = rb_next(node);
1849 child = rb_entry(node, struct hist_entry, rb_node);
1850 }
1851
1852 if (node)
1853 percent = hist_entry__get_percent_limit(child);
1854 else
1855 percent = 0;
1856
1857 return node && percent >= limit;
1858}
1859
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001860static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001861 enum hist_filter filter)
1862{
1863 h->filtered &= ~(1 << filter);
Namhyung Kim155e9af2016-02-25 00:13:38 +09001864
1865 if (symbol_conf.report_hierarchy) {
1866 struct hist_entry *parent = h->parent_he;
1867
1868 while (parent) {
1869 he_stat__add_stat(&parent->stat, &h->stat);
1870
1871 parent->filtered &= ~(1 << filter);
1872
1873 if (parent->filtered)
1874 goto next;
1875
1876 /* force fold unfiltered entry for simplicity */
1877 parent->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001878 parent->has_no_entry = false;
Namhyung Kim155e9af2016-02-25 00:13:38 +09001879 parent->row_offset = 0;
1880 parent->nr_rows = 0;
1881next:
1882 parent = parent->parent_he;
1883 }
1884 }
1885
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001886 if (h->filtered)
1887 return;
1888
Namhyung Kim87e90f42014-04-24 16:44:16 +09001889 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09001890 h->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09001891 h->has_no_entry = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03001892 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08001893 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09001894
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09001895 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001896
Namhyung Kim9283ba92014-04-24 16:37:26 +09001897 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03001898 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03001899}
1900
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001901
1902static bool hists__filter_entry_by_dso(struct hists *hists,
1903 struct hist_entry *he)
1904{
1905 if (hists->dso_filter != NULL &&
1906 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1907 he->filtered |= (1 << HIST_FILTER__DSO);
1908 return true;
1909 }
1910
1911 return false;
1912}
1913
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001914static bool hists__filter_entry_by_thread(struct hists *hists,
1915 struct hist_entry *he)
1916{
1917 if (hists->thread_filter != NULL &&
1918 he->thread != hists->thread_filter) {
1919 he->filtered |= (1 << HIST_FILTER__THREAD);
1920 return true;
1921 }
1922
1923 return false;
1924}
1925
Namhyung Kime94d53e2012-03-16 17:50:51 +09001926static bool hists__filter_entry_by_symbol(struct hists *hists,
1927 struct hist_entry *he)
1928{
1929 if (hists->symbol_filter_str != NULL &&
1930 (!he->ms.sym || strstr(he->ms.sym->name,
1931 hists->symbol_filter_str) == NULL)) {
1932 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1933 return true;
1934 }
1935
1936 return false;
1937}
1938
Kan Liang21394d92015-09-04 10:45:44 -04001939static bool hists__filter_entry_by_socket(struct hists *hists,
1940 struct hist_entry *he)
1941{
1942 if ((hists->socket_filter > -1) &&
1943 (he->socket != hists->socket_filter)) {
1944 he->filtered |= (1 << HIST_FILTER__SOCKET);
1945 return true;
1946 }
1947
1948 return false;
1949}
1950
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001951typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1952
1953static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04001954{
1955 struct rb_node *nd;
1956
1957 hists->stats.nr_non_filtered_samples = 0;
1958
1959 hists__reset_filter_stats(hists);
1960 hists__reset_col_len(hists);
1961
1962 for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1963 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1964
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001965 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04001966 continue;
1967
Namhyung Kim1f7c2542016-01-20 10:15:21 +09001968 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04001969 }
1970}
1971
Namhyung Kim70642852016-02-25 00:13:39 +09001972static void resort_filtered_entry(struct rb_root *root, struct hist_entry *he)
1973{
1974 struct rb_node **p = &root->rb_node;
1975 struct rb_node *parent = NULL;
1976 struct hist_entry *iter;
1977 struct rb_root new_root = RB_ROOT;
1978 struct rb_node *nd;
1979
1980 while (*p != NULL) {
1981 parent = *p;
1982 iter = rb_entry(parent, struct hist_entry, rb_node);
1983
1984 if (hist_entry__sort(he, iter) > 0)
1985 p = &(*p)->rb_left;
1986 else
1987 p = &(*p)->rb_right;
1988 }
1989
1990 rb_link_node(&he->rb_node, parent, p);
1991 rb_insert_color(&he->rb_node, root);
1992
1993 if (he->leaf || he->filtered)
1994 return;
1995
1996 nd = rb_first(&he->hroot_out);
1997 while (nd) {
1998 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1999
2000 nd = rb_next(nd);
2001 rb_erase(&h->rb_node, &he->hroot_out);
2002
2003 resort_filtered_entry(&new_root, h);
2004 }
2005
2006 he->hroot_out = new_root;
2007}
2008
Namhyung Kim155e9af2016-02-25 00:13:38 +09002009static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2010{
2011 struct rb_node *nd;
Namhyung Kim70642852016-02-25 00:13:39 +09002012 struct rb_root new_root = RB_ROOT;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002013
2014 hists->stats.nr_non_filtered_samples = 0;
2015
2016 hists__reset_filter_stats(hists);
2017 hists__reset_col_len(hists);
2018
2019 nd = rb_first(&hists->entries);
2020 while (nd) {
2021 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2022 int ret;
2023
2024 ret = hist_entry__filter(h, type, arg);
2025
2026 /*
2027 * case 1. non-matching type
2028 * zero out the period, set filter marker and move to child
2029 */
2030 if (ret < 0) {
2031 memset(&h->stat, 0, sizeof(h->stat));
2032 h->filtered |= (1 << type);
2033
2034 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2035 }
2036 /*
2037 * case 2. matched type (filter out)
2038 * set filter marker and move to next
2039 */
2040 else if (ret == 1) {
2041 h->filtered |= (1 << type);
2042
2043 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2044 }
2045 /*
2046 * case 3. ok (not filtered)
2047 * add period to hists and parents, erase the filter marker
2048 * and move to next sibling
2049 */
2050 else {
2051 hists__remove_entry_filter(hists, h, type);
2052
2053 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2054 }
2055 }
Namhyung Kim70642852016-02-25 00:13:39 +09002056
Namhyung Kimf7fb5382016-03-09 22:47:02 +09002057 hierarchy_recalc_total_periods(hists);
2058
Namhyung Kim70642852016-02-25 00:13:39 +09002059 /*
2060 * resort output after applying a new filter since filter in a lower
2061 * hierarchy can change periods in a upper hierarchy.
2062 */
2063 nd = rb_first(&hists->entries);
2064 while (nd) {
2065 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2066
2067 nd = rb_next(nd);
2068 rb_erase(&h->rb_node, &hists->entries);
2069
2070 resort_filtered_entry(&new_root, h);
2071 }
2072
2073 hists->entries = new_root;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002074}
2075
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002076void hists__filter_by_thread(struct hists *hists)
2077{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002078 if (symbol_conf.report_hierarchy)
2079 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2080 hists->thread_filter);
2081 else
2082 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2083 hists__filter_entry_by_thread);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002084}
2085
2086void hists__filter_by_dso(struct hists *hists)
2087{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002088 if (symbol_conf.report_hierarchy)
2089 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2090 hists->dso_filter);
2091 else
2092 hists__filter_by_type(hists, HIST_FILTER__DSO,
2093 hists__filter_entry_by_dso);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002094}
2095
2096void hists__filter_by_symbol(struct hists *hists)
2097{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002098 if (symbol_conf.report_hierarchy)
2099 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2100 hists->symbol_filter_str);
2101 else
2102 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2103 hists__filter_entry_by_symbol);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002104}
2105
2106void hists__filter_by_socket(struct hists *hists)
2107{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002108 if (symbol_conf.report_hierarchy)
2109 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2110 &hists->socket_filter);
2111 else
2112 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2113 hists__filter_entry_by_socket);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002114}
2115
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002116void events_stats__inc(struct events_stats *stats, u32 type)
2117{
2118 ++stats->nr_events[0];
2119 ++stats->nr_events[type];
2120}
2121
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002122void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002123{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002124 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002125}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002126
Namhyung Kim1844dbc2014-05-28 14:12:18 +09002127void hists__inc_nr_samples(struct hists *hists, bool filtered)
2128{
2129 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2130 if (!filtered)
2131 hists->stats.nr_non_filtered_samples++;
2132}
2133
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002134static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2135 struct hist_entry *pair)
2136{
Namhyung Kimce74f602012-12-10 17:29:55 +09002137 struct rb_root *root;
2138 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002139 struct rb_node *parent = NULL;
2140 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07002141 int64_t cmp;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002142
Jiri Olsa52225032016-05-03 13:54:42 +02002143 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002144 root = &hists->entries_collapsed;
2145 else
2146 root = hists->entries_in;
2147
2148 p = &root->rb_node;
2149
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002150 while (*p != NULL) {
2151 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09002152 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002153
Namhyung Kimce74f602012-12-10 17:29:55 +09002154 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002155
2156 if (!cmp)
2157 goto out;
2158
2159 if (cmp < 0)
2160 p = &(*p)->rb_left;
2161 else
2162 p = &(*p)->rb_right;
2163 }
2164
Namhyung Kima0b51af2012-09-11 13:34:27 +09002165 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002166 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03002167 memset(&he->stat, 0, sizeof(he->stat));
2168 he->hists = hists;
Kan Liang09623d72016-04-24 23:28:09 -07002169 if (symbol_conf.cumulate_callchain)
2170 memset(he->stat_acc, 0, sizeof(he->stat));
Namhyung Kimce74f602012-12-10 17:29:55 +09002171 rb_link_node(&he->rb_node_in, parent, p);
2172 rb_insert_color(&he->rb_node_in, root);
Namhyung Kim62638352014-04-24 16:21:46 +09002173 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01002174 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002175 }
2176out:
2177 return he;
2178}
2179
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002180static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
2181 struct rb_root *root,
2182 struct hist_entry *pair)
2183{
2184 struct rb_node **p;
2185 struct rb_node *parent = NULL;
2186 struct hist_entry *he;
2187 struct perf_hpp_fmt *fmt;
2188
2189 p = &root->rb_node;
2190 while (*p != NULL) {
2191 int64_t cmp = 0;
2192
2193 parent = *p;
2194 he = rb_entry(parent, struct hist_entry, rb_node_in);
2195
2196 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2197 cmp = fmt->collapse(fmt, he, pair);
2198 if (cmp)
2199 break;
2200 }
2201 if (!cmp)
2202 goto out;
2203
2204 if (cmp < 0)
2205 p = &parent->rb_left;
2206 else
2207 p = &parent->rb_right;
2208 }
2209
2210 he = hist_entry__new(pair, true);
2211 if (he) {
2212 rb_link_node(&he->rb_node_in, parent, p);
2213 rb_insert_color(&he->rb_node_in, root);
2214
2215 he->dummy = true;
2216 he->hists = hists;
2217 memset(&he->stat, 0, sizeof(he->stat));
2218 hists__inc_stats(hists, he);
2219 }
2220out:
2221 return he;
2222}
2223
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002224static struct hist_entry *hists__find_entry(struct hists *hists,
2225 struct hist_entry *he)
2226{
Namhyung Kimce74f602012-12-10 17:29:55 +09002227 struct rb_node *n;
2228
Jiri Olsa52225032016-05-03 13:54:42 +02002229 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002230 n = hists->entries_collapsed.rb_node;
2231 else
2232 n = hists->entries_in->rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002233
2234 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002235 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2236 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002237
2238 if (cmp < 0)
2239 n = n->rb_left;
2240 else if (cmp > 0)
2241 n = n->rb_right;
2242 else
2243 return iter;
2244 }
2245
2246 return NULL;
2247}
2248
Namhyung Kim09034de2016-09-13 16:45:46 +09002249static struct hist_entry *hists__find_hierarchy_entry(struct rb_root *root,
2250 struct hist_entry *he)
2251{
2252 struct rb_node *n = root->rb_node;
2253
2254 while (n) {
2255 struct hist_entry *iter;
2256 struct perf_hpp_fmt *fmt;
2257 int64_t cmp = 0;
2258
2259 iter = rb_entry(n, struct hist_entry, rb_node_in);
2260 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2261 cmp = fmt->collapse(fmt, iter, he);
2262 if (cmp)
2263 break;
2264 }
2265
2266 if (cmp < 0)
2267 n = n->rb_left;
2268 else if (cmp > 0)
2269 n = n->rb_right;
2270 else
2271 return iter;
2272 }
2273
2274 return NULL;
2275}
2276
2277static void hists__match_hierarchy(struct rb_root *leader_root,
2278 struct rb_root *other_root)
2279{
2280 struct rb_node *nd;
2281 struct hist_entry *pos, *pair;
2282
2283 for (nd = rb_first(leader_root); nd; nd = rb_next(nd)) {
2284 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2285 pair = hists__find_hierarchy_entry(other_root, pos);
2286
2287 if (pair) {
2288 hist_entry__add_pair(pair, pos);
2289 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2290 }
2291 }
2292}
2293
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002294/*
2295 * Look for pairs to link to the leader buckets (hist_entries):
2296 */
2297void hists__match(struct hists *leader, struct hists *other)
2298{
Namhyung Kimce74f602012-12-10 17:29:55 +09002299 struct rb_root *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002300 struct rb_node *nd;
2301 struct hist_entry *pos, *pair;
2302
Namhyung Kim09034de2016-09-13 16:45:46 +09002303 if (symbol_conf.report_hierarchy) {
2304 /* hierarchy report always collapses entries */
2305 return hists__match_hierarchy(&leader->entries_collapsed,
2306 &other->entries_collapsed);
2307 }
2308
Jiri Olsa52225032016-05-03 13:54:42 +02002309 if (hists__has(leader, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002310 root = &leader->entries_collapsed;
2311 else
2312 root = leader->entries_in;
2313
2314 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2315 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002316 pair = hists__find_entry(other, pos);
2317
2318 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09002319 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002320 }
2321}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002322
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002323static int hists__link_hierarchy(struct hists *leader_hists,
2324 struct hist_entry *parent,
2325 struct rb_root *leader_root,
2326 struct rb_root *other_root)
2327{
2328 struct rb_node *nd;
2329 struct hist_entry *pos, *leader;
2330
2331 for (nd = rb_first(other_root); nd; nd = rb_next(nd)) {
2332 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2333
2334 if (hist_entry__has_pairs(pos)) {
2335 bool found = false;
2336
2337 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2338 if (leader->hists == leader_hists) {
2339 found = true;
2340 break;
2341 }
2342 }
2343 if (!found)
2344 return -1;
2345 } else {
2346 leader = add_dummy_hierarchy_entry(leader_hists,
2347 leader_root, pos);
2348 if (leader == NULL)
2349 return -1;
2350
2351 /* do not point parent in the pos */
2352 leader->parent_he = parent;
2353
2354 hist_entry__add_pair(pos, leader);
2355 }
2356
2357 if (!pos->leaf) {
2358 if (hists__link_hierarchy(leader_hists, leader,
2359 &leader->hroot_in,
2360 &pos->hroot_in) < 0)
2361 return -1;
2362 }
2363 }
2364 return 0;
2365}
2366
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002367/*
2368 * Look for entries in the other hists that are not present in the leader, if
2369 * we find them, just add a dummy entry on the leader hists, with period=0,
2370 * nr_events=0, to serve as the list header.
2371 */
2372int hists__link(struct hists *leader, struct hists *other)
2373{
Namhyung Kimce74f602012-12-10 17:29:55 +09002374 struct rb_root *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002375 struct rb_node *nd;
2376 struct hist_entry *pos, *pair;
2377
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002378 if (symbol_conf.report_hierarchy) {
2379 /* hierarchy report always collapses entries */
2380 return hists__link_hierarchy(leader, NULL,
2381 &leader->entries_collapsed,
2382 &other->entries_collapsed);
2383 }
2384
Jiri Olsa52225032016-05-03 13:54:42 +02002385 if (hists__has(other, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002386 root = &other->entries_collapsed;
2387 else
2388 root = other->entries_in;
2389
2390 for (nd = rb_first(root); nd; nd = rb_next(nd)) {
2391 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002392
2393 if (!hist_entry__has_pairs(pos)) {
2394 pair = hists__add_dummy_entry(leader, pos);
2395 if (pair == NULL)
2396 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09002397 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002398 }
2399 }
2400
2401 return 0;
2402}
Namhyung Kimf2148332014-01-14 11:52:48 +09002403
Andi Kleen578499982015-07-18 08:24:49 -07002404void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2405 struct perf_sample *sample, bool nonany_branch_mode)
2406{
2407 struct branch_info *bi;
2408
2409 /* If we have branch cycles always annotate them. */
2410 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2411 int i;
2412
2413 bi = sample__resolve_bstack(sample, al);
2414 if (bi) {
2415 struct addr_map_symbol *prev = NULL;
2416
2417 /*
2418 * Ignore errors, still want to process the
2419 * other entries.
2420 *
2421 * For non standard branch modes always
2422 * force no IPC (prev == NULL)
2423 *
2424 * Note that perf stores branches reversed from
2425 * program order!
2426 */
2427 for (i = bs->nr - 1; i >= 0; i--) {
2428 addr_map_symbol__account_cycles(&bi[i].from,
2429 nonany_branch_mode ? NULL : prev,
2430 bi[i].flags.cycles);
2431 prev = &bi[i].to;
2432 }
2433 free(bi);
2434 }
2435 }
2436}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002437
2438size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
2439{
2440 struct perf_evsel *pos;
2441 size_t ret = 0;
2442
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002443 evlist__for_each_entry(evlist, pos) {
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002444 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2445 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2446 }
2447
2448 return ret;
2449}
2450
2451
Namhyung Kimf2148332014-01-14 11:52:48 +09002452u64 hists__total_period(struct hists *hists)
2453{
2454 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2455 hists->stats.total_period;
2456}
Namhyung Kim33db4562014-02-07 12:06:07 +09002457
2458int parse_filter_percentage(const struct option *opt __maybe_unused,
2459 const char *arg, int unset __maybe_unused)
2460{
2461 if (!strcmp(arg, "relative"))
2462 symbol_conf.filter_relative = true;
2463 else if (!strcmp(arg, "absolute"))
2464 symbol_conf.filter_relative = false;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002465 else {
Colin Ian Kinga596a872017-03-30 10:54:40 +01002466 pr_debug("Invalid percentage: %s\n", arg);
Namhyung Kim33db4562014-02-07 12:06:07 +09002467 return -1;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002468 }
Namhyung Kim33db4562014-02-07 12:06:07 +09002469
2470 return 0;
2471}
Namhyung Kim0b93da12014-01-14 12:02:15 +09002472
2473int perf_hist_config(const char *var, const char *value)
2474{
2475 if (!strcmp(var, "hist.percentage"))
2476 return parse_filter_percentage(NULL, value, 0);
2477
2478 return 0;
2479}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002480
Jiri Olsa5b658552016-01-18 10:24:22 +01002481int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002482{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002483 memset(hists, 0, sizeof(*hists));
2484 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
2485 hists->entries_in = &hists->entries_in_array[0];
2486 hists->entries_collapsed = RB_ROOT;
2487 hists->entries = RB_ROOT;
2488 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04002489 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01002490 hists->hpp_list = hpp_list;
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002491 INIT_LIST_HEAD(&hists->hpp_formats);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002492 return 0;
2493}
2494
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002495static void hists__delete_remaining_entries(struct rb_root *root)
2496{
2497 struct rb_node *node;
2498 struct hist_entry *he;
2499
2500 while (!RB_EMPTY_ROOT(root)) {
2501 node = rb_first(root);
2502 rb_erase(node, root);
2503
2504 he = rb_entry(node, struct hist_entry, rb_node_in);
2505 hist_entry__delete(he);
2506 }
2507}
2508
2509static void hists__delete_all_entries(struct hists *hists)
2510{
2511 hists__delete_entries(hists);
2512 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2513 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2514 hists__delete_remaining_entries(&hists->entries_collapsed);
2515}
2516
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002517static void hists_evsel__exit(struct perf_evsel *evsel)
2518{
2519 struct hists *hists = evsel__hists(evsel);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002520 struct perf_hpp_fmt *fmt, *pos;
2521 struct perf_hpp_list_node *node, *tmp;
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002522
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002523 hists__delete_all_entries(hists);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002524
2525 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2526 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2527 list_del(&fmt->list);
2528 free(fmt);
2529 }
2530 list_del(&node->list);
2531 free(node);
2532 }
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002533}
2534
Namhyung Kimfc284be2016-01-07 10:14:10 +01002535static int hists_evsel__init(struct perf_evsel *evsel)
2536{
2537 struct hists *hists = evsel__hists(evsel);
2538
Jiri Olsa5b658552016-01-18 10:24:22 +01002539 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01002540 return 0;
2541}
2542
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002543/*
2544 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2545 * stored in the rbtree...
2546 */
2547
2548int hists__init(void)
2549{
2550 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002551 hists_evsel__init,
2552 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002553 if (err)
2554 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2555
2556 return err;
2557}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01002558
2559void perf_hpp_list__init(struct perf_hpp_list *list)
2560{
2561 INIT_LIST_HEAD(&list->fields);
2562 INIT_LIST_HEAD(&list->sorts);
2563}