blob: 65fe65ba03c257bf85049b1eadc0bb6cba3d080d [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
Kan Lianga50d03e2020-12-16 10:57:58 -0800191 hists__new_col_len(hists, HISTC_MEM_DATA_PAGE_SIZE,
192 unresolved_col_width + 4 + 2);
193
Stephane Eranian98a3b322013-01-24 16:10:35 +0100194 } else {
195 symlen = unresolved_col_width + 4 + 2;
196 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
Jiri Olsab34b3bf2015-10-05 20:06:08 +0200197 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100198 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
199 }
200
Namhyung Kimb629f3e2020-03-25 21:45:32 +0900201 hists__new_col_len(hists, HISTC_CGROUP, 6);
Hari Bathinid890a982017-03-08 02:12:13 +0530202 hists__new_col_len(hists, HISTC_CGROUP_ID, 20);
Arnaldo Carvalho de Meloa4978ec2015-09-09 12:14:00 -0300203 hists__new_col_len(hists, HISTC_CPU, 3);
Kan Liang2e7ea3a2015-09-04 10:45:43 -0400204 hists__new_col_len(hists, HISTC_SOCKET, 6);
Stephane Eranian98a3b322013-01-24 16:10:35 +0100205 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
206 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
207 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
208 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
209 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
210 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
Kan Lianga054c292021-02-02 12:09:07 -0800211 hists__new_col_len(hists, HISTC_MEM_BLOCKED, 10);
Kan Liang590db422021-02-02 12:09:10 -0800212 hists__new_col_len(hists, HISTC_LOCAL_INS_LAT, 13);
213 hists__new_col_len(hists, HISTC_GLOBAL_INS_LAT, 13);
Athira Rajeev06e5ca72021-03-22 10:57:26 -0400214 hists__new_col_len(hists, HISTC_P_STAGE_CYC, 13);
Andi Kleen3dab6ac2019-08-23 14:03:38 -0700215 if (symbol_conf.nanosecs)
216 hists__new_col_len(hists, HISTC_TIME, 16);
217 else
218 hists__new_col_len(hists, HISTC_TIME, 12);
Stephane Eranian9fd74f22021-01-05 11:57:51 -0800219 hists__new_col_len(hists, HISTC_CODE_PAGE_SIZE, 6);
Andi Kleen475eeab2013-09-20 07:40:43 -0700220
Jiri Olsaf666ac02016-09-19 15:10:10 +0200221 if (h->srcline) {
222 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
223 hists__new_col_len(hists, HISTC_SRCLINE, len);
224 }
Arnaldo Carvalho de Meloe8e6d372015-08-10 16:53:54 -0300225
Andi Kleen31191a82015-08-07 15:54:24 -0700226 if (h->srcfile)
227 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
228
Andi Kleen475eeab2013-09-20 07:40:43 -0700229 if (h->transaction)
230 hists__new_col_len(hists, HISTC_TRANSACTION,
231 hist_entry__transaction_len());
Namhyung Kim0c0af782016-02-21 23:22:38 +0900232
233 if (h->trace_output)
234 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
Namhyung Kimb629f3e2020-03-25 21:45:32 +0900235
236 if (h->cgroup) {
237 const char *cgrp_name = "unknown";
238 struct cgroup *cgrp = cgroup__find(h->ms.maps->machine->env,
239 h->cgroup);
240 if (cgrp != NULL)
241 cgrp_name = cgrp->name;
242
243 hists__new_col_len(hists, HISTC_CGROUP, strlen(cgrp_name));
244 }
Arnaldo Carvalho de Melo8a6c5b22010-07-20 14:42:52 -0300245}
246
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900247void hists__output_recalc_col_len(struct hists *hists, int max_rows)
248{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800249 struct rb_node *next = rb_first_cached(&hists->entries);
Namhyung Kim7ccf4f92012-08-20 13:52:05 +0900250 struct hist_entry *n;
251 int row = 0;
252
253 hists__reset_col_len(hists);
254
255 while (next && row++ < max_rows) {
256 n = rb_entry(next, struct hist_entry, rb_node);
257 if (!n->filtered)
258 hists__calc_col_len(hists, n);
259 next = rb_next(&n->rb_node);
260 }
261}
262
Namhyung Kimf39056f2014-01-14 14:25:37 +0900263static void he_stat__add_cpumode_period(struct he_stat *he_stat,
264 unsigned int cpumode, u64 period)
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800265{
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300266 switch (cpumode) {
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800267 case PERF_RECORD_MISC_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900268 he_stat->period_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800269 break;
270 case PERF_RECORD_MISC_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900271 he_stat->period_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800272 break;
273 case PERF_RECORD_MISC_GUEST_KERNEL:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900274 he_stat->period_guest_sys += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800275 break;
276 case PERF_RECORD_MISC_GUEST_USER:
Namhyung Kimf39056f2014-01-14 14:25:37 +0900277 he_stat->period_guest_us += period;
Zhang, Yanmina1645ce2010-04-19 13:32:50 +0800278 break;
279 default:
280 break;
281 }
282}
283
Andi Kleen37239082019-03-11 07:44:54 -0700284static long hist_time(unsigned long htime)
285{
286 unsigned long time_quantum = symbol_conf.time_quantum;
287 if (time_quantum)
288 return (htime / time_quantum) * time_quantum;
289 return htime;
290}
291
Andi Kleen05484292013-01-24 16:10:29 +0100292static void he_stat__add_period(struct he_stat *he_stat, u64 period,
Athira Rajeev06e5ca72021-03-22 10:57:26 -0400293 u64 weight, u64 ins_lat, u64 p_stage_cyc)
Namhyung Kim139c0812012-10-04 21:49:43 +0900294{
Stephane Eranian98a3b322013-01-24 16:10:35 +0100295
Namhyung Kim139c0812012-10-04 21:49:43 +0900296 he_stat->period += period;
Andi Kleen05484292013-01-24 16:10:29 +0100297 he_stat->weight += weight;
Namhyung Kim139c0812012-10-04 21:49:43 +0900298 he_stat->nr_events += 1;
Kan Liang590db422021-02-02 12:09:10 -0800299 he_stat->ins_lat += ins_lat;
Athira Rajeev06e5ca72021-03-22 10:57:26 -0400300 he_stat->p_stage_cyc += p_stage_cyc;
Namhyung Kim139c0812012-10-04 21:49:43 +0900301}
302
303static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
304{
305 dest->period += src->period;
306 dest->period_sys += src->period_sys;
307 dest->period_us += src->period_us;
308 dest->period_guest_sys += src->period_guest_sys;
309 dest->period_guest_us += src->period_guest_us;
310 dest->nr_events += src->nr_events;
Andi Kleen05484292013-01-24 16:10:29 +0100311 dest->weight += src->weight;
Kan Liang590db422021-02-02 12:09:10 -0800312 dest->ins_lat += src->ins_lat;
Athira Rajeev06e5ca72021-03-22 10:57:26 -0400313 dest->p_stage_cyc += src->p_stage_cyc;
Namhyung Kim139c0812012-10-04 21:49:43 +0900314}
315
Namhyung Kimf39056f2014-01-14 14:25:37 +0900316static void he_stat__decay(struct he_stat *he_stat)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300317{
Namhyung Kimf39056f2014-01-14 14:25:37 +0900318 he_stat->period = (he_stat->period * 7) / 8;
319 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
Andi Kleen05484292013-01-24 16:10:29 +0100320 /* XXX need decay for weight too? */
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300321}
322
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900323static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
324
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300325static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
326{
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900327 u64 prev_period = he->stat.period;
Namhyung Kim3186b682014-04-22 13:44:23 +0900328 u64 diff;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200329
330 if (prev_period == 0)
Arnaldo Carvalho de Melodf71d952011-10-13 08:01:33 -0300331 return true;
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200332
Namhyung Kimf39056f2014-01-14 14:25:37 +0900333 he_stat__decay(&he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900334 if (symbol_conf.cumulate_callchain)
335 he_stat__decay(he->stat_acc);
Namhyung Kim42b276a2016-01-05 12:06:00 +0900336 decay_callchain(he->callchain);
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200337
Namhyung Kim3186b682014-04-22 13:44:23 +0900338 diff = prev_period - he->stat.period;
339
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900340 if (!he->depth) {
341 hists->stats.total_period -= diff;
342 if (!he->filtered)
343 hists->stats.total_non_filtered_period -= diff;
344 }
345
346 if (!he->leaf) {
347 struct hist_entry *child;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800348 struct rb_node *node = rb_first_cached(&he->hroot_out);
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900349 while (node) {
350 child = rb_entry(node, struct hist_entry, rb_node);
351 node = rb_next(node);
352
353 if (hists__decay_entry(hists, child))
354 hists__delete_entry(hists, child);
355 }
356 }
Arnaldo Carvalho de Meloc64550c2011-10-20 06:45:44 -0200357
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900358 return he->stat.period == 0;
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300359}
360
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300361static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
362{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800363 struct rb_root_cached *root_in;
364 struct rb_root_cached *root_out;
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300365
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900366 if (he->parent_he) {
367 root_in = &he->parent_he->hroot_in;
368 root_out = &he->parent_he->hroot_out;
369 } else {
Jiri Olsa52225032016-05-03 13:54:42 +0200370 if (hists__has(hists, need_collapse))
Namhyung Kim5d8200a2016-02-25 00:13:49 +0900371 root_in = &hists->entries_collapsed;
372 else
373 root_in = hists->entries_in;
374 root_out = &hists->entries;
375 }
376
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800377 rb_erase_cached(&he->rb_node_in, root_in);
378 rb_erase_cached(&he->rb_node, root_out);
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300379
380 --hists->nr_entries;
381 if (!he->filtered)
382 --hists->nr_non_filtered_entries;
383
384 hist_entry__delete(he);
385}
386
Namhyung Kim3a5714f2013-05-14 11:09:01 +0900387void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300388{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800389 struct rb_node *next = rb_first_cached(&hists->entries);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300390 struct hist_entry *n;
391
392 while (next) {
393 n = rb_entry(next, struct hist_entry, rb_node);
394 next = rb_next(&n->rb_node);
Arnaldo Carvalho de Melob079d4e2011-10-17 09:05:04 -0200395 if (((zap_user && n->level == '.') ||
396 (zap_kernel && n->level != '.') ||
Arnaldo Carvalho de Melo4c47f4f2015-03-17 17:18:58 -0300397 hists__decay_entry(hists, n))) {
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300398 hists__delete_entry(hists, n);
Arnaldo Carvalho de Meloab81f3fd2011-10-05 19:16:15 -0300399 }
400 }
401}
402
Namhyung Kim701937b2014-08-12 17:16:05 +0900403void hists__delete_entries(struct hists *hists)
404{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800405 struct rb_node *next = rb_first_cached(&hists->entries);
Namhyung Kim701937b2014-08-12 17:16:05 +0900406 struct hist_entry *n;
407
408 while (next) {
409 n = rb_entry(next, struct hist_entry, rb_node);
410 next = rb_next(&n->rb_node);
411
Arnaldo Carvalho de Melo956b65e2014-12-19 12:41:28 -0300412 hists__delete_entry(hists, n);
Namhyung Kim701937b2014-08-12 17:16:05 +0900413 }
414}
415
Jin Yaob10c78c2019-06-28 17:23:03 +0800416struct hist_entry *hists__get_entry(struct hists *hists, int idx)
417{
418 struct rb_node *next = rb_first_cached(&hists->entries);
419 struct hist_entry *n;
420 int i = 0;
421
422 while (next) {
423 n = rb_entry(next, struct hist_entry, rb_node);
424 if (i == idx)
425 return n;
426
427 next = rb_next(&n->rb_node);
428 i++;
429 }
430
431 return NULL;
432}
433
John Kacur3d1d07e2009-09-28 15:32:55 +0200434/*
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300435 * histogram, sorted on item, collects periods
John Kacur3d1d07e2009-09-28 15:32:55 +0200436 */
437
Jiri Olsa0a269a62016-07-05 08:56:03 +0200438static int hist_entry__init(struct hist_entry *he,
439 struct hist_entry *template,
Arnaldo Carvalho de Melo41477ac2018-06-07 14:19:54 -0300440 bool sample_self,
441 size_t callchain_size)
Jiri Olsa0a269a62016-07-05 08:56:03 +0200442{
443 *he = *template;
Arnaldo Carvalho de Melo41477ac2018-06-07 14:19:54 -0300444 he->callchain_size = callchain_size;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200445
446 if (symbol_conf.cumulate_callchain) {
447 he->stat_acc = malloc(sizeof(he->stat));
448 if (he->stat_acc == NULL)
449 return -ENOMEM;
450 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
451 if (!sample_self)
452 memset(&he->stat, 0, sizeof(he->stat));
453 }
454
455 map__get(he->ms.map);
456
457 if (he->branch_info) {
458 /*
459 * This branch info is (a part of) allocated from
460 * sample__resolve_bstack() and will be freed after
461 * adding new entries. So we need to save a copy.
462 */
463 he->branch_info = malloc(sizeof(*he->branch_info));
Jiri Olsac5758912019-03-05 16:25:30 +0100464 if (he->branch_info == NULL)
465 goto err;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200466
467 memcpy(he->branch_info, template->branch_info,
468 sizeof(*he->branch_info));
469
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300470 map__get(he->branch_info->from.ms.map);
471 map__get(he->branch_info->to.ms.map);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200472 }
473
474 if (he->mem_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300475 map__get(he->mem_info->iaddr.ms.map);
476 map__get(he->mem_info->daddr.ms.map);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200477 }
478
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -0300479 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
Jiri Olsa0a269a62016-07-05 08:56:03 +0200480 callchain_init(he->callchain);
481
482 if (he->raw_data) {
483 he->raw_data = memdup(he->raw_data, he->raw_size);
Jiri Olsac5758912019-03-05 16:25:30 +0100484 if (he->raw_data == NULL)
485 goto err_infos;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200486 }
Jiri Olsa26349582019-03-05 16:25:31 +0100487
488 if (he->srcline) {
489 he->srcline = strdup(he->srcline);
490 if (he->srcline == NULL)
491 goto err_rawdata;
492 }
493
Andi Kleen4968ac82019-03-11 07:44:58 -0700494 if (symbol_conf.res_sample) {
495 he->res_samples = calloc(sizeof(struct res_sample),
496 symbol_conf.res_sample);
497 if (!he->res_samples)
498 goto err_srcline;
499 }
500
Jiri Olsa0a269a62016-07-05 08:56:03 +0200501 INIT_LIST_HEAD(&he->pairs.node);
502 thread__get(he->thread);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800503 he->hroot_in = RB_ROOT_CACHED;
504 he->hroot_out = RB_ROOT_CACHED;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200505
506 if (!symbol_conf.report_hierarchy)
507 he->leaf = true;
508
509 return 0;
Jiri Olsac5758912019-03-05 16:25:30 +0100510
Andi Kleen4968ac82019-03-11 07:44:58 -0700511err_srcline:
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300512 zfree(&he->srcline);
Andi Kleen4968ac82019-03-11 07:44:58 -0700513
Jiri Olsa26349582019-03-05 16:25:31 +0100514err_rawdata:
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300515 zfree(&he->raw_data);
Jiri Olsa26349582019-03-05 16:25:31 +0100516
Jiri Olsac5758912019-03-05 16:25:30 +0100517err_infos:
518 if (he->branch_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300519 map__put(he->branch_info->from.ms.map);
520 map__put(he->branch_info->to.ms.map);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300521 zfree(&he->branch_info);
Jiri Olsac5758912019-03-05 16:25:30 +0100522 }
523 if (he->mem_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300524 map__put(he->mem_info->iaddr.ms.map);
525 map__put(he->mem_info->daddr.ms.map);
Jiri Olsac5758912019-03-05 16:25:30 +0100526 }
527err:
528 map__zput(he->ms.map);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -0300529 zfree(&he->stat_acc);
Jiri Olsac5758912019-03-05 16:25:30 +0100530 return -ENOMEM;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200531}
532
Jiri Olsaf542e762016-07-05 08:56:04 +0200533static void *hist_entry__zalloc(size_t size)
534{
535 return zalloc(size + sizeof(struct hist_entry));
536}
537
538static void hist_entry__free(void *ptr)
539{
540 free(ptr);
541}
542
543static struct hist_entry_ops default_ops = {
544 .new = hist_entry__zalloc,
545 .free = hist_entry__free,
546};
547
Namhyung Kima0b51af2012-09-11 13:34:27 +0900548static struct hist_entry *hist_entry__new(struct hist_entry *template,
549 bool sample_self)
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300550{
Jiri Olsaf542e762016-07-05 08:56:04 +0200551 struct hist_entry_ops *ops = template->ops;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900552 size_t callchain_size = 0;
553 struct hist_entry *he;
Jiri Olsa0a269a62016-07-05 08:56:03 +0200554 int err = 0;
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900555
Jiri Olsaf542e762016-07-05 08:56:04 +0200556 if (!ops)
557 ops = template->ops = &default_ops;
558
Namhyung Kim82aa0192014-12-22 13:44:14 +0900559 if (symbol_conf.use_callchain)
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900560 callchain_size = sizeof(struct callchain_root);
561
Jiri Olsaf542e762016-07-05 08:56:04 +0200562 he = ops->new(callchain_size);
Jiri Olsa0a269a62016-07-05 08:56:03 +0200563 if (he) {
Arnaldo Carvalho de Melo41477ac2018-06-07 14:19:54 -0300564 err = hist_entry__init(he, template, sample_self, callchain_size);
Jiri Olsaf542e762016-07-05 08:56:04 +0200565 if (err) {
566 ops->free(he);
567 he = NULL;
568 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300569 }
570
Arnaldo Carvalho de Melo12c14272012-01-04 12:27:03 -0200571 return he;
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300572}
573
Arnaldo Carvalho de Melo7a007ca2010-07-21 09:19:41 -0300574static u8 symbol__parent_filter(const struct symbol *parent)
575{
576 if (symbol_conf.exclude_other && parent == NULL)
577 return 1 << HIST_FILTER__PARENT;
578 return 0;
579}
580
Namhyung Kim467ef102016-02-16 23:08:19 +0900581static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
582{
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -0300583 if (!hist_entry__has_callchains(he) || !symbol_conf.use_callchain)
Namhyung Kim467ef102016-02-16 23:08:19 +0900584 return;
585
586 he->hists->callchain_period += period;
587 if (!he->filtered)
588 he->hists->callchain_non_filtered_period += period;
589}
590
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300591static struct hist_entry *hists__findnew_entry(struct hists *hists,
592 struct hist_entry *entry,
593 struct addr_location *al,
594 bool sample_self)
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300595{
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300596 struct rb_node **p;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300597 struct rb_node *parent = NULL;
598 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -0700599 int64_t cmp;
Namhyung Kimf1cbf782013-12-18 14:21:11 +0900600 u64 period = entry->stat.period;
601 u64 weight = entry->stat.weight;
Kan Liang590db422021-02-02 12:09:10 -0800602 u64 ins_lat = entry->stat.ins_lat;
Athira Rajeev06e5ca72021-03-22 10:57:26 -0400603 u64 p_stage_cyc = entry->stat.p_stage_cyc;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800604 bool leftmost = true;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300605
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800606 p = &hists->entries_in->rb_root.rb_node;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300607
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300608 while (*p != NULL) {
609 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300610 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300611
Namhyung Kim9afcf932012-12-10 17:29:54 +0900612 /*
613 * Make sure that it receives arguments in a same order as
614 * hist_entry__collapse() so that we can use an appropriate
615 * function when searching an entry regardless which sort
616 * keys were used.
617 */
618 cmp = hist_entry__cmp(he, entry);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300619
620 if (!cmp) {
Namhyung Kim0f584742016-01-28 00:40:49 +0900621 if (sample_self) {
Athira Rajeev06e5ca72021-03-22 10:57:26 -0400622 he_stat__add_period(&he->stat, period, weight, ins_lat, p_stage_cyc);
Namhyung Kim467ef102016-02-16 23:08:19 +0900623 hist_entry__add_callchain_period(he, period);
Namhyung Kim0f584742016-01-28 00:40:49 +0900624 }
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900625 if (symbol_conf.cumulate_callchain)
Athira Rajeev06e5ca72021-03-22 10:57:26 -0400626 he_stat__add_period(he->stat_acc, period, weight, ins_lat, p_stage_cyc);
David Miller63fa4712012-03-27 03:14:18 -0400627
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900628 /*
Arnaldo Carvalho de Meloe80faac2014-01-22 13:05:06 -0300629 * This mem info was allocated from sample__resolve_mem
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900630 * and will not be used anymore.
631 */
Jiri Olsa9f874982018-03-07 16:50:06 +0100632 mem_info__zput(entry->mem_info);
Namhyung Kimceb2acb2013-04-01 20:35:18 +0900633
Jin Yaofe962452019-06-28 17:22:59 +0800634 block_info__zput(entry->block_info);
635
David Miller63fa4712012-03-27 03:14:18 -0400636 /* If the map of an existing hist_entry has
637 * become out-of-date due to an exec() or
638 * similar, update it. Otherwise we will
639 * mis-adjust symbol addresses when computing
640 * the history counter to increment.
641 */
642 if (he->ms.map != entry->ms.map) {
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -0300643 map__put(he->ms.map);
644 he->ms.map = map__get(entry->ms.map);
David Miller63fa4712012-03-27 03:14:18 -0400645 }
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300646 goto out;
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300647 }
648
649 if (cmp < 0)
650 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800651 else {
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300652 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800653 leftmost = false;
654 }
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300655 }
656
Namhyung Kima0b51af2012-09-11 13:34:27 +0900657 he = hist_entry__new(entry, sample_self);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300658 if (!he)
Namhyung Kim27a0dcb2013-05-14 11:09:02 +0900659 return NULL;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300660
Namhyung Kim0f584742016-01-28 00:40:49 +0900661 if (sample_self)
Namhyung Kim467ef102016-02-16 23:08:19 +0900662 hist_entry__add_callchain_period(he, period);
663 hists->nr_entries++;
Namhyung Kim590cd342014-12-22 13:44:09 +0900664
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300665 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -0800666 rb_insert_color_cached(&he->rb_node_in, hists->entries_in, leftmost);
Arnaldo Carvalho de Melo28e2a102010-05-09 13:02:23 -0300667out:
Namhyung Kima0b51af2012-09-11 13:34:27 +0900668 if (sample_self)
669 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
Namhyung Kimf8be1c82012-09-11 13:15:07 +0900670 if (symbol_conf.cumulate_callchain)
671 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300672 return he;
673}
674
Andi Kleen4968ac82019-03-11 07:44:58 -0700675static unsigned random_max(unsigned high)
676{
677 unsigned thresh = -high % high;
678 for (;;) {
679 unsigned r = random();
680 if (r >= thresh)
681 return r % high;
682 }
683}
684
685static void hists__res_sample(struct hist_entry *he, struct perf_sample *sample)
686{
687 struct res_sample *r;
688 int j;
689
690 if (he->num_res < symbol_conf.res_sample) {
691 j = he->num_res++;
692 } else {
693 j = random_max(symbol_conf.res_sample);
694 }
695 r = &he->res_samples[j];
696 r->time = sample->time;
697 r->cpu = sample->cpu;
698 r->tid = sample->tid;
699}
700
Jiri Olsaa5051972016-07-05 08:56:05 +0200701static struct hist_entry*
702__hists__add_entry(struct hists *hists,
703 struct addr_location *al,
704 struct symbol *sym_parent,
705 struct branch_info *bi,
706 struct mem_info *mi,
Jin Yaofe962452019-06-28 17:22:59 +0800707 struct block_info *block_info,
Jiri Olsaa5051972016-07-05 08:56:05 +0200708 struct perf_sample *sample,
709 bool sample_self,
710 struct hist_entry_ops *ops)
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100711{
Hari Bathinid890a982017-03-08 02:12:13 +0530712 struct namespaces *ns = thread__namespaces(al->thread);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100713 struct hist_entry entry = {
714 .thread = al->thread,
Namhyung Kim4dfced32013-09-13 16:28:57 +0900715 .comm = thread__comm(al->thread),
Hari Bathinid890a982017-03-08 02:12:13 +0530716 .cgroup_id = {
717 .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0,
718 .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0,
719 },
Namhyung Kimb629f3e2020-03-25 21:45:32 +0900720 .cgroup = sample->cgroup,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100721 .ms = {
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -0300722 .maps = al->maps,
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100723 .map = al->map,
724 .sym = al->sym,
725 },
Jiri Olsa26349582019-03-05 16:25:31 +0100726 .srcline = (char *) al->srcline,
Kan Liang0c4c4deb2015-09-04 10:45:42 -0400727 .socket = al->socket,
Don Zickus7365be52014-05-27 12:28:05 -0400728 .cpu = al->cpu,
729 .cpumode = al->cpumode,
730 .ip = al->addr,
731 .level = al->level,
Stephane Eranian9fd74f22021-01-05 11:57:51 -0800732 .code_page_size = sample->code_page_size,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900733 .stat = {
Namhyung Kimc4b35352012-10-04 21:49:42 +0900734 .nr_events = 1,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900735 .period = sample->period,
736 .weight = sample->weight,
Kan Liang590db422021-02-02 12:09:10 -0800737 .ins_lat = sample->ins_lat,
Athira Rajeev06e5ca72021-03-22 10:57:26 -0400738 .p_stage_cyc = sample->p_stage_cyc,
Namhyung Kimb24c28f2012-10-04 21:49:41 +0900739 },
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100740 .parent = sym_parent,
Namhyung Kim2c86c7c2014-03-17 18:18:54 -0300741 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300742 .hists = hists,
Namhyung Kim41a4e6e2013-10-31 15:56:03 +0900743 .branch_info = bi,
744 .mem_info = mi,
Jin Yaofe962452019-06-28 17:22:59 +0800745 .block_info = block_info,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900746 .transaction = sample->transaction,
Namhyung Kim72392832015-12-24 11:16:17 +0900747 .raw_data = sample->raw_data,
748 .raw_size = sample->raw_size,
Jiri Olsaa5051972016-07-05 08:56:05 +0200749 .ops = ops,
Andi Kleen37239082019-03-11 07:44:54 -0700750 .time = hist_time(sample->time),
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300751 }, *he = hists__findnew_entry(hists, &entry, al, sample_self);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100752
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300753 if (!hists->has_callchains && he && he->callchain_size != 0)
754 hists->has_callchains = true;
Andi Kleen4968ac82019-03-11 07:44:58 -0700755 if (he && symbol_conf.res_sample)
756 hists__res_sample(he, sample);
Arnaldo Carvalho de Meloc9d36622018-06-07 14:42:27 -0300757 return he;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100758}
759
Jiri Olsaa5051972016-07-05 08:56:05 +0200760struct hist_entry *hists__add_entry(struct hists *hists,
761 struct addr_location *al,
762 struct symbol *sym_parent,
763 struct branch_info *bi,
764 struct mem_info *mi,
765 struct perf_sample *sample,
766 bool sample_self)
767{
Jin Yaofe962452019-06-28 17:22:59 +0800768 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
Jiri Olsaa5051972016-07-05 08:56:05 +0200769 sample, sample_self, NULL);
770}
771
772struct hist_entry *hists__add_entry_ops(struct hists *hists,
773 struct hist_entry_ops *ops,
774 struct addr_location *al,
775 struct symbol *sym_parent,
776 struct branch_info *bi,
777 struct mem_info *mi,
778 struct perf_sample *sample,
779 bool sample_self)
780{
Jin Yaofe962452019-06-28 17:22:59 +0800781 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
Jiri Olsaa5051972016-07-05 08:56:05 +0200782 sample, sample_self, ops);
783}
784
Jin Yaofe962452019-06-28 17:22:59 +0800785struct hist_entry *hists__add_entry_block(struct hists *hists,
786 struct addr_location *al,
787 struct block_info *block_info)
788{
789 struct hist_entry entry = {
790 .block_info = block_info,
791 .hists = hists,
Jin Yaob65a7d32019-11-07 15:47:16 +0800792 .ms = {
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -0300793 .maps = al->maps,
Jin Yaob65a7d32019-11-07 15:47:16 +0800794 .map = al->map,
795 .sym = al->sym,
796 },
Jin Yaofe962452019-06-28 17:22:59 +0800797 }, *he = hists__findnew_entry(hists, &entry, al, false);
798
799 return he;
800}
801
Namhyung Kim69bcb012013-10-30 09:40:34 +0900802static int
803iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
804 struct addr_location *al __maybe_unused)
805{
806 return 0;
807}
808
809static int
810iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
811 struct addr_location *al __maybe_unused)
812{
813 return 0;
814}
815
816static int
817iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
818{
819 struct perf_sample *sample = iter->sample;
820 struct mem_info *mi;
821
822 mi = sample__resolve_mem(sample, al);
823 if (mi == NULL)
824 return -ENOMEM;
825
826 iter->priv = mi;
827 return 0;
828}
829
830static int
831iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
832{
833 u64 cost;
834 struct mem_info *mi = iter->priv;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300835 struct hists *hists = evsel__hists(iter->evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900836 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900837 struct hist_entry *he;
838
839 if (mi == NULL)
840 return -EINVAL;
841
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900842 cost = sample->weight;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900843 if (!cost)
844 cost = 1;
845
846 /*
847 * must pass period=weight in order to get the correct
848 * sorting from hists__collapse_resort() which is solely
849 * based on periods. We want sorting be done on nr_events * weight
850 * and this is indirectly achieved by passing period=weight here
851 * and the he_stat__add_period() function.
852 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900853 sample->period = cost;
854
Jiri Olsa0102ef32016-06-14 20:19:21 +0200855 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
856 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900857 if (!he)
858 return -ENOMEM;
859
860 iter->he = he;
861 return 0;
862}
863
864static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900865iter_finish_mem_entry(struct hist_entry_iter *iter,
866 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +0900867{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200868 struct evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300869 struct hists *hists = evsel__hists(evsel);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900870 struct hist_entry *he = iter->he;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900871 int err = -EINVAL;
872
873 if (he == NULL)
874 goto out;
875
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300876 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900877
878 err = hist_entry__append_callchain(he, iter->sample);
879
880out:
881 /*
Arnaldo Carvalho de Meloe7e0efc2015-05-19 11:31:22 -0300882 * We don't need to free iter->priv (mem_info) here since the mem info
883 * was either already freed in hists__findnew_entry() or passed to a
884 * new hist entry by hist_entry__new().
Namhyung Kim69bcb012013-10-30 09:40:34 +0900885 */
886 iter->priv = NULL;
887
888 iter->he = NULL;
889 return err;
890}
891
892static int
893iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
894{
895 struct branch_info *bi;
896 struct perf_sample *sample = iter->sample;
897
898 bi = sample__resolve_bstack(sample, al);
899 if (!bi)
900 return -ENOMEM;
901
902 iter->curr = 0;
903 iter->total = sample->branch_stack->nr;
904
905 iter->priv = bi;
906 return 0;
907}
908
909static int
Jin Yao2d78b182017-07-18 20:13:14 +0800910iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
Namhyung Kim69bcb012013-10-30 09:40:34 +0900911 struct addr_location *al __maybe_unused)
912{
913 return 0;
914}
915
916static int
917iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
918{
919 struct branch_info *bi = iter->priv;
920 int i = iter->curr;
921
922 if (bi == NULL)
923 return 0;
924
925 if (iter->curr >= iter->total)
926 return 0;
927
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -0300928 al->maps = bi[i].to.ms.maps;
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300929 al->map = bi[i].to.ms.map;
930 al->sym = bi[i].to.ms.sym;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900931 al->addr = bi[i].to.addr;
932 return 1;
933}
934
935static int
936iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
937{
Namhyung Kim9d3c02d2014-01-07 17:02:25 +0900938 struct branch_info *bi;
Jiri Olsa32dcd022019-07-21 13:23:51 +0200939 struct evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300940 struct hists *hists = evsel__hists(evsel);
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900941 struct perf_sample *sample = iter->sample;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900942 struct hist_entry *he = NULL;
943 int i = iter->curr;
944 int err = 0;
945
946 bi = iter->priv;
947
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -0300948 if (iter->hide_unresolved && !(bi[i].from.ms.sym && bi[i].to.ms.sym))
Namhyung Kim69bcb012013-10-30 09:40:34 +0900949 goto out;
950
951 /*
952 * The report shows the percentage of total branches captured
953 * and not events sampled. Thus we use a pseudo period of 1.
954 */
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900955 sample->period = 1;
956 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
957
Jiri Olsa0102ef32016-06-14 20:19:21 +0200958 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
959 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900960 if (he == NULL)
961 return -ENOMEM;
962
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300963 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900964
965out:
966 iter->he = he;
967 iter->curr++;
968 return err;
969}
970
971static int
972iter_finish_branch_entry(struct hist_entry_iter *iter,
973 struct addr_location *al __maybe_unused)
974{
975 zfree(&iter->priv);
976 iter->he = NULL;
977
978 return iter->curr >= iter->total ? 0 : -1;
979}
980
981static int
982iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
983 struct addr_location *al __maybe_unused)
984{
985 return 0;
986}
987
988static int
989iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
990{
Jiri Olsa32dcd022019-07-21 13:23:51 +0200991 struct evsel *evsel = iter->evsel;
Namhyung Kim69bcb012013-10-30 09:40:34 +0900992 struct perf_sample *sample = iter->sample;
993 struct hist_entry *he;
994
Jiri Olsa0102ef32016-06-14 20:19:21 +0200995 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
996 sample, true);
Namhyung Kim69bcb012013-10-30 09:40:34 +0900997 if (he == NULL)
998 return -ENOMEM;
999
1000 iter->he = he;
1001 return 0;
1002}
1003
1004static int
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001005iter_finish_normal_entry(struct hist_entry_iter *iter,
1006 struct addr_location *al __maybe_unused)
Namhyung Kim69bcb012013-10-30 09:40:34 +09001007{
Namhyung Kim69bcb012013-10-30 09:40:34 +09001008 struct hist_entry *he = iter->he;
Jiri Olsa32dcd022019-07-21 13:23:51 +02001009 struct evsel *evsel = iter->evsel;
Namhyung Kim69bcb012013-10-30 09:40:34 +09001010 struct perf_sample *sample = iter->sample;
1011
1012 if (he == NULL)
1013 return 0;
1014
1015 iter->he = NULL;
1016
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001017 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001018
1019 return hist_entry__append_callchain(he, sample);
1020}
1021
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001022static int
Adrian Hunter96b40f32015-09-25 16:15:47 +03001023iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001024 struct addr_location *al __maybe_unused)
1025{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001026 struct hist_entry **he_cache;
1027
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001028 callchain_cursor_commit(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001029
1030 /*
1031 * This is for detecting cycles or recursions so that they're
1032 * cumulated only one time to prevent entries more than 100%
1033 * overhead.
1034 */
Jiri Olsae3ebaa42018-02-16 13:36:19 +01001035 he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1));
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001036 if (he_cache == NULL)
1037 return -ENOMEM;
1038
1039 iter->priv = he_cache;
1040 iter->curr = 0;
1041
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001042 return 0;
1043}
1044
1045static int
1046iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
1047 struct addr_location *al)
1048{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001049 struct evsel *evsel = iter->evsel;
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001050 struct hists *hists = evsel__hists(evsel);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001051 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001052 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001053 struct hist_entry *he;
1054 int err = 0;
1055
Jiri Olsa0102ef32016-06-14 20:19:21 +02001056 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
1057 sample, true);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001058 if (he == NULL)
1059 return -ENOMEM;
1060
1061 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001062 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001063
Namhyung Kim82aa0192014-12-22 13:44:14 +09001064 hist_entry__append_callchain(he, sample);
Namhyung Kimbe7f8552013-12-26 17:44:10 +09001065
1066 /*
1067 * We need to re-initialize the cursor since callchain_append()
1068 * advanced the cursor to the end.
1069 */
1070 callchain_cursor_commit(&callchain_cursor);
1071
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -03001072 hists__inc_nr_samples(hists, he->filtered);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001073
1074 return err;
1075}
1076
1077static int
1078iter_next_cumulative_entry(struct hist_entry_iter *iter,
1079 struct addr_location *al)
1080{
1081 struct callchain_cursor_node *node;
1082
1083 node = callchain_cursor_current(&callchain_cursor);
1084 if (node == NULL)
1085 return 0;
1086
Namhyung Kimc7405d82013-10-31 13:58:30 +09001087 return fill_callchain_info(al, node, iter->hide_unresolved);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001088}
1089
Kan Liang12e89e62020-03-19 13:25:17 -07001090static bool
1091hist_entry__fast__sym_diff(struct hist_entry *left,
1092 struct hist_entry *right)
1093{
1094 struct symbol *sym_l = left->ms.sym;
1095 struct symbol *sym_r = right->ms.sym;
1096
1097 if (!sym_l && !sym_r)
1098 return left->ip != right->ip;
1099
1100 return !!_sort__sym_cmp(sym_l, sym_r);
1101}
1102
1103
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001104static int
1105iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
1106 struct addr_location *al)
1107{
Jiri Olsa32dcd022019-07-21 13:23:51 +02001108 struct evsel *evsel = iter->evsel;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001109 struct perf_sample *sample = iter->sample;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001110 struct hist_entry **he_cache = iter->priv;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001111 struct hist_entry *he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001112 struct hist_entry he_tmp = {
Arnaldo Carvalho de Melo5cef8972015-08-10 15:45:55 -03001113 .hists = evsel__hists(evsel),
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001114 .cpu = al->cpu,
1115 .thread = al->thread,
1116 .comm = thread__comm(al->thread),
1117 .ip = al->addr,
1118 .ms = {
Arnaldo Carvalho de Melof2eaea02019-11-25 22:15:35 -03001119 .maps = al->maps,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001120 .map = al->map,
1121 .sym = al->sym,
1122 },
Jiri Olsa26349582019-03-05 16:25:31 +01001123 .srcline = (char *) al->srcline,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001124 .parent = iter->parent,
Namhyung Kim72392832015-12-24 11:16:17 +09001125 .raw_data = sample->raw_data,
1126 .raw_size = sample->raw_size,
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001127 };
1128 int i;
Namhyung Kimbe7f8552013-12-26 17:44:10 +09001129 struct callchain_cursor cursor;
Kan Liang12e89e62020-03-19 13:25:17 -07001130 bool fast = hists__has(he_tmp.hists, sym);
Namhyung Kimbe7f8552013-12-26 17:44:10 +09001131
1132 callchain_cursor_snapshot(&cursor, &callchain_cursor);
1133
1134 callchain_cursor_advance(&callchain_cursor);
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001135
1136 /*
1137 * Check if there's duplicate entries in the callchain.
1138 * It's possible that it has cycles or recursive calls.
1139 */
1140 for (i = 0; i < iter->curr; i++) {
Kan Liang12e89e62020-03-19 13:25:17 -07001141 /*
1142 * For most cases, there are no duplicate entries in callchain.
1143 * The symbols are usually different. Do a quick check for
1144 * symbols first.
1145 */
1146 if (fast && hist_entry__fast__sym_diff(he_cache[i], &he_tmp))
1147 continue;
1148
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001149 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
1150 /* to avoid calling callback function */
1151 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001152 return 0;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001153 }
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001154 }
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001155
Jiri Olsa0102ef32016-06-14 20:19:21 +02001156 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
1157 sample, false);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001158 if (he == NULL)
1159 return -ENOMEM;
1160
1161 iter->he = he;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001162 he_cache[iter->curr++] = he;
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001163
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001164 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
Namhyung Kim82aa0192014-12-22 13:44:14 +09001165 callchain_append(he->callchain, &cursor, sample->period);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001166 return 0;
1167}
1168
1169static int
1170iter_finish_cumulative_entry(struct hist_entry_iter *iter,
1171 struct addr_location *al __maybe_unused)
1172{
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001173 zfree(&iter->priv);
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001174 iter->he = NULL;
Namhyung Kimb4d3c8b2013-10-31 10:05:29 +09001175
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001176 return 0;
1177}
1178
Namhyung Kim69bcb012013-10-30 09:40:34 +09001179const struct hist_iter_ops hist_iter_mem = {
1180 .prepare_entry = iter_prepare_mem_entry,
1181 .add_single_entry = iter_add_single_mem_entry,
1182 .next_entry = iter_next_nop_entry,
1183 .add_next_entry = iter_add_next_nop_entry,
1184 .finish_entry = iter_finish_mem_entry,
1185};
1186
1187const struct hist_iter_ops hist_iter_branch = {
1188 .prepare_entry = iter_prepare_branch_entry,
1189 .add_single_entry = iter_add_single_branch_entry,
1190 .next_entry = iter_next_branch_entry,
1191 .add_next_entry = iter_add_next_branch_entry,
1192 .finish_entry = iter_finish_branch_entry,
1193};
1194
1195const struct hist_iter_ops hist_iter_normal = {
1196 .prepare_entry = iter_prepare_normal_entry,
1197 .add_single_entry = iter_add_single_normal_entry,
1198 .next_entry = iter_next_nop_entry,
1199 .add_next_entry = iter_add_next_nop_entry,
1200 .finish_entry = iter_finish_normal_entry,
1201};
1202
Namhyung Kim7a13aa22012-09-11 14:13:04 +09001203const struct hist_iter_ops hist_iter_cumulative = {
1204 .prepare_entry = iter_prepare_cumulative_entry,
1205 .add_single_entry = iter_add_single_cumulative_entry,
1206 .next_entry = iter_next_cumulative_entry,
1207 .add_next_entry = iter_add_next_cumulative_entry,
1208 .finish_entry = iter_finish_cumulative_entry,
1209};
1210
Namhyung Kim69bcb012013-10-30 09:40:34 +09001211int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001212 int max_stack_depth, void *arg)
Namhyung Kim69bcb012013-10-30 09:40:34 +09001213{
1214 int err, err2;
Krister Johansen9c68ae92017-01-05 22:23:31 -08001215 struct map *alm = NULL;
1216
Arnaldo Carvalho de Melo362379a2018-05-24 11:20:39 -03001217 if (al)
Krister Johansen9c68ae92017-01-05 22:23:31 -08001218 alm = map__get(al->map);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001219
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -03001220 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
Namhyung Kim063bd932015-05-19 17:04:10 +09001221 iter->evsel, al, max_stack_depth);
Changbin Ducb6186a2019-03-16 16:05:49 +08001222 if (err) {
1223 map__put(alm);
Namhyung Kim69bcb012013-10-30 09:40:34 +09001224 return err;
Changbin Ducb6186a2019-03-16 16:05:49 +08001225 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001226
Namhyung Kim69bcb012013-10-30 09:40:34 +09001227 err = iter->ops->prepare_entry(iter, al);
1228 if (err)
1229 goto out;
1230
1231 err = iter->ops->add_single_entry(iter, al);
1232 if (err)
1233 goto out;
1234
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001235 if (iter->he && iter->add_entry_cb) {
1236 err = iter->add_entry_cb(iter, al, true, arg);
1237 if (err)
1238 goto out;
1239 }
1240
Namhyung Kim69bcb012013-10-30 09:40:34 +09001241 while (iter->ops->next_entry(iter, al)) {
1242 err = iter->ops->add_next_entry(iter, al);
1243 if (err)
1244 break;
Namhyung Kim9d3c02d2014-01-07 17:02:25 +09001245
1246 if (iter->he && iter->add_entry_cb) {
1247 err = iter->add_entry_cb(iter, al, false, arg);
1248 if (err)
1249 goto out;
1250 }
Namhyung Kim69bcb012013-10-30 09:40:34 +09001251 }
1252
1253out:
1254 err2 = iter->ops->finish_entry(iter, al);
1255 if (!err)
1256 err = err2;
1257
Krister Johansen9c68ae92017-01-05 22:23:31 -08001258 map__put(alm);
1259
Namhyung Kim69bcb012013-10-30 09:40:34 +09001260 return err;
1261}
1262
John Kacur3d1d07e2009-09-28 15:32:55 +02001263int64_t
1264hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1265{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001266 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001267 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001268 int64_t cmp = 0;
1269
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001270 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001271 if (perf_hpp__is_dynamic_entry(fmt) &&
1272 !perf_hpp__defined_dynamic_entry(fmt, hists))
1273 continue;
1274
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001275 cmp = fmt->cmp(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001276 if (cmp)
1277 break;
1278 }
1279
1280 return cmp;
1281}
1282
1283int64_t
1284hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1285{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001286 struct hists *hists = left->hists;
Namhyung Kim093f0ef32014-03-03 12:07:47 +09001287 struct perf_hpp_fmt *fmt;
John Kacur3d1d07e2009-09-28 15:32:55 +02001288 int64_t cmp = 0;
1289
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001290 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim84b6ee82016-02-27 03:52:43 +09001291 if (perf_hpp__is_dynamic_entry(fmt) &&
1292 !perf_hpp__defined_dynamic_entry(fmt, hists))
1293 continue;
1294
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001295 cmp = fmt->collapse(fmt, left, right);
John Kacur3d1d07e2009-09-28 15:32:55 +02001296 if (cmp)
1297 break;
1298 }
1299
1300 return cmp;
1301}
1302
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001303void hist_entry__delete(struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001304{
Jiri Olsaf542e762016-07-05 08:56:04 +02001305 struct hist_entry_ops *ops = he->ops;
1306
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -03001307 thread__zput(he->thread);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001308 map__zput(he->ms.map);
1309
1310 if (he->branch_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -03001311 map__zput(he->branch_info->from.ms.map);
1312 map__zput(he->branch_info->to.ms.map);
Andi Kleen508be0d2016-05-20 13:15:08 -07001313 free_srcline(he->branch_info->srcline_from);
1314 free_srcline(he->branch_info->srcline_to);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001315 zfree(&he->branch_info);
1316 }
1317
1318 if (he->mem_info) {
Arnaldo Carvalho de Melod46a4cd2019-11-04 15:57:38 -03001319 map__zput(he->mem_info->iaddr.ms.map);
1320 map__zput(he->mem_info->daddr.ms.map);
Jiri Olsa9f874982018-03-07 16:50:06 +01001321 mem_info__zput(he->mem_info);
Arnaldo Carvalho de Melo5c24b672015-06-15 23:29:51 -03001322 }
1323
Jin Yao99150a12019-06-28 17:23:01 +08001324 if (he->block_info)
1325 block_info__zput(he->block_info);
1326
Andi Kleen4968ac82019-03-11 07:44:58 -07001327 zfree(&he->res_samples);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001328 zfree(&he->stat_acc);
Namhyung Kimf048d542013-09-11 14:09:28 +09001329 free_srcline(he->srcline);
Andi Kleen31191a82015-08-07 15:54:24 -07001330 if (he->srcfile && he->srcfile[0])
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001331 zfree(&he->srcfile);
Namhyung Kimd1149602014-12-30 14:38:13 +09001332 free_callchain(he->callchain);
Arnaldo Carvalho de Melod8f9da22019-07-04 12:06:20 -03001333 zfree(&he->trace_output);
1334 zfree(&he->raw_data);
Jiri Olsaf542e762016-07-05 08:56:04 +02001335 ops->free(he);
John Kacur3d1d07e2009-09-28 15:32:55 +02001336}
1337
1338/*
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001339 * If this is not the last column, then we need to pad it according to the
Ingo Molnaradba1632018-12-03 11:22:00 +01001340 * pre-calculated max length for this column, otherwise don't bother adding
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001341 * spaces because that would break viewing this with, for instance, 'less',
1342 * that would show tons of trailing spaces when a long C++ demangled method
1343 * names is sampled.
1344*/
1345int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1346 struct perf_hpp_fmt *fmt, int printed)
1347{
1348 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
Jiri Olsada1b0402016-06-14 20:19:20 +02001349 const int width = fmt->width(fmt, hpp, he->hists);
Arnaldo Carvalho de Melo89fee702016-02-11 17:14:13 -03001350 if (printed < width) {
1351 advance_hpp(hpp, printed);
1352 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1353 }
1354 }
1355
1356 return printed;
1357}
1358
1359/*
John Kacur3d1d07e2009-09-28 15:32:55 +02001360 * collapse the histogram
1361 */
1362
Namhyung Kimaef810e2016-02-25 00:13:34 +09001363static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
Namhyung Kimaec13a72016-03-09 22:46:58 +09001364static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1365 enum hist_filter type);
1366
1367typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1368
1369static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1370{
1371 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1372}
1373
1374static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1375 enum hist_filter type,
1376 fmt_chk_fn check)
1377{
1378 struct perf_hpp_fmt *fmt;
1379 bool type_match = false;
1380 struct hist_entry *parent = he->parent_he;
1381
1382 switch (type) {
1383 case HIST_FILTER__THREAD:
1384 if (symbol_conf.comm_list == NULL &&
1385 symbol_conf.pid_list == NULL &&
1386 symbol_conf.tid_list == NULL)
1387 return;
1388 break;
1389 case HIST_FILTER__DSO:
1390 if (symbol_conf.dso_list == NULL)
1391 return;
1392 break;
1393 case HIST_FILTER__SYMBOL:
1394 if (symbol_conf.sym_list == NULL)
1395 return;
1396 break;
1397 case HIST_FILTER__PARENT:
1398 case HIST_FILTER__GUEST:
1399 case HIST_FILTER__HOST:
1400 case HIST_FILTER__SOCKET:
Jiri Olsa9857b712016-08-17 14:55:23 +02001401 case HIST_FILTER__C2C:
Namhyung Kimaec13a72016-03-09 22:46:58 +09001402 default:
1403 return;
1404 }
1405
1406 /* if it's filtered by own fmt, it has to have filter bits */
1407 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1408 if (check(fmt)) {
1409 type_match = true;
1410 break;
1411 }
1412 }
1413
1414 if (type_match) {
1415 /*
1416 * If the filter is for current level entry, propagate
1417 * filter marker to parents. The marker bit was
1418 * already set by default so it only needs to clear
1419 * non-filtered entries.
1420 */
1421 if (!(he->filtered & (1 << type))) {
1422 while (parent) {
1423 parent->filtered &= ~(1 << type);
1424 parent = parent->parent_he;
1425 }
1426 }
1427 } else {
1428 /*
1429 * If current entry doesn't have matching formats, set
1430 * filter marker for upper level entries. it will be
1431 * cleared if its lower level entries is not filtered.
1432 *
1433 * For lower-level entries, it inherits parent's
1434 * filter bit so that lower level entries of a
1435 * non-filtered entry won't set the filter marker.
1436 */
1437 if (parent == NULL)
1438 he->filtered |= (1 << type);
1439 else
1440 he->filtered |= (parent->filtered & (1 << type));
1441 }
1442}
1443
1444static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1445{
1446 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1447 check_thread_entry);
1448
1449 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1450 perf_hpp__is_dso_entry);
1451
1452 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1453 perf_hpp__is_sym_entry);
1454
1455 hists__apply_filters(he->hists, he);
1456}
Namhyung Kimaef810e2016-02-25 00:13:34 +09001457
1458static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001459 struct rb_root_cached *root,
Namhyung Kimaef810e2016-02-25 00:13:34 +09001460 struct hist_entry *he,
Namhyung Kimaec13a72016-03-09 22:46:58 +09001461 struct hist_entry *parent_he,
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001462 struct perf_hpp_list *hpp_list)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001463{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001464 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001465 struct rb_node *parent = NULL;
1466 struct hist_entry *iter, *new;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001467 struct perf_hpp_fmt *fmt;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001468 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001469 bool leftmost = true;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001470
1471 while (*p != NULL) {
1472 parent = *p;
1473 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1474
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001475 cmp = 0;
1476 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1477 cmp = fmt->collapse(fmt, iter, he);
1478 if (cmp)
1479 break;
1480 }
1481
Namhyung Kimaef810e2016-02-25 00:13:34 +09001482 if (!cmp) {
1483 he_stat__add_stat(&iter->stat, &he->stat);
1484 return iter;
1485 }
1486
1487 if (cmp < 0)
1488 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001489 else {
Namhyung Kimaef810e2016-02-25 00:13:34 +09001490 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001491 leftmost = false;
1492 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001493 }
1494
1495 new = hist_entry__new(he, true);
1496 if (new == NULL)
1497 return NULL;
1498
Namhyung Kimaef810e2016-02-25 00:13:34 +09001499 hists->nr_entries++;
1500
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001501 /* save related format list for output */
1502 new->hpp_list = hpp_list;
Namhyung Kimaec13a72016-03-09 22:46:58 +09001503 new->parent_he = parent_he;
1504
1505 hist_entry__apply_hierarchy_filters(new);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001506
1507 /* some fields are now passed to 'new' */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001508 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1509 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1510 he->trace_output = NULL;
1511 else
1512 new->trace_output = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001513
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001514 if (perf_hpp__is_srcline_entry(fmt))
1515 he->srcline = NULL;
1516 else
1517 new->srcline = NULL;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001518
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001519 if (perf_hpp__is_srcfile_entry(fmt))
1520 he->srcfile = NULL;
1521 else
1522 new->srcfile = NULL;
1523 }
Namhyung Kimaef810e2016-02-25 00:13:34 +09001524
1525 rb_link_node(&new->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001526 rb_insert_color_cached(&new->rb_node_in, root, leftmost);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001527 return new;
1528}
1529
1530static int hists__hierarchy_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001531 struct rb_root_cached *root,
Namhyung Kimaef810e2016-02-25 00:13:34 +09001532 struct hist_entry *he)
1533{
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001534 struct perf_hpp_list_node *node;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001535 struct hist_entry *new_he = NULL;
1536 struct hist_entry *parent = NULL;
1537 int depth = 0;
1538 int ret = 0;
1539
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001540 list_for_each_entry(node, &hists->hpp_formats, list) {
1541 /* skip period (overhead) and elided columns */
1542 if (node->level == 0 || node->skip)
Namhyung Kimaef810e2016-02-25 00:13:34 +09001543 continue;
1544
1545 /* insert copy of 'he' for each fmt into the hierarchy */
Namhyung Kimaec13a72016-03-09 22:46:58 +09001546 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
Namhyung Kimaef810e2016-02-25 00:13:34 +09001547 if (new_he == NULL) {
1548 ret = -1;
1549 break;
1550 }
1551
1552 root = &new_he->hroot_in;
Namhyung Kimaef810e2016-02-25 00:13:34 +09001553 new_he->depth = depth++;
1554 parent = new_he;
1555 }
1556
1557 if (new_he) {
1558 new_he->leaf = true;
1559
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001560 if (hist_entry__has_callchains(new_he) &&
1561 symbol_conf.use_callchain) {
Namhyung Kimaef810e2016-02-25 00:13:34 +09001562 callchain_cursor_reset(&callchain_cursor);
1563 if (callchain_merge(&callchain_cursor,
1564 new_he->callchain,
1565 he->callchain) < 0)
1566 ret = -1;
1567 }
1568 }
1569
1570 /* 'he' is no longer used */
1571 hist_entry__delete(he);
1572
1573 /* return 0 (or -1) since it already applied filters */
1574 return ret;
1575}
1576
Jiri Olsa592dac62016-03-24 13:52:17 +01001577static int hists__collapse_insert_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001578 struct rb_root_cached *root,
Jiri Olsa592dac62016-03-24 13:52:17 +01001579 struct hist_entry *he)
John Kacur3d1d07e2009-09-28 15:32:55 +02001580{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001581 struct rb_node **p = &root->rb_root.rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001582 struct rb_node *parent = NULL;
1583 struct hist_entry *iter;
1584 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001585 bool leftmost = true;
John Kacur3d1d07e2009-09-28 15:32:55 +02001586
Namhyung Kimaef810e2016-02-25 00:13:34 +09001587 if (symbol_conf.report_hierarchy)
1588 return hists__hierarchy_insert_entry(hists, root, he);
1589
John Kacur3d1d07e2009-09-28 15:32:55 +02001590 while (*p != NULL) {
1591 parent = *p;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001592 iter = rb_entry(parent, struct hist_entry, rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001593
1594 cmp = hist_entry__collapse(iter, he);
1595
1596 if (!cmp) {
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001597 int ret = 0;
1598
Namhyung Kim139c0812012-10-04 21:49:43 +09001599 he_stat__add_stat(&iter->stat, &he->stat);
Namhyung Kimf8be1c82012-09-11 13:15:07 +09001600 if (symbol_conf.cumulate_callchain)
1601 he_stat__add_stat(iter->stat_acc, he->stat_acc);
Namhyung Kim9ec60972012-09-26 16:47:28 +09001602
Arnaldo Carvalho de Melofabd37b2018-05-29 13:59:24 -03001603 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
Namhyung Kim47260642012-05-31 14:43:26 +09001604 callchain_cursor_reset(&callchain_cursor);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001605 if (callchain_merge(&callchain_cursor,
1606 iter->callchain,
1607 he->callchain) < 0)
1608 ret = -1;
Frederic Weisbecker1b3a0e92011-01-14 04:51:58 +01001609 }
Arnaldo Carvalho de Melo6733d1b2014-12-19 12:31:40 -03001610 hist_entry__delete(he);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001611 return ret;
John Kacur3d1d07e2009-09-28 15:32:55 +02001612 }
1613
1614 if (cmp < 0)
1615 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001616 else {
John Kacur3d1d07e2009-09-28 15:32:55 +02001617 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001618 leftmost = false;
1619 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001620 }
Namhyung Kim740b97f2014-12-22 13:44:10 +09001621 hists->nr_entries++;
John Kacur3d1d07e2009-09-28 15:32:55 +02001622
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001623 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001624 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001625 return 1;
John Kacur3d1d07e2009-09-28 15:32:55 +02001626}
1627
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001628struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001629{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001630 struct rb_root_cached *root;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001631
1632 pthread_mutex_lock(&hists->lock);
1633
1634 root = hists->entries_in;
1635 if (++hists->entries_in > &hists->entries_in_array[1])
1636 hists->entries_in = &hists->entries_in_array[0];
1637
1638 pthread_mutex_unlock(&hists->lock);
1639
1640 return root;
1641}
1642
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001643static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1644{
1645 hists__filter_entry_by_dso(hists, he);
1646 hists__filter_entry_by_thread(hists, he);
Namhyung Kime94d53e2012-03-16 17:50:51 +09001647 hists__filter_entry_by_symbol(hists, he);
Kan Liang21394d92015-09-04 10:45:44 -04001648 hists__filter_entry_by_socket(hists, he);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001649}
1650
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001651int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001652{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001653 struct rb_root_cached *root;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001654 struct rb_node *next;
1655 struct hist_entry *n;
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001656 int ret;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001657
Jiri Olsa52225032016-05-03 13:54:42 +02001658 if (!hists__has(hists, need_collapse))
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001659 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001660
Namhyung Kim740b97f2014-12-22 13:44:10 +09001661 hists->nr_entries = 0;
1662
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001663 root = hists__get_rotate_entries_in(hists);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001664
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001665 next = rb_first_cached(root);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001666
1667 while (next) {
Arnaldo Carvalho de Melo33e940a2013-09-17 16:34:28 -03001668 if (session_done())
1669 break;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001670 n = rb_entry(next, struct hist_entry, rb_node_in);
1671 next = rb_next(&n->rb_node_in);
1672
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001673 rb_erase_cached(&n->rb_node_in, root);
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001674 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1675 if (ret < 0)
1676 return -1;
1677
1678 if (ret) {
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001679 /*
1680 * If it wasn't combined with one of the entries already
1681 * collapsed, we need to apply the filters that may have
1682 * been set by, say, the hist_browser.
1683 */
1684 hists__apply_filters(hists, n);
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02001685 }
Namhyung Kimc1fb5652013-10-11 14:15:38 +09001686 if (prog)
1687 ui_progress__update(prog, 1);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001688 }
Namhyung Kimbba58cd2016-02-16 23:08:25 +09001689 return 0;
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001690}
1691
Jiri Olsa722ddfd2019-11-05 00:27:11 +01001692static int64_t hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001693{
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001694 struct hists *hists = a->hists;
Namhyung Kim043ca3892014-03-03 14:18:00 +09001695 struct perf_hpp_fmt *fmt;
1696 int64_t cmp = 0;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001697
Jiri Olsaaa6f50a2016-01-18 10:24:24 +01001698 hists__for_each_sort_list(hists, fmt) {
Namhyung Kim361459f2015-12-23 02:07:08 +09001699 if (perf_hpp__should_skip(fmt, a->hists))
Namhyung Kime67d49a2014-03-18 13:00:59 +09001700 continue;
1701
Namhyung Kim87bbdf72015-01-08 09:45:46 +09001702 cmp = fmt->sort(fmt, a, b);
Namhyung Kim043ca3892014-03-03 14:18:00 +09001703 if (cmp)
Namhyung Kim29d720e2013-01-22 18:09:33 +09001704 break;
1705 }
1706
Namhyung Kim043ca3892014-03-03 14:18:00 +09001707 return cmp;
Namhyung Kim29d720e2013-01-22 18:09:33 +09001708}
1709
Namhyung Kim9283ba92014-04-24 16:37:26 +09001710static void hists__reset_filter_stats(struct hists *hists)
1711{
1712 hists->nr_non_filtered_entries = 0;
1713 hists->stats.total_non_filtered_period = 0;
1714}
1715
1716void hists__reset_stats(struct hists *hists)
1717{
1718 hists->nr_entries = 0;
1719 hists->stats.total_period = 0;
1720
1721 hists__reset_filter_stats(hists);
1722}
1723
1724static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1725{
1726 hists->nr_non_filtered_entries++;
1727 hists->stats.total_non_filtered_period += h->stat.period;
1728}
1729
1730void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1731{
1732 if (!h->filtered)
1733 hists__inc_filter_stats(hists, h);
1734
1735 hists->nr_entries++;
1736 hists->stats.total_period += h->stat.period;
1737}
1738
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001739static void hierarchy_recalc_total_periods(struct hists *hists)
1740{
1741 struct rb_node *node;
1742 struct hist_entry *he;
1743
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001744 node = rb_first_cached(&hists->entries);
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001745
1746 hists->stats.total_period = 0;
1747 hists->stats.total_non_filtered_period = 0;
1748
1749 /*
1750 * recalculate total period using top-level entries only
1751 * since lower level entries only see non-filtered entries
1752 * but upper level entries have sum of both entries.
1753 */
1754 while (node) {
1755 he = rb_entry(node, struct hist_entry, rb_node);
1756 node = rb_next(node);
1757
1758 hists->stats.total_period += he->stat.period;
1759 if (!he->filtered)
1760 hists->stats.total_non_filtered_period += he->stat.period;
1761 }
1762}
1763
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001764static void hierarchy_insert_output_entry(struct rb_root_cached *root,
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001765 struct hist_entry *he)
1766{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001767 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001768 struct rb_node *parent = NULL;
1769 struct hist_entry *iter;
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001770 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001771 bool leftmost = true;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001772
1773 while (*p != NULL) {
1774 parent = *p;
1775 iter = rb_entry(parent, struct hist_entry, rb_node);
1776
1777 if (hist_entry__sort(he, iter) > 0)
1778 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001779 else {
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001780 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001781 leftmost = false;
1782 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001783 }
1784
1785 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001786 rb_insert_color_cached(&he->rb_node, root, leftmost);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001787
1788 /* update column width of dynamic entry */
Namhyung Kim1b2dbbf2016-03-07 16:44:46 -03001789 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1790 if (perf_hpp__is_dynamic_entry(fmt))
1791 fmt->sort(fmt, he, NULL);
1792 }
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001793}
1794
1795static void hists__hierarchy_output_resort(struct hists *hists,
1796 struct ui_progress *prog,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001797 struct rb_root_cached *root_in,
1798 struct rb_root_cached *root_out,
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001799 u64 min_callchain_hits,
1800 bool use_callchain)
1801{
1802 struct rb_node *node;
1803 struct hist_entry *he;
1804
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001805 *root_out = RB_ROOT_CACHED;
1806 node = rb_first_cached(root_in);
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001807
1808 while (node) {
1809 he = rb_entry(node, struct hist_entry, rb_node_in);
1810 node = rb_next(node);
1811
1812 hierarchy_insert_output_entry(root_out, he);
1813
1814 if (prog)
1815 ui_progress__update(prog, 1);
1816
Namhyung Kimc72ab442016-11-08 22:08:33 +09001817 hists->nr_entries++;
1818 if (!he->filtered) {
1819 hists->nr_non_filtered_entries++;
1820 hists__calc_col_len(hists, he);
1821 }
1822
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001823 if (!he->leaf) {
1824 hists__hierarchy_output_resort(hists, prog,
1825 &he->hroot_in,
1826 &he->hroot_out,
1827 min_callchain_hits,
1828 use_callchain);
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001829 continue;
1830 }
1831
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001832 if (!use_callchain)
1833 continue;
1834
1835 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1836 u64 total = he->stat.period;
1837
1838 if (symbol_conf.cumulate_callchain)
1839 total = he->stat_acc->period;
1840
1841 min_callchain_hits = total * (callchain_param.min_percent / 100);
1842 }
1843
1844 callchain_param.sort(&he->sorted_chain, he->callchain,
1845 min_callchain_hits, &callchain_param);
1846 }
1847}
1848
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001849static void __hists__insert_output_entry(struct rb_root_cached *entries,
Arnaldo Carvalho de Melo1c02c4d2010-05-10 13:04:11 -03001850 struct hist_entry *he,
Kan Liangf9db0d02015-08-11 06:30:48 -04001851 u64 min_callchain_hits,
1852 bool use_callchain)
John Kacur3d1d07e2009-09-28 15:32:55 +02001853{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001854 struct rb_node **p = &entries->rb_root.rb_node;
John Kacur3d1d07e2009-09-28 15:32:55 +02001855 struct rb_node *parent = NULL;
1856 struct hist_entry *iter;
Namhyung Kimabab5e72016-02-27 03:52:47 +09001857 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001858 bool leftmost = true;
John Kacur3d1d07e2009-09-28 15:32:55 +02001859
Namhyung Kim744070e2016-01-28 00:40:48 +09001860 if (use_callchain) {
1861 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1862 u64 total = he->stat.period;
1863
1864 if (symbol_conf.cumulate_callchain)
1865 total = he->stat_acc->period;
1866
1867 min_callchain_hits = total * (callchain_param.min_percent / 100);
1868 }
Arnaldo Carvalho de Melob9fb9302010-04-02 09:50:42 -03001869 callchain_param.sort(&he->sorted_chain, he->callchain,
John Kacur3d1d07e2009-09-28 15:32:55 +02001870 min_callchain_hits, &callchain_param);
Namhyung Kim744070e2016-01-28 00:40:48 +09001871 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001872
1873 while (*p != NULL) {
1874 parent = *p;
1875 iter = rb_entry(parent, struct hist_entry, rb_node);
1876
Namhyung Kim043ca3892014-03-03 14:18:00 +09001877 if (hist_entry__sort(he, iter) > 0)
John Kacur3d1d07e2009-09-28 15:32:55 +02001878 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001879 else {
John Kacur3d1d07e2009-09-28 15:32:55 +02001880 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001881 leftmost = false;
1882 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001883 }
1884
1885 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001886 rb_insert_color_cached(&he->rb_node, entries, leftmost);
Namhyung Kimabab5e72016-02-27 03:52:47 +09001887
1888 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1889 if (perf_hpp__is_dynamic_entry(fmt) &&
1890 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1891 fmt->sort(fmt, he, NULL); /* update column width */
1892 }
John Kacur3d1d07e2009-09-28 15:32:55 +02001893}
1894
Jiri Olsa01441af2016-01-18 10:23:59 +01001895static void output_resort(struct hists *hists, struct ui_progress *prog,
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001896 bool use_callchain, hists__resort_cb_t cb,
1897 void *cb_arg)
John Kacur3d1d07e2009-09-28 15:32:55 +02001898{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001899 struct rb_root_cached *root;
John Kacur3d1d07e2009-09-28 15:32:55 +02001900 struct rb_node *next;
1901 struct hist_entry *n;
Namhyung Kim467ef102016-02-16 23:08:19 +09001902 u64 callchain_total;
John Kacur3d1d07e2009-09-28 15:32:55 +02001903 u64 min_callchain_hits;
1904
Namhyung Kim467ef102016-02-16 23:08:19 +09001905 callchain_total = hists->callchain_period;
1906 if (symbol_conf.filter_relative)
1907 callchain_total = hists->callchain_non_filtered_period;
1908
1909 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
John Kacur3d1d07e2009-09-28 15:32:55 +02001910
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001911 hists__reset_stats(hists);
1912 hists__reset_col_len(hists);
1913
1914 if (symbol_conf.report_hierarchy) {
Namhyung Kimf7fb5382016-03-09 22:47:02 +09001915 hists__hierarchy_output_resort(hists, prog,
1916 &hists->entries_collapsed,
1917 &hists->entries,
1918 min_callchain_hits,
1919 use_callchain);
1920 hierarchy_recalc_total_periods(hists);
1921 return;
Namhyung Kim1a3906a2016-02-25 00:13:35 +09001922 }
1923
Jiri Olsa52225032016-05-03 13:54:42 +02001924 if (hists__has(hists, need_collapse))
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001925 root = &hists->entries_collapsed;
1926 else
1927 root = hists->entries_in;
1928
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001929 next = rb_first_cached(root);
1930 hists->entries = RB_ROOT_CACHED;
John Kacur3d1d07e2009-09-28 15:32:55 +02001931
1932 while (next) {
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001933 n = rb_entry(next, struct hist_entry, rb_node_in);
1934 next = rb_next(&n->rb_node_in);
John Kacur3d1d07e2009-09-28 15:32:55 +02001935
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001936 if (cb && cb(n, cb_arg))
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001937 continue;
1938
Kan Liangf9db0d02015-08-11 06:30:48 -04001939 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
Namhyung Kim62638352014-04-24 16:21:46 +09001940 hists__inc_stats(hists, n);
Namhyung Kimae993ef2014-04-24 16:25:19 +09001941
1942 if (!n->filtered)
1943 hists__calc_col_len(hists, n);
Namhyung Kim740b97f2014-12-22 13:44:10 +09001944
1945 if (prog)
1946 ui_progress__update(prog, 1);
John Kacur3d1d07e2009-09-28 15:32:55 +02001947 }
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -03001948}
Arnaldo Carvalho de Melob9bf0892009-12-14 11:37:11 -02001949
Arnaldo Carvalho de Melo10c513f2020-05-06 12:58:55 -03001950void evsel__output_resort_cb(struct evsel *evsel, struct ui_progress *prog,
1951 hists__resort_cb_t cb, void *cb_arg)
Jiri Olsa01441af2016-01-18 10:23:59 +01001952{
Jiri Olsa01441af2016-01-18 10:23:59 +01001953 bool use_callchain;
1954
1955 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
Arnaldo Carvalho de Melo27de9b22018-05-28 16:00:29 -03001956 use_callchain = evsel__has_callchain(evsel);
Jiri Olsa01441af2016-01-18 10:23:59 +01001957 else
1958 use_callchain = symbol_conf.use_callchain;
1959
Jin Yaob49a8212017-05-08 18:43:02 +08001960 use_callchain |= symbol_conf.show_branchflag_count;
1961
Jiri Olsa57496182019-02-04 15:18:07 +01001962 output_resort(evsel__hists(evsel), prog, use_callchain, cb, cb_arg);
1963}
1964
Arnaldo Carvalho de Melo10c513f2020-05-06 12:58:55 -03001965void evsel__output_resort(struct evsel *evsel, struct ui_progress *prog)
Jiri Olsa57496182019-02-04 15:18:07 +01001966{
Arnaldo Carvalho de Melo10c513f2020-05-06 12:58:55 -03001967 return evsel__output_resort_cb(evsel, prog, NULL, NULL);
Jiri Olsa452ce032016-01-18 10:24:00 +01001968}
1969
1970void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1971{
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001972 output_resort(hists, prog, symbol_conf.use_callchain, NULL, NULL);
Jiri Olsa52c5cc32016-08-01 20:02:34 +02001973}
1974
1975void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1976 hists__resort_cb_t cb)
1977{
Jiri Olsae4c38fd2019-02-04 15:18:06 +01001978 output_resort(hists, prog, symbol_conf.use_callchain, cb, NULL);
Jiri Olsa01441af2016-01-18 10:23:59 +01001979}
1980
Namhyung Kim8c018722016-02-25 00:13:36 +09001981static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1982{
1983 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1984 return false;
1985
1986 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1987 return true;
1988
1989 return false;
1990}
1991
1992struct rb_node *rb_hierarchy_last(struct rb_node *node)
1993{
1994 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1995
1996 while (can_goto_child(he, HMD_NORMAL)) {
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08001997 node = rb_last(&he->hroot_out.rb_root);
Namhyung Kim8c018722016-02-25 00:13:36 +09001998 he = rb_entry(node, struct hist_entry, rb_node);
1999 }
2000 return node;
2001}
2002
2003struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
2004{
2005 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
2006
2007 if (can_goto_child(he, hmd))
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002008 node = rb_first_cached(&he->hroot_out);
Namhyung Kim8c018722016-02-25 00:13:36 +09002009 else
2010 node = rb_next(node);
2011
2012 while (node == NULL) {
2013 he = he->parent_he;
2014 if (he == NULL)
2015 break;
2016
2017 node = rb_next(&he->rb_node);
2018 }
2019 return node;
2020}
2021
2022struct rb_node *rb_hierarchy_prev(struct rb_node *node)
2023{
2024 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
2025
2026 node = rb_prev(node);
2027 if (node)
2028 return rb_hierarchy_last(node);
2029
2030 he = he->parent_he;
2031 if (he == NULL)
2032 return NULL;
2033
2034 return &he->rb_node;
2035}
2036
Namhyung Kima7b58952016-02-26 21:13:16 +09002037bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
2038{
2039 struct rb_node *node;
2040 struct hist_entry *child;
2041 float percent;
2042
2043 if (he->leaf)
2044 return false;
2045
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002046 node = rb_first_cached(&he->hroot_out);
Namhyung Kima7b58952016-02-26 21:13:16 +09002047 child = rb_entry(node, struct hist_entry, rb_node);
2048
2049 while (node && child->filtered) {
2050 node = rb_next(node);
2051 child = rb_entry(node, struct hist_entry, rb_node);
2052 }
2053
2054 if (node)
2055 percent = hist_entry__get_percent_limit(child);
2056 else
2057 percent = 0;
2058
2059 return node && percent >= limit;
2060}
2061
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002062static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002063 enum hist_filter filter)
2064{
2065 h->filtered &= ~(1 << filter);
Namhyung Kim155e9af2016-02-25 00:13:38 +09002066
2067 if (symbol_conf.report_hierarchy) {
2068 struct hist_entry *parent = h->parent_he;
2069
2070 while (parent) {
2071 he_stat__add_stat(&parent->stat, &h->stat);
2072
2073 parent->filtered &= ~(1 << filter);
2074
2075 if (parent->filtered)
2076 goto next;
2077
2078 /* force fold unfiltered entry for simplicity */
2079 parent->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09002080 parent->has_no_entry = false;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002081 parent->row_offset = 0;
2082 parent->nr_rows = 0;
2083next:
2084 parent = parent->parent_he;
2085 }
2086 }
2087
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002088 if (h->filtered)
2089 return;
2090
Namhyung Kim87e90f42014-04-24 16:44:16 +09002091 /* force fold unfiltered entry for simplicity */
Namhyung Kim3698dab2015-05-05 23:55:46 +09002092 h->unfolded = false;
Namhyung Kim79dded82016-02-26 21:13:19 +09002093 h->has_no_entry = false;
Arnaldo Carvalho de Melo0f0cbf72010-07-26 17:13:40 -03002094 h->row_offset = 0;
He Kuanga8cd1f42015-03-11 20:36:03 +08002095 h->nr_rows = 0;
Namhyung Kim9283ba92014-04-24 16:37:26 +09002096
Namhyung Kim1ab1fa52013-12-26 15:11:52 +09002097 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002098
Namhyung Kim9283ba92014-04-24 16:37:26 +09002099 hists__inc_filter_stats(hists, h);
Arnaldo Carvalho de Melo42b28ac2011-09-26 12:33:28 -03002100 hists__calc_col_len(hists, h);
Arnaldo Carvalho de Melocc5edb02010-07-16 12:35:07 -03002101}
2102
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02002103
2104static bool hists__filter_entry_by_dso(struct hists *hists,
2105 struct hist_entry *he)
2106{
2107 if (hists->dso_filter != NULL &&
2108 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
2109 he->filtered |= (1 << HIST_FILTER__DSO);
2110 return true;
2111 }
2112
2113 return false;
2114}
2115
Arnaldo Carvalho de Melo90cf1fb2011-10-19 13:09:10 -02002116static bool hists__filter_entry_by_thread(struct hists *hists,
2117 struct hist_entry *he)
2118{
2119 if (hists->thread_filter != NULL &&
2120 he->thread != hists->thread_filter) {
2121 he->filtered |= (1 << HIST_FILTER__THREAD);
2122 return true;
2123 }
2124
2125 return false;
2126}
2127
Namhyung Kime94d53e2012-03-16 17:50:51 +09002128static bool hists__filter_entry_by_symbol(struct hists *hists,
2129 struct hist_entry *he)
2130{
2131 if (hists->symbol_filter_str != NULL &&
2132 (!he->ms.sym || strstr(he->ms.sym->name,
2133 hists->symbol_filter_str) == NULL)) {
2134 he->filtered |= (1 << HIST_FILTER__SYMBOL);
2135 return true;
2136 }
2137
2138 return false;
2139}
2140
Kan Liang21394d92015-09-04 10:45:44 -04002141static bool hists__filter_entry_by_socket(struct hists *hists,
2142 struct hist_entry *he)
2143{
2144 if ((hists->socket_filter > -1) &&
2145 (he->socket != hists->socket_filter)) {
2146 he->filtered |= (1 << HIST_FILTER__SOCKET);
2147 return true;
2148 }
2149
2150 return false;
2151}
2152
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002153typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
2154
2155static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
Kan Liang84734b02015-09-04 10:45:45 -04002156{
2157 struct rb_node *nd;
2158
2159 hists->stats.nr_non_filtered_samples = 0;
2160
2161 hists__reset_filter_stats(hists);
2162 hists__reset_col_len(hists);
2163
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002164 for (nd = rb_first_cached(&hists->entries); nd; nd = rb_next(nd)) {
Kan Liang84734b02015-09-04 10:45:45 -04002165 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2166
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002167 if (filter(hists, h))
Kan Liang84734b02015-09-04 10:45:45 -04002168 continue;
2169
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002170 hists__remove_entry_filter(hists, h, type);
Kan Liang84734b02015-09-04 10:45:45 -04002171 }
2172}
2173
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002174static void resort_filtered_entry(struct rb_root_cached *root,
2175 struct hist_entry *he)
Namhyung Kim70642852016-02-25 00:13:39 +09002176{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002177 struct rb_node **p = &root->rb_root.rb_node;
Namhyung Kim70642852016-02-25 00:13:39 +09002178 struct rb_node *parent = NULL;
2179 struct hist_entry *iter;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002180 struct rb_root_cached new_root = RB_ROOT_CACHED;
Namhyung Kim70642852016-02-25 00:13:39 +09002181 struct rb_node *nd;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002182 bool leftmost = true;
Namhyung Kim70642852016-02-25 00:13:39 +09002183
2184 while (*p != NULL) {
2185 parent = *p;
2186 iter = rb_entry(parent, struct hist_entry, rb_node);
2187
2188 if (hist_entry__sort(he, iter) > 0)
2189 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002190 else {
Namhyung Kim70642852016-02-25 00:13:39 +09002191 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002192 leftmost = false;
2193 }
Namhyung Kim70642852016-02-25 00:13:39 +09002194 }
2195
2196 rb_link_node(&he->rb_node, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002197 rb_insert_color_cached(&he->rb_node, root, leftmost);
Namhyung Kim70642852016-02-25 00:13:39 +09002198
2199 if (he->leaf || he->filtered)
2200 return;
2201
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002202 nd = rb_first_cached(&he->hroot_out);
Namhyung Kim70642852016-02-25 00:13:39 +09002203 while (nd) {
2204 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2205
2206 nd = rb_next(nd);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002207 rb_erase_cached(&h->rb_node, &he->hroot_out);
Namhyung Kim70642852016-02-25 00:13:39 +09002208
2209 resort_filtered_entry(&new_root, h);
2210 }
2211
2212 he->hroot_out = new_root;
2213}
2214
Namhyung Kim155e9af2016-02-25 00:13:38 +09002215static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2216{
2217 struct rb_node *nd;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002218 struct rb_root_cached new_root = RB_ROOT_CACHED;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002219
2220 hists->stats.nr_non_filtered_samples = 0;
2221
2222 hists__reset_filter_stats(hists);
2223 hists__reset_col_len(hists);
2224
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002225 nd = rb_first_cached(&hists->entries);
Namhyung Kim155e9af2016-02-25 00:13:38 +09002226 while (nd) {
2227 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2228 int ret;
2229
2230 ret = hist_entry__filter(h, type, arg);
2231
2232 /*
2233 * case 1. non-matching type
2234 * zero out the period, set filter marker and move to child
2235 */
2236 if (ret < 0) {
2237 memset(&h->stat, 0, sizeof(h->stat));
2238 h->filtered |= (1 << type);
2239
2240 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2241 }
2242 /*
2243 * case 2. matched type (filter out)
2244 * set filter marker and move to next
2245 */
2246 else if (ret == 1) {
2247 h->filtered |= (1 << type);
2248
2249 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2250 }
2251 /*
2252 * case 3. ok (not filtered)
2253 * add period to hists and parents, erase the filter marker
2254 * and move to next sibling
2255 */
2256 else {
2257 hists__remove_entry_filter(hists, h, type);
2258
2259 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2260 }
2261 }
Namhyung Kim70642852016-02-25 00:13:39 +09002262
Namhyung Kimf7fb5382016-03-09 22:47:02 +09002263 hierarchy_recalc_total_periods(hists);
2264
Namhyung Kim70642852016-02-25 00:13:39 +09002265 /*
2266 * resort output after applying a new filter since filter in a lower
2267 * hierarchy can change periods in a upper hierarchy.
2268 */
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002269 nd = rb_first_cached(&hists->entries);
Namhyung Kim70642852016-02-25 00:13:39 +09002270 while (nd) {
2271 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2272
2273 nd = rb_next(nd);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002274 rb_erase_cached(&h->rb_node, &hists->entries);
Namhyung Kim70642852016-02-25 00:13:39 +09002275
2276 resort_filtered_entry(&new_root, h);
2277 }
2278
2279 hists->entries = new_root;
Namhyung Kim155e9af2016-02-25 00:13:38 +09002280}
2281
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002282void hists__filter_by_thread(struct hists *hists)
2283{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002284 if (symbol_conf.report_hierarchy)
2285 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2286 hists->thread_filter);
2287 else
2288 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2289 hists__filter_entry_by_thread);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002290}
2291
2292void hists__filter_by_dso(struct hists *hists)
2293{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002294 if (symbol_conf.report_hierarchy)
2295 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2296 hists->dso_filter);
2297 else
2298 hists__filter_by_type(hists, HIST_FILTER__DSO,
2299 hists__filter_entry_by_dso);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002300}
2301
2302void hists__filter_by_symbol(struct hists *hists)
2303{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002304 if (symbol_conf.report_hierarchy)
2305 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2306 hists->symbol_filter_str);
2307 else
2308 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2309 hists__filter_entry_by_symbol);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002310}
2311
2312void hists__filter_by_socket(struct hists *hists)
2313{
Namhyung Kim155e9af2016-02-25 00:13:38 +09002314 if (symbol_conf.report_hierarchy)
2315 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2316 &hists->socket_filter);
2317 else
2318 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2319 hists__filter_entry_by_socket);
Namhyung Kim1f7c2542016-01-20 10:15:21 +09002320}
2321
Arnaldo Carvalho de Melo28a6b6a2012-12-18 16:24:46 -03002322void events_stats__inc(struct events_stats *stats, u32 type)
2323{
2324 ++stats->nr_events[0];
2325 ++stats->nr_events[type];
2326}
2327
Namhyung Kim0f0abba2021-04-26 18:37:13 -07002328static void hists_stats__inc(struct hists_stats *stats)
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002329{
Namhyung Kim0f0abba2021-04-26 18:37:13 -07002330 ++stats->nr_samples;
2331}
2332
2333void hists__inc_nr_events(struct hists *hists)
2334{
2335 hists_stats__inc(&hists->stats);
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -03002336}
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002337
Namhyung Kim1844dbc2014-05-28 14:12:18 +09002338void hists__inc_nr_samples(struct hists *hists, bool filtered)
2339{
Namhyung Kim0f0abba2021-04-26 18:37:13 -07002340 hists_stats__inc(&hists->stats);
Namhyung Kim1844dbc2014-05-28 14:12:18 +09002341 if (!filtered)
2342 hists->stats.nr_non_filtered_samples++;
2343}
2344
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002345static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2346 struct hist_entry *pair)
2347{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002348 struct rb_root_cached *root;
Namhyung Kimce74f602012-12-10 17:29:55 +09002349 struct rb_node **p;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002350 struct rb_node *parent = NULL;
2351 struct hist_entry *he;
Andi Kleen354cc402013-10-01 07:22:15 -07002352 int64_t cmp;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002353 bool leftmost = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002354
Jiri Olsa52225032016-05-03 13:54:42 +02002355 if (hists__has(hists, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002356 root = &hists->entries_collapsed;
2357 else
2358 root = hists->entries_in;
2359
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002360 p = &root->rb_root.rb_node;
Namhyung Kimce74f602012-12-10 17:29:55 +09002361
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002362 while (*p != NULL) {
2363 parent = *p;
Namhyung Kimce74f602012-12-10 17:29:55 +09002364 he = rb_entry(parent, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002365
Namhyung Kimce74f602012-12-10 17:29:55 +09002366 cmp = hist_entry__collapse(he, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002367
2368 if (!cmp)
2369 goto out;
2370
2371 if (cmp < 0)
2372 p = &(*p)->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002373 else {
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002374 p = &(*p)->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002375 leftmost = false;
2376 }
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002377 }
2378
Namhyung Kima0b51af2012-09-11 13:34:27 +09002379 he = hist_entry__new(pair, true);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002380 if (he) {
Arnaldo Carvalho de Melo30193d72012-11-12 13:20:03 -03002381 memset(&he->stat, 0, sizeof(he->stat));
2382 he->hists = hists;
Kan Liang09623d72016-04-24 23:28:09 -07002383 if (symbol_conf.cumulate_callchain)
2384 memset(he->stat_acc, 0, sizeof(he->stat));
Namhyung Kimce74f602012-12-10 17:29:55 +09002385 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002386 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kim62638352014-04-24 16:21:46 +09002387 hists__inc_stats(hists, he);
Jiri Olsae0af43d2012-12-01 21:18:20 +01002388 he->dummy = true;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002389 }
2390out:
2391 return he;
2392}
2393
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002394static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002395 struct rb_root_cached *root,
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002396 struct hist_entry *pair)
2397{
2398 struct rb_node **p;
2399 struct rb_node *parent = NULL;
2400 struct hist_entry *he;
2401 struct perf_hpp_fmt *fmt;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002402 bool leftmost = true;
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002403
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002404 p = &root->rb_root.rb_node;
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002405 while (*p != NULL) {
2406 int64_t cmp = 0;
2407
2408 parent = *p;
2409 he = rb_entry(parent, struct hist_entry, rb_node_in);
2410
2411 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2412 cmp = fmt->collapse(fmt, he, pair);
2413 if (cmp)
2414 break;
2415 }
2416 if (!cmp)
2417 goto out;
2418
2419 if (cmp < 0)
2420 p = &parent->rb_left;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002421 else {
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002422 p = &parent->rb_right;
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002423 leftmost = false;
2424 }
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002425 }
2426
2427 he = hist_entry__new(pair, true);
2428 if (he) {
2429 rb_link_node(&he->rb_node_in, parent, p);
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002430 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002431
2432 he->dummy = true;
2433 he->hists = hists;
2434 memset(&he->stat, 0, sizeof(he->stat));
2435 hists__inc_stats(hists, he);
2436 }
2437out:
2438 return he;
2439}
2440
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002441static struct hist_entry *hists__find_entry(struct hists *hists,
2442 struct hist_entry *he)
2443{
Namhyung Kimce74f602012-12-10 17:29:55 +09002444 struct rb_node *n;
2445
Jiri Olsa52225032016-05-03 13:54:42 +02002446 if (hists__has(hists, need_collapse))
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002447 n = hists->entries_collapsed.rb_root.rb_node;
Namhyung Kimce74f602012-12-10 17:29:55 +09002448 else
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002449 n = hists->entries_in->rb_root.rb_node;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002450
2451 while (n) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002452 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2453 int64_t cmp = hist_entry__collapse(iter, he);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002454
2455 if (cmp < 0)
2456 n = n->rb_left;
2457 else if (cmp > 0)
2458 n = n->rb_right;
2459 else
2460 return iter;
2461 }
2462
2463 return NULL;
2464}
2465
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002466static struct hist_entry *hists__find_hierarchy_entry(struct rb_root_cached *root,
Namhyung Kim09034de2016-09-13 16:45:46 +09002467 struct hist_entry *he)
2468{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002469 struct rb_node *n = root->rb_root.rb_node;
Namhyung Kim09034de2016-09-13 16:45:46 +09002470
2471 while (n) {
2472 struct hist_entry *iter;
2473 struct perf_hpp_fmt *fmt;
2474 int64_t cmp = 0;
2475
2476 iter = rb_entry(n, struct hist_entry, rb_node_in);
2477 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2478 cmp = fmt->collapse(fmt, iter, he);
2479 if (cmp)
2480 break;
2481 }
2482
2483 if (cmp < 0)
2484 n = n->rb_left;
2485 else if (cmp > 0)
2486 n = n->rb_right;
2487 else
2488 return iter;
2489 }
2490
2491 return NULL;
2492}
2493
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002494static void hists__match_hierarchy(struct rb_root_cached *leader_root,
2495 struct rb_root_cached *other_root)
Namhyung Kim09034de2016-09-13 16:45:46 +09002496{
2497 struct rb_node *nd;
2498 struct hist_entry *pos, *pair;
2499
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002500 for (nd = rb_first_cached(leader_root); nd; nd = rb_next(nd)) {
Namhyung Kim09034de2016-09-13 16:45:46 +09002501 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2502 pair = hists__find_hierarchy_entry(other_root, pos);
2503
2504 if (pair) {
2505 hist_entry__add_pair(pair, pos);
2506 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2507 }
2508 }
2509}
2510
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002511/*
2512 * Look for pairs to link to the leader buckets (hist_entries):
2513 */
2514void hists__match(struct hists *leader, struct hists *other)
2515{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002516 struct rb_root_cached *root;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002517 struct rb_node *nd;
Namhyung Kimbe5863b2019-08-28 08:15:55 +09002518 struct hist_entry *pos, *pair;
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002519
Namhyung Kim09034de2016-09-13 16:45:46 +09002520 if (symbol_conf.report_hierarchy) {
2521 /* hierarchy report always collapses entries */
2522 return hists__match_hierarchy(&leader->entries_collapsed,
2523 &other->entries_collapsed);
2524 }
2525
Jiri Olsa52225032016-05-03 13:54:42 +02002526 if (hists__has(leader, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002527 root = &leader->entries_collapsed;
2528 else
2529 root = leader->entries_in;
2530
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002531 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002532 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002533 pair = hists__find_entry(other, pos);
2534
Namhyung Kimbe5863b2019-08-28 08:15:55 +09002535 if (pair)
Namhyung Kim5fa90412012-11-29 15:38:34 +09002536 hist_entry__add_pair(pair, pos);
Arnaldo Carvalho de Melo95529be2012-11-08 17:54:33 -03002537 }
2538}
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002539
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002540static int hists__link_hierarchy(struct hists *leader_hists,
2541 struct hist_entry *parent,
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002542 struct rb_root_cached *leader_root,
2543 struct rb_root_cached *other_root)
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002544{
2545 struct rb_node *nd;
2546 struct hist_entry *pos, *leader;
2547
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002548 for (nd = rb_first_cached(other_root); nd; nd = rb_next(nd)) {
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002549 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2550
2551 if (hist_entry__has_pairs(pos)) {
2552 bool found = false;
2553
2554 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2555 if (leader->hists == leader_hists) {
2556 found = true;
2557 break;
2558 }
2559 }
2560 if (!found)
2561 return -1;
2562 } else {
2563 leader = add_dummy_hierarchy_entry(leader_hists,
2564 leader_root, pos);
2565 if (leader == NULL)
2566 return -1;
2567
2568 /* do not point parent in the pos */
2569 leader->parent_he = parent;
2570
2571 hist_entry__add_pair(pos, leader);
2572 }
2573
2574 if (!pos->leaf) {
2575 if (hists__link_hierarchy(leader_hists, leader,
2576 &leader->hroot_in,
2577 &pos->hroot_in) < 0)
2578 return -1;
2579 }
2580 }
2581 return 0;
2582}
2583
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002584/*
2585 * Look for entries in the other hists that are not present in the leader, if
2586 * we find them, just add a dummy entry on the leader hists, with period=0,
2587 * nr_events=0, to serve as the list header.
2588 */
2589int hists__link(struct hists *leader, struct hists *other)
2590{
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002591 struct rb_root_cached *root;
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002592 struct rb_node *nd;
2593 struct hist_entry *pos, *pair;
2594
Namhyung Kim9d97b8f2016-09-13 16:45:47 +09002595 if (symbol_conf.report_hierarchy) {
2596 /* hierarchy report always collapses entries */
2597 return hists__link_hierarchy(leader, NULL,
2598 &leader->entries_collapsed,
2599 &other->entries_collapsed);
2600 }
2601
Jiri Olsa52225032016-05-03 13:54:42 +02002602 if (hists__has(other, need_collapse))
Namhyung Kimce74f602012-12-10 17:29:55 +09002603 root = &other->entries_collapsed;
2604 else
2605 root = other->entries_in;
2606
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002607 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
Namhyung Kimce74f602012-12-10 17:29:55 +09002608 pos = rb_entry(nd, struct hist_entry, rb_node_in);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002609
2610 if (!hist_entry__has_pairs(pos)) {
2611 pair = hists__add_dummy_entry(leader, pos);
2612 if (pair == NULL)
2613 return -1;
Namhyung Kim5fa90412012-11-29 15:38:34 +09002614 hist_entry__add_pair(pos, pair);
Arnaldo Carvalho de Melo494d70a2012-11-08 18:03:09 -03002615 }
2616 }
2617
2618 return 0;
2619}
Namhyung Kimf2148332014-01-14 11:52:48 +09002620
Namhyung Kimbe5863b2019-08-28 08:15:55 +09002621int hists__unlink(struct hists *hists)
2622{
2623 struct rb_root_cached *root;
2624 struct rb_node *nd;
2625 struct hist_entry *pos;
2626
2627 if (hists__has(hists, need_collapse))
2628 root = &hists->entries_collapsed;
2629 else
2630 root = hists->entries_in;
2631
2632 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2633 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2634 list_del_init(&pos->pairs.node);
2635 }
2636
2637 return 0;
2638}
2639
Andi Kleen578499982015-07-18 08:24:49 -07002640void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
Jin Yao7841f402019-11-07 15:47:15 +08002641 struct perf_sample *sample, bool nonany_branch_mode,
2642 u64 *total_cycles)
Andi Kleen578499982015-07-18 08:24:49 -07002643{
2644 struct branch_info *bi;
Kan Liang42bbabe2020-02-28 08:30:00 -08002645 struct branch_entry *entries = perf_sample__branch_entries(sample);
Andi Kleen578499982015-07-18 08:24:49 -07002646
2647 /* If we have branch cycles always annotate them. */
Kan Liang42bbabe2020-02-28 08:30:00 -08002648 if (bs && bs->nr && entries[0].flags.cycles) {
Andi Kleen578499982015-07-18 08:24:49 -07002649 int i;
2650
2651 bi = sample__resolve_bstack(sample, al);
2652 if (bi) {
2653 struct addr_map_symbol *prev = NULL;
2654
2655 /*
2656 * Ignore errors, still want to process the
2657 * other entries.
2658 *
2659 * For non standard branch modes always
2660 * force no IPC (prev == NULL)
2661 *
2662 * Note that perf stores branches reversed from
2663 * program order!
2664 */
2665 for (i = bs->nr - 1; i >= 0; i--) {
2666 addr_map_symbol__account_cycles(&bi[i].from,
2667 nonany_branch_mode ? NULL : prev,
2668 bi[i].flags.cycles);
2669 prev = &bi[i].to;
Jin Yao7841f402019-11-07 15:47:15 +08002670
2671 if (total_cycles)
2672 *total_cycles += bi[i].flags.cycles;
Andi Kleen578499982015-07-18 08:24:49 -07002673 }
2674 free(bi);
2675 }
2676 }
2677}
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002678
Namhyung Kim2775de02021-04-26 18:37:15 -07002679size_t evlist__fprintf_nr_events(struct evlist *evlist, FILE *fp,
2680 bool skip_empty)
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002681{
Jiri Olsa32dcd022019-07-21 13:23:51 +02002682 struct evsel *pos;
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002683 size_t ret = 0;
2684
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002685 evlist__for_each_entry(evlist, pos) {
Namhyung Kim0f0abba2021-04-26 18:37:13 -07002686 struct hists *hists = evsel__hists(pos);
2687
Namhyung Kim2775de02021-04-26 18:37:15 -07002688 if (skip_empty && !hists->stats.nr_samples)
2689 continue;
2690
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03002691 ret += fprintf(fp, "%s stats:\n", evsel__name(pos));
Namhyung Kim0f0abba2021-04-26 18:37:13 -07002692 ret += fprintf(fp, "%16s events: %10d\n",
2693 "SAMPLE", hists->stats.nr_samples);
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -03002694 }
2695
2696 return ret;
2697}
2698
2699
Namhyung Kimf2148332014-01-14 11:52:48 +09002700u64 hists__total_period(struct hists *hists)
2701{
2702 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2703 hists->stats.total_period;
2704}
Namhyung Kim33db4562014-02-07 12:06:07 +09002705
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002706int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq)
2707{
2708 char unit;
2709 int printed;
2710 const struct dso *dso = hists->dso_filter;
Namhyung Kim7cb10a082019-05-27 15:11:49 +09002711 struct thread *thread = hists->thread_filter;
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002712 int socket_id = hists->socket_filter;
Namhyung Kim0f0abba2021-04-26 18:37:13 -07002713 unsigned long nr_samples = hists->stats.nr_samples;
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002714 u64 nr_events = hists->stats.total_period;
Jiri Olsa32dcd022019-07-21 13:23:51 +02002715 struct evsel *evsel = hists_to_evsel(hists);
Arnaldo Carvalho de Melo8ab2e962020-04-29 16:07:09 -03002716 const char *ev_name = evsel__name(evsel);
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002717 char buf[512], sample_freq_str[64] = "";
2718 size_t buflen = sizeof(buf);
2719 char ref[30] = " show reference callgraph, ";
2720 bool enable_ref = false;
2721
2722 if (symbol_conf.filter_relative) {
2723 nr_samples = hists->stats.nr_non_filtered_samples;
2724 nr_events = hists->stats.total_non_filtered_period;
2725 }
2726
Arnaldo Carvalho de Meloc754c382020-04-30 10:51:16 -03002727 if (evsel__is_group_event(evsel)) {
Jiri Olsa32dcd022019-07-21 13:23:51 +02002728 struct evsel *pos;
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002729
Arnaldo Carvalho de Melo347c7512020-04-29 16:09:12 -03002730 evsel__group_desc(evsel, buf, buflen);
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002731 ev_name = buf;
2732
2733 for_each_group_member(pos, evsel) {
2734 struct hists *pos_hists = evsel__hists(pos);
2735
2736 if (symbol_conf.filter_relative) {
2737 nr_samples += pos_hists->stats.nr_non_filtered_samples;
2738 nr_events += pos_hists->stats.total_non_filtered_period;
2739 } else {
Namhyung Kim0f0abba2021-04-26 18:37:13 -07002740 nr_samples += pos_hists->stats.nr_samples;
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002741 nr_events += pos_hists->stats.total_period;
2742 }
2743 }
2744 }
2745
2746 if (symbol_conf.show_ref_callgraph &&
2747 strstr(ev_name, "call-graph=no"))
2748 enable_ref = true;
2749
2750 if (show_freq)
Jiri Olsa1fc632c2019-07-21 13:24:29 +02002751 scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->core.attr.sample_freq);
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002752
2753 nr_samples = convert_unit(nr_samples, &unit);
2754 printed = scnprintf(bf, size,
2755 "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64,
Jiri Olsa5643b1a2019-07-21 13:24:46 +02002756 nr_samples, unit, evsel->core.nr_members > 1 ? "s" : "",
Arnaldo Carvalho de Melo25c312d2018-04-02 14:24:28 -03002757 ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events);
2758
2759
2760 if (hists->uid_filter_str)
2761 printed += snprintf(bf + printed, size - printed,
2762 ", UID: %s", hists->uid_filter_str);
2763 if (thread) {
2764 if (hists__has(hists, thread)) {
2765 printed += scnprintf(bf + printed, size - printed,
2766 ", Thread: %s(%d)",
2767 (thread->comm_set ? thread__comm_str(thread) : ""),
2768 thread->tid);
2769 } else {
2770 printed += scnprintf(bf + printed, size - printed,
2771 ", Thread: %s",
2772 (thread->comm_set ? thread__comm_str(thread) : ""));
2773 }
2774 }
2775 if (dso)
2776 printed += scnprintf(bf + printed, size - printed,
2777 ", DSO: %s", dso->short_name);
2778 if (socket_id > -1)
2779 printed += scnprintf(bf + printed, size - printed,
2780 ", Processor Socket: %d", socket_id);
2781
2782 return printed;
2783}
2784
Namhyung Kim33db4562014-02-07 12:06:07 +09002785int parse_filter_percentage(const struct option *opt __maybe_unused,
2786 const char *arg, int unset __maybe_unused)
2787{
2788 if (!strcmp(arg, "relative"))
2789 symbol_conf.filter_relative = true;
2790 else if (!strcmp(arg, "absolute"))
2791 symbol_conf.filter_relative = false;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002792 else {
Colin Ian Kinga596a872017-03-30 10:54:40 +01002793 pr_debug("Invalid percentage: %s\n", arg);
Namhyung Kim33db4562014-02-07 12:06:07 +09002794 return -1;
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03002795 }
Namhyung Kim33db4562014-02-07 12:06:07 +09002796
2797 return 0;
2798}
Namhyung Kim0b93da12014-01-14 12:02:15 +09002799
2800int perf_hist_config(const char *var, const char *value)
2801{
2802 if (!strcmp(var, "hist.percentage"))
2803 return parse_filter_percentage(NULL, value, 0);
2804
2805 return 0;
2806}
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002807
Jiri Olsa5b658552016-01-18 10:24:22 +01002808int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002809{
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002810 memset(hists, 0, sizeof(*hists));
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002811 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT_CACHED;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002812 hists->entries_in = &hists->entries_in_array[0];
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002813 hists->entries_collapsed = RB_ROOT_CACHED;
2814 hists->entries = RB_ROOT_CACHED;
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002815 pthread_mutex_init(&hists->lock, NULL);
Kan Liang21394d92015-09-04 10:45:44 -04002816 hists->socket_filter = -1;
Jiri Olsa5b658552016-01-18 10:24:22 +01002817 hists->hpp_list = hpp_list;
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002818 INIT_LIST_HEAD(&hists->hpp_formats);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002819 return 0;
2820}
2821
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002822static void hists__delete_remaining_entries(struct rb_root_cached *root)
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002823{
2824 struct rb_node *node;
2825 struct hist_entry *he;
2826
Davidlohr Bueso2eb3d682018-12-06 11:18:18 -08002827 while (!RB_EMPTY_ROOT(&root->rb_root)) {
2828 node = rb_first_cached(root);
2829 rb_erase_cached(node, root);
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002830
2831 he = rb_entry(node, struct hist_entry, rb_node_in);
2832 hist_entry__delete(he);
2833 }
2834}
2835
2836static void hists__delete_all_entries(struct hists *hists)
2837{
2838 hists__delete_entries(hists);
2839 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2840 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2841 hists__delete_remaining_entries(&hists->entries_collapsed);
2842}
2843
Jiri Olsa32dcd022019-07-21 13:23:51 +02002844static void hists_evsel__exit(struct evsel *evsel)
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002845{
2846 struct hists *hists = evsel__hists(evsel);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002847 struct perf_hpp_fmt *fmt, *pos;
2848 struct perf_hpp_list_node *node, *tmp;
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002849
Namhyung Kim61fa0e92015-12-10 16:53:20 +09002850 hists__delete_all_entries(hists);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002851
2852 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2853 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03002854 list_del_init(&fmt->list);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002855 free(fmt);
2856 }
Arnaldo Carvalho de Meloe56fbc92019-07-04 12:13:46 -03002857 list_del_init(&node->list);
Namhyung Kimc3bc0c42016-03-07 16:44:45 -03002858 free(node);
2859 }
Masami Hiramatsu17577de2015-12-09 11:11:29 +09002860}
2861
Jiri Olsa32dcd022019-07-21 13:23:51 +02002862static int hists_evsel__init(struct evsel *evsel)
Namhyung Kimfc284be2016-01-07 10:14:10 +01002863{
2864 struct hists *hists = evsel__hists(evsel);
2865
Jiri Olsa5b658552016-01-18 10:24:22 +01002866 __hists__init(hists, &perf_hpp_list);
Namhyung Kimfc284be2016-01-07 10:14:10 +01002867 return 0;
2868}
2869
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002870/*
2871 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2872 * stored in the rbtree...
2873 */
2874
2875int hists__init(void)
2876{
Arnaldo Carvalho de Melo4c703822020-05-06 12:55:06 -03002877 int err = evsel__object_config(sizeof(struct hists_evsel),
2878 hists_evsel__init, hists_evsel__exit);
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -03002879 if (err)
2880 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2881
2882 return err;
2883}
Jiri Olsa94b3dc32016-01-18 10:24:13 +01002884
2885void perf_hpp_list__init(struct perf_hpp_list *list)
2886{
2887 INIT_LIST_HEAD(&list->fields);
2888 INIT_LIST_HEAD(&list->sorts);
2889}