xref: /linux/tools/perf/util/hist.c (revision 784e8adda4cdb3e2510742023729851b6c08803c)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "callchain.h"
3 #include "debug.h"
4 #include "dso.h"
5 #include "build-id.h"
6 #include "hist.h"
7 #include "map.h"
8 #include "map_symbol.h"
9 #include "branch.h"
10 #include "mem-events.h"
11 #include "session.h"
12 #include "namespaces.h"
13 #include "cgroup.h"
14 #include "sort.h"
15 #include "units.h"
16 #include "evlist.h"
17 #include "evsel.h"
18 #include "annotate.h"
19 #include "srcline.h"
20 #include "symbol.h"
21 #include "thread.h"
22 #include "block-info.h"
23 #include "ui/progress.h"
24 #include <errno.h>
25 #include <math.h>
26 #include <inttypes.h>
27 #include <sys/param.h>
28 #include <linux/rbtree.h>
29 #include <linux/string.h>
30 #include <linux/time64.h>
31 #include <linux/zalloc.h>
32 
33 static bool hists__filter_entry_by_dso(struct hists *hists,
34 				       struct hist_entry *he);
35 static bool hists__filter_entry_by_thread(struct hists *hists,
36 					  struct hist_entry *he);
37 static bool hists__filter_entry_by_symbol(struct hists *hists,
38 					  struct hist_entry *he);
39 static bool hists__filter_entry_by_socket(struct hists *hists,
40 					  struct hist_entry *he);
41 
42 u16 hists__col_len(struct hists *hists, enum hist_column col)
43 {
44 	return hists->col_len[col];
45 }
46 
47 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
48 {
49 	hists->col_len[col] = len;
50 }
51 
52 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
53 {
54 	if (len > hists__col_len(hists, col)) {
55 		hists__set_col_len(hists, col, len);
56 		return true;
57 	}
58 	return false;
59 }
60 
61 void hists__reset_col_len(struct hists *hists)
62 {
63 	enum hist_column col;
64 
65 	for (col = 0; col < HISTC_NR_COLS; ++col)
66 		hists__set_col_len(hists, col, 0);
67 }
68 
69 static 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 
79 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
80 {
81 	const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
82 	int symlen;
83 	u16 len;
84 
85 	if (h->block_info)
86 		return;
87 	/*
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;
94 		if (verbose > 0)
95 			symlen += BITS_PER_LONG / 4 + 2 + 3;
96 		hists__new_col_len(hists, HISTC_SYMBOL, symlen);
97 	} else {
98 		symlen = unresolved_col_width + 4 + 2;
99 		hists__new_col_len(hists, HISTC_SYMBOL, symlen);
100 		hists__set_unres_dso_col_len(hists, HISTC_DSO);
101 	}
102 
103 	len = thread__comm_len(h->thread);
104 	if (hists__new_col_len(hists, HISTC_COMM, len))
105 		hists__set_col_len(hists, HISTC_THREAD, len + 8);
106 
107 	if (h->ms.map) {
108 		len = dso__name_len(h->ms.map->dso);
109 		hists__new_col_len(hists, HISTC_DSO, len);
110 	}
111 
112 	if (h->parent)
113 		hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
114 
115 	if (h->branch_info) {
116 		if (h->branch_info->from.ms.sym) {
117 			symlen = (int)h->branch_info->from.ms.sym->namelen + 4;
118 			if (verbose > 0)
119 				symlen += BITS_PER_LONG / 4 + 2 + 3;
120 			hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
121 
122 			symlen = dso__name_len(h->branch_info->from.ms.map->dso);
123 			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 
130 		if (h->branch_info->to.ms.sym) {
131 			symlen = (int)h->branch_info->to.ms.sym->namelen + 4;
132 			if (verbose > 0)
133 				symlen += BITS_PER_LONG / 4 + 2 + 3;
134 			hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
135 
136 			symlen = dso__name_len(h->branch_info->to.ms.map->dso);
137 			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 		}
143 
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));
150 	}
151 
152 	if (h->mem_info) {
153 		if (h->mem_info->daddr.ms.sym) {
154 			symlen = (int)h->mem_info->daddr.ms.sym->namelen + 4
155 			       + unresolved_col_width + 2;
156 			hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
157 					   symlen);
158 			hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
159 					   symlen + 1);
160 		} else {
161 			symlen = unresolved_col_width + 4 + 2;
162 			hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
163 					   symlen);
164 			hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
165 					   symlen);
166 		}
167 
168 		if (h->mem_info->iaddr.ms.sym) {
169 			symlen = (int)h->mem_info->iaddr.ms.sym->namelen + 4
170 			       + 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 
179 		if (h->mem_info->daddr.ms.map) {
180 			symlen = dso__name_len(h->mem_info->daddr.ms.map->dso);
181 			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 		}
187 
188 		hists__new_col_len(hists, HISTC_MEM_PHYS_DADDR,
189 				   unresolved_col_width + 4 + 2);
190 
191 		hists__new_col_len(hists, HISTC_MEM_DATA_PAGE_SIZE,
192 				   unresolved_col_width + 4 + 2);
193 
194 	} else {
195 		symlen = unresolved_col_width + 4 + 2;
196 		hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
197 		hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
198 		hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
199 	}
200 
201 	hists__new_col_len(hists, HISTC_CGROUP, 6);
202 	hists__new_col_len(hists, HISTC_CGROUP_ID, 20);
203 	hists__new_col_len(hists, HISTC_CPU, 3);
204 	hists__new_col_len(hists, HISTC_SOCKET, 6);
205 	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);
211 	hists__new_col_len(hists, HISTC_MEM_BLOCKED, 10);
212 	hists__new_col_len(hists, HISTC_LOCAL_INS_LAT, 13);
213 	hists__new_col_len(hists, HISTC_GLOBAL_INS_LAT, 13);
214 	hists__new_col_len(hists, HISTC_P_STAGE_CYC, 13);
215 	if (symbol_conf.nanosecs)
216 		hists__new_col_len(hists, HISTC_TIME, 16);
217 	else
218 		hists__new_col_len(hists, HISTC_TIME, 12);
219 	hists__new_col_len(hists, HISTC_CODE_PAGE_SIZE, 6);
220 
221 	if (h->srcline) {
222 		len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
223 		hists__new_col_len(hists, HISTC_SRCLINE, len);
224 	}
225 
226 	if (h->srcfile)
227 		hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
228 
229 	if (h->transaction)
230 		hists__new_col_len(hists, HISTC_TRANSACTION,
231 				   hist_entry__transaction_len());
232 
233 	if (h->trace_output)
234 		hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
235 
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 	}
245 }
246 
247 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
248 {
249 	struct rb_node *next = rb_first_cached(&hists->entries);
250 	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 
263 static void he_stat__add_cpumode_period(struct he_stat *he_stat,
264 					unsigned int cpumode, u64 period)
265 {
266 	switch (cpumode) {
267 	case PERF_RECORD_MISC_KERNEL:
268 		he_stat->period_sys += period;
269 		break;
270 	case PERF_RECORD_MISC_USER:
271 		he_stat->period_us += period;
272 		break;
273 	case PERF_RECORD_MISC_GUEST_KERNEL:
274 		he_stat->period_guest_sys += period;
275 		break;
276 	case PERF_RECORD_MISC_GUEST_USER:
277 		he_stat->period_guest_us += period;
278 		break;
279 	default:
280 		break;
281 	}
282 }
283 
284 static 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 
292 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
293 				u64 ins_lat, u64 p_stage_cyc)
294 {
295 	he_stat->period		+= period;
296 	he_stat->nr_events	+= 1;
297 	he_stat->ins_lat	+= ins_lat;
298 	he_stat->p_stage_cyc	+= p_stage_cyc;
299 }
300 
301 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
302 {
303 	dest->period		+= src->period;
304 	dest->period_sys	+= src->period_sys;
305 	dest->period_us		+= src->period_us;
306 	dest->period_guest_sys	+= src->period_guest_sys;
307 	dest->period_guest_us	+= src->period_guest_us;
308 	dest->nr_events		+= src->nr_events;
309 	dest->ins_lat		+= src->ins_lat;
310 	dest->p_stage_cyc	+= src->p_stage_cyc;
311 }
312 
313 static void he_stat__decay(struct he_stat *he_stat)
314 {
315 	he_stat->period = (he_stat->period * 7) / 8;
316 	he_stat->nr_events = (he_stat->nr_events * 7) / 8;
317 	/* XXX need decay for weight too? */
318 }
319 
320 static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
321 
322 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
323 {
324 	u64 prev_period = he->stat.period;
325 	u64 diff;
326 
327 	if (prev_period == 0)
328 		return true;
329 
330 	he_stat__decay(&he->stat);
331 	if (symbol_conf.cumulate_callchain)
332 		he_stat__decay(he->stat_acc);
333 	decay_callchain(he->callchain);
334 
335 	diff = prev_period - he->stat.period;
336 
337 	if (!he->depth) {
338 		hists->stats.total_period -= diff;
339 		if (!he->filtered)
340 			hists->stats.total_non_filtered_period -= diff;
341 	}
342 
343 	if (!he->leaf) {
344 		struct hist_entry *child;
345 		struct rb_node *node = rb_first_cached(&he->hroot_out);
346 		while (node) {
347 			child = rb_entry(node, struct hist_entry, rb_node);
348 			node = rb_next(node);
349 
350 			if (hists__decay_entry(hists, child))
351 				hists__delete_entry(hists, child);
352 		}
353 	}
354 
355 	return he->stat.period == 0;
356 }
357 
358 static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
359 {
360 	struct rb_root_cached *root_in;
361 	struct rb_root_cached *root_out;
362 
363 	if (he->parent_he) {
364 		root_in  = &he->parent_he->hroot_in;
365 		root_out = &he->parent_he->hroot_out;
366 	} else {
367 		if (hists__has(hists, need_collapse))
368 			root_in = &hists->entries_collapsed;
369 		else
370 			root_in = hists->entries_in;
371 		root_out = &hists->entries;
372 	}
373 
374 	rb_erase_cached(&he->rb_node_in, root_in);
375 	rb_erase_cached(&he->rb_node, root_out);
376 
377 	--hists->nr_entries;
378 	if (!he->filtered)
379 		--hists->nr_non_filtered_entries;
380 
381 	hist_entry__delete(he);
382 }
383 
384 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
385 {
386 	struct rb_node *next = rb_first_cached(&hists->entries);
387 	struct hist_entry *n;
388 
389 	while (next) {
390 		n = rb_entry(next, struct hist_entry, rb_node);
391 		next = rb_next(&n->rb_node);
392 		if (((zap_user && n->level == '.') ||
393 		     (zap_kernel && n->level != '.') ||
394 		     hists__decay_entry(hists, n))) {
395 			hists__delete_entry(hists, n);
396 		}
397 	}
398 }
399 
400 void hists__delete_entries(struct hists *hists)
401 {
402 	struct rb_node *next = rb_first_cached(&hists->entries);
403 	struct hist_entry *n;
404 
405 	while (next) {
406 		n = rb_entry(next, struct hist_entry, rb_node);
407 		next = rb_next(&n->rb_node);
408 
409 		hists__delete_entry(hists, n);
410 	}
411 }
412 
413 struct hist_entry *hists__get_entry(struct hists *hists, int idx)
414 {
415 	struct rb_node *next = rb_first_cached(&hists->entries);
416 	struct hist_entry *n;
417 	int i = 0;
418 
419 	while (next) {
420 		n = rb_entry(next, struct hist_entry, rb_node);
421 		if (i == idx)
422 			return n;
423 
424 		next = rb_next(&n->rb_node);
425 		i++;
426 	}
427 
428 	return NULL;
429 }
430 
431 /*
432  * histogram, sorted on item, collects periods
433  */
434 
435 static int hist_entry__init(struct hist_entry *he,
436 			    struct hist_entry *template,
437 			    bool sample_self,
438 			    size_t callchain_size)
439 {
440 	*he = *template;
441 	he->callchain_size = callchain_size;
442 
443 	if (symbol_conf.cumulate_callchain) {
444 		he->stat_acc = malloc(sizeof(he->stat));
445 		if (he->stat_acc == NULL)
446 			return -ENOMEM;
447 		memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
448 		if (!sample_self)
449 			memset(&he->stat, 0, sizeof(he->stat));
450 	}
451 
452 	map__get(he->ms.map);
453 
454 	if (he->branch_info) {
455 		/*
456 		 * This branch info is (a part of) allocated from
457 		 * sample__resolve_bstack() and will be freed after
458 		 * adding new entries.  So we need to save a copy.
459 		 */
460 		he->branch_info = malloc(sizeof(*he->branch_info));
461 		if (he->branch_info == NULL)
462 			goto err;
463 
464 		memcpy(he->branch_info, template->branch_info,
465 		       sizeof(*he->branch_info));
466 
467 		map__get(he->branch_info->from.ms.map);
468 		map__get(he->branch_info->to.ms.map);
469 	}
470 
471 	if (he->mem_info) {
472 		map__get(he->mem_info->iaddr.ms.map);
473 		map__get(he->mem_info->daddr.ms.map);
474 	}
475 
476 	if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
477 		callchain_init(he->callchain);
478 
479 	if (he->raw_data) {
480 		he->raw_data = memdup(he->raw_data, he->raw_size);
481 		if (he->raw_data == NULL)
482 			goto err_infos;
483 	}
484 
485 	if (he->srcline) {
486 		he->srcline = strdup(he->srcline);
487 		if (he->srcline == NULL)
488 			goto err_rawdata;
489 	}
490 
491 	if (symbol_conf.res_sample) {
492 		he->res_samples = calloc(sizeof(struct res_sample),
493 					symbol_conf.res_sample);
494 		if (!he->res_samples)
495 			goto err_srcline;
496 	}
497 
498 	INIT_LIST_HEAD(&he->pairs.node);
499 	thread__get(he->thread);
500 	he->hroot_in  = RB_ROOT_CACHED;
501 	he->hroot_out = RB_ROOT_CACHED;
502 
503 	if (!symbol_conf.report_hierarchy)
504 		he->leaf = true;
505 
506 	return 0;
507 
508 err_srcline:
509 	zfree(&he->srcline);
510 
511 err_rawdata:
512 	zfree(&he->raw_data);
513 
514 err_infos:
515 	if (he->branch_info) {
516 		map__put(he->branch_info->from.ms.map);
517 		map__put(he->branch_info->to.ms.map);
518 		zfree(&he->branch_info);
519 	}
520 	if (he->mem_info) {
521 		map__put(he->mem_info->iaddr.ms.map);
522 		map__put(he->mem_info->daddr.ms.map);
523 	}
524 err:
525 	map__zput(he->ms.map);
526 	zfree(&he->stat_acc);
527 	return -ENOMEM;
528 }
529 
530 static void *hist_entry__zalloc(size_t size)
531 {
532 	return zalloc(size + sizeof(struct hist_entry));
533 }
534 
535 static void hist_entry__free(void *ptr)
536 {
537 	free(ptr);
538 }
539 
540 static struct hist_entry_ops default_ops = {
541 	.new	= hist_entry__zalloc,
542 	.free	= hist_entry__free,
543 };
544 
545 static struct hist_entry *hist_entry__new(struct hist_entry *template,
546 					  bool sample_self)
547 {
548 	struct hist_entry_ops *ops = template->ops;
549 	size_t callchain_size = 0;
550 	struct hist_entry *he;
551 	int err = 0;
552 
553 	if (!ops)
554 		ops = template->ops = &default_ops;
555 
556 	if (symbol_conf.use_callchain)
557 		callchain_size = sizeof(struct callchain_root);
558 
559 	he = ops->new(callchain_size);
560 	if (he) {
561 		err = hist_entry__init(he, template, sample_self, callchain_size);
562 		if (err) {
563 			ops->free(he);
564 			he = NULL;
565 		}
566 	}
567 
568 	return he;
569 }
570 
571 static u8 symbol__parent_filter(const struct symbol *parent)
572 {
573 	if (symbol_conf.exclude_other && parent == NULL)
574 		return 1 << HIST_FILTER__PARENT;
575 	return 0;
576 }
577 
578 static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
579 {
580 	if (!hist_entry__has_callchains(he) || !symbol_conf.use_callchain)
581 		return;
582 
583 	he->hists->callchain_period += period;
584 	if (!he->filtered)
585 		he->hists->callchain_non_filtered_period += period;
586 }
587 
588 static struct hist_entry *hists__findnew_entry(struct hists *hists,
589 					       struct hist_entry *entry,
590 					       struct addr_location *al,
591 					       bool sample_self)
592 {
593 	struct rb_node **p;
594 	struct rb_node *parent = NULL;
595 	struct hist_entry *he;
596 	int64_t cmp;
597 	u64 period = entry->stat.period;
598 	u64 ins_lat = entry->stat.ins_lat;
599 	u64 p_stage_cyc = entry->stat.p_stage_cyc;
600 	bool leftmost = true;
601 
602 	p = &hists->entries_in->rb_root.rb_node;
603 
604 	while (*p != NULL) {
605 		parent = *p;
606 		he = rb_entry(parent, struct hist_entry, rb_node_in);
607 
608 		/*
609 		 * Make sure that it receives arguments in a same order as
610 		 * hist_entry__collapse() so that we can use an appropriate
611 		 * function when searching an entry regardless which sort
612 		 * keys were used.
613 		 */
614 		cmp = hist_entry__cmp(he, entry);
615 
616 		if (!cmp) {
617 			if (sample_self) {
618 				he_stat__add_period(&he->stat, period, ins_lat, p_stage_cyc);
619 				hist_entry__add_callchain_period(he, period);
620 			}
621 			if (symbol_conf.cumulate_callchain)
622 				he_stat__add_period(he->stat_acc, period, ins_lat, p_stage_cyc);
623 
624 			/*
625 			 * This mem info was allocated from sample__resolve_mem
626 			 * and will not be used anymore.
627 			 */
628 			mem_info__zput(entry->mem_info);
629 
630 			block_info__zput(entry->block_info);
631 
632 			/* If the map of an existing hist_entry has
633 			 * become out-of-date due to an exec() or
634 			 * similar, update it.  Otherwise we will
635 			 * mis-adjust symbol addresses when computing
636 			 * the history counter to increment.
637 			 */
638 			if (he->ms.map != entry->ms.map) {
639 				map__put(he->ms.map);
640 				he->ms.map = map__get(entry->ms.map);
641 			}
642 			goto out;
643 		}
644 
645 		if (cmp < 0)
646 			p = &(*p)->rb_left;
647 		else {
648 			p = &(*p)->rb_right;
649 			leftmost = false;
650 		}
651 	}
652 
653 	he = hist_entry__new(entry, sample_self);
654 	if (!he)
655 		return NULL;
656 
657 	if (sample_self)
658 		hist_entry__add_callchain_period(he, period);
659 	hists->nr_entries++;
660 
661 	rb_link_node(&he->rb_node_in, parent, p);
662 	rb_insert_color_cached(&he->rb_node_in, hists->entries_in, leftmost);
663 out:
664 	if (sample_self)
665 		he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
666 	if (symbol_conf.cumulate_callchain)
667 		he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
668 	return he;
669 }
670 
671 static unsigned random_max(unsigned high)
672 {
673 	unsigned thresh = -high % high;
674 	for (;;) {
675 		unsigned r = random();
676 		if (r >= thresh)
677 			return r % high;
678 	}
679 }
680 
681 static void hists__res_sample(struct hist_entry *he, struct perf_sample *sample)
682 {
683 	struct res_sample *r;
684 	int j;
685 
686 	if (he->num_res < symbol_conf.res_sample) {
687 		j = he->num_res++;
688 	} else {
689 		j = random_max(symbol_conf.res_sample);
690 	}
691 	r = &he->res_samples[j];
692 	r->time = sample->time;
693 	r->cpu = sample->cpu;
694 	r->tid = sample->tid;
695 }
696 
697 static struct hist_entry*
698 __hists__add_entry(struct hists *hists,
699 		   struct addr_location *al,
700 		   struct symbol *sym_parent,
701 		   struct branch_info *bi,
702 		   struct mem_info *mi,
703 		   struct block_info *block_info,
704 		   struct perf_sample *sample,
705 		   bool sample_self,
706 		   struct hist_entry_ops *ops)
707 {
708 	struct namespaces *ns = thread__namespaces(al->thread);
709 	struct hist_entry entry = {
710 		.thread	= al->thread,
711 		.comm = thread__comm(al->thread),
712 		.cgroup_id = {
713 			.dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0,
714 			.ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0,
715 		},
716 		.cgroup = sample->cgroup,
717 		.ms = {
718 			.maps	= al->maps,
719 			.map	= al->map,
720 			.sym	= al->sym,
721 		},
722 		.srcline = (char *) al->srcline,
723 		.socket	 = al->socket,
724 		.cpu	 = al->cpu,
725 		.cpumode = al->cpumode,
726 		.ip	 = al->addr,
727 		.level	 = al->level,
728 		.code_page_size = sample->code_page_size,
729 		.stat = {
730 			.nr_events = 1,
731 			.period	= sample->period,
732 			.ins_lat = sample->ins_lat,
733 			.p_stage_cyc = sample->p_stage_cyc,
734 		},
735 		.parent = sym_parent,
736 		.filtered = symbol__parent_filter(sym_parent) | al->filtered,
737 		.hists	= hists,
738 		.branch_info = bi,
739 		.mem_info = mi,
740 		.block_info = block_info,
741 		.transaction = sample->transaction,
742 		.raw_data = sample->raw_data,
743 		.raw_size = sample->raw_size,
744 		.ops = ops,
745 		.time = hist_time(sample->time),
746 		.weight = sample->weight,
747 	}, *he = hists__findnew_entry(hists, &entry, al, sample_self);
748 
749 	if (!hists->has_callchains && he && he->callchain_size != 0)
750 		hists->has_callchains = true;
751 	if (he && symbol_conf.res_sample)
752 		hists__res_sample(he, sample);
753 	return he;
754 }
755 
756 struct hist_entry *hists__add_entry(struct hists *hists,
757 				    struct addr_location *al,
758 				    struct symbol *sym_parent,
759 				    struct branch_info *bi,
760 				    struct mem_info *mi,
761 				    struct perf_sample *sample,
762 				    bool sample_self)
763 {
764 	return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
765 				  sample, sample_self, NULL);
766 }
767 
768 struct hist_entry *hists__add_entry_ops(struct hists *hists,
769 					struct hist_entry_ops *ops,
770 					struct addr_location *al,
771 					struct symbol *sym_parent,
772 					struct branch_info *bi,
773 					struct mem_info *mi,
774 					struct perf_sample *sample,
775 					bool sample_self)
776 {
777 	return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
778 				  sample, sample_self, ops);
779 }
780 
781 struct hist_entry *hists__add_entry_block(struct hists *hists,
782 					  struct addr_location *al,
783 					  struct block_info *block_info)
784 {
785 	struct hist_entry entry = {
786 		.block_info = block_info,
787 		.hists = hists,
788 		.ms = {
789 			.maps = al->maps,
790 			.map = al->map,
791 			.sym = al->sym,
792 		},
793 	}, *he = hists__findnew_entry(hists, &entry, al, false);
794 
795 	return he;
796 }
797 
798 static int
799 iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
800 		    struct addr_location *al __maybe_unused)
801 {
802 	return 0;
803 }
804 
805 static int
806 iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
807 			struct addr_location *al __maybe_unused)
808 {
809 	return 0;
810 }
811 
812 static int
813 iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
814 {
815 	struct perf_sample *sample = iter->sample;
816 	struct mem_info *mi;
817 
818 	mi = sample__resolve_mem(sample, al);
819 	if (mi == NULL)
820 		return -ENOMEM;
821 
822 	iter->priv = mi;
823 	return 0;
824 }
825 
826 static int
827 iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
828 {
829 	u64 cost;
830 	struct mem_info *mi = iter->priv;
831 	struct hists *hists = evsel__hists(iter->evsel);
832 	struct perf_sample *sample = iter->sample;
833 	struct hist_entry *he;
834 
835 	if (mi == NULL)
836 		return -EINVAL;
837 
838 	cost = sample->weight;
839 	if (!cost)
840 		cost = 1;
841 
842 	/*
843 	 * must pass period=weight in order to get the correct
844 	 * sorting from hists__collapse_resort() which is solely
845 	 * based on periods. We want sorting be done on nr_events * weight
846 	 * and this is indirectly achieved by passing period=weight here
847 	 * and the he_stat__add_period() function.
848 	 */
849 	sample->period = cost;
850 
851 	he = hists__add_entry(hists, al, iter->parent, NULL, mi,
852 			      sample, true);
853 	if (!he)
854 		return -ENOMEM;
855 
856 	iter->he = he;
857 	return 0;
858 }
859 
860 static int
861 iter_finish_mem_entry(struct hist_entry_iter *iter,
862 		      struct addr_location *al __maybe_unused)
863 {
864 	struct evsel *evsel = iter->evsel;
865 	struct hists *hists = evsel__hists(evsel);
866 	struct hist_entry *he = iter->he;
867 	int err = -EINVAL;
868 
869 	if (he == NULL)
870 		goto out;
871 
872 	hists__inc_nr_samples(hists, he->filtered);
873 
874 	err = hist_entry__append_callchain(he, iter->sample);
875 
876 out:
877 	/*
878 	 * We don't need to free iter->priv (mem_info) here since the mem info
879 	 * was either already freed in hists__findnew_entry() or passed to a
880 	 * new hist entry by hist_entry__new().
881 	 */
882 	iter->priv = NULL;
883 
884 	iter->he = NULL;
885 	return err;
886 }
887 
888 static int
889 iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
890 {
891 	struct branch_info *bi;
892 	struct perf_sample *sample = iter->sample;
893 
894 	bi = sample__resolve_bstack(sample, al);
895 	if (!bi)
896 		return -ENOMEM;
897 
898 	iter->curr = 0;
899 	iter->total = sample->branch_stack->nr;
900 
901 	iter->priv = bi;
902 	return 0;
903 }
904 
905 static int
906 iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
907 			     struct addr_location *al __maybe_unused)
908 {
909 	return 0;
910 }
911 
912 static int
913 iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
914 {
915 	struct branch_info *bi = iter->priv;
916 	int i = iter->curr;
917 
918 	if (bi == NULL)
919 		return 0;
920 
921 	if (iter->curr >= iter->total)
922 		return 0;
923 
924 	al->maps = bi[i].to.ms.maps;
925 	al->map = bi[i].to.ms.map;
926 	al->sym = bi[i].to.ms.sym;
927 	al->addr = bi[i].to.addr;
928 	return 1;
929 }
930 
931 static int
932 iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
933 {
934 	struct branch_info *bi;
935 	struct evsel *evsel = iter->evsel;
936 	struct hists *hists = evsel__hists(evsel);
937 	struct perf_sample *sample = iter->sample;
938 	struct hist_entry *he = NULL;
939 	int i = iter->curr;
940 	int err = 0;
941 
942 	bi = iter->priv;
943 
944 	if (iter->hide_unresolved && !(bi[i].from.ms.sym && bi[i].to.ms.sym))
945 		goto out;
946 
947 	/*
948 	 * The report shows the percentage of total branches captured
949 	 * and not events sampled. Thus we use a pseudo period of 1.
950 	 */
951 	sample->period = 1;
952 	sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
953 
954 	he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
955 			      sample, true);
956 	if (he == NULL)
957 		return -ENOMEM;
958 
959 	hists__inc_nr_samples(hists, he->filtered);
960 
961 out:
962 	iter->he = he;
963 	iter->curr++;
964 	return err;
965 }
966 
967 static int
968 iter_finish_branch_entry(struct hist_entry_iter *iter,
969 			 struct addr_location *al __maybe_unused)
970 {
971 	zfree(&iter->priv);
972 	iter->he = NULL;
973 
974 	return iter->curr >= iter->total ? 0 : -1;
975 }
976 
977 static int
978 iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
979 			  struct addr_location *al __maybe_unused)
980 {
981 	return 0;
982 }
983 
984 static int
985 iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
986 {
987 	struct evsel *evsel = iter->evsel;
988 	struct perf_sample *sample = iter->sample;
989 	struct hist_entry *he;
990 
991 	he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
992 			      sample, true);
993 	if (he == NULL)
994 		return -ENOMEM;
995 
996 	iter->he = he;
997 	return 0;
998 }
999 
1000 static int
1001 iter_finish_normal_entry(struct hist_entry_iter *iter,
1002 			 struct addr_location *al __maybe_unused)
1003 {
1004 	struct hist_entry *he = iter->he;
1005 	struct evsel *evsel = iter->evsel;
1006 	struct perf_sample *sample = iter->sample;
1007 
1008 	if (he == NULL)
1009 		return 0;
1010 
1011 	iter->he = NULL;
1012 
1013 	hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
1014 
1015 	return hist_entry__append_callchain(he, sample);
1016 }
1017 
1018 static int
1019 iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
1020 			      struct addr_location *al __maybe_unused)
1021 {
1022 	struct hist_entry **he_cache;
1023 
1024 	callchain_cursor_commit(&callchain_cursor);
1025 
1026 	/*
1027 	 * This is for detecting cycles or recursions so that they're
1028 	 * cumulated only one time to prevent entries more than 100%
1029 	 * overhead.
1030 	 */
1031 	he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1));
1032 	if (he_cache == NULL)
1033 		return -ENOMEM;
1034 
1035 	iter->priv = he_cache;
1036 	iter->curr = 0;
1037 
1038 	return 0;
1039 }
1040 
1041 static int
1042 iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
1043 				 struct addr_location *al)
1044 {
1045 	struct evsel *evsel = iter->evsel;
1046 	struct hists *hists = evsel__hists(evsel);
1047 	struct perf_sample *sample = iter->sample;
1048 	struct hist_entry **he_cache = iter->priv;
1049 	struct hist_entry *he;
1050 	int err = 0;
1051 
1052 	he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
1053 			      sample, true);
1054 	if (he == NULL)
1055 		return -ENOMEM;
1056 
1057 	iter->he = he;
1058 	he_cache[iter->curr++] = he;
1059 
1060 	hist_entry__append_callchain(he, sample);
1061 
1062 	/*
1063 	 * We need to re-initialize the cursor since callchain_append()
1064 	 * advanced the cursor to the end.
1065 	 */
1066 	callchain_cursor_commit(&callchain_cursor);
1067 
1068 	hists__inc_nr_samples(hists, he->filtered);
1069 
1070 	return err;
1071 }
1072 
1073 static int
1074 iter_next_cumulative_entry(struct hist_entry_iter *iter,
1075 			   struct addr_location *al)
1076 {
1077 	struct callchain_cursor_node *node;
1078 
1079 	node = callchain_cursor_current(&callchain_cursor);
1080 	if (node == NULL)
1081 		return 0;
1082 
1083 	return fill_callchain_info(al, node, iter->hide_unresolved);
1084 }
1085 
1086 static bool
1087 hist_entry__fast__sym_diff(struct hist_entry *left,
1088 			   struct hist_entry *right)
1089 {
1090 	struct symbol *sym_l = left->ms.sym;
1091 	struct symbol *sym_r = right->ms.sym;
1092 
1093 	if (!sym_l && !sym_r)
1094 		return left->ip != right->ip;
1095 
1096 	return !!_sort__sym_cmp(sym_l, sym_r);
1097 }
1098 
1099 
1100 static int
1101 iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
1102 			       struct addr_location *al)
1103 {
1104 	struct evsel *evsel = iter->evsel;
1105 	struct perf_sample *sample = iter->sample;
1106 	struct hist_entry **he_cache = iter->priv;
1107 	struct hist_entry *he;
1108 	struct hist_entry he_tmp = {
1109 		.hists = evsel__hists(evsel),
1110 		.cpu = al->cpu,
1111 		.thread = al->thread,
1112 		.comm = thread__comm(al->thread),
1113 		.ip = al->addr,
1114 		.ms = {
1115 			.maps = al->maps,
1116 			.map = al->map,
1117 			.sym = al->sym,
1118 		},
1119 		.srcline = (char *) al->srcline,
1120 		.parent = iter->parent,
1121 		.raw_data = sample->raw_data,
1122 		.raw_size = sample->raw_size,
1123 	};
1124 	int i;
1125 	struct callchain_cursor cursor;
1126 	bool fast = hists__has(he_tmp.hists, sym);
1127 
1128 	callchain_cursor_snapshot(&cursor, &callchain_cursor);
1129 
1130 	callchain_cursor_advance(&callchain_cursor);
1131 
1132 	/*
1133 	 * Check if there's duplicate entries in the callchain.
1134 	 * It's possible that it has cycles or recursive calls.
1135 	 */
1136 	for (i = 0; i < iter->curr; i++) {
1137 		/*
1138 		 * For most cases, there are no duplicate entries in callchain.
1139 		 * The symbols are usually different. Do a quick check for
1140 		 * symbols first.
1141 		 */
1142 		if (fast && hist_entry__fast__sym_diff(he_cache[i], &he_tmp))
1143 			continue;
1144 
1145 		if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
1146 			/* to avoid calling callback function */
1147 			iter->he = NULL;
1148 			return 0;
1149 		}
1150 	}
1151 
1152 	he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
1153 			      sample, false);
1154 	if (he == NULL)
1155 		return -ENOMEM;
1156 
1157 	iter->he = he;
1158 	he_cache[iter->curr++] = he;
1159 
1160 	if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
1161 		callchain_append(he->callchain, &cursor, sample->period);
1162 	return 0;
1163 }
1164 
1165 static int
1166 iter_finish_cumulative_entry(struct hist_entry_iter *iter,
1167 			     struct addr_location *al __maybe_unused)
1168 {
1169 	zfree(&iter->priv);
1170 	iter->he = NULL;
1171 
1172 	return 0;
1173 }
1174 
1175 const struct hist_iter_ops hist_iter_mem = {
1176 	.prepare_entry 		= iter_prepare_mem_entry,
1177 	.add_single_entry 	= iter_add_single_mem_entry,
1178 	.next_entry 		= iter_next_nop_entry,
1179 	.add_next_entry 	= iter_add_next_nop_entry,
1180 	.finish_entry 		= iter_finish_mem_entry,
1181 };
1182 
1183 const struct hist_iter_ops hist_iter_branch = {
1184 	.prepare_entry 		= iter_prepare_branch_entry,
1185 	.add_single_entry 	= iter_add_single_branch_entry,
1186 	.next_entry 		= iter_next_branch_entry,
1187 	.add_next_entry 	= iter_add_next_branch_entry,
1188 	.finish_entry 		= iter_finish_branch_entry,
1189 };
1190 
1191 const struct hist_iter_ops hist_iter_normal = {
1192 	.prepare_entry 		= iter_prepare_normal_entry,
1193 	.add_single_entry 	= iter_add_single_normal_entry,
1194 	.next_entry 		= iter_next_nop_entry,
1195 	.add_next_entry 	= iter_add_next_nop_entry,
1196 	.finish_entry 		= iter_finish_normal_entry,
1197 };
1198 
1199 const struct hist_iter_ops hist_iter_cumulative = {
1200 	.prepare_entry 		= iter_prepare_cumulative_entry,
1201 	.add_single_entry 	= iter_add_single_cumulative_entry,
1202 	.next_entry 		= iter_next_cumulative_entry,
1203 	.add_next_entry 	= iter_add_next_cumulative_entry,
1204 	.finish_entry 		= iter_finish_cumulative_entry,
1205 };
1206 
1207 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
1208 			 int max_stack_depth, void *arg)
1209 {
1210 	int err, err2;
1211 	struct map *alm = NULL;
1212 
1213 	if (al)
1214 		alm = map__get(al->map);
1215 
1216 	err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
1217 					iter->evsel, al, max_stack_depth);
1218 	if (err) {
1219 		map__put(alm);
1220 		return err;
1221 	}
1222 
1223 	err = iter->ops->prepare_entry(iter, al);
1224 	if (err)
1225 		goto out;
1226 
1227 	err = iter->ops->add_single_entry(iter, al);
1228 	if (err)
1229 		goto out;
1230 
1231 	if (iter->he && iter->add_entry_cb) {
1232 		err = iter->add_entry_cb(iter, al, true, arg);
1233 		if (err)
1234 			goto out;
1235 	}
1236 
1237 	while (iter->ops->next_entry(iter, al)) {
1238 		err = iter->ops->add_next_entry(iter, al);
1239 		if (err)
1240 			break;
1241 
1242 		if (iter->he && iter->add_entry_cb) {
1243 			err = iter->add_entry_cb(iter, al, false, arg);
1244 			if (err)
1245 				goto out;
1246 		}
1247 	}
1248 
1249 out:
1250 	err2 = iter->ops->finish_entry(iter, al);
1251 	if (!err)
1252 		err = err2;
1253 
1254 	map__put(alm);
1255 
1256 	return err;
1257 }
1258 
1259 int64_t
1260 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1261 {
1262 	struct hists *hists = left->hists;
1263 	struct perf_hpp_fmt *fmt;
1264 	int64_t cmp = 0;
1265 
1266 	hists__for_each_sort_list(hists, fmt) {
1267 		if (perf_hpp__is_dynamic_entry(fmt) &&
1268 		    !perf_hpp__defined_dynamic_entry(fmt, hists))
1269 			continue;
1270 
1271 		cmp = fmt->cmp(fmt, left, right);
1272 		if (cmp)
1273 			break;
1274 	}
1275 
1276 	return cmp;
1277 }
1278 
1279 int64_t
1280 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1281 {
1282 	struct hists *hists = left->hists;
1283 	struct perf_hpp_fmt *fmt;
1284 	int64_t cmp = 0;
1285 
1286 	hists__for_each_sort_list(hists, fmt) {
1287 		if (perf_hpp__is_dynamic_entry(fmt) &&
1288 		    !perf_hpp__defined_dynamic_entry(fmt, hists))
1289 			continue;
1290 
1291 		cmp = fmt->collapse(fmt, left, right);
1292 		if (cmp)
1293 			break;
1294 	}
1295 
1296 	return cmp;
1297 }
1298 
1299 void hist_entry__delete(struct hist_entry *he)
1300 {
1301 	struct hist_entry_ops *ops = he->ops;
1302 
1303 	thread__zput(he->thread);
1304 	map__zput(he->ms.map);
1305 
1306 	if (he->branch_info) {
1307 		map__zput(he->branch_info->from.ms.map);
1308 		map__zput(he->branch_info->to.ms.map);
1309 		free_srcline(he->branch_info->srcline_from);
1310 		free_srcline(he->branch_info->srcline_to);
1311 		zfree(&he->branch_info);
1312 	}
1313 
1314 	if (he->mem_info) {
1315 		map__zput(he->mem_info->iaddr.ms.map);
1316 		map__zput(he->mem_info->daddr.ms.map);
1317 		mem_info__zput(he->mem_info);
1318 	}
1319 
1320 	if (he->block_info)
1321 		block_info__zput(he->block_info);
1322 
1323 	zfree(&he->res_samples);
1324 	zfree(&he->stat_acc);
1325 	free_srcline(he->srcline);
1326 	if (he->srcfile && he->srcfile[0])
1327 		zfree(&he->srcfile);
1328 	free_callchain(he->callchain);
1329 	zfree(&he->trace_output);
1330 	zfree(&he->raw_data);
1331 	ops->free(he);
1332 }
1333 
1334 /*
1335  * If this is not the last column, then we need to pad it according to the
1336  * pre-calculated max length for this column, otherwise don't bother adding
1337  * spaces because that would break viewing this with, for instance, 'less',
1338  * that would show tons of trailing spaces when a long C++ demangled method
1339  * names is sampled.
1340 */
1341 int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1342 				   struct perf_hpp_fmt *fmt, int printed)
1343 {
1344 	if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
1345 		const int width = fmt->width(fmt, hpp, he->hists);
1346 		if (printed < width) {
1347 			advance_hpp(hpp, printed);
1348 			printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1349 		}
1350 	}
1351 
1352 	return printed;
1353 }
1354 
1355 /*
1356  * collapse the histogram
1357  */
1358 
1359 static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
1360 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1361 				       enum hist_filter type);
1362 
1363 typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1364 
1365 static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1366 {
1367 	return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1368 }
1369 
1370 static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1371 						enum hist_filter type,
1372 						fmt_chk_fn check)
1373 {
1374 	struct perf_hpp_fmt *fmt;
1375 	bool type_match = false;
1376 	struct hist_entry *parent = he->parent_he;
1377 
1378 	switch (type) {
1379 	case HIST_FILTER__THREAD:
1380 		if (symbol_conf.comm_list == NULL &&
1381 		    symbol_conf.pid_list == NULL &&
1382 		    symbol_conf.tid_list == NULL)
1383 			return;
1384 		break;
1385 	case HIST_FILTER__DSO:
1386 		if (symbol_conf.dso_list == NULL)
1387 			return;
1388 		break;
1389 	case HIST_FILTER__SYMBOL:
1390 		if (symbol_conf.sym_list == NULL)
1391 			return;
1392 		break;
1393 	case HIST_FILTER__PARENT:
1394 	case HIST_FILTER__GUEST:
1395 	case HIST_FILTER__HOST:
1396 	case HIST_FILTER__SOCKET:
1397 	case HIST_FILTER__C2C:
1398 	default:
1399 		return;
1400 	}
1401 
1402 	/* if it's filtered by own fmt, it has to have filter bits */
1403 	perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1404 		if (check(fmt)) {
1405 			type_match = true;
1406 			break;
1407 		}
1408 	}
1409 
1410 	if (type_match) {
1411 		/*
1412 		 * If the filter is for current level entry, propagate
1413 		 * filter marker to parents.  The marker bit was
1414 		 * already set by default so it only needs to clear
1415 		 * non-filtered entries.
1416 		 */
1417 		if (!(he->filtered & (1 << type))) {
1418 			while (parent) {
1419 				parent->filtered &= ~(1 << type);
1420 				parent = parent->parent_he;
1421 			}
1422 		}
1423 	} else {
1424 		/*
1425 		 * If current entry doesn't have matching formats, set
1426 		 * filter marker for upper level entries.  it will be
1427 		 * cleared if its lower level entries is not filtered.
1428 		 *
1429 		 * For lower-level entries, it inherits parent's
1430 		 * filter bit so that lower level entries of a
1431 		 * non-filtered entry won't set the filter marker.
1432 		 */
1433 		if (parent == NULL)
1434 			he->filtered |= (1 << type);
1435 		else
1436 			he->filtered |= (parent->filtered & (1 << type));
1437 	}
1438 }
1439 
1440 static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1441 {
1442 	hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1443 					    check_thread_entry);
1444 
1445 	hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1446 					    perf_hpp__is_dso_entry);
1447 
1448 	hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1449 					    perf_hpp__is_sym_entry);
1450 
1451 	hists__apply_filters(he->hists, he);
1452 }
1453 
1454 static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1455 						 struct rb_root_cached *root,
1456 						 struct hist_entry *he,
1457 						 struct hist_entry *parent_he,
1458 						 struct perf_hpp_list *hpp_list)
1459 {
1460 	struct rb_node **p = &root->rb_root.rb_node;
1461 	struct rb_node *parent = NULL;
1462 	struct hist_entry *iter, *new;
1463 	struct perf_hpp_fmt *fmt;
1464 	int64_t cmp;
1465 	bool leftmost = true;
1466 
1467 	while (*p != NULL) {
1468 		parent = *p;
1469 		iter = rb_entry(parent, struct hist_entry, rb_node_in);
1470 
1471 		cmp = 0;
1472 		perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1473 			cmp = fmt->collapse(fmt, iter, he);
1474 			if (cmp)
1475 				break;
1476 		}
1477 
1478 		if (!cmp) {
1479 			he_stat__add_stat(&iter->stat, &he->stat);
1480 			return iter;
1481 		}
1482 
1483 		if (cmp < 0)
1484 			p = &parent->rb_left;
1485 		else {
1486 			p = &parent->rb_right;
1487 			leftmost = false;
1488 		}
1489 	}
1490 
1491 	new = hist_entry__new(he, true);
1492 	if (new == NULL)
1493 		return NULL;
1494 
1495 	hists->nr_entries++;
1496 
1497 	/* save related format list for output */
1498 	new->hpp_list = hpp_list;
1499 	new->parent_he = parent_he;
1500 
1501 	hist_entry__apply_hierarchy_filters(new);
1502 
1503 	/* some fields are now passed to 'new' */
1504 	perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1505 		if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1506 			he->trace_output = NULL;
1507 		else
1508 			new->trace_output = NULL;
1509 
1510 		if (perf_hpp__is_srcline_entry(fmt))
1511 			he->srcline = NULL;
1512 		else
1513 			new->srcline = NULL;
1514 
1515 		if (perf_hpp__is_srcfile_entry(fmt))
1516 			he->srcfile = NULL;
1517 		else
1518 			new->srcfile = NULL;
1519 	}
1520 
1521 	rb_link_node(&new->rb_node_in, parent, p);
1522 	rb_insert_color_cached(&new->rb_node_in, root, leftmost);
1523 	return new;
1524 }
1525 
1526 static int hists__hierarchy_insert_entry(struct hists *hists,
1527 					 struct rb_root_cached *root,
1528 					 struct hist_entry *he)
1529 {
1530 	struct perf_hpp_list_node *node;
1531 	struct hist_entry *new_he = NULL;
1532 	struct hist_entry *parent = NULL;
1533 	int depth = 0;
1534 	int ret = 0;
1535 
1536 	list_for_each_entry(node, &hists->hpp_formats, list) {
1537 		/* skip period (overhead) and elided columns */
1538 		if (node->level == 0 || node->skip)
1539 			continue;
1540 
1541 		/* insert copy of 'he' for each fmt into the hierarchy */
1542 		new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
1543 		if (new_he == NULL) {
1544 			ret = -1;
1545 			break;
1546 		}
1547 
1548 		root = &new_he->hroot_in;
1549 		new_he->depth = depth++;
1550 		parent = new_he;
1551 	}
1552 
1553 	if (new_he) {
1554 		new_he->leaf = true;
1555 
1556 		if (hist_entry__has_callchains(new_he) &&
1557 		    symbol_conf.use_callchain) {
1558 			callchain_cursor_reset(&callchain_cursor);
1559 			if (callchain_merge(&callchain_cursor,
1560 					    new_he->callchain,
1561 					    he->callchain) < 0)
1562 				ret = -1;
1563 		}
1564 	}
1565 
1566 	/* 'he' is no longer used */
1567 	hist_entry__delete(he);
1568 
1569 	/* return 0 (or -1) since it already applied filters */
1570 	return ret;
1571 }
1572 
1573 static int hists__collapse_insert_entry(struct hists *hists,
1574 					struct rb_root_cached *root,
1575 					struct hist_entry *he)
1576 {
1577 	struct rb_node **p = &root->rb_root.rb_node;
1578 	struct rb_node *parent = NULL;
1579 	struct hist_entry *iter;
1580 	int64_t cmp;
1581 	bool leftmost = true;
1582 
1583 	if (symbol_conf.report_hierarchy)
1584 		return hists__hierarchy_insert_entry(hists, root, he);
1585 
1586 	while (*p != NULL) {
1587 		parent = *p;
1588 		iter = rb_entry(parent, struct hist_entry, rb_node_in);
1589 
1590 		cmp = hist_entry__collapse(iter, he);
1591 
1592 		if (!cmp) {
1593 			int ret = 0;
1594 
1595 			he_stat__add_stat(&iter->stat, &he->stat);
1596 			if (symbol_conf.cumulate_callchain)
1597 				he_stat__add_stat(iter->stat_acc, he->stat_acc);
1598 
1599 			if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
1600 				callchain_cursor_reset(&callchain_cursor);
1601 				if (callchain_merge(&callchain_cursor,
1602 						    iter->callchain,
1603 						    he->callchain) < 0)
1604 					ret = -1;
1605 			}
1606 			hist_entry__delete(he);
1607 			return ret;
1608 		}
1609 
1610 		if (cmp < 0)
1611 			p = &(*p)->rb_left;
1612 		else {
1613 			p = &(*p)->rb_right;
1614 			leftmost = false;
1615 		}
1616 	}
1617 	hists->nr_entries++;
1618 
1619 	rb_link_node(&he->rb_node_in, parent, p);
1620 	rb_insert_color_cached(&he->rb_node_in, root, leftmost);
1621 	return 1;
1622 }
1623 
1624 struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists)
1625 {
1626 	struct rb_root_cached *root;
1627 
1628 	pthread_mutex_lock(&hists->lock);
1629 
1630 	root = hists->entries_in;
1631 	if (++hists->entries_in > &hists->entries_in_array[1])
1632 		hists->entries_in = &hists->entries_in_array[0];
1633 
1634 	pthread_mutex_unlock(&hists->lock);
1635 
1636 	return root;
1637 }
1638 
1639 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1640 {
1641 	hists__filter_entry_by_dso(hists, he);
1642 	hists__filter_entry_by_thread(hists, he);
1643 	hists__filter_entry_by_symbol(hists, he);
1644 	hists__filter_entry_by_socket(hists, he);
1645 }
1646 
1647 int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
1648 {
1649 	struct rb_root_cached *root;
1650 	struct rb_node *next;
1651 	struct hist_entry *n;
1652 	int ret;
1653 
1654 	if (!hists__has(hists, need_collapse))
1655 		return 0;
1656 
1657 	hists->nr_entries = 0;
1658 
1659 	root = hists__get_rotate_entries_in(hists);
1660 
1661 	next = rb_first_cached(root);
1662 
1663 	while (next) {
1664 		if (session_done())
1665 			break;
1666 		n = rb_entry(next, struct hist_entry, rb_node_in);
1667 		next = rb_next(&n->rb_node_in);
1668 
1669 		rb_erase_cached(&n->rb_node_in, root);
1670 		ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1671 		if (ret < 0)
1672 			return -1;
1673 
1674 		if (ret) {
1675 			/*
1676 			 * If it wasn't combined with one of the entries already
1677 			 * collapsed, we need to apply the filters that may have
1678 			 * been set by, say, the hist_browser.
1679 			 */
1680 			hists__apply_filters(hists, n);
1681 		}
1682 		if (prog)
1683 			ui_progress__update(prog, 1);
1684 	}
1685 	return 0;
1686 }
1687 
1688 static int64_t hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
1689 {
1690 	struct hists *hists = a->hists;
1691 	struct perf_hpp_fmt *fmt;
1692 	int64_t cmp = 0;
1693 
1694 	hists__for_each_sort_list(hists, fmt) {
1695 		if (perf_hpp__should_skip(fmt, a->hists))
1696 			continue;
1697 
1698 		cmp = fmt->sort(fmt, a, b);
1699 		if (cmp)
1700 			break;
1701 	}
1702 
1703 	return cmp;
1704 }
1705 
1706 static void hists__reset_filter_stats(struct hists *hists)
1707 {
1708 	hists->nr_non_filtered_entries = 0;
1709 	hists->stats.total_non_filtered_period = 0;
1710 }
1711 
1712 void hists__reset_stats(struct hists *hists)
1713 {
1714 	hists->nr_entries = 0;
1715 	hists->stats.total_period = 0;
1716 
1717 	hists__reset_filter_stats(hists);
1718 }
1719 
1720 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1721 {
1722 	hists->nr_non_filtered_entries++;
1723 	hists->stats.total_non_filtered_period += h->stat.period;
1724 }
1725 
1726 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1727 {
1728 	if (!h->filtered)
1729 		hists__inc_filter_stats(hists, h);
1730 
1731 	hists->nr_entries++;
1732 	hists->stats.total_period += h->stat.period;
1733 }
1734 
1735 static void hierarchy_recalc_total_periods(struct hists *hists)
1736 {
1737 	struct rb_node *node;
1738 	struct hist_entry *he;
1739 
1740 	node = rb_first_cached(&hists->entries);
1741 
1742 	hists->stats.total_period = 0;
1743 	hists->stats.total_non_filtered_period = 0;
1744 
1745 	/*
1746 	 * recalculate total period using top-level entries only
1747 	 * since lower level entries only see non-filtered entries
1748 	 * but upper level entries have sum of both entries.
1749 	 */
1750 	while (node) {
1751 		he = rb_entry(node, struct hist_entry, rb_node);
1752 		node = rb_next(node);
1753 
1754 		hists->stats.total_period += he->stat.period;
1755 		if (!he->filtered)
1756 			hists->stats.total_non_filtered_period += he->stat.period;
1757 	}
1758 }
1759 
1760 static void hierarchy_insert_output_entry(struct rb_root_cached *root,
1761 					  struct hist_entry *he)
1762 {
1763 	struct rb_node **p = &root->rb_root.rb_node;
1764 	struct rb_node *parent = NULL;
1765 	struct hist_entry *iter;
1766 	struct perf_hpp_fmt *fmt;
1767 	bool leftmost = true;
1768 
1769 	while (*p != NULL) {
1770 		parent = *p;
1771 		iter = rb_entry(parent, struct hist_entry, rb_node);
1772 
1773 		if (hist_entry__sort(he, iter) > 0)
1774 			p = &parent->rb_left;
1775 		else {
1776 			p = &parent->rb_right;
1777 			leftmost = false;
1778 		}
1779 	}
1780 
1781 	rb_link_node(&he->rb_node, parent, p);
1782 	rb_insert_color_cached(&he->rb_node, root, leftmost);
1783 
1784 	/* update column width of dynamic entry */
1785 	perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1786 		if (perf_hpp__is_dynamic_entry(fmt))
1787 			fmt->sort(fmt, he, NULL);
1788 	}
1789 }
1790 
1791 static void hists__hierarchy_output_resort(struct hists *hists,
1792 					   struct ui_progress *prog,
1793 					   struct rb_root_cached *root_in,
1794 					   struct rb_root_cached *root_out,
1795 					   u64 min_callchain_hits,
1796 					   bool use_callchain)
1797 {
1798 	struct rb_node *node;
1799 	struct hist_entry *he;
1800 
1801 	*root_out = RB_ROOT_CACHED;
1802 	node = rb_first_cached(root_in);
1803 
1804 	while (node) {
1805 		he = rb_entry(node, struct hist_entry, rb_node_in);
1806 		node = rb_next(node);
1807 
1808 		hierarchy_insert_output_entry(root_out, he);
1809 
1810 		if (prog)
1811 			ui_progress__update(prog, 1);
1812 
1813 		hists->nr_entries++;
1814 		if (!he->filtered) {
1815 			hists->nr_non_filtered_entries++;
1816 			hists__calc_col_len(hists, he);
1817 		}
1818 
1819 		if (!he->leaf) {
1820 			hists__hierarchy_output_resort(hists, prog,
1821 						       &he->hroot_in,
1822 						       &he->hroot_out,
1823 						       min_callchain_hits,
1824 						       use_callchain);
1825 			continue;
1826 		}
1827 
1828 		if (!use_callchain)
1829 			continue;
1830 
1831 		if (callchain_param.mode == CHAIN_GRAPH_REL) {
1832 			u64 total = he->stat.period;
1833 
1834 			if (symbol_conf.cumulate_callchain)
1835 				total = he->stat_acc->period;
1836 
1837 			min_callchain_hits = total * (callchain_param.min_percent / 100);
1838 		}
1839 
1840 		callchain_param.sort(&he->sorted_chain, he->callchain,
1841 				     min_callchain_hits, &callchain_param);
1842 	}
1843 }
1844 
1845 static void __hists__insert_output_entry(struct rb_root_cached *entries,
1846 					 struct hist_entry *he,
1847 					 u64 min_callchain_hits,
1848 					 bool use_callchain)
1849 {
1850 	struct rb_node **p = &entries->rb_root.rb_node;
1851 	struct rb_node *parent = NULL;
1852 	struct hist_entry *iter;
1853 	struct perf_hpp_fmt *fmt;
1854 	bool leftmost = true;
1855 
1856 	if (use_callchain) {
1857 		if (callchain_param.mode == CHAIN_GRAPH_REL) {
1858 			u64 total = he->stat.period;
1859 
1860 			if (symbol_conf.cumulate_callchain)
1861 				total = he->stat_acc->period;
1862 
1863 			min_callchain_hits = total * (callchain_param.min_percent / 100);
1864 		}
1865 		callchain_param.sort(&he->sorted_chain, he->callchain,
1866 				      min_callchain_hits, &callchain_param);
1867 	}
1868 
1869 	while (*p != NULL) {
1870 		parent = *p;
1871 		iter = rb_entry(parent, struct hist_entry, rb_node);
1872 
1873 		if (hist_entry__sort(he, iter) > 0)
1874 			p = &(*p)->rb_left;
1875 		else {
1876 			p = &(*p)->rb_right;
1877 			leftmost = false;
1878 		}
1879 	}
1880 
1881 	rb_link_node(&he->rb_node, parent, p);
1882 	rb_insert_color_cached(&he->rb_node, entries, leftmost);
1883 
1884 	perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1885 		if (perf_hpp__is_dynamic_entry(fmt) &&
1886 		    perf_hpp__defined_dynamic_entry(fmt, he->hists))
1887 			fmt->sort(fmt, he, NULL);  /* update column width */
1888 	}
1889 }
1890 
1891 static void output_resort(struct hists *hists, struct ui_progress *prog,
1892 			  bool use_callchain, hists__resort_cb_t cb,
1893 			  void *cb_arg)
1894 {
1895 	struct rb_root_cached *root;
1896 	struct rb_node *next;
1897 	struct hist_entry *n;
1898 	u64 callchain_total;
1899 	u64 min_callchain_hits;
1900 
1901 	callchain_total = hists->callchain_period;
1902 	if (symbol_conf.filter_relative)
1903 		callchain_total = hists->callchain_non_filtered_period;
1904 
1905 	min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
1906 
1907 	hists__reset_stats(hists);
1908 	hists__reset_col_len(hists);
1909 
1910 	if (symbol_conf.report_hierarchy) {
1911 		hists__hierarchy_output_resort(hists, prog,
1912 					       &hists->entries_collapsed,
1913 					       &hists->entries,
1914 					       min_callchain_hits,
1915 					       use_callchain);
1916 		hierarchy_recalc_total_periods(hists);
1917 		return;
1918 	}
1919 
1920 	if (hists__has(hists, need_collapse))
1921 		root = &hists->entries_collapsed;
1922 	else
1923 		root = hists->entries_in;
1924 
1925 	next = rb_first_cached(root);
1926 	hists->entries = RB_ROOT_CACHED;
1927 
1928 	while (next) {
1929 		n = rb_entry(next, struct hist_entry, rb_node_in);
1930 		next = rb_next(&n->rb_node_in);
1931 
1932 		if (cb && cb(n, cb_arg))
1933 			continue;
1934 
1935 		__hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
1936 		hists__inc_stats(hists, n);
1937 
1938 		if (!n->filtered)
1939 			hists__calc_col_len(hists, n);
1940 
1941 		if (prog)
1942 			ui_progress__update(prog, 1);
1943 	}
1944 }
1945 
1946 void evsel__output_resort_cb(struct evsel *evsel, struct ui_progress *prog,
1947 			     hists__resort_cb_t cb, void *cb_arg)
1948 {
1949 	bool use_callchain;
1950 
1951 	if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1952 		use_callchain = evsel__has_callchain(evsel);
1953 	else
1954 		use_callchain = symbol_conf.use_callchain;
1955 
1956 	use_callchain |= symbol_conf.show_branchflag_count;
1957 
1958 	output_resort(evsel__hists(evsel), prog, use_callchain, cb, cb_arg);
1959 }
1960 
1961 void evsel__output_resort(struct evsel *evsel, struct ui_progress *prog)
1962 {
1963 	return evsel__output_resort_cb(evsel, prog, NULL, NULL);
1964 }
1965 
1966 void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1967 {
1968 	output_resort(hists, prog, symbol_conf.use_callchain, NULL, NULL);
1969 }
1970 
1971 void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1972 			     hists__resort_cb_t cb)
1973 {
1974 	output_resort(hists, prog, symbol_conf.use_callchain, cb, NULL);
1975 }
1976 
1977 static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1978 {
1979 	if (he->leaf || hmd == HMD_FORCE_SIBLING)
1980 		return false;
1981 
1982 	if (he->unfolded || hmd == HMD_FORCE_CHILD)
1983 		return true;
1984 
1985 	return false;
1986 }
1987 
1988 struct rb_node *rb_hierarchy_last(struct rb_node *node)
1989 {
1990 	struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1991 
1992 	while (can_goto_child(he, HMD_NORMAL)) {
1993 		node = rb_last(&he->hroot_out.rb_root);
1994 		he = rb_entry(node, struct hist_entry, rb_node);
1995 	}
1996 	return node;
1997 }
1998 
1999 struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
2000 {
2001 	struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
2002 
2003 	if (can_goto_child(he, hmd))
2004 		node = rb_first_cached(&he->hroot_out);
2005 	else
2006 		node = rb_next(node);
2007 
2008 	while (node == NULL) {
2009 		he = he->parent_he;
2010 		if (he == NULL)
2011 			break;
2012 
2013 		node = rb_next(&he->rb_node);
2014 	}
2015 	return node;
2016 }
2017 
2018 struct rb_node *rb_hierarchy_prev(struct rb_node *node)
2019 {
2020 	struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
2021 
2022 	node = rb_prev(node);
2023 	if (node)
2024 		return rb_hierarchy_last(node);
2025 
2026 	he = he->parent_he;
2027 	if (he == NULL)
2028 		return NULL;
2029 
2030 	return &he->rb_node;
2031 }
2032 
2033 bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
2034 {
2035 	struct rb_node *node;
2036 	struct hist_entry *child;
2037 	float percent;
2038 
2039 	if (he->leaf)
2040 		return false;
2041 
2042 	node = rb_first_cached(&he->hroot_out);
2043 	child = rb_entry(node, struct hist_entry, rb_node);
2044 
2045 	while (node && child->filtered) {
2046 		node = rb_next(node);
2047 		child = rb_entry(node, struct hist_entry, rb_node);
2048 	}
2049 
2050 	if (node)
2051 		percent = hist_entry__get_percent_limit(child);
2052 	else
2053 		percent = 0;
2054 
2055 	return node && percent >= limit;
2056 }
2057 
2058 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
2059 				       enum hist_filter filter)
2060 {
2061 	h->filtered &= ~(1 << filter);
2062 
2063 	if (symbol_conf.report_hierarchy) {
2064 		struct hist_entry *parent = h->parent_he;
2065 
2066 		while (parent) {
2067 			he_stat__add_stat(&parent->stat, &h->stat);
2068 
2069 			parent->filtered &= ~(1 << filter);
2070 
2071 			if (parent->filtered)
2072 				goto next;
2073 
2074 			/* force fold unfiltered entry for simplicity */
2075 			parent->unfolded = false;
2076 			parent->has_no_entry = false;
2077 			parent->row_offset = 0;
2078 			parent->nr_rows = 0;
2079 next:
2080 			parent = parent->parent_he;
2081 		}
2082 	}
2083 
2084 	if (h->filtered)
2085 		return;
2086 
2087 	/* force fold unfiltered entry for simplicity */
2088 	h->unfolded = false;
2089 	h->has_no_entry = false;
2090 	h->row_offset = 0;
2091 	h->nr_rows = 0;
2092 
2093 	hists->stats.nr_non_filtered_samples += h->stat.nr_events;
2094 
2095 	hists__inc_filter_stats(hists, h);
2096 	hists__calc_col_len(hists, h);
2097 }
2098 
2099 
2100 static bool hists__filter_entry_by_dso(struct hists *hists,
2101 				       struct hist_entry *he)
2102 {
2103 	if (hists->dso_filter != NULL &&
2104 	    (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
2105 		he->filtered |= (1 << HIST_FILTER__DSO);
2106 		return true;
2107 	}
2108 
2109 	return false;
2110 }
2111 
2112 static bool hists__filter_entry_by_thread(struct hists *hists,
2113 					  struct hist_entry *he)
2114 {
2115 	if (hists->thread_filter != NULL &&
2116 	    he->thread != hists->thread_filter) {
2117 		he->filtered |= (1 << HIST_FILTER__THREAD);
2118 		return true;
2119 	}
2120 
2121 	return false;
2122 }
2123 
2124 static bool hists__filter_entry_by_symbol(struct hists *hists,
2125 					  struct hist_entry *he)
2126 {
2127 	if (hists->symbol_filter_str != NULL &&
2128 	    (!he->ms.sym || strstr(he->ms.sym->name,
2129 				   hists->symbol_filter_str) == NULL)) {
2130 		he->filtered |= (1 << HIST_FILTER__SYMBOL);
2131 		return true;
2132 	}
2133 
2134 	return false;
2135 }
2136 
2137 static bool hists__filter_entry_by_socket(struct hists *hists,
2138 					  struct hist_entry *he)
2139 {
2140 	if ((hists->socket_filter > -1) &&
2141 	    (he->socket != hists->socket_filter)) {
2142 		he->filtered |= (1 << HIST_FILTER__SOCKET);
2143 		return true;
2144 	}
2145 
2146 	return false;
2147 }
2148 
2149 typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
2150 
2151 static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
2152 {
2153 	struct rb_node *nd;
2154 
2155 	hists->stats.nr_non_filtered_samples = 0;
2156 
2157 	hists__reset_filter_stats(hists);
2158 	hists__reset_col_len(hists);
2159 
2160 	for (nd = rb_first_cached(&hists->entries); nd; nd = rb_next(nd)) {
2161 		struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2162 
2163 		if (filter(hists, h))
2164 			continue;
2165 
2166 		hists__remove_entry_filter(hists, h, type);
2167 	}
2168 }
2169 
2170 static void resort_filtered_entry(struct rb_root_cached *root,
2171 				  struct hist_entry *he)
2172 {
2173 	struct rb_node **p = &root->rb_root.rb_node;
2174 	struct rb_node *parent = NULL;
2175 	struct hist_entry *iter;
2176 	struct rb_root_cached new_root = RB_ROOT_CACHED;
2177 	struct rb_node *nd;
2178 	bool leftmost = true;
2179 
2180 	while (*p != NULL) {
2181 		parent = *p;
2182 		iter = rb_entry(parent, struct hist_entry, rb_node);
2183 
2184 		if (hist_entry__sort(he, iter) > 0)
2185 			p = &(*p)->rb_left;
2186 		else {
2187 			p = &(*p)->rb_right;
2188 			leftmost = false;
2189 		}
2190 	}
2191 
2192 	rb_link_node(&he->rb_node, parent, p);
2193 	rb_insert_color_cached(&he->rb_node, root, leftmost);
2194 
2195 	if (he->leaf || he->filtered)
2196 		return;
2197 
2198 	nd = rb_first_cached(&he->hroot_out);
2199 	while (nd) {
2200 		struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2201 
2202 		nd = rb_next(nd);
2203 		rb_erase_cached(&h->rb_node, &he->hroot_out);
2204 
2205 		resort_filtered_entry(&new_root, h);
2206 	}
2207 
2208 	he->hroot_out = new_root;
2209 }
2210 
2211 static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2212 {
2213 	struct rb_node *nd;
2214 	struct rb_root_cached new_root = RB_ROOT_CACHED;
2215 
2216 	hists->stats.nr_non_filtered_samples = 0;
2217 
2218 	hists__reset_filter_stats(hists);
2219 	hists__reset_col_len(hists);
2220 
2221 	nd = rb_first_cached(&hists->entries);
2222 	while (nd) {
2223 		struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2224 		int ret;
2225 
2226 		ret = hist_entry__filter(h, type, arg);
2227 
2228 		/*
2229 		 * case 1. non-matching type
2230 		 * zero out the period, set filter marker and move to child
2231 		 */
2232 		if (ret < 0) {
2233 			memset(&h->stat, 0, sizeof(h->stat));
2234 			h->filtered |= (1 << type);
2235 
2236 			nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2237 		}
2238 		/*
2239 		 * case 2. matched type (filter out)
2240 		 * set filter marker and move to next
2241 		 */
2242 		else if (ret == 1) {
2243 			h->filtered |= (1 << type);
2244 
2245 			nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2246 		}
2247 		/*
2248 		 * case 3. ok (not filtered)
2249 		 * add period to hists and parents, erase the filter marker
2250 		 * and move to next sibling
2251 		 */
2252 		else {
2253 			hists__remove_entry_filter(hists, h, type);
2254 
2255 			nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2256 		}
2257 	}
2258 
2259 	hierarchy_recalc_total_periods(hists);
2260 
2261 	/*
2262 	 * resort output after applying a new filter since filter in a lower
2263 	 * hierarchy can change periods in a upper hierarchy.
2264 	 */
2265 	nd = rb_first_cached(&hists->entries);
2266 	while (nd) {
2267 		struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2268 
2269 		nd = rb_next(nd);
2270 		rb_erase_cached(&h->rb_node, &hists->entries);
2271 
2272 		resort_filtered_entry(&new_root, h);
2273 	}
2274 
2275 	hists->entries = new_root;
2276 }
2277 
2278 void hists__filter_by_thread(struct hists *hists)
2279 {
2280 	if (symbol_conf.report_hierarchy)
2281 		hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2282 					hists->thread_filter);
2283 	else
2284 		hists__filter_by_type(hists, HIST_FILTER__THREAD,
2285 				      hists__filter_entry_by_thread);
2286 }
2287 
2288 void hists__filter_by_dso(struct hists *hists)
2289 {
2290 	if (symbol_conf.report_hierarchy)
2291 		hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2292 					hists->dso_filter);
2293 	else
2294 		hists__filter_by_type(hists, HIST_FILTER__DSO,
2295 				      hists__filter_entry_by_dso);
2296 }
2297 
2298 void hists__filter_by_symbol(struct hists *hists)
2299 {
2300 	if (symbol_conf.report_hierarchy)
2301 		hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2302 					hists->symbol_filter_str);
2303 	else
2304 		hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2305 				      hists__filter_entry_by_symbol);
2306 }
2307 
2308 void hists__filter_by_socket(struct hists *hists)
2309 {
2310 	if (symbol_conf.report_hierarchy)
2311 		hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2312 					&hists->socket_filter);
2313 	else
2314 		hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2315 				      hists__filter_entry_by_socket);
2316 }
2317 
2318 void events_stats__inc(struct events_stats *stats, u32 type)
2319 {
2320 	++stats->nr_events[0];
2321 	++stats->nr_events[type];
2322 }
2323 
2324 static void hists_stats__inc(struct hists_stats *stats)
2325 {
2326 	++stats->nr_samples;
2327 }
2328 
2329 void hists__inc_nr_events(struct hists *hists)
2330 {
2331 	hists_stats__inc(&hists->stats);
2332 }
2333 
2334 void hists__inc_nr_samples(struct hists *hists, bool filtered)
2335 {
2336 	hists_stats__inc(&hists->stats);
2337 	if (!filtered)
2338 		hists->stats.nr_non_filtered_samples++;
2339 }
2340 
2341 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2342 						 struct hist_entry *pair)
2343 {
2344 	struct rb_root_cached *root;
2345 	struct rb_node **p;
2346 	struct rb_node *parent = NULL;
2347 	struct hist_entry *he;
2348 	int64_t cmp;
2349 	bool leftmost = true;
2350 
2351 	if (hists__has(hists, need_collapse))
2352 		root = &hists->entries_collapsed;
2353 	else
2354 		root = hists->entries_in;
2355 
2356 	p = &root->rb_root.rb_node;
2357 
2358 	while (*p != NULL) {
2359 		parent = *p;
2360 		he = rb_entry(parent, struct hist_entry, rb_node_in);
2361 
2362 		cmp = hist_entry__collapse(he, pair);
2363 
2364 		if (!cmp)
2365 			goto out;
2366 
2367 		if (cmp < 0)
2368 			p = &(*p)->rb_left;
2369 		else {
2370 			p = &(*p)->rb_right;
2371 			leftmost = false;
2372 		}
2373 	}
2374 
2375 	he = hist_entry__new(pair, true);
2376 	if (he) {
2377 		memset(&he->stat, 0, sizeof(he->stat));
2378 		he->hists = hists;
2379 		if (symbol_conf.cumulate_callchain)
2380 			memset(he->stat_acc, 0, sizeof(he->stat));
2381 		rb_link_node(&he->rb_node_in, parent, p);
2382 		rb_insert_color_cached(&he->rb_node_in, root, leftmost);
2383 		hists__inc_stats(hists, he);
2384 		he->dummy = true;
2385 	}
2386 out:
2387 	return he;
2388 }
2389 
2390 static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
2391 						    struct rb_root_cached *root,
2392 						    struct hist_entry *pair)
2393 {
2394 	struct rb_node **p;
2395 	struct rb_node *parent = NULL;
2396 	struct hist_entry *he;
2397 	struct perf_hpp_fmt *fmt;
2398 	bool leftmost = true;
2399 
2400 	p = &root->rb_root.rb_node;
2401 	while (*p != NULL) {
2402 		int64_t cmp = 0;
2403 
2404 		parent = *p;
2405 		he = rb_entry(parent, struct hist_entry, rb_node_in);
2406 
2407 		perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2408 			cmp = fmt->collapse(fmt, he, pair);
2409 			if (cmp)
2410 				break;
2411 		}
2412 		if (!cmp)
2413 			goto out;
2414 
2415 		if (cmp < 0)
2416 			p = &parent->rb_left;
2417 		else {
2418 			p = &parent->rb_right;
2419 			leftmost = false;
2420 		}
2421 	}
2422 
2423 	he = hist_entry__new(pair, true);
2424 	if (he) {
2425 		rb_link_node(&he->rb_node_in, parent, p);
2426 		rb_insert_color_cached(&he->rb_node_in, root, leftmost);
2427 
2428 		he->dummy = true;
2429 		he->hists = hists;
2430 		memset(&he->stat, 0, sizeof(he->stat));
2431 		hists__inc_stats(hists, he);
2432 	}
2433 out:
2434 	return he;
2435 }
2436 
2437 static struct hist_entry *hists__find_entry(struct hists *hists,
2438 					    struct hist_entry *he)
2439 {
2440 	struct rb_node *n;
2441 
2442 	if (hists__has(hists, need_collapse))
2443 		n = hists->entries_collapsed.rb_root.rb_node;
2444 	else
2445 		n = hists->entries_in->rb_root.rb_node;
2446 
2447 	while (n) {
2448 		struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2449 		int64_t cmp = hist_entry__collapse(iter, he);
2450 
2451 		if (cmp < 0)
2452 			n = n->rb_left;
2453 		else if (cmp > 0)
2454 			n = n->rb_right;
2455 		else
2456 			return iter;
2457 	}
2458 
2459 	return NULL;
2460 }
2461 
2462 static struct hist_entry *hists__find_hierarchy_entry(struct rb_root_cached *root,
2463 						      struct hist_entry *he)
2464 {
2465 	struct rb_node *n = root->rb_root.rb_node;
2466 
2467 	while (n) {
2468 		struct hist_entry *iter;
2469 		struct perf_hpp_fmt *fmt;
2470 		int64_t cmp = 0;
2471 
2472 		iter = rb_entry(n, struct hist_entry, rb_node_in);
2473 		perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2474 			cmp = fmt->collapse(fmt, iter, he);
2475 			if (cmp)
2476 				break;
2477 		}
2478 
2479 		if (cmp < 0)
2480 			n = n->rb_left;
2481 		else if (cmp > 0)
2482 			n = n->rb_right;
2483 		else
2484 			return iter;
2485 	}
2486 
2487 	return NULL;
2488 }
2489 
2490 static void hists__match_hierarchy(struct rb_root_cached *leader_root,
2491 				   struct rb_root_cached *other_root)
2492 {
2493 	struct rb_node *nd;
2494 	struct hist_entry *pos, *pair;
2495 
2496 	for (nd = rb_first_cached(leader_root); nd; nd = rb_next(nd)) {
2497 		pos  = rb_entry(nd, struct hist_entry, rb_node_in);
2498 		pair = hists__find_hierarchy_entry(other_root, pos);
2499 
2500 		if (pair) {
2501 			hist_entry__add_pair(pair, pos);
2502 			hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2503 		}
2504 	}
2505 }
2506 
2507 /*
2508  * Look for pairs to link to the leader buckets (hist_entries):
2509  */
2510 void hists__match(struct hists *leader, struct hists *other)
2511 {
2512 	struct rb_root_cached *root;
2513 	struct rb_node *nd;
2514 	struct hist_entry *pos, *pair;
2515 
2516 	if (symbol_conf.report_hierarchy) {
2517 		/* hierarchy report always collapses entries */
2518 		return hists__match_hierarchy(&leader->entries_collapsed,
2519 					      &other->entries_collapsed);
2520 	}
2521 
2522 	if (hists__has(leader, need_collapse))
2523 		root = &leader->entries_collapsed;
2524 	else
2525 		root = leader->entries_in;
2526 
2527 	for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2528 		pos  = rb_entry(nd, struct hist_entry, rb_node_in);
2529 		pair = hists__find_entry(other, pos);
2530 
2531 		if (pair)
2532 			hist_entry__add_pair(pair, pos);
2533 	}
2534 }
2535 
2536 static int hists__link_hierarchy(struct hists *leader_hists,
2537 				 struct hist_entry *parent,
2538 				 struct rb_root_cached *leader_root,
2539 				 struct rb_root_cached *other_root)
2540 {
2541 	struct rb_node *nd;
2542 	struct hist_entry *pos, *leader;
2543 
2544 	for (nd = rb_first_cached(other_root); nd; nd = rb_next(nd)) {
2545 		pos = rb_entry(nd, struct hist_entry, rb_node_in);
2546 
2547 		if (hist_entry__has_pairs(pos)) {
2548 			bool found = false;
2549 
2550 			list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2551 				if (leader->hists == leader_hists) {
2552 					found = true;
2553 					break;
2554 				}
2555 			}
2556 			if (!found)
2557 				return -1;
2558 		} else {
2559 			leader = add_dummy_hierarchy_entry(leader_hists,
2560 							   leader_root, pos);
2561 			if (leader == NULL)
2562 				return -1;
2563 
2564 			/* do not point parent in the pos */
2565 			leader->parent_he = parent;
2566 
2567 			hist_entry__add_pair(pos, leader);
2568 		}
2569 
2570 		if (!pos->leaf) {
2571 			if (hists__link_hierarchy(leader_hists, leader,
2572 						  &leader->hroot_in,
2573 						  &pos->hroot_in) < 0)
2574 				return -1;
2575 		}
2576 	}
2577 	return 0;
2578 }
2579 
2580 /*
2581  * Look for entries in the other hists that are not present in the leader, if
2582  * we find them, just add a dummy entry on the leader hists, with period=0,
2583  * nr_events=0, to serve as the list header.
2584  */
2585 int hists__link(struct hists *leader, struct hists *other)
2586 {
2587 	struct rb_root_cached *root;
2588 	struct rb_node *nd;
2589 	struct hist_entry *pos, *pair;
2590 
2591 	if (symbol_conf.report_hierarchy) {
2592 		/* hierarchy report always collapses entries */
2593 		return hists__link_hierarchy(leader, NULL,
2594 					     &leader->entries_collapsed,
2595 					     &other->entries_collapsed);
2596 	}
2597 
2598 	if (hists__has(other, need_collapse))
2599 		root = &other->entries_collapsed;
2600 	else
2601 		root = other->entries_in;
2602 
2603 	for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2604 		pos = rb_entry(nd, struct hist_entry, rb_node_in);
2605 
2606 		if (!hist_entry__has_pairs(pos)) {
2607 			pair = hists__add_dummy_entry(leader, pos);
2608 			if (pair == NULL)
2609 				return -1;
2610 			hist_entry__add_pair(pos, pair);
2611 		}
2612 	}
2613 
2614 	return 0;
2615 }
2616 
2617 int hists__unlink(struct hists *hists)
2618 {
2619 	struct rb_root_cached *root;
2620 	struct rb_node *nd;
2621 	struct hist_entry *pos;
2622 
2623 	if (hists__has(hists, need_collapse))
2624 		root = &hists->entries_collapsed;
2625 	else
2626 		root = hists->entries_in;
2627 
2628 	for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2629 		pos = rb_entry(nd, struct hist_entry, rb_node_in);
2630 		list_del_init(&pos->pairs.node);
2631 	}
2632 
2633 	return 0;
2634 }
2635 
2636 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2637 			  struct perf_sample *sample, bool nonany_branch_mode,
2638 			  u64 *total_cycles)
2639 {
2640 	struct branch_info *bi;
2641 	struct branch_entry *entries = perf_sample__branch_entries(sample);
2642 
2643 	/* If we have branch cycles always annotate them. */
2644 	if (bs && bs->nr && entries[0].flags.cycles) {
2645 		int i;
2646 
2647 		bi = sample__resolve_bstack(sample, al);
2648 		if (bi) {
2649 			struct addr_map_symbol *prev = NULL;
2650 
2651 			/*
2652 			 * Ignore errors, still want to process the
2653 			 * other entries.
2654 			 *
2655 			 * For non standard branch modes always
2656 			 * force no IPC (prev == NULL)
2657 			 *
2658 			 * Note that perf stores branches reversed from
2659 			 * program order!
2660 			 */
2661 			for (i = bs->nr - 1; i >= 0; i--) {
2662 				addr_map_symbol__account_cycles(&bi[i].from,
2663 					nonany_branch_mode ? NULL : prev,
2664 					bi[i].flags.cycles);
2665 				prev = &bi[i].to;
2666 
2667 				if (total_cycles)
2668 					*total_cycles += bi[i].flags.cycles;
2669 			}
2670 			free(bi);
2671 		}
2672 	}
2673 }
2674 
2675 size_t evlist__fprintf_nr_events(struct evlist *evlist, FILE *fp,
2676 				 bool skip_empty)
2677 {
2678 	struct evsel *pos;
2679 	size_t ret = 0;
2680 
2681 	evlist__for_each_entry(evlist, pos) {
2682 		struct hists *hists = evsel__hists(pos);
2683 
2684 		if (skip_empty && !hists->stats.nr_samples)
2685 			continue;
2686 
2687 		ret += fprintf(fp, "%s stats:\n", evsel__name(pos));
2688 		ret += fprintf(fp, "%16s events: %10d\n",
2689 			       "SAMPLE", hists->stats.nr_samples);
2690 	}
2691 
2692 	return ret;
2693 }
2694 
2695 
2696 u64 hists__total_period(struct hists *hists)
2697 {
2698 	return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2699 		hists->stats.total_period;
2700 }
2701 
2702 int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq)
2703 {
2704 	char unit;
2705 	int printed;
2706 	const struct dso *dso = hists->dso_filter;
2707 	struct thread *thread = hists->thread_filter;
2708 	int socket_id = hists->socket_filter;
2709 	unsigned long nr_samples = hists->stats.nr_samples;
2710 	u64 nr_events = hists->stats.total_period;
2711 	struct evsel *evsel = hists_to_evsel(hists);
2712 	const char *ev_name = evsel__name(evsel);
2713 	char buf[512], sample_freq_str[64] = "";
2714 	size_t buflen = sizeof(buf);
2715 	char ref[30] = " show reference callgraph, ";
2716 	bool enable_ref = false;
2717 
2718 	if (symbol_conf.filter_relative) {
2719 		nr_samples = hists->stats.nr_non_filtered_samples;
2720 		nr_events = hists->stats.total_non_filtered_period;
2721 	}
2722 
2723 	if (evsel__is_group_event(evsel)) {
2724 		struct evsel *pos;
2725 
2726 		evsel__group_desc(evsel, buf, buflen);
2727 		ev_name = buf;
2728 
2729 		for_each_group_member(pos, evsel) {
2730 			struct hists *pos_hists = evsel__hists(pos);
2731 
2732 			if (symbol_conf.filter_relative) {
2733 				nr_samples += pos_hists->stats.nr_non_filtered_samples;
2734 				nr_events += pos_hists->stats.total_non_filtered_period;
2735 			} else {
2736 				nr_samples += pos_hists->stats.nr_samples;
2737 				nr_events += pos_hists->stats.total_period;
2738 			}
2739 		}
2740 	}
2741 
2742 	if (symbol_conf.show_ref_callgraph &&
2743 	    strstr(ev_name, "call-graph=no"))
2744 		enable_ref = true;
2745 
2746 	if (show_freq)
2747 		scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->core.attr.sample_freq);
2748 
2749 	nr_samples = convert_unit(nr_samples, &unit);
2750 	printed = scnprintf(bf, size,
2751 			   "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64,
2752 			   nr_samples, unit, evsel->core.nr_members > 1 ? "s" : "",
2753 			   ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events);
2754 
2755 
2756 	if (hists->uid_filter_str)
2757 		printed += snprintf(bf + printed, size - printed,
2758 				    ", UID: %s", hists->uid_filter_str);
2759 	if (thread) {
2760 		if (hists__has(hists, thread)) {
2761 			printed += scnprintf(bf + printed, size - printed,
2762 				    ", Thread: %s(%d)",
2763 				     (thread->comm_set ? thread__comm_str(thread) : ""),
2764 				    thread->tid);
2765 		} else {
2766 			printed += scnprintf(bf + printed, size - printed,
2767 				    ", Thread: %s",
2768 				     (thread->comm_set ? thread__comm_str(thread) : ""));
2769 		}
2770 	}
2771 	if (dso)
2772 		printed += scnprintf(bf + printed, size - printed,
2773 				    ", DSO: %s", dso->short_name);
2774 	if (socket_id > -1)
2775 		printed += scnprintf(bf + printed, size - printed,
2776 				    ", Processor Socket: %d", socket_id);
2777 
2778 	return printed;
2779 }
2780 
2781 int parse_filter_percentage(const struct option *opt __maybe_unused,
2782 			    const char *arg, int unset __maybe_unused)
2783 {
2784 	if (!strcmp(arg, "relative"))
2785 		symbol_conf.filter_relative = true;
2786 	else if (!strcmp(arg, "absolute"))
2787 		symbol_conf.filter_relative = false;
2788 	else {
2789 		pr_debug("Invalid percentage: %s\n", arg);
2790 		return -1;
2791 	}
2792 
2793 	return 0;
2794 }
2795 
2796 int perf_hist_config(const char *var, const char *value)
2797 {
2798 	if (!strcmp(var, "hist.percentage"))
2799 		return parse_filter_percentage(NULL, value, 0);
2800 
2801 	return 0;
2802 }
2803 
2804 int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
2805 {
2806 	memset(hists, 0, sizeof(*hists));
2807 	hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT_CACHED;
2808 	hists->entries_in = &hists->entries_in_array[0];
2809 	hists->entries_collapsed = RB_ROOT_CACHED;
2810 	hists->entries = RB_ROOT_CACHED;
2811 	pthread_mutex_init(&hists->lock, NULL);
2812 	hists->socket_filter = -1;
2813 	hists->hpp_list = hpp_list;
2814 	INIT_LIST_HEAD(&hists->hpp_formats);
2815 	return 0;
2816 }
2817 
2818 static void hists__delete_remaining_entries(struct rb_root_cached *root)
2819 {
2820 	struct rb_node *node;
2821 	struct hist_entry *he;
2822 
2823 	while (!RB_EMPTY_ROOT(&root->rb_root)) {
2824 		node = rb_first_cached(root);
2825 		rb_erase_cached(node, root);
2826 
2827 		he = rb_entry(node, struct hist_entry, rb_node_in);
2828 		hist_entry__delete(he);
2829 	}
2830 }
2831 
2832 static void hists__delete_all_entries(struct hists *hists)
2833 {
2834 	hists__delete_entries(hists);
2835 	hists__delete_remaining_entries(&hists->entries_in_array[0]);
2836 	hists__delete_remaining_entries(&hists->entries_in_array[1]);
2837 	hists__delete_remaining_entries(&hists->entries_collapsed);
2838 }
2839 
2840 static void hists_evsel__exit(struct evsel *evsel)
2841 {
2842 	struct hists *hists = evsel__hists(evsel);
2843 	struct perf_hpp_fmt *fmt, *pos;
2844 	struct perf_hpp_list_node *node, *tmp;
2845 
2846 	hists__delete_all_entries(hists);
2847 
2848 	list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2849 		perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2850 			list_del_init(&fmt->list);
2851 			free(fmt);
2852 		}
2853 		list_del_init(&node->list);
2854 		free(node);
2855 	}
2856 }
2857 
2858 static int hists_evsel__init(struct evsel *evsel)
2859 {
2860 	struct hists *hists = evsel__hists(evsel);
2861 
2862 	__hists__init(hists, &perf_hpp_list);
2863 	return 0;
2864 }
2865 
2866 /*
2867  * XXX We probably need a hists_evsel__exit() to free the hist_entries
2868  * stored in the rbtree...
2869  */
2870 
2871 int hists__init(void)
2872 {
2873 	int err = evsel__object_config(sizeof(struct hists_evsel),
2874 				       hists_evsel__init, hists_evsel__exit);
2875 	if (err)
2876 		fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2877 
2878 	return err;
2879 }
2880 
2881 void perf_hpp_list__init(struct perf_hpp_list *list)
2882 {
2883 	INIT_LIST_HEAD(&list->fields);
2884 	INIT_LIST_HEAD(&list->sorts);
2885 }
2886