1 // SPDX-License-Identifier: GPL-2.0
8 #include "namespaces.h"
17 #include "ui/progress.h"
21 #include <sys/param.h>
23 static bool hists__filter_entry_by_dso(struct hists *hists,
24 struct hist_entry *he);
25 static bool hists__filter_entry_by_thread(struct hists *hists,
26 struct hist_entry *he);
27 static bool hists__filter_entry_by_symbol(struct hists *hists,
28 struct hist_entry *he);
29 static bool hists__filter_entry_by_socket(struct hists *hists,
30 struct hist_entry *he);
32 u16 hists__col_len(struct hists *hists, enum hist_column col)
34 return hists->col_len[col];
37 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
39 hists->col_len[col] = len;
42 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
44 if (len > hists__col_len(hists, col)) {
45 hists__set_col_len(hists, col, len);
51 void hists__reset_col_len(struct hists *hists)
55 for (col = 0; col < HISTC_NR_COLS; ++col)
56 hists__set_col_len(hists, col, 0);
59 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
61 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
63 if (hists__col_len(hists, dso) < unresolved_col_width &&
64 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
65 !symbol_conf.dso_list)
66 hists__set_col_len(hists, dso, unresolved_col_width);
69 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
71 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
76 * +4 accounts for '[x] ' priv level info
77 * +2 accounts for 0x prefix on raw addresses
78 * +3 accounts for ' y ' symtab origin info
81 symlen = h->ms.sym->namelen + 4;
83 symlen += BITS_PER_LONG / 4 + 2 + 3;
84 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
86 symlen = unresolved_col_width + 4 + 2;
87 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
88 hists__set_unres_dso_col_len(hists, HISTC_DSO);
91 len = thread__comm_len(h->thread);
92 if (hists__new_col_len(hists, HISTC_COMM, len))
93 hists__set_col_len(hists, HISTC_THREAD, len + 8);
96 len = dso__name_len(h->ms.map->dso);
97 hists__new_col_len(hists, HISTC_DSO, len);
101 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
103 if (h->branch_info) {
104 if (h->branch_info->from.sym) {
105 symlen = (int)h->branch_info->from.sym->namelen + 4;
107 symlen += BITS_PER_LONG / 4 + 2 + 3;
108 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
110 symlen = dso__name_len(h->branch_info->from.map->dso);
111 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
113 symlen = unresolved_col_width + 4 + 2;
114 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
115 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
118 if (h->branch_info->to.sym) {
119 symlen = (int)h->branch_info->to.sym->namelen + 4;
121 symlen += BITS_PER_LONG / 4 + 2 + 3;
122 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
124 symlen = dso__name_len(h->branch_info->to.map->dso);
125 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
127 symlen = unresolved_col_width + 4 + 2;
128 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
129 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
132 if (h->branch_info->srcline_from)
133 hists__new_col_len(hists, HISTC_SRCLINE_FROM,
134 strlen(h->branch_info->srcline_from));
135 if (h->branch_info->srcline_to)
136 hists__new_col_len(hists, HISTC_SRCLINE_TO,
137 strlen(h->branch_info->srcline_to));
141 if (h->mem_info->daddr.sym) {
142 symlen = (int)h->mem_info->daddr.sym->namelen + 4
143 + unresolved_col_width + 2;
144 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
146 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
149 symlen = unresolved_col_width + 4 + 2;
150 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
152 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
156 if (h->mem_info->iaddr.sym) {
157 symlen = (int)h->mem_info->iaddr.sym->namelen + 4
158 + unresolved_col_width + 2;
159 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
162 symlen = unresolved_col_width + 4 + 2;
163 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
167 if (h->mem_info->daddr.map) {
168 symlen = dso__name_len(h->mem_info->daddr.map->dso);
169 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
172 symlen = unresolved_col_width + 4 + 2;
173 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
176 hists__new_col_len(hists, HISTC_MEM_PHYS_DADDR,
177 unresolved_col_width + 4 + 2);
180 symlen = unresolved_col_width + 4 + 2;
181 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
182 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
183 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
186 hists__new_col_len(hists, HISTC_CGROUP_ID, 20);
187 hists__new_col_len(hists, HISTC_CPU, 3);
188 hists__new_col_len(hists, HISTC_SOCKET, 6);
189 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
190 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
191 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
192 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
193 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
194 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
197 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
198 hists__new_col_len(hists, HISTC_SRCLINE, len);
202 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
205 hists__new_col_len(hists, HISTC_TRANSACTION,
206 hist_entry__transaction_len());
209 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
212 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
214 struct rb_node *next = rb_first_cached(&hists->entries);
215 struct hist_entry *n;
218 hists__reset_col_len(hists);
220 while (next && row++ < max_rows) {
221 n = rb_entry(next, struct hist_entry, rb_node);
223 hists__calc_col_len(hists, n);
224 next = rb_next(&n->rb_node);
228 static void he_stat__add_cpumode_period(struct he_stat *he_stat,
229 unsigned int cpumode, u64 period)
232 case PERF_RECORD_MISC_KERNEL:
233 he_stat->period_sys += period;
235 case PERF_RECORD_MISC_USER:
236 he_stat->period_us += period;
238 case PERF_RECORD_MISC_GUEST_KERNEL:
239 he_stat->period_guest_sys += period;
241 case PERF_RECORD_MISC_GUEST_USER:
242 he_stat->period_guest_us += period;
249 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
253 he_stat->period += period;
254 he_stat->weight += weight;
255 he_stat->nr_events += 1;
258 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
260 dest->period += src->period;
261 dest->period_sys += src->period_sys;
262 dest->period_us += src->period_us;
263 dest->period_guest_sys += src->period_guest_sys;
264 dest->period_guest_us += src->period_guest_us;
265 dest->nr_events += src->nr_events;
266 dest->weight += src->weight;
269 static void he_stat__decay(struct he_stat *he_stat)
271 he_stat->period = (he_stat->period * 7) / 8;
272 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
273 /* XXX need decay for weight too? */
276 static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
278 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
280 u64 prev_period = he->stat.period;
283 if (prev_period == 0)
286 he_stat__decay(&he->stat);
287 if (symbol_conf.cumulate_callchain)
288 he_stat__decay(he->stat_acc);
289 decay_callchain(he->callchain);
291 diff = prev_period - he->stat.period;
294 hists->stats.total_period -= diff;
296 hists->stats.total_non_filtered_period -= diff;
300 struct hist_entry *child;
301 struct rb_node *node = rb_first_cached(&he->hroot_out);
303 child = rb_entry(node, struct hist_entry, rb_node);
304 node = rb_next(node);
306 if (hists__decay_entry(hists, child))
307 hists__delete_entry(hists, child);
311 return he->stat.period == 0;
314 static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
316 struct rb_root_cached *root_in;
317 struct rb_root_cached *root_out;
320 root_in = &he->parent_he->hroot_in;
321 root_out = &he->parent_he->hroot_out;
323 if (hists__has(hists, need_collapse))
324 root_in = &hists->entries_collapsed;
326 root_in = hists->entries_in;
327 root_out = &hists->entries;
330 rb_erase_cached(&he->rb_node_in, root_in);
331 rb_erase_cached(&he->rb_node, root_out);
335 --hists->nr_non_filtered_entries;
337 hist_entry__delete(he);
340 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
342 struct rb_node *next = rb_first_cached(&hists->entries);
343 struct hist_entry *n;
346 n = rb_entry(next, struct hist_entry, rb_node);
347 next = rb_next(&n->rb_node);
348 if (((zap_user && n->level == '.') ||
349 (zap_kernel && n->level != '.') ||
350 hists__decay_entry(hists, n))) {
351 hists__delete_entry(hists, n);
356 void hists__delete_entries(struct hists *hists)
358 struct rb_node *next = rb_first_cached(&hists->entries);
359 struct hist_entry *n;
362 n = rb_entry(next, struct hist_entry, rb_node);
363 next = rb_next(&n->rb_node);
365 hists__delete_entry(hists, n);
370 * histogram, sorted on item, collects periods
373 static int hist_entry__init(struct hist_entry *he,
374 struct hist_entry *template,
376 size_t callchain_size)
379 he->callchain_size = callchain_size;
381 if (symbol_conf.cumulate_callchain) {
382 he->stat_acc = malloc(sizeof(he->stat));
383 if (he->stat_acc == NULL)
385 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
387 memset(&he->stat, 0, sizeof(he->stat));
390 map__get(he->ms.map);
392 if (he->branch_info) {
394 * This branch info is (a part of) allocated from
395 * sample__resolve_bstack() and will be freed after
396 * adding new entries. So we need to save a copy.
398 he->branch_info = malloc(sizeof(*he->branch_info));
399 if (he->branch_info == NULL)
402 memcpy(he->branch_info, template->branch_info,
403 sizeof(*he->branch_info));
405 map__get(he->branch_info->from.map);
406 map__get(he->branch_info->to.map);
410 map__get(he->mem_info->iaddr.map);
411 map__get(he->mem_info->daddr.map);
414 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
415 callchain_init(he->callchain);
418 he->raw_data = memdup(he->raw_data, he->raw_size);
419 if (he->raw_data == NULL)
422 INIT_LIST_HEAD(&he->pairs.node);
423 thread__get(he->thread);
424 he->hroot_in = RB_ROOT_CACHED;
425 he->hroot_out = RB_ROOT_CACHED;
427 if (!symbol_conf.report_hierarchy)
433 if (he->branch_info) {
434 map__put(he->branch_info->from.map);
435 map__put(he->branch_info->to.map);
436 free(he->branch_info);
439 map__put(he->mem_info->iaddr.map);
440 map__put(he->mem_info->daddr.map);
443 map__zput(he->ms.map);
448 static void *hist_entry__zalloc(size_t size)
450 return zalloc(size + sizeof(struct hist_entry));
453 static void hist_entry__free(void *ptr)
458 static struct hist_entry_ops default_ops = {
459 .new = hist_entry__zalloc,
460 .free = hist_entry__free,
463 static struct hist_entry *hist_entry__new(struct hist_entry *template,
466 struct hist_entry_ops *ops = template->ops;
467 size_t callchain_size = 0;
468 struct hist_entry *he;
472 ops = template->ops = &default_ops;
474 if (symbol_conf.use_callchain)
475 callchain_size = sizeof(struct callchain_root);
477 he = ops->new(callchain_size);
479 err = hist_entry__init(he, template, sample_self, callchain_size);
489 static u8 symbol__parent_filter(const struct symbol *parent)
491 if (symbol_conf.exclude_other && parent == NULL)
492 return 1 << HIST_FILTER__PARENT;
496 static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
498 if (!hist_entry__has_callchains(he) || !symbol_conf.use_callchain)
501 he->hists->callchain_period += period;
503 he->hists->callchain_non_filtered_period += period;
506 static struct hist_entry *hists__findnew_entry(struct hists *hists,
507 struct hist_entry *entry,
508 struct addr_location *al,
512 struct rb_node *parent = NULL;
513 struct hist_entry *he;
515 u64 period = entry->stat.period;
516 u64 weight = entry->stat.weight;
517 bool leftmost = true;
519 p = &hists->entries_in->rb_root.rb_node;
523 he = rb_entry(parent, struct hist_entry, rb_node_in);
526 * Make sure that it receives arguments in a same order as
527 * hist_entry__collapse() so that we can use an appropriate
528 * function when searching an entry regardless which sort
531 cmp = hist_entry__cmp(he, entry);
535 he_stat__add_period(&he->stat, period, weight);
536 hist_entry__add_callchain_period(he, period);
538 if (symbol_conf.cumulate_callchain)
539 he_stat__add_period(he->stat_acc, period, weight);
542 * This mem info was allocated from sample__resolve_mem
543 * and will not be used anymore.
545 mem_info__zput(entry->mem_info);
547 /* If the map of an existing hist_entry has
548 * become out-of-date due to an exec() or
549 * similar, update it. Otherwise we will
550 * mis-adjust symbol addresses when computing
551 * the history counter to increment.
553 if (he->ms.map != entry->ms.map) {
554 map__put(he->ms.map);
555 he->ms.map = map__get(entry->ms.map);
568 he = hist_entry__new(entry, sample_self);
573 hist_entry__add_callchain_period(he, period);
576 rb_link_node(&he->rb_node_in, parent, p);
577 rb_insert_color_cached(&he->rb_node_in, hists->entries_in, leftmost);
580 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
581 if (symbol_conf.cumulate_callchain)
582 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
586 static struct hist_entry*
587 __hists__add_entry(struct hists *hists,
588 struct addr_location *al,
589 struct symbol *sym_parent,
590 struct branch_info *bi,
592 struct perf_sample *sample,
594 struct hist_entry_ops *ops)
596 struct namespaces *ns = thread__namespaces(al->thread);
597 struct hist_entry entry = {
598 .thread = al->thread,
599 .comm = thread__comm(al->thread),
601 .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0,
602 .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0,
608 .srcline = al->srcline ? strdup(al->srcline) : NULL,
609 .socket = al->socket,
611 .cpumode = al->cpumode,
616 .period = sample->period,
617 .weight = sample->weight,
619 .parent = sym_parent,
620 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
624 .transaction = sample->transaction,
625 .raw_data = sample->raw_data,
626 .raw_size = sample->raw_size,
628 }, *he = hists__findnew_entry(hists, &entry, al, sample_self);
630 if (!hists->has_callchains && he && he->callchain_size != 0)
631 hists->has_callchains = true;
635 struct hist_entry *hists__add_entry(struct hists *hists,
636 struct addr_location *al,
637 struct symbol *sym_parent,
638 struct branch_info *bi,
640 struct perf_sample *sample,
643 return __hists__add_entry(hists, al, sym_parent, bi, mi,
644 sample, sample_self, NULL);
647 struct hist_entry *hists__add_entry_ops(struct hists *hists,
648 struct hist_entry_ops *ops,
649 struct addr_location *al,
650 struct symbol *sym_parent,
651 struct branch_info *bi,
653 struct perf_sample *sample,
656 return __hists__add_entry(hists, al, sym_parent, bi, mi,
657 sample, sample_self, ops);
661 iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
662 struct addr_location *al __maybe_unused)
668 iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
669 struct addr_location *al __maybe_unused)
675 iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
677 struct perf_sample *sample = iter->sample;
680 mi = sample__resolve_mem(sample, al);
689 iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
692 struct mem_info *mi = iter->priv;
693 struct hists *hists = evsel__hists(iter->evsel);
694 struct perf_sample *sample = iter->sample;
695 struct hist_entry *he;
700 cost = sample->weight;
705 * must pass period=weight in order to get the correct
706 * sorting from hists__collapse_resort() which is solely
707 * based on periods. We want sorting be done on nr_events * weight
708 * and this is indirectly achieved by passing period=weight here
709 * and the he_stat__add_period() function.
711 sample->period = cost;
713 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
723 iter_finish_mem_entry(struct hist_entry_iter *iter,
724 struct addr_location *al __maybe_unused)
726 struct perf_evsel *evsel = iter->evsel;
727 struct hists *hists = evsel__hists(evsel);
728 struct hist_entry *he = iter->he;
734 hists__inc_nr_samples(hists, he->filtered);
736 err = hist_entry__append_callchain(he, iter->sample);
740 * We don't need to free iter->priv (mem_info) here since the mem info
741 * was either already freed in hists__findnew_entry() or passed to a
742 * new hist entry by hist_entry__new().
751 iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
753 struct branch_info *bi;
754 struct perf_sample *sample = iter->sample;
756 bi = sample__resolve_bstack(sample, al);
761 iter->total = sample->branch_stack->nr;
768 iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
769 struct addr_location *al __maybe_unused)
775 iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
777 struct branch_info *bi = iter->priv;
783 if (iter->curr >= iter->total)
786 al->map = bi[i].to.map;
787 al->sym = bi[i].to.sym;
788 al->addr = bi[i].to.addr;
793 iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
795 struct branch_info *bi;
796 struct perf_evsel *evsel = iter->evsel;
797 struct hists *hists = evsel__hists(evsel);
798 struct perf_sample *sample = iter->sample;
799 struct hist_entry *he = NULL;
805 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
809 * The report shows the percentage of total branches captured
810 * and not events sampled. Thus we use a pseudo period of 1.
813 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
815 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
820 hists__inc_nr_samples(hists, he->filtered);
829 iter_finish_branch_entry(struct hist_entry_iter *iter,
830 struct addr_location *al __maybe_unused)
835 return iter->curr >= iter->total ? 0 : -1;
839 iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
840 struct addr_location *al __maybe_unused)
846 iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
848 struct perf_evsel *evsel = iter->evsel;
849 struct perf_sample *sample = iter->sample;
850 struct hist_entry *he;
852 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
862 iter_finish_normal_entry(struct hist_entry_iter *iter,
863 struct addr_location *al __maybe_unused)
865 struct hist_entry *he = iter->he;
866 struct perf_evsel *evsel = iter->evsel;
867 struct perf_sample *sample = iter->sample;
874 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
876 return hist_entry__append_callchain(he, sample);
880 iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
881 struct addr_location *al __maybe_unused)
883 struct hist_entry **he_cache;
885 callchain_cursor_commit(&callchain_cursor);
888 * This is for detecting cycles or recursions so that they're
889 * cumulated only one time to prevent entries more than 100%
892 he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1));
893 if (he_cache == NULL)
896 iter->priv = he_cache;
903 iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
904 struct addr_location *al)
906 struct perf_evsel *evsel = iter->evsel;
907 struct hists *hists = evsel__hists(evsel);
908 struct perf_sample *sample = iter->sample;
909 struct hist_entry **he_cache = iter->priv;
910 struct hist_entry *he;
913 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
919 he_cache[iter->curr++] = he;
921 hist_entry__append_callchain(he, sample);
924 * We need to re-initialize the cursor since callchain_append()
925 * advanced the cursor to the end.
927 callchain_cursor_commit(&callchain_cursor);
929 hists__inc_nr_samples(hists, he->filtered);
935 iter_next_cumulative_entry(struct hist_entry_iter *iter,
936 struct addr_location *al)
938 struct callchain_cursor_node *node;
940 node = callchain_cursor_current(&callchain_cursor);
944 return fill_callchain_info(al, node, iter->hide_unresolved);
948 iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
949 struct addr_location *al)
951 struct perf_evsel *evsel = iter->evsel;
952 struct perf_sample *sample = iter->sample;
953 struct hist_entry **he_cache = iter->priv;
954 struct hist_entry *he;
955 struct hist_entry he_tmp = {
956 .hists = evsel__hists(evsel),
958 .thread = al->thread,
959 .comm = thread__comm(al->thread),
965 .srcline = al->srcline ? strdup(al->srcline) : NULL,
966 .parent = iter->parent,
967 .raw_data = sample->raw_data,
968 .raw_size = sample->raw_size,
971 struct callchain_cursor cursor;
973 callchain_cursor_snapshot(&cursor, &callchain_cursor);
975 callchain_cursor_advance(&callchain_cursor);
978 * Check if there's duplicate entries in the callchain.
979 * It's possible that it has cycles or recursive calls.
981 for (i = 0; i < iter->curr; i++) {
982 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
983 /* to avoid calling callback function */
989 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
995 he_cache[iter->curr++] = he;
997 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
998 callchain_append(he->callchain, &cursor, sample->period);
1003 iter_finish_cumulative_entry(struct hist_entry_iter *iter,
1004 struct addr_location *al __maybe_unused)
1012 const struct hist_iter_ops hist_iter_mem = {
1013 .prepare_entry = iter_prepare_mem_entry,
1014 .add_single_entry = iter_add_single_mem_entry,
1015 .next_entry = iter_next_nop_entry,
1016 .add_next_entry = iter_add_next_nop_entry,
1017 .finish_entry = iter_finish_mem_entry,
1020 const struct hist_iter_ops hist_iter_branch = {
1021 .prepare_entry = iter_prepare_branch_entry,
1022 .add_single_entry = iter_add_single_branch_entry,
1023 .next_entry = iter_next_branch_entry,
1024 .add_next_entry = iter_add_next_branch_entry,
1025 .finish_entry = iter_finish_branch_entry,
1028 const struct hist_iter_ops hist_iter_normal = {
1029 .prepare_entry = iter_prepare_normal_entry,
1030 .add_single_entry = iter_add_single_normal_entry,
1031 .next_entry = iter_next_nop_entry,
1032 .add_next_entry = iter_add_next_nop_entry,
1033 .finish_entry = iter_finish_normal_entry,
1036 const struct hist_iter_ops hist_iter_cumulative = {
1037 .prepare_entry = iter_prepare_cumulative_entry,
1038 .add_single_entry = iter_add_single_cumulative_entry,
1039 .next_entry = iter_next_cumulative_entry,
1040 .add_next_entry = iter_add_next_cumulative_entry,
1041 .finish_entry = iter_finish_cumulative_entry,
1044 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
1045 int max_stack_depth, void *arg)
1048 struct map *alm = NULL;
1051 alm = map__get(al->map);
1053 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
1054 iter->evsel, al, max_stack_depth);
1058 err = iter->ops->prepare_entry(iter, al);
1062 err = iter->ops->add_single_entry(iter, al);
1066 if (iter->he && iter->add_entry_cb) {
1067 err = iter->add_entry_cb(iter, al, true, arg);
1072 while (iter->ops->next_entry(iter, al)) {
1073 err = iter->ops->add_next_entry(iter, al);
1077 if (iter->he && iter->add_entry_cb) {
1078 err = iter->add_entry_cb(iter, al, false, arg);
1085 err2 = iter->ops->finish_entry(iter, al);
1095 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1097 struct hists *hists = left->hists;
1098 struct perf_hpp_fmt *fmt;
1101 hists__for_each_sort_list(hists, fmt) {
1102 if (perf_hpp__is_dynamic_entry(fmt) &&
1103 !perf_hpp__defined_dynamic_entry(fmt, hists))
1106 cmp = fmt->cmp(fmt, left, right);
1115 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1117 struct hists *hists = left->hists;
1118 struct perf_hpp_fmt *fmt;
1121 hists__for_each_sort_list(hists, fmt) {
1122 if (perf_hpp__is_dynamic_entry(fmt) &&
1123 !perf_hpp__defined_dynamic_entry(fmt, hists))
1126 cmp = fmt->collapse(fmt, left, right);
1134 void hist_entry__delete(struct hist_entry *he)
1136 struct hist_entry_ops *ops = he->ops;
1138 thread__zput(he->thread);
1139 map__zput(he->ms.map);
1141 if (he->branch_info) {
1142 map__zput(he->branch_info->from.map);
1143 map__zput(he->branch_info->to.map);
1144 free_srcline(he->branch_info->srcline_from);
1145 free_srcline(he->branch_info->srcline_to);
1146 zfree(&he->branch_info);
1150 map__zput(he->mem_info->iaddr.map);
1151 map__zput(he->mem_info->daddr.map);
1152 mem_info__zput(he->mem_info);
1155 zfree(&he->stat_acc);
1156 free_srcline(he->srcline);
1157 if (he->srcfile && he->srcfile[0])
1159 free_callchain(he->callchain);
1160 free(he->trace_output);
1166 * If this is not the last column, then we need to pad it according to the
1167 * pre-calculated max length for this column, otherwise don't bother adding
1168 * spaces because that would break viewing this with, for instance, 'less',
1169 * that would show tons of trailing spaces when a long C++ demangled method
1172 int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1173 struct perf_hpp_fmt *fmt, int printed)
1175 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
1176 const int width = fmt->width(fmt, hpp, he->hists);
1177 if (printed < width) {
1178 advance_hpp(hpp, printed);
1179 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1187 * collapse the histogram
1190 static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
1191 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1192 enum hist_filter type);
1194 typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1196 static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1198 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1201 static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1202 enum hist_filter type,
1205 struct perf_hpp_fmt *fmt;
1206 bool type_match = false;
1207 struct hist_entry *parent = he->parent_he;
1210 case HIST_FILTER__THREAD:
1211 if (symbol_conf.comm_list == NULL &&
1212 symbol_conf.pid_list == NULL &&
1213 symbol_conf.tid_list == NULL)
1216 case HIST_FILTER__DSO:
1217 if (symbol_conf.dso_list == NULL)
1220 case HIST_FILTER__SYMBOL:
1221 if (symbol_conf.sym_list == NULL)
1224 case HIST_FILTER__PARENT:
1225 case HIST_FILTER__GUEST:
1226 case HIST_FILTER__HOST:
1227 case HIST_FILTER__SOCKET:
1228 case HIST_FILTER__C2C:
1233 /* if it's filtered by own fmt, it has to have filter bits */
1234 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1243 * If the filter is for current level entry, propagate
1244 * filter marker to parents. The marker bit was
1245 * already set by default so it only needs to clear
1246 * non-filtered entries.
1248 if (!(he->filtered & (1 << type))) {
1250 parent->filtered &= ~(1 << type);
1251 parent = parent->parent_he;
1256 * If current entry doesn't have matching formats, set
1257 * filter marker for upper level entries. it will be
1258 * cleared if its lower level entries is not filtered.
1260 * For lower-level entries, it inherits parent's
1261 * filter bit so that lower level entries of a
1262 * non-filtered entry won't set the filter marker.
1265 he->filtered |= (1 << type);
1267 he->filtered |= (parent->filtered & (1 << type));
1271 static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1273 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1274 check_thread_entry);
1276 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1277 perf_hpp__is_dso_entry);
1279 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1280 perf_hpp__is_sym_entry);
1282 hists__apply_filters(he->hists, he);
1285 static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1286 struct rb_root_cached *root,
1287 struct hist_entry *he,
1288 struct hist_entry *parent_he,
1289 struct perf_hpp_list *hpp_list)
1291 struct rb_node **p = &root->rb_root.rb_node;
1292 struct rb_node *parent = NULL;
1293 struct hist_entry *iter, *new;
1294 struct perf_hpp_fmt *fmt;
1296 bool leftmost = true;
1298 while (*p != NULL) {
1300 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1303 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1304 cmp = fmt->collapse(fmt, iter, he);
1310 he_stat__add_stat(&iter->stat, &he->stat);
1315 p = &parent->rb_left;
1317 p = &parent->rb_right;
1322 new = hist_entry__new(he, true);
1326 hists->nr_entries++;
1328 /* save related format list for output */
1329 new->hpp_list = hpp_list;
1330 new->parent_he = parent_he;
1332 hist_entry__apply_hierarchy_filters(new);
1334 /* some fields are now passed to 'new' */
1335 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1336 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1337 he->trace_output = NULL;
1339 new->trace_output = NULL;
1341 if (perf_hpp__is_srcline_entry(fmt))
1344 new->srcline = NULL;
1346 if (perf_hpp__is_srcfile_entry(fmt))
1349 new->srcfile = NULL;
1352 rb_link_node(&new->rb_node_in, parent, p);
1353 rb_insert_color_cached(&new->rb_node_in, root, leftmost);
1357 static int hists__hierarchy_insert_entry(struct hists *hists,
1358 struct rb_root_cached *root,
1359 struct hist_entry *he)
1361 struct perf_hpp_list_node *node;
1362 struct hist_entry *new_he = NULL;
1363 struct hist_entry *parent = NULL;
1367 list_for_each_entry(node, &hists->hpp_formats, list) {
1368 /* skip period (overhead) and elided columns */
1369 if (node->level == 0 || node->skip)
1372 /* insert copy of 'he' for each fmt into the hierarchy */
1373 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
1374 if (new_he == NULL) {
1379 root = &new_he->hroot_in;
1380 new_he->depth = depth++;
1385 new_he->leaf = true;
1387 if (hist_entry__has_callchains(new_he) &&
1388 symbol_conf.use_callchain) {
1389 callchain_cursor_reset(&callchain_cursor);
1390 if (callchain_merge(&callchain_cursor,
1397 /* 'he' is no longer used */
1398 hist_entry__delete(he);
1400 /* return 0 (or -1) since it already applied filters */
1404 static int hists__collapse_insert_entry(struct hists *hists,
1405 struct rb_root_cached *root,
1406 struct hist_entry *he)
1408 struct rb_node **p = &root->rb_root.rb_node;
1409 struct rb_node *parent = NULL;
1410 struct hist_entry *iter;
1412 bool leftmost = true;
1414 if (symbol_conf.report_hierarchy)
1415 return hists__hierarchy_insert_entry(hists, root, he);
1417 while (*p != NULL) {
1419 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1421 cmp = hist_entry__collapse(iter, he);
1426 he_stat__add_stat(&iter->stat, &he->stat);
1427 if (symbol_conf.cumulate_callchain)
1428 he_stat__add_stat(iter->stat_acc, he->stat_acc);
1430 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
1431 callchain_cursor_reset(&callchain_cursor);
1432 if (callchain_merge(&callchain_cursor,
1437 hist_entry__delete(he);
1444 p = &(*p)->rb_right;
1448 hists->nr_entries++;
1450 rb_link_node(&he->rb_node_in, parent, p);
1451 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
1455 struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists)
1457 struct rb_root_cached *root;
1459 pthread_mutex_lock(&hists->lock);
1461 root = hists->entries_in;
1462 if (++hists->entries_in > &hists->entries_in_array[1])
1463 hists->entries_in = &hists->entries_in_array[0];
1465 pthread_mutex_unlock(&hists->lock);
1470 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1472 hists__filter_entry_by_dso(hists, he);
1473 hists__filter_entry_by_thread(hists, he);
1474 hists__filter_entry_by_symbol(hists, he);
1475 hists__filter_entry_by_socket(hists, he);
1478 int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
1480 struct rb_root_cached *root;
1481 struct rb_node *next;
1482 struct hist_entry *n;
1485 if (!hists__has(hists, need_collapse))
1488 hists->nr_entries = 0;
1490 root = hists__get_rotate_entries_in(hists);
1492 next = rb_first_cached(root);
1497 n = rb_entry(next, struct hist_entry, rb_node_in);
1498 next = rb_next(&n->rb_node_in);
1500 rb_erase_cached(&n->rb_node_in, root);
1501 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1507 * If it wasn't combined with one of the entries already
1508 * collapsed, we need to apply the filters that may have
1509 * been set by, say, the hist_browser.
1511 hists__apply_filters(hists, n);
1514 ui_progress__update(prog, 1);
1519 static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
1521 struct hists *hists = a->hists;
1522 struct perf_hpp_fmt *fmt;
1525 hists__for_each_sort_list(hists, fmt) {
1526 if (perf_hpp__should_skip(fmt, a->hists))
1529 cmp = fmt->sort(fmt, a, b);
1537 static void hists__reset_filter_stats(struct hists *hists)
1539 hists->nr_non_filtered_entries = 0;
1540 hists->stats.total_non_filtered_period = 0;
1543 void hists__reset_stats(struct hists *hists)
1545 hists->nr_entries = 0;
1546 hists->stats.total_period = 0;
1548 hists__reset_filter_stats(hists);
1551 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1553 hists->nr_non_filtered_entries++;
1554 hists->stats.total_non_filtered_period += h->stat.period;
1557 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1560 hists__inc_filter_stats(hists, h);
1562 hists->nr_entries++;
1563 hists->stats.total_period += h->stat.period;
1566 static void hierarchy_recalc_total_periods(struct hists *hists)
1568 struct rb_node *node;
1569 struct hist_entry *he;
1571 node = rb_first_cached(&hists->entries);
1573 hists->stats.total_period = 0;
1574 hists->stats.total_non_filtered_period = 0;
1577 * recalculate total period using top-level entries only
1578 * since lower level entries only see non-filtered entries
1579 * but upper level entries have sum of both entries.
1582 he = rb_entry(node, struct hist_entry, rb_node);
1583 node = rb_next(node);
1585 hists->stats.total_period += he->stat.period;
1587 hists->stats.total_non_filtered_period += he->stat.period;
1591 static void hierarchy_insert_output_entry(struct rb_root_cached *root,
1592 struct hist_entry *he)
1594 struct rb_node **p = &root->rb_root.rb_node;
1595 struct rb_node *parent = NULL;
1596 struct hist_entry *iter;
1597 struct perf_hpp_fmt *fmt;
1598 bool leftmost = true;
1600 while (*p != NULL) {
1602 iter = rb_entry(parent, struct hist_entry, rb_node);
1604 if (hist_entry__sort(he, iter) > 0)
1605 p = &parent->rb_left;
1607 p = &parent->rb_right;
1612 rb_link_node(&he->rb_node, parent, p);
1613 rb_insert_color_cached(&he->rb_node, root, leftmost);
1615 /* update column width of dynamic entry */
1616 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1617 if (perf_hpp__is_dynamic_entry(fmt))
1618 fmt->sort(fmt, he, NULL);
1622 static void hists__hierarchy_output_resort(struct hists *hists,
1623 struct ui_progress *prog,
1624 struct rb_root_cached *root_in,
1625 struct rb_root_cached *root_out,
1626 u64 min_callchain_hits,
1629 struct rb_node *node;
1630 struct hist_entry *he;
1632 *root_out = RB_ROOT_CACHED;
1633 node = rb_first_cached(root_in);
1636 he = rb_entry(node, struct hist_entry, rb_node_in);
1637 node = rb_next(node);
1639 hierarchy_insert_output_entry(root_out, he);
1642 ui_progress__update(prog, 1);
1644 hists->nr_entries++;
1645 if (!he->filtered) {
1646 hists->nr_non_filtered_entries++;
1647 hists__calc_col_len(hists, he);
1651 hists__hierarchy_output_resort(hists, prog,
1662 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1663 u64 total = he->stat.period;
1665 if (symbol_conf.cumulate_callchain)
1666 total = he->stat_acc->period;
1668 min_callchain_hits = total * (callchain_param.min_percent / 100);
1671 callchain_param.sort(&he->sorted_chain, he->callchain,
1672 min_callchain_hits, &callchain_param);
1676 static void __hists__insert_output_entry(struct rb_root_cached *entries,
1677 struct hist_entry *he,
1678 u64 min_callchain_hits,
1681 struct rb_node **p = &entries->rb_root.rb_node;
1682 struct rb_node *parent = NULL;
1683 struct hist_entry *iter;
1684 struct perf_hpp_fmt *fmt;
1685 bool leftmost = true;
1687 if (use_callchain) {
1688 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1689 u64 total = he->stat.period;
1691 if (symbol_conf.cumulate_callchain)
1692 total = he->stat_acc->period;
1694 min_callchain_hits = total * (callchain_param.min_percent / 100);
1696 callchain_param.sort(&he->sorted_chain, he->callchain,
1697 min_callchain_hits, &callchain_param);
1700 while (*p != NULL) {
1702 iter = rb_entry(parent, struct hist_entry, rb_node);
1704 if (hist_entry__sort(he, iter) > 0)
1707 p = &(*p)->rb_right;
1712 rb_link_node(&he->rb_node, parent, p);
1713 rb_insert_color_cached(&he->rb_node, entries, leftmost);
1715 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1716 if (perf_hpp__is_dynamic_entry(fmt) &&
1717 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1718 fmt->sort(fmt, he, NULL); /* update column width */
1722 static void output_resort(struct hists *hists, struct ui_progress *prog,
1723 bool use_callchain, hists__resort_cb_t cb,
1726 struct rb_root_cached *root;
1727 struct rb_node *next;
1728 struct hist_entry *n;
1729 u64 callchain_total;
1730 u64 min_callchain_hits;
1732 callchain_total = hists->callchain_period;
1733 if (symbol_conf.filter_relative)
1734 callchain_total = hists->callchain_non_filtered_period;
1736 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
1738 hists__reset_stats(hists);
1739 hists__reset_col_len(hists);
1741 if (symbol_conf.report_hierarchy) {
1742 hists__hierarchy_output_resort(hists, prog,
1743 &hists->entries_collapsed,
1747 hierarchy_recalc_total_periods(hists);
1751 if (hists__has(hists, need_collapse))
1752 root = &hists->entries_collapsed;
1754 root = hists->entries_in;
1756 next = rb_first_cached(root);
1757 hists->entries = RB_ROOT_CACHED;
1760 n = rb_entry(next, struct hist_entry, rb_node_in);
1761 next = rb_next(&n->rb_node_in);
1763 if (cb && cb(n, cb_arg))
1766 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
1767 hists__inc_stats(hists, n);
1770 hists__calc_col_len(hists, n);
1773 ui_progress__update(prog, 1);
1777 void perf_evsel__output_resort_cb(struct perf_evsel *evsel, struct ui_progress *prog,
1778 hists__resort_cb_t cb, void *cb_arg)
1782 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1783 use_callchain = evsel__has_callchain(evsel);
1785 use_callchain = symbol_conf.use_callchain;
1787 use_callchain |= symbol_conf.show_branchflag_count;
1789 output_resort(evsel__hists(evsel), prog, use_callchain, cb, cb_arg);
1792 void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
1794 return perf_evsel__output_resort_cb(evsel, prog, NULL, NULL);
1797 void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1799 output_resort(hists, prog, symbol_conf.use_callchain, NULL, NULL);
1802 void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1803 hists__resort_cb_t cb)
1805 output_resort(hists, prog, symbol_conf.use_callchain, cb, NULL);
1808 static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1810 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1813 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1819 struct rb_node *rb_hierarchy_last(struct rb_node *node)
1821 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1823 while (can_goto_child(he, HMD_NORMAL)) {
1824 node = rb_last(&he->hroot_out.rb_root);
1825 he = rb_entry(node, struct hist_entry, rb_node);
1830 struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1832 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1834 if (can_goto_child(he, hmd))
1835 node = rb_first_cached(&he->hroot_out);
1837 node = rb_next(node);
1839 while (node == NULL) {
1844 node = rb_next(&he->rb_node);
1849 struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1851 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1853 node = rb_prev(node);
1855 return rb_hierarchy_last(node);
1861 return &he->rb_node;
1864 bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1866 struct rb_node *node;
1867 struct hist_entry *child;
1873 node = rb_first_cached(&he->hroot_out);
1874 child = rb_entry(node, struct hist_entry, rb_node);
1876 while (node && child->filtered) {
1877 node = rb_next(node);
1878 child = rb_entry(node, struct hist_entry, rb_node);
1882 percent = hist_entry__get_percent_limit(child);
1886 return node && percent >= limit;
1889 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
1890 enum hist_filter filter)
1892 h->filtered &= ~(1 << filter);
1894 if (symbol_conf.report_hierarchy) {
1895 struct hist_entry *parent = h->parent_he;
1898 he_stat__add_stat(&parent->stat, &h->stat);
1900 parent->filtered &= ~(1 << filter);
1902 if (parent->filtered)
1905 /* force fold unfiltered entry for simplicity */
1906 parent->unfolded = false;
1907 parent->has_no_entry = false;
1908 parent->row_offset = 0;
1909 parent->nr_rows = 0;
1911 parent = parent->parent_he;
1918 /* force fold unfiltered entry for simplicity */
1919 h->unfolded = false;
1920 h->has_no_entry = false;
1924 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
1926 hists__inc_filter_stats(hists, h);
1927 hists__calc_col_len(hists, h);
1931 static bool hists__filter_entry_by_dso(struct hists *hists,
1932 struct hist_entry *he)
1934 if (hists->dso_filter != NULL &&
1935 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1936 he->filtered |= (1 << HIST_FILTER__DSO);
1943 static bool hists__filter_entry_by_thread(struct hists *hists,
1944 struct hist_entry *he)
1946 if (hists->thread_filter != NULL &&
1947 he->thread != hists->thread_filter) {
1948 he->filtered |= (1 << HIST_FILTER__THREAD);
1955 static bool hists__filter_entry_by_symbol(struct hists *hists,
1956 struct hist_entry *he)
1958 if (hists->symbol_filter_str != NULL &&
1959 (!he->ms.sym || strstr(he->ms.sym->name,
1960 hists->symbol_filter_str) == NULL)) {
1961 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1968 static bool hists__filter_entry_by_socket(struct hists *hists,
1969 struct hist_entry *he)
1971 if ((hists->socket_filter > -1) &&
1972 (he->socket != hists->socket_filter)) {
1973 he->filtered |= (1 << HIST_FILTER__SOCKET);
1980 typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1982 static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
1986 hists->stats.nr_non_filtered_samples = 0;
1988 hists__reset_filter_stats(hists);
1989 hists__reset_col_len(hists);
1991 for (nd = rb_first_cached(&hists->entries); nd; nd = rb_next(nd)) {
1992 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
1994 if (filter(hists, h))
1997 hists__remove_entry_filter(hists, h, type);
2001 static void resort_filtered_entry(struct rb_root_cached *root,
2002 struct hist_entry *he)
2004 struct rb_node **p = &root->rb_root.rb_node;
2005 struct rb_node *parent = NULL;
2006 struct hist_entry *iter;
2007 struct rb_root_cached new_root = RB_ROOT_CACHED;
2009 bool leftmost = true;
2011 while (*p != NULL) {
2013 iter = rb_entry(parent, struct hist_entry, rb_node);
2015 if (hist_entry__sort(he, iter) > 0)
2018 p = &(*p)->rb_right;
2023 rb_link_node(&he->rb_node, parent, p);
2024 rb_insert_color_cached(&he->rb_node, root, leftmost);
2026 if (he->leaf || he->filtered)
2029 nd = rb_first_cached(&he->hroot_out);
2031 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2034 rb_erase_cached(&h->rb_node, &he->hroot_out);
2036 resort_filtered_entry(&new_root, h);
2039 he->hroot_out = new_root;
2042 static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2045 struct rb_root_cached new_root = RB_ROOT_CACHED;
2047 hists->stats.nr_non_filtered_samples = 0;
2049 hists__reset_filter_stats(hists);
2050 hists__reset_col_len(hists);
2052 nd = rb_first_cached(&hists->entries);
2054 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2057 ret = hist_entry__filter(h, type, arg);
2060 * case 1. non-matching type
2061 * zero out the period, set filter marker and move to child
2064 memset(&h->stat, 0, sizeof(h->stat));
2065 h->filtered |= (1 << type);
2067 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2070 * case 2. matched type (filter out)
2071 * set filter marker and move to next
2073 else if (ret == 1) {
2074 h->filtered |= (1 << type);
2076 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2079 * case 3. ok (not filtered)
2080 * add period to hists and parents, erase the filter marker
2081 * and move to next sibling
2084 hists__remove_entry_filter(hists, h, type);
2086 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2090 hierarchy_recalc_total_periods(hists);
2093 * resort output after applying a new filter since filter in a lower
2094 * hierarchy can change periods in a upper hierarchy.
2096 nd = rb_first_cached(&hists->entries);
2098 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2101 rb_erase_cached(&h->rb_node, &hists->entries);
2103 resort_filtered_entry(&new_root, h);
2106 hists->entries = new_root;
2109 void hists__filter_by_thread(struct hists *hists)
2111 if (symbol_conf.report_hierarchy)
2112 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2113 hists->thread_filter);
2115 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2116 hists__filter_entry_by_thread);
2119 void hists__filter_by_dso(struct hists *hists)
2121 if (symbol_conf.report_hierarchy)
2122 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2125 hists__filter_by_type(hists, HIST_FILTER__DSO,
2126 hists__filter_entry_by_dso);
2129 void hists__filter_by_symbol(struct hists *hists)
2131 if (symbol_conf.report_hierarchy)
2132 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2133 hists->symbol_filter_str);
2135 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2136 hists__filter_entry_by_symbol);
2139 void hists__filter_by_socket(struct hists *hists)
2141 if (symbol_conf.report_hierarchy)
2142 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2143 &hists->socket_filter);
2145 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2146 hists__filter_entry_by_socket);
2149 void events_stats__inc(struct events_stats *stats, u32 type)
2151 ++stats->nr_events[0];
2152 ++stats->nr_events[type];
2155 void hists__inc_nr_events(struct hists *hists, u32 type)
2157 events_stats__inc(&hists->stats, type);
2160 void hists__inc_nr_samples(struct hists *hists, bool filtered)
2162 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2164 hists->stats.nr_non_filtered_samples++;
2167 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2168 struct hist_entry *pair)
2170 struct rb_root_cached *root;
2172 struct rb_node *parent = NULL;
2173 struct hist_entry *he;
2175 bool leftmost = true;
2177 if (hists__has(hists, need_collapse))
2178 root = &hists->entries_collapsed;
2180 root = hists->entries_in;
2182 p = &root->rb_root.rb_node;
2184 while (*p != NULL) {
2186 he = rb_entry(parent, struct hist_entry, rb_node_in);
2188 cmp = hist_entry__collapse(he, pair);
2196 p = &(*p)->rb_right;
2201 he = hist_entry__new(pair, true);
2203 memset(&he->stat, 0, sizeof(he->stat));
2205 if (symbol_conf.cumulate_callchain)
2206 memset(he->stat_acc, 0, sizeof(he->stat));
2207 rb_link_node(&he->rb_node_in, parent, p);
2208 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
2209 hists__inc_stats(hists, he);
2216 static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
2217 struct rb_root_cached *root,
2218 struct hist_entry *pair)
2221 struct rb_node *parent = NULL;
2222 struct hist_entry *he;
2223 struct perf_hpp_fmt *fmt;
2224 bool leftmost = true;
2226 p = &root->rb_root.rb_node;
2227 while (*p != NULL) {
2231 he = rb_entry(parent, struct hist_entry, rb_node_in);
2233 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2234 cmp = fmt->collapse(fmt, he, pair);
2242 p = &parent->rb_left;
2244 p = &parent->rb_right;
2249 he = hist_entry__new(pair, true);
2251 rb_link_node(&he->rb_node_in, parent, p);
2252 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
2256 memset(&he->stat, 0, sizeof(he->stat));
2257 hists__inc_stats(hists, he);
2263 static struct hist_entry *hists__find_entry(struct hists *hists,
2264 struct hist_entry *he)
2268 if (hists__has(hists, need_collapse))
2269 n = hists->entries_collapsed.rb_root.rb_node;
2271 n = hists->entries_in->rb_root.rb_node;
2274 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2275 int64_t cmp = hist_entry__collapse(iter, he);
2288 static struct hist_entry *hists__find_hierarchy_entry(struct rb_root_cached *root,
2289 struct hist_entry *he)
2291 struct rb_node *n = root->rb_root.rb_node;
2294 struct hist_entry *iter;
2295 struct perf_hpp_fmt *fmt;
2298 iter = rb_entry(n, struct hist_entry, rb_node_in);
2299 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2300 cmp = fmt->collapse(fmt, iter, he);
2316 static void hists__match_hierarchy(struct rb_root_cached *leader_root,
2317 struct rb_root_cached *other_root)
2320 struct hist_entry *pos, *pair;
2322 for (nd = rb_first_cached(leader_root); nd; nd = rb_next(nd)) {
2323 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2324 pair = hists__find_hierarchy_entry(other_root, pos);
2327 hist_entry__add_pair(pair, pos);
2328 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2334 * Look for pairs to link to the leader buckets (hist_entries):
2336 void hists__match(struct hists *leader, struct hists *other)
2338 struct rb_root_cached *root;
2340 struct hist_entry *pos, *pair;
2342 if (symbol_conf.report_hierarchy) {
2343 /* hierarchy report always collapses entries */
2344 return hists__match_hierarchy(&leader->entries_collapsed,
2345 &other->entries_collapsed);
2348 if (hists__has(leader, need_collapse))
2349 root = &leader->entries_collapsed;
2351 root = leader->entries_in;
2353 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2354 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2355 pair = hists__find_entry(other, pos);
2358 hist_entry__add_pair(pair, pos);
2362 static int hists__link_hierarchy(struct hists *leader_hists,
2363 struct hist_entry *parent,
2364 struct rb_root_cached *leader_root,
2365 struct rb_root_cached *other_root)
2368 struct hist_entry *pos, *leader;
2370 for (nd = rb_first_cached(other_root); nd; nd = rb_next(nd)) {
2371 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2373 if (hist_entry__has_pairs(pos)) {
2376 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2377 if (leader->hists == leader_hists) {
2385 leader = add_dummy_hierarchy_entry(leader_hists,
2390 /* do not point parent in the pos */
2391 leader->parent_he = parent;
2393 hist_entry__add_pair(pos, leader);
2397 if (hists__link_hierarchy(leader_hists, leader,
2399 &pos->hroot_in) < 0)
2407 * Look for entries in the other hists that are not present in the leader, if
2408 * we find them, just add a dummy entry on the leader hists, with period=0,
2409 * nr_events=0, to serve as the list header.
2411 int hists__link(struct hists *leader, struct hists *other)
2413 struct rb_root_cached *root;
2415 struct hist_entry *pos, *pair;
2417 if (symbol_conf.report_hierarchy) {
2418 /* hierarchy report always collapses entries */
2419 return hists__link_hierarchy(leader, NULL,
2420 &leader->entries_collapsed,
2421 &other->entries_collapsed);
2424 if (hists__has(other, need_collapse))
2425 root = &other->entries_collapsed;
2427 root = other->entries_in;
2429 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2430 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2432 if (!hist_entry__has_pairs(pos)) {
2433 pair = hists__add_dummy_entry(leader, pos);
2436 hist_entry__add_pair(pos, pair);
2443 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2444 struct perf_sample *sample, bool nonany_branch_mode)
2446 struct branch_info *bi;
2448 /* If we have branch cycles always annotate them. */
2449 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2452 bi = sample__resolve_bstack(sample, al);
2454 struct addr_map_symbol *prev = NULL;
2457 * Ignore errors, still want to process the
2460 * For non standard branch modes always
2461 * force no IPC (prev == NULL)
2463 * Note that perf stores branches reversed from
2466 for (i = bs->nr - 1; i >= 0; i--) {
2467 addr_map_symbol__account_cycles(&bi[i].from,
2468 nonany_branch_mode ? NULL : prev,
2469 bi[i].flags.cycles);
2477 size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
2479 struct perf_evsel *pos;
2482 evlist__for_each_entry(evlist, pos) {
2483 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2484 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2491 u64 hists__total_period(struct hists *hists)
2493 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2494 hists->stats.total_period;
2497 int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq)
2501 const struct dso *dso = hists->dso_filter;
2502 const struct thread *thread = hists->thread_filter;
2503 int socket_id = hists->socket_filter;
2504 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
2505 u64 nr_events = hists->stats.total_period;
2506 struct perf_evsel *evsel = hists_to_evsel(hists);
2507 const char *ev_name = perf_evsel__name(evsel);
2508 char buf[512], sample_freq_str[64] = "";
2509 size_t buflen = sizeof(buf);
2510 char ref[30] = " show reference callgraph, ";
2511 bool enable_ref = false;
2513 if (symbol_conf.filter_relative) {
2514 nr_samples = hists->stats.nr_non_filtered_samples;
2515 nr_events = hists->stats.total_non_filtered_period;
2518 if (perf_evsel__is_group_event(evsel)) {
2519 struct perf_evsel *pos;
2521 perf_evsel__group_desc(evsel, buf, buflen);
2524 for_each_group_member(pos, evsel) {
2525 struct hists *pos_hists = evsel__hists(pos);
2527 if (symbol_conf.filter_relative) {
2528 nr_samples += pos_hists->stats.nr_non_filtered_samples;
2529 nr_events += pos_hists->stats.total_non_filtered_period;
2531 nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
2532 nr_events += pos_hists->stats.total_period;
2537 if (symbol_conf.show_ref_callgraph &&
2538 strstr(ev_name, "call-graph=no"))
2542 scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->attr.sample_freq);
2544 nr_samples = convert_unit(nr_samples, &unit);
2545 printed = scnprintf(bf, size,
2546 "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64,
2547 nr_samples, unit, evsel->nr_members > 1 ? "s" : "",
2548 ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events);
2551 if (hists->uid_filter_str)
2552 printed += snprintf(bf + printed, size - printed,
2553 ", UID: %s", hists->uid_filter_str);
2555 if (hists__has(hists, thread)) {
2556 printed += scnprintf(bf + printed, size - printed,
2558 (thread->comm_set ? thread__comm_str(thread) : ""),
2561 printed += scnprintf(bf + printed, size - printed,
2563 (thread->comm_set ? thread__comm_str(thread) : ""));
2567 printed += scnprintf(bf + printed, size - printed,
2568 ", DSO: %s", dso->short_name);
2570 printed += scnprintf(bf + printed, size - printed,
2571 ", Processor Socket: %d", socket_id);
2576 int parse_filter_percentage(const struct option *opt __maybe_unused,
2577 const char *arg, int unset __maybe_unused)
2579 if (!strcmp(arg, "relative"))
2580 symbol_conf.filter_relative = true;
2581 else if (!strcmp(arg, "absolute"))
2582 symbol_conf.filter_relative = false;
2584 pr_debug("Invalid percentage: %s\n", arg);
2591 int perf_hist_config(const char *var, const char *value)
2593 if (!strcmp(var, "hist.percentage"))
2594 return parse_filter_percentage(NULL, value, 0);
2599 int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
2601 memset(hists, 0, sizeof(*hists));
2602 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT_CACHED;
2603 hists->entries_in = &hists->entries_in_array[0];
2604 hists->entries_collapsed = RB_ROOT_CACHED;
2605 hists->entries = RB_ROOT_CACHED;
2606 pthread_mutex_init(&hists->lock, NULL);
2607 hists->socket_filter = -1;
2608 hists->hpp_list = hpp_list;
2609 INIT_LIST_HEAD(&hists->hpp_formats);
2613 static void hists__delete_remaining_entries(struct rb_root_cached *root)
2615 struct rb_node *node;
2616 struct hist_entry *he;
2618 while (!RB_EMPTY_ROOT(&root->rb_root)) {
2619 node = rb_first_cached(root);
2620 rb_erase_cached(node, root);
2622 he = rb_entry(node, struct hist_entry, rb_node_in);
2623 hist_entry__delete(he);
2627 static void hists__delete_all_entries(struct hists *hists)
2629 hists__delete_entries(hists);
2630 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2631 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2632 hists__delete_remaining_entries(&hists->entries_collapsed);
2635 static void hists_evsel__exit(struct perf_evsel *evsel)
2637 struct hists *hists = evsel__hists(evsel);
2638 struct perf_hpp_fmt *fmt, *pos;
2639 struct perf_hpp_list_node *node, *tmp;
2641 hists__delete_all_entries(hists);
2643 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2644 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2645 list_del(&fmt->list);
2648 list_del(&node->list);
2653 static int hists_evsel__init(struct perf_evsel *evsel)
2655 struct hists *hists = evsel__hists(evsel);
2657 __hists__init(hists, &perf_hpp_list);
2662 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2663 * stored in the rbtree...
2666 int hists__init(void)
2668 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
2672 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2677 void perf_hpp_list__init(struct perf_hpp_list *list)
2679 INIT_LIST_HEAD(&list->fields);
2680 INIT_LIST_HEAD(&list->sorts);