blob: fa77574f12cacd8885c844d8b557f3c5dd36d17d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Melob10ba7f2019-01-29 11:11:04 +01002#include "callchain.h"
Arnaldo Carvalho de Melob4209022019-08-29 15:56:40 -03003#include "debug.h"
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -03004#include "dso.h"
Frederic Weisbecker598357e2010-05-21 12:48:39 +02005#include "build-id.h"
John Kacur3d1d07e2009-09-28 15:32:55 +02006#include "hist.h"
Krister Johansen9c68ae92017-01-05 22:23:31 -08007#include "map.h"
Arnaldo Carvalho de Melod3300a32019-08-30 15:09:54 -03008#include "map_symbol.h"
9#include "branch.h"
10#include "mem-events.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -020011#include "session.h"
Hari Bathinid890a982017-03-08 02:12:13 +053012#include "namespaces.h"
Namhyung Kimb629f3e2020-03-25 21:45:32 +090013#include "cgroup.h"
Arnaldo Carvalho de Melo4e4f06e2009-12-14 13:10:39 -020014#include "sort.h"
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -030015#include "units.h"
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -030016#include "evlist.h"
Namhyung Kim29d720e2013-01-22 18:09:33 +090017#include "evsel.h"
Namhyung Kim69bcb012013-10-30 09:40:34 +090018#include "annotate.h"
Arnaldo Carvalho de Melo632a5ca2017-04-17 16:30:49 -030019#include "srcline.h"
Arnaldo Carvalho de Melodaecf9e2019-01-28 00:03:34 +010020#include "symbol.h"
Arnaldo Carvalho de Meloe7ff8922017-04-19 21:34:35 -030021#include "thread.h"
Jin Yao60414412019-11-07 15:47:14 +080022#include "block-info.h"
Namhyung Kim740b97f2014-12-22 13:44:10 +090023#include "ui/progress.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030024#include <errno.h>
Arnaldo Carvalho de Melo9b338272009-12-16 14:31:49 -020025#include <math.h>
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -030026#include <inttypes.h>
Arnaldo Carvalho de Melo391e4202017-04-19 18:51:14 -030027#include <sys/param.h>
Arnaldo Carvalho de Melo5c9dbe62019-08-30 11:54:00 -030028#include <linux/rbtree.h>
Arnaldo Carvalho de Melo8520a982019-08-29 16:18:59 -030029#include <linux/string.h>
Andi Kleen37239082019-03-11 07:44:54 -070030#include <linux/time64.h>
Arnaldo Carvalho de Melo7f7c5362019-07-04 11:32:27 -030031#include <linux/zalloc.h>
John Kacur3d1d07e2009-09-28 15:32:55 +020032
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020033static bool hists__filter_entry_by_dso(struct hists *hists,
34 struct hist_entry *he);
35static bool hists__filter_entry_by_thread(struct hists *hists,
36 struct hist_entry *he);
Namhyung Kime94d53e2012-03-16 17:50:51 +090037static bool hists__filter_entry_by_symbol(struct hists *hists,
38 struct hist_entry *he);
Kan Liang21394d92015-09-04 10:45:44 -040039static bool hists__filter_entry_by_socket(struct hists *hists,
40 struct hist_entry *he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -020041
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030042u16 hists__col_len(struct hists *hists, enum hist_column col)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030043{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030044 return hists->col_len[col];
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030045}
46
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030047void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030048{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030049 hists->col_len[col] = len;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030050}
51
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030052bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030053{
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030054 if (len > hists__col_len(hists, col)) {
55 hists__set_col_len(hists, col, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030056 return true;
57 }
58 return false;
59}
60
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090061void hists__reset_col_len(struct hists *hists)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030062{
63 enum hist_column col;
64
65 for (col = 0; col < HISTC_NR_COLS; ++col)
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -030066 hists__set_col_len(hists, col, 0);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030067}
68
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010069static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
70{
71 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
72
73 if (hists__col_len(hists, dso) < unresolved_col_width &&
74 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
75 !symbol_conf.dso_list)
76 hists__set_col_len(hists, dso, unresolved_col_width);
77}
78
Namhyung Kim7ccf4f92012-08-20 13:52:05 +090079void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030080{
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +010081 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
Stephane Eranian98a3b322013-01-24 16:10:35 +010082 int symlen;
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -030083 u16 len;
84
Jin Yao0bdf1812019-11-07 15:47:13 +080085 if (h->block_info)
86 return;
Namhyung Kimded19d52013-04-01 20:35:19 +090087 /*
88 * +4 accounts for '[x] ' priv level info
89 * +2 accounts for 0x prefix on raw addresses
90 * +3 accounts for ' y ' symtab origin info
91 */
92 if (h->ms.sym) {
93 symlen = h->ms.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +090094 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +090095 symlen += BITS_PER_LONG / 4 + 2 + 3;
96 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
97 } else {
Stephane Eranian98a3b322013-01-24 16:10:35 +010098 symlen = unresolved_col_width + 4 + 2;
99 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100100 hists__set_unres_dso_col_len(hists, HISTC_DSO);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100101 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300102
103 len = thread__comm_len(h->thread);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300104 if (hists__new_col_len(hists, HISTC_COMM, len))
Jiri Olsa89c7cb22016-06-20 23:58:19 +0200105 hists__set_col_len(hists, HISTC_THREAD, len + 8);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300106
107 if (h->ms.map) {
108 len = dso__name_len(h->ms.map->dso);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -0300109 hists__new_col_len(hists, HISTC_DSO, len);
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300110 }
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100111
Namhyung Kimcb993742012-12-27 18:11:42 +0900112 if (h->parent)
113 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
114
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100115 if (h->branch_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300116 if (h->branch_info->from.ms.sym) {
117 symlen = (int)h->branch_info->from.ms.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +0900118 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +0900119 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100120 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
121
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300122 symlen = dso__name_len(h->branch_info->from.ms.map->dso);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100123 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
124 } else {
125 symlen = unresolved_col_width + 4 + 2;
126 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
127 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
128 }
129
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300130 if (h->branch_info->to.ms.sym) {
131 symlen = (int)h->branch_info->to.ms.sym->namelen + 4;
Namhyung Kimbb963e12017-02-17 17:17:38 +0900132 if (verbose > 0)
Namhyung Kimded19d52013-04-01 20:35:19 +0900133 symlen += BITS_PER_LONG / 4 + 2 + 3;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100134 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
135
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300136 symlen = dso__name_len(h->branch_info->to.ms.map->dso);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100137 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
138 } else {
139 symlen = unresolved_col_width + 4 + 2;
140 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
141 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
142 }
Andi Kleen508be0d2016-05-20 13:15:08 -0700143
144 if (h->branch_info->srcline_from)
145 hists__new_col_len(hists, HISTC_SRCLINE_FROM,
146 strlen(h->branch_info->srcline_from));
147 if (h->branch_info->srcline_to)
148 hists__new_col_len(hists, HISTC_SRCLINE_TO,
149 strlen(h->branch_info->srcline_to));
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100150 }
Stephane Eranian98a3b322013-01-24 16:10:35 +0100151
152 if (h->mem_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300153 if (h->mem_info->daddr.ms.sym) {
154 symlen = (int)h->mem_info->daddr.ms.sym->namelen + 4
Stephane Eranian98a3b322013-01-24 16:10:35 +0100155 + unresolved_col_width + 2;
156 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
157 symlen);
Don Zickus9b32ba72014-06-01 15:38:29 +0200158 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
159 symlen + 1);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100160 } else {
161 symlen = unresolved_col_width + 4 + 2;
162 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
163 symlen);
Jiri Olsa08059092016-01-20 12:56:33 +0100164 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
165 symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100166 }
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200167
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300168 if (h->mem_info->iaddr.ms.sym) {
169 symlen = (int)h->mem_info->iaddr.ms.sym->namelen + 4
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200170 + unresolved_col_width + 2;
171 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
172 symlen);
173 } else {
174 symlen = unresolved_col_width + 4 + 2;
175 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
176 symlen);
177 }
178
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300179 if (h->mem_info->daddr.ms.map) {
180 symlen = dso__name_len(h->mem_info->daddr.ms.map->dso);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100181 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
182 symlen);
183 } else {
184 symlen = unresolved_col_width + 4 + 2;
185 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
186 }
Kan Liang8780fb22017-08-29 13:11:09 -0400187
188 hists__new_col_len(hists, HISTC_MEM_PHYS_DADDR,
189 unresolved_col_width + 4 + 2);
190
Stephane Eranian98a3b322013-01-24 16:10:35 +0100191 } else {
192 symlen = unresolved_col_width + 4 + 2;
193 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200194 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100195 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
196 }
197
Namhyung Kimb629f3e2020-03-25 21:45:32 +0900198 hists__new_col_len(hists, HISTC_CGROUP, 6);
Hari Bathinid890a982017-03-08 02:12:13 +0530199 hists__new_col_len(hists, HISTC_CGROUP_ID, 20);
Arnaldo Carvalho de Meloa4978ec2015-09-09 12:14:00 -0300200 hists__new_col_len(hists, HISTC_CPU, 3);
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400201 hists__new_col_len(hists, HISTC_SOCKET, 6);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100202 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
203 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
204 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
205 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
206 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
207 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Andi Kleen3dab6ac2019-08-23 14:03:38 -0700208 if (symbol_conf.nanosecs)
209 hists__new_col_len(hists, HISTC_TIME, 16);
210 else
211 hists__new_col_len(hists, HISTC_TIME, 12);
Andi Kleen475eeab2013-09-20 07:40:43 -0700212
Jiri Olsaf666ac02016-09-19 15:10:10 +0200213 if (h->srcline) {
214 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
215 hists__new_col_len(hists, HISTC_SRCLINE, len);
216 }
Arnaldo Carvalho de Meloe8e6d372015-08-10 16:53:54 -0300217
Andi Kleen31191a82015-08-07 15:54:24 -0700218 if (h->srcfile)
219 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
220
Andi Kleen475eeab2013-09-20 07:40:43 -0700221 if (h->transaction)
222 hists__new_col_len(hists, HISTC_TRANSACTION,
223 hist_entry__transaction_len());
Namhyung Kim0c0af782016-02-21 23:22:38 +0900224
225 if (h->trace_output)
226 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
Namhyung Kimb629f3e2020-03-25 21:45:32 +0900227
228 if (h->cgroup) {
229 const char *cgrp_name = "unknown";
230 struct cgroup *cgrp = cgroup__find(h->ms.maps->machine->env,
231 h->cgroup);
232 if (cgrp != NULL)
233 cgrp_name = cgrp->name;
234
235 hists__new_col_len(hists, HISTC_CGROUP, strlen(cgrp_name));
236 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300237}
238
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900239void hists__output_recalc_col_len(struct hists *hists, int max_rows)
240{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800241 struct rb_node *next = rb_first_cached(&hists->entries);
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900242 struct hist_entry *n;
243 int row = 0;
244
245 hists__reset_col_len(hists);
246
247 while (next && row++ < max_rows) {
248 n = rb_entry(next, struct hist_entry, rb_node);
249 if (!n->filtered)
250 hists__calc_col_len(hists, n);
251 next = rb_next(&n->rb_node);
252 }
253}
254
Namhyung Kimf39056f2014-01-14 14:25:37 +0900255static void he_stat__add_cpumode_period(struct he_stat *he_stat,
256 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800257{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300258 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800259 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900260 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800261 break;
262 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900263 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800264 break;
265 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900266 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800267 break;
268 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900269 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800270 break;
271 default:
272 break;
273 }
274}
275
Andi Kleen37239082019-03-11 07:44:54 -0700276static long hist_time(unsigned long htime)
277{
278 unsigned long time_quantum = symbol_conf.time_quantum;
279 if (time_quantum)
280 return (htime / time_quantum) * time_quantum;
281 return htime;
282}
283
Andi Kleen05484292013-01-24 16:10:29 +0100284static void he_stat__add_period(struct he_stat *he_stat, u64 period,
285 u64 weight)
Namhyung Kim139c0812012-10-04 21:49:43 +0900286{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100287
Namhyung Kim139c0812012-10-04 21:49:43 +0900288 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100289 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900290 he_stat->nr_events += 1;
291}
292
293static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
294{
295 dest->period += src->period;
296 dest->period_sys += src->period_sys;
297 dest->period_us += src->period_us;
298 dest->period_guest_sys += src->period_guest_sys;
299 dest->period_guest_us += src->period_guest_us;
300 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100301 dest->weight += src->weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900302}
303
Namhyung Kimf39056f2014-01-14 14:25:37 +0900304static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300305{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900306 he_stat->period = (he_stat->period * 7) / 8;
307 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100308 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300309}
310
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900311static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
312
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300313static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
314{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900315 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900316 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200317
318 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300319 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200320
Namhyung Kimf39056f2014-01-14 14:25:37 +0900321 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900322 if (symbol_conf.cumulate_callchain)
323 he_stat__decay(he->stat_acc);
Namhyung Kim42b276a2016-01-05 12:06:00 +0900324 decay_callchain(he->callchain);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200325
Namhyung Kim3186b682014-04-22 13:44:23 +0900326 diff = prev_period - he->stat.period;
327
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900328 if (!he->depth) {
329 hists->stats.total_period -= diff;
330 if (!he->filtered)
331 hists->stats.total_non_filtered_period -= diff;
332 }
333
334 if (!he->leaf) {
335 struct hist_entry *child;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800336 struct rb_node *node = rb_first_cached(&he->hroot_out);
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900337 while (node) {
338 child = rb_entry(node, struct hist_entry, rb_node);
339 node = rb_next(node);
340
341 if (hists__decay_entry(hists, child))
342 hists__delete_entry(hists, child);
343 }
344 }
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200345
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900346 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300347}
348
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300349static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
350{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800351 struct rb_root_cached *root_in;
352 struct rb_root_cached *root_out;
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300353
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900354 if (he->parent_he) {
355 root_in = &he->parent_he->hroot_in;
356 root_out = &he->parent_he->hroot_out;
357 } else {
Jiri Olsa52225032016-05-03 13:54:42 +0200358 if (hists__has(hists, need_collapse))
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900359 root_in = &hists->entries_collapsed;
360 else
361 root_in = hists->entries_in;
362 root_out = &hists->entries;
363 }
364
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800365 rb_erase_cached(&he->rb_node_in, root_in);
366 rb_erase_cached(&he->rb_node, root_out);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300367
368 --hists->nr_entries;
369 if (!he->filtered)
370 --hists->nr_non_filtered_entries;
371
372 hist_entry__delete(he);
373}
374
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900375void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300376{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800377 struct rb_node *next = rb_first_cached(&hists->entries);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300378 struct hist_entry *n;
379
380 while (next) {
381 n = rb_entry(next, struct hist_entry, rb_node);
382 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200383 if (((zap_user && n->level == '.') ||
384 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300385 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300386 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300387 }
388 }
389}
390
Namhyung Kim701937b2014-08-12 17:16:05 +0900391void hists__delete_entries(struct hists *hists)
392{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800393 struct rb_node *next = rb_first_cached(&hists->entries);
Namhyung Kim701937b2014-08-12 17:16:05 +0900394 struct hist_entry *n;
395
396 while (next) {
397 n = rb_entry(next, struct hist_entry, rb_node);
398 next = rb_next(&n->rb_node);
399
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300400 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900401 }
402}
403
Jin Yaob10c78c2019-06-28 17:23:03 +0800404struct hist_entry *hists__get_entry(struct hists *hists, int idx)
405{
406 struct rb_node *next = rb_first_cached(&hists->entries);
407 struct hist_entry *n;
408 int i = 0;
409
410 while (next) {
411 n = rb_entry(next, struct hist_entry, rb_node);
412 if (i == idx)
413 return n;
414
415 next = rb_next(&n->rb_node);
416 i++;
417 }
418
419 return NULL;
420}
421
John Kacur3d1d07e2009-09-28 15:32:55 +0200422/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300423 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200424 */
425
Jiri Olsa0a269a62016-07-05 08:56:03 +0200426static int hist_entry__init(struct hist_entry *he,
427 struct hist_entry *template,
Arnaldo Carvalho de Melo41477ac2018-06-07 14:19:54 -0300428 bool sample_self,
429 size_t callchain_size)
Jiri Olsa0a269a62016-07-05 08:56:03 +0200430{
431 *he = *template;
Arnaldo Carvalho de Melo41477ac2018-06-07 14:19:54 -0300432 he->callchain_size = callchain_size;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200433
434 if (symbol_conf.cumulate_callchain) {
435 he->stat_acc = malloc(sizeof(he->stat));
436 if (he->stat_acc == NULL)
437 return -ENOMEM;
438 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
439 if (!sample_self)
440 memset(&he->stat, 0, sizeof(he->stat));
441 }
442
443 map__get(he->ms.map);
444
445 if (he->branch_info) {
446 /*
447 * This branch info is (a part of) allocated from
448 * sample__resolve_bstack() and will be freed after
449 * adding new entries. So we need to save a copy.
450 */
451 he->branch_info = malloc(sizeof(*he->branch_info));
Jiri Olsac5758912019-03-05 16:25:30 +0100452 if (he->branch_info == NULL)
453 goto err;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200454
455 memcpy(he->branch_info, template->branch_info,
456 sizeof(*he->branch_info));
457
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300458 map__get(he->branch_info->from.ms.map);
459 map__get(he->branch_info->to.ms.map);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200460 }
461
462 if (he->mem_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300463 map__get(he->mem_info->iaddr.ms.map);
464 map__get(he->mem_info->daddr.ms.map);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200465 }
466
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -0300467 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
Jiri Olsa0a269a62016-07-05 08:56:03 +0200468 callchain_init(he->callchain);
469
470 if (he->raw_data) {
471 he->raw_data = memdup(he->raw_data, he->raw_size);
Jiri Olsac5758912019-03-05 16:25:30 +0100472 if (he->raw_data == NULL)
473 goto err_infos;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200474 }
Jiri Olsa26349582019-03-05 16:25:31 +0100475
476 if (he->srcline) {
477 he->srcline = strdup(he->srcline);
478 if (he->srcline == NULL)
479 goto err_rawdata;
480 }
481
Andi Kleen4968ac82019-03-11 07:44:58 -0700482 if (symbol_conf.res_sample) {
483 he->res_samples = calloc(sizeof(struct res_sample),
484 symbol_conf.res_sample);
485 if (!he->res_samples)
486 goto err_srcline;
487 }
488
Jiri Olsa0a269a62016-07-05 08:56:03 +0200489 INIT_LIST_HEAD(&he->pairs.node);
490 thread__get(he->thread);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800491 he->hroot_in = RB_ROOT_CACHED;
492 he->hroot_out = RB_ROOT_CACHED;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200493
494 if (!symbol_conf.report_hierarchy)
495 he->leaf = true;
496
497 return 0;
Jiri Olsac5758912019-03-05 16:25:30 +0100498
Andi Kleen4968ac82019-03-11 07:44:58 -0700499err_srcline:
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300500 zfree(&he->srcline);
Andi Kleen4968ac82019-03-11 07:44:58 -0700501
Jiri Olsa26349582019-03-05 16:25:31 +0100502err_rawdata:
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300503 zfree(&he->raw_data);
Jiri Olsa26349582019-03-05 16:25:31 +0100504
Jiri Olsac5758912019-03-05 16:25:30 +0100505err_infos:
506 if (he->branch_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300507 map__put(he->branch_info->from.ms.map);
508 map__put(he->branch_info->to.ms.map);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300509 zfree(&he->branch_info);
Jiri Olsac5758912019-03-05 16:25:30 +0100510 }
511 if (he->mem_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300512 map__put(he->mem_info->iaddr.ms.map);
513 map__put(he->mem_info->daddr.ms.map);
Jiri Olsac5758912019-03-05 16:25:30 +0100514 }
515err:
516 map__zput(he->ms.map);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300517 zfree(&he->stat_acc);
Jiri Olsac5758912019-03-05 16:25:30 +0100518 return -ENOMEM;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200519}
520
Jiri Olsaf542e762016-07-05 08:56:04 +0200521static void *hist_entry__zalloc(size_t size)
522{
523 return zalloc(size + sizeof(struct hist_entry));
524}
525
526static void hist_entry__free(void *ptr)
527{
528 free(ptr);
529}
530
531static struct hist_entry_ops default_ops = {
532 .new = hist_entry__zalloc,
533 .free = hist_entry__free,
534};
535
Namhyung Kima0b51af2012-09-11 13:34:27 +0900536static struct hist_entry *hist_entry__new(struct hist_entry *template,
537 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300538{
Jiri Olsaf542e762016-07-05 08:56:04 +0200539 struct hist_entry_ops *ops = template->ops;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900540 size_t callchain_size = 0;
541 struct hist_entry *he;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200542 int err = 0;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900543
Jiri Olsaf542e762016-07-05 08:56:04 +0200544 if (!ops)
545 ops = template->ops = &default_ops;
546
Namhyung Kim82aa0192014-12-22 13:44:14 +0900547 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900548 callchain_size = sizeof(struct callchain_root);
549
Jiri Olsaf542e762016-07-05 08:56:04 +0200550 he = ops->new(callchain_size);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200551 if (he) {
Arnaldo Carvalho de Melo41477ac2018-06-07 14:19:54 -0300552 err = hist_entry__init(he, template, sample_self, callchain_size);
Jiri Olsaf542e762016-07-05 08:56:04 +0200553 if (err) {
554 ops->free(he);
555 he = NULL;
556 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300557 }
558
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200559 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300560}
561
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300562static u8 symbol__parent_filter(const struct symbol *parent)
563{
564 if (symbol_conf.exclude_other && parent == NULL)
565 return 1 << HIST_FILTER__PARENT;
566 return 0;
567}
568
Namhyung Kim467ef102016-02-16 23:08:19 +0900569static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
570{
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -0300571 if (!hist_entry__has_callchains(he) || !symbol_conf.use_callchain)
Namhyung Kim467ef102016-02-16 23:08:19 +0900572 return;
573
574 he->hists->callchain_period += period;
575 if (!he->filtered)
576 he->hists->callchain_non_filtered_period += period;
577}
578
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300579static struct hist_entry *hists__findnew_entry(struct hists *hists,
580 struct hist_entry *entry,
581 struct addr_location *al,
582 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300583{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300584 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300585 struct rb_node *parent = NULL;
586 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700587 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900588 u64 period = entry->stat.period;
589 u64 weight = entry->stat.weight;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800590 bool leftmost = true;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300591
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800592 p = &hists->entries_in->rb_root.rb_node;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300593
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300594 while (*p != NULL) {
595 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300596 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300597
Namhyung Kim9afcf932012-12-10 17:29:54 +0900598 /*
599 * Make sure that it receives arguments in a same order as
600 * hist_entry__collapse() so that we can use an appropriate
601 * function when searching an entry regardless which sort
602 * keys were used.
603 */
604 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300605
606 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900607 if (sample_self) {
Namhyung Kima0b51af2012-09-11 13:34:27 +0900608 he_stat__add_period(&he->stat, period, weight);
Namhyung Kim467ef102016-02-16 23:08:19 +0900609 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900610 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900611 if (symbol_conf.cumulate_callchain)
612 he_stat__add_period(he->stat_acc, period, weight);
David Miller63fa4712012-03-27 03:14:18 -0400613
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900614 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300615 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900616 * and will not be used anymore.
617 */
Jiri Olsa9f874982018-03-07 16:50:06 +0100618 mem_info__zput(entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900619
Jin Yaofe962452019-06-28 17:22:59 +0800620 block_info__zput(entry->block_info);
621
David Miller63fa4712012-03-27 03:14:18 -0400622 /* If the map of an existing hist_entry has
623 * become out-of-date due to an exec() or
624 * similar, update it. Otherwise we will
625 * mis-adjust symbol addresses when computing
626 * the history counter to increment.
627 */
628 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300629 map__put(he->ms.map);
630 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400631 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300632 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300633 }
634
635 if (cmp < 0)
636 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800637 else {
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300638 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800639 leftmost = false;
640 }
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300641 }
642
Namhyung Kima0b51af2012-09-11 13:34:27 +0900643 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300644 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900645 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300646
Namhyung Kim0f584742016-01-28 00:40:49 +0900647 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900648 hist_entry__add_callchain_period(he, period);
649 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900650
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300651 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800652 rb_insert_color_cached(&he->rb_node_in, hists->entries_in, leftmost);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300653out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900654 if (sample_self)
655 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900656 if (symbol_conf.cumulate_callchain)
657 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300658 return he;
659}
660
Andi Kleen4968ac82019-03-11 07:44:58 -0700661static unsigned random_max(unsigned high)
662{
663 unsigned thresh = -high % high;
664 for (;;) {
665 unsigned r = random();
666 if (r >= thresh)
667 return r % high;
668 }
669}
670
671static void hists__res_sample(struct hist_entry *he, struct perf_sample *sample)
672{
673 struct res_sample *r;
674 int j;
675
676 if (he->num_res < symbol_conf.res_sample) {
677 j = he->num_res++;
678 } else {
679 j = random_max(symbol_conf.res_sample);
680 }
681 r = &he->res_samples[j];
682 r->time = sample->time;
683 r->cpu = sample->cpu;
684 r->tid = sample->tid;
685}
686
Jiri Olsaa5051972016-07-05 08:56:05 +0200687static struct hist_entry*
688__hists__add_entry(struct hists *hists,
689 struct addr_location *al,
690 struct symbol *sym_parent,
691 struct branch_info *bi,
692 struct mem_info *mi,
Jin Yaofe962452019-06-28 17:22:59 +0800693 struct block_info *block_info,
Jiri Olsaa5051972016-07-05 08:56:05 +0200694 struct perf_sample *sample,
695 bool sample_self,
696 struct hist_entry_ops *ops)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100697{
Hari Bathinid890a982017-03-08 02:12:13 +0530698 struct namespaces *ns = thread__namespaces(al->thread);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100699 struct hist_entry entry = {
700 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900701 .comm = thread__comm(al->thread),
Hari Bathinid890a982017-03-08 02:12:13 +0530702 .cgroup_id = {
703 .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0,
704 .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0,
705 },
Namhyung Kimb629f3e2020-03-25 21:45:32 +0900706 .cgroup = sample->cgroup,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100707 .ms = {
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -0300708 .maps = al->maps,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100709 .map = al->map,
710 .sym = al->sym,
711 },
Jiri Olsa26349582019-03-05 16:25:31 +0100712 .srcline = (char *) al->srcline,
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400713 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400714 .cpu = al->cpu,
715 .cpumode = al->cpumode,
716 .ip = al->addr,
717 .level = al->level,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900718 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900719 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900720 .period = sample->period,
721 .weight = sample->weight,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900722 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100723 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300724 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300725 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900726 .branch_info = bi,
727 .mem_info = mi,
Jin Yaofe962452019-06-28 17:22:59 +0800728 .block_info = block_info,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900729 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900730 .raw_data = sample->raw_data,
731 .raw_size = sample->raw_size,
Jiri Olsaa5051972016-07-05 08:56:05 +0200732 .ops = ops,
Andi Kleen37239082019-03-11 07:44:54 -0700733 .time = hist_time(sample->time),
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300734 }, *he = hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100735
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300736 if (!hists->has_callchains && he && he->callchain_size != 0)
737 hists->has_callchains = true;
Andi Kleen4968ac82019-03-11 07:44:58 -0700738 if (he && symbol_conf.res_sample)
739 hists__res_sample(he, sample);
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300740 return he;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100741}
742
Jiri Olsaa5051972016-07-05 08:56:05 +0200743struct hist_entry *hists__add_entry(struct hists *hists,
744 struct addr_location *al,
745 struct symbol *sym_parent,
746 struct branch_info *bi,
747 struct mem_info *mi,
748 struct perf_sample *sample,
749 bool sample_self)
750{
Jin Yaofe962452019-06-28 17:22:59 +0800751 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
Jiri Olsaa5051972016-07-05 08:56:05 +0200752 sample, sample_self, NULL);
753}
754
755struct hist_entry *hists__add_entry_ops(struct hists *hists,
756 struct hist_entry_ops *ops,
757 struct addr_location *al,
758 struct symbol *sym_parent,
759 struct branch_info *bi,
760 struct mem_info *mi,
761 struct perf_sample *sample,
762 bool sample_self)
763{
Jin Yaofe962452019-06-28 17:22:59 +0800764 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
Jiri Olsaa5051972016-07-05 08:56:05 +0200765 sample, sample_self, ops);
766}
767
Jin Yaofe962452019-06-28 17:22:59 +0800768struct hist_entry *hists__add_entry_block(struct hists *hists,
769 struct addr_location *al,
770 struct block_info *block_info)
771{
772 struct hist_entry entry = {
773 .block_info = block_info,
774 .hists = hists,
Jin Yaob65a7d32019-11-07 15:47:16 +0800775 .ms = {
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -0300776 .maps = al->maps,
Jin Yaob65a7d32019-11-07 15:47:16 +0800777 .map = al->map,
778 .sym = al->sym,
779 },
Jin Yaofe962452019-06-28 17:22:59 +0800780 }, *he = hists__findnew_entry(hists, &entry, al, false);
781
782 return he;
783}
784
Namhyung Kim69bcb012013-10-30 09:40:34 +0900785static int
786iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
787 struct addr_location *al __maybe_unused)
788{
789 return 0;
790}
791
792static int
793iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
794 struct addr_location *al __maybe_unused)
795{
796 return 0;
797}
798
799static int
800iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
801{
802 struct perf_sample *sample = iter->sample;
803 struct mem_info *mi;
804
805 mi = sample__resolve_mem(sample, al);
806 if (mi == NULL)
807 return -ENOMEM;
808
809 iter->priv = mi;
810 return 0;
811}
812
813static int
814iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
815{
816 u64 cost;
817 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300818 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900819 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900820 struct hist_entry *he;
821
822 if (mi == NULL)
823 return -EINVAL;
824
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900825 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900826 if (!cost)
827 cost = 1;
828
829 /*
830 * must pass period=weight in order to get the correct
831 * sorting from hists__collapse_resort() which is solely
832 * based on periods. We want sorting be done on nr_events * weight
833 * and this is indirectly achieved by passing period=weight here
834 * and the he_stat__add_period() function.
835 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900836 sample->period = cost;
837
Jiri Olsa0102ef32016-06-14 20:19:21 +0200838 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
839 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900840 if (!he)
841 return -ENOMEM;
842
843 iter->he = he;
844 return 0;
845}
846
847static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900848iter_finish_mem_entry(struct hist_entry_iter *iter,
849 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900850{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200851 struct evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300852 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900853 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900854 int err = -EINVAL;
855
856 if (he == NULL)
857 goto out;
858
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300859 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900860
861 err = hist_entry__append_callchain(he, iter->sample);
862
863out:
864 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300865 * We don't need to free iter->priv (mem_info) here since the mem info
866 * was either already freed in hists__findnew_entry() or passed to a
867 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900868 */
869 iter->priv = NULL;
870
871 iter->he = NULL;
872 return err;
873}
874
875static int
876iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
877{
878 struct branch_info *bi;
879 struct perf_sample *sample = iter->sample;
880
881 bi = sample__resolve_bstack(sample, al);
882 if (!bi)
883 return -ENOMEM;
884
885 iter->curr = 0;
886 iter->total = sample->branch_stack->nr;
887
888 iter->priv = bi;
889 return 0;
890}
891
892static int
Jin Yao2d78b182017-07-18 20:13:14 +0800893iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900894 struct addr_location *al __maybe_unused)
895{
896 return 0;
897}
898
899static int
900iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
901{
902 struct branch_info *bi = iter->priv;
903 int i = iter->curr;
904
905 if (bi == NULL)
906 return 0;
907
908 if (iter->curr >= iter->total)
909 return 0;
910
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -0300911 al->maps = bi[i].to.ms.maps;
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300912 al->map = bi[i].to.ms.map;
913 al->sym = bi[i].to.ms.sym;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900914 al->addr = bi[i].to.addr;
915 return 1;
916}
917
918static int
919iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
920{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900921 struct branch_info *bi;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200922 struct evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300923 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900924 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900925 struct hist_entry *he = NULL;
926 int i = iter->curr;
927 int err = 0;
928
929 bi = iter->priv;
930
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300931 if (iter->hide_unresolved && !(bi[i].from.ms.sym && bi[i].to.ms.sym))
Namhyung Kim69bcb012013-10-30 09:40:34 +0900932 goto out;
933
934 /*
935 * The report shows the percentage of total branches captured
936 * and not events sampled. Thus we use a pseudo period of 1.
937 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900938 sample->period = 1;
939 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
940
Jiri Olsa0102ef32016-06-14 20:19:21 +0200941 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
942 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900943 if (he == NULL)
944 return -ENOMEM;
945
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300946 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900947
948out:
949 iter->he = he;
950 iter->curr++;
951 return err;
952}
953
954static int
955iter_finish_branch_entry(struct hist_entry_iter *iter,
956 struct addr_location *al __maybe_unused)
957{
958 zfree(&iter->priv);
959 iter->he = NULL;
960
961 return iter->curr >= iter->total ? 0 : -1;
962}
963
964static int
965iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
966 struct addr_location *al __maybe_unused)
967{
968 return 0;
969}
970
971static int
972iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
973{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200974 struct evsel *evsel = iter->evsel;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900975 struct perf_sample *sample = iter->sample;
976 struct hist_entry *he;
977
Jiri Olsa0102ef32016-06-14 20:19:21 +0200978 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
979 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900980 if (he == NULL)
981 return -ENOMEM;
982
983 iter->he = he;
984 return 0;
985}
986
987static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900988iter_finish_normal_entry(struct hist_entry_iter *iter,
989 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900990{
Namhyung Kim69bcb012013-10-30 09:40:34 +0900991 struct hist_entry *he = iter->he;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200992 struct evsel *evsel = iter->evsel;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900993 struct perf_sample *sample = iter->sample;
994
995 if (he == NULL)
996 return 0;
997
998 iter->he = NULL;
999
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001000 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001001
1002 return hist_entry__append_callchain(he, sample);
1003}
1004
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001005static int
Adrian Hunter96b40f32015-09-25 16:15:47 +03001006iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001007 struct addr_location *al __maybe_unused)
1008{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001009 struct hist_entry **he_cache;
1010
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001011 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001012
1013 /*
1014 * This is for detecting cycles or recursions so that they're
1015 * cumulated only one time to prevent entries more than 100%
1016 * overhead.
1017 */
Jiri Olsae3ebaa42018-02-16 13:36:19 +01001018 he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001019 if (he_cache == NULL)
1020 return -ENOMEM;
1021
1022 iter->priv = he_cache;
1023 iter->curr = 0;
1024
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001025 return 0;
1026}
1027
1028static int
1029iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
1030 struct addr_location *al)
1031{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001032 struct evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001033 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001034 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001035 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001036 struct hist_entry *he;
1037 int err = 0;
1038
Jiri Olsa0102ef32016-06-14 20:19:21 +02001039 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
1040 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001041 if (he == NULL)
1042 return -ENOMEM;
1043
1044 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001045 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001046
Namhyung Kim82aa0192014-12-22 13:44:14 +09001047 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +09001048
1049 /*
1050 * We need to re-initialize the cursor since callchain_append()
1051 * advanced the cursor to the end.
1052 */
1053 callchain_cursor_commit(&callchain_cursor);
1054
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001055 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001056
1057 return err;
1058}
1059
1060static int
1061iter_next_cumulative_entry(struct hist_entry_iter *iter,
1062 struct addr_location *al)
1063{
1064 struct callchain_cursor_node *node;
1065
1066 node = callchain_cursor_current(&callchain_cursor);
1067 if (node == NULL)
1068 return 0;
1069
Namhyung Kimc7405d82013-10-31 13:58:30 +09001070 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001071}
1072
Kan Liang12e89e62020-03-19 13:25:17 -07001073static bool
1074hist_entry__fast__sym_diff(struct hist_entry *left,
1075 struct hist_entry *right)
1076{
1077 struct symbol *sym_l = left->ms.sym;
1078 struct symbol *sym_r = right->ms.sym;
1079
1080 if (!sym_l && !sym_r)
1081 return left->ip != right->ip;
1082
1083 return !!_sort__sym_cmp(sym_l, sym_r);
1084}
1085
1086
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001087static int
1088iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
1089 struct addr_location *al)
1090{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001091 struct evsel *evsel = iter->evsel;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001092 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001093 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001094 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001095 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -03001096 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001097 .cpu = al->cpu,
1098 .thread = al->thread,
1099 .comm = thread__comm(al->thread),
1100 .ip = al->addr,
1101 .ms = {
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -03001102 .maps = al->maps,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001103 .map = al->map,
1104 .sym = al->sym,
1105 },
Jiri Olsa26349582019-03-05 16:25:31 +01001106 .srcline = (char *) al->srcline,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001107 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +09001108 .raw_data = sample->raw_data,
1109 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001110 };
1111 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +09001112 struct callchain_cursor cursor;
Kan Liang12e89e62020-03-19 13:25:17 -07001113 bool fast = hists__has(he_tmp.hists, sym);
Namhyung Kimbe7f8552013-12-26 17:44:10 +09001114
1115 callchain_cursor_snapshot(&cursor, &callchain_cursor);
1116
1117 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001118
1119 /*
1120 * Check if there's duplicate entries in the callchain.
1121 * It's possible that it has cycles or recursive calls.
1122 */
1123 for (i = 0; i < iter->curr; i++) {
Kan Liang12e89e62020-03-19 13:25:17 -07001124 /*
1125 * For most cases, there are no duplicate entries in callchain.
1126 * The symbols are usually different. Do a quick check for
1127 * symbols first.
1128 */
1129 if (fast && hist_entry__fast__sym_diff(he_cache[i], &he_tmp))
1130 continue;
1131
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001132 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
1133 /* to avoid calling callback function */
1134 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001135 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001136 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001137 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001138
Jiri Olsa0102ef32016-06-14 20:19:21 +02001139 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
1140 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001141 if (he == NULL)
1142 return -ENOMEM;
1143
1144 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001145 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001146
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001147 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
Namhyung Kim82aa0192014-12-22 13:44:14 +09001148 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001149 return 0;
1150}
1151
1152static int
1153iter_finish_cumulative_entry(struct hist_entry_iter *iter,
1154 struct addr_location *al __maybe_unused)
1155{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001156 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001157 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001158
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001159 return 0;
1160}
1161
Namhyung Kim69bcb012013-10-30 09:40:34 +09001162const struct hist_iter_ops hist_iter_mem = {
1163 .prepare_entry = iter_prepare_mem_entry,
1164 .add_single_entry = iter_add_single_mem_entry,
1165 .next_entry = iter_next_nop_entry,
1166 .add_next_entry = iter_add_next_nop_entry,
1167 .finish_entry = iter_finish_mem_entry,
1168};
1169
1170const struct hist_iter_ops hist_iter_branch = {
1171 .prepare_entry = iter_prepare_branch_entry,
1172 .add_single_entry = iter_add_single_branch_entry,
1173 .next_entry = iter_next_branch_entry,
1174 .add_next_entry = iter_add_next_branch_entry,
1175 .finish_entry = iter_finish_branch_entry,
1176};
1177
1178const struct hist_iter_ops hist_iter_normal = {
1179 .prepare_entry = iter_prepare_normal_entry,
1180 .add_single_entry = iter_add_single_normal_entry,
1181 .next_entry = iter_next_nop_entry,
1182 .add_next_entry = iter_add_next_nop_entry,
1183 .finish_entry = iter_finish_normal_entry,
1184};
1185
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001186const struct hist_iter_ops hist_iter_cumulative = {
1187 .prepare_entry = iter_prepare_cumulative_entry,
1188 .add_single_entry = iter_add_single_cumulative_entry,
1189 .next_entry = iter_next_cumulative_entry,
1190 .add_next_entry = iter_add_next_cumulative_entry,
1191 .finish_entry = iter_finish_cumulative_entry,
1192};
1193
Namhyung Kim69bcb012013-10-30 09:40:34 +09001194int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001195 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +09001196{
1197 int err, err2;
Krister Johansen9c68ae92017-01-05 22:23:31 -08001198 struct map *alm = NULL;
1199
Arnaldo Carvalho de Melo362379a2018-05-24 11:20:39 -03001200 if (al)
Krister Johansen9c68ae92017-01-05 22:23:31 -08001201 alm = map__get(al->map);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001202
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001203 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
Namhyung Kim063bd932015-05-19 17:04:10 +09001204 iter->evsel, al, max_stack_depth);
Changbin Ducb6186a2019-03-16 16:05:49 +08001205 if (err) {
1206 map__put(alm);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001207 return err;
Changbin Ducb6186a2019-03-16 16:05:49 +08001208 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001209
Namhyung Kim69bcb012013-10-30 09:40:34 +09001210 err = iter->ops->prepare_entry(iter, al);
1211 if (err)
1212 goto out;
1213
1214 err = iter->ops->add_single_entry(iter, al);
1215 if (err)
1216 goto out;
1217
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001218 if (iter->he && iter->add_entry_cb) {
1219 err = iter->add_entry_cb(iter, al, true, arg);
1220 if (err)
1221 goto out;
1222 }
1223
Namhyung Kim69bcb012013-10-30 09:40:34 +09001224 while (iter->ops->next_entry(iter, al)) {
1225 err = iter->ops->add_next_entry(iter, al);
1226 if (err)
1227 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001228
1229 if (iter->he && iter->add_entry_cb) {
1230 err = iter->add_entry_cb(iter, al, false, arg);
1231 if (err)
1232 goto out;
1233 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001234 }
1235
1236out:
1237 err2 = iter->ops->finish_entry(iter, al);
1238 if (!err)
1239 err = err2;
1240
Krister Johansen9c68ae92017-01-05 22:23:31 -08001241 map__put(alm);
1242
Namhyung Kim69bcb012013-10-30 09:40:34 +09001243 return err;
1244}
1245
John Kacur3d1d07e2009-09-28 15:32:55 +02001246int64_t
1247hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1248{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001249 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001250 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001251 int64_t cmp = 0;
1252
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001253 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001254 if (perf_hpp__is_dynamic_entry(fmt) &&
1255 !perf_hpp__defined_dynamic_entry(fmt, hists))
1256 continue;
1257
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001258 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001259 if (cmp)
1260 break;
1261 }
1262
1263 return cmp;
1264}
1265
1266int64_t
1267hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1268{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001269 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001270 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001271 int64_t cmp = 0;
1272
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001273 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001274 if (perf_hpp__is_dynamic_entry(fmt) &&
1275 !perf_hpp__defined_dynamic_entry(fmt, hists))
1276 continue;
1277
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001278 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001279 if (cmp)
1280 break;
1281 }
1282
1283 return cmp;
1284}
1285
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001286void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001287{
Jiri Olsaf542e762016-07-05 08:56:04 +02001288 struct hist_entry_ops *ops = he->ops;
1289
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001290 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001291 map__zput(he->ms.map);
1292
1293 if (he->branch_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -03001294 map__zput(he->branch_info->from.ms.map);
1295 map__zput(he->branch_info->to.ms.map);
Andi Kleen508be0d2016-05-20 13:15:08 -07001296 free_srcline(he->branch_info->srcline_from);
1297 free_srcline(he->branch_info->srcline_to);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001298 zfree(&he->branch_info);
1299 }
1300
1301 if (he->mem_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -03001302 map__zput(he->mem_info->iaddr.ms.map);
1303 map__zput(he->mem_info->daddr.ms.map);
Jiri Olsa9f874982018-03-07 16:50:06 +01001304 mem_info__zput(he->mem_info);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001305 }
1306
Jin Yao99150a12019-06-28 17:23:01 +08001307 if (he->block_info)
1308 block_info__zput(he->block_info);
1309
Andi Kleen4968ac82019-03-11 07:44:58 -07001310 zfree(&he->res_samples);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001311 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +09001312 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -07001313 if (he->srcfile && he->srcfile[0])
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001314 zfree(&he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001315 free_callchain(he->callchain);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001316 zfree(&he->trace_output);
1317 zfree(&he->raw_data);
Jiri Olsaf542e762016-07-05 08:56:04 +02001318 ops->free(he);
John Kacur3d1d07e2009-09-28 15:32:55 +02001319}
1320
1321/*
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001322 * If this is not the last column, then we need to pad it according to the
Ingo Molnaradba1632018-12-03 11:22:00 +01001323 * pre-calculated max length for this column, otherwise don't bother adding
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001324 * spaces because that would break viewing this with, for instance, 'less',
1325 * that would show tons of trailing spaces when a long C++ demangled method
1326 * names is sampled.
1327*/
1328int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1329 struct perf_hpp_fmt *fmt, int printed)
1330{
1331 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
Jiri Olsada1b0402016-06-14 20:19:20 +02001332 const int width = fmt->width(fmt, hpp, he->hists);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001333 if (printed < width) {
1334 advance_hpp(hpp, printed);
1335 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1336 }
1337 }
1338
1339 return printed;
1340}
1341
1342/*
John Kacur3d1d07e2009-09-28 15:32:55 +02001343 * collapse the histogram
1344 */
1345
Namhyung Kimaef810e2016-02-25 00:13:34 +09001346static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
Namhyung Kimaec13a72016-03-09 22:46:58 +09001347static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1348 enum hist_filter type);
1349
1350typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1351
1352static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1353{
1354 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1355}
1356
1357static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1358 enum hist_filter type,
1359 fmt_chk_fn check)
1360{
1361 struct perf_hpp_fmt *fmt;
1362 bool type_match = false;
1363 struct hist_entry *parent = he->parent_he;
1364
1365 switch (type) {
1366 case HIST_FILTER__THREAD:
1367 if (symbol_conf.comm_list == NULL &&
1368 symbol_conf.pid_list == NULL &&
1369 symbol_conf.tid_list == NULL)
1370 return;
1371 break;
1372 case HIST_FILTER__DSO:
1373 if (symbol_conf.dso_list == NULL)
1374 return;
1375 break;
1376 case HIST_FILTER__SYMBOL:
1377 if (symbol_conf.sym_list == NULL)
1378 return;
1379 break;
1380 case HIST_FILTER__PARENT:
1381 case HIST_FILTER__GUEST:
1382 case HIST_FILTER__HOST:
1383 case HIST_FILTER__SOCKET:
Jiri Olsa9857b712016-08-17 14:55:23 +02001384 case HIST_FILTER__C2C:
Namhyung Kimaec13a72016-03-09 22:46:58 +09001385 default:
1386 return;
1387 }
1388
1389 /* if it's filtered by own fmt, it has to have filter bits */
1390 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1391 if (check(fmt)) {
1392 type_match = true;
1393 break;
1394 }
1395 }
1396
1397 if (type_match) {
1398 /*
1399 * If the filter is for current level entry, propagate
1400 * filter marker to parents. The marker bit was
1401 * already set by default so it only needs to clear
1402 * non-filtered entries.
1403 */
1404 if (!(he->filtered & (1 << type))) {
1405 while (parent) {
1406 parent->filtered &= ~(1 << type);
1407 parent = parent->parent_he;
1408 }
1409 }
1410 } else {
1411 /*
1412 * If current entry doesn't have matching formats, set
1413 * filter marker for upper level entries. it will be
1414 * cleared if its lower level entries is not filtered.
1415 *
1416 * For lower-level entries, it inherits parent's
1417 * filter bit so that lower level entries of a
1418 * non-filtered entry won't set the filter marker.
1419 */
1420 if (parent == NULL)
1421 he->filtered |= (1 << type);
1422 else
1423 he->filtered |= (parent->filtered & (1 << type));
1424 }
1425}
1426
1427static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1428{
1429 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1430 check_thread_entry);
1431
1432 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1433 perf_hpp__is_dso_entry);
1434
1435 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1436 perf_hpp__is_sym_entry);
1437
1438 hists__apply_filters(he->hists, he);
1439}
Namhyung Kimaef810e2016-02-25 00:13:34 +09001440
1441static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001442 struct rb_root_cached *root,
Namhyung Kimaef810e2016-02-25 00:13:34 +09001443 struct hist_entry *he,
Namhyung Kimaec13a72016-03-09 22:46:58 +09001444 struct hist_entry *parent_he,
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001445 struct perf_hpp_list *hpp_list)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001446{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001447 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001448 struct rb_node *parent = NULL;
1449 struct hist_entry *iter, *new;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001450 struct perf_hpp_fmt *fmt;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001451 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001452 bool leftmost = true;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001453
1454 while (*p != NULL) {
1455 parent = *p;
1456 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1457
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001458 cmp = 0;
1459 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1460 cmp = fmt->collapse(fmt, iter, he);
1461 if (cmp)
1462 break;
1463 }
1464
Namhyung Kimaef810e2016-02-25 00:13:34 +09001465 if (!cmp) {
1466 he_stat__add_stat(&iter->stat, &he->stat);
1467 return iter;
1468 }
1469
1470 if (cmp < 0)
1471 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001472 else {
Namhyung Kimaef810e2016-02-25 00:13:34 +09001473 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001474 leftmost = false;
1475 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001476 }
1477
1478 new = hist_entry__new(he, true);
1479 if (new == NULL)
1480 return NULL;
1481
Namhyung Kimaef810e2016-02-25 00:13:34 +09001482 hists->nr_entries++;
1483
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001484 /* save related format list for output */
1485 new->hpp_list = hpp_list;
Namhyung Kimaec13a72016-03-09 22:46:58 +09001486 new->parent_he = parent_he;
1487
1488 hist_entry__apply_hierarchy_filters(new);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001489
1490 /* some fields are now passed to 'new' */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001491 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1492 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1493 he->trace_output = NULL;
1494 else
1495 new->trace_output = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001496
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001497 if (perf_hpp__is_srcline_entry(fmt))
1498 he->srcline = NULL;
1499 else
1500 new->srcline = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001501
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001502 if (perf_hpp__is_srcfile_entry(fmt))
1503 he->srcfile = NULL;
1504 else
1505 new->srcfile = NULL;
1506 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001507
1508 rb_link_node(&new->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001509 rb_insert_color_cached(&new->rb_node_in, root, leftmost);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001510 return new;
1511}
1512
1513static int hists__hierarchy_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001514 struct rb_root_cached *root,
Namhyung Kimaef810e2016-02-25 00:13:34 +09001515 struct hist_entry *he)
1516{
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001517 struct perf_hpp_list_node *node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001518 struct hist_entry *new_he = NULL;
1519 struct hist_entry *parent = NULL;
1520 int depth = 0;
1521 int ret = 0;
1522
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001523 list_for_each_entry(node, &hists->hpp_formats, list) {
1524 /* skip period (overhead) and elided columns */
1525 if (node->level == 0 || node->skip)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001526 continue;
1527
1528 /* insert copy of 'he' for each fmt into the hierarchy */
Namhyung Kimaec13a72016-03-09 22:46:58 +09001529 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001530 if (new_he == NULL) {
1531 ret = -1;
1532 break;
1533 }
1534
1535 root = &new_he->hroot_in;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001536 new_he->depth = depth++;
1537 parent = new_he;
1538 }
1539
1540 if (new_he) {
1541 new_he->leaf = true;
1542
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001543 if (hist_entry__has_callchains(new_he) &&
1544 symbol_conf.use_callchain) {
Namhyung Kimaef810e2016-02-25 00:13:34 +09001545 callchain_cursor_reset(&callchain_cursor);
1546 if (callchain_merge(&callchain_cursor,
1547 new_he->callchain,
1548 he->callchain) < 0)
1549 ret = -1;
1550 }
1551 }
1552
1553 /* 'he' is no longer used */
1554 hist_entry__delete(he);
1555
1556 /* return 0 (or -1) since it already applied filters */
1557 return ret;
1558}
1559
Jiri Olsa592dac62016-03-24 13:52:17 +01001560static int hists__collapse_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001561 struct rb_root_cached *root,
Jiri Olsa592dac62016-03-24 13:52:17 +01001562 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001563{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001564 struct rb_node **p = &root->rb_root.rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001565 struct rb_node *parent = NULL;
1566 struct hist_entry *iter;
1567 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001568 bool leftmost = true;
John Kacur3d1d07e2009-09-28 15:32:55 +02001569
Namhyung Kimaef810e2016-02-25 00:13:34 +09001570 if (symbol_conf.report_hierarchy)
1571 return hists__hierarchy_insert_entry(hists, root, he);
1572
John Kacur3d1d07e2009-09-28 15:32:55 +02001573 while (*p != NULL) {
1574 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001575 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001576
1577 cmp = hist_entry__collapse(iter, he);
1578
1579 if (!cmp) {
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001580 int ret = 0;
1581
Namhyung Kim139c0812012-10-04 21:49:43 +09001582 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001583 if (symbol_conf.cumulate_callchain)
1584 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001585
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001586 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001587 callchain_cursor_reset(&callchain_cursor);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001588 if (callchain_merge(&callchain_cursor,
1589 iter->callchain,
1590 he->callchain) < 0)
1591 ret = -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001592 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001593 hist_entry__delete(he);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001594 return ret;
John Kacur3d1d07e2009-09-28 15:32:55 +02001595 }
1596
1597 if (cmp < 0)
1598 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001599 else {
John Kacur3d1d07e2009-09-28 15:32:55 +02001600 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001601 leftmost = false;
1602 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001603 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001604 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001605
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001606 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001607 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001608 return 1;
John Kacur3d1d07e2009-09-28 15:32:55 +02001609}
1610
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001611struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001612{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001613 struct rb_root_cached *root;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001614
1615 pthread_mutex_lock(&hists->lock);
1616
1617 root = hists->entries_in;
1618 if (++hists->entries_in > &hists->entries_in_array[1])
1619 hists->entries_in = &hists->entries_in_array[0];
1620
1621 pthread_mutex_unlock(&hists->lock);
1622
1623 return root;
1624}
1625
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001626static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1627{
1628 hists__filter_entry_by_dso(hists, he);
1629 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001630 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001631 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001632}
1633
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001634int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001635{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001636 struct rb_root_cached *root;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001637 struct rb_node *next;
1638 struct hist_entry *n;
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001639 int ret;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001640
Jiri Olsa52225032016-05-03 13:54:42 +02001641 if (!hists__has(hists, need_collapse))
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001642 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001643
Namhyung Kim740b97f2014-12-22 13:44:10 +09001644 hists->nr_entries = 0;
1645
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001646 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001647
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001648 next = rb_first_cached(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001649
1650 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001651 if (session_done())
1652 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001653 n = rb_entry(next, struct hist_entry, rb_node_in);
1654 next = rb_next(&n->rb_node_in);
1655
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001656 rb_erase_cached(&n->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001657 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1658 if (ret < 0)
1659 return -1;
1660
1661 if (ret) {
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001662 /*
1663 * If it wasn't combined with one of the entries already
1664 * collapsed, we need to apply the filters that may have
1665 * been set by, say, the hist_browser.
1666 */
1667 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001668 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001669 if (prog)
1670 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001671 }
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001672 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001673}
1674
Jiri Olsa722ddfd2019-11-05 00:27:11 +01001675static int64_t hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001676{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001677 struct hists *hists = a->hists;
Namhyung Kim043ca3892014-03-03 14:18:00 +09001678 struct perf_hpp_fmt *fmt;
1679 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001680
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001681 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001682 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001683 continue;
1684
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001685 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001686 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001687 break;
1688 }
1689
Namhyung Kim043ca3892014-03-03 14:18:00 +09001690 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001691}
1692
Namhyung Kim9283ba92014-04-24 16:37:26 +09001693static void hists__reset_filter_stats(struct hists *hists)
1694{
1695 hists->nr_non_filtered_entries = 0;
1696 hists->stats.total_non_filtered_period = 0;
1697}
1698
1699void hists__reset_stats(struct hists *hists)
1700{
1701 hists->nr_entries = 0;
1702 hists->stats.total_period = 0;
1703
1704 hists__reset_filter_stats(hists);
1705}
1706
1707static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1708{
1709 hists->nr_non_filtered_entries++;
1710 hists->stats.total_non_filtered_period += h->stat.period;
1711}
1712
1713void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1714{
1715 if (!h->filtered)
1716 hists__inc_filter_stats(hists, h);
1717
1718 hists->nr_entries++;
1719 hists->stats.total_period += h->stat.period;
1720}
1721
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001722static void hierarchy_recalc_total_periods(struct hists *hists)
1723{
1724 struct rb_node *node;
1725 struct hist_entry *he;
1726
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001727 node = rb_first_cached(&hists->entries);
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001728
1729 hists->stats.total_period = 0;
1730 hists->stats.total_non_filtered_period = 0;
1731
1732 /*
1733 * recalculate total period using top-level entries only
1734 * since lower level entries only see non-filtered entries
1735 * but upper level entries have sum of both entries.
1736 */
1737 while (node) {
1738 he = rb_entry(node, struct hist_entry, rb_node);
1739 node = rb_next(node);
1740
1741 hists->stats.total_period += he->stat.period;
1742 if (!he->filtered)
1743 hists->stats.total_non_filtered_period += he->stat.period;
1744 }
1745}
1746
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001747static void hierarchy_insert_output_entry(struct rb_root_cached *root,
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001748 struct hist_entry *he)
1749{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001750 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001751 struct rb_node *parent = NULL;
1752 struct hist_entry *iter;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001753 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001754 bool leftmost = true;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001755
1756 while (*p != NULL) {
1757 parent = *p;
1758 iter = rb_entry(parent, struct hist_entry, rb_node);
1759
1760 if (hist_entry__sort(he, iter) > 0)
1761 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001762 else {
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001763 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001764 leftmost = false;
1765 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001766 }
1767
1768 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001769 rb_insert_color_cached(&he->rb_node, root, leftmost);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001770
1771 /* update column width of dynamic entry */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001772 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1773 if (perf_hpp__is_dynamic_entry(fmt))
1774 fmt->sort(fmt, he, NULL);
1775 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001776}
1777
1778static void hists__hierarchy_output_resort(struct hists *hists,
1779 struct ui_progress *prog,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001780 struct rb_root_cached *root_in,
1781 struct rb_root_cached *root_out,
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001782 u64 min_callchain_hits,
1783 bool use_callchain)
1784{
1785 struct rb_node *node;
1786 struct hist_entry *he;
1787
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001788 *root_out = RB_ROOT_CACHED;
1789 node = rb_first_cached(root_in);
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001790
1791 while (node) {
1792 he = rb_entry(node, struct hist_entry, rb_node_in);
1793 node = rb_next(node);
1794
1795 hierarchy_insert_output_entry(root_out, he);
1796
1797 if (prog)
1798 ui_progress__update(prog, 1);
1799
Namhyung Kimc72ab442016-11-08 22:08:33 +09001800 hists->nr_entries++;
1801 if (!he->filtered) {
1802 hists->nr_non_filtered_entries++;
1803 hists__calc_col_len(hists, he);
1804 }
1805
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001806 if (!he->leaf) {
1807 hists__hierarchy_output_resort(hists, prog,
1808 &he->hroot_in,
1809 &he->hroot_out,
1810 min_callchain_hits,
1811 use_callchain);
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001812 continue;
1813 }
1814
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001815 if (!use_callchain)
1816 continue;
1817
1818 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1819 u64 total = he->stat.period;
1820
1821 if (symbol_conf.cumulate_callchain)
1822 total = he->stat_acc->period;
1823
1824 min_callchain_hits = total * (callchain_param.min_percent / 100);
1825 }
1826
1827 callchain_param.sort(&he->sorted_chain, he->callchain,
1828 min_callchain_hits, &callchain_param);
1829 }
1830}
1831
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001832static void __hists__insert_output_entry(struct rb_root_cached *entries,
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001833 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001834 u64 min_callchain_hits,
1835 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001836{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001837 struct rb_node **p = &entries->rb_root.rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001838 struct rb_node *parent = NULL;
1839 struct hist_entry *iter;
Namhyung Kimabab5e72016-02-27 03:52:47 +09001840 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001841 bool leftmost = true;
John Kacur3d1d07e2009-09-28 15:32:55 +02001842
Namhyung Kim744070e2016-01-28 00:40:48 +09001843 if (use_callchain) {
1844 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1845 u64 total = he->stat.period;
1846
1847 if (symbol_conf.cumulate_callchain)
1848 total = he->stat_acc->period;
1849
1850 min_callchain_hits = total * (callchain_param.min_percent / 100);
1851 }
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001852 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001853 min_callchain_hits, &callchain_param);
Namhyung Kim744070e2016-01-28 00:40:48 +09001854 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001855
1856 while (*p != NULL) {
1857 parent = *p;
1858 iter = rb_entry(parent, struct hist_entry, rb_node);
1859
Namhyung Kim043ca3892014-03-03 14:18:00 +09001860 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001861 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001862 else {
John Kacur3d1d07e2009-09-28 15:32:55 +02001863 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001864 leftmost = false;
1865 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001866 }
1867
1868 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001869 rb_insert_color_cached(&he->rb_node, entries, leftmost);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001870
1871 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1872 if (perf_hpp__is_dynamic_entry(fmt) &&
1873 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1874 fmt->sort(fmt, he, NULL); /* update column width */
1875 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001876}
1877
Jiri Olsa01441af2016-01-18 10:23:59 +01001878static void output_resort(struct hists *hists, struct ui_progress *prog,
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001879 bool use_callchain, hists__resort_cb_t cb,
1880 void *cb_arg)
John Kacur3d1d07e2009-09-28 15:32:55 +02001881{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001882 struct rb_root_cached *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001883 struct rb_node *next;
1884 struct hist_entry *n;
Namhyung Kim467ef102016-02-16 23:08:19 +09001885 u64 callchain_total;
John Kacur3d1d07e2009-09-28 15:32:55 +02001886 u64 min_callchain_hits;
1887
Namhyung Kim467ef102016-02-16 23:08:19 +09001888 callchain_total = hists->callchain_period;
1889 if (symbol_conf.filter_relative)
1890 callchain_total = hists->callchain_non_filtered_period;
1891
1892 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001893
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001894 hists__reset_stats(hists);
1895 hists__reset_col_len(hists);
1896
1897 if (symbol_conf.report_hierarchy) {
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001898 hists__hierarchy_output_resort(hists, prog,
1899 &hists->entries_collapsed,
1900 &hists->entries,
1901 min_callchain_hits,
1902 use_callchain);
1903 hierarchy_recalc_total_periods(hists);
1904 return;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001905 }
1906
Jiri Olsa52225032016-05-03 13:54:42 +02001907 if (hists__has(hists, need_collapse))
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001908 root = &hists->entries_collapsed;
1909 else
1910 root = hists->entries_in;
1911
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001912 next = rb_first_cached(root);
1913 hists->entries = RB_ROOT_CACHED;
John Kacur3d1d07e2009-09-28 15:32:55 +02001914
1915 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001916 n = rb_entry(next, struct hist_entry, rb_node_in);
1917 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001918
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001919 if (cb && cb(n, cb_arg))
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001920 continue;
1921
Kan Liangf9db0d02015-08-11 06:30:48 -04001922 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001923 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001924
1925 if (!n->filtered)
1926 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001927
1928 if (prog)
1929 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001930 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001931}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001932
Jiri Olsa32dcd022019-07-21 13:23:51 +02001933void perf_evsel__output_resort_cb(struct evsel *evsel, struct ui_progress *prog,
Jiri Olsa57496182019-02-04 15:18:07 +01001934 hists__resort_cb_t cb, void *cb_arg)
Jiri Olsa01441af2016-01-18 10:23:59 +01001935{
Jiri Olsa01441af2016-01-18 10:23:59 +01001936 bool use_callchain;
1937
1938 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -03001939 use_callchain = evsel__has_callchain(evsel);
Jiri Olsa01441af2016-01-18 10:23:59 +01001940 else
1941 use_callchain = symbol_conf.use_callchain;
1942
Jin Yaob49a8212017-05-08 18:43:02 +08001943 use_callchain |= symbol_conf.show_branchflag_count;
1944
Jiri Olsa57496182019-02-04 15:18:07 +01001945 output_resort(evsel__hists(evsel), prog, use_callchain, cb, cb_arg);
1946}
1947
Jiri Olsa32dcd022019-07-21 13:23:51 +02001948void perf_evsel__output_resort(struct evsel *evsel, struct ui_progress *prog)
Jiri Olsa57496182019-02-04 15:18:07 +01001949{
1950 return perf_evsel__output_resort_cb(evsel, prog, NULL, NULL);
Jiri Olsa452ce032016-01-18 10:24:00 +01001951}
1952
1953void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1954{
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001955 output_resort(hists, prog, symbol_conf.use_callchain, NULL, NULL);
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001956}
1957
1958void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1959 hists__resort_cb_t cb)
1960{
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001961 output_resort(hists, prog, symbol_conf.use_callchain, cb, NULL);
Jiri Olsa01441af2016-01-18 10:23:59 +01001962}
1963
Namhyung Kim8c018722016-02-25 00:13:36 +09001964static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1965{
1966 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1967 return false;
1968
1969 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1970 return true;
1971
1972 return false;
1973}
1974
1975struct rb_node *rb_hierarchy_last(struct rb_node *node)
1976{
1977 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1978
1979 while (can_goto_child(he, HMD_NORMAL)) {
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001980 node = rb_last(&he->hroot_out.rb_root);
Namhyung Kim8c018722016-02-25 00:13:36 +09001981 he = rb_entry(node, struct hist_entry, rb_node);
1982 }
1983 return node;
1984}
1985
1986struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1987{
1988 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1989
1990 if (can_goto_child(he, hmd))
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001991 node = rb_first_cached(&he->hroot_out);
Namhyung Kim8c018722016-02-25 00:13:36 +09001992 else
1993 node = rb_next(node);
1994
1995 while (node == NULL) {
1996 he = he->parent_he;
1997 if (he == NULL)
1998 break;
1999
2000 node = rb_next(&he->rb_node);
2001 }
2002 return node;
2003}
2004
2005struct rb_node *rb_hierarchy_prev(struct rb_node *node)
2006{
2007 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
2008
2009 node = rb_prev(node);
2010 if (node)
2011 return rb_hierarchy_last(node);
2012
2013 he = he->parent_he;
2014 if (he == NULL)
2015 return NULL;
2016
2017 return &he->rb_node;
2018}
2019
Namhyung Kima7b58952016-02-26 21:13:16 +09002020bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
2021{
2022 struct rb_node *node;
2023 struct hist_entry *child;
2024 float percent;
2025
2026 if (he->leaf)
2027 return false;
2028
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002029 node = rb_first_cached(&he->hroot_out);
Namhyung Kima7b58952016-02-26 21:13:16 +09002030 child = rb_entry(node, struct hist_entry, rb_node);
2031
2032 while (node && child->filtered) {
2033 node = rb_next(node);
2034 child = rb_entry(node, struct hist_entry, rb_node);
2035 }
2036
2037 if (node)
2038 percent = hist_entry__get_percent_limit(child);
2039 else
2040 percent = 0;
2041
2042 return node && percent >= limit;
2043}
2044
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002045static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002046 enum hist_filter filter)
2047{
2048 h->filtered &= ~(1 << filter);
Namhyung Kim155e9af2016-02-25 00:13:38 +09002049
2050 if (symbol_conf.report_hierarchy) {
2051 struct hist_entry *parent = h->parent_he;
2052
2053 while (parent) {
2054 he_stat__add_stat(&parent->stat, &h->stat);
2055
2056 parent->filtered &= ~(1 << filter);
2057
2058 if (parent->filtered)
2059 goto next;
2060
2061 /* force fold unfiltered entry for simplicity */
2062 parent->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09002063 parent->has_no_entry = false;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002064 parent->row_offset = 0;
2065 parent->nr_rows = 0;
2066next:
2067 parent = parent->parent_he;
2068 }
2069 }
2070
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002071 if (h->filtered)
2072 return;
2073
Namhyung Kim87e90f42014-04-24 16:44:16 +09002074 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09002075 h->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09002076 h->has_no_entry = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03002077 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08002078 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09002079
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09002080 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002081
Namhyung Kim9283ba92014-04-24 16:37:26 +09002082 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002083 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002084}
2085
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02002086
2087static bool hists__filter_entry_by_dso(struct hists *hists,
2088 struct hist_entry *he)
2089{
2090 if (hists->dso_filter != NULL &&
2091 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
2092 he->filtered |= (1 << HIST_FILTER__DSO);
2093 return true;
2094 }
2095
2096 return false;
2097}
2098
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02002099static bool hists__filter_entry_by_thread(struct hists *hists,
2100 struct hist_entry *he)
2101{
2102 if (hists->thread_filter != NULL &&
2103 he->thread != hists->thread_filter) {
2104 he->filtered |= (1 << HIST_FILTER__THREAD);
2105 return true;
2106 }
2107
2108 return false;
2109}
2110
Namhyung Kime94d53e2012-03-16 17:50:51 +09002111static bool hists__filter_entry_by_symbol(struct hists *hists,
2112 struct hist_entry *he)
2113{
2114 if (hists->symbol_filter_str != NULL &&
2115 (!he->ms.sym || strstr(he->ms.sym->name,
2116 hists->symbol_filter_str) == NULL)) {
2117 he->filtered |= (1 << HIST_FILTER__SYMBOL);
2118 return true;
2119 }
2120
2121 return false;
2122}
2123
Kan Liang21394d92015-09-04 10:45:44 -04002124static bool hists__filter_entry_by_socket(struct hists *hists,
2125 struct hist_entry *he)
2126{
2127 if ((hists->socket_filter > -1) &&
2128 (he->socket != hists->socket_filter)) {
2129 he->filtered |= (1 << HIST_FILTER__SOCKET);
2130 return true;
2131 }
2132
2133 return false;
2134}
2135
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002136typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
2137
2138static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04002139{
2140 struct rb_node *nd;
2141
2142 hists->stats.nr_non_filtered_samples = 0;
2143
2144 hists__reset_filter_stats(hists);
2145 hists__reset_col_len(hists);
2146
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002147 for (nd = rb_first_cached(&hists->entries); nd; nd = rb_next(nd)) {
Kan Liang84734b02015-09-04 10:45:45 -04002148 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2149
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002150 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04002151 continue;
2152
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002153 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04002154 }
2155}
2156
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002157static void resort_filtered_entry(struct rb_root_cached *root,
2158 struct hist_entry *he)
Namhyung Kim70642852016-02-25 00:13:39 +09002159{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002160 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kim70642852016-02-25 00:13:39 +09002161 struct rb_node *parent = NULL;
2162 struct hist_entry *iter;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002163 struct rb_root_cached new_root = RB_ROOT_CACHED;
Namhyung Kim70642852016-02-25 00:13:39 +09002164 struct rb_node *nd;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002165 bool leftmost = true;
Namhyung Kim70642852016-02-25 00:13:39 +09002166
2167 while (*p != NULL) {
2168 parent = *p;
2169 iter = rb_entry(parent, struct hist_entry, rb_node);
2170
2171 if (hist_entry__sort(he, iter) > 0)
2172 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002173 else {
Namhyung Kim70642852016-02-25 00:13:39 +09002174 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002175 leftmost = false;
2176 }
Namhyung Kim70642852016-02-25 00:13:39 +09002177 }
2178
2179 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002180 rb_insert_color_cached(&he->rb_node, root, leftmost);
Namhyung Kim70642852016-02-25 00:13:39 +09002181
2182 if (he->leaf || he->filtered)
2183 return;
2184
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002185 nd = rb_first_cached(&he->hroot_out);
Namhyung Kim70642852016-02-25 00:13:39 +09002186 while (nd) {
2187 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2188
2189 nd = rb_next(nd);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002190 rb_erase_cached(&h->rb_node, &he->hroot_out);
Namhyung Kim70642852016-02-25 00:13:39 +09002191
2192 resort_filtered_entry(&new_root, h);
2193 }
2194
2195 he->hroot_out = new_root;
2196}
2197
Namhyung Kim155e9af2016-02-25 00:13:38 +09002198static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2199{
2200 struct rb_node *nd;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002201 struct rb_root_cached new_root = RB_ROOT_CACHED;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002202
2203 hists->stats.nr_non_filtered_samples = 0;
2204
2205 hists__reset_filter_stats(hists);
2206 hists__reset_col_len(hists);
2207
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002208 nd = rb_first_cached(&hists->entries);
Namhyung Kim155e9af2016-02-25 00:13:38 +09002209 while (nd) {
2210 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2211 int ret;
2212
2213 ret = hist_entry__filter(h, type, arg);
2214
2215 /*
2216 * case 1. non-matching type
2217 * zero out the period, set filter marker and move to child
2218 */
2219 if (ret < 0) {
2220 memset(&h->stat, 0, sizeof(h->stat));
2221 h->filtered |= (1 << type);
2222
2223 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2224 }
2225 /*
2226 * case 2. matched type (filter out)
2227 * set filter marker and move to next
2228 */
2229 else if (ret == 1) {
2230 h->filtered |= (1 << type);
2231
2232 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2233 }
2234 /*
2235 * case 3. ok (not filtered)
2236 * add period to hists and parents, erase the filter marker
2237 * and move to next sibling
2238 */
2239 else {
2240 hists__remove_entry_filter(hists, h, type);
2241
2242 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2243 }
2244 }
Namhyung Kim70642852016-02-25 00:13:39 +09002245
Namhyung Kimf7fb5382016-03-09 22:47:02 +09002246 hierarchy_recalc_total_periods(hists);
2247
Namhyung Kim70642852016-02-25 00:13:39 +09002248 /*
2249 * resort output after applying a new filter since filter in a lower
2250 * hierarchy can change periods in a upper hierarchy.
2251 */
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002252 nd = rb_first_cached(&hists->entries);
Namhyung Kim70642852016-02-25 00:13:39 +09002253 while (nd) {
2254 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2255
2256 nd = rb_next(nd);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002257 rb_erase_cached(&h->rb_node, &hists->entries);
Namhyung Kim70642852016-02-25 00:13:39 +09002258
2259 resort_filtered_entry(&new_root, h);
2260 }
2261
2262 hists->entries = new_root;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002263}
2264
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002265void hists__filter_by_thread(struct hists *hists)
2266{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002267 if (symbol_conf.report_hierarchy)
2268 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2269 hists->thread_filter);
2270 else
2271 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2272 hists__filter_entry_by_thread);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002273}
2274
2275void hists__filter_by_dso(struct hists *hists)
2276{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002277 if (symbol_conf.report_hierarchy)
2278 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2279 hists->dso_filter);
2280 else
2281 hists__filter_by_type(hists, HIST_FILTER__DSO,
2282 hists__filter_entry_by_dso);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002283}
2284
2285void hists__filter_by_symbol(struct hists *hists)
2286{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002287 if (symbol_conf.report_hierarchy)
2288 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2289 hists->symbol_filter_str);
2290 else
2291 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2292 hists__filter_entry_by_symbol);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002293}
2294
2295void hists__filter_by_socket(struct hists *hists)
2296{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002297 if (symbol_conf.report_hierarchy)
2298 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2299 &hists->socket_filter);
2300 else
2301 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2302 hists__filter_entry_by_socket);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002303}
2304
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002305void events_stats__inc(struct events_stats *stats, u32 type)
2306{
2307 ++stats->nr_events[0];
2308 ++stats->nr_events[type];
2309}
2310
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002311void hists__inc_nr_events(struct hists *hists, u32 type)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002312{
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002313 events_stats__inc(&hists->stats, type);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002314}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002315
Namhyung Kim1844dbc2014-05-28 14:12:18 +09002316void hists__inc_nr_samples(struct hists *hists, bool filtered)
2317{
2318 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2319 if (!filtered)
2320 hists->stats.nr_non_filtered_samples++;
2321}
2322
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002323static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2324 struct hist_entry *pair)
2325{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002326 struct rb_root_cached *root;
Namhyung Kimce74f602012-12-10 17:29:55 +09002327 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002328 struct rb_node *parent = NULL;
2329 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07002330 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002331 bool leftmost = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002332
Jiri Olsa52225032016-05-03 13:54:42 +02002333 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002334 root = &hists->entries_collapsed;
2335 else
2336 root = hists->entries_in;
2337
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002338 p = &root->rb_root.rb_node;
Namhyung Kimce74f602012-12-10 17:29:55 +09002339
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002340 while (*p != NULL) {
2341 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09002342 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002343
Namhyung Kimce74f602012-12-10 17:29:55 +09002344 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002345
2346 if (!cmp)
2347 goto out;
2348
2349 if (cmp < 0)
2350 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002351 else {
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002352 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002353 leftmost = false;
2354 }
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002355 }
2356
Namhyung Kima0b51af2012-09-11 13:34:27 +09002357 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002358 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03002359 memset(&he->stat, 0, sizeof(he->stat));
2360 he->hists = hists;
Kan Liang09623d72016-04-24 23:28:09 -07002361 if (symbol_conf.cumulate_callchain)
2362 memset(he->stat_acc, 0, sizeof(he->stat));
Namhyung Kimce74f602012-12-10 17:29:55 +09002363 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002364 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kim62638352014-04-24 16:21:46 +09002365 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01002366 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002367 }
2368out:
2369 return he;
2370}
2371
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002372static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002373 struct rb_root_cached *root,
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002374 struct hist_entry *pair)
2375{
2376 struct rb_node **p;
2377 struct rb_node *parent = NULL;
2378 struct hist_entry *he;
2379 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002380 bool leftmost = true;
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002381
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002382 p = &root->rb_root.rb_node;
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002383 while (*p != NULL) {
2384 int64_t cmp = 0;
2385
2386 parent = *p;
2387 he = rb_entry(parent, struct hist_entry, rb_node_in);
2388
2389 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2390 cmp = fmt->collapse(fmt, he, pair);
2391 if (cmp)
2392 break;
2393 }
2394 if (!cmp)
2395 goto out;
2396
2397 if (cmp < 0)
2398 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002399 else {
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002400 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002401 leftmost = false;
2402 }
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002403 }
2404
2405 he = hist_entry__new(pair, true);
2406 if (he) {
2407 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002408 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002409
2410 he->dummy = true;
2411 he->hists = hists;
2412 memset(&he->stat, 0, sizeof(he->stat));
2413 hists__inc_stats(hists, he);
2414 }
2415out:
2416 return he;
2417}
2418
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002419static struct hist_entry *hists__find_entry(struct hists *hists,
2420 struct hist_entry *he)
2421{
Namhyung Kimce74f602012-12-10 17:29:55 +09002422 struct rb_node *n;
2423
Jiri Olsa52225032016-05-03 13:54:42 +02002424 if (hists__has(hists, need_collapse))
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002425 n = hists->entries_collapsed.rb_root.rb_node;
Namhyung Kimce74f602012-12-10 17:29:55 +09002426 else
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002427 n = hists->entries_in->rb_root.rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002428
2429 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002430 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2431 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002432
2433 if (cmp < 0)
2434 n = n->rb_left;
2435 else if (cmp > 0)
2436 n = n->rb_right;
2437 else
2438 return iter;
2439 }
2440
2441 return NULL;
2442}
2443
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002444static struct hist_entry *hists__find_hierarchy_entry(struct rb_root_cached *root,
Namhyung Kim09034de2016-09-13 16:45:46 +09002445 struct hist_entry *he)
2446{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002447 struct rb_node *n = root->rb_root.rb_node;
Namhyung Kim09034de2016-09-13 16:45:46 +09002448
2449 while (n) {
2450 struct hist_entry *iter;
2451 struct perf_hpp_fmt *fmt;
2452 int64_t cmp = 0;
2453
2454 iter = rb_entry(n, struct hist_entry, rb_node_in);
2455 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2456 cmp = fmt->collapse(fmt, iter, he);
2457 if (cmp)
2458 break;
2459 }
2460
2461 if (cmp < 0)
2462 n = n->rb_left;
2463 else if (cmp > 0)
2464 n = n->rb_right;
2465 else
2466 return iter;
2467 }
2468
2469 return NULL;
2470}
2471
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002472static void hists__match_hierarchy(struct rb_root_cached *leader_root,
2473 struct rb_root_cached *other_root)
Namhyung Kim09034de2016-09-13 16:45:46 +09002474{
2475 struct rb_node *nd;
2476 struct hist_entry *pos, *pair;
2477
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002478 for (nd = rb_first_cached(leader_root); nd; nd = rb_next(nd)) {
Namhyung Kim09034de2016-09-13 16:45:46 +09002479 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2480 pair = hists__find_hierarchy_entry(other_root, pos);
2481
2482 if (pair) {
2483 hist_entry__add_pair(pair, pos);
2484 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2485 }
2486 }
2487}
2488
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002489/*
2490 * Look for pairs to link to the leader buckets (hist_entries):
2491 */
2492void hists__match(struct hists *leader, struct hists *other)
2493{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002494 struct rb_root_cached *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002495 struct rb_node *nd;
Namhyung Kimbe5863b2019-08-28 08:15:55 +09002496 struct hist_entry *pos, *pair;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002497
Namhyung Kim09034de2016-09-13 16:45:46 +09002498 if (symbol_conf.report_hierarchy) {
2499 /* hierarchy report always collapses entries */
2500 return hists__match_hierarchy(&leader->entries_collapsed,
2501 &other->entries_collapsed);
2502 }
2503
Jiri Olsa52225032016-05-03 13:54:42 +02002504 if (hists__has(leader, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002505 root = &leader->entries_collapsed;
2506 else
2507 root = leader->entries_in;
2508
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002509 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002510 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002511 pair = hists__find_entry(other, pos);
2512
Namhyung Kimbe5863b2019-08-28 08:15:55 +09002513 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09002514 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002515 }
2516}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002517
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002518static int hists__link_hierarchy(struct hists *leader_hists,
2519 struct hist_entry *parent,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002520 struct rb_root_cached *leader_root,
2521 struct rb_root_cached *other_root)
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002522{
2523 struct rb_node *nd;
2524 struct hist_entry *pos, *leader;
2525
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002526 for (nd = rb_first_cached(other_root); nd; nd = rb_next(nd)) {
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002527 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2528
2529 if (hist_entry__has_pairs(pos)) {
2530 bool found = false;
2531
2532 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2533 if (leader->hists == leader_hists) {
2534 found = true;
2535 break;
2536 }
2537 }
2538 if (!found)
2539 return -1;
2540 } else {
2541 leader = add_dummy_hierarchy_entry(leader_hists,
2542 leader_root, pos);
2543 if (leader == NULL)
2544 return -1;
2545
2546 /* do not point parent in the pos */
2547 leader->parent_he = parent;
2548
2549 hist_entry__add_pair(pos, leader);
2550 }
2551
2552 if (!pos->leaf) {
2553 if (hists__link_hierarchy(leader_hists, leader,
2554 &leader->hroot_in,
2555 &pos->hroot_in) < 0)
2556 return -1;
2557 }
2558 }
2559 return 0;
2560}
2561
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002562/*
2563 * Look for entries in the other hists that are not present in the leader, if
2564 * we find them, just add a dummy entry on the leader hists, with period=0,
2565 * nr_events=0, to serve as the list header.
2566 */
2567int hists__link(struct hists *leader, struct hists *other)
2568{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002569 struct rb_root_cached *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002570 struct rb_node *nd;
2571 struct hist_entry *pos, *pair;
2572
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002573 if (symbol_conf.report_hierarchy) {
2574 /* hierarchy report always collapses entries */
2575 return hists__link_hierarchy(leader, NULL,
2576 &leader->entries_collapsed,
2577 &other->entries_collapsed);
2578 }
2579
Jiri Olsa52225032016-05-03 13:54:42 +02002580 if (hists__has(other, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002581 root = &other->entries_collapsed;
2582 else
2583 root = other->entries_in;
2584
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002585 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002586 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002587
2588 if (!hist_entry__has_pairs(pos)) {
2589 pair = hists__add_dummy_entry(leader, pos);
2590 if (pair == NULL)
2591 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09002592 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002593 }
2594 }
2595
2596 return 0;
2597}
Namhyung Kimf2148332014-01-14 11:52:48 +09002598
Namhyung Kimbe5863b2019-08-28 08:15:55 +09002599int hists__unlink(struct hists *hists)
2600{
2601 struct rb_root_cached *root;
2602 struct rb_node *nd;
2603 struct hist_entry *pos;
2604
2605 if (hists__has(hists, need_collapse))
2606 root = &hists->entries_collapsed;
2607 else
2608 root = hists->entries_in;
2609
2610 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2611 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2612 list_del_init(&pos->pairs.node);
2613 }
2614
2615 return 0;
2616}
2617
Andi Kleen578499982015-07-18 08:24:49 -07002618void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
Jin Yao7841f402019-11-07 15:47:15 +08002619 struct perf_sample *sample, bool nonany_branch_mode,
2620 u64 *total_cycles)
Andi Kleen578499982015-07-18 08:24:49 -07002621{
2622 struct branch_info *bi;
Kan Liang42bbabe2020-02-28 08:30:00 -08002623 struct branch_entry *entries = perf_sample__branch_entries(sample);
Andi Kleen578499982015-07-18 08:24:49 -07002624
2625 /* If we have branch cycles always annotate them. */
Kan Liang42bbabe2020-02-28 08:30:00 -08002626 if (bs && bs->nr && entries[0].flags.cycles) {
Andi Kleen578499982015-07-18 08:24:49 -07002627 int i;
2628
2629 bi = sample__resolve_bstack(sample, al);
2630 if (bi) {
2631 struct addr_map_symbol *prev = NULL;
2632
2633 /*
2634 * Ignore errors, still want to process the
2635 * other entries.
2636 *
2637 * For non standard branch modes always
2638 * force no IPC (prev == NULL)
2639 *
2640 * Note that perf stores branches reversed from
2641 * program order!
2642 */
2643 for (i = bs->nr - 1; i >= 0; i--) {
2644 addr_map_symbol__account_cycles(&bi[i].from,
2645 nonany_branch_mode ? NULL : prev,
2646 bi[i].flags.cycles);
2647 prev = &bi[i].to;
Jin Yao7841f402019-11-07 15:47:15 +08002648
2649 if (total_cycles)
2650 *total_cycles += bi[i].flags.cycles;
Andi Kleen578499982015-07-18 08:24:49 -07002651 }
2652 free(bi);
2653 }
2654 }
2655}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002656
Jiri Olsa63503db2019-07-21 13:23:52 +02002657size_t perf_evlist__fprintf_nr_events(struct evlist *evlist, FILE *fp)
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002658{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002659 struct evsel *pos;
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002660 size_t ret = 0;
2661
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002662 evlist__for_each_entry(evlist, pos) {
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03002663 ret += fprintf(fp, "%s stats:\n", evsel__name(pos));
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002664 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2665 }
2666
2667 return ret;
2668}
2669
2670
Namhyung Kimf2148332014-01-14 11:52:48 +09002671u64 hists__total_period(struct hists *hists)
2672{
2673 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2674 hists->stats.total_period;
2675}
Namhyung Kim33db4562014-02-07 12:06:07 +09002676
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002677int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq)
2678{
2679 char unit;
2680 int printed;
2681 const struct dso *dso = hists->dso_filter;
Namhyung Kim7cb10a082019-05-27 15:11:49 +09002682 struct thread *thread = hists->thread_filter;
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002683 int socket_id = hists->socket_filter;
2684 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
2685 u64 nr_events = hists->stats.total_period;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002686 struct evsel *evsel = hists_to_evsel(hists);
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03002687 const char *ev_name = evsel__name(evsel);
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002688 char buf[512], sample_freq_str[64] = "";
2689 size_t buflen = sizeof(buf);
2690 char ref[30] = " show reference callgraph, ";
2691 bool enable_ref = false;
2692
2693 if (symbol_conf.filter_relative) {
2694 nr_samples = hists->stats.nr_non_filtered_samples;
2695 nr_events = hists->stats.total_non_filtered_period;
2696 }
2697
2698 if (perf_evsel__is_group_event(evsel)) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002699 struct evsel *pos;
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002700
2701 perf_evsel__group_desc(evsel, buf, buflen);
2702 ev_name = buf;
2703
2704 for_each_group_member(pos, evsel) {
2705 struct hists *pos_hists = evsel__hists(pos);
2706
2707 if (symbol_conf.filter_relative) {
2708 nr_samples += pos_hists->stats.nr_non_filtered_samples;
2709 nr_events += pos_hists->stats.total_non_filtered_period;
2710 } else {
2711 nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
2712 nr_events += pos_hists->stats.total_period;
2713 }
2714 }
2715 }
2716
2717 if (symbol_conf.show_ref_callgraph &&
2718 strstr(ev_name, "call-graph=no"))
2719 enable_ref = true;
2720
2721 if (show_freq)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002722 scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->core.attr.sample_freq);
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002723
2724 nr_samples = convert_unit(nr_samples, &unit);
2725 printed = scnprintf(bf, size,
2726 "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64,
Jiri Olsa5643b1a2019-07-21 13:24:46 +02002727 nr_samples, unit, evsel->core.nr_members > 1 ? "s" : "",
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002728 ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events);
2729
2730
2731 if (hists->uid_filter_str)
2732 printed += snprintf(bf + printed, size - printed,
2733 ", UID: %s", hists->uid_filter_str);
2734 if (thread) {
2735 if (hists__has(hists, thread)) {
2736 printed += scnprintf(bf + printed, size - printed,
2737 ", Thread: %s(%d)",
2738 (thread->comm_set ? thread__comm_str(thread) : ""),
2739 thread->tid);
2740 } else {
2741 printed += scnprintf(bf + printed, size - printed,
2742 ", Thread: %s",
2743 (thread->comm_set ? thread__comm_str(thread) : ""));
2744 }
2745 }
2746 if (dso)
2747 printed += scnprintf(bf + printed, size - printed,
2748 ", DSO: %s", dso->short_name);
2749 if (socket_id > -1)
2750 printed += scnprintf(bf + printed, size - printed,
2751 ", Processor Socket: %d", socket_id);
2752
2753 return printed;
2754}
2755
Namhyung Kim33db4562014-02-07 12:06:07 +09002756int parse_filter_percentage(const struct option *opt __maybe_unused,
2757 const char *arg, int unset __maybe_unused)
2758{
2759 if (!strcmp(arg, "relative"))
2760 symbol_conf.filter_relative = true;
2761 else if (!strcmp(arg, "absolute"))
2762 symbol_conf.filter_relative = false;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002763 else {
Colin Ian Kinga596a872017-03-30 10:54:40 +01002764 pr_debug("Invalid percentage: %s\n", arg);
Namhyung Kim33db4562014-02-07 12:06:07 +09002765 return -1;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002766 }
Namhyung Kim33db4562014-02-07 12:06:07 +09002767
2768 return 0;
2769}
Namhyung Kim0b93da12014-01-14 12:02:15 +09002770
2771int perf_hist_config(const char *var, const char *value)
2772{
2773 if (!strcmp(var, "hist.percentage"))
2774 return parse_filter_percentage(NULL, value, 0);
2775
2776 return 0;
2777}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002778
Jiri Olsa5b658552016-01-18 10:24:22 +01002779int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002780{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002781 memset(hists, 0, sizeof(*hists));
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002782 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT_CACHED;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002783 hists->entries_in = &hists->entries_in_array[0];
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002784 hists->entries_collapsed = RB_ROOT_CACHED;
2785 hists->entries = RB_ROOT_CACHED;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002786 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04002787 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01002788 hists->hpp_list = hpp_list;
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002789 INIT_LIST_HEAD(&hists->hpp_formats);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002790 return 0;
2791}
2792
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002793static void hists__delete_remaining_entries(struct rb_root_cached *root)
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002794{
2795 struct rb_node *node;
2796 struct hist_entry *he;
2797
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002798 while (!RB_EMPTY_ROOT(&root->rb_root)) {
2799 node = rb_first_cached(root);
2800 rb_erase_cached(node, root);
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002801
2802 he = rb_entry(node, struct hist_entry, rb_node_in);
2803 hist_entry__delete(he);
2804 }
2805}
2806
2807static void hists__delete_all_entries(struct hists *hists)
2808{
2809 hists__delete_entries(hists);
2810 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2811 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2812 hists__delete_remaining_entries(&hists->entries_collapsed);
2813}
2814
Jiri Olsa32dcd022019-07-21 13:23:51 +02002815static void hists_evsel__exit(struct evsel *evsel)
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002816{
2817 struct hists *hists = evsel__hists(evsel);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002818 struct perf_hpp_fmt *fmt, *pos;
2819 struct perf_hpp_list_node *node, *tmp;
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002820
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002821 hists__delete_all_entries(hists);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002822
2823 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2824 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03002825 list_del_init(&fmt->list);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002826 free(fmt);
2827 }
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03002828 list_del_init(&node->list);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002829 free(node);
2830 }
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002831}
2832
Jiri Olsa32dcd022019-07-21 13:23:51 +02002833static int hists_evsel__init(struct evsel *evsel)
Namhyung Kimfc284be2016-01-07 10:14:10 +01002834{
2835 struct hists *hists = evsel__hists(evsel);
2836
Jiri Olsa5b658552016-01-18 10:24:22 +01002837 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01002838 return 0;
2839}
2840
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002841/*
2842 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2843 * stored in the rbtree...
2844 */
2845
2846int hists__init(void)
2847{
2848 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002849 hists_evsel__init,
2850 hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002851 if (err)
2852 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2853
2854 return err;
2855}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01002856
2857void perf_hpp_list__init(struct perf_hpp_list *list)
2858{
2859 INIT_LIST_HEAD(&list->fields);
2860 INIT_LIST_HEAD(&list->sorts);
2861}