• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "util.h"
2 #include "build-id.h"
3 #include "hist.h"
4 #include "session.h"
5 #include "sort.h"
6 #include "evlist.h"
7 #include "evsel.h"
8 #include "annotate.h"
9 #include "ui/progress.h"
10 #include <math.h>
11 
12 static bool hists__filter_entry_by_dso(struct hists *hists,
13 				       struct hist_entry *he);
14 static bool hists__filter_entry_by_thread(struct hists *hists,
15 					  struct hist_entry *he);
16 static bool hists__filter_entry_by_symbol(struct hists *hists,
17 					  struct hist_entry *he);
18 static bool hists__filter_entry_by_socket(struct hists *hists,
19 					  struct hist_entry *he);
20 
hists__col_len(struct hists * hists,enum hist_column col)21 u16 hists__col_len(struct hists *hists, enum hist_column col)
22 {
23 	return hists->col_len[col];
24 }
25 
hists__set_col_len(struct hists * hists,enum hist_column col,u16 len)26 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
27 {
28 	hists->col_len[col] = len;
29 }
30 
hists__new_col_len(struct hists * hists,enum hist_column col,u16 len)31 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
32 {
33 	if (len > hists__col_len(hists, col)) {
34 		hists__set_col_len(hists, col, len);
35 		return true;
36 	}
37 	return false;
38 }
39 
hists__reset_col_len(struct hists * hists)40 void hists__reset_col_len(struct hists *hists)
41 {
42 	enum hist_column col;
43 
44 	for (col = 0; col < HISTC_NR_COLS; ++col)
45 		hists__set_col_len(hists, col, 0);
46 }
47 
hists__set_unres_dso_col_len(struct hists * hists,int dso)48 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
49 {
50 	const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
51 
52 	if (hists__col_len(hists, dso) < unresolved_col_width &&
53 	    !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
54 	    !symbol_conf.dso_list)
55 		hists__set_col_len(hists, dso, unresolved_col_width);
56 }
57 
hists__calc_col_len(struct hists * hists,struct hist_entry * h)58 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
59 {
60 	const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
61 	int symlen;
62 	u16 len;
63 
64 	/*
65 	 * +4 accounts for '[x] ' priv level info
66 	 * +2 accounts for 0x prefix on raw addresses
67 	 * +3 accounts for ' y ' symtab origin info
68 	 */
69 	if (h->ms.sym) {
70 		symlen = h->ms.sym->namelen + 4;
71 		if (verbose)
72 			symlen += BITS_PER_LONG / 4 + 2 + 3;
73 		hists__new_col_len(hists, HISTC_SYMBOL, symlen);
74 	} else {
75 		symlen = unresolved_col_width + 4 + 2;
76 		hists__new_col_len(hists, HISTC_SYMBOL, symlen);
77 		hists__set_unres_dso_col_len(hists, HISTC_DSO);
78 	}
79 
80 	len = thread__comm_len(h->thread);
81 	if (hists__new_col_len(hists, HISTC_COMM, len))
82 		hists__set_col_len(hists, HISTC_THREAD, len + 6);
83 
84 	if (h->ms.map) {
85 		len = dso__name_len(h->ms.map->dso);
86 		hists__new_col_len(hists, HISTC_DSO, len);
87 	}
88 
89 	if (h->parent)
90 		hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
91 
92 	if (h->branch_info) {
93 		if (h->branch_info->from.sym) {
94 			symlen = (int)h->branch_info->from.sym->namelen + 4;
95 			if (verbose)
96 				symlen += BITS_PER_LONG / 4 + 2 + 3;
97 			hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
98 
99 			symlen = dso__name_len(h->branch_info->from.map->dso);
100 			hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
101 		} else {
102 			symlen = unresolved_col_width + 4 + 2;
103 			hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
104 			hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
105 		}
106 
107 		if (h->branch_info->to.sym) {
108 			symlen = (int)h->branch_info->to.sym->namelen + 4;
109 			if (verbose)
110 				symlen += BITS_PER_LONG / 4 + 2 + 3;
111 			hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
112 
113 			symlen = dso__name_len(h->branch_info->to.map->dso);
114 			hists__new_col_len(hists, HISTC_DSO_TO, symlen);
115 		} else {
116 			symlen = unresolved_col_width + 4 + 2;
117 			hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
118 			hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
119 		}
120 	}
121 
122 	if (h->mem_info) {
123 		if (h->mem_info->daddr.sym) {
124 			symlen = (int)h->mem_info->daddr.sym->namelen + 4
125 			       + unresolved_col_width + 2;
126 			hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
127 					   symlen);
128 			hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
129 					   symlen + 1);
130 		} else {
131 			symlen = unresolved_col_width + 4 + 2;
132 			hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
133 					   symlen);
134 			hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
135 					   symlen);
136 		}
137 
138 		if (h->mem_info->iaddr.sym) {
139 			symlen = (int)h->mem_info->iaddr.sym->namelen + 4
140 			       + unresolved_col_width + 2;
141 			hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
142 					   symlen);
143 		} else {
144 			symlen = unresolved_col_width + 4 + 2;
145 			hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
146 					   symlen);
147 		}
148 
149 		if (h->mem_info->daddr.map) {
150 			symlen = dso__name_len(h->mem_info->daddr.map->dso);
151 			hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
152 					   symlen);
153 		} else {
154 			symlen = unresolved_col_width + 4 + 2;
155 			hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
156 		}
157 	} else {
158 		symlen = unresolved_col_width + 4 + 2;
159 		hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
160 		hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
161 		hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
162 	}
163 
164 	hists__new_col_len(hists, HISTC_CPU, 3);
165 	hists__new_col_len(hists, HISTC_SOCKET, 6);
166 	hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
167 	hists__new_col_len(hists, HISTC_MEM_TLB, 22);
168 	hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
169 	hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
170 	hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
171 	hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
172 
173 	if (h->srcline)
174 		hists__new_col_len(hists, HISTC_SRCLINE, strlen(h->srcline));
175 
176 	if (h->srcfile)
177 		hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
178 
179 	if (h->transaction)
180 		hists__new_col_len(hists, HISTC_TRANSACTION,
181 				   hist_entry__transaction_len());
182 }
183 
hists__output_recalc_col_len(struct hists * hists,int max_rows)184 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
185 {
186 	struct rb_node *next = rb_first(&hists->entries);
187 	struct hist_entry *n;
188 	int row = 0;
189 
190 	hists__reset_col_len(hists);
191 
192 	while (next && row++ < max_rows) {
193 		n = rb_entry(next, struct hist_entry, rb_node);
194 		if (!n->filtered)
195 			hists__calc_col_len(hists, n);
196 		next = rb_next(&n->rb_node);
197 	}
198 }
199 
he_stat__add_cpumode_period(struct he_stat * he_stat,unsigned int cpumode,u64 period)200 static void he_stat__add_cpumode_period(struct he_stat *he_stat,
201 					unsigned int cpumode, u64 period)
202 {
203 	switch (cpumode) {
204 	case PERF_RECORD_MISC_KERNEL:
205 		he_stat->period_sys += period;
206 		break;
207 	case PERF_RECORD_MISC_USER:
208 		he_stat->period_us += period;
209 		break;
210 	case PERF_RECORD_MISC_GUEST_KERNEL:
211 		he_stat->period_guest_sys += period;
212 		break;
213 	case PERF_RECORD_MISC_GUEST_USER:
214 		he_stat->period_guest_us += period;
215 		break;
216 	default:
217 		break;
218 	}
219 }
220 
he_stat__add_period(struct he_stat * he_stat,u64 period,u64 weight)221 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
222 				u64 weight)
223 {
224 
225 	he_stat->period		+= period;
226 	he_stat->weight		+= weight;
227 	he_stat->nr_events	+= 1;
228 }
229 
he_stat__add_stat(struct he_stat * dest,struct he_stat * src)230 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
231 {
232 	dest->period		+= src->period;
233 	dest->period_sys	+= src->period_sys;
234 	dest->period_us		+= src->period_us;
235 	dest->period_guest_sys	+= src->period_guest_sys;
236 	dest->period_guest_us	+= src->period_guest_us;
237 	dest->nr_events		+= src->nr_events;
238 	dest->weight		+= src->weight;
239 }
240 
he_stat__decay(struct he_stat * he_stat)241 static void he_stat__decay(struct he_stat *he_stat)
242 {
243 	he_stat->period = (he_stat->period * 7) / 8;
244 	he_stat->nr_events = (he_stat->nr_events * 7) / 8;
245 	/* XXX need decay for weight too? */
246 }
247 
hists__decay_entry(struct hists * hists,struct hist_entry * he)248 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
249 {
250 	u64 prev_period = he->stat.period;
251 	u64 diff;
252 
253 	if (prev_period == 0)
254 		return true;
255 
256 	he_stat__decay(&he->stat);
257 	if (symbol_conf.cumulate_callchain)
258 		he_stat__decay(he->stat_acc);
259 
260 	diff = prev_period - he->stat.period;
261 
262 	hists->stats.total_period -= diff;
263 	if (!he->filtered)
264 		hists->stats.total_non_filtered_period -= diff;
265 
266 	return he->stat.period == 0;
267 }
268 
hists__delete_entry(struct hists * hists,struct hist_entry * he)269 static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
270 {
271 	rb_erase(&he->rb_node, &hists->entries);
272 
273 	if (sort__need_collapse)
274 		rb_erase(&he->rb_node_in, &hists->entries_collapsed);
275 
276 	--hists->nr_entries;
277 	if (!he->filtered)
278 		--hists->nr_non_filtered_entries;
279 
280 	hist_entry__delete(he);
281 }
282 
hists__decay_entries(struct hists * hists,bool zap_user,bool zap_kernel)283 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
284 {
285 	struct rb_node *next = rb_first(&hists->entries);
286 	struct hist_entry *n;
287 
288 	while (next) {
289 		n = rb_entry(next, struct hist_entry, rb_node);
290 		next = rb_next(&n->rb_node);
291 		if (((zap_user && n->level == '.') ||
292 		     (zap_kernel && n->level != '.') ||
293 		     hists__decay_entry(hists, n))) {
294 			hists__delete_entry(hists, n);
295 		}
296 	}
297 }
298 
hists__delete_entries(struct hists * hists)299 void hists__delete_entries(struct hists *hists)
300 {
301 	struct rb_node *next = rb_first(&hists->entries);
302 	struct hist_entry *n;
303 
304 	while (next) {
305 		n = rb_entry(next, struct hist_entry, rb_node);
306 		next = rb_next(&n->rb_node);
307 
308 		hists__delete_entry(hists, n);
309 	}
310 }
311 
312 /*
313  * histogram, sorted on item, collects periods
314  */
315 
hist_entry__new(struct hist_entry * template,bool sample_self)316 static struct hist_entry *hist_entry__new(struct hist_entry *template,
317 					  bool sample_self)
318 {
319 	size_t callchain_size = 0;
320 	struct hist_entry *he;
321 
322 	if (symbol_conf.use_callchain)
323 		callchain_size = sizeof(struct callchain_root);
324 
325 	he = zalloc(sizeof(*he) + callchain_size);
326 
327 	if (he != NULL) {
328 		*he = *template;
329 
330 		if (symbol_conf.cumulate_callchain) {
331 			he->stat_acc = malloc(sizeof(he->stat));
332 			if (he->stat_acc == NULL) {
333 				free(he);
334 				return NULL;
335 			}
336 			memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
337 			if (!sample_self)
338 				memset(&he->stat, 0, sizeof(he->stat));
339 		}
340 
341 		map__get(he->ms.map);
342 
343 		if (he->branch_info) {
344 			/*
345 			 * This branch info is (a part of) allocated from
346 			 * sample__resolve_bstack() and will be freed after
347 			 * adding new entries.  So we need to save a copy.
348 			 */
349 			he->branch_info = malloc(sizeof(*he->branch_info));
350 			if (he->branch_info == NULL) {
351 				map__zput(he->ms.map);
352 				free(he->stat_acc);
353 				free(he);
354 				return NULL;
355 			}
356 
357 			memcpy(he->branch_info, template->branch_info,
358 			       sizeof(*he->branch_info));
359 
360 			map__get(he->branch_info->from.map);
361 			map__get(he->branch_info->to.map);
362 		}
363 
364 		if (he->mem_info) {
365 			map__get(he->mem_info->iaddr.map);
366 			map__get(he->mem_info->daddr.map);
367 		}
368 
369 		if (symbol_conf.use_callchain)
370 			callchain_init(he->callchain);
371 
372 		INIT_LIST_HEAD(&he->pairs.node);
373 		thread__get(he->thread);
374 	}
375 
376 	return he;
377 }
378 
symbol__parent_filter(const struct symbol * parent)379 static u8 symbol__parent_filter(const struct symbol *parent)
380 {
381 	if (symbol_conf.exclude_other && parent == NULL)
382 		return 1 << HIST_FILTER__PARENT;
383 	return 0;
384 }
385 
hists__findnew_entry(struct hists * hists,struct hist_entry * entry,struct addr_location * al,bool sample_self)386 static struct hist_entry *hists__findnew_entry(struct hists *hists,
387 					       struct hist_entry *entry,
388 					       struct addr_location *al,
389 					       bool sample_self)
390 {
391 	struct rb_node **p;
392 	struct rb_node *parent = NULL;
393 	struct hist_entry *he;
394 	int64_t cmp;
395 	u64 period = entry->stat.period;
396 	u64 weight = entry->stat.weight;
397 
398 	p = &hists->entries_in->rb_node;
399 
400 	while (*p != NULL) {
401 		parent = *p;
402 		he = rb_entry(parent, struct hist_entry, rb_node_in);
403 
404 		/*
405 		 * Make sure that it receives arguments in a same order as
406 		 * hist_entry__collapse() so that we can use an appropriate
407 		 * function when searching an entry regardless which sort
408 		 * keys were used.
409 		 */
410 		cmp = hist_entry__cmp(he, entry);
411 
412 		if (!cmp) {
413 			if (sample_self)
414 				he_stat__add_period(&he->stat, period, weight);
415 			if (symbol_conf.cumulate_callchain)
416 				he_stat__add_period(he->stat_acc, period, weight);
417 
418 			/*
419 			 * This mem info was allocated from sample__resolve_mem
420 			 * and will not be used anymore.
421 			 */
422 			zfree(&entry->mem_info);
423 
424 			/* If the map of an existing hist_entry has
425 			 * become out-of-date due to an exec() or
426 			 * similar, update it.  Otherwise we will
427 			 * mis-adjust symbol addresses when computing
428 			 * the history counter to increment.
429 			 */
430 			if (he->ms.map != entry->ms.map) {
431 				map__put(he->ms.map);
432 				he->ms.map = map__get(entry->ms.map);
433 			}
434 			goto out;
435 		}
436 
437 		if (cmp < 0)
438 			p = &(*p)->rb_left;
439 		else
440 			p = &(*p)->rb_right;
441 	}
442 
443 	he = hist_entry__new(entry, sample_self);
444 	if (!he)
445 		return NULL;
446 
447 	hists->nr_entries++;
448 
449 	rb_link_node(&he->rb_node_in, parent, p);
450 	rb_insert_color(&he->rb_node_in, hists->entries_in);
451 out:
452 	if (sample_self)
453 		he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
454 	if (symbol_conf.cumulate_callchain)
455 		he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
456 	return he;
457 }
458 
__hists__add_entry(struct hists * hists,struct addr_location * al,struct symbol * sym_parent,struct branch_info * bi,struct mem_info * mi,u64 period,u64 weight,u64 transaction,bool sample_self)459 struct hist_entry *__hists__add_entry(struct hists *hists,
460 				      struct addr_location *al,
461 				      struct symbol *sym_parent,
462 				      struct branch_info *bi,
463 				      struct mem_info *mi,
464 				      u64 period, u64 weight, u64 transaction,
465 				      bool sample_self)
466 {
467 	struct hist_entry entry = {
468 		.thread	= al->thread,
469 		.comm = thread__comm(al->thread),
470 		.ms = {
471 			.map	= al->map,
472 			.sym	= al->sym,
473 		},
474 		.socket	 = al->socket,
475 		.cpu	 = al->cpu,
476 		.cpumode = al->cpumode,
477 		.ip	 = al->addr,
478 		.level	 = al->level,
479 		.stat = {
480 			.nr_events = 1,
481 			.period	= period,
482 			.weight = weight,
483 		},
484 		.parent = sym_parent,
485 		.filtered = symbol__parent_filter(sym_parent) | al->filtered,
486 		.hists	= hists,
487 		.branch_info = bi,
488 		.mem_info = mi,
489 		.transaction = transaction,
490 	};
491 
492 	return hists__findnew_entry(hists, &entry, al, sample_self);
493 }
494 
495 static int
iter_next_nop_entry(struct hist_entry_iter * iter __maybe_unused,struct addr_location * al __maybe_unused)496 iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
497 		    struct addr_location *al __maybe_unused)
498 {
499 	return 0;
500 }
501 
502 static int
iter_add_next_nop_entry(struct hist_entry_iter * iter __maybe_unused,struct addr_location * al __maybe_unused)503 iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
504 			struct addr_location *al __maybe_unused)
505 {
506 	return 0;
507 }
508 
509 static int
iter_prepare_mem_entry(struct hist_entry_iter * iter,struct addr_location * al)510 iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
511 {
512 	struct perf_sample *sample = iter->sample;
513 	struct mem_info *mi;
514 
515 	mi = sample__resolve_mem(sample, al);
516 	if (mi == NULL)
517 		return -ENOMEM;
518 
519 	iter->priv = mi;
520 	return 0;
521 }
522 
523 static int
iter_add_single_mem_entry(struct hist_entry_iter * iter,struct addr_location * al)524 iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
525 {
526 	u64 cost;
527 	struct mem_info *mi = iter->priv;
528 	struct hists *hists = evsel__hists(iter->evsel);
529 	struct hist_entry *he;
530 
531 	if (mi == NULL)
532 		return -EINVAL;
533 
534 	cost = iter->sample->weight;
535 	if (!cost)
536 		cost = 1;
537 
538 	/*
539 	 * must pass period=weight in order to get the correct
540 	 * sorting from hists__collapse_resort() which is solely
541 	 * based on periods. We want sorting be done on nr_events * weight
542 	 * and this is indirectly achieved by passing period=weight here
543 	 * and the he_stat__add_period() function.
544 	 */
545 	he = __hists__add_entry(hists, al, iter->parent, NULL, mi,
546 				cost, cost, 0, true);
547 	if (!he)
548 		return -ENOMEM;
549 
550 	iter->he = he;
551 	return 0;
552 }
553 
554 static int
iter_finish_mem_entry(struct hist_entry_iter * iter,struct addr_location * al __maybe_unused)555 iter_finish_mem_entry(struct hist_entry_iter *iter,
556 		      struct addr_location *al __maybe_unused)
557 {
558 	struct perf_evsel *evsel = iter->evsel;
559 	struct hists *hists = evsel__hists(evsel);
560 	struct hist_entry *he = iter->he;
561 	int err = -EINVAL;
562 
563 	if (he == NULL)
564 		goto out;
565 
566 	hists__inc_nr_samples(hists, he->filtered);
567 
568 	err = hist_entry__append_callchain(he, iter->sample);
569 
570 out:
571 	/*
572 	 * We don't need to free iter->priv (mem_info) here since the mem info
573 	 * was either already freed in hists__findnew_entry() or passed to a
574 	 * new hist entry by hist_entry__new().
575 	 */
576 	iter->priv = NULL;
577 
578 	iter->he = NULL;
579 	return err;
580 }
581 
582 static int
iter_prepare_branch_entry(struct hist_entry_iter * iter,struct addr_location * al)583 iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
584 {
585 	struct branch_info *bi;
586 	struct perf_sample *sample = iter->sample;
587 
588 	bi = sample__resolve_bstack(sample, al);
589 	if (!bi)
590 		return -ENOMEM;
591 
592 	iter->curr = 0;
593 	iter->total = sample->branch_stack->nr;
594 
595 	iter->priv = bi;
596 	return 0;
597 }
598 
599 static int
iter_add_single_branch_entry(struct hist_entry_iter * iter __maybe_unused,struct addr_location * al __maybe_unused)600 iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
601 			     struct addr_location *al __maybe_unused)
602 {
603 	/* to avoid calling callback function */
604 	iter->he = NULL;
605 
606 	return 0;
607 }
608 
609 static int
iter_next_branch_entry(struct hist_entry_iter * iter,struct addr_location * al)610 iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
611 {
612 	struct branch_info *bi = iter->priv;
613 	int i = iter->curr;
614 
615 	if (bi == NULL)
616 		return 0;
617 
618 	if (iter->curr >= iter->total)
619 		return 0;
620 
621 	al->map = bi[i].to.map;
622 	al->sym = bi[i].to.sym;
623 	al->addr = bi[i].to.addr;
624 	return 1;
625 }
626 
627 static int
iter_add_next_branch_entry(struct hist_entry_iter * iter,struct addr_location * al)628 iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
629 {
630 	struct branch_info *bi;
631 	struct perf_evsel *evsel = iter->evsel;
632 	struct hists *hists = evsel__hists(evsel);
633 	struct hist_entry *he = NULL;
634 	int i = iter->curr;
635 	int err = 0;
636 
637 	bi = iter->priv;
638 
639 	if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
640 		goto out;
641 
642 	/*
643 	 * The report shows the percentage of total branches captured
644 	 * and not events sampled. Thus we use a pseudo period of 1.
645 	 */
646 	he = __hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
647 				1, bi->flags.cycles ? bi->flags.cycles : 1,
648 				0, true);
649 	if (he == NULL)
650 		return -ENOMEM;
651 
652 	hists__inc_nr_samples(hists, he->filtered);
653 
654 out:
655 	iter->he = he;
656 	iter->curr++;
657 	return err;
658 }
659 
660 static int
iter_finish_branch_entry(struct hist_entry_iter * iter,struct addr_location * al __maybe_unused)661 iter_finish_branch_entry(struct hist_entry_iter *iter,
662 			 struct addr_location *al __maybe_unused)
663 {
664 	zfree(&iter->priv);
665 	iter->he = NULL;
666 
667 	return iter->curr >= iter->total ? 0 : -1;
668 }
669 
670 static int
iter_prepare_normal_entry(struct hist_entry_iter * iter __maybe_unused,struct addr_location * al __maybe_unused)671 iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
672 			  struct addr_location *al __maybe_unused)
673 {
674 	return 0;
675 }
676 
677 static int
iter_add_single_normal_entry(struct hist_entry_iter * iter,struct addr_location * al)678 iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
679 {
680 	struct perf_evsel *evsel = iter->evsel;
681 	struct perf_sample *sample = iter->sample;
682 	struct hist_entry *he;
683 
684 	he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
685 				sample->period, sample->weight,
686 				sample->transaction, true);
687 	if (he == NULL)
688 		return -ENOMEM;
689 
690 	iter->he = he;
691 	return 0;
692 }
693 
694 static int
iter_finish_normal_entry(struct hist_entry_iter * iter,struct addr_location * al __maybe_unused)695 iter_finish_normal_entry(struct hist_entry_iter *iter,
696 			 struct addr_location *al __maybe_unused)
697 {
698 	struct hist_entry *he = iter->he;
699 	struct perf_evsel *evsel = iter->evsel;
700 	struct perf_sample *sample = iter->sample;
701 
702 	if (he == NULL)
703 		return 0;
704 
705 	iter->he = NULL;
706 
707 	hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
708 
709 	return hist_entry__append_callchain(he, sample);
710 }
711 
712 static int
iter_prepare_cumulative_entry(struct hist_entry_iter * iter,struct addr_location * al __maybe_unused)713 iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
714 			      struct addr_location *al __maybe_unused)
715 {
716 	struct hist_entry **he_cache;
717 
718 	callchain_cursor_commit(&callchain_cursor);
719 
720 	/*
721 	 * This is for detecting cycles or recursions so that they're
722 	 * cumulated only one time to prevent entries more than 100%
723 	 * overhead.
724 	 */
725 	he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1));
726 	if (he_cache == NULL)
727 		return -ENOMEM;
728 
729 	iter->priv = he_cache;
730 	iter->curr = 0;
731 
732 	return 0;
733 }
734 
735 static int
iter_add_single_cumulative_entry(struct hist_entry_iter * iter,struct addr_location * al)736 iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
737 				 struct addr_location *al)
738 {
739 	struct perf_evsel *evsel = iter->evsel;
740 	struct hists *hists = evsel__hists(evsel);
741 	struct perf_sample *sample = iter->sample;
742 	struct hist_entry **he_cache = iter->priv;
743 	struct hist_entry *he;
744 	int err = 0;
745 
746 	he = __hists__add_entry(hists, al, iter->parent, NULL, NULL,
747 				sample->period, sample->weight,
748 				sample->transaction, true);
749 	if (he == NULL)
750 		return -ENOMEM;
751 
752 	iter->he = he;
753 	he_cache[iter->curr++] = he;
754 
755 	hist_entry__append_callchain(he, sample);
756 
757 	/*
758 	 * We need to re-initialize the cursor since callchain_append()
759 	 * advanced the cursor to the end.
760 	 */
761 	callchain_cursor_commit(&callchain_cursor);
762 
763 	hists__inc_nr_samples(hists, he->filtered);
764 
765 	return err;
766 }
767 
768 static int
iter_next_cumulative_entry(struct hist_entry_iter * iter,struct addr_location * al)769 iter_next_cumulative_entry(struct hist_entry_iter *iter,
770 			   struct addr_location *al)
771 {
772 	struct callchain_cursor_node *node;
773 
774 	node = callchain_cursor_current(&callchain_cursor);
775 	if (node == NULL)
776 		return 0;
777 
778 	return fill_callchain_info(al, node, iter->hide_unresolved);
779 }
780 
781 static int
iter_add_next_cumulative_entry(struct hist_entry_iter * iter,struct addr_location * al)782 iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
783 			       struct addr_location *al)
784 {
785 	struct perf_evsel *evsel = iter->evsel;
786 	struct perf_sample *sample = iter->sample;
787 	struct hist_entry **he_cache = iter->priv;
788 	struct hist_entry *he;
789 	struct hist_entry he_tmp = {
790 		.hists = evsel__hists(evsel),
791 		.cpu = al->cpu,
792 		.thread = al->thread,
793 		.comm = thread__comm(al->thread),
794 		.ip = al->addr,
795 		.ms = {
796 			.map = al->map,
797 			.sym = al->sym,
798 		},
799 		.parent = iter->parent,
800 	};
801 	int i;
802 	struct callchain_cursor cursor;
803 
804 	callchain_cursor_snapshot(&cursor, &callchain_cursor);
805 
806 	callchain_cursor_advance(&callchain_cursor);
807 
808 	/*
809 	 * Check if there's duplicate entries in the callchain.
810 	 * It's possible that it has cycles or recursive calls.
811 	 */
812 	for (i = 0; i < iter->curr; i++) {
813 		if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
814 			/* to avoid calling callback function */
815 			iter->he = NULL;
816 			return 0;
817 		}
818 	}
819 
820 	he = __hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
821 				sample->period, sample->weight,
822 				sample->transaction, false);
823 	if (he == NULL)
824 		return -ENOMEM;
825 
826 	iter->he = he;
827 	he_cache[iter->curr++] = he;
828 
829 	if (symbol_conf.use_callchain)
830 		callchain_append(he->callchain, &cursor, sample->period);
831 	return 0;
832 }
833 
834 static int
iter_finish_cumulative_entry(struct hist_entry_iter * iter,struct addr_location * al __maybe_unused)835 iter_finish_cumulative_entry(struct hist_entry_iter *iter,
836 			     struct addr_location *al __maybe_unused)
837 {
838 	zfree(&iter->priv);
839 	iter->he = NULL;
840 
841 	return 0;
842 }
843 
844 const struct hist_iter_ops hist_iter_mem = {
845 	.prepare_entry 		= iter_prepare_mem_entry,
846 	.add_single_entry 	= iter_add_single_mem_entry,
847 	.next_entry 		= iter_next_nop_entry,
848 	.add_next_entry 	= iter_add_next_nop_entry,
849 	.finish_entry 		= iter_finish_mem_entry,
850 };
851 
852 const struct hist_iter_ops hist_iter_branch = {
853 	.prepare_entry 		= iter_prepare_branch_entry,
854 	.add_single_entry 	= iter_add_single_branch_entry,
855 	.next_entry 		= iter_next_branch_entry,
856 	.add_next_entry 	= iter_add_next_branch_entry,
857 	.finish_entry 		= iter_finish_branch_entry,
858 };
859 
860 const struct hist_iter_ops hist_iter_normal = {
861 	.prepare_entry 		= iter_prepare_normal_entry,
862 	.add_single_entry 	= iter_add_single_normal_entry,
863 	.next_entry 		= iter_next_nop_entry,
864 	.add_next_entry 	= iter_add_next_nop_entry,
865 	.finish_entry 		= iter_finish_normal_entry,
866 };
867 
868 const struct hist_iter_ops hist_iter_cumulative = {
869 	.prepare_entry 		= iter_prepare_cumulative_entry,
870 	.add_single_entry 	= iter_add_single_cumulative_entry,
871 	.next_entry 		= iter_next_cumulative_entry,
872 	.add_next_entry 	= iter_add_next_cumulative_entry,
873 	.finish_entry 		= iter_finish_cumulative_entry,
874 };
875 
hist_entry_iter__add(struct hist_entry_iter * iter,struct addr_location * al,int max_stack_depth,void * arg)876 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
877 			 int max_stack_depth, void *arg)
878 {
879 	int err, err2;
880 
881 	err = sample__resolve_callchain(iter->sample, &iter->parent,
882 					iter->evsel, al, max_stack_depth);
883 	if (err)
884 		return err;
885 
886 	err = iter->ops->prepare_entry(iter, al);
887 	if (err)
888 		goto out;
889 
890 	err = iter->ops->add_single_entry(iter, al);
891 	if (err)
892 		goto out;
893 
894 	if (iter->he && iter->add_entry_cb) {
895 		err = iter->add_entry_cb(iter, al, true, arg);
896 		if (err)
897 			goto out;
898 	}
899 
900 	while (iter->ops->next_entry(iter, al)) {
901 		err = iter->ops->add_next_entry(iter, al);
902 		if (err)
903 			break;
904 
905 		if (iter->he && iter->add_entry_cb) {
906 			err = iter->add_entry_cb(iter, al, false, arg);
907 			if (err)
908 				goto out;
909 		}
910 	}
911 
912 out:
913 	err2 = iter->ops->finish_entry(iter, al);
914 	if (!err)
915 		err = err2;
916 
917 	return err;
918 }
919 
920 int64_t
hist_entry__cmp(struct hist_entry * left,struct hist_entry * right)921 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
922 {
923 	struct perf_hpp_fmt *fmt;
924 	int64_t cmp = 0;
925 
926 	perf_hpp__for_each_sort_list(fmt) {
927 		if (perf_hpp__should_skip(fmt))
928 			continue;
929 
930 		cmp = fmt->cmp(fmt, left, right);
931 		if (cmp)
932 			break;
933 	}
934 
935 	return cmp;
936 }
937 
938 int64_t
hist_entry__collapse(struct hist_entry * left,struct hist_entry * right)939 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
940 {
941 	struct perf_hpp_fmt *fmt;
942 	int64_t cmp = 0;
943 
944 	perf_hpp__for_each_sort_list(fmt) {
945 		if (perf_hpp__should_skip(fmt))
946 			continue;
947 
948 		cmp = fmt->collapse(fmt, left, right);
949 		if (cmp)
950 			break;
951 	}
952 
953 	return cmp;
954 }
955 
hist_entry__delete(struct hist_entry * he)956 void hist_entry__delete(struct hist_entry *he)
957 {
958 	thread__zput(he->thread);
959 	map__zput(he->ms.map);
960 
961 	if (he->branch_info) {
962 		map__zput(he->branch_info->from.map);
963 		map__zput(he->branch_info->to.map);
964 		zfree(&he->branch_info);
965 	}
966 
967 	if (he->mem_info) {
968 		map__zput(he->mem_info->iaddr.map);
969 		map__zput(he->mem_info->daddr.map);
970 		zfree(&he->mem_info);
971 	}
972 
973 	zfree(&he->stat_acc);
974 	free_srcline(he->srcline);
975 	if (he->srcfile && he->srcfile[0])
976 		free(he->srcfile);
977 	free_callchain(he->callchain);
978 	free(he);
979 }
980 
981 /*
982  * collapse the histogram
983  */
984 
hists__collapse_insert_entry(struct hists * hists __maybe_unused,struct rb_root * root,struct hist_entry * he)985 static bool hists__collapse_insert_entry(struct hists *hists __maybe_unused,
986 					 struct rb_root *root,
987 					 struct hist_entry *he)
988 {
989 	struct rb_node **p = &root->rb_node;
990 	struct rb_node *parent = NULL;
991 	struct hist_entry *iter;
992 	int64_t cmp;
993 
994 	while (*p != NULL) {
995 		parent = *p;
996 		iter = rb_entry(parent, struct hist_entry, rb_node_in);
997 
998 		cmp = hist_entry__collapse(iter, he);
999 
1000 		if (!cmp) {
1001 			he_stat__add_stat(&iter->stat, &he->stat);
1002 			if (symbol_conf.cumulate_callchain)
1003 				he_stat__add_stat(iter->stat_acc, he->stat_acc);
1004 
1005 			if (symbol_conf.use_callchain) {
1006 				callchain_cursor_reset(&callchain_cursor);
1007 				callchain_merge(&callchain_cursor,
1008 						iter->callchain,
1009 						he->callchain);
1010 			}
1011 			hist_entry__delete(he);
1012 			return false;
1013 		}
1014 
1015 		if (cmp < 0)
1016 			p = &(*p)->rb_left;
1017 		else
1018 			p = &(*p)->rb_right;
1019 	}
1020 	hists->nr_entries++;
1021 
1022 	rb_link_node(&he->rb_node_in, parent, p);
1023 	rb_insert_color(&he->rb_node_in, root);
1024 	return true;
1025 }
1026 
hists__get_rotate_entries_in(struct hists * hists)1027 static struct rb_root *hists__get_rotate_entries_in(struct hists *hists)
1028 {
1029 	struct rb_root *root;
1030 
1031 	pthread_mutex_lock(&hists->lock);
1032 
1033 	root = hists->entries_in;
1034 	if (++hists->entries_in > &hists->entries_in_array[1])
1035 		hists->entries_in = &hists->entries_in_array[0];
1036 
1037 	pthread_mutex_unlock(&hists->lock);
1038 
1039 	return root;
1040 }
1041 
hists__apply_filters(struct hists * hists,struct hist_entry * he)1042 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1043 {
1044 	hists__filter_entry_by_dso(hists, he);
1045 	hists__filter_entry_by_thread(hists, he);
1046 	hists__filter_entry_by_symbol(hists, he);
1047 	hists__filter_entry_by_socket(hists, he);
1048 }
1049 
hists__collapse_resort(struct hists * hists,struct ui_progress * prog)1050 void hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
1051 {
1052 	struct rb_root *root;
1053 	struct rb_node *next;
1054 	struct hist_entry *n;
1055 
1056 	if (!sort__need_collapse)
1057 		return;
1058 
1059 	hists->nr_entries = 0;
1060 
1061 	root = hists__get_rotate_entries_in(hists);
1062 
1063 	next = rb_first(root);
1064 
1065 	while (next) {
1066 		if (session_done())
1067 			break;
1068 		n = rb_entry(next, struct hist_entry, rb_node_in);
1069 		next = rb_next(&n->rb_node_in);
1070 
1071 		rb_erase(&n->rb_node_in, root);
1072 		if (hists__collapse_insert_entry(hists, &hists->entries_collapsed, n)) {
1073 			/*
1074 			 * If it wasn't combined with one of the entries already
1075 			 * collapsed, we need to apply the filters that may have
1076 			 * been set by, say, the hist_browser.
1077 			 */
1078 			hists__apply_filters(hists, n);
1079 		}
1080 		if (prog)
1081 			ui_progress__update(prog, 1);
1082 	}
1083 }
1084 
hist_entry__sort(struct hist_entry * a,struct hist_entry * b)1085 static int64_t hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
1086 {
1087 	struct perf_hpp_fmt *fmt;
1088 	int64_t cmp = 0;
1089 
1090 	perf_hpp__for_each_sort_list(fmt) {
1091 		if (perf_hpp__should_skip(fmt))
1092 			continue;
1093 
1094 		cmp = fmt->sort(fmt, a, b);
1095 		if (cmp)
1096 			break;
1097 	}
1098 
1099 	return cmp;
1100 }
1101 
hists__reset_filter_stats(struct hists * hists)1102 static void hists__reset_filter_stats(struct hists *hists)
1103 {
1104 	hists->nr_non_filtered_entries = 0;
1105 	hists->stats.total_non_filtered_period = 0;
1106 }
1107 
hists__reset_stats(struct hists * hists)1108 void hists__reset_stats(struct hists *hists)
1109 {
1110 	hists->nr_entries = 0;
1111 	hists->stats.total_period = 0;
1112 
1113 	hists__reset_filter_stats(hists);
1114 }
1115 
hists__inc_filter_stats(struct hists * hists,struct hist_entry * h)1116 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1117 {
1118 	hists->nr_non_filtered_entries++;
1119 	hists->stats.total_non_filtered_period += h->stat.period;
1120 }
1121 
hists__inc_stats(struct hists * hists,struct hist_entry * h)1122 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1123 {
1124 	if (!h->filtered)
1125 		hists__inc_filter_stats(hists, h);
1126 
1127 	hists->nr_entries++;
1128 	hists->stats.total_period += h->stat.period;
1129 }
1130 
__hists__insert_output_entry(struct rb_root * entries,struct hist_entry * he,u64 min_callchain_hits,bool use_callchain)1131 static void __hists__insert_output_entry(struct rb_root *entries,
1132 					 struct hist_entry *he,
1133 					 u64 min_callchain_hits,
1134 					 bool use_callchain)
1135 {
1136 	struct rb_node **p = &entries->rb_node;
1137 	struct rb_node *parent = NULL;
1138 	struct hist_entry *iter;
1139 
1140 	if (use_callchain)
1141 		callchain_param.sort(&he->sorted_chain, he->callchain,
1142 				      min_callchain_hits, &callchain_param);
1143 
1144 	while (*p != NULL) {
1145 		parent = *p;
1146 		iter = rb_entry(parent, struct hist_entry, rb_node);
1147 
1148 		if (hist_entry__sort(he, iter) > 0)
1149 			p = &(*p)->rb_left;
1150 		else
1151 			p = &(*p)->rb_right;
1152 	}
1153 
1154 	rb_link_node(&he->rb_node, parent, p);
1155 	rb_insert_color(&he->rb_node, entries);
1156 }
1157 
hists__output_resort(struct hists * hists,struct ui_progress * prog)1158 void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1159 {
1160 	struct rb_root *root;
1161 	struct rb_node *next;
1162 	struct hist_entry *n;
1163 	u64 min_callchain_hits;
1164 	struct perf_evsel *evsel = hists_to_evsel(hists);
1165 	bool use_callchain;
1166 
1167 	if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1168 		use_callchain = evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN;
1169 	else
1170 		use_callchain = symbol_conf.use_callchain;
1171 
1172 	min_callchain_hits = hists->stats.total_period * (callchain_param.min_percent / 100);
1173 
1174 	if (sort__need_collapse)
1175 		root = &hists->entries_collapsed;
1176 	else
1177 		root = hists->entries_in;
1178 
1179 	next = rb_first(root);
1180 	hists->entries = RB_ROOT;
1181 
1182 	hists__reset_stats(hists);
1183 	hists__reset_col_len(hists);
1184 
1185 	while (next) {
1186 		n = rb_entry(next, struct hist_entry, rb_node_in);
1187 		next = rb_next(&n->rb_node_in);
1188 
1189 		__hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
1190 		hists__inc_stats(hists, n);
1191 
1192 		if (!n->filtered)
1193 			hists__calc_col_len(hists, n);
1194 
1195 		if (prog)
1196 			ui_progress__update(prog, 1);
1197 	}
1198 }
1199 
hists__remove_entry_filter(struct hists * hists,struct hist_entry * h,enum hist_filter filter)1200 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
1201 				       enum hist_filter filter)
1202 {
1203 	h->filtered &= ~(1 << filter);
1204 	if (h->filtered)
1205 		return;
1206 
1207 	/* force fold unfiltered entry for simplicity */
1208 	h->unfolded = false;
1209 	h->row_offset = 0;
1210 	h->nr_rows = 0;
1211 
1212 	hists->stats.nr_non_filtered_samples += h->stat.nr_events;
1213 
1214 	hists__inc_filter_stats(hists, h);
1215 	hists__calc_col_len(hists, h);
1216 }
1217 
1218 
hists__filter_entry_by_dso(struct hists * hists,struct hist_entry * he)1219 static bool hists__filter_entry_by_dso(struct hists *hists,
1220 				       struct hist_entry *he)
1221 {
1222 	if (hists->dso_filter != NULL &&
1223 	    (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1224 		he->filtered |= (1 << HIST_FILTER__DSO);
1225 		return true;
1226 	}
1227 
1228 	return false;
1229 }
1230 
hists__filter_by_dso(struct hists * hists)1231 void hists__filter_by_dso(struct hists *hists)
1232 {
1233 	struct rb_node *nd;
1234 
1235 	hists->stats.nr_non_filtered_samples = 0;
1236 
1237 	hists__reset_filter_stats(hists);
1238 	hists__reset_col_len(hists);
1239 
1240 	for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1241 		struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1242 
1243 		if (symbol_conf.exclude_other && !h->parent)
1244 			continue;
1245 
1246 		if (hists__filter_entry_by_dso(hists, h))
1247 			continue;
1248 
1249 		hists__remove_entry_filter(hists, h, HIST_FILTER__DSO);
1250 	}
1251 }
1252 
hists__filter_entry_by_thread(struct hists * hists,struct hist_entry * he)1253 static bool hists__filter_entry_by_thread(struct hists *hists,
1254 					  struct hist_entry *he)
1255 {
1256 	if (hists->thread_filter != NULL &&
1257 	    he->thread != hists->thread_filter) {
1258 		he->filtered |= (1 << HIST_FILTER__THREAD);
1259 		return true;
1260 	}
1261 
1262 	return false;
1263 }
1264 
hists__filter_by_thread(struct hists * hists)1265 void hists__filter_by_thread(struct hists *hists)
1266 {
1267 	struct rb_node *nd;
1268 
1269 	hists->stats.nr_non_filtered_samples = 0;
1270 
1271 	hists__reset_filter_stats(hists);
1272 	hists__reset_col_len(hists);
1273 
1274 	for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1275 		struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1276 
1277 		if (hists__filter_entry_by_thread(hists, h))
1278 			continue;
1279 
1280 		hists__remove_entry_filter(hists, h, HIST_FILTER__THREAD);
1281 	}
1282 }
1283 
hists__filter_entry_by_symbol(struct hists * hists,struct hist_entry * he)1284 static bool hists__filter_entry_by_symbol(struct hists *hists,
1285 					  struct hist_entry *he)
1286 {
1287 	if (hists->symbol_filter_str != NULL &&
1288 	    (!he->ms.sym || strstr(he->ms.sym->name,
1289 				   hists->symbol_filter_str) == NULL)) {
1290 		he->filtered |= (1 << HIST_FILTER__SYMBOL);
1291 		return true;
1292 	}
1293 
1294 	return false;
1295 }
1296 
hists__filter_by_symbol(struct hists * hists)1297 void hists__filter_by_symbol(struct hists *hists)
1298 {
1299 	struct rb_node *nd;
1300 
1301 	hists->stats.nr_non_filtered_samples = 0;
1302 
1303 	hists__reset_filter_stats(hists);
1304 	hists__reset_col_len(hists);
1305 
1306 	for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1307 		struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1308 
1309 		if (hists__filter_entry_by_symbol(hists, h))
1310 			continue;
1311 
1312 		hists__remove_entry_filter(hists, h, HIST_FILTER__SYMBOL);
1313 	}
1314 }
1315 
hists__filter_entry_by_socket(struct hists * hists,struct hist_entry * he)1316 static bool hists__filter_entry_by_socket(struct hists *hists,
1317 					  struct hist_entry *he)
1318 {
1319 	if ((hists->socket_filter > -1) &&
1320 	    (he->socket != hists->socket_filter)) {
1321 		he->filtered |= (1 << HIST_FILTER__SOCKET);
1322 		return true;
1323 	}
1324 
1325 	return false;
1326 }
1327 
hists__filter_by_socket(struct hists * hists)1328 void hists__filter_by_socket(struct hists *hists)
1329 {
1330 	struct rb_node *nd;
1331 
1332 	hists->stats.nr_non_filtered_samples = 0;
1333 
1334 	hists__reset_filter_stats(hists);
1335 	hists__reset_col_len(hists);
1336 
1337 	for (nd = rb_first(&hists->entries); nd; nd = rb_next(nd)) {
1338 		struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1339 
1340 		if (hists__filter_entry_by_socket(hists, h))
1341 			continue;
1342 
1343 		hists__remove_entry_filter(hists, h, HIST_FILTER__SOCKET);
1344 	}
1345 }
1346 
events_stats__inc(struct events_stats * stats,u32 type)1347 void events_stats__inc(struct events_stats *stats, u32 type)
1348 {
1349 	++stats->nr_events[0];
1350 	++stats->nr_events[type];
1351 }
1352 
hists__inc_nr_events(struct hists * hists,u32 type)1353 void hists__inc_nr_events(struct hists *hists, u32 type)
1354 {
1355 	events_stats__inc(&hists->stats, type);
1356 }
1357 
hists__inc_nr_samples(struct hists * hists,bool filtered)1358 void hists__inc_nr_samples(struct hists *hists, bool filtered)
1359 {
1360 	events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
1361 	if (!filtered)
1362 		hists->stats.nr_non_filtered_samples++;
1363 }
1364 
hists__add_dummy_entry(struct hists * hists,struct hist_entry * pair)1365 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
1366 						 struct hist_entry *pair)
1367 {
1368 	struct rb_root *root;
1369 	struct rb_node **p;
1370 	struct rb_node *parent = NULL;
1371 	struct hist_entry *he;
1372 	int64_t cmp;
1373 
1374 	if (sort__need_collapse)
1375 		root = &hists->entries_collapsed;
1376 	else
1377 		root = hists->entries_in;
1378 
1379 	p = &root->rb_node;
1380 
1381 	while (*p != NULL) {
1382 		parent = *p;
1383 		he = rb_entry(parent, struct hist_entry, rb_node_in);
1384 
1385 		cmp = hist_entry__collapse(he, pair);
1386 
1387 		if (!cmp)
1388 			goto out;
1389 
1390 		if (cmp < 0)
1391 			p = &(*p)->rb_left;
1392 		else
1393 			p = &(*p)->rb_right;
1394 	}
1395 
1396 	he = hist_entry__new(pair, true);
1397 	if (he) {
1398 		memset(&he->stat, 0, sizeof(he->stat));
1399 		he->hists = hists;
1400 		rb_link_node(&he->rb_node_in, parent, p);
1401 		rb_insert_color(&he->rb_node_in, root);
1402 		hists__inc_stats(hists, he);
1403 		he->dummy = true;
1404 	}
1405 out:
1406 	return he;
1407 }
1408 
hists__find_entry(struct hists * hists,struct hist_entry * he)1409 static struct hist_entry *hists__find_entry(struct hists *hists,
1410 					    struct hist_entry *he)
1411 {
1412 	struct rb_node *n;
1413 
1414 	if (sort__need_collapse)
1415 		n = hists->entries_collapsed.rb_node;
1416 	else
1417 		n = hists->entries_in->rb_node;
1418 
1419 	while (n) {
1420 		struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
1421 		int64_t cmp = hist_entry__collapse(iter, he);
1422 
1423 		if (cmp < 0)
1424 			n = n->rb_left;
1425 		else if (cmp > 0)
1426 			n = n->rb_right;
1427 		else
1428 			return iter;
1429 	}
1430 
1431 	return NULL;
1432 }
1433 
1434 /*
1435  * Look for pairs to link to the leader buckets (hist_entries):
1436  */
hists__match(struct hists * leader,struct hists * other)1437 void hists__match(struct hists *leader, struct hists *other)
1438 {
1439 	struct rb_root *root;
1440 	struct rb_node *nd;
1441 	struct hist_entry *pos, *pair;
1442 
1443 	if (sort__need_collapse)
1444 		root = &leader->entries_collapsed;
1445 	else
1446 		root = leader->entries_in;
1447 
1448 	for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1449 		pos  = rb_entry(nd, struct hist_entry, rb_node_in);
1450 		pair = hists__find_entry(other, pos);
1451 
1452 		if (pair)
1453 			hist_entry__add_pair(pair, pos);
1454 	}
1455 }
1456 
1457 /*
1458  * Look for entries in the other hists that are not present in the leader, if
1459  * we find them, just add a dummy entry on the leader hists, with period=0,
1460  * nr_events=0, to serve as the list header.
1461  */
hists__link(struct hists * leader,struct hists * other)1462 int hists__link(struct hists *leader, struct hists *other)
1463 {
1464 	struct rb_root *root;
1465 	struct rb_node *nd;
1466 	struct hist_entry *pos, *pair;
1467 
1468 	if (sort__need_collapse)
1469 		root = &other->entries_collapsed;
1470 	else
1471 		root = other->entries_in;
1472 
1473 	for (nd = rb_first(root); nd; nd = rb_next(nd)) {
1474 		pos = rb_entry(nd, struct hist_entry, rb_node_in);
1475 
1476 		if (!hist_entry__has_pairs(pos)) {
1477 			pair = hists__add_dummy_entry(leader, pos);
1478 			if (pair == NULL)
1479 				return -1;
1480 			hist_entry__add_pair(pos, pair);
1481 		}
1482 	}
1483 
1484 	return 0;
1485 }
1486 
hist__account_cycles(struct branch_stack * bs,struct addr_location * al,struct perf_sample * sample,bool nonany_branch_mode)1487 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
1488 			  struct perf_sample *sample, bool nonany_branch_mode)
1489 {
1490 	struct branch_info *bi;
1491 
1492 	/* If we have branch cycles always annotate them. */
1493 	if (bs && bs->nr && bs->entries[0].flags.cycles) {
1494 		int i;
1495 
1496 		bi = sample__resolve_bstack(sample, al);
1497 		if (bi) {
1498 			struct addr_map_symbol *prev = NULL;
1499 
1500 			/*
1501 			 * Ignore errors, still want to process the
1502 			 * other entries.
1503 			 *
1504 			 * For non standard branch modes always
1505 			 * force no IPC (prev == NULL)
1506 			 *
1507 			 * Note that perf stores branches reversed from
1508 			 * program order!
1509 			 */
1510 			for (i = bs->nr - 1; i >= 0; i--) {
1511 				addr_map_symbol__account_cycles(&bi[i].from,
1512 					nonany_branch_mode ? NULL : prev,
1513 					bi[i].flags.cycles);
1514 				prev = &bi[i].to;
1515 			}
1516 			free(bi);
1517 		}
1518 	}
1519 }
1520 
perf_evlist__fprintf_nr_events(struct perf_evlist * evlist,FILE * fp)1521 size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
1522 {
1523 	struct perf_evsel *pos;
1524 	size_t ret = 0;
1525 
1526 	evlist__for_each(evlist, pos) {
1527 		ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
1528 		ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
1529 	}
1530 
1531 	return ret;
1532 }
1533 
1534 
hists__total_period(struct hists * hists)1535 u64 hists__total_period(struct hists *hists)
1536 {
1537 	return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
1538 		hists->stats.total_period;
1539 }
1540 
parse_filter_percentage(const struct option * opt __maybe_unused,const char * arg,int unset __maybe_unused)1541 int parse_filter_percentage(const struct option *opt __maybe_unused,
1542 			    const char *arg, int unset __maybe_unused)
1543 {
1544 	if (!strcmp(arg, "relative"))
1545 		symbol_conf.filter_relative = true;
1546 	else if (!strcmp(arg, "absolute"))
1547 		symbol_conf.filter_relative = false;
1548 	else
1549 		return -1;
1550 
1551 	return 0;
1552 }
1553 
perf_hist_config(const char * var,const char * value)1554 int perf_hist_config(const char *var, const char *value)
1555 {
1556 	if (!strcmp(var, "hist.percentage"))
1557 		return parse_filter_percentage(NULL, value, 0);
1558 
1559 	return 0;
1560 }
1561 
hists_evsel__init(struct perf_evsel * evsel)1562 static int hists_evsel__init(struct perf_evsel *evsel)
1563 {
1564 	struct hists *hists = evsel__hists(evsel);
1565 
1566 	memset(hists, 0, sizeof(*hists));
1567 	hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
1568 	hists->entries_in = &hists->entries_in_array[0];
1569 	hists->entries_collapsed = RB_ROOT;
1570 	hists->entries = RB_ROOT;
1571 	pthread_mutex_init(&hists->lock, NULL);
1572 	hists->socket_filter = -1;
1573 	return 0;
1574 }
1575 
1576 /*
1577  * XXX We probably need a hists_evsel__exit() to free the hist_entries
1578  * stored in the rbtree...
1579  */
1580 
hists__init(void)1581 int hists__init(void)
1582 {
1583 	int err = perf_evsel__object_config(sizeof(struct hists_evsel),
1584 					    hists_evsel__init, NULL);
1585 	if (err)
1586 		fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
1587 
1588 	return err;
1589 }
1590