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);
191 hists__new_col_len(hists, HISTC_MEM_DATA_PAGE_SIZE,
192 unresolved_col_width + 4 + 2);
195 symlen = unresolved_col_width + 4 + 2;
196 hists__new_col_len(hists, HISTC_MEM_DADDR_SYMBOL, symlen);
197 hists__new_col_len(hists, HISTC_MEM_IADDR_SYMBOL, symlen);
198 hists__set_unres_dso_col_len(hists, HISTC_MEM_DADDR_DSO);
201 hists__new_col_len(hists, HISTC_CGROUP, 6);
202 hists__new_col_len(hists, HISTC_CGROUP_ID, 20);
203 hists__new_col_len(hists, HISTC_CPU, 3);
204 hists__new_col_len(hists, HISTC_SOCKET, 6);
205 hists__new_col_len(hists, HISTC_MEM_LOCKED, 6);
206 hists__new_col_len(hists, HISTC_MEM_TLB, 22);
207 hists__new_col_len(hists, HISTC_MEM_SNOOP, 12);
208 hists__new_col_len(hists, HISTC_MEM_LVL, 21 + 3);
209 hists__new_col_len(hists, HISTC_LOCAL_WEIGHT, 12);
210 hists__new_col_len(hists, HISTC_GLOBAL_WEIGHT, 12);
211 hists__new_col_len(hists, HISTC_MEM_BLOCKED, 10);
212 hists__new_col_len(hists, HISTC_LOCAL_INS_LAT, 13);
213 hists__new_col_len(hists, HISTC_GLOBAL_INS_LAT, 13);
214 hists__new_col_len(hists, HISTC_P_STAGE_CYC, 13);
215 if (symbol_conf.nanosecs)
216 hists__new_col_len(hists, HISTC_TIME, 16);
218 hists__new_col_len(hists, HISTC_TIME, 12);
219 hists__new_col_len(hists, HISTC_CODE_PAGE_SIZE, 6);
222 len = MAX(strlen(h->srcline), strlen(sort_srcline.se_header));
223 hists__new_col_len(hists, HISTC_SRCLINE, len);
227 hists__new_col_len(hists, HISTC_SRCFILE, strlen(h->srcfile));
230 hists__new_col_len(hists, HISTC_TRANSACTION,
231 hist_entry__transaction_len());
234 hists__new_col_len(hists, HISTC_TRACE, strlen(h->trace_output));
237 const char *cgrp_name = "unknown";
238 struct cgroup *cgrp = cgroup__find(h->ms.maps->machine->env,
241 cgrp_name = cgrp->name;
243 hists__new_col_len(hists, HISTC_CGROUP, strlen(cgrp_name));
247 void hists__output_recalc_col_len(struct hists *hists, int max_rows)
249 struct rb_node *next = rb_first_cached(&hists->entries);
250 struct hist_entry *n;
253 hists__reset_col_len(hists);
255 while (next && row++ < max_rows) {
256 n = rb_entry(next, struct hist_entry, rb_node);
258 hists__calc_col_len(hists, n);
259 next = rb_next(&n->rb_node);
263 static void he_stat__add_cpumode_period(struct he_stat *he_stat,
264 unsigned int cpumode, u64 period)
267 case PERF_RECORD_MISC_KERNEL:
268 he_stat->period_sys += period;
270 case PERF_RECORD_MISC_USER:
271 he_stat->period_us += period;
273 case PERF_RECORD_MISC_GUEST_KERNEL:
274 he_stat->period_guest_sys += period;
276 case PERF_RECORD_MISC_GUEST_USER:
277 he_stat->period_guest_us += period;
284 static long hist_time(unsigned long htime)
286 unsigned long time_quantum = symbol_conf.time_quantum;
288 return (htime / time_quantum) * time_quantum;
292 static void he_stat__add_period(struct he_stat *he_stat, u64 period)
294 he_stat->period += period;
295 he_stat->nr_events += 1;
298 static void he_stat__add_stat(struct he_stat *dest, struct he_stat *src)
300 dest->period += src->period;
301 dest->period_sys += src->period_sys;
302 dest->period_us += src->period_us;
303 dest->period_guest_sys += src->period_guest_sys;
304 dest->period_guest_us += src->period_guest_us;
305 dest->nr_events += src->nr_events;
308 static void he_stat__decay(struct he_stat *he_stat)
310 he_stat->period = (he_stat->period * 7) / 8;
311 he_stat->nr_events = (he_stat->nr_events * 7) / 8;
312 /* XXX need decay for weight too? */
315 static void hists__delete_entry(struct hists *hists, struct hist_entry *he);
317 static bool hists__decay_entry(struct hists *hists, struct hist_entry *he)
319 u64 prev_period = he->stat.period;
322 if (prev_period == 0)
325 he_stat__decay(&he->stat);
326 if (symbol_conf.cumulate_callchain)
327 he_stat__decay(he->stat_acc);
328 decay_callchain(he->callchain);
330 diff = prev_period - he->stat.period;
333 hists->stats.total_period -= diff;
335 hists->stats.total_non_filtered_period -= diff;
339 struct hist_entry *child;
340 struct rb_node *node = rb_first_cached(&he->hroot_out);
342 child = rb_entry(node, struct hist_entry, rb_node);
343 node = rb_next(node);
345 if (hists__decay_entry(hists, child))
346 hists__delete_entry(hists, child);
350 return he->stat.period == 0;
353 static void hists__delete_entry(struct hists *hists, struct hist_entry *he)
355 struct rb_root_cached *root_in;
356 struct rb_root_cached *root_out;
359 root_in = &he->parent_he->hroot_in;
360 root_out = &he->parent_he->hroot_out;
362 if (hists__has(hists, need_collapse))
363 root_in = &hists->entries_collapsed;
365 root_in = hists->entries_in;
366 root_out = &hists->entries;
369 rb_erase_cached(&he->rb_node_in, root_in);
370 rb_erase_cached(&he->rb_node, root_out);
374 --hists->nr_non_filtered_entries;
376 hist_entry__delete(he);
379 void hists__decay_entries(struct hists *hists, bool zap_user, bool zap_kernel)
381 struct rb_node *next = rb_first_cached(&hists->entries);
382 struct hist_entry *n;
385 n = rb_entry(next, struct hist_entry, rb_node);
386 next = rb_next(&n->rb_node);
387 if (((zap_user && n->level == '.') ||
388 (zap_kernel && n->level != '.') ||
389 hists__decay_entry(hists, n))) {
390 hists__delete_entry(hists, n);
395 void hists__delete_entries(struct hists *hists)
397 struct rb_node *next = rb_first_cached(&hists->entries);
398 struct hist_entry *n;
401 n = rb_entry(next, struct hist_entry, rb_node);
402 next = rb_next(&n->rb_node);
404 hists__delete_entry(hists, n);
408 struct hist_entry *hists__get_entry(struct hists *hists, int idx)
410 struct rb_node *next = rb_first_cached(&hists->entries);
411 struct hist_entry *n;
415 n = rb_entry(next, struct hist_entry, rb_node);
419 next = rb_next(&n->rb_node);
427 * histogram, sorted on item, collects periods
430 static int hist_entry__init(struct hist_entry *he,
431 struct hist_entry *template,
433 size_t callchain_size)
436 he->callchain_size = callchain_size;
438 if (symbol_conf.cumulate_callchain) {
439 he->stat_acc = malloc(sizeof(he->stat));
440 if (he->stat_acc == NULL)
442 memcpy(he->stat_acc, &he->stat, sizeof(he->stat));
444 memset(&he->stat, 0, sizeof(he->stat));
447 map__get(he->ms.map);
449 if (he->branch_info) {
451 * This branch info is (a part of) allocated from
452 * sample__resolve_bstack() and will be freed after
453 * adding new entries. So we need to save a copy.
455 he->branch_info = malloc(sizeof(*he->branch_info));
456 if (he->branch_info == NULL)
459 memcpy(he->branch_info, template->branch_info,
460 sizeof(*he->branch_info));
462 map__get(he->branch_info->from.ms.map);
463 map__get(he->branch_info->to.ms.map);
467 map__get(he->mem_info->iaddr.ms.map);
468 map__get(he->mem_info->daddr.ms.map);
471 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
472 callchain_init(he->callchain);
475 he->raw_data = memdup(he->raw_data, he->raw_size);
476 if (he->raw_data == NULL)
481 he->srcline = strdup(he->srcline);
482 if (he->srcline == NULL)
486 if (symbol_conf.res_sample) {
487 he->res_samples = calloc(sizeof(struct res_sample),
488 symbol_conf.res_sample);
489 if (!he->res_samples)
493 INIT_LIST_HEAD(&he->pairs.node);
494 thread__get(he->thread);
495 he->hroot_in = RB_ROOT_CACHED;
496 he->hroot_out = RB_ROOT_CACHED;
498 if (!symbol_conf.report_hierarchy)
507 zfree(&he->raw_data);
510 if (he->branch_info) {
511 map__put(he->branch_info->from.ms.map);
512 map__put(he->branch_info->to.ms.map);
513 zfree(&he->branch_info);
516 map__put(he->mem_info->iaddr.ms.map);
517 map__put(he->mem_info->daddr.ms.map);
520 map__zput(he->ms.map);
521 zfree(&he->stat_acc);
525 static void *hist_entry__zalloc(size_t size)
527 return zalloc(size + sizeof(struct hist_entry));
530 static void hist_entry__free(void *ptr)
535 static struct hist_entry_ops default_ops = {
536 .new = hist_entry__zalloc,
537 .free = hist_entry__free,
540 static struct hist_entry *hist_entry__new(struct hist_entry *template,
543 struct hist_entry_ops *ops = template->ops;
544 size_t callchain_size = 0;
545 struct hist_entry *he;
549 ops = template->ops = &default_ops;
551 if (symbol_conf.use_callchain)
552 callchain_size = sizeof(struct callchain_root);
554 he = ops->new(callchain_size);
556 err = hist_entry__init(he, template, sample_self, callchain_size);
566 static u8 symbol__parent_filter(const struct symbol *parent)
568 if (symbol_conf.exclude_other && parent == NULL)
569 return 1 << HIST_FILTER__PARENT;
573 static void hist_entry__add_callchain_period(struct hist_entry *he, u64 period)
575 if (!hist_entry__has_callchains(he) || !symbol_conf.use_callchain)
578 he->hists->callchain_period += period;
580 he->hists->callchain_non_filtered_period += period;
583 static struct hist_entry *hists__findnew_entry(struct hists *hists,
584 struct hist_entry *entry,
585 struct addr_location *al,
589 struct rb_node *parent = NULL;
590 struct hist_entry *he;
592 u64 period = entry->stat.period;
593 bool leftmost = true;
595 p = &hists->entries_in->rb_root.rb_node;
599 he = rb_entry(parent, struct hist_entry, rb_node_in);
602 * Make sure that it receives arguments in a same order as
603 * hist_entry__collapse() so that we can use an appropriate
604 * function when searching an entry regardless which sort
607 cmp = hist_entry__cmp(he, entry);
611 he_stat__add_period(&he->stat, period);
612 hist_entry__add_callchain_period(he, period);
614 if (symbol_conf.cumulate_callchain)
615 he_stat__add_period(he->stat_acc, period);
618 * This mem info was allocated from sample__resolve_mem
619 * and will not be used anymore.
621 mem_info__zput(entry->mem_info);
623 block_info__zput(entry->block_info);
625 /* If the map of an existing hist_entry has
626 * become out-of-date due to an exec() or
627 * similar, update it. Otherwise we will
628 * mis-adjust symbol addresses when computing
629 * the history counter to increment.
631 if (he->ms.map != entry->ms.map) {
632 map__put(he->ms.map);
633 he->ms.map = map__get(entry->ms.map);
646 he = hist_entry__new(entry, sample_self);
651 hist_entry__add_callchain_period(he, period);
654 rb_link_node(&he->rb_node_in, parent, p);
655 rb_insert_color_cached(&he->rb_node_in, hists->entries_in, leftmost);
658 he_stat__add_cpumode_period(&he->stat, al->cpumode, period);
659 if (symbol_conf.cumulate_callchain)
660 he_stat__add_cpumode_period(he->stat_acc, al->cpumode, period);
664 static unsigned random_max(unsigned high)
666 unsigned thresh = -high % high;
668 unsigned r = random();
674 static void hists__res_sample(struct hist_entry *he, struct perf_sample *sample)
676 struct res_sample *r;
679 if (he->num_res < symbol_conf.res_sample) {
682 j = random_max(symbol_conf.res_sample);
684 r = &he->res_samples[j];
685 r->time = sample->time;
686 r->cpu = sample->cpu;
687 r->tid = sample->tid;
690 static struct hist_entry*
691 __hists__add_entry(struct hists *hists,
692 struct addr_location *al,
693 struct symbol *sym_parent,
694 struct branch_info *bi,
696 struct block_info *block_info,
697 struct perf_sample *sample,
699 struct hist_entry_ops *ops)
701 struct namespaces *ns = thread__namespaces(al->thread);
702 struct hist_entry entry = {
703 .thread = al->thread,
704 .comm = thread__comm(al->thread),
706 .dev = ns ? ns->link_info[CGROUP_NS_INDEX].dev : 0,
707 .ino = ns ? ns->link_info[CGROUP_NS_INDEX].ino : 0,
709 .cgroup = sample->cgroup,
715 .srcline = (char *) al->srcline,
716 .socket = al->socket,
718 .cpumode = al->cpumode,
721 .code_page_size = sample->code_page_size,
724 .period = sample->period,
726 .parent = sym_parent,
727 .filtered = symbol__parent_filter(sym_parent) | al->filtered,
731 .block_info = block_info,
732 .transaction = sample->transaction,
733 .raw_data = sample->raw_data,
734 .raw_size = sample->raw_size,
736 .time = hist_time(sample->time),
737 .weight = sample->weight,
738 .ins_lat = sample->ins_lat,
739 .p_stage_cyc = sample->p_stage_cyc,
740 }, *he = hists__findnew_entry(hists, &entry, al, sample_self);
742 if (!hists->has_callchains && he && he->callchain_size != 0)
743 hists->has_callchains = true;
744 if (he && symbol_conf.res_sample)
745 hists__res_sample(he, sample);
749 struct hist_entry *hists__add_entry(struct hists *hists,
750 struct addr_location *al,
751 struct symbol *sym_parent,
752 struct branch_info *bi,
754 struct perf_sample *sample,
757 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
758 sample, sample_self, NULL);
761 struct hist_entry *hists__add_entry_ops(struct hists *hists,
762 struct hist_entry_ops *ops,
763 struct addr_location *al,
764 struct symbol *sym_parent,
765 struct branch_info *bi,
767 struct perf_sample *sample,
770 return __hists__add_entry(hists, al, sym_parent, bi, mi, NULL,
771 sample, sample_self, ops);
774 struct hist_entry *hists__add_entry_block(struct hists *hists,
775 struct addr_location *al,
776 struct block_info *block_info)
778 struct hist_entry entry = {
779 .block_info = block_info,
786 }, *he = hists__findnew_entry(hists, &entry, al, false);
792 iter_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
793 struct addr_location *al __maybe_unused)
799 iter_add_next_nop_entry(struct hist_entry_iter *iter __maybe_unused,
800 struct addr_location *al __maybe_unused)
806 iter_prepare_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
808 struct perf_sample *sample = iter->sample;
811 mi = sample__resolve_mem(sample, al);
820 iter_add_single_mem_entry(struct hist_entry_iter *iter, struct addr_location *al)
823 struct mem_info *mi = iter->priv;
824 struct hists *hists = evsel__hists(iter->evsel);
825 struct perf_sample *sample = iter->sample;
826 struct hist_entry *he;
831 cost = sample->weight;
836 * must pass period=weight in order to get the correct
837 * sorting from hists__collapse_resort() which is solely
838 * based on periods. We want sorting be done on nr_events * weight
839 * and this is indirectly achieved by passing period=weight here
840 * and the he_stat__add_period() function.
842 sample->period = cost;
844 he = hists__add_entry(hists, al, iter->parent, NULL, mi,
854 iter_finish_mem_entry(struct hist_entry_iter *iter,
855 struct addr_location *al __maybe_unused)
857 struct evsel *evsel = iter->evsel;
858 struct hists *hists = evsel__hists(evsel);
859 struct hist_entry *he = iter->he;
865 hists__inc_nr_samples(hists, he->filtered);
867 err = hist_entry__append_callchain(he, iter->sample);
871 * We don't need to free iter->priv (mem_info) here since the mem info
872 * was either already freed in hists__findnew_entry() or passed to a
873 * new hist entry by hist_entry__new().
882 iter_prepare_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
884 struct branch_info *bi;
885 struct perf_sample *sample = iter->sample;
887 bi = sample__resolve_bstack(sample, al);
892 iter->total = sample->branch_stack->nr;
899 iter_add_single_branch_entry(struct hist_entry_iter *iter __maybe_unused,
900 struct addr_location *al __maybe_unused)
906 iter_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
908 struct branch_info *bi = iter->priv;
914 if (iter->curr >= iter->total)
917 al->maps = bi[i].to.ms.maps;
918 al->map = bi[i].to.ms.map;
919 al->sym = bi[i].to.ms.sym;
920 al->addr = bi[i].to.addr;
925 iter_add_next_branch_entry(struct hist_entry_iter *iter, struct addr_location *al)
927 struct branch_info *bi;
928 struct evsel *evsel = iter->evsel;
929 struct hists *hists = evsel__hists(evsel);
930 struct perf_sample *sample = iter->sample;
931 struct hist_entry *he = NULL;
937 if (iter->hide_unresolved && !(bi[i].from.ms.sym && bi[i].to.ms.sym))
941 * The report shows the percentage of total branches captured
942 * and not events sampled. Thus we use a pseudo period of 1.
945 sample->weight = bi->flags.cycles ? bi->flags.cycles : 1;
947 he = hists__add_entry(hists, al, iter->parent, &bi[i], NULL,
952 hists__inc_nr_samples(hists, he->filtered);
961 iter_finish_branch_entry(struct hist_entry_iter *iter,
962 struct addr_location *al __maybe_unused)
967 return iter->curr >= iter->total ? 0 : -1;
971 iter_prepare_normal_entry(struct hist_entry_iter *iter __maybe_unused,
972 struct addr_location *al __maybe_unused)
978 iter_add_single_normal_entry(struct hist_entry_iter *iter, struct addr_location *al)
980 struct evsel *evsel = iter->evsel;
981 struct perf_sample *sample = iter->sample;
982 struct hist_entry *he;
984 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
994 iter_finish_normal_entry(struct hist_entry_iter *iter,
995 struct addr_location *al __maybe_unused)
997 struct hist_entry *he = iter->he;
998 struct evsel *evsel = iter->evsel;
999 struct perf_sample *sample = iter->sample;
1006 hists__inc_nr_samples(evsel__hists(evsel), he->filtered);
1008 return hist_entry__append_callchain(he, sample);
1012 iter_prepare_cumulative_entry(struct hist_entry_iter *iter,
1013 struct addr_location *al __maybe_unused)
1015 struct hist_entry **he_cache;
1017 callchain_cursor_commit(&callchain_cursor);
1020 * This is for detecting cycles or recursions so that they're
1021 * cumulated only one time to prevent entries more than 100%
1024 he_cache = malloc(sizeof(*he_cache) * (callchain_cursor.nr + 1));
1025 if (he_cache == NULL)
1028 iter->priv = he_cache;
1035 iter_add_single_cumulative_entry(struct hist_entry_iter *iter,
1036 struct addr_location *al)
1038 struct evsel *evsel = iter->evsel;
1039 struct hists *hists = evsel__hists(evsel);
1040 struct perf_sample *sample = iter->sample;
1041 struct hist_entry **he_cache = iter->priv;
1042 struct hist_entry *he;
1045 he = hists__add_entry(hists, al, iter->parent, NULL, NULL,
1051 he_cache[iter->curr++] = he;
1053 hist_entry__append_callchain(he, sample);
1056 * We need to re-initialize the cursor since callchain_append()
1057 * advanced the cursor to the end.
1059 callchain_cursor_commit(&callchain_cursor);
1061 hists__inc_nr_samples(hists, he->filtered);
1067 iter_next_cumulative_entry(struct hist_entry_iter *iter,
1068 struct addr_location *al)
1070 struct callchain_cursor_node *node;
1072 node = callchain_cursor_current(&callchain_cursor);
1076 return fill_callchain_info(al, node, iter->hide_unresolved);
1080 hist_entry__fast__sym_diff(struct hist_entry *left,
1081 struct hist_entry *right)
1083 struct symbol *sym_l = left->ms.sym;
1084 struct symbol *sym_r = right->ms.sym;
1086 if (!sym_l && !sym_r)
1087 return left->ip != right->ip;
1089 return !!_sort__sym_cmp(sym_l, sym_r);
1094 iter_add_next_cumulative_entry(struct hist_entry_iter *iter,
1095 struct addr_location *al)
1097 struct evsel *evsel = iter->evsel;
1098 struct perf_sample *sample = iter->sample;
1099 struct hist_entry **he_cache = iter->priv;
1100 struct hist_entry *he;
1101 struct hist_entry he_tmp = {
1102 .hists = evsel__hists(evsel),
1104 .thread = al->thread,
1105 .comm = thread__comm(al->thread),
1112 .srcline = (char *) al->srcline,
1113 .parent = iter->parent,
1114 .raw_data = sample->raw_data,
1115 .raw_size = sample->raw_size,
1118 struct callchain_cursor cursor;
1119 bool fast = hists__has(he_tmp.hists, sym);
1121 callchain_cursor_snapshot(&cursor, &callchain_cursor);
1123 callchain_cursor_advance(&callchain_cursor);
1126 * Check if there's duplicate entries in the callchain.
1127 * It's possible that it has cycles or recursive calls.
1129 for (i = 0; i < iter->curr; i++) {
1131 * For most cases, there are no duplicate entries in callchain.
1132 * The symbols are usually different. Do a quick check for
1135 if (fast && hist_entry__fast__sym_diff(he_cache[i], &he_tmp))
1138 if (hist_entry__cmp(he_cache[i], &he_tmp) == 0) {
1139 /* to avoid calling callback function */
1145 he = hists__add_entry(evsel__hists(evsel), al, iter->parent, NULL, NULL,
1151 he_cache[iter->curr++] = he;
1153 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain)
1154 callchain_append(he->callchain, &cursor, sample->period);
1159 iter_finish_cumulative_entry(struct hist_entry_iter *iter,
1160 struct addr_location *al __maybe_unused)
1168 const struct hist_iter_ops hist_iter_mem = {
1169 .prepare_entry = iter_prepare_mem_entry,
1170 .add_single_entry = iter_add_single_mem_entry,
1171 .next_entry = iter_next_nop_entry,
1172 .add_next_entry = iter_add_next_nop_entry,
1173 .finish_entry = iter_finish_mem_entry,
1176 const struct hist_iter_ops hist_iter_branch = {
1177 .prepare_entry = iter_prepare_branch_entry,
1178 .add_single_entry = iter_add_single_branch_entry,
1179 .next_entry = iter_next_branch_entry,
1180 .add_next_entry = iter_add_next_branch_entry,
1181 .finish_entry = iter_finish_branch_entry,
1184 const struct hist_iter_ops hist_iter_normal = {
1185 .prepare_entry = iter_prepare_normal_entry,
1186 .add_single_entry = iter_add_single_normal_entry,
1187 .next_entry = iter_next_nop_entry,
1188 .add_next_entry = iter_add_next_nop_entry,
1189 .finish_entry = iter_finish_normal_entry,
1192 const struct hist_iter_ops hist_iter_cumulative = {
1193 .prepare_entry = iter_prepare_cumulative_entry,
1194 .add_single_entry = iter_add_single_cumulative_entry,
1195 .next_entry = iter_next_cumulative_entry,
1196 .add_next_entry = iter_add_next_cumulative_entry,
1197 .finish_entry = iter_finish_cumulative_entry,
1200 int hist_entry_iter__add(struct hist_entry_iter *iter, struct addr_location *al,
1201 int max_stack_depth, void *arg)
1204 struct map *alm = NULL;
1207 alm = map__get(al->map);
1209 err = sample__resolve_callchain(iter->sample, &callchain_cursor, &iter->parent,
1210 iter->evsel, al, max_stack_depth);
1216 err = iter->ops->prepare_entry(iter, al);
1220 err = iter->ops->add_single_entry(iter, al);
1224 if (iter->he && iter->add_entry_cb) {
1225 err = iter->add_entry_cb(iter, al, true, arg);
1230 while (iter->ops->next_entry(iter, al)) {
1231 err = iter->ops->add_next_entry(iter, al);
1235 if (iter->he && iter->add_entry_cb) {
1236 err = iter->add_entry_cb(iter, al, false, arg);
1243 err2 = iter->ops->finish_entry(iter, al);
1253 hist_entry__cmp(struct hist_entry *left, struct hist_entry *right)
1255 struct hists *hists = left->hists;
1256 struct perf_hpp_fmt *fmt;
1259 hists__for_each_sort_list(hists, fmt) {
1260 if (perf_hpp__is_dynamic_entry(fmt) &&
1261 !perf_hpp__defined_dynamic_entry(fmt, hists))
1264 cmp = fmt->cmp(fmt, left, right);
1273 hist_entry__collapse(struct hist_entry *left, struct hist_entry *right)
1275 struct hists *hists = left->hists;
1276 struct perf_hpp_fmt *fmt;
1279 hists__for_each_sort_list(hists, fmt) {
1280 if (perf_hpp__is_dynamic_entry(fmt) &&
1281 !perf_hpp__defined_dynamic_entry(fmt, hists))
1284 cmp = fmt->collapse(fmt, left, right);
1292 void hist_entry__delete(struct hist_entry *he)
1294 struct hist_entry_ops *ops = he->ops;
1296 thread__zput(he->thread);
1297 map__zput(he->ms.map);
1299 if (he->branch_info) {
1300 map__zput(he->branch_info->from.ms.map);
1301 map__zput(he->branch_info->to.ms.map);
1302 free_srcline(he->branch_info->srcline_from);
1303 free_srcline(he->branch_info->srcline_to);
1304 zfree(&he->branch_info);
1308 map__zput(he->mem_info->iaddr.ms.map);
1309 map__zput(he->mem_info->daddr.ms.map);
1310 mem_info__zput(he->mem_info);
1314 block_info__zput(he->block_info);
1316 zfree(&he->res_samples);
1317 zfree(&he->stat_acc);
1318 free_srcline(he->srcline);
1319 if (he->srcfile && he->srcfile[0])
1320 zfree(&he->srcfile);
1321 free_callchain(he->callchain);
1322 zfree(&he->trace_output);
1323 zfree(&he->raw_data);
1328 * If this is not the last column, then we need to pad it according to the
1329 * pre-calculated max length for this column, otherwise don't bother adding
1330 * spaces because that would break viewing this with, for instance, 'less',
1331 * that would show tons of trailing spaces when a long C++ demangled method
1334 int hist_entry__snprintf_alignment(struct hist_entry *he, struct perf_hpp *hpp,
1335 struct perf_hpp_fmt *fmt, int printed)
1337 if (!list_is_last(&fmt->list, &he->hists->hpp_list->fields)) {
1338 const int width = fmt->width(fmt, hpp, he->hists);
1339 if (printed < width) {
1340 advance_hpp(hpp, printed);
1341 printed = scnprintf(hpp->buf, hpp->size, "%-*s", width - printed, " ");
1349 * collapse the histogram
1352 static void hists__apply_filters(struct hists *hists, struct hist_entry *he);
1353 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *he,
1354 enum hist_filter type);
1356 typedef bool (*fmt_chk_fn)(struct perf_hpp_fmt *fmt);
1358 static bool check_thread_entry(struct perf_hpp_fmt *fmt)
1360 return perf_hpp__is_thread_entry(fmt) || perf_hpp__is_comm_entry(fmt);
1363 static void hist_entry__check_and_remove_filter(struct hist_entry *he,
1364 enum hist_filter type,
1367 struct perf_hpp_fmt *fmt;
1368 bool type_match = false;
1369 struct hist_entry *parent = he->parent_he;
1372 case HIST_FILTER__THREAD:
1373 if (symbol_conf.comm_list == NULL &&
1374 symbol_conf.pid_list == NULL &&
1375 symbol_conf.tid_list == NULL)
1378 case HIST_FILTER__DSO:
1379 if (symbol_conf.dso_list == NULL)
1382 case HIST_FILTER__SYMBOL:
1383 if (symbol_conf.sym_list == NULL)
1386 case HIST_FILTER__PARENT:
1387 case HIST_FILTER__GUEST:
1388 case HIST_FILTER__HOST:
1389 case HIST_FILTER__SOCKET:
1390 case HIST_FILTER__C2C:
1395 /* if it's filtered by own fmt, it has to have filter bits */
1396 perf_hpp_list__for_each_format(he->hpp_list, fmt) {
1405 * If the filter is for current level entry, propagate
1406 * filter marker to parents. The marker bit was
1407 * already set by default so it only needs to clear
1408 * non-filtered entries.
1410 if (!(he->filtered & (1 << type))) {
1412 parent->filtered &= ~(1 << type);
1413 parent = parent->parent_he;
1418 * If current entry doesn't have matching formats, set
1419 * filter marker for upper level entries. it will be
1420 * cleared if its lower level entries is not filtered.
1422 * For lower-level entries, it inherits parent's
1423 * filter bit so that lower level entries of a
1424 * non-filtered entry won't set the filter marker.
1427 he->filtered |= (1 << type);
1429 he->filtered |= (parent->filtered & (1 << type));
1433 static void hist_entry__apply_hierarchy_filters(struct hist_entry *he)
1435 hist_entry__check_and_remove_filter(he, HIST_FILTER__THREAD,
1436 check_thread_entry);
1438 hist_entry__check_and_remove_filter(he, HIST_FILTER__DSO,
1439 perf_hpp__is_dso_entry);
1441 hist_entry__check_and_remove_filter(he, HIST_FILTER__SYMBOL,
1442 perf_hpp__is_sym_entry);
1444 hists__apply_filters(he->hists, he);
1447 static struct hist_entry *hierarchy_insert_entry(struct hists *hists,
1448 struct rb_root_cached *root,
1449 struct hist_entry *he,
1450 struct hist_entry *parent_he,
1451 struct perf_hpp_list *hpp_list)
1453 struct rb_node **p = &root->rb_root.rb_node;
1454 struct rb_node *parent = NULL;
1455 struct hist_entry *iter, *new;
1456 struct perf_hpp_fmt *fmt;
1458 bool leftmost = true;
1460 while (*p != NULL) {
1462 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1465 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1466 cmp = fmt->collapse(fmt, iter, he);
1472 he_stat__add_stat(&iter->stat, &he->stat);
1477 p = &parent->rb_left;
1479 p = &parent->rb_right;
1484 new = hist_entry__new(he, true);
1488 hists->nr_entries++;
1490 /* save related format list for output */
1491 new->hpp_list = hpp_list;
1492 new->parent_he = parent_he;
1494 hist_entry__apply_hierarchy_filters(new);
1496 /* some fields are now passed to 'new' */
1497 perf_hpp_list__for_each_sort_list(hpp_list, fmt) {
1498 if (perf_hpp__is_trace_entry(fmt) || perf_hpp__is_dynamic_entry(fmt))
1499 he->trace_output = NULL;
1501 new->trace_output = NULL;
1503 if (perf_hpp__is_srcline_entry(fmt))
1506 new->srcline = NULL;
1508 if (perf_hpp__is_srcfile_entry(fmt))
1511 new->srcfile = NULL;
1514 rb_link_node(&new->rb_node_in, parent, p);
1515 rb_insert_color_cached(&new->rb_node_in, root, leftmost);
1519 static int hists__hierarchy_insert_entry(struct hists *hists,
1520 struct rb_root_cached *root,
1521 struct hist_entry *he)
1523 struct perf_hpp_list_node *node;
1524 struct hist_entry *new_he = NULL;
1525 struct hist_entry *parent = NULL;
1529 list_for_each_entry(node, &hists->hpp_formats, list) {
1530 /* skip period (overhead) and elided columns */
1531 if (node->level == 0 || node->skip)
1534 /* insert copy of 'he' for each fmt into the hierarchy */
1535 new_he = hierarchy_insert_entry(hists, root, he, parent, &node->hpp);
1536 if (new_he == NULL) {
1541 root = &new_he->hroot_in;
1542 new_he->depth = depth++;
1547 new_he->leaf = true;
1549 if (hist_entry__has_callchains(new_he) &&
1550 symbol_conf.use_callchain) {
1551 callchain_cursor_reset(&callchain_cursor);
1552 if (callchain_merge(&callchain_cursor,
1559 /* 'he' is no longer used */
1560 hist_entry__delete(he);
1562 /* return 0 (or -1) since it already applied filters */
1566 static int hists__collapse_insert_entry(struct hists *hists,
1567 struct rb_root_cached *root,
1568 struct hist_entry *he)
1570 struct rb_node **p = &root->rb_root.rb_node;
1571 struct rb_node *parent = NULL;
1572 struct hist_entry *iter;
1574 bool leftmost = true;
1576 if (symbol_conf.report_hierarchy)
1577 return hists__hierarchy_insert_entry(hists, root, he);
1579 while (*p != NULL) {
1581 iter = rb_entry(parent, struct hist_entry, rb_node_in);
1583 cmp = hist_entry__collapse(iter, he);
1588 he_stat__add_stat(&iter->stat, &he->stat);
1589 if (symbol_conf.cumulate_callchain)
1590 he_stat__add_stat(iter->stat_acc, he->stat_acc);
1592 if (hist_entry__has_callchains(he) && symbol_conf.use_callchain) {
1593 callchain_cursor_reset(&callchain_cursor);
1594 if (callchain_merge(&callchain_cursor,
1599 hist_entry__delete(he);
1606 p = &(*p)->rb_right;
1610 hists->nr_entries++;
1612 rb_link_node(&he->rb_node_in, parent, p);
1613 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
1617 struct rb_root_cached *hists__get_rotate_entries_in(struct hists *hists)
1619 struct rb_root_cached *root;
1621 pthread_mutex_lock(&hists->lock);
1623 root = hists->entries_in;
1624 if (++hists->entries_in > &hists->entries_in_array[1])
1625 hists->entries_in = &hists->entries_in_array[0];
1627 pthread_mutex_unlock(&hists->lock);
1632 static void hists__apply_filters(struct hists *hists, struct hist_entry *he)
1634 hists__filter_entry_by_dso(hists, he);
1635 hists__filter_entry_by_thread(hists, he);
1636 hists__filter_entry_by_symbol(hists, he);
1637 hists__filter_entry_by_socket(hists, he);
1640 int hists__collapse_resort(struct hists *hists, struct ui_progress *prog)
1642 struct rb_root_cached *root;
1643 struct rb_node *next;
1644 struct hist_entry *n;
1647 if (!hists__has(hists, need_collapse))
1650 hists->nr_entries = 0;
1652 root = hists__get_rotate_entries_in(hists);
1654 next = rb_first_cached(root);
1659 n = rb_entry(next, struct hist_entry, rb_node_in);
1660 next = rb_next(&n->rb_node_in);
1662 rb_erase_cached(&n->rb_node_in, root);
1663 ret = hists__collapse_insert_entry(hists, &hists->entries_collapsed, n);
1669 * If it wasn't combined with one of the entries already
1670 * collapsed, we need to apply the filters that may have
1671 * been set by, say, the hist_browser.
1673 hists__apply_filters(hists, n);
1676 ui_progress__update(prog, 1);
1681 static int64_t hist_entry__sort(struct hist_entry *a, struct hist_entry *b)
1683 struct hists *hists = a->hists;
1684 struct perf_hpp_fmt *fmt;
1687 hists__for_each_sort_list(hists, fmt) {
1688 if (perf_hpp__should_skip(fmt, a->hists))
1691 cmp = fmt->sort(fmt, a, b);
1699 static void hists__reset_filter_stats(struct hists *hists)
1701 hists->nr_non_filtered_entries = 0;
1702 hists->stats.total_non_filtered_period = 0;
1705 void hists__reset_stats(struct hists *hists)
1707 hists->nr_entries = 0;
1708 hists->stats.total_period = 0;
1710 hists__reset_filter_stats(hists);
1713 static void hists__inc_filter_stats(struct hists *hists, struct hist_entry *h)
1715 hists->nr_non_filtered_entries++;
1716 hists->stats.total_non_filtered_period += h->stat.period;
1719 void hists__inc_stats(struct hists *hists, struct hist_entry *h)
1722 hists__inc_filter_stats(hists, h);
1724 hists->nr_entries++;
1725 hists->stats.total_period += h->stat.period;
1728 static void hierarchy_recalc_total_periods(struct hists *hists)
1730 struct rb_node *node;
1731 struct hist_entry *he;
1733 node = rb_first_cached(&hists->entries);
1735 hists->stats.total_period = 0;
1736 hists->stats.total_non_filtered_period = 0;
1739 * recalculate total period using top-level entries only
1740 * since lower level entries only see non-filtered entries
1741 * but upper level entries have sum of both entries.
1744 he = rb_entry(node, struct hist_entry, rb_node);
1745 node = rb_next(node);
1747 hists->stats.total_period += he->stat.period;
1749 hists->stats.total_non_filtered_period += he->stat.period;
1753 static void hierarchy_insert_output_entry(struct rb_root_cached *root,
1754 struct hist_entry *he)
1756 struct rb_node **p = &root->rb_root.rb_node;
1757 struct rb_node *parent = NULL;
1758 struct hist_entry *iter;
1759 struct perf_hpp_fmt *fmt;
1760 bool leftmost = true;
1762 while (*p != NULL) {
1764 iter = rb_entry(parent, struct hist_entry, rb_node);
1766 if (hist_entry__sort(he, iter) > 0)
1767 p = &parent->rb_left;
1769 p = &parent->rb_right;
1774 rb_link_node(&he->rb_node, parent, p);
1775 rb_insert_color_cached(&he->rb_node, root, leftmost);
1777 /* update column width of dynamic entry */
1778 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
1779 if (perf_hpp__is_dynamic_entry(fmt))
1780 fmt->sort(fmt, he, NULL);
1784 static void hists__hierarchy_output_resort(struct hists *hists,
1785 struct ui_progress *prog,
1786 struct rb_root_cached *root_in,
1787 struct rb_root_cached *root_out,
1788 u64 min_callchain_hits,
1791 struct rb_node *node;
1792 struct hist_entry *he;
1794 *root_out = RB_ROOT_CACHED;
1795 node = rb_first_cached(root_in);
1798 he = rb_entry(node, struct hist_entry, rb_node_in);
1799 node = rb_next(node);
1801 hierarchy_insert_output_entry(root_out, he);
1804 ui_progress__update(prog, 1);
1806 hists->nr_entries++;
1807 if (!he->filtered) {
1808 hists->nr_non_filtered_entries++;
1809 hists__calc_col_len(hists, he);
1813 hists__hierarchy_output_resort(hists, prog,
1824 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1825 u64 total = he->stat.period;
1827 if (symbol_conf.cumulate_callchain)
1828 total = he->stat_acc->period;
1830 min_callchain_hits = total * (callchain_param.min_percent / 100);
1833 callchain_param.sort(&he->sorted_chain, he->callchain,
1834 min_callchain_hits, &callchain_param);
1838 static void __hists__insert_output_entry(struct rb_root_cached *entries,
1839 struct hist_entry *he,
1840 u64 min_callchain_hits,
1843 struct rb_node **p = &entries->rb_root.rb_node;
1844 struct rb_node *parent = NULL;
1845 struct hist_entry *iter;
1846 struct perf_hpp_fmt *fmt;
1847 bool leftmost = true;
1849 if (use_callchain) {
1850 if (callchain_param.mode == CHAIN_GRAPH_REL) {
1851 u64 total = he->stat.period;
1853 if (symbol_conf.cumulate_callchain)
1854 total = he->stat_acc->period;
1856 min_callchain_hits = total * (callchain_param.min_percent / 100);
1858 callchain_param.sort(&he->sorted_chain, he->callchain,
1859 min_callchain_hits, &callchain_param);
1862 while (*p != NULL) {
1864 iter = rb_entry(parent, struct hist_entry, rb_node);
1866 if (hist_entry__sort(he, iter) > 0)
1869 p = &(*p)->rb_right;
1874 rb_link_node(&he->rb_node, parent, p);
1875 rb_insert_color_cached(&he->rb_node, entries, leftmost);
1877 perf_hpp_list__for_each_sort_list(&perf_hpp_list, fmt) {
1878 if (perf_hpp__is_dynamic_entry(fmt) &&
1879 perf_hpp__defined_dynamic_entry(fmt, he->hists))
1880 fmt->sort(fmt, he, NULL); /* update column width */
1884 static void output_resort(struct hists *hists, struct ui_progress *prog,
1885 bool use_callchain, hists__resort_cb_t cb,
1888 struct rb_root_cached *root;
1889 struct rb_node *next;
1890 struct hist_entry *n;
1891 u64 callchain_total;
1892 u64 min_callchain_hits;
1894 callchain_total = hists->callchain_period;
1895 if (symbol_conf.filter_relative)
1896 callchain_total = hists->callchain_non_filtered_period;
1898 min_callchain_hits = callchain_total * (callchain_param.min_percent / 100);
1900 hists__reset_stats(hists);
1901 hists__reset_col_len(hists);
1903 if (symbol_conf.report_hierarchy) {
1904 hists__hierarchy_output_resort(hists, prog,
1905 &hists->entries_collapsed,
1909 hierarchy_recalc_total_periods(hists);
1913 if (hists__has(hists, need_collapse))
1914 root = &hists->entries_collapsed;
1916 root = hists->entries_in;
1918 next = rb_first_cached(root);
1919 hists->entries = RB_ROOT_CACHED;
1922 n = rb_entry(next, struct hist_entry, rb_node_in);
1923 next = rb_next(&n->rb_node_in);
1925 if (cb && cb(n, cb_arg))
1928 __hists__insert_output_entry(&hists->entries, n, min_callchain_hits, use_callchain);
1929 hists__inc_stats(hists, n);
1932 hists__calc_col_len(hists, n);
1935 ui_progress__update(prog, 1);
1939 void evsel__output_resort_cb(struct evsel *evsel, struct ui_progress *prog,
1940 hists__resort_cb_t cb, void *cb_arg)
1944 if (evsel && symbol_conf.use_callchain && !symbol_conf.show_ref_callgraph)
1945 use_callchain = evsel__has_callchain(evsel);
1947 use_callchain = symbol_conf.use_callchain;
1949 use_callchain |= symbol_conf.show_branchflag_count;
1951 output_resort(evsel__hists(evsel), prog, use_callchain, cb, cb_arg);
1954 void evsel__output_resort(struct evsel *evsel, struct ui_progress *prog)
1956 return evsel__output_resort_cb(evsel, prog, NULL, NULL);
1959 void hists__output_resort(struct hists *hists, struct ui_progress *prog)
1961 output_resort(hists, prog, symbol_conf.use_callchain, NULL, NULL);
1964 void hists__output_resort_cb(struct hists *hists, struct ui_progress *prog,
1965 hists__resort_cb_t cb)
1967 output_resort(hists, prog, symbol_conf.use_callchain, cb, NULL);
1970 static bool can_goto_child(struct hist_entry *he, enum hierarchy_move_dir hmd)
1972 if (he->leaf || hmd == HMD_FORCE_SIBLING)
1975 if (he->unfolded || hmd == HMD_FORCE_CHILD)
1981 struct rb_node *rb_hierarchy_last(struct rb_node *node)
1983 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1985 while (can_goto_child(he, HMD_NORMAL)) {
1986 node = rb_last(&he->hroot_out.rb_root);
1987 he = rb_entry(node, struct hist_entry, rb_node);
1992 struct rb_node *__rb_hierarchy_next(struct rb_node *node, enum hierarchy_move_dir hmd)
1994 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
1996 if (can_goto_child(he, hmd))
1997 node = rb_first_cached(&he->hroot_out);
1999 node = rb_next(node);
2001 while (node == NULL) {
2006 node = rb_next(&he->rb_node);
2011 struct rb_node *rb_hierarchy_prev(struct rb_node *node)
2013 struct hist_entry *he = rb_entry(node, struct hist_entry, rb_node);
2015 node = rb_prev(node);
2017 return rb_hierarchy_last(node);
2023 return &he->rb_node;
2026 bool hist_entry__has_hierarchy_children(struct hist_entry *he, float limit)
2028 struct rb_node *node;
2029 struct hist_entry *child;
2035 node = rb_first_cached(&he->hroot_out);
2036 child = rb_entry(node, struct hist_entry, rb_node);
2038 while (node && child->filtered) {
2039 node = rb_next(node);
2040 child = rb_entry(node, struct hist_entry, rb_node);
2044 percent = hist_entry__get_percent_limit(child);
2048 return node && percent >= limit;
2051 static void hists__remove_entry_filter(struct hists *hists, struct hist_entry *h,
2052 enum hist_filter filter)
2054 h->filtered &= ~(1 << filter);
2056 if (symbol_conf.report_hierarchy) {
2057 struct hist_entry *parent = h->parent_he;
2060 he_stat__add_stat(&parent->stat, &h->stat);
2062 parent->filtered &= ~(1 << filter);
2064 if (parent->filtered)
2067 /* force fold unfiltered entry for simplicity */
2068 parent->unfolded = false;
2069 parent->has_no_entry = false;
2070 parent->row_offset = 0;
2071 parent->nr_rows = 0;
2073 parent = parent->parent_he;
2080 /* force fold unfiltered entry for simplicity */
2081 h->unfolded = false;
2082 h->has_no_entry = false;
2086 hists->stats.nr_non_filtered_samples += h->stat.nr_events;
2088 hists__inc_filter_stats(hists, h);
2089 hists__calc_col_len(hists, h);
2093 static bool hists__filter_entry_by_dso(struct hists *hists,
2094 struct hist_entry *he)
2096 if (hists->dso_filter != NULL &&
2097 (he->ms.map == NULL || he->ms.map->dso != hists->dso_filter)) {
2098 he->filtered |= (1 << HIST_FILTER__DSO);
2105 static bool hists__filter_entry_by_thread(struct hists *hists,
2106 struct hist_entry *he)
2108 if (hists->thread_filter != NULL &&
2109 he->thread != hists->thread_filter) {
2110 he->filtered |= (1 << HIST_FILTER__THREAD);
2117 static bool hists__filter_entry_by_symbol(struct hists *hists,
2118 struct hist_entry *he)
2120 if (hists->symbol_filter_str != NULL &&
2121 (!he->ms.sym || strstr(he->ms.sym->name,
2122 hists->symbol_filter_str) == NULL)) {
2123 he->filtered |= (1 << HIST_FILTER__SYMBOL);
2130 static bool hists__filter_entry_by_socket(struct hists *hists,
2131 struct hist_entry *he)
2133 if ((hists->socket_filter > -1) &&
2134 (he->socket != hists->socket_filter)) {
2135 he->filtered |= (1 << HIST_FILTER__SOCKET);
2142 typedef bool (*filter_fn_t)(struct hists *hists, struct hist_entry *he);
2144 static void hists__filter_by_type(struct hists *hists, int type, filter_fn_t filter)
2148 hists->stats.nr_non_filtered_samples = 0;
2150 hists__reset_filter_stats(hists);
2151 hists__reset_col_len(hists);
2153 for (nd = rb_first_cached(&hists->entries); nd; nd = rb_next(nd)) {
2154 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2156 if (filter(hists, h))
2159 hists__remove_entry_filter(hists, h, type);
2163 static void resort_filtered_entry(struct rb_root_cached *root,
2164 struct hist_entry *he)
2166 struct rb_node **p = &root->rb_root.rb_node;
2167 struct rb_node *parent = NULL;
2168 struct hist_entry *iter;
2169 struct rb_root_cached new_root = RB_ROOT_CACHED;
2171 bool leftmost = true;
2173 while (*p != NULL) {
2175 iter = rb_entry(parent, struct hist_entry, rb_node);
2177 if (hist_entry__sort(he, iter) > 0)
2180 p = &(*p)->rb_right;
2185 rb_link_node(&he->rb_node, parent, p);
2186 rb_insert_color_cached(&he->rb_node, root, leftmost);
2188 if (he->leaf || he->filtered)
2191 nd = rb_first_cached(&he->hroot_out);
2193 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2196 rb_erase_cached(&h->rb_node, &he->hroot_out);
2198 resort_filtered_entry(&new_root, h);
2201 he->hroot_out = new_root;
2204 static void hists__filter_hierarchy(struct hists *hists, int type, const void *arg)
2207 struct rb_root_cached new_root = RB_ROOT_CACHED;
2209 hists->stats.nr_non_filtered_samples = 0;
2211 hists__reset_filter_stats(hists);
2212 hists__reset_col_len(hists);
2214 nd = rb_first_cached(&hists->entries);
2216 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2219 ret = hist_entry__filter(h, type, arg);
2222 * case 1. non-matching type
2223 * zero out the period, set filter marker and move to child
2226 memset(&h->stat, 0, sizeof(h->stat));
2227 h->filtered |= (1 << type);
2229 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_CHILD);
2232 * case 2. matched type (filter out)
2233 * set filter marker and move to next
2235 else if (ret == 1) {
2236 h->filtered |= (1 << type);
2238 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2241 * case 3. ok (not filtered)
2242 * add period to hists and parents, erase the filter marker
2243 * and move to next sibling
2246 hists__remove_entry_filter(hists, h, type);
2248 nd = __rb_hierarchy_next(&h->rb_node, HMD_FORCE_SIBLING);
2252 hierarchy_recalc_total_periods(hists);
2255 * resort output after applying a new filter since filter in a lower
2256 * hierarchy can change periods in a upper hierarchy.
2258 nd = rb_first_cached(&hists->entries);
2260 struct hist_entry *h = rb_entry(nd, struct hist_entry, rb_node);
2263 rb_erase_cached(&h->rb_node, &hists->entries);
2265 resort_filtered_entry(&new_root, h);
2268 hists->entries = new_root;
2271 void hists__filter_by_thread(struct hists *hists)
2273 if (symbol_conf.report_hierarchy)
2274 hists__filter_hierarchy(hists, HIST_FILTER__THREAD,
2275 hists->thread_filter);
2277 hists__filter_by_type(hists, HIST_FILTER__THREAD,
2278 hists__filter_entry_by_thread);
2281 void hists__filter_by_dso(struct hists *hists)
2283 if (symbol_conf.report_hierarchy)
2284 hists__filter_hierarchy(hists, HIST_FILTER__DSO,
2287 hists__filter_by_type(hists, HIST_FILTER__DSO,
2288 hists__filter_entry_by_dso);
2291 void hists__filter_by_symbol(struct hists *hists)
2293 if (symbol_conf.report_hierarchy)
2294 hists__filter_hierarchy(hists, HIST_FILTER__SYMBOL,
2295 hists->symbol_filter_str);
2297 hists__filter_by_type(hists, HIST_FILTER__SYMBOL,
2298 hists__filter_entry_by_symbol);
2301 void hists__filter_by_socket(struct hists *hists)
2303 if (symbol_conf.report_hierarchy)
2304 hists__filter_hierarchy(hists, HIST_FILTER__SOCKET,
2305 &hists->socket_filter);
2307 hists__filter_by_type(hists, HIST_FILTER__SOCKET,
2308 hists__filter_entry_by_socket);
2311 void events_stats__inc(struct events_stats *stats, u32 type)
2313 ++stats->nr_events[0];
2314 ++stats->nr_events[type];
2317 static void hists_stats__inc(struct hists_stats *stats)
2319 ++stats->nr_samples;
2322 void hists__inc_nr_events(struct hists *hists)
2324 hists_stats__inc(&hists->stats);
2327 void hists__inc_nr_samples(struct hists *hists, bool filtered)
2329 hists_stats__inc(&hists->stats);
2331 hists->stats.nr_non_filtered_samples++;
2334 static struct hist_entry *hists__add_dummy_entry(struct hists *hists,
2335 struct hist_entry *pair)
2337 struct rb_root_cached *root;
2339 struct rb_node *parent = NULL;
2340 struct hist_entry *he;
2342 bool leftmost = true;
2344 if (hists__has(hists, need_collapse))
2345 root = &hists->entries_collapsed;
2347 root = hists->entries_in;
2349 p = &root->rb_root.rb_node;
2351 while (*p != NULL) {
2353 he = rb_entry(parent, struct hist_entry, rb_node_in);
2355 cmp = hist_entry__collapse(he, pair);
2363 p = &(*p)->rb_right;
2368 he = hist_entry__new(pair, true);
2370 memset(&he->stat, 0, sizeof(he->stat));
2372 if (symbol_conf.cumulate_callchain)
2373 memset(he->stat_acc, 0, sizeof(he->stat));
2374 rb_link_node(&he->rb_node_in, parent, p);
2375 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
2376 hists__inc_stats(hists, he);
2383 static struct hist_entry *add_dummy_hierarchy_entry(struct hists *hists,
2384 struct rb_root_cached *root,
2385 struct hist_entry *pair)
2388 struct rb_node *parent = NULL;
2389 struct hist_entry *he;
2390 struct perf_hpp_fmt *fmt;
2391 bool leftmost = true;
2393 p = &root->rb_root.rb_node;
2394 while (*p != NULL) {
2398 he = rb_entry(parent, struct hist_entry, rb_node_in);
2400 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2401 cmp = fmt->collapse(fmt, he, pair);
2409 p = &parent->rb_left;
2411 p = &parent->rb_right;
2416 he = hist_entry__new(pair, true);
2418 rb_link_node(&he->rb_node_in, parent, p);
2419 rb_insert_color_cached(&he->rb_node_in, root, leftmost);
2423 memset(&he->stat, 0, sizeof(he->stat));
2424 hists__inc_stats(hists, he);
2430 static struct hist_entry *hists__find_entry(struct hists *hists,
2431 struct hist_entry *he)
2435 if (hists__has(hists, need_collapse))
2436 n = hists->entries_collapsed.rb_root.rb_node;
2438 n = hists->entries_in->rb_root.rb_node;
2441 struct hist_entry *iter = rb_entry(n, struct hist_entry, rb_node_in);
2442 int64_t cmp = hist_entry__collapse(iter, he);
2455 static struct hist_entry *hists__find_hierarchy_entry(struct rb_root_cached *root,
2456 struct hist_entry *he)
2458 struct rb_node *n = root->rb_root.rb_node;
2461 struct hist_entry *iter;
2462 struct perf_hpp_fmt *fmt;
2465 iter = rb_entry(n, struct hist_entry, rb_node_in);
2466 perf_hpp_list__for_each_sort_list(he->hpp_list, fmt) {
2467 cmp = fmt->collapse(fmt, iter, he);
2483 static void hists__match_hierarchy(struct rb_root_cached *leader_root,
2484 struct rb_root_cached *other_root)
2487 struct hist_entry *pos, *pair;
2489 for (nd = rb_first_cached(leader_root); nd; nd = rb_next(nd)) {
2490 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2491 pair = hists__find_hierarchy_entry(other_root, pos);
2494 hist_entry__add_pair(pair, pos);
2495 hists__match_hierarchy(&pos->hroot_in, &pair->hroot_in);
2501 * Look for pairs to link to the leader buckets (hist_entries):
2503 void hists__match(struct hists *leader, struct hists *other)
2505 struct rb_root_cached *root;
2507 struct hist_entry *pos, *pair;
2509 if (symbol_conf.report_hierarchy) {
2510 /* hierarchy report always collapses entries */
2511 return hists__match_hierarchy(&leader->entries_collapsed,
2512 &other->entries_collapsed);
2515 if (hists__has(leader, need_collapse))
2516 root = &leader->entries_collapsed;
2518 root = leader->entries_in;
2520 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2521 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2522 pair = hists__find_entry(other, pos);
2525 hist_entry__add_pair(pair, pos);
2529 static int hists__link_hierarchy(struct hists *leader_hists,
2530 struct hist_entry *parent,
2531 struct rb_root_cached *leader_root,
2532 struct rb_root_cached *other_root)
2535 struct hist_entry *pos, *leader;
2537 for (nd = rb_first_cached(other_root); nd; nd = rb_next(nd)) {
2538 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2540 if (hist_entry__has_pairs(pos)) {
2543 list_for_each_entry(leader, &pos->pairs.head, pairs.node) {
2544 if (leader->hists == leader_hists) {
2552 leader = add_dummy_hierarchy_entry(leader_hists,
2557 /* do not point parent in the pos */
2558 leader->parent_he = parent;
2560 hist_entry__add_pair(pos, leader);
2564 if (hists__link_hierarchy(leader_hists, leader,
2566 &pos->hroot_in) < 0)
2574 * Look for entries in the other hists that are not present in the leader, if
2575 * we find them, just add a dummy entry on the leader hists, with period=0,
2576 * nr_events=0, to serve as the list header.
2578 int hists__link(struct hists *leader, struct hists *other)
2580 struct rb_root_cached *root;
2582 struct hist_entry *pos, *pair;
2584 if (symbol_conf.report_hierarchy) {
2585 /* hierarchy report always collapses entries */
2586 return hists__link_hierarchy(leader, NULL,
2587 &leader->entries_collapsed,
2588 &other->entries_collapsed);
2591 if (hists__has(other, need_collapse))
2592 root = &other->entries_collapsed;
2594 root = other->entries_in;
2596 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2597 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2599 if (!hist_entry__has_pairs(pos)) {
2600 pair = hists__add_dummy_entry(leader, pos);
2603 hist_entry__add_pair(pos, pair);
2610 int hists__unlink(struct hists *hists)
2612 struct rb_root_cached *root;
2614 struct hist_entry *pos;
2616 if (hists__has(hists, need_collapse))
2617 root = &hists->entries_collapsed;
2619 root = hists->entries_in;
2621 for (nd = rb_first_cached(root); nd; nd = rb_next(nd)) {
2622 pos = rb_entry(nd, struct hist_entry, rb_node_in);
2623 list_del_init(&pos->pairs.node);
2629 void hist__account_cycles(struct branch_stack *bs, struct addr_location *al,
2630 struct perf_sample *sample, bool nonany_branch_mode,
2633 struct branch_info *bi;
2634 struct branch_entry *entries = perf_sample__branch_entries(sample);
2636 /* If we have branch cycles always annotate them. */
2637 if (bs && bs->nr && entries[0].flags.cycles) {
2640 bi = sample__resolve_bstack(sample, al);
2642 struct addr_map_symbol *prev = NULL;
2645 * Ignore errors, still want to process the
2648 * For non standard branch modes always
2649 * force no IPC (prev == NULL)
2651 * Note that perf stores branches reversed from
2654 for (i = bs->nr - 1; i >= 0; i--) {
2655 addr_map_symbol__account_cycles(&bi[i].from,
2656 nonany_branch_mode ? NULL : prev,
2657 bi[i].flags.cycles);
2661 *total_cycles += bi[i].flags.cycles;
2668 size_t evlist__fprintf_nr_events(struct evlist *evlist, FILE *fp,
2674 evlist__for_each_entry(evlist, pos) {
2675 struct hists *hists = evsel__hists(pos);
2677 if (skip_empty && !hists->stats.nr_samples)
2680 ret += fprintf(fp, "%s stats:\n", evsel__name(pos));
2681 ret += fprintf(fp, "%16s events: %10d\n",
2682 "SAMPLE", hists->stats.nr_samples);
2689 u64 hists__total_period(struct hists *hists)
2691 return symbol_conf.filter_relative ? hists->stats.total_non_filtered_period :
2692 hists->stats.total_period;
2695 int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool show_freq)
2699 const struct dso *dso = hists->dso_filter;
2700 struct thread *thread = hists->thread_filter;
2701 int socket_id = hists->socket_filter;
2702 unsigned long nr_samples = hists->stats.nr_samples;
2703 u64 nr_events = hists->stats.total_period;
2704 struct evsel *evsel = hists_to_evsel(hists);
2705 const char *ev_name = evsel__name(evsel);
2706 char buf[512], sample_freq_str[64] = "";
2707 size_t buflen = sizeof(buf);
2708 char ref[30] = " show reference callgraph, ";
2709 bool enable_ref = false;
2711 if (symbol_conf.filter_relative) {
2712 nr_samples = hists->stats.nr_non_filtered_samples;
2713 nr_events = hists->stats.total_non_filtered_period;
2716 if (evsel__is_group_event(evsel)) {
2719 evsel__group_desc(evsel, buf, buflen);
2722 for_each_group_member(pos, evsel) {
2723 struct hists *pos_hists = evsel__hists(pos);
2725 if (symbol_conf.filter_relative) {
2726 nr_samples += pos_hists->stats.nr_non_filtered_samples;
2727 nr_events += pos_hists->stats.total_non_filtered_period;
2729 nr_samples += pos_hists->stats.nr_samples;
2730 nr_events += pos_hists->stats.total_period;
2735 if (symbol_conf.show_ref_callgraph &&
2736 strstr(ev_name, "call-graph=no"))
2740 scnprintf(sample_freq_str, sizeof(sample_freq_str), " %d Hz,", evsel->core.attr.sample_freq);
2742 nr_samples = convert_unit(nr_samples, &unit);
2743 printed = scnprintf(bf, size,
2744 "Samples: %lu%c of event%s '%s',%s%sEvent count (approx.): %" PRIu64,
2745 nr_samples, unit, evsel->core.nr_members > 1 ? "s" : "",
2746 ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events);
2749 if (hists->uid_filter_str)
2750 printed += snprintf(bf + printed, size - printed,
2751 ", UID: %s", hists->uid_filter_str);
2753 if (hists__has(hists, thread)) {
2754 printed += scnprintf(bf + printed, size - printed,
2756 (thread->comm_set ? thread__comm_str(thread) : ""),
2759 printed += scnprintf(bf + printed, size - printed,
2761 (thread->comm_set ? thread__comm_str(thread) : ""));
2765 printed += scnprintf(bf + printed, size - printed,
2766 ", DSO: %s", dso->short_name);
2768 printed += scnprintf(bf + printed, size - printed,
2769 ", Processor Socket: %d", socket_id);
2774 int parse_filter_percentage(const struct option *opt __maybe_unused,
2775 const char *arg, int unset __maybe_unused)
2777 if (!strcmp(arg, "relative"))
2778 symbol_conf.filter_relative = true;
2779 else if (!strcmp(arg, "absolute"))
2780 symbol_conf.filter_relative = false;
2782 pr_debug("Invalid percentage: %s\n", arg);
2789 int perf_hist_config(const char *var, const char *value)
2791 if (!strcmp(var, "hist.percentage"))
2792 return parse_filter_percentage(NULL, value, 0);
2797 int __hists__init(struct hists *hists, struct perf_hpp_list *hpp_list)
2799 memset(hists, 0, sizeof(*hists));
2800 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT_CACHED;
2801 hists->entries_in = &hists->entries_in_array[0];
2802 hists->entries_collapsed = RB_ROOT_CACHED;
2803 hists->entries = RB_ROOT_CACHED;
2804 pthread_mutex_init(&hists->lock, NULL);
2805 hists->socket_filter = -1;
2806 hists->hpp_list = hpp_list;
2807 INIT_LIST_HEAD(&hists->hpp_formats);
2811 static void hists__delete_remaining_entries(struct rb_root_cached *root)
2813 struct rb_node *node;
2814 struct hist_entry *he;
2816 while (!RB_EMPTY_ROOT(&root->rb_root)) {
2817 node = rb_first_cached(root);
2818 rb_erase_cached(node, root);
2820 he = rb_entry(node, struct hist_entry, rb_node_in);
2821 hist_entry__delete(he);
2825 static void hists__delete_all_entries(struct hists *hists)
2827 hists__delete_entries(hists);
2828 hists__delete_remaining_entries(&hists->entries_in_array[0]);
2829 hists__delete_remaining_entries(&hists->entries_in_array[1]);
2830 hists__delete_remaining_entries(&hists->entries_collapsed);
2833 static void hists_evsel__exit(struct evsel *evsel)
2835 struct hists *hists = evsel__hists(evsel);
2836 struct perf_hpp_fmt *fmt, *pos;
2837 struct perf_hpp_list_node *node, *tmp;
2839 hists__delete_all_entries(hists);
2841 list_for_each_entry_safe(node, tmp, &hists->hpp_formats, list) {
2842 perf_hpp_list__for_each_format_safe(&node->hpp, fmt, pos) {
2843 list_del_init(&fmt->list);
2846 list_del_init(&node->list);
2851 static int hists_evsel__init(struct evsel *evsel)
2853 struct hists *hists = evsel__hists(evsel);
2855 __hists__init(hists, &perf_hpp_list);
2860 * XXX We probably need a hists_evsel__exit() to free the hist_entries
2861 * stored in the rbtree...
2864 int hists__init(void)
2866 int err = evsel__object_config(sizeof(struct hists_evsel),
2867 hists_evsel__init, hists_evsel__exit);
2869 fputs("FATAL ERROR: Couldn't setup hists class\n", stderr);
2874 void perf_hpp_list__init(struct perf_hpp_list *list)
2876 INIT_LIST_HEAD(&list->fields);
2877 INIT_LIST_HEAD(&list->sorts);