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