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