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)
424 he->srcline = strdup(he->srcline);
425 if (he->srcline == NULL)
429 INIT_LIST_HEAD(&he->pairs.node);
430 thread__get(he->thread);
431 he->hroot_in = RB_ROOT_CACHED;
432 he->hroot_out = RB_ROOT_CACHED;
434 if (!symbol_conf.report_hierarchy)
443 if (he->branch_info) {
444 map__put(he->branch_info->from.map);
445 map__put(he->branch_info->to.map);
446 free(he->branch_info);
449 map__put(he->mem_info->iaddr.map);
450 map__put(he->mem_info->daddr.map);
453 map__zput(he->ms.map);
458 static void *hist_entry__zalloc(size_t size)
460 return zalloc(size + sizeof(struct hist_entry));
463 static void hist_entry__free(void *ptr)
468 static struct hist_entry_ops default_ops = {
469 .new = hist_entry__zalloc,
470 .free = hist_entry__free,
473 static struct hist_entry *hist_entry__new(struct hist_entry *template,
476 struct hist_entry_ops *ops = template->ops;
477 size_t callchain_size = 0;
478 struct hist_entry *he;
482 ops = template->ops = &default_ops;
484 if (symbol_conf.use_callchain)
485 callchain_size = sizeof(struct callchain_root);
487 he = ops->new(callchain_size);
489 err = hist_entry__init(he, template, sample_self, callchain_size);
499 static u8 symbol__parent_filter(const struct symbol *parent)
501 if (symbol_conf.exclude_other && parent == NULL)
502 return 1 << HIST_FILTER__PARENT;
506 static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
508 if (!hist_entry__has_callchains(he) || !symbol_conf.use_callchain)
511 he->hists->callchain_period += period;
513 he->hists->callchain_non_filtered_period += period;
516 static struct hist_entry *hists__findnew_entry(struct hists *hists,
517 struct hist_entry *entry,
518 struct addr_location *al,
522 struct rb_node *parent = NULL;
523 struct hist_entry *he;
525 u64 period = entry->stat.period;
526 u64 weight = entry->stat.weight;
527 bool leftmost = true;
529 p = &hists->entries_in->rb_root.rb_node;
533 he = rb_entry(parent, struct hist_entry, rb_node_in);
536 * Make sure that it receives arguments in a same order as
537 * hist_entry__collapse() so that we can use an appropriate
538 * function when searching an entry regardless which sort
541 cmp = hist_entry__cmp(he, entry);
545 he_stat__add_period(&he->stat, period, weight);
546 hist_entry__add_callchain_period(he, period);
548 if (symbol_conf.cumulate_callchain)
549 he_stat__add_period(he->stat_acc, period, weight);
552 * This mem info was allocated from sample__resolve_mem
553 * and will not be used anymore.
555 mem_info__zput(entry->mem_info);
557 /* If the map of an existing hist_entry has
558 * become out-of-date due to an exec() or
559 * similar, update it. Otherwise we will
560 * mis-adjust symbol addresses when computing
561 * the history counter to increment.
563 if (he->ms.map != entry->ms.map) {
564 map__put(he->ms.map);
565 he->ms.map = map__get(entry->ms.map);
578 he = hist_entry__new(entry, sample_self);
583 hist_entry__add_callchain_period(he, period);
586 rb_link_node(&he->rb_node_in, parent, p);
587 rb_insert_color_cached(&he->rb_node_in, hists->entries_in, leftmost);
590 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
591 if (symbol_conf.cumulate_callchain)
592 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
596 static struct hist_entry*
597 __hists__add_entry(struct hists *hists,
598 struct addr_location *al,
599 struct symbol *sym_parent,
600 struct branch_info *bi,
602 struct perf_sample *sample,
604 struct hist_entry_ops *ops)
606 struct namespaces *ns = thread__namespaces(al->thread);
607 struct hist_entry entry = {
608 .thread = al->thread,
609 .comm = thread__comm(al->thread),
611 .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0,
612 .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0,
618 .srcline = (char *) al->srcline,
619 .socket = al->socket,
621 .cpumode = al->cpumode,
626 .period = sample->period,
627 .weight = sample->weight,
629 .parent = sym_parent,
630 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
634 .transaction = sample->transaction,
635 .raw_data = sample->raw_data,
636 .raw_size = sample->raw_size,
638 }, *he = hists__findnew_entry(hists, &entry, al, sample_self);
640 if (!hists->has_callchains && he && he->callchain_size != 0)
641 hists->has_callchains = true;
645 struct hist_entry *hists__add_entry(struct hists *hists,
646 struct addr_location *al,
647 struct symbol *sym_parent,
648 struct branch_info *bi,
650 struct perf_sample *sample,
653 return __hists__add_entry(hists, al, sym_parent, bi, mi,
654 sample, sample_self, NULL);
657 struct hist_entry *hists__add_entry_ops(struct hists *hists,
658 struct hist_entry_ops *ops,
659 struct addr_location *al,
660 struct symbol *sym_parent,
661 struct branch_info *bi,
663 struct perf_sample *sample,
666 return __hists__add_entry(hists, al, sym_parent, bi, mi,
667 sample, sample_self, ops);
671 iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
672 struct addr_location *al __maybe_unused)
678 iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
679 struct addr_location *al __maybe_unused)
685 iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
687 struct perf_sample *sample = iter->sample;
690 mi = sample__resolve_mem(sample, al);
699 iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
702 struct mem_info *mi = iter->priv;
703 struct hists *hists = evsel__hists(iter->evsel);
704 struct perf_sample *sample = iter->sample;
705 struct hist_entry *he;
710 cost = sample->weight;
715 * must pass period=weight in order to get the correct
716 * sorting from hists__collapse_resort() which is solely
717 * based on periods. We want sorting be done on nr_events * weight
718 * and this is indirectly achieved by passing period=weight here
719 * and the he_stat__add_period() function.
721 sample->period = cost;
723 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
733 iter_finish_mem_entry(struct hist_entry_iter *iter,
734 struct addr_location *al __maybe_unused)
736 struct perf_evsel *evsel = iter->evsel;
737 struct hists *hists = evsel__hists(evsel);
738 struct hist_entry *he = iter->he;
744 hists__inc_nr_samples(hists, he->filtered);
746 err = hist_entry__append_callchain(he, iter->sample);
750 * We don't need to free iter->priv (mem_info) here since the mem info
751 * was either already freed in hists__findnew_entry() or passed to a
752 * new hist entry by hist_entry__new().
761 iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
763 struct branch_info *bi;
764 struct perf_sample *sample = iter->sample;
766 bi = sample__resolve_bstack(sample, al);
771 iter->total = sample->branch_stack->nr;
778 iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
779 struct addr_location *al __maybe_unused)
785 iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
787 struct branch_info *bi = iter->priv;
793 if (iter->curr >= iter->total)
796 al->map = bi[i].to.map;
797 al->sym = bi[i].to.sym;
798 al->addr = bi[i].to.addr;
803 iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
805 struct branch_info *bi;
806 struct perf_evsel *evsel = iter->evsel;
807 struct hists *hists = evsel__hists(evsel);
808 struct perf_sample *sample = iter->sample;
809 struct hist_entry *he = NULL;
815 if (iter->hide_unresolved && !(bi[i].from.sym && bi[i].to.sym))
819 * The report shows the percentage of total branches captured
820 * and not events sampled. Thus we use a pseudo period of 1.
823 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
825 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
830 hists__inc_nr_samples(hists, he->filtered);
839 iter_finish_branch_entry(struct hist_entry_iter *iter,
840 struct addr_location *al __maybe_unused)
845 return iter->curr >= iter->total ? 0 : -1;
849 iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
850 struct addr_location *al __maybe_unused)
856 iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
858 struct perf_evsel *evsel = iter->evsel;
859 struct perf_sample *sample = iter->sample;
860 struct hist_entry *he;
862 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
872 iter_finish_normal_entry(struct hist_entry_iter *iter,
873 struct addr_location *al __maybe_unused)
875 struct hist_entry *he = iter->he;
876 struct perf_evsel *evsel = iter->evsel;
877 struct perf_sample *sample = iter->sample;
884 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
886 return hist_entry__append_callchain(he, sample);
890 iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
891 struct addr_location *al __maybe_unused)
893 struct hist_entry **he_cache;
895 callchain_cursor_commit(&callchain_cursor);
898 * This is for detecting cycles or recursions so that they're
899 * cumulated only one time to prevent entries more than 100%
902 he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1));
903 if (he_cache == NULL)
906 iter->priv = he_cache;
913 iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
914 struct addr_location *al)
916 struct perf_evsel *evsel = iter->evsel;
917 struct hists *hists = evsel__hists(evsel);
918 struct perf_sample *sample = iter->sample;
919 struct hist_entry **he_cache = iter->priv;
920 struct hist_entry *he;
923 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
929 he_cache[iter->curr++] = he;
931 hist_entry__append_callchain(he, sample);
934 * We need to re-initialize the cursor since callchain_append()
935 * advanced the cursor to the end.
937 callchain_cursor_commit(&callchain_cursor);
939 hists__inc_nr_samples(hists, he->filtered);
945 iter_next_cumulative_entry(struct hist_entry_iter *iter,
946 struct addr_location *al)
948 struct callchain_cursor_node *node;
950 node = callchain_cursor_current(&callchain_cursor);
954 return fill_callchain_info(al, node, iter->hide_unresolved);
958 iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
959 struct addr_location *al)
961 struct perf_evsel *evsel = iter->evsel;
962 struct perf_sample *sample = iter->sample;
963 struct hist_entry **he_cache = iter->priv;
964 struct hist_entry *he;
965 struct hist_entry he_tmp = {
966 .hists = evsel__hists(evsel),
968 .thread = al->thread,
969 .comm = thread__comm(al->thread),
975 .srcline = (char *) al->srcline,
976 .parent = iter->parent,
977 .raw_data = sample->raw_data,
978 .raw_size = sample->raw_size,
981 struct callchain_cursor cursor;
983 callchain_cursor_snapshot(&cursor, &callchain_cursor);
985 callchain_cursor_advance(&callchain_cursor);
988 * Check if there's duplicate entries in the callchain.
989 * It's possible that it has cycles or recursive calls.
991 for (i = 0; i < iter->curr; i++) {
992 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
993 /* to avoid calling callback function */
999 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
1005 he_cache[iter->curr++] = he;
1007 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
1008 callchain_append(he->callchain, &cursor, sample->period);
1013 iter_finish_cumulative_entry(struct hist_entry_iter *iter,
1014 struct addr_location *al __maybe_unused)
1022 const struct hist_iter_ops hist_iter_mem = {
1023 .prepare_entry = iter_prepare_mem_entry,
1024 .add_single_entry = iter_add_single_mem_entry,
1025 .next_entry = iter_next_nop_entry,
1026 .add_next_entry = iter_add_next_nop_entry,
1027 .finish_entry = iter_finish_mem_entry,
1030 const struct hist_iter_ops hist_iter_branch = {
1031 .prepare_entry = iter_prepare_branch_entry,
1032 .add_single_entry = iter_add_single_branch_entry,
1033 .next_entry = iter_next_branch_entry,
1034 .add_next_entry = iter_add_next_branch_entry,
1035 .finish_entry = iter_finish_branch_entry,
1038 const struct hist_iter_ops hist_iter_normal = {
1039 .prepare_entry = iter_prepare_normal_entry,
1040 .add_single_entry = iter_add_single_normal_entry,
1041 .next_entry = iter_next_nop_entry,
1042 .add_next_entry = iter_add_next_nop_entry,
1043 .finish_entry = iter_finish_normal_entry,
1046 const struct hist_iter_ops hist_iter_cumulative = {
1047 .prepare_entry = iter_prepare_cumulative_entry,
1048 .add_single_entry = iter_add_single_cumulative_entry,
1049 .next_entry = iter_next_cumulative_entry,
1050 .add_next_entry = iter_add_next_cumulative_entry,
1051 .finish_entry = iter_finish_cumulative_entry,
1054 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
1055 int max_stack_depth, void *arg)
1058 struct map *alm = NULL;
1061 alm = map__get(al->map);
1063 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
1064 iter->evsel, al, max_stack_depth);
1068 err = iter->ops->prepare_entry(iter, al);
1072 err = iter->ops->add_single_entry(iter, al);
1076 if (iter->he && iter->add_entry_cb) {
1077 err = iter->add_entry_cb(iter, al, true, arg);
1082 while (iter->ops->next_entry(iter, al)) {
1083 err = iter->ops->add_next_entry(iter, al);
1087 if (iter->he && iter->add_entry_cb) {
1088 err = iter->add_entry_cb(iter, al, false, arg);
1095 err2 = iter->ops->finish_entry(iter, al);
1105 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1107 struct hists *hists = left->hists;
1108 struct perf_hpp_fmt *fmt;
1111 hists__for_each_sort_list(hists, fmt) {
1112 if (perf_hpp__is_dynamic_entry(fmt) &&
1113 !perf_hpp__defined_dynamic_entry(fmt, hists))
1116 cmp = fmt->cmp(fmt, left, right);
1125 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1127 struct hists *hists = left->hists;
1128 struct perf_hpp_fmt *fmt;
1131 hists__for_each_sort_list(hists, fmt) {
1132 if (perf_hpp__is_dynamic_entry(fmt) &&
1133 !perf_hpp__defined_dynamic_entry(fmt, hists))
1136 cmp = fmt->collapse(fmt, left, right);
1144 void hist_entry__delete(struct hist_entry *he)
1146 struct hist_entry_ops *ops = he->ops;
1148 thread__zput(he->thread);
1149 map__zput(he->ms.map);
1151 if (he->branch_info) {
1152 map__zput(he->branch_info->from.map);
1153 map__zput(he->branch_info->to.map);
1154 free_srcline(he->branch_info->srcline_from);
1155 free_srcline(he->branch_info->srcline_to);
1156 zfree(&he->branch_info);
1160 map__zput(he->mem_info->iaddr.map);
1161 map__zput(he->mem_info->daddr.map);
1162 mem_info__zput(he->mem_info);
1165 zfree(&he->stat_acc);
1166 free_srcline(he->srcline);
1167 if (he->srcfile && he->srcfile[0])
1169 free_callchain(he->callchain);
1170 free(he->trace_output);
1176 * If this is not the last column, then we need to pad it according to the
1177 * pre-calculated max length for this column, otherwise don't bother adding
1178 * spaces because that would break viewing this with, for instance, 'less',
1179 * that would show tons of trailing spaces when a long C++ demangled method
1182 int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1183 struct perf_hpp_fmt *fmt, int printed)
1185 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
1186 const int width = fmt->width(fmt, hpp, he->hists);
1187 if (printed < width) {
1188 advance_hpp(hpp, printed);
1189 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1197 * collapse the histogram
1200 static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
1201 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1202 enum hist_filter type);
1204 typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1206 static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1208 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1211 static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1212 enum hist_filter type,
1215 struct perf_hpp_fmt *fmt;
1216 bool type_match = false;
1217 struct hist_entry *parent = he->parent_he;
1220 case HIST_FILTER__THREAD:
1221 if (symbol_conf.comm_list == NULL &&
1222 symbol_conf.pid_list == NULL &&
1223 symbol_conf.tid_list == NULL)
1226 case HIST_FILTER__DSO:
1227 if (symbol_conf.dso_list == NULL)
1230 case HIST_FILTER__SYMBOL:
1231 if (symbol_conf.sym_list == NULL)
1234 case HIST_FILTER__PARENT:
1235 case HIST_FILTER__GUEST:
1236 case HIST_FILTER__HOST:
1237 case HIST_FILTER__SOCKET:
1238 case HIST_FILTER__C2C:
1243 /* if it's filtered by own fmt, it has to have filter bits */
1244 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1253 * If the filter is for current level entry, propagate
1254 * filter marker to parents. The marker bit was
1255 * already set by default so it only needs to clear
1256 * non-filtered entries.
1258 if (!(he->filtered & (1 << type))) {
1260 parent->filtered &= ~(1 << type);
1261 parent = parent->parent_he;
1266 * If current entry doesn't have matching formats, set
1267 * filter marker for upper level entries. it will be
1268 * cleared if its lower level entries is not filtered.
1270 * For lower-level entries, it inherits parent's
1271 * filter bit so that lower level entries of a
1272 * non-filtered entry won't set the filter marker.
1275 he->filtered |= (1 << type);
1277 he->filtered |= (parent->filtered & (1 << type));
1281 static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1283 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1284 check_thread_entry);
1286 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1287 perf_hpp__is_dso_entry);
1289 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1290 perf_hpp__is_sym_entry);
1292 hists__apply_filters(he->hists, he);
1295 static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1296 struct rb_root_cached *root,
1297 struct hist_entry *he,
1298 struct hist_entry *parent_he,
1299 struct perf_hpp_list *hpp_list)
1301 struct rb_node **p = &root->rb_root.rb_node;
1302 struct rb_node *parent = NULL;
1303 struct hist_entry *iter, *new;
1304 struct perf_hpp_fmt *fmt;
1306 bool leftmost = true;
1308 while (*p != NULL) {
1310 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1313 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1314 cmp = fmt->collapse(fmt, iter, he);
1320 he_stat__add_stat(&iter->stat, &he->stat);
1325 p = &parent->rb_left;
1327 p = &parent->rb_right;
1332 new = hist_entry__new(he, true);
1336 hists->nr_entries++;
1338 /* save related format list for output */
1339 new->hpp_list = hpp_list;
1340 new->parent_he = parent_he;
1342 hist_entry__apply_hierarchy_filters(new);
1344 /* some fields are now passed to 'new' */
1345 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1346 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1347 he->trace_output = NULL;
1349 new->trace_output = NULL;
1351 if (perf_hpp__is_srcline_entry(fmt))
1354 new->srcline = NULL;
1356 if (perf_hpp__is_srcfile_entry(fmt))
1359 new->srcfile = NULL;
1362 rb_link_node(&new->rb_node_in, parent, p);
1363 rb_insert_color_cached(&new->rb_node_in, root, leftmost);
1367 static int hists__hierarchy_insert_entry(struct hists *hists,
1368 struct rb_root_cached *root,
1369 struct hist_entry *he)
1371 struct perf_hpp_list_node *node;
1372 struct hist_entry *new_he = NULL;
1373 struct hist_entry *parent = NULL;
1377 list_for_each_entry(node, &hists->hpp_formats, list) {
1378 /* skip period (overhead) and elided columns */
1379 if (node->level == 0 || node->skip)
1382 /* insert copy of 'he' for each fmt into the hierarchy */
1383 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
1384 if (new_he == NULL) {
1389 root = &new_he->hroot_in;
1390 new_he->depth = depth++;
1395 new_he->leaf = true;
1397 if (hist_entry__has_callchains(new_he) &&
1398 symbol_conf.use_callchain) {
1399 callchain_cursor_reset(&callchain_cursor);
1400 if (callchain_merge(&callchain_cursor,
1407 /* 'he' is no longer used */
1408 hist_entry__delete(he);
1410 /* return 0 (or -1) since it already applied filters */
1414 static int hists__collapse_insert_entry(struct hists *hists,
1415 struct rb_root_cached *root,
1416 struct hist_entry *he)
1418 struct rb_node **p = &root->rb_root.rb_node;
1419 struct rb_node *parent = NULL;
1420 struct hist_entry *iter;
1422 bool leftmost = true;
1424 if (symbol_conf.report_hierarchy)
1425 return hists__hierarchy_insert_entry(hists, root, he);
1427 while (*p != NULL) {
1429 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1431 cmp = hist_entry__collapse(iter, he);
1436 he_stat__add_stat(&iter->stat, &he->stat);
1437 if (symbol_conf.cumulate_callchain)
1438 he_stat__add_stat(iter->stat_acc, he->stat_acc);
1440 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
1441 callchain_cursor_reset(&callchain_cursor);
1442 if (callchain_merge(&callchain_cursor,
1447 hist_entry__delete(he);
1454 p = &(*p)->rb_right;
1458 hists->nr_entries++;
1460 rb_link_node(&he->rb_node_in, parent, p);
1461 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
1465 struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists)
1467 struct rb_root_cached *root;
1469 pthread_mutex_lock(&hists->lock);
1471 root = hists->entries_in;
1472 if (++hists->entries_in > &hists->entries_in_array[1])
1473 hists->entries_in = &hists->entries_in_array[0];
1475 pthread_mutex_unlock(&hists->lock);
1480 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1482 hists__filter_entry_by_dso(hists, he);
1483 hists__filter_entry_by_thread(hists, he);
1484 hists__filter_entry_by_symbol(hists, he);
1485 hists__filter_entry_by_socket(hists, he);
1488 int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
1490 struct rb_root_cached *root;
1491 struct rb_node *next;
1492 struct hist_entry *n;
1495 if (!hists__has(hists, need_collapse))
1498 hists->nr_entries = 0;
1500 root = hists__get_rotate_entries_in(hists);
1502 next = rb_first_cached(root);
1507 n = rb_entry(next, struct hist_entry, rb_node_in);
1508 next = rb_next(&n->rb_node_in);
1510 rb_erase_cached(&n->rb_node_in, root);
1511 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1517 * If it wasn't combined with one of the entries already
1518 * collapsed, we need to apply the filters that may have
1519 * been set by, say, the hist_browser.
1521 hists__apply_filters(hists, n);
1524 ui_progress__update(prog, 1);
1529 static int hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
1531 struct hists *hists = a->hists;
1532 struct perf_hpp_fmt *fmt;
1535 hists__for_each_sort_list(hists, fmt) {
1536 if (perf_hpp__should_skip(fmt, a->hists))
1539 cmp = fmt->sort(fmt, a, b);
1547 static void hists__reset_filter_stats(struct hists *hists)
1549 hists->nr_non_filtered_entries = 0;
1550 hists->stats.total_non_filtered_period = 0;
1553 void hists__reset_stats(struct hists *hists)
1555 hists->nr_entries = 0;
1556 hists->stats.total_period = 0;
1558 hists__reset_filter_stats(hists);
1561 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1563 hists->nr_non_filtered_entries++;
1564 hists->stats.total_non_filtered_period += h->stat.period;
1567 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1570 hists__inc_filter_stats(hists, h);
1572 hists->nr_entries++;
1573 hists->stats.total_period += h->stat.period;
1576 static void hierarchy_recalc_total_periods(struct hists *hists)
1578 struct rb_node *node;
1579 struct hist_entry *he;
1581 node = rb_first_cached(&hists->entries);
1583 hists->stats.total_period = 0;
1584 hists->stats.total_non_filtered_period = 0;
1587 * recalculate total period using top-level entries only
1588 * since lower level entries only see non-filtered entries
1589 * but upper level entries have sum of both entries.
1592 he = rb_entry(node, struct hist_entry, rb_node);
1593 node = rb_next(node);
1595 hists->stats.total_period += he->stat.period;
1597 hists->stats.total_non_filtered_period += he->stat.period;
1601 static void hierarchy_insert_output_entry(struct rb_root_cached *root,
1602 struct hist_entry *he)
1604 struct rb_node **p = &root->rb_root.rb_node;
1605 struct rb_node *parent = NULL;
1606 struct hist_entry *iter;
1607 struct perf_hpp_fmt *fmt;
1608 bool leftmost = true;
1610 while (*p != NULL) {
1612 iter = rb_entry(parent, struct hist_entry, rb_node);
1614 if (hist_entry__sort(he, iter) > 0)
1615 p = &parent->rb_left;
1617 p = &parent->rb_right;
1622 rb_link_node(&he->rb_node, parent, p);
1623 rb_insert_color_cached(&he->rb_node, root, leftmost);
1625 /* update column width of dynamic entry */
1626 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1627 if (perf_hpp__is_dynamic_entry(fmt))
1628 fmt->sort(fmt, he, NULL);
1632 static void hists__hierarchy_output_resort(struct hists *hists,
1633 struct ui_progress *prog,
1634 struct rb_root_cached *root_in,
1635 struct rb_root_cached *root_out,
1636 u64 min_callchain_hits,
1639 struct rb_node *node;
1640 struct hist_entry *he;
1642 *root_out = RB_ROOT_CACHED;
1643 node = rb_first_cached(root_in);
1646 he = rb_entry(node, struct hist_entry, rb_node_in);
1647 node = rb_next(node);
1649 hierarchy_insert_output_entry(root_out, he);
1652 ui_progress__update(prog, 1);
1654 hists->nr_entries++;
1655 if (!he->filtered) {
1656 hists->nr_non_filtered_entries++;
1657 hists__calc_col_len(hists, he);
1661 hists__hierarchy_output_resort(hists, prog,
1672 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1673 u64 total = he->stat.period;
1675 if (symbol_conf.cumulate_callchain)
1676 total = he->stat_acc->period;
1678 min_callchain_hits = total * (callchain_param.min_percent / 100);
1681 callchain_param.sort(&he->sorted_chain, he->callchain,
1682 min_callchain_hits, &callchain_param);
1686 static void __hists__insert_output_entry(struct rb_root_cached *entries,
1687 struct hist_entry *he,
1688 u64 min_callchain_hits,
1691 struct rb_node **p = &entries->rb_root.rb_node;
1692 struct rb_node *parent = NULL;
1693 struct hist_entry *iter;
1694 struct perf_hpp_fmt *fmt;
1695 bool leftmost = true;
1697 if (use_callchain) {
1698 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1699 u64 total = he->stat.period;
1701 if (symbol_conf.cumulate_callchain)
1702 total = he->stat_acc->period;
1704 min_callchain_hits = total * (callchain_param.min_percent / 100);
1706 callchain_param.sort(&he->sorted_chain, he->callchain,
1707 min_callchain_hits, &callchain_param);
1710 while (*p != NULL) {
1712 iter = rb_entry(parent, struct hist_entry, rb_node);
1714 if (hist_entry__sort(he, iter) > 0)
1717 p = &(*p)->rb_right;
1722 rb_link_node(&he->rb_node, parent, p);
1723 rb_insert_color_cached(&he->rb_node, entries, leftmost);
1725 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1726 if (perf_hpp__is_dynamic_entry(fmt) &&
1727 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1728 fmt->sort(fmt, he, NULL); /* update column width */
1732 static void output_resort(struct hists *hists, struct ui_progress *prog,
1733 bool use_callchain, hists__resort_cb_t cb,
1736 struct rb_root_cached *root;
1737 struct rb_node *next;
1738 struct hist_entry *n;
1739 u64 callchain_total;
1740 u64 min_callchain_hits;
1742 callchain_total = hists->callchain_period;
1743 if (symbol_conf.filter_relative)
1744 callchain_total = hists->callchain_non_filtered_period;
1746 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
1748 hists__reset_stats(hists);
1749 hists__reset_col_len(hists);
1751 if (symbol_conf.report_hierarchy) {
1752 hists__hierarchy_output_resort(hists, prog,
1753 &hists->entries_collapsed,
1757 hierarchy_recalc_total_periods(hists);
1761 if (hists__has(hists, need_collapse))
1762 root = &hists->entries_collapsed;
1764 root = hists->entries_in;
1766 next = rb_first_cached(root);
1767 hists->entries = RB_ROOT_CACHED;
1770 n = rb_entry(next, struct hist_entry, rb_node_in);
1771 next = rb_next(&n->rb_node_in);
1773 if (cb && cb(n, cb_arg))
1776 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
1777 hists__inc_stats(hists, n);
1780 hists__calc_col_len(hists, n);
1783 ui_progress__update(prog, 1);
1787 void perf_evsel__output_resort_cb(struct perf_evsel *evsel, struct ui_progress *prog,
1788 hists__resort_cb_t cb, void *cb_arg)
1792 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1793 use_callchain = evsel__has_callchain(evsel);
1795 use_callchain = symbol_conf.use_callchain;
1797 use_callchain |= symbol_conf.show_branchflag_count;
1799 output_resort(evsel__hists(evsel), prog, use_callchain, cb, cb_arg);
1802 void perf_evsel__output_resort(struct perf_evsel *evsel, struct ui_progress *prog)
1804 return perf_evsel__output_resort_cb(evsel, prog, NULL, NULL);
1807 void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1809 output_resort(hists, prog, symbol_conf.use_callchain, NULL, NULL);
1812 void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1813 hists__resort_cb_t cb)
1815 output_resort(hists, prog, symbol_conf.use_callchain, cb, NULL);
1818 static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1820 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1823 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1829 struct rb_node *rb_hierarchy_last(struct rb_node *node)
1831 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1833 while (can_goto_child(he, HMD_NORMAL)) {
1834 node = rb_last(&he->hroot_out.rb_root);
1835 he = rb_entry(node, struct hist_entry, rb_node);
1840 struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1842 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1844 if (can_goto_child(he, hmd))
1845 node = rb_first_cached(&he->hroot_out);
1847 node = rb_next(node);
1849 while (node == NULL) {
1854 node = rb_next(&he->rb_node);
1859 struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1861 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1863 node = rb_prev(node);
1865 return rb_hierarchy_last(node);
1871 return &he->rb_node;
1874 bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1876 struct rb_node *node;
1877 struct hist_entry *child;
1883 node = rb_first_cached(&he->hroot_out);
1884 child = rb_entry(node, struct hist_entry, rb_node);
1886 while (node && child->filtered) {
1887 node = rb_next(node);
1888 child = rb_entry(node, struct hist_entry, rb_node);
1892 percent = hist_entry__get_percent_limit(child);
1896 return node && percent >= limit;
1899 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
1900 enum hist_filter filter)
1902 h->filtered &= ~(1 << filter);
1904 if (symbol_conf.report_hierarchy) {
1905 struct hist_entry *parent = h->parent_he;
1908 he_stat__add_stat(&parent->stat, &h->stat);
1910 parent->filtered &= ~(1 << filter);
1912 if (parent->filtered)
1915 /* force fold unfiltered entry for simplicity */
1916 parent->unfolded = false;
1917 parent->has_no_entry = false;
1918 parent->row_offset = 0;
1919 parent->nr_rows = 0;
1921 parent = parent->parent_he;
1928 /* force fold unfiltered entry for simplicity */
1929 h->unfolded = false;
1930 h->has_no_entry = false;
1934 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
1936 hists__inc_filter_stats(hists, h);
1937 hists__calc_col_len(hists, h);
1941 static bool hists__filter_entry_by_dso(struct hists *hists,
1942 struct hist_entry *he)
1944 if (hists->dso_filter != NULL &&
1945 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
1946 he->filtered |= (1 << HIST_FILTER__DSO);
1953 static bool hists__filter_entry_by_thread(struct hists *hists,
1954 struct hist_entry *he)
1956 if (hists->thread_filter != NULL &&
1957 he->thread != hists->thread_filter) {
1958 he->filtered |= (1 << HIST_FILTER__THREAD);
1965 static bool hists__filter_entry_by_symbol(struct hists *hists,
1966 struct hist_entry *he)
1968 if (hists->symbol_filter_str != NULL &&
1969 (!he->ms.sym || strstr(he->ms.sym->name,
1970 hists->symbol_filter_str) == NULL)) {
1971 he->filtered |= (1 << HIST_FILTER__SYMBOL);
1978 static bool hists__filter_entry_by_socket(struct hists *hists,
1979 struct hist_entry *he)
1981 if ((hists->socket_filter > -1) &&
1982 (he->socket != hists->socket_filter)) {
1983 he->filtered |= (1 << HIST_FILTER__SOCKET);
1990 typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
1992 static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
1996 hists->stats.nr_non_filtered_samples = 0;
1998 hists__reset_filter_stats(hists);
1999 hists__reset_col_len(hists);
2001 for (nd = rb_first_cached(&hists->entries); nd; nd = rb_next(nd)) {
2002 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2004 if (filter(hists, h))
2007 hists__remove_entry_filter(hists, h, type);
2011 static void resort_filtered_entry(struct rb_root_cached *root,
2012 struct hist_entry *he)
2014 struct rb_node **p = &root->rb_root.rb_node;
2015 struct rb_node *parent = NULL;
2016 struct hist_entry *iter;
2017 struct rb_root_cached new_root = RB_ROOT_CACHED;
2019 bool leftmost = true;
2021 while (*p != NULL) {
2023 iter = rb_entry(parent, struct hist_entry, rb_node);
2025 if (hist_entry__sort(he, iter) > 0)
2028 p = &(*p)->rb_right;
2033 rb_link_node(&he->rb_node, parent, p);
2034 rb_insert_color_cached(&he->rb_node, root, leftmost);
2036 if (he->leaf || he->filtered)
2039 nd = rb_first_cached(&he->hroot_out);
2041 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2044 rb_erase_cached(&h->rb_node, &he->hroot_out);
2046 resort_filtered_entry(&new_root, h);
2049 he->hroot_out = new_root;
2052 static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2055 struct rb_root_cached new_root = RB_ROOT_CACHED;
2057 hists->stats.nr_non_filtered_samples = 0;
2059 hists__reset_filter_stats(hists);
2060 hists__reset_col_len(hists);
2062 nd = rb_first_cached(&hists->entries);
2064 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2067 ret = hist_entry__filter(h, type, arg);
2070 * case 1. non-matching type
2071 * zero out the period, set filter marker and move to child
2074 memset(&h->stat, 0, sizeof(h->stat));
2075 h->filtered |= (1 << type);
2077 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2080 * case 2. matched type (filter out)
2081 * set filter marker and move to next
2083 else if (ret == 1) {
2084 h->filtered |= (1 << type);
2086 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2089 * case 3. ok (not filtered)
2090 * add period to hists and parents, erase the filter marker
2091 * and move to next sibling
2094 hists__remove_entry_filter(hists, h, type);
2096 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2100 hierarchy_recalc_total_periods(hists);
2103 * resort output after applying a new filter since filter in a lower
2104 * hierarchy can change periods in a upper hierarchy.
2106 nd = rb_first_cached(&hists->entries);
2108 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2111 rb_erase_cached(&h->rb_node, &hists->entries);
2113 resort_filtered_entry(&new_root, h);
2116 hists->entries = new_root;
2119 void hists__filter_by_thread(struct hists *hists)
2121 if (symbol_conf.report_hierarchy)
2122 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2123 hists->thread_filter);
2125 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2126 hists__filter_entry_by_thread);
2129 void hists__filter_by_dso(struct hists *hists)
2131 if (symbol_conf.report_hierarchy)
2132 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2135 hists__filter_by_type(hists, HIST_FILTER__DSO,
2136 hists__filter_entry_by_dso);
2139 void hists__filter_by_symbol(struct hists *hists)
2141 if (symbol_conf.report_hierarchy)
2142 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2143 hists->symbol_filter_str);
2145 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2146 hists__filter_entry_by_symbol);
2149 void hists__filter_by_socket(struct hists *hists)
2151 if (symbol_conf.report_hierarchy)
2152 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2153 &hists->socket_filter);
2155 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2156 hists__filter_entry_by_socket);
2159 void events_stats__inc(struct events_stats *stats, u32 type)
2161 ++stats->nr_events[0];
2162 ++stats->nr_events[type];
2165 void hists__inc_nr_events(struct hists *hists, u32 type)
2167 events_stats__inc(&hists->stats, type);
2170 void hists__inc_nr_samples(struct hists *hists, bool filtered)
2172 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2174 hists->stats.nr_non_filtered_samples++;
2177 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2178 struct hist_entry *pair)
2180 struct rb_root_cached *root;
2182 struct rb_node *parent = NULL;
2183 struct hist_entry *he;
2185 bool leftmost = true;
2187 if (hists__has(hists, need_collapse))
2188 root = &hists->entries_collapsed;
2190 root = hists->entries_in;
2192 p = &root->rb_root.rb_node;
2194 while (*p != NULL) {
2196 he = rb_entry(parent, struct hist_entry, rb_node_in);
2198 cmp = hist_entry__collapse(he, pair);
2206 p = &(*p)->rb_right;
2211 he = hist_entry__new(pair, true);
2213 memset(&he->stat, 0, sizeof(he->stat));
2215 if (symbol_conf.cumulate_callchain)
2216 memset(he->stat_acc, 0, sizeof(he->stat));
2217 rb_link_node(&he->rb_node_in, parent, p);
2218 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
2219 hists__inc_stats(hists, he);
2226 static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
2227 struct rb_root_cached *root,
2228 struct hist_entry *pair)
2231 struct rb_node *parent = NULL;
2232 struct hist_entry *he;
2233 struct perf_hpp_fmt *fmt;
2234 bool leftmost = true;
2236 p = &root->rb_root.rb_node;
2237 while (*p != NULL) {
2241 he = rb_entry(parent, struct hist_entry, rb_node_in);
2243 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2244 cmp = fmt->collapse(fmt, he, pair);
2252 p = &parent->rb_left;
2254 p = &parent->rb_right;
2259 he = hist_entry__new(pair, true);
2261 rb_link_node(&he->rb_node_in, parent, p);
2262 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
2266 memset(&he->stat, 0, sizeof(he->stat));
2267 hists__inc_stats(hists, he);
2273 static struct hist_entry *hists__find_entry(struct hists *hists,
2274 struct hist_entry *he)
2278 if (hists__has(hists, need_collapse))
2279 n = hists->entries_collapsed.rb_root.rb_node;
2281 n = hists->entries_in->rb_root.rb_node;
2284 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2285 int64_t cmp = hist_entry__collapse(iter, he);
2298 static struct hist_entry *hists__find_hierarchy_entry(struct rb_root_cached *root,
2299 struct hist_entry *he)
2301 struct rb_node *n = root->rb_root.rb_node;
2304 struct hist_entry *iter;
2305 struct perf_hpp_fmt *fmt;
2308 iter = rb_entry(n, struct hist_entry, rb_node_in);
2309 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2310 cmp = fmt->collapse(fmt, iter, he);
2326 static void hists__match_hierarchy(struct rb_root_cached *leader_root,
2327 struct rb_root_cached *other_root)
2330 struct hist_entry *pos, *pair;
2332 for (nd = rb_first_cached(leader_root); nd; nd = rb_next(nd)) {
2333 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2334 pair = hists__find_hierarchy_entry(other_root, pos);
2337 hist_entry__add_pair(pair, pos);
2338 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2344 * Look for pairs to link to the leader buckets (hist_entries):
2346 void hists__match(struct hists *leader, struct hists *other)
2348 struct rb_root_cached *root;
2350 struct hist_entry *pos, *pair;
2352 if (symbol_conf.report_hierarchy) {
2353 /* hierarchy report always collapses entries */
2354 return hists__match_hierarchy(&leader->entries_collapsed,
2355 &other->entries_collapsed);
2358 if (hists__has(leader, need_collapse))
2359 root = &leader->entries_collapsed;
2361 root = leader->entries_in;
2363 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2364 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2365 pair = hists__find_entry(other, pos);
2368 hist_entry__add_pair(pair, pos);
2372 static int hists__link_hierarchy(struct hists *leader_hists,
2373 struct hist_entry *parent,
2374 struct rb_root_cached *leader_root,
2375 struct rb_root_cached *other_root)
2378 struct hist_entry *pos, *leader;
2380 for (nd = rb_first_cached(other_root); nd; nd = rb_next(nd)) {
2381 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2383 if (hist_entry__has_pairs(pos)) {
2386 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2387 if (leader->hists == leader_hists) {
2395 leader = add_dummy_hierarchy_entry(leader_hists,
2400 /* do not point parent in the pos */
2401 leader->parent_he = parent;
2403 hist_entry__add_pair(pos, leader);
2407 if (hists__link_hierarchy(leader_hists, leader,
2409 &pos->hroot_in) < 0)
2417 * Look for entries in the other hists that are not present in the leader, if
2418 * we find them, just add a dummy entry on the leader hists, with period=0,
2419 * nr_events=0, to serve as the list header.
2421 int hists__link(struct hists *leader, struct hists *other)
2423 struct rb_root_cached *root;
2425 struct hist_entry *pos, *pair;
2427 if (symbol_conf.report_hierarchy) {
2428 /* hierarchy report always collapses entries */
2429 return hists__link_hierarchy(leader, NULL,
2430 &leader->entries_collapsed,
2431 &other->entries_collapsed);
2434 if (hists__has(other, need_collapse))
2435 root = &other->entries_collapsed;
2437 root = other->entries_in;
2439 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2440 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2442 if (!hist_entry__has_pairs(pos)) {
2443 pair = hists__add_dummy_entry(leader, pos);
2446 hist_entry__add_pair(pos, pair);
2453 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2454 struct perf_sample *sample, bool nonany_branch_mode)
2456 struct branch_info *bi;
2458 /* If we have branch cycles always annotate them. */
2459 if (bs && bs->nr && bs->entries[0].flags.cycles) {
2462 bi = sample__resolve_bstack(sample, al);
2464 struct addr_map_symbol *prev = NULL;
2467 * Ignore errors, still want to process the
2470 * For non standard branch modes always
2471 * force no IPC (prev == NULL)
2473 * Note that perf stores branches reversed from
2476 for (i = bs->nr - 1; i >= 0; i--) {
2477 addr_map_symbol__account_cycles(&bi[i].from,
2478 nonany_branch_mode ? NULL : prev,
2479 bi[i].flags.cycles);
2487 size_t perf_evlist__fprintf_nr_events(struct perf_evlist *evlist, FILE *fp)
2489 struct perf_evsel *pos;
2492 evlist__for_each_entry(evlist, pos) {
2493 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2494 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2501 u64 hists__total_period(struct hists *hists)
2503 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2504 hists->stats.total_period;
2507 int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq)
2511 const struct dso *dso = hists->dso_filter;
2512 const struct thread *thread = hists->thread_filter;
2513 int socket_id = hists->socket_filter;
2514 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
2515 u64 nr_events = hists->stats.total_period;
2516 struct perf_evsel *evsel = hists_to_evsel(hists);
2517 const char *ev_name = perf_evsel__name(evsel);
2518 char buf[512], sample_freq_str[64] = "";
2519 size_t buflen = sizeof(buf);
2520 char ref[30] = " show reference callgraph, ";
2521 bool enable_ref = false;
2523 if (symbol_conf.filter_relative) {
2524 nr_samples = hists->stats.nr_non_filtered_samples;
2525 nr_events = hists->stats.total_non_filtered_period;
2528 if (perf_evsel__is_group_event(evsel)) {
2529 struct perf_evsel *pos;
2531 perf_evsel__group_desc(evsel, buf, buflen);
2534 for_each_group_member(pos, evsel) {
2535 struct hists *pos_hists = evsel__hists(pos);
2537 if (symbol_conf.filter_relative) {
2538 nr_samples += pos_hists->stats.nr_non_filtered_samples;
2539 nr_events += pos_hists->stats.total_non_filtered_period;
2541 nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
2542 nr_events += pos_hists->stats.total_period;
2547 if (symbol_conf.show_ref_callgraph &&
2548 strstr(ev_name, "call-graph=no"))
2552 scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->attr.sample_freq);
2554 nr_samples = convert_unit(nr_samples, &unit);
2555 printed = scnprintf(bf, size,
2556 "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64,
2557 nr_samples, unit, evsel->nr_members > 1 ? "s" : "",
2558 ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events);
2561 if (hists->uid_filter_str)
2562 printed += snprintf(bf + printed, size - printed,
2563 ", UID: %s", hists->uid_filter_str);
2565 if (hists__has(hists, thread)) {
2566 printed += scnprintf(bf + printed, size - printed,
2568 (thread->comm_set ? thread__comm_str(thread) : ""),
2571 printed += scnprintf(bf + printed, size - printed,
2573 (thread->comm_set ? thread__comm_str(thread) : ""));
2577 printed += scnprintf(bf + printed, size - printed,
2578 ", DSO: %s", dso->short_name);
2580 printed += scnprintf(bf + printed, size - printed,
2581 ", Processor Socket: %d", socket_id);
2586 int parse_filter_percentage(const struct option *opt __maybe_unused,
2587 const char *arg, int unset __maybe_unused)
2589 if (!strcmp(arg, "relative"))
2590 symbol_conf.filter_relative = true;
2591 else if (!strcmp(arg, "absolute"))
2592 symbol_conf.filter_relative = false;
2594 pr_debug("Invalid percentage: %s\n", arg);
2601 int perf_hist_config(const char *var, const char *value)
2603 if (!strcmp(var, "hist.percentage"))
2604 return parse_filter_percentage(NULL, value, 0);
2609 int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
2611 memset(hists, 0, sizeof(*hists));
2612 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT_CACHED;
2613 hists->entries_in = &hists->entries_in_array[0];
2614 hists->entries_collapsed = RB_ROOT_CACHED;
2615 hists->entries = RB_ROOT_CACHED;
2616 pthread_mutex_init(&hists->lock, NULL);
2617 hists->socket_filter = -1;
2618 hists->hpp_list = hpp_list;
2619 INIT_LIST_HEAD(&hists->hpp_formats);
2623 static void hists__delete_remaining_entries(struct rb_root_cached *root)
2625 struct rb_node *node;
2626 struct hist_entry *he;
2628 while (!RB_EMPTY_ROOT(&root->rb_root)) {
2629 node = rb_first_cached(root);
2630 rb_erase_cached(node, root);
2632 he = rb_entry(node, struct hist_entry, rb_node_in);
2633 hist_entry__delete(he);
2637 static void hists__delete_all_entries(struct hists *hists)
2639 hists__delete_entries(hists);
2640 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2641 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2642 hists__delete_remaining_entries(&hists->entries_collapsed);
2645 static void hists_evsel__exit(struct perf_evsel *evsel)
2647 struct hists *hists = evsel__hists(evsel);
2648 struct perf_hpp_fmt *fmt, *pos;
2649 struct perf_hpp_list_node *node, *tmp;
2651 hists__delete_all_entries(hists);
2653 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2654 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2655 list_del(&fmt->list);
2658 list_del(&node->list);
2663 static int hists_evsel__init(struct perf_evsel *evsel)
2665 struct hists *hists = evsel__hists(evsel);
2667 __hists__init(hists, &perf_hpp_list);
2672 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2673 * stored in the rbtree...
2676 int hists__init(void)
2678 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
2682 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2687 void perf_hpp_list__init(struct perf_hpp_list *list)
2689 INIT_LIST_HEAD(&list->fields);
2690 INIT_LIST_HEAD(&list->sorts);