1 // SPDX-License-Identifier: GPL-2.0
8 #include "map_symbol.h"
10 #include "mem-events.h"
12 #include "namespaces.h"
22 #include "block-info.h"
23 #include "ui/progress.h"
27 #include <sys/param.h>
28 #include <linux/rbtree.h>
29 #include <linux/string.h>
30 #include <linux/time64.h>
31 #include <linux/zalloc.h>
33 static bool hists__filter_entry_by_dso(struct hists *hists,
34 struct hist_entry *he);
35 static bool hists__filter_entry_by_thread(struct hists *hists,
36 struct hist_entry *he);
37 static bool hists__filter_entry_by_symbol(struct hists *hists,
38 struct hist_entry *he);
39 static bool hists__filter_entry_by_socket(struct hists *hists,
40 struct hist_entry *he);
42 u16 hists__col_len(struct hists *hists, enum hist_column col)
44 return hists->col_len[col];
47 void hists__set_col_len(struct hists *hists, enum hist_column col, u16 len)
49 hists->col_len[col] = len;
52 bool hists__new_col_len(struct hists *hists, enum hist_column col, u16 len)
54 if (len > hists__col_len(hists, col)) {
55 hists__set_col_len(hists, col, len);
61 void hists__reset_col_len(struct hists *hists)
65 for (col = 0; col < HISTC_NR_COLS; ++col)
66 hists__set_col_len(hists, col, 0);
69 static void hists__set_unres_dso_col_len(struct hists *hists, int dso)
71 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
73 if (hists__col_len(hists, dso) < unresolved_col_width &&
74 !symbol_conf.col_width_list_str && !symbol_conf.field_sep &&
75 !symbol_conf.dso_list)
76 hists__set_col_len(hists, dso, unresolved_col_width);
79 void hists__calc_col_len(struct hists *hists, struct hist_entry *h)
81 const unsigned int unresolved_col_width = BITS_PER_LONG / 4;
88 * +4 accounts for '[x] ' priv level info
89 * +2 accounts for 0x prefix on raw addresses
90 * +3 accounts for ' y ' symtab origin info
93 symlen = h->ms.sym->namelen + 4;
95 symlen += BITS_PER_LONG / 4 + 2 + 3;
96 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
98 symlen = unresolved_col_width + 4 + 2;
99 hists__new_col_len(hists, HISTC_SYMBOL, symlen);
100 hists__set_unres_dso_col_len(hists, HISTC_DSO);
103 len = thread__comm_len(h->thread);
104 if (hists__new_col_len(hists, HISTC_COMM, len))
105 hists__set_col_len(hists, HISTC_THREAD, len + 8);
108 len = dso__name_len(h->ms.map->dso);
109 hists__new_col_len(hists, HISTC_DSO, len);
113 hists__new_col_len(hists, HISTC_PARENT, h->parent->namelen);
115 if (h->branch_info) {
116 if (h->branch_info->from.ms.sym) {
117 symlen = (int)h->branch_info->from.ms.sym->namelen + 4;
119 symlen += BITS_PER_LONG / 4 + 2 + 3;
120 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
122 symlen = dso__name_len(h->branch_info->from.ms.map->dso);
123 hists__new_col_len(hists, HISTC_DSO_FROM, symlen);
125 symlen = unresolved_col_width + 4 + 2;
126 hists__new_col_len(hists, HISTC_SYMBOL_FROM, symlen);
127 hists__set_unres_dso_col_len(hists, HISTC_DSO_FROM);
130 if (h->branch_info->to.ms.sym) {
131 symlen = (int)h->branch_info->to.ms.sym->namelen + 4;
133 symlen += BITS_PER_LONG / 4 + 2 + 3;
134 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
136 symlen = dso__name_len(h->branch_info->to.ms.map->dso);
137 hists__new_col_len(hists, HISTC_DSO_TO, symlen);
139 symlen = unresolved_col_width + 4 + 2;
140 hists__new_col_len(hists, HISTC_SYMBOL_TO, symlen);
141 hists__set_unres_dso_col_len(hists, HISTC_DSO_TO);
144 if (h->branch_info->srcline_from)
145 hists__new_col_len(hists, HISTC_SRCLINE_FROM,
146 strlen(h->branch_info->srcline_from));
147 if (h->branch_info->srcline_to)
148 hists__new_col_len(hists, HISTC_SRCLINE_TO,
149 strlen(h->branch_info->srcline_to));
153 if (h->mem_info->daddr.ms.sym) {
154 symlen = (int)h->mem_info->daddr.ms.sym->namelen + 4
155 + unresolved_col_width + 2;
156 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
158 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
161 symlen = unresolved_col_width + 4 + 2;
162 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL,
164 hists__new_col_len(hists, HISTC_MEM_DCACHELINE,
168 if (h->mem_info->iaddr.ms.sym) {
169 symlen = (int)h->mem_info->iaddr.ms.sym->namelen + 4
170 + unresolved_col_width + 2;
171 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
174 symlen = unresolved_col_width + 4 + 2;
175 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL,
179 if (h->mem_info->daddr.ms.map) {
180 symlen = dso__name_len(h->mem_info->daddr.ms.map->dso);
181 hists__new_col_len(hists, HISTC_MEM_DADDR_DSO,
184 symlen = unresolved_col_width + 4 + 2;
185 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
188 hists__new_col_len(hists, HISTC_MEM_PHYS_DADDR,
189 unresolved_col_width + 4 + 2);
192 symlen = unresolved_col_width + 4 + 2;
193 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
194 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
195 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
198 hists__new_col_len(hists, HISTC_CGROUP, 6);
199 hists__new_col_len(hists, HISTC_CGROUP_ID, 20);
200 hists__new_col_len(hists, HISTC_CPU, 3);
201 hists__new_col_len(hists, HISTC_SOCKET, 6);
202 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
203 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
204 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
205 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
206 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
207 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
208 if (symbol_conf.nanosecs)
209 hists__new_col_len(hists, HISTC_TIME, 16);
211 hists__new_col_len(hists, HISTC_TIME, 12);
214 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
215 hists__new_col_len(hists, HISTC_SRCLINE, len);
219 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
222 hists__new_col_len(hists, HISTC_TRANSACTION,
223 hist_entry__transaction_len());
226 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
229 const char *cgrp_name = "unknown";
230 struct cgroup *cgrp = cgroup__find(h->ms.maps->machine->env,
233 cgrp_name = cgrp->name;
235 hists__new_col_len(hists, HISTC_CGROUP, strlen(cgrp_name));
239 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
241 struct rb_node *next = rb_first_cached(&hists->entries);
242 struct hist_entry *n;
245 hists__reset_col_len(hists);
247 while (next && row++ < max_rows) {
248 n = rb_entry(next, struct hist_entry, rb_node);
250 hists__calc_col_len(hists, n);
251 next = rb_next(&n->rb_node);
255 static void he_stat__add_cpumode_period(struct he_stat *he_stat,
256 unsigned int cpumode, u64 period)
259 case PERF_RECORD_MISC_KERNEL:
260 he_stat->period_sys += period;
262 case PERF_RECORD_MISC_USER:
263 he_stat->period_us += period;
265 case PERF_RECORD_MISC_GUEST_KERNEL:
266 he_stat->period_guest_sys += period;
268 case PERF_RECORD_MISC_GUEST_USER:
269 he_stat->period_guest_us += period;
276 static long hist_time(unsigned long htime)
278 unsigned long time_quantum = symbol_conf.time_quantum;
280 return (htime / time_quantum) * time_quantum;
284 static void he_stat__add_period(struct he_stat *he_stat, u64 period,
288 he_stat->period += period;
289 he_stat->weight += weight;
290 he_stat->nr_events += 1;
293 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
295 dest->period += src->period;
296 dest->period_sys += src->period_sys;
297 dest->period_us += src->period_us;
298 dest->period_guest_sys += src->period_guest_sys;
299 dest->period_guest_us += src->period_guest_us;
300 dest->nr_events += src->nr_events;
301 dest->weight += src->weight;
304 static void he_stat__decay(struct he_stat *he_stat)
306 he_stat->period = (he_stat->period * 7) / 8;
307 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
308 /* XXX need decay for weight too? */
311 static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
313 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
315 u64 prev_period = he->stat.period;
318 if (prev_period == 0)
321 he_stat__decay(&he->stat);
322 if (symbol_conf.cumulate_callchain)
323 he_stat__decay(he->stat_acc);
324 decay_callchain(he->callchain);
326 diff = prev_period - he->stat.period;
329 hists->stats.total_period -= diff;
331 hists->stats.total_non_filtered_period -= diff;
335 struct hist_entry *child;
336 struct rb_node *node = rb_first_cached(&he->hroot_out);
338 child = rb_entry(node, struct hist_entry, rb_node);
339 node = rb_next(node);
341 if (hists__decay_entry(hists, child))
342 hists__delete_entry(hists, child);
346 return he->stat.period == 0;
349 static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
351 struct rb_root_cached *root_in;
352 struct rb_root_cached *root_out;
355 root_in = &he->parent_he->hroot_in;
356 root_out = &he->parent_he->hroot_out;
358 if (hists__has(hists, need_collapse))
359 root_in = &hists->entries_collapsed;
361 root_in = hists->entries_in;
362 root_out = &hists->entries;
365 rb_erase_cached(&he->rb_node_in, root_in);
366 rb_erase_cached(&he->rb_node, root_out);
370 --hists->nr_non_filtered_entries;
372 hist_entry__delete(he);
375 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
377 struct rb_node *next = rb_first_cached(&hists->entries);
378 struct hist_entry *n;
381 n = rb_entry(next, struct hist_entry, rb_node);
382 next = rb_next(&n->rb_node);
383 if (((zap_user && n->level == '.') ||
384 (zap_kernel && n->level != '.') ||
385 hists__decay_entry(hists, n))) {
386 hists__delete_entry(hists, n);
391 void hists__delete_entries(struct hists *hists)
393 struct rb_node *next = rb_first_cached(&hists->entries);
394 struct hist_entry *n;
397 n = rb_entry(next, struct hist_entry, rb_node);
398 next = rb_next(&n->rb_node);
400 hists__delete_entry(hists, n);
404 struct hist_entry *hists__get_entry(struct hists *hists, int idx)
406 struct rb_node *next = rb_first_cached(&hists->entries);
407 struct hist_entry *n;
411 n = rb_entry(next, struct hist_entry, rb_node);
415 next = rb_next(&n->rb_node);
423 * histogram, sorted on item, collects periods
426 static int hist_entry__init(struct hist_entry *he,
427 struct hist_entry *template,
429 size_t callchain_size)
432 he->callchain_size = callchain_size;
434 if (symbol_conf.cumulate_callchain) {
435 he->stat_acc = malloc(sizeof(he->stat));
436 if (he->stat_acc == NULL)
438 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
440 memset(&he->stat, 0, sizeof(he->stat));
443 map__get(he->ms.map);
445 if (he->branch_info) {
447 * This branch info is (a part of) allocated from
448 * sample__resolve_bstack() and will be freed after
449 * adding new entries. So we need to save a copy.
451 he->branch_info = malloc(sizeof(*he->branch_info));
452 if (he->branch_info == NULL)
455 memcpy(he->branch_info, template->branch_info,
456 sizeof(*he->branch_info));
458 map__get(he->branch_info->from.ms.map);
459 map__get(he->branch_info->to.ms.map);
463 map__get(he->mem_info->iaddr.ms.map);
464 map__get(he->mem_info->daddr.ms.map);
467 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
468 callchain_init(he->callchain);
471 he->raw_data = memdup(he->raw_data, he->raw_size);
472 if (he->raw_data == NULL)
477 he->srcline = strdup(he->srcline);
478 if (he->srcline == NULL)
482 if (symbol_conf.res_sample) {
483 he->res_samples = calloc(sizeof(struct res_sample),
484 symbol_conf.res_sample);
485 if (!he->res_samples)
489 INIT_LIST_HEAD(&he->pairs.node);
490 thread__get(he->thread);
491 he->hroot_in = RB_ROOT_CACHED;
492 he->hroot_out = RB_ROOT_CACHED;
494 if (!symbol_conf.report_hierarchy)
503 zfree(&he->raw_data);
506 if (he->branch_info) {
507 map__put(he->branch_info->from.ms.map);
508 map__put(he->branch_info->to.ms.map);
509 zfree(&he->branch_info);
512 map__put(he->mem_info->iaddr.ms.map);
513 map__put(he->mem_info->daddr.ms.map);
516 map__zput(he->ms.map);
517 zfree(&he->stat_acc);
521 static void *hist_entry__zalloc(size_t size)
523 return zalloc(size + sizeof(struct hist_entry));
526 static void hist_entry__free(void *ptr)
531 static struct hist_entry_ops default_ops = {
532 .new = hist_entry__zalloc,
533 .free = hist_entry__free,
536 static struct hist_entry *hist_entry__new(struct hist_entry *template,
539 struct hist_entry_ops *ops = template->ops;
540 size_t callchain_size = 0;
541 struct hist_entry *he;
545 ops = template->ops = &default_ops;
547 if (symbol_conf.use_callchain)
548 callchain_size = sizeof(struct callchain_root);
550 he = ops->new(callchain_size);
552 err = hist_entry__init(he, template, sample_self, callchain_size);
562 static u8 symbol__parent_filter(const struct symbol *parent)
564 if (symbol_conf.exclude_other && parent == NULL)
565 return 1 << HIST_FILTER__PARENT;
569 static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
571 if (!hist_entry__has_callchains(he) || !symbol_conf.use_callchain)
574 he->hists->callchain_period += period;
576 he->hists->callchain_non_filtered_period += period;
579 static struct hist_entry *hists__findnew_entry(struct hists *hists,
580 struct hist_entry *entry,
581 struct addr_location *al,
585 struct rb_node *parent = NULL;
586 struct hist_entry *he;
588 u64 period = entry->stat.period;
589 u64 weight = entry->stat.weight;
590 bool leftmost = true;
592 p = &hists->entries_in->rb_root.rb_node;
596 he = rb_entry(parent, struct hist_entry, rb_node_in);
599 * Make sure that it receives arguments in a same order as
600 * hist_entry__collapse() so that we can use an appropriate
601 * function when searching an entry regardless which sort
604 cmp = hist_entry__cmp(he, entry);
608 he_stat__add_period(&he->stat, period, weight);
609 hist_entry__add_callchain_period(he, period);
611 if (symbol_conf.cumulate_callchain)
612 he_stat__add_period(he->stat_acc, period, weight);
615 * This mem info was allocated from sample__resolve_mem
616 * and will not be used anymore.
618 mem_info__zput(entry->mem_info);
620 block_info__zput(entry->block_info);
622 /* If the map of an existing hist_entry has
623 * become out-of-date due to an exec() or
624 * similar, update it. Otherwise we will
625 * mis-adjust symbol addresses when computing
626 * the history counter to increment.
628 if (he->ms.map != entry->ms.map) {
629 map__put(he->ms.map);
630 he->ms.map = map__get(entry->ms.map);
643 he = hist_entry__new(entry, sample_self);
648 hist_entry__add_callchain_period(he, period);
651 rb_link_node(&he->rb_node_in, parent, p);
652 rb_insert_color_cached(&he->rb_node_in, hists->entries_in, leftmost);
655 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
656 if (symbol_conf.cumulate_callchain)
657 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
661 static unsigned random_max(unsigned high)
663 unsigned thresh = -high % high;
665 unsigned r = random();
671 static void hists__res_sample(struct hist_entry *he, struct perf_sample *sample)
673 struct res_sample *r;
676 if (he->num_res < symbol_conf.res_sample) {
679 j = random_max(symbol_conf.res_sample);
681 r = &he->res_samples[j];
682 r->time = sample->time;
683 r->cpu = sample->cpu;
684 r->tid = sample->tid;
687 static struct hist_entry*
688 __hists__add_entry(struct hists *hists,
689 struct addr_location *al,
690 struct symbol *sym_parent,
691 struct branch_info *bi,
693 struct block_info *block_info,
694 struct perf_sample *sample,
696 struct hist_entry_ops *ops)
698 struct namespaces *ns = thread__namespaces(al->thread);
699 struct hist_entry entry = {
700 .thread = al->thread,
701 .comm = thread__comm(al->thread),
703 .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0,
704 .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0,
706 .cgroup = sample->cgroup,
712 .srcline = (char *) al->srcline,
713 .socket = al->socket,
715 .cpumode = al->cpumode,
720 .period = sample->period,
721 .weight = sample->weight,
723 .parent = sym_parent,
724 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
728 .block_info = block_info,
729 .transaction = sample->transaction,
730 .raw_data = sample->raw_data,
731 .raw_size = sample->raw_size,
733 .time = hist_time(sample->time),
734 }, *he = hists__findnew_entry(hists, &entry, al, sample_self);
736 if (!hists->has_callchains && he && he->callchain_size != 0)
737 hists->has_callchains = true;
738 if (he && symbol_conf.res_sample)
739 hists__res_sample(he, sample);
743 struct hist_entry *hists__add_entry(struct hists *hists,
744 struct addr_location *al,
745 struct symbol *sym_parent,
746 struct branch_info *bi,
748 struct perf_sample *sample,
751 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
752 sample, sample_self, NULL);
755 struct hist_entry *hists__add_entry_ops(struct hists *hists,
756 struct hist_entry_ops *ops,
757 struct addr_location *al,
758 struct symbol *sym_parent,
759 struct branch_info *bi,
761 struct perf_sample *sample,
764 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
765 sample, sample_self, ops);
768 struct hist_entry *hists__add_entry_block(struct hists *hists,
769 struct addr_location *al,
770 struct block_info *block_info)
772 struct hist_entry entry = {
773 .block_info = block_info,
780 }, *he = hists__findnew_entry(hists, &entry, al, false);
786 iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
787 struct addr_location *al __maybe_unused)
793 iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
794 struct addr_location *al __maybe_unused)
800 iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
802 struct perf_sample *sample = iter->sample;
805 mi = sample__resolve_mem(sample, al);
814 iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
817 struct mem_info *mi = iter->priv;
818 struct hists *hists = evsel__hists(iter->evsel);
819 struct perf_sample *sample = iter->sample;
820 struct hist_entry *he;
825 cost = sample->weight;
830 * must pass period=weight in order to get the correct
831 * sorting from hists__collapse_resort() which is solely
832 * based on periods. We want sorting be done on nr_events * weight
833 * and this is indirectly achieved by passing period=weight here
834 * and the he_stat__add_period() function.
836 sample->period = cost;
838 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
848 iter_finish_mem_entry(struct hist_entry_iter *iter,
849 struct addr_location *al __maybe_unused)
851 struct evsel *evsel = iter->evsel;
852 struct hists *hists = evsel__hists(evsel);
853 struct hist_entry *he = iter->he;
859 hists__inc_nr_samples(hists, he->filtered);
861 err = hist_entry__append_callchain(he, iter->sample);
865 * We don't need to free iter->priv (mem_info) here since the mem info
866 * was either already freed in hists__findnew_entry() or passed to a
867 * new hist entry by hist_entry__new().
876 iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
878 struct branch_info *bi;
879 struct perf_sample *sample = iter->sample;
881 bi = sample__resolve_bstack(sample, al);
886 iter->total = sample->branch_stack->nr;
893 iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
894 struct addr_location *al __maybe_unused)
900 iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
902 struct branch_info *bi = iter->priv;
908 if (iter->curr >= iter->total)
911 al->maps = bi[i].to.ms.maps;
912 al->map = bi[i].to.ms.map;
913 al->sym = bi[i].to.ms.sym;
914 al->addr = bi[i].to.addr;
919 iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
921 struct branch_info *bi;
922 struct evsel *evsel = iter->evsel;
923 struct hists *hists = evsel__hists(evsel);
924 struct perf_sample *sample = iter->sample;
925 struct hist_entry *he = NULL;
931 if (iter->hide_unresolved && !(bi[i].from.ms.sym && bi[i].to.ms.sym))
935 * The report shows the percentage of total branches captured
936 * and not events sampled. Thus we use a pseudo period of 1.
939 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
941 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
946 hists__inc_nr_samples(hists, he->filtered);
955 iter_finish_branch_entry(struct hist_entry_iter *iter,
956 struct addr_location *al __maybe_unused)
961 return iter->curr >= iter->total ? 0 : -1;
965 iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
966 struct addr_location *al __maybe_unused)
972 iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
974 struct evsel *evsel = iter->evsel;
975 struct perf_sample *sample = iter->sample;
976 struct hist_entry *he;
978 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
988 iter_finish_normal_entry(struct hist_entry_iter *iter,
989 struct addr_location *al __maybe_unused)
991 struct hist_entry *he = iter->he;
992 struct evsel *evsel = iter->evsel;
993 struct perf_sample *sample = iter->sample;
1000 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
1002 return hist_entry__append_callchain(he, sample);
1006 iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
1007 struct addr_location *al __maybe_unused)
1009 struct hist_entry **he_cache;
1011 callchain_cursor_commit(&callchain_cursor);
1014 * This is for detecting cycles or recursions so that they're
1015 * cumulated only one time to prevent entries more than 100%
1018 he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1));
1019 if (he_cache == NULL)
1022 iter->priv = he_cache;
1029 iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
1030 struct addr_location *al)
1032 struct evsel *evsel = iter->evsel;
1033 struct hists *hists = evsel__hists(evsel);
1034 struct perf_sample *sample = iter->sample;
1035 struct hist_entry **he_cache = iter->priv;
1036 struct hist_entry *he;
1039 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
1045 he_cache[iter->curr++] = he;
1047 hist_entry__append_callchain(he, sample);
1050 * We need to re-initialize the cursor since callchain_append()
1051 * advanced the cursor to the end.
1053 callchain_cursor_commit(&callchain_cursor);
1055 hists__inc_nr_samples(hists, he->filtered);
1061 iter_next_cumulative_entry(struct hist_entry_iter *iter,
1062 struct addr_location *al)
1064 struct callchain_cursor_node *node;
1066 node = callchain_cursor_current(&callchain_cursor);
1070 return fill_callchain_info(al, node, iter->hide_unresolved);
1074 iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
1075 struct addr_location *al)
1077 struct evsel *evsel = iter->evsel;
1078 struct perf_sample *sample = iter->sample;
1079 struct hist_entry **he_cache = iter->priv;
1080 struct hist_entry *he;
1081 struct hist_entry he_tmp = {
1082 .hists = evsel__hists(evsel),
1084 .thread = al->thread,
1085 .comm = thread__comm(al->thread),
1092 .srcline = (char *) al->srcline,
1093 .parent = iter->parent,
1094 .raw_data = sample->raw_data,
1095 .raw_size = sample->raw_size,
1098 struct callchain_cursor cursor;
1100 callchain_cursor_snapshot(&cursor, &callchain_cursor);
1102 callchain_cursor_advance(&callchain_cursor);
1105 * Check if there's duplicate entries in the callchain.
1106 * It's possible that it has cycles or recursive calls.
1108 for (i = 0; i < iter->curr; i++) {
1109 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
1110 /* to avoid calling callback function */
1116 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
1122 he_cache[iter->curr++] = he;
1124 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
1125 callchain_append(he->callchain, &cursor, sample->period);
1130 iter_finish_cumulative_entry(struct hist_entry_iter *iter,
1131 struct addr_location *al __maybe_unused)
1139 const struct hist_iter_ops hist_iter_mem = {
1140 .prepare_entry = iter_prepare_mem_entry,
1141 .add_single_entry = iter_add_single_mem_entry,
1142 .next_entry = iter_next_nop_entry,
1143 .add_next_entry = iter_add_next_nop_entry,
1144 .finish_entry = iter_finish_mem_entry,
1147 const struct hist_iter_ops hist_iter_branch = {
1148 .prepare_entry = iter_prepare_branch_entry,
1149 .add_single_entry = iter_add_single_branch_entry,
1150 .next_entry = iter_next_branch_entry,
1151 .add_next_entry = iter_add_next_branch_entry,
1152 .finish_entry = iter_finish_branch_entry,
1155 const struct hist_iter_ops hist_iter_normal = {
1156 .prepare_entry = iter_prepare_normal_entry,
1157 .add_single_entry = iter_add_single_normal_entry,
1158 .next_entry = iter_next_nop_entry,
1159 .add_next_entry = iter_add_next_nop_entry,
1160 .finish_entry = iter_finish_normal_entry,
1163 const struct hist_iter_ops hist_iter_cumulative = {
1164 .prepare_entry = iter_prepare_cumulative_entry,
1165 .add_single_entry = iter_add_single_cumulative_entry,
1166 .next_entry = iter_next_cumulative_entry,
1167 .add_next_entry = iter_add_next_cumulative_entry,
1168 .finish_entry = iter_finish_cumulative_entry,
1171 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
1172 int max_stack_depth, void *arg)
1175 struct map *alm = NULL;
1178 alm = map__get(al->map);
1180 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
1181 iter->evsel, al, max_stack_depth);
1187 err = iter->ops->prepare_entry(iter, al);
1191 err = iter->ops->add_single_entry(iter, al);
1195 if (iter->he && iter->add_entry_cb) {
1196 err = iter->add_entry_cb(iter, al, true, arg);
1201 while (iter->ops->next_entry(iter, al)) {
1202 err = iter->ops->add_next_entry(iter, al);
1206 if (iter->he && iter->add_entry_cb) {
1207 err = iter->add_entry_cb(iter, al, false, arg);
1214 err2 = iter->ops->finish_entry(iter, al);
1224 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1226 struct hists *hists = left->hists;
1227 struct perf_hpp_fmt *fmt;
1230 hists__for_each_sort_list(hists, fmt) {
1231 if (perf_hpp__is_dynamic_entry(fmt) &&
1232 !perf_hpp__defined_dynamic_entry(fmt, hists))
1235 cmp = fmt->cmp(fmt, left, right);
1244 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1246 struct hists *hists = left->hists;
1247 struct perf_hpp_fmt *fmt;
1250 hists__for_each_sort_list(hists, fmt) {
1251 if (perf_hpp__is_dynamic_entry(fmt) &&
1252 !perf_hpp__defined_dynamic_entry(fmt, hists))
1255 cmp = fmt->collapse(fmt, left, right);
1263 void hist_entry__delete(struct hist_entry *he)
1265 struct hist_entry_ops *ops = he->ops;
1267 thread__zput(he->thread);
1268 map__zput(he->ms.map);
1270 if (he->branch_info) {
1271 map__zput(he->branch_info->from.ms.map);
1272 map__zput(he->branch_info->to.ms.map);
1273 free_srcline(he->branch_info->srcline_from);
1274 free_srcline(he->branch_info->srcline_to);
1275 zfree(&he->branch_info);
1279 map__zput(he->mem_info->iaddr.ms.map);
1280 map__zput(he->mem_info->daddr.ms.map);
1281 mem_info__zput(he->mem_info);
1285 block_info__zput(he->block_info);
1287 zfree(&he->res_samples);
1288 zfree(&he->stat_acc);
1289 free_srcline(he->srcline);
1290 if (he->srcfile && he->srcfile[0])
1291 zfree(&he->srcfile);
1292 free_callchain(he->callchain);
1293 zfree(&he->trace_output);
1294 zfree(&he->raw_data);
1299 * If this is not the last column, then we need to pad it according to the
1300 * pre-calculated max length for this column, otherwise don't bother adding
1301 * spaces because that would break viewing this with, for instance, 'less',
1302 * that would show tons of trailing spaces when a long C++ demangled method
1305 int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1306 struct perf_hpp_fmt *fmt, int printed)
1308 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
1309 const int width = fmt->width(fmt, hpp, he->hists);
1310 if (printed < width) {
1311 advance_hpp(hpp, printed);
1312 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1320 * collapse the histogram
1323 static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
1324 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1325 enum hist_filter type);
1327 typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1329 static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1331 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1334 static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1335 enum hist_filter type,
1338 struct perf_hpp_fmt *fmt;
1339 bool type_match = false;
1340 struct hist_entry *parent = he->parent_he;
1343 case HIST_FILTER__THREAD:
1344 if (symbol_conf.comm_list == NULL &&
1345 symbol_conf.pid_list == NULL &&
1346 symbol_conf.tid_list == NULL)
1349 case HIST_FILTER__DSO:
1350 if (symbol_conf.dso_list == NULL)
1353 case HIST_FILTER__SYMBOL:
1354 if (symbol_conf.sym_list == NULL)
1357 case HIST_FILTER__PARENT:
1358 case HIST_FILTER__GUEST:
1359 case HIST_FILTER__HOST:
1360 case HIST_FILTER__SOCKET:
1361 case HIST_FILTER__C2C:
1366 /* if it's filtered by own fmt, it has to have filter bits */
1367 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1376 * If the filter is for current level entry, propagate
1377 * filter marker to parents. The marker bit was
1378 * already set by default so it only needs to clear
1379 * non-filtered entries.
1381 if (!(he->filtered & (1 << type))) {
1383 parent->filtered &= ~(1 << type);
1384 parent = parent->parent_he;
1389 * If current entry doesn't have matching formats, set
1390 * filter marker for upper level entries. it will be
1391 * cleared if its lower level entries is not filtered.
1393 * For lower-level entries, it inherits parent's
1394 * filter bit so that lower level entries of a
1395 * non-filtered entry won't set the filter marker.
1398 he->filtered |= (1 << type);
1400 he->filtered |= (parent->filtered & (1 << type));
1404 static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1406 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1407 check_thread_entry);
1409 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1410 perf_hpp__is_dso_entry);
1412 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1413 perf_hpp__is_sym_entry);
1415 hists__apply_filters(he->hists, he);
1418 static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1419 struct rb_root_cached *root,
1420 struct hist_entry *he,
1421 struct hist_entry *parent_he,
1422 struct perf_hpp_list *hpp_list)
1424 struct rb_node **p = &root->rb_root.rb_node;
1425 struct rb_node *parent = NULL;
1426 struct hist_entry *iter, *new;
1427 struct perf_hpp_fmt *fmt;
1429 bool leftmost = true;
1431 while (*p != NULL) {
1433 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1436 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1437 cmp = fmt->collapse(fmt, iter, he);
1443 he_stat__add_stat(&iter->stat, &he->stat);
1448 p = &parent->rb_left;
1450 p = &parent->rb_right;
1455 new = hist_entry__new(he, true);
1459 hists->nr_entries++;
1461 /* save related format list for output */
1462 new->hpp_list = hpp_list;
1463 new->parent_he = parent_he;
1465 hist_entry__apply_hierarchy_filters(new);
1467 /* some fields are now passed to 'new' */
1468 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1469 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1470 he->trace_output = NULL;
1472 new->trace_output = NULL;
1474 if (perf_hpp__is_srcline_entry(fmt))
1477 new->srcline = NULL;
1479 if (perf_hpp__is_srcfile_entry(fmt))
1482 new->srcfile = NULL;
1485 rb_link_node(&new->rb_node_in, parent, p);
1486 rb_insert_color_cached(&new->rb_node_in, root, leftmost);
1490 static int hists__hierarchy_insert_entry(struct hists *hists,
1491 struct rb_root_cached *root,
1492 struct hist_entry *he)
1494 struct perf_hpp_list_node *node;
1495 struct hist_entry *new_he = NULL;
1496 struct hist_entry *parent = NULL;
1500 list_for_each_entry(node, &hists->hpp_formats, list) {
1501 /* skip period (overhead) and elided columns */
1502 if (node->level == 0 || node->skip)
1505 /* insert copy of 'he' for each fmt into the hierarchy */
1506 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
1507 if (new_he == NULL) {
1512 root = &new_he->hroot_in;
1513 new_he->depth = depth++;
1518 new_he->leaf = true;
1520 if (hist_entry__has_callchains(new_he) &&
1521 symbol_conf.use_callchain) {
1522 callchain_cursor_reset(&callchain_cursor);
1523 if (callchain_merge(&callchain_cursor,
1530 /* 'he' is no longer used */
1531 hist_entry__delete(he);
1533 /* return 0 (or -1) since it already applied filters */
1537 static int hists__collapse_insert_entry(struct hists *hists,
1538 struct rb_root_cached *root,
1539 struct hist_entry *he)
1541 struct rb_node **p = &root->rb_root.rb_node;
1542 struct rb_node *parent = NULL;
1543 struct hist_entry *iter;
1545 bool leftmost = true;
1547 if (symbol_conf.report_hierarchy)
1548 return hists__hierarchy_insert_entry(hists, root, he);
1550 while (*p != NULL) {
1552 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1554 cmp = hist_entry__collapse(iter, he);
1559 he_stat__add_stat(&iter->stat, &he->stat);
1560 if (symbol_conf.cumulate_callchain)
1561 he_stat__add_stat(iter->stat_acc, he->stat_acc);
1563 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
1564 callchain_cursor_reset(&callchain_cursor);
1565 if (callchain_merge(&callchain_cursor,
1570 hist_entry__delete(he);
1577 p = &(*p)->rb_right;
1581 hists->nr_entries++;
1583 rb_link_node(&he->rb_node_in, parent, p);
1584 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
1588 struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists)
1590 struct rb_root_cached *root;
1592 pthread_mutex_lock(&hists->lock);
1594 root = hists->entries_in;
1595 if (++hists->entries_in > &hists->entries_in_array[1])
1596 hists->entries_in = &hists->entries_in_array[0];
1598 pthread_mutex_unlock(&hists->lock);
1603 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1605 hists__filter_entry_by_dso(hists, he);
1606 hists__filter_entry_by_thread(hists, he);
1607 hists__filter_entry_by_symbol(hists, he);
1608 hists__filter_entry_by_socket(hists, he);
1611 int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
1613 struct rb_root_cached *root;
1614 struct rb_node *next;
1615 struct hist_entry *n;
1618 if (!hists__has(hists, need_collapse))
1621 hists->nr_entries = 0;
1623 root = hists__get_rotate_entries_in(hists);
1625 next = rb_first_cached(root);
1630 n = rb_entry(next, struct hist_entry, rb_node_in);
1631 next = rb_next(&n->rb_node_in);
1633 rb_erase_cached(&n->rb_node_in, root);
1634 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1640 * If it wasn't combined with one of the entries already
1641 * collapsed, we need to apply the filters that may have
1642 * been set by, say, the hist_browser.
1644 hists__apply_filters(hists, n);
1647 ui_progress__update(prog, 1);
1652 static int64_t hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
1654 struct hists *hists = a->hists;
1655 struct perf_hpp_fmt *fmt;
1658 hists__for_each_sort_list(hists, fmt) {
1659 if (perf_hpp__should_skip(fmt, a->hists))
1662 cmp = fmt->sort(fmt, a, b);
1670 static void hists__reset_filter_stats(struct hists *hists)
1672 hists->nr_non_filtered_entries = 0;
1673 hists->stats.total_non_filtered_period = 0;
1676 void hists__reset_stats(struct hists *hists)
1678 hists->nr_entries = 0;
1679 hists->stats.total_period = 0;
1681 hists__reset_filter_stats(hists);
1684 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1686 hists->nr_non_filtered_entries++;
1687 hists->stats.total_non_filtered_period += h->stat.period;
1690 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1693 hists__inc_filter_stats(hists, h);
1695 hists->nr_entries++;
1696 hists->stats.total_period += h->stat.period;
1699 static void hierarchy_recalc_total_periods(struct hists *hists)
1701 struct rb_node *node;
1702 struct hist_entry *he;
1704 node = rb_first_cached(&hists->entries);
1706 hists->stats.total_period = 0;
1707 hists->stats.total_non_filtered_period = 0;
1710 * recalculate total period using top-level entries only
1711 * since lower level entries only see non-filtered entries
1712 * but upper level entries have sum of both entries.
1715 he = rb_entry(node, struct hist_entry, rb_node);
1716 node = rb_next(node);
1718 hists->stats.total_period += he->stat.period;
1720 hists->stats.total_non_filtered_period += he->stat.period;
1724 static void hierarchy_insert_output_entry(struct rb_root_cached *root,
1725 struct hist_entry *he)
1727 struct rb_node **p = &root->rb_root.rb_node;
1728 struct rb_node *parent = NULL;
1729 struct hist_entry *iter;
1730 struct perf_hpp_fmt *fmt;
1731 bool leftmost = true;
1733 while (*p != NULL) {
1735 iter = rb_entry(parent, struct hist_entry, rb_node);
1737 if (hist_entry__sort(he, iter) > 0)
1738 p = &parent->rb_left;
1740 p = &parent->rb_right;
1745 rb_link_node(&he->rb_node, parent, p);
1746 rb_insert_color_cached(&he->rb_node, root, leftmost);
1748 /* update column width of dynamic entry */
1749 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1750 if (perf_hpp__is_dynamic_entry(fmt))
1751 fmt->sort(fmt, he, NULL);
1755 static void hists__hierarchy_output_resort(struct hists *hists,
1756 struct ui_progress *prog,
1757 struct rb_root_cached *root_in,
1758 struct rb_root_cached *root_out,
1759 u64 min_callchain_hits,
1762 struct rb_node *node;
1763 struct hist_entry *he;
1765 *root_out = RB_ROOT_CACHED;
1766 node = rb_first_cached(root_in);
1769 he = rb_entry(node, struct hist_entry, rb_node_in);
1770 node = rb_next(node);
1772 hierarchy_insert_output_entry(root_out, he);
1775 ui_progress__update(prog, 1);
1777 hists->nr_entries++;
1778 if (!he->filtered) {
1779 hists->nr_non_filtered_entries++;
1780 hists__calc_col_len(hists, he);
1784 hists__hierarchy_output_resort(hists, prog,
1795 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1796 u64 total = he->stat.period;
1798 if (symbol_conf.cumulate_callchain)
1799 total = he->stat_acc->period;
1801 min_callchain_hits = total * (callchain_param.min_percent / 100);
1804 callchain_param.sort(&he->sorted_chain, he->callchain,
1805 min_callchain_hits, &callchain_param);
1809 static void __hists__insert_output_entry(struct rb_root_cached *entries,
1810 struct hist_entry *he,
1811 u64 min_callchain_hits,
1814 struct rb_node **p = &entries->rb_root.rb_node;
1815 struct rb_node *parent = NULL;
1816 struct hist_entry *iter;
1817 struct perf_hpp_fmt *fmt;
1818 bool leftmost = true;
1820 if (use_callchain) {
1821 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1822 u64 total = he->stat.period;
1824 if (symbol_conf.cumulate_callchain)
1825 total = he->stat_acc->period;
1827 min_callchain_hits = total * (callchain_param.min_percent / 100);
1829 callchain_param.sort(&he->sorted_chain, he->callchain,
1830 min_callchain_hits, &callchain_param);
1833 while (*p != NULL) {
1835 iter = rb_entry(parent, struct hist_entry, rb_node);
1837 if (hist_entry__sort(he, iter) > 0)
1840 p = &(*p)->rb_right;
1845 rb_link_node(&he->rb_node, parent, p);
1846 rb_insert_color_cached(&he->rb_node, entries, leftmost);
1848 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1849 if (perf_hpp__is_dynamic_entry(fmt) &&
1850 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1851 fmt->sort(fmt, he, NULL); /* update column width */
1855 static void output_resort(struct hists *hists, struct ui_progress *prog,
1856 bool use_callchain, hists__resort_cb_t cb,
1859 struct rb_root_cached *root;
1860 struct rb_node *next;
1861 struct hist_entry *n;
1862 u64 callchain_total;
1863 u64 min_callchain_hits;
1865 callchain_total = hists->callchain_period;
1866 if (symbol_conf.filter_relative)
1867 callchain_total = hists->callchain_non_filtered_period;
1869 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
1871 hists__reset_stats(hists);
1872 hists__reset_col_len(hists);
1874 if (symbol_conf.report_hierarchy) {
1875 hists__hierarchy_output_resort(hists, prog,
1876 &hists->entries_collapsed,
1880 hierarchy_recalc_total_periods(hists);
1884 if (hists__has(hists, need_collapse))
1885 root = &hists->entries_collapsed;
1887 root = hists->entries_in;
1889 next = rb_first_cached(root);
1890 hists->entries = RB_ROOT_CACHED;
1893 n = rb_entry(next, struct hist_entry, rb_node_in);
1894 next = rb_next(&n->rb_node_in);
1896 if (cb && cb(n, cb_arg))
1899 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
1900 hists__inc_stats(hists, n);
1903 hists__calc_col_len(hists, n);
1906 ui_progress__update(prog, 1);
1910 void perf_evsel__output_resort_cb(struct evsel *evsel, struct ui_progress *prog,
1911 hists__resort_cb_t cb, void *cb_arg)
1915 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1916 use_callchain = evsel__has_callchain(evsel);
1918 use_callchain = symbol_conf.use_callchain;
1920 use_callchain |= symbol_conf.show_branchflag_count;
1922 output_resort(evsel__hists(evsel), prog, use_callchain, cb, cb_arg);
1925 void perf_evsel__output_resort(struct evsel *evsel, struct ui_progress *prog)
1927 return perf_evsel__output_resort_cb(evsel, prog, NULL, NULL);
1930 void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1932 output_resort(hists, prog, symbol_conf.use_callchain, NULL, NULL);
1935 void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1936 hists__resort_cb_t cb)
1938 output_resort(hists, prog, symbol_conf.use_callchain, cb, NULL);
1941 static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1943 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1946 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1952 struct rb_node *rb_hierarchy_last(struct rb_node *node)
1954 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1956 while (can_goto_child(he, HMD_NORMAL)) {
1957 node = rb_last(&he->hroot_out.rb_root);
1958 he = rb_entry(node, struct hist_entry, rb_node);
1963 struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1965 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1967 if (can_goto_child(he, hmd))
1968 node = rb_first_cached(&he->hroot_out);
1970 node = rb_next(node);
1972 while (node == NULL) {
1977 node = rb_next(&he->rb_node);
1982 struct rb_node *rb_hierarchy_prev(struct rb_node *node)
1984 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1986 node = rb_prev(node);
1988 return rb_hierarchy_last(node);
1994 return &he->rb_node;
1997 bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
1999 struct rb_node *node;
2000 struct hist_entry *child;
2006 node = rb_first_cached(&he->hroot_out);
2007 child = rb_entry(node, struct hist_entry, rb_node);
2009 while (node && child->filtered) {
2010 node = rb_next(node);
2011 child = rb_entry(node, struct hist_entry, rb_node);
2015 percent = hist_entry__get_percent_limit(child);
2019 return node && percent >= limit;
2022 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
2023 enum hist_filter filter)
2025 h->filtered &= ~(1 << filter);
2027 if (symbol_conf.report_hierarchy) {
2028 struct hist_entry *parent = h->parent_he;
2031 he_stat__add_stat(&parent->stat, &h->stat);
2033 parent->filtered &= ~(1 << filter);
2035 if (parent->filtered)
2038 /* force fold unfiltered entry for simplicity */
2039 parent->unfolded = false;
2040 parent->has_no_entry = false;
2041 parent->row_offset = 0;
2042 parent->nr_rows = 0;
2044 parent = parent->parent_he;
2051 /* force fold unfiltered entry for simplicity */
2052 h->unfolded = false;
2053 h->has_no_entry = false;
2057 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
2059 hists__inc_filter_stats(hists, h);
2060 hists__calc_col_len(hists, h);
2064 static bool hists__filter_entry_by_dso(struct hists *hists,
2065 struct hist_entry *he)
2067 if (hists->dso_filter != NULL &&
2068 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
2069 he->filtered |= (1 << HIST_FILTER__DSO);
2076 static bool hists__filter_entry_by_thread(struct hists *hists,
2077 struct hist_entry *he)
2079 if (hists->thread_filter != NULL &&
2080 he->thread != hists->thread_filter) {
2081 he->filtered |= (1 << HIST_FILTER__THREAD);
2088 static bool hists__filter_entry_by_symbol(struct hists *hists,
2089 struct hist_entry *he)
2091 if (hists->symbol_filter_str != NULL &&
2092 (!he->ms.sym || strstr(he->ms.sym->name,
2093 hists->symbol_filter_str) == NULL)) {
2094 he->filtered |= (1 << HIST_FILTER__SYMBOL);
2101 static bool hists__filter_entry_by_socket(struct hists *hists,
2102 struct hist_entry *he)
2104 if ((hists->socket_filter > -1) &&
2105 (he->socket != hists->socket_filter)) {
2106 he->filtered |= (1 << HIST_FILTER__SOCKET);
2113 typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
2115 static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
2119 hists->stats.nr_non_filtered_samples = 0;
2121 hists__reset_filter_stats(hists);
2122 hists__reset_col_len(hists);
2124 for (nd = rb_first_cached(&hists->entries); nd; nd = rb_next(nd)) {
2125 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2127 if (filter(hists, h))
2130 hists__remove_entry_filter(hists, h, type);
2134 static void resort_filtered_entry(struct rb_root_cached *root,
2135 struct hist_entry *he)
2137 struct rb_node **p = &root->rb_root.rb_node;
2138 struct rb_node *parent = NULL;
2139 struct hist_entry *iter;
2140 struct rb_root_cached new_root = RB_ROOT_CACHED;
2142 bool leftmost = true;
2144 while (*p != NULL) {
2146 iter = rb_entry(parent, struct hist_entry, rb_node);
2148 if (hist_entry__sort(he, iter) > 0)
2151 p = &(*p)->rb_right;
2156 rb_link_node(&he->rb_node, parent, p);
2157 rb_insert_color_cached(&he->rb_node, root, leftmost);
2159 if (he->leaf || he->filtered)
2162 nd = rb_first_cached(&he->hroot_out);
2164 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2167 rb_erase_cached(&h->rb_node, &he->hroot_out);
2169 resort_filtered_entry(&new_root, h);
2172 he->hroot_out = new_root;
2175 static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2178 struct rb_root_cached new_root = RB_ROOT_CACHED;
2180 hists->stats.nr_non_filtered_samples = 0;
2182 hists__reset_filter_stats(hists);
2183 hists__reset_col_len(hists);
2185 nd = rb_first_cached(&hists->entries);
2187 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2190 ret = hist_entry__filter(h, type, arg);
2193 * case 1. non-matching type
2194 * zero out the period, set filter marker and move to child
2197 memset(&h->stat, 0, sizeof(h->stat));
2198 h->filtered |= (1 << type);
2200 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2203 * case 2. matched type (filter out)
2204 * set filter marker and move to next
2206 else if (ret == 1) {
2207 h->filtered |= (1 << type);
2209 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2212 * case 3. ok (not filtered)
2213 * add period to hists and parents, erase the filter marker
2214 * and move to next sibling
2217 hists__remove_entry_filter(hists, h, type);
2219 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2223 hierarchy_recalc_total_periods(hists);
2226 * resort output after applying a new filter since filter in a lower
2227 * hierarchy can change periods in a upper hierarchy.
2229 nd = rb_first_cached(&hists->entries);
2231 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2234 rb_erase_cached(&h->rb_node, &hists->entries);
2236 resort_filtered_entry(&new_root, h);
2239 hists->entries = new_root;
2242 void hists__filter_by_thread(struct hists *hists)
2244 if (symbol_conf.report_hierarchy)
2245 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2246 hists->thread_filter);
2248 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2249 hists__filter_entry_by_thread);
2252 void hists__filter_by_dso(struct hists *hists)
2254 if (symbol_conf.report_hierarchy)
2255 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2258 hists__filter_by_type(hists, HIST_FILTER__DSO,
2259 hists__filter_entry_by_dso);
2262 void hists__filter_by_symbol(struct hists *hists)
2264 if (symbol_conf.report_hierarchy)
2265 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2266 hists->symbol_filter_str);
2268 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2269 hists__filter_entry_by_symbol);
2272 void hists__filter_by_socket(struct hists *hists)
2274 if (symbol_conf.report_hierarchy)
2275 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2276 &hists->socket_filter);
2278 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2279 hists__filter_entry_by_socket);
2282 void events_stats__inc(struct events_stats *stats, u32 type)
2284 ++stats->nr_events[0];
2285 ++stats->nr_events[type];
2288 void hists__inc_nr_events(struct hists *hists, u32 type)
2290 events_stats__inc(&hists->stats, type);
2293 void hists__inc_nr_samples(struct hists *hists, bool filtered)
2295 events_stats__inc(&hists->stats, PERF_RECORD_SAMPLE);
2297 hists->stats.nr_non_filtered_samples++;
2300 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2301 struct hist_entry *pair)
2303 struct rb_root_cached *root;
2305 struct rb_node *parent = NULL;
2306 struct hist_entry *he;
2308 bool leftmost = true;
2310 if (hists__has(hists, need_collapse))
2311 root = &hists->entries_collapsed;
2313 root = hists->entries_in;
2315 p = &root->rb_root.rb_node;
2317 while (*p != NULL) {
2319 he = rb_entry(parent, struct hist_entry, rb_node_in);
2321 cmp = hist_entry__collapse(he, pair);
2329 p = &(*p)->rb_right;
2334 he = hist_entry__new(pair, true);
2336 memset(&he->stat, 0, sizeof(he->stat));
2338 if (symbol_conf.cumulate_callchain)
2339 memset(he->stat_acc, 0, sizeof(he->stat));
2340 rb_link_node(&he->rb_node_in, parent, p);
2341 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
2342 hists__inc_stats(hists, he);
2349 static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
2350 struct rb_root_cached *root,
2351 struct hist_entry *pair)
2354 struct rb_node *parent = NULL;
2355 struct hist_entry *he;
2356 struct perf_hpp_fmt *fmt;
2357 bool leftmost = true;
2359 p = &root->rb_root.rb_node;
2360 while (*p != NULL) {
2364 he = rb_entry(parent, struct hist_entry, rb_node_in);
2366 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2367 cmp = fmt->collapse(fmt, he, pair);
2375 p = &parent->rb_left;
2377 p = &parent->rb_right;
2382 he = hist_entry__new(pair, true);
2384 rb_link_node(&he->rb_node_in, parent, p);
2385 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
2389 memset(&he->stat, 0, sizeof(he->stat));
2390 hists__inc_stats(hists, he);
2396 static struct hist_entry *hists__find_entry(struct hists *hists,
2397 struct hist_entry *he)
2401 if (hists__has(hists, need_collapse))
2402 n = hists->entries_collapsed.rb_root.rb_node;
2404 n = hists->entries_in->rb_root.rb_node;
2407 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2408 int64_t cmp = hist_entry__collapse(iter, he);
2421 static struct hist_entry *hists__find_hierarchy_entry(struct rb_root_cached *root,
2422 struct hist_entry *he)
2424 struct rb_node *n = root->rb_root.rb_node;
2427 struct hist_entry *iter;
2428 struct perf_hpp_fmt *fmt;
2431 iter = rb_entry(n, struct hist_entry, rb_node_in);
2432 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2433 cmp = fmt->collapse(fmt, iter, he);
2449 static void hists__match_hierarchy(struct rb_root_cached *leader_root,
2450 struct rb_root_cached *other_root)
2453 struct hist_entry *pos, *pair;
2455 for (nd = rb_first_cached(leader_root); nd; nd = rb_next(nd)) {
2456 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2457 pair = hists__find_hierarchy_entry(other_root, pos);
2460 hist_entry__add_pair(pair, pos);
2461 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2467 * Look for pairs to link to the leader buckets (hist_entries):
2469 void hists__match(struct hists *leader, struct hists *other)
2471 struct rb_root_cached *root;
2473 struct hist_entry *pos, *pair;
2475 if (symbol_conf.report_hierarchy) {
2476 /* hierarchy report always collapses entries */
2477 return hists__match_hierarchy(&leader->entries_collapsed,
2478 &other->entries_collapsed);
2481 if (hists__has(leader, need_collapse))
2482 root = &leader->entries_collapsed;
2484 root = leader->entries_in;
2486 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2487 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2488 pair = hists__find_entry(other, pos);
2491 hist_entry__add_pair(pair, pos);
2495 static int hists__link_hierarchy(struct hists *leader_hists,
2496 struct hist_entry *parent,
2497 struct rb_root_cached *leader_root,
2498 struct rb_root_cached *other_root)
2501 struct hist_entry *pos, *leader;
2503 for (nd = rb_first_cached(other_root); nd; nd = rb_next(nd)) {
2504 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2506 if (hist_entry__has_pairs(pos)) {
2509 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2510 if (leader->hists == leader_hists) {
2518 leader = add_dummy_hierarchy_entry(leader_hists,
2523 /* do not point parent in the pos */
2524 leader->parent_he = parent;
2526 hist_entry__add_pair(pos, leader);
2530 if (hists__link_hierarchy(leader_hists, leader,
2532 &pos->hroot_in) < 0)
2540 * Look for entries in the other hists that are not present in the leader, if
2541 * we find them, just add a dummy entry on the leader hists, with period=0,
2542 * nr_events=0, to serve as the list header.
2544 int hists__link(struct hists *leader, struct hists *other)
2546 struct rb_root_cached *root;
2548 struct hist_entry *pos, *pair;
2550 if (symbol_conf.report_hierarchy) {
2551 /* hierarchy report always collapses entries */
2552 return hists__link_hierarchy(leader, NULL,
2553 &leader->entries_collapsed,
2554 &other->entries_collapsed);
2557 if (hists__has(other, need_collapse))
2558 root = &other->entries_collapsed;
2560 root = other->entries_in;
2562 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2563 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2565 if (!hist_entry__has_pairs(pos)) {
2566 pair = hists__add_dummy_entry(leader, pos);
2569 hist_entry__add_pair(pos, pair);
2576 int hists__unlink(struct hists *hists)
2578 struct rb_root_cached *root;
2580 struct hist_entry *pos;
2582 if (hists__has(hists, need_collapse))
2583 root = &hists->entries_collapsed;
2585 root = hists->entries_in;
2587 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2588 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2589 list_del_init(&pos->pairs.node);
2595 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2596 struct perf_sample *sample, bool nonany_branch_mode,
2599 struct branch_info *bi;
2600 struct branch_entry *entries = perf_sample__branch_entries(sample);
2602 /* If we have branch cycles always annotate them. */
2603 if (bs && bs->nr && entries[0].flags.cycles) {
2606 bi = sample__resolve_bstack(sample, al);
2608 struct addr_map_symbol *prev = NULL;
2611 * Ignore errors, still want to process the
2614 * For non standard branch modes always
2615 * force no IPC (prev == NULL)
2617 * Note that perf stores branches reversed from
2620 for (i = bs->nr - 1; i >= 0; i--) {
2621 addr_map_symbol__account_cycles(&bi[i].from,
2622 nonany_branch_mode ? NULL : prev,
2623 bi[i].flags.cycles);
2627 *total_cycles += bi[i].flags.cycles;
2634 size_t perf_evlist__fprintf_nr_events(struct evlist *evlist, FILE *fp)
2639 evlist__for_each_entry(evlist, pos) {
2640 ret += fprintf(fp, "%s stats:\n", perf_evsel__name(pos));
2641 ret += events_stats__fprintf(&evsel__hists(pos)->stats, fp);
2648 u64 hists__total_period(struct hists *hists)
2650 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2651 hists->stats.total_period;
2654 int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq)
2658 const struct dso *dso = hists->dso_filter;
2659 struct thread *thread = hists->thread_filter;
2660 int socket_id = hists->socket_filter;
2661 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
2662 u64 nr_events = hists->stats.total_period;
2663 struct evsel *evsel = hists_to_evsel(hists);
2664 const char *ev_name = perf_evsel__name(evsel);
2665 char buf[512], sample_freq_str[64] = "";
2666 size_t buflen = sizeof(buf);
2667 char ref[30] = " show reference callgraph, ";
2668 bool enable_ref = false;
2670 if (symbol_conf.filter_relative) {
2671 nr_samples = hists->stats.nr_non_filtered_samples;
2672 nr_events = hists->stats.total_non_filtered_period;
2675 if (perf_evsel__is_group_event(evsel)) {
2678 perf_evsel__group_desc(evsel, buf, buflen);
2681 for_each_group_member(pos, evsel) {
2682 struct hists *pos_hists = evsel__hists(pos);
2684 if (symbol_conf.filter_relative) {
2685 nr_samples += pos_hists->stats.nr_non_filtered_samples;
2686 nr_events += pos_hists->stats.total_non_filtered_period;
2688 nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
2689 nr_events += pos_hists->stats.total_period;
2694 if (symbol_conf.show_ref_callgraph &&
2695 strstr(ev_name, "call-graph=no"))
2699 scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->core.attr.sample_freq);
2701 nr_samples = convert_unit(nr_samples, &unit);
2702 printed = scnprintf(bf, size,
2703 "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64,
2704 nr_samples, unit, evsel->core.nr_members > 1 ? "s" : "",
2705 ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events);
2708 if (hists->uid_filter_str)
2709 printed += snprintf(bf + printed, size - printed,
2710 ", UID: %s", hists->uid_filter_str);
2712 if (hists__has(hists, thread)) {
2713 printed += scnprintf(bf + printed, size - printed,
2715 (thread->comm_set ? thread__comm_str(thread) : ""),
2718 printed += scnprintf(bf + printed, size - printed,
2720 (thread->comm_set ? thread__comm_str(thread) : ""));
2724 printed += scnprintf(bf + printed, size - printed,
2725 ", DSO: %s", dso->short_name);
2727 printed += scnprintf(bf + printed, size - printed,
2728 ", Processor Socket: %d", socket_id);
2733 int parse_filter_percentage(const struct option *opt __maybe_unused,
2734 const char *arg, int unset __maybe_unused)
2736 if (!strcmp(arg, "relative"))
2737 symbol_conf.filter_relative = true;
2738 else if (!strcmp(arg, "absolute"))
2739 symbol_conf.filter_relative = false;
2741 pr_debug("Invalid percentage: %s\n", arg);
2748 int perf_hist_config(const char *var, const char *value)
2750 if (!strcmp(var, "hist.percentage"))
2751 return parse_filter_percentage(NULL, value, 0);
2756 int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
2758 memset(hists, 0, sizeof(*hists));
2759 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT_CACHED;
2760 hists->entries_in = &hists->entries_in_array[0];
2761 hists->entries_collapsed = RB_ROOT_CACHED;
2762 hists->entries = RB_ROOT_CACHED;
2763 pthread_mutex_init(&hists->lock, NULL);
2764 hists->socket_filter = -1;
2765 hists->hpp_list = hpp_list;
2766 INIT_LIST_HEAD(&hists->hpp_formats);
2770 static void hists__delete_remaining_entries(struct rb_root_cached *root)
2772 struct rb_node *node;
2773 struct hist_entry *he;
2775 while (!RB_EMPTY_ROOT(&root->rb_root)) {
2776 node = rb_first_cached(root);
2777 rb_erase_cached(node, root);
2779 he = rb_entry(node, struct hist_entry, rb_node_in);
2780 hist_entry__delete(he);
2784 static void hists__delete_all_entries(struct hists *hists)
2786 hists__delete_entries(hists);
2787 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2788 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2789 hists__delete_remaining_entries(&hists->entries_collapsed);
2792 static void hists_evsel__exit(struct evsel *evsel)
2794 struct hists *hists = evsel__hists(evsel);
2795 struct perf_hpp_fmt *fmt, *pos;
2796 struct perf_hpp_list_node *node, *tmp;
2798 hists__delete_all_entries(hists);
2800 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2801 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2802 list_del_init(&fmt->list);
2805 list_del_init(&node->list);
2810 static int hists_evsel__init(struct evsel *evsel)
2812 struct hists *hists = evsel__hists(evsel);
2814 __hists__init(hists, &perf_hpp_list);
2819 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2820 * stored in the rbtree...
2823 int hists__init(void)
2825 int err = perf_evsel__object_config(sizeof(struct hists_evsel),
2829 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2834 void perf_hpp_list__init(struct perf_hpp_list *list)
2836 INIT_LIST_HEAD(&list->fields);
2837 INIT_LIST_HEAD(&list->sorts);